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