| 1 |
|
| 2 |
|
| 3 |
#ifdef __MINGW32__ |
| 4 |
#include <winsock.h> |
| 5 |
#else |
| 6 |
#include <arpa/inet.h> |
| 7 |
#endif |
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#ifndef __MINGW32__ |
| 11 |
#include <sys/mman.h> |
| 12 |
#endif |
| 13 |
#include <stdlib.h> |
| 14 |
#include <fcntl.h> |
| 15 |
#include <string.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include "cbfs.h" |
| 18 |
#include "common.h" |
| 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; |
| 24 |
static void *cbfs_offset; |
| 25 |
static void* virt_to_phys(u32 virt) |
| 26 |
{ |
| 27 |
return cbfs_offset + virt; |
| 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(); |
| 62 |
void *offset; |
| 63 |
|
| 64 |
if (header == NULL) |
| 65 |
return NULL; |
| 66 |
offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset)); |
| 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); |
| 79 |
debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen); |
| 80 |
|
| 81 |
void *oldoffset = offset; |
| 82 |
offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align); |
| 83 |
debug("%p\n", (void *)offset); |
| 84 |
if (offset <= oldoffset) return NULL; |
| 85 |
|
| 86 |
if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize))) |
| 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); |
| 128 |
close(cbfs_fd); |
| 129 |
if (cbfs_mapped == MAP_FAILED) { |
| 130 |
printf("Couldn't map '%s'\n", filename); |
| 131 |
exit(-1); |
| 132 |
} |
| 133 |
cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1); |
| 134 |
} |