aboutsummaryrefslogtreecommitdiff
path: root/util/cbfstool/cbfstool.c
blob: cbf448163f3273dd0256792b479a63ad91b33af9 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * cbfstool
 *
 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
 *
 * 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
 */

/* v2 compat: First, assumes a 64K bootblock. 
 * cbfstool coreboot.rom create 0x80000 coreboot.strip 
 * cbfstool coreboot.rom add-payload /tmp/filo.elf payload 
 * cbfstool coreboot.rom extract new_filo.elf payload 
 * cbfstool coreboot.rom print
 */


#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/mman.h>
#include "cbfstool.h"

extern int create_handler(struct rom *, int, char **);
extern int bootblock_handler(struct rom *, int, char **);
extern int print_handler(struct rom *, int, char **);
extern int add_handler(struct rom *, int, char **);
extern int extract_handler(struct rom *, int, char **);
extern int delete_handler(struct rom *, int, char **);
extern int resize_handler(struct rom *, int, char **);
extern int add_payload_handler(struct rom *, int, char **);
extern int add_stage_handler(struct rom *, int, char **);

extern void create_usage(void);
extern void bootblock_usage(void);
extern void print_usage(void);
extern void add_usage(void);
extern void delete_usage(void);
extern void extract_usage(void);
extern void resize_usage(void);
extern void add_payload_usage(void);
extern void add_stage_usage(void);

struct {
	char *command;
	int (*handler) (struct rom *, int, char **);
	void (*help) (void);
} commands[] = {
	{
	"add", add_handler, add_usage}, {
	"add-payload", add_payload_handler, add_payload_usage}, {
	"add-stage", add_stage_handler, add_stage_usage}, {
	"bootblock", bootblock_handler, bootblock_usage}, {
	"create", create_handler, create_usage}, {
	"delete", delete_handler, delete_usage}, {
	"extract", extract_handler, extract_usage}, {
	"print", print_handler, print_usage}, {
	"resize", resize_handler, resize_usage}, {
"", NULL},};

static struct rom rom;

char cbfstool_bindir[255];

void show_help(void)
{
	int i;

	printf("cbfstool [OPTION] [[FILE] [COMMAND] [PARAMETERS]...\n");
	printf("Apply COMMANDS with PARAMETERS to FILE.  If no COMMAND is\n");
	printf("given, run in interactive mode\n\n");
	printf("OPTIONs:\n");
	printf(" -h\t\tDisplay this help message\n");
	printf(" -C <dir>\tChange to the directory before operating\n\n");
	printf("COMMANDs:\n");

	for (i = 0; commands[i].handler != NULL; i++)
		commands[i].help();
}

int main(int argc, char **argv)
{
	char *cdir = NULL;
	char *rname;
	char *cmd;
	int ret = -1, i;

	strncpy(cbfstool_bindir, dirname(argv[0]), 254);

	while (1) {
		signed ch = getopt(argc, argv, "hC:");
		if (ch == -1)
			break;
		switch (ch) {
		case 'h':
			show_help();
			return -1;
		case 'C':
			cdir = optarg;
			break;
		}
	}

	if (optind >= argc) {
		show_help();
		return -1;
	}

	if (cdir != NULL && chdir(cdir)) {
		ERROR("Unable to switch to %s: %m\n", cdir);
		return -1;
	}

	rname = argv[optind];
	cmd = optind + 1 < argc ? argv[optind + 1] : NULL;

	/* Open the ROM (if it exists) */
	rom.name = (unsigned char *)strdup(rname);

	if (!access(rname, F_OK)) {
		if (open_rom(&rom, rname)) {
			ERROR("Problem while reading the ROM\n");
			return -1;
		}
	}

	if (cmd) {
		/* Process the incoming comand */

		for (i = 0; commands[i].handler != NULL; i++) {
			if (!strcmp(commands[i].command, cmd)) {
				ret = commands[i].handler(&rom,
							  argc - 3, &argv[3]);
				goto leave;
			}
		}

		ERROR("Command %s not valid\n", cmd);
	} else {
		printf("Interactive mode not ready yet!\n");
	}

leave:
	if (rom.ptr != NULL && rom.ptr != MAP_FAILED)
		munmap(rom.ptr, rom.size);

	if (rom.fd > 0)
		close(rom.fd);

	return ret;
}