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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 19 | * Foundation, Inc. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 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> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 25 | #include <strings.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 26 | #include <ctype.h> |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <getopt.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 29 | #include "common.h" |
| 30 | #include "cbfs.h" |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 31 | #include "cbfs_image.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 32 | #include "cbfs_sections.h" |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 33 | #include "fit.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 34 | #include "partitioned_file.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 35 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame^] | 36 | #define SECTION_WITH_FIT_TABLE "BOOTBLOCK" |
| 37 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 38 | struct command { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 39 | const char *name; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 40 | const char *optstring; |
| 41 | int (*function) (void); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 42 | // Whether to populate param.image_region before invoking function |
| 43 | bool accesses_region; |
| 44 | // Whether to write that region's contents back to image_file at the end |
| 45 | bool modifies_region; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 48 | static struct param { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 49 | partitioned_file_t *image_file; |
| 50 | struct buffer *image_region; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 51 | const char *name; |
| 52 | const char *filename; |
| 53 | const char *fmap; |
| 54 | const char *region_name; |
| 55 | const char *bootblock; |
| 56 | const char *ignore_section; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 57 | uint64_t u64val; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 58 | uint32_t type; |
| 59 | uint32_t baseaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 60 | uint32_t baseaddress_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 61 | uint32_t loadaddress; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 62 | uint32_t copyoffset; |
| 63 | uint32_t copyoffset_assigned; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 64 | uint32_t headeroffset; |
| 65 | uint32_t headeroffset_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 66 | uint32_t entrypoint; |
| 67 | uint32_t size; |
| 68 | uint32_t alignment; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 69 | uint32_t pagesize; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 70 | uint32_t cbfsoffset; |
| 71 | uint32_t cbfsoffset_assigned; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 72 | uint32_t arch; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 73 | bool top_aligned; |
| 74 | bool fill_partial_upward; |
| 75 | bool fill_partial_downward; |
| 76 | bool show_immutable; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 77 | int fit_empty_entries; |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 78 | enum comp_algo algo; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 79 | /* for linux payloads */ |
| 80 | char *initrd; |
| 81 | char *cmdline; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 82 | } param = { |
| 83 | /* All variables not listed are initialized as zero. */ |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 84 | .arch = CBFS_ARCHITECTURE_UNKNOWN, |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 85 | .algo = CBFS_COMPRESS_NONE, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 86 | .headeroffset = ~0, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 87 | .region_name = SECTION_NAME_PRIMARY_CBFS, |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 88 | }; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 89 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 90 | static bool region_is_flashmap(const char *region) |
| 91 | { |
| 92 | return partitioned_file_region_check_magic(param.image_file, region, |
| 93 | FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)); |
| 94 | } |
| 95 | |
| 96 | /* @return Same as cbfs_is_valid_cbfs(), but for a named region. */ |
| 97 | static bool region_is_modern_cbfs(const char *region) |
| 98 | { |
| 99 | return partitioned_file_region_check_magic(param.image_file, region, |
| 100 | CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC)); |
| 101 | } |
| 102 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Converts between offsets from the start of the specified image region and |
| 105 | * "top-aligned" offsets from the top of the entire flash image. Works in either |
| 106 | * direction: pass in one type of offset and receive the other type. |
| 107 | * N.B. A top-aligned offset is always a positive number, and should not be |
| 108 | * confused with a top-aliged *address*, which is its arithmetic inverse. */ |
| 109 | static unsigned convert_to_from_top_aligned(const struct buffer *region, |
| 110 | unsigned offset) |
| 111 | { |
| 112 | assert(region); |
| 113 | |
| 114 | size_t image_size = partitioned_file_total_size(param.image_file); |
| 115 | return image_size - region->offset - offset; |
| 116 | } |
| 117 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 118 | typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 119 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 120 | static int cbfs_add_integer_component(const char *name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 121 | uint64_t u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 122 | uint32_t offset, |
| 123 | uint32_t headeroffset) { |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 124 | struct cbfs_image image; |
| 125 | struct buffer buffer; |
| 126 | int i, ret = 1; |
| 127 | |
| 128 | if (!name) { |
| 129 | ERROR("You need to specify -n/--name.\n"); |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | if (buffer_create(&buffer, 8, name) != 0) |
| 134 | return 1; |
| 135 | |
| 136 | for (i = 0; i < 8; i++) |
| 137 | buffer.data[i] = (u64val >> i*8) & 0xff; |
| 138 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 139 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) { |
| 140 | ERROR("Selected image region is not a CBFS.\n"); |
| 141 | goto done; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (cbfs_get_entry(&image, name)) { |
| 145 | ERROR("'%s' already in ROM image.\n", name); |
| 146 | goto done; |
| 147 | } |
| 148 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 149 | if (IS_TOP_ALIGNED_ADDRESS(offset)) |
| 150 | offset = convert_to_from_top_aligned(param.image_region, |
| 151 | -offset); |
| 152 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 153 | if (cbfs_add_entry(&image, &buffer, name, CBFS_COMPONENT_RAW, offset) != |
| 154 | 0) { |
| 155 | ERROR("Failed to add %llu into ROM image as '%s'.\n", |
| 156 | (long long unsigned)u64val, name); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 157 | goto done; |
| 158 | } |
| 159 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 160 | ret = 0; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 161 | |
| 162 | done: |
| 163 | buffer_delete(&buffer); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 167 | static int cbfs_add_component(const char *filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 168 | const char *name, |
| 169 | uint32_t type, |
| 170 | uint32_t offset, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 171 | uint32_t headeroffset, |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 172 | convert_buffer_t convert) |
| 173 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 174 | if (!filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 175 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 179 | if (!name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 180 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 184 | if (type == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 185 | ERROR("You need to specify a valid -t/--type.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 189 | struct cbfs_image image; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 190 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 191 | return 1; |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 192 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 193 | struct buffer buffer; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 194 | if (buffer_from_file(&buffer, filename) != 0) { |
| 195 | ERROR("Could not load file '%s'.\n", filename); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 196 | return 1; |
| 197 | } |
| 198 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 199 | if (convert && convert(&buffer, &offset) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 200 | ERROR("Failed to parse file '%s'.\n", filename); |
| 201 | buffer_delete(&buffer); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 202 | return 1; |
Stefan Reinauer | fbadc49 | 2011-10-14 12:44:14 -0700 | [diff] [blame] | 203 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 204 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 205 | if (cbfs_get_entry(&image, name)) { |
| 206 | ERROR("'%s' already in ROM image.\n", name); |
| 207 | buffer_delete(&buffer); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 208 | return 1; |
| 209 | } |
| 210 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 211 | if (IS_TOP_ALIGNED_ADDRESS(offset)) |
| 212 | offset = convert_to_from_top_aligned(param.image_region, |
| 213 | -offset); |
| 214 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 215 | if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0) { |
| 216 | ERROR("Failed to add '%s' into ROM image.\n", filename); |
| 217 | buffer_delete(&buffer); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | buffer_delete(&buffer); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 225 | static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset) |
| 226 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 227 | struct buffer output; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 228 | int ret; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 229 | ret = parse_elf_to_stage(buffer, &output, param.algo, offset, |
| 230 | param.ignore_section); |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 231 | if (ret != 0) |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 232 | return -1; |
| 233 | buffer_delete(buffer); |
| 234 | // direct assign, no dupe. |
| 235 | memcpy(buffer, &output, sizeof(*buffer)); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 239 | static int cbfstool_convert_mkpayload(struct buffer *buffer, |
| 240 | unused uint32_t *offset) |
| 241 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 242 | struct buffer output; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 243 | int ret; |
| 244 | /* per default, try and see if payload is an ELF binary */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 245 | ret = parse_elf_to_payload(buffer, &output, param.algo); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 246 | |
| 247 | /* If it's not an ELF, see if it's a UEFI FV */ |
| 248 | if (ret != 0) |
| 249 | ret = parse_fv_to_payload(buffer, &output, param.algo); |
| 250 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 251 | /* If it's neither ELF nor UEFI Fv, try bzImage */ |
| 252 | if (ret != 0) |
| 253 | ret = parse_bzImage_to_payload(buffer, &output, |
| 254 | param.initrd, param.cmdline, param.algo); |
| 255 | |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 256 | /* Not a supported payload type */ |
| 257 | if (ret != 0) { |
| 258 | ERROR("Not a supported payload type (ELF / FV).\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 259 | buffer_delete(buffer); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 260 | return -1; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 263 | buffer_delete(buffer); |
| 264 | // direct assign, no dupe. |
| 265 | memcpy(buffer, &output, sizeof(*buffer)); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int cbfstool_convert_mkflatpayload(struct buffer *buffer, |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 270 | unused uint32_t *offset) |
| 271 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 272 | struct buffer output; |
| 273 | if (parse_flat_binary_to_payload(buffer, &output, |
| 274 | param.loadaddress, |
| 275 | param.entrypoint, |
| 276 | param.algo) != 0) { |
| 277 | return -1; |
| 278 | } |
| 279 | buffer_delete(buffer); |
| 280 | // direct assign, no dupe. |
| 281 | memcpy(buffer, &output, sizeof(*buffer)); |
| 282 | return 0; |
| 283 | } |
| 284 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 285 | static int cbfs_add(void) |
| 286 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 287 | return cbfs_add_component(param.filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 288 | param.name, |
| 289 | param.type, |
| 290 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 291 | param.headeroffset, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 292 | NULL); |
| 293 | } |
| 294 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 295 | static int cbfs_add_stage(void) |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 296 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 297 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 298 | param.name, |
| 299 | CBFS_COMPONENT_STAGE, |
| 300 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 301 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 302 | cbfstool_convert_mkstage); |
| 303 | } |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 304 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 305 | static int cbfs_add_payload(void) |
| 306 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 307 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 308 | param.name, |
| 309 | CBFS_COMPONENT_PAYLOAD, |
| 310 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 311 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 312 | cbfstool_convert_mkpayload); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int cbfs_add_flat_binary(void) |
| 316 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 317 | if (param.loadaddress == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 318 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 319 | "-l/--load-address.\n"); |
| 320 | return 1; |
| 321 | } |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 322 | if (param.entrypoint == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 323 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 324 | "-e/--entry-point.\n"); |
| 325 | return 1; |
| 326 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 327 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 328 | param.name, |
| 329 | CBFS_COMPONENT_PAYLOAD, |
| 330 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 331 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 332 | cbfstool_convert_mkflatpayload); |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 335 | static int cbfs_add_integer(void) |
| 336 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 337 | return cbfs_add_integer_component(param.name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 338 | param.u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 339 | param.baseaddress, |
| 340 | param.headeroffset); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 343 | static int cbfs_remove(void) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 344 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 345 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 346 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 347 | return 1; |
| 348 | } |
| 349 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 350 | struct cbfs_image image; |
| 351 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 352 | param.headeroffset)) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 353 | return 1; |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 354 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 355 | if (cbfs_remove_entry(&image, param.name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 356 | ERROR("Removing file '%s' failed.\n", |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 357 | param.name); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 358 | return 1; |
| 359 | } |
| 360 | |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 364 | static int cbfs_create(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 365 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 366 | struct cbfs_image image; |
| 367 | memset(&image, 0, sizeof(image)); |
| 368 | buffer_clone(&image.buffer, param.image_region); |
| 369 | |
| 370 | if (param.fmap) { |
| 371 | if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size || |
| 372 | param.baseaddress_assigned || |
| 373 | param.headeroffset_assigned || |
| 374 | param.cbfsoffset_assigned || |
| 375 | param.alignment || |
| 376 | param.bootblock) { |
| 377 | ERROR("Since -M was provided, -m, -s, -b, -o, -H, -a, and -B should be omitted\n"); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | return cbfs_image_create(&image, image.buffer.size); |
| 382 | } |
| 383 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 384 | if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) { |
Stefan Reinauer | 60a4a73 | 2013-03-28 16:46:07 -0700 | [diff] [blame] | 385 | ERROR("You need to specify -m/--machine arch.\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 389 | struct buffer bootblock; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 390 | if (!param.bootblock) { |
| 391 | DEBUG("-B not given, creating image without bootblock.\n"); |
| 392 | buffer_create(&bootblock, 0, "(dummy)"); |
| 393 | } else if (buffer_from_file(&bootblock, param.bootblock)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 394 | return 1; |
| 395 | } |
| 396 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 397 | if (!param.alignment) |
| 398 | param.alignment = 64; // default CBFS entry alignment |
| 399 | |
| 400 | // 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] | 401 | if (!param.baseaddress_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 402 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 403 | // Make sure there's at least enough room for rel_offset |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 404 | param.baseaddress = param.size - |
| 405 | MAX(bootblock.size, sizeof(int32_t)); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 406 | DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n", |
| 407 | param.baseaddress); |
| 408 | } else { |
| 409 | param.baseaddress = 0; |
| 410 | DEBUG("bootblock starts at address 0x0.\n"); |
| 411 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 412 | } |
| 413 | if (!param.headeroffset_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 414 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 415 | param.headeroffset = param.baseaddress - |
| 416 | sizeof(struct cbfs_header); |
| 417 | DEBUG("x86 -> CBFS header before bootblock (%#x).\n", |
| 418 | param.headeroffset); |
| 419 | } else { |
| 420 | param.headeroffset = align_up(param.baseaddress + |
| 421 | bootblock.size, sizeof(uint32_t)); |
| 422 | DEBUG("CBFS header placed behind bootblock (%#x).\n", |
| 423 | param.headeroffset); |
| 424 | } |
| 425 | } |
| 426 | if (!param.cbfsoffset_assigned) { |
| 427 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 428 | param.cbfsoffset = 0; |
| 429 | DEBUG("x86 -> CBFS entries start at address 0x0.\n"); |
| 430 | } else { |
| 431 | param.cbfsoffset = align_up(param.headeroffset + |
| 432 | sizeof(struct cbfs_header), |
| 433 | param.alignment); |
| 434 | DEBUG("CBFS entries start beind master header (%#x).\n", |
| 435 | param.cbfsoffset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 439 | int ret = cbfs_legacy_image_create(&image, |
| 440 | param.arch, |
| 441 | param.alignment, |
| 442 | &bootblock, |
| 443 | param.baseaddress, |
| 444 | param.headeroffset, |
| 445 | param.cbfsoffset); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 446 | buffer_delete(&bootblock); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 447 | return ret; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 450 | static int cbfs_locate(void) |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 451 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 452 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 453 | ERROR("You need to specify -f/--filename.\n"); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 454 | return 1; |
| 455 | } |
| 456 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 457 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 458 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 459 | return 1; |
| 460 | } |
| 461 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 462 | struct cbfs_image image; |
| 463 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 464 | param.headeroffset)) |
| 465 | return 1; |
| 466 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 467 | if (cbfs_get_entry(&image, param.name)) |
| 468 | WARN("'%s' already in CBFS.\n", param.name); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 469 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 470 | struct buffer buffer; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 471 | if (buffer_from_file(&buffer, param.filename) != 0) { |
| 472 | ERROR("Cannot load %s.\n", param.filename); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 476 | int32_t address = cbfs_locate_entry(&image, param.name, buffer.size, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 477 | param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 478 | buffer_delete(&buffer); |
| 479 | |
| 480 | if (address == -1) { |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 481 | ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n", |
| 482 | param.name, param.pagesize, param.alignment); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | if (param.top_aligned) |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 487 | address = -convert_to_from_top_aligned(param.image_region, |
| 488 | address); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 489 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 490 | printf("0x%x\n", address); |
| 491 | return 0; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 494 | static int cbfs_layout(void) |
| 495 | { |
| 496 | const struct fmap *fmap = partitioned_file_get_fmap(param.image_file); |
| 497 | if (!fmap) { |
| 498 | LOG("This is a legacy image composed entirely of a single CBFS.\n"); |
| 499 | return 1; |
| 500 | } |
| 501 | |
| 502 | printf("This image contains the following sections that can be %s with this tool:\n", |
| 503 | param.show_immutable ? "accessed" : "manipulated"); |
| 504 | puts(""); |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 505 | for (unsigned i = 0; i < fmap->nareas; ++i) { |
| 506 | const struct fmap_area *current = fmap->areas + i; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 507 | |
| 508 | bool readonly = partitioned_file_fmap_count(param.image_file, |
| 509 | partitioned_file_fmap_select_children_of, current) || |
| 510 | region_is_flashmap((const char *)current->name); |
| 511 | if (!param.show_immutable && readonly) |
| 512 | continue; |
| 513 | |
| 514 | printf("'%s'", current->name); |
| 515 | |
| 516 | // Detect consecutive sections that describe the same region and |
| 517 | // show them as aliases. This cannot find equivalent entries |
| 518 | // that aren't adjacent; however, fmaptool doesn't generate |
| 519 | // FMAPs with such sections, so this convenience feature works |
| 520 | // for all but the strangest manually created FMAP binaries. |
| 521 | // TODO: This could be done by parsing the FMAP into some kind |
| 522 | // of tree that had duplicate lists in addition to child lists, |
| 523 | // which would allow covering that weird, unlikely case as well. |
| 524 | unsigned lookahead; |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 525 | for (lookahead = 1; i + lookahead < fmap->nareas; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 526 | ++lookahead) { |
| 527 | const struct fmap_area *consecutive = |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 528 | fmap->areas + i + lookahead; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 529 | if (consecutive->offset != current->offset || |
| 530 | consecutive->size != current->size) |
| 531 | break; |
| 532 | printf(", '%s'", consecutive->name); |
| 533 | } |
| 534 | if (lookahead > 1) |
| 535 | fputs(" are aliases for the same region", stdout); |
| 536 | |
| 537 | const char *qualifier = ""; |
| 538 | if (readonly) |
| 539 | qualifier = "read-only, "; |
| 540 | else if (region_is_modern_cbfs((const char *)current->name)) |
| 541 | qualifier = "CBFS, "; |
| 542 | printf(" (%ssize %u)\n", qualifier, current->size); |
| 543 | |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 544 | i += lookahead - 1; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 545 | } |
| 546 | puts(""); |
| 547 | |
| 548 | if (param.show_immutable) { |
| 549 | puts("It is at least possible to perform the read action on every section listed above."); |
| 550 | } else { |
| 551 | puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above."); |
| 552 | puts("To see the image's read-only sections as well, rerun with the -w option."); |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 558 | static int cbfs_print(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 559 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 560 | struct cbfs_image image; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 561 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 562 | param.headeroffset)) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 563 | return 1; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 564 | cbfs_print_directory(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 568 | static int cbfs_extract(void) |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 569 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 570 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 571 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 572 | return 1; |
| 573 | } |
| 574 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 575 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 576 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 577 | return 1; |
| 578 | } |
| 579 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 580 | struct cbfs_image image; |
| 581 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 582 | param.headeroffset)) |
| 583 | return 1; |
| 584 | |
| 585 | return cbfs_export_entry(&image, param.name, param.filename); |
| 586 | } |
| 587 | |
| 588 | static int cbfs_write(void) |
| 589 | { |
| 590 | if (!param.filename) { |
| 591 | ERROR("You need to specify a valid input -f/--file.\n"); |
| 592 | return 1; |
| 593 | } |
| 594 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 595 | ERROR("This operation isn't valid on legacy images having CBFS master headers\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 596 | return 1; |
| 597 | } |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 598 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 599 | if (region_is_modern_cbfs(param.region_name)) { |
| 600 | ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n", |
| 601 | param.region_name); |
| 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | struct buffer new_content; |
| 606 | if (buffer_from_file(&new_content, param.filename)) |
| 607 | return 1; |
| 608 | |
| 609 | if (buffer_check_magic(&new_content, FMAP_SIGNATURE, |
| 610 | strlen(FMAP_SIGNATURE))) { |
| 611 | ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n", |
| 612 | param.filename); |
| 613 | buffer_delete(&new_content); |
| 614 | return 1; |
| 615 | } |
| 616 | if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC, |
| 617 | strlen(CBFS_FILE_MAGIC))) { |
| 618 | ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n", |
| 619 | param.filename); |
| 620 | buffer_delete(&new_content); |
| 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | unsigned offset = 0; |
| 625 | if (param.fill_partial_upward && param.fill_partial_downward) { |
| 626 | ERROR("You may only specify one of -u and -d.\n"); |
| 627 | buffer_delete(&new_content); |
| 628 | return 1; |
| 629 | } else if (!param.fill_partial_upward && !param.fill_partial_downward) { |
| 630 | if (new_content.size != param.image_region->size) { |
| 631 | ERROR("File to add is %zu bytes and would not fill %zu-byte target region (did you mean to pass either -u or -d?)\n", |
| 632 | new_content.size, param.image_region->size); |
| 633 | buffer_delete(&new_content); |
| 634 | return 1; |
| 635 | } |
| 636 | } else { |
| 637 | if (new_content.size > param.image_region->size) { |
| 638 | ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n", |
| 639 | new_content.size, param.image_region->size); |
| 640 | buffer_delete(&new_content); |
| 641 | return 1; |
| 642 | } |
| 643 | WARN("Written area will abut %s of target region: any unused space will keep its current contents\n", |
| 644 | param.fill_partial_upward ? "bottom" : "top"); |
| 645 | if (param.fill_partial_downward) |
| 646 | offset = param.image_region->size - new_content.size; |
| 647 | } |
| 648 | |
| 649 | memcpy(param.image_region->data + offset, new_content.data, |
| 650 | new_content.size); |
| 651 | buffer_delete(&new_content); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int cbfs_read(void) |
| 656 | { |
| 657 | if (!param.filename) { |
| 658 | ERROR("You need to specify a valid output -f/--file.\n"); |
| 659 | return 1; |
| 660 | } |
| 661 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 662 | ERROR("This operation isn't valid on legacy images having CBFS master headers\n"); |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | return buffer_write_file(param.image_region, param.filename); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 669 | static int cbfs_update_fit(void) |
| 670 | { |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 671 | if (!param.name) { |
| 672 | ERROR("You need to specify -n/--name.\n"); |
| 673 | return 1; |
| 674 | } |
| 675 | |
| 676 | if (param.fit_empty_entries <= 0) { |
| 677 | ERROR("Invalid number of fit entries " |
| 678 | "(-x/--empty-fits): %d\n", param.fit_empty_entries); |
| 679 | return 1; |
| 680 | } |
| 681 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame^] | 682 | // Decide which region to read/write the FIT table from/to. |
| 683 | struct buffer bootblock; |
| 684 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 685 | if (!partitioned_file_read_region(&bootblock, param.image_file, |
| 686 | SECTION_WITH_FIT_TABLE)) |
| 687 | return 1; |
| 688 | } else { |
| 689 | // In legacy images, the bootblock is part of the CBFS. |
| 690 | buffer_clone(&bootblock, param.image_region); |
| 691 | } |
| 692 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 693 | struct cbfs_image image; |
| 694 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 695 | param.headeroffset)) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 696 | return 1; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 697 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame^] | 698 | if (fit_update_table(&bootblock, &image, param.name, |
| 699 | param.fit_empty_entries, convert_to_from_top_aligned)) |
| 700 | return 1; |
| 701 | |
| 702 | // The region to be written depends on the type of image, so we write it |
| 703 | // here rather than having main() write the CBFS region back as usual. |
| 704 | return !partitioned_file_write_region(param.image_file, &bootblock); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 705 | } |
| 706 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 707 | static int cbfs_copy(void) |
| 708 | { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 709 | if (!param.copyoffset_assigned) { |
| 710 | ERROR("You need to specify -D/--copy_offset.\n"); |
| 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | if (!param.size) { |
| 715 | ERROR("You need to specify -s/--size.\n"); |
| 716 | return 1; |
| 717 | } |
| 718 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 719 | struct cbfs_image image; |
| 720 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 721 | param.headeroffset)) |
| 722 | return 1; |
| 723 | |
| 724 | if (!cbfs_is_legacy_cbfs(&image)) { |
| 725 | ERROR("This operation is only valid on legacy images having CBFS master headers\n"); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 726 | return 1; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 727 | } |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 728 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 729 | return cbfs_copy_instance(&image, param.copyoffset, param.size); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 732 | static bool cbfs_is_legacy_format(struct buffer *buffer) |
| 733 | { |
| 734 | // Legacy CBFSes are those containing the deprecated CBFS master header. |
| 735 | return cbfs_find_header(buffer->data, buffer->size, -1); |
| 736 | } |
| 737 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 738 | static const struct command commands[] = { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 739 | {"add", "H:r:f:n:t:b:vh?", cbfs_add, true, true}, |
| 740 | {"add-flat-binary", "H:r:f:n:l:e:c:b:vh?", cbfs_add_flat_binary, true, |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 741 | true}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 742 | {"add-payload", "H:r:f:n:t:c:b:C:I:vh?", cbfs_add_payload, true, true}, |
| 743 | {"add-stage", "H:r:f:n:t:c:b:S:vh?", cbfs_add_stage, true, true}, |
| 744 | {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true}, |
| 745 | {"copy", "H:D:s:h?", cbfs_copy, true, true}, |
| 746 | {"create", "M:r:s:B:b:H:a:o:m:vh?", cbfs_create, true, true}, |
| 747 | {"extract", "H:r:n:f:vh?", cbfs_extract, true, false}, |
| 748 | {"locate", "H:r:f:n:P:a:Tvh?", cbfs_locate, true, false}, |
| 749 | {"layout", "wvh?", cbfs_layout, false, false}, |
| 750 | {"print", "H:r:vh?", cbfs_print, true, false}, |
| 751 | {"read", "r:f:vh?", cbfs_read, true, false}, |
| 752 | {"remove", "H:r:n:vh?", cbfs_remove, true, true}, |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame^] | 753 | {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 754 | {"write", "r:f:udvh?", cbfs_write, true, true}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 755 | }; |
| 756 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 757 | static struct option long_options[] = { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 758 | {"alignment", required_argument, 0, 'a' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 759 | {"base-address", required_argument, 0, 'b' }, |
| 760 | {"bootblock", required_argument, 0, 'B' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 761 | {"cmdline", required_argument, 0, 'C' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 762 | {"compression", required_argument, 0, 'c' }, |
| 763 | {"copy-offset", required_argument, 0, 'D' }, |
| 764 | {"empty-fits", required_argument, 0, 'x' }, |
| 765 | {"entry-point", required_argument, 0, 'e' }, |
| 766 | {"file", required_argument, 0, 'f' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 767 | {"fill-downward", no_argument, 0, 'd' }, |
| 768 | {"fill-upward", no_argument, 0, 'u' }, |
| 769 | {"flashmap", required_argument, 0, 'M' }, |
| 770 | {"fmap-regions", required_argument, 0, 'r' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 771 | {"header-offset", required_argument, 0, 'H' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 772 | {"help", no_argument, 0, 'h' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 773 | {"ignore-sec", required_argument, 0, 'S' }, |
| 774 | {"initrd", required_argument, 0, 'I' }, |
| 775 | {"int", required_argument, 0, 'i' }, |
| 776 | {"load-address", required_argument, 0, 'l' }, |
| 777 | {"machine", required_argument, 0, 'm' }, |
| 778 | {"name", required_argument, 0, 'n' }, |
| 779 | {"offset", required_argument, 0, 'o' }, |
| 780 | {"page-size", required_argument, 0, 'P' }, |
| 781 | {"size", required_argument, 0, 's' }, |
| 782 | {"top-aligned", required_argument, 0, 'T' }, |
| 783 | {"type", required_argument, 0, 't' }, |
| 784 | {"verbose", no_argument, 0, 'v' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 785 | {"with-readonly", no_argument, 0, 'w' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 786 | {NULL, 0, 0, 0 } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 787 | }; |
| 788 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 789 | static int dispatch_command(struct command command) |
| 790 | { |
| 791 | if (command.accesses_region) { |
| 792 | assert(param.image_file); |
| 793 | |
| 794 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 795 | LOG("Performing operation on '%s' region...\n", |
| 796 | param.region_name); |
| 797 | } |
| 798 | if (!partitioned_file_read_region(param.image_region, |
| 799 | param.image_file, param.region_name)) { |
| 800 | ERROR("The image will be left unmodified.\n"); |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | if (command.modifies_region) { |
| 805 | // We (intentionally) don't support overwriting the FMAP |
| 806 | // section. If you find yourself wanting to do this, |
| 807 | // consider creating a new image rather than performing |
| 808 | // whatever hacky transformation you were planning. |
| 809 | if (region_is_flashmap(param.region_name)) { |
| 810 | ERROR("Image region '%s' is read-only because it contains the FMAP.\n", |
| 811 | param.region_name); |
| 812 | ERROR("The image will be left unmodified.\n"); |
| 813 | return 1; |
| 814 | } |
| 815 | // We don't allow writing raw data to regions that |
| 816 | // contain nested regions, since doing so would |
| 817 | // overwrite all such subregions. |
| 818 | if (partitioned_file_region_contains_nested( |
| 819 | param.image_file, param.region_name)) { |
| 820 | ERROR("Image region '%s' is read-only because it contains nested regions.\n", |
| 821 | param.region_name); |
| 822 | ERROR("The image will be left unmodified.\n"); |
| 823 | return 1; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (command.function()) { |
| 829 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 830 | ERROR("Failed while operating on '%s' region!\n", |
| 831 | param.region_name); |
| 832 | ERROR("The image will be left unmodified.\n"); |
| 833 | } |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 840 | static void usage(char *name) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 841 | { |
| 842 | printf |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 843 | ("cbfstool: Management utility for CBFS formatted ROM images\n\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 844 | "USAGE:\n" " %s [-h]\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 845 | " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 846 | " -H header_offset Do not search for header; use this offset*\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 847 | " -T Output top-aligned memory address\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 848 | " -u Accept short data; fill upward/from bottom\n" |
| 849 | " -d Accept short data; fill downward/from top\n" |
| 850 | " -v Provide verbose output\n" |
| 851 | " -h Display this help message\n\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 852 | "COMMANDs:\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 853 | " add [-r image,regions] -f FILE -n NAME -t TYPE \\\n" |
| 854 | " [-b base-address] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 855 | "Add a component\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 856 | " add-payload [-r image,regions] -f FILE -n NAME \\\n" |
| 857 | " [-c compression] [-b base-address] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 858 | "Add a payload to the ROM\n" |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 859 | " (linux specific: [-C cmdline] [-I initrd])\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 860 | " add-stage [-r image,regions] -f FILE -n NAME \\\n" |
| 861 | " [-c compression] [-b base] [-S section-to-ignore] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 862 | "Add a stage to the ROM\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 863 | " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n" |
| 864 | " -l load-address -e entry-point [-c compression] \\\n" |
| 865 | " [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 866 | "Add a 32bit flat mode binary\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 867 | " add-int [-r image,regions] -i INTEGER -n NAME [-b base] " |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 868 | "Add a raw 64-bit integer value\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 869 | " remove [-r image,regions] -n NAME " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 870 | "Remove a component\n" |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 871 | " copy -D new_header_offset -s region size \\\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 872 | " [-H source header offset] " |
| 873 | "Create a copy (duplicate) cbfs instance*\n" |
| 874 | " create -m ARCH -s size [-b bootblock offset] \\\n" |
| 875 | " [-o CBFS offset] [-H header offset] [-B bootblock] \\\n" |
| 876 | " [-a align] " |
| 877 | "Create a legacy ROM file with CBFS master header*\n" |
| 878 | " create -M flashmap [-r list,of,regions,containing,cbfses] " |
| 879 | "Create a new-style partitioned firmware image\n" |
| 880 | " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n" |
| 881 | " [-a align] [-T] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 882 | "Find a place for a file of that size\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 883 | " layout [-w] " |
| 884 | "List mutable (or, with -w, readable) image regions\n" |
| 885 | " print [-r image,regions] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 886 | "Show the contents of the ROM\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 887 | " extract [-r image,regions] -n NAME -f FILE " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 888 | "Extracts a raw payload from ROM\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 889 | " write -r image,regions -f file [-u | -d] " |
| 890 | "Write file into same-size [or larger] raw region\n" |
| 891 | " read [-r fmap-region] -f file " |
| 892 | "Extract raw region contents into binary file\n" |
| 893 | " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n" |
| 894 | " -x EMTPY_FIT_ENTRIES " |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 895 | "Updates the FIT table with microcode entries\n" |
Peter Stuge | b347e0d | 2011-01-17 05:02:09 +0000 | [diff] [blame] | 896 | "\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 897 | "OFFSETs:\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 898 | " Numbers accompanying -b, -H, and -o switches* may be provided\n" |
| 899 | " in two possible formats: if their value is greater than\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 900 | " 0x80000000, they are interpreted as a top-aligned x86 memory\n" |
| 901 | " address; otherwise, they are treated as an offset into flash.\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 902 | "ARCHes:\n" |
Paul Burton | 3318692 | 2014-06-13 23:56:45 +0100 | [diff] [blame] | 903 | " arm64, arm, mips, x86\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 904 | "TYPEs:\n", name, name |
| 905 | ); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 906 | print_supported_filetypes(); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 907 | |
| 908 | printf( |
| 909 | "\n* Note that these actions and switches are only valid when\n" |
| 910 | " working with legacy images whose structure is described\n" |
| 911 | " primarily by a CBFS master header. New-style images, in\n" |
| 912 | " contrast, exclusively make use of an FMAP to describe their\n" |
| 913 | " layout: this must minimally contain an '%s' section\n" |
| 914 | " specifying the location of this FMAP itself and a '%s'\n" |
| 915 | " section describing the primary CBFS. It should also be noted\n" |
| 916 | " that, when working with such images, the -F and -r switches\n" |
| 917 | " default to '%s' for convenience, and both the -b switch to\n" |
| 918 | " CBFS operations and the output of the locate action become\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 919 | " relative to the selected CBFS region's lowest address.\n" |
| 920 | " The one exception to this rule is the top-aligned address,\n" |
| 921 | " which is always relative to the end of the entire image\n" |
| 922 | " rather than relative to the local region; this is true for\n" |
| 923 | " for both input (sufficiently large) and output (-T) data.\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 924 | SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS, |
| 925 | SECTION_NAME_PRIMARY_CBFS |
| 926 | ); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | int main(int argc, char **argv) |
| 930 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 931 | size_t i; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 932 | int c; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 933 | |
| 934 | if (argc < 3) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 935 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 936 | return 1; |
| 937 | } |
| 938 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 939 | char *image_name = argv[1]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 940 | char *cmd = argv[2]; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 941 | optind += 2; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 942 | |
| 943 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 944 | if (strcmp(cmd, commands[i].name) != 0) |
| 945 | continue; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 946 | |
| 947 | while (1) { |
| 948 | char *suffix = NULL; |
| 949 | int option_index = 0; |
| 950 | |
| 951 | c = getopt_long(argc, argv, commands[i].optstring, |
| 952 | long_options, &option_index); |
| 953 | if (c == -1) |
| 954 | break; |
| 955 | |
| 956 | /* filter out illegal long options */ |
zbao | 062730d | 2013-01-08 10:10:16 +0800 | [diff] [blame] | 957 | if (strchr(commands[i].optstring, c) == NULL) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 958 | /* TODO maybe print actual long option instead */ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 959 | ERROR("%s: invalid option -- '%c'\n", |
| 960 | argv[0], c); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 961 | c = '?'; |
| 962 | } |
| 963 | |
| 964 | switch(c) { |
| 965 | case 'n': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 966 | param.name = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 967 | break; |
| 968 | case 't': |
| 969 | if (intfiletype(optarg) != ((uint64_t) - 1)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 970 | param.type = intfiletype(optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 971 | else |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 972 | param.type = strtoul(optarg, NULL, 0); |
| 973 | if (param.type == 0) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 974 | WARN("Unknown type '%s' ignored\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 975 | optarg); |
| 976 | break; |
| 977 | case 'c': |
| 978 | if (!strncasecmp(optarg, "lzma", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 979 | param.algo = CBFS_COMPRESS_LZMA; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 980 | else if (!strncasecmp(optarg, "none", 5)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 981 | param.algo = CBFS_COMPRESS_NONE; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 982 | else |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 983 | WARN("Unknown compression '%s'" |
| 984 | " ignored.\n", optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 985 | break; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 986 | case 'M': |
| 987 | param.fmap = optarg; |
| 988 | break; |
| 989 | case 'r': |
| 990 | param.region_name = optarg; |
| 991 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 992 | case 'b': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 993 | param.baseaddress = strtoul(optarg, NULL, 0); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 994 | // baseaddress may be zero on non-x86, so we |
| 995 | // need an explicit "baseaddress_assigned". |
| 996 | param.baseaddress = strtoul(optarg, NULL, 0); |
| 997 | param.baseaddress_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 998 | break; |
| 999 | case 'l': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1000 | param.loadaddress = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1001 | break; |
| 1002 | case 'e': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1003 | param.entrypoint = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | case 's': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1006 | param.size = strtoul(optarg, &suffix, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1007 | if (tolower(suffix[0])=='k') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1008 | param.size *= 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | if (tolower(suffix[0])=='m') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1011 | param.size *= 1024 * 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1012 | } |
Patrick Georgi | e887ca5 | 2014-08-09 17:44:39 +0200 | [diff] [blame] | 1013 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1014 | case 'B': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1015 | param.bootblock = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1016 | break; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1017 | case 'H': |
| 1018 | param.headeroffset = strtoul( |
| 1019 | optarg, NULL, 0); |
| 1020 | param.headeroffset_assigned = 1; |
| 1021 | break; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1022 | case 'D': |
| 1023 | param.copyoffset = strtoul(optarg, NULL, 0); |
| 1024 | param.copyoffset_assigned = 1; |
Vadim Bendebury | 1161473 | 2015-01-08 16:36:35 -0800 | [diff] [blame] | 1025 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1026 | case 'a': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1027 | param.alignment = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1028 | break; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 1029 | case 'P': |
| 1030 | param.pagesize = strtoul(optarg, NULL, 0); |
| 1031 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1032 | case 'o': |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1033 | param.cbfsoffset = strtoul(optarg, NULL, 0); |
| 1034 | param.cbfsoffset_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1035 | break; |
| 1036 | case 'f': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1037 | param.filename = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1038 | break; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1039 | case 'i': |
| 1040 | param.u64val = strtoull(optarg, NULL, 0); |
| 1041 | break; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1042 | case 'T': |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1043 | param.top_aligned = true; |
| 1044 | break; |
| 1045 | case 'u': |
| 1046 | param.fill_partial_upward = true; |
| 1047 | break; |
| 1048 | case 'd': |
| 1049 | param.fill_partial_downward = true; |
| 1050 | break; |
| 1051 | case 'w': |
| 1052 | param.show_immutable = true; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1053 | break; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 1054 | case 'x': |
| 1055 | param.fit_empty_entries = strtol(optarg, NULL, 0); |
| 1056 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1057 | case 'v': |
| 1058 | verbose++; |
| 1059 | break; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1060 | case 'm': |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1061 | param.arch = string_to_arch(optarg); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1062 | break; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 1063 | case 'I': |
| 1064 | param.initrd = optarg; |
| 1065 | break; |
| 1066 | case 'C': |
| 1067 | param.cmdline = optarg; |
| 1068 | break; |
Furquan Shaikh | 405304a | 2014-10-30 11:44:20 -0700 | [diff] [blame] | 1069 | case 'S': |
| 1070 | param.ignore_section = optarg; |
| 1071 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1072 | case 'h': |
| 1073 | case '?': |
| 1074 | usage(argv[0]); |
| 1075 | return 1; |
| 1076 | default: |
| 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1081 | if (commands[i].function == cbfs_create) { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1082 | if (param.fmap) { |
| 1083 | struct buffer flashmap; |
| 1084 | if (buffer_from_file(&flashmap, param.fmap)) |
| 1085 | return 1; |
| 1086 | param.image_file = partitioned_file_create( |
| 1087 | image_name, &flashmap); |
| 1088 | buffer_delete(&flashmap); |
| 1089 | } else if (param.size) { |
| 1090 | param.image_file = partitioned_file_create_flat( |
| 1091 | image_name, param.size); |
| 1092 | } else { |
| 1093 | ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1094 | return 1; |
| 1095 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1096 | } else { |
| 1097 | param.image_file = |
| 1098 | partitioned_file_reopen(image_name, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1099 | cbfs_is_legacy_format); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1100 | } |
| 1101 | if (!param.image_file) |
| 1102 | return 1; |
| 1103 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1104 | unsigned num_regions = 1; |
| 1105 | for (const char *list = strchr(param.region_name, ','); list; |
| 1106 | list = strchr(list + 1, ',')) |
| 1107 | ++num_regions; |
| 1108 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1109 | // If the action needs to read an image region, as indicated by |
| 1110 | // having accesses_region set in its command struct, that |
| 1111 | // region's buffer struct will be stored here and the client |
| 1112 | // will receive a pointer to it via param.image_region. It |
| 1113 | // need not write the buffer back to the image file itself, |
| 1114 | // since this behavior can be requested via its modifies_region |
| 1115 | // field. Additionally, it should never free the region buffer, |
| 1116 | // as that is performed automatically once it completes. |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1117 | struct buffer image_regions[num_regions]; |
| 1118 | memset(image_regions, 0, sizeof(image_regions)); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1119 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1120 | bool seen_primary_cbfs = false; |
| 1121 | char region_name_scratch[strlen(param.region_name) + 1]; |
| 1122 | strcpy(region_name_scratch, param.region_name); |
| 1123 | param.region_name = strtok(region_name_scratch, ","); |
| 1124 | for (unsigned region = 0; region < num_regions; ++region) { |
| 1125 | if (!param.region_name) { |
| 1126 | ERROR("Encountered illegal degenerate region name in -r list\n"); |
| 1127 | ERROR("The image will be left unmodified.\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1128 | partitioned_file_close(param.image_file); |
| 1129 | return 1; |
| 1130 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1131 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1132 | if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS) |
| 1133 | == 0) |
| 1134 | seen_primary_cbfs = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1135 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1136 | param.image_region = image_regions + region; |
| 1137 | if (dispatch_command(commands[i])) { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1138 | partitioned_file_close(param.image_file); |
| 1139 | return 1; |
| 1140 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1141 | |
| 1142 | param.region_name = strtok(NULL, ","); |
| 1143 | } |
| 1144 | |
| 1145 | if (commands[i].function == cbfs_create && !seen_primary_cbfs) { |
| 1146 | ERROR("The creation -r list must include the mandatory '%s' section.\n", |
| 1147 | SECTION_NAME_PRIMARY_CBFS); |
| 1148 | ERROR("The image will be left unmodified.\n"); |
| 1149 | partitioned_file_close(param.image_file); |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
| 1153 | if (commands[i].modifies_region) { |
| 1154 | assert(param.image_file); |
| 1155 | assert(commands[i].accesses_region); |
| 1156 | for (unsigned region = 0; region < num_regions; |
| 1157 | ++region) { |
| 1158 | |
| 1159 | if (!partitioned_file_write_region( |
| 1160 | param.image_file, |
| 1161 | image_regions + region)) { |
| 1162 | partitioned_file_close( |
| 1163 | param.image_file); |
| 1164 | return 1; |
| 1165 | } |
| 1166 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | partitioned_file_close(param.image_file); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1170 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1173 | ERROR("Unknown command '%s'.\n", cmd); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1174 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1175 | return 1; |
| 1176 | } |