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);
42 * find_fd() - Find the flash description in the ROM image
44 * @image: Pointer to image
45 * @size: Size of image in bytes
46 * @return pointer to structure, or NULL if not found
48 static struct fdbar_t *find_fd(char *image, int size)
52 /* Scan for FD signature */
53 for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) {
54 if (*ptr == FD_SIGNATURE)
59 printf("No Flash Descriptor found in this image\n");
63 debug("Found Flash Descriptor signature at 0x%08lx\n",
66 return (struct fdbar_t *)ptr;
70 * get_region() - Get information about the selected region
72 * @frba: Flash region list
73 * @region_type: Type of region (0..MAX_REGIONS-1)
74 * @region: Region information is written here
75 * @return 0 if OK, else -ve
77 static int get_region(struct frba_t *frba, int region_type,
78 struct region_t *region)
80 if (region_type >= MAX_REGIONS) {
81 fprintf(stderr, "Invalid region type.\n");
85 region->base = FLREG_BASE(frba->flreg[region_type]);
86 region->limit = FLREG_LIMIT(frba->flreg[region_type]);
87 region->size = region->limit - region->base + 1;
92 static const char *region_name(int region_type)
94 static const char *const regions[] = {
102 assert(region_type < MAX_REGIONS);
104 return regions[region_type];
107 static const char *region_filename(int region_type)
109 static const char *const region_filenames[] = {
110 "flashregion_0_flashdescriptor.bin",
111 "flashregion_1_bios.bin",
112 "flashregion_2_intel_me.bin",
113 "flashregion_3_gbe.bin",
114 "flashregion_4_platform_data.bin"
117 assert(region_type < MAX_REGIONS);
119 return region_filenames[region_type];
122 static int dump_region(int num, struct frba_t *frba)
124 struct region_t region;
127 ret = get_region(frba, num, ®ion);
131 printf(" Flash Region %d (%s): %08x - %08x %s\n",
132 num, region_name(num), region.base, region.limit,
133 region.size < 1 ? "(unused)" : "");
138 static void dump_frba(struct frba_t *frba)
142 printf("Found Region Section\n");
143 for (i = 0; i < MAX_REGIONS; i++) {
144 printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
145 dump_region(i, frba);
149 static void decode_spi_frequency(unsigned int freq)
152 case SPI_FREQUENCY_20MHZ:
155 case SPI_FREQUENCY_33MHZ:
158 case SPI_FREQUENCY_50MHZ:
162 printf("unknown<%x>MHz", freq);
166 static void decode_component_density(unsigned int density)
169 case COMPONENT_DENSITY_512KB:
172 case COMPONENT_DENSITY_1MB:
175 case COMPONENT_DENSITY_2MB:
178 case COMPONENT_DENSITY_4MB:
181 case COMPONENT_DENSITY_8MB:
184 case COMPONENT_DENSITY_16MB:
188 printf("unknown<%x>MiB", density);
192 static void dump_fcba(struct fcba_t *fcba)
194 printf("\nFound Component Section\n");
195 printf("FLCOMP 0x%08x\n", fcba->flcomp);
196 printf(" Dual Output Fast Read Support: %ssupported\n",
197 (fcba->flcomp & (1 << 30)) ? "" : "not ");
198 printf(" Read ID/Read Status Clock Frequency: ");
199 decode_spi_frequency((fcba->flcomp >> 27) & 7);
200 printf("\n Write/Erase Clock Frequency: ");
201 decode_spi_frequency((fcba->flcomp >> 24) & 7);
202 printf("\n Fast Read Clock Frequency: ");
203 decode_spi_frequency((fcba->flcomp >> 21) & 7);
204 printf("\n Fast Read Support: %ssupported",
205 (fcba->flcomp & (1 << 20)) ? "" : "not ");
206 printf("\n Read Clock Frequency: ");
207 decode_spi_frequency((fcba->flcomp >> 17) & 7);
208 printf("\n Component 2 Density: ");
209 decode_component_density((fcba->flcomp >> 3) & 7);
210 printf("\n Component 1 Density: ");
211 decode_component_density(fcba->flcomp & 7);
213 printf("FLILL 0x%08x\n", fcba->flill);
214 printf(" Invalid Instruction 3: 0x%02x\n",
215 (fcba->flill >> 24) & 0xff);
216 printf(" Invalid Instruction 2: 0x%02x\n",
217 (fcba->flill >> 16) & 0xff);
218 printf(" Invalid Instruction 1: 0x%02x\n",
219 (fcba->flill >> 8) & 0xff);
220 printf(" Invalid Instruction 0: 0x%02x\n",
222 printf("FLPB 0x%08x\n", fcba->flpb);
223 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
224 (fcba->flpb & 0xfff) << 12);
227 static void dump_fpsba(struct fpsba_t *fpsba)
231 printf("Found PCH Strap Section\n");
232 for (i = 0; i < MAX_STRAPS; i++)
233 printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
236 static const char *get_enabled(int flag)
238 return flag ? "enabled" : "disabled";
241 static void decode_flmstr(uint32_t flmstr)
243 printf(" Platform Data Region Write Access: %s\n",
244 get_enabled(flmstr & (1 << 28)));
245 printf(" GbE Region Write Access: %s\n",
246 get_enabled(flmstr & (1 << 27)));
247 printf(" Intel ME Region Write Access: %s\n",
248 get_enabled(flmstr & (1 << 26)));
249 printf(" Host CPU/BIOS Region Write Access: %s\n",
250 get_enabled(flmstr & (1 << 25)));
251 printf(" Flash Descriptor Write Access: %s\n",
252 get_enabled(flmstr & (1 << 24)));
254 printf(" Platform Data Region Read Access: %s\n",
255 get_enabled(flmstr & (1 << 20)));
256 printf(" GbE Region Read Access: %s\n",
257 get_enabled(flmstr & (1 << 19)));
258 printf(" Intel ME Region Read Access: %s\n",
259 get_enabled(flmstr & (1 << 18)));
260 printf(" Host CPU/BIOS Region Read Access: %s\n",
261 get_enabled(flmstr & (1 << 17)));
262 printf(" Flash Descriptor Read Access: %s\n",
263 get_enabled(flmstr & (1 << 16)));
265 printf(" Requester ID: 0x%04x\n\n",
269 static void dump_fmba(struct fmba_t *fmba)
271 printf("Found Master Section\n");
272 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
273 decode_flmstr(fmba->flmstr1);
274 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
275 decode_flmstr(fmba->flmstr2);
276 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
277 decode_flmstr(fmba->flmstr3);
280 static void dump_fmsba(struct fmsba_t *fmsba)
284 printf("Found Processor Strap Section\n");
285 for (i = 0; i < 4; i++)
286 printf("????: 0x%08x\n", fmsba->data[0]);
289 static void dump_jid(uint32_t jid)
291 printf(" SPI Component Device ID 1: 0x%02x\n",
293 printf(" SPI Component Device ID 0: 0x%02x\n",
295 printf(" SPI Component Vendor ID: 0x%02x\n",
299 static void dump_vscc(uint32_t vscc)
301 printf(" Lower Erase Opcode: 0x%02x\n",
303 printf(" Lower Write Enable on Write Status: 0x%02x\n",
304 vscc & (1 << 20) ? 0x06 : 0x50);
305 printf(" Lower Write Status Required: %s\n",
306 vscc & (1 << 19) ? "Yes" : "No");
307 printf(" Lower Write Granularity: %d bytes\n",
308 vscc & (1 << 18) ? 64 : 1);
309 printf(" Lower Block / Sector Erase Size: ");
310 switch ((vscc >> 16) & 0x3) {
312 printf("256 Byte\n");
325 printf(" Upper Erase Opcode: 0x%02x\n",
327 printf(" Upper Write Enable on Write Status: 0x%02x\n",
328 vscc & (1 << 4) ? 0x06 : 0x50);
329 printf(" Upper Write Status Required: %s\n",
330 vscc & (1 << 3) ? "Yes" : "No");
331 printf(" Upper Write Granularity: %d bytes\n",
332 vscc & (1 << 2) ? 64 : 1);
333 printf(" Upper Block / Sector Erase Size: ");
334 switch (vscc & 0x3) {
336 printf("256 Byte\n");
350 static void dump_vtba(struct vtba_t *vtba, int vtl)
353 int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
355 printf("ME VSCC table:\n");
356 for (i = 0; i < num; i++) {
357 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
358 dump_jid(vtba->entry[i].jid);
359 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
360 dump_vscc(vtba->entry[i].vscc);
365 static void dump_oem(uint8_t *oem)
368 printf("OEM Section:\n");
369 for (i = 0; i < 4; i++) {
370 printf("%02x:", i << 4);
371 for (j = 0; j < 16; j++)
372 printf(" %02x", oem[(i<<4)+j]);
379 * dump_fd() - Display a dump of the full flash description
381 * @image: Pointer to image
382 * @size: Size of image in bytes
383 * @return 0 if OK, -1 on error
385 static int dump_fd(char *image, int size)
387 struct fdbar_t *fdb = find_fd(image, size);
392 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
393 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
394 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
395 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
396 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
398 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
399 printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
400 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
401 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
402 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
404 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
405 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
406 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
408 printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
409 printf(" Intel ME VSCC Table Length (VTL): %d\n",
410 (fdb->flumap1 >> 8) & 0xff);
411 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
412 (fdb->flumap1 & 0xff) << 4);
413 dump_vtba((struct vtba_t *)
414 (image + ((fdb->flumap1 & 0xff) << 4)),
415 (fdb->flumap1 >> 8) & 0xff);
416 dump_oem((uint8_t *)image + 0xf00);
417 dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
419 dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
420 dump_fpsba((struct fpsba_t *)
421 (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
422 dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
423 dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
429 * write_regions() - Write each region from an image to its own file
431 * The filename to use in each case is fixed - see region_filename()
433 * @image: Pointer to image
434 * @size: Size of image in bytes
435 * @return 0 if OK, -ve on error
437 static int write_regions(char *image, int size)
444 fdb = find_fd(image, size);
448 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
450 for (i = 0; i < MAX_REGIONS; i++) {
451 struct region_t region;
454 ret = get_region(frba, i, ®ion);
457 dump_region(i, frba);
458 if (region.size <= 0)
460 region_fd = open(region_filename(i),
461 O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
462 S_IWUSR | S_IRGRP | S_IROTH);
463 if (write(region_fd, image + region.base, region.size) !=
465 perror("Error while writing");
474 static int perror_fname(const char *fmt, const char *fname)
476 char msg[strlen(fmt) + strlen(fname) + 1];
478 sprintf(msg, fmt, fname);
485 * write_image() - Write the image to a file
487 * @filename: Filename to use for the image
488 * @image: Pointer to image
489 * @size: Size of image in bytes
490 * @return 0 if OK, -ve on error
492 static int write_image(char *filename, char *image, int size)
496 debug("Writing new image to %s\n", filename);
498 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
499 S_IWUSR | S_IRGRP | S_IROTH);
501 return perror_fname("Could not open file '%s'", filename);
502 if (write(new_fd, image, size) != size)
503 return perror_fname("Could not write file '%s'", filename);
510 * set_spi_frequency() - Set the SPI frequency to use when booting
512 * Several frequencies are supported, some of which work with fast devices.
513 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
514 * Intel boot system uses this information somehow on boot.
516 * The image is updated with the supplied value
518 * @image: Pointer to image
519 * @size: Size of image in bytes
520 * @freq: SPI frequency to use
522 static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
524 struct fdbar_t *fdb = find_fd(image, size);
527 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
529 /* clear bits 21-29 */
530 fcba->flcomp &= ~0x3fe00000;
531 /* Read ID and Read Status Clock Frequency */
532 fcba->flcomp |= freq << 27;
533 /* Write and Erase Clock Frequency */
534 fcba->flcomp |= freq << 24;
535 /* Fast Read Clock Frequency */
536 fcba->flcomp |= freq << 21;
540 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
542 * @image: Pointer to image
543 * @size: Size of image in bytes
545 static void set_em100_mode(char *image, int size)
547 struct fdbar_t *fdb = find_fd(image, size);
550 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
551 fcba->flcomp &= ~(1 << 30);
552 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
556 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
558 * @image: Pointer to image
559 * @size: Size of image in bytes
561 static void lock_descriptor(char *image, int size)
563 struct fdbar_t *fdb = find_fd(image, size);
567 * TODO: Dynamically take Platform Data Region and GbE Region into
570 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
571 fmba->flmstr1 = 0x0a0b0000;
572 fmba->flmstr2 = 0x0c0d0000;
573 fmba->flmstr3 = 0x08080118;
577 * unlock_descriptor() - Lock the NE descriptor so it can be updated
579 * @image: Pointer to image
580 * @size: Size of image in bytes
582 static void unlock_descriptor(char *image, int size)
584 struct fdbar_t *fdb = find_fd(image, size);
587 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
588 fmba->flmstr1 = 0xffff0000;
589 fmba->flmstr2 = 0xffff0000;
590 fmba->flmstr3 = 0x08080118;
594 * open_for_read() - Open a file for reading
596 * @fname: Filename to open
597 * @sizep: Returns file size in bytes
598 * @return 0 if OK, -1 on error
600 int open_for_read(const char *fname, int *sizep)
602 int fd = open(fname, O_RDONLY);
606 return perror_fname("Could not open file '%s'", fname);
607 if (fstat(fd, &buf) == -1)
608 return perror_fname("Could not stat file '%s'", fname);
609 *sizep = buf.st_size;
610 debug("File %s is %d bytes\n", fname, *sizep);
616 * inject_region() - Add a file to an image region
618 * This puts a file into a particular region of the flash. Several pre-defined
621 * @image: Pointer to image
622 * @size: Size of image in bytes
623 * @region_type: Region where the file should be added
624 * @region_fname: Filename to add to the image
625 * @return 0 if OK, -ve on error
627 int inject_region(char *image, int size, int region_type, char *region_fname)
629 struct fdbar_t *fdb = find_fd(image, size);
630 struct region_t region;
639 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
641 ret = get_region(frba, region_type, ®ion);
644 if (region.size <= 0xfff) {
645 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
646 region_name(region_type));
650 region_fd = open_for_read(region_fname, ®ion_size);
654 if ((region_size > region.size) ||
655 ((region_type != 1) && (region_size > region.size))) {
656 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
657 region_name(region_type), region.size,
658 region.size, region_size, region_size);
662 if ((region_type == 1) && (region_size < region.size)) {
663 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
664 region_name(region_type), region.size,
665 region.size, region_size, region_size);
666 offset = region.size - region_size;
667 memset(image + region.base, 0xff, offset);
670 if (size < region.base + offset + region_size) {
671 fprintf(stderr, "Output file is too small. (%d < %d)\n",
672 size, region.base + offset + region_size);
676 if (read(region_fd, image + region.base + offset, region_size)
678 perror("Could not read file");
684 debug("Adding %s as the %s section\n", region_fname,
685 region_name(region_type));
691 * write_data() - Write some raw data into a region
693 * This puts a file into a particular place in the flash, ignoring the
694 * regions. Be careful not to overwrite something important.
696 * @image: Pointer to image
697 * @size: Size of image in bytes
698 * @addr: x86 ROM address to put file. The ROM ends at
699 * 0xffffffff so use an address relative to that. For an
700 * 8MB ROM the start address is 0xfff80000.
701 * @write_fname: Filename to add to the image
702 * @offset_uboot_top: Offset of the top of U-Boot
703 * @offset_uboot_start: Offset of the start of U-Boot
704 * @return number of bytes written if OK, -ve on error
706 static int write_data(char *image, int size, unsigned int addr,
707 const char *write_fname, int offset_uboot_top,
708 int offset_uboot_start)
710 int write_fd, write_size;
713 write_fd = open_for_read(write_fname, &write_size);
717 offset = (uint32_t)(addr + size);
718 if (offset_uboot_top) {
719 if (offset_uboot_start < offset &&
720 offset_uboot_top >= offset) {
721 fprintf(stderr, "U-Boot image overlaps with region '%s'\n",
724 "U-Boot finishes at offset %x, file starts at %x\n",
725 offset_uboot_top, offset);
728 if (offset_uboot_start > offset &&
729 offset_uboot_start <= offset + write_size) {
730 fprintf(stderr, "U-Boot image overlaps with region '%s'\n",
733 "U-Boot starts at offset %x, file finishes at %x\n",
734 offset_uboot_start, offset + write_size);
738 debug("Writing %s to offset %#x\n", write_fname, offset);
740 if (offset < 0 || offset + write_size > size) {
741 fprintf(stderr, "Output file is too small. (%d < %d)\n",
742 size, offset + write_size);
746 if (read(write_fd, image + offset, write_size) != write_size) {
747 perror("Could not read file");
756 static void print_version(void)
758 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
759 printf("Copyright (C) 2014 Google Inc.\n\n");
760 printf("SPDX-License-Identifier: GPL-2.0+\n");
763 static void print_usage(const char *name)
765 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
767 " -d | --dump: dump intel firmware descriptor\n"
768 " -x | --extract: extract intel fd modules\n"
769 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
770 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
771 " multiple files can be written simultaneously\n"
772 " -s | --spifreq <20|33|50> set the SPI frequency\n"
773 " -e | --em100 set SPI frequency to 20MHz and disable\n"
774 " Dual Output Fast Read Support\n"
775 " -l | --lock Lock firmware descriptor and ME region\n"
776 " -u | --unlock Unlock firmware descriptor and ME region\n"
777 " -r | --romsize Specify ROM size\n"
778 " -D | --write-descriptor <file> Write descriptor at base\n"
779 " -c | --create Create a new empty image\n"
780 " -v | --version: print the version\n"
781 " -h | --help: print this help\n\n"
782 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
787 * get_two_words() - Convert a string into two words separated by :
789 * The supplied string is split at ':', two substrings are allocated and
792 * @str: String to split
793 * @firstp: Returns first string
794 * @secondp: Returns second string
795 * @return 0 if OK, -ve if @str does not have a :
797 static int get_two_words(const char *str, char **firstp, char **secondp)
801 p = strchr(str, ':');
804 *firstp = strdup(str);
805 (*firstp)[p - str] = '\0';
806 *secondp = strdup(p + 1);
811 int main(int argc, char *argv[])
813 int opt, option_index = 0;
814 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
815 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
816 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
818 char *region_type_string = NULL, *inject_fname = NULL;
819 char *desc_fname = NULL, *addr_str = NULL;
820 int region_type = -1, inputfreq = 0;
821 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
822 struct input_file input_file[WRITE_MAX], *ifile, *fdt = NULL;
823 unsigned char wr_idx, wr_num = 0;
827 char *outfile = NULL;
830 bool have_uboot = false;
834 static struct option long_options[] = {
835 {"create", 0, NULL, 'c'},
836 {"dump", 0, NULL, 'd'},
837 {"descriptor", 1, NULL, 'D'},
838 {"em100", 0, NULL, 'e'},
839 {"extract", 0, NULL, 'x'},
840 {"fdt", 1, NULL, 'f'},
841 {"inject", 1, NULL, 'i'},
842 {"lock", 0, NULL, 'l'},
843 {"romsize", 1, NULL, 'r'},
844 {"spifreq", 1, NULL, 's'},
845 {"unlock", 0, NULL, 'u'},
846 {"uboot", 1, NULL, 'U'},
847 {"write", 1, NULL, 'w'},
848 {"version", 0, NULL, 'v'},
849 {"help", 0, NULL, 'h'},
853 while ((opt = getopt_long(argc, argv, "cdD:ef:hi:lr:s:uU:vw:x?",
854 long_options, &option_index)) != EOF) {
863 mode_write_descriptor = 1;
870 if (get_two_words(optarg, ®ion_type_string,
872 print_usage(argv[0]);
875 if (!strcasecmp("Descriptor", region_type_string))
877 else if (!strcasecmp("BIOS", region_type_string))
879 else if (!strcasecmp("ME", region_type_string))
881 else if (!strcasecmp("GbE", region_type_string))
883 else if (!strcasecmp("Platform", region_type_string))
885 if (region_type == -1) {
886 fprintf(stderr, "No such region type: '%s'\n\n",
888 print_usage(argv[0]);
897 rom_size = strtol(optarg, NULL, 0);
898 debug("ROM size %d\n", rom_size);
901 /* Parse the requested SPI frequency */
902 inputfreq = strtol(optarg, NULL, 0);
905 spifreq = SPI_FREQUENCY_20MHZ;
908 spifreq = SPI_FREQUENCY_33MHZ;
911 spifreq = SPI_FREQUENCY_50MHZ;
914 fprintf(stderr, "Invalid SPI Frequency: %d\n",
916 print_usage(argv[0]);
931 ifile = &input_file[wr_num];
933 if (wr_num < WRITE_MAX) {
934 if (get_two_words(optarg, &addr_str,
936 print_usage(argv[0]);
939 ifile->addr = strtoll(optarg, NULL, 0);
943 "The number of files to write simultaneously exceeds the limitation (%d)\n",
953 print_usage(argv[0]);
959 if (mode_locked == 1 && mode_unlocked == 1) {
960 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
964 if (mode_inject == 1 && mode_write == 1) {
965 fprintf(stderr, "Inject/Write are mutually exclusive\n");
969 if ((mode_dump + mode_extract + mode_inject +
970 (mode_spifreq | mode_em100 | mode_unlocked |
972 fprintf(stderr, "You may not specify more than one mode.\n\n");
973 print_usage(argv[0]);
977 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
978 mode_em100 + mode_locked + mode_unlocked + mode_write +
979 mode_write_descriptor) == 0 && !create) {
980 fprintf(stderr, "You need to specify a mode.\n\n");
981 print_usage(argv[0]);
985 if (create && rom_size == -1) {
986 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
990 if (optind + 1 != argc) {
991 fprintf(stderr, "You need to specify a file.\n\n");
992 print_usage(argv[0]);
996 if (have_uboot && !fdt) {
998 "You must supply a device tree file for U-Boot\n\n");
999 print_usage(argv[0]);
1003 filename = argv[optind];
1004 if (optind + 2 != argc)
1005 outfile = argv[optind + 1];
1008 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
1010 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
1012 if (bios_fd == -1) {
1013 perror("Could not open file");
1018 if (fstat(bios_fd, &buf) == -1) {
1019 perror("Could not stat file");
1025 debug("File %s is %d bytes\n", filename, size);
1030 image = malloc(rom_size);
1032 printf("Out of memory.\n");
1036 memset(image, '\xff', rom_size);
1037 if (!create && read(bios_fd, image, size) != size) {
1038 perror("Could not read file");
1041 if (size != rom_size) {
1042 debug("ROM size changed to %d bytes\n", rom_size);
1049 ret = dump_fd(image, size);
1054 ret = write_regions(image, size);
1058 if (mode_write_descriptor)
1059 ret = write_data(image, size, -size, desc_fname, 0, 0);
1062 ret = inject_region(image, size, region_type, inject_fname);
1065 int offset_uboot_top = 0;
1066 int offset_uboot_start = 0;
1068 for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
1069 ifile = &input_file[wr_idx];
1070 ret = write_data(image, size, ifile->addr,
1071 ifile->fname, offset_uboot_top,
1072 offset_uboot_start);
1079 set_spi_frequency(image, size, spifreq);
1082 set_em100_mode(image, size);
1085 lock_descriptor(image, size);
1088 unlock_descriptor(image, size);
1092 ret = write_image(outfile, image, size);
1094 if (lseek(bios_fd, 0, SEEK_SET)) {
1095 perror("Error while seeking");
1098 if (write(bios_fd, image, size) != size) {
1099 perror("Error while writing");
1108 return ret < 0 ? 1 : 0;