2 * ifdtool - Manage Intel Firmware Descriptor information
4 * Copyright 2014 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0
8 * From Coreboot project, but it got a serious code clean-up
9 * and a few new features
19 #include <sys/types.h>
27 #define debug(fmt, args...) printf(fmt, ##args)
29 #define debug(fmt, args...)
32 #define FD_SIGNATURE 0x0FF0A55A
33 #define FLREG_BASE(reg) ((reg & 0x00000fff) << 12);
34 #define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff);
36 enum input_file_type_t {
45 enum input_file_type_t type;
49 * find_fd() - Find the flash description in the ROM image
51 * @image: Pointer to image
52 * @size: Size of image in bytes
53 * @return pointer to structure, or NULL if not found
55 static struct fdbar_t *find_fd(char *image, int size)
59 /* Scan for FD signature */
60 for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) {
61 if (*ptr == FD_SIGNATURE)
66 printf("No Flash Descriptor found in this image\n");
70 debug("Found Flash Descriptor signature at 0x%08lx\n",
73 return (struct fdbar_t *)ptr;
77 * get_region() - Get information about the selected region
79 * @frba: Flash region list
80 * @region_type: Type of region (0..MAX_REGIONS-1)
81 * @region: Region information is written here
82 * @return 0 if OK, else -ve
84 static int get_region(struct frba_t *frba, int region_type,
85 struct region_t *region)
87 if (region_type >= MAX_REGIONS) {
88 fprintf(stderr, "Invalid region type.\n");
92 region->base = FLREG_BASE(frba->flreg[region_type]);
93 region->limit = FLREG_LIMIT(frba->flreg[region_type]);
94 region->size = region->limit - region->base + 1;
99 static const char *region_name(int region_type)
101 static const char *const regions[] = {
109 assert(region_type < MAX_REGIONS);
111 return regions[region_type];
114 static const char *region_filename(int region_type)
116 static const char *const region_filenames[] = {
117 "flashregion_0_flashdescriptor.bin",
118 "flashregion_1_bios.bin",
119 "flashregion_2_intel_me.bin",
120 "flashregion_3_gbe.bin",
121 "flashregion_4_platform_data.bin"
124 assert(region_type < MAX_REGIONS);
126 return region_filenames[region_type];
129 static int dump_region(int num, struct frba_t *frba)
131 struct region_t region;
134 ret = get_region(frba, num, ®ion);
138 printf(" Flash Region %d (%s): %08x - %08x %s\n",
139 num, region_name(num), region.base, region.limit,
140 region.size < 1 ? "(unused)" : "");
145 static void dump_frba(struct frba_t *frba)
149 printf("Found Region Section\n");
150 for (i = 0; i < MAX_REGIONS; i++) {
151 printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
152 dump_region(i, frba);
156 static void decode_spi_frequency(unsigned int freq)
159 case SPI_FREQUENCY_20MHZ:
162 case SPI_FREQUENCY_33MHZ:
165 case SPI_FREQUENCY_50MHZ:
169 printf("unknown<%x>MHz", freq);
173 static void decode_component_density(unsigned int density)
176 case COMPONENT_DENSITY_512KB:
179 case COMPONENT_DENSITY_1MB:
182 case COMPONENT_DENSITY_2MB:
185 case COMPONENT_DENSITY_4MB:
188 case COMPONENT_DENSITY_8MB:
191 case COMPONENT_DENSITY_16MB:
195 printf("unknown<%x>MiB", density);
199 static void dump_fcba(struct fcba_t *fcba)
201 printf("\nFound Component Section\n");
202 printf("FLCOMP 0x%08x\n", fcba->flcomp);
203 printf(" Dual Output Fast Read Support: %ssupported\n",
204 (fcba->flcomp & (1 << 30)) ? "" : "not ");
205 printf(" Read ID/Read Status Clock Frequency: ");
206 decode_spi_frequency((fcba->flcomp >> 27) & 7);
207 printf("\n Write/Erase Clock Frequency: ");
208 decode_spi_frequency((fcba->flcomp >> 24) & 7);
209 printf("\n Fast Read Clock Frequency: ");
210 decode_spi_frequency((fcba->flcomp >> 21) & 7);
211 printf("\n Fast Read Support: %ssupported",
212 (fcba->flcomp & (1 << 20)) ? "" : "not ");
213 printf("\n Read Clock Frequency: ");
214 decode_spi_frequency((fcba->flcomp >> 17) & 7);
215 printf("\n Component 2 Density: ");
216 decode_component_density((fcba->flcomp >> 3) & 7);
217 printf("\n Component 1 Density: ");
218 decode_component_density(fcba->flcomp & 7);
220 printf("FLILL 0x%08x\n", fcba->flill);
221 printf(" Invalid Instruction 3: 0x%02x\n",
222 (fcba->flill >> 24) & 0xff);
223 printf(" Invalid Instruction 2: 0x%02x\n",
224 (fcba->flill >> 16) & 0xff);
225 printf(" Invalid Instruction 1: 0x%02x\n",
226 (fcba->flill >> 8) & 0xff);
227 printf(" Invalid Instruction 0: 0x%02x\n",
229 printf("FLPB 0x%08x\n", fcba->flpb);
230 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
231 (fcba->flpb & 0xfff) << 12);
234 static void dump_fpsba(struct fpsba_t *fpsba)
238 printf("Found PCH Strap Section\n");
239 for (i = 0; i < MAX_STRAPS; i++)
240 printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
243 static const char *get_enabled(int flag)
245 return flag ? "enabled" : "disabled";
248 static void decode_flmstr(uint32_t flmstr)
250 printf(" Platform Data Region Write Access: %s\n",
251 get_enabled(flmstr & (1 << 28)));
252 printf(" GbE Region Write Access: %s\n",
253 get_enabled(flmstr & (1 << 27)));
254 printf(" Intel ME Region Write Access: %s\n",
255 get_enabled(flmstr & (1 << 26)));
256 printf(" Host CPU/BIOS Region Write Access: %s\n",
257 get_enabled(flmstr & (1 << 25)));
258 printf(" Flash Descriptor Write Access: %s\n",
259 get_enabled(flmstr & (1 << 24)));
261 printf(" Platform Data Region Read Access: %s\n",
262 get_enabled(flmstr & (1 << 20)));
263 printf(" GbE Region Read Access: %s\n",
264 get_enabled(flmstr & (1 << 19)));
265 printf(" Intel ME Region Read Access: %s\n",
266 get_enabled(flmstr & (1 << 18)));
267 printf(" Host CPU/BIOS Region Read Access: %s\n",
268 get_enabled(flmstr & (1 << 17)));
269 printf(" Flash Descriptor Read Access: %s\n",
270 get_enabled(flmstr & (1 << 16)));
272 printf(" Requester ID: 0x%04x\n\n",
276 static void dump_fmba(struct fmba_t *fmba)
278 printf("Found Master Section\n");
279 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
280 decode_flmstr(fmba->flmstr1);
281 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
282 decode_flmstr(fmba->flmstr2);
283 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
284 decode_flmstr(fmba->flmstr3);
287 static void dump_fmsba(struct fmsba_t *fmsba)
291 printf("Found Processor Strap Section\n");
292 for (i = 0; i < 4; i++)
293 printf("????: 0x%08x\n", fmsba->data[0]);
296 static void dump_jid(uint32_t jid)
298 printf(" SPI Component Device ID 1: 0x%02x\n",
300 printf(" SPI Component Device ID 0: 0x%02x\n",
302 printf(" SPI Component Vendor ID: 0x%02x\n",
306 static void dump_vscc(uint32_t vscc)
308 printf(" Lower Erase Opcode: 0x%02x\n",
310 printf(" Lower Write Enable on Write Status: 0x%02x\n",
311 vscc & (1 << 20) ? 0x06 : 0x50);
312 printf(" Lower Write Status Required: %s\n",
313 vscc & (1 << 19) ? "Yes" : "No");
314 printf(" Lower Write Granularity: %d bytes\n",
315 vscc & (1 << 18) ? 64 : 1);
316 printf(" Lower Block / Sector Erase Size: ");
317 switch ((vscc >> 16) & 0x3) {
319 printf("256 Byte\n");
332 printf(" Upper Erase Opcode: 0x%02x\n",
334 printf(" Upper Write Enable on Write Status: 0x%02x\n",
335 vscc & (1 << 4) ? 0x06 : 0x50);
336 printf(" Upper Write Status Required: %s\n",
337 vscc & (1 << 3) ? "Yes" : "No");
338 printf(" Upper Write Granularity: %d bytes\n",
339 vscc & (1 << 2) ? 64 : 1);
340 printf(" Upper Block / Sector Erase Size: ");
341 switch (vscc & 0x3) {
343 printf("256 Byte\n");
357 static void dump_vtba(struct vtba_t *vtba, int vtl)
360 int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
362 printf("ME VSCC table:\n");
363 for (i = 0; i < num; i++) {
364 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
365 dump_jid(vtba->entry[i].jid);
366 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
367 dump_vscc(vtba->entry[i].vscc);
372 static void dump_oem(uint8_t *oem)
375 printf("OEM Section:\n");
376 for (i = 0; i < 4; i++) {
377 printf("%02x:", i << 4);
378 for (j = 0; j < 16; j++)
379 printf(" %02x", oem[(i<<4)+j]);
386 * dump_fd() - Display a dump of the full flash description
388 * @image: Pointer to image
389 * @size: Size of image in bytes
390 * @return 0 if OK, -1 on error
392 static int dump_fd(char *image, int size)
394 struct fdbar_t *fdb = find_fd(image, size);
399 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
400 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
401 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
402 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
403 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
405 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
406 printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
407 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
408 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
409 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
411 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
412 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
413 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
415 printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
416 printf(" Intel ME VSCC Table Length (VTL): %d\n",
417 (fdb->flumap1 >> 8) & 0xff);
418 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
419 (fdb->flumap1 & 0xff) << 4);
420 dump_vtba((struct vtba_t *)
421 (image + ((fdb->flumap1 & 0xff) << 4)),
422 (fdb->flumap1 >> 8) & 0xff);
423 dump_oem((uint8_t *)image + 0xf00);
424 dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
426 dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
427 dump_fpsba((struct fpsba_t *)
428 (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
429 dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
430 dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
436 * write_regions() - Write each region from an image to its own file
438 * The filename to use in each case is fixed - see region_filename()
440 * @image: Pointer to image
441 * @size: Size of image in bytes
442 * @return 0 if OK, -ve on error
444 static int write_regions(char *image, int size)
451 fdb = find_fd(image, size);
455 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
457 for (i = 0; i < MAX_REGIONS; i++) {
458 struct region_t region;
461 ret = get_region(frba, i, ®ion);
464 dump_region(i, frba);
465 if (region.size <= 0)
467 region_fd = open(region_filename(i),
468 O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
469 S_IWUSR | S_IRGRP | S_IROTH);
470 if (write(region_fd, image + region.base, region.size) !=
472 perror("Error while writing");
481 static int perror_fname(const char *fmt, const char *fname)
483 char msg[strlen(fmt) + strlen(fname) + 1];
485 sprintf(msg, fmt, fname);
492 * write_image() - Write the image to a file
494 * @filename: Filename to use for the image
495 * @image: Pointer to image
496 * @size: Size of image in bytes
497 * @return 0 if OK, -ve on error
499 static int write_image(char *filename, char *image, int size)
503 debug("Writing new image to %s\n", filename);
505 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
506 S_IWUSR | S_IRGRP | S_IROTH);
508 return perror_fname("Could not open file '%s'", filename);
509 if (write(new_fd, image, size) != size)
510 return perror_fname("Could not write file '%s'", filename);
517 * set_spi_frequency() - Set the SPI frequency to use when booting
519 * Several frequencies are supported, some of which work with fast devices.
520 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
521 * Intel boot system uses this information somehow on boot.
523 * The image is updated with the supplied value
525 * @image: Pointer to image
526 * @size: Size of image in bytes
527 * @freq: SPI frequency to use
529 static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
531 struct fdbar_t *fdb = find_fd(image, size);
534 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
536 /* clear bits 21-29 */
537 fcba->flcomp &= ~0x3fe00000;
538 /* Read ID and Read Status Clock Frequency */
539 fcba->flcomp |= freq << 27;
540 /* Write and Erase Clock Frequency */
541 fcba->flcomp |= freq << 24;
542 /* Fast Read Clock Frequency */
543 fcba->flcomp |= freq << 21;
547 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
549 * @image: Pointer to image
550 * @size: Size of image in bytes
552 static void set_em100_mode(char *image, int size)
554 struct fdbar_t *fdb = find_fd(image, size);
557 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
558 fcba->flcomp &= ~(1 << 30);
559 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
563 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
565 * @image: Pointer to image
566 * @size: Size of image in bytes
568 static void lock_descriptor(char *image, int size)
570 struct fdbar_t *fdb = find_fd(image, size);
574 * TODO: Dynamically take Platform Data Region and GbE Region into
577 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
578 fmba->flmstr1 = 0x0a0b0000;
579 fmba->flmstr2 = 0x0c0d0000;
580 fmba->flmstr3 = 0x08080118;
584 * unlock_descriptor() - Lock the NE descriptor so it can be updated
586 * @image: Pointer to image
587 * @size: Size of image in bytes
589 static void unlock_descriptor(char *image, int size)
591 struct fdbar_t *fdb = find_fd(image, size);
594 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
595 fmba->flmstr1 = 0xffff0000;
596 fmba->flmstr2 = 0xffff0000;
597 fmba->flmstr3 = 0x08080118;
601 * open_for_read() - Open a file for reading
603 * @fname: Filename to open
604 * @sizep: Returns file size in bytes
605 * @return 0 if OK, -1 on error
607 int open_for_read(const char *fname, int *sizep)
609 int fd = open(fname, O_RDONLY);
613 return perror_fname("Could not open file '%s'", fname);
614 if (fstat(fd, &buf) == -1)
615 return perror_fname("Could not stat file '%s'", fname);
616 *sizep = buf.st_size;
617 debug("File %s is %d bytes\n", fname, *sizep);
623 * inject_region() - Add a file to an image region
625 * This puts a file into a particular region of the flash. Several pre-defined
628 * @image: Pointer to image
629 * @size: Size of image in bytes
630 * @region_type: Region where the file should be added
631 * @region_fname: Filename to add to the image
632 * @return 0 if OK, -ve on error
634 int inject_region(char *image, int size, int region_type, char *region_fname)
636 struct fdbar_t *fdb = find_fd(image, size);
637 struct region_t region;
646 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
648 ret = get_region(frba, region_type, ®ion);
651 if (region.size <= 0xfff) {
652 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
653 region_name(region_type));
657 region_fd = open_for_read(region_fname, ®ion_size);
661 if ((region_size > region.size) ||
662 ((region_type != 1) && (region_size > region.size))) {
663 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
664 region_name(region_type), region.size,
665 region.size, region_size, region_size);
669 if ((region_type == 1) && (region_size < region.size)) {
670 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
671 region_name(region_type), region.size,
672 region.size, region_size, region_size);
673 offset = region.size - region_size;
674 memset(image + region.base, 0xff, offset);
677 if (size < region.base + offset + region_size) {
678 fprintf(stderr, "Output file is too small. (%d < %d)\n",
679 size, region.base + offset + region_size);
683 if (read(region_fd, image + region.base + offset, region_size)
685 perror("Could not read file");
691 debug("Adding %s as the %s section\n", region_fname,
692 region_name(region_type));
698 * write_data() - Write some raw data into a region
700 * This puts a file into a particular place in the flash, ignoring the
701 * regions. Be careful not to overwrite something important.
703 * @image: Pointer to image
704 * @size: Size of image in bytes
705 * @addr: x86 ROM address to put file. The ROM ends at
706 * 0xffffffff so use an address relative to that. For an
707 * 8MB ROM the start address is 0xfff80000.
708 * @write_fname: Filename to add to the image
709 * @return number of bytes written if OK, -ve on error
711 static int write_data(char *image, int size, unsigned int addr,
712 const char *write_fname)
714 int write_fd, write_size;
717 write_fd = open_for_read(write_fname, &write_size);
721 offset = (uint32_t)(addr + size);
722 debug("Writing %s to offset %#x\n", write_fname, offset);
724 if (offset < 0 || offset + write_size > size) {
725 fprintf(stderr, "Output file is too small. (%d < %d)\n",
726 size, offset + write_size);
730 if (read(write_fd, image + offset, write_size) != write_size) {
731 perror("Could not read file");
741 * write_uboot() - Write U-Boot, device tree and microcode pointer
743 * This writes U-Boot into a place in the flash, followed by its device tree.
744 * The microcode pointer is written so that U-Boot can find the microcode in
745 * the device tree very early in boot.
747 * @image: Pointer to image
748 * @size: Size of image in bytes
749 * @uboot: Input file information for u-boot.bin
750 * @fdt: Input file information for u-boot.dtb
751 * @ucode_ptr: Address in U-Boot where the microcode pointer should be placed
752 * @return 0 if OK, -ve on error
754 static int write_uboot(char *image, int size, struct input_file *uboot,
755 struct input_file *fdt, unsigned int ucode_ptr)
766 uboot_size = write_data(image, size, uboot->addr, uboot->fname);
769 fdt->addr = uboot->addr + uboot_size;
770 debug("U-Boot size %#x, FDT at %#x\n", uboot_size, fdt->addr);
771 ret = write_data(image, size, fdt->addr, fdt->fname);
776 blob = (void *)image + (uint32_t)(fdt->addr + size);
777 debug("DTB at %lx\n", (char *)blob - image);
778 node = fdt_node_offset_by_compatible(blob, 0,
781 debug("No microcode found in FDT: %s\n",
785 data = fdt_getprop(blob, node, "data", &data_size);
787 debug("No microcode data found in FDT: %s\n",
788 fdt_strerror(data_size));
791 offset = (uint32_t)(ucode_ptr + size);
792 ptr = (void *)image + offset;
793 ptr[0] = (data - image) - size;
795 debug("Wrote microcode pointer at %x: addr=%x, size=%x\n",
796 ucode_ptr, ptr[0], ptr[1]);
802 static void print_version(void)
804 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
805 printf("Copyright (C) 2014 Google Inc.\n\n");
806 printf("SPDX-License-Identifier: GPL-2.0+\n");
809 static void print_usage(const char *name)
811 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
813 " -d | --dump: dump intel firmware descriptor\n"
814 " -x | --extract: extract intel fd modules\n"
815 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
816 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
817 " multiple files can be written simultaneously\n"
818 " -s | --spifreq <20|33|50> set the SPI frequency\n"
819 " -e | --em100 set SPI frequency to 20MHz and disable\n"
820 " Dual Output Fast Read Support\n"
821 " -l | --lock Lock firmware descriptor and ME region\n"
822 " -u | --unlock Unlock firmware descriptor and ME region\n"
823 " -r | --romsize Specify ROM size\n"
824 " -D | --write-descriptor <file> Write descriptor at base\n"
825 " -c | --create Create a new empty image\n"
826 " -v | --version: print the version\n"
827 " -h | --help: print this help\n\n"
828 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
833 * get_two_words() - Convert a string into two words separated by :
835 * The supplied string is split at ':', two substrings are allocated and
838 * @str: String to split
839 * @firstp: Returns first string
840 * @secondp: Returns second string
841 * @return 0 if OK, -ve if @str does not have a :
843 static int get_two_words(const char *str, char **firstp, char **secondp)
847 p = strchr(str, ':');
850 *firstp = strdup(str);
851 (*firstp)[p - str] = '\0';
852 *secondp = strdup(p + 1);
857 int main(int argc, char *argv[])
859 int opt, option_index = 0;
860 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
861 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
862 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
864 char *region_type_string = NULL, *inject_fname = NULL;
865 char *desc_fname = NULL, *addr_str = NULL;
866 int region_type = -1, inputfreq = 0;
867 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
868 struct input_file input_file[WRITE_MAX], *ifile, *fdt = NULL;
869 unsigned char wr_idx, wr_num = 0;
873 char *outfile = NULL;
876 unsigned int ucode_ptr = 0;
877 bool have_uboot = false;
881 static struct option long_options[] = {
882 {"create", 0, NULL, 'c'},
883 {"dump", 0, NULL, 'd'},
884 {"descriptor", 1, NULL, 'D'},
885 {"em100", 0, NULL, 'e'},
886 {"extract", 0, NULL, 'x'},
887 {"fdt", 1, NULL, 'f'},
888 {"inject", 1, NULL, 'i'},
889 {"lock", 0, NULL, 'l'},
890 {"microcode", 1, NULL, 'm'},
891 {"romsize", 1, NULL, 'r'},
892 {"spifreq", 1, NULL, 's'},
893 {"unlock", 0, NULL, 'u'},
894 {"uboot", 1, NULL, 'U'},
895 {"write", 1, NULL, 'w'},
896 {"version", 0, NULL, 'v'},
897 {"help", 0, NULL, 'h'},
901 while ((opt = getopt_long(argc, argv, "cdD:ef:hi:lm:r:s:uU:vw:x?",
902 long_options, &option_index)) != EOF) {
911 mode_write_descriptor = 1;
918 if (get_two_words(optarg, ®ion_type_string,
920 print_usage(argv[0]);
923 if (!strcasecmp("Descriptor", region_type_string))
925 else if (!strcasecmp("BIOS", region_type_string))
927 else if (!strcasecmp("ME", region_type_string))
929 else if (!strcasecmp("GbE", region_type_string))
931 else if (!strcasecmp("Platform", region_type_string))
933 if (region_type == -1) {
934 fprintf(stderr, "No such region type: '%s'\n\n",
936 print_usage(argv[0]);
945 ucode_ptr = strtoul(optarg, NULL, 0);
948 rom_size = strtol(optarg, NULL, 0);
949 debug("ROM size %d\n", rom_size);
952 /* Parse the requested SPI frequency */
953 inputfreq = strtol(optarg, NULL, 0);
956 spifreq = SPI_FREQUENCY_20MHZ;
959 spifreq = SPI_FREQUENCY_33MHZ;
962 spifreq = SPI_FREQUENCY_50MHZ;
965 fprintf(stderr, "Invalid SPI Frequency: %d\n",
967 print_usage(argv[0]);
982 ifile = &input_file[wr_num];
984 if (wr_num < WRITE_MAX) {
985 if (get_two_words(optarg, &addr_str,
987 print_usage(argv[0]);
990 ifile->addr = strtoll(optarg, NULL, 0);
991 ifile->type = opt == 'f' ? IF_fdt :
992 opt == 'U' ? IF_uboot : IF_normal;
993 if (ifile->type == IF_fdt)
995 else if (ifile->type == IF_uboot)
1000 "The number of files to write simultaneously exceeds the limitation (%d)\n",
1010 print_usage(argv[0]);
1016 if (mode_locked == 1 && mode_unlocked == 1) {
1017 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
1021 if (mode_inject == 1 && mode_write == 1) {
1022 fprintf(stderr, "Inject/Write are mutually exclusive\n");
1026 if ((mode_dump + mode_extract + mode_inject +
1027 (mode_spifreq | mode_em100 | mode_unlocked |
1028 mode_locked)) > 1) {
1029 fprintf(stderr, "You may not specify more than one mode.\n\n");
1030 print_usage(argv[0]);
1034 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
1035 mode_em100 + mode_locked + mode_unlocked + mode_write +
1036 mode_write_descriptor) == 0 && !create) {
1037 fprintf(stderr, "You need to specify a mode.\n\n");
1038 print_usage(argv[0]);
1042 if (create && rom_size == -1) {
1043 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
1047 if (optind + 1 != argc) {
1048 fprintf(stderr, "You need to specify a file.\n\n");
1049 print_usage(argv[0]);
1053 if (have_uboot && !fdt) {
1055 "You must supply a device tree file for U-Boot\n\n");
1056 print_usage(argv[0]);
1060 filename = argv[optind];
1061 if (optind + 2 != argc)
1062 outfile = argv[optind + 1];
1065 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
1067 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
1069 if (bios_fd == -1) {
1070 perror("Could not open file");
1075 if (fstat(bios_fd, &buf) == -1) {
1076 perror("Could not stat file");
1082 debug("File %s is %d bytes\n", filename, size);
1087 image = malloc(rom_size);
1089 printf("Out of memory.\n");
1093 memset(image, '\xff', rom_size);
1094 if (!create && read(bios_fd, image, size) != size) {
1095 perror("Could not read file");
1098 if (size != rom_size) {
1099 debug("ROM size changed to %d bytes\n", rom_size);
1106 ret = dump_fd(image, size);
1111 ret = write_regions(image, size);
1115 if (mode_write_descriptor)
1116 ret = write_data(image, size, -size, desc_fname);
1119 ret = inject_region(image, size, region_type, inject_fname);
1122 for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
1123 ifile = &input_file[wr_idx];
1124 if (ifile->type == IF_fdt) {
1126 } else if (ifile->type == IF_uboot) {
1127 ret = write_uboot(image, size, ifile, fdt,
1130 ret = write_data(image, size, ifile->addr,
1139 set_spi_frequency(image, size, spifreq);
1142 set_em100_mode(image, size);
1145 lock_descriptor(image, size);
1148 unlock_descriptor(image, size);
1152 ret = write_image(outfile, image, size);
1154 if (lseek(bios_fd, 0, SEEK_SET)) {
1155 perror("Error while seeking");
1158 if (write(bios_fd, image, size) != size) {
1159 perror("Error while writing");
1168 return ret < 0 ? 1 : 0;