blob: cbf95c47f4442e0fa642cb531e14e528e167fa80 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi269e9322011-01-21 07:24:08 +00002
Zheng Bao54516722012-10-22 16:41:42 +08003#ifdef __MINGW32__
4#include <winsock.h>
5#else
Patrick Georgi269e9322011-01-21 07:24:08 +00006#include <arpa/inet.h>
Zheng Bao54516722012-10-22 16:41:42 +08007#endif
Patrick Georgi269e9322011-01-21 07:24:08 +00008#include <sys/types.h>
9#include <sys/stat.h>
Zheng Bao54516722012-10-22 16:41:42 +080010#ifndef __MINGW32__
Patrick Georgi269e9322011-01-21 07:24:08 +000011#include <sys/mman.h>
Zheng Bao54516722012-10-22 16:41:42 +080012#endif
Patrick Georgi269e9322011-01-21 07:24:08 +000013#include <stdlib.h>
14#include <fcntl.h>
15#include <string.h>
16#include <stdio.h>
17#include "cbfs.h"
Zheng Bao54516722012-10-22 16:41:42 +080018#include "common.h"
Patrick Georgi269e9322011-01-21 07:24:08 +000019
20#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
21#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
22
23static void *cbfs_mapped;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010024static void *cbfs_offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000025static void* virt_to_phys(u32 virt)
26{
Patrick Georgi4a2daf62012-03-09 12:54:03 +010027 return cbfs_offset + virt;
Patrick Georgi269e9322011-01-21 07:24:08 +000028}
29
30#ifdef DEBUG
31#define debug(x...) printf(x)
32#else
33#define debug(x...) while(0) {}
34#endif
35
36static int cbfs_check_magic(struct cbfs_file *file)
37{
38 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
39}
40
41static 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
59struct cbfs_file *cbfs_find(const char *name)
60{
61 struct cbfs_header *header = cbfs_master_header();
Patrick Georgi4a2daf62012-03-09 12:54:03 +010062 void *offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000063
64 if (header == NULL)
65 return NULL;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010066 offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset));
Patrick Georgi269e9322011-01-21 07:24:08 +000067
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 Georgi4a2daf62012-03-09 12:54:03 +010079 debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen);
Patrick Georgi269e9322011-01-21 07:24:08 +000080
Patrick Georgi4a2daf62012-03-09 12:54:03 +010081 void *oldoffset = offset;
82 offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align);
Patrick Georgi269e9322011-01-21 07:24:08 +000083 debug("%p\n", (void *)offset);
84 if (offset <= oldoffset) return NULL;
85
Patrick Georgi4a2daf62012-03-09 12:54:03 +010086 if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize)))
Patrick Georgi269e9322011-01-21 07:24:08 +000087 return NULL;
88 }
89}
90
91void *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
112void 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 Georgiedb0a612014-08-03 12:10:53 +0200128 close(cbfs_fd);
Patrick Georgi269e9322011-01-21 07:24:08 +0000129 if (cbfs_mapped == MAP_FAILED) {
130 printf("Couldn't map '%s'\n", filename);
131 exit(-1);
132 }
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100133 cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1);
Patrick Georgi269e9322011-01-21 07:24:08 +0000134}