- AMD selected a couple of options that are incompatible with QEmu (and
[coreboot.git] / util / cbfstool / cbfstool.c
1 /*
2  * cbfstool
3  *
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18  */
19
20 /* v2 compat: First, assumes a 64K bootblock. 
21  * cbfstool coreboot.rom create 0x80000 coreboot.strip 
22  * cbfstool coreboot.rom add-payload /tmp/filo.elf payload 
23  * cbfstool coreboot.rom extract new_filo.elf payload 
24  * cbfstool coreboot.rom print
25  */
26
27
28 #include <string.h>
29 #include <unistd.h>
30 #include <libgen.h>
31 #include <sys/mman.h>
32 #include "cbfstool.h"
33
34 extern int create_handler(struct rom *, int, char **);
35 extern int bootblock_handler(struct rom *, int, char **);
36 extern int print_handler(struct rom *, int, char **);
37 extern int add_handler(struct rom *, int, char **);
38 extern int extract_handler(struct rom *, int, char **);
39 extern int delete_handler(struct rom *, int, char **);
40 extern int resize_handler(struct rom *, int, char **);
41 extern int add_payload_handler(struct rom *, int, char **);
42 extern int add_stage_handler(struct rom *, int, char **);
43
44 extern void create_usage(void);
45 extern void bootblock_usage(void);
46 extern void print_usage(void);
47 extern void add_usage(void);
48 extern void delete_usage(void);
49 extern void extract_usage(void);
50 extern void resize_usage(void);
51 extern void add_payload_usage(void);
52 extern void add_stage_usage(void);
53
54 struct {
55         char *command;
56         int (*handler) (struct rom *, int, char **);
57         void (*help) (void);
58 } commands[] = {
59         {
60         "add", add_handler, add_usage}, {
61         "add-payload", add_payload_handler, add_payload_usage}, {
62         "add-stage", add_stage_handler, add_stage_usage}, {
63         "bootblock", bootblock_handler, bootblock_usage}, {
64         "create", create_handler, create_usage}, {
65         "delete", delete_handler, delete_usage}, {
66         "extract", extract_handler, extract_usage}, {
67         "print", print_handler, print_usage}, {
68         "resize", resize_handler, resize_usage}, {
69 "", NULL, NULL},};
70
71 static struct rom rom;
72
73 char cbfstool_bindir[255];
74
75 void show_help(void)
76 {
77         int i;
78
79         printf("cbfstool [OPTION] [[FILE] [COMMAND] [PARAMETERS]...\n");
80         printf("Apply COMMANDS with PARAMETERS to FILE.  If no COMMAND is\n");
81         printf("given, run in interactive mode\n\n");
82         printf("OPTIONs:\n");
83         printf(" -h\t\tDisplay this help message\n");
84         printf(" -C <dir>\tChange to the directory before operating\n\n");
85         printf("COMMANDs:\n");
86
87         for (i = 0; commands[i].handler != NULL; i++)
88                 commands[i].help();
89 }
90
91 int main(int argc, char **argv)
92 {
93         char *cdir = NULL;
94         char *rname;
95         char *cmd;
96         int ret = -1, i;
97
98         strncpy(cbfstool_bindir, dirname(argv[0]), 254);
99
100         while (1) {
101                 signed ch = getopt(argc, argv, "hC:");
102                 if (ch == -1)
103                         break;
104                 switch (ch) {
105                 case 'h':
106                         show_help();
107                         return -1;
108                 case 'C':
109                         cdir = optarg;
110                         break;
111                 }
112         }
113
114         if (optind >= argc) {
115                 show_help();
116                 return -1;
117         }
118
119         if (cdir != NULL && chdir(cdir)) {
120                 ERROR("Unable to switch to %s: %m\n", cdir);
121                 return -1;
122         }
123
124         rname = argv[optind];
125         cmd = optind + 1 < argc ? argv[optind + 1] : NULL;
126
127         /* Open the ROM (if it exists) */
128         rom.name = (unsigned char *)strdup(rname);
129
130         if (!access(rname, F_OK)) {
131                 if (open_rom(&rom, rname)) {
132                         ERROR("Problem while reading the ROM\n");
133                         return -1;
134                 }
135         }
136
137         if (cmd) {
138                 /* Process the incoming comand */
139
140                 for (i = 0; commands[i].handler != NULL; i++) {
141                         if (!strcmp(commands[i].command, cmd)) {
142                                 ret = commands[i].handler(&rom,
143                                                           argc - 3, &argv[3]);
144                                 goto leave;
145                         }
146                 }
147
148                 ERROR("Command %s not valid\n", cmd);
149         } else {
150                 printf("Interactive mode not ready yet!\n");
151         }
152
153 leave:
154         if (rom.ptr != NULL && rom.ptr != MAP_FAILED)
155                 munmap(rom.ptr, rom.size);
156
157         if (rom.fd > 0)
158                 close(rom.fd);
159
160         return ret;
161 }