ifwitool.c 58 KB

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