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" |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 29 | #include "elf.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 30 | #include "cbfs.h" |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 31 | #include "cbfs_image.h" |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 32 | #include "fit.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 33 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 34 | struct command { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 35 | const char *name; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 36 | const char *optstring; |
| 37 | int (*function) (void); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 40 | int verbose = 0; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 41 | static struct param { |
| 42 | char *cbfs_name; |
| 43 | char *name; |
| 44 | char *filename; |
| 45 | char *bootblock; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 46 | uint64_t u64val; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 47 | uint32_t type; |
| 48 | uint32_t baseaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 49 | uint32_t baseaddress_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 50 | uint32_t loadaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 51 | uint32_t headeroffset; |
| 52 | uint32_t headeroffset_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 53 | uint32_t entrypoint; |
| 54 | uint32_t size; |
| 55 | uint32_t alignment; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 56 | uint32_t pagesize; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 57 | uint32_t offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 58 | uint32_t top_aligned; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 59 | uint32_t arch; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 60 | int fit_empty_entries; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 61 | comp_algo algo; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 62 | /* for linux payloads */ |
| 63 | char *initrd; |
| 64 | char *cmdline; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 65 | } param = { |
| 66 | /* All variables not listed are initialized as zero. */ |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 67 | .arch = CBFS_ARCHITECTURE_UNKNOWN, |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 68 | .algo = CBFS_COMPRESS_NONE, |
| 69 | }; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 70 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 71 | typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 72 | |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 73 | static int cbfs_add_integer_component(const char *cbfs_name, |
| 74 | const char *name, |
| 75 | uint64_t u64val, |
| 76 | uint32_t offset) { |
| 77 | struct cbfs_image image; |
| 78 | struct buffer buffer; |
| 79 | int i, ret = 1; |
| 80 | |
| 81 | if (!name) { |
| 82 | ERROR("You need to specify -n/--name.\n"); |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | if (buffer_create(&buffer, 8, name) != 0) |
| 87 | return 1; |
| 88 | |
| 89 | for (i = 0; i < 8; i++) |
| 90 | buffer.data[i] = (u64val >> i*8) & 0xff; |
| 91 | |
| 92 | if (cbfs_image_from_file(&image, cbfs_name) != 0) { |
| 93 | ERROR("Could not load ROM image '%s'.\n", cbfs_name); |
| 94 | buffer_delete(&buffer); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | if (cbfs_get_entry(&image, name)) { |
| 99 | ERROR("'%s' already in ROM image.\n", name); |
| 100 | goto done; |
| 101 | } |
| 102 | |
| 103 | if (cbfs_add_entry(&image, &buffer, name, CBFS_COMPONENT_RAW, param.baseaddress) != 0) { |
| 104 | ERROR("Failed to add %llu into ROM image as '%s'.\n", (long long unsigned)u64val, name); |
| 105 | goto done; |
| 106 | } |
| 107 | |
| 108 | if (cbfs_image_write_file(&image, cbfs_name) == 0) |
| 109 | ret = 0; |
| 110 | |
| 111 | done: |
| 112 | buffer_delete(&buffer); |
| 113 | cbfs_image_delete(&image); |
| 114 | return ret; |
| 115 | } |
| 116 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 117 | static int cbfs_add_component(const char *cbfs_name, |
| 118 | const char *filename, |
| 119 | const char *name, |
| 120 | uint32_t type, |
| 121 | uint32_t offset, |
| 122 | convert_buffer_t convert) { |
| 123 | struct cbfs_image image; |
| 124 | struct buffer buffer; |
| 125 | |
| 126 | if (!filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 127 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 131 | if (!name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 132 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 136 | if (type == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 137 | ERROR("You need to specify a valid -t/--type.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 141 | if (buffer_from_file(&buffer, filename) != 0) { |
| 142 | ERROR("Could not load file '%s'.\n", filename); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 146 | if (convert && convert(&buffer, &offset) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 147 | ERROR("Failed to parse file '%s'.\n", filename); |
| 148 | buffer_delete(&buffer); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 149 | return 1; |
| 150 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 151 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 152 | if (cbfs_image_from_file(&image, cbfs_name) != 0) { |
| 153 | ERROR("Could not load ROM image '%s'.\n", cbfs_name); |
| 154 | buffer_delete(&buffer); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 155 | return 1; |
Stefan Reinauer | fbadc49 | 2011-10-14 12:44:14 -0700 | [diff] [blame] | 156 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 157 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 158 | if (cbfs_get_entry(&image, name)) { |
| 159 | ERROR("'%s' already in ROM image.\n", name); |
| 160 | buffer_delete(&buffer); |
| 161 | cbfs_image_delete(&image); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0) { |
| 166 | ERROR("Failed to add '%s' into ROM image.\n", filename); |
| 167 | buffer_delete(&buffer); |
| 168 | cbfs_image_delete(&image); |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | if (cbfs_image_write_file(&image, cbfs_name) != 0) { |
| 173 | buffer_delete(&buffer); |
| 174 | cbfs_image_delete(&image); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | buffer_delete(&buffer); |
| 179 | cbfs_image_delete(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 183 | static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset) |
| 184 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 185 | struct buffer output; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 186 | int ret; |
| 187 | ret = parse_elf_to_stage(buffer, &output, param.arch, param.algo, |
| 188 | offset); |
| 189 | if (ret != 0) |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 190 | return -1; |
| 191 | buffer_delete(buffer); |
| 192 | // direct assign, no dupe. |
| 193 | memcpy(buffer, &output, sizeof(*buffer)); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int cbfstool_convert_mkpayload(struct buffer *buffer, uint32_t *offset) { |
| 198 | struct buffer output; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 199 | int ret; |
| 200 | /* per default, try and see if payload is an ELF binary */ |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 201 | ret = parse_elf_to_payload(buffer, &output, param.arch, param.algo); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 202 | |
| 203 | /* If it's not an ELF, see if it's a UEFI FV */ |
| 204 | if (ret != 0) |
| 205 | ret = parse_fv_to_payload(buffer, &output, param.algo); |
| 206 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 207 | /* If it's neither ELF nor UEFI Fv, try bzImage */ |
| 208 | if (ret != 0) |
| 209 | ret = parse_bzImage_to_payload(buffer, &output, |
| 210 | param.initrd, param.cmdline, param.algo); |
| 211 | |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 212 | /* Not a supported payload type */ |
| 213 | if (ret != 0) { |
| 214 | ERROR("Not a supported payload type (ELF / FV).\n"); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 215 | return -1; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 218 | buffer_delete(buffer); |
| 219 | // direct assign, no dupe. |
| 220 | memcpy(buffer, &output, sizeof(*buffer)); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int cbfstool_convert_mkflatpayload(struct buffer *buffer, |
| 225 | uint32_t *offset) { |
| 226 | struct buffer output; |
| 227 | if (parse_flat_binary_to_payload(buffer, &output, |
| 228 | param.loadaddress, |
| 229 | param.entrypoint, |
| 230 | param.algo) != 0) { |
| 231 | return -1; |
| 232 | } |
| 233 | buffer_delete(buffer); |
| 234 | // direct assign, no dupe. |
| 235 | memcpy(buffer, &output, sizeof(*buffer)); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 240 | static int cbfs_add(void) |
| 241 | { |
| 242 | return cbfs_add_component(param.cbfs_name, |
| 243 | param.filename, |
| 244 | param.name, |
| 245 | param.type, |
| 246 | param.baseaddress, |
| 247 | NULL); |
| 248 | } |
| 249 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 250 | static int cbfs_add_stage(void) |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 251 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 252 | return cbfs_add_component(param.cbfs_name, |
| 253 | param.filename, |
| 254 | param.name, |
| 255 | CBFS_COMPONENT_STAGE, |
| 256 | param.baseaddress, |
| 257 | cbfstool_convert_mkstage); |
| 258 | } |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 259 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 260 | static int cbfs_add_payload(void) |
| 261 | { |
| 262 | return cbfs_add_component(param.cbfs_name, |
| 263 | param.filename, |
| 264 | param.name, |
| 265 | CBFS_COMPONENT_PAYLOAD, |
| 266 | param.baseaddress, |
| 267 | cbfstool_convert_mkpayload); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int cbfs_add_flat_binary(void) |
| 271 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 272 | if (param.loadaddress == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 273 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 274 | "-l/--load-address.\n"); |
| 275 | return 1; |
| 276 | } |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 277 | if (param.entrypoint == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 278 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 279 | "-e/--entry-point.\n"); |
| 280 | return 1; |
| 281 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 282 | return cbfs_add_component(param.cbfs_name, |
| 283 | param.filename, |
| 284 | param.name, |
| 285 | CBFS_COMPONENT_PAYLOAD, |
| 286 | param.baseaddress, |
| 287 | cbfstool_convert_mkflatpayload); |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 290 | static int cbfs_add_integer(void) |
| 291 | { |
| 292 | return cbfs_add_integer_component(param.cbfs_name, |
| 293 | param.name, |
| 294 | param.u64val, |
| 295 | param.baseaddress); |
| 296 | } |
| 297 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 298 | static int cbfs_remove(void) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 299 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 300 | struct cbfs_image image; |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 301 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 302 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 303 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 304 | return 1; |
| 305 | } |
| 306 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 307 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 308 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 309 | param.cbfs_name); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 313 | if (cbfs_remove_entry(&image, param.name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 314 | ERROR("Removing file '%s' failed.\n", |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 315 | param.name); |
| 316 | cbfs_image_delete(&image); |
| 317 | return 1; |
| 318 | } |
| 319 | if (cbfs_image_write_file(&image, param.cbfs_name) != 0) { |
| 320 | cbfs_image_delete(&image); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 321 | return 1; |
| 322 | } |
| 323 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 324 | cbfs_image_delete(&image); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 328 | static int cbfs_create(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 329 | { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 330 | struct cbfs_image image; |
| 331 | struct buffer bootblock; |
| 332 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 333 | if (param.size == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 334 | ERROR("You need to specify a valid -s/--size.\n"); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 335 | return 1; |
| 336 | } |
| 337 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 338 | if (!param.bootblock) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 339 | ERROR("You need to specify -B/--bootblock.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 340 | return 1; |
Patrick Georgi | 467b12a | 2009-12-21 13:50:37 +0000 | [diff] [blame] | 341 | } |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 342 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 343 | if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) { |
Stefan Reinauer | 60a4a73 | 2013-03-28 16:46:07 -0700 | [diff] [blame] | 344 | ERROR("You need to specify -m/--machine arch.\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 348 | if (buffer_from_file(&bootblock, param.bootblock) != 0) { |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | // Setup default boot offset and header offset. |
| 353 | if (!param.baseaddress_assigned) { |
| 354 | // put boot block before end of ROM. |
| 355 | param.baseaddress = param.size - bootblock.size; |
| 356 | DEBUG("bootblock in end of ROM.\n"); |
| 357 | } |
| 358 | if (!param.headeroffset_assigned) { |
| 359 | // Put header before bootblock, and make a reference in end of |
| 360 | // bootblock. |
| 361 | param.headeroffset = ( |
| 362 | param.baseaddress - |
| 363 | sizeof(struct cbfs_header)); |
| 364 | if (bootblock.size >= sizeof(uint32_t)) { |
| 365 | // TODO this only works for 32b top-aligned system now... |
| 366 | uint32_t ptr = param.headeroffset - param.size; |
| 367 | uint32_t *sig = (uint32_t *)(bootblock.data + |
| 368 | bootblock.size - |
| 369 | sizeof(ptr)); |
| 370 | *sig = ptr; |
| 371 | DEBUG("CBFS header reference in end of bootblock.\n"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (cbfs_image_create(&image, |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 376 | param.arch, |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 377 | param.size, |
| 378 | param.alignment, |
| 379 | &bootblock, |
| 380 | param.baseaddress, |
| 381 | param.headeroffset, |
| 382 | param.offset) != 0) { |
| 383 | ERROR("Failed to create %s.\n", param.cbfs_name); |
| 384 | return 1; |
| 385 | } |
| 386 | buffer_delete(&bootblock); |
| 387 | |
| 388 | if (cbfs_image_write_file(&image, param.cbfs_name) != 0) { |
| 389 | ERROR("Failed to write %s.\n", param.cbfs_name); |
| 390 | cbfs_image_delete(&image); |
| 391 | return 1; |
| 392 | } |
| 393 | cbfs_image_delete(&image); |
| 394 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 397 | static int cbfs_locate(void) |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 398 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 399 | struct cbfs_image image; |
| 400 | struct buffer buffer; |
| 401 | int32_t address; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 402 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 403 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 404 | ERROR("You need to specify -f/--filename.\n"); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 405 | return 1; |
| 406 | } |
| 407 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 408 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 409 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 410 | return 1; |
| 411 | } |
| 412 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 413 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
| 414 | ERROR("Failed to load %s.\n", param.cbfs_name); |
| 415 | return 1; |
| 416 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 417 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 418 | if (cbfs_get_entry(&image, param.name)) |
| 419 | WARN("'%s' already in CBFS.\n", param.name); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 420 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 421 | if (buffer_from_file(&buffer, param.filename) != 0) { |
| 422 | ERROR("Cannot load %s.\n", param.filename); |
| 423 | cbfs_image_delete(&image); |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | address = cbfs_locate_entry(&image, param.name, buffer.size, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 428 | param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 429 | buffer_delete(&buffer); |
| 430 | |
| 431 | if (address == -1) { |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 432 | ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n", |
| 433 | param.name, param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 434 | cbfs_image_delete(&image); |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | if (param.top_aligned) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame^] | 439 | address = address - image.header->romsize; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 440 | |
| 441 | cbfs_image_delete(&image); |
| 442 | printf("0x%x\n", address); |
| 443 | return 0; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 446 | static int cbfs_print(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 447 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 448 | struct cbfs_image image; |
| 449 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 450 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 451 | param.cbfs_name); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 452 | return 1; |
| 453 | } |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 454 | cbfs_print_directory(&image); |
| 455 | cbfs_image_delete(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 459 | static int cbfs_extract(void) |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 460 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 461 | int result = 0; |
| 462 | struct cbfs_image image; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 463 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 464 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 465 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 466 | return 1; |
| 467 | } |
| 468 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 469 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 470 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 471 | return 1; |
| 472 | } |
| 473 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 474 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 475 | ERROR("Could not load ROM image '%s'.\n", |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 476 | param.cbfs_name); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 477 | result = 1; |
| 478 | } else if (cbfs_export_entry(&image, param.name, |
| 479 | param.filename) != 0) { |
| 480 | result = 1; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 483 | cbfs_image_delete(&image); |
| 484 | return result; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 487 | static int cbfs_update_fit(void) |
| 488 | { |
| 489 | int ret = 0; |
| 490 | struct cbfs_image image; |
| 491 | |
| 492 | if (!param.name) { |
| 493 | ERROR("You need to specify -n/--name.\n"); |
| 494 | return 1; |
| 495 | } |
| 496 | |
| 497 | if (param.fit_empty_entries <= 0) { |
| 498 | ERROR("Invalid number of fit entries " |
| 499 | "(-x/--empty-fits): %d\n", param.fit_empty_entries); |
| 500 | return 1; |
| 501 | } |
| 502 | |
| 503 | if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { |
| 504 | ERROR("Could not load ROM image '%s'.\n", |
| 505 | param.cbfs_name); |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | ret = fit_update_table(&image, param.fit_empty_entries, param.name); |
| 510 | if (!ret) |
| 511 | ret = cbfs_image_write_file(&image, param.cbfs_name); |
| 512 | |
| 513 | cbfs_image_delete(&image); |
| 514 | return ret; |
| 515 | } |
| 516 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 517 | static const struct command commands[] = { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 518 | {"add", "f:n:t:b:vh?", cbfs_add}, |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 519 | {"add-payload", "f:n:t:c:b:vh?C:I:", cbfs_add_payload}, |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 520 | {"add-stage", "f:n:t:c:b:vh?", cbfs_add_stage}, |
| 521 | {"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary}, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 522 | {"add-int", "i:n:b:vh?", cbfs_add_integer}, |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 523 | {"remove", "n:vh?", cbfs_remove}, |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 524 | {"create", "s:B:b:H:a:o:m:vh?", cbfs_create}, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 525 | {"locate", "f:n:P:a:Tvh?", cbfs_locate}, |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 526 | {"print", "vh?", cbfs_print}, |
| 527 | {"extract", "n:f:vh?", cbfs_extract}, |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 528 | {"update-fit", "n:x:vh?", cbfs_update_fit}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 529 | }; |
| 530 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 531 | static struct option long_options[] = { |
| 532 | {"name", required_argument, 0, 'n' }, |
| 533 | {"type", required_argument, 0, 't' }, |
| 534 | {"compression", required_argument, 0, 'c' }, |
| 535 | {"base-address", required_argument, 0, 'b' }, |
| 536 | {"load-address", required_argument, 0, 'l' }, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 537 | {"top-aligned", required_argument, 0, 'T' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 538 | {"entry-point", required_argument, 0, 'e' }, |
| 539 | {"size", required_argument, 0, 's' }, |
| 540 | {"bootblock", required_argument, 0, 'B' }, |
| 541 | {"alignment", required_argument, 0, 'a' }, |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 542 | {"page-size", required_argument, 0, 'P' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 543 | {"offset", required_argument, 0, 'o' }, |
| 544 | {"file", required_argument, 0, 'f' }, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 545 | {"int", required_argument, 0, 'i' }, |
Stefan Reinauer | 60a4a73 | 2013-03-28 16:46:07 -0700 | [diff] [blame] | 546 | {"machine", required_argument, 0, 'm' }, |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 547 | {"empty-fits", required_argument, 0, 'x' }, |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 548 | {"initrd", required_argument, 0, 'I' }, |
| 549 | {"cmdline", required_argument, 0, 'C' }, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 550 | {"verbose", no_argument, 0, 'v' }, |
| 551 | {"help", no_argument, 0, 'h' }, |
| 552 | {NULL, 0, 0, 0 } |
| 553 | }; |
| 554 | |
| 555 | static void usage(char *name) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 556 | { |
| 557 | printf |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 558 | ("cbfstool: Management utility for CBFS formatted ROM images\n\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 559 | "USAGE:\n" " %s [-h]\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 560 | " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 561 | " -T Output top-aligned memory address\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 562 | " -v Provide verbose output\n" |
| 563 | " -h Display this help message\n\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 564 | "COMMANDs:\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 565 | " add -f FILE -n NAME -t TYPE [-b base-address] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 566 | "Add a component\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 567 | " add-payload -f FILE -n NAME [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 568 | "Add a payload to the ROM\n" |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 569 | " (linux specific: [-C cmdline] [-I initrd])\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 570 | " add-stage -f FILE -n NAME [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 571 | "Add a stage to the ROM\n" |
| 572 | " add-flat-binary -f FILE -n NAME -l load-address \\\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 573 | " -e entry-point [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 574 | "Add a 32bit flat mode binary\n" |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 575 | " add-int -i INTEGER -n NAME [-b base] " |
| 576 | "Add a raw 64-bit integer value\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 577 | " remove -n NAME " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 578 | "Remove a component\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 579 | " create -s size -B bootblock -m ARCH [-a align] [-o offset] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 580 | "Create a ROM file\n" |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 581 | " locate -f FILE -n NAME [-P page-size] [-a align] [-T] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 582 | "Find a place for a file of that size\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 583 | " print " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 584 | "Show the contents of the ROM\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 585 | " extract -n NAME -f FILE " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 586 | "Extracts a raw payload from ROM\n" |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 587 | " update-fit -n MICROCODE_BLOB_NAME -x EMTPY_FIT_ENTRIES\n " |
| 588 | "Updates the FIT table with microcode entries\n" |
Peter Stuge | b347e0d | 2011-01-17 05:02:09 +0000 | [diff] [blame] | 589 | "\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 590 | "ARCHes:\n" |
| 591 | " armv7, x86\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 592 | "TYPEs:\n", name, name |
| 593 | ); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 594 | print_supported_filetypes(); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | int main(int argc, char **argv) |
| 598 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 599 | size_t i; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 600 | int c; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 601 | |
| 602 | if (argc < 3) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 603 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 604 | return 1; |
| 605 | } |
| 606 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 607 | param.cbfs_name = argv[1]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 608 | char *cmd = argv[2]; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 609 | optind += 2; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 610 | |
| 611 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 612 | if (strcmp(cmd, commands[i].name) != 0) |
| 613 | continue; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 614 | |
| 615 | while (1) { |
| 616 | char *suffix = NULL; |
| 617 | int option_index = 0; |
| 618 | |
| 619 | c = getopt_long(argc, argv, commands[i].optstring, |
| 620 | long_options, &option_index); |
| 621 | if (c == -1) |
| 622 | break; |
| 623 | |
| 624 | /* filter out illegal long options */ |
zbao | 062730d | 2013-01-08 10:10:16 +0800 | [diff] [blame] | 625 | if (strchr(commands[i].optstring, c) == NULL) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 626 | /* TODO maybe print actual long option instead */ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 627 | ERROR("%s: invalid option -- '%c'\n", |
| 628 | argv[0], c); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 629 | c = '?'; |
| 630 | } |
| 631 | |
| 632 | switch(c) { |
| 633 | case 'n': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 634 | param.name = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 635 | break; |
| 636 | case 't': |
| 637 | if (intfiletype(optarg) != ((uint64_t) - 1)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 638 | param.type = intfiletype(optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 639 | else |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 640 | param.type = strtoul(optarg, NULL, 0); |
| 641 | if (param.type == 0) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 642 | WARN("Unknown type '%s' ignored\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 643 | optarg); |
| 644 | break; |
| 645 | case 'c': |
| 646 | if (!strncasecmp(optarg, "lzma", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 647 | param.algo = CBFS_COMPRESS_LZMA; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 648 | else if (!strncasecmp(optarg, "none", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 649 | param.algo = CBFS_COMPRESS_NONE; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 650 | else |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 651 | WARN("Unknown compression '%s'" |
| 652 | " ignored.\n", optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 653 | break; |
| 654 | case 'b': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 655 | param.baseaddress = strtoul(optarg, NULL, 0); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 656 | // baseaddress may be zero on non-x86, so we |
| 657 | // need an explicit "baseaddress_assigned". |
| 658 | param.baseaddress = strtoul(optarg, NULL, 0); |
| 659 | param.baseaddress_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 660 | break; |
| 661 | case 'l': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 662 | param.loadaddress = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 663 | |
| 664 | break; |
| 665 | case 'e': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 666 | param.entrypoint = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 667 | break; |
| 668 | case 's': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 669 | param.size = strtoul(optarg, &suffix, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 670 | if (tolower(suffix[0])=='k') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 671 | param.size *= 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 672 | } |
| 673 | if (tolower(suffix[0])=='m') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 674 | param.size *= 1024 * 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 675 | } |
| 676 | case 'B': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 677 | param.bootblock = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 678 | break; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 679 | case 'H': |
| 680 | param.headeroffset = strtoul( |
| 681 | optarg, NULL, 0); |
| 682 | param.headeroffset_assigned = 1; |
| 683 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 684 | case 'a': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 685 | param.alignment = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 686 | break; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 687 | case 'P': |
| 688 | param.pagesize = strtoul(optarg, NULL, 0); |
| 689 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 690 | case 'o': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 691 | param.offset = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 692 | break; |
| 693 | case 'f': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 694 | param.filename = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 695 | break; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 696 | case 'i': |
| 697 | param.u64val = strtoull(optarg, NULL, 0); |
| 698 | break; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 699 | case 'T': |
| 700 | param.top_aligned = 1; |
| 701 | break; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 702 | case 'x': |
| 703 | param.fit_empty_entries = strtol(optarg, NULL, 0); |
| 704 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 705 | case 'v': |
| 706 | verbose++; |
| 707 | break; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 708 | case 'm': |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 709 | param.arch = string_to_arch(optarg); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 710 | break; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 711 | case 'I': |
| 712 | param.initrd = optarg; |
| 713 | break; |
| 714 | case 'C': |
| 715 | param.cmdline = optarg; |
| 716 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 717 | case 'h': |
| 718 | case '?': |
| 719 | usage(argv[0]); |
| 720 | return 1; |
| 721 | default: |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | return commands[i].function(); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 729 | ERROR("Unknown command '%s'.\n", cmd); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 730 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 731 | return 1; |
| 732 | } |