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%08x\n", i);
59 return (struct fdbar_t *)ptr;
63 * get_region() - Get information about the selected region
65 * @frba: Flash region list
66 * @region_type: Type of region (0..MAX_REGIONS-1)
67 * @region: Region information is written here
68 * @return 0 if OK, else -ve
70 static int get_region(struct frba_t *frba, int region_type,
71 struct region_t *region)
73 if (region_type >= MAX_REGIONS) {
74 fprintf(stderr, "Invalid region type.\n");
78 region->base = FLREG_BASE(frba->flreg[region_type]);
79 region->limit = FLREG_LIMIT(frba->flreg[region_type]);
80 region->size = region->limit - region->base + 1;
85 static const char *region_name(int region_type)
87 static const char *const regions[] = {
95 assert(region_type < MAX_REGIONS);
97 return regions[region_type];
100 static const char *region_filename(int region_type)
102 static const char *const region_filenames[] = {
103 "flashregion_0_flashdescriptor.bin",
104 "flashregion_1_bios.bin",
105 "flashregion_2_intel_me.bin",
106 "flashregion_3_gbe.bin",
107 "flashregion_4_platform_data.bin"
110 assert(region_type < MAX_REGIONS);
112 return region_filenames[region_type];
115 static int dump_region(int num, struct frba_t *frba)
117 struct region_t region;
120 ret = get_region(frba, num, ®ion);
124 printf(" Flash Region %d (%s): %08x - %08x %s\n",
125 num, region_name(num), region.base, region.limit,
126 region.size < 1 ? "(unused)" : "");
131 static void dump_frba(struct frba_t *frba)
135 printf("Found Region Section\n");
136 for (i = 0; i < MAX_REGIONS; i++) {
137 printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
138 dump_region(i, frba);
142 static void decode_spi_frequency(unsigned int freq)
145 case SPI_FREQUENCY_20MHZ:
148 case SPI_FREQUENCY_33MHZ:
151 case SPI_FREQUENCY_50MHZ:
155 printf("unknown<%x>MHz", freq);
159 static void decode_component_density(unsigned int density)
162 case COMPONENT_DENSITY_512KB:
165 case COMPONENT_DENSITY_1MB:
168 case COMPONENT_DENSITY_2MB:
171 case COMPONENT_DENSITY_4MB:
174 case COMPONENT_DENSITY_8MB:
177 case COMPONENT_DENSITY_16MB:
181 printf("unknown<%x>MiB", density);
185 static void dump_fcba(struct fcba_t *fcba)
187 printf("\nFound Component Section\n");
188 printf("FLCOMP 0x%08x\n", fcba->flcomp);
189 printf(" Dual Output Fast Read Support: %ssupported\n",
190 (fcba->flcomp & (1 << 30)) ? "" : "not ");
191 printf(" Read ID/Read Status Clock Frequency: ");
192 decode_spi_frequency((fcba->flcomp >> 27) & 7);
193 printf("\n Write/Erase Clock Frequency: ");
194 decode_spi_frequency((fcba->flcomp >> 24) & 7);
195 printf("\n Fast Read Clock Frequency: ");
196 decode_spi_frequency((fcba->flcomp >> 21) & 7);
197 printf("\n Fast Read Support: %ssupported",
198 (fcba->flcomp & (1 << 20)) ? "" : "not ");
199 printf("\n Read Clock Frequency: ");
200 decode_spi_frequency((fcba->flcomp >> 17) & 7);
201 printf("\n Component 2 Density: ");
202 decode_component_density((fcba->flcomp >> 3) & 7);
203 printf("\n Component 1 Density: ");
204 decode_component_density(fcba->flcomp & 7);
206 printf("FLILL 0x%08x\n", fcba->flill);
207 printf(" Invalid Instruction 3: 0x%02x\n",
208 (fcba->flill >> 24) & 0xff);
209 printf(" Invalid Instruction 2: 0x%02x\n",
210 (fcba->flill >> 16) & 0xff);
211 printf(" Invalid Instruction 1: 0x%02x\n",
212 (fcba->flill >> 8) & 0xff);
213 printf(" Invalid Instruction 0: 0x%02x\n",
215 printf("FLPB 0x%08x\n", fcba->flpb);
216 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
217 (fcba->flpb & 0xfff) << 12);
220 static void dump_fpsba(struct fpsba_t *fpsba)
224 printf("Found PCH Strap Section\n");
225 for (i = 0; i < MAX_STRAPS; i++)
226 printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
229 static const char *get_enabled(int flag)
231 return flag ? "enabled" : "disabled";
234 static void decode_flmstr(uint32_t flmstr)
236 printf(" Platform Data Region Write Access: %s\n",
237 get_enabled(flmstr & (1 << 28)));
238 printf(" GbE Region Write Access: %s\n",
239 get_enabled(flmstr & (1 << 27)));
240 printf(" Intel ME Region Write Access: %s\n",
241 get_enabled(flmstr & (1 << 26)));
242 printf(" Host CPU/BIOS Region Write Access: %s\n",
243 get_enabled(flmstr & (1 << 25)));
244 printf(" Flash Descriptor Write Access: %s\n",
245 get_enabled(flmstr & (1 << 24)));
247 printf(" Platform Data Region Read Access: %s\n",
248 get_enabled(flmstr & (1 << 20)));
249 printf(" GbE Region Read Access: %s\n",
250 get_enabled(flmstr & (1 << 19)));
251 printf(" Intel ME Region Read Access: %s\n",
252 get_enabled(flmstr & (1 << 18)));
253 printf(" Host CPU/BIOS Region Read Access: %s\n",
254 get_enabled(flmstr & (1 << 17)));
255 printf(" Flash Descriptor Read Access: %s\n",
256 get_enabled(flmstr & (1 << 16)));
258 printf(" Requester ID: 0x%04x\n\n",
262 static void dump_fmba(struct fmba_t *fmba)
264 printf("Found Master Section\n");
265 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
266 decode_flmstr(fmba->flmstr1);
267 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
268 decode_flmstr(fmba->flmstr2);
269 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
270 decode_flmstr(fmba->flmstr3);
273 static void dump_fmsba(struct fmsba_t *fmsba)
277 printf("Found Processor Strap Section\n");
278 for (i = 0; i < 4; i++)
279 printf("????: 0x%08x\n", fmsba->data[0]);
282 static void dump_jid(uint32_t jid)
284 printf(" SPI Component Device ID 1: 0x%02x\n",
286 printf(" SPI Component Device ID 0: 0x%02x\n",
288 printf(" SPI Component Vendor ID: 0x%02x\n",
292 static void dump_vscc(uint32_t vscc)
294 printf(" Lower Erase Opcode: 0x%02x\n",
296 printf(" Lower Write Enable on Write Status: 0x%02x\n",
297 vscc & (1 << 20) ? 0x06 : 0x50);
298 printf(" Lower Write Status Required: %s\n",
299 vscc & (1 << 19) ? "Yes" : "No");
300 printf(" Lower Write Granularity: %d bytes\n",
301 vscc & (1 << 18) ? 64 : 1);
302 printf(" Lower Block / Sector Erase Size: ");
303 switch ((vscc >> 16) & 0x3) {
305 printf("256 Byte\n");
318 printf(" Upper Erase Opcode: 0x%02x\n",
320 printf(" Upper Write Enable on Write Status: 0x%02x\n",
321 vscc & (1 << 4) ? 0x06 : 0x50);
322 printf(" Upper Write Status Required: %s\n",
323 vscc & (1 << 3) ? "Yes" : "No");
324 printf(" Upper Write Granularity: %d bytes\n",
325 vscc & (1 << 2) ? 64 : 1);
326 printf(" Upper Block / Sector Erase Size: ");
327 switch (vscc & 0x3) {
329 printf("256 Byte\n");
343 static void dump_vtba(struct vtba_t *vtba, int vtl)
346 int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
348 printf("ME VSCC table:\n");
349 for (i = 0; i < num; i++) {
350 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
351 dump_jid(vtba->entry[i].jid);
352 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
353 dump_vscc(vtba->entry[i].vscc);
358 static void dump_oem(uint8_t *oem)
361 printf("OEM Section:\n");
362 for (i = 0; i < 4; i++) {
363 printf("%02x:", i << 4);
364 for (j = 0; j < 16; j++)
365 printf(" %02x", oem[(i<<4)+j]);
372 * dump_fd() - Display a dump of the full flash description
374 * @image: Pointer to image
375 * @size: Size of image in bytes
376 * @return 0 if OK, -1 on error
378 static int dump_fd(char *image, int size)
380 struct fdbar_t *fdb = find_fd(image, size);
385 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
386 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
387 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
388 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
389 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
391 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
392 printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
393 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
394 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
395 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
397 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
398 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
399 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
401 printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
402 printf(" Intel ME VSCC Table Length (VTL): %d\n",
403 (fdb->flumap1 >> 8) & 0xff);
404 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
405 (fdb->flumap1 & 0xff) << 4);
406 dump_vtba((struct vtba_t *)
407 (image + ((fdb->flumap1 & 0xff) << 4)),
408 (fdb->flumap1 >> 8) & 0xff);
409 dump_oem((uint8_t *)image + 0xf00);
410 dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
412 dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
413 dump_fpsba((struct fpsba_t *)
414 (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
415 dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
416 dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
422 * write_regions() - Write each region from an image to its own file
424 * The filename to use in each case is fixed - see region_filename()
426 * @image: Pointer to image
427 * @size: Size of image in bytes
428 * @return 0 if OK, -ve on error
430 static int write_regions(char *image, int size)
437 fdb = find_fd(image, size);
441 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
443 for (i = 0; i < MAX_REGIONS; i++) {
444 struct region_t region;
447 ret = get_region(frba, i, ®ion);
450 dump_region(i, frba);
451 if (region.size == 0)
453 region_fd = open(region_filename(i),
454 O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
455 S_IWUSR | S_IRGRP | S_IROTH);
456 if (write(region_fd, image + region.base, region.size) !=
458 perror("Error while writing");
468 * write_image() - Write the image to a file
470 * @filename: Filename to use for the image
471 * @image: Pointer to image
472 * @size: Size of image in bytes
473 * @return 0 if OK, -ve on error
475 static int write_image(char *filename, char *image, int size)
479 debug("Writing new image to %s\n", filename);
481 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
482 S_IWUSR | S_IRGRP | S_IROTH);
483 if (write(new_fd, image, size) != size) {
484 perror("Error while writing");
493 * set_spi_frequency() - Set the SPI frequency to use when booting
495 * Several frequencies are supported, some of which work with fast devices.
496 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
497 * Intel boot system uses this information somehow on boot.
499 * The image is updated with the supplied value
501 * @image: Pointer to image
502 * @size: Size of image in bytes
503 * @freq: SPI frequency to use
505 static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
507 struct fdbar_t *fdb = find_fd(image, size);
510 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
512 /* clear bits 21-29 */
513 fcba->flcomp &= ~0x3fe00000;
514 /* Read ID and Read Status Clock Frequency */
515 fcba->flcomp |= freq << 27;
516 /* Write and Erase Clock Frequency */
517 fcba->flcomp |= freq << 24;
518 /* Fast Read Clock Frequency */
519 fcba->flcomp |= freq << 21;
523 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
525 * @image: Pointer to image
526 * @size: Size of image in bytes
528 static void set_em100_mode(char *image, int size)
530 struct fdbar_t *fdb = find_fd(image, size);
533 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
534 fcba->flcomp &= ~(1 << 30);
535 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
539 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
541 * @image: Pointer to image
542 * @size: Size of image in bytes
544 static void lock_descriptor(char *image, int size)
546 struct fdbar_t *fdb = find_fd(image, size);
550 * TODO: Dynamically take Platform Data Region and GbE Region into
553 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
554 fmba->flmstr1 = 0x0a0b0000;
555 fmba->flmstr2 = 0x0c0d0000;
556 fmba->flmstr3 = 0x08080118;
560 * unlock_descriptor() - Lock the NE descriptor so it can be updated
562 * @image: Pointer to image
563 * @size: Size of image in bytes
565 static void unlock_descriptor(char *image, int size)
567 struct fdbar_t *fdb = find_fd(image, size);
570 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
571 fmba->flmstr1 = 0xffff0000;
572 fmba->flmstr2 = 0xffff0000;
573 fmba->flmstr3 = 0x08080118;
577 * open_for_read() - Open a file for reading
579 * @fname: Filename to open
580 * @sizep: Returns file size in bytes
581 * @return 0 if OK, -1 on error
583 int open_for_read(const char *fname, int *sizep)
585 int fd = open(fname, O_RDONLY);
589 perror("Could not open file");
592 if (fstat(fd, &buf) == -1) {
593 perror("Could not stat file");
596 *sizep = buf.st_size;
597 debug("File %s is %d bytes\n", fname, *sizep);
603 * inject_region() - Add a file to an image region
605 * This puts a file into a particular region of the flash. Several pre-defined
608 * @image: Pointer to image
609 * @size: Size of image in bytes
610 * @region_type: Region where the file should be added
611 * @region_fname: Filename to add to the image
612 * @return 0 if OK, -ve on error
614 int inject_region(char *image, int size, int region_type, char *region_fname)
616 struct fdbar_t *fdb = find_fd(image, size);
617 struct region_t region;
626 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
628 ret = get_region(frba, region_type, ®ion);
631 if (region.size <= 0xfff) {
632 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
633 region_name(region_type));
637 region_fd = open_for_read(region_fname, ®ion_size);
641 if ((region_size > region.size) ||
642 ((region_type != 1) && (region_size > region.size))) {
643 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
644 region_name(region_type), region.size,
645 region.size, region_size, region_size);
649 if ((region_type == 1) && (region_size < region.size)) {
650 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
651 region_name(region_type), region.size,
652 region.size, region_size, region_size);
653 offset = region.size - region_size;
654 memset(image + region.base, 0xff, offset);
657 if (size < region.base + offset + region_size) {
658 fprintf(stderr, "Output file is too small. (%d < %d)\n",
659 size, region.base + offset + region_size);
663 if (read(region_fd, image + region.base + offset, region_size)
665 perror("Could not read file");
671 debug("Adding %s as the %s section\n", region_fname,
672 region_name(region_type));
678 * write_data() - Write some raw data into a region
680 * This puts a file into a particular place in the flash, ignoring the
681 * regions. Be careful not to overwrite something important.
683 * @image: Pointer to image
684 * @size: Size of image in bytes
685 * @addr: x86 ROM address to put file. The ROM ends at
686 * 0xffffffff so use an address relative to that. For an
687 * 8MB ROM the start address is 0xfff80000.
688 * @write_fname: Filename to add to the image
689 * @return 0 if OK, -ve on error
691 static int write_data(char *image, int size, unsigned int addr,
692 const char *write_fname)
694 int write_fd, write_size;
697 write_fd = open_for_read(write_fname, &write_size);
701 offset = addr + size;
702 debug("Writing %s to offset %#x\n", write_fname, offset);
704 if (offset < 0 || offset + write_size > size) {
705 fprintf(stderr, "Output file is too small. (%d < %d)\n",
706 size, offset + write_size);
710 if (read(write_fd, image + offset, write_size) != write_size) {
711 perror("Could not read file");
720 static void print_version(void)
722 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
723 printf("Copyright (C) 2014 Google Inc.\n\n");
724 printf("SPDX-License-Identifier: GPL-2.0+\n");
727 static void print_usage(const char *name)
729 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
731 " -d | --dump: dump intel firmware descriptor\n"
732 " -x | --extract: extract intel fd modules\n"
733 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
734 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
735 " -s | --spifreq <20|33|50> set the SPI frequency\n"
736 " -e | --em100 set SPI frequency to 20MHz and disable\n"
737 " Dual Output Fast Read Support\n"
738 " -l | --lock Lock firmware descriptor and ME region\n"
739 " -u | --unlock Unlock firmware descriptor and ME region\n"
740 " -r | --romsize Specify ROM size\n"
741 " -D | --write-descriptor <file> Write descriptor at base\n"
742 " -c | --create Create a new empty image\n"
743 " -v | --version: print the version\n"
744 " -h | --help: print this help\n\n"
745 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
750 * get_two_words() - Convert a string into two words separated by :
752 * The supplied string is split at ':', two substrings are allocated and
755 * @str: String to split
756 * @firstp: Returns first string
757 * @secondp: Returns second string
758 * @return 0 if OK, -ve if @str does not have a :
760 static int get_two_words(const char *str, char **firstp, char **secondp)
764 p = strchr(str, ':');
767 *firstp = strdup(str);
768 (*firstp)[p - str] = '\0';
769 *secondp = strdup(p + 1);
774 int main(int argc, char *argv[])
776 int opt, option_index = 0;
777 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
778 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
779 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
781 char *region_type_string = NULL, *src_fname = NULL;
782 char *addr_str = NULL;
783 int region_type = -1, inputfreq = 0;
784 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
785 unsigned int addr = 0;
789 char *outfile = NULL;
795 static struct option long_options[] = {
796 {"create", 0, NULL, 'c'},
797 {"dump", 0, NULL, 'd'},
798 {"descriptor", 1, NULL, 'D'},
799 {"em100", 0, NULL, 'e'},
800 {"extract", 0, NULL, 'x'},
801 {"inject", 1, NULL, 'i'},
802 {"lock", 0, NULL, 'l'},
803 {"romsize", 1, NULL, 'r'},
804 {"spifreq", 1, NULL, 's'},
805 {"unlock", 0, NULL, 'u'},
806 {"write", 1, NULL, 'w'},
807 {"version", 0, NULL, 'v'},
808 {"help", 0, NULL, 'h'},
812 while ((opt = getopt_long(argc, argv, "cdD:ehi:lr:s:uvw:x?",
813 long_options, &option_index)) != EOF) {
822 mode_write_descriptor = 1;
829 if (get_two_words(optarg, ®ion_type_string,
831 print_usage(argv[0]);
834 if (!strcasecmp("Descriptor", region_type_string))
836 else if (!strcasecmp("BIOS", region_type_string))
838 else if (!strcasecmp("ME", region_type_string))
840 else if (!strcasecmp("GbE", region_type_string))
842 else if (!strcasecmp("Platform", region_type_string))
844 if (region_type == -1) {
845 fprintf(stderr, "No such region type: '%s'\n\n",
847 print_usage(argv[0]);
856 rom_size = strtol(optarg, NULL, 0);
857 debug("ROM size %d\n", rom_size);
860 /* Parse the requested SPI frequency */
861 inputfreq = strtol(optarg, NULL, 0);
864 spifreq = SPI_FREQUENCY_20MHZ;
867 spifreq = SPI_FREQUENCY_33MHZ;
870 spifreq = SPI_FREQUENCY_50MHZ;
873 fprintf(stderr, "Invalid SPI Frequency: %d\n",
875 print_usage(argv[0]);
889 if (get_two_words(optarg, &addr_str, &src_fname)) {
890 print_usage(argv[0]);
893 addr = strtol(optarg, NULL, 0);
901 print_usage(argv[0]);
907 if (mode_locked == 1 && mode_unlocked == 1) {
908 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
912 if (mode_inject == 1 && mode_write == 1) {
913 fprintf(stderr, "Inject/Write are mutually exclusive\n");
917 if ((mode_dump + mode_extract + mode_inject +
918 (mode_spifreq | mode_em100 | mode_unlocked |
920 fprintf(stderr, "You may not specify more than one mode.\n\n");
921 print_usage(argv[0]);
925 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
926 mode_em100 + mode_locked + mode_unlocked + mode_write +
927 mode_write_descriptor) == 0 && !create) {
928 fprintf(stderr, "You need to specify a mode.\n\n");
929 print_usage(argv[0]);
933 if (create && rom_size == -1) {
934 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
938 if (optind + 1 != argc) {
939 fprintf(stderr, "You need to specify a file.\n\n");
940 print_usage(argv[0]);
944 filename = argv[optind];
945 if (optind + 2 != argc)
946 outfile = argv[optind + 1];
949 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
951 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
954 perror("Could not open file");
959 if (fstat(bios_fd, &buf) == -1) {
960 perror("Could not stat file");
966 debug("File %s is %d bytes\n", filename, size);
971 image = malloc(rom_size);
973 printf("Out of memory.\n");
977 memset(image, '\xff', rom_size);
978 if (!create && read(bios_fd, image, size) != size) {
979 perror("Could not read file");
982 if (size != rom_size) {
983 debug("ROM size changed to %d bytes\n", rom_size);
990 ret = dump_fd(image, size);
995 ret = write_regions(image, size);
999 if (mode_write_descriptor)
1000 ret = write_data(image, size, -size, src_fname);
1003 ret = inject_region(image, size, region_type, src_fname);
1006 ret = write_data(image, size, addr, src_fname);
1009 set_spi_frequency(image, size, spifreq);
1012 set_em100_mode(image, size);
1015 lock_descriptor(image, size);
1018 unlock_descriptor(image, size);
1022 ret = write_image(outfile, image, size);
1024 if (lseek(bios_fd, 0, SEEK_SET)) {
1025 perror("Error while seeking");
1028 if (write(bios_fd, image, size) != size) {
1029 perror("Error while writing");