Hung-Te Lin (hungte@chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/2194
-gerrit
commit c843585cec8affebcecb2cceb16a30c8cf647b01 Author: Hung-Te Lin hungte@chromium.org Date: Sat Jan 26 02:03:28 2013 +0800
Improve CBFS tool to support non-x86 platform better.
CBFS tool was written with lots of global variables and implemented with assumptions based on x86 architecture. To support other platforms better, we should rewrite using logical offset.
Verified to work on ARM (google/snow). Does not work on x86 yet.
DO NOT SUBMIT. WIP.
Change-Id: I28c737c8f290e51332119188248ac9e28042024c Signed-off-by: Hung-Te Lin hungte@chromium.org --- src/arch/armv7/Makefile.inc | 4 +- util/cbfstool/cbfs-mkpayload.c | 82 ++- util/cbfstool/cbfs-mkstage.c | 39 +- util/cbfstool/cbfs.h | 3 + util/cbfstool/cbfstool.c | 645 ++++++++++------------ util/cbfstool/common.c | 1195 ++++++++++++++++++---------------------- util/cbfstool/common.h | 196 +++++-- 7 files changed, 1060 insertions(+), 1104 deletions(-)
diff --git a/src/arch/armv7/Makefile.inc b/src/arch/armv7/Makefile.inc index 872365c..d32b530 100644 --- a/src/arch/armv7/Makefile.inc +++ b/src/arch/armv7/Makefile.inc @@ -46,9 +46,11 @@ prebuild-files = \ $(if $(call extract_nth,4,$(file)),-b $(call extract_nth,4,$(file))) &&) prebuilt-files = $(foreach file,$(cbfs-files), $(call extract_nth,1,$(file)))
+# TODO(hungte) Change CBFS command to use per-board params. $(obj)/coreboot.pre1: $(objcbfs)/bootblock.bin $$(prebuilt-files) $(CBFSTOOL) $(CBFSTOOL) $@.tmp create -m armv7 -s $(CONFIG_COREBOOT_ROMSIZE_KB)K \ - -B $(objcbfs)/bootblock.bin -a 64 + -B $(objcbfs)/bootblock.bin -a 64 -A 0x0000 -H 0x2040 \ + -o 0x5000 $(prebuild-files) true mv $@.tmp $@ else diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c index 0d3986e..3c19d55 100644 --- a/util/cbfstool/cbfs-mkpayload.c +++ b/util/cbfstool/cbfs-mkpayload.c @@ -22,24 +22,18 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include "elf.h" -#include <fcntl.h> -#include <getopt.h> -#include <sys/stat.h>
#include "common.h" -#include "cbfs.h" +#include "elf.h"
-int parse_elf_to_payload(unsigned char *input, unsigned char **output, - comp_algo algo) +int parse_elf_to_payload(char *input, char **output, comp_algo algo) { Elf32_Phdr *phdr; Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; Elf32_Shdr *shdr; char *header; char *strtab; - unsigned char *sptr; + char *sptr; int headers; int segments = 1; int isize = 0, osize = 0; @@ -47,21 +41,16 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, struct cbfs_payload_segment *segs; int i;
- if(!iself(input)){ + if(!is_elf_object(input)){ fprintf(stderr, "E: The payload file is not in ELF format!\n"); return -1; }
- if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && - !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { - fprintf(stderr, "E: The payload file has the wrong architecture\n"); - return -1; - } - comp_func_ptr compress = compression_function(algo); if (!compress) return -1;
+ DEBUG("start: parse_elf_to_payload\n"); headers = ehdr->e_phnum; header = (char *)ehdr;
@@ -156,9 +145,9 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, if (phdr[i].p_filesz == 0) { segs[segments].type = PAYLOAD_SEGMENT_BSS; segs[segments].load_addr = - (uint64_t)htonll(phdr[i].p_paddr); + htonll(phdr[i].p_paddr); segs[segments].mem_len = - (uint32_t)htonl(phdr[i].p_memsz); + htonl(phdr[i].p_memsz); segs[segments].offset = htonl(doffset);
segments++; @@ -169,8 +158,8 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, segs[segments].type = PAYLOAD_SEGMENT_CODE; else segs[segments].type = PAYLOAD_SEGMENT_DATA; - segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr); - segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz); + segs[segments].load_addr = htonll(phdr[i].p_paddr); + segs[segments].mem_len = htonl(phdr[i].p_memsz); segs[segments].compression = htonl(algo); segs[segments].offset = htonl(doffset);
@@ -197,7 +186,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, }
segs[segments].type = PAYLOAD_SEGMENT_ENTRY; - segs[segments++].load_addr = (uint64_t)htonll(ehdr->e_entry); + segs[segments++].load_addr = htonll(ehdr->e_entry);
*output = sptr;
@@ -206,3 +195,54 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, err: return -1; } + +int parse_flat_binary_to_payload(char *input, + char **output, + uint32_t input_size, + uint32_t loadaddress, + uint32_t entrypoint, + comp_algo algo) +{ + comp_func_ptr compress; + char *payload; + struct cbfs_payload_segment *segs; + int doffset, len = 0; + + compress = compression_function(algo); + if (!compress) + return 1; + + DEBUG("start: parse_flat_binary_to_payload\n"); + /* FIXME compressed file size might be bigger than original file */ + payload = calloc((2 * sizeof(*segs)) + input_size, 1); + if (payload == NULL) { + fprintf(stderr, "E: Could not allocate memory.\n"); + return 1; + } + + segs = (struct cbfs_payload_segment *)payload; + doffset = (2 * sizeof(*segs)); + + /* Prepare code segment */ + segs[0].type = PAYLOAD_SEGMENT_CODE; + segs[0].load_addr = htonll(loadaddress); + segs[0].mem_len = htonl(input_size); + segs[0].offset = htonl(doffset); + + compress(input, input_size, (char *)(payload + doffset), &len); + segs[0].compression = htonl(algo); + segs[0].len = htonl(len); + + if ((unsigned int)len >= input_size) { + segs[0].compression = 0; + segs[0].len = htonl(input_size); + memcpy(payload + doffset, input, input_size); + } + + /* prepare entry point segment */ + segs[1].type = PAYLOAD_SEGMENT_ENTRY; + segs[1].load_addr = htonll(entrypoint); + + *output = payload; + return doffset + ntohl(segs[0].len); +} diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c index 0541d4b..9e47047 100644 --- a/util/cbfstool/cbfs-mkstage.c +++ b/util/cbfstool/cbfs-mkstage.c @@ -23,14 +23,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include "elf.h" -#include <fcntl.h> -#include <getopt.h> -#include <sys/stat.h>
#include "common.h" -#include "cbfs.h" +#include "elf.h"
static unsigned int idemp(unsigned int x) { @@ -48,13 +43,13 @@ static unsigned int swap32(unsigned int x) unsigned int (*elf32_to_native) (unsigned int) = idemp;
/* returns size of result, or -1 if error */ -int parse_elf_to_stage(unsigned char *input, unsigned char **output, - comp_algo algo, uint32_t * location) +int parse_elf_to_stage(char *input, char **output, + comp_algo algo, uint32_t location) { Elf32_Phdr *phdr; Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; char *header, *buffer; - unsigned char *out; + char *out;
int headers; int i; @@ -67,21 +62,16 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, if (!compress) return -1;
- if (!iself(input)) { + DEBUG("start: parse_elf_to_stage(location=0x%x)\n", location); + if (!is_elf_object(input)) { fprintf(stderr, "E: The stage file is not in ELF format!\n"); return -1; }
- if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && - !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { - fprintf(stderr, "E: The stage file has the wrong architecture\n"); - return -1; - } - if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) { elf_bigendian = 1; } - if (elf_bigendian != host_bigendian) { + if (elf_bigendian != is_big_endian()) { elf32_to_native = swap32; }
@@ -125,8 +115,8 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, mem_end = mend; }
- if (data_start < *location) { - data_start = *location; + if (data_start < location) { + data_start = location; }
if (data_end <= data_start) { @@ -155,9 +145,9 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, continue;
l_start = elf32_to_native(phdr[i].p_paddr); - if (l_start < *location) { - l_offset = *location - l_start; - l_start = *location; + if (l_start < location) { + l_offset = location - l_start; + l_start = location; }
memcpy(buffer + (l_start - data_start), @@ -184,10 +174,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
free(buffer); - *output = out;
- if (*location) - *location -= sizeof(struct cbfs_stage); - return sizeof(struct cbfs_stage) + stage->len; + return sizeof(*stage) + stage->len; } diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h index 3dbeefd..35d0670 100644 --- a/util/cbfstool/cbfs.h +++ b/util/cbfstool/cbfs.h @@ -42,6 +42,8 @@ struct cbfs_header { #define CBFS_ARCHITECTURE_X86 0x00000001 #define CBFS_ARCHITECTURE_ARMV7 0x00000010
+#define CBFS_FILE_MAGIC "LARCHIVE" + struct cbfs_file { uint8_t magic[8]; uint32_t len; @@ -106,6 +108,7 @@ struct cbfs_payload { #define CBFS_COMPONENT_NULL 0xFFFFFFFF
int cbfs_file_header(unsigned long physaddr); +#define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file)) #define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) )
struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size); diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 4202801..6aecfcb 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -4,6 +4,7 @@ * Copyright (C) 2009 coresystems GmbH * written by Patrick Georgi patrick.georgi@coresystems.de * Copyright (C) 2012 Google, Inc. + * Copyright (C) 2013 The ChromiumOS Authors. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,7 +27,6 @@ #include <unistd.h> #include <getopt.h> #include "common.h" -#include "cbfs.h"
struct command { const char *name; @@ -35,23 +35,36 @@ struct command { };
int verbose = 0; -static char *cbfs_name = NULL; -static char *rom_name = NULL; -static char *rom_filename = NULL; -static char *rom_bootblock = NULL; -static uint32_t rom_type = 0; -static uint32_t rom_baseaddress = 0; -static uint32_t rom_loadaddress = 0; -static uint32_t rom_entrypoint = 0; -static uint32_t rom_size = 0; -static uint32_t rom_alignment = 0; -static uint32_t rom_offset = 0; -static comp_algo rom_algo = CBFS_COMPRESS_NONE; - -static int cbfs_add(void) -{ - uint32_t filesize = 0; - void *rom, *filedata, *cbfsfile; + +struct param { + char *cbfs_name; + char *rom_name; + char *rom_filename; + char *rom_bootblock; + uint32_t rom_type; + uint32_t rom_baseaddress; + uint32_t rom_loadaddress; + uint32_t rom_bootaddress; + uint32_t rom_bootaddress_assigned; + uint32_t rom_headeraddress; + uint32_t rom_headeradress_assigned; + uint32_t rom_entrypoint; + uint32_t rom_size; + uint32_t rom_alignment; + uint32_t rom_offset; + comp_algo rom_algo; +} param; + +typedef int (*convert_buffer_t)(struct buffer *buffer); + +static int cbfs_add_file(const char *rom_filename, + const char *rom_name, + uint32_t rom_type, + convert_buffer_t convert, + const char *cbfs_name) { + struct cbfs_image image; + struct buffer buffer; + int result = 1;
if (!rom_filename) { fprintf(stderr, "E: You need to specify -f/--filename.\n"); @@ -68,404 +81,331 @@ static int cbfs_add(void) return 1; }
- rom = loadrom(cbfs_name); - if (rom == NULL) { + if (cbfs_image_from_file(&image, cbfs_name) != 0) { fprintf(stderr, "E: Could not load ROM image '%s'.\n", cbfs_name); return 1; }
- filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET); - if (filedata == NULL) { - fprintf(stderr, "E: Could not load file '%s'.\n", - rom_filename); - free(rom); + if (buffer_from_file(&buffer, rom_filename) != 0) { + fprintf(stderr, "E: Could not load file '%s'.\n", rom_filename); + cbfs_image_delete(&image); return 1; }
- cbfsfile = create_cbfs_file(rom_name, filedata, &filesize, - rom_type, &rom_baseaddress); - free(filedata);
- if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) { - fprintf(stderr, "E: Adding file '%s' failed.\n", rom_filename); - free(cbfsfile); - free(rom); - return 1; - } - if (writerom(cbfs_name, rom, romsize)) { - free(cbfsfile); - free(rom); - return 1; - } + do { + if (convert && convert(&buffer) != 0) { + fprintf(stderr, "E: Failed to parse file '%s'.\n", + rom_filename); + break; + } + if (cbfs_add_entry(&image, rom_name, rom_type, 0, &buffer) != 0) + break; + if (cbfs_image_write_file(&image, cbfs_name) != 0) + break;
- free(cbfsfile); - free(rom); - return 0; -} + result = 0; + } while (0);
-static int cbfs_add_payload(void) -{ - int32_t filesize = 0; - void *rom, *filedata, *cbfsfile; - unsigned char *payload; + buffer_delete(&buffer); + cbfs_image_delete(&image); + return result; +}
- if (!rom_filename) { - fprintf(stderr, "E: You need to specify -f/--filename.\n"); - return 1; - } +static int cbfs_convert_mkstage(struct buffer *buffer) { + char *name; + char *output; + int result = parse_elf_to_stage(buffer->data, &output, param.rom_algo, + param.rom_baseaddress); + if (result < 0) + return -1; + name = strdup(buffer->name); + buffer_delete(buffer); + buffer->name = name; + buffer->size = (size_t)result; + buffer->data = (char*)output; + return 0; +}
- if (!rom_name) { - fprintf(stderr, "E: You need to specify -n/--name.\n"); - return 1; - } +static int cbfs_convert_elf_mkpayload(struct buffer *buffer) { + char *name; + char *output; + int result = parse_elf_to_payload(buffer->data, &output, + param.rom_algo); + if (result < 0) + return -1; + name = strdup(buffer->name); + buffer_delete(buffer); + buffer->name = name; + buffer->size = (size_t)result; + buffer->data = (char*)output; + return 0; +}
- rom = loadrom(cbfs_name); - if (rom == NULL) { - fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); - return 1; - } +static int cbfs_convert_flat_mkpayload(struct buffer *buffer) { + char *name; + char *output; + int result; + result = parse_flat_binary_to_payload(buffer->data, &output, + buffer->size, + param.rom_loadaddress, + param.rom_entrypoint, + param.rom_algo); + if (result < 0) + return -1; + name = strdup(buffer->name); + buffer_delete(buffer); + buffer->name = name; + buffer->size = (size_t)result; + buffer->data = (char*)output; + return 0; +}
- filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET); - if (filedata == NULL) { - fprintf(stderr, "E: Could not load file '%s'.\n", - rom_filename); - free(rom); - return 1; - } +static int cbfstool_add(void) +{ + // TODO where is rom_baseaddress? + return cbfs_add_file(param.rom_filename, + param.rom_name, + param.rom_type, + NULL, + param.cbfs_name); +}
- filesize = parse_elf_to_payload(filedata, &payload, rom_algo); - if (filesize <= 0) { - fprintf(stderr, "E: Adding payload '%s' failed.\n", - rom_filename); - free(rom); - return 1; - }
- cbfsfile = create_cbfs_file(rom_name, payload, &filesize, - CBFS_COMPONENT_PAYLOAD, &rom_baseaddress); +static int cbfstool_add_payload(void) +{ + return cbfs_add_file(param.rom_filename, + param.rom_name, + CBFS_COMPONENT_PAYLOAD, + cbfs_convert_elf_mkpayload, + param.cbfs_name); +}
- free(filedata); - free(payload); +static int cbfstool_add_stage(void) +{ + return cbfs_add_file(param.rom_filename, + param.rom_name, + CBFS_COMPONENT_STAGE, + cbfs_convert_mkstage, + param.cbfs_name); +}
- if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) { - fprintf(stderr, "E: Adding payload '%s' failed.\n", - rom_filename); - free(cbfsfile); - free(rom); +static int cbfstool_add_flat_binary(void) +{ + if (param.rom_loadaddress == 0) { + fprintf(stderr, "E: You need to specify a valid " + "-l/--load-address.\n"); return 1; } - - if (writerom(cbfs_name, rom, romsize)) { - free(cbfsfile); - free(rom); + if (param.rom_entrypoint == 0) { + fprintf(stderr, "E: You need to specify a valid " + "-e/--entry-point.\n"); return 1; } - - free(cbfsfile); - free(rom); - return 0; + return cbfs_add_file(param.rom_filename, + param.rom_name, + CBFS_COMPONENT_PAYLOAD, + cbfs_convert_flat_mkpayload, + param.cbfs_name); }
-static int cbfs_add_stage(void) +static int cbfstool_remove(void) { - uint32_t filesize = 0; - void *rom, *filedata, *cbfsfile; - unsigned char *stage; + int result = 1; + struct cbfs_image image;
- if (!rom_filename) { - fprintf(stderr, "E: You need to specify -f/--filename.\n"); - return 1; - } - - if (!rom_name) { + if (!param.rom_name) { fprintf(stderr, "E: You need to specify -n/--name.\n"); return 1; }
- rom = loadrom(cbfs_name); - if (rom == NULL) { + if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); - return 1; - } - - filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET); - if (filedata == NULL) { - fprintf(stderr, "E: Could not load file '%s'.\n", - rom_filename); - free(rom); + param.cbfs_name); return 1; }
- filesize = parse_elf_to_stage(filedata, &stage, rom_algo, &rom_baseaddress); - - cbfsfile = create_cbfs_file(rom_name, stage, &filesize, - CBFS_COMPONENT_STAGE, &rom_baseaddress); - - free(filedata); - free(stage); - - if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) { - fprintf(stderr, "E: Adding stage '%s' failed.\n", - rom_filename); - free(cbfsfile); - free(rom); - return 1; - } - - if (writerom(cbfs_name, rom, romsize)) { - free(cbfsfile); - free(rom); - return 1; - } + do { + if (cbfs_remove_entry(&image, param.rom_name) != 0) { + fprintf(stderr, "E: Removing file '%s' failed.\n", + param.rom_name); + break; + } + if (cbfs_image_write_file(&image, param.cbfs_name) != 0) + break; + result = 0; + } while (0);
- free(cbfsfile); - free(rom); - return 0; + cbfs_image_delete(&image); + return result; }
-static int cbfs_add_flat_binary(void) +static int cbfstool_create(void) { - uint32_t filesize = 0; - uint32_t final_size; - void *rom, *filedata, *cbfsfile; - unsigned char *payload; - comp_func_ptr compress; - struct cbfs_payload_segment *segs; - int doffset, len = 0; + struct cbfs_image image; + struct buffer bootblock;
- if (!rom_filename) { - fprintf(stderr, "E: You need to specify -f/--filename.\n"); - return 1; - } - - if (!rom_name) { - fprintf(stderr, "E: You need to specify -n/--name.\n"); - return 1; - } - - if (rom_loadaddress == 0) { - fprintf(stderr, "E: You need to specify a valid " - "-l/--load-address.\n"); - return 1; - } - - if (rom_entrypoint == 0) { - fprintf(stderr, "E: You need to specify a valid " - "-e/--entry-point.\n"); + if (param.rom_size == 0) { + fprintf(stderr, "E: You need to specify a valid -s/--size.\n"); return 1; }
- compress = compression_function(rom_algo); - if (!compress) - return 1; - - rom = loadrom(cbfs_name); - if (rom == NULL) { - fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); + if (!param.rom_bootblock) { + fprintf(stderr, "E: You need to specify -b/--bootblock.\n"); return 1; }
- filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET); - if (filedata == NULL) { - fprintf(stderr, "E: Could not load file '%s'.\n", - rom_filename); - free(rom); + if (buffer_from_file(&bootblock, param.rom_bootblock) != 0) { return 1; }
- /* FIXME compressed file size might be bigger than original file */ - payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1); - if (payload == NULL) { - fprintf(stderr, "E: Could not allocate memory.\n"); - free(filedata); - free(rom); - return 1; + // Setup default boot address and header address. + if (!param.rom_bootaddress_assigned) { + // put boot block before end of ROM. + param.rom_bootaddress = param.rom_size - bootblock.size; + DEBUG("bootblock in end of ROM.\n"); } - - segs = (struct cbfs_payload_segment *)payload; - doffset = (2 * sizeof(struct cbfs_payload_segment)); - - /* Prepare code segment */ - segs[0].type = PAYLOAD_SEGMENT_CODE; - segs[0].load_addr = (uint64_t)htonll(rom_loadaddress); - segs[0].mem_len = (uint32_t)htonl(filesize); - segs[0].offset = (uint32_t)htonl(doffset); - - compress(filedata, filesize, (char *)(payload + doffset), &len); - segs[0].compression = htonl(rom_algo); - segs[0].len = htonl(len); - - if ((unsigned int)len >= filesize) { - segs[0].compression = 0; - segs[0].len = htonl(filesize); - memcpy((char *)(payload + doffset), filedata, filesize); + if (!param.rom_headeradress_assigned) { + // Put header before bootblock, and make a reference in end of + // bootblock. + param.rom_headeraddress = ( + param.rom_bootaddress - + sizeof(struct cbfs_header)); + if (bootblock.size >= sizeof(uint32_t)) { + // TODO this only works for 32b top-aligned system now... + uint32_t ptr = param.rom_headeraddress - param.rom_size; + uint32_t *sig = (uint32_t*)(bootblock.data + + bootblock.size - + sizeof(ptr)); + *sig = ptr; + DEBUG("CBFS header address set in end of bootblock.\n"); + } }
- /* prepare entry point segment */ - segs[1].type = PAYLOAD_SEGMENT_ENTRY; - segs[1].load_addr = (uint64_t)htonll(rom_entrypoint); - - final_size = doffset + ntohl(segs[0].len); - cbfsfile = - create_cbfs_file(rom_name, payload, &final_size, - CBFS_COMPONENT_PAYLOAD, &rom_baseaddress); - - free(filedata); - free(payload); - - if (add_file_to_cbfs(cbfsfile, final_size, rom_baseaddress)) { - fprintf(stderr, "E: Adding payload '%s' failed.\n", - rom_filename); - free(cbfsfile); - free(rom); + if (cbfs_image_create(&image, + param.rom_size, + param.rom_alignment, + &bootblock, + param.rom_bootaddress, + param.rom_headeraddress, + param.rom_offset) != 0) { + fprintf(stderr, "E: Failed to create %s.\n", param.cbfs_name); return 1; } - if (writerom(cbfs_name, rom, romsize)) { - free(cbfsfile); - free(rom); + if (cbfs_image_write_file(&image, param.cbfs_name) != 0) { + fprintf(stderr, "E: Failed to write %s.\n", param.cbfs_name); return 1; } - - free(cbfsfile); - free(rom); + cbfs_image_delete(&image); return 0; + }
-static int cbfs_remove(void) +static int cbfstool_locate(void) { - void *rom; + struct cbfs_image image; + struct cbfs_file *entry; + struct buffer buffer;
- if (!rom_name) { - fprintf(stderr, "E: You need to specify -n/--name.\n"); - return 1; - } - - rom = loadrom(cbfs_name); - if (rom == NULL) { - fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); + if (!param.rom_filename) { + fprintf(stderr, "E: You need to specify -f/--filename.\n"); return 1; }
- if (remove_file_from_cbfs(rom_name)) { - fprintf(stderr, "E: Removing file '%s' failed.\n", - rom_name); - free(rom); + if (!param.rom_name) { + fprintf(stderr, "E: You need to specify -n/--name.\n"); return 1; }
- if (writerom(cbfs_name, rom, romsize)) { - free(rom); + if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { + fprintf(stderr, "E: Failed to load %s.\n", param.cbfs_name); return 1; }
- free(rom); - return 0; -}
-static int cbfs_create(void) -{ - if (rom_size == 0) { - fprintf(stderr, "E: You need to specify a valid -s/--size.\n"); + if (buffer_from_file(&buffer, param.rom_filename) != 0) { + fprintf(stderr, "E: Cannot load %s.\n", param.rom_filename); + cbfs_image_delete(&image); return 1; } - - if (!rom_bootblock) { - fprintf(stderr, "E: You need to specify -b/--bootblock.\n"); + if (cbfs_get_entry(&image, param.rom_name)) { + fprintf(stderr, "E: '%s' already in CBFS.\n", param.rom_name); + buffer_delete(&buffer); + cbfs_image_delete(&image); return 1; }
- if (arch == CBFS_ARCHITECTURE_UNKNOWN) { - fprintf(stderr, "E: You need to specify -m/--machine arch\n"); + if (cbfs_add_entry(&image, param.rom_name, CBFS_COMPONENT_RAW, + param.rom_alignment, &buffer) != 0) { + // Legacy output. + printf("0\n"); + buffer_delete(&buffer); + cbfs_image_delete(&image); return 1; } - - return create_cbfs_image(cbfs_name, rom_size, rom_bootblock, - rom_alignment, rom_offset); -} - -static int cbfs_locate(void) -{ - uint32_t filesize, location; - - if (!rom_filename) { - fprintf(stderr, "E: You need to specify -f/--filename.\n"); - return 1; - } - - if (!rom_name) { - fprintf(stderr, "E: You need to specify -n/--name.\n"); - return 1; - } - - filesize = getfilesize(rom_filename); - - location = cbfs_find_location(cbfs_name, filesize, - rom_name, rom_alignment); - - printf("%x\n", location); - return location == 0 ? 1 : 0; + entry = cbfs_get_entry(&image, param.rom_name); + printf("%x\n", (cbfs_get_entry_addr(&image, entry) + + ntohl(entry->offset))); + buffer_delete(&buffer); + cbfs_image_delete(&image); + return 0; }
-static int cbfs_print(void) +static int cbfstool_print(void) { - void *rom; - - rom = loadrom(cbfs_name); - if (rom == NULL) { + struct cbfs_image image; + if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); + param.cbfs_name); return 1; } + cbfs_print_header_info(&image); + printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); + cbfs_walk(&image, cbfs_print_entry_info, &verbose);
- print_cbfs_directory(cbfs_name); - - free(rom); + cbfs_image_delete(&image); return 0; }
-static int cbfs_extract(void) +static int cbfstool_extract(void) { - void *rom; - int ret; + int result = 0; + struct cbfs_image image;
- if (!rom_filename) { + if (!param.rom_filename) { fprintf(stderr, "E: You need to specify -f/--filename.\n"); return 1; }
- if (!rom_name) { + if (!param.rom_name) { fprintf(stderr, "E: You need to specify -n/--name.\n"); return 1; }
- rom = loadrom(cbfs_name); - if (rom == NULL) { + if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { fprintf(stderr, "E: Could not load ROM image '%s'.\n", - cbfs_name); - return 1; + param.cbfs_name); + result = 1; + } else if (cbfs_export_entry(&image, param.rom_name, + param.rom_filename) != 0) { + result = 1; }
- ret = extract_file_from_cbfs(cbfs_name, rom_name, rom_filename); - - free(rom); - return ret; + cbfs_image_delete(&image); + return result; }
static const struct command commands[] = { - {"add", "f:n:t:b:h?", cbfs_add}, - {"add-payload", "f:n:t:c:b:h?", cbfs_add_payload}, - {"add-stage", "f:n:t:c:b:h?", cbfs_add_stage}, - {"add-flat-binary", "f:n:l:e:c:b:h?", cbfs_add_flat_binary}, - {"remove", "n:h?", cbfs_remove}, - {"create", "s:B:a:o:m:h?", cbfs_create}, - {"locate", "f:n:a:h?", cbfs_locate}, - {"print", "h?", cbfs_print}, - {"extract", "n:f:h?", cbfs_extract}, + {"add", "vf:n:t:b:h?", cbfstool_add}, + {"add-payload", "vf:n:t:c:b:h?", cbfstool_add_payload}, + {"add-stage", "vf:n:t:c:b:h?", cbfstool_add_stage}, + {"add-flat-binary", "vf:n:l:e:c:b:h?", cbfstool_add_flat_binary}, + {"remove", "vn:h?", cbfstool_remove}, + {"create", "vs:B:A:H:a:o:m:h?", cbfstool_create}, + {"locate", "vf:n:a:h?", cbfstool_locate}, + {"print", "vh?", cbfstool_print}, + {"extract", "vn:f:h?", cbfstool_extract}, };
static struct option long_options[] = { @@ -477,6 +417,8 @@ static struct option long_options[] = { {"entry-point", required_argument, 0, 'e' }, {"size", required_argument, 0, 's' }, {"bootblock", required_argument, 0, 'B' }, + {"boot-address", required_argument, 0, 'A' }, + {"header-address",required_argument,0, 'H' }, {"alignment", required_argument, 0, 'a' }, {"offset", required_argument, 0, 'o' }, {"file", required_argument, 0, 'f' }, @@ -491,8 +433,9 @@ static void usage(char *name) printf ("cbfstool: Management utility for CBFS formatted ROM images\n\n" "USAGE:\n" " %s [-h]\n" - " %s FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n" - " -h Display this help message\n\n" + " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" + " -h Display this help message\n\n" + " -v Verbose output\n" "COMMANDs:\n" " add -f FILE -n NAME -t TYPE [-b base-address] " "Add a component\n" @@ -505,11 +448,12 @@ static void usage(char *name) "Add a 32bit flat mode binary\n" " remove -n NAME " "Remove a component\n" - " create -s size -B bootblock -m ARCH [-a align] [-o offset] " + " create -s size -B bootblock [-A boot-address] \\n" + " [-H header-address] -m ARCH [-a align] [-o offset] " "Create a ROM file\n" " locate -f FILE -n NAME -a align " "Find a place for a file of that size\n" - " print " + " print " "Show the contents of the ROM\n" " extract -n NAME -f FILE " "Extracts a raw payload from ROM\n" @@ -518,19 +462,7 @@ static void usage(char *name) " armv7, x86\n" "TYPEs:\n", name, name ); - print_supported_filetypes(); -} - -/* Small, OS/libc independent runtime check for endianess */ -int host_bigendian = 0; - -static void which_endian(void) -{ - static const uint32_t inttest = 0x12345678; - uint8_t inttest_lsb = *(uint8_t *)&inttest; - if (inttest_lsb == 0x12) { - host_bigendian = 1; - } + print_all_cbfs_entry_types(); }
int main(int argc, char **argv) @@ -543,9 +475,7 @@ int main(int argc, char **argv) return 1; }
- which_endian(); - - cbfs_name = argv[1]; + param.cbfs_name = argv[1]; char *cmd = argv[2]; optind += 2;
@@ -572,61 +502,70 @@ int main(int argc, char **argv)
switch(c) { case 'n': - rom_name = optarg; + param.rom_name = optarg; break; case 't': - if (intfiletype(optarg) != ((uint64_t) - 1)) - rom_type = intfiletype(optarg); - else - rom_type = strtoul(optarg, NULL, 0); - if (rom_type == 0) + param.rom_type = get_cbfs_entry_type(optarg); + if (param.rom_type == (uint32_t)-1) + param.rom_type = strtoul(optarg, NULL, + 0); + if (param.rom_type == 0) printf("W: Unknown type '%s' ignored\n", - optarg); + optarg); break; case 'c': if (!strncasecmp(optarg, "lzma", 5)) - rom_algo = CBFS_COMPRESS_LZMA; + param.rom_algo = CBFS_COMPRESS_LZMA; else if (!strncasecmp(optarg, "none", 5)) - rom_algo = CBFS_COMPRESS_NONE; + param.rom_algo = CBFS_COMPRESS_NONE; else printf("W: Unknown compression '%s'" - " ignored.\n", optarg); + " ignored.\n", optarg); break; case 'b': - rom_baseaddress = strtoul(optarg, NULL, 0); + param.rom_baseaddress = strtoul(optarg, NULL, 0); break; case 'l': - rom_loadaddress = strtoul(optarg, NULL, 0); + param.rom_loadaddress = strtoul(optarg, NULL, 0);
break; case 'e': - rom_entrypoint = strtoul(optarg, NULL, 0); + param.rom_entrypoint = strtoul(optarg, NULL, 0); break; case 's': - rom_size = strtoul(optarg, &suffix, 0); + param.rom_size = strtoul(optarg, &suffix, 0); if (tolower(suffix[0])=='k') { - rom_size *= 1024; + param.rom_size *= 1024; } if (tolower(suffix[0])=='m') { - rom_size *= 1024 * 1024; + param.rom_size *= 1024 * 1024; } case 'B': - rom_bootblock = optarg; + param.rom_bootblock = optarg; + break; + case 'A': + param.rom_bootaddress = strtoul(optarg, NULL, 0); + param.rom_bootaddress_assigned = 1; + break; + case 'H': + param.rom_headeraddress = strtoul( + optarg, NULL, 0); + param.rom_headeradress_assigned = 1; break; case 'a': - rom_alignment = strtoul(optarg, NULL, 0); + param.rom_alignment = strtoul(optarg, NULL, 0); break; case 'o': - rom_offset = strtoul(optarg, NULL, 0); + param.rom_offset = strtoul(optarg, NULL, 0); break; case 'f': - rom_filename = optarg; + param.rom_filename = optarg; break; case 'v': verbose++; break; case 'm': - arch = string_to_arch(optarg); + // arch = string_to_arch(optarg); break; case 'h': case '?': diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index 97bf168..680e59c 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -4,6 +4,7 @@ * Copyright (C) 2009 coresystems GmbH * written by Patrick Georgi patrick.georgi@coresystems.de * Copyright (C) 2012 Google, Inc. + * Copyright (C) 2013 The ChromiumOS Authors. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,215 +20,105 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA */
+#include <assert.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <libgen.h> + #include "common.h" -#include "cbfs.h" #include "elf.h"
-#define dprintf(x...) +/* Utilities */
-size_t getfilesize(const char *filename) +/* Small, OS/libc independent runtime check for endianess */ +int is_big_endian(void) { - size_t size; - FILE *file = fopen(filename, "rb"); - if (file == NULL) - return -1; - - fseek(file, 0, SEEK_END); - size = ftell(file); - fclose(file); - return size; -} - -void *loadfile(const char *filename, uint32_t * romsize_p, void *content, - int place) -{ - FILE *file = fopen(filename, "rb"); - if (file == NULL) - return NULL; - - fseek(file, 0, SEEK_END); - *romsize_p = ftell(file); - fseek(file, 0, SEEK_SET); - if (!content) { - content = malloc(*romsize_p); - if (!content) { - fprintf(stderr, "E: Could not get %d bytes for file %s\n", - *romsize_p, filename); - exit(1); - } - } else if (place == SEEK_END) - content -= *romsize_p; - - if (!fread(content, *romsize_p, 1, file)) { - fprintf(stderr, "E: Failed to read %s\n", filename); - return NULL; + static const uint32_t inttest = 0x12345678; + uint8_t inttest_lsb = *(uint8_t *)&inttest; + if (inttest_lsb == 0x12) { + return 1; } - fclose(file); - return content; + return 0; }
-static struct cbfs_header *master_header; -static uint32_t phys_start, phys_end, align; -uint32_t romsize; -void *offset; -uint32_t arch = CBFS_ARCHITECTURE_UNKNOWN; - -static struct { - uint32_t arch; - const char *name; -} arch_names[] = { - { CBFS_ARCHITECTURE_ARMV7, "armv7" }, - { CBFS_ARCHITECTURE_X86, "x86" }, - { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } -}; - -uint32_t string_to_arch(const char *arch_string) -{ - int i; - uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; - - for (i = 0; i < ARRAY_SIZE(arch_names); i++) { - if (!strcasecmp(arch_string, arch_names[i].name)) { - ret = arch_names[i].arch; - break; - } - } - - return ret; +uint32_t align_up(uint32_t value, uint32_t align) { + if (value % align) + value += align - (value % align); + return value; }
-const char *arch_to_string(uint32_t a) -{ - int i; - const char *ret = NULL; - - for (i = 0; i < ARRAY_SIZE(arch_names); i++) { - if (a == arch_names[i].arch) { - ret = arch_names[i].name; - break; - } - } - - return ret; +/* Buffer and file I/O */
+int buffer_create(struct buffer *buffer, size_t size, const char *name) { + buffer->name = strdup(name); + buffer->size = size; + buffer->data = (char*)malloc(buffer->size); + return (buffer->data == NULL); }
-int find_master_header(void *romarea, size_t size) -{ - size_t offset; - - if (master_header) - return 0; - - for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) { - struct cbfs_header *tmp = romarea + offset; - - if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) { - master_header = tmp; - break; - } +int buffer_from_file(struct buffer *buffer, const char *filename) { + FILE *fp = fopen(filename, "rb"); + if (!fp) { + perror(filename); + return -1; } - - return master_header ? 0 : 1; -} - -void recalculate_rom_geometry(void *romarea) -{ - if (find_master_header(romarea, romsize)) { - fprintf(stderr, "E: Cannot find master header\n"); - exit(1); + fseek(fp, 0, SEEK_END); + buffer->size = ftell(fp); + buffer->name = strdup(filename); + rewind(fp); + buffer->data = (char*)malloc(buffer->size); + assert(buffer->data); + if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { + ERROR("incomplete read: %s\n", filename); + fclose(fp); + return -1; } + fclose(fp); + return 0; +}
- /* Update old headers */ - if (master_header->version == CBFS_HEADER_VERSION1 && - ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) { - dprintf("Updating CBFS master header to version 2\n"); - master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); +int buffer_write_file(struct buffer *buffer, const char *filename) { + FILE *fp = fopen(filename, "wb"); + if (!fp) { + perror(filename); + return -1; } - - arch = ntohl(master_header->architecture); - - switch (arch) { - case CBFS_ARCHITECTURE_ARMV7: - offset = romarea; - phys_start = (0 + ntohl(master_header->offset)) & 0xffffffff; - phys_end = romsize & 0xffffffff; - break; - case CBFS_ARCHITECTURE_X86: - offset = romarea + romsize - 0x100000000ULL; - phys_start = (0 - romsize + ntohl(master_header->offset)) & - 0xffffffff; - phys_end = (0 - ntohl(master_header->bootblocksize) - - sizeof(struct cbfs_header)) & 0xffffffff; - break; - default: - fprintf(stderr, "E: Unknown architecture\n"); - exit(1); + assert(buffer && buffer->data); + if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { + ERROR("incomplete write: %s\n", filename); + fclose(fp); + return -1; } - - align = ntohl(master_header->align); -} - -void *loadrom(const char *filename) -{ - void *romarea = loadfile(filename, &romsize, 0, SEEK_SET); - if (romarea == NULL) - return NULL; - recalculate_rom_geometry(romarea); - return romarea; + fclose(fp); + return 0; }
-int writerom(const char *filename, void *start, uint32_t size) -{ - FILE *file = fopen(filename, "wb"); - if (!file) { - fprintf(stderr, "Could not open '%s' for writing: ", filename); - perror(""); - return 1; +int buffer_delete(struct buffer *buffer) { + assert(buffer); + if (buffer->name) { + free(buffer->name); + buffer->name = NULL; } - - if (fwrite(start, size, 1, file) != 1) { - fprintf(stderr, "Could not write to '%s': ", filename); - perror(""); - return 1; + if (buffer->data) { + free(buffer->data); + buffer->data = NULL; } - - fclose(file); + buffer->size = 0; return 0; }
-int cbfs_file_header(unsigned long physaddr) -{ - /* maybe improve this test */ - return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0); -} - -struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size) -{ - struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr); - strncpy((char *)(nextfile->magic), "LARCHIVE", 8); - nextfile->len = htonl(size); - nextfile->type = htonl(0xffffffff); - nextfile->checksum = 0; // FIXME? - nextfile->offset = htonl(sizeof(struct cbfs_file) + 16); - memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16); - return nextfile; -} +/* Type and format */
-int iself(unsigned char *input) -{ - Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; +int is_elf_object(const void *input) { + Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input; return !memcmp(ehdr->e_ident, ELFMAG, 4); }
static struct filetypes_t { uint32_t type; const char *name; -} filetypes[] = { +} cbfs_filetypes[] = { {CBFS_COMPONENT_STAGE, "stage"}, {CBFS_COMPONENT_PAYLOAD, "payload"}, {CBFS_COMPONENT_OPTIONROM, "optionrom"}, @@ -236,550 +127,544 @@ static struct filetypes_t { {CBFS_COMPONENT_VSA, "vsa"}, {CBFS_COMPONENT_MBI, "mbi"}, {CBFS_COMPONENT_MICROCODE, "microcode"}, - {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, - {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, + {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"}, + {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"}, {CBFS_COMPONENT_DELETED, "deleted"}, {CBFS_COMPONENT_NULL, "null"} };
-void print_supported_filetypes(void) -{ - int i, number = ARRAY_SIZE(filetypes); - - for (i=0; i<number; i++) { - printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); - if ((i%8) == 7) - printf("\n"); - } -} - -const char *strfiletype(uint32_t number) -{ +uint32_t get_cbfs_entry_type(const char *name) { size_t i; - for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) - if (filetypes[i].type == number) - return filetypes[i].name; - return "unknown"; + for (i = 0; i < ARRAY_SIZE(cbfs_filetypes); i++) + if (strcmp(cbfs_filetypes[i].name, name) == 0) + return cbfs_filetypes[i].type; + return -1; }
-uint64_t intfiletype(const char *name) -{ +const char * get_cbfs_entry_type_name(uint32_t type) { size_t i; - for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) - if (strcmp(filetypes[i].name, name) == 0) - return filetypes[i].type; - return -1; + for (i = 0; i < ARRAY_SIZE(cbfs_filetypes); i++) + if (cbfs_filetypes[i].type == type) + return cbfs_filetypes[i].name; + return "(unknown)"; }
-void print_cbfs_directory(const char *filename) -{ - printf - ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\n" - "alignment: %d bytes, architecture: %s\n\n", - basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), - romsize, ntohl(master_header->offset), align, arch_to_string(arch)); - printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); - uint32_t current = phys_start; - while (current < phys_end) { - if (!cbfs_file_header(current)) { - current += align; - continue; - } - struct cbfs_file *thisfile = - (struct cbfs_file *)phys_to_virt(current); - uint32_t length = ntohl(thisfile->len); - char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); - if (strlen(fname) == 0) - fname = "(empty)"; - - printf("%-30s 0x%-8x %-12s %d\n", fname, - current - phys_start + ntohl(master_header->offset), - strfiletype(ntohl(thisfile->type)), length); - - /* note the components of the subheader are in host order ... */ - switch (ntohl(thisfile->type)) { - case CBFS_COMPONENT_STAGE: - { - struct cbfs_stage *stage = CBFS_SUBHEADER(thisfile); - dprintf(" %s compression, entry: 0x%llx, load: 0x%llx, length: %d/%d\n", - stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", - (unsigned long long)stage->entry, - (unsigned long long)stage->load, - stage->len, - stage->memlen); - break; - } - case CBFS_COMPONENT_PAYLOAD: - { - struct cbfs_payload_segment *payload = CBFS_SUBHEADER(thisfile); - while(payload) { - switch(payload->type) { - case PAYLOAD_SEGMENT_CODE: - case PAYLOAD_SEGMENT_DATA: - dprintf(" %s (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d)\n", - payload->type == PAYLOAD_SEGMENT_CODE ? "code " : "data" , - payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", - ntohl(payload->offset), - (unsigned long long)ntohll(payload->load_addr), - ntohl(payload->len), ntohl(payload->mem_len)); - break; - case PAYLOAD_SEGMENT_ENTRY: - dprintf(" entry (0x%llx)\n", (unsigned long long)ntohll(payload->load_addr)); - break; - case PAYLOAD_SEGMENT_BSS: - dprintf(" BSS (address 0x%016llx, length 0x%x)\n", (unsigned long long)ntohll(payload->load_addr), ntohl(payload->len)); - break; - case PAYLOAD_SEGMENT_PARAMS: - dprintf(" parameters\n"); - break; - default: - dprintf(" %x (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d\n", - payload->type, - payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", - ntohl(payload->offset), - (unsigned long long)ntohll(payload->load_addr), - ntohl(payload->len), - ntohl(payload->mem_len)); - break; - } +void print_all_cbfs_entry_types(void) { + size_t i, total = ARRAY_SIZE(cbfs_filetypes);
- if(payload->type == PAYLOAD_SEGMENT_ENTRY) - payload=NULL; - else - payload++; - } - break; - } - default: - break; - } - current = - ALIGN(current + ntohl(thisfile->len) + - ntohl(thisfile->offset), align); + for (i = 0; i < total; i++) { + printf(" %s%c", cbfs_filetypes[i].name, + (i + 1 == total) ? '\n' : ','); + if ((i + 1) % 8 == 0) + printf("\n"); } }
-int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath) +/* CBFS image processing */ + +/* The file name align is not defined in CBFS spec -- only a preference by + * cbfstool. */ +#define CBFS_FILENAME_ALIGN (16) + +int cbfs_image_create(struct cbfs_image *image, + size_t size, + uint32_t align, + struct buffer *bootblock, + int32_t bootblock_offset, + int32_t header_offset, + int32_t entries_offset) { - FILE *outfile = NULL; - uint32_t current = phys_start; - while (current < phys_end) { - if (!cbfs_file_header(current)) { - current += align; - continue; - } + struct cbfs_header *header; + struct cbfs_file *entry; + uint32_t cbfs_len;
- // Locate the file start struct - struct cbfs_file *thisfile = - (struct cbfs_file *)phys_to_virt(current); - // And its length - uint32_t length = ntohl(thisfile->len); - // Locate the file name - char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); - - // It's not the file we are looking for.. - if (strcmp(fname, payloadname) != 0) - { - current = - ALIGN(current + ntohl(thisfile->len) + - ntohl(thisfile->offset), align); - continue; - } + DEBUG("cbfs_create_image: bootblock=0x%x+0x%zx, " + "header=0x%x+0x%zx, entries_offset=0x%x\n", + bootblock_offset, bootblock->size, + header_offset, sizeof(*header), entries_offset);
- // Else, it's our file. - printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname, - current - phys_start, strfiletype(ntohl(thisfile->type)), - length); - - // If we are not dumping to stdout, open the out file. - outfile = fopen(outpath, "wb"); - if (!outfile) - { - fprintf(stderr, "E: Could not open the file %s for writing.\n", outpath); - return 1; - } + if (buffer_create(&image->buffer, size, "(new)") != 0) { + ERROR("Cannot allocate memory for %zd bytes.\n", size); + return -1; + } + image->header = NULL; + memset(image->buffer.data, -1, size);
- if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW) - { - fprintf(stderr, "W: Only 'raw' files are safe to extract.\n"); - } + // Adjust relative offset values (<0) to ROM address (>=0). + if (entries_offset < 0) + entries_offset += (int32_t)size; + if (bootblock_offset < 0) + bootblock_offset += (int32_t)size; + if (header_offset < 0) + header_offset += (int32_t) size;
- fwrite(((char *)thisfile) - + ntohl(thisfile->offset), length, 1, outfile); + DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " + "header=0x%x, entries_offset=0x%x\n", + bootblock_offset, header_offset, entries_offset);
- fclose(outfile); - printf("Successfully dumped the file.\n"); + if (align == 0) + align = 64; // default align size.
- // We'll only dump one file. - return 0; + // Prepare bootblock + if (bootblock_offset + bootblock->size > size) { + ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", + bootblock_offset, bootblock->size, size); + return -1; } - fprintf(stderr, "E: File %s not found.\n", payloadname); - return 1; -} + memcpy(image->buffer.data + bootblock_offset, bootblock->data, + bootblock->size);
- -int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location) -{ - uint32_t current = phys_start; - while (current < phys_end) { - if (!cbfs_file_header(current)) { - current += align; - continue; - } - struct cbfs_file *thisfile = - (struct cbfs_file *)phys_to_virt(current); - uint32_t length = ntohl(thisfile->len); - - dprintf("at %x, %x bytes\n", current, length); - /* Is this a free chunk? */ - if ((thisfile->type == CBFS_COMPONENT_DELETED) - || (thisfile->type == CBFS_COMPONENT_NULL)) { - dprintf("null||deleted at %x, %x bytes\n", current, - length); - /* if this is the right size, and if specified, the right location, use it */ - if ((contentsize <= length) - && ((location == 0) || (current == location))) { - if (contentsize < length) { - dprintf - ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n", - length, contentsize, - ALIGN(current + contentsize, - align), - length - contentsize); - uint32_t start = - ALIGN(current + contentsize, align); - uint32_t size = - current + ntohl(thisfile->offset) - + length - start - 16 - - sizeof(struct cbfs_file); - cbfs_create_empty_file(start, size); - } - dprintf("copying data\n"); - memcpy(phys_to_virt(current), content, - contentsize); - return 0; - } - if (location != 0) { - /* CBFS has the constraint that the chain always moves up in memory. so once - we're past the place we seek, we don't need to look any further */ - if (current > location) { - fprintf - (stderr, "E: The requested space is not available\n"); - return 1; - } - - /* Is the requested location inside the current chunk? */ - if ((current < location) - && ((location + contentsize) <= - (current + length))) { - /* Split it up. In the next iteration the code will be at the right place. */ - dprintf("split up. new length: %x\n", - location - current - - ntohl(thisfile->offset)); - thisfile->len = - htonl(location - current - - ntohl(thisfile->offset)); - cbfs_create_empty_file(location, - length - - (location - - current)); - } - } - } - current = - ALIGN(current + ntohl(thisfile->len) + - ntohl(thisfile->offset), align); + // Prepare header + if (header_offset + sizeof(*header) > size) { + ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", + header_offset, sizeof(*header), size); + return -1; + } + header = (struct cbfs_header*)(image->buffer.data + header_offset); + image->header = header; + header->magic = htonl(CBFS_HEADER_MAGIC); + header->version = htonl(CBFS_HEADER_VERSION); + header->romsize = htonl(size); + header->bootblocksize = htonl(bootblock->size); + header->align = htonl(align); + header->offset = htonl(entries_offset); + header->architecture = ntohl(CBFS_ARCHITECTURE_ARMV7); + + // Prepare entries + if (align_up(entries_offset, align) != entries_offset) { + ERROR("Offset (0x%x) must be aligned to 0x%x.\n", + entries_offset, align); + return -1; } - fprintf(stderr, "E: Could not add the file to CBFS, it's probably too big.\n"); - fprintf(stderr, "E: File size: %d bytes (%d KB).\n", contentsize, contentsize/1024); - return 1; + if (entries_offset + sizeof(*entry) > size) { + ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n", + entries_offset, sizeof(*entry), size); + return -1; + } + entry = (struct cbfs_file*)(image->buffer.data + entries_offset); + // To calculate available length, find + // e = min(bootblock, header, size) where e > entries_offset. + cbfs_len = size; + if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) + cbfs_len = bootblock_offset; + if (header_offset > entries_offset && header_offset < cbfs_len) + cbfs_len = header_offset; + cbfs_len -= entries_offset + align; + cbfs_create_entry(image, entry, cbfs_len, ""); + LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len); + return 0; }
-static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first, - struct cbfs_file *second) -{ - uint32_t new_length = - ntohl(first->len) + ntohl(second->len) + ntohl(second->offset); - first->len = htonl(new_length); - first->checksum = 0; // FIXME? - return first; +int cbfs_image_from_file(struct cbfs_image *image, const char *filename) { + if (buffer_from_file(&image->buffer, filename) != 0) + return -1; + DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name, + image->buffer.size); + image->header = cbfs_find_header(image->buffer.data, + image->buffer.size); + if (!image->header) { + ERROR("%s does not have CBFS master header.\n", filename); + cbfs_image_delete(image); + return -1; + } + return 0; }
-static struct cbfs_file *next_file(struct cbfs_file *prev) -{ - uint32_t pos = (prev == NULL) ? phys_start : - ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset), - align); - - for (; pos < phys_end; pos += align) { - if (cbfs_file_header(pos)) - return (struct cbfs_file *)phys_to_virt(pos); - } - return NULL; +int cbfs_image_write_file(struct cbfs_image *image, const char *filename) { + assert(image && image->buffer.data); + return buffer_write_file(&image->buffer, filename); }
+int cbfs_image_delete(struct cbfs_image *image) { + buffer_delete(&image->buffer); + image->header = NULL; + return 0; +}
-int remove_file_from_cbfs(const char *filename) -{ - struct cbfs_file *prev = NULL; - struct cbfs_file *cur = next_file(prev); - struct cbfs_file *next = next_file(cur); - for (; cur; prev = cur, cur = next, next = next_file(next)) { - - /* Check if this is the file to remove. */ - char *name = (char *)cur + sizeof(*cur); - if (strcmp(name, filename)) +int cbfs_add_entry(struct cbfs_image *image, const char *name, + uint32_t type, size_t data_align, struct buffer *buffer) { + uint32_t entry_type, entry_capacity; + uint32_t addr; + struct cbfs_file *entry, *next; + uint32_t need_size, new_size; + + need_size = (sizeof(*entry) + buffer->size + + align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); + DEBUG("cbfs_add_entry('%s', %zd) => need_size = %u\n", + name, buffer->size, need_size); + + // Merge empty sections. + DEBUG("(trying to merge empty sections...)\n"); + cbfs_walk(image, cbfs_merge_empty_entry, NULL); + + for (entry = cbfs_find_first_entry(image); + entry && cbfs_is_valid_entry(entry); + entry = cbfs_find_next_entry(image, entry)) { + + entry_type = ntohl(entry->type); + if (entry_type != CBFS_COMPONENT_DELETED && + entry_type != CBFS_COMPONENT_NULL) continue;
- /* Mark the file as free space and erase its name. */ - cur->type = CBFS_COMPONENT_NULL; - name[0] = '\0'; - - /* Merge it with the previous file if possible. */ - if (prev && prev->type == CBFS_COMPONENT_NULL) - cur = merge_adjacent_files(prev, cur); - - /* Merge it with the next file if possible. */ - if (next && next->type == CBFS_COMPONENT_NULL) - merge_adjacent_files(cur, next); + addr = cbfs_get_entry_addr(image, entry); + next = cbfs_find_next_entry(image, entry); + entry_capacity = cbfs_get_entry_addr(image, next) - addr; + DEBUG("cbfs_add_entry: space %s at 0x%x, %d bytes\n", + get_cbfs_entry_type_name(entry_type), addr, + entry_capacity); + if (entry_capacity < need_size) + continue;
+ // TODO check if we want to put blob in particular address + // (baseaddress). + + DEBUG("Filling new entry data (%zd bytes).\n", buffer->size); + cbfs_create_entry(image, entry, buffer->size, name); + entry->type = htonl(type); + memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size); + if (verbose) + cbfs_print_entry_info(image, entry, NULL); + + // setup new entry + entry = cbfs_find_next_entry(image, entry); + new_size = (cbfs_get_entry_addr(image, next) - + cbfs_get_entry_addr(image, entry)); + new_size -= sizeof(*entry) + CBFS_FILENAME_ALIGN; + cbfs_create_entry(image, entry, new_size, ""); + if (verbose) + cbfs_print_entry_info(image, entry, NULL); return 0; } - fprintf(stderr, "E: CBFS file %s not found.\n", filename); - return 1; + + ERROR("Could not add [%s, %zd bytes (%zd KB)]; probably too big?\n", + buffer->name, buffer->size, buffer->size / 1024); + return 0; }
+int cbfs_remove_entry(struct cbfs_image *image, const char *name) { + struct cbfs_file *entry, *next; + size_t len; + entry = cbfs_get_entry(image, name); + if (!entry) { + ERROR("CBFS file %s not found.\n", name); + return -1; + } + next = cbfs_find_next_entry(image, entry); + assert(next); + DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", + CBFS_NAME(entry), cbfs_get_entry_addr(image, entry)); + entry->type = htonl(CBFS_COMPONENT_DELETED); + len = (cbfs_get_entry_addr(image, next) - + cbfs_get_entry_addr(image, entry)); + entry->offset = htonl(sizeof(*entry) + CBFS_FILENAME_ALIGN); + entry->len = htonl(len - ntohl(entry->offset)); + memset(CBFS_NAME(entry), 0, CBFS_FILENAME_ALIGN); + memset(CBFS_SUBHEADER(entry), -1, ntohl(entry->len)); + return 0; +}
-/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns - the new location that points to the cbfs_file header */ -void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, - uint32_t type, uint32_t * location) -{ - uint32_t filename_len = ALIGN(strlen(filename) + 1, 16); - uint32_t headersize = sizeof(struct cbfs_file) + filename_len; - if ((location != 0) && (*location != 0)) { - uint32_t offset = *location % align; - /* If offset >= (headersize % align), we can stuff the header into the offset. - Otherwise the header has to be aligned itself, and put before the offset data */ - if (offset >= (headersize % align)) { - offset -= (headersize % align); - } else { - offset += align - (headersize % align); +struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) { + struct cbfs_file *entry; + for (entry = cbfs_find_first_entry(image); + entry && cbfs_is_valid_entry(entry); + entry = cbfs_find_next_entry(image, entry)) { + if (strcmp(CBFS_NAME(entry), name) == 0) { + DEBUG("cbfs_get_entry: found %s\n", name); + return entry; } - headersize += offset; - *location -= headersize; } - void *newdata = malloc(*datasize + headersize); - if (!newdata) { - fprintf(stderr, "E: Could not get %d bytes for CBFS file.\n", *datasize + - headersize); - exit(1); - } - memset(newdata, 0xff, *datasize + headersize); - struct cbfs_file *nextfile = (struct cbfs_file *)newdata; - strncpy((char *)(nextfile->magic), "LARCHIVE", 8); - nextfile->len = htonl(*datasize); - nextfile->type = htonl(type); - nextfile->checksum = 0; // FIXME? - nextfile->offset = htonl(headersize); - strcpy(newdata + sizeof(struct cbfs_file), filename); - memcpy(newdata + headersize, data, *datasize); - *datasize += headersize; - return newdata; -} - -int create_cbfs_image(const char *romfile, uint32_t _romsize, - const char *bootblock, uint32_t align, uint32_t offs) -{ - uint32_t bootblocksize = 0; - struct cbfs_header *master_header; - unsigned char *romarea, *bootblk; - - romsize = _romsize; - romarea = malloc(romsize); - if (!romarea) { - fprintf(stderr, "E: Could not get %d bytes of memory" - " for CBFS image.\n", romsize); - exit(1); + return NULL; +} + +int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, + const char *filename) { + struct cbfs_file *entry = cbfs_get_entry(image, entry_name); + struct buffer buffer; + if (!entry) { + ERROR("File not found: %s\n", entry_name); + return -1; } - memset(romarea, 0xff, romsize); + LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", + entry_name, cbfs_get_entry_addr(image, entry), + get_cbfs_entry_type_name(ntohl(entry->type)), + ntohl(entry->len));
- if (align == 0) - align = 64; - - bootblk = loadfile(bootblock, &bootblocksize, - romarea + romsize, SEEK_END); - if (!bootblk) { - fprintf(stderr, "E: Could not load bootblock %s.\n", - bootblock); - free(romarea); - return 1; + if (ntohl(entry->type) != CBFS_COMPONENT_RAW) { + WARN("Only 'raw' files are safe to extract.\n"); }
- // TODO(hungte) Replace magic numbers by named constants. - switch (arch) { - case CBFS_ARCHITECTURE_ARMV7: - /* Set up physical/virtual mapping */ - offset = romarea; - - /* - * The initial jump instruction and bootblock will be placed - * before and after the master header, respectively. The - * bootblock image must contain a blank, aligned region large - * enough for the master header to fit. - * - * An anchor string must be left such that when cbfstool is run - * we can find it and insert the master header at the next - * aligned boundary. - */ - loadfile(bootblock, &bootblocksize, romarea + offs, SEEK_SET); - - unsigned char *p = romarea + offs; - while (1) { - /* FIXME: assumes little endian... */ - if (*(uint32_t *)p == 0xdeadbeef) - break; - if (p >= (romarea + _romsize)) { - fprintf(stderr, "E: Could not determine CBFS " - "header location.\n", bootblock); - return 1; - } - p += (sizeof(unsigned int)); - } - unsigned int u = ALIGN((unsigned int)(p - romarea), align); - master_header = (struct cbfs_header *)(romarea + u); - - master_header->magic = ntohl(CBFS_HEADER_MAGIC); - master_header->version = ntohl(CBFS_HEADER_VERSION); - master_header->romsize = htonl(romsize); - master_header->bootblocksize = htonl(bootblocksize); - master_header->align = htonl(align); - master_header->offset = htonl( - ALIGN((0x40 + bootblocksize), align)); - master_header->architecture = htonl(CBFS_ARCHITECTURE_ARMV7); - - ((uint32_t *) phys_to_virt(0x4 + offs))[0] = - virt_to_phys(master_header); - - recalculate_rom_geometry(romarea); - - cbfs_create_empty_file( - offs + ALIGN((0x40 + bootblocksize), align), - romsize - offs - sizeof(struct cbfs_file) - - ALIGN((bootblocksize + 0x40), align)); - break; - - case CBFS_ARCHITECTURE_X86: - // Set up physical/virtual mapping - offset = romarea + romsize - 0x100000000ULL; - - loadfile(bootblock, &bootblocksize, romarea + romsize, - SEEK_END); - master_header = (struct cbfs_header *)(romarea + romsize - - bootblocksize - sizeof(struct cbfs_header)); - - master_header->magic = ntohl(CBFS_HEADER_MAGIC); - master_header->version = ntohl(CBFS_HEADER_VERSION); - master_header->romsize = htonl(romsize); - master_header->bootblocksize = htonl(bootblocksize); - master_header->align = htonl(align); - master_header->offset = htonl(offs); - master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); - - ((uint32_t *) phys_to_virt(CBFS_HEADPTR_ADDR_X86))[0] = - virt_to_phys(master_header); - - recalculate_rom_geometry(romarea); - - cbfs_create_empty_file((0 - romsize + offs) & 0xffffffff, - romsize - offs - bootblocksize - - sizeof(struct cbfs_header) - - sizeof(struct cbfs_file) - 16); - break; - - default: - // Should not happen. - fprintf(stderr, "E: You found a bug in cbfstool.\n"); - exit(1); + buffer.data = CBFS_SUBHEADER(entry); + buffer.size = ntohl(entry->len); + buffer.name = "(cbfs_export_entry)"; + if (buffer_write_file(&buffer, filename) != 0) { + ERROR("Failed to write %s into %s.\n", + entry_name, filename); + return -1; } + printf("Successfully dumped the file to: %s\n", filename); + return 0; +} + +/* basename(3) may modify buffer, so we want a tiny alternative. */ +static const char *simple_basename(const char *name) { + const char *slash = strrchr(name, '/'); + if (slash) + return slash + 1; + else + return name; +}
- writerom(romfile, romarea, romsize); - free(romarea); +int cbfs_print_header_info(struct cbfs_image *image) { + assert(image && image->header); + printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" + "alignment: %d bytes\n\n", + simple_basename(image->buffer.name), + image->buffer.size / 1024, + ntohl(image->header->bootblocksize), + ntohl(image->header->romsize), + ntohl(image->header->offset), + ntohl(image->header->align)); return 0; }
-static int in_segment(int addr, int size, int gran) -{ - return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); +static int cbfs_print_stage_info(struct cbfs_stage *stage) { + printf(" %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " + "length: %d/%d\n", + stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", + stage->entry, + stage->load, + stage->len, + stage->memlen); + return 0; }
-uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, - const char *filename, uint32_t alignment) +static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload) { - void *rom; - size_t filename_size, headersize, totalsize; - int ret = 0; - uint32_t current; - - rom = loadrom(romfile); - if (rom == NULL) { - fprintf(stderr, "E: Could not load ROM image '%s'.\n", - romfile); + switch(payload->type) { + case PAYLOAD_SEGMENT_CODE: + case PAYLOAD_SEGMENT_DATA: + printf(" %s (%s compression, offset: 0x%x, " + "load: 0x%" PRIx64 ", length: %d/%d)\n", + (payload->type == PAYLOAD_SEGMENT_CODE ? + "code " : "data"), + (payload->compression == CBFS_COMPRESS_LZMA ? + "LZMA" : "no"), + ntohl(payload->offset), + ntohll(payload->load_addr), + ntohl(payload->len), ntohl(payload->mem_len)); + break; + + case PAYLOAD_SEGMENT_ENTRY: + printf(" entry (0x%" PRIx64 ")\n", + ntohll(payload->load_addr)); + break; + + case PAYLOAD_SEGMENT_BSS: + printf(" BSS (address 0x%016" PRIx64 ", " + "length 0x%x)\n", + ntohll(payload->load_addr), + ntohl(payload->len)); + break; + + case PAYLOAD_SEGMENT_PARAMS: + printf(" parameters\n"); + break; + + default: + printf(" %x (%s compression, offset: 0x%x, " + "load: 0x%" PRIx64 ", length: %d/%d\n", + payload->type, + (payload->compression == CBFS_COMPRESS_LZMA ? + "LZMA" : "no"), + ntohl(payload->offset), + ntohll(payload->load_addr), + ntohl(payload->len), + ntohl(payload->mem_len)); + break; + } + return 0; +} + +int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, + void *arg) { + const char *name = CBFS_NAME(entry); + struct cbfs_payload_segment *payload; + int *verbose = (int*)arg; + if (!cbfs_is_valid_entry(entry)) { + ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", + cbfs_get_entry_addr(image, entry)); + return -1; + } + + printf("%-30s 0x%-8x %-12s %d\n", + *name ? name : "(empty)", + cbfs_get_entry_addr(image, entry), + get_cbfs_entry_type_name(ntohl(entry->type)), + ntohl(entry->len)); + + if (!(verbose && *verbose)) return 0; + + /* note the components of the subheader may be in host order ... */ + switch (ntohl(entry->type)) { + case CBFS_COMPONENT_STAGE: + cbfs_print_stage_info((struct cbfs_stage*) + CBFS_SUBHEADER(entry)); + break; + + case CBFS_COMPONENT_PAYLOAD: + payload = (struct cbfs_payload_segment*) + CBFS_SUBHEADER(entry); + while (payload) { + cbfs_print_payload_segment_info(payload); + if (payload->type == PAYLOAD_SEGMENT_ENTRY) + break; + else + payload ++; + } + break; + default: + break; } + return 0; +}
- filename_size = strlen(filename); - headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) + - sizeof(struct cbfs_stage); - totalsize = headersize + filesize; +int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, + void *arg) { + struct cbfs_file *next, *last; + uint32_t type; + size_t newlen; + type = ntohl(entry->type); + if (type != CBFS_COMPONENT_NULL && type != CBFS_COMPONENT_DELETED) + return 0; + next = cbfs_find_next_entry(image, entry); + if (!next || !cbfs_is_valid_entry(next)) + return -1; + last = cbfs_find_next_entry(image, next); + assert(last); + type = ntohl(next->type); + if (type != CBFS_COMPONENT_NULL && type != CBFS_COMPONENT_DELETED) + return 0; + + // Now, we find two deleted/empty sections; try to merge now. + DEBUG("cbfs_join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", + cbfs_get_entry_addr(image, entry), ntohl(entry->len), + cbfs_get_entry_addr(image, next), ntohl(next->len)); + entry->type = htonl(CBFS_COMPONENT_NULL); + newlen = (cbfs_get_entry_addr(image, last) - + cbfs_get_entry_addr(image, entry) - ntohl(entry->offset)); + entry->len = htonl(newlen); + DEBUG("new empty entry: length=0x%zx\n", newlen); + // TODO clear name + memset(CBFS_SUBHEADER(entry), -1, newlen); + // TODO we should try again until no more sections are found... + return 0; +}
- current = phys_start; - while (current < phys_end) { - uint32_t top; - struct cbfs_file *thisfile; +int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, + void *arg) { + int count = 0; + struct cbfs_file *entry; + for (entry = cbfs_find_first_entry(image); + entry && cbfs_is_valid_entry(entry); + entry = cbfs_find_next_entry(image, entry)) { + count ++; + if (callback(image, entry, arg) != 0) + break; + } + return count; +}
- if (!cbfs_file_header(current)) { - current += align; +struct cbfs_header *cbfs_find_header(char *data, size_t size) { + size_t offset; + int found = 0; + uint32_t x86sig; + struct cbfs_header *header, *result = NULL; + + // Try x86 style (check signature in bottom) header first. + x86sig = *(uint32_t*)(data + size - sizeof(uint32_t)); + offset = (x86sig + (uint32_t)size); + DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset); + if (offset >= size - sizeof(*header) || + ntohl(((struct cbfs_header*)(data + offset))->magic) != + CBFS_HEADER_MAGIC) + offset = 0; + + for (; offset + sizeof(*header) < size; offset++) { + header = (struct cbfs_header*)(data + offset); + if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC)) + continue; + if (ntohl(header->version) != CBFS_HEADER_VERSION1 && + ntohl(header->version) != CBFS_HEADER_VERSION2) { + // Probably not a real CBFS header? continue; } + found++; + result = header; + } + if (found > 1) { + ERROR("multiple (%d) CBFS headers found!\n", + found); + result = NULL; + } + return result; +}
- thisfile = (struct cbfs_file *)phys_to_virt(current); +struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) { + assert(image && image->header); + return (struct cbfs_file*)(image->buffer.data + + ntohl(image->header->offset)); +}
- top = current + ntohl(thisfile->len) + ntohl(thisfile->offset); +struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, + struct cbfs_file *entry) { + uint32_t addr = cbfs_get_entry_addr(image, entry); + int align = ntohl(image->header->align); + assert(entry && cbfs_is_valid_entry(entry)); + addr += ntohl(entry->offset) + ntohl(entry->len); + addr = align_up(addr, align); + return (struct cbfs_file*)(image->buffer.data + addr); +}
- if (((ntohl(thisfile->type) == 0x0) - || (ntohl(thisfile->type) == 0xffffffff)) - && (ntohl(thisfile->len) + ntohl(thisfile->offset) >= - totalsize)) { - if (in_segment - (current + headersize, filesize, alignment)) { - ret = current + headersize; - break; - } - if ((ALIGN(current, alignment) + filesize < top) - && (ALIGN(current, alignment) - headersize > - current) - && in_segment(ALIGN(current, alignment), filesize, - alignment)) { - ret = ALIGN(current, alignment); - break; - } - if ((ALIGN(current, alignment) + alignment + filesize < - top) - && (ALIGN(current, alignment) + alignment - - headersize > current) - && in_segment(ALIGN(current, alignment) + alignment, - filesize, alignment)) { - ret = ALIGN(current, alignment) + alignment; - break; - } - } - current = - ALIGN(current + ntohl(thisfile->len) + - ntohl(thisfile->offset), align); - } +uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) { + assert(image && image->buffer.data && entry); + return (int32_t)((char*)entry - image->buffer.data); +} + +int cbfs_is_valid_entry(struct cbfs_file *entry) { + return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC, + sizeof(entry->magic)) == 0); +} + +int cbfs_init_entry(struct cbfs_file *entry, + struct buffer *buffer) { + memset(entry, 0, sizeof(*entry)); + memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); + entry->len = htonl(buffer->size); + entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1); + return 0; +}
- free(rom); - return ret; +int cbfs_create_entry(struct cbfs_image *image, struct cbfs_file *entry, + size_t len, const char *name) { + memset(entry, -1, sizeof(*entry)); + memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); + entry->type = htonl(CBFS_COMPONENT_NULL); + entry->len = htonl(len); + entry->checksum = 0; // TODO Build a checksum algorithm. + entry->offset = htonl(sizeof(*entry) + align_up(strlen(name) + 1, + CBFS_FILENAME_ALIGN)); + memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry)); + strcpy(CBFS_NAME(entry), name); + memset(CBFS_SUBHEADER(entry), -1, len); + return 0; +} + +#if 0 + +static int in_segment(int addr, int size, int gran) +{ + return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); } +#endif diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index 853e529..918def6 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -2,6 +2,7 @@ * Copyright (C) 2009 coresystems GmbH * written by Patrick Georgi patrick.georgi@coresystems.de * Copyright (C) 2012 Google, Inc. + * Copyright (C) 2013 The ChromiumOS Authors. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,71 +22,170 @@ #define __CBFSTOOL_COMMON_H
#include <stdint.h> +#include "cbfs.h" + +/* Message output */ +extern int verbose; +#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); } +#define ERROR(x...) { fprintf(stderr, "ERROR: " x); } +#define WARN(x...) { fprintf(stderr, "WARN: " x); } +#define LOG(x...) { if (verbose > 0) printf(x); } +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* Endianess */ +#include <arpa/inet.h> // for ntohl, htonl #include "swab.h" -#ifndef __APPLE__ -#define ntohl(x) (host_bigendian?(x):swab32(x)) -#define htonl(x) (host_bigendian?(x):swab32(x)) -#endif -#define ntohll(x) (host_bigendian?(x):swab64(x)) -#define htonll(x) (host_bigendian?(x):swab64(x)) +int is_big_endian(void); +#define ntohll(x) (is_big_endian() ? (x) : swab64(x)) +#define htonll(x) (is_big_endian() ? (x) : swab64(x))
-extern void *offset; -extern uint32_t romsize; -extern int host_bigendian; -extern uint32_t arch; +/* Buffer and file I/O */ +struct buffer { + char *name; + char *data; + size_t size; +};
-const char *arch_to_string(uint32_t a); -uint32_t string_to_arch(const char *arch_string); +/* Creates an empty memory buffer with given size. + * Returns 0 on success, otherwise non-zero. */ +int buffer_create(struct buffer *buffer, size_t size, const char *name);
-static inline void *phys_to_virt(uint32_t addr) -{ - return offset + addr; -} +/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ +int buffer_from_file(struct buffer *buffer, const char *filename);
-static inline uint32_t virt_to_phys(void *addr) -{ - return (unsigned long)(addr - offset) & 0xffffffff; -} +/* Writes memory buffer content into file. + * Returns 0 on success, otherwise non-zero. */ +int buffer_write_file(struct buffer *buffer, const char *filename);
-#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1)) +/* Destroys a memory buffer. Returns 0 on success, otherwise non-zero. */ +int buffer_delete(struct buffer *buffer);
-size_t getfilesize(const char *filename); -void *loadfile(const char *filename, uint32_t * romsize_p, void *content, - int place); -void *loadrom(const char *filename); -int writerom(const char *filename, void *start, uint32_t size);
-int iself(unsigned char *input); +/* Type and format */
-typedef void (*comp_func_ptr) (char *, int, char *, int *); -typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo; +/* Returns 1 if input points to an ELF object, otherwise 0. */ +int is_elf_object(const void *input);
-comp_func_ptr compression_function(comp_algo algo); +/* Returns CBFS entry type value for given name, or (uint32_t)-1 as unknown. */ +uint32_t get_cbfs_entry_type(const char *name); + +/* Gets the name of given CBFS entry type value, or "(unknown)". */ +const char *get_cbfs_entry_type_name(uint32_t type); + +/* Prints all known CBFS entry types. */ +void print_all_cbfs_entry_types(); + + +/* CBFS image processing */ + +struct cbfs_image { + struct buffer buffer; + struct cbfs_header *header; +}; + +/* Creates an empty CBFS image by given size, and description to its content + * (bootblock, align, header location, starting offset of CBFS entries. + * The output image will contain a valid cbfs_header, with one cbfs_file + * entry with type CBFS_COMPONENT_NULL, with max available size. + * Returns 0 on success, otherwise none-zero. */ +int cbfs_image_create(struct cbfs_image *image, + size_t size, + uint32_t align, + struct buffer *bootblock, + int32_t bootblock_offset, + int32_t header_offset, + int32_t entries_offset); + + +/* Loads a CBFS image from file. Returns 0 on success, otherwise non-zero. */ +int cbfs_image_from_file(struct cbfs_image *image, const char *filename);
-uint64_t intfiletype(const char *name); +/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */ +int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
-int parse_elf_to_payload(unsigned char *input, unsigned char **output, - comp_algo algo); -int parse_elf_to_stage(unsigned char *input, unsigned char **output, - comp_algo algo, uint32_t * location); +/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */ +int cbfs_image_delete(struct cbfs_image *image);
-void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, - uint32_t type, uint32_t * location); +/* Adds an entry to CBFS image by given name and type. + * If data_align is specified, entry data (CBFS_SUBHEADER(entry)) must be + * aligned (usually for eXecute-In-Place). + * Returns 0 on success, otherwise non-zero. */ +int cbfs_add_entry(struct cbfs_image *image, const char *name, + uint32_t type, size_t data_align, struct buffer *buffer);
-int create_cbfs_image(const char *romfile, uint32_t romsize, - const char *bootblock, uint32_t align, uint32_t offs); +/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */ +int cbfs_remove_entry(struct cbfs_image *image, const char *name);
-int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location); -int remove_file_from_cbfs(const char *filename); -void print_cbfs_directory(const char *filename); -int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath); -int remove_file_from_cbfs(const char *filename); +/* Returns a pointer to entry by name, or NULL if name is not found. */ +struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
-uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, - const char *filename, uint32_t align); +/* Exports an entry to external file. + * Returns 0 on success, otherwise (ex, not found) non-zero. */ +int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, + const char *filename);
-void print_supported_filetypes(void); +/* Initializes a new entry with size and name in CBFS image. + * Returns 0 on success, otherwise (ex, not found) non-zero. */ +int cbfs_create_entry(struct cbfs_image *image, struct cbfs_file *entry, + size_t len, const char *name);
-#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) +/* Callback function used by cbfs_walk. + * Returns 0 on success, or non-zero to stop further iteration. */ +typedef int (*cbfs_entry_callback)(struct cbfs_image *image, + struct cbfs_file *file, + void *arg); + +/* Iterates through all entries in CBFS image, and invoke with callback. + * Stops if callback returns non-zero values. + * Returns number of entries invoked. */ +int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg); + +/* Primitive CBFS utilities */ + +/* Returns a pointer to the only valid CBFS header in give buffer, otherwise + * NULL (including when multiple headers were found). If there is a X86 ROM + * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as + * the only header.*/ +struct cbfs_header *cbfs_find_header(char *data, size_t size); + +/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if + * the entry has valid content or not), otherwise NULL. */ +struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image); + +/* Returns next cbfs_file entry (no matter if its content is valid or not), or + * NULL on failure. */ +struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, + struct cbfs_file *entry); + +/* Returns ROM address (offset) of entry. + * This is different from entry->offset (pointer to content). */ +uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry); + +int cbfs_print_header_info(struct cbfs_image *image); +int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, + void *arg); +int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, + void *arg); + +/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */ +int cbfs_is_valid_entry(struct cbfs_file *entry); + +/* Utilities */ + +/* compress.c */ +typedef void (*comp_func_ptr) (char *, int, char *, int *); +typedef enum { + CBFS_COMPRESS_NONE = 0, + CBFS_COMPRESS_LZMA +} comp_algo; +comp_func_ptr compression_function(comp_algo algo);
+/* cbfs-mkpayload.c */ +int parse_elf_to_payload(char *input, char **output, comp_algo algo); +int parse_flat_binary_to_payload(char *input, char **output, + uint32_t input_size, uint32_t loadaddress, + uint32_t entrypoint, comp_algo algo); +/* cbfs-mkstage.c */ +int parse_elf_to_stage(char *input, char **output, comp_algo algo, + uint32_t location); #endif