ifdtool.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. * @return number of bytes written if OK, -ve on error
  622. */
  623. static int write_data(char *image, int size, unsigned int addr,
  624. const char *write_fname)
  625. {
  626. int write_fd, write_size;
  627. int offset;
  628. write_fd = open_for_read(write_fname, &write_size);
  629. if (write_fd < 0)
  630. return write_fd;
  631. offset = (uint32_t)(addr + size);
  632. debug("Writing %s to offset %#x\n", write_fname, offset);
  633. if (offset < 0 || offset + write_size > size) {
  634. fprintf(stderr, "Output file is too small. (%d < %d)\n",
  635. size, offset + write_size);
  636. return -1;
  637. }
  638. if (read(write_fd, image + offset, write_size) != write_size) {
  639. perror("Could not read file");
  640. return -1;
  641. }
  642. close(write_fd);
  643. return write_size;
  644. }
  645. /**
  646. * write_uboot() - Write U-Boot, device tree and microcode pointer
  647. *
  648. * This writes U-Boot into a place in the flash, followed by its device tree.
  649. * The microcode pointer is written so that U-Boot can find the microcode in
  650. * the device tree very early in boot.
  651. *
  652. * @image: Pointer to image
  653. * @size: Size of image in bytes
  654. * @uboot: Input file information for u-boot.bin
  655. * @fdt: Input file information for u-boot.dtb
  656. * @ucode_ptr: Address in U-Boot where the microcode pointer should be placed
  657. * @return 0 if OK, -ve on error
  658. */
  659. static int write_uboot(char *image, int size, struct input_file *uboot,
  660. struct input_file *fdt, unsigned int ucode_ptr)
  661. {
  662. const void *blob;
  663. const char *data;
  664. int uboot_size;
  665. uint32_t *ptr;
  666. int data_size;
  667. int offset;
  668. int node;
  669. int ret;
  670. uboot_size = write_data(image, size, uboot->addr, uboot->fname);
  671. if (uboot_size < 0)
  672. return uboot_size;
  673. fdt->addr = uboot->addr + uboot_size;
  674. debug("U-Boot size %#x, FDT at %#x\n", uboot_size, fdt->addr);
  675. ret = write_data(image, size, fdt->addr, fdt->fname);
  676. if (ret < 0)
  677. return ret;
  678. if (ucode_ptr) {
  679. blob = (void *)image + (uint32_t)(fdt->addr + size);
  680. debug("DTB at %lx\n", (char *)blob - image);
  681. node = fdt_node_offset_by_compatible(blob, 0,
  682. "intel,microcode");
  683. if (node < 0) {
  684. debug("No microcode found in FDT: %s\n",
  685. fdt_strerror(node));
  686. return -ENOENT;
  687. }
  688. data = fdt_getprop(blob, node, "data", &data_size);
  689. if (!data) {
  690. debug("No microcode data found in FDT: %s\n",
  691. fdt_strerror(data_size));
  692. return -ENOENT;
  693. }
  694. offset = (uint32_t)(ucode_ptr + size);
  695. ptr = (void *)image + offset;
  696. ptr[0] = (data - image) - size;
  697. ptr[1] = data_size;
  698. debug("Wrote microcode pointer at %x: addr=%x, size=%x\n",
  699. ucode_ptr, ptr[0], ptr[1]);
  700. }
  701. return 0;
  702. }
  703. static void print_version(void)
  704. {
  705. printf("ifdtool v%s -- ", IFDTOOL_VERSION);
  706. printf("Copyright (C) 2014 Google Inc.\n\n");
  707. printf("SPDX-License-Identifier: GPL-2.0+\n");
  708. }
  709. static void print_usage(const char *name)
  710. {
  711. printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
  712. printf("\n"
  713. " -d | --dump: dump intel firmware descriptor\n"
  714. " -x | --extract: extract intel fd modules\n"
  715. " -i | --inject <region>:<module> inject file <module> into region <region>\n"
  716. " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
  717. " multiple files can be written simultaneously\n"
  718. " -s | --spifreq <20|33|50> set the SPI frequency\n"
  719. " -e | --em100 set SPI frequency to 20MHz and disable\n"
  720. " Dual Output Fast Read Support\n"
  721. " -l | --lock Lock firmware descriptor and ME region\n"
  722. " -u | --unlock Unlock firmware descriptor and ME region\n"
  723. " -r | --romsize Specify ROM size\n"
  724. " -D | --write-descriptor <file> Write descriptor at base\n"
  725. " -c | --create Create a new empty image\n"
  726. " -v | --version: print the version\n"
  727. " -h | --help: print this help\n\n"
  728. "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
  729. "\n");
  730. }
  731. /**
  732. * get_two_words() - Convert a string into two words separated by :
  733. *
  734. * The supplied string is split at ':', two substrings are allocated and
  735. * returned.
  736. *
  737. * @str: String to split
  738. * @firstp: Returns first string
  739. * @secondp: Returns second string
  740. * @return 0 if OK, -ve if @str does not have a :
  741. */
  742. static int get_two_words(const char *str, char **firstp, char **secondp)
  743. {
  744. const char *p;
  745. p = strchr(str, ':');
  746. if (!p)
  747. return -1;
  748. *firstp = strdup(str);
  749. (*firstp)[p - str] = '\0';
  750. *secondp = strdup(p + 1);
  751. return 0;
  752. }
  753. int main(int argc, char *argv[])
  754. {
  755. int opt, option_index = 0;
  756. int mode_dump = 0, mode_extract = 0, mode_inject = 0;
  757. int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
  758. int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
  759. int create = 0;
  760. char *region_type_string = NULL, *inject_fname = NULL;
  761. char *desc_fname = NULL, *addr_str = NULL;
  762. int region_type = -1, inputfreq = 0;
  763. enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
  764. struct input_file input_file[WRITE_MAX], *ifile, *fdt = NULL;
  765. unsigned char wr_idx, wr_num = 0;
  766. int rom_size = -1;
  767. bool write_it;
  768. char *filename;
  769. char *outfile = NULL;
  770. struct stat buf;
  771. int size = 0;
  772. unsigned int ucode_ptr = 0;
  773. bool have_uboot = false;
  774. int bios_fd;
  775. char *image;
  776. int ret;
  777. static struct option long_options[] = {
  778. {"create", 0, NULL, 'c'},
  779. {"dump", 0, NULL, 'd'},
  780. {"descriptor", 1, NULL, 'D'},
  781. {"em100", 0, NULL, 'e'},
  782. {"extract", 0, NULL, 'x'},
  783. {"fdt", 1, NULL, 'f'},
  784. {"inject", 1, NULL, 'i'},
  785. {"lock", 0, NULL, 'l'},
  786. {"microcode", 1, NULL, 'm'},
  787. {"romsize", 1, NULL, 'r'},
  788. {"spifreq", 1, NULL, 's'},
  789. {"unlock", 0, NULL, 'u'},
  790. {"uboot", 1, NULL, 'U'},
  791. {"write", 1, NULL, 'w'},
  792. {"version", 0, NULL, 'v'},
  793. {"help", 0, NULL, 'h'},
  794. {0, 0, 0, 0}
  795. };
  796. while ((opt = getopt_long(argc, argv, "cdD:ef:hi:lm:r:s:uU:vw:x?",
  797. long_options, &option_index)) != EOF) {
  798. switch (opt) {
  799. case 'c':
  800. create = 1;
  801. break;
  802. case 'd':
  803. mode_dump = 1;
  804. break;
  805. case 'D':
  806. mode_write_descriptor = 1;
  807. desc_fname = optarg;
  808. break;
  809. case 'e':
  810. mode_em100 = 1;
  811. break;
  812. case 'i':
  813. if (get_two_words(optarg, &region_type_string,
  814. &inject_fname)) {
  815. print_usage(argv[0]);
  816. exit(EXIT_FAILURE);
  817. }
  818. if (!strcasecmp("Descriptor", region_type_string))
  819. region_type = 0;
  820. else if (!strcasecmp("BIOS", region_type_string))
  821. region_type = 1;
  822. else if (!strcasecmp("ME", region_type_string))
  823. region_type = 2;
  824. else if (!strcasecmp("GbE", region_type_string))
  825. region_type = 3;
  826. else if (!strcasecmp("Platform", region_type_string))
  827. region_type = 4;
  828. if (region_type == -1) {
  829. fprintf(stderr, "No such region type: '%s'\n\n",
  830. region_type_string);
  831. print_usage(argv[0]);
  832. exit(EXIT_FAILURE);
  833. }
  834. mode_inject = 1;
  835. break;
  836. case 'l':
  837. mode_locked = 1;
  838. break;
  839. case 'm':
  840. ucode_ptr = strtoul(optarg, NULL, 0);
  841. break;
  842. case 'r':
  843. rom_size = strtol(optarg, NULL, 0);
  844. debug("ROM size %d\n", rom_size);
  845. break;
  846. case 's':
  847. /* Parse the requested SPI frequency */
  848. inputfreq = strtol(optarg, NULL, 0);
  849. switch (inputfreq) {
  850. case 20:
  851. spifreq = SPI_FREQUENCY_20MHZ;
  852. break;
  853. case 33:
  854. spifreq = SPI_FREQUENCY_33MHZ;
  855. break;
  856. case 50:
  857. spifreq = SPI_FREQUENCY_50MHZ;
  858. break;
  859. default:
  860. fprintf(stderr, "Invalid SPI Frequency: %d\n",
  861. inputfreq);
  862. print_usage(argv[0]);
  863. exit(EXIT_FAILURE);
  864. }
  865. mode_spifreq = 1;
  866. break;
  867. case 'u':
  868. mode_unlocked = 1;
  869. break;
  870. case 'v':
  871. print_version();
  872. exit(EXIT_SUCCESS);
  873. break;
  874. case 'w':
  875. case 'U':
  876. case 'f':
  877. ifile = &input_file[wr_num];
  878. mode_write = 1;
  879. if (wr_num < WRITE_MAX) {
  880. if (get_two_words(optarg, &addr_str,
  881. &ifile->fname)) {
  882. print_usage(argv[0]);
  883. exit(EXIT_FAILURE);
  884. }
  885. ifile->addr = strtol(optarg, NULL, 0);
  886. ifile->type = opt == 'f' ? IF_fdt :
  887. opt == 'U' ? IF_uboot : IF_normal;
  888. if (ifile->type == IF_fdt)
  889. fdt = ifile;
  890. else if (ifile->type == IF_uboot)
  891. have_uboot = true;
  892. wr_num++;
  893. } else {
  894. fprintf(stderr,
  895. "The number of files to write simultaneously exceeds the limitation (%d)\n",
  896. WRITE_MAX);
  897. }
  898. break;
  899. case 'x':
  900. mode_extract = 1;
  901. break;
  902. case 'h':
  903. case '?':
  904. default:
  905. print_usage(argv[0]);
  906. exit(EXIT_SUCCESS);
  907. break;
  908. }
  909. }
  910. if (mode_locked == 1 && mode_unlocked == 1) {
  911. fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
  912. exit(EXIT_FAILURE);
  913. }
  914. if (mode_inject == 1 && mode_write == 1) {
  915. fprintf(stderr, "Inject/Write are mutually exclusive\n");
  916. exit(EXIT_FAILURE);
  917. }
  918. if ((mode_dump + mode_extract + mode_inject +
  919. (mode_spifreq | mode_em100 | mode_unlocked |
  920. mode_locked)) > 1) {
  921. fprintf(stderr, "You may not specify more than one mode.\n\n");
  922. print_usage(argv[0]);
  923. exit(EXIT_FAILURE);
  924. }
  925. if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
  926. mode_em100 + mode_locked + mode_unlocked + mode_write +
  927. mode_write_descriptor) == 0 && !create) {
  928. fprintf(stderr, "You need to specify a mode.\n\n");
  929. print_usage(argv[0]);
  930. exit(EXIT_FAILURE);
  931. }
  932. if (create && rom_size == -1) {
  933. fprintf(stderr, "You need to specify a rom size when creating.\n\n");
  934. exit(EXIT_FAILURE);
  935. }
  936. if (optind + 1 != argc) {
  937. fprintf(stderr, "You need to specify a file.\n\n");
  938. print_usage(argv[0]);
  939. exit(EXIT_FAILURE);
  940. }
  941. if (have_uboot && !fdt) {
  942. fprintf(stderr,
  943. "You must supply a device tree file for U-Boot\n\n");
  944. print_usage(argv[0]);
  945. exit(EXIT_FAILURE);
  946. }
  947. filename = argv[optind];
  948. if (optind + 2 != argc)
  949. outfile = argv[optind + 1];
  950. if (create)
  951. bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
  952. else
  953. bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
  954. if (bios_fd == -1) {
  955. perror("Could not open file");
  956. exit(EXIT_FAILURE);
  957. }
  958. if (!create) {
  959. if (fstat(bios_fd, &buf) == -1) {
  960. perror("Could not stat file");
  961. exit(EXIT_FAILURE);
  962. }
  963. size = buf.st_size;
  964. }
  965. debug("File %s is %d bytes\n", filename, size);
  966. if (rom_size == -1)
  967. rom_size = size;
  968. image = malloc(rom_size);
  969. if (!image) {
  970. printf("Out of memory.\n");
  971. exit(EXIT_FAILURE);
  972. }
  973. memset(image, '\xff', rom_size);
  974. if (!create && read(bios_fd, image, size) != size) {
  975. perror("Could not read file");
  976. exit(EXIT_FAILURE);
  977. }
  978. if (size != rom_size) {
  979. debug("ROM size changed to %d bytes\n", rom_size);
  980. size = rom_size;
  981. }
  982. write_it = true;
  983. ret = 0;
  984. if (mode_dump) {
  985. ret = dump_fd(image, size);
  986. write_it = false;
  987. }
  988. if (mode_extract) {
  989. ret = write_regions(image, size);
  990. write_it = false;
  991. }
  992. if (mode_write_descriptor)
  993. ret = write_data(image, size, -size, desc_fname);
  994. if (mode_inject)
  995. ret = inject_region(image, size, region_type, inject_fname);
  996. if (mode_write) {
  997. for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
  998. ifile = &input_file[wr_idx];
  999. if (ifile->type == IF_fdt) {
  1000. continue;
  1001. } else if (ifile->type == IF_uboot) {
  1002. ret = write_uboot(image, size, ifile, fdt,
  1003. ucode_ptr);
  1004. } else {
  1005. ret = write_data(image, size, ifile->addr,
  1006. ifile->fname);
  1007. }
  1008. if (ret < 0)
  1009. break;
  1010. }
  1011. }
  1012. if (mode_spifreq)
  1013. set_spi_frequency(image, size, spifreq);
  1014. if (mode_em100)
  1015. set_em100_mode(image, size);
  1016. if (mode_locked)
  1017. lock_descriptor(image, size);
  1018. if (mode_unlocked)
  1019. unlock_descriptor(image, size);
  1020. if (write_it) {
  1021. if (outfile) {
  1022. ret = write_image(outfile, image, size);
  1023. } else {
  1024. if (lseek(bios_fd, 0, SEEK_SET)) {
  1025. perror("Error while seeking");
  1026. ret = -1;
  1027. }
  1028. if (write(bios_fd, image, size) != size) {
  1029. perror("Error while writing");
  1030. ret = -1;
  1031. }
  1032. }
  1033. }
  1034. free(image);
  1035. close(bios_fd);
  1036. return ret < 0 ? 1 : 0;
  1037. }