Update cbfs-init script for new cbfs tool option

This commit is contained in:
Francis Lam 2018-03-23 09:29:47 -07:00
parent d0294b1142
commit e0c1ab2d79
No known key found for this signature in database
GPG Key ID: 0A59C698920806EB
2 changed files with 142 additions and 81 deletions

View File

@ -8,9 +8,8 @@ if [ -z "$CBFS_PCR" ]; then
fi fi
# Load individual files # Load individual files
cbfsfiles=`cbfs -l 2>/dev/null | grep "^initrd/"` # \ cbfsfiles=`cbfs -t 50 -l 2>/dev/null | grep "^initrd/"` \
# || die "cbfs list files failed" || die "cbfs list files failed"
# qemu dies - ignore check for now
for cbfsname in `echo $cbfsfiles`; do for cbfsname in `echo $cbfsfiles`; do
filename=${cbfsname:6} filename=${cbfsname:6}
@ -18,7 +17,7 @@ for cbfsname in `echo $cbfsfiles`; do
echo "Loading $filename from CBFS" echo "Loading $filename from CBFS"
mkdir -p `dirname $filename` \ mkdir -p `dirname $filename` \
|| die "$filename: mkdir failed" || die "$filename: mkdir failed"
cbfs -r $cbfsname > "$filename" \ cbfs -t 50 -r $cbfsname > "$filename" \
|| die "$filename: cbfs file read failed" || die "$filename: cbfs file read failed"
if [ "$CONFIG_TPM" = "y" ]; then if [ "$CONFIG_TPM" = "y" ]; then
TMPFILE=/tmp/cbfs.$$ TMPFILE=/tmp/cbfs.$$

View File

@ -8,7 +8,7 @@ index eb53396..bf46e4e 100644
poke poke
+cbfs +cbfs
diff --git ./Makefile ./Makefile diff --git ./Makefile ./Makefile
index 27e28b5..ffb187c 100644 index 27e28b5..f02daee 100644
--- ./Makefile --- ./Makefile
+++ ./Makefile +++ ./Makefile
@@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
@ -23,16 +23,16 @@ index 27e28b5..ffb187c 100644
flashtool: flashtool.o spiflash.o util.o flashtool: flashtool.o spiflash.o util.o
peek: peek.o util.o peek: peek.o util.o
poke: poke.o util.o poke: poke.o util.o
+cbfs: cbfs.o +cbfs: cbfs.o util.o
$(TARGETS): $(TARGETS):
$(CC) $(LDFLAGS) -o $@ $^ $(CC) $(LDFLAGS) -o $@ $^
diff --git ./cbfs.c ./cbfs.c diff --git ./cbfs.c ./cbfs.c
new file mode 100644 new file mode 100644
index 0000000..7e11c88 index 0000000..bab5f76
--- /dev/null --- /dev/null
+++ ./cbfs.c +++ ./cbfs.c
@@ -0,0 +1,267 @@ @@ -0,0 +1,271 @@
+#include <stdio.h> +#include <stdio.h>
+#include <fcntl.h> +#include <fcntl.h>
+#include <stdlib.h> +#include <stdlib.h>
@ -43,6 +43,7 @@ index 0000000..7e11c88
+#include <unistd.h> +#include <unistd.h>
+#include <getopt.h> +#include <getopt.h>
+#include <arpa/inet.h> +#include <arpa/inet.h>
+#include "util.h"
+ +
+#define CBFS_HEADER_MAGIC 0x4F524243 +#define CBFS_HEADER_MAGIC 0x4F524243
+#define CBFS_HEADER_VERSION1 0x31313131 +#define CBFS_HEADER_VERSION1 0x31313131
@ -55,6 +56,7 @@ index 0000000..7e11c88
+ { "verbose", 0, NULL, 'v' }, + { "verbose", 0, NULL, 'v' },
+ { "read", 1, NULL, 'r' }, + { "read", 1, NULL, 'r' },
+ { "list", 0, NULL, 'l' }, + { "list", 0, NULL, 'l' },
+ { "type", 0, NULL, 't' },
+ { "help", 0, NULL, 'h' }, + { "help", 0, NULL, 'h' },
+ { NULL, 0, NULL, 0 }, + { NULL, 0, NULL, 0 },
+}; +};
@ -65,8 +67,9 @@ index 0000000..7e11c88
+"\n" +"\n"
+"-h | -? | --help This help\n" +"-h | -? | --help This help\n"
+"-v | --verbose Increase verbosity\n" +"-v | --verbose Increase verbosity\n"
+"-r | --read file Read the CBFS filed and dump to stdout\n" +"-r | --read file Export a CBFS file to stdout\n"
+"-l | --list List the raw files in the CBFS\n" +"-l | --list List the names of CBFS files\n"
+"-t | --type 50 Filter to specific CBFS file type (hex)\n"
+"\n"; +"\n";
+ +
+struct cbfs_header { +struct cbfs_header {
@ -106,8 +109,11 @@ index 0000000..7e11c88
+ int opt; + int opt;
+ int do_read = 0; + int do_read = 0;
+ int do_list = 0; + int do_list = 0;
+ int do_type = 0;
+ uint32_t cbfs_file_type = 0;
+ const char * filename = NULL; + const char * filename = NULL;
+ while ((opt = getopt_long(argc, argv, "h?vlr:", long_options, NULL)) != -1) + while ((opt = getopt_long(argc, argv, "h?vlr:t:",
+ long_options, NULL)) != -1)
+ { + {
+ switch(opt) + switch(opt)
+ { + {
@ -121,8 +127,12 @@ index 0000000..7e11c88
+ do_read = 1; + do_read = 1;
+ filename = optarg; + filename = optarg;
+ break; + break;
+ case 't':
+ do_type = 1;
+ cbfs_file_type = strtoul(optarg, NULL, 16);
+ break;
+ case '?': case 'h': + case '?': case 'h':
+ printf("%s", usage); + fprintf(stderr, "%s", usage);
+ return EXIT_SUCCESS; + return EXIT_SUCCESS;
+ default: + default:
+ fprintf(stderr, "%s", usage); + fprintf(stderr, "%s", usage);
@ -147,20 +157,13 @@ index 0000000..7e11c88
+ +
+ uint64_t end = 0x100000000; + uint64_t end = 0x100000000;
+ if (verbose) { + if (verbose) {
+ printf("Seeking to %lx\n", end-4); + fprintf(stderr, "Seeking to %lx\n", end-4);
+ } + }
+ if (lseek(fd, end-4, SEEK_SET) == -1) {
+ fprintf(stderr, "Failed to seek to %lx\n", end-4);
+ return EXIT_FAILURE;
+ }
+
+ int32_t header_delta; + int32_t header_delta;
+ if (read(fd, &header_delta, sizeof(header_delta)) < 0) { + copy_physical(end-4, sizeof(header_delta), &header_delta);
+ fprintf(stderr, "Failed to read header offset\n"); +
+ return EXIT_FAILURE;
+ }
+ if (verbose) { + if (verbose) {
+ printf("Header Offset: %d\n", header_delta); + fprintf(stderr, "Header Offset: %d\n", header_delta);
+ } + }
+ +
+ uint64_t header_off; + uint64_t header_off;
@ -171,18 +174,11 @@ index 0000000..7e11c88
+ } + }
+ +
+ if (verbose) { + if (verbose) {
+ printf("Seeking to %lx\n", header_off); + fprintf(stderr, "Seeking to %lx\n", header_off);
+ } + }
+ if (lseek(fd, header_off, SEEK_SET) == -1) {
+ fprintf(stderr, "Failed to seek to %lx\n", header_off);
+ return EXIT_FAILURE;
+ }
+
+ struct cbfs_header header; + struct cbfs_header header;
+ if (read(fd, &header, sizeof(struct cbfs_header)) < 0) { + copy_physical(header_off, sizeof(struct cbfs_header), &header);
+ fprintf(stderr, "Failed to read header\n"); +
+ return EXIT_FAILURE;
+ }
+ header.magic = ntohl(header.magic); + header.magic = ntohl(header.magic);
+ header.version = ntohl(header.version); + header.version = ntohl(header.version);
+ header.romsize = ntohl(header.romsize); + header.romsize = ntohl(header.romsize);
@ -192,13 +188,13 @@ index 0000000..7e11c88
+ header.architecture = ntohl(header.architecture); + header.architecture = ntohl(header.architecture);
+ +
+ if (verbose) { + if (verbose) {
+ printf("Header magic : %x\n", header.magic); + fprintf(stderr, "Header magic : %x\n", header.magic);
+ printf("Header version : %x\n", header.version); + fprintf(stderr, "Header version : %x\n", header.version);
+ printf("Header ROM size : %x\n", header.romsize); + fprintf(stderr, "Header ROM size : %x\n", header.romsize);
+ printf("Header boot block size: %x\n", header.bootblocksize); + fprintf(stderr, "Header boot block size: %x\n", header.bootblocksize);
+ printf("Header align : %x\n", header.align); + fprintf(stderr, "Header align : %x\n", header.align);
+ printf("Header offset : %x\n", header.offset); + fprintf(stderr, "Header offset : %x\n", header.offset);
+ printf("Header arch : %x\n", header.architecture); + fprintf(stderr, "Header arch : %x\n", header.architecture);
+ } + }
+ +
+ if (header.magic != CBFS_HEADER_MAGIC) { + if (header.magic != CBFS_HEADER_MAGIC) {
@ -208,21 +204,14 @@ index 0000000..7e11c88
+ +
+ uint32_t align = header.align; + uint32_t align = header.align;
+ // loop through files + // loop through files
+ uint64_t off = end - ((uint64_t) header.romsize) + ((uint64_t) header.offset); + uint64_t off = end - ((uint64_t) header.romsize) +
+ while (1) { + ((uint64_t) header.offset);
+ while (off < end) {
+ if (verbose) { + if (verbose) {
+ printf("Potential CBFS File Offset: %lx\n", off); + fprintf(stderr, "Potential CBFS File Offset: %lx\n", off);
+ } + }
+ if (lseek(fd, off, SEEK_SET) == -1) {
+ fprintf(stderr, "Failed to seek to next CBFS offset %lx\n", off);
+ return EXIT_FAILURE;
+ }
+
+ struct cbfs_file file; + struct cbfs_file file;
+ if (read(fd, &file, sizeof(struct cbfs_file)) < 0) { + copy_physical(off, sizeof(struct cbfs_file), &file);
+ fprintf(stderr, "Failed to read cbfs file header\n");
+ return EXIT_FAILURE;
+ }
+ +
+ file.len = ntohl(file.len); + file.len = ntohl(file.len);
+ file.type = ntohl(file.type); + file.type = ntohl(file.type);
@ -230,11 +219,11 @@ index 0000000..7e11c88
+ file.offset = ntohl(file.offset); + file.offset = ntohl(file.offset);
+ +
+ if (verbose) { + if (verbose) {
+ printf("File magic : %.8s\n", file.magic); + fprintf(stderr, "File magic : %.8s\n", file.magic);
+ printf("File len : %x\n", file.len); + fprintf(stderr, "File len : %x\n", file.len);
+ printf("File type : %x\n", file.type); + fprintf(stderr, "File type : %x\n", file.type);
+ printf("File attributes_offset : %x\n", file.attributes_offset); + fprintf(stderr, "File attributes_offset : %x\n", file.attributes_offset);
+ printf("File offset : %x\n", file.offset); + fprintf(stderr, "File offset : %x\n", file.offset);
+ } + }
+ +
+ if (strncmp((char *)file.magic, CBFS_FILE_MAGIC, 8) != 0) { + if (strncmp((char *)file.magic, CBFS_FILE_MAGIC, 8) != 0) {
@ -243,53 +232,68 @@ index 0000000..7e11c88
+ +
+ size_t name_size = file.offset - sizeof(struct cbfs_file); + size_t name_size = file.offset - sizeof(struct cbfs_file);
+ char *name = calloc(name_size, sizeof(char)); + char *name = calloc(name_size, sizeof(char));
+ if (read(fd, name, name_size) < 0) { + copy_physical(off+sizeof(struct cbfs_file), name_size, name);
+ fprintf(stderr, "Failed to read cbfs file name\n"); +
+ return EXIT_FAILURE;
+ }
+ if (verbose) { + if (verbose) {
+ printf("File name : '%s'\n", name); + fprintf(stderr, "File name : '%s'\n", name);
+ } + }
+ +
+ if (do_list && file.type == 0x50) { + if (do_list &&
+ (!do_type || (do_type && file.type == cbfs_file_type))) {
+ printf("%s\n", name); + printf("%s\n", name);
+ } + }
+ if (do_read && file.type == 0x50 && +
+ if (do_read &&
+ (!do_type || (do_type && file.type == cbfs_file_type)) &&
+ strncmp(name, filename, name_size) == 0) + strncmp(name, filename, name_size) == 0)
+ { + {
+ uint64_t file_off = off + file.offset; + uint64_t file_off = off + file.offset;
+ if (verbose) { + if (verbose) {
+ printf("Seeking to %lx\n-------- Start Data\n", file_off); + fprintf(stderr, "Seeking to %lx\n-------- Start Data\n", file_off);
+ }
+ if (lseek(fd, file_off, SEEK_SET) == -1) {
+ fprintf(stderr, "Failed to seek to file data at %lx\n", file_off);
+ return EXIT_FAILURE;
+ } + }
+ +
+ char *file_data = malloc(file.len); + char *file_data = malloc(file.len);
+ if (read(fd, file_data, file.len) < 0) { + if (file_data == NULL) {
+ fprintf(stderr, "Failed to read file data\n"); + fprintf(stderr, "Failed to allocate memory for file data: length=%d\n",
+ file.len);
+ return EXIT_FAILURE; + return EXIT_FAILURE;
+ } + }
+ if (write(STDOUT_FILENO, file_data, file.len) == -1) { + copy_physical(file_off, file.len, file_data);
+
+ for (size_t offset = 0 ; offset < file.len ; ) {
+ const ssize_t rc = write(
+ STDOUT_FILENO,
+ file_data + offset,
+ file.len - offset
+ );
+
+ if (rc <= 0) {
+ fprintf(stderr, "Failed to write file to stdout: %s\n", + fprintf(stderr, "Failed to write file to stdout: %s\n",
+ strerror(errno)); + strerror(errno));
+ return EXIT_FAILURE; + return EXIT_FAILURE;
+ } + }
+ free(file_data); +
+ if (verbose) { + offset += rc;
+ printf("\n-------- End Data\n");
+ }
+ do_read++;
+ } + }
+ +
+ if (verbose) {
+ fprintf(stderr, "\n-------- End Data\n");
+ }
+
+ do_read++;
+ free(file_data);
+ free(name);
+ break;
+ }
+
+ free(name);
+ uint64_t inc = (align + (file.offset + file.len) - 1) & (~(align-1)); + uint64_t inc = (align + (file.offset + file.len) - 1) & (~(align-1));
+ off += inc; + off += inc;
+ if (verbose) { + if (verbose) {
+ printf("File Off+Len : %x\n", file.offset + file.len); + fprintf(stderr, "File Off+Len : %x\n", file.offset + file.len);
+ printf("Align : %x\n", align); + fprintf(stderr, "Align : %x\n", align);
+ printf("Inc : %lx\n", inc); + fprintf(stderr, "Inc : %lx\n", inc);
+ printf("Next file align : %lx\n", off); + fprintf(stderr, "Next file align : %lx\n", off);
+ } + }
+ } + }
+ +
@ -300,3 +304,61 @@ index 0000000..7e11c88
+ +
+ return EXIT_SUCCESS; + return EXIT_SUCCESS;
+} +}
diff --git ./flashtool.c ./flashtool.c
index be429e8..a756519 100644
--- ./flashtool.c
+++ ./flashtool.c
@@ -204,8 +204,8 @@ write_to_spi(
#ifdef __darwin__
#define PCIEXBAR 0xE0000000 // MBP11,2
#else
-#define PCIEXBAR 0x80000000 // Winterfell
-//#define PCIEXBAR 0xF8000000 // x230
+//#define PCIEXBAR 0x80000000 // Winterfell
+#define PCIEXBAR 0xF8000000 // x230
#endif
int
diff --git ./util.c ./util.c
index 55ae71f..d79eda7 100644
--- ./util.c
+++ ./util.c
@@ -153,3 +153,22 @@ map_physical(
return (void *)(addr + page_offset);
}
+
+void
+copy_physical(
+ uint64_t phys_addr,
+ size_t len,
+ volatile void * dest
+)
+{
+ const uint8_t * const buf = map_physical(phys_addr, len);
+ if (buf == NULL)
+ {
+ perror("mmap");
+ exit(EXIT_FAILURE);
+ }
+
+ memcpy(dest, buf, len);
+
+ munmap((uint8_t *)buf, len);
+}
diff --git ./util.h ./util.h
index 6846007..d9fb59c 100644
--- ./util.h
+++ ./util.h
@@ -34,4 +34,11 @@ memcpy_width(
mem_op_t op
);
+extern void
+copy_physical(
+ uint64_t phys_addr,
+ size_t len,
+ volatile void *dest
+);
+
#endif