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");
469 * write_image() - Write the image to a file
471 * @filename: Filename to use for the image
472 * @image: Pointer to image
473 * @size: Size of image in bytes
474 * @return 0 if OK, -ve on error
476 static int write_image(char *filename, char *image, int size)
480 debug("Writing new image to %s\n", filename);
482 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
483 S_IWUSR | S_IRGRP | S_IROTH);
484 if (write(new_fd, image, size) != size) {
485 perror("Error while writing");
494 * set_spi_frequency() - Set the SPI frequency to use when booting
496 * Several frequencies are supported, some of which work with fast devices.
497 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
498 * Intel boot system uses this information somehow on boot.
500 * The image is updated with the supplied value
502 * @image: Pointer to image
503 * @size: Size of image in bytes
504 * @freq: SPI frequency to use
506 static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
508 struct fdbar_t *fdb = find_fd(image, size);
511 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
513 /* clear bits 21-29 */
514 fcba->flcomp &= ~0x3fe00000;
515 /* Read ID and Read Status Clock Frequency */
516 fcba->flcomp |= freq << 27;
517 /* Write and Erase Clock Frequency */
518 fcba->flcomp |= freq << 24;
519 /* Fast Read Clock Frequency */
520 fcba->flcomp |= freq << 21;
524 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
526 * @image: Pointer to image
527 * @size: Size of image in bytes
529 static void set_em100_mode(char *image, int size)
531 struct fdbar_t *fdb = find_fd(image, size);
534 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
535 fcba->flcomp &= ~(1 << 30);
536 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
540 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
542 * @image: Pointer to image
543 * @size: Size of image in bytes
545 static void lock_descriptor(char *image, int size)
547 struct fdbar_t *fdb = find_fd(image, size);
551 * TODO: Dynamically take Platform Data Region and GbE Region into
554 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
555 fmba->flmstr1 = 0x0a0b0000;
556 fmba->flmstr2 = 0x0c0d0000;
557 fmba->flmstr3 = 0x08080118;
561 * unlock_descriptor() - Lock the NE descriptor so it can be updated
563 * @image: Pointer to image
564 * @size: Size of image in bytes
566 static void unlock_descriptor(char *image, int size)
568 struct fdbar_t *fdb = find_fd(image, size);
571 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
572 fmba->flmstr1 = 0xffff0000;
573 fmba->flmstr2 = 0xffff0000;
574 fmba->flmstr3 = 0x08080118;
578 * open_for_read() - Open a file for reading
580 * @fname: Filename to open
581 * @sizep: Returns file size in bytes
582 * @return 0 if OK, -1 on error
584 int open_for_read(const char *fname, int *sizep)
586 int fd = open(fname, O_RDONLY);
590 perror("Could not open file");
593 if (fstat(fd, &buf) == -1) {
594 perror("Could not stat file");
597 *sizep = buf.st_size;
598 debug("File %s is %d bytes\n", fname, *sizep);
604 * inject_region() - Add a file to an image region
606 * This puts a file into a particular region of the flash. Several pre-defined
609 * @image: Pointer to image
610 * @size: Size of image in bytes
611 * @region_type: Region where the file should be added
612 * @region_fname: Filename to add to the image
613 * @return 0 if OK, -ve on error
615 int inject_region(char *image, int size, int region_type, char *region_fname)
617 struct fdbar_t *fdb = find_fd(image, size);
618 struct region_t region;
627 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
629 ret = get_region(frba, region_type, ®ion);
632 if (region.size <= 0xfff) {
633 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
634 region_name(region_type));
638 region_fd = open_for_read(region_fname, ®ion_size);
642 if ((region_size > region.size) ||
643 ((region_type != 1) && (region_size > region.size))) {
644 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
645 region_name(region_type), region.size,
646 region.size, region_size, region_size);
650 if ((region_type == 1) && (region_size < region.size)) {
651 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
652 region_name(region_type), region.size,
653 region.size, region_size, region_size);
654 offset = region.size - region_size;
655 memset(image + region.base, 0xff, offset);
658 if (size < region.base + offset + region_size) {
659 fprintf(stderr, "Output file is too small. (%d < %d)\n",
660 size, region.base + offset + region_size);
664 if (read(region_fd, image + region.base + offset, region_size)
666 perror("Could not read file");
672 debug("Adding %s as the %s section\n", region_fname,
673 region_name(region_type));
679 * write_data() - Write some raw data into a region
681 * This puts a file into a particular place in the flash, ignoring the
682 * regions. Be careful not to overwrite something important.
684 * @image: Pointer to image
685 * @size: Size of image in bytes
686 * @addr: x86 ROM address to put file. The ROM ends at
687 * 0xffffffff so use an address relative to that. For an
688 * 8MB ROM the start address is 0xfff80000.
689 * @write_fname: Filename to add to the image
690 * @return 0 if OK, -ve on error
692 static int write_data(char *image, int size, unsigned int addr,
693 const char *write_fname)
695 int write_fd, write_size;
698 write_fd = open_for_read(write_fname, &write_size);
702 offset = addr + size;
703 debug("Writing %s to offset %#x\n", write_fname, offset);
705 if (offset < 0 || offset + write_size > size) {
706 fprintf(stderr, "Output file is too small. (%d < %d)\n",
707 size, offset + write_size);
711 if (read(write_fd, image + offset, write_size) != write_size) {
712 perror("Could not read file");
721 static void print_version(void)
723 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
724 printf("Copyright (C) 2014 Google Inc.\n\n");
725 printf("SPDX-License-Identifier: GPL-2.0+\n");
728 static void print_usage(const char *name)
730 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
732 " -d | --dump: dump intel firmware descriptor\n"
733 " -x | --extract: extract intel fd modules\n"
734 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
735 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
736 " multiple files can be written simultaneously\n"
737 " -s | --spifreq <20|33|50> set the SPI frequency\n"
738 " -e | --em100 set SPI frequency to 20MHz and disable\n"
739 " Dual Output Fast Read Support\n"
740 " -l | --lock Lock firmware descriptor and ME region\n"
741 " -u | --unlock Unlock firmware descriptor and ME region\n"
742 " -r | --romsize Specify ROM size\n"
743 " -D | --write-descriptor <file> Write descriptor at base\n"
744 " -c | --create Create a new empty image\n"
745 " -v | --version: print the version\n"
746 " -h | --help: print this help\n\n"
747 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
752 * get_two_words() - Convert a string into two words separated by :
754 * The supplied string is split at ':', two substrings are allocated and
757 * @str: String to split
758 * @firstp: Returns first string
759 * @secondp: Returns second string
760 * @return 0 if OK, -ve if @str does not have a :
762 static int get_two_words(const char *str, char **firstp, char **secondp)
766 p = strchr(str, ':');
769 *firstp = strdup(str);
770 (*firstp)[p - str] = '\0';
771 *secondp = strdup(p + 1);
776 int main(int argc, char *argv[])
778 int opt, option_index = 0;
779 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
780 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
781 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
783 char *region_type_string = NULL, *inject_fname = NULL;
784 char *desc_fname = NULL, *addr_str = NULL;
785 int region_type = -1, inputfreq = 0;
786 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
787 unsigned int addr[WRITE_MAX];
788 char *wr_fname[WRITE_MAX];
789 unsigned char wr_idx, wr_num = 0;
793 char *outfile = NULL;
799 static struct option long_options[] = {
800 {"create", 0, NULL, 'c'},
801 {"dump", 0, NULL, 'd'},
802 {"descriptor", 1, NULL, 'D'},
803 {"em100", 0, NULL, 'e'},
804 {"extract", 0, NULL, 'x'},
805 {"inject", 1, NULL, 'i'},
806 {"lock", 0, NULL, 'l'},
807 {"romsize", 1, NULL, 'r'},
808 {"spifreq", 1, NULL, 's'},
809 {"unlock", 0, NULL, 'u'},
810 {"write", 1, NULL, 'w'},
811 {"version", 0, NULL, 'v'},
812 {"help", 0, NULL, 'h'},
816 while ((opt = getopt_long(argc, argv, "cdD:ehi:lr:s:uvw:x?",
817 long_options, &option_index)) != EOF) {
826 mode_write_descriptor = 1;
833 if (get_two_words(optarg, ®ion_type_string,
835 print_usage(argv[0]);
838 if (!strcasecmp("Descriptor", region_type_string))
840 else if (!strcasecmp("BIOS", region_type_string))
842 else if (!strcasecmp("ME", region_type_string))
844 else if (!strcasecmp("GbE", region_type_string))
846 else if (!strcasecmp("Platform", region_type_string))
848 if (region_type == -1) {
849 fprintf(stderr, "No such region type: '%s'\n\n",
851 print_usage(argv[0]);
860 rom_size = strtol(optarg, NULL, 0);
861 debug("ROM size %d\n", rom_size);
864 /* Parse the requested SPI frequency */
865 inputfreq = strtol(optarg, NULL, 0);
868 spifreq = SPI_FREQUENCY_20MHZ;
871 spifreq = SPI_FREQUENCY_33MHZ;
874 spifreq = SPI_FREQUENCY_50MHZ;
877 fprintf(stderr, "Invalid SPI Frequency: %d\n",
879 print_usage(argv[0]);
893 if (wr_num < WRITE_MAX) {
894 if (get_two_words(optarg, &addr_str,
895 &wr_fname[wr_num])) {
896 print_usage(argv[0]);
899 addr[wr_num] = strtol(optarg, NULL, 0);
903 "The number of files to write simultaneously exceeds the limitation (%d)\n",
913 print_usage(argv[0]);
919 if (mode_locked == 1 && mode_unlocked == 1) {
920 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
924 if (mode_inject == 1 && mode_write == 1) {
925 fprintf(stderr, "Inject/Write are mutually exclusive\n");
929 if ((mode_dump + mode_extract + mode_inject +
930 (mode_spifreq | mode_em100 | mode_unlocked |
932 fprintf(stderr, "You may not specify more than one mode.\n\n");
933 print_usage(argv[0]);
937 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
938 mode_em100 + mode_locked + mode_unlocked + mode_write +
939 mode_write_descriptor) == 0 && !create) {
940 fprintf(stderr, "You need to specify a mode.\n\n");
941 print_usage(argv[0]);
945 if (create && rom_size == -1) {
946 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
950 if (optind + 1 != argc) {
951 fprintf(stderr, "You need to specify a file.\n\n");
952 print_usage(argv[0]);
956 filename = argv[optind];
957 if (optind + 2 != argc)
958 outfile = argv[optind + 1];
961 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
963 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
966 perror("Could not open file");
971 if (fstat(bios_fd, &buf) == -1) {
972 perror("Could not stat file");
978 debug("File %s is %d bytes\n", filename, size);
983 image = malloc(rom_size);
985 printf("Out of memory.\n");
989 memset(image, '\xff', rom_size);
990 if (!create && read(bios_fd, image, size) != size) {
991 perror("Could not read file");
994 if (size != rom_size) {
995 debug("ROM size changed to %d bytes\n", rom_size);
1002 ret = dump_fd(image, size);
1007 ret = write_regions(image, size);
1011 if (mode_write_descriptor)
1012 ret = write_data(image, size, -size, desc_fname);
1015 ret = inject_region(image, size, region_type, inject_fname);
1018 for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
1019 ret = write_data(image, size,
1020 addr[wr_idx], wr_fname[wr_idx]);
1027 set_spi_frequency(image, size, spifreq);
1030 set_em100_mode(image, size);
1033 lock_descriptor(image, size);
1036 unlock_descriptor(image, size);
1040 ret = write_image(outfile, image, size);
1042 if (lseek(bios_fd, 0, SEEK_SET)) {
1043 perror("Error while seeking");
1046 if (write(bios_fd, image, size) != size) {
1047 perror("Error while writing");