]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/ifdtool.c
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 * @offset_uboot_top: Offset of the top of U-Boot
710 * @offset_uboot_start: Offset of the start of U-Boot
711 * @return number of bytes written if OK, -ve on error
713 static int write_data(char *image
, int size
, unsigned int addr
,
714 const char *write_fname
, int offset_uboot_top
,
715 int offset_uboot_start
)
717 int write_fd
, write_size
;
720 write_fd
= open_for_read(write_fname
, &write_size
);
724 offset
= (uint32_t)(addr
+ size
);
725 if (offset_uboot_top
) {
726 if (offset_uboot_start
< offset
&&
727 offset_uboot_top
>= offset
) {
728 fprintf(stderr
, "U-Boot image overlaps with region '%s'\n",
731 "U-Boot finishes at offset %x, file starts at %x\n",
732 offset_uboot_top
, offset
);
735 if (offset_uboot_start
> offset
&&
736 offset_uboot_start
<= offset
+ write_size
) {
737 fprintf(stderr
, "U-Boot image overlaps with region '%s'\n",
740 "U-Boot starts at offset %x, file finishes at %x\n",
741 offset_uboot_start
, offset
+ write_size
);
745 debug("Writing %s to offset %#x\n", write_fname
, offset
);
747 if (offset
< 0 || offset
+ write_size
> size
) {
748 fprintf(stderr
, "Output file is too small. (%d < %d)\n",
749 size
, offset
+ write_size
);
753 if (read(write_fd
, image
+ offset
, write_size
) != write_size
) {
754 perror("Could not read file");
763 static int scan_ucode(const void *blob
, char *ucode_base
, int *countp
,
764 const char **datap
, int *data_sizep
)
766 const char *data
= NULL
;
771 for (node
= 0, count
= 0, ucode
= ucode_base
; node
>= 0; count
++) {
772 node
= fdt_node_offset_by_compatible(blob
, node
,
777 data
= fdt_getprop(blob
, node
, "data", &data_size
);
779 debug("Missing microcode data in FDT '%s': %s\n",
780 fdt_get_name(blob
, node
, NULL
),
781 fdt_strerror(data_size
));
786 memcpy(ucode
, data
, data_size
);
795 *data_sizep
= data_size
;
797 return ucode
- ucode_base
;
800 static int remove_ucode(char *blob
)
805 /* Keep going until we find no more microcode to remove */
807 for (node
= 0, count
= 0; node
>= 0;) {
810 node
= fdt_node_offset_by_compatible(blob
, node
,
815 ret
= fdt_delprop(blob
, node
, "data");
818 * -FDT_ERR_NOTFOUND means we already removed the
819 * data for this one, so we just continue.
820 * 0 means we did remove it, so offsets may have
821 * changed and we need to restart our scan.
822 * Anything else indicates an error we should report.
824 if (ret
== -FDT_ERR_NOTFOUND
)
833 /* Pack down to remove excees space */
834 ret
= fdt_pack(blob
);
838 return fdt_totalsize(blob
);
841 static int write_ucode(char *image
, int size
, struct input_file
*fdt
,
842 int fdt_size
, unsigned int ucode_ptr
,
845 const char *data
= NULL
;
856 blob
= (void *)image
+ (uint32_t)(fdt
->addr
+ size
);
858 debug("DTB at %lx\n", (char *)blob
- image
);
860 /* Find out about the micrcode we have */
861 ucode_size
= scan_ucode(blob
, NULL
, &count
, &data
, &data_size
);
865 debug("No microcode found in FDT\n");
869 if (count
> 1 && !collate_ucode
) {
871 "Cannot handle multiple microcode blocks - please use -C flag to collate them\n");
876 * Collect the microcode into a buffer, remove it from the device
877 * tree and place it immediately above the (now smaller) device tree.
879 if (collate_ucode
&& count
> 1) {
880 ucode_buf
= malloc(ucode_size
);
883 "Out of memory for microcode (%d bytes)\n",
887 ret
= scan_ucode(blob
, ucode_buf
, NULL
, NULL
, NULL
);
891 /* Remove the microcode from the device tree */
892 ret
= remove_ucode((char *)blob
);
894 debug("Could not remove FDT microcode: %s\n",
898 debug("Collated %d microcode block(s)\n", count
);
899 debug("Device tree reduced from %x to %x bytes\n",
904 * Place microcode area immediately above the FDT, aligned
905 * to a 16-byte boundary.
907 ucode_base
= (char *)(((unsigned long)blob
+ fdt_size
+ 15) &
911 data_size
= ucode_size
;
912 memcpy(ucode_base
, ucode_buf
, ucode_size
);
916 offset
= (uint32_t)(ucode_ptr
+ size
);
917 ptr
= (void *)image
+ offset
;
919 ptr
[0] = (data
- image
) - size
;
921 debug("Wrote microcode pointer at %x: addr=%x, size=%x\n", ucode_ptr
,
924 return (collate_ucode
? data
+ data_size
: (char *)blob
+ fdt_size
) -
929 * write_uboot() - Write U-Boot, device tree and microcode pointer
931 * This writes U-Boot into a place in the flash, followed by its device tree.
932 * The microcode pointer is written so that U-Boot can find the microcode in
933 * the device tree very early in boot.
935 * @image: Pointer to image
936 * @size: Size of image in bytes
937 * @uboot: Input file information for u-boot.bin
938 * @fdt: Input file information for u-boot.dtb
939 * @ucode_ptr: Address in U-Boot where the microcode pointer should be placed
940 * @return 0 if OK, -ve on error
942 static int write_uboot(char *image
, int size
, struct input_file
*uboot
,
943 struct input_file
*fdt
, unsigned int ucode_ptr
,
944 int collate_ucode
, int *offset_uboot_top
,
945 int *offset_uboot_start
)
947 int uboot_size
, fdt_size
;
950 uboot_size
= write_data(image
, size
, uboot
->addr
, uboot
->fname
, 0, 0);
953 fdt
->addr
= uboot
->addr
+ uboot_size
;
954 debug("U-Boot size %#x, FDT at %#x\n", uboot_size
, fdt
->addr
);
955 fdt_size
= write_data(image
, size
, fdt
->addr
, fdt
->fname
, 0, 0);
959 uboot_top
= (uint32_t)(fdt
->addr
+ size
) + fdt_size
;
962 uboot_top
= write_ucode(image
, size
, fdt
, fdt_size
, ucode_ptr
,
968 if (offset_uboot_top
&& offset_uboot_start
) {
969 *offset_uboot_top
= uboot_top
;
970 *offset_uboot_start
= (uint32_t)(uboot
->addr
+ size
);
976 static void print_version(void)
978 printf("ifdtool v%s -- ", IFDTOOL_VERSION
);
979 printf("Copyright (C) 2014 Google Inc.\n\n");
980 printf("SPDX-License-Identifier: GPL-2.0+\n");
983 static void print_usage(const char *name
)
985 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name
);
987 " -d | --dump: dump intel firmware descriptor\n"
988 " -x | --extract: extract intel fd modules\n"
989 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
990 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
991 " multiple files can be written simultaneously\n"
992 " -s | --spifreq <20|33|50> set the SPI frequency\n"
993 " -e | --em100 set SPI frequency to 20MHz and disable\n"
994 " Dual Output Fast Read Support\n"
995 " -l | --lock Lock firmware descriptor and ME region\n"
996 " -u | --unlock Unlock firmware descriptor and ME region\n"
997 " -r | --romsize Specify ROM size\n"
998 " -D | --write-descriptor <file> Write descriptor at base\n"
999 " -c | --create Create a new empty image\n"
1000 " -v | --version: print the version\n"
1001 " -h | --help: print this help\n\n"
1002 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
1007 * get_two_words() - Convert a string into two words separated by :
1009 * The supplied string is split at ':', two substrings are allocated and
1012 * @str: String to split
1013 * @firstp: Returns first string
1014 * @secondp: Returns second string
1015 * @return 0 if OK, -ve if @str does not have a :
1017 static int get_two_words(const char *str
, char **firstp
, char **secondp
)
1021 p
= strchr(str
, ':');
1024 *firstp
= strdup(str
);
1025 (*firstp
)[p
- str
] = '\0';
1026 *secondp
= strdup(p
+ 1);
1031 int main(int argc
, char *argv
[])
1033 int opt
, option_index
= 0;
1034 int mode_dump
= 0, mode_extract
= 0, mode_inject
= 0;
1035 int mode_spifreq
= 0, mode_em100
= 0, mode_locked
= 0;
1036 int mode_unlocked
= 0, mode_write
= 0, mode_write_descriptor
= 0;
1037 int create
= 0, collate_ucode
= 0;
1038 char *region_type_string
= NULL
, *inject_fname
= NULL
;
1039 char *desc_fname
= NULL
, *addr_str
= NULL
;
1040 int region_type
= -1, inputfreq
= 0;
1041 enum spi_frequency spifreq
= SPI_FREQUENCY_20MHZ
;
1042 struct input_file input_file
[WRITE_MAX
], *ifile
, *fdt
= NULL
;
1043 unsigned char wr_idx
, wr_num
= 0;
1047 char *outfile
= NULL
;
1050 unsigned int ucode_ptr
= 0;
1051 bool have_uboot
= false;
1055 static struct option long_options
[] = {
1056 {"create", 0, NULL
, 'c'},
1057 {"collate-microcode", 0, NULL
, 'C'},
1058 {"dump", 0, NULL
, 'd'},
1059 {"descriptor", 1, NULL
, 'D'},
1060 {"em100", 0, NULL
, 'e'},
1061 {"extract", 0, NULL
, 'x'},
1062 {"fdt", 1, NULL
, 'f'},
1063 {"inject", 1, NULL
, 'i'},
1064 {"lock", 0, NULL
, 'l'},
1065 {"microcode", 1, NULL
, 'm'},
1066 {"romsize", 1, NULL
, 'r'},
1067 {"spifreq", 1, NULL
, 's'},
1068 {"unlock", 0, NULL
, 'u'},
1069 {"uboot", 1, NULL
, 'U'},
1070 {"write", 1, NULL
, 'w'},
1071 {"version", 0, NULL
, 'v'},
1072 {"help", 0, NULL
, 'h'},
1076 while ((opt
= getopt_long(argc
, argv
, "cCdD:ef:hi:lm:r:s:uU:vw:x?",
1077 long_options
, &option_index
)) != EOF
) {
1089 mode_write_descriptor
= 1;
1090 desc_fname
= optarg
;
1096 if (get_two_words(optarg
, ®ion_type_string
,
1098 print_usage(argv
[0]);
1101 if (!strcasecmp("Descriptor", region_type_string
))
1103 else if (!strcasecmp("BIOS", region_type_string
))
1105 else if (!strcasecmp("ME", region_type_string
))
1107 else if (!strcasecmp("GbE", region_type_string
))
1109 else if (!strcasecmp("Platform", region_type_string
))
1111 if (region_type
== -1) {
1112 fprintf(stderr
, "No such region type: '%s'\n\n",
1113 region_type_string
);
1114 print_usage(argv
[0]);
1123 ucode_ptr
= strtoul(optarg
, NULL
, 0);
1126 rom_size
= strtol(optarg
, NULL
, 0);
1127 debug("ROM size %d\n", rom_size
);
1130 /* Parse the requested SPI frequency */
1131 inputfreq
= strtol(optarg
, NULL
, 0);
1132 switch (inputfreq
) {
1134 spifreq
= SPI_FREQUENCY_20MHZ
;
1137 spifreq
= SPI_FREQUENCY_33MHZ
;
1140 spifreq
= SPI_FREQUENCY_50MHZ
;
1143 fprintf(stderr
, "Invalid SPI Frequency: %d\n",
1145 print_usage(argv
[0]);
1160 ifile
= &input_file
[wr_num
];
1162 if (wr_num
< WRITE_MAX
) {
1163 if (get_two_words(optarg
, &addr_str
,
1165 print_usage(argv
[0]);
1168 ifile
->addr
= strtoll(optarg
, NULL
, 0);
1169 ifile
->type
= opt
== 'f' ? IF_fdt
:
1170 opt
== 'U' ? IF_uboot
: IF_normal
;
1171 if (ifile
->type
== IF_fdt
)
1173 else if (ifile
->type
== IF_uboot
)
1178 "The number of files to write simultaneously exceeds the limitation (%d)\n",
1188 print_usage(argv
[0]);
1194 if (mode_locked
== 1 && mode_unlocked
== 1) {
1195 fprintf(stderr
, "Locking/Unlocking FD and ME are mutually exclusive\n");
1199 if (mode_inject
== 1 && mode_write
== 1) {
1200 fprintf(stderr
, "Inject/Write are mutually exclusive\n");
1204 if ((mode_dump
+ mode_extract
+ mode_inject
+
1205 (mode_spifreq
| mode_em100
| mode_unlocked
|
1206 mode_locked
)) > 1) {
1207 fprintf(stderr
, "You may not specify more than one mode.\n\n");
1208 print_usage(argv
[0]);
1212 if ((mode_dump
+ mode_extract
+ mode_inject
+ mode_spifreq
+
1213 mode_em100
+ mode_locked
+ mode_unlocked
+ mode_write
+
1214 mode_write_descriptor
) == 0 && !create
) {
1215 fprintf(stderr
, "You need to specify a mode.\n\n");
1216 print_usage(argv
[0]);
1220 if (create
&& rom_size
== -1) {
1221 fprintf(stderr
, "You need to specify a rom size when creating.\n\n");
1225 if (optind
+ 1 != argc
) {
1226 fprintf(stderr
, "You need to specify a file.\n\n");
1227 print_usage(argv
[0]);
1231 if (have_uboot
&& !fdt
) {
1233 "You must supply a device tree file for U-Boot\n\n");
1234 print_usage(argv
[0]);
1238 filename
= argv
[optind
];
1239 if (optind
+ 2 != argc
)
1240 outfile
= argv
[optind
+ 1];
1243 bios_fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
1245 bios_fd
= open(filename
, outfile
? O_RDONLY
: O_RDWR
);
1247 if (bios_fd
== -1) {
1248 perror("Could not open file");
1253 if (fstat(bios_fd
, &buf
) == -1) {
1254 perror("Could not stat file");
1260 debug("File %s is %d bytes\n", filename
, size
);
1265 image
= malloc(rom_size
);
1267 printf("Out of memory.\n");
1271 memset(image
, '\xff', rom_size
);
1272 if (!create
&& read(bios_fd
, image
, size
) != size
) {
1273 perror("Could not read file");
1276 if (size
!= rom_size
) {
1277 debug("ROM size changed to %d bytes\n", rom_size
);
1284 ret
= dump_fd(image
, size
);
1289 ret
= write_regions(image
, size
);
1293 if (mode_write_descriptor
)
1294 ret
= write_data(image
, size
, -size
, desc_fname
, 0, 0);
1297 ret
= inject_region(image
, size
, region_type
, inject_fname
);
1300 int offset_uboot_top
= 0;
1301 int offset_uboot_start
= 0;
1303 for (wr_idx
= 0; wr_idx
< wr_num
; wr_idx
++) {
1304 ifile
= &input_file
[wr_idx
];
1305 if (ifile
->type
== IF_fdt
) {
1307 } else if (ifile
->type
== IF_uboot
) {
1308 ret
= write_uboot(image
, size
, ifile
, fdt
,
1309 ucode_ptr
, collate_ucode
,
1311 &offset_uboot_start
);
1313 ret
= write_data(image
, size
, ifile
->addr
,
1314 ifile
->fname
, offset_uboot_top
,
1315 offset_uboot_start
);
1323 set_spi_frequency(image
, size
, spifreq
);
1326 set_em100_mode(image
, size
);
1329 lock_descriptor(image
, size
);
1332 unlock_descriptor(image
, size
);
1336 ret
= write_image(outfile
, image
, size
);
1338 if (lseek(bios_fd
, 0, SEEK_SET
)) {
1339 perror("Error while seeking");
1342 if (write(bios_fd
, image
, size
) != size
) {
1343 perror("Error while writing");
1352 return ret
< 0 ? 1 : 0;