2009-03-31 13:57:36 +02:00
|
|
|
/*
|
2009-09-14 15:29:27 +02:00
|
|
|
* cbfstool, CLI utility for CBFS file manipulation
|
2009-03-31 13:57:36 +02:00
|
|
|
*
|
2009-09-14 15:29:27 +02:00
|
|
|
* Copyright (C) 2009 coresystems GmbH
|
|
|
|
* written by Patrick Georgi <patrick.georgi@coresystems.de>
|
2012-11-16 23:48:22 +01:00
|
|
|
* Copyright (C) 2012 Google, Inc.
|
2009-03-31 13:57:36 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
#include <stdio.h>
|
2011-10-21 23:24:57 +02:00
|
|
|
#include <stdlib.h>
|
2009-12-21 16:09:01 +01:00
|
|
|
#include <string.h>
|
2011-10-21 23:24:57 +02:00
|
|
|
#include <ctype.h>
|
2012-10-30 00:52:36 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2009-09-14 15:29:27 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "cbfs.h"
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2009-09-22 17:58:19 +02:00
|
|
|
struct command {
|
|
|
|
const char *name;
|
2012-10-30 00:52:36 +01:00
|
|
|
const char *optstring;
|
|
|
|
int (*function) (void);
|
2009-09-22 17:58:19 +02:00
|
|
|
};
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
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)
|
2009-03-31 13:57:36 +02:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
uint32_t filesize = 0;
|
|
|
|
void *rom, *filedata, *cbfsfile;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_filename) {
|
|
|
|
fprintf(stderr, "E: You need to specify -f/--filename.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_name) {
|
|
|
|
fprintf(stderr, "E: You need to specify -n/--name.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom_type == 0) {
|
|
|
|
fprintf(stderr, "E: You need to specify a valid -t/--type.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
|
2009-09-22 17:58:19 +02:00
|
|
|
if (filedata == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not load file '%s'.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
cbfsfile = create_cbfs_file(rom_name, filedata, &filesize,
|
|
|
|
rom_type, &rom_baseaddress);
|
|
|
|
free(filedata);
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
|
|
|
|
fprintf(stderr, "E: Adding file '%s' failed.\n", rom_filename);
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
if (writerom(cbfs_name, rom, romsize)) {
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-30 13:21:18 +02:00
|
|
|
return 1;
|
2011-10-14 21:44:14 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_add_payload(void)
|
2009-09-22 17:58:19 +02:00
|
|
|
{
|
2012-12-12 02:35:30 +01:00
|
|
|
int32_t filesize = 0;
|
2012-10-30 00:52:36 +01:00
|
|
|
void *rom, *filedata, *cbfsfile;
|
|
|
|
unsigned char *payload;
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_filename) {
|
|
|
|
fprintf(stderr, "E: You need to specify -f/--filename.\n");
|
2009-09-15 10:21:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2009-09-14 15:29:27 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_name) {
|
|
|
|
fprintf(stderr, "E: You need to specify -n/--name.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
|
2009-09-22 17:58:19 +02:00
|
|
|
if (filedata == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not load file '%s'.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
filesize = parse_elf_to_payload(filedata, &payload, rom_algo);
|
2012-12-12 02:35:30 +01:00
|
|
|
if (filesize <= 0) {
|
|
|
|
fprintf(stderr, "E: Adding payload '%s' failed.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(rom);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
cbfsfile = create_cbfs_file(rom_name, payload, &filesize,
|
|
|
|
CBFS_COMPONENT_PAYLOAD, &rom_baseaddress);
|
|
|
|
|
|
|
|
free(filedata);
|
|
|
|
free(payload);
|
|
|
|
|
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
|
|
|
|
fprintf(stderr, "E: Adding payload '%s' failed.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-30 13:21:18 +02:00
|
|
|
return 1;
|
2011-10-14 21:44:14 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
if (writerom(cbfs_name, rom, romsize)) {
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2010-06-24 15:37:59 +02:00
|
|
|
return 1;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_add_stage(void)
|
2009-09-22 17:58:19 +02:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
uint32_t filesize = 0;
|
|
|
|
void *rom, *filedata, *cbfsfile;
|
|
|
|
unsigned char *stage;
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_filename) {
|
|
|
|
fprintf(stderr, "E: You need to specify -f/--filename.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_name) {
|
|
|
|
fprintf(stderr, "E: You need to specify -n/--name.\n");
|
2009-09-14 15:29:27 +02:00
|
|
|
return 1;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
|
2009-09-15 10:21:46 +02:00
|
|
|
if (filedata == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not load file '%s'.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(rom);
|
2009-09-15 10:21:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
filesize = parse_elf_to_stage(filedata, &stage, rom_algo, &rom_baseaddress);
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
cbfsfile = create_cbfs_file(rom_name, stage, &filesize,
|
|
|
|
CBFS_COMPONENT_STAGE, &rom_baseaddress);
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
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);
|
2009-09-30 13:21:18 +02:00
|
|
|
return 1;
|
2011-10-14 21:44:14 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
if (writerom(cbfs_name, rom, romsize)) {
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2010-06-24 15:37:59 +02:00
|
|
|
return 1;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-14 15:29:27 +02:00
|
|
|
return 0;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_add_flat_binary(void)
|
2012-10-23 01:04:13 +02:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
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;
|
2012-10-23 01:04:13 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_filename) {
|
|
|
|
fprintf(stderr, "E: You need to specify -f/--filename.\n");
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_name) {
|
|
|
|
fprintf(stderr, "E: You need to specify -n/--name.\n");
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom_loadaddress == 0) {
|
|
|
|
fprintf(stderr, "E: You need to specify a valid "
|
|
|
|
"-l/--load-address.\n");
|
|
|
|
return 1;
|
2012-10-23 01:04:13 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
if (rom_entrypoint == 0) {
|
2012-11-30 20:21:05 +01:00
|
|
|
fprintf(stderr, "E: You need to specify a valid "
|
2012-10-30 00:52:36 +01:00
|
|
|
"-e/--entry-point.\n");
|
|
|
|
return 1;
|
2012-10-23 01:04:13 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
compress = compression_function(rom_algo);
|
2012-10-23 01:04:13 +02:00
|
|
|
if (!compress)
|
|
|
|
return 1;
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
2012-11-30 20:21:05 +01:00
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
2012-10-30 00:52:36 +01:00
|
|
|
cbfs_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
|
2012-10-23 01:04:13 +02:00
|
|
|
if (filedata == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not load file '%s'.\n",
|
|
|
|
rom_filename);
|
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
/* FIXME compressed file size might be bigger than original file */
|
2012-10-23 01:04:13 +02:00
|
|
|
payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1);
|
|
|
|
if (payload == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not allocate memory.\n");
|
|
|
|
free(filedata);
|
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
segs = (struct cbfs_payload_segment *)payload;
|
2012-10-30 00:52:36 +01:00
|
|
|
doffset = (2 * sizeof(struct cbfs_payload_segment));
|
2012-10-23 01:04:13 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
/* Prepare code segment */
|
2012-10-23 01:04:13 +02:00
|
|
|
segs[0].type = PAYLOAD_SEGMENT_CODE;
|
2012-10-30 00:52:36 +01:00
|
|
|
segs[0].load_addr = (uint64_t)htonll(rom_loadaddress);
|
2012-10-23 01:04:13 +02:00
|
|
|
segs[0].mem_len = (uint32_t)htonl(filesize);
|
2012-10-30 00:52:36 +01:00
|
|
|
segs[0].offset = (uint32_t)htonl(doffset);
|
2012-10-23 01:04:13 +02:00
|
|
|
|
|
|
|
compress(filedata, filesize, (char *)(payload + doffset), &len);
|
2012-10-30 00:52:36 +01:00
|
|
|
segs[0].compression = htonl(rom_algo);
|
2012-10-23 01:04:13 +02:00
|
|
|
segs[0].len = htonl(len);
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if ((unsigned int)len >= filesize) {
|
2012-10-23 01:04:13 +02:00
|
|
|
segs[0].compression = 0;
|
|
|
|
segs[0].len = htonl(filesize);
|
|
|
|
memcpy((char *)(payload + doffset), filedata, filesize);
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
/* prepare entry point segment */
|
2012-10-23 01:04:13 +02:00
|
|
|
segs[1].type = PAYLOAD_SEGMENT_ENTRY;
|
2012-10-30 00:52:36 +01:00
|
|
|
segs[1].load_addr = (uint64_t)htonll(rom_entrypoint);
|
2012-10-23 01:04:13 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
final_size = doffset + ntohl(segs[0].len);
|
2012-10-23 01:04:13 +02:00
|
|
|
cbfsfile =
|
2012-10-30 00:52:36 +01:00
|
|
|
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);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
if (writerom(cbfs_name, rom, romsize)) {
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_remove(void)
|
2012-01-27 09:33:47 +01:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
void *rom;
|
2012-01-27 09:33:47 +01:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_name) {
|
|
|
|
fprintf(stderr, "E: You need to specify -n/--name.\n");
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (remove_file_from_cbfs(rom_name)) {
|
|
|
|
fprintf(stderr, "E: Removing file '%s' failed.\n",
|
|
|
|
rom_name);
|
|
|
|
free(rom);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
if (writerom(cbfs_name, rom, romsize)) {
|
|
|
|
free(rom);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(rom);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_create(void)
|
2009-09-22 17:58:19 +02:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom_size == 0) {
|
2012-11-30 20:21:05 +01:00
|
|
|
fprintf(stderr, "E: You need to specify a valid -s/--size.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
if (!rom_bootblock) {
|
2012-11-30 20:21:05 +01:00
|
|
|
fprintf(stderr, "E: You need to specify -b/--bootblock.\n");
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
2009-12-21 14:50:37 +01:00
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-11-16 23:48:22 +01:00
|
|
|
if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
|
|
|
|
fprintf(stderr, "E: You need to specify -m/--machine arch\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
return create_cbfs_image(cbfs_name, rom_size, rom_bootblock,
|
|
|
|
rom_alignment, rom_offset);
|
2009-09-22 17:58:19 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_locate(void)
|
2009-11-09 18:18:02 +01:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
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");
|
2009-11-09 18:18:02 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
filesize = getfilesize(rom_filename);
|
|
|
|
|
|
|
|
location = cbfs_find_location(cbfs_name, filesize,
|
|
|
|
rom_name, rom_alignment);
|
2009-11-09 18:18:02 +01:00
|
|
|
|
2012-02-17 14:44:09 +01:00
|
|
|
printf("%x\n", location);
|
|
|
|
return location == 0 ? 1 : 0;
|
2009-11-09 18:18:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_print(void)
|
2009-09-22 17:58:19 +02:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
void *rom;
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
2009-09-22 17:58:19 +02:00
|
|
|
if (rom == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
print_cbfs_directory(cbfs_name);
|
|
|
|
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static int cbfs_extract(void)
|
2011-01-13 10:09:21 +01:00
|
|
|
{
|
2012-10-30 00:52:36 +01:00
|
|
|
void *rom;
|
|
|
|
int ret;
|
2011-01-13 10:09:21 +01:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
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");
|
2011-01-13 10:09:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
rom = loadrom(cbfs_name);
|
|
|
|
if (rom == NULL) {
|
|
|
|
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
|
|
|
|
cbfs_name);
|
2011-01-13 10:09:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
ret = extract_file_from_cbfs(cbfs_name, rom_name, rom_filename);
|
|
|
|
|
|
|
|
free(rom);
|
|
|
|
return ret;
|
2011-01-13 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2011-10-21 23:24:57 +02:00
|
|
|
static const struct command commands[] = {
|
2012-10-30 00:52:36 +01:00
|
|
|
{"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},
|
2012-11-16 23:48:22 +01:00
|
|
|
{"create", "s:B:a:o:m:h?", cbfs_create},
|
2012-10-30 00:52:36 +01:00
|
|
|
{"locate", "f:n:a:h?", cbfs_locate},
|
|
|
|
{"print", "h?", cbfs_print},
|
|
|
|
{"extract", "n:f:h?", cbfs_extract},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"name", required_argument, 0, 'n' },
|
|
|
|
{"type", required_argument, 0, 't' },
|
|
|
|
{"compression", required_argument, 0, 'c' },
|
|
|
|
{"base-address", required_argument, 0, 'b' },
|
|
|
|
{"load-address", required_argument, 0, 'l' },
|
|
|
|
{"entry-point", required_argument, 0, 'e' },
|
|
|
|
{"size", required_argument, 0, 's' },
|
|
|
|
{"bootblock", required_argument, 0, 'B' },
|
|
|
|
{"alignment", required_argument, 0, 'a' },
|
|
|
|
{"offset", required_argument, 0, 'o' },
|
|
|
|
{"file", required_argument, 0, 'f' },
|
2012-11-16 23:48:22 +01:00
|
|
|
{"arch", required_argument, 0, 'm' },
|
2012-10-30 00:52:36 +01:00
|
|
|
{"verbose", no_argument, 0, 'v' },
|
|
|
|
{"help", no_argument, 0, 'h' },
|
|
|
|
{NULL, 0, 0, 0 }
|
2009-09-22 17:58:19 +02:00
|
|
|
};
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
static void usage(char *name)
|
2009-09-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
printf
|
2010-04-24 23:24:06 +02:00
|
|
|
("cbfstool: Management utility for CBFS formatted ROM images\n\n"
|
2012-10-30 00:52:36 +01:00
|
|
|
"USAGE:\n" " %s [-h]\n"
|
|
|
|
" %s FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
|
2010-04-24 23:24:06 +02:00
|
|
|
" -h Display this help message\n\n"
|
2009-09-22 17:58:19 +02:00
|
|
|
"COMMANDs:\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" add -f FILE -n NAME -t TYPE [-b base-address] "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Add a component\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" add-payload -f FILE -n NAME [-c compression] [-b base] "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Add a payload to the ROM\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" add-stage -f FILE -n NAME [-c compression] [-b base] "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Add a stage to the ROM\n"
|
|
|
|
" add-flat-binary -f FILE -n NAME -l load-address \\\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" -e entry-point [-c compression] [-b base] "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Add a 32bit flat mode binary\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" remove -n NAME "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Remove a component\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" create -s size -B bootblock -m ARCH [-a align] [-o offset] "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Create a ROM file\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" locate -f FILE -n NAME -a align "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Find a place for a file of that size\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" print "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Show the contents of the ROM\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
" extract -n NAME -f FILE "
|
2012-10-30 00:52:36 +01:00
|
|
|
"Extracts a raw payload from ROM\n"
|
2011-01-17 06:02:09 +01:00
|
|
|
"\n"
|
2012-11-16 23:48:22 +01:00
|
|
|
"ARCHes:\n"
|
|
|
|
" armv7, x86\n"
|
2012-10-30 00:52:36 +01:00
|
|
|
"TYPEs:\n", name, name
|
|
|
|
);
|
2010-04-24 23:24:06 +02:00
|
|
|
print_supported_filetypes();
|
2009-09-22 17:58:19 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
/* Small, OS/libc independent runtime check for endianess */
|
2011-10-21 23:24:57 +02:00
|
|
|
int host_bigendian = 0;
|
|
|
|
|
|
|
|
static void which_endian(void)
|
|
|
|
{
|
2012-07-17 20:52:17 +02:00
|
|
|
static const uint32_t inttest = 0x12345678;
|
|
|
|
uint8_t inttest_lsb = *(uint8_t *)&inttest;
|
|
|
|
if (inttest_lsb == 0x12) {
|
2011-10-21 23:24:57 +02:00
|
|
|
host_bigendian = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-22 17:58:19 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-07-17 21:17:15 +02:00
|
|
|
size_t i;
|
2012-10-30 00:52:36 +01:00
|
|
|
int c;
|
2009-09-22 17:58:19 +02:00
|
|
|
|
|
|
|
if (argc < 3) {
|
2012-10-30 00:52:36 +01:00
|
|
|
usage(argv[0]);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:24:57 +02:00
|
|
|
which_endian();
|
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
cbfs_name = argv[1];
|
2009-09-22 17:58:19 +02:00
|
|
|
char *cmd = argv[2];
|
2012-10-30 00:52:36 +01:00
|
|
|
optind += 2;
|
2009-09-22 17:58:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(commands); i++) {
|
|
|
|
if (strcmp(cmd, commands[i].name) != 0)
|
|
|
|
continue;
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
char *suffix = NULL;
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, commands[i].optstring,
|
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* filter out illegal long options */
|
|
|
|
if (index(commands[i].optstring, c) == NULL) {
|
|
|
|
/* TODO maybe print actual long option instead */
|
|
|
|
printf("%s: invalid option -- '%c'\n",
|
|
|
|
argv[0], c);
|
|
|
|
c = '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case 'n':
|
|
|
|
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)
|
|
|
|
printf("W: Unknown type '%s' ignored\n",
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (!strncasecmp(optarg, "lzma", 5))
|
|
|
|
rom_algo = CBFS_COMPRESS_LZMA;
|
|
|
|
else if (!strncasecmp(optarg, "none", 5))
|
|
|
|
rom_algo = CBFS_COMPRESS_NONE;
|
|
|
|
else
|
|
|
|
printf("W: Unknown compression '%s'"
|
|
|
|
" ignored.\n", optarg);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
rom_baseaddress = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
rom_loadaddress = strtoul(optarg, NULL, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
rom_entrypoint = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
rom_size = strtoul(optarg, &suffix, 0);
|
|
|
|
if (tolower(suffix[0])=='k') {
|
|
|
|
rom_size *= 1024;
|
|
|
|
}
|
|
|
|
if (tolower(suffix[0])=='m') {
|
|
|
|
rom_size *= 1024 * 1024;
|
|
|
|
}
|
|
|
|
case 'B':
|
|
|
|
rom_bootblock = optarg;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
rom_alignment = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
rom_offset = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
rom_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2012-11-16 23:48:22 +01:00
|
|
|
case 'm':
|
|
|
|
arch = string_to_arch(optarg);
|
|
|
|
break;
|
2012-10-30 00:52:36 +01:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands[i].function();
|
2009-09-22 17:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Unknown command '%s'.\n", cmd);
|
2012-10-30 00:52:36 +01:00
|
|
|
usage(argv[0]);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|