v2/util: romfs -> cbfs rename
[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 print
24  */
25
26
27 #include <string.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include <sys/mman.h>
31 #include "cbfstool.h"
32
33 extern int create_handler(struct rom *, int, char **);
34 extern int bootblock_handler(struct rom *, int, char **);
35 extern int print_handler(struct rom *, int, char **);
36 extern int add_handler(struct rom *, int, char **);
37 extern int delete_handler(struct rom *, int, char **);
38 extern int resize_handler(struct rom *, int, char **);
39 extern int add_payload_handler(struct rom *, int, char **);
40 extern int add_stage_handler(struct rom *, int, char **);
41
42 extern void create_usage(void);
43 extern void bootblock_usage(void);
44 extern void print_usage(void);
45 extern void add_usage(void);
46 extern void delete_usage(void);
47 extern void resize_usage(void);
48 extern void add_payload_usage(void);
49 extern void add_stage_usage(void);
50
51 struct {
52         char *command;
53         int (*handler) (struct rom *, int, char **);
54         void (*help) (void);
55 } commands[] = {
56         {
57         "add", add_handler, add_usage}, {
58         "add-payload", add_payload_handler, add_payload_usage}, {
59         "add-stage", add_stage_handler, add_stage_usage}, {
60         "bootblock", bootblock_handler, bootblock_usage}, {
61         "create", create_handler, create_usage}, {
62         "delete", delete_handler, delete_usage}, {
63         "print", print_handler, print_usage}, {
64         "resize", resize_handler, resize_usage}, {
65 "", NULL},};
66
67 static struct rom rom;
68
69 char cbfstool_bindir[255];
70
71 void show_help(void)
72 {
73         int i;
74
75         printf("cbfstool [OPTION] [[FILE] [COMMAND] [PARAMETERS]...\n");
76         printf("Apply COMMANDS with PARAMETERS to FILE.  If no COMMAND is\n");
77         printf("given, run in interactive mode\n\n");
78         printf("OPTIONs:\n");
79         printf(" -h\t\tDisplay this help message\n");
80         printf(" -C <dir>\tChange to the directory before operating\n\n");
81         printf("COMMANDs:\n");
82
83         for (i = 0; commands[i].handler != NULL; i++)
84                 commands[i].help();
85 }
86
87 int main(int argc, char **argv)
88 {
89         char *cdir = NULL;
90         char *rname;
91         char *cmd;
92         int ret = -1, i;
93
94         strncpy(cbfstool_bindir, dirname(argv[0]), 254);
95
96         while (1) {
97                 signed ch = getopt(argc, argv, "hC:");
98                 if (ch == -1)
99                         break;
100                 switch (ch) {
101                 case 'h':
102                         show_help();
103                         return -1;
104                 case 'C':
105                         cdir = optarg;
106                         break;
107                 }
108         }
109
110         if (optind >= argc) {
111                 show_help();
112                 return -1;
113         }
114
115         if (cdir != NULL && chdir(cdir)) {
116                 ERROR("Unable to switch to %s: %m\n", cdir);
117                 return -1;
118         }
119
120         rname = argv[optind];
121         cmd = optind + 1 < argc ? argv[optind + 1] : NULL;
122
123         /* Open the ROM (if it exists) */
124         rom.name = (unsigned char *)strdup(rname);
125
126         if (!access(rname, F_OK)) {
127                 if (open_rom(&rom, rname)) {
128                         ERROR("Problem while reading the ROM\n");
129                         return -1;
130                 }
131         }
132
133         if (cmd) {
134                 /* Process the incoming comand */
135
136                 for (i = 0; commands[i].handler != NULL; i++) {
137                         if (!strcmp(commands[i].command, cmd)) {
138                                 ret = commands[i].handler(&rom,
139                                                           argc - 3, &argv[3]);
140                                 goto leave;
141                         }
142                 }
143
144                 ERROR("Command %s not valid\n", cmd);
145         } else {
146                 printf("Interactive mode not ready yet!\n");
147         }
148
149 leave:
150         if (rom.ptr != NULL && rom.ptr != MAP_FAILED)
151                 munmap(rom.ptr, rom.size);
152
153         if (rom.fd > 0)
154                 close(rom.fd);
155
156         return ret;
157 }