You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1432 lines
38 KiB

  1. /*
  2. * ifdtool - dump Intel Firmware Descriptor information
  3. *
  4. * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <getopt.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <commonlib/helpers.h>
  24. #include "ifdtool.h"
  25. #ifndef O_BINARY
  26. #define O_BINARY 0
  27. #endif
  28. /**
  29. * PTR_IN_RANGE - examine whether a pointer falls in [base, base + limit)
  30. * @param ptr: the non-void* pointer to a single arbitrary-sized object.
  31. * @param base: base address represented with char* type.
  32. * @param limit: upper limit of the legal address.
  33. *
  34. */
  35. #define PTR_IN_RANGE(ptr, base, limit) \
  36. ((const char *)(ptr) >= (base) && \
  37. (const char *)&(ptr)[1] <= (base) + (limit))
  38. static int ifd_version;
  39. static unsigned int max_regions = 0;
  40. static int selected_chip = 0;
  41. static int platform = -1;
  42. static const struct region_name region_names[MAX_REGIONS] = {
  43. { "Flash Descriptor", "fd", "flashregion_0_flashdescriptor.bin" },
  44. { "BIOS", "bios", "flashregion_1_bios.bin" },
  45. { "Intel ME", "me", "flashregion_2_intel_me.bin" },
  46. { "GbE", "gbe", "flashregion_3_gbe.bin" },
  47. { "Platform Data", "pd", "flashregion_4_platform_data.bin" },
  48. { "Reserved", "res1", "flashregion_5_reserved.bin" },
  49. { "Reserved", "res2", "flashregion_6_reserved.bin" },
  50. { "Reserved", "res3", "flashregion_7_reserved.bin" },
  51. { "EC", "ec", "flashregion_8_ec.bin" },
  52. };
  53. static fdbar_t *find_fd(char *image, int size)
  54. {
  55. int i, found = 0;
  56. /* Scan for FD signature */
  57. for (i = 0; i < (size - 4); i += 4) {
  58. if (*(uint32_t *) (image + i) == 0x0FF0A55A) {
  59. found = 1;
  60. break; // signature found.
  61. }
  62. }
  63. if (!found) {
  64. printf("No Flash Descriptor found in this image\n");
  65. return NULL;
  66. }
  67. fdbar_t *fdb = (fdbar_t *) (image + i);
  68. return PTR_IN_RANGE(fdb, image, size) ? fdb : NULL;
  69. }
  70. static fcba_t *find_fcba(char *image, int size)
  71. {
  72. fdbar_t *fdb = find_fd(image, size);
  73. if (!fdb)
  74. return NULL;
  75. fcba_t *fcba = (fcba_t *) (image + ((fdb->flmap0 & 0xff) << 4));
  76. return PTR_IN_RANGE(fcba, image, size) ? fcba : NULL;
  77. }
  78. static fmba_t *find_fmba(char *image, int size)
  79. {
  80. fdbar_t *fdb = find_fd(image, size);
  81. if (!fdb)
  82. return NULL;
  83. fmba_t *fmba = (fmba_t *) (image + ((fdb->flmap1 & 0xff) << 4));
  84. return PTR_IN_RANGE(fmba, image, size) ? fmba : NULL;
  85. }
  86. static frba_t *find_frba(char *image, int size)
  87. {
  88. fdbar_t *fdb = find_fd(image, size);
  89. if (!fdb)
  90. return NULL;
  91. frba_t *frba =
  92. (frba_t *) (image + (((fdb->flmap0 >> 16) & 0xff) << 4));
  93. return PTR_IN_RANGE(frba, image, size) ? frba : NULL;
  94. }
  95. static fpsba_t *find_fpsba(char *image, int size)
  96. {
  97. fdbar_t *fdb = find_fd(image, size);
  98. if (!fdb)
  99. return NULL;
  100. fpsba_t *fpsba =
  101. (fpsba_t *) (image + (((fdb->flmap1 >> 16) & 0xff) << 4));
  102. return PTR_IN_RANGE(fpsba, image, size) ? fpsba : NULL;
  103. }
  104. static fmsba_t *find_fmsba(char *image, int size)
  105. {
  106. fdbar_t *fdb = find_fd(image, size);
  107. if (!fdb)
  108. return NULL;
  109. fmsba_t *fmsba = (fmsba_t *) (image + ((fdb->flmap2 & 0xff) << 4));
  110. return PTR_IN_RANGE(fmsba, image, size) ? fmsba : NULL;
  111. }
  112. /*
  113. * There is no version field in the descriptor so to determine
  114. * if this is a new descriptor format we check the hardcoded SPI
  115. * read frequency to see if it is fixed at 20MHz or 17MHz.
  116. */
  117. static void check_ifd_version(char *image, int size)
  118. {
  119. int read_freq;
  120. const fcba_t *fcba = find_fcba(image, size);
  121. if (!fcba)
  122. exit(EXIT_FAILURE);
  123. read_freq = (fcba->flcomp >> 17) & 7;
  124. switch (read_freq) {
  125. case SPI_FREQUENCY_20MHZ:
  126. ifd_version = IFD_VERSION_1;
  127. max_regions = MAX_REGIONS_OLD;
  128. break;
  129. case SPI_FREQUENCY_17MHZ:
  130. case SPI_FREQUENCY_50MHZ_30MHZ:
  131. ifd_version = IFD_VERSION_2;
  132. max_regions = MAX_REGIONS;
  133. break;
  134. default:
  135. fprintf(stderr, "Unknown descriptor version: %d\n",
  136. read_freq);
  137. exit(EXIT_FAILURE);
  138. }
  139. }
  140. static region_t get_region(const frba_t *frba, unsigned int region_type)
  141. {
  142. int base_mask;
  143. int limit_mask;
  144. uint32_t flreg;
  145. region_t region;
  146. if (ifd_version >= IFD_VERSION_2)
  147. base_mask = 0x7fff;
  148. else
  149. base_mask = 0xfff;
  150. limit_mask = base_mask << 16;
  151. if (region_type >= max_regions) {
  152. fprintf(stderr, "Invalid region type %d.\n", region_type);
  153. exit (EXIT_FAILURE);
  154. }
  155. flreg = frba->flreg[region_type];
  156. region.base = (flreg & base_mask) << 12;
  157. region.limit = ((flreg & limit_mask) >> 4) | 0xfff;
  158. region.size = region.limit - region.base + 1;
  159. if (region.size < 0)
  160. region.size = 0;
  161. return region;
  162. }
  163. static void set_region(frba_t *frba, unsigned int region_type,
  164. const region_t *region)
  165. {
  166. if (region_type >= max_regions) {
  167. fprintf(stderr, "Invalid region type %u.\n", region_type);
  168. exit (EXIT_FAILURE);
  169. }
  170. frba->flreg[region_type] =
  171. (((region->limit >> 12) & 0x7fff) << 16) |
  172. ((region->base >> 12) & 0x7fff);
  173. }
  174. static const char *region_name(unsigned int region_type)
  175. {
  176. if (region_type >= max_regions) {
  177. fprintf(stderr, "Invalid region type.\n");
  178. exit (EXIT_FAILURE);
  179. }
  180. return region_names[region_type].pretty;
  181. }
  182. static const char *region_name_short(unsigned int region_type)
  183. {
  184. if (region_type >= max_regions) {
  185. fprintf(stderr, "Invalid region type.\n");
  186. exit (EXIT_FAILURE);
  187. }
  188. return region_names[region_type].terse;
  189. }
  190. static int region_num(const char *name)
  191. {
  192. unsigned int i;
  193. for (i = 0; i < max_regions; i++) {
  194. if (strcasecmp(name, region_names[i].pretty) == 0)
  195. return i;
  196. if (strcasecmp(name, region_names[i].terse) == 0)
  197. return i;
  198. }
  199. return -1;
  200. }
  201. static const char *region_filename(unsigned int region_type)
  202. {
  203. if (region_type >= max_regions) {
  204. fprintf(stderr, "Invalid region type %d.\n", region_type);
  205. exit (EXIT_FAILURE);
  206. }
  207. return region_names[region_type].filename;
  208. }
  209. static void dump_region(unsigned int num, const frba_t *frba)
  210. {
  211. region_t region = get_region(frba, num);
  212. printf(" Flash Region %d (%s): %08x - %08x %s\n",
  213. num, region_name(num), region.base, region.limit,
  214. region.size < 1 ? "(unused)" : "");
  215. }
  216. static void dump_region_layout(char *buf, size_t bufsize, unsigned int num,
  217. const frba_t *frba)
  218. {
  219. region_t region = get_region(frba, num);
  220. snprintf(buf, bufsize, "%08x:%08x %s\n",
  221. region.base, region.limit, region_name_short(num));
  222. }
  223. static void dump_frba(const frba_t *frba)
  224. {
  225. unsigned int i;
  226. printf("Found Region Section\n");
  227. for (i = 0; i < max_regions; i++) {
  228. printf("FLREG%u: 0x%08x\n", i, frba->flreg[i]);
  229. dump_region(i, frba);
  230. }
  231. }
  232. static void dump_frba_layout(const frba_t *frba, const char *layout_fname)
  233. {
  234. char buf[LAYOUT_LINELEN];
  235. size_t bufsize = LAYOUT_LINELEN;
  236. unsigned int i;
  237. int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC,
  238. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  239. if (layout_fd == -1) {
  240. perror("Could not open file");
  241. exit(EXIT_FAILURE);
  242. }
  243. for (i = 0; i < max_regions; i++) {
  244. region_t region = get_region(frba, i);
  245. /* is region invalid? */
  246. if (region.size < 1)
  247. continue;
  248. dump_region_layout(buf, bufsize, i, frba);
  249. if (write(layout_fd, buf, strlen(buf)) < 0) {
  250. perror("Could not write to file");
  251. exit(EXIT_FAILURE);
  252. }
  253. }
  254. close(layout_fd);
  255. printf("Wrote layout to %s\n", layout_fname);
  256. }
  257. static void decode_spi_frequency(unsigned int freq)
  258. {
  259. switch (freq) {
  260. case SPI_FREQUENCY_20MHZ:
  261. printf("20MHz");
  262. break;
  263. case SPI_FREQUENCY_33MHZ:
  264. printf("33MHz");
  265. break;
  266. case SPI_FREQUENCY_48MHZ:
  267. printf("48MHz");
  268. break;
  269. case SPI_FREQUENCY_50MHZ_30MHZ:
  270. switch (ifd_version) {
  271. case IFD_VERSION_1:
  272. printf("50MHz");
  273. break;
  274. case IFD_VERSION_2:
  275. printf("30MHz");
  276. break;
  277. }
  278. break;
  279. case SPI_FREQUENCY_17MHZ:
  280. printf("17MHz");
  281. break;
  282. default:
  283. printf("unknown<%x>MHz", freq);
  284. }
  285. }
  286. static void decode_component_density(unsigned int density)
  287. {
  288. switch (density) {
  289. case COMPONENT_DENSITY_512KB:
  290. printf("512KB");
  291. break;
  292. case COMPONENT_DENSITY_1MB:
  293. printf("1MB");
  294. break;
  295. case COMPONENT_DENSITY_2MB:
  296. printf("2MB");
  297. break;
  298. case COMPONENT_DENSITY_4MB:
  299. printf("4MB");
  300. break;
  301. case COMPONENT_DENSITY_8MB:
  302. printf("8MB");
  303. break;
  304. case COMPONENT_DENSITY_16MB:
  305. printf("16MB");
  306. break;
  307. case COMPONENT_DENSITY_32MB:
  308. printf("32MB");
  309. break;
  310. case COMPONENT_DENSITY_64MB:
  311. printf("64MB");
  312. break;
  313. case COMPONENT_DENSITY_UNUSED:
  314. printf("UNUSED");
  315. break;
  316. default:
  317. printf("unknown<%x>MB", density);
  318. }
  319. }
  320. static void dump_fcba(const fcba_t *fcba)
  321. {
  322. printf("\nFound Component Section\n");
  323. printf("FLCOMP 0x%08x\n", fcba->flcomp);
  324. printf(" Dual Output Fast Read Support: %ssupported\n",
  325. (fcba->flcomp & (1 << 30))?"":"not ");
  326. printf(" Read ID/Read Status Clock Frequency: ");
  327. decode_spi_frequency((fcba->flcomp >> 27) & 7);
  328. printf("\n Write/Erase Clock Frequency: ");
  329. decode_spi_frequency((fcba->flcomp >> 24) & 7);
  330. printf("\n Fast Read Clock Frequency: ");
  331. decode_spi_frequency((fcba->flcomp >> 21) & 7);
  332. printf("\n Fast Read Support: %ssupported",
  333. (fcba->flcomp & (1 << 20))?"":"not ");
  334. printf("\n Read Clock Frequency: ");
  335. decode_spi_frequency((fcba->flcomp >> 17) & 7);
  336. switch (ifd_version) {
  337. case IFD_VERSION_1:
  338. printf("\n Component 2 Density: ");
  339. decode_component_density((fcba->flcomp >> 3) & 7);
  340. printf("\n Component 1 Density: ");
  341. decode_component_density(fcba->flcomp & 7);
  342. break;
  343. case IFD_VERSION_2:
  344. printf("\n Component 2 Density: ");
  345. decode_component_density((fcba->flcomp >> 4) & 0xf);
  346. printf("\n Component 1 Density: ");
  347. decode_component_density(fcba->flcomp & 0xf);
  348. break;
  349. }
  350. printf("\n");
  351. printf("FLILL 0x%08x\n", fcba->flill);
  352. printf(" Invalid Instruction 3: 0x%02x\n",
  353. (fcba->flill >> 24) & 0xff);
  354. printf(" Invalid Instruction 2: 0x%02x\n",
  355. (fcba->flill >> 16) & 0xff);
  356. printf(" Invalid Instruction 1: 0x%02x\n",
  357. (fcba->flill >> 8) & 0xff);
  358. printf(" Invalid Instruction 0: 0x%02x\n",
  359. fcba->flill & 0xff);
  360. printf("FLPB 0x%08x\n", fcba->flpb);
  361. printf(" Flash Partition Boundary Address: 0x%06x\n\n",
  362. (fcba->flpb & 0xfff) << 12);
  363. }
  364. static void dump_fpsba(const fpsba_t *fpsba)
  365. {
  366. unsigned int i;
  367. printf("Found PCH Strap Section\n");
  368. for (i = 0; i < ARRAY_SIZE(fpsba->pchstrp); i++)
  369. printf("PCHSTRP%u:%s 0x%08x\n", i,
  370. i < 10 ? " " : "", fpsba->pchstrp[i]);
  371. printf("\n");
  372. }
  373. static void decode_flmstr(uint32_t flmstr)
  374. {
  375. int wr_shift, rd_shift;
  376. if (ifd_version >= IFD_VERSION_2) {
  377. wr_shift = FLMSTR_WR_SHIFT_V2;
  378. rd_shift = FLMSTR_RD_SHIFT_V2;
  379. } else {
  380. wr_shift = FLMSTR_WR_SHIFT_V1;
  381. rd_shift = FLMSTR_RD_SHIFT_V1;
  382. }
  383. /* EC region access only available on v2+ */
  384. if (ifd_version >= IFD_VERSION_2)
  385. printf(" EC Region Write Access: %s\n",
  386. (flmstr & (1 << (wr_shift + 8))) ?
  387. "enabled" : "disabled");
  388. printf(" Platform Data Region Write Access: %s\n",
  389. (flmstr & (1 << (wr_shift + 4))) ? "enabled" : "disabled");
  390. printf(" GbE Region Write Access: %s\n",
  391. (flmstr & (1 << (wr_shift + 3))) ? "enabled" : "disabled");
  392. printf(" Intel ME Region Write Access: %s\n",
  393. (flmstr & (1 << (wr_shift + 2))) ? "enabled" : "disabled");
  394. printf(" Host CPU/BIOS Region Write Access: %s\n",
  395. (flmstr & (1 << (wr_shift + 1))) ? "enabled" : "disabled");
  396. printf(" Flash Descriptor Write Access: %s\n",
  397. (flmstr & (1 << wr_shift)) ? "enabled" : "disabled");
  398. if (ifd_version >= IFD_VERSION_2)
  399. printf(" EC Region Read Access: %s\n",
  400. (flmstr & (1 << (rd_shift + 8))) ?
  401. "enabled" : "disabled");
  402. printf(" Platform Data Region Read Access: %s\n",
  403. (flmstr & (1 << (rd_shift + 4))) ? "enabled" : "disabled");
  404. printf(" GbE Region Read Access: %s\n",
  405. (flmstr & (1 << (rd_shift + 3))) ? "enabled" : "disabled");
  406. printf(" Intel ME Region Read Access: %s\n",
  407. (flmstr & (1 << (rd_shift + 2))) ? "enabled" : "disabled");
  408. printf(" Host CPU/BIOS Region Read Access: %s\n",
  409. (flmstr & (1 << (rd_shift + 1))) ? "enabled" : "disabled");
  410. printf(" Flash Descriptor Read Access: %s\n",
  411. (flmstr & (1 << rd_shift)) ? "enabled" : "disabled");
  412. /* Requestor ID doesn't exist for ifd 2 */
  413. if (ifd_version < IFD_VERSION_2)
  414. printf(" Requester ID: 0x%04x\n\n",
  415. flmstr & 0xffff);
  416. }
  417. static void dump_fmba(const fmba_t *fmba)
  418. {
  419. printf("Found Master Section\n");
  420. printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
  421. decode_flmstr(fmba->flmstr1);
  422. printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
  423. decode_flmstr(fmba->flmstr2);
  424. printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
  425. decode_flmstr(fmba->flmstr3);
  426. if (ifd_version >= IFD_VERSION_2) {
  427. printf("FLMSTR5: 0x%08x (EC)\n", fmba->flmstr5);
  428. decode_flmstr(fmba->flmstr5);
  429. }
  430. }
  431. static void dump_fmsba(const fmsba_t *fmsba)
  432. {
  433. unsigned int i;
  434. printf("Found Processor Strap Section\n");
  435. for (i = 0; i < ARRAY_SIZE(fmsba->data); i++)
  436. printf("????: 0x%08x\n", fmsba->data[i]);
  437. }
  438. static void dump_jid(uint32_t jid)
  439. {
  440. printf(" SPI Componend Device ID 1: 0x%02x\n",
  441. (jid >> 16) & 0xff);
  442. printf(" SPI Componend Device ID 0: 0x%02x\n",
  443. (jid >> 8) & 0xff);
  444. printf(" SPI Componend Vendor ID: 0x%02x\n",
  445. jid & 0xff);
  446. }
  447. static void dump_vscc(uint32_t vscc)
  448. {
  449. printf(" Lower Erase Opcode: 0x%02x\n",
  450. vscc >> 24);
  451. printf(" Lower Write Enable on Write Status: 0x%02x\n",
  452. vscc & (1 << 20) ? 0x06 : 0x50);
  453. printf(" Lower Write Status Required: %s\n",
  454. vscc & (1 << 19) ? "Yes" : "No");
  455. printf(" Lower Write Granularity: %d bytes\n",
  456. vscc & (1 << 18) ? 64 : 1);
  457. printf(" Lower Block / Sector Erase Size: ");
  458. switch ((vscc >> 16) & 0x3) {
  459. case 0:
  460. printf("256 Byte\n");
  461. break;
  462. case 1:
  463. printf("4KB\n");
  464. break;
  465. case 2:
  466. printf("8KB\n");
  467. break;
  468. case 3:
  469. printf("64KB\n");
  470. break;
  471. }
  472. printf(" Upper Erase Opcode: 0x%02x\n",
  473. (vscc >> 8) & 0xff);
  474. printf(" Upper Write Enable on Write Status: 0x%02x\n",
  475. vscc & (1 << 4) ? 0x06 : 0x50);
  476. printf(" Upper Write Status Required: %s\n",
  477. vscc & (1 << 3) ? "Yes" : "No");
  478. printf(" Upper Write Granularity: %d bytes\n",
  479. vscc & (1 << 2) ? 64 : 1);
  480. printf(" Upper Block / Sector Erase Size: ");
  481. switch (vscc & 0x3) {
  482. case 0:
  483. printf("256 Byte\n");
  484. break;
  485. case 1:
  486. printf("4KB\n");
  487. break;
  488. case 2:
  489. printf("8KB\n");
  490. break;
  491. case 3:
  492. printf("64KB\n");
  493. break;
  494. }
  495. }
  496. static void dump_vtba(const vtba_t *vtba, int vtl)
  497. {
  498. int i;
  499. int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
  500. printf("ME VSCC table:\n");
  501. for (i = 0; i < num; i++) {
  502. printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
  503. dump_jid(vtba->entry[i].jid);
  504. printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
  505. dump_vscc(vtba->entry[i].vscc);
  506. }
  507. printf("\n");
  508. }
  509. static void dump_oem(const uint8_t *oem)
  510. {
  511. int i, j;
  512. printf("OEM Section:\n");
  513. for (i = 0; i < 4; i++) {
  514. printf("%02x:", i << 4);
  515. for (j = 0; j < 16; j++)
  516. printf(" %02x", oem[(i<<4)+j]);
  517. printf ("\n");
  518. }
  519. printf ("\n");
  520. }
  521. static void dump_fd(char *image, int size)
  522. {
  523. const fdbar_t *fdb = find_fd(image, size);
  524. if (!fdb)
  525. exit(EXIT_FAILURE);
  526. printf("FLMAP0: 0x%08x\n", fdb->flmap0);
  527. printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
  528. printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
  529. printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
  530. printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
  531. printf("FLMAP1: 0x%08x\n", fdb->flmap1);
  532. printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
  533. printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
  534. printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
  535. printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
  536. printf("FLMAP2: 0x%08x\n", fdb->flmap2);
  537. printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
  538. printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
  539. printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
  540. printf(" Intel ME VSCC Table Length (VTL): %d\n",
  541. (fdb->flumap1 >> 8) & 0xff);
  542. printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
  543. (fdb->flumap1 & 0xff) << 4);
  544. dump_vtba((vtba_t *)
  545. (image + ((fdb->flumap1 & 0xff) << 4)),
  546. (fdb->flumap1 >> 8) & 0xff);
  547. dump_oem((const uint8_t *)image + 0xf00);
  548. const frba_t *frba = find_frba(image, size);
  549. const fcba_t *fcba = find_fcba(image, size);
  550. const fpsba_t *fpsba = find_fpsba(image, size);
  551. const fmba_t *fmba = find_fmba(image, size);
  552. const fmsba_t *fmsba = find_fmsba(image, size);
  553. if (frba && fcba && fpsba && fmba && fmsba) {
  554. dump_frba(frba);
  555. dump_fcba(fcba);
  556. dump_fpsba(fpsba);
  557. dump_fmba(fmba);
  558. dump_fmsba(fmsba);
  559. } else {
  560. printf("FD is corrupted!\n");
  561. }
  562. }
  563. static void dump_layout(char *image, int size, const char *layout_fname)
  564. {
  565. const frba_t *frba = find_frba(image, size);
  566. if (!frba)
  567. exit(EXIT_FAILURE);
  568. dump_frba_layout(frba, layout_fname);
  569. }
  570. static void write_regions(char *image, int size)
  571. {
  572. unsigned int i;
  573. const frba_t *frba = find_frba(image, size);
  574. if (!frba)
  575. exit(EXIT_FAILURE);
  576. for (i = 0; i < max_regions; i++) {
  577. region_t region = get_region(frba, i);
  578. dump_region(i, frba);
  579. if (region.size > 0) {
  580. int region_fd;
  581. region_fd = open(region_filename(i),
  582. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
  583. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  584. if (region_fd < 0) {
  585. perror("Error while trying to open file");
  586. exit(EXIT_FAILURE);
  587. }
  588. if (write(region_fd, image + region.base, region.size) != region.size)
  589. perror("Error while writing");
  590. close(region_fd);
  591. }
  592. }
  593. }
  594. static void write_image(const char *filename, char *image, int size)
  595. {
  596. char new_filename[FILENAME_MAX]; // allow long file names
  597. int new_fd;
  598. // - 5: leave room for ".new\0"
  599. strncpy(new_filename, filename, FILENAME_MAX - 5);
  600. strncat(new_filename, ".new", FILENAME_MAX - strlen(filename));
  601. printf("Writing new image to %s\n", new_filename);
  602. // Now write out new image
  603. new_fd = open(new_filename,
  604. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
  605. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  606. if (new_fd < 0) {
  607. perror("Error while trying to open file");
  608. exit(EXIT_FAILURE);
  609. }
  610. if (write(new_fd, image, size) != size)
  611. perror("Error while writing");
  612. close(new_fd);
  613. }
  614. static void set_spi_frequency(const char *filename, char *image, int size,
  615. enum spi_frequency freq)
  616. {
  617. fcba_t *fcba = find_fcba(image, size);
  618. if (!fcba)
  619. exit(EXIT_FAILURE);
  620. /* clear bits 21-29 */
  621. fcba->flcomp &= ~0x3fe00000;
  622. /* Read ID and Read Status Clock Frequency */
  623. fcba->flcomp |= freq << 27;
  624. /* Write and Erase Clock Frequency */
  625. fcba->flcomp |= freq << 24;
  626. /* Fast Read Clock Frequency */
  627. fcba->flcomp |= freq << 21;
  628. write_image(filename, image, size);
  629. }
  630. static void set_em100_mode(const char *filename, char *image, int size)
  631. {
  632. fcba_t *fcba = find_fcba(image, size);
  633. if (!fcba)
  634. exit(EXIT_FAILURE);
  635. int freq;
  636. switch (ifd_version) {
  637. case IFD_VERSION_1:
  638. freq = SPI_FREQUENCY_20MHZ;
  639. break;
  640. case IFD_VERSION_2:
  641. freq = SPI_FREQUENCY_17MHZ;
  642. break;
  643. default:
  644. freq = SPI_FREQUENCY_17MHZ;
  645. break;
  646. }
  647. fcba->flcomp &= ~(1 << 30);
  648. set_spi_frequency(filename, image, size, freq);
  649. }
  650. static void set_chipdensity(const char *filename, char *image, int size,
  651. unsigned int density)
  652. {
  653. fcba_t *fcba = find_fcba(image, size);
  654. if (!fcba)
  655. exit(EXIT_FAILURE);
  656. printf("Setting chip density to ");
  657. decode_component_density(density);
  658. printf("\n");
  659. switch (ifd_version) {
  660. case IFD_VERSION_1:
  661. /* fail if selected density is not supported by this version */
  662. if ( (density == COMPONENT_DENSITY_32MB) ||
  663. (density == COMPONENT_DENSITY_64MB) ||
  664. (density == COMPONENT_DENSITY_UNUSED) ) {
  665. printf("error: Selected density not supported in IFD version 1.\n");
  666. exit(EXIT_FAILURE);
  667. }
  668. break;
  669. case IFD_VERSION_2:
  670. /* I do not have a version 2 IFD nor do i have the docs. */
  671. printf("error: Changing the chip density for IFD version 2 has not been"
  672. " implemented yet.\n");
  673. exit(EXIT_FAILURE);
  674. default:
  675. printf("error: Unknown IFD version\n");
  676. exit(EXIT_FAILURE);
  677. break;
  678. }
  679. /* clear chip density for corresponding chip */
  680. switch (selected_chip) {
  681. case 1:
  682. fcba->flcomp &= ~(0x7);
  683. break;
  684. case 2:
  685. fcba->flcomp &= ~(0x7 << 3);
  686. break;
  687. default: /*both chips*/
  688. fcba->flcomp &= ~(0x3F);
  689. break;
  690. }
  691. /* set the new density */
  692. if (selected_chip == 1 || selected_chip == 0)
  693. fcba->flcomp |= (density); /* first chip */
  694. if (selected_chip == 2 || selected_chip == 0)
  695. fcba->flcomp |= (density << 3); /* second chip */
  696. write_image(filename, image, size);
  697. }
  698. static void lock_descriptor(const char *filename, char *image, int size)
  699. {
  700. int wr_shift, rd_shift;
  701. fmba_t *fmba = find_fmba(image, size);
  702. if (!fmba)
  703. exit(EXIT_FAILURE);
  704. /* TODO: Dynamically take Platform Data Region and GbE Region
  705. * into regard.
  706. */
  707. if (ifd_version >= IFD_VERSION_2) {
  708. wr_shift = FLMSTR_WR_SHIFT_V2;
  709. rd_shift = FLMSTR_RD_SHIFT_V2;
  710. /* Clear non-reserved bits */
  711. fmba->flmstr1 &= 0xff;
  712. fmba->flmstr2 &= 0xff;
  713. fmba->flmstr3 &= 0xff;
  714. } else {
  715. wr_shift = FLMSTR_WR_SHIFT_V1;
  716. rd_shift = FLMSTR_RD_SHIFT_V1;
  717. fmba->flmstr1 = 0;
  718. fmba->flmstr2 = 0;
  719. /* Requestor ID */
  720. fmba->flmstr3 = 0x118;
  721. }
  722. switch (platform) {
  723. case PLATFORM_APOLLOLAKE:
  724. /* CPU/BIOS can read descriptor and BIOS */
  725. fmba->flmstr1 |= 0x3 << rd_shift;
  726. /* CPU/BIOS can write BIOS */
  727. fmba->flmstr1 |= 0x2 << wr_shift;
  728. /* TXE can read descriptor, BIOS and Device Expansion */
  729. fmba->flmstr2 |= 0x23 << rd_shift;
  730. /* TXE can only write Device Expansion */
  731. fmba->flmstr2 |= 0x20 << wr_shift;
  732. break;
  733. default:
  734. /* CPU/BIOS can read descriptor, BIOS, and GbE. */
  735. fmba->flmstr1 |= 0xb << rd_shift;
  736. /* CPU/BIOS can write BIOS and GbE. */
  737. fmba->flmstr1 |= 0xa << wr_shift;
  738. /* ME can read descriptor, ME, and GbE. */
  739. fmba->flmstr2 |= 0xd << rd_shift;
  740. /* ME can write ME and GbE. */
  741. fmba->flmstr2 |= 0xc << wr_shift;
  742. /* GbE can write only GbE. */
  743. fmba->flmstr3 |= 0x8 << rd_shift;
  744. /* GbE can read only GbE. */
  745. fmba->flmstr3 |= 0x8 << wr_shift;
  746. break;
  747. }
  748. write_image(filename, image, size);
  749. }
  750. static void unlock_descriptor(const char *filename, char *image, int size)
  751. {
  752. fmba_t *fmba = find_fmba(image, size);
  753. if (!fmba)
  754. exit(EXIT_FAILURE);
  755. if (ifd_version >= IFD_VERSION_2) {
  756. /* Access bits for each region are read: 19:8 write: 31:20 */
  757. fmba->flmstr1 = 0xffffff00 | (fmba->flmstr1 & 0xff);
  758. fmba->flmstr2 = 0xffffff00 | (fmba->flmstr2 & 0xff);
  759. fmba->flmstr3 = 0xffffff00 | (fmba->flmstr3 & 0xff);
  760. } else {
  761. fmba->flmstr1 = 0xffff0000;
  762. fmba->flmstr2 = 0xffff0000;
  763. /* Keep chipset specific Requester ID */
  764. fmba->flmstr3 = 0x08080000 | (fmba->flmstr3 & 0xffff);
  765. }
  766. write_image(filename, image, size);
  767. }
  768. void inject_region(const char *filename, char *image, int size,
  769. unsigned int region_type, const char *region_fname)
  770. {
  771. frba_t *frba = find_frba(image, size);
  772. if (!frba)
  773. exit(EXIT_FAILURE);
  774. region_t region = get_region(frba, region_type);
  775. if (region.size <= 0xfff) {
  776. fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
  777. region_name(region_type));
  778. exit(EXIT_FAILURE);
  779. }
  780. int region_fd = open(region_fname, O_RDONLY | O_BINARY);
  781. if (region_fd == -1) {
  782. perror("Could not open file");
  783. exit(EXIT_FAILURE);
  784. }
  785. struct stat buf;
  786. if (fstat(region_fd, &buf) == -1) {
  787. perror("Could not stat file");
  788. exit(EXIT_FAILURE);
  789. }
  790. int region_size = buf.st_size;
  791. printf("File %s is %d bytes\n", region_fname, region_size);
  792. if ( (region_size > region.size) || ((region_type != 1) &&
  793. (region_size > region.size))) {
  794. fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
  795. " bytes. Not injecting.\n",
  796. region_name(region_type), region.size,
  797. region.size, region_size, region_size);
  798. exit(EXIT_FAILURE);
  799. }
  800. int offset = 0;
  801. if ((region_type == 1) && (region_size < region.size)) {
  802. fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
  803. " bytes. Padding before injecting.\n",
  804. region_name(region_type), region.size,
  805. region.size, region_size, region_size);
  806. offset = region.size - region_size;
  807. memset(image + region.base, 0xff, offset);
  808. }
  809. if (size < region.base + offset + region_size) {
  810. fprintf(stderr, "Output file is too small. (%d < %d)\n",
  811. size, region.base + offset + region_size);
  812. exit(EXIT_FAILURE);
  813. }
  814. if (read(region_fd, image + region.base + offset, region_size)
  815. != region_size) {
  816. perror("Could not read file");
  817. exit(EXIT_FAILURE);
  818. }
  819. close(region_fd);
  820. printf("Adding %s as the %s section of %s\n",
  821. region_fname, region_name(region_type), filename);
  822. write_image(filename, image, size);
  823. }
  824. unsigned int next_pow2(unsigned int x)
  825. {
  826. unsigned int y = 1;
  827. if (x == 0)
  828. return 0;
  829. while (y <= x)
  830. y = y << 1;
  831. return y;
  832. }
  833. /**
  834. * Determine if two memory regions overlap.
  835. *
  836. * @param r1, r2 Memory regions to compare.
  837. * @return 0 if the two regions are seperate
  838. * @return 1 if the two regions overlap
  839. */
  840. static int regions_collide(const region_t *r1, const region_t *r2)
  841. {
  842. if ((r1->size == 0) || (r2->size == 0))
  843. return 0;
  844. if ( ((r1->base >= r2->base) && (r1->base <= r2->limit)) ||
  845. ((r1->limit >= r2->base) && (r1->limit <= r2->limit)) )
  846. return 1;
  847. return 0;
  848. }
  849. void new_layout(const char *filename, char *image, int size,
  850. const char *layout_fname)
  851. {
  852. FILE *romlayout;
  853. char tempstr[256];
  854. char layout_region_name[256];
  855. unsigned int i, j;
  856. int region_number;
  857. region_t current_regions[MAX_REGIONS];
  858. region_t new_regions[MAX_REGIONS];
  859. int new_extent = 0;
  860. char *new_image;
  861. /* load current descriptor map and regions */
  862. frba_t *frba = find_frba(image, size);
  863. if (!frba)
  864. exit(EXIT_FAILURE);
  865. for (i = 0; i < max_regions; i++) {
  866. current_regions[i] = get_region(frba, i);
  867. new_regions[i] = get_region(frba, i);
  868. }
  869. /* read new layout */
  870. romlayout = fopen(layout_fname, "r");
  871. if (!romlayout) {
  872. perror("Could not read layout file.\n");
  873. exit(EXIT_FAILURE);
  874. }
  875. while (!feof(romlayout)) {
  876. char *tstr1, *tstr2;
  877. if (2 != fscanf(romlayout, "%255s %255s\n", tempstr,
  878. layout_region_name))
  879. continue;
  880. region_number = region_num(layout_region_name);
  881. if (region_number < 0)
  882. continue;
  883. tstr1 = strtok(tempstr, ":");
  884. tstr2 = strtok(NULL, ":");
  885. if (!tstr1 || !tstr2) {
  886. fprintf(stderr, "Could not parse layout file.\n");
  887. exit(EXIT_FAILURE);
  888. }
  889. new_regions[region_number].base = strtol(tstr1,
  890. (char **)NULL, 16);
  891. new_regions[region_number].limit = strtol(tstr2,
  892. (char **)NULL, 16);
  893. new_regions[region_number].size =
  894. new_regions[region_number].limit -
  895. new_regions[region_number].base + 1;
  896. if (new_regions[region_number].size < 0)
  897. new_regions[region_number].size = 0;
  898. }
  899. fclose(romlayout);
  900. /* check new layout */
  901. for (i = 0; i < max_regions; i++) {
  902. if (new_regions[i].size == 0)
  903. continue;
  904. if (new_regions[i].size < current_regions[i].size) {
  905. printf("DANGER: Region %s is shrinking.\n",
  906. region_name(i));
  907. printf(" The region will be truncated to fit.\n");
  908. printf(" This may result in an unusable image.\n");
  909. }
  910. for (j = i + 1; j < max_regions; j++) {
  911. if (regions_collide(&new_regions[i], &new_regions[j])) {
  912. fprintf(stderr, "Regions would overlap.\n");
  913. exit(EXIT_FAILURE);
  914. }
  915. }
  916. /* detect if the image size should grow */
  917. if (new_extent < new_regions[i].limit)
  918. new_extent = new_regions[i].limit;
  919. }
  920. new_extent = next_pow2(new_extent - 1);
  921. if (new_extent != size) {
  922. printf("The image has changed in size.\n");
  923. printf("The old image is %d bytes.\n", size);
  924. printf("The new image is %d bytes.\n", new_extent);
  925. }
  926. /* copy regions to a new image */
  927. new_image = malloc(new_extent);
  928. memset(new_image, 0xff, new_extent);
  929. for (i = 0; i < max_regions; i++) {
  930. int copy_size = new_regions[i].size;
  931. int offset_current = 0, offset_new = 0;
  932. const region_t *current = &current_regions[i];
  933. const region_t *new = &new_regions[i];
  934. if (new->size == 0)
  935. continue;
  936. if (new->size > current->size) {
  937. /* copy from the end of the current region */
  938. copy_size = current->size;
  939. offset_new = new->size - current->size;
  940. }
  941. if (new->size < current->size) {
  942. /* copy to the end of the new region */
  943. offset_current = current->size - new->size;
  944. }
  945. printf("Copy Descriptor %d (%s) (%d bytes)\n", i,
  946. region_name(i), copy_size);
  947. printf(" from %08x+%08x:%08x (%10d)\n", current->base,
  948. offset_current, current->limit, current->size);
  949. printf(" to %08x+%08x:%08x (%10d)\n", new->base,
  950. offset_new, new->limit, new->size);
  951. memcpy(new_image + new->base + offset_new,
  952. image + current->base + offset_current,
  953. copy_size);
  954. }
  955. /* update new descriptor regions */
  956. frba = find_frba(new_image, new_extent);
  957. if (!frba)
  958. exit(EXIT_FAILURE);
  959. for (i = 1; i < max_regions; i++)
  960. set_region(frba, i, &new_regions[i]);
  961. write_image(filename, new_image, new_extent);
  962. free(new_image);
  963. }
  964. static void print_version(void)
  965. {
  966. printf("ifdtool v%s -- ", IFDTOOL_VERSION);
  967. printf("Copyright (C) 2011 Google Inc.\n\n");
  968. printf
  969. ("This program is free software: you can redistribute it and/or modify\n"
  970. "it under the terms of the GNU General Public License as published by\n"
  971. "the Free Software Foundation, version 2 of the License.\n\n"
  972. "This program is distributed in the hope that it will be useful,\n"
  973. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  974. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  975. "GNU General Public License for more details.\n\n");
  976. }
  977. static void print_usage(const char *name)
  978. {
  979. printf("usage: %s [-vhdix?] <filename>\n", name);
  980. printf("\n"
  981. " -d | --dump: dump intel firmware descriptor\n"
  982. " -f | --layout <filename> dump regions into a flashrom layout file\n"
  983. " -x | --extract: extract intel fd modules\n"
  984. " -i | --inject <region>:<module> inject file <module> into region <region>\n"
  985. " -n | --newlayout <filename> update regions using a flashrom layout file\n"
  986. " -s | --spifreq <17|20|30|33|48|50> set the SPI frequency\n"
  987. " -D | --density <512|1|2|4|8|16> set chip density (512 in KByte, others in MByte)\n"
  988. " -C | --chip <0|1|2> select spi chip on which to operate\n"
  989. " can only be used once per run:\n"
  990. " 0 - both chips (default), 1 - first chip, 2 - second chip\n"
  991. " -e | --em100 set SPI frequency to 20MHz and disable\n"
  992. " Dual Output Fast Read Support\n"
  993. " -l | --lock Lock firmware descriptor and ME region\n"
  994. " -u | --unlock Unlock firmware descriptor and ME region\n"
  995. " -p | --platform Add platform-specific quirks\n"
  996. " aplk - Apollo Lake\n"
  997. " -v | --version: print the version\n"
  998. " -h | --help: print this help\n\n"
  999. "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
  1000. "\n");
  1001. }
  1002. int main(int argc, char *argv[])
  1003. {
  1004. int opt, option_index = 0;
  1005. int mode_dump = 0, mode_extract = 0, mode_inject = 0, mode_spifreq = 0;
  1006. int mode_em100 = 0, mode_locked = 0, mode_unlocked = 0;
  1007. int mode_layout = 0, mode_newlayout = 0, mode_density = 0;
  1008. char *region_type_string = NULL, *region_fname = NULL;
  1009. const char *layout_fname = NULL;
  1010. int region_type = -1, inputfreq = 0;
  1011. unsigned int new_density = 0;
  1012. enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
  1013. static const struct option long_options[] = {
  1014. {"dump", 0, NULL, 'd'},
  1015. {"layout", 1, NULL, 'f'},
  1016. {"extract", 0, NULL, 'x'},
  1017. {"inject", 1, NULL, 'i'},
  1018. {"newlayout", 1, NULL, 'n'},
  1019. {"spifreq", 1, NULL, 's'},
  1020. {"density", 1, NULL, 'D'},
  1021. {"chip", 1, NULL, 'C'},
  1022. {"em100", 0, NULL, 'e'},
  1023. {"lock", 0, NULL, 'l'},
  1024. {"unlock", 0, NULL, 'u'},
  1025. {"version", 0, NULL, 'v'},
  1026. {"help", 0, NULL, 'h'},
  1027. {"platform", 0, NULL, 'p'},
  1028. {0, 0, 0, 0}
  1029. };
  1030. while ((opt = getopt_long(argc, argv, "df:D:C:xi:n:s:p:eluvh?",
  1031. long_options, &option_index)) != EOF) {
  1032. switch (opt) {
  1033. case 'd':
  1034. mode_dump = 1;
  1035. break;
  1036. case 'f':
  1037. mode_layout = 1;
  1038. layout_fname = strdup(optarg);
  1039. if (!layout_fname) {
  1040. fprintf(stderr, "No layout file specified\n");
  1041. print_usage(argv[0]);
  1042. exit(EXIT_FAILURE);
  1043. }
  1044. break;
  1045. case 'x':
  1046. mode_extract = 1;
  1047. break;
  1048. case 'i':
  1049. // separate type and file name
  1050. region_type_string = strdup(optarg);
  1051. region_fname = strchr(region_type_string, ':');
  1052. if (!region_fname) {
  1053. print_usage(argv[0]);
  1054. exit(EXIT_FAILURE);
  1055. }
  1056. region_fname[0] = '\0';
  1057. region_fname++;
  1058. // Descriptor, BIOS, ME, GbE, Platform
  1059. // valid type?
  1060. if (!strcasecmp("Descriptor", region_type_string))
  1061. region_type = 0;
  1062. else if (!strcasecmp("BIOS", region_type_string))
  1063. region_type = 1;
  1064. else if (!strcasecmp("ME", region_type_string))
  1065. region_type = 2;
  1066. else if (!strcasecmp("GbE", region_type_string))
  1067. region_type = 3;
  1068. else if (!strcasecmp("Platform", region_type_string))
  1069. region_type = 4;
  1070. else if (!strcasecmp("EC", region_type_string))
  1071. region_type = 8;
  1072. if (region_type == -1) {
  1073. fprintf(stderr, "No such region type: '%s'\n\n",
  1074. region_type_string);
  1075. print_usage(argv[0]);
  1076. exit(EXIT_FAILURE);
  1077. }
  1078. mode_inject = 1;
  1079. break;
  1080. case 'n':
  1081. mode_newlayout = 1;
  1082. layout_fname = strdup(optarg);
  1083. if (!layout_fname) {
  1084. fprintf(stderr, "No layout file specified\n");
  1085. print_usage(argv[0]);
  1086. exit(EXIT_FAILURE);
  1087. }
  1088. break;
  1089. case 'D':
  1090. mode_density = 1;
  1091. new_density = strtoul(optarg, NULL, 0);
  1092. switch (new_density) {
  1093. case 512:
  1094. new_density = COMPONENT_DENSITY_512KB;
  1095. break;
  1096. case 1:
  1097. new_density = COMPONENT_DENSITY_1MB;
  1098. break;
  1099. case 2:
  1100. new_density = COMPONENT_DENSITY_2MB;
  1101. break;
  1102. case 4:
  1103. new_density = COMPONENT_DENSITY_4MB;
  1104. break;
  1105. case 8:
  1106. new_density = COMPONENT_DENSITY_8MB;
  1107. break;
  1108. case 16:
  1109. new_density = COMPONENT_DENSITY_16MB;
  1110. break;
  1111. case 32:
  1112. new_density = COMPONENT_DENSITY_32MB;
  1113. break;
  1114. case 64:
  1115. new_density = COMPONENT_DENSITY_64MB;
  1116. break;
  1117. case 0:
  1118. new_density = COMPONENT_DENSITY_UNUSED;
  1119. break;
  1120. default:
  1121. printf("error: Unknown density\n");
  1122. print_usage(argv[0]);
  1123. exit(EXIT_FAILURE);
  1124. }
  1125. break;
  1126. case 'C':
  1127. selected_chip = strtol(optarg, NULL, 0);
  1128. if (selected_chip > 2) {
  1129. fprintf(stderr, "error: Invalid chip selection\n");
  1130. print_usage(argv[0]);
  1131. exit(EXIT_FAILURE);
  1132. }
  1133. break;
  1134. case 's':
  1135. // Parse the requested SPI frequency
  1136. inputfreq = strtol(optarg, NULL, 0);
  1137. switch (inputfreq) {
  1138. case 17:
  1139. spifreq = SPI_FREQUENCY_17MHZ;
  1140. break;
  1141. case 20:
  1142. spifreq = SPI_FREQUENCY_20MHZ;
  1143. break;
  1144. case 30:
  1145. spifreq = SPI_FREQUENCY_50MHZ_30MHZ;
  1146. break;
  1147. case 33:
  1148. spifreq = SPI_FREQUENCY_33MHZ;
  1149. break;
  1150. case 48:
  1151. spifreq = SPI_FREQUENCY_48MHZ;
  1152. break;
  1153. case 50:
  1154. spifreq = SPI_FREQUENCY_50MHZ_30MHZ;
  1155. break;
  1156. default:
  1157. fprintf(stderr, "Invalid SPI Frequency: %d\n",
  1158. inputfreq);
  1159. print_usage(argv[0]);
  1160. exit(EXIT_FAILURE);
  1161. }
  1162. mode_spifreq = 1;
  1163. break;
  1164. case 'e':
  1165. mode_em100 = 1;
  1166. break;
  1167. case 'l':
  1168. mode_locked = 1;
  1169. if (mode_unlocked == 1) {
  1170. fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
  1171. exit(EXIT_FAILURE);
  1172. }
  1173. break;
  1174. case 'u':
  1175. mode_unlocked = 1;
  1176. if (mode_locked == 1) {
  1177. fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
  1178. exit(EXIT_FAILURE);
  1179. }
  1180. break;
  1181. case 'p':
  1182. if (!strcmp(optarg, "aplk")) {
  1183. platform = PLATFORM_APOLLOLAKE;
  1184. } else {
  1185. fprintf(stderr, "Unknown platform: %s\n", optarg);
  1186. exit(EXIT_FAILURE);
  1187. }
  1188. break;
  1189. case 'v':
  1190. print_version();
  1191. exit(EXIT_SUCCESS);
  1192. break;
  1193. case 'h':
  1194. case '?':
  1195. default:
  1196. print_usage(argv[0]);
  1197. exit(EXIT_SUCCESS);
  1198. break;
  1199. }
  1200. }
  1201. if ((mode_dump + mode_layout + mode_extract + mode_inject +
  1202. mode_newlayout + (mode_spifreq | mode_em100 | mode_unlocked |
  1203. mode_locked)) > 1) {
  1204. fprintf(stderr, "You may not specify more than one mode.\n\n");
  1205. print_usage(argv[0]);
  1206. exit(EXIT_FAILURE);
  1207. }
  1208. if ((mode_dump + mode_layout + mode_extract + mode_inject +
  1209. mode_newlayout + mode_spifreq + mode_em100 + mode_locked +
  1210. mode_unlocked + mode_density) == 0) {
  1211. fprintf(stderr, "You need to specify a mode.\n\n");
  1212. print_usage(argv[0]);
  1213. exit(EXIT_FAILURE);
  1214. }
  1215. if (optind + 1 != argc) {
  1216. fprintf(stderr, "You need to specify a file.\n\n");
  1217. print_usage(argv[0]);
  1218. exit(EXIT_FAILURE);
  1219. }
  1220. char *filename = argv[optind];
  1221. int bios_fd = open(filename, O_RDONLY | O_BINARY);
  1222. if (bios_fd == -1) {
  1223. perror("Could not open file");
  1224. exit(EXIT_FAILURE);
  1225. }
  1226. struct stat buf;
  1227. if (fstat(bios_fd, &buf) == -1) {
  1228. perror("Could not stat file");
  1229. exit(EXIT_FAILURE);
  1230. }
  1231. int size = buf.st_size;
  1232. printf("File %s is %d bytes\n", filename, size);
  1233. char *image = malloc(size);
  1234. if (!image) {
  1235. printf("Out of memory.\n");
  1236. exit(EXIT_FAILURE);
  1237. }
  1238. if (read(bios_fd, image, size) != size) {
  1239. perror("Could not read file");
  1240. exit(EXIT_FAILURE);
  1241. }
  1242. close(bios_fd);
  1243. check_ifd_version(image, size);
  1244. if (mode_dump)
  1245. dump_fd(image, size);
  1246. if (mode_layout)
  1247. dump_layout(image, size, layout_fname);
  1248. if (mode_extract)
  1249. write_regions(image, size);
  1250. if (mode_inject)
  1251. inject_region(filename, image, size, region_type,
  1252. region_fname);
  1253. if (mode_newlayout)
  1254. new_layout(filename, image, size, layout_fname);
  1255. if (mode_spifreq)
  1256. set_spi_frequency(filename, image, size, spifreq);
  1257. if (mode_density)
  1258. set_chipdensity(filename, image, size, new_density);
  1259. if (mode_em100)
  1260. set_em100_mode(filename, image, size);
  1261. if (mode_locked)
  1262. lock_descriptor(filename, image, size);
  1263. if (mode_unlocked)
  1264. unlock_descriptor(filename, image, size);
  1265. free(image);
  1266. return 0;
  1267. }