Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 5 | * Copyright (C) 2011 secunet Security Networks AG |
| 6 | * (Written by Patrick Georgi <patrick.georgi@secunet.com>) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 18 | #ifdef __MINGW32__ |
| 19 | #include <winsock.h> |
| 20 | #else |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 21 | #include <arpa/inet.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 22 | #endif |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 25 | #ifndef __MINGW32__ |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 26 | #include <sys/mman.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 27 | #endif |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | #include "cbfs.h" |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 33 | #include "common.h" |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 34 | |
| 35 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
| 36 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
| 37 | |
| 38 | static void *cbfs_mapped; |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 39 | static void *cbfs_offset; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 40 | static void* virt_to_phys(u32 virt) |
| 41 | { |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 42 | return cbfs_offset + virt; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #ifdef DEBUG |
| 46 | #define debug(x...) printf(x) |
| 47 | #else |
| 48 | #define debug(x...) while(0) {} |
| 49 | #endif |
| 50 | |
| 51 | static int cbfs_check_magic(struct cbfs_file *file) |
| 52 | { |
| 53 | return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0; |
| 54 | } |
| 55 | |
| 56 | static struct cbfs_header *cbfs_master_header(void) |
| 57 | { |
| 58 | struct cbfs_header *header; |
| 59 | |
| 60 | void *ptr = virt_to_phys(*((u32*)virt_to_phys(CBFS_HEADPTR_ADDR))); |
| 61 | debug("Check CBFS header at %p\n", ptr); |
| 62 | header = (struct cbfs_header *) ptr; |
| 63 | |
| 64 | debug("magic is %08x\n", ntohl(header->magic)); |
| 65 | if (ntohl(header->magic) != CBFS_HEADER_MAGIC) { |
| 66 | printf("ERROR: No valid CBFS header found!\n"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | debug("Found CBFS header at %p\n", ptr); |
| 71 | return header; |
| 72 | } |
| 73 | |
| 74 | struct cbfs_file *cbfs_find(const char *name) |
| 75 | { |
| 76 | struct cbfs_header *header = cbfs_master_header(); |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 77 | void *offset; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 78 | |
| 79 | if (header == NULL) |
| 80 | return NULL; |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 81 | offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset)); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 82 | |
| 83 | int align= ntohl(header->align); |
| 84 | |
| 85 | while(1) { |
| 86 | struct cbfs_file *file = (struct cbfs_file *) offset; |
| 87 | if (!cbfs_check_magic(file)) return NULL; |
| 88 | debug("Check %s\n", CBFS_NAME(file)); |
| 89 | if (!strcmp(CBFS_NAME(file), name)) |
| 90 | return file; |
| 91 | |
| 92 | int flen = ntohl(file->len); |
| 93 | int foffset = ntohl(file->offset); |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 94 | debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 95 | |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 96 | void *oldoffset = offset; |
| 97 | offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 98 | debug("%p\n", (void *)offset); |
| 99 | if (offset <= oldoffset) return NULL; |
| 100 | |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 101 | if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize))) |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len) |
| 107 | { |
| 108 | struct cbfs_file *file = cbfs_find(name); |
| 109 | |
| 110 | if (file == NULL) { |
| 111 | printf("CBFS: Could not find file %s\n", |
| 112 | name); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | if (ntohl(file->type) != type) { |
| 117 | printf("CBFS: File %s is of type %x instead of" |
| 118 | "type %x\n", name, file->type, type); |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | if (len != NULL) *len = file->len; |
| 123 | |
| 124 | return (void *) CBFS_SUBHEADER(file); |
| 125 | } |
| 126 | |
| 127 | void open_cbfs(const char *filename) |
| 128 | { |
| 129 | struct stat cbfs_stat; |
| 130 | int cbfs_fd; |
| 131 | |
| 132 | cbfs_fd = open(filename, O_RDWR); |
| 133 | if (cbfs_fd == -1) { |
| 134 | printf("Couldn't open '%s'\n", filename); |
| 135 | exit(-1); |
| 136 | } |
| 137 | if (fstat(cbfs_fd, &cbfs_stat) == -1) { |
| 138 | printf("Couldn't stat '%s'\n", filename); |
| 139 | exit(-1); |
| 140 | } |
| 141 | cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE, |
| 142 | MAP_SHARED, cbfs_fd, 0); |
Patrick Georgi | edb0a61 | 2014-08-03 12:10:53 +0200 | [diff] [blame] | 143 | close(cbfs_fd); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 144 | if (cbfs_mapped == MAP_FAILED) { |
| 145 | printf("Couldn't map '%s'\n", filename); |
| 146 | exit(-1); |
| 147 | } |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 148 | cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 149 | } |