blob: 1fbb61bfac29c4eeb1c9467f4303a178c223204d [file] [log] [blame]
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001/*
2 * ifwitool, CLI utility for IFWI manipulation
3 *
4 * Copyright 2016 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <commonlib/endian.h>
17#include <getopt.h>
18#include <stdlib.h>
19#include <time.h>
20
21#include "common.h"
22
23/*
24 * BPDT is Boot Partition Descriptor Table. It is located at the start of a
25 * logical boot partition(LBP). It stores information about the critical
26 * sub-partitions present within the LBP.
27 *
28 * S-BPDT is Secondary Boot Partition Descriptor Table. It is located after the
29 * critical sub-partitions and contains information about the non-critical
30 * sub-partitions present within the LBP.
31 *
32 * Both tables are identified by BPDT_SIGNATURE stored at the start of the
33 * table.
34 */
35#define BPDT_SIGNATURE (0x000055AA)
36
37/* Parameters passed in by caller. */
38static struct param {
39 const char *file_name;
40 const char *subpart_name;
41 const char *image_name;
42 bool dir_ops;
43 const char *dentry_name;
44} param;
45
46struct bpdt_header {
47 /*
48 * This is used to identify start of BPDT. It should always be
49 * BPDT_SIGNATURE.
50 */
51 uint32_t signature;
52 /* Count of BPDT entries present. */
53 uint16_t descriptor_count;
54 /* Version - Currently supported = 1. */
55 uint16_t bpdt_version;
56 /* Unused - Should be 0. */
57 uint32_t xor_redundant_block;
58 /* Version of IFWI build. */
59 uint32_t ifwi_version;
60 /* Version of FIT tool used to create IFWI. */
61 uint64_t fit_tool_version;
Stefan Reinauer6a001132017-07-13 02:20:27 +020062} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -070063#define BPDT_HEADER_SIZE (sizeof(struct bpdt_header))
64
65struct bpdt_entry {
66 /* Type of sub-partition. */
67 uint16_t type;
68 /* Attributes of sub-partition. */
69 uint16_t flags;
70 /* Offset of sub-partition from beginning of LBP. */
71 uint32_t offset;
72 /* Size in bytes of sub-partition. */
73 uint32_t size;
Stefan Reinauer6a001132017-07-13 02:20:27 +020074} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -070075#define BPDT_ENTRY_SIZE (sizeof(struct bpdt_entry))
76
77struct bpdt {
78 struct bpdt_header h;
79 /* In practice, this could be an array of 0 to n entries. */
80 struct bpdt_entry e[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020081} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -070082
Werner Zeh310580e2016-06-24 13:03:38 +020083static inline size_t get_bpdt_size(struct bpdt_header *h)
Furquan Shaikh233f1b62016-05-19 16:12:16 -070084{
85 return (sizeof(*h) + BPDT_ENTRY_SIZE * h->descriptor_count);
86}
87
88/* Minimum size in bytes allocated to BPDT in IFWI. */
89#define BPDT_MIN_SIZE (512)
90
91/* Header to define directory header for sub-partition. */
92struct subpart_dir_header {
93 /* Should be SUBPART_DIR_MARKER. */
94 uint32_t marker;
95 /* Number of directory entries in the sub-partition. */
96 uint32_t num_entries;
97 /* Currenty supported - 1. */
98 uint8_t header_version;
99 /* Currenty supported - 1. */
100 uint8_t entry_version;
101 /* Length of directory header in bytes. */
102 uint8_t header_length;
103 /*
Furquan Shaikhe51c20b2016-06-15 08:01:10 -0700104 * 2s complement of 8-bit sum from first byte of header to last byte of
105 * last directory entry.
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700106 */
107 uint8_t checksum;
108 /* ASCII short name of sub-partition. */
109 uint8_t name[4];
Stefan Reinauer6a001132017-07-13 02:20:27 +0200110} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700111#define SUBPART_DIR_HEADER_SIZE \
112 (sizeof(struct subpart_dir_header))
113#define SUBPART_DIR_MARKER 0x44504324
114#define SUBPART_DIR_HEADER_VERSION_SUPPORTED 1
115#define SUBPART_DIR_ENTRY_VERSION_SUPPORTED 1
116
117/* Structure for each directory entry for sub-partition. */
118struct subpart_dir_entry {
119 /* Name of directory entry - Not guaranteed to be NULL-terminated. */
120 uint8_t name[12];
121 /* Offset of entry from beginning of sub-partition. */
122 uint32_t offset;
123 /* Length in bytes of sub-directory entry. */
124 uint32_t length;
125 /* Must be zero. */
126 uint32_t rsvd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200127} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700128#define SUBPART_DIR_ENTRY_SIZE \
129 (sizeof(struct subpart_dir_entry))
130
131struct subpart_dir {
132 struct subpart_dir_header h;
133 /* In practice, this could be an array of 0 to n entries. */
134 struct subpart_dir_entry e[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +0200135} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700136
137static inline size_t subpart_dir_size(struct subpart_dir_header *h)
138{
139 return (sizeof(*h) + SUBPART_DIR_ENTRY_SIZE * h->num_entries);
140}
141
142struct manifest_header {
143 uint32_t header_type;
144 uint32_t header_length;
145 uint32_t header_version;
146 uint32_t flags;
147 uint32_t vendor;
148 uint32_t date;
149 uint32_t size;
150 uint32_t id;
151 uint32_t rsvd;
152 uint64_t version;
153 uint32_t svn;
154 uint64_t rsvd1;
155 uint8_t rsvd2[64];
156 uint32_t modulus_size;
157 uint32_t exponent_size;
158 uint8_t public_key[256];
159 uint32_t exponent;
160 uint8_t signature[256];
Stefan Reinauer6a001132017-07-13 02:20:27 +0200161} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700162
163#define DWORD_SIZE 4
164#define MANIFEST_HDR_SIZE (sizeof(struct manifest_header))
165#define MANIFEST_ID_MAGIC (0x324e4d24)
166
167struct module {
168 uint8_t name[12];
169 uint8_t type;
170 uint8_t hash_alg;
171 uint16_t hash_size;
172 uint32_t metadata_size;
173 uint8_t metadata_hash[32];
Stefan Reinauer6a001132017-07-13 02:20:27 +0200174} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700175
176#define MODULE_SIZE (sizeof(struct module))
177
178struct signed_pkg_info_ext {
179 uint32_t ext_type;
180 uint32_t ext_length;
181 uint8_t name[4];
182 uint32_t vcn;
183 uint8_t bitmap[16];
184 uint32_t svn;
185 uint8_t rsvd[16];
Stefan Reinauer6a001132017-07-13 02:20:27 +0200186} __packed;
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700187
188#define SIGNED_PKG_INFO_EXT_TYPE 0x15
189#define SIGNED_PKG_INFO_EXT_SIZE \
190 (sizeof(struct signed_pkg_info_ext))
191
192/*
193 * Attributes for various IFWI sub-partitions.
194 * LIES_WITHIN_BPDT_4K = Sub-Partition should lie within the same 4K block as
195 * BPDT.
196 * NON_CRITICAL_SUBPART = Sub-Partition entry should be present in S-BPDT.
197 * CONTAINS_DIR = Sub-Partition contains directory.
198 * AUTO_GENERATED = Sub-Partition is generated by the tool.
199 * MANDATORY_BPDT_ENTRY = Even if sub-partition is deleted, BPDT should contain
200 * an entry for it with size 0 and offset 0.
201 */
202enum subpart_attributes {
203 LIES_WITHIN_BPDT_4K = (1 << 0),
204 NON_CRITICAL_SUBPART = (1 << 1),
205 CONTAINS_DIR = (1 << 2),
206 AUTO_GENERATED = (1 << 3),
207 MANDATORY_BPDT_ENTRY = (1 << 4),
208};
209
210/* Type value for various IFWI sub-partitions. */
211enum bpdt_entry_type {
212 SMIP_TYPE = 0,
213 CSE_RBE_TYPE = 1,
214 CSE_BUP_TYPE = 2,
215 UCODE_TYPE = 3,
216 IBB_TYPE = 4,
217 S_BPDT_TYPE = 5,
218 OBB_TYPE = 6,
219 CSE_MAIN_TYPE = 7,
220 ISH_TYPE = 8,
221 CSE_IDLM_TYPE = 9,
222 IFP_OVERRIDE_TYPE = 10,
223 DEBUG_TOKENS_TYPE = 11,
224 UFS_PHY_TYPE = 12,
225 UFS_GPP_TYPE = 13,
226 PMC_TYPE = 14,
227 IUNIT_TYPE = 15,
228 NVM_CONFIG_TYPE = 16,
229 UEP_TYPE = 17,
230 UFS_RATE_B_TYPE = 18,
231 MAX_SUBPARTS = 19,
232};
233
234/*
235 * There are two order requirements for an IFWI image:
236 * 1. Order in which the sub-partitions lie within the BPDT entries.
237 * 2. Order in which the sub-partitions lie within the image.
238 *
239 * header_order defines #1 i.e. the order in which the sub-partitions should
240 * appear in the BPDT entries. pack_order defines #2 i.e. the order in which
241 * sub-partitions appear in the IFWI image. pack_order controls the offset and
242 * thus sub-partitions would have increasing offsets as we loop over pack_order.
243 */
244const enum bpdt_entry_type bpdt_header_order[MAX_SUBPARTS] = {
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700245 /* Order of the following entries is mandatory. */
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700246 CSE_IDLM_TYPE,
247 IFP_OVERRIDE_TYPE,
248 S_BPDT_TYPE,
249 CSE_RBE_TYPE,
250 UFS_PHY_TYPE,
251 UFS_GPP_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700252 /* Order of the following entries is recommended. */
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700253 UEP_TYPE,
254 NVM_CONFIG_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700255 UFS_RATE_B_TYPE,
256 IBB_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700257 SMIP_TYPE,
258 PMC_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700259 CSE_BUP_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700260 UCODE_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700261 DEBUG_TOKENS_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700262 IUNIT_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700263 CSE_MAIN_TYPE,
264 ISH_TYPE,
265 OBB_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700266};
267
268const enum bpdt_entry_type bpdt_pack_order[MAX_SUBPARTS] = {
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700269 /* Order of the following entries is mandatory. */
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700270 UFS_GPP_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700271 UFS_PHY_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700272 IFP_OVERRIDE_TYPE,
273 UEP_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700274 NVM_CONFIG_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700275 UFS_RATE_B_TYPE,
276 /* Order of the following entries is recommended. */
277 IBB_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700278 SMIP_TYPE,
279 CSE_RBE_TYPE,
280 PMC_TYPE,
281 CSE_BUP_TYPE,
282 UCODE_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700283 CSE_IDLM_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700284 DEBUG_TOKENS_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700285 S_BPDT_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700286 IUNIT_TYPE,
Furquan Shaikhfe42b112016-06-10 11:05:16 -0700287 CSE_MAIN_TYPE,
288 ISH_TYPE,
289 OBB_TYPE,
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700290};
291
292/* Utility functions. */
293enum ifwi_ret {
294 COMMAND_ERR = -1,
295 NO_ACTION_REQUIRED = 0,
296 REPACK_REQUIRED = 1,
297};
298
299struct dir_ops {
300 enum ifwi_ret (*dir_add)(int);
301};
302
303static enum ifwi_ret ibbp_dir_add(int);
304
305const struct subpart_info {
306 const char *name;
307 const char *readable_name;
308 uint32_t attr;
309 struct dir_ops dir_ops;
310} subparts[MAX_SUBPARTS] = {
311 /* OEM SMIP */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700312 [SMIP_TYPE] = {"SMIP", "SMIP", CONTAINS_DIR, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700313 /* CSE RBE */
314 [CSE_RBE_TYPE] = {"RBEP", "CSE_RBE", CONTAINS_DIR |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700315 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700316 /* CSE BUP */
317 [CSE_BUP_TYPE] = {"FTPR", "CSE_BUP", CONTAINS_DIR |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700318 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700319 /* uCode */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700320 [UCODE_TYPE] = {"UCOD", "Microcode", CONTAINS_DIR, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700321 /* IBB */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700322 [IBB_TYPE] = {"IBBP", "Bootblock", CONTAINS_DIR, {ibbp_dir_add} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700323 /* S-BPDT */
324 [S_BPDT_TYPE] = {"S_BPDT", "S-BPDT", AUTO_GENERATED |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700325 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700326 /* OBB */
327 [OBB_TYPE] = {"OBBP", "OEM boot block", CONTAINS_DIR |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700328 NON_CRITICAL_SUBPART, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700329 /* CSE Main */
330 [CSE_MAIN_TYPE] = {"NFTP", "CSE_MAIN", CONTAINS_DIR |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700331 NON_CRITICAL_SUBPART, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700332 /* ISH */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700333 [ISH_TYPE] = {"ISHP", "ISH", NON_CRITICAL_SUBPART, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700334 /* CSE IDLM */
335 [CSE_IDLM_TYPE] = {"DLMP", "CSE_IDLM", CONTAINS_DIR |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700336 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700337 /* IFP Override */
338 [IFP_OVERRIDE_TYPE] = {"IFP_OVERRIDE", "IFP_OVERRIDE",
339 LIES_WITHIN_BPDT_4K | MANDATORY_BPDT_ENTRY,
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700340 {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700341 /* Debug Tokens */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700342 [DEBUG_TOKENS_TYPE] = {"DEBUG_TOKENS", "Debug Tokens", 0, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700343 /* UFS Phy Configuration */
344 [UFS_PHY_TYPE] = {"UFS_PHY", "UFS Phy", LIES_WITHIN_BPDT_4K |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700345 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700346 /* UFS GPP LUN ID */
347 [UFS_GPP_TYPE] = {"UFS_GPP", "UFS GPP", LIES_WITHIN_BPDT_4K |
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700348 MANDATORY_BPDT_ENTRY, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700349 /* PMC */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700350 [PMC_TYPE] = {"PMCP", "PMC firmware", CONTAINS_DIR, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700351 /* IUNIT */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700352 [IUNIT_TYPE] = {"IUNP", "IUNIT", NON_CRITICAL_SUBPART, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700353 /* NVM Config */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700354 [NVM_CONFIG_TYPE] = {"NVM_CONFIG", "NVM Config", 0, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700355 /* UEP */
356 [UEP_TYPE] = {"UEP", "UEP", LIES_WITHIN_BPDT_4K | MANDATORY_BPDT_ENTRY,
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700357 {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700358 /* UFS Rate B Config */
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700359 [UFS_RATE_B_TYPE] = {"UFS_RATE_B", "UFS Rate B Config", 0, {NULL} },
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700360};
361
362struct ifwi_image {
363 /* Data read from input file. */
364 struct buffer input_buff;
365
366 /* BPDT header and entries. */
367 struct buffer bpdt;
368 size_t input_ifwi_start_offset;
369 size_t input_ifwi_end_offset;
370
371 /* Subpartition content. */
372 struct buffer subpart_buf[MAX_SUBPARTS];
373} ifwi_image;
374
375static void alloc_buffer(struct buffer *b, size_t s, const char *n)
376{
377 if (buffer_create(b, s, n) == 0)
378 return;
379
380 ERROR("Buffer allocation failure for %s (size = %zx).\n", n, s);
381 exit(-1);
382}
383
384/*
385 * Read header/entry members in little-endian format.
386 * Returns the offset upto which the read was performed.
387*/
388static size_t read_member(void *src, size_t offset, size_t size_bytes,
389 void *dst)
390{
391 switch (size_bytes) {
392 case 1:
393 *(uint8_t *)dst = read_at_le8(src, offset);
394 break;
395 case 2:
396 *(uint16_t *)dst = read_at_le16(src, offset);
397 break;
398 case 4:
399 *(uint32_t *)dst = read_at_le32(src, offset);
400 break;
401 case 8:
402 *(uint64_t *)dst = read_at_le64(src, offset);
403 break;
404 default:
405 ERROR("Read size not supported %zd\n", size_bytes);
406 exit(-1);
407 }
408
409 return (offset + size_bytes);
410}
411
412/*
413 * Convert to little endian format.
414 * Returns the offset upto which the fixup was performed.
415 */
416static size_t fix_member(void *data, size_t offset, size_t size_bytes)
417{
418 uint8_t *src = (uint8_t *)data + offset;
419
420 switch (size_bytes) {
421 case 1:
422 write_at_le8(data, *(uint8_t *)src, offset);
423 break;
424 case 2:
425 write_at_le16(data, *(uint16_t *)src, offset);
426 break;
427 case 4:
428 write_at_le32(data, *(uint32_t *)src, offset);
429 break;
430 case 8:
431 write_at_le64(data, *(uint64_t *)src, offset);
432 break;
433 default:
434 ERROR("Write size not supported %zd\n", size_bytes);
435 exit(-1);
436 }
437 return (offset + size_bytes);
438}
439
440
441static void print_subpart_dir(struct subpart_dir *s)
442{
443 if (verbose == 0)
444 return;
445
446 size_t i;
447
448 printf("%-25s 0x%-23.8x\n", "Marker", s->h.marker);
449 printf("%-25s %-25d\n", "Num entries", s->h.num_entries);
450 printf("%-25s %-25d\n", "Header Version", s->h.header_version);
451 printf("%-25s %-25d\n", "Entry Version", s->h.entry_version);
452 printf("%-25s 0x%-23x\n", "Header Length", s->h.header_length);
453 printf("%-25s 0x%-23x\n", "Checksum", s->h.checksum);
454 printf("%-25s ", "Name");
455 for (i = 0; i < sizeof(s->h.name); i++)
456 printf("%c", s->h.name[i]);
457
458 printf("\n");
459
460 printf("%-25s%-25s%-25s%-25s%-25s\n", "Entry #", "Name", "Offset",
461 "Length", "Rsvd");
462
463 printf("=============================================================="
464 "===========================================================\n");
465
466 for (i = 0; i < s->h.num_entries; i++) {
467 printf("%-25zd%-25.12s0x%-23x0x%-23x0x%-23x\n", i+1,
468 s->e[i].name, s->e[i].offset, s->e[i].length,
469 s->e[i].rsvd);
470 }
471
472 printf("=============================================================="
473 "===========================================================\n");
474}
475
476static void bpdt_print_header(struct bpdt_header *h, const char *name)
477{
478 if (verbose == 0)
479 return;
480
481 printf("%-25s %-25s\n", "Header", name);
482 printf("%-25s 0x%-23.8x\n", "Signature", h->signature);
483 printf("%-25s %-25d\n", "Descriptor count", h->descriptor_count);
484 printf("%-25s %-25d\n", "BPDT Version", h->bpdt_version);
485 printf("%-25s 0x%-23x\n", "XOR checksum", h->xor_redundant_block);
486 printf("%-25s 0x%-23x\n", "IFWI Version", h->ifwi_version);
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700487 printf("%-25s 0x%-23llx\n", "FIT Tool Version",
488 (long long)h->fit_tool_version);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700489}
490
491static void bpdt_print_entries(struct bpdt_entry *e, size_t count,
492 const char *name)
493{
494 if (verbose == 0)
495 return;
496
497 printf("%s entries\n", name);
498
499 printf("%-25s%-25s%-25s%-25s%-25s%-25s%-25s%-25s\n", "Entry #",
500 "Sub-Partition", "Name", "Type", "Flags", "Offset", "Size",
501 "File Offset");
502
503 printf("=============================================================="
504 "=============================================================="
505 "=============================================================="
506 "===============\n");
507
508
509 size_t i;
510 for (i = 0; i < count; i++) {
511 printf("%-25zd%-25s%-25s%-25d0x%-23.08x0x%-23x0x%-23x0x%-23zx"
512 "\n", i+1, subparts[e[i].type].name,
513 subparts[e[i].type].readable_name, e[i].type, e[i].flags,
514 e[i].offset, e[i].size,
515 e[i].offset + ifwi_image.input_ifwi_start_offset);
516 }
517
518 printf("=============================================================="
519 "=============================================================="
520 "=============================================================="
521 "===============\n");
522
523}
524
525static void bpdt_validate_header(struct bpdt_header *h, const char *name)
526{
Furquan Shaikh55d2e532016-05-31 11:02:47 -0700527 assert(h->signature == BPDT_SIGNATURE);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700528
529 if (h->bpdt_version != 1) {
530 ERROR("Invalid header : %s\n", name);
531 exit(-1);
532 }
533
534 DEBUG("Validated header : %s\n", name);
535}
536
537static void bpdt_read_header(void *data, struct bpdt_header *h,
538 const char *name)
539{
540 size_t offset = 0;
541
542 offset = read_member(data, offset, sizeof(h->signature), &h->signature);
543 offset = read_member(data, offset, sizeof(h->descriptor_count),
544 &h->descriptor_count);
545 offset = read_member(data, offset, sizeof(h->bpdt_version),
546 &h->bpdt_version);
547 offset = read_member(data, offset, sizeof(h->xor_redundant_block),
548 &h->xor_redundant_block);
549 offset = read_member(data, offset, sizeof(h->ifwi_version),
550 &h->ifwi_version);
Paul Menzeldeb9b032017-03-08 10:51:52 +0100551 read_member(data, offset, sizeof(h->fit_tool_version),
552 &h->fit_tool_version);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700553
554 bpdt_validate_header(h, name);
555 bpdt_print_header(h, name);
556}
557
558static void bpdt_read_entries(void *data, struct bpdt *bpdt, const char *name)
559{
560 size_t i, offset = 0;
561 struct bpdt_entry *e = &bpdt->e[0];
562 size_t count = bpdt->h.descriptor_count;
563
564 for (i = 0; i < count; i++) {
565 offset = read_member(data, offset, sizeof(e[i].type),
566 &e[i].type);
567 offset = read_member(data, offset, sizeof(e[i].flags),
568 &e[i].flags);
569 offset = read_member(data, offset, sizeof(e[i].offset),
570 &e[i].offset);
571 offset = read_member(data, offset, sizeof(e[i].size),
572 &e[i].size);
573 }
574
575 bpdt_print_entries(e, count, name);
576}
577
578/*
579 * Given type of sub-partition, identify BPDT entry for it.
580 * Sub-Partition could lie either within BPDT or S-BPDT.
581 */
582static struct bpdt_entry *__find_entry_by_type(struct bpdt_entry *e,
583 size_t count, int type)
584{
585 size_t i;
586 for (i = 0; i < count; i++) {
587 if (e[i].type == type)
588 break;
589 }
590
591 if (i == count)
592 return NULL;
593
594 return &e[i];
595}
596
597static struct bpdt_entry *find_entry_by_type(int type)
598{
599 struct bpdt *b = buffer_get(&ifwi_image.bpdt);
600 if (b == NULL)
601 return NULL;
602
603 struct bpdt_entry *curr = __find_entry_by_type(&b->e[0],
604 b->h.descriptor_count,
605 type);
606
607 if (curr)
608 return curr;
609
610 b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
611 if (b == NULL)
612 return NULL;
613
614 return __find_entry_by_type(&b->e[0], b->h.descriptor_count, type);
615}
616
617/*
618 * Find sub-partition type given its name. If the name does not exist, returns
619 * -1.
620 */
621static int find_type_by_name(const char *name)
622{
623 int i;
624
625 for (i = 0; i < MAX_SUBPARTS; i++) {
626 if ((strlen(subparts[i].name) == strlen(name)) &&
627 (!strcmp(subparts[i].name, name)))
628 break;
629 }
630
631 if (i == MAX_SUBPARTS) {
632 ERROR("Invalid sub-partition name %s.\n", name);
633 return -1;
634 }
635
636 return i;
637}
638
639/*
640 * Read the content of a sub-partition from input file and store it in
641 * ifwi_image.subpart_buf[SUB-PARTITION_TYPE].
642 *
643 * Returns the maximum offset occupied by the sub-partitions.
644 */
645static size_t read_subpart_buf(void *data, size_t size, struct bpdt_entry *e,
646 size_t count)
647{
648 size_t i, type;
649 struct buffer *buf;
650 size_t max_offset = 0;
651
652 for (i = 0; i < count; i++) {
653 type = e[i].type;
654
655 if (type >= MAX_SUBPARTS) {
656 ERROR("Invalid sub-partition type %zd.\n", type);
657 exit(-1);
658 }
659
660 if (buffer_size(&ifwi_image.subpart_buf[type])) {
661 ERROR("Multiple sub-partitions of type %zd(%s).\n",
662 type, subparts[type].name);
663 exit(-1);
664 }
665
666 if (e[i].size == 0) {
667 INFO("Dummy sub-partition %zd(%s). Skipping.\n", type,
668 subparts[type].name);
669 continue;
670 }
671
672 assert((e[i].offset + e[i].size) <= size);
673
674 /*
675 * Sub-partitions in IFWI image are not in the same order as
676 * in BPDT entries. BPDT entires are in header_order whereas
677 * sub-partition offsets in the image are in pack_order.
678 */
679 if ((e[i].offset + e[i].size) > max_offset)
680 max_offset = e[i].offset + e[i].size;
681
682 /*
683 * S-BPDT sub-partition contains information about all the
684 * non-critical sub-partitions. Thus, size of S-BPDT
685 * sub-partition equals size of S-BPDT plus size of all the
686 * non-critical sub-partitions. Thus, reading whole of S-BPDT
687 * here would be redundant as the non-critical partitions are
688 * read and allocated buffers separately. Also, S-BPDT requires
689 * special handling for reading header and entries.
690 */
691 if (type == S_BPDT_TYPE)
692 continue;
693
694 buf = &ifwi_image.subpart_buf[type];
695
696 alloc_buffer(buf, e[i].size, subparts[type].name);
697 memcpy(buffer_get(buf), (uint8_t *)data + e[i].offset,
698 e[i].size);
699 }
700
701 assert(max_offset);
702 return max_offset;
703}
704
705/*
706 * Allocate buffer for bpdt header, entries and all sub-partition content.
707 * Returns offset in data where BPDT ends.
708 */
709static size_t alloc_bpdt_buffer(void *data, size_t size, size_t offset,
710 struct buffer *b, const char *name)
711{
712 struct bpdt_header bpdt_header;
713 assert((offset + BPDT_HEADER_SIZE) < size);
714 bpdt_read_header((uint8_t *)data + offset, &bpdt_header, name);
715
716 /* Buffer to read BPDT header and entries. */
Werner Zeh310580e2016-06-24 13:03:38 +0200717 alloc_buffer(b, get_bpdt_size(&bpdt_header), name);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700718
719 struct bpdt *bpdt = buffer_get(b);
720 memcpy(&bpdt->h, &bpdt_header, BPDT_HEADER_SIZE);
721
722 /*
723 * If no entries are present, maximum offset occupied is (offset +
724 * BPDT_HEADER_SIZE).
725 */
726 if (bpdt->h.descriptor_count == 0)
727 return (offset + BPDT_HEADER_SIZE);
728
729 /* Read all entries. */
Werner Zeh310580e2016-06-24 13:03:38 +0200730 assert((offset + get_bpdt_size(&bpdt->h)) < size);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700731 bpdt_read_entries((uint8_t *)data + offset + BPDT_HEADER_SIZE, bpdt,
732 name);
733
734 /* Read all sub-partition content in subpart_buf. */
735 return read_subpart_buf(data, size, &bpdt->e[0],
736 bpdt->h.descriptor_count);
737}
738
739static void parse_sbpdt(void *data, size_t size)
740{
741 struct bpdt_entry *s;
742 s = find_entry_by_type(S_BPDT_TYPE);
743 if (!s)
744 return;
745
746 assert(size > s->offset);
747
748 alloc_bpdt_buffer(data, size, s->offset,
749 &ifwi_image.subpart_buf[S_BPDT_TYPE],
750 "S-BPDT");
751}
752
Furquan Shaikhe51c20b2016-06-15 08:01:10 -0700753static uint8_t calc_checksum(struct subpart_dir *s)
754{
755 size_t size = subpart_dir_size(&s->h);
756 uint8_t *data = (uint8_t *)s;
757 uint8_t checksum = 0;
758 size_t i;
759
760 uint8_t old_checksum = s->h.checksum;
761 s->h.checksum = 0;
762
763 for (i = 0; i < size; i++)
764 checksum += data[i];
765
766 s->h.checksum = old_checksum;
767
768 /* 2s complement */
769 return -checksum;
770}
771
772static void validate_subpart_dir(struct subpart_dir *s, const char *name,
773 bool checksum_check)
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700774{
775 if ((s->h.marker != SUBPART_DIR_MARKER) ||
776 (s->h.header_version != SUBPART_DIR_HEADER_VERSION_SUPPORTED) ||
777 (s->h.entry_version != SUBPART_DIR_ENTRY_VERSION_SUPPORTED) ||
778 (s->h.header_length != SUBPART_DIR_HEADER_SIZE)) {
779 ERROR("Invalid subpart_dir for %s.\n", name);
780 exit(-1);
781 }
Furquan Shaikhe51c20b2016-06-15 08:01:10 -0700782
783 if (checksum_check == false)
784 return;
785
786 uint8_t checksum = calc_checksum(s);
787
788 if (checksum != s->h.checksum)
789 ERROR("Invalid checksum for %s (Expected=0x%x, Actual=0x%x).\n",
790 name, checksum, s->h.checksum);
791}
792
793static void validate_subpart_dir_without_checksum(struct subpart_dir *s,
794 const char *name)
795{
796 validate_subpart_dir(s, name, 0);
797}
798
799static void validate_subpart_dir_with_checksum(struct subpart_dir *s,
800 const char *name)
801{
802 validate_subpart_dir(s, name, 1);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700803}
804
805static void parse_subpart_dir(struct buffer *subpart_dir_buf,
806 struct buffer *input_buf, const char *name)
807{
808 struct subpart_dir_header hdr;
809 size_t offset = 0;
810 uint8_t *data = buffer_get(input_buf);
811 size_t size = buffer_size(input_buf);
812
813 /* Read Subpart_Dir header. */
814 assert(size >= SUBPART_DIR_HEADER_SIZE);
815 offset = read_member(data, offset, sizeof(hdr.marker), &hdr.marker);
816 offset = read_member(data, offset, sizeof(hdr.num_entries),
817 &hdr.num_entries);
818 offset = read_member(data, offset, sizeof(hdr.header_version),
819 &hdr.header_version);
820 offset = read_member(data, offset, sizeof(hdr.entry_version),
821 &hdr.entry_version);
822 offset = read_member(data, offset, sizeof(hdr.header_length),
823 &hdr.header_length);
824 offset = read_member(data, offset, sizeof(hdr.checksum), &hdr.checksum);
825 memcpy(hdr.name, data + offset, sizeof(hdr.name));
826 offset += sizeof(hdr.name);
827
Furquan Shaikhe51c20b2016-06-15 08:01:10 -0700828 validate_subpart_dir_without_checksum((struct subpart_dir *)&hdr, name);
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700829
830 assert(size > subpart_dir_size(&hdr));
831 alloc_buffer(subpart_dir_buf, subpart_dir_size(&hdr), "Subpart Dir");
832 memcpy(buffer_get(subpart_dir_buf), &hdr, SUBPART_DIR_HEADER_SIZE);
833
834 /* Read Subpart Dir entries. */
835 struct subpart_dir *subpart_dir = buffer_get(subpart_dir_buf);
836 struct subpart_dir_entry *e = &subpart_dir->e[0];
837 uint32_t i;
838 for (i = 0; i < hdr.num_entries; i++) {
839 memcpy(e[i].name, data + offset, sizeof(e[i].name));
840 offset += sizeof(e[i].name);
841 offset = read_member(data, offset, sizeof(e[i].offset),
842 &e[i].offset);
843 offset = read_member(data, offset, sizeof(e[i].length),
844 &e[i].length);
845 offset = read_member(data, offset, sizeof(e[i].rsvd),
846 &e[i].rsvd);
847 }
848
Furquan Shaikhe51c20b2016-06-15 08:01:10 -0700849 validate_subpart_dir_with_checksum(subpart_dir, name);
850
Furquan Shaikh233f1b62016-05-19 16:12:16 -0700851 print_subpart_dir(subpart_dir);
852}
853
854/* Parse input image file to identify different sub-partitions. */
855static int ifwi_parse(void)
856{
857 DEBUG("Parsing IFWI image...\n");
858 const char *image_name = param.image_name;
859
860 /* Read input file. */
861 struct buffer *buff = &ifwi_image.input_buff;
862 if (buffer_from_file(buff, image_name)) {
863 ERROR("Failed to read input file %s.\n", image_name);
864 return -1;
865 }
866
867 INFO("Buffer %p size 0x%zx\n", buff->data, buff->size);
868
869 /* Look for BPDT signature at 4K intervals. */
870 size_t offset = 0;
871 void *data = buffer_get(buff);
872
873 while (offset < buffer_size(buff)) {
874 if (read_at_le32(data, offset) == BPDT_SIGNATURE)
875 break;
876 offset += 4 * KiB;
877 }
878
879 if (offset >= buffer_size(buff)) {
880 ERROR("Image does not contain BPDT!!\n");
881 return -1;
882 }
883
884 ifwi_image.input_ifwi_start_offset = offset;
885 INFO("BPDT starts at offset 0x%zx.\n", offset);
886
887 data = (uint8_t *)data + offset;
888 size_t ifwi_size = buffer_size(buff) - offset;
889
890 /* Read BPDT and sub-partitions. */
891 uintptr_t end_offset;
892 end_offset = ifwi_image.input_ifwi_start_offset +
893 alloc_bpdt_buffer(data, ifwi_size, 0, &ifwi_image.bpdt, "BPDT");
894
895 /* Parse S-BPDT, if any. */
896 parse_sbpdt(data, ifwi_size);
897
898 /*
899 * Store end offset of IFWI. Required for copying any trailing non-IFWI
900 * part of the image.
901 * ASSUMPTION: IFWI image always ends on a 4K boundary.
902 */
903 ifwi_image.input_ifwi_end_offset = ALIGN(end_offset, 4 * KiB);
904 DEBUG("Parsing done.\n");
905
906 return 0;
907}
908
909/*
910 * This function is used by repack to count the number of BPDT and S-BPDT
911 * entries that are present. It frees the current buffers used by the entries
912 * and allocates fresh buffers that can be used for repacking. Returns BPDT
913 * entries which are empty and need to be filled in.
914 */
915static void __bpdt_reset(struct buffer *b, size_t count, size_t size)
916{
917 size_t bpdt_size = BPDT_HEADER_SIZE + count * BPDT_ENTRY_SIZE;
918 assert(size >= bpdt_size);
919
920 /*
921 * If buffer does not have the required size, allocate a fresh buffer.
922 */
923 if (buffer_size(b) != size) {
924 struct buffer temp;
925 alloc_buffer(&temp, size, b->name);
926 memcpy(buffer_get(&temp), buffer_get(b), buffer_size(b));
927 buffer_delete(b);
928 *b = temp;
929 }
930
931 struct bpdt *bpdt = buffer_get(b);
932 uint8_t *ptr = (uint8_t *)&bpdt->e[0];
933 size_t entries_size = BPDT_ENTRY_SIZE * count;
934
935 /* Zero out BPDT entries. */
936 memset(ptr, 0, entries_size);
937 /* Fill any pad-space with FF. */
938 memset(ptr + entries_size, 0xFF, size - bpdt_size);
939
940 bpdt->h.descriptor_count = count;
941}
942
943static void bpdt_reset(void)
944{
945 size_t i;
946 size_t bpdt_count = 0, sbpdt_count = 0, dummy_bpdt_count = 0;
947
948 /* Count number of BPDT and S-BPDT entries. */
949 for (i = 0; i < MAX_SUBPARTS; i++) {
950 if (buffer_size(&ifwi_image.subpart_buf[i]) == 0) {
951 if (subparts[i].attr & MANDATORY_BPDT_ENTRY) {
952 bpdt_count++;
953 dummy_bpdt_count++;
954 }
955 continue;
956 }
957
958 if (subparts[i].attr & NON_CRITICAL_SUBPART)
959 sbpdt_count++;
960 else
961 bpdt_count++;
962 }
963
964 DEBUG("Count: BPDT = %zd, Dummy BPDT = %zd, S-BPDT = %zd\n", bpdt_count,
965 dummy_bpdt_count, sbpdt_count);
966
967 /* Update BPDT if required. */
968 size_t bpdt_size = MAX(BPDT_MIN_SIZE,
969 BPDT_HEADER_SIZE + bpdt_count * BPDT_ENTRY_SIZE);
970 __bpdt_reset(&ifwi_image.bpdt, bpdt_count, bpdt_size);
971
972 /* Update S-BPDT if required. */
973 bpdt_size = ALIGN(BPDT_HEADER_SIZE + sbpdt_count * BPDT_ENTRY_SIZE,
974 4 * KiB);
975 __bpdt_reset(&ifwi_image.subpart_buf[S_BPDT_TYPE], sbpdt_count,
976 bpdt_size);
977}
978
979/* Initialize BPDT entries in header order. */
980static void bpdt_entries_init_header_order(void)
981{
982 int i, type;
983 size_t size;
984
985 struct bpdt *bpdt, *sbpdt, *curr;
986 size_t bpdt_curr = 0, sbpdt_curr = 0, *count_ptr;
987
988 bpdt = buffer_get(&ifwi_image.bpdt);
989 sbpdt = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
990
991 for (i = 0; i < MAX_SUBPARTS; i++) {
992 type = bpdt_header_order[i];
993 size = buffer_size(&ifwi_image.subpart_buf[type]);
994
995 if ((size == 0) && !(subparts[type].attr &
996 MANDATORY_BPDT_ENTRY))
997 continue;
998
999 if (subparts[type].attr & NON_CRITICAL_SUBPART) {
1000 curr = sbpdt;
1001 count_ptr = &sbpdt_curr;
1002 } else {
1003 curr = bpdt;
1004 count_ptr = &bpdt_curr;
1005 }
1006
1007 assert(*count_ptr < curr->h.descriptor_count);
1008 curr->e[*count_ptr].type = type;
1009 curr->e[*count_ptr].flags = 0;
1010 curr->e[*count_ptr].offset = 0;
1011 curr->e[*count_ptr].size = size;
1012
1013 (*count_ptr)++;
1014 }
1015}
1016
1017static void pad_buffer(struct buffer *b, size_t size)
1018{
1019 size_t buff_size = buffer_size(b);
1020
1021 assert(buff_size <= size);
1022
1023 if (buff_size == size)
1024 return;
1025
1026 struct buffer temp;
1027 alloc_buffer(&temp, size, b->name);
1028 uint8_t *data = buffer_get(&temp);
1029
1030 memcpy(data, buffer_get(b), buff_size);
1031 memset(data + buff_size, 0xFF, size - buff_size);
1032
1033 *b = temp;
1034}
1035
1036/* Initialize offsets of entries using pack order. */
1037static void bpdt_entries_init_pack_order(void)
1038{
1039 int i, type;
1040 struct bpdt_entry *curr;
1041 size_t curr_offset, curr_end;
1042
1043 curr_offset = MAX(BPDT_MIN_SIZE, buffer_size(&ifwi_image.bpdt));
1044
1045 /*
1046 * There are two types of sub-partitions that need to be handled here:
1047 * 1. Sub-partitions that lie within the same 4K as BPDT
1048 * 2. Sub-partitions that lie outside the 4K of BPDT
1049 *
1050 * For sub-partitions of type # 1, there is no requirement on the start
1051 * or end of the sub-partition. They need to be packed in without any
1052 * holes left in between. If there is any empty space left after the end
1053 * of the last sub-partition in 4K of BPDT, then that space needs to be
1054 * padded with FF bytes, but the size of the last sub-partition remains
1055 * unchanged.
1056 *
1057 * For sub-partitions of type # 2, both the start and end should be a
1058 * multiple of 4K. If not, then it needs to be padded with FF bytes and
1059 * size adjusted such that the sub-partition ends on 4K boundary.
1060 */
1061
1062 /* #1 Sub-partitions that lie within same 4K as BPDT. */
1063 struct buffer *last_bpdt_buff = &ifwi_image.bpdt;
1064
1065 for (i = 0; i < MAX_SUBPARTS; i++) {
1066 type = bpdt_pack_order[i];
1067 curr = find_entry_by_type(type);
1068
1069 if ((curr == NULL) || (curr->size == 0))
1070 continue;
1071
1072 if (!(subparts[type].attr & LIES_WITHIN_BPDT_4K))
1073 continue;
1074
1075 curr->offset = curr_offset;
1076 curr_offset = curr->offset + curr->size;
1077 last_bpdt_buff = &ifwi_image.subpart_buf[type];
1078 DEBUG("type=%d, curr_offset=0x%zx, curr->offset=0x%x, "
1079 "curr->size=0x%x, buff_size=0x%zx\n", type, curr_offset,
1080 curr->offset, curr->size,
1081 buffer_size(&ifwi_image.subpart_buf[type]));
1082 }
1083
1084 /* Pad ff bytes if there is any empty space left in BPDT 4K. */
1085 curr_end = ALIGN(curr_offset, 4 * KiB);
1086 pad_buffer(last_bpdt_buff,
1087 buffer_size(last_bpdt_buff) + (curr_end - curr_offset));
1088 curr_offset = curr_end;
1089
1090 /* #2 Sub-partitions that lie outside of BPDT 4K. */
1091 for (i = 0; i < MAX_SUBPARTS; i++) {
1092 type = bpdt_pack_order[i];
1093 curr = find_entry_by_type(type);
1094
1095 if ((curr == NULL) || (curr->size == 0))
1096 continue;
1097
1098 if (subparts[type].attr & LIES_WITHIN_BPDT_4K)
1099 continue;
1100
1101 assert(curr_offset == ALIGN(curr_offset, 4 * KiB));
1102 curr->offset = curr_offset;
1103 curr_end = ALIGN(curr->offset + curr->size, 4 * KiB);
1104 curr->size = curr_end - curr->offset;
1105
1106 pad_buffer(&ifwi_image.subpart_buf[type], curr->size);
1107
1108 curr_offset = curr_end;
1109 DEBUG("type=%d, curr_offset=0x%zx, curr->offset=0x%x, "
1110 "curr->size=0x%x, buff_size=0x%zx\n", type, curr_offset,
1111 curr->offset, curr->size,
1112 buffer_size(&ifwi_image.subpart_buf[type]));
1113 }
1114
1115 /*
1116 * Update size of S-BPDT to include size of all non-critical
1117 * sub-partitions.
1118 *
1119 * Assumption: S-BPDT always lies at the end of IFWI image.
1120 */
1121 curr = find_entry_by_type(S_BPDT_TYPE);
1122 assert(curr);
1123
1124 assert(curr_offset == ALIGN(curr_offset, 4 * KiB));
1125 curr->size = curr_offset - curr->offset;
1126}
1127
1128/* Convert all members of BPDT to little-endian format. */
1129static void bpdt_fixup_write_buffer(struct buffer *buf)
1130{
1131 struct bpdt *s = buffer_get(buf);
1132
1133 struct bpdt_header *h = &s->h;
1134 struct bpdt_entry *e = &s->e[0];
1135
1136 size_t count = h->descriptor_count;
1137
1138 size_t offset = 0;
1139
1140 offset = fix_member(&h->signature, offset, sizeof(h->signature));
1141 offset = fix_member(&h->descriptor_count, offset,
1142 sizeof(h->descriptor_count));
1143 offset = fix_member(&h->bpdt_version, offset, sizeof(h->bpdt_version));
1144 offset = fix_member(&h->xor_redundant_block, offset,
1145 sizeof(h->xor_redundant_block));
1146 offset = fix_member(&h->ifwi_version, offset, sizeof(h->ifwi_version));
1147 offset = fix_member(&h->fit_tool_version, offset,
1148 sizeof(h->fit_tool_version));
1149
1150 uint32_t i;
1151 for (i = 0; i < count; i++) {
1152 offset = fix_member(&e[i].type, offset, sizeof(e[i].type));
1153 offset = fix_member(&e[i].flags, offset, sizeof(e[i].flags));
1154 offset = fix_member(&e[i].offset, offset, sizeof(e[i].offset));
1155 offset = fix_member(&e[i].size, offset, sizeof(e[i].size));
1156 }
1157}
1158
1159/* Write BPDT to output buffer after fixup. */
1160static void bpdt_write(struct buffer *dst, size_t offset, struct buffer *src)
1161{
1162 bpdt_fixup_write_buffer(src);
1163 memcpy(buffer_get(dst) + offset, buffer_get(src), buffer_size(src));
1164}
1165
1166/*
1167 * Follows these steps to re-create image:
1168 * 1. Write any non-IFWI prefix.
1169 * 2. Write out BPDT header and entries.
1170 * 3. Write sub-partition buffers to respective offsets.
1171 * 4. Write any non-IFWI suffix.
1172 *
1173 * While performing the above steps, make sure that any empty holes are filled
1174 * with FF.
1175 */
1176static void ifwi_write(const char *image_name)
1177{
1178 struct bpdt_entry *s = find_entry_by_type(S_BPDT_TYPE);
1179 assert(s);
1180
1181 size_t ifwi_start, ifwi_end, file_end;
1182
1183 ifwi_start = ifwi_image.input_ifwi_start_offset;
1184 ifwi_end = ifwi_start + ALIGN(s->offset + s->size, 4 * KiB);
1185 file_end = ifwi_end + (buffer_size(&ifwi_image.input_buff) -
1186 ifwi_image.input_ifwi_end_offset);
1187
1188 struct buffer b;
1189
1190 alloc_buffer(&b, file_end, "Final-IFWI");
1191
1192 uint8_t *input_data = buffer_get(&ifwi_image.input_buff);
1193 uint8_t *output_data = buffer_get(&b);
1194
1195 DEBUG("ifwi_start:0x%zx, ifwi_end:0x%zx, file_end:0x%zx\n", ifwi_start,
1196 ifwi_end, file_end);
1197
1198 /* Copy non-IFWI prefix, if any. */
1199 memcpy(output_data, input_data, ifwi_start);
1200
1201 DEBUG("Copied non-IFWI prefix (offset=0x0, size=0x%zx).\n", ifwi_start);
1202
1203 struct buffer ifwi;
1204 buffer_splice(&ifwi, &b, ifwi_start, ifwi_end - ifwi_start);
1205 uint8_t *ifwi_data = buffer_get(&ifwi);
1206
1207 /* Copy sub-partitions using pack_order. */
1208 struct bpdt_entry *curr;
1209 struct buffer *subpart_buf;
1210 int i, type;
1211 for (i = 0; i < MAX_SUBPARTS; i++) {
1212 type = bpdt_pack_order[i];
1213
1214 if (type == S_BPDT_TYPE)
1215 continue;
1216
1217 curr = find_entry_by_type(type);
1218
1219 if ((curr == NULL) || (curr->size == 0))
1220 continue;
1221
1222 subpart_buf = &ifwi_image.subpart_buf[type];
1223
1224 DEBUG("curr->offset=0x%x, curr->size=0x%x, type=%d, "
1225 "write_size=0x%zx\n", curr->offset, curr->size, type,
1226 buffer_size(subpart_buf));
1227
1228 assert((curr->offset + buffer_size(subpart_buf)) <=
1229 buffer_size(&ifwi));
1230
1231 memcpy(ifwi_data + curr->offset, buffer_get(subpart_buf),
1232 buffer_size(subpart_buf));
1233 }
1234
1235 /* Copy non-IFWI suffix, if any. */
1236 if (ifwi_end != file_end) {
1237 memcpy(output_data + ifwi_end,
1238 input_data + ifwi_image.input_ifwi_end_offset,
1239 file_end - ifwi_end);
1240 DEBUG("Copied non-IFWI suffix (offset=0x%zx,size=0x%zx).\n",
1241 ifwi_end, file_end - ifwi_end);
1242 }
1243
1244 /*
1245 * Convert BPDT to little-endian format and write it to output buffer.
1246 * S-BPDT is written first and then BPDT.
1247 */
1248 bpdt_write(&ifwi, s->offset, &ifwi_image.subpart_buf[S_BPDT_TYPE]);
1249 bpdt_write(&ifwi, 0, &ifwi_image.bpdt);
1250
1251 if (buffer_write_file(&b, image_name)) {
1252 ERROR("File write error\n");
1253 exit(-1);
1254 }
1255
1256 buffer_delete(&b);
1257 printf("Image written successfully to %s.\n", image_name);
1258}
1259
1260/*
1261 * Calculate size and offset of each sub-partition again since it might have
1262 * changed because of add/delete operation. Also, re-create BPDT and S-BPDT
1263 * entries and write back the new IFWI image to file.
1264 */
1265static void ifwi_repack(void)
1266{
1267 bpdt_reset();
1268 bpdt_entries_init_header_order();
1269 bpdt_entries_init_pack_order();
1270
1271 struct bpdt *b = buffer_get(&ifwi_image.bpdt);
1272 bpdt_print_entries(&b->e[0], b->h.descriptor_count, "BPDT");
1273
1274 b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
1275 bpdt_print_entries(&b->e[0], b->h.descriptor_count, "S-BPDT");
1276
1277 DEBUG("Repack done.. writing image.\n");
1278 ifwi_write(param.image_name);
1279}
1280
1281static void init_subpart_dir_header(struct subpart_dir_header *hdr,
1282 size_t count, const char *name)
1283{
1284 memset(hdr, 0, sizeof(*hdr));
1285
1286 hdr->marker = SUBPART_DIR_MARKER;
1287 hdr->num_entries = count;
1288 hdr->header_version = SUBPART_DIR_HEADER_VERSION_SUPPORTED;
1289 hdr->entry_version = SUBPART_DIR_ENTRY_VERSION_SUPPORTED;
1290 hdr->header_length = SUBPART_DIR_HEADER_SIZE;
1291 memcpy(hdr->name, name, sizeof(hdr->name));
1292}
1293
1294static size_t init_subpart_dir_entry(struct subpart_dir_entry *e,
1295 struct buffer *b, size_t offset)
1296{
1297 memset(e, 0, sizeof(*e));
1298
1299 assert(strlen(b->name) <= sizeof(e->name));
1300 strncpy((char *)e->name, (char *)b->name, sizeof(e->name));
1301 e->offset = offset;
1302 e->length = buffer_size(b);
1303
1304 return (offset + buffer_size(b));
1305}
1306
1307static void init_manifest_header(struct manifest_header *hdr, size_t size)
1308{
1309 memset(hdr, 0, sizeof(*hdr));
1310
1311 hdr->header_type = 0x4;
1312 assert((MANIFEST_HDR_SIZE % DWORD_SIZE) == 0);
1313 hdr->header_length = MANIFEST_HDR_SIZE / DWORD_SIZE;
1314 hdr->header_version = 0x10000;
1315 hdr->vendor = 0x8086;
1316
1317 struct tm *local_time;
1318 time_t curr_time;
1319 char buffer[11];
1320
1321 curr_time = time(NULL);
1322 local_time = localtime(&curr_time);
1323 strftime(buffer, sizeof(buffer), "0x%Y%m%d", local_time);
1324 hdr->date = strtoul(buffer, NULL, 16);
1325
1326 assert((size % DWORD_SIZE) == 0);
1327 hdr->size = size / DWORD_SIZE;
1328 hdr->id = MANIFEST_ID_MAGIC;
1329}
1330
1331static void init_signed_pkg_info_ext(struct signed_pkg_info_ext *ext,
1332 size_t count, const char *name)
1333{
1334 memset(ext, 0, sizeof(*ext));
1335
1336 ext->ext_type = SIGNED_PKG_INFO_EXT_TYPE;
1337 ext->ext_length = SIGNED_PKG_INFO_EXT_SIZE + count * MODULE_SIZE;
1338 memcpy(ext->name, name, sizeof(ext->name));
1339}
1340
1341static void subpart_dir_fixup_write_buffer(struct buffer *buf)
1342{
1343 struct subpart_dir *s = buffer_get(buf);
1344 struct subpart_dir_header *h = &s->h;
1345 struct subpart_dir_entry *e = &s->e[0];
1346
1347 size_t count = h->num_entries;
1348 size_t offset = 0;
1349
1350 offset = fix_member(&h->marker, offset, sizeof(h->marker));
1351 offset = fix_member(&h->num_entries, offset, sizeof(h->num_entries));
1352 offset = fix_member(&h->header_version, offset,
1353 sizeof(h->header_version));
1354 offset = fix_member(&h->entry_version, offset,
1355 sizeof(h->entry_version));
1356 offset = fix_member(&h->header_length, offset,
1357 sizeof(h->header_length));
1358 offset = fix_member(&h->checksum, offset, sizeof(h->checksum));
1359 offset += sizeof(h->name);
1360
1361 uint32_t i;
1362 for (i = 0; i < count; i++) {
1363 offset += sizeof(e[i].name);
1364 offset = fix_member(&e[i].offset, offset, sizeof(e[i].offset));
1365 offset = fix_member(&e[i].length, offset, sizeof(e[i].length));
1366 offset = fix_member(&e[i].rsvd, offset, sizeof(e[i].rsvd));
1367 }
1368}
1369
1370static void create_subpart(struct buffer *dst, struct buffer *info[],
1371 size_t count, const char *name)
1372{
1373 struct buffer subpart_dir_buff;
1374 size_t size = SUBPART_DIR_HEADER_SIZE + count * SUBPART_DIR_ENTRY_SIZE;
1375
1376 alloc_buffer(&subpart_dir_buff, size, "subpart-dir");
1377
1378 struct subpart_dir_header *h = buffer_get(&subpart_dir_buff);
1379 struct subpart_dir_entry *e = (struct subpart_dir_entry *)(h + 1);
1380
1381 init_subpart_dir_header(h, count, name);
1382
1383 size_t curr_offset = size;
1384 size_t i;
1385
1386 for (i = 0; i < count; i++) {
1387 curr_offset = init_subpart_dir_entry(&e[i], info[i],
1388 curr_offset);
1389 }
1390
1391 alloc_buffer(dst, curr_offset, name);
1392 uint8_t *data = buffer_get(dst);
1393
1394 for (i = 0; i < count; i++) {
1395 memcpy(data + e[i].offset, buffer_get(info[i]),
1396 buffer_size(info[i]));
1397 }
1398
Furquan Shaikhe51c20b2016-06-15 08:01:10 -07001399 h->checksum = calc_checksum(buffer_get(&subpart_dir_buff));
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001400
1401 struct subpart_dir *dir = buffer_get(&subpart_dir_buff);
1402
1403 print_subpart_dir(dir);
1404
1405 subpart_dir_fixup_write_buffer(&subpart_dir_buff);
1406 memcpy(data, dir, buffer_size(&subpart_dir_buff));
1407
1408 buffer_delete(&subpart_dir_buff);
1409}
1410
1411static enum ifwi_ret ibbp_dir_add(int type)
1412{
1413#define DUMMY_IBB_SIZE (4 * KiB)
1414
Furquan Shaikh55d2e532016-05-31 11:02:47 -07001415 assert(type == IBB_TYPE);
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001416
1417 /*
1418 * Entry # 1 - IBBP.man
1419 * Contains manifest header and signed pkg info extension.
1420 */
1421 struct buffer manifest;
1422 size_t size = MANIFEST_HDR_SIZE + SIGNED_PKG_INFO_EXT_SIZE;
1423 alloc_buffer(&manifest, size, "IBBP.man");
1424
1425 struct manifest_header *man_hdr = buffer_get(&manifest);
1426 init_manifest_header(man_hdr, size);
1427
1428 struct signed_pkg_info_ext *ext;
1429 ext = (struct signed_pkg_info_ext *)(man_hdr + 1);
1430
1431 init_signed_pkg_info_ext(ext, 0, subparts[type].name);
1432
1433 /* Entry # 2 - IBBL */
1434 struct buffer ibbl;
1435 if (buffer_from_file(&ibbl, param.file_name))
1436 return COMMAND_ERR;
1437
1438 /* Entry # 3 - IBB */
1439 struct buffer ibb;
1440 alloc_buffer(&ibb, DUMMY_IBB_SIZE, "IBB");
1441 memset(buffer_get(&ibb), 0xFF, DUMMY_IBB_SIZE);
1442
1443 /* Create subpartition. */
1444 struct buffer *info[] = {
1445 &manifest, &ibbl, &ibb,
1446 };
1447 create_subpart(&ifwi_image.subpart_buf[type], &info[0],
1448 ARRAY_SIZE(info), subparts[type].name);
1449
1450 return REPACK_REQUIRED;
1451}
1452
1453static enum ifwi_ret ifwi_raw_add(int type)
1454{
1455 if (buffer_from_file(&ifwi_image.subpart_buf[type], param.file_name))
1456 return COMMAND_ERR;
1457
1458 printf("Sub-partition %s(%d) added from file %s.\n", param.subpart_name,
1459 type, param.file_name);
1460 return REPACK_REQUIRED;
1461}
1462
1463static enum ifwi_ret ifwi_dir_add(int type)
1464{
1465 if (!(subparts[type].attr & CONTAINS_DIR) ||
1466 (subparts[type].dir_ops.dir_add == NULL)) {
1467 ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
1468 subparts[type].name, type);
1469 return COMMAND_ERR;
1470 }
1471
1472 if (!param.dentry_name) {
1473 ERROR("%s: -e option required\n", __func__);
1474 return COMMAND_ERR;
1475 }
1476
1477 enum ifwi_ret ret = subparts[type].dir_ops.dir_add(type);
1478 if (ret != COMMAND_ERR)
1479 printf("Sub-partition %s(%d) entry %s added from file %s.\n",
1480 param.subpart_name, type, param.dentry_name,
1481 param.file_name);
1482 else
1483 ERROR("Sub-partition dir operation failed.\n");
1484
1485 return ret;
1486}
1487
1488static enum ifwi_ret ifwi_add(void)
1489{
1490 if (!param.file_name) {
1491 ERROR("%s: -f option required\n", __func__);
1492 return COMMAND_ERR;
1493 }
1494
1495 if (!param.subpart_name) {
1496 ERROR("%s: -n option required\n", __func__);
1497 return COMMAND_ERR;
1498 }
1499
1500 int type = find_type_by_name(param.subpart_name);
1501 if (type == -1)
1502 return COMMAND_ERR;
1503
1504 const struct subpart_info *curr_subpart = &subparts[type];
1505
1506 if (curr_subpart->attr & AUTO_GENERATED) {
1507 ERROR("Cannot add auto-generated sub-partitions.\n");
1508 return COMMAND_ERR;
1509 }
1510
1511 if (buffer_size(&ifwi_image.subpart_buf[type])) {
1512 ERROR("Image already contains sub-partition %s(%d).\n",
1513 param.subpart_name, type);
1514 return COMMAND_ERR;
1515 }
1516
1517 if (param.dir_ops)
1518 return ifwi_dir_add(type);
1519
1520 return ifwi_raw_add(type);
1521}
1522
1523static enum ifwi_ret ifwi_delete(void)
1524{
1525 if (!param.subpart_name) {
1526 ERROR("%s: -n option required\n", __func__);
1527 return COMMAND_ERR;
1528 }
1529
1530 int type = find_type_by_name(param.subpart_name);
1531 if (type == -1)
1532 return COMMAND_ERR;
1533
1534 const struct subpart_info *curr_subpart = &subparts[type];
1535
1536 if (curr_subpart->attr & AUTO_GENERATED) {
1537 ERROR("Cannot delete auto-generated sub-partitions.\n");
1538 return COMMAND_ERR;
1539 }
1540
1541 if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
1542 printf("Image does not contain sub-partition %s(%d).\n",
1543 param.subpart_name, type);
1544 return NO_ACTION_REQUIRED;
1545 }
1546
1547 buffer_delete(&ifwi_image.subpart_buf[type]);
1548 printf("Sub-Partition %s(%d) deleted.\n", subparts[type].name, type);
1549 return REPACK_REQUIRED;
1550}
1551
1552static enum ifwi_ret ifwi_dir_extract(int type)
1553{
1554 if (!(subparts[type].attr & CONTAINS_DIR)) {
1555 ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
1556 subparts[type].name, type);
1557 return COMMAND_ERR;
1558 }
1559
1560 if (!param.dentry_name) {
1561 ERROR("%s: -e option required.\n", __func__);
1562 return COMMAND_ERR;
1563 }
1564
1565 struct buffer subpart_dir_buff;
1566 parse_subpart_dir(&subpart_dir_buff, &ifwi_image.subpart_buf[type],
1567 subparts[type].name);
1568
1569 uint32_t i;
1570 struct subpart_dir *s = buffer_get(&subpart_dir_buff);
1571
1572 for (i = 0; i < s->h.num_entries; i++) {
1573 if (!strncmp((char *)s->e[i].name, param.dentry_name,
1574 sizeof(s->e[i].name)))
1575 break;
1576 }
1577
1578 if (i == s->h.num_entries) {
1579 ERROR("Entry %s not found in subpartition for %s.\n",
1580 param.dentry_name, param.subpart_name);
1581 exit(-1);
1582 }
1583
1584 struct buffer dst;
1585
1586 DEBUG("Splicing buffer at 0x%x size 0x%x\n", s->e[i].offset,
1587 s->e[i].length);
1588 buffer_splice(&dst, &ifwi_image.subpart_buf[type], s->e[i].offset,
1589 s->e[i].length);
1590
1591 if (buffer_write_file(&dst, param.file_name))
1592 return COMMAND_ERR;
1593
1594 printf("Sub-Partition %s(%d), entry(%s) stored in %s.\n",
1595 param.subpart_name, type, param.dentry_name, param.file_name);
1596
1597 return NO_ACTION_REQUIRED;
1598}
1599
1600static enum ifwi_ret ifwi_raw_extract(int type)
1601{
1602 if (buffer_write_file(&ifwi_image.subpart_buf[type], param.file_name))
1603 return COMMAND_ERR;
1604
1605 printf("Sub-Partition %s(%d) stored in %s.\n", param.subpart_name, type,
1606 param.file_name);
1607
1608 return NO_ACTION_REQUIRED;
1609}
1610
1611static enum ifwi_ret ifwi_extract(void)
1612{
1613 if (!param.file_name) {
1614 ERROR("%s: -f option required\n", __func__);
1615 return COMMAND_ERR;
1616 }
1617
1618 if (!param.subpart_name) {
1619 ERROR("%s: -n option required\n", __func__);
1620 return COMMAND_ERR;
1621 }
1622
1623 int type = find_type_by_name(param.subpart_name);
1624 if (type == -1)
1625 return COMMAND_ERR;
1626
1627 if (type == S_BPDT_TYPE) {
1628 INFO("Tool does not support raw extract for %s\n",
1629 param.subpart_name);
1630 return NO_ACTION_REQUIRED;
1631 }
1632
1633 if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
1634 ERROR("Image does not contain sub-partition %s(%d).\n",
1635 param.subpart_name, type);
1636 return COMMAND_ERR;
1637 }
1638
1639 INFO("Extracting sub-partition %s(%d).\n", param.subpart_name, type);
1640 if (param.dir_ops)
1641 return ifwi_dir_extract(type);
1642
1643 return ifwi_raw_extract(type);
1644}
1645
1646static enum ifwi_ret ifwi_print(void)
1647{
1648 verbose += 2;
1649
1650 struct bpdt *b = buffer_get(&ifwi_image.bpdt);
1651
1652 bpdt_print_header(&b->h, "BPDT");
1653 bpdt_print_entries(&b->e[0], b->h.descriptor_count, "BPDT");
1654
1655 b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
1656 bpdt_print_header(&b->h, "S-BPDT");
1657 bpdt_print_entries(&b->e[0], b->h.descriptor_count, "S-BPDT");
1658
1659 if (param.dir_ops == 0) {
1660 verbose -= 2;
1661 return NO_ACTION_REQUIRED;
1662 }
1663
1664 int i;
1665 struct buffer subpart_dir_buf;
1666 for (i = 0; i < MAX_SUBPARTS ; i++) {
1667 if (!(subparts[i].attr & CONTAINS_DIR) ||
1668 (buffer_size(&ifwi_image.subpart_buf[i]) == 0))
1669 continue;
1670
1671 parse_subpart_dir(&subpart_dir_buf, &ifwi_image.subpart_buf[i],
1672 subparts[i].name);
1673 buffer_delete(&subpart_dir_buf);
1674 }
1675
1676 verbose -= 2;
1677
1678 return NO_ACTION_REQUIRED;
1679}
1680
1681static enum ifwi_ret ifwi_raw_replace(int type)
1682{
1683 buffer_delete(&ifwi_image.subpart_buf[type]);
1684 return ifwi_raw_add(type);
1685}
1686
1687static enum ifwi_ret ifwi_dir_replace(int type)
1688{
1689 if (!(subparts[type].attr & CONTAINS_DIR)) {
1690 ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
1691 subparts[type].name, type);
1692 return COMMAND_ERR;
1693 }
1694
1695 if (!param.dentry_name) {
1696 ERROR("%s: -e option required.\n", __func__);
1697 return COMMAND_ERR;
1698 }
1699
1700 struct buffer subpart_dir_buf;
1701 parse_subpart_dir(&subpart_dir_buf, &ifwi_image.subpart_buf[type],
1702 subparts[type].name);
1703
1704 uint32_t i;
1705 struct subpart_dir *s = buffer_get(&subpart_dir_buf);
1706
1707 for (i = 0; i < s->h.num_entries; i++) {
1708 if (!strcmp((char *)s->e[i].name, param.dentry_name))
1709 break;
1710 }
1711
1712 if (i == s->h.num_entries) {
1713 ERROR("Entry %s not found in subpartition for %s.\n",
1714 param.dentry_name, param.subpart_name);
1715 exit(-1);
1716 }
1717
1718 struct buffer b;
1719 if (buffer_from_file(&b, param.file_name)) {
1720 ERROR("Failed to read %s\n", param.file_name);
1721 exit(-1);
1722 }
1723
1724 struct buffer dst;
Rolf Evers-Fischereb8b7d62016-06-13 13:51:41 +02001725 size_t dst_size = buffer_size(&ifwi_image.subpart_buf[type]) +
1726 buffer_size(&b) - s->e[i].length;
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001727 size_t subpart_start = s->e[i].offset;
1728 size_t subpart_end = s->e[i].offset + s->e[i].length;
1729
1730 alloc_buffer(&dst, dst_size, ifwi_image.subpart_buf[type].name);
1731
1732 uint8_t *src_data = buffer_get(&ifwi_image.subpart_buf[type]);
1733 uint8_t *dst_data = buffer_get(&dst);
1734 size_t curr_offset = 0;
1735
1736 /* Copy data before the sub-partition entry. */
1737 memcpy(dst_data + curr_offset, src_data, subpart_start);
1738 curr_offset += subpart_start;
1739
1740 /* Copy sub-partition entry. */
1741 memcpy(dst_data + curr_offset, buffer_get(&b), buffer_size(&b));
1742 curr_offset += buffer_size(&b);
1743
1744 /* Copy remaining data. */
1745 memcpy(dst_data + curr_offset, src_data + subpart_end,
1746 buffer_size(&ifwi_image.subpart_buf[type]) - subpart_end);
1747
1748 /* Update sub-partition buffer. */
1749 int offset = s->e[i].offset;
1750 buffer_delete(&ifwi_image.subpart_buf[type]);
1751 ifwi_image.subpart_buf[type] = dst;
1752
1753 /* Update length of entry in the subpartition. */
1754 s->e[i].length = buffer_size(&b);
1755 buffer_delete(&b);
1756
1757 /* Adjust offsets of affected entries in subpartition. */
1758 offset = s->e[i].offset - offset;
1759 for (; i < s->h.num_entries; i++) {
1760 s->e[i].offset += offset;
1761 }
1762
Furquan Shaikhe51c20b2016-06-15 08:01:10 -07001763 /* Re-calculate checksum. */
1764 s->h.checksum = calc_checksum(s);
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001765
1766 /* Convert members to litte-endian. */
1767 subpart_dir_fixup_write_buffer(&subpart_dir_buf);
1768
1769 memcpy(dst_data, buffer_get(&subpart_dir_buf),
1770 buffer_size(&subpart_dir_buf));
1771
1772 buffer_delete(&subpart_dir_buf);
1773
1774 printf("Sub-partition %s(%d) entry %s replaced from file %s.\n",
1775 param.subpart_name, type, param.dentry_name, param.file_name);
1776
1777 return REPACK_REQUIRED;
1778}
1779
1780static enum ifwi_ret ifwi_replace(void)
1781{
1782 if (!param.file_name) {
1783 ERROR("%s: -f option required\n", __func__);
1784 return COMMAND_ERR;
1785 }
1786
1787 if (!param.subpart_name) {
1788 ERROR("%s: -n option required\n", __func__);
1789 return COMMAND_ERR;
1790 }
1791
1792 int type = find_type_by_name(param.subpart_name);
1793 if (type == -1)
1794 return COMMAND_ERR;
1795
1796 const struct subpart_info *curr_subpart = &subparts[type];
1797
1798 if (curr_subpart->attr & AUTO_GENERATED) {
1799 ERROR("Cannot replace auto-generated sub-partitions.\n");
1800 return COMMAND_ERR;
1801 }
1802
1803 if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
1804 ERROR("Image does not contain sub-partition %s(%d).\n",
1805 param.subpart_name, type);
1806 return COMMAND_ERR;
1807 }
1808
1809 if (param.dir_ops)
1810 return ifwi_dir_replace(type);
1811
1812 return ifwi_raw_replace(type);
1813}
1814
1815static enum ifwi_ret ifwi_create(void)
1816{
1817 /*
1818 * Create peels off any non-IFWI content present in the input buffer and
1819 * creates output file with only the IFWI present.
1820 */
1821
1822 if (!param.file_name) {
1823 ERROR("%s: -f option required\n", __func__);
1824 return COMMAND_ERR;
1825 }
1826
1827 /* Peel off any non-IFWI prefix. */
1828 buffer_seek(&ifwi_image.input_buff,
1829 ifwi_image.input_ifwi_start_offset);
1830 /* Peel off any non-IFWI suffix. */
1831 buffer_set_size(&ifwi_image.input_buff,
1832 ifwi_image.input_ifwi_end_offset -
1833 ifwi_image.input_ifwi_start_offset);
1834
1835 /*
1836 * Adjust start and end offset of IFWI now that non-IFWI prefix is gone.
1837 */
1838 ifwi_image.input_ifwi_end_offset -= ifwi_image.input_ifwi_start_offset;
1839 ifwi_image.input_ifwi_start_offset = 0;
1840
1841 param.image_name = param.file_name;
1842
1843 return REPACK_REQUIRED;
1844}
1845
1846struct command {
1847 const char *name;
1848 const char *optstring;
1849 enum ifwi_ret (*function)(void);
1850};
1851
1852static const struct command commands[] = {
1853 {"add", "f:n:e:dvh?", ifwi_add},
1854 {"create", "f:vh?", ifwi_create},
1855 {"delete", "f:n:vh?", ifwi_delete},
1856 {"extract", "f:n:e:dvh?", ifwi_extract},
1857 {"print", "dh?", ifwi_print},
1858 {"replace", "f:n:e:dvh?", ifwi_replace},
1859};
1860
1861static struct option long_options[] = {
1862 {"subpart_dentry", required_argument, 0, 'e'},
1863 {"file", required_argument, 0, 'f'},
1864 {"help", required_argument, 0, 'h'},
1865 {"name", required_argument, 0, 'n'},
1866 {"dir_ops", no_argument, 0, 'd'},
1867 {"verbose", no_argument, 0, 'v'},
1868 {NULL, 0, 0, 0 }
1869};
1870
1871static void usage(const char *name)
1872{
1873 printf("ifwitool: Utility for IFWI manipulation\n\n"
1874 "USAGE:\n"
1875 " %s [-h]\n"
1876 " %s FILE COMMAND [PARAMETERS]\n\n"
1877 "COMMANDs:\n"
1878 " add -f FILE -n NAME [-d -e ENTRY]\n"
1879 " create -f FILE\n"
1880 " delete -n NAME\n"
1881 " extract -f FILE -n NAME [-d -e ENTRY]\n"
1882 " print [-d]\n"
1883 " replace -f FILE -n NAME [-d -e ENTRY]\n"
1884 "OPTIONs:\n"
1885 " -f FILE : File to read/write/create/extract\n"
1886 " -d : Perform directory operation\n"
1887 " -e ENTRY: Name of directory entry to operate on\n"
1888 " -v : Verbose level\n"
1889 " -h : Help message\n"
1890 " -n NAME : Name of sub-partition to operate on\n",
1891 name, name
1892 );
1893
1894 printf("\nNAME should be one of:\n");
1895 int i;
1896 for (i = 0; i < MAX_SUBPARTS; i++)
1897 printf("%s(%s)\n", subparts[i].name, subparts[i].readable_name);
1898 printf("\n");
1899}
1900
1901int main(int argc, char **argv)
1902{
1903 if (argc < 3) {
1904 usage(argv[0]);
1905 return 1;
1906 }
1907
1908 param.image_name = argv[1];
1909 char *cmd = argv[2];
1910 optind += 2;
1911
1912 uint32_t i;
1913
1914 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1915 if (strcmp(cmd, commands[i].name) != 0)
1916 continue;
1917
1918 int c;
1919
1920 while (1) {
1921 int option_index;
1922
1923 c = getopt_long(argc, argv, commands[i].optstring,
1924 long_options, &option_index);
1925
1926 if (c == -1)
1927 break;
1928
1929 /* Filter out illegal long options. */
1930 if (strchr(commands[i].optstring, c) == NULL) {
1931 ERROR("%s: invalid option -- '%c'\n", argv[0],
1932 c);
1933 c = '?';
1934 }
1935
Furquan Shaikh55d2e532016-05-31 11:02:47 -07001936 switch (c) {
Furquan Shaikh233f1b62016-05-19 16:12:16 -07001937 case 'n':
1938 param.subpart_name = optarg;
1939 break;
1940 case 'f':
1941 param.file_name = optarg;
1942 break;
1943 case 'd':
1944 param.dir_ops = 1;
1945 break;
1946 case 'e':
1947 param.dentry_name = optarg;
1948 break;
1949 case 'v':
1950 verbose++;
1951 break;
1952 case 'h':
1953 case '?':
1954 usage(argv[0]);
1955 return 1;
1956 default:
1957 break;
1958 }
1959 }
1960
1961 if (ifwi_parse()) {
1962 ERROR("%s: ifwi parsing failed\n", argv[0]);
1963 return 1;
1964 }
1965
1966 enum ifwi_ret ret = commands[i].function();
1967
1968 if (ret == COMMAND_ERR) {
1969 ERROR("%s: failed execution\n", argv[0]);
1970 return 1;
1971 }
1972
1973 if (ret == REPACK_REQUIRED)
1974 ifwi_repack();
1975
1976 return 0;
1977 }
1978
1979 ERROR("%s: invalid command\n", argv[0]);
1980 return 1;
1981}