aboutsummaryrefslogtreecommitdiff
path: root/util/cbfstool/cbfstool.c
blob: 637bddb1fcb26af06369b0c9101b07a2b0af9c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * cbfstool, CLI utility for CBFS file manipulation
 *
 * Copyright (C) 2009 coresystems GmbH
 *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
 *
 * 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
 */

#include <stdio.h>
#include <stdint.h>
#include "common.h"
#include "cbfs.h"

int main(int argc, char **argv)
{
	if (argc < 3) {
		printf
		    ("cbfstool: Management utility for CBFS formatted ROM images\n"
		     "USAGE:\n" "cbfstool [-h]\n"
		     "cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
		     " -h		Display this help message\n\n"
		     "COMMANDs:\n"
		     "add FILE NAME TYPE [base address]    Add a component\n"
		     "add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
		     "add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
		     "create SIZE BSIZE BOOTBLOCK [ALIGN]  Create a ROM file\n"
		     "print                                Show the contents of the ROM\n");
		return 1;
	}
	char *romname = argv[1];
	char *cmd = argv[2];

	if (strcmp(cmd, "create") == 0) {
		if (argc < 6) {
			printf("not enough arguments to 'create'.\n");
			return 1;
		}
		uint32_t size = strtoul(argv[3], NULL, 0);
		/* ignore bootblock size. we use whatever we get and won't allocate any larger */
		char *bootblock = argv[5];
		uint32_t align = 0;
		if (argc > 6)
			align = strtoul(argv[6], NULL, 0);
		return create_cbfs_image(romname, size, bootblock, align);
	}

	void *rom = loadrom(romname);

	if (strcmp(cmd, "print") == 0) {
		print_cbfs_directory(romname);
		return 0;
	}

	if (argc < 5) {
		printf("not enough arguments to '%s'.\n", cmd);
		return 1;
	}

	void *filename = argv[3];
	void *cbfsname = argv[4];

	uint32_t filesize = 0;
	void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);

	uint32_t base = 0;
	void *cbfsfile;

	if (strcmp(cmd, "add") == 0) {
		if (argc < 6) {
			printf("not enough arguments to 'add'.\n");
			return 1;
		}
		uint32_t type;
		if (intfiletype(argv[5]) != ((uint64_t) - 1))
			type = intfiletype(argv[5]);
		else
			type = strtoul(argv[5], NULL, 0);
		if (argc > 6) {
			base = strtoul(argv[6], NULL, 0);
		}
		cbfsfile =
		    create_cbfs_file(cbfsname, filedata, &filesize, type,
				     &base);
	}

	if (strcmp(cmd, "add-payload") == 0) {
		comp_algo algo = CBFS_COMPRESS_NONE;
		if (argc > 5) {
			if (argv[5][0] == 'l')
				algo = CBFS_COMPRESS_LZMA;
		}
		if (argc > 6) {
			base = strtoul(argv[6], NULL, 0);
		}
		unsigned char *payload;
		filesize = parse_elf_to_payload(filedata, &payload, algo);
		cbfsfile =
		    create_cbfs_file(cbfsname, payload, &filesize,
				     CBFS_COMPONENT_PAYLOAD, &base);
	}

	if (strcmp(cmd, "add-stage") == 0) {
		comp_algo algo = CBFS_COMPRESS_NONE;
		if (argc > 5) {
			if (argv[5][0] == 'l')
				algo = CBFS_COMPRESS_LZMA;
		}
		if (argc > 6) {
			base = strtoul(argv[6], NULL, 0);
		}
		unsigned char *stage;
		filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
		cbfsfile =
		    create_cbfs_file(cbfsname, stage, &filesize,
				     CBFS_COMPONENT_STAGE, &base);
	}

	add_file_to_cbfs(cbfsfile, filesize, base);
	writerom(romname, rom, romsize);
	return 0;
}