ifdtool.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * ifdtool - Manage Intel Firmware Descriptor information
  3. *
  4. * Copyright 2014 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. *
  8. * From Coreboot project, but it got a serious code clean-up
  9. * and a few new features
  10. */
  11. #include <assert.h>
  12. #include <fcntl.h>
  13. #include <getopt.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <libfdt.h>
  21. #include "ifdtool.h"
  22. #undef DEBUG
  23. #ifdef DEBUG
  24. #define debug(fmt, args...) printf(fmt, ##args)
  25. #else
  26. #define debug(fmt, args...)
  27. #endif
  28. #define FD_SIGNATURE 0x0FF0A55A
  29. #define FLREG_BASE(reg) ((reg & 0x00000fff) << 12);
  30. #define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff);
  31. enum input_file_type_t {
  32. IF_normal,
  33. IF_fdt,
  34. IF_uboot,
  35. };
  36. struct input_file {
  37. char *fname;
  38. unsigned int addr;
  39. enum input_file_type_t type;
  40. };
  41. /**
  42. * find_fd() - Find the flash description in the ROM image
  43. *
  44. * @image: Pointer to image
  45. * @size: Size of image in bytes
  46. * @return pointer to structure, or NULL if not found
  47. */
  48. static struct fdbar_t *find_fd(char *image, int size)
  49. {
  50. uint32_t *ptr, *end;
  51. /* Scan for FD signature */
  52. for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) {
  53. if (*ptr == FD_SIGNATURE)
  54. break;
  55. }
  56. if (ptr == end) {
  57. printf("No Flash Descriptor found in this image\n");
  58. return NULL;
  59. }
  60. debug("Found Flash Descriptor signature at 0x%08lx\n",
  61. (char *)ptr - image);
  62. return (struct fdbar_t *)ptr;
  63. }
  64. /**
  65. * get_region() - Get information about the selected region
  66. *
  67. * @frba: Flash region list
  68. * @region_type: Type of region (0..MAX_REGIONS-1)
  69. * @region: Region information is written here
  70. * @return 0 if OK, else -ve
  71. */
  72. static int get_region(struct frba_t *frba, int region_type,
  73. struct region_t *region)
  74. {
  75. if (region_type >= MAX_REGIONS) {
  76. fprintf(stderr, "Invalid region type.\n");
  77. return -1;
  78. }
  79. region->base = FLREG_BASE(frba->flreg[region_type]);
  80. region->limit = FLREG_LIMIT(frba->flreg[region_type]);
  81. region->size = region->limit - region->base + 1;
  82. return 0;
  83. }
  84. static const char *region_name(int region_type)
  85. {
  86. static const char *const regions[] = {
  87. "Flash Descriptor",
  88. "BIOS",
  89. "Intel ME",
  90. "GbE",
  91. "Platform Data"
  92. };
  93. assert(region_type < MAX_REGIONS);
  94. return regions[region_type];
  95. }
  96. static const char *region_filename(int region_type)
  97. {
  98. static const char *const region_filenames[] = {
  99. "flashregion_0_flashdescriptor.bin",
  100. "flashregion_1_bios.bin",
  101. "flashregion_2_intel_me.bin",
  102. "flashregion_3_gbe.bin",
  103. "flashregion_4_platform_data.bin"
  104. };
  105. assert(region_type < MAX_REGIONS);
  106. return region_filenames[region_type];
  107. }
  108. static int dump_region(int num, struct frba_t *frba)
  109. {
  110. struct region_t region;
  111. int ret;
  112. ret = get_region(frba, num, &region);
  113. if (ret)
  114. return ret;
  115. printf(" Flash Region %d (%s): %08x - %08x %s\n",
  116. num, region_name(num), region.base, region.limit,
  117. region.size < 1 ? "(unused)" : "");
  118. return ret;
  119. }
  120. static void dump_frba(struct frba_t *frba)
  121. {
  122. int i;
  123. printf("Found Region Section\n");
  124. for (i = 0; i < MAX_REGIONS; i++) {
  125. printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
  126. dump_region(i, frba);
  127. }
  128. }
  129. static void decode_spi_frequency(unsigned int freq)
  130. {
  131. switch (freq) {
  132. case SPI_FREQUENCY_20MHZ:
  133. printf("20MHz");
  134. break;
  135. case SPI_FREQUENCY_33MHZ:
  136. printf("33MHz");
  137. break;
  138. case SPI_FREQUENCY_50MHZ:
  139. printf("50MHz");
  140. break;
  141. default:
  142. printf("unknown<%x>MHz", freq);
  143. }
  144. }
  145. static void decode_component_density(unsigned int density)
  146. {
  147. switch (density) {
  148. case COMPONENT_DENSITY_512KB:
  149. printf("512KiB");
  150. break;
  151. case COMPONENT_DENSITY_1MB:
  152. printf("1MiB");
  153. break;
  154. case COMPONENT_DENSITY_2MB:
  155. printf("2MiB");
  156. break;
  157. case COMPONENT_DENSITY_4MB:
  158. printf("4MiB");
  159. break;
  160. case COMPONENT_DENSITY_8MB:
  161. printf("8MiB");
  162. break;
  163. case COMPONENT_DENSITY_16MB:
  164. printf("16MiB");
  165. break;
  166. default:
  167. printf("unknown<%x>MiB", density);
  168. }
  169. }
  170. static void dump_fcba(struct fcba_t *fcba)
  171. {
  172. printf("\nFound Component Section\n");
  173. printf("FLCOMP 0x%08x\n", fcba->flcomp);
  174. printf(" Dual Output Fast Read Support: %ssupported\n",
  175. (fcba->flcomp & (1 << 30)) ? "" : "not ");
  176. printf(" Read ID/Read Status Clock Frequency: ");
  177. decode_spi_frequency((fcba->flcomp >> 27) & 7);
  178. printf("\n Write/Erase Clock Frequency: ");
  179. decode_spi_frequency((fcba->flcomp >> 24) & 7);
  180. printf("\n Fast Read Clock Frequency: ");
  181. decode_spi_frequency((fcba->flcomp >> 21) & 7);
  182. printf("\n Fast Read Support: %ssupported",
  183. (fcba->flcomp & (1 << 20)) ? "" : "not ");
  184. printf("\n Read Clock Frequency: ");
  185. decode_spi_frequency((fcba->flcomp >> 17) & 7);
  186. printf("\n Component 2 Density: ");
  187. decode_component_density((fcba->flcomp >> 3) & 7);
  188. printf("\n Component 1 Density: ");
  189. decode_component_density(fcba->flcomp & 7);
  190. printf("\n");
  191. printf("FLILL 0x%08x\n", fcba->flill);
  192. printf(" Invalid Instruction 3: 0x%02x\n",
  193. (fcba->flill >> 24) & 0xff);
  194. printf(" Invalid Instruction 2: 0x%02x\n",
  195. (fcba->flill >> 16) & 0xff);
  196. printf(" Invalid Instruction 1: 0x%02x\n",
  197. (fcba->flill >> 8) & 0xff);
  198. printf(" Invalid Instruction 0: 0x%02x\n",
  199. fcba->flill & 0xff);
  200. printf("FLPB 0x%08x\n", fcba->flpb);
  201. printf(" Flash Partition Boundary Address: 0x%06x\n\n",
  202. (fcba->flpb & 0xfff) << 12);
  203. }
  204. static void dump_fpsba(struct fpsba_t *fpsba)
  205. {
  206. int i;
  207. printf("Found PCH Strap Section\n");
  208. for (i = 0; i < MAX_STRAPS; i++)
  209. printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
  210. }
  211. static const char *get_enabled(int flag)
  212. {
  213. return flag ? "enabled" : "disabled";
  214. }
  215. static void decode_flmstr(uint32_t flmstr)
  216. {
  217. printf(" Platform Data Region Write Access: %s\n",
  218. get_enabled(flmstr & (1 << 28)));
  219. printf(" GbE Region Write Access: %s\n",
  220. get_enabled(flmstr & (1 << 27)));
  221. printf(" Intel ME Region Write Access: %s\n",
  222. get_enabled(flmstr & (1 << 26)));
  223. printf(" Host CPU/BIOS Region Write Access: %s\n",
  224. get_enabled(flmstr & (1 << 25)));
  225. printf(" Flash Descriptor Write Access: %s\n",
  226. get_enabled(flmstr & (1 << 24)));
  227. printf(" Platform Data Region Read Access: %s\n",
  228. get_enabled(flmstr & (1 << 20)));
  229. printf(" GbE Region Read Access: %s\n",
  230. get_enabled(flmstr & (1 << 19)));
  231. printf(" Intel ME Region Read Access: %s\n",
  232. get_enabled(flmstr & (1 << 18)));
  233. printf(" Host CPU/BIOS Region Read Access: %s\n",
  234. get_enabled(flmstr & (1 << 17)));
  235. printf(" Flash Descriptor Read Access: %s\n",
  236. get_enabled(flmstr & (1 << 16)));
  237. printf(" Requester ID: 0x%04x\n\n",
  238. flmstr & 0xffff);
  239. }
  240. static void dump_fmba(struct fmba_t *fmba)
  241. {
  242. printf("Found Master Section\n");
  243. printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
  244. decode_flmstr(fmba->flmstr1);
  245. printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
  246. decode_flmstr(fmba->flmstr2);
  247. printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
  248. decode_flmstr(fmba->flmstr3);
  249. }
  250. static void dump_fmsba(struct fmsba_t *fmsba)
  251. {
  252. int i;
  253. printf("Found Processor Strap Section\n");
  254. for (i = 0; i < 4; i++)
  255. printf("????: 0x%08x\n", fmsba->data[0]);
  256. }
  257. static void dump_jid(uint32_t jid)
  258. {
  259. printf(" SPI Component Device ID 1: 0x%02x\n",
  260. (jid >> 16) & 0xff);
  261. printf(" SPI Component Device ID 0: 0x%02x\n",
  262. (jid >> 8) & 0xff);
  263. printf(" SPI Component Vendor ID: 0x%02x\n",
  264. jid & 0xff);
  265. }
  266. static void dump_vscc(uint32_t vscc)
  267. {
  268. printf(" Lower Erase Opcode: 0x%02x\n",
  269. vscc >> 24);
  270. printf(" Lower Write Enable on Write Status: 0x%02x\n",
  271. vscc & (1 << 20) ? 0x06 : 0x50);
  272. printf(" Lower Write Status Required: %s\n",
  273. vscc & (1 << 19) ? "Yes" : "No");
  274. printf(" Lower Write Granularity: %d bytes\n",
  275. vscc & (1 << 18) ? 64 : 1);
  276. printf(" Lower Block / Sector Erase Size: ");
  277. switch ((vscc >> 16) & 0x3) {
  278. case 0:
  279. printf("256 Byte\n");
  280. break;
  281. case 1:
  282. printf("4KB\n");
  283. break;
  284. case 2:
  285. printf("8KB\n");
  286. break;
  287. case 3:
  288. printf("64KB\n");
  289. break;
  290. }
  291. printf(" Upper Erase Opcode: 0x%02x\n",
  292. (vscc >> 8) & 0xff);
  293. printf(" Upper Write Enable on Write Status: 0x%02x\n",
  294. vscc & (1 << 4) ? 0x06 : 0x50);
  295. printf(" Upper Write Status Required: %s\n",
  296. vscc & (1 << 3) ? "Yes" : "No");
  297. printf(" Upper Write Granularity: %d bytes\n",
  298. vscc & (1 << 2) ? 64 : 1);
  299. printf(" Upper Block / Sector Erase Size: ");
  300. switch (vscc & 0x3) {
  301. case 0:
  302. printf("256 Byte\n");
  303. break;
  304. case 1:
  305. printf("4KB\n");
  306. break;
  307. case 2:
  308. printf("8KB\n");
  309. break;
  310. case 3:
  311. printf("64KB\n");
  312. break;
  313. }
  314. }
  315. static void dump_vtba(struct vtba_t *vtba, int vtl)
  316. {
  317. int i;
  318. int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
  319. printf("ME VSCC table:\n");
  320. for (i = 0; i < num; i++) {
  321. printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
  322. dump_jid(vtba->entry[i].jid);
  323. printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
  324. dump_vscc(vtba->entry[i].vscc);
  325. }
  326. printf("\n");
  327. }
  328. static void dump_oem(uint8_t *oem)
  329. {
  330. int i, j;
  331. printf("OEM Section:\n");
  332. for (i = 0; i < 4; i++) {
  333. printf("%02x:", i << 4);
  334. for (j = 0; j < 16; j++)
  335. printf(" %02x", oem[(i<<4)+j]);
  336. printf("\n");
  337. }
  338. printf("\n");
  339. }
  340. /**
  341. * dump_fd() - Display a dump of the full flash description
  342. *
  343. * @image: Pointer to image
  344. * @size: Size of image in bytes
  345. * @return 0 if OK, -1 on error
  346. */
  347. static int dump_fd(char *image, int size)
  348. {
  349. struct fdbar_t *fdb = find_fd(image, size);
  350. if (!fdb)
  351. return -1;
  352. printf("FLMAP0: 0x%08x\n", fdb->flmap0);
  353. printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
  354. printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
  355. printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
  356. printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
  357. printf("FLMAP1: 0x%08x\n", fdb->flmap1);
  358. printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
  359. printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
  360. printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
  361. printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
  362. printf("FLMAP2: 0x%08x\n", fdb->flmap2);
  363. printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
  364. printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
  365. printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
  366. printf(" Intel ME VSCC Table Length (VTL): %d\n",
  367. (fdb->flumap1 >> 8) & 0xff);
  368. printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
  369. (fdb->flumap1 & 0xff) << 4);
  370. dump_vtba((struct vtba_t *)
  371. (image + ((fdb->flumap1 & 0xff) << 4)),
  372. (fdb->flumap1 >> 8) & 0xff);
  373. dump_oem((uint8_t *)image + 0xf00);
  374. dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
  375. << 4)));
  376. dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
  377. dump_fpsba((struct fpsba_t *)
  378. (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
  379. dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
  380. dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
  381. return 0;
  382. }
  383. /**
  384. * write_regions() - Write each region from an image to its own file
  385. *
  386. * The filename to use in each case is fixed - see region_filename()
  387. *
  388. * @image: Pointer to image
  389. * @size: Size of image in bytes
  390. * @return 0 if OK, -ve on error
  391. */
  392. static int write_regions(char *image, int size)
  393. {
  394. struct fdbar_t *fdb;
  395. struct frba_t *frba;
  396. int ret = 0;
  397. int i;
  398. fdb = find_fd(image, size);
  399. if (!fdb)
  400. return -1;
  401. frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
  402. for (i = 0; i < MAX_REGIONS; i++) {
  403. struct region_t region;
  404. int region_fd;
  405. ret = get_region(frba, i, &region);
  406. if (ret)
  407. return ret;
  408. dump_region(i, frba);
  409. if (region.size <= 0)
  410. continue;
  411. region_fd = open(region_filename(i),
  412. O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
  413. S_IWUSR | S_IRGRP | S_IROTH);
  414. if (write(region_fd, image + region.base, region.size) !=
  415. region.size) {
  416. perror("Error while writing");
  417. ret = -1;
  418. }
  419. close(region_fd);
  420. }
  421. return ret;
  422. }
  423. static int perror_fname(const char *fmt, const char *fname)
  424. {
  425. char msg[strlen(fmt) + strlen(fname) + 1];
  426. sprintf(msg, fmt, fname);
  427. perror(msg);
  428. return -1;
  429. }
  430. /**
  431. * write_image() - Write the image to a file
  432. *
  433. * @filename: Filename to use for the image
  434. * @image: Pointer to image
  435. * @size: Size of image in bytes
  436. * @return 0 if OK, -ve on error
  437. */
  438. static int write_image(char *filename, char *image, int size)
  439. {
  440. int new_fd;
  441. debug("Writing new image to %s\n", filename);
  442. new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
  443. S_IWUSR | S_IRGRP | S_IROTH);
  444. if (new_fd < 0)
  445. return perror_fname("Could not open file '%s'", filename);
  446. if (write(new_fd, image, size) != size)
  447. return perror_fname("Could not write file '%s'", filename);
  448. close(new_fd);
  449. return 0;
  450. }
  451. /**
  452. * set_spi_frequency() - Set the SPI frequency to use when booting
  453. *
  454. * Several frequencies are supported, some of which work with fast devices.
  455. * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
  456. * Intel boot system uses this information somehow on boot.
  457. *
  458. * The image is updated with the supplied value
  459. *
  460. * @image: Pointer to image
  461. * @size: Size of image in bytes
  462. * @freq: SPI frequency to use
  463. */
  464. static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
  465. {
  466. struct fdbar_t *fdb = find_fd(image, size);
  467. struct fcba_t *fcba;
  468. fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
  469. /* clear bits 21-29 */
  470. fcba->flcomp &= ~0x3fe00000;
  471. /* Read ID and Read Status Clock Frequency */
  472. fcba->flcomp |= freq << 27;
  473. /* Write and Erase Clock Frequency */
  474. fcba->flcomp |= freq << 24;
  475. /* Fast Read Clock Frequency */
  476. fcba->flcomp |= freq << 21;
  477. }
  478. /**
  479. * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
  480. *
  481. * @image: Pointer to image
  482. * @size: Size of image in bytes
  483. */
  484. static void set_em100_mode(char *image, int size)
  485. {
  486. struct fdbar_t *fdb = find_fd(image, size);
  487. struct fcba_t *fcba;
  488. fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
  489. fcba->flcomp &= ~(1 << 30);
  490. set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
  491. }
  492. /**
  493. * lock_descriptor() - Lock the NE descriptor so it cannot be updated
  494. *
  495. * @image: Pointer to image
  496. * @size: Size of image in bytes
  497. */
  498. static void lock_descriptor(char *image, int size)
  499. {
  500. struct fdbar_t *fdb = find_fd(image, size);
  501. struct fmba_t *fmba;
  502. /*
  503. * TODO: Dynamically take Platform Data Region and GbE Region into
  504. * account.
  505. */
  506. fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
  507. fmba->flmstr1 = 0x0a0b0000;
  508. fmba->flmstr2 = 0x0c0d0000;
  509. fmba->flmstr3 = 0x08080118;
  510. }
  511. /**
  512. * unlock_descriptor() - Lock the NE descriptor so it can be updated
  513. *
  514. * @image: Pointer to image
  515. * @size: Size of image in bytes
  516. */
  517. static void unlock_descriptor(char *image, int size)
  518. {
  519. struct fdbar_t *fdb = find_fd(image, size);
  520. struct fmba_t *fmba;
  521. fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
  522. fmba->flmstr1 = 0xffff0000;
  523. fmba->flmstr2 = 0xffff0000;
  524. fmba->flmstr3 = 0x08080118;
  525. }
  526. /**
  527. * open_for_read() - Open a file for reading
  528. *
  529. * @fname: Filename to open
  530. * @sizep: Returns file size in bytes
  531. * @return 0 if OK, -1 on error
  532. */
  533. int open_for_read(const char *fname, int *sizep)
  534. {
  535. int fd = open(fname, O_RDONLY);
  536. struct stat buf;
  537. if (fd == -1)
  538. return perror_fname("Could not open file '%s'", fname);
  539. if (fstat(fd, &buf) == -1)
  540. return perror_fname("Could not stat file '%s'", fname);
  541. *sizep = buf.st_size;
  542. debug("File %s is %d bytes\n", fname, *sizep);
  543. return fd;
  544. }
  545. /**
  546. * inject_region() - Add a file to an image region
  547. *
  548. * This puts a file into a particular region of the flash. Several pre-defined
  549. * regions are used.
  550. *
  551. * @image: Pointer to image
  552. * @size: Size of image in bytes
  553. * @region_type: Region where the file should be added
  554. * @region_fname: Filename to add to the image
  555. * @return 0 if OK, -ve on error
  556. */
  557. int inject_region(char *image, int size, int region_type, char *region_fname)
  558. {
  559. struct fdbar_t *fdb = find_fd(image, size);
  560. struct region_t region;
  561. struct frba_t *frba;
  562. int region_size;
  563. int offset = 0;
  564. int region_fd;
  565. int ret;
  566. if (!fdb)
  567. exit(EXIT_FAILURE);
  568. frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
  569. ret = get_region(frba, region_type, &region);
  570. if (ret)
  571. return -1;
  572. if (region.size <= 0xfff) {
  573. fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
  574. region_name(region_type));
  575. return -1;
  576. }
  577. region_fd = open_for_read(region_fname, &region_size);
  578. if (region_fd < 0)
  579. return region_fd;
  580. if ((region_size > region.size) ||
  581. ((region_type != 1) && (region_size > region.size))) {
  582. fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
  583. region_name(region_type), region.size,
  584. region.size, region_size, region_size);
  585. return -1;
  586. }
  587. if ((region_type == 1) && (region_size < region.size)) {
  588. fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
  589. region_name(region_type), region.size,
  590. region.size, region_size, region_size);
  591. offset = region.size - region_size;
  592. memset(image + region.base, 0xff, offset);
  593. }
  594. if (size < region.base + offset + region_size) {
  595. fprintf(stderr, "Output file is too small. (%d < %d)\n",
  596. size, region.base + offset + region_size);
  597. return -1;
  598. }
  599. if (read(region_fd, image + region.base + offset, region_size)
  600. != region_size) {
  601. perror("Could not read file");
  602. return -1;
  603. }
  604. close(region_fd);
  605. debug("Adding %s as the %s section\n", region_fname,
  606. region_name(region_type));
  607. return 0;
  608. }
  609. /**
  610. * write_data() - Write some raw data into a region
  611. *
  612. * This puts a file into a particular place in the flash, ignoring the
  613. * regions. Be careful not to overwrite something important.
  614. *
  615. * @image: Pointer to image
  616. * @size: Size of image in bytes
  617. * @addr: x86 ROM address to put file. The ROM ends at
  618. * 0xffffffff so use an address relative to that. For an
  619. * 8MB ROM the start address is 0xfff80000.
  620. * @write_fname: Filename to add to the image
  621. * @offset_uboot_top: Offset of the top of U-Boot
  622. * @offset_uboot_start: Offset of the start of U-Boot
  623. * @return number of bytes written if OK, -ve on error
  624. */
  625. static int write_data(char *image, int size, unsigned int addr,
  626. const char *write_fname, int offset_uboot_top,
  627. int offset_uboot_start)
  628. {
  629. int write_fd, write_size;
  630. int offset;
  631. write_fd = open_for_read(write_fname, &write_size);
  632. if (write_fd < 0)
  633. return write_fd;
  634. offset = (uint32_t)(addr + size);
  635. if (offset_uboot_top) {
  636. if (offset_uboot_start < offset &&
  637. offset_uboot_top >= offset) {
  638. fprintf(stderr, "U-Boot image overlaps with region '%s'\n",
  639. write_fname);
  640. fprintf(stderr,
  641. "U-Boot finishes at offset %x, file starts at %x\n",
  642. offset_uboot_top, offset);
  643. return -EXDEV;
  644. }
  645. if (offset_uboot_start > offset &&
  646. offset_uboot_start <= offset + write_size) {
  647. fprintf(stderr, "U-Boot image overlaps with region '%s'\n",
  648. write_fname);
  649. fprintf(stderr,
  650. "U-Boot starts at offset %x, file finishes at %x\n",
  651. offset_uboot_start, offset + write_size);
  652. return -EXDEV;
  653. }
  654. }
  655. debug("Writing %s to offset %#x\n", write_fname, offset);
  656. if (offset < 0 || offset + write_size > size) {
  657. fprintf(stderr, "Output file is too small. (%d < %d)\n",
  658. size, offset + write_size);
  659. return -1;
  660. }
  661. if (read(write_fd, image + offset, write_size) != write_size) {
  662. perror("Could not read file");
  663. return -1;
  664. }
  665. close(write_fd);
  666. return write_size;
  667. }
  668. static int scan_ucode(const void *blob, char *ucode_base, int *countp,
  669. const char **datap, int *data_sizep)
  670. {
  671. const char *data = NULL;
  672. int node, count;
  673. int data_size;
  674. char *ucode;
  675. for (node = 0, count = 0, ucode = ucode_base; node >= 0; count++) {
  676. node = fdt_node_offset_by_compatible(blob, node,
  677. "intel,microcode");
  678. if (node < 0)
  679. break;
  680. data = fdt_getprop(blob, node, "data", &data_size);
  681. if (!data) {
  682. debug("Missing microcode data in FDT '%s': %s\n",
  683. fdt_get_name(blob, node, NULL),
  684. fdt_strerror(data_size));
  685. return -ENOENT;
  686. }
  687. if (ucode_base)
  688. memcpy(ucode, data, data_size);
  689. ucode += data_size;
  690. }
  691. if (countp)
  692. *countp = count;
  693. if (datap)
  694. *datap = data;
  695. if (data_sizep)
  696. *data_sizep = data_size;
  697. return ucode - ucode_base;
  698. }
  699. static int remove_ucode(char *blob)
  700. {
  701. int node, count;
  702. int ret;
  703. /* Keep going until we find no more microcode to remove */
  704. do {
  705. for (node = 0, count = 0; node >= 0;) {
  706. int ret;
  707. node = fdt_node_offset_by_compatible(blob, node,
  708. "intel,microcode");
  709. if (node < 0)
  710. break;
  711. ret = fdt_delprop(blob, node, "data");
  712. /*
  713. * -FDT_ERR_NOTFOUND means we already removed the
  714. * data for this one, so we just continue.
  715. * 0 means we did remove it, so offsets may have
  716. * changed and we need to restart our scan.
  717. * Anything else indicates an error we should report.
  718. */
  719. if (ret == -FDT_ERR_NOTFOUND)
  720. continue;
  721. else if (!ret)
  722. node = 0;
  723. else
  724. return ret;
  725. }
  726. } while (count);
  727. /* Pack down to remove excees space */
  728. ret = fdt_pack(blob);
  729. if (ret)
  730. return ret;
  731. return fdt_totalsize(blob);
  732. }
  733. static int write_ucode(char *image, int size, struct input_file *fdt,
  734. int fdt_size, unsigned int ucode_ptr,
  735. int collate_ucode)
  736. {
  737. const char *data = NULL;
  738. char *ucode_buf;
  739. const void *blob;
  740. char *ucode_base;
  741. uint32_t *ptr;
  742. int ucode_size;
  743. int data_size;
  744. int offset;
  745. int count;
  746. int ret;
  747. blob = (void *)image + (uint32_t)(fdt->addr + size);
  748. debug("DTB at %lx\n", (char *)blob - image);
  749. /* Find out about the micrcode we have */
  750. ucode_size = scan_ucode(blob, NULL, &count, &data, &data_size);
  751. if (ucode_size < 0)
  752. return ucode_size;
  753. if (!count) {
  754. debug("No microcode found in FDT\n");
  755. return -ENOENT;
  756. }
  757. if (count > 1 && !collate_ucode) {
  758. fprintf(stderr,
  759. "Cannot handle multiple microcode blocks - please use -C flag to collate them\n");
  760. return -EMLINK;
  761. }
  762. /*
  763. * Collect the microcode into a buffer, remove it from the device
  764. * tree and place it immediately above the (now smaller) device tree.
  765. */
  766. if (collate_ucode && count > 1) {
  767. ucode_buf = malloc(ucode_size);
  768. if (!ucode_buf) {
  769. fprintf(stderr,
  770. "Out of memory for microcode (%d bytes)\n",
  771. ucode_size);
  772. return -ENOMEM;
  773. }
  774. ret = scan_ucode(blob, ucode_buf, NULL, NULL, NULL);
  775. if (ret < 0)
  776. return ret;
  777. /* Remove the microcode from the device tree */
  778. ret = remove_ucode((char *)blob);
  779. if (ret < 0) {
  780. debug("Could not remove FDT microcode: %s\n",
  781. fdt_strerror(ret));
  782. return -EINVAL;
  783. }
  784. debug("Collated %d microcode block(s)\n", count);
  785. debug("Device tree reduced from %x to %x bytes\n",
  786. fdt_size, ret);
  787. fdt_size = ret;
  788. /*
  789. * Place microcode area immediately above the FDT, aligned
  790. * to a 16-byte boundary.
  791. */
  792. ucode_base = (char *)(((unsigned long)blob + fdt_size + 15) &
  793. ~15);
  794. data = ucode_base;
  795. data_size = ucode_size;
  796. memcpy(ucode_base, ucode_buf, ucode_size);
  797. free(ucode_buf);
  798. }
  799. offset = (uint32_t)(ucode_ptr + size);
  800. ptr = (void *)image + offset;
  801. ptr[0] = (data - image) - size;
  802. ptr[1] = data_size;
  803. debug("Wrote microcode pointer at %x: addr=%x, size=%x\n", ucode_ptr,
  804. ptr[0], ptr[1]);
  805. return (collate_ucode ? data + data_size : (char *)blob + fdt_size) -
  806. image;
  807. }
  808. /**
  809. * write_uboot() - Write U-Boot, device tree and microcode pointer
  810. *
  811. * This writes U-Boot into a place in the flash, followed by its device tree.
  812. * The microcode pointer is written so that U-Boot can find the microcode in
  813. * the device tree very early in boot.
  814. *
  815. * @image: Pointer to image
  816. * @size: Size of image in bytes
  817. * @uboot: Input file information for u-boot.bin
  818. * @fdt: Input file information for u-boot.dtb
  819. * @ucode_ptr: Address in U-Boot where the microcode pointer should be placed
  820. * @return 0 if OK, -ve on error
  821. */
  822. static int write_uboot(char *image, int size, struct input_file *uboot,
  823. struct input_file *fdt, unsigned int ucode_ptr,
  824. int collate_ucode, int *offset_uboot_top,
  825. int *offset_uboot_start)
  826. {
  827. int uboot_size, fdt_size;
  828. int uboot_top;
  829. uboot_size = write_data(image, size, uboot->addr, uboot->fname, 0, 0);
  830. if (uboot_size < 0)
  831. return uboot_size;
  832. fdt->addr = uboot->addr + uboot_size;
  833. debug("U-Boot size %#x, FDT at %#x\n", uboot_size, fdt->addr);
  834. fdt_size = write_data(image, size, fdt->addr, fdt->fname, 0, 0);
  835. if (fdt_size < 0)
  836. return fdt_size;
  837. uboot_top = (uint32_t)(fdt->addr + size) + fdt_size;
  838. if (ucode_ptr) {
  839. uboot_top = write_ucode(image, size, fdt, fdt_size, ucode_ptr,
  840. collate_ucode);
  841. if (uboot_top < 0)
  842. return uboot_top;
  843. }
  844. if (offset_uboot_top && offset_uboot_start) {
  845. *offset_uboot_top = uboot_top;
  846. *offset_uboot_start = (uint32_t)(uboot->addr + size);
  847. }
  848. return 0;
  849. }
  850. static void print_version(void)
  851. {
  852. printf("ifdtool v%s -- ", IFDTOOL_VERSION);
  853. printf("Copyright (C) 2014 Google Inc.\n\n");
  854. printf("SPDX-License-Identifier: GPL-2.0+\n");
  855. }
  856. static void print_usage(const char *name)
  857. {
  858. printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
  859. printf("\n"
  860. " -d | --dump: dump intel firmware descriptor\n"
  861. " -x | --extract: extract intel fd modules\n"
  862. " -i | --inject <region>:<module> inject file <module> into region <region>\n"
  863. " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
  864. " multiple files can be written simultaneously\n"
  865. " -s | --spifreq <20|33|50> set the SPI frequency\n"
  866. " -e | --em100 set SPI frequency to 20MHz and disable\n"
  867. " Dual Output Fast Read Support\n"
  868. " -l | --lock Lock firmware descriptor and ME region\n"
  869. " -u | --unlock Unlock firmware descriptor and ME region\n"
  870. " -r | --romsize Specify ROM size\n"
  871. " -D | --write-descriptor <file> Write descriptor at base\n"
  872. " -c | --create Create a new empty image\n"
  873. " -v | --version: print the version\n"
  874. " -h | --help: print this help\n\n"
  875. "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
  876. "\n");
  877. }
  878. /**
  879. * get_two_words() - Convert a string into two words separated by :
  880. *
  881. * The supplied string is split at ':', two substrings are allocated and
  882. * returned.
  883. *
  884. * @str: String to split
  885. * @firstp: Returns first string
  886. * @secondp: Returns second string
  887. * @return 0 if OK, -ve if @str does not have a :
  888. */
  889. static int get_two_words(const char *str, char **firstp, char **secondp)
  890. {
  891. const char *p;
  892. p = strchr(str, ':');
  893. if (!p)
  894. return -1;
  895. *firstp = strdup(str);
  896. (*firstp)[p - str] = '\0';
  897. *secondp = strdup(p + 1);
  898. return 0;
  899. }
  900. int main(int argc, char *argv[])
  901. {
  902. int opt, option_index = 0;
  903. int mode_dump = 0, mode_extract = 0, mode_inject = 0;
  904. int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
  905. int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
  906. int create = 0, collate_ucode = 0;
  907. char *region_type_string = NULL, *inject_fname = NULL;
  908. char *desc_fname = NULL, *addr_str = NULL;
  909. int region_type = -1, inputfreq = 0;
  910. enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
  911. struct input_file input_file[WRITE_MAX], *ifile, *fdt = NULL;
  912. unsigned char wr_idx, wr_num = 0;
  913. int rom_size = -1;
  914. bool write_it;
  915. char *filename;
  916. char *outfile = NULL;
  917. struct stat buf;
  918. int size = 0;
  919. unsigned int ucode_ptr = 0;
  920. bool have_uboot = false;
  921. int bios_fd;
  922. char *image;
  923. int ret;
  924. static struct option long_options[] = {
  925. {"create", 0, NULL, 'c'},
  926. {"collate-microcode", 0, NULL, 'C'},
  927. {"dump", 0, NULL, 'd'},
  928. {"descriptor", 1, NULL, 'D'},
  929. {"em100", 0, NULL, 'e'},
  930. {"extract", 0, NULL, 'x'},
  931. {"fdt", 1, NULL, 'f'},
  932. {"inject", 1, NULL, 'i'},
  933. {"lock", 0, NULL, 'l'},
  934. {"microcode", 1, NULL, 'm'},
  935. {"romsize", 1, NULL, 'r'},
  936. {"spifreq", 1, NULL, 's'},
  937. {"unlock", 0, NULL, 'u'},
  938. {"uboot", 1, NULL, 'U'},
  939. {"write", 1, NULL, 'w'},
  940. {"version", 0, NULL, 'v'},
  941. {"help", 0, NULL, 'h'},
  942. {0, 0, 0, 0}
  943. };
  944. while ((opt = getopt_long(argc, argv, "cCdD:ef:hi:lm:r:s:uU:vw:x?",
  945. long_options, &option_index)) != EOF) {
  946. switch (opt) {
  947. case 'c':
  948. create = 1;
  949. break;
  950. case 'C':
  951. collate_ucode = 1;
  952. break;
  953. case 'd':
  954. mode_dump = 1;
  955. break;
  956. case 'D':
  957. mode_write_descriptor = 1;
  958. desc_fname = optarg;
  959. break;
  960. case 'e':
  961. mode_em100 = 1;
  962. break;
  963. case 'i':
  964. if (get_two_words(optarg, &region_type_string,
  965. &inject_fname)) {
  966. print_usage(argv[0]);
  967. exit(EXIT_FAILURE);
  968. }
  969. if (!strcasecmp("Descriptor", region_type_string))
  970. region_type = 0;
  971. else if (!strcasecmp("BIOS", region_type_string))
  972. region_type = 1;
  973. else if (!strcasecmp("ME", region_type_string))
  974. region_type = 2;
  975. else if (!strcasecmp("GbE", region_type_string))
  976. region_type = 3;
  977. else if (!strcasecmp("Platform", region_type_string))
  978. region_type = 4;
  979. if (region_type == -1) {
  980. fprintf(stderr, "No such region type: '%s'\n\n",
  981. region_type_string);
  982. print_usage(argv[0]);
  983. exit(EXIT_FAILURE);
  984. }
  985. mode_inject = 1;
  986. break;
  987. case 'l':
  988. mode_locked = 1;
  989. break;
  990. case 'm':
  991. ucode_ptr = strtoul(optarg, NULL, 0);
  992. break;
  993. case 'r':
  994. rom_size = strtol(optarg, NULL, 0);
  995. debug("ROM size %d\n", rom_size);
  996. break;
  997. case 's':
  998. /* Parse the requested SPI frequency */
  999. inputfreq = strtol(optarg, NULL, 0);
  1000. switch (inputfreq) {
  1001. case 20:
  1002. spifreq = SPI_FREQUENCY_20MHZ;
  1003. break;
  1004. case 33:
  1005. spifreq = SPI_FREQUENCY_33MHZ;
  1006. break;
  1007. case 50:
  1008. spifreq = SPI_FREQUENCY_50MHZ;
  1009. break;
  1010. default:
  1011. fprintf(stderr, "Invalid SPI Frequency: %d\n",
  1012. inputfreq);
  1013. print_usage(argv[0]);
  1014. exit(EXIT_FAILURE);
  1015. }
  1016. mode_spifreq = 1;
  1017. break;
  1018. case 'u':
  1019. mode_unlocked = 1;
  1020. break;
  1021. case 'v':
  1022. print_version();
  1023. exit(EXIT_SUCCESS);
  1024. break;
  1025. case 'w':
  1026. case 'U':
  1027. case 'f':
  1028. ifile = &input_file[wr_num];
  1029. mode_write = 1;
  1030. if (wr_num < WRITE_MAX) {
  1031. if (get_two_words(optarg, &addr_str,
  1032. &ifile->fname)) {
  1033. print_usage(argv[0]);
  1034. exit(EXIT_FAILURE);
  1035. }
  1036. ifile->addr = strtoll(optarg, NULL, 0);
  1037. ifile->type = opt == 'f' ? IF_fdt :
  1038. opt == 'U' ? IF_uboot : IF_normal;
  1039. if (ifile->type == IF_fdt)
  1040. fdt = ifile;
  1041. else if (ifile->type == IF_uboot)
  1042. have_uboot = true;
  1043. wr_num++;
  1044. } else {
  1045. fprintf(stderr,
  1046. "The number of files to write simultaneously exceeds the limitation (%d)\n",
  1047. WRITE_MAX);
  1048. }
  1049. break;
  1050. case 'x':
  1051. mode_extract = 1;
  1052. break;
  1053. case 'h':
  1054. case '?':
  1055. default:
  1056. print_usage(argv[0]);
  1057. exit(EXIT_SUCCESS);
  1058. break;
  1059. }
  1060. }
  1061. if (mode_locked == 1 && mode_unlocked == 1) {
  1062. fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
  1063. exit(EXIT_FAILURE);
  1064. }
  1065. if (mode_inject == 1 && mode_write == 1) {
  1066. fprintf(stderr, "Inject/Write are mutually exclusive\n");
  1067. exit(EXIT_FAILURE);
  1068. }
  1069. if ((mode_dump + mode_extract + mode_inject +
  1070. (mode_spifreq | mode_em100 | mode_unlocked |
  1071. mode_locked)) > 1) {
  1072. fprintf(stderr, "You may not specify more than one mode.\n\n");
  1073. print_usage(argv[0]);
  1074. exit(EXIT_FAILURE);
  1075. }
  1076. if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
  1077. mode_em100 + mode_locked + mode_unlocked + mode_write +
  1078. mode_write_descriptor) == 0 && !create) {
  1079. fprintf(stderr, "You need to specify a mode.\n\n");
  1080. print_usage(argv[0]);
  1081. exit(EXIT_FAILURE);
  1082. }
  1083. if (create && rom_size == -1) {
  1084. fprintf(stderr, "You need to specify a rom size when creating.\n\n");
  1085. exit(EXIT_FAILURE);
  1086. }
  1087. if (optind + 1 != argc) {
  1088. fprintf(stderr, "You need to specify a file.\n\n");
  1089. print_usage(argv[0]);
  1090. exit(EXIT_FAILURE);
  1091. }
  1092. if (have_uboot && !fdt) {
  1093. fprintf(stderr,
  1094. "You must supply a device tree file for U-Boot\n\n");
  1095. print_usage(argv[0]);
  1096. exit(EXIT_FAILURE);
  1097. }
  1098. filename = argv[optind];
  1099. if (optind + 2 != argc)
  1100. outfile = argv[optind + 1];
  1101. if (create)
  1102. bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
  1103. else
  1104. bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
  1105. if (bios_fd == -1) {
  1106. perror("Could not open file");
  1107. exit(EXIT_FAILURE);
  1108. }
  1109. if (!create) {
  1110. if (fstat(bios_fd, &buf) == -1) {
  1111. perror("Could not stat file");
  1112. exit(EXIT_FAILURE);
  1113. }
  1114. size = buf.st_size;
  1115. }
  1116. debug("File %s is %d bytes\n", filename, size);
  1117. if (rom_size == -1)
  1118. rom_size = size;
  1119. image = malloc(rom_size);
  1120. if (!image) {
  1121. printf("Out of memory.\n");
  1122. exit(EXIT_FAILURE);
  1123. }
  1124. memset(image, '\xff', rom_size);
  1125. if (!create && read(bios_fd, image, size) != size) {
  1126. perror("Could not read file");
  1127. exit(EXIT_FAILURE);
  1128. }
  1129. if (size != rom_size) {
  1130. debug("ROM size changed to %d bytes\n", rom_size);
  1131. size = rom_size;
  1132. }
  1133. write_it = true;
  1134. ret = 0;
  1135. if (mode_dump) {
  1136. ret = dump_fd(image, size);
  1137. write_it = false;
  1138. }
  1139. if (mode_extract) {
  1140. ret = write_regions(image, size);
  1141. write_it = false;
  1142. }
  1143. if (mode_write_descriptor)
  1144. ret = write_data(image, size, -size, desc_fname, 0, 0);
  1145. if (mode_inject)
  1146. ret = inject_region(image, size, region_type, inject_fname);
  1147. if (mode_write) {
  1148. int offset_uboot_top = 0;
  1149. int offset_uboot_start = 0;
  1150. for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
  1151. ifile = &input_file[wr_idx];
  1152. if (ifile->type == IF_fdt) {
  1153. continue;
  1154. } else if (ifile->type == IF_uboot) {
  1155. ret = write_uboot(image, size, ifile, fdt,
  1156. ucode_ptr, collate_ucode,
  1157. &offset_uboot_top,
  1158. &offset_uboot_start);
  1159. } else {
  1160. ret = write_data(image, size, ifile->addr,
  1161. ifile->fname, offset_uboot_top,
  1162. offset_uboot_start);
  1163. }
  1164. if (ret < 0)
  1165. break;
  1166. }
  1167. }
  1168. if (mode_spifreq)
  1169. set_spi_frequency(image, size, spifreq);
  1170. if (mode_em100)
  1171. set_em100_mode(image, size);
  1172. if (mode_locked)
  1173. lock_descriptor(image, size);
  1174. if (mode_unlocked)
  1175. unlock_descriptor(image, size);
  1176. if (write_it) {
  1177. if (outfile) {
  1178. ret = write_image(outfile, image, size);
  1179. } else {
  1180. if (lseek(bios_fd, 0, SEEK_SET)) {
  1181. perror("Error while seeking");
  1182. ret = -1;
  1183. }
  1184. if (write(bios_fd, image, size) != size) {
  1185. perror("Error while writing");
  1186. ret = -1;
  1187. }
  1188. }
  1189. }
  1190. free(image);
  1191. close(bios_fd);
  1192. return ret < 0 ? 1 : 0;
  1193. }