Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 2 | |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 3 | #ifdef __MINGW32__ |
| 4 | #include <winsock.h> |
| 5 | #else |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 6 | #include <arpa/inet.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 7 | #endif |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 10 | #ifndef __MINGW32__ |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 11 | #include <sys/mman.h> |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 12 | #endif |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <string.h> |
| 16 | #include <stdio.h> |
| 17 | #include "cbfs.h" |
Zheng Bao | 5451672 | 2012-10-22 16:41:42 +0800 | [diff] [blame] | 18 | #include "common.h" |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 19 | |
| 20 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
| 21 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
| 22 | |
| 23 | static void *cbfs_mapped; |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 24 | static void *cbfs_offset; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 25 | static void* virt_to_phys(u32 virt) |
| 26 | { |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 27 | return cbfs_offset + virt; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | #ifdef DEBUG |
| 31 | #define debug(x...) printf(x) |
| 32 | #else |
| 33 | #define debug(x...) while(0) {} |
| 34 | #endif |
| 35 | |
| 36 | static int cbfs_check_magic(struct cbfs_file *file) |
| 37 | { |
| 38 | return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0; |
| 39 | } |
| 40 | |
| 41 | static struct cbfs_header *cbfs_master_header(void) |
| 42 | { |
| 43 | struct cbfs_header *header; |
| 44 | |
| 45 | void *ptr = virt_to_phys(*((u32*)virt_to_phys(CBFS_HEADPTR_ADDR))); |
| 46 | debug("Check CBFS header at %p\n", ptr); |
| 47 | header = (struct cbfs_header *) ptr; |
| 48 | |
| 49 | debug("magic is %08x\n", ntohl(header->magic)); |
| 50 | if (ntohl(header->magic) != CBFS_HEADER_MAGIC) { |
| 51 | printf("ERROR: No valid CBFS header found!\n"); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | debug("Found CBFS header at %p\n", ptr); |
| 56 | return header; |
| 57 | } |
| 58 | |
| 59 | struct cbfs_file *cbfs_find(const char *name) |
| 60 | { |
| 61 | struct cbfs_header *header = cbfs_master_header(); |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 62 | void *offset; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 63 | |
| 64 | if (header == NULL) |
| 65 | return NULL; |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 66 | offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset)); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 67 | |
| 68 | int align= ntohl(header->align); |
| 69 | |
| 70 | while(1) { |
| 71 | struct cbfs_file *file = (struct cbfs_file *) offset; |
| 72 | if (!cbfs_check_magic(file)) return NULL; |
| 73 | debug("Check %s\n", CBFS_NAME(file)); |
| 74 | if (!strcmp(CBFS_NAME(file), name)) |
| 75 | return file; |
| 76 | |
| 77 | int flen = ntohl(file->len); |
| 78 | int foffset = ntohl(file->offset); |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 79 | debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 80 | |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 81 | void *oldoffset = offset; |
| 82 | offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 83 | debug("%p\n", (void *)offset); |
| 84 | if (offset <= oldoffset) return NULL; |
| 85 | |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 86 | if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize))) |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len) |
| 92 | { |
| 93 | struct cbfs_file *file = cbfs_find(name); |
| 94 | |
| 95 | if (file == NULL) { |
| 96 | printf("CBFS: Could not find file %s\n", |
| 97 | name); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | if (ntohl(file->type) != type) { |
| 102 | printf("CBFS: File %s is of type %x instead of" |
| 103 | "type %x\n", name, file->type, type); |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | if (len != NULL) *len = file->len; |
| 108 | |
| 109 | return (void *) CBFS_SUBHEADER(file); |
| 110 | } |
| 111 | |
| 112 | void open_cbfs(const char *filename) |
| 113 | { |
| 114 | struct stat cbfs_stat; |
| 115 | int cbfs_fd; |
| 116 | |
| 117 | cbfs_fd = open(filename, O_RDWR); |
| 118 | if (cbfs_fd == -1) { |
| 119 | printf("Couldn't open '%s'\n", filename); |
| 120 | exit(-1); |
| 121 | } |
| 122 | if (fstat(cbfs_fd, &cbfs_stat) == -1) { |
| 123 | printf("Couldn't stat '%s'\n", filename); |
| 124 | exit(-1); |
| 125 | } |
| 126 | cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE, |
| 127 | MAP_SHARED, cbfs_fd, 0); |
Patrick Georgi | edb0a61 | 2014-08-03 12:10:53 +0200 | [diff] [blame] | 128 | close(cbfs_fd); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 129 | if (cbfs_mapped == MAP_FAILED) { |
| 130 | printf("Couldn't map '%s'\n", filename); |
| 131 | exit(-1); |
| 132 | } |
Patrick Georgi | 4a2daf6 | 2012-03-09 12:54:03 +0100 | [diff] [blame] | 133 | cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1); |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 134 | } |