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"
|
2013-01-28 19:15:49 +01:00
|
|
|
#include "cbfs_image.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;
|
2013-01-28 07:23:49 +01:00
|
|
|
static struct param {
|
|
|
|
char *cbfs_name;
|
|
|
|
char *name;
|
|
|
|
char *filename;
|
|
|
|
char *bootblock;
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t baseaddress;
|
|
|
|
uint32_t loadaddress;
|
|
|
|
uint32_t entrypoint;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t alignment;
|
|
|
|
uint32_t offset;
|
|
|
|
comp_algo algo;
|
|
|
|
} param = {
|
|
|
|
/* All variables not listed are initialized as zero. */
|
|
|
|
.algo = CBFS_COMPRESS_NONE,
|
|
|
|
};
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -f/--filename.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (param.type == 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify a valid -t/--type.\n");
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
rom = loadrom(param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
|
2009-09-22 17:58:19 +02:00
|
|
|
if (filedata == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load file '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
cbfsfile = create_cbfs_file(param.name, filedata, &filesize,
|
|
|
|
param.type, ¶m.baseaddress);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(filedata);
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Adding file '%s' failed.\n", param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 07:23:49 +01:00
|
|
|
if (writerom(param.cbfs_name, rom, romsize)) {
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
{
|
2013-01-28 07:23:49 +01:00
|
|
|
uint32_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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -n/--name.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
rom = loadrom(param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
|
2009-09-22 17:58:19 +02:00
|
|
|
if (filedata == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load file '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(rom);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filesize = parse_elf_to_payload(filedata, &payload, param.algo);
|
2012-12-12 02:35:30 +01:00
|
|
|
if (filesize <= 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Adding payload '%s' failed.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-12-12 02:35:30 +01:00
|
|
|
free(rom);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
cbfsfile = create_cbfs_file(param.name, payload, &filesize,
|
|
|
|
CBFS_COMPONENT_PAYLOAD, ¶m.baseaddress);
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
free(filedata);
|
|
|
|
free(payload);
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Adding payload '%s' failed.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (writerom(param.cbfs_name, rom, romsize)) {
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
rom = loadrom(param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
|
2009-09-15 10:21:46 +02:00
|
|
|
if (filedata == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load file '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(rom);
|
2009-09-15 10:21:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filesize = parse_elf_to_stage(filedata, &stage, param.algo, ¶m.baseaddress);
|
2009-09-22 17:58:19 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
cbfsfile = create_cbfs_file(param.name, stage, &filesize,
|
|
|
|
CBFS_COMPONENT_STAGE, ¶m.baseaddress);
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
free(filedata);
|
|
|
|
free(stage);
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Adding stage '%s' failed.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (writerom(param.cbfs_name, rom, romsize)) {
|
2012-10-30 00:52:36 +01:00
|
|
|
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;
|
|
|
|
void *rom, *filedata, *cbfsfile;
|
|
|
|
unsigned char *payload;
|
2012-10-23 01:04:13 +02:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -f/--filename.\n");
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -n/--name.\n");
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (param.loadaddress == 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify a valid "
|
2012-10-30 00:52:36 +01:00
|
|
|
"-l/--load-address.\n");
|
|
|
|
return 1;
|
2012-10-23 01:04:13 +02:00
|
|
|
}
|
2012-10-30 00:52:36 +01:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (param.entrypoint == 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
rom = loadrom(param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
if (rom == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
|
2012-10-23 01:04:13 +02:00
|
|
|
if (filedata == NULL) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load file '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 08:04:30 +01:00
|
|
|
filesize = parse_flat_binary_to_payload(filedata, &payload,
|
|
|
|
filesize,
|
|
|
|
param.loadaddress,
|
|
|
|
param.entrypoint,
|
|
|
|
param.algo);
|
|
|
|
free(filedata);
|
|
|
|
|
|
|
|
if ((int)filesize <= 0) {
|
|
|
|
ERROR("Adding payload '%s' failed.\n",
|
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 08:04:30 +01:00
|
|
|
cbfsfile = create_cbfs_file(param.name, payload, &filesize,
|
|
|
|
CBFS_COMPONENT_PAYLOAD, ¶m.baseaddress);
|
2012-10-23 01:04:13 +02:00
|
|
|
|
2012-10-30 00:52:36 +01:00
|
|
|
free(payload);
|
2013-01-28 08:04:30 +01:00
|
|
|
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Adding payload '%s' failed.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
free(cbfsfile);
|
|
|
|
free(rom);
|
2012-10-23 01:04:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 07:23:49 +01:00
|
|
|
if (writerom(param.cbfs_name, rom, romsize)) {
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
{
|
2013-01-28 19:38:40 +01:00
|
|
|
struct cbfs_image image;
|
2012-01-27 09:33:47 +01:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -n/--name.\n");
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:38:40 +01:00
|
|
|
if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:38:40 +01:00
|
|
|
if (cbfs_remove_entry(&image, param.name) != 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Removing file '%s' failed.\n",
|
2013-01-28 19:38:40 +01:00
|
|
|
param.name);
|
|
|
|
cbfs_image_delete(&image);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 19:38:40 +01:00
|
|
|
if (cbfs_image_write_file(&image, param.cbfs_name) != 0) {
|
|
|
|
cbfs_image_delete(&image);
|
2012-01-27 09:33:47 +01:00
|
|
|
return 1;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-28 19:38:40 +01:00
|
|
|
cbfs_image_delete(&image);
|
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
|
|
|
{
|
2013-01-28 07:23:49 +01:00
|
|
|
if (param.size == 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify a valid -s/--size.\n");
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.bootblock) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -m/--machine arch\n");
|
2012-11-16 23:48:22 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
return create_cbfs_image(param.cbfs_name, param.size, param.bootblock,
|
|
|
|
param.alignment, param.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;
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -f/--filename.\n");
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -n/--name.\n");
|
2009-11-09 18:18:02 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
filesize = getfilesize(param.filename);
|
2012-10-30 00:52:36 +01:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
location = cbfs_find_location(param.cbfs_name, filesize,
|
|
|
|
param.name, param.alignment);
|
2009-11-09 18:18:02 +01:00
|
|
|
|
2013-01-28 16:00:47 +01:00
|
|
|
printf("0x%x\n", location);
|
2012-02-17 14:44:09 +01:00
|
|
|
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
|
|
|
{
|
2013-01-28 19:15:49 +01:00
|
|
|
struct cbfs_image image;
|
|
|
|
if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2009-09-22 17:58:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 19:15:49 +01:00
|
|
|
cbfs_print_directory(&image);
|
|
|
|
cbfs_image_delete(&image);
|
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
|
|
|
{
|
2013-01-28 19:29:49 +01:00
|
|
|
int result = 0;
|
|
|
|
struct cbfs_image image;
|
2011-01-13 10:09:21 +01:00
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.filename) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -f/--filename.\n");
|
2012-10-30 00:52:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
if (!param.name) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("You need to specify -n/--name.\n");
|
2011-01-13 10:09:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:29:49 +01:00
|
|
|
if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("Could not load ROM image '%s'.\n",
|
2013-01-28 07:23:49 +01:00
|
|
|
param.cbfs_name);
|
2013-01-28 19:29:49 +01:00
|
|
|
result = 1;
|
|
|
|
} else if (cbfs_export_entry(&image, param.name,
|
|
|
|
param.filename) != 0) {
|
|
|
|
result = 1;
|
2011-01-13 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2013-01-28 19:29:49 +01:00
|
|
|
cbfs_image_delete(&image);
|
|
|
|
return result;
|
2011-01-13 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2011-10-21 23:24:57 +02:00
|
|
|
static const struct command commands[] = {
|
2013-01-28 07:39:43 +01:00
|
|
|
{"add", "f:n:t:b:vh?", cbfs_add},
|
|
|
|
{"add-payload", "f:n:t:c:b:vh?", cbfs_add_payload},
|
|
|
|
{"add-stage", "f:n:t:c:b:vh?", cbfs_add_stage},
|
|
|
|
{"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary},
|
|
|
|
{"remove", "n:vh?", cbfs_remove},
|
|
|
|
{"create", "s:B:a:o:m:vh?", cbfs_create},
|
|
|
|
{"locate", "f:n:a:vh?", cbfs_locate},
|
|
|
|
{"print", "vh?", cbfs_print},
|
|
|
|
{"extract", "n:f:vh?", cbfs_extract},
|
2012-10-30 00:52:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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"
|
2013-01-28 07:39:43 +01:00
|
|
|
" %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
|
|
|
|
" -v Provide verbose output\n"
|
|
|
|
" -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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-28 07:23:49 +01:00
|
|
|
param.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 */
|
2013-01-08 03:10:16 +01:00
|
|
|
if (strchr(commands[i].optstring, c) == NULL) {
|
2012-10-30 00:52:36 +01:00
|
|
|
/* TODO maybe print actual long option instead */
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("%s: invalid option -- '%c'\n",
|
|
|
|
argv[0], c);
|
2012-10-30 00:52:36 +01:00
|
|
|
c = '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case 'n':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.name = optarg;
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (intfiletype(optarg) != ((uint64_t) - 1))
|
2013-01-28 07:23:49 +01:00
|
|
|
param.type = intfiletype(optarg);
|
2012-10-30 00:52:36 +01:00
|
|
|
else
|
2013-01-28 07:23:49 +01:00
|
|
|
param.type = strtoul(optarg, NULL, 0);
|
|
|
|
if (param.type == 0)
|
2013-01-28 07:39:43 +01:00
|
|
|
WARN("Unknown type '%s' ignored\n",
|
2012-10-30 00:52:36 +01:00
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (!strncasecmp(optarg, "lzma", 5))
|
2013-01-28 07:23:49 +01:00
|
|
|
param.algo = CBFS_COMPRESS_LZMA;
|
2012-10-30 00:52:36 +01:00
|
|
|
else if (!strncasecmp(optarg, "none", 5))
|
2013-01-28 07:23:49 +01:00
|
|
|
param.algo = CBFS_COMPRESS_NONE;
|
2012-10-30 00:52:36 +01:00
|
|
|
else
|
2013-01-28 07:39:43 +01:00
|
|
|
WARN("Unknown compression '%s'"
|
|
|
|
" ignored.\n", optarg);
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 'b':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.baseaddress = strtoul(optarg, NULL, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 'l':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.loadaddress = strtoul(optarg, NULL, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'e':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.entrypoint = strtoul(optarg, NULL, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.size = strtoul(optarg, &suffix, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
if (tolower(suffix[0])=='k') {
|
2013-01-28 07:23:49 +01:00
|
|
|
param.size *= 1024;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
if (tolower(suffix[0])=='m') {
|
2013-01-28 07:23:49 +01:00
|
|
|
param.size *= 1024 * 1024;
|
2012-10-30 00:52:36 +01:00
|
|
|
}
|
|
|
|
case 'B':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.bootblock = optarg;
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 'a':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.alignment = strtoul(optarg, NULL, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 'o':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.offset = strtoul(optarg, NULL, 0);
|
2012-10-30 00:52:36 +01:00
|
|
|
break;
|
|
|
|
case 'f':
|
2013-01-28 07:23:49 +01:00
|
|
|
param.filename = optarg;
|
2012-10-30 00:52:36 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("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;
|
|
|
|
}
|