@@ -29,10 +29,10 @@ index 27e28b5..f02daee 100644
2929 $(CC) $(LDFLAGS) -o $@ $^
3030diff --git ./cbfs.c ./cbfs.c
3131new file mode 100644
32- index 0000000..bab5f76
32+ index 0000000..c0ddf48
3333--- /dev/null
3434+++ ./cbfs.c
35- @@ -0,0 +1,271 @@
35+ @@ -0,0 +1,262 @@
3636+ #include <stdio.h>
3737+ #include <fcntl.h>
3838+ #include <stdlib.h>
@@ -56,7 +56,7 @@ index 0000000..bab5f76
5656+ { "verbose", 0, NULL, 'v' },
5757+ { "read", 1, NULL, 'r' },
5858+ { "list", 0, NULL, 'l' },
59- + { "type", 0 , NULL, 't' },
59+ + { "type", 1 , NULL, 't' },
6060+ { "help", 0, NULL, 'h' },
6161+ { NULL, 0, NULL, 0 },
6262+ };
@@ -140,6 +140,11 @@ index 0000000..bab5f76
140140+ }
141141+ }
142142+
143+ + if (!do_list && !do_read) {
144+ + fprintf(stderr, "%s", usage);
145+ + return EXIT_FAILURE;
146+ + }
147+ +
143148+ argc -= optind;
144149+ argv += optind;
145150+ if (argc != 0)
@@ -148,14 +153,8 @@ index 0000000..bab5f76
148153+ return EXIT_FAILURE;
149154+ }
150155+
151- + int fd;
152- + fd = open("/dev/mem", O_RDONLY);
153- + if (fd < 0) {
154- + fprintf(stderr, "Failed to open /dev/mem : %s\n", strerror(errno));
155- + return EXIT_FAILURE;
156- + }
157- +
158156+ uint64_t end = 0x100000000;
157+ +
159158+ if (verbose) {
160159+ fprintf(stderr, "Seeking to %lx\n", end-4);
161160+ }
@@ -166,18 +165,13 @@ index 0000000..bab5f76
166165+ fprintf(stderr, "Header Offset: %d\n", header_delta);
167166+ }
168167+
169- + uint64_t header_off;
170- + if (header_delta < 0) {
171- + header_off = end - ((uint64_t)(-header_delta));
172- + } else {
173- + header_off = ((uint64_t)header_delta);
174- + }
168+ + uint64_t header_off = end + header_delta;
175169+
176170+ if (verbose) {
177171+ fprintf(stderr, "Seeking to %lx\n", header_off);
178172+ }
179173+ struct cbfs_header header;
180- + copy_physical(header_off, sizeof(struct cbfs_header ), &header);
174+ + copy_physical(header_off, sizeof(header ), &header);
181175+
182176+ header.magic = ntohl(header.magic);
183177+ header.version = ntohl(header.version);
@@ -203,15 +197,17 @@ index 0000000..bab5f76
203197+ }
204198+
205199+ uint32_t align = header.align;
200+ +
206201+ // loop through files
207202+ uint64_t off = end - ((uint64_t) header.romsize) +
208203+ ((uint64_t) header.offset);
204+ + void *rom = map_physical(off, end - off);
209205+ while (off < end) {
210206+ if (verbose) {
211207+ fprintf(stderr, "Potential CBFS File Offset: %lx\n", off);
212208+ }
213209+ struct cbfs_file file;
214- + copy_physical(off, sizeof(struct cbfs_file), & file);
210+ + memcpy(&file, rom, sizeof( file) );
215211+
216212+ file.len = ntohl(file.len);
217213+ file.type = ntohl(file.type);
@@ -230,9 +226,8 @@ index 0000000..bab5f76
230226+ break;
231227+ }
232228+
233- + size_t name_size = file.offset - sizeof(struct cbfs_file);
234- + char *name = calloc(name_size, sizeof(char));
235- + copy_physical(off+sizeof(struct cbfs_file), name_size, name);
229+ + size_t name_size = file.offset - sizeof(file);
230+ + char *name = (char *)rom + sizeof(file);
236231+
237232+ if (verbose) {
238233+ fprintf(stderr, "File name : '%s'\n", name);
@@ -252,14 +247,12 @@ index 0000000..bab5f76
252247+ fprintf(stderr, "Seeking to %lx\n-------- Start Data\n", file_off);
253248+ }
254249+
255- + char *file_data = malloc(file.len);
256- + if (file_data == NULL) {
257- + fprintf(stderr, "Failed to allocate memory for file data: length=%d\n",
258- + file.len);
250+ + if (file_off + file.len > end) {
251+ + fprintf(stderr, "File offset/length extends beyond ROM");
259252+ return EXIT_FAILURE;
260253+ }
261- + copy_physical(file_off, file.len, file_data);
262254+
255+ + char *file_data = (char *) rom + file.offset;
263256+ for (size_t offset = 0 ; offset < file.len ; ) {
264257+ const ssize_t rc = write(
265258+ STDOUT_FILENO,
@@ -281,19 +274,17 @@ index 0000000..bab5f76
281274+ }
282275+
283276+ do_read++;
284- + free(file_data);
285- + free(name);
286277+ break;
287278+ }
288279+
289- + free(name);
290280+ uint64_t inc = (align + (file.offset + file.len) - 1) & (~(align-1));
291281+ off += inc;
282+ + rom += inc;
292283+ if (verbose) {
293284+ fprintf(stderr, "File Off+Len : %x\n", file.offset + file.len);
294285+ fprintf(stderr, "Align : %x\n", align);
295286+ fprintf(stderr, "Inc : %lx\n", inc);
296- + fprintf(stderr, "Next file align : %lx\n", off);
287+ + fprintf(stderr, "Next file off : %lx\n", off);
297288+ }
298289+ }
299290+
@@ -320,13 +311,16 @@ index be429e8..a756519 100644
320311
321312 int
322313diff --git ./util.c ./util.c
323- index 55ae71f..d79eda7 100644
314+ index 55ae71f..a3cf593 100644
324315--- ./util.c
325316+++ ./util.c
326- @@ -153,3 +153 ,22 @@ map_physical(
317+ @@ -151,5 +151 ,22 @@ map_physical(
327318
328- return (void *)(addr + page_offset);
329- }
319+ close(fd);
320+
321+ - return (void *)(addr + page_offset);
322+ + return (void *)(addr + page_offset);
323+ + }
330324+
331325+ void
332326+ copy_physical(
@@ -343,9 +337,7 @@ index 55ae71f..d79eda7 100644
343337+ }
344338+
345339+ memcpy(dest, buf, len);
346- +
347- + munmap((uint8_t *)buf, len);
348- + }
340+ }
349341diff --git ./util.h ./util.h
350342index 6846007..d9fb59c 100644
351343--- ./util.h
0 commit comments