Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | * cbfstool, CLI utility for CBFS file manipulation |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | * |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 20 | */ |
| 21 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
Stefan Reinauer | 336daa7 | 2009-12-21 15:09:01 +0000 | [diff] [blame] | 24 | #include <string.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 25 | #include <ctype.h> |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 28 | #include "common.h" |
| 29 | #include "cbfs.h" |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 30 | #include "cbfs_image.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 31 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 32 | struct command { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 33 | const char *name; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 34 | const char *optstring; |
| 35 | int (*function) (void); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 38 | int verbose = 0; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 39 | static struct param { |
| 40 | char *cbfs_name; |
| 41 | char *name; |
| 42 | char *filename; |
| 43 | char *bootblock; |
| 44 | uint32_t type; |
| 45 | uint32_t baseaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 46 | uint32_t baseaddress_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 47 | uint32_t loadaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 48 | uint32_t headeroffset; |
| 49 | uint32_t headeroffset_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 50 | uint32_t entrypoint; |
| 51 | uint32_t size; |
| 52 | uint32_t alignment; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 53 | uint32_t pagesize; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 54 | uint32_t offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 55 | uint32_t top_aligned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 56 | comp_algo algo; |
| 57 | } param = { |
| 58 | /* All variables not listed are initialized as zero. */ |
| 59 | .algo = CBFS_COMPRESS_NONE, |
| 60 | }; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 61 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 62 | typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 63 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 64 | static int cbfs_add_component(const char *cbfs_name, |
| 65 | const char *filename, |
| 66 | const char *name, |
| 67 | uint32_t type, |
| 68 | uint32_t offset, |
| 69 | convert_buffer_t convert) { |
| 70 | struct cbfs_image image; |
| 71 | struct buffer buffer; |
| 72 | |
| 73 | if (!filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 74 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 75 | return 1; |
| 76 | } |
| 77 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 78 | if (!name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 79 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 83 | if (type == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 84 | ERROR("You need to specify a valid -t/--type.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 85 | return 1; |
| 86 | } |
| 87 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 88 | if (buffer_from_file(&buffer, filename) != 0) { |
| 89 | ERROR("Could not load file '%s'.\n", filename); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 93 | if (convert && convert(&buffer, &offset) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 94 | ERROR("Failed to parse file '%s'.\n", filename); |
| 95 | buffer_delete(&buffer); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 96 | return 1; |
| 97 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 98 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 99 | if (cbfs_image_from_file(&image, cbfs_name) != 0) { |
| 100 | ERROR("Could not load ROM image '%s'.\n", cbfs_name); |
| 101 | buffer_delete(&buffer); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 102 | return 1; |
Stefan Reinauer | fbadc49 | 2011-10-14 12:44:14 -0700 | [diff] [blame] | 103 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 104 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 105 | if (cbfs_get_entry(&image, name)) { |
| 106 | ERROR("'%s' already in ROM image.\n", name); |
| 107 | buffer_delete(&buffer); |
| 108 | cbfs_image_delete(&image); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0) { |
| 113 | ERROR("Failed to add '%s' into ROM image.\n", filename); |
| 114 | buffer_delete(&buffer); |
| 115 | cbfs_image_delete(&image); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | if (cbfs_image_write_file(&image, cbfs_name) != 0) { |
| 120 | buffer_delete(&buffer); |
| 121 | cbfs_image_delete(&image); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | buffer_delete(&buffer); |
| 126 | cbfs_image_delete(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 130 | static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset) { |
| 131 | struct buffer output; |
| 132 | if (parse_elf_to_stage(buffer, &output, param.algo, offset) != 0) |
| 133 | return -1; |
| 134 | buffer_delete(buffer); |
| 135 | // direct assign, no dupe. |
| 136 | memcpy(buffer, &output, sizeof(*buffer)); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int cbfstool_convert_mkpayload(struct buffer *buffer, uint32_t *offset) { |
| 141 | struct buffer output; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 142 | int ret; |
| 143 | /* per default, try and see if payload is an ELF binary */ |
| 144 | ret = parse_elf_to_payload(buffer, &output, param.algo); |
| 145 | |
| 146 | /* If it's not an ELF, see if it's a UEFI FV */ |
| 147 | if (ret != 0) |
| 148 | ret = parse_fv_to_payload(buffer, &output, param.algo); |
| 149 | |
| 150 | /* Not a supported payload type */ |
| 151 | if (ret != 0) { |
| 152 | ERROR("Not a supported payload type (ELF / FV).\n"); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 153 | return -1; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 156 | buffer_delete(buffer); |
| 157 | // direct assign, no dupe. |
| 158 | memcpy(buffer, &output, sizeof(*buffer)); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int cbfstool_convert_mkflatpayload(struct buffer *buffer, |
| 163 | uint32_t *offset) { |
| 164 | struct buffer output; |
| 165 | if (parse_flat_binary_to_payload(buffer, &output, |
| 166 | param.loadaddress, |
| 167 | param.entrypoint, |
| 168 | param.algo) != 0) { |
| 169 | return -1; |
| 170 | } |
| 171 | buffer_delete(buffer); |
| 172 | // direct assign, no dupe. |
| 173 | memcpy(buffer, &output, sizeof(*buffer)); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 178 | static int cbfs_add(void) |
| 179 | { |
| 180 | return cbfs_add_component(param.cbfs_name, |
| 181 | param.filename, |
| 182 | param.name, |
| 183 | param.type, |
| 184 | param.baseaddress, |
| 185 | NULL); |
| 186 | } |
| 187 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 188 | static int cbfs_add_stage(void) |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 189 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 190 | return cbfs_add_component(param.cbfs_name, |
| 191 | param.filename, |
| 192 | param.name, |
| 193 | CBFS_COMPONENT_STAGE, |
| 194 | param.baseaddress, |
| 195 | cbfstool_convert_mkstage); |
| 196 | } |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 197 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 198 | static int cbfs_add_payload(void) |
| 199 | { |
| 200 | return cbfs_add_component(param.cbfs_name, |
| 201 | param.filename, |
| 202 | param.name, |
| 203 | CBFS_COMPONENT_PAYLOAD, |
| 204 | param.baseaddress, |
| 205 | cbfstool_convert_mkpayload); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int cbfs_add_flat_binary(void) |
| 209 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 210 | if (param.loadaddress == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 211 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 212 | "-l/--load-address.\n"); |
| 213 | return 1; |
| 214 | } |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 215 | if (param.entrypoint == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 216 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 217 | "-e/--entry-point.\n"); |
| 218 | return 1; |
| 219 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 220 | return cbfs_add_component(param.cbfs_name, |
| 221 | param.filename, |
| 222 | param.name, |
| 223 | CBFS_COMPONENT_PAYLOAD, |
| 224 | param.baseaddress, |
| 225 | cbfstool_convert_mkflatpayload); |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 228 | static int cbfs_remove(void) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 229 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 230 | struct cbfs_image image; |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 231 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 232 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 233 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 237 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 238 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 239 | param.cbfs_name); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 240 | return 1; |
| 241 | } |
| 242 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 243 | if (cbfs_remove_entry(&image, param.name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 244 | ERROR("Removing file '%s' failed.\n", |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 245 | param.name); |
| 246 | cbfs_image_delete(&image); |
| 247 | return 1; |
| 248 | } |
| 249 | if (cbfs_image_write_file(&image, param.cbfs_name) != 0) { |
| 250 | cbfs_image_delete(&image); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 251 | return 1; |
| 252 | } |
| 253 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 254 | cbfs_image_delete(&image); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 258 | static int cbfs_create(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 259 | { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 260 | struct cbfs_image image; |
| 261 | struct buffer bootblock; |
| 262 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 263 | if (param.size == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 264 | ERROR("You need to specify a valid -s/--size.\n"); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 265 | return 1; |
| 266 | } |
| 267 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 268 | if (!param.bootblock) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 269 | ERROR("You need to specify -B/--bootblock.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 270 | return 1; |
Patrick Georgi | 467b12a | 2009-12-21 13:50:37 +0000 | [diff] [blame] | 271 | } |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 272 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 273 | // TODO Remove arch or pack into param. |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 274 | if (arch == CBFS_ARCHITECTURE_UNKNOWN) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 275 | ERROR("You need to specify -m/--machine arch\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 276 | return 1; |
| 277 | } |
| 278 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 279 | if (buffer_from_file(&bootblock, param.bootblock) != 0) { |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | // Setup default boot offset and header offset. |
| 284 | if (!param.baseaddress_assigned) { |
| 285 | // put boot block before end of ROM. |
| 286 | param.baseaddress = param.size - bootblock.size; |
| 287 | DEBUG("bootblock in end of ROM.\n"); |
| 288 | } |
| 289 | if (!param.headeroffset_assigned) { |
| 290 | // Put header before bootblock, and make a reference in end of |
| 291 | // bootblock. |
| 292 | param.headeroffset = ( |
| 293 | param.baseaddress - |
| 294 | sizeof(struct cbfs_header)); |
| 295 | if (bootblock.size >= sizeof(uint32_t)) { |
| 296 | // TODO this only works for 32b top-aligned system now... |
| 297 | uint32_t ptr = param.headeroffset - param.size; |
| 298 | uint32_t *sig = (uint32_t *)(bootblock.data + |
| 299 | bootblock.size - |
| 300 | sizeof(ptr)); |
| 301 | *sig = ptr; |
| 302 | DEBUG("CBFS header reference in end of bootblock.\n"); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (cbfs_image_create(&image, |
| 307 | arch, |
| 308 | param.size, |
| 309 | param.alignment, |
| 310 | &bootblock, |
| 311 | param.baseaddress, |
| 312 | param.headeroffset, |
| 313 | param.offset) != 0) { |
| 314 | ERROR("Failed to create %s.\n", param.cbfs_name); |
| 315 | return 1; |
| 316 | } |
| 317 | buffer_delete(&bootblock); |
| 318 | |
| 319 | if (cbfs_image_write_file(&image, param.cbfs_name) != 0) { |
| 320 | ERROR("Failed to write %s.\n", param.cbfs_name); |
| 321 | cbfs_image_delete(&image); |
| 322 | return 1; |
| 323 | } |
| 324 | cbfs_image_delete(&image); |
| 325 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 328 | static int cbfs_locate(void) |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 329 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 330 | struct cbfs_image image; |
| 331 | struct buffer buffer; |
| 332 | int32_t address; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 333 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 334 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 335 | ERROR("You need to specify -f/--filename.\n"); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 336 | return 1; |
| 337 | } |
| 338 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 339 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 340 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 344 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
| 345 | ERROR("Failed to load %s.\n", param.cbfs_name); |
| 346 | return 1; |
| 347 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 348 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 349 | if (cbfs_get_entry(&image, param.name)) |
| 350 | WARN("'%s' already in CBFS.\n", param.name); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 351 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 352 | if (buffer_from_file(&buffer, param.filename) != 0) { |
| 353 | ERROR("Cannot load %s.\n", param.filename); |
| 354 | cbfs_image_delete(&image); |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | address = cbfs_locate_entry(&image, param.name, buffer.size, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame^] | 359 | param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 360 | buffer_delete(&buffer); |
| 361 | |
| 362 | if (address == -1) { |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame^] | 363 | ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n", |
| 364 | param.name, param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 365 | cbfs_image_delete(&image); |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | if (param.top_aligned) |
| 370 | address = address - ntohl(image.header->romsize); |
| 371 | |
| 372 | cbfs_image_delete(&image); |
| 373 | printf("0x%x\n", address); |
| 374 | return 0; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 377 | static int cbfs_print(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 378 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 379 | struct cbfs_image image; |
| 380 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 381 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 382 | param.cbfs_name); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 383 | return 1; |
| 384 | } |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 385 | cbfs_print_directory(&image); |
| 386 | cbfs_image_delete(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 390 | static int cbfs_extract(void) |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 391 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 392 | int result = 0; |
| 393 | struct cbfs_image image; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 394 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 395 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 396 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 400 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 401 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 402 | return 1; |
| 403 | } |
| 404 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 405 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 406 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 407 | param.cbfs_name); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 408 | result = 1; |
| 409 | } else if (cbfs_export_entry(&image, param.name, |
| 410 | param.filename) != 0) { |
| 411 | result = 1; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 414 | cbfs_image_delete(&image); |
| 415 | return result; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 418 | static const struct command commands[] = { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 419 | {"add", "f:n:t:b:vh?", cbfs_add}, |
| 420 | {"add-payload", "f:n:t:c:b:vh?", cbfs_add_payload}, |
| 421 | {"add-stage", "f:n:t:c:b:vh?", cbfs_add_stage}, |
| 422 | {"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary}, |
| 423 | {"remove", "n:vh?", cbfs_remove}, |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 424 | {"create", "s:B:b:H:a:o:m:vh?", cbfs_create}, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame^] | 425 | {"locate", "f:n:P:a:Tvh?", cbfs_locate}, |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 426 | {"print", "vh?", cbfs_print}, |
| 427 | {"extract", "n:f:vh?", cbfs_extract}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 430 | static struct option long_options[] = { |
| 431 | {"name", required_argument, 0, 'n' }, |
| 432 | {"type", required_argument, 0, 't' }, |
| 433 | {"compression", required_argument, 0, 'c' }, |
| 434 | {"base-address", required_argument, 0, 'b' }, |
| 435 | {"load-address", required_argument, 0, 'l' }, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 436 | {"top-aligned", required_argument, 0, 'T' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 437 | {"entry-point", required_argument, 0, 'e' }, |
| 438 | {"size", required_argument, 0, 's' }, |
| 439 | {"bootblock", required_argument, 0, 'B' }, |
| 440 | {"alignment", required_argument, 0, 'a' }, |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 441 | {"page-size", required_argument, 0, 'P' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 442 | {"offset", required_argument, 0, 'o' }, |
| 443 | {"file", required_argument, 0, 'f' }, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 444 | {"arch", required_argument, 0, 'm' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 445 | {"verbose", no_argument, 0, 'v' }, |
| 446 | {"help", no_argument, 0, 'h' }, |
| 447 | {NULL, 0, 0, 0 } |
| 448 | }; |
| 449 | |
| 450 | static void usage(char *name) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 451 | { |
| 452 | printf |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 453 | ("cbfstool: Management utility for CBFS formatted ROM images\n\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 454 | "USAGE:\n" " %s [-h]\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 455 | " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 456 | " -T Output top-aligned memory address\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 457 | " -v Provide verbose output\n" |
| 458 | " -h Display this help message\n\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 459 | "COMMANDs:\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 460 | " add -f FILE -n NAME -t TYPE [-b base-address] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 461 | "Add a component\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 462 | " add-payload -f FILE -n NAME [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 463 | "Add a payload to the ROM\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 464 | " add-stage -f FILE -n NAME [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 465 | "Add a stage to the ROM\n" |
| 466 | " add-flat-binary -f FILE -n NAME -l load-address \\\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 467 | " -e entry-point [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 468 | "Add a 32bit flat mode binary\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 469 | " remove -n NAME " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 470 | "Remove a component\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 471 | " create -s size -B bootblock -m ARCH [-a align] [-o offset] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 472 | "Create a ROM file\n" |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame^] | 473 | " locate -f FILE -n NAME [-P page-size] [-a align] [-T] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 474 | "Find a place for a file of that size\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 475 | " print " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 476 | "Show the contents of the ROM\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 477 | " extract -n NAME -f FILE " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 478 | "Extracts a raw payload from ROM\n" |
Peter Stuge | b347e0d | 2011-01-17 05:02:09 +0000 | [diff] [blame] | 479 | "\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 480 | "ARCHes:\n" |
| 481 | " armv7, x86\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 482 | "TYPEs:\n", name, name |
| 483 | ); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 484 | print_supported_filetypes(); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | int main(int argc, char **argv) |
| 488 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 489 | size_t i; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 490 | int c; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 491 | |
| 492 | if (argc < 3) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 493 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 494 | return 1; |
| 495 | } |
| 496 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 497 | param.cbfs_name = argv[1]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 498 | char *cmd = argv[2]; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 499 | optind += 2; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 500 | |
| 501 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 502 | if (strcmp(cmd, commands[i].name) != 0) |
| 503 | continue; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 504 | |
| 505 | while (1) { |
| 506 | char *suffix = NULL; |
| 507 | int option_index = 0; |
| 508 | |
| 509 | c = getopt_long(argc, argv, commands[i].optstring, |
| 510 | long_options, &option_index); |
| 511 | if (c == -1) |
| 512 | break; |
| 513 | |
| 514 | /* filter out illegal long options */ |
zbao | 062730d | 2013-01-08 10:10:16 +0800 | [diff] [blame] | 515 | if (strchr(commands[i].optstring, c) == NULL) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 516 | /* TODO maybe print actual long option instead */ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 517 | ERROR("%s: invalid option -- '%c'\n", |
| 518 | argv[0], c); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 519 | c = '?'; |
| 520 | } |
| 521 | |
| 522 | switch(c) { |
| 523 | case 'n': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 524 | param.name = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 525 | break; |
| 526 | case 't': |
| 527 | if (intfiletype(optarg) != ((uint64_t) - 1)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 528 | param.type = intfiletype(optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 529 | else |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 530 | param.type = strtoul(optarg, NULL, 0); |
| 531 | if (param.type == 0) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 532 | WARN("Unknown type '%s' ignored\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 533 | optarg); |
| 534 | break; |
| 535 | case 'c': |
| 536 | if (!strncasecmp(optarg, "lzma", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 537 | param.algo = CBFS_COMPRESS_LZMA; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 538 | else if (!strncasecmp(optarg, "none", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 539 | param.algo = CBFS_COMPRESS_NONE; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 540 | else |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 541 | WARN("Unknown compression '%s'" |
| 542 | " ignored.\n", optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 543 | break; |
| 544 | case 'b': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 545 | param.baseaddress = strtoul(optarg, NULL, 0); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 546 | // baseaddress may be zero on non-x86, so we |
| 547 | // need an explicit "baseaddress_assigned". |
| 548 | param.baseaddress = strtoul(optarg, NULL, 0); |
| 549 | param.baseaddress_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 550 | break; |
| 551 | case 'l': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 552 | param.loadaddress = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 553 | |
| 554 | break; |
| 555 | case 'e': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 556 | param.entrypoint = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 557 | break; |
| 558 | case 's': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 559 | param.size = strtoul(optarg, &suffix, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 560 | if (tolower(suffix[0])=='k') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 561 | param.size *= 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 562 | } |
| 563 | if (tolower(suffix[0])=='m') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 564 | param.size *= 1024 * 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 565 | } |
| 566 | case 'B': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 567 | param.bootblock = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 568 | break; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 569 | case 'H': |
| 570 | param.headeroffset = strtoul( |
| 571 | optarg, NULL, 0); |
| 572 | param.headeroffset_assigned = 1; |
| 573 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 574 | case 'a': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 575 | param.alignment = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 576 | break; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 577 | case 'P': |
| 578 | param.pagesize = strtoul(optarg, NULL, 0); |
| 579 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 580 | case 'o': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 581 | param.offset = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 582 | break; |
| 583 | case 'f': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 584 | param.filename = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 585 | break; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 586 | case 'T': |
| 587 | param.top_aligned = 1; |
| 588 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 589 | case 'v': |
| 590 | verbose++; |
| 591 | break; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 592 | case 'm': |
| 593 | arch = string_to_arch(optarg); |
| 594 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 595 | case 'h': |
| 596 | case '?': |
| 597 | usage(argv[0]); |
| 598 | return 1; |
| 599 | default: |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return commands[i].function(); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 607 | ERROR("Unknown command '%s'.\n", cmd); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 608 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 609 | return 1; |
| 610 | } |