ifwitool.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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. void *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(s, offset, sizeof(h->signature));
  1211. offset = fix_member(s, offset, sizeof(h->descriptor_count));
  1212. offset = fix_member(s, offset, sizeof(h->bpdt_version));
  1213. offset = fix_member(s, offset, sizeof(h->xor_redundant_block));
  1214. offset = fix_member(s, offset, sizeof(h->ifwi_version));
  1215. offset = fix_member(s, offset, sizeof(h->fit_tool_version));
  1216. uint32_t i;
  1217. for (i = 0; i < count; i++) {
  1218. offset = fix_member(s, offset, sizeof(e[i].type));
  1219. offset = fix_member(s, offset, sizeof(e[i].flags));
  1220. offset = fix_member(s, offset, sizeof(e[i].offset));
  1221. offset = fix_member(s, offset, sizeof(e[i].size));
  1222. }
  1223. }
  1224. /* Write BPDT to output buffer after fixup */
  1225. static void bpdt_write(struct buffer *dst, size_t offset, struct buffer *src)
  1226. {
  1227. bpdt_fixup_write_buffer(src);
  1228. memcpy(buffer_get(dst) + offset, buffer_get(src), buffer_size(src));
  1229. }
  1230. /*
  1231. * Follows these steps to re-create image:
  1232. * 1. Write any non-IFWI prefix.
  1233. * 2. Write out BPDT header and entries.
  1234. * 3. Write sub-partition buffers to respective offsets.
  1235. * 4. Write any non-IFWI suffix.
  1236. *
  1237. * While performing the above steps, make sure that any empty holes are filled
  1238. * with FF.
  1239. */
  1240. static void ifwi_write(const char *image_name)
  1241. {
  1242. struct bpdt_entry *s = find_entry_by_type(S_BPDT_TYPE);
  1243. assert(s);
  1244. size_t ifwi_start, ifwi_end, file_end;
  1245. ifwi_start = ifwi_image.input_ifwi_start_offset;
  1246. ifwi_end = ifwi_start + ALIGN(s->offset + s->size, 4 * KiB);
  1247. file_end = ifwi_end + (buffer_size(&ifwi_image.input_buff) -
  1248. ifwi_image.input_ifwi_end_offset);
  1249. struct buffer b;
  1250. alloc_buffer(&b, file_end, "Final-IFWI");
  1251. uint8_t *input_data = buffer_get(&ifwi_image.input_buff);
  1252. uint8_t *output_data = buffer_get(&b);
  1253. DEBUG("ifwi_start:0x%zx, ifwi_end:0x%zx, file_end:0x%zx\n", ifwi_start,
  1254. ifwi_end, file_end);
  1255. /* Copy non-IFWI prefix, if any */
  1256. memcpy(output_data, input_data, ifwi_start);
  1257. DEBUG("Copied non-IFWI prefix (offset=0x0, size=0x%zx).\n", ifwi_start);
  1258. struct buffer ifwi;
  1259. buffer_splice(&ifwi, &b, ifwi_start, ifwi_end - ifwi_start);
  1260. uint8_t *ifwi_data = buffer_get(&ifwi);
  1261. /* Copy sub-partitions using pack_order */
  1262. struct bpdt_entry *curr;
  1263. struct buffer *subpart_buf;
  1264. int i, type;
  1265. for (i = 0; i < MAX_SUBPARTS; i++) {
  1266. type = bpdt_pack_order[i];
  1267. if (type == S_BPDT_TYPE)
  1268. continue;
  1269. curr = find_entry_by_type(type);
  1270. if (!curr || !curr->size)
  1271. continue;
  1272. subpart_buf = &ifwi_image.subpart_buf[type];
  1273. DEBUG("curr->offset=0x%x, curr->size=0x%x, type=%d, write_size=0x%zx\n",
  1274. curr->offset, curr->size, type, buffer_size(subpart_buf));
  1275. assert((curr->offset + buffer_size(subpart_buf)) <=
  1276. buffer_size(&ifwi));
  1277. memcpy(ifwi_data + curr->offset, buffer_get(subpart_buf),
  1278. buffer_size(subpart_buf));
  1279. }
  1280. /* Copy non-IFWI suffix, if any */
  1281. if (ifwi_end != file_end) {
  1282. memcpy(output_data + ifwi_end,
  1283. input_data + ifwi_image.input_ifwi_end_offset,
  1284. file_end - ifwi_end);
  1285. DEBUG("Copied non-IFWI suffix (offset=0x%zx,size=0x%zx).\n",
  1286. ifwi_end, file_end - ifwi_end);
  1287. }
  1288. /*
  1289. * Convert BPDT to little-endian format and write it to output buffer.
  1290. * S-BPDT is written first and then BPDT.
  1291. */
  1292. bpdt_write(&ifwi, s->offset, &ifwi_image.subpart_buf[S_BPDT_TYPE]);
  1293. bpdt_write(&ifwi, 0, &ifwi_image.bpdt);
  1294. if (buffer_write_file(&b, image_name)) {
  1295. ERROR("File write error\n");
  1296. exit(-1);
  1297. }
  1298. buffer_delete(&b);
  1299. printf("Image written successfully to %s.\n", image_name);
  1300. }
  1301. /*
  1302. * Calculate size and offset of each sub-partition again since it might have
  1303. * changed because of add/delete operation. Also, re-create BPDT and S-BPDT
  1304. * entries and write back the new IFWI image to file.
  1305. */
  1306. static void ifwi_repack(void)
  1307. {
  1308. bpdt_reset();
  1309. bpdt_entries_init_header_order();
  1310. bpdt_entries_init_pack_order();
  1311. struct bpdt *b = buffer_get(&ifwi_image.bpdt);
  1312. bpdt_print_entries(&b->e[0], b->h.descriptor_count, "BPDT");
  1313. b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
  1314. bpdt_print_entries(&b->e[0], b->h.descriptor_count, "S-BPDT");
  1315. DEBUG("Repack done.. writing image.\n");
  1316. ifwi_write(param.image_name);
  1317. }
  1318. static void init_subpart_dir_header(struct subpart_dir_header *hdr,
  1319. size_t count, const char *name)
  1320. {
  1321. memset(hdr, 0, sizeof(*hdr));
  1322. hdr->marker = SUBPART_DIR_MARKER;
  1323. hdr->num_entries = count;
  1324. hdr->header_version = SUBPART_DIR_HEADER_VERSION_SUPPORTED;
  1325. hdr->entry_version = SUBPART_DIR_ENTRY_VERSION_SUPPORTED;
  1326. hdr->header_length = SUBPART_DIR_HEADER_SIZE;
  1327. memcpy(hdr->name, name, sizeof(hdr->name));
  1328. }
  1329. static size_t init_subpart_dir_entry(struct subpart_dir_entry *e,
  1330. struct buffer *b, size_t offset)
  1331. {
  1332. memset(e, 0, sizeof(*e));
  1333. assert(strlen(b->name) <= sizeof(e->name));
  1334. strncpy((char *)e->name, (char *)b->name, sizeof(e->name));
  1335. e->offset = offset;
  1336. e->length = buffer_size(b);
  1337. return (offset + buffer_size(b));
  1338. }
  1339. static void init_manifest_header(struct manifest_header *hdr, size_t size)
  1340. {
  1341. memset(hdr, 0, sizeof(*hdr));
  1342. hdr->header_type = 0x4;
  1343. assert((MANIFEST_HDR_SIZE % DWORD_SIZE) == 0);
  1344. hdr->header_length = MANIFEST_HDR_SIZE / DWORD_SIZE;
  1345. hdr->header_version = 0x10000;
  1346. hdr->vendor = 0x8086;
  1347. struct tm *local_time;
  1348. time_t curr_time;
  1349. char buffer[11];
  1350. curr_time = time(NULL);
  1351. local_time = localtime(&curr_time);
  1352. assert(local_time != NULL);
  1353. strftime(buffer, sizeof(buffer), "0x%Y%m%d", local_time);
  1354. hdr->date = strtoul(buffer, NULL, 16);
  1355. assert((size % DWORD_SIZE) == 0);
  1356. hdr->size = size / DWORD_SIZE;
  1357. hdr->id = MANIFEST_ID_MAGIC;
  1358. }
  1359. static void init_signed_pkg_info_ext(struct signed_pkg_info_ext *ext,
  1360. size_t count, const char *name)
  1361. {
  1362. memset(ext, 0, sizeof(*ext));
  1363. ext->ext_type = SIGNED_PKG_INFO_EXT_TYPE;
  1364. ext->ext_length = SIGNED_PKG_INFO_EXT_SIZE + count * MODULE_SIZE;
  1365. memcpy(ext->name, name, sizeof(ext->name));
  1366. }
  1367. static void subpart_dir_fixup_write_buffer(struct buffer *buf)
  1368. {
  1369. struct subpart_dir *s = buffer_get(buf);
  1370. struct subpart_dir_header *h = &s->h;
  1371. struct subpart_dir_entry *e = &s->e[0];
  1372. size_t count = h->num_entries;
  1373. size_t offset = 0;
  1374. offset = fix_member(s, offset, sizeof(h->marker));
  1375. offset = fix_member(s, offset, sizeof(h->num_entries));
  1376. offset = fix_member(s, offset, sizeof(h->header_version));
  1377. offset = fix_member(s, offset, sizeof(h->entry_version));
  1378. offset = fix_member(s, offset, sizeof(h->header_length));
  1379. offset = fix_member(s, offset, sizeof(h->checksum));
  1380. offset += sizeof(h->name);
  1381. uint32_t i;
  1382. for (i = 0; i < count; i++) {
  1383. offset += sizeof(e[i].name);
  1384. offset = fix_member(s, offset, sizeof(e[i].offset));
  1385. offset = fix_member(s, offset, sizeof(e[i].length));
  1386. offset = fix_member(s, offset, sizeof(e[i].rsvd));
  1387. }
  1388. }
  1389. static void create_subpart(struct buffer *dst, struct buffer *info[],
  1390. size_t count, const char *name)
  1391. {
  1392. struct buffer subpart_dir_buff;
  1393. size_t size = SUBPART_DIR_HEADER_SIZE + count * SUBPART_DIR_ENTRY_SIZE;
  1394. alloc_buffer(&subpart_dir_buff, size, "subpart-dir");
  1395. struct subpart_dir_header *h = buffer_get(&subpart_dir_buff);
  1396. struct subpart_dir_entry *e = (struct subpart_dir_entry *)(h + 1);
  1397. init_subpart_dir_header(h, count, name);
  1398. size_t curr_offset = size;
  1399. size_t i;
  1400. for (i = 0; i < count; i++) {
  1401. curr_offset = init_subpart_dir_entry(&e[i], info[i],
  1402. curr_offset);
  1403. }
  1404. alloc_buffer(dst, curr_offset, name);
  1405. uint8_t *data = buffer_get(dst);
  1406. for (i = 0; i < count; i++) {
  1407. memcpy(data + e[i].offset, buffer_get(info[i]),
  1408. buffer_size(info[i]));
  1409. }
  1410. h->checksum = calc_checksum(buffer_get(&subpart_dir_buff));
  1411. struct subpart_dir *dir = buffer_get(&subpart_dir_buff);
  1412. print_subpart_dir(dir);
  1413. subpart_dir_fixup_write_buffer(&subpart_dir_buff);
  1414. memcpy(data, dir, buffer_size(&subpart_dir_buff));
  1415. buffer_delete(&subpart_dir_buff);
  1416. }
  1417. static enum ifwi_ret ibbp_dir_add(int type)
  1418. {
  1419. struct buffer manifest;
  1420. struct signed_pkg_info_ext *ext;
  1421. struct buffer ibbl;
  1422. struct buffer ibb;
  1423. #define DUMMY_IBB_SIZE (4 * KiB)
  1424. assert(type == IBB_TYPE);
  1425. /*
  1426. * Entry # 1 - IBBP.man
  1427. * Contains manifest header and signed pkg info extension.
  1428. */
  1429. size_t size = MANIFEST_HDR_SIZE + SIGNED_PKG_INFO_EXT_SIZE;
  1430. alloc_buffer(&manifest, size, "IBBP.man");
  1431. struct manifest_header *man_hdr = buffer_get(&manifest);
  1432. init_manifest_header(man_hdr, size);
  1433. ext = (struct signed_pkg_info_ext *)(man_hdr + 1);
  1434. init_signed_pkg_info_ext(ext, 0, subparts[type].name);
  1435. /* Entry # 2 - IBBL */
  1436. if (buffer_from_file(&ibbl, param.file_name))
  1437. return COMMAND_ERR;
  1438. /* Entry # 3 - IBB */
  1439. alloc_buffer(&ibb, DUMMY_IBB_SIZE, "IBB");
  1440. memset(buffer_get(&ibb), 0xFF, DUMMY_IBB_SIZE);
  1441. /* Create subpartition */
  1442. struct buffer *info[] = {
  1443. &manifest, &ibbl, &ibb,
  1444. };
  1445. create_subpart(&ifwi_image.subpart_buf[type], &info[0],
  1446. ARRAY_SIZE(info), subparts[type].name);
  1447. return REPACK_REQUIRED;
  1448. }
  1449. static enum ifwi_ret ifwi_raw_add(int type)
  1450. {
  1451. if (buffer_from_file(&ifwi_image.subpart_buf[type], param.file_name))
  1452. return COMMAND_ERR;
  1453. printf("Sub-partition %s(%d) added from file %s.\n", param.subpart_name,
  1454. type, param.file_name);
  1455. return REPACK_REQUIRED;
  1456. }
  1457. static enum ifwi_ret ifwi_dir_add(int type)
  1458. {
  1459. if (!(subparts[type].attr & CONTAINS_DIR) ||
  1460. !subparts[type].dir_ops.dir_add) {
  1461. ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
  1462. subparts[type].name, type);
  1463. return COMMAND_ERR;
  1464. }
  1465. if (!param.dentry_name) {
  1466. ERROR("%s: -e option required\n", __func__);
  1467. return COMMAND_ERR;
  1468. }
  1469. enum ifwi_ret ret = subparts[type].dir_ops.dir_add(type);
  1470. if (ret != COMMAND_ERR)
  1471. printf("Sub-partition %s(%d) entry %s added from file %s.\n",
  1472. param.subpart_name, type, param.dentry_name,
  1473. param.file_name);
  1474. else
  1475. ERROR("Sub-partition dir operation failed.\n");
  1476. return ret;
  1477. }
  1478. static enum ifwi_ret ifwi_add(void)
  1479. {
  1480. if (!param.file_name) {
  1481. ERROR("%s: -f option required\n", __func__);
  1482. return COMMAND_ERR;
  1483. }
  1484. if (!param.subpart_name) {
  1485. ERROR("%s: -n option required\n", __func__);
  1486. return COMMAND_ERR;
  1487. }
  1488. int type = find_type_by_name(param.subpart_name);
  1489. if (type == -1)
  1490. return COMMAND_ERR;
  1491. const struct subpart_info *curr_subpart = &subparts[type];
  1492. if (curr_subpart->attr & AUTO_GENERATED) {
  1493. ERROR("Cannot add auto-generated sub-partitions.\n");
  1494. return COMMAND_ERR;
  1495. }
  1496. if (buffer_size(&ifwi_image.subpart_buf[type])) {
  1497. ERROR("Image already contains sub-partition %s(%d).\n",
  1498. param.subpart_name, type);
  1499. return COMMAND_ERR;
  1500. }
  1501. if (param.dir_ops)
  1502. return ifwi_dir_add(type);
  1503. return ifwi_raw_add(type);
  1504. }
  1505. static enum ifwi_ret ifwi_delete(void)
  1506. {
  1507. if (!param.subpart_name) {
  1508. ERROR("%s: -n option required\n", __func__);
  1509. return COMMAND_ERR;
  1510. }
  1511. int type = find_type_by_name(param.subpart_name);
  1512. if (type == -1)
  1513. return COMMAND_ERR;
  1514. const struct subpart_info *curr_subpart = &subparts[type];
  1515. if (curr_subpart->attr & AUTO_GENERATED) {
  1516. ERROR("Cannot delete auto-generated sub-partitions.\n");
  1517. return COMMAND_ERR;
  1518. }
  1519. if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
  1520. printf("Image does not contain sub-partition %s(%d).\n",
  1521. param.subpart_name, type);
  1522. return NO_ACTION_REQUIRED;
  1523. }
  1524. buffer_delete(&ifwi_image.subpart_buf[type]);
  1525. printf("Sub-Partition %s(%d) deleted.\n", subparts[type].name, type);
  1526. return REPACK_REQUIRED;
  1527. }
  1528. static enum ifwi_ret ifwi_dir_extract(int type)
  1529. {
  1530. if (!(subparts[type].attr & CONTAINS_DIR)) {
  1531. ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
  1532. subparts[type].name, type);
  1533. return COMMAND_ERR;
  1534. }
  1535. if (!param.dentry_name) {
  1536. ERROR("%s: -e option required.\n", __func__);
  1537. return COMMAND_ERR;
  1538. }
  1539. struct buffer subpart_dir_buff;
  1540. parse_subpart_dir(&subpart_dir_buff, &ifwi_image.subpart_buf[type],
  1541. subparts[type].name);
  1542. uint32_t i;
  1543. struct subpart_dir *s = buffer_get(&subpart_dir_buff);
  1544. for (i = 0; i < s->h.num_entries; i++) {
  1545. if (!strncmp((char *)s->e[i].name, param.dentry_name,
  1546. sizeof(s->e[i].name)))
  1547. break;
  1548. }
  1549. if (i == s->h.num_entries) {
  1550. ERROR("Entry %s not found in subpartition for %s.\n",
  1551. param.dentry_name, param.subpart_name);
  1552. exit(-1);
  1553. }
  1554. struct buffer dst;
  1555. DEBUG("Splicing buffer at 0x%x size 0x%x\n", s->e[i].offset,
  1556. s->e[i].length);
  1557. buffer_splice(&dst, &ifwi_image.subpart_buf[type], s->e[i].offset,
  1558. s->e[i].length);
  1559. if (buffer_write_file(&dst, param.file_name))
  1560. return COMMAND_ERR;
  1561. printf("Sub-Partition %s(%d), entry(%s) stored in %s.\n",
  1562. param.subpart_name, type, param.dentry_name, param.file_name);
  1563. return NO_ACTION_REQUIRED;
  1564. }
  1565. static enum ifwi_ret ifwi_raw_extract(int type)
  1566. {
  1567. if (buffer_write_file(&ifwi_image.subpart_buf[type], param.file_name))
  1568. return COMMAND_ERR;
  1569. printf("Sub-Partition %s(%d) stored in %s.\n", param.subpart_name, type,
  1570. param.file_name);
  1571. return NO_ACTION_REQUIRED;
  1572. }
  1573. static enum ifwi_ret ifwi_extract(void)
  1574. {
  1575. if (!param.file_name) {
  1576. ERROR("%s: -f option required\n", __func__);
  1577. return COMMAND_ERR;
  1578. }
  1579. if (!param.subpart_name) {
  1580. ERROR("%s: -n option required\n", __func__);
  1581. return COMMAND_ERR;
  1582. }
  1583. int type = find_type_by_name(param.subpart_name);
  1584. if (type == -1)
  1585. return COMMAND_ERR;
  1586. if (type == S_BPDT_TYPE) {
  1587. INFO("Tool does not support raw extract for %s\n",
  1588. param.subpart_name);
  1589. return NO_ACTION_REQUIRED;
  1590. }
  1591. if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
  1592. ERROR("Image does not contain sub-partition %s(%d).\n",
  1593. param.subpart_name, type);
  1594. return COMMAND_ERR;
  1595. }
  1596. INFO("Extracting sub-partition %s(%d).\n", param.subpart_name, type);
  1597. if (param.dir_ops)
  1598. return ifwi_dir_extract(type);
  1599. return ifwi_raw_extract(type);
  1600. }
  1601. static enum ifwi_ret ifwi_print(void)
  1602. {
  1603. verbose += 2;
  1604. struct bpdt *b = buffer_get(&ifwi_image.bpdt);
  1605. bpdt_print_header(&b->h, "BPDT");
  1606. bpdt_print_entries(&b->e[0], b->h.descriptor_count, "BPDT");
  1607. b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
  1608. bpdt_print_header(&b->h, "S-BPDT");
  1609. bpdt_print_entries(&b->e[0], b->h.descriptor_count, "S-BPDT");
  1610. if (param.dir_ops == 0) {
  1611. verbose -= 2;
  1612. return NO_ACTION_REQUIRED;
  1613. }
  1614. int i;
  1615. struct buffer subpart_dir_buf;
  1616. for (i = 0; i < MAX_SUBPARTS ; i++) {
  1617. if (!(subparts[i].attr & CONTAINS_DIR) ||
  1618. (buffer_size(&ifwi_image.subpart_buf[i]) == 0))
  1619. continue;
  1620. parse_subpart_dir(&subpart_dir_buf, &ifwi_image.subpart_buf[i],
  1621. subparts[i].name);
  1622. buffer_delete(&subpart_dir_buf);
  1623. }
  1624. verbose -= 2;
  1625. return NO_ACTION_REQUIRED;
  1626. }
  1627. static enum ifwi_ret ifwi_raw_replace(int type)
  1628. {
  1629. buffer_delete(&ifwi_image.subpart_buf[type]);
  1630. return ifwi_raw_add(type);
  1631. }
  1632. static enum ifwi_ret ifwi_dir_replace(int type)
  1633. {
  1634. if (!(subparts[type].attr & CONTAINS_DIR)) {
  1635. ERROR("Sub-Partition %s(%d) does not support dir ops.\n",
  1636. subparts[type].name, type);
  1637. return COMMAND_ERR;
  1638. }
  1639. if (!param.dentry_name) {
  1640. ERROR("%s: -e option required.\n", __func__);
  1641. return COMMAND_ERR;
  1642. }
  1643. struct buffer subpart_dir_buf;
  1644. parse_subpart_dir(&subpart_dir_buf, &ifwi_image.subpart_buf[type],
  1645. subparts[type].name);
  1646. uint32_t i;
  1647. struct subpart_dir *s = buffer_get(&subpart_dir_buf);
  1648. for (i = 0; i < s->h.num_entries; i++) {
  1649. if (!strcmp((char *)s->e[i].name, param.dentry_name))
  1650. break;
  1651. }
  1652. if (i == s->h.num_entries) {
  1653. ERROR("Entry %s not found in subpartition for %s.\n",
  1654. param.dentry_name, param.subpart_name);
  1655. exit(-1);
  1656. }
  1657. struct buffer b;
  1658. if (buffer_from_file(&b, param.file_name)) {
  1659. ERROR("Failed to read %s\n", param.file_name);
  1660. exit(-1);
  1661. }
  1662. struct buffer dst;
  1663. size_t dst_size = buffer_size(&ifwi_image.subpart_buf[type]) +
  1664. buffer_size(&b) - s->e[i].length;
  1665. size_t subpart_start = s->e[i].offset;
  1666. size_t subpart_end = s->e[i].offset + s->e[i].length;
  1667. alloc_buffer(&dst, dst_size, ifwi_image.subpart_buf[type].name);
  1668. uint8_t *src_data = buffer_get(&ifwi_image.subpart_buf[type]);
  1669. uint8_t *dst_data = buffer_get(&dst);
  1670. size_t curr_offset = 0;
  1671. /* Copy data before the sub-partition entry */
  1672. memcpy(dst_data + curr_offset, src_data, subpart_start);
  1673. curr_offset += subpart_start;
  1674. /* Copy sub-partition entry */
  1675. memcpy(dst_data + curr_offset, buffer_get(&b), buffer_size(&b));
  1676. curr_offset += buffer_size(&b);
  1677. /* Copy remaining data */
  1678. memcpy(dst_data + curr_offset, src_data + subpart_end,
  1679. buffer_size(&ifwi_image.subpart_buf[type]) - subpart_end);
  1680. /* Update sub-partition buffer */
  1681. int offset = s->e[i].offset;
  1682. buffer_delete(&ifwi_image.subpart_buf[type]);
  1683. ifwi_image.subpart_buf[type] = dst;
  1684. /* Update length of entry in the subpartition */
  1685. s->e[i].length = buffer_size(&b);
  1686. buffer_delete(&b);
  1687. /* Adjust offsets of affected entries in subpartition */
  1688. offset = s->e[i].offset - offset;
  1689. for (; i < s->h.num_entries; i++)
  1690. s->e[i].offset += offset;
  1691. /* Re-calculate checksum */
  1692. s->h.checksum = calc_checksum(s);
  1693. /* Convert members to litte-endian */
  1694. subpart_dir_fixup_write_buffer(&subpart_dir_buf);
  1695. memcpy(dst_data, buffer_get(&subpart_dir_buf),
  1696. buffer_size(&subpart_dir_buf));
  1697. buffer_delete(&subpart_dir_buf);
  1698. printf("Sub-partition %s(%d) entry %s replaced from file %s.\n",
  1699. param.subpart_name, type, param.dentry_name, param.file_name);
  1700. return REPACK_REQUIRED;
  1701. }
  1702. static enum ifwi_ret ifwi_replace(void)
  1703. {
  1704. if (!param.file_name) {
  1705. ERROR("%s: -f option required\n", __func__);
  1706. return COMMAND_ERR;
  1707. }
  1708. if (!param.subpart_name) {
  1709. ERROR("%s: -n option required\n", __func__);
  1710. return COMMAND_ERR;
  1711. }
  1712. int type = find_type_by_name(param.subpart_name);
  1713. if (type == -1)
  1714. return COMMAND_ERR;
  1715. const struct subpart_info *curr_subpart = &subparts[type];
  1716. if (curr_subpart->attr & AUTO_GENERATED) {
  1717. ERROR("Cannot replace auto-generated sub-partitions.\n");
  1718. return COMMAND_ERR;
  1719. }
  1720. if (buffer_size(&ifwi_image.subpart_buf[type]) == 0) {
  1721. ERROR("Image does not contain sub-partition %s(%d).\n",
  1722. param.subpart_name, type);
  1723. return COMMAND_ERR;
  1724. }
  1725. if (param.dir_ops)
  1726. return ifwi_dir_replace(type);
  1727. return ifwi_raw_replace(type);
  1728. }
  1729. static enum ifwi_ret ifwi_create(void)
  1730. {
  1731. /*
  1732. * Create peels off any non-IFWI content present in the input buffer and
  1733. * creates output file with only the IFWI present.
  1734. */
  1735. if (!param.file_name) {
  1736. ERROR("%s: -f option required\n", __func__);
  1737. return COMMAND_ERR;
  1738. }
  1739. /* Peel off any non-IFWI prefix */
  1740. buffer_seek(&ifwi_image.input_buff,
  1741. ifwi_image.input_ifwi_start_offset);
  1742. /* Peel off any non-IFWI suffix */
  1743. buffer_set_size(&ifwi_image.input_buff,
  1744. ifwi_image.input_ifwi_end_offset -
  1745. ifwi_image.input_ifwi_start_offset);
  1746. /*
  1747. * Adjust start and end offset of IFWI now that non-IFWI prefix is gone.
  1748. */
  1749. ifwi_image.input_ifwi_end_offset -= ifwi_image.input_ifwi_start_offset;
  1750. ifwi_image.input_ifwi_start_offset = 0;
  1751. param.image_name = param.file_name;
  1752. return REPACK_REQUIRED;
  1753. }
  1754. struct command {
  1755. const char *name;
  1756. const char *optstring;
  1757. enum ifwi_ret (*function)(void);
  1758. };
  1759. static const struct command commands[] = {
  1760. {"add", "f:n:e:dvh?", ifwi_add},
  1761. {"create", "f:vh?", ifwi_create},
  1762. {"delete", "f:n:vh?", ifwi_delete},
  1763. {"extract", "f:n:e:dvh?", ifwi_extract},
  1764. {"print", "dh?", ifwi_print},
  1765. {"replace", "f:n:e:dvh?", ifwi_replace},
  1766. };
  1767. static struct option long_options[] = {
  1768. {"subpart_dentry", required_argument, 0, 'e'},
  1769. {"file", required_argument, 0, 'f'},
  1770. {"help", required_argument, 0, 'h'},
  1771. {"name", required_argument, 0, 'n'},
  1772. {"dir_ops", no_argument, 0, 'd'},
  1773. {"verbose", no_argument, 0, 'v'},
  1774. {NULL, 0, 0, 0 }
  1775. };
  1776. static void usage(const char *name)
  1777. {
  1778. printf("ifwitool: Utility for IFWI manipulation\n\n"
  1779. "USAGE:\n"
  1780. " %s [-h]\n"
  1781. " %s FILE COMMAND [PARAMETERS]\n\n"
  1782. "COMMANDs:\n"
  1783. " add -f FILE -n NAME [-d -e ENTRY]\n"
  1784. " create -f FILE\n"
  1785. " delete -n NAME\n"
  1786. " extract -f FILE -n NAME [-d -e ENTRY]\n"
  1787. " print [-d]\n"
  1788. " replace -f FILE -n NAME [-d -e ENTRY]\n"
  1789. "OPTIONs:\n"
  1790. " -f FILE : File to read/write/create/extract\n"
  1791. " -d : Perform directory operation\n"
  1792. " -e ENTRY: Name of directory entry to operate on\n"
  1793. " -v : Verbose level\n"
  1794. " -h : Help message\n"
  1795. " -n NAME : Name of sub-partition to operate on\n",
  1796. name, name
  1797. );
  1798. printf("\nNAME should be one of:\n");
  1799. int i;
  1800. for (i = 0; i < MAX_SUBPARTS; i++)
  1801. printf("%s(%s)\n", subparts[i].name, subparts[i].readable_name);
  1802. printf("\n");
  1803. }
  1804. int main(int argc, char **argv)
  1805. {
  1806. if (argc < 3) {
  1807. usage(argv[0]);
  1808. return 1;
  1809. }
  1810. param.image_name = argv[1];
  1811. char *cmd = argv[2];
  1812. optind += 2;
  1813. uint32_t i;
  1814. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  1815. if (strcmp(cmd, commands[i].name) != 0)
  1816. continue;
  1817. int c;
  1818. while (1) {
  1819. int option_index;
  1820. c = getopt_long(argc, argv, commands[i].optstring,
  1821. long_options, &option_index);
  1822. if (c == -1)
  1823. break;
  1824. /* Filter out illegal long options */
  1825. if (!strchr(commands[i].optstring, c)) {
  1826. ERROR("%s: invalid option -- '%c'\n", argv[0],
  1827. c);
  1828. c = '?';
  1829. }
  1830. switch (c) {
  1831. case 'n':
  1832. param.subpart_name = optarg;
  1833. break;
  1834. case 'f':
  1835. param.file_name = optarg;
  1836. break;
  1837. case 'd':
  1838. param.dir_ops = 1;
  1839. break;
  1840. case 'e':
  1841. param.dentry_name = optarg;
  1842. break;
  1843. case 'v':
  1844. verbose++;
  1845. break;
  1846. case 'h':
  1847. case '?':
  1848. usage(argv[0]);
  1849. return 1;
  1850. default:
  1851. break;
  1852. }
  1853. }
  1854. if (ifwi_parse()) {
  1855. ERROR("%s: ifwi parsing failed\n", argv[0]);
  1856. return 1;
  1857. }
  1858. enum ifwi_ret ret = commands[i].function();
  1859. if (ret == COMMAND_ERR) {
  1860. ERROR("%s: failed execution\n", argv[0]);
  1861. return 1;
  1862. }
  1863. if (ret == REPACK_REQUIRED)
  1864. ifwi_repack();
  1865. return 0;
  1866. }
  1867. ERROR("%s: invalid command\n", argv[0]);
  1868. return 1;
  1869. }