ifwitool.c 58 KB

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