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>
26 #define debug(fmt, args...) printf(fmt, ##args)
28 #define debug(fmt, args...)
31 #define FD_SIGNATURE 0x0FF0A55A
32 #define FLREG_BASE(reg) ((reg & 0x00000fff) << 12);
33 #define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff);
36 * find_fd() - Find the flash description in the ROM image
38 * @image: Pointer to image
39 * @size: Size of image in bytes
40 * @return pointer to structure, or NULL if not found
42 static struct fdbar_t *find_fd(char *image, int size)
46 /* Scan for FD signature */
47 for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) {
48 if (*ptr == FD_SIGNATURE)
53 printf("No Flash Descriptor found in this image\n");
57 debug("Found Flash Descriptor signature at 0x%08lx\n",
60 return (struct fdbar_t *)ptr;
64 * get_region() - Get information about the selected region
66 * @frba: Flash region list
67 * @region_type: Type of region (0..MAX_REGIONS-1)
68 * @region: Region information is written here
69 * @return 0 if OK, else -ve
71 static int get_region(struct frba_t *frba, int region_type,
72 struct region_t *region)
74 if (region_type >= MAX_REGIONS) {
75 fprintf(stderr, "Invalid region type.\n");
79 region->base = FLREG_BASE(frba->flreg[region_type]);
80 region->limit = FLREG_LIMIT(frba->flreg[region_type]);
81 region->size = region->limit - region->base + 1;
86 static const char *region_name(int region_type)
88 static const char *const regions[] = {
96 assert(region_type < MAX_REGIONS);
98 return regions[region_type];
101 static const char *region_filename(int region_type)
103 static const char *const region_filenames[] = {
104 "flashregion_0_flashdescriptor.bin",
105 "flashregion_1_bios.bin",
106 "flashregion_2_intel_me.bin",
107 "flashregion_3_gbe.bin",
108 "flashregion_4_platform_data.bin"
111 assert(region_type < MAX_REGIONS);
113 return region_filenames[region_type];
116 static int dump_region(int num, struct frba_t *frba)
118 struct region_t region;
121 ret = get_region(frba, num, ®ion);
125 printf(" Flash Region %d (%s): %08x - %08x %s\n",
126 num, region_name(num), region.base, region.limit,
127 region.size < 1 ? "(unused)" : "");
132 static void dump_frba(struct frba_t *frba)
136 printf("Found Region Section\n");
137 for (i = 0; i < MAX_REGIONS; i++) {
138 printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
139 dump_region(i, frba);
143 static void decode_spi_frequency(unsigned int freq)
146 case SPI_FREQUENCY_20MHZ:
149 case SPI_FREQUENCY_33MHZ:
152 case SPI_FREQUENCY_50MHZ:
156 printf("unknown<%x>MHz", freq);
160 static void decode_component_density(unsigned int density)
163 case COMPONENT_DENSITY_512KB:
166 case COMPONENT_DENSITY_1MB:
169 case COMPONENT_DENSITY_2MB:
172 case COMPONENT_DENSITY_4MB:
175 case COMPONENT_DENSITY_8MB:
178 case COMPONENT_DENSITY_16MB:
182 printf("unknown<%x>MiB", density);
186 static void dump_fcba(struct fcba_t *fcba)
188 printf("\nFound Component Section\n");
189 printf("FLCOMP 0x%08x\n", fcba->flcomp);
190 printf(" Dual Output Fast Read Support: %ssupported\n",
191 (fcba->flcomp & (1 << 30)) ? "" : "not ");
192 printf(" Read ID/Read Status Clock Frequency: ");
193 decode_spi_frequency((fcba->flcomp >> 27) & 7);
194 printf("\n Write/Erase Clock Frequency: ");
195 decode_spi_frequency((fcba->flcomp >> 24) & 7);
196 printf("\n Fast Read Clock Frequency: ");
197 decode_spi_frequency((fcba->flcomp >> 21) & 7);
198 printf("\n Fast Read Support: %ssupported",
199 (fcba->flcomp & (1 << 20)) ? "" : "not ");
200 printf("\n Read Clock Frequency: ");
201 decode_spi_frequency((fcba->flcomp >> 17) & 7);
202 printf("\n Component 2 Density: ");
203 decode_component_density((fcba->flcomp >> 3) & 7);
204 printf("\n Component 1 Density: ");
205 decode_component_density(fcba->flcomp & 7);
207 printf("FLILL 0x%08x\n", fcba->flill);
208 printf(" Invalid Instruction 3: 0x%02x\n",
209 (fcba->flill >> 24) & 0xff);
210 printf(" Invalid Instruction 2: 0x%02x\n",
211 (fcba->flill >> 16) & 0xff);
212 printf(" Invalid Instruction 1: 0x%02x\n",
213 (fcba->flill >> 8) & 0xff);
214 printf(" Invalid Instruction 0: 0x%02x\n",
216 printf("FLPB 0x%08x\n", fcba->flpb);
217 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
218 (fcba->flpb & 0xfff) << 12);
221 static void dump_fpsba(struct fpsba_t *fpsba)
225 printf("Found PCH Strap Section\n");
226 for (i = 0; i < MAX_STRAPS; i++)
227 printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
230 static const char *get_enabled(int flag)
232 return flag ? "enabled" : "disabled";
235 static void decode_flmstr(uint32_t flmstr)
237 printf(" Platform Data Region Write Access: %s\n",
238 get_enabled(flmstr & (1 << 28)));
239 printf(" GbE Region Write Access: %s\n",
240 get_enabled(flmstr & (1 << 27)));
241 printf(" Intel ME Region Write Access: %s\n",
242 get_enabled(flmstr & (1 << 26)));
243 printf(" Host CPU/BIOS Region Write Access: %s\n",
244 get_enabled(flmstr & (1 << 25)));
245 printf(" Flash Descriptor Write Access: %s\n",
246 get_enabled(flmstr & (1 << 24)));
248 printf(" Platform Data Region Read Access: %s\n",
249 get_enabled(flmstr & (1 << 20)));
250 printf(" GbE Region Read Access: %s\n",
251 get_enabled(flmstr & (1 << 19)));
252 printf(" Intel ME Region Read Access: %s\n",
253 get_enabled(flmstr & (1 << 18)));
254 printf(" Host CPU/BIOS Region Read Access: %s\n",
255 get_enabled(flmstr & (1 << 17)));
256 printf(" Flash Descriptor Read Access: %s\n",
257 get_enabled(flmstr & (1 << 16)));
259 printf(" Requester ID: 0x%04x\n\n",
263 static void dump_fmba(struct fmba_t *fmba)
265 printf("Found Master Section\n");
266 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
267 decode_flmstr(fmba->flmstr1);
268 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
269 decode_flmstr(fmba->flmstr2);
270 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
271 decode_flmstr(fmba->flmstr3);
274 static void dump_fmsba(struct fmsba_t *fmsba)
278 printf("Found Processor Strap Section\n");
279 for (i = 0; i < 4; i++)
280 printf("????: 0x%08x\n", fmsba->data[0]);
283 static void dump_jid(uint32_t jid)
285 printf(" SPI Component Device ID 1: 0x%02x\n",
287 printf(" SPI Component Device ID 0: 0x%02x\n",
289 printf(" SPI Component Vendor ID: 0x%02x\n",
293 static void dump_vscc(uint32_t vscc)
295 printf(" Lower Erase Opcode: 0x%02x\n",
297 printf(" Lower Write Enable on Write Status: 0x%02x\n",
298 vscc & (1 << 20) ? 0x06 : 0x50);
299 printf(" Lower Write Status Required: %s\n",
300 vscc & (1 << 19) ? "Yes" : "No");
301 printf(" Lower Write Granularity: %d bytes\n",
302 vscc & (1 << 18) ? 64 : 1);
303 printf(" Lower Block / Sector Erase Size: ");
304 switch ((vscc >> 16) & 0x3) {
306 printf("256 Byte\n");
319 printf(" Upper Erase Opcode: 0x%02x\n",
321 printf(" Upper Write Enable on Write Status: 0x%02x\n",
322 vscc & (1 << 4) ? 0x06 : 0x50);
323 printf(" Upper Write Status Required: %s\n",
324 vscc & (1 << 3) ? "Yes" : "No");
325 printf(" Upper Write Granularity: %d bytes\n",
326 vscc & (1 << 2) ? 64 : 1);
327 printf(" Upper Block / Sector Erase Size: ");
328 switch (vscc & 0x3) {
330 printf("256 Byte\n");
344 static void dump_vtba(struct vtba_t *vtba, int vtl)
347 int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
349 printf("ME VSCC table:\n");
350 for (i = 0; i < num; i++) {
351 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
352 dump_jid(vtba->entry[i].jid);
353 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
354 dump_vscc(vtba->entry[i].vscc);
359 static void dump_oem(uint8_t *oem)
362 printf("OEM Section:\n");
363 for (i = 0; i < 4; i++) {
364 printf("%02x:", i << 4);
365 for (j = 0; j < 16; j++)
366 printf(" %02x", oem[(i<<4)+j]);
373 * dump_fd() - Display a dump of the full flash description
375 * @image: Pointer to image
376 * @size: Size of image in bytes
377 * @return 0 if OK, -1 on error
379 static int dump_fd(char *image, int size)
381 struct fdbar_t *fdb = find_fd(image, size);
386 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
387 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
388 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
389 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
390 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
392 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
393 printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
394 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
395 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
396 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
398 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
399 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
400 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
402 printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
403 printf(" Intel ME VSCC Table Length (VTL): %d\n",
404 (fdb->flumap1 >> 8) & 0xff);
405 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
406 (fdb->flumap1 & 0xff) << 4);
407 dump_vtba((struct vtba_t *)
408 (image + ((fdb->flumap1 & 0xff) << 4)),
409 (fdb->flumap1 >> 8) & 0xff);
410 dump_oem((uint8_t *)image + 0xf00);
411 dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
413 dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
414 dump_fpsba((struct fpsba_t *)
415 (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
416 dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
417 dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
423 * write_regions() - Write each region from an image to its own file
425 * The filename to use in each case is fixed - see region_filename()
427 * @image: Pointer to image
428 * @size: Size of image in bytes
429 * @return 0 if OK, -ve on error
431 static int write_regions(char *image, int size)
438 fdb = find_fd(image, size);
442 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
444 for (i = 0; i < MAX_REGIONS; i++) {
445 struct region_t region;
448 ret = get_region(frba, i, ®ion);
451 dump_region(i, frba);
452 if (region.size == 0)
454 region_fd = open(region_filename(i),
455 O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
456 S_IWUSR | S_IRGRP | S_IROTH);
457 if (write(region_fd, image + region.base, region.size) !=
459 perror("Error while writing");
468 static int perror_fname(const char *fmt, const char *fname)
470 char msg[strlen(fmt) + strlen(fname) + 1];
472 sprintf(msg, fmt, fname);
479 * write_image() - Write the image to a file
481 * @filename: Filename to use for the image
482 * @image: Pointer to image
483 * @size: Size of image in bytes
484 * @return 0 if OK, -ve on error
486 static int write_image(char *filename, char *image, int size)
490 debug("Writing new image to %s\n", filename);
492 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
493 S_IWUSR | S_IRGRP | S_IROTH);
495 return perror_fname("Could not open file '%s'", filename);
496 if (write(new_fd, image, size) != size)
497 return perror_fname("Could not write file '%s'", filename);
504 * set_spi_frequency() - Set the SPI frequency to use when booting
506 * Several frequencies are supported, some of which work with fast devices.
507 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
508 * Intel boot system uses this information somehow on boot.
510 * The image is updated with the supplied value
512 * @image: Pointer to image
513 * @size: Size of image in bytes
514 * @freq: SPI frequency to use
516 static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
518 struct fdbar_t *fdb = find_fd(image, size);
521 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
523 /* clear bits 21-29 */
524 fcba->flcomp &= ~0x3fe00000;
525 /* Read ID and Read Status Clock Frequency */
526 fcba->flcomp |= freq << 27;
527 /* Write and Erase Clock Frequency */
528 fcba->flcomp |= freq << 24;
529 /* Fast Read Clock Frequency */
530 fcba->flcomp |= freq << 21;
534 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
536 * @image: Pointer to image
537 * @size: Size of image in bytes
539 static void set_em100_mode(char *image, int size)
541 struct fdbar_t *fdb = find_fd(image, size);
544 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
545 fcba->flcomp &= ~(1 << 30);
546 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
550 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
552 * @image: Pointer to image
553 * @size: Size of image in bytes
555 static void lock_descriptor(char *image, int size)
557 struct fdbar_t *fdb = find_fd(image, size);
561 * TODO: Dynamically take Platform Data Region and GbE Region into
564 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
565 fmba->flmstr1 = 0x0a0b0000;
566 fmba->flmstr2 = 0x0c0d0000;
567 fmba->flmstr3 = 0x08080118;
571 * unlock_descriptor() - Lock the NE descriptor so it can be updated
573 * @image: Pointer to image
574 * @size: Size of image in bytes
576 static void unlock_descriptor(char *image, int size)
578 struct fdbar_t *fdb = find_fd(image, size);
581 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
582 fmba->flmstr1 = 0xffff0000;
583 fmba->flmstr2 = 0xffff0000;
584 fmba->flmstr3 = 0x08080118;
588 * open_for_read() - Open a file for reading
590 * @fname: Filename to open
591 * @sizep: Returns file size in bytes
592 * @return 0 if OK, -1 on error
594 int open_for_read(const char *fname, int *sizep)
596 int fd = open(fname, O_RDONLY);
600 return perror_fname("Could not open file '%s'", fname);
601 if (fstat(fd, &buf) == -1)
602 return perror_fname("Could not stat file '%s'", fname);
603 *sizep = buf.st_size;
604 debug("File %s is %d bytes\n", fname, *sizep);
610 * inject_region() - Add a file to an image region
612 * This puts a file into a particular region of the flash. Several pre-defined
615 * @image: Pointer to image
616 * @size: Size of image in bytes
617 * @region_type: Region where the file should be added
618 * @region_fname: Filename to add to the image
619 * @return 0 if OK, -ve on error
621 int inject_region(char *image, int size, int region_type, char *region_fname)
623 struct fdbar_t *fdb = find_fd(image, size);
624 struct region_t region;
633 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
635 ret = get_region(frba, region_type, ®ion);
638 if (region.size <= 0xfff) {
639 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
640 region_name(region_type));
644 region_fd = open_for_read(region_fname, ®ion_size);
648 if ((region_size > region.size) ||
649 ((region_type != 1) && (region_size > region.size))) {
650 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
651 region_name(region_type), region.size,
652 region.size, region_size, region_size);
656 if ((region_type == 1) && (region_size < region.size)) {
657 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
658 region_name(region_type), region.size,
659 region.size, region_size, region_size);
660 offset = region.size - region_size;
661 memset(image + region.base, 0xff, offset);
664 if (size < region.base + offset + region_size) {
665 fprintf(stderr, "Output file is too small. (%d < %d)\n",
666 size, region.base + offset + region_size);
670 if (read(region_fd, image + region.base + offset, region_size)
672 perror("Could not read file");
678 debug("Adding %s as the %s section\n", region_fname,
679 region_name(region_type));
685 * write_data() - Write some raw data into a region
687 * This puts a file into a particular place in the flash, ignoring the
688 * regions. Be careful not to overwrite something important.
690 * @image: Pointer to image
691 * @size: Size of image in bytes
692 * @addr: x86 ROM address to put file. The ROM ends at
693 * 0xffffffff so use an address relative to that. For an
694 * 8MB ROM the start address is 0xfff80000.
695 * @write_fname: Filename to add to the image
696 * @return 0 if OK, -ve on error
698 static int write_data(char *image, int size, unsigned int addr,
699 const char *write_fname)
701 int write_fd, write_size;
704 write_fd = open_for_read(write_fname, &write_size);
708 offset = addr + size;
709 debug("Writing %s to offset %#x\n", write_fname, offset);
711 if (offset < 0 || offset + write_size > size) {
712 fprintf(stderr, "Output file is too small. (%d < %d)\n",
713 size, offset + write_size);
717 if (read(write_fd, image + offset, write_size) != write_size) {
718 perror("Could not read file");
727 static void print_version(void)
729 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
730 printf("Copyright (C) 2014 Google Inc.\n\n");
731 printf("SPDX-License-Identifier: GPL-2.0+\n");
734 static void print_usage(const char *name)
736 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
738 " -d | --dump: dump intel firmware descriptor\n"
739 " -x | --extract: extract intel fd modules\n"
740 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
741 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
742 " multiple files can be written simultaneously\n"
743 " -s | --spifreq <20|33|50> set the SPI frequency\n"
744 " -e | --em100 set SPI frequency to 20MHz and disable\n"
745 " Dual Output Fast Read Support\n"
746 " -l | --lock Lock firmware descriptor and ME region\n"
747 " -u | --unlock Unlock firmware descriptor and ME region\n"
748 " -r | --romsize Specify ROM size\n"
749 " -D | --write-descriptor <file> Write descriptor at base\n"
750 " -c | --create Create a new empty image\n"
751 " -v | --version: print the version\n"
752 " -h | --help: print this help\n\n"
753 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
758 * get_two_words() - Convert a string into two words separated by :
760 * The supplied string is split at ':', two substrings are allocated and
763 * @str: String to split
764 * @firstp: Returns first string
765 * @secondp: Returns second string
766 * @return 0 if OK, -ve if @str does not have a :
768 static int get_two_words(const char *str, char **firstp, char **secondp)
772 p = strchr(str, ':');
775 *firstp = strdup(str);
776 (*firstp)[p - str] = '\0';
777 *secondp = strdup(p + 1);
782 int main(int argc, char *argv[])
784 int opt, option_index = 0;
785 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
786 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
787 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
789 char *region_type_string = NULL, *inject_fname = NULL;
790 char *desc_fname = NULL, *addr_str = NULL;
791 int region_type = -1, inputfreq = 0;
792 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
793 unsigned int addr[WRITE_MAX];
794 char *wr_fname[WRITE_MAX];
795 unsigned char wr_idx, wr_num = 0;
799 char *outfile = NULL;
805 static struct option long_options[] = {
806 {"create", 0, NULL, 'c'},
807 {"dump", 0, NULL, 'd'},
808 {"descriptor", 1, NULL, 'D'},
809 {"em100", 0, NULL, 'e'},
810 {"extract", 0, NULL, 'x'},
811 {"inject", 1, NULL, 'i'},
812 {"lock", 0, NULL, 'l'},
813 {"romsize", 1, NULL, 'r'},
814 {"spifreq", 1, NULL, 's'},
815 {"unlock", 0, NULL, 'u'},
816 {"write", 1, NULL, 'w'},
817 {"version", 0, NULL, 'v'},
818 {"help", 0, NULL, 'h'},
822 while ((opt = getopt_long(argc, argv, "cdD:ehi:lr:s:uvw:x?",
823 long_options, &option_index)) != EOF) {
832 mode_write_descriptor = 1;
839 if (get_two_words(optarg, ®ion_type_string,
841 print_usage(argv[0]);
844 if (!strcasecmp("Descriptor", region_type_string))
846 else if (!strcasecmp("BIOS", region_type_string))
848 else if (!strcasecmp("ME", region_type_string))
850 else if (!strcasecmp("GbE", region_type_string))
852 else if (!strcasecmp("Platform", region_type_string))
854 if (region_type == -1) {
855 fprintf(stderr, "No such region type: '%s'\n\n",
857 print_usage(argv[0]);
866 rom_size = strtol(optarg, NULL, 0);
867 debug("ROM size %d\n", rom_size);
870 /* Parse the requested SPI frequency */
871 inputfreq = strtol(optarg, NULL, 0);
874 spifreq = SPI_FREQUENCY_20MHZ;
877 spifreq = SPI_FREQUENCY_33MHZ;
880 spifreq = SPI_FREQUENCY_50MHZ;
883 fprintf(stderr, "Invalid SPI Frequency: %d\n",
885 print_usage(argv[0]);
899 if (wr_num < WRITE_MAX) {
900 if (get_two_words(optarg, &addr_str,
901 &wr_fname[wr_num])) {
902 print_usage(argv[0]);
905 addr[wr_num] = strtol(optarg, NULL, 0);
909 "The number of files to write simultaneously exceeds the limitation (%d)\n",
919 print_usage(argv[0]);
925 if (mode_locked == 1 && mode_unlocked == 1) {
926 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
930 if (mode_inject == 1 && mode_write == 1) {
931 fprintf(stderr, "Inject/Write are mutually exclusive\n");
935 if ((mode_dump + mode_extract + mode_inject +
936 (mode_spifreq | mode_em100 | mode_unlocked |
938 fprintf(stderr, "You may not specify more than one mode.\n\n");
939 print_usage(argv[0]);
943 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
944 mode_em100 + mode_locked + mode_unlocked + mode_write +
945 mode_write_descriptor) == 0 && !create) {
946 fprintf(stderr, "You need to specify a mode.\n\n");
947 print_usage(argv[0]);
951 if (create && rom_size == -1) {
952 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
956 if (optind + 1 != argc) {
957 fprintf(stderr, "You need to specify a file.\n\n");
958 print_usage(argv[0]);
962 filename = argv[optind];
963 if (optind + 2 != argc)
964 outfile = argv[optind + 1];
967 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
969 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
972 perror("Could not open file");
977 if (fstat(bios_fd, &buf) == -1) {
978 perror("Could not stat file");
984 debug("File %s is %d bytes\n", filename, size);
989 image = malloc(rom_size);
991 printf("Out of memory.\n");
995 memset(image, '\xff', rom_size);
996 if (!create && read(bios_fd, image, size) != size) {
997 perror("Could not read file");
1000 if (size != rom_size) {
1001 debug("ROM size changed to %d bytes\n", rom_size);
1008 ret = dump_fd(image, size);
1013 ret = write_regions(image, size);
1017 if (mode_write_descriptor)
1018 ret = write_data(image, size, -size, desc_fname);
1021 ret = inject_region(image, size, region_type, inject_fname);
1024 for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
1025 ret = write_data(image, size,
1026 addr[wr_idx], wr_fname[wr_idx]);
1033 set_spi_frequency(image, size, spifreq);
1036 set_em100_mode(image, size);
1039 lock_descriptor(image, size);
1042 unlock_descriptor(image, size);
1046 ret = write_image(outfile, image, size);
1048 if (lseek(bios_fd, 0, SEEK_SET)) {
1049 perror("Error while seeking");
1052 if (write(bios_fd, image, size) != size) {
1053 perror("Error while writing");