ifdtool.c 28 KB

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