mxsimage.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*
  2. * Freescale i.MX23/i.MX28 SB image generator
  3. *
  4. * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifdef CONFIG_MXS
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <limits.h>
  15. #include <openssl/evp.h>
  16. #include "imagetool.h"
  17. #include "mxsimage.h"
  18. #include <image.h>
  19. /*
  20. * DCD block
  21. * |-Write to address command block
  22. * | 0xf00 == 0xf33d
  23. * | 0xba2 == 0xb33f
  24. * |-ORR address with mask command block
  25. * | 0xf00 |= 0x1337
  26. * |-Write to address command block
  27. * | 0xba2 == 0xd00d
  28. * :
  29. */
  30. #define SB_HAB_DCD_WRITE 0xccUL
  31. #define SB_HAB_DCD_CHECK 0xcfUL
  32. #define SB_HAB_DCD_NOOP 0xc0UL
  33. #define SB_HAB_DCD_MASK_BIT (1 << 3)
  34. #define SB_HAB_DCD_SET_BIT (1 << 4)
  35. /* Addr.n = Value.n */
  36. #define SB_DCD_WRITE \
  37. (SB_HAB_DCD_WRITE << 24)
  38. /* Addr.n &= ~Value.n */
  39. #define SB_DCD_ANDC \
  40. ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT)
  41. /* Addr.n |= Value.n */
  42. #define SB_DCD_ORR \
  43. ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
  44. /* (Addr.n & Value.n) == 0 */
  45. #define SB_DCD_CHK_EQZ \
  46. (SB_HAB_DCD_CHECK << 24)
  47. /* (Addr.n & Value.n) == Value.n */
  48. #define SB_DCD_CHK_EQ \
  49. ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT)
  50. /* (Addr.n & Value.n) != Value.n */
  51. #define SB_DCD_CHK_NEQ \
  52. ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_MASK_BIT)
  53. /* (Addr.n & Value.n) != 0 */
  54. #define SB_DCD_CHK_NEZ \
  55. ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
  56. /* NOP */
  57. #define SB_DCD_NOOP \
  58. (SB_HAB_DCD_NOOP << 24)
  59. struct sb_dcd_ctx {
  60. struct sb_dcd_ctx *dcd;
  61. uint32_t id;
  62. /* The DCD block. */
  63. uint32_t *payload;
  64. /* Size of the whole DCD block. */
  65. uint32_t size;
  66. /* Pointer to previous DCD command block. */
  67. uint32_t *prev_dcd_head;
  68. };
  69. /*
  70. * IMAGE
  71. * |-SECTION
  72. * | |-CMD
  73. * | |-CMD
  74. * | `-CMD
  75. * |-SECTION
  76. * | |-CMD
  77. * : :
  78. */
  79. struct sb_cmd_list {
  80. char *cmd;
  81. size_t len;
  82. unsigned int lineno;
  83. };
  84. struct sb_cmd_ctx {
  85. uint32_t size;
  86. struct sb_cmd_ctx *cmd;
  87. uint8_t *data;
  88. uint32_t length;
  89. struct sb_command payload;
  90. struct sb_command c_payload;
  91. };
  92. struct sb_section_ctx {
  93. uint32_t size;
  94. /* Section flags */
  95. unsigned int boot:1;
  96. struct sb_section_ctx *sect;
  97. struct sb_cmd_ctx *cmd_head;
  98. struct sb_cmd_ctx *cmd_tail;
  99. struct sb_sections_header payload;
  100. };
  101. struct sb_image_ctx {
  102. unsigned int in_section:1;
  103. unsigned int in_dcd:1;
  104. /* Image configuration */
  105. unsigned int verbose_boot:1;
  106. unsigned int silent_dump:1;
  107. char *input_filename;
  108. char *output_filename;
  109. char *cfg_filename;
  110. uint8_t image_key[16];
  111. /* Number of section in the image */
  112. unsigned int sect_count;
  113. /* Bootable section */
  114. unsigned int sect_boot;
  115. unsigned int sect_boot_found:1;
  116. struct sb_section_ctx *sect_head;
  117. struct sb_section_ctx *sect_tail;
  118. struct sb_dcd_ctx *dcd_head;
  119. struct sb_dcd_ctx *dcd_tail;
  120. EVP_CIPHER_CTX cipher_ctx;
  121. EVP_MD_CTX md_ctx;
  122. uint8_t digest[32];
  123. struct sb_key_dictionary_key sb_dict_key;
  124. struct sb_boot_image_header payload;
  125. };
  126. /*
  127. * Instruction semantics:
  128. * NOOP
  129. * TAG [LAST]
  130. * LOAD address file
  131. * LOAD IVT address IVT_entry_point
  132. * FILL address pattern length
  133. * JUMP [HAB] address [r0_arg]
  134. * CALL [HAB] address [r0_arg]
  135. * MODE mode
  136. * For i.MX23, mode = USB/I2C/SPI1_FLASH/SPI2_FLASH/NAND_BCH
  137. * JTAG/SPI3_EEPROM/SD_SSP0/SD_SSP1
  138. * For i.MX28, mode = USB/I2C/SPI2_FLASH/SPI3_FLASH/NAND_BCH
  139. * JTAG/SPI2_EEPROM/SD_SSP0/SD_SSP1
  140. */
  141. /*
  142. * AES libcrypto
  143. */
  144. static int sb_aes_init(struct sb_image_ctx *ictx, uint8_t *iv, int enc)
  145. {
  146. EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
  147. int ret;
  148. /* If there is no init vector, init vector is all zeroes. */
  149. if (!iv)
  150. iv = ictx->image_key;
  151. EVP_CIPHER_CTX_init(ctx);
  152. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), ictx->image_key, iv, enc);
  153. if (ret == 1)
  154. EVP_CIPHER_CTX_set_padding(ctx, 0);
  155. return ret;
  156. }
  157. static int sb_aes_crypt(struct sb_image_ctx *ictx, uint8_t *in_data,
  158. uint8_t *out_data, int in_len)
  159. {
  160. EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
  161. int ret, outlen;
  162. uint8_t *outbuf;
  163. outbuf = malloc(in_len);
  164. if (!outbuf)
  165. return -ENOMEM;
  166. memset(outbuf, 0, sizeof(in_len));
  167. ret = EVP_CipherUpdate(ctx, outbuf, &outlen, in_data, in_len);
  168. if (!ret) {
  169. ret = -EINVAL;
  170. goto err;
  171. }
  172. if (out_data)
  173. memcpy(out_data, outbuf, outlen);
  174. err:
  175. free(outbuf);
  176. return ret;
  177. }
  178. static int sb_aes_deinit(EVP_CIPHER_CTX *ctx)
  179. {
  180. return EVP_CIPHER_CTX_cleanup(ctx);
  181. }
  182. static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
  183. {
  184. int ret;
  185. EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
  186. struct sb_boot_image_header *sb_header = &ictx->payload;
  187. uint8_t *iv = sb_header->iv;
  188. ret = sb_aes_deinit(ctx);
  189. if (!ret)
  190. return ret;
  191. return sb_aes_init(ictx, iv, enc);
  192. }
  193. /*
  194. * CRC32
  195. */
  196. static uint32_t crc32(uint8_t *data, uint32_t len)
  197. {
  198. const uint32_t poly = 0x04c11db7;
  199. uint32_t crc32 = 0xffffffff;
  200. unsigned int byte, bit;
  201. for (byte = 0; byte < len; byte++) {
  202. crc32 ^= data[byte] << 24;
  203. for (bit = 8; bit > 0; bit--) {
  204. if (crc32 & (1UL << 31))
  205. crc32 = (crc32 << 1) ^ poly;
  206. else
  207. crc32 = (crc32 << 1);
  208. }
  209. }
  210. return crc32;
  211. }
  212. /*
  213. * Debug
  214. */
  215. static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
  216. {
  217. va_list ap;
  218. if (ictx->silent_dump)
  219. return;
  220. va_start(ap, fmt);
  221. vfprintf(stdout, fmt, ap);
  222. va_end(ap);
  223. }
  224. /*
  225. * Code
  226. */
  227. static time_t sb_get_timestamp(void)
  228. {
  229. struct tm time_2000 = {
  230. .tm_yday = 1, /* Jan. 1st */
  231. .tm_year = 100, /* 2000 */
  232. };
  233. time_t seconds_to_2000 = mktime(&time_2000);
  234. time_t seconds_to_now = time(NULL);
  235. return seconds_to_now - seconds_to_2000;
  236. }
  237. static int sb_get_time(time_t time, struct tm *tm)
  238. {
  239. struct tm time_2000 = {
  240. .tm_yday = 1, /* Jan. 1st */
  241. .tm_year = 0, /* 1900 */
  242. };
  243. const time_t seconds_to_2000 = mktime(&time_2000);
  244. const time_t seconds_to_now = seconds_to_2000 + time;
  245. struct tm *ret;
  246. ret = gmtime_r(&seconds_to_now, tm);
  247. return ret ? 0 : -EINVAL;
  248. }
  249. static void sb_encrypt_sb_header(struct sb_image_ctx *ictx)
  250. {
  251. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  252. struct sb_boot_image_header *sb_header = &ictx->payload;
  253. uint8_t *sb_header_ptr = (uint8_t *)sb_header;
  254. /* Encrypt the header, compute the digest. */
  255. sb_aes_crypt(ictx, sb_header_ptr, NULL, sizeof(*sb_header));
  256. EVP_DigestUpdate(md_ctx, sb_header_ptr, sizeof(*sb_header));
  257. }
  258. static void sb_encrypt_sb_sections_header(struct sb_image_ctx *ictx)
  259. {
  260. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  261. struct sb_section_ctx *sctx = ictx->sect_head;
  262. struct sb_sections_header *shdr;
  263. uint8_t *sb_sections_header_ptr;
  264. const int size = sizeof(*shdr);
  265. while (sctx) {
  266. shdr = &sctx->payload;
  267. sb_sections_header_ptr = (uint8_t *)shdr;
  268. sb_aes_crypt(ictx, sb_sections_header_ptr,
  269. ictx->sb_dict_key.cbc_mac, size);
  270. EVP_DigestUpdate(md_ctx, sb_sections_header_ptr, size);
  271. sctx = sctx->sect;
  272. };
  273. }
  274. static void sb_encrypt_key_dictionary_key(struct sb_image_ctx *ictx)
  275. {
  276. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  277. sb_aes_crypt(ictx, ictx->image_key, ictx->sb_dict_key.key,
  278. sizeof(ictx->sb_dict_key.key));
  279. EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
  280. }
  281. static void sb_decrypt_key_dictionary_key(struct sb_image_ctx *ictx)
  282. {
  283. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  284. EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
  285. sb_aes_crypt(ictx, ictx->sb_dict_key.key, ictx->image_key,
  286. sizeof(ictx->sb_dict_key.key));
  287. }
  288. static void sb_encrypt_tag(struct sb_image_ctx *ictx,
  289. struct sb_cmd_ctx *cctx)
  290. {
  291. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  292. struct sb_command *cmd = &cctx->payload;
  293. sb_aes_crypt(ictx, (uint8_t *)cmd,
  294. (uint8_t *)&cctx->c_payload, sizeof(*cmd));
  295. EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
  296. }
  297. static int sb_encrypt_image(struct sb_image_ctx *ictx)
  298. {
  299. /* Start image-wide crypto. */
  300. EVP_MD_CTX_init(&ictx->md_ctx);
  301. EVP_DigestInit(&ictx->md_ctx, EVP_sha1());
  302. /*
  303. * SB image header.
  304. */
  305. sb_aes_init(ictx, NULL, 1);
  306. sb_encrypt_sb_header(ictx);
  307. /*
  308. * SB sections header.
  309. */
  310. sb_encrypt_sb_sections_header(ictx);
  311. /*
  312. * Key dictionary.
  313. */
  314. sb_aes_reinit(ictx, 1);
  315. sb_encrypt_key_dictionary_key(ictx);
  316. /*
  317. * Section tags.
  318. */
  319. struct sb_cmd_ctx *cctx;
  320. struct sb_command *ccmd;
  321. struct sb_section_ctx *sctx = ictx->sect_head;
  322. while (sctx) {
  323. cctx = sctx->cmd_head;
  324. sb_aes_reinit(ictx, 1);
  325. while (cctx) {
  326. ccmd = &cctx->payload;
  327. sb_encrypt_tag(ictx, cctx);
  328. if (ccmd->header.tag == ROM_TAG_CMD) {
  329. sb_aes_reinit(ictx, 1);
  330. } else if (ccmd->header.tag == ROM_LOAD_CMD) {
  331. sb_aes_crypt(ictx, cctx->data, cctx->data,
  332. cctx->length);
  333. EVP_DigestUpdate(&ictx->md_ctx, cctx->data,
  334. cctx->length);
  335. }
  336. cctx = cctx->cmd;
  337. }
  338. sctx = sctx->sect;
  339. };
  340. /*
  341. * Dump the SHA1 of the whole image.
  342. */
  343. sb_aes_reinit(ictx, 1);
  344. EVP_DigestFinal(&ictx->md_ctx, ictx->digest, NULL);
  345. sb_aes_crypt(ictx, ictx->digest, ictx->digest, sizeof(ictx->digest));
  346. /* Stop the encryption session. */
  347. sb_aes_deinit(&ictx->cipher_ctx);
  348. return 0;
  349. }
  350. static int sb_load_file(struct sb_cmd_ctx *cctx, char *filename)
  351. {
  352. long real_size, roundup_size;
  353. uint8_t *data;
  354. long ret;
  355. unsigned long size;
  356. FILE *fp;
  357. if (!filename) {
  358. fprintf(stderr, "ERR: Missing filename!\n");
  359. return -EINVAL;
  360. }
  361. fp = fopen(filename, "r");
  362. if (!fp)
  363. goto err_open;
  364. ret = fseek(fp, 0, SEEK_END);
  365. if (ret < 0)
  366. goto err_file;
  367. real_size = ftell(fp);
  368. if (real_size < 0)
  369. goto err_file;
  370. ret = fseek(fp, 0, SEEK_SET);
  371. if (ret < 0)
  372. goto err_file;
  373. roundup_size = roundup(real_size, SB_BLOCK_SIZE);
  374. data = calloc(1, roundup_size);
  375. if (!data)
  376. goto err_file;
  377. size = fread(data, 1, real_size, fp);
  378. if (size != (unsigned long)real_size)
  379. goto err_alloc;
  380. cctx->data = data;
  381. cctx->length = roundup_size;
  382. fclose(fp);
  383. return 0;
  384. err_alloc:
  385. free(data);
  386. err_file:
  387. fclose(fp);
  388. err_open:
  389. fprintf(stderr, "ERR: Failed to load file \"%s\"\n", filename);
  390. return -EINVAL;
  391. }
  392. static uint8_t sb_command_checksum(struct sb_command *inst)
  393. {
  394. uint8_t *inst_ptr = (uint8_t *)inst;
  395. uint8_t csum = 0;
  396. unsigned int i;
  397. for (i = 0; i < sizeof(struct sb_command); i++)
  398. csum += inst_ptr[i];
  399. return csum;
  400. }
  401. static int sb_token_to_long(char *tok, uint32_t *rid)
  402. {
  403. char *endptr;
  404. unsigned long id;
  405. if (tok[0] != '0' || tok[1] != 'x') {
  406. fprintf(stderr, "ERR: Invalid hexadecimal number!\n");
  407. return -EINVAL;
  408. }
  409. tok += 2;
  410. errno = 0;
  411. id = strtoul(tok, &endptr, 16);
  412. if ((errno == ERANGE && id == ULONG_MAX) || (errno != 0 && id == 0)) {
  413. fprintf(stderr, "ERR: Value can't be decoded!\n");
  414. return -EINVAL;
  415. }
  416. /* Check for 32-bit overflow. */
  417. if (id > 0xffffffff) {
  418. fprintf(stderr, "ERR: Value too big!\n");
  419. return -EINVAL;
  420. }
  421. if (endptr == tok) {
  422. fprintf(stderr, "ERR: Deformed value!\n");
  423. return -EINVAL;
  424. }
  425. *rid = (uint32_t)id;
  426. return 0;
  427. }
  428. static int sb_grow_dcd(struct sb_dcd_ctx *dctx, unsigned int inc_size)
  429. {
  430. uint32_t *tmp;
  431. if (!inc_size)
  432. return 0;
  433. dctx->size += inc_size;
  434. tmp = realloc(dctx->payload, dctx->size);
  435. if (!tmp)
  436. return -ENOMEM;
  437. dctx->payload = tmp;
  438. /* Assemble and update the HAB DCD header. */
  439. dctx->payload[0] = htonl((SB_HAB_DCD_TAG << 24) |
  440. (dctx->size << 8) |
  441. SB_HAB_VERSION);
  442. return 0;
  443. }
  444. static int sb_build_dcd(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
  445. {
  446. struct sb_dcd_ctx *dctx;
  447. char *tok;
  448. uint32_t id;
  449. int ret;
  450. dctx = calloc(1, sizeof(*dctx));
  451. if (!dctx)
  452. return -ENOMEM;
  453. ret = sb_grow_dcd(dctx, 4);
  454. if (ret)
  455. goto err_dcd;
  456. /* Read DCD block number. */
  457. tok = strtok(cmd->cmd, " ");
  458. if (!tok) {
  459. fprintf(stderr, "#%i ERR: DCD block without number!\n",
  460. cmd->lineno);
  461. ret = -EINVAL;
  462. goto err_dcd;
  463. }
  464. /* Parse the DCD block number. */
  465. ret = sb_token_to_long(tok, &id);
  466. if (ret) {
  467. fprintf(stderr, "#%i ERR: Malformed DCD block number!\n",
  468. cmd->lineno);
  469. goto err_dcd;
  470. }
  471. dctx->id = id;
  472. /*
  473. * The DCD block is now constructed. Append it to the list.
  474. * WARNING: The DCD size is still not computed and will be
  475. * updated while parsing it's commands.
  476. */
  477. if (!ictx->dcd_head) {
  478. ictx->dcd_head = dctx;
  479. ictx->dcd_tail = dctx;
  480. } else {
  481. ictx->dcd_tail->dcd = dctx;
  482. ictx->dcd_tail = dctx;
  483. }
  484. return 0;
  485. err_dcd:
  486. free(dctx->payload);
  487. free(dctx);
  488. return ret;
  489. }
  490. static int sb_build_dcd_block(struct sb_image_ctx *ictx,
  491. struct sb_cmd_list *cmd,
  492. uint32_t type)
  493. {
  494. char *tok;
  495. uint32_t address, value, length;
  496. int ret;
  497. struct sb_dcd_ctx *dctx = ictx->dcd_tail;
  498. uint32_t *dcd;
  499. if (dctx->prev_dcd_head && (type != SB_DCD_NOOP) &&
  500. ((dctx->prev_dcd_head[0] & 0xff0000ff) == type)) {
  501. /* Same instruction as before, just append it. */
  502. ret = sb_grow_dcd(dctx, 8);
  503. if (ret)
  504. return ret;
  505. } else if (type == SB_DCD_NOOP) {
  506. ret = sb_grow_dcd(dctx, 4);
  507. if (ret)
  508. return ret;
  509. /* Update DCD command block pointer. */
  510. dctx->prev_dcd_head = dctx->payload +
  511. dctx->size / sizeof(*dctx->payload) - 1;
  512. /* NOOP has only 4 bytes and no payload. */
  513. goto noop;
  514. } else {
  515. /*
  516. * Either a different instruction block started now
  517. * or this is the first instruction block.
  518. */
  519. ret = sb_grow_dcd(dctx, 12);
  520. if (ret)
  521. return ret;
  522. /* Update DCD command block pointer. */
  523. dctx->prev_dcd_head = dctx->payload +
  524. dctx->size / sizeof(*dctx->payload) - 3;
  525. }
  526. dcd = dctx->payload + dctx->size / sizeof(*dctx->payload) - 2;
  527. /*
  528. * Prepare the command.
  529. */
  530. tok = strtok(cmd->cmd, " ");
  531. if (!tok) {
  532. fprintf(stderr, "#%i ERR: Missing DCD address!\n",
  533. cmd->lineno);
  534. ret = -EINVAL;
  535. goto err;
  536. }
  537. /* Read DCD destination address. */
  538. ret = sb_token_to_long(tok, &address);
  539. if (ret) {
  540. fprintf(stderr, "#%i ERR: Incorrect DCD address!\n",
  541. cmd->lineno);
  542. goto err;
  543. }
  544. tok = strtok(NULL, " ");
  545. if (!tok) {
  546. fprintf(stderr, "#%i ERR: Missing DCD value!\n",
  547. cmd->lineno);
  548. ret = -EINVAL;
  549. goto err;
  550. }
  551. /* Read DCD operation value. */
  552. ret = sb_token_to_long(tok, &value);
  553. if (ret) {
  554. fprintf(stderr, "#%i ERR: Incorrect DCD value!\n",
  555. cmd->lineno);
  556. goto err;
  557. }
  558. /* Fill in the new DCD entry. */
  559. dcd[0] = htonl(address);
  560. dcd[1] = htonl(value);
  561. noop:
  562. /* Update the DCD command block. */
  563. length = dctx->size -
  564. ((dctx->prev_dcd_head - dctx->payload) *
  565. sizeof(*dctx->payload));
  566. dctx->prev_dcd_head[0] = htonl(type | (length << 8));
  567. err:
  568. return ret;
  569. }
  570. static int sb_build_section(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
  571. {
  572. struct sb_section_ctx *sctx;
  573. struct sb_sections_header *shdr;
  574. char *tok;
  575. uint32_t bootable = 0;
  576. uint32_t id;
  577. int ret;
  578. sctx = calloc(1, sizeof(*sctx));
  579. if (!sctx)
  580. return -ENOMEM;
  581. /* Read section number. */
  582. tok = strtok(cmd->cmd, " ");
  583. if (!tok) {
  584. fprintf(stderr, "#%i ERR: Section without number!\n",
  585. cmd->lineno);
  586. ret = -EINVAL;
  587. goto err_sect;
  588. }
  589. /* Parse the section number. */
  590. ret = sb_token_to_long(tok, &id);
  591. if (ret) {
  592. fprintf(stderr, "#%i ERR: Malformed section number!\n",
  593. cmd->lineno);
  594. goto err_sect;
  595. }
  596. /* Read section's BOOTABLE flag. */
  597. tok = strtok(NULL, " ");
  598. if (tok && (strlen(tok) == 8) && !strncmp(tok, "BOOTABLE", 8))
  599. bootable = SB_SECTION_FLAG_BOOTABLE;
  600. sctx->boot = bootable;
  601. shdr = &sctx->payload;
  602. shdr->section_number = id;
  603. shdr->section_flags = bootable;
  604. /*
  605. * The section is now constructed. Append it to the list.
  606. * WARNING: The section size is still not computed and will
  607. * be updated while parsing it's commands.
  608. */
  609. ictx->sect_count++;
  610. /* Mark that this section is bootable one. */
  611. if (bootable) {
  612. if (ictx->sect_boot_found) {
  613. fprintf(stderr,
  614. "#%i WARN: Multiple bootable section!\n",
  615. cmd->lineno);
  616. } else {
  617. ictx->sect_boot = id;
  618. ictx->sect_boot_found = 1;
  619. }
  620. }
  621. if (!ictx->sect_head) {
  622. ictx->sect_head = sctx;
  623. ictx->sect_tail = sctx;
  624. } else {
  625. ictx->sect_tail->sect = sctx;
  626. ictx->sect_tail = sctx;
  627. }
  628. return 0;
  629. err_sect:
  630. free(sctx);
  631. return ret;
  632. }
  633. static int sb_build_command_nop(struct sb_image_ctx *ictx)
  634. {
  635. struct sb_section_ctx *sctx = ictx->sect_tail;
  636. struct sb_cmd_ctx *cctx;
  637. struct sb_command *ccmd;
  638. cctx = calloc(1, sizeof(*cctx));
  639. if (!cctx)
  640. return -ENOMEM;
  641. ccmd = &cctx->payload;
  642. /*
  643. * Construct the command.
  644. */
  645. ccmd->header.checksum = 0x5a;
  646. ccmd->header.tag = ROM_NOP_CMD;
  647. cctx->size = sizeof(*ccmd);
  648. /*
  649. * Append the command to the last section.
  650. */
  651. if (!sctx->cmd_head) {
  652. sctx->cmd_head = cctx;
  653. sctx->cmd_tail = cctx;
  654. } else {
  655. sctx->cmd_tail->cmd = cctx;
  656. sctx->cmd_tail = cctx;
  657. }
  658. return 0;
  659. }
  660. static int sb_build_command_tag(struct sb_image_ctx *ictx,
  661. struct sb_cmd_list *cmd)
  662. {
  663. struct sb_section_ctx *sctx = ictx->sect_tail;
  664. struct sb_cmd_ctx *cctx;
  665. struct sb_command *ccmd;
  666. char *tok;
  667. cctx = calloc(1, sizeof(*cctx));
  668. if (!cctx)
  669. return -ENOMEM;
  670. ccmd = &cctx->payload;
  671. /*
  672. * Prepare the command.
  673. */
  674. /* Check for the LAST keyword. */
  675. tok = strtok(cmd->cmd, " ");
  676. if (tok && !strcmp(tok, "LAST"))
  677. ccmd->header.flags = ROM_TAG_CMD_FLAG_ROM_LAST_TAG;
  678. /*
  679. * Construct the command.
  680. */
  681. ccmd->header.checksum = 0x5a;
  682. ccmd->header.tag = ROM_TAG_CMD;
  683. cctx->size = sizeof(*ccmd);
  684. /*
  685. * Append the command to the last section.
  686. */
  687. if (!sctx->cmd_head) {
  688. sctx->cmd_head = cctx;
  689. sctx->cmd_tail = cctx;
  690. } else {
  691. sctx->cmd_tail->cmd = cctx;
  692. sctx->cmd_tail = cctx;
  693. }
  694. return 0;
  695. }
  696. static int sb_build_command_load(struct sb_image_ctx *ictx,
  697. struct sb_cmd_list *cmd)
  698. {
  699. struct sb_section_ctx *sctx = ictx->sect_tail;
  700. struct sb_cmd_ctx *cctx;
  701. struct sb_command *ccmd;
  702. char *tok;
  703. int ret, is_ivt = 0, is_dcd = 0;
  704. uint32_t dest, dcd = 0;
  705. cctx = calloc(1, sizeof(*cctx));
  706. if (!cctx)
  707. return -ENOMEM;
  708. ccmd = &cctx->payload;
  709. /*
  710. * Prepare the command.
  711. */
  712. tok = strtok(cmd->cmd, " ");
  713. if (!tok) {
  714. fprintf(stderr, "#%i ERR: Missing LOAD address or 'IVT'!\n",
  715. cmd->lineno);
  716. ret = -EINVAL;
  717. goto err;
  718. }
  719. /* Check for "IVT" flag. */
  720. if (!strcmp(tok, "IVT"))
  721. is_ivt = 1;
  722. if (!strcmp(tok, "DCD"))
  723. is_dcd = 1;
  724. if (is_ivt || is_dcd) {
  725. tok = strtok(NULL, " ");
  726. if (!tok) {
  727. fprintf(stderr, "#%i ERR: Missing LOAD address!\n",
  728. cmd->lineno);
  729. ret = -EINVAL;
  730. goto err;
  731. }
  732. }
  733. /* Read load destination address. */
  734. ret = sb_token_to_long(tok, &dest);
  735. if (ret) {
  736. fprintf(stderr, "#%i ERR: Incorrect LOAD address!\n",
  737. cmd->lineno);
  738. goto err;
  739. }
  740. /* Read filename or IVT entrypoint or DCD block ID. */
  741. tok = strtok(NULL, " ");
  742. if (!tok) {
  743. fprintf(stderr,
  744. "#%i ERR: Missing LOAD filename or IVT ep or DCD block ID!\n",
  745. cmd->lineno);
  746. ret = -EINVAL;
  747. goto err;
  748. }
  749. if (is_ivt) {
  750. /* Handle IVT. */
  751. struct sb_ivt_header *ivt;
  752. uint32_t ivtep;
  753. ret = sb_token_to_long(tok, &ivtep);
  754. if (ret) {
  755. fprintf(stderr,
  756. "#%i ERR: Incorrect IVT entry point!\n",
  757. cmd->lineno);
  758. goto err;
  759. }
  760. ivt = calloc(1, sizeof(*ivt));
  761. if (!ivt) {
  762. ret = -ENOMEM;
  763. goto err;
  764. }
  765. ivt->header = sb_hab_ivt_header();
  766. ivt->entry = ivtep;
  767. ivt->self = dest;
  768. cctx->data = (uint8_t *)ivt;
  769. cctx->length = sizeof(*ivt);
  770. } else if (is_dcd) {
  771. struct sb_dcd_ctx *dctx = ictx->dcd_head;
  772. uint32_t dcdid;
  773. uint8_t *payload;
  774. uint32_t asize;
  775. ret = sb_token_to_long(tok, &dcdid);
  776. if (ret) {
  777. fprintf(stderr,
  778. "#%i ERR: Incorrect DCD block ID!\n",
  779. cmd->lineno);
  780. goto err;
  781. }
  782. while (dctx) {
  783. if (dctx->id == dcdid)
  784. break;
  785. dctx = dctx->dcd;
  786. }
  787. if (!dctx) {
  788. fprintf(stderr, "#%i ERR: DCD block %08x not found!\n",
  789. cmd->lineno, dcdid);
  790. goto err;
  791. }
  792. asize = roundup(dctx->size, SB_BLOCK_SIZE);
  793. payload = calloc(1, asize);
  794. if (!payload) {
  795. ret = -ENOMEM;
  796. goto err;
  797. }
  798. memcpy(payload, dctx->payload, dctx->size);
  799. cctx->data = payload;
  800. cctx->length = asize;
  801. /* Set the Load DCD flag. */
  802. dcd = ROM_LOAD_CMD_FLAG_DCD_LOAD;
  803. } else {
  804. /* Regular LOAD of a file. */
  805. ret = sb_load_file(cctx, tok);
  806. if (ret) {
  807. fprintf(stderr, "#%i ERR: Cannot load '%s'!\n",
  808. cmd->lineno, tok);
  809. goto err;
  810. }
  811. }
  812. if (cctx->length & (SB_BLOCK_SIZE - 1)) {
  813. fprintf(stderr, "#%i ERR: Unaligned payload!\n",
  814. cmd->lineno);
  815. }
  816. /*
  817. * Construct the command.
  818. */
  819. ccmd->header.checksum = 0x5a;
  820. ccmd->header.tag = ROM_LOAD_CMD;
  821. ccmd->header.flags = dcd;
  822. ccmd->load.address = dest;
  823. ccmd->load.count = cctx->length;
  824. ccmd->load.crc32 = crc32(cctx->data, cctx->length);
  825. cctx->size = sizeof(*ccmd) + cctx->length;
  826. /*
  827. * Append the command to the last section.
  828. */
  829. if (!sctx->cmd_head) {
  830. sctx->cmd_head = cctx;
  831. sctx->cmd_tail = cctx;
  832. } else {
  833. sctx->cmd_tail->cmd = cctx;
  834. sctx->cmd_tail = cctx;
  835. }
  836. return 0;
  837. err:
  838. free(cctx);
  839. return ret;
  840. }
  841. static int sb_build_command_fill(struct sb_image_ctx *ictx,
  842. struct sb_cmd_list *cmd)
  843. {
  844. struct sb_section_ctx *sctx = ictx->sect_tail;
  845. struct sb_cmd_ctx *cctx;
  846. struct sb_command *ccmd;
  847. char *tok;
  848. uint32_t address, pattern, length;
  849. int ret;
  850. cctx = calloc(1, sizeof(*cctx));
  851. if (!cctx)
  852. return -ENOMEM;
  853. ccmd = &cctx->payload;
  854. /*
  855. * Prepare the command.
  856. */
  857. tok = strtok(cmd->cmd, " ");
  858. if (!tok) {
  859. fprintf(stderr, "#%i ERR: Missing FILL address!\n",
  860. cmd->lineno);
  861. ret = -EINVAL;
  862. goto err;
  863. }
  864. /* Read fill destination address. */
  865. ret = sb_token_to_long(tok, &address);
  866. if (ret) {
  867. fprintf(stderr, "#%i ERR: Incorrect FILL address!\n",
  868. cmd->lineno);
  869. goto err;
  870. }
  871. tok = strtok(NULL, " ");
  872. if (!tok) {
  873. fprintf(stderr, "#%i ERR: Missing FILL pattern!\n",
  874. cmd->lineno);
  875. ret = -EINVAL;
  876. goto err;
  877. }
  878. /* Read fill pattern address. */
  879. ret = sb_token_to_long(tok, &pattern);
  880. if (ret) {
  881. fprintf(stderr, "#%i ERR: Incorrect FILL pattern!\n",
  882. cmd->lineno);
  883. goto err;
  884. }
  885. tok = strtok(NULL, " ");
  886. if (!tok) {
  887. fprintf(stderr, "#%i ERR: Missing FILL length!\n",
  888. cmd->lineno);
  889. ret = -EINVAL;
  890. goto err;
  891. }
  892. /* Read fill pattern address. */
  893. ret = sb_token_to_long(tok, &length);
  894. if (ret) {
  895. fprintf(stderr, "#%i ERR: Incorrect FILL length!\n",
  896. cmd->lineno);
  897. goto err;
  898. }
  899. /*
  900. * Construct the command.
  901. */
  902. ccmd->header.checksum = 0x5a;
  903. ccmd->header.tag = ROM_FILL_CMD;
  904. ccmd->fill.address = address;
  905. ccmd->fill.count = length;
  906. ccmd->fill.pattern = pattern;
  907. cctx->size = sizeof(*ccmd);
  908. /*
  909. * Append the command to the last section.
  910. */
  911. if (!sctx->cmd_head) {
  912. sctx->cmd_head = cctx;
  913. sctx->cmd_tail = cctx;
  914. } else {
  915. sctx->cmd_tail->cmd = cctx;
  916. sctx->cmd_tail = cctx;
  917. }
  918. return 0;
  919. err:
  920. free(cctx);
  921. return ret;
  922. }
  923. static int sb_build_command_jump_call(struct sb_image_ctx *ictx,
  924. struct sb_cmd_list *cmd,
  925. unsigned int is_call)
  926. {
  927. struct sb_section_ctx *sctx = ictx->sect_tail;
  928. struct sb_cmd_ctx *cctx;
  929. struct sb_command *ccmd;
  930. char *tok;
  931. uint32_t dest, arg = 0x0;
  932. uint32_t hab = 0;
  933. int ret;
  934. const char *cmdname = is_call ? "CALL" : "JUMP";
  935. cctx = calloc(1, sizeof(*cctx));
  936. if (!cctx)
  937. return -ENOMEM;
  938. ccmd = &cctx->payload;
  939. /*
  940. * Prepare the command.
  941. */
  942. tok = strtok(cmd->cmd, " ");
  943. if (!tok) {
  944. fprintf(stderr,
  945. "#%i ERR: Missing %s address or 'HAB'!\n",
  946. cmd->lineno, cmdname);
  947. ret = -EINVAL;
  948. goto err;
  949. }
  950. /* Check for "HAB" flag. */
  951. if (!strcmp(tok, "HAB")) {
  952. hab = is_call ? ROM_CALL_CMD_FLAG_HAB : ROM_JUMP_CMD_FLAG_HAB;
  953. tok = strtok(NULL, " ");
  954. if (!tok) {
  955. fprintf(stderr, "#%i ERR: Missing %s address!\n",
  956. cmd->lineno, cmdname);
  957. ret = -EINVAL;
  958. goto err;
  959. }
  960. }
  961. /* Read load destination address. */
  962. ret = sb_token_to_long(tok, &dest);
  963. if (ret) {
  964. fprintf(stderr, "#%i ERR: Incorrect %s address!\n",
  965. cmd->lineno, cmdname);
  966. goto err;
  967. }
  968. tok = strtok(NULL, " ");
  969. if (tok) {
  970. ret = sb_token_to_long(tok, &arg);
  971. if (ret) {
  972. fprintf(stderr,
  973. "#%i ERR: Incorrect %s argument!\n",
  974. cmd->lineno, cmdname);
  975. goto err;
  976. }
  977. }
  978. /*
  979. * Construct the command.
  980. */
  981. ccmd->header.checksum = 0x5a;
  982. ccmd->header.tag = is_call ? ROM_CALL_CMD : ROM_JUMP_CMD;
  983. ccmd->header.flags = hab;
  984. ccmd->call.address = dest;
  985. ccmd->call.argument = arg;
  986. cctx->size = sizeof(*ccmd);
  987. /*
  988. * Append the command to the last section.
  989. */
  990. if (!sctx->cmd_head) {
  991. sctx->cmd_head = cctx;
  992. sctx->cmd_tail = cctx;
  993. } else {
  994. sctx->cmd_tail->cmd = cctx;
  995. sctx->cmd_tail = cctx;
  996. }
  997. return 0;
  998. err:
  999. free(cctx);
  1000. return ret;
  1001. }
  1002. static int sb_build_command_jump(struct sb_image_ctx *ictx,
  1003. struct sb_cmd_list *cmd)
  1004. {
  1005. return sb_build_command_jump_call(ictx, cmd, 0);
  1006. }
  1007. static int sb_build_command_call(struct sb_image_ctx *ictx,
  1008. struct sb_cmd_list *cmd)
  1009. {
  1010. return sb_build_command_jump_call(ictx, cmd, 1);
  1011. }
  1012. static int sb_build_command_mode(struct sb_image_ctx *ictx,
  1013. struct sb_cmd_list *cmd)
  1014. {
  1015. struct sb_section_ctx *sctx = ictx->sect_tail;
  1016. struct sb_cmd_ctx *cctx;
  1017. struct sb_command *ccmd;
  1018. char *tok;
  1019. int ret;
  1020. unsigned int i;
  1021. uint32_t mode = 0xffffffff;
  1022. cctx = calloc(1, sizeof(*cctx));
  1023. if (!cctx)
  1024. return -ENOMEM;
  1025. ccmd = &cctx->payload;
  1026. /*
  1027. * Prepare the command.
  1028. */
  1029. tok = strtok(cmd->cmd, " ");
  1030. if (!tok) {
  1031. fprintf(stderr, "#%i ERR: Missing MODE boot mode argument!\n",
  1032. cmd->lineno);
  1033. ret = -EINVAL;
  1034. goto err;
  1035. }
  1036. for (i = 0; i < ARRAY_SIZE(modetable); i++) {
  1037. if (!strcmp(tok, modetable[i].name)) {
  1038. mode = modetable[i].mode;
  1039. break;
  1040. }
  1041. if (!modetable[i].altname)
  1042. continue;
  1043. if (!strcmp(tok, modetable[i].altname)) {
  1044. mode = modetable[i].mode;
  1045. break;
  1046. }
  1047. }
  1048. if (mode == 0xffffffff) {
  1049. fprintf(stderr, "#%i ERR: Invalid MODE boot mode argument!\n",
  1050. cmd->lineno);
  1051. ret = -EINVAL;
  1052. goto err;
  1053. }
  1054. /*
  1055. * Construct the command.
  1056. */
  1057. ccmd->header.checksum = 0x5a;
  1058. ccmd->header.tag = ROM_MODE_CMD;
  1059. ccmd->mode.mode = mode;
  1060. cctx->size = sizeof(*ccmd);
  1061. /*
  1062. * Append the command to the last section.
  1063. */
  1064. if (!sctx->cmd_head) {
  1065. sctx->cmd_head = cctx;
  1066. sctx->cmd_tail = cctx;
  1067. } else {
  1068. sctx->cmd_tail->cmd = cctx;
  1069. sctx->cmd_tail = cctx;
  1070. }
  1071. return 0;
  1072. err:
  1073. free(cctx);
  1074. return ret;
  1075. }
  1076. static int sb_prefill_image_header(struct sb_image_ctx *ictx)
  1077. {
  1078. struct sb_boot_image_header *hdr = &ictx->payload;
  1079. /* Fill signatures */
  1080. memcpy(hdr->signature1, "STMP", 4);
  1081. memcpy(hdr->signature2, "sgtl", 4);
  1082. /* SB Image version 1.1 */
  1083. hdr->major_version = SB_VERSION_MAJOR;
  1084. hdr->minor_version = SB_VERSION_MINOR;
  1085. /* Boot image major version */
  1086. hdr->product_version.major = htons(0x999);
  1087. hdr->product_version.minor = htons(0x999);
  1088. hdr->product_version.revision = htons(0x999);
  1089. /* Boot image major version */
  1090. hdr->component_version.major = htons(0x999);
  1091. hdr->component_version.minor = htons(0x999);
  1092. hdr->component_version.revision = htons(0x999);
  1093. /* Drive tag must be 0x0 for i.MX23 */
  1094. hdr->drive_tag = 0;
  1095. hdr->header_blocks =
  1096. sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
  1097. hdr->section_header_size =
  1098. sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
  1099. hdr->timestamp_us = sb_get_timestamp() * 1000000;
  1100. /* FIXME -- add proper config option */
  1101. hdr->flags = ictx->verbose_boot ? SB_IMAGE_FLAG_VERBOSE : 0,
  1102. /* FIXME -- We support only default key */
  1103. hdr->key_count = 1;
  1104. return 0;
  1105. }
  1106. static int sb_postfill_image_header(struct sb_image_ctx *ictx)
  1107. {
  1108. struct sb_boot_image_header *hdr = &ictx->payload;
  1109. struct sb_section_ctx *sctx = ictx->sect_head;
  1110. uint32_t kd_size, sections_blocks;
  1111. EVP_MD_CTX md_ctx;
  1112. /* The main SB header size in blocks. */
  1113. hdr->image_blocks = hdr->header_blocks;
  1114. /* Size of the key dictionary, which has single zero entry. */
  1115. kd_size = hdr->key_count * sizeof(struct sb_key_dictionary_key);
  1116. hdr->image_blocks += kd_size / SB_BLOCK_SIZE;
  1117. /* Now count the payloads. */
  1118. hdr->section_count = ictx->sect_count;
  1119. while (sctx) {
  1120. hdr->image_blocks += sctx->size / SB_BLOCK_SIZE;
  1121. sctx = sctx->sect;
  1122. }
  1123. if (!ictx->sect_boot_found) {
  1124. fprintf(stderr, "ERR: No bootable section selected!\n");
  1125. return -EINVAL;
  1126. }
  1127. hdr->first_boot_section_id = ictx->sect_boot;
  1128. /* The n * SB section size in blocks. */
  1129. sections_blocks = hdr->section_count * hdr->section_header_size;
  1130. hdr->image_blocks += sections_blocks;
  1131. /* Key dictionary offset. */
  1132. hdr->key_dictionary_block = hdr->header_blocks + sections_blocks;
  1133. /* Digest of the whole image. */
  1134. hdr->image_blocks += 2;
  1135. /* Pointer past the dictionary. */
  1136. hdr->first_boot_tag_block =
  1137. hdr->key_dictionary_block + kd_size / SB_BLOCK_SIZE;
  1138. /* Compute header digest. */
  1139. EVP_MD_CTX_init(&md_ctx);
  1140. EVP_DigestInit(&md_ctx, EVP_sha1());
  1141. EVP_DigestUpdate(&md_ctx, hdr->signature1,
  1142. sizeof(struct sb_boot_image_header) -
  1143. sizeof(hdr->digest));
  1144. EVP_DigestFinal(&md_ctx, hdr->digest, NULL);
  1145. return 0;
  1146. }
  1147. static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx)
  1148. {
  1149. /* Fixup the placement of sections. */
  1150. struct sb_boot_image_header *ihdr = &ictx->payload;
  1151. struct sb_section_ctx *sctx = ictx->sect_head;
  1152. struct sb_sections_header *shdr;
  1153. struct sb_cmd_ctx *cctx;
  1154. struct sb_command *ccmd;
  1155. uint32_t offset = ihdr->first_boot_tag_block;
  1156. while (sctx) {
  1157. shdr = &sctx->payload;
  1158. /* Fill in the section TAG offset. */
  1159. shdr->section_offset = offset + 1;
  1160. offset += shdr->section_size;
  1161. /* Section length is measured from the TAG block. */
  1162. shdr->section_size--;
  1163. /* Fixup the TAG command. */
  1164. cctx = sctx->cmd_head;
  1165. while (cctx) {
  1166. ccmd = &cctx->payload;
  1167. if (ccmd->header.tag == ROM_TAG_CMD) {
  1168. ccmd->tag.section_number = shdr->section_number;
  1169. ccmd->tag.section_length = shdr->section_size;
  1170. ccmd->tag.section_flags = shdr->section_flags;
  1171. }
  1172. /* Update the command checksum. */
  1173. ccmd->header.checksum = sb_command_checksum(ccmd);
  1174. cctx = cctx->cmd;
  1175. }
  1176. sctx = sctx->sect;
  1177. }
  1178. return 0;
  1179. }
  1180. static int sb_parse_line(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
  1181. {
  1182. char *tok;
  1183. char *line = cmd->cmd;
  1184. char *rptr;
  1185. int ret;
  1186. /* Analyze the identifier on this line first. */
  1187. tok = strtok_r(line, " ", &rptr);
  1188. if (!tok || (strlen(tok) == 0)) {
  1189. fprintf(stderr, "#%i ERR: Invalid line!\n", cmd->lineno);
  1190. return -EINVAL;
  1191. }
  1192. cmd->cmd = rptr;
  1193. /* DCD */
  1194. if (!strcmp(tok, "DCD")) {
  1195. ictx->in_section = 0;
  1196. ictx->in_dcd = 1;
  1197. sb_build_dcd(ictx, cmd);
  1198. return 0;
  1199. }
  1200. /* Section */
  1201. if (!strcmp(tok, "SECTION")) {
  1202. ictx->in_section = 1;
  1203. ictx->in_dcd = 0;
  1204. sb_build_section(ictx, cmd);
  1205. return 0;
  1206. }
  1207. if (!ictx->in_section && !ictx->in_dcd) {
  1208. fprintf(stderr, "#%i ERR: Data outside of a section!\n",
  1209. cmd->lineno);
  1210. return -EINVAL;
  1211. }
  1212. if (ictx->in_section) {
  1213. /* Section commands */
  1214. if (!strcmp(tok, "NOP")) {
  1215. ret = sb_build_command_nop(ictx);
  1216. } else if (!strcmp(tok, "TAG")) {
  1217. ret = sb_build_command_tag(ictx, cmd);
  1218. } else if (!strcmp(tok, "LOAD")) {
  1219. ret = sb_build_command_load(ictx, cmd);
  1220. } else if (!strcmp(tok, "FILL")) {
  1221. ret = sb_build_command_fill(ictx, cmd);
  1222. } else if (!strcmp(tok, "JUMP")) {
  1223. ret = sb_build_command_jump(ictx, cmd);
  1224. } else if (!strcmp(tok, "CALL")) {
  1225. ret = sb_build_command_call(ictx, cmd);
  1226. } else if (!strcmp(tok, "MODE")) {
  1227. ret = sb_build_command_mode(ictx, cmd);
  1228. } else {
  1229. fprintf(stderr,
  1230. "#%i ERR: Unsupported instruction '%s'!\n",
  1231. cmd->lineno, tok);
  1232. return -ENOTSUP;
  1233. }
  1234. } else if (ictx->in_dcd) {
  1235. char *lptr;
  1236. uint32_t ilen = '1';
  1237. tok = strtok_r(tok, ".", &lptr);
  1238. if (!tok || (strlen(tok) == 0) || (lptr && strlen(lptr) != 1)) {
  1239. fprintf(stderr, "#%i ERR: Invalid line!\n",
  1240. cmd->lineno);
  1241. return -EINVAL;
  1242. }
  1243. if (lptr &&
  1244. (lptr[0] != '1' && lptr[0] != '2' && lptr[0] != '4')) {
  1245. fprintf(stderr, "#%i ERR: Invalid instruction width!\n",
  1246. cmd->lineno);
  1247. return -EINVAL;
  1248. }
  1249. if (lptr)
  1250. ilen = lptr[0] - '1';
  1251. /* DCD commands */
  1252. if (!strcmp(tok, "WRITE")) {
  1253. ret = sb_build_dcd_block(ictx, cmd,
  1254. SB_DCD_WRITE | ilen);
  1255. } else if (!strcmp(tok, "ANDC")) {
  1256. ret = sb_build_dcd_block(ictx, cmd,
  1257. SB_DCD_ANDC | ilen);
  1258. } else if (!strcmp(tok, "ORR")) {
  1259. ret = sb_build_dcd_block(ictx, cmd,
  1260. SB_DCD_ORR | ilen);
  1261. } else if (!strcmp(tok, "EQZ")) {
  1262. ret = sb_build_dcd_block(ictx, cmd,
  1263. SB_DCD_CHK_EQZ | ilen);
  1264. } else if (!strcmp(tok, "EQ")) {
  1265. ret = sb_build_dcd_block(ictx, cmd,
  1266. SB_DCD_CHK_EQ | ilen);
  1267. } else if (!strcmp(tok, "NEQ")) {
  1268. ret = sb_build_dcd_block(ictx, cmd,
  1269. SB_DCD_CHK_NEQ | ilen);
  1270. } else if (!strcmp(tok, "NEZ")) {
  1271. ret = sb_build_dcd_block(ictx, cmd,
  1272. SB_DCD_CHK_NEZ | ilen);
  1273. } else if (!strcmp(tok, "NOOP")) {
  1274. ret = sb_build_dcd_block(ictx, cmd, SB_DCD_NOOP);
  1275. } else {
  1276. fprintf(stderr,
  1277. "#%i ERR: Unsupported instruction '%s'!\n",
  1278. cmd->lineno, tok);
  1279. return -ENOTSUP;
  1280. }
  1281. } else {
  1282. fprintf(stderr, "#%i ERR: Unsupported instruction '%s'!\n",
  1283. cmd->lineno, tok);
  1284. return -ENOTSUP;
  1285. }
  1286. /*
  1287. * Here we have at least one section with one command, otherwise we
  1288. * would have failed already higher above.
  1289. *
  1290. * FIXME -- should the updating happen here ?
  1291. */
  1292. if (ictx->in_section && !ret) {
  1293. ictx->sect_tail->size += ictx->sect_tail->cmd_tail->size;
  1294. ictx->sect_tail->payload.section_size =
  1295. ictx->sect_tail->size / SB_BLOCK_SIZE;
  1296. }
  1297. return ret;
  1298. }
  1299. static int sb_load_cmdfile(struct sb_image_ctx *ictx)
  1300. {
  1301. struct sb_cmd_list cmd;
  1302. int lineno = 1;
  1303. FILE *fp;
  1304. char *line = NULL;
  1305. ssize_t rlen;
  1306. size_t len;
  1307. fp = fopen(ictx->cfg_filename, "r");
  1308. if (!fp)
  1309. goto err_file;
  1310. while ((rlen = getline(&line, &len, fp)) > 0) {
  1311. memset(&cmd, 0, sizeof(cmd));
  1312. /* Strip the trailing newline. */
  1313. line[rlen - 1] = '\0';
  1314. cmd.cmd = line;
  1315. cmd.len = rlen;
  1316. cmd.lineno = lineno++;
  1317. sb_parse_line(ictx, &cmd);
  1318. }
  1319. free(line);
  1320. fclose(fp);
  1321. return 0;
  1322. err_file:
  1323. fclose(fp);
  1324. fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
  1325. ictx->cfg_filename);
  1326. return -EINVAL;
  1327. }
  1328. static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx)
  1329. {
  1330. int ret;
  1331. ret = sb_load_cmdfile(ictx);
  1332. if (ret)
  1333. return ret;
  1334. ret = sb_prefill_image_header(ictx);
  1335. if (ret)
  1336. return ret;
  1337. ret = sb_postfill_image_header(ictx);
  1338. if (ret)
  1339. return ret;
  1340. ret = sb_fixup_sections_and_tags(ictx);
  1341. if (ret)
  1342. return ret;
  1343. return 0;
  1344. }
  1345. static int sb_verify_image_header(struct sb_image_ctx *ictx,
  1346. FILE *fp, long fsize)
  1347. {
  1348. /* Verify static fields in the image header. */
  1349. struct sb_boot_image_header *hdr = &ictx->payload;
  1350. const char *stat[2] = { "[PASS]", "[FAIL]" };
  1351. struct tm tm;
  1352. int sz, ret = 0;
  1353. unsigned char digest[20];
  1354. EVP_MD_CTX md_ctx;
  1355. unsigned long size;
  1356. /* Start image-wide crypto. */
  1357. EVP_MD_CTX_init(&ictx->md_ctx);
  1358. EVP_DigestInit(&ictx->md_ctx, EVP_sha1());
  1359. soprintf(ictx, "---------- Verifying SB Image Header ----------\n");
  1360. size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp);
  1361. if (size != sizeof(ictx->payload)) {
  1362. fprintf(stderr, "ERR: SB image header too short!\n");
  1363. return -EINVAL;
  1364. }
  1365. /* Compute header digest. */
  1366. EVP_MD_CTX_init(&md_ctx);
  1367. EVP_DigestInit(&md_ctx, EVP_sha1());
  1368. EVP_DigestUpdate(&md_ctx, hdr->signature1,
  1369. sizeof(struct sb_boot_image_header) -
  1370. sizeof(hdr->digest));
  1371. EVP_DigestFinal(&md_ctx, digest, NULL);
  1372. sb_aes_init(ictx, NULL, 1);
  1373. sb_encrypt_sb_header(ictx);
  1374. if (memcmp(digest, hdr->digest, 20))
  1375. ret = -EINVAL;
  1376. soprintf(ictx, "%s Image header checksum: %s\n", stat[!!ret],
  1377. ret ? "BAD" : "OK");
  1378. if (ret)
  1379. return ret;
  1380. if (memcmp(hdr->signature1, "STMP", 4) ||
  1381. memcmp(hdr->signature2, "sgtl", 4))
  1382. ret = -EINVAL;
  1383. soprintf(ictx, "%s Signatures: '%.4s' '%.4s'\n",
  1384. stat[!!ret], hdr->signature1, hdr->signature2);
  1385. if (ret)
  1386. return ret;
  1387. if ((hdr->major_version != SB_VERSION_MAJOR) ||
  1388. ((hdr->minor_version != 1) && (hdr->minor_version != 2)))
  1389. ret = -EINVAL;
  1390. soprintf(ictx, "%s Image version: v%i.%i\n", stat[!!ret],
  1391. hdr->major_version, hdr->minor_version);
  1392. if (ret)
  1393. return ret;
  1394. ret = sb_get_time(hdr->timestamp_us / 1000000, &tm);
  1395. soprintf(ictx,
  1396. "%s Creation time: %02i:%02i:%02i %02i/%02i/%04i\n",
  1397. stat[!!ret], tm.tm_hour, tm.tm_min, tm.tm_sec,
  1398. tm.tm_mday, tm.tm_mon, tm.tm_year + 2000);
  1399. if (ret)
  1400. return ret;
  1401. soprintf(ictx, "%s Product version: %x.%x.%x\n", stat[0],
  1402. ntohs(hdr->product_version.major),
  1403. ntohs(hdr->product_version.minor),
  1404. ntohs(hdr->product_version.revision));
  1405. soprintf(ictx, "%s Component version: %x.%x.%x\n", stat[0],
  1406. ntohs(hdr->component_version.major),
  1407. ntohs(hdr->component_version.minor),
  1408. ntohs(hdr->component_version.revision));
  1409. if (hdr->flags & ~SB_IMAGE_FLAG_VERBOSE)
  1410. ret = -EINVAL;
  1411. soprintf(ictx, "%s Image flags: %s\n", stat[!!ret],
  1412. hdr->flags & SB_IMAGE_FLAG_VERBOSE ? "Verbose_boot" : "");
  1413. if (ret)
  1414. return ret;
  1415. if (hdr->drive_tag != 0)
  1416. ret = -EINVAL;
  1417. soprintf(ictx, "%s Drive tag: %i\n", stat[!!ret],
  1418. hdr->drive_tag);
  1419. if (ret)
  1420. return ret;
  1421. sz = sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
  1422. if (hdr->header_blocks != sz)
  1423. ret = -EINVAL;
  1424. soprintf(ictx, "%s Image header size (blocks): %i\n", stat[!!ret],
  1425. hdr->header_blocks);
  1426. if (ret)
  1427. return ret;
  1428. sz = sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
  1429. if (hdr->section_header_size != sz)
  1430. ret = -EINVAL;
  1431. soprintf(ictx, "%s Section header size (blocks): %i\n", stat[!!ret],
  1432. hdr->section_header_size);
  1433. if (ret)
  1434. return ret;
  1435. soprintf(ictx, "%s Sections count: %i\n", stat[!!ret],
  1436. hdr->section_count);
  1437. soprintf(ictx, "%s First bootable section %i\n", stat[!!ret],
  1438. hdr->first_boot_section_id);
  1439. if (hdr->image_blocks != fsize / SB_BLOCK_SIZE)
  1440. ret = -EINVAL;
  1441. soprintf(ictx, "%s Image size (blocks): %i\n", stat[!!ret],
  1442. hdr->image_blocks);
  1443. if (ret)
  1444. return ret;
  1445. sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
  1446. if (hdr->key_dictionary_block != sz)
  1447. ret = -EINVAL;
  1448. soprintf(ictx, "%s Key dict offset (blocks): %i\n", stat[!!ret],
  1449. hdr->key_dictionary_block);
  1450. if (ret)
  1451. return ret;
  1452. if (hdr->key_count != 1)
  1453. ret = -EINVAL;
  1454. soprintf(ictx, "%s Number of encryption keys: %i\n", stat[!!ret],
  1455. hdr->key_count);
  1456. if (ret)
  1457. return ret;
  1458. sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
  1459. sz += hdr->key_count *
  1460. sizeof(struct sb_key_dictionary_key) / SB_BLOCK_SIZE;
  1461. if (hdr->first_boot_tag_block != (unsigned)sz)
  1462. ret = -EINVAL;
  1463. soprintf(ictx, "%s First TAG block (blocks): %i\n", stat[!!ret],
  1464. hdr->first_boot_tag_block);
  1465. if (ret)
  1466. return ret;
  1467. return 0;
  1468. }
  1469. static void sb_decrypt_tag(struct sb_image_ctx *ictx,
  1470. struct sb_cmd_ctx *cctx)
  1471. {
  1472. EVP_MD_CTX *md_ctx = &ictx->md_ctx;
  1473. struct sb_command *cmd = &cctx->payload;
  1474. sb_aes_crypt(ictx, (uint8_t *)&cctx->c_payload,
  1475. (uint8_t *)&cctx->payload, sizeof(*cmd));
  1476. EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
  1477. }
  1478. static int sb_verify_command(struct sb_image_ctx *ictx,
  1479. struct sb_cmd_ctx *cctx, FILE *fp,
  1480. unsigned long *tsize)
  1481. {
  1482. struct sb_command *ccmd = &cctx->payload;
  1483. unsigned long size, asize;
  1484. char *csum, *flag = "";
  1485. int ret;
  1486. unsigned int i;
  1487. uint8_t csn, csc = ccmd->header.checksum;
  1488. ccmd->header.checksum = 0x5a;
  1489. csn = sb_command_checksum(ccmd);
  1490. ccmd->header.checksum = csc;
  1491. if (csc == csn)
  1492. ret = 0;
  1493. else
  1494. ret = -EINVAL;
  1495. csum = ret ? "checksum BAD" : "checksum OK";
  1496. switch (ccmd->header.tag) {
  1497. case ROM_NOP_CMD:
  1498. soprintf(ictx, " NOOP # %s\n", csum);
  1499. return ret;
  1500. case ROM_TAG_CMD:
  1501. if (ccmd->header.flags & ROM_TAG_CMD_FLAG_ROM_LAST_TAG)
  1502. flag = "LAST";
  1503. soprintf(ictx, " TAG %s # %s\n", flag, csum);
  1504. sb_aes_reinit(ictx, 0);
  1505. return ret;
  1506. case ROM_LOAD_CMD:
  1507. soprintf(ictx, " LOAD addr=0x%08x length=0x%08x # %s\n",
  1508. ccmd->load.address, ccmd->load.count, csum);
  1509. cctx->length = ccmd->load.count;
  1510. asize = roundup(cctx->length, SB_BLOCK_SIZE);
  1511. cctx->data = malloc(asize);
  1512. if (!cctx->data)
  1513. return -ENOMEM;
  1514. size = fread(cctx->data, 1, asize, fp);
  1515. if (size != asize) {
  1516. fprintf(stderr,
  1517. "ERR: SB LOAD command payload too short!\n");
  1518. return -EINVAL;
  1519. }
  1520. *tsize += size;
  1521. EVP_DigestUpdate(&ictx->md_ctx, cctx->data, asize);
  1522. sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
  1523. if (ccmd->load.crc32 != crc32(cctx->data, asize)) {
  1524. fprintf(stderr,
  1525. "ERR: SB LOAD command payload CRC32 invalid!\n");
  1526. return -EINVAL;
  1527. }
  1528. return 0;
  1529. case ROM_FILL_CMD:
  1530. soprintf(ictx,
  1531. " FILL addr=0x%08x length=0x%08x pattern=0x%08x # %s\n",
  1532. ccmd->fill.address, ccmd->fill.count,
  1533. ccmd->fill.pattern, csum);
  1534. return 0;
  1535. case ROM_JUMP_CMD:
  1536. if (ccmd->header.flags & ROM_JUMP_CMD_FLAG_HAB)
  1537. flag = " HAB";
  1538. soprintf(ictx,
  1539. " JUMP%s addr=0x%08x r0_arg=0x%08x # %s\n",
  1540. flag, ccmd->fill.address, ccmd->jump.argument, csum);
  1541. return 0;
  1542. case ROM_CALL_CMD:
  1543. if (ccmd->header.flags & ROM_CALL_CMD_FLAG_HAB)
  1544. flag = " HAB";
  1545. soprintf(ictx,
  1546. " CALL%s addr=0x%08x r0_arg=0x%08x # %s\n",
  1547. flag, ccmd->fill.address, ccmd->jump.argument, csum);
  1548. return 0;
  1549. case ROM_MODE_CMD:
  1550. for (i = 0; i < ARRAY_SIZE(modetable); i++) {
  1551. if (ccmd->mode.mode == modetable[i].mode) {
  1552. soprintf(ictx, " MODE %s # %s\n",
  1553. modetable[i].name, csum);
  1554. break;
  1555. }
  1556. }
  1557. fprintf(stderr, " MODE !INVALID! # %s\n", csum);
  1558. return 0;
  1559. }
  1560. return ret;
  1561. }
  1562. static int sb_verify_commands(struct sb_image_ctx *ictx,
  1563. struct sb_section_ctx *sctx, FILE *fp)
  1564. {
  1565. unsigned long size, tsize = 0;
  1566. struct sb_cmd_ctx *cctx;
  1567. int ret;
  1568. sb_aes_reinit(ictx, 0);
  1569. while (tsize < sctx->size) {
  1570. cctx = calloc(1, sizeof(*cctx));
  1571. if (!cctx)
  1572. return -ENOMEM;
  1573. if (!sctx->cmd_head) {
  1574. sctx->cmd_head = cctx;
  1575. sctx->cmd_tail = cctx;
  1576. } else {
  1577. sctx->cmd_tail->cmd = cctx;
  1578. sctx->cmd_tail = cctx;
  1579. }
  1580. size = fread(&cctx->c_payload, 1, sizeof(cctx->c_payload), fp);
  1581. if (size != sizeof(cctx->c_payload)) {
  1582. fprintf(stderr, "ERR: SB command header too short!\n");
  1583. return -EINVAL;
  1584. }
  1585. tsize += size;
  1586. sb_decrypt_tag(ictx, cctx);
  1587. ret = sb_verify_command(ictx, cctx, fp, &tsize);
  1588. if (ret)
  1589. return -EINVAL;
  1590. }
  1591. return 0;
  1592. }
  1593. static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
  1594. {
  1595. struct sb_boot_image_header *hdr = &ictx->payload;
  1596. struct sb_sections_header *shdr;
  1597. unsigned int i;
  1598. int ret;
  1599. struct sb_section_ctx *sctx;
  1600. unsigned long size;
  1601. char *bootable = "";
  1602. soprintf(ictx, "----- Verifying SB Sections and Commands -----\n");
  1603. for (i = 0; i < hdr->section_count; i++) {
  1604. sctx = calloc(1, sizeof(*sctx));
  1605. if (!sctx)
  1606. return -ENOMEM;
  1607. if (!ictx->sect_head) {
  1608. ictx->sect_head = sctx;
  1609. ictx->sect_tail = sctx;
  1610. } else {
  1611. ictx->sect_tail->sect = sctx;
  1612. ictx->sect_tail = sctx;
  1613. }
  1614. size = fread(&sctx->payload, 1, sizeof(sctx->payload), fp);
  1615. if (size != sizeof(sctx->payload)) {
  1616. fprintf(stderr, "ERR: SB section header too short!\n");
  1617. return -EINVAL;
  1618. }
  1619. }
  1620. size = fread(&ictx->sb_dict_key, 1, sizeof(ictx->sb_dict_key), fp);
  1621. if (size != sizeof(ictx->sb_dict_key)) {
  1622. fprintf(stderr, "ERR: SB key dictionary too short!\n");
  1623. return -EINVAL;
  1624. }
  1625. sb_encrypt_sb_sections_header(ictx);
  1626. sb_aes_reinit(ictx, 0);
  1627. sb_decrypt_key_dictionary_key(ictx);
  1628. sb_aes_reinit(ictx, 0);
  1629. sctx = ictx->sect_head;
  1630. while (sctx) {
  1631. shdr = &sctx->payload;
  1632. if (shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) {
  1633. sctx->boot = 1;
  1634. bootable = " BOOTABLE";
  1635. }
  1636. sctx->size = (shdr->section_size * SB_BLOCK_SIZE) +
  1637. sizeof(struct sb_command);
  1638. soprintf(ictx, "SECTION 0x%x%s # size = %i bytes\n",
  1639. shdr->section_number, bootable, sctx->size);
  1640. if (shdr->section_flags & ~SB_SECTION_FLAG_BOOTABLE)
  1641. fprintf(stderr, " WARN: Unknown section flag(s) %08x\n",
  1642. shdr->section_flags);
  1643. if ((shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) &&
  1644. (hdr->first_boot_section_id != shdr->section_number)) {
  1645. fprintf(stderr,
  1646. " WARN: Bootable section does ID not match image header ID!\n");
  1647. }
  1648. ret = sb_verify_commands(ictx, sctx, fp);
  1649. if (ret)
  1650. return ret;
  1651. sctx = sctx->sect;
  1652. }
  1653. /*
  1654. * FIXME IDEA:
  1655. * check if the first TAG command is at sctx->section_offset
  1656. */
  1657. return 0;
  1658. }
  1659. static int sb_verify_image_end(struct sb_image_ctx *ictx,
  1660. FILE *fp, off_t filesz)
  1661. {
  1662. uint8_t digest[32];
  1663. unsigned long size;
  1664. off_t pos;
  1665. int ret;
  1666. soprintf(ictx, "------------- Verifying image end -------------\n");
  1667. size = fread(digest, 1, sizeof(digest), fp);
  1668. if (size != sizeof(digest)) {
  1669. fprintf(stderr, "ERR: SB key dictionary too short!\n");
  1670. return -EINVAL;
  1671. }
  1672. pos = ftell(fp);
  1673. if (pos != filesz) {
  1674. fprintf(stderr, "ERR: Trailing data past the image!\n");
  1675. return -EINVAL;
  1676. }
  1677. /* Check the image digest. */
  1678. EVP_DigestFinal(&ictx->md_ctx, ictx->digest, NULL);
  1679. /* Decrypt the image digest from the input image. */
  1680. sb_aes_reinit(ictx, 0);
  1681. sb_aes_crypt(ictx, digest, digest, sizeof(digest));
  1682. /* Check all of 20 bytes of the SHA1 hash. */
  1683. ret = memcmp(digest, ictx->digest, 20) ? -EINVAL : 0;
  1684. if (ret)
  1685. soprintf(ictx, "[FAIL] Full-image checksum: BAD\n");
  1686. else
  1687. soprintf(ictx, "[PASS] Full-image checksum: OK\n");
  1688. return ret;
  1689. }
  1690. static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
  1691. {
  1692. long filesize;
  1693. int ret;
  1694. FILE *fp;
  1695. if (!ictx->input_filename) {
  1696. fprintf(stderr, "ERR: Missing filename!\n");
  1697. return -EINVAL;
  1698. }
  1699. fp = fopen(ictx->input_filename, "r");
  1700. if (!fp)
  1701. goto err_open;
  1702. ret = fseek(fp, 0, SEEK_END);
  1703. if (ret < 0)
  1704. goto err_file;
  1705. filesize = ftell(fp);
  1706. if (filesize < 0)
  1707. goto err_file;
  1708. ret = fseek(fp, 0, SEEK_SET);
  1709. if (ret < 0)
  1710. goto err_file;
  1711. if (filesize < (signed)sizeof(ictx->payload)) {
  1712. fprintf(stderr, "ERR: File too short!\n");
  1713. goto err_file;
  1714. }
  1715. if (filesize & (SB_BLOCK_SIZE - 1)) {
  1716. fprintf(stderr, "ERR: The file is not aligned!\n");
  1717. goto err_file;
  1718. }
  1719. /* Load and verify image header */
  1720. ret = sb_verify_image_header(ictx, fp, filesize);
  1721. if (ret)
  1722. goto err_verify;
  1723. /* Load and verify sections and commands */
  1724. ret = sb_verify_sections_cmds(ictx, fp);
  1725. if (ret)
  1726. goto err_verify;
  1727. ret = sb_verify_image_end(ictx, fp, filesize);
  1728. if (ret)
  1729. goto err_verify;
  1730. ret = 0;
  1731. err_verify:
  1732. soprintf(ictx, "-------------------- Result -------------------\n");
  1733. soprintf(ictx, "Verification %s\n", ret ? "FAILED" : "PASSED");
  1734. /* Stop the encryption session. */
  1735. sb_aes_deinit(&ictx->cipher_ctx);
  1736. fclose(fp);
  1737. return ret;
  1738. err_file:
  1739. fclose(fp);
  1740. err_open:
  1741. fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
  1742. ictx->input_filename);
  1743. return -EINVAL;
  1744. }
  1745. static void sb_free_image(struct sb_image_ctx *ictx)
  1746. {
  1747. struct sb_section_ctx *sctx = ictx->sect_head, *s_head;
  1748. struct sb_dcd_ctx *dctx = ictx->dcd_head, *d_head;
  1749. struct sb_cmd_ctx *cctx, *c_head;
  1750. while (sctx) {
  1751. s_head = sctx;
  1752. c_head = sctx->cmd_head;
  1753. while (c_head) {
  1754. cctx = c_head;
  1755. c_head = c_head->cmd;
  1756. if (cctx->data)
  1757. free(cctx->data);
  1758. free(cctx);
  1759. }
  1760. sctx = sctx->sect;
  1761. free(s_head);
  1762. }
  1763. while (dctx) {
  1764. d_head = dctx;
  1765. dctx = dctx->dcd;
  1766. free(d_head->payload);
  1767. free(d_head);
  1768. }
  1769. }
  1770. /*
  1771. * MXSSB-MKIMAGE glue code.
  1772. */
  1773. static int mxsimage_check_image_types(uint8_t type)
  1774. {
  1775. if (type == IH_TYPE_MXSIMAGE)
  1776. return EXIT_SUCCESS;
  1777. else
  1778. return EXIT_FAILURE;
  1779. }
  1780. static void mxsimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  1781. struct image_tool_params *params)
  1782. {
  1783. }
  1784. int mxsimage_check_params(struct image_tool_params *params)
  1785. {
  1786. if (!params)
  1787. return -1;
  1788. if (!strlen(params->imagename)) {
  1789. fprintf(stderr,
  1790. "Error: %s - Configuration file not specified, it is needed for mxsimage generation\n",
  1791. params->cmdname);
  1792. return -1;
  1793. }
  1794. /*
  1795. * Check parameters:
  1796. * XIP is not allowed and verify that incompatible
  1797. * parameters are not sent at the same time
  1798. * For example, if list is required a data image must not be provided
  1799. */
  1800. return (params->dflag && (params->fflag || params->lflag)) ||
  1801. (params->fflag && (params->dflag || params->lflag)) ||
  1802. (params->lflag && (params->dflag || params->fflag)) ||
  1803. (params->xflag) || !(strlen(params->imagename));
  1804. }
  1805. static int mxsimage_verify_print_header(char *file, int silent)
  1806. {
  1807. int ret;
  1808. struct sb_image_ctx ctx;
  1809. memset(&ctx, 0, sizeof(ctx));
  1810. ctx.input_filename = file;
  1811. ctx.silent_dump = silent;
  1812. ret = sb_build_tree_from_img(&ctx);
  1813. sb_free_image(&ctx);
  1814. return ret;
  1815. }
  1816. char *imagefile;
  1817. static int mxsimage_verify_header(unsigned char *ptr, int image_size,
  1818. struct image_tool_params *params)
  1819. {
  1820. struct sb_boot_image_header *hdr;
  1821. if (!ptr)
  1822. return -EINVAL;
  1823. hdr = (struct sb_boot_image_header *)ptr;
  1824. /*
  1825. * Check if the header contains the MXS image signatures,
  1826. * if so, do a full-image verification.
  1827. */
  1828. if (memcmp(hdr->signature1, "STMP", 4) ||
  1829. memcmp(hdr->signature2, "sgtl", 4))
  1830. return -EINVAL;
  1831. imagefile = params->imagefile;
  1832. return mxsimage_verify_print_header(params->imagefile, 1);
  1833. }
  1834. static void mxsimage_print_header(const void *hdr)
  1835. {
  1836. if (imagefile)
  1837. mxsimage_verify_print_header(imagefile, 0);
  1838. }
  1839. static int sb_build_image(struct sb_image_ctx *ictx,
  1840. struct image_type_params *tparams)
  1841. {
  1842. struct sb_boot_image_header *sb_header = &ictx->payload;
  1843. struct sb_section_ctx *sctx;
  1844. struct sb_cmd_ctx *cctx;
  1845. struct sb_command *ccmd;
  1846. struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
  1847. uint8_t *image, *iptr;
  1848. /* Calculate image size. */
  1849. uint32_t size = sizeof(*sb_header) +
  1850. ictx->sect_count * sizeof(struct sb_sections_header) +
  1851. sizeof(*sb_dict_key) + sizeof(ictx->digest);
  1852. sctx = ictx->sect_head;
  1853. while (sctx) {
  1854. size += sctx->size;
  1855. sctx = sctx->sect;
  1856. };
  1857. image = malloc(size);
  1858. if (!image)
  1859. return -ENOMEM;
  1860. iptr = image;
  1861. memcpy(iptr, sb_header, sizeof(*sb_header));
  1862. iptr += sizeof(*sb_header);
  1863. sctx = ictx->sect_head;
  1864. while (sctx) {
  1865. memcpy(iptr, &sctx->payload, sizeof(struct sb_sections_header));
  1866. iptr += sizeof(struct sb_sections_header);
  1867. sctx = sctx->sect;
  1868. };
  1869. memcpy(iptr, sb_dict_key, sizeof(*sb_dict_key));
  1870. iptr += sizeof(*sb_dict_key);
  1871. sctx = ictx->sect_head;
  1872. while (sctx) {
  1873. cctx = sctx->cmd_head;
  1874. while (cctx) {
  1875. ccmd = &cctx->payload;
  1876. memcpy(iptr, &cctx->c_payload, sizeof(cctx->payload));
  1877. iptr += sizeof(cctx->payload);
  1878. if (ccmd->header.tag == ROM_LOAD_CMD) {
  1879. memcpy(iptr, cctx->data, cctx->length);
  1880. iptr += cctx->length;
  1881. }
  1882. cctx = cctx->cmd;
  1883. }
  1884. sctx = sctx->sect;
  1885. };
  1886. memcpy(iptr, ictx->digest, sizeof(ictx->digest));
  1887. iptr += sizeof(ictx->digest);
  1888. /* Configure the mkimage */
  1889. tparams->hdr = image;
  1890. tparams->header_size = size;
  1891. return 0;
  1892. }
  1893. static int mxsimage_generate(struct image_tool_params *params,
  1894. struct image_type_params *tparams)
  1895. {
  1896. int ret;
  1897. struct sb_image_ctx ctx;
  1898. /* Do not copy the U-Boot image! */
  1899. params->skipcpy = 1;
  1900. memset(&ctx, 0, sizeof(ctx));
  1901. ctx.cfg_filename = params->imagename;
  1902. ctx.output_filename = params->imagefile;
  1903. ctx.verbose_boot = 1;
  1904. ret = sb_build_tree_from_cfg(&ctx);
  1905. if (ret)
  1906. goto fail;
  1907. ret = sb_encrypt_image(&ctx);
  1908. if (!ret)
  1909. ret = sb_build_image(&ctx, tparams);
  1910. fail:
  1911. sb_free_image(&ctx);
  1912. return ret;
  1913. }
  1914. /*
  1915. * mxsimage parameters
  1916. */
  1917. static struct image_type_params mxsimage_params = {
  1918. .name = "Freescale MXS Boot Image support",
  1919. .header_size = 0,
  1920. .hdr = NULL,
  1921. .check_image_type = mxsimage_check_image_types,
  1922. .verify_header = mxsimage_verify_header,
  1923. .print_header = mxsimage_print_header,
  1924. .set_header = mxsimage_set_header,
  1925. .check_params = mxsimage_check_params,
  1926. .vrec_header = mxsimage_generate,
  1927. };
  1928. void init_mxs_image_type(void)
  1929. {
  1930. register_image_type(&mxsimage_params);
  1931. }
  1932. #else
  1933. void init_mxs_image_type(void)
  1934. {
  1935. }
  1936. #endif