1 /* ifdtool - dump Intel Firmware Descriptor information */
2 /* SPDX-License-Identifier: GPL-2.0-only */
10 #include <sys/types.h>
12 #include <commonlib/helpers.h>
21 * PTR_IN_RANGE - examine whether a pointer falls in [base, base + limit)
22 * @param ptr: the non-void* pointer to a single arbitrary-sized object.
23 * @param base: base address represented with char* type.
24 * @param limit: upper limit of the legal address.
27 #define PTR_IN_RANGE(ptr, base, limit) \
28 ((const char *)(ptr) >= (base) && \
29 (const char *)&(ptr)[1] <= (base) + (limit))
32 * PLATFORM_HAS_GBE_REGION - some platforms do not support the PCH GbE LAN region
34 #define PLATFORM_HAS_GBE_REGION (platform != PLATFORM_DNV)
37 * PLATFORM_HAS_EC_REGION - some platforms do not support the EC region
39 #define PLATFORM_HAS_EC_REGION (ifd_version >= IFD_VERSION_2 && platform != PLATFORM_DNV)
42 * PLATFORM_HAS_10GBE_X_REGION - some platforms have 1 or more 10GbE LAN regions
44 #define PLATFORM_HAS_10GBE_0_REGION (platform == PLATFORM_DNV)
45 #define PLATFORM_HAS_10GBE_1_REGION (platform == PLATFORM_DNV)
50 * Start Address: bit 0-14 of the GPRD represents the
51 * protected region start address, where bit 0-11 of
52 * the start address are assumed to be zero.
56 /* Specifies read protection is enabled */
57 uint32_t read_protect_en
: 1;
60 * End Address: bit 16-30 of the GPRD represents the
61 * protected region end address, where bit 0-11 of
62 * the end address are assumed to be 0xfff.
66 /* Specifies write protection is enabled */
67 uint32_t write_protect_en
: 1;
73 static int max_regions_from_fdbar(const struct fdbar
*fdb
);
75 static int ifd_version
;
77 static unsigned int max_regions
= 0;
78 static int selected_chip
= 0;
79 static int platform
= -1;
81 static const struct region_name region_names
[MAX_REGIONS
] = {
82 { "Flash Descriptor", "fd", "flashregion_0_flashdescriptor.bin", "SI_DESC" },
83 { "BIOS", "bios", "flashregion_1_bios.bin", "SI_BIOS" },
84 { "Intel ME", "me", "flashregion_2_intel_me.bin", "SI_ME" },
85 { "GbE", "gbe", "flashregion_3_gbe.bin", "SI_GBE" },
86 { "Platform Data", "pd", "flashregion_4_platform_data.bin", "SI_PDR" },
87 { "Device Exp1", "devexp", "flashregion_5_device_exp.bin", "SI_DEVICEEXT" },
88 { "Secondary BIOS", "bios2", "flashregion_6_bios2.bin", "SI_BIOS2" },
89 { "Reserved", "res7", "flashregion_7_reserved.bin", NULL
},
90 { "EC", "ec", "flashregion_8_ec.bin", "SI_EC" },
91 { "Device Exp2", "devexp2", "flashregion_9_device_exp.bin", "SI_DEVICEEXT2" },
92 { "IE", "ie", "flashregion_10_ie.bin", "SI_IE" },
93 { "10GbE_0", "10gbe_0", "flashregion_11_10gbe0.bin", "SI_10GBE0" },
94 { "10GbE_1", "10gbe_1", "flashregion_12_10gbe1.bin", "SI_10GBE1" },
95 { "Reserved", "res13", "flashregion_13_reserved.bin", NULL
},
96 { "Reserved", "res14", "flashregion_14_reserved.bin", NULL
},
97 { "PTT", "ptt", "flashregion_15_ptt.bin", "SI_PTT" },
100 /* port from flashrom */
101 static const char *const ich_chipset_names
[] = {
107 "5 series Ibex Peak",
108 "6 series Cougar Point",
109 "7 series Panther Point",
110 "8 series Lynx Point",
112 "8 series Lynx Point LP",
113 "8 series Wellsburg",
114 "9 series Wildcat Point",
115 "9 series Wildcat Point LP",
116 "Apollo Lake: N3xxx, J3xxx",
117 "Gemini Lake: N5xxx, J5xxx, N4xxx, J4xxx",
118 "Jasper Lake: N6xxx, N51xx, N45xx",
119 "Elkhart Lake: x6000 series Atom",
120 "100/200 series Sunrise Point",
121 "300 series Cannon Point",
122 "400 series Ice Point",
123 "500 series Tiger Point/ 600 series Alder Point",
124 "800 series Meteor Lake",
125 "C620 series Lewisburg",
130 static struct fdbar
*find_fd(char *image
, int size
)
134 /* Scan for FD signature */
135 for (i
= 0; i
< (size
- 4); i
+= 4) {
136 if (*(uint32_t *)(image
+ i
) == 0x0FF0A55A) {
138 break; // signature found.
143 printf("No Flash Descriptor found in this image\n");
147 struct fdbar
*fdb
= (struct fdbar
*)(image
+ i
);
148 return PTR_IN_RANGE(fdb
, image
, size
) ? fdb
: NULL
;
151 static char *find_flumap(char *image
, int size
)
153 /* The upper map is located in the word before the 256B-long OEM section
154 * at the end of the 4kB-long flash descriptor. In the official
155 * documentation this is defined as FDBAR + 0xEFC. However, starting
156 * with B-Step of Ibex Peak (5 series) the signature (and thus FDBAR)
157 * has moved 16 bytes back to offset 0x10 of the image. Although
158 * official documentation still maintains the offset relative to FDBAR
159 * this is wrong and a simple fixed offset from the start of the image
162 char *flumap
= image
+ 4096 - 256 - 4;
163 return PTR_IN_RANGE(flumap
, image
, size
) ? flumap
: NULL
;
166 static struct fcba
*find_fcba(char *image
, int size
)
168 struct fdbar
*fdb
= find_fd(image
, size
);
171 struct fcba
*fcba
= (struct fcba
*)(image
+ ((fdb
->flmap0
& 0xff) << 4));
172 return PTR_IN_RANGE(fcba
, image
, size
) ? fcba
: NULL
;
175 static struct fmba
*find_fmba(char *image
, int size
)
177 struct fdbar
*fdb
= find_fd(image
, size
);
180 struct fmba
*fmba
= (struct fmba
*)(image
+ ((fdb
->flmap1
& 0xff) << 4));
181 return PTR_IN_RANGE(fmba
, image
, size
) ? fmba
: NULL
;
184 static struct frba
*find_frba(char *image
, int size
)
186 struct fdbar
*fdb
= find_fd(image
, size
);
190 (struct frba
*) (image
+ (((fdb
->flmap0
>> 16) & 0xff) << 4));
191 return PTR_IN_RANGE(frba
, image
, size
) ? frba
: NULL
;
194 static struct fpsba
*find_fpsba(char *image
, int size
)
196 struct fdbar
*fdb
= find_fd(image
, size
);
199 struct fpsba
*fpsba
=
200 (struct fpsba
*) (image
+ (((fdb
->flmap1
>> 16) & 0xff) << 4));
202 int SSL
= ((fdb
->flmap1
>> 24) & 0xff) * sizeof(uint32_t);
203 if ((((char *)fpsba
) + SSL
) >= (image
+ size
))
208 static struct fmsba
*find_fmsba(char *image
, int size
)
210 struct fdbar
*fdb
= find_fd(image
, size
);
213 struct fmsba
*fmsba
= (struct fmsba
*)(image
+ ((fdb
->flmap2
& 0xff) << 4));
214 return PTR_IN_RANGE(fmsba
, image
, size
) ? fmsba
: NULL
;
217 /* port from flashrom */
218 static enum ich_chipset
ifd1_guess_chipset(char *image
, int size
)
220 const struct fdbar
*fdb
= find_fd(image
, size
);
223 uint32_t iccriba
= (fdb
->flmap2
>> 16) & 0xff;
224 uint32_t msl
= (fdb
->flmap2
>> 8) & 0xff;
225 uint32_t isl
= (fdb
->flmap1
>> 24);
227 /* Rest for IFD1 chipset type */
228 if (iccriba
== 0x00) {
229 if (msl
== 0 && isl
<= 2)
234 return CHIPSET_ICH10
;
236 return CHIPSET_5_SERIES_IBEX_PEAK
;
237 printf("Peculiar firmware descriptor, assuming Ibex Peak compatibility.\n");
238 return CHIPSET_5_SERIES_IBEX_PEAK
;
239 } else if (iccriba
< 0x31 && (fdb
->flmap2
& 0xff) < 0x30) {
240 if (msl
== 0 && isl
<= 17)
241 return CHIPSET_BAYTRAIL
;
242 else if (msl
<= 1 && isl
<= 18)
243 return CHIPSET_6_SERIES_COUGAR_POINT
;
244 else if (msl
<= 1 && isl
<= 21)
245 return CHIPSET_8_SERIES_LYNX_POINT
;
246 printf("Peculiar firmware descriptor, assuming Wildcat Point compatibility.\n");
247 return CHIPSET_9_SERIES_WILDCAT_POINT
;
249 return CHIPSET_PCH_UNKNOWN
;
252 static enum ich_chipset
ifd2_platform_to_chipset(const int pindex
)
256 return CHIPSET_N_J_SERIES_APOLLO_LAKE
;
258 return CHIPSET_N_J_SERIES_GEMINI_LAKE
;
260 return CHIPSET_N_SERIES_JASPER_LAKE
;
262 return CHIPSET_x6000_SERIES_ELKHART_LAKE
;
263 case PLATFORM_SKLKBL
:
264 return CHIPSET_100_200_SERIES_SUNRISE_POINT
;
266 return CHIPSET_300_SERIES_CANNON_POINT
;
270 return CHIPSET_500_600_SERIES_TIGER_ALDER_POINT
;
272 return CHIPSET_800_SERIES_METEOR_LAKE
;
274 return CHIPSET_900_SERIES_PANTHER_LAKE
;
276 return CHIPSET_400_SERIES_ICE_POINT
;
278 return CHIPSET_C620_SERIES_LEWISBURG
;
280 return CHIPSET_DENVERTON
;
282 return CHIPSET_8_SERIES_WELLSBURG
;
284 return CHIPSET_PCH_UNKNOWN
;
289 * Some newer platforms have re-defined the FCBA field that was used to
290 * distinguish IFD v1 v/s v2. Define a list of platforms that we know do not
291 * have the required FCBA field, but are IFD v2 and return true if current
292 * platform is one of them.
294 static int is_platform_ifd_2(void)
296 static const int ifd_2_platforms
[] = {
315 for (i
= 0; i
< ARRAY_SIZE(ifd_2_platforms
); i
++) {
316 if (platform
== ifd_2_platforms
[i
])
323 static void check_ifd_version(char *image
, int size
)
325 const struct fdbar
*fdb
= find_fd(image
, size
);
327 if (is_platform_ifd_2()) {
328 chipset
= ifd2_platform_to_chipset(platform
);
329 if (chipset
== CHIPSET_8_SERIES_WELLSBURG
)
330 ifd_version
= IFD_VERSION_1_5
;
332 ifd_version
= IFD_VERSION_2
;
333 max_regions
= MIN(max_regions_from_fdbar(fdb
), MAX_REGIONS
);
335 ifd_version
= IFD_VERSION_1
;
336 chipset
= ifd1_guess_chipset(image
, size
);
337 max_regions
= MIN(max_regions_from_fdbar(fdb
), MAX_REGIONS_OLD
);
341 static struct region
get_region(const struct frba
*frba
, unsigned int region_type
)
346 struct region region
;
348 if (ifd_version
>= IFD_VERSION_2
)
353 limit_mask
= base_mask
<< 16;
355 if (region_type
>= max_regions
) {
356 fprintf(stderr
, "Invalid region type %d.\n", region_type
);
360 flreg
= frba
->flreg
[region_type
];
361 region
.base
= (flreg
& base_mask
) << 12;
362 region
.limit
= ((flreg
& limit_mask
) >> 4) | 0xfff;
363 region
.size
= region
.limit
- region
.base
+ 1;
364 region
.type
= region_type
;
372 static void set_region(struct frba
*frba
, unsigned int region_type
,
373 const struct region
*region
)
375 if (region_type
>= max_regions
) {
376 fprintf(stderr
, "Invalid region type %u.\n", region_type
);
380 frba
->flreg
[region_type
] =
381 (((region
->limit
>> 12) & 0x7fff) << 16) |
382 ((region
->base
>> 12) & 0x7fff);
385 static const char *region_name(unsigned int region_type
)
387 if (region_type
>= max_regions
) {
388 fprintf(stderr
, "Invalid region type.\n");
392 return region_names
[region_type
].pretty
;
395 static int region_num(const char *name
)
399 for (i
= 0; i
< max_regions
; i
++) {
400 if (strcasecmp(name
, region_names
[i
].pretty
) == 0)
402 if (strcasecmp(name
, region_names
[i
].terse
) == 0)
409 static void dump_region(unsigned int num
, const struct frba
*frba
)
411 struct region region
= get_region(frba
, num
);
412 printf(" Flash Region %d (%s): %08x - %08x %s\n",
413 num
, region_name(num
), region
.base
, region
.limit
,
414 region
.size
< 1 ? "(unused)" : "");
417 static int sort_compare(const void *a
, const void *b
)
419 return *(size_t *)a
- *(size_t *)b
;
423 * IFDv1 always has 8 regions, while IFDv2 always has 16 regions.
425 * It's platform specific which regions are used or are reserved.
426 * The 'SPI programming guide' as the name says is a guide only,
427 * not a specification what the hardware actually does.
428 * The best to do is not to rely on the guide, but detect how many
429 * regions are present in the IFD and expose them all.
431 * Very early IFDv2 chipsets, sometimes unofficially referred to as
432 * IFDv1.5 platforms, only have 8 regions. To not corrupt the IFD when
433 * operating on an IFDv1.5 detect how much space is actually present
436 static int max_regions_from_fdbar(const struct fdbar
*fdb
)
438 const size_t fcba
= (fdb
->flmap0
& 0xff) << 4;
439 const size_t fmba
= (fdb
->flmap1
& 0xff) << 4;
440 const size_t frba
= ((fdb
->flmap0
>> 16) & 0xff) << 4;
441 const size_t fpsba
= ((fdb
->flmap1
>> 16) & 0xff) << 4;
442 const size_t flumap
= 4096 - 256 - 4;
443 size_t sorted
[5] = {fcba
, fmba
, frba
, fpsba
, flumap
};
445 qsort(sorted
, ARRAY_SIZE(sorted
), sizeof(size_t), sort_compare
);
447 for (size_t i
= 0; i
< 4; i
++) {
449 * Find FRBA in the sorted array and determine the size of the
450 * region by the start of the next region. Every region requires
453 if (sorted
[i
] == frba
)
454 return MIN((sorted
[i
+ 1] - sorted
[i
]) / 4, MAX_REGIONS
);
456 /* Never reaches this point */
460 static void dump_frba(const struct frba
*frba
)
463 struct region region
;
464 printf("Found Region Section\n");
465 for (i
= 0; i
< max_regions
; i
++) {
466 region
= get_region(frba
, i
);
467 /* Skip unused & reserved Flash Region */
468 if (region
.size
< 1 && !strcmp(region_name(i
), "Reserved"))
471 printf("FLREG%u: 0x%08x\n", i
, frba
->flreg
[i
]);
472 dump_region(i
, frba
);
476 static void dump_flashrom_layout(char *image
, int size
, const char *layout_fname
)
478 const struct frba
*frba
= find_frba(image
, size
);
482 int layout_fd
= open(layout_fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
483 if (layout_fd
== -1) {
484 perror("Could not open file");
488 for (unsigned int i
= 0; i
< max_regions
; i
++) {
489 struct region region
= get_region(frba
, i
);
491 /* A region limit of 0 is an indicator of an unused region
492 * A region base of 7FFFh is an indicator of a reserved region
494 if (region
.limit
== 0 || region
.base
== 0x07FFF000)
497 char buf
[LAYOUT_LINELEN
];
498 snprintf(buf
, LAYOUT_LINELEN
, "%08x:%08x %s\n", region
.base
, region
.limit
, region_names
[i
].terse
);
499 if (write(layout_fd
, buf
, strlen(buf
)) < 0) {
500 perror("Could not write to file");
505 printf("Wrote layout to %s\n", layout_fname
);
508 static void _decode_spi_frequency(unsigned int freq
)
511 case SPI_FREQUENCY_20MHZ
:
514 case SPI_FREQUENCY_33MHZ
:
517 case SPI_FREQUENCY_48MHZ
:
520 case SPI_FREQUENCY_50MHZ_30MHZ
:
521 switch (ifd_version
) {
523 case IFD_VERSION_1_5
:
531 case SPI_FREQUENCY_17MHZ
:
535 printf("unknown<%x>MHz", freq
);
539 static void _decode_spi_frequency_500_series(unsigned int freq
)
542 case SPI_FREQUENCY_100MHZ
:
545 case SPI_FREQUENCY_50MHZ
:
548 case SPI_FREQUENCY_500SERIES_33MHZ
:
551 case SPI_FREQUENCY_25MHZ
:
554 case SPI_FREQUENCY_14MHZ
:
558 printf("unknown<%x>MHz", freq
);
562 static void decode_spi_frequency(unsigned int freq
)
565 case CHIPSET_500_600_SERIES_TIGER_ALDER_POINT
:
566 case CHIPSET_800_SERIES_METEOR_LAKE
:
567 case CHIPSET_900_SERIES_PANTHER_LAKE
:
568 _decode_spi_frequency_500_series(freq
);
571 _decode_spi_frequency(freq
);
575 static void _decode_espi_frequency(unsigned int freq
)
578 case ESPI_FREQUENCY_20MHZ
:
581 case ESPI_FREQUENCY_24MHZ
:
584 case ESPI_FREQUENCY_30MHZ
:
587 case ESPI_FREQUENCY_48MHZ
:
590 case ESPI_FREQUENCY_60MHZ
:
593 case ESPI_FREQUENCY_17MHZ
:
597 printf("unknown<%x>MHz", freq
);
601 static void _decode_espi_frequency_500_series(unsigned int freq
)
604 case ESPI_FREQUENCY_500SERIES_20MHZ
:
607 case ESPI_FREQUENCY_500SERIES_24MHZ
:
610 case ESPI_FREQUENCY_500SERIES_25MHZ
:
613 case ESPI_FREQUENCY_500SERIES_48MHZ
:
616 case ESPI_FREQUENCY_500SERIES_60MHZ
:
620 printf("unknown<%x>MHz", freq
);
624 static void _decode_espi_frequency_800_series(unsigned int freq
)
627 case ESPI_FREQUENCY_800SERIES_20MHZ
:
630 case ESPI_FREQUENCY_800SERIES_25MHZ
:
633 case ESPI_FREQUENCY_800SERIES_33MHZ
:
636 case ESPI_FREQUENCY_800SERIES_50MHZ
:
640 printf("unknown<%x>MHz", freq
);
644 static void decode_espi_frequency(unsigned int freq
)
647 case CHIPSET_500_600_SERIES_TIGER_ALDER_POINT
:
648 _decode_espi_frequency_500_series(freq
);
650 case CHIPSET_800_SERIES_METEOR_LAKE
:
651 case CHIPSET_900_SERIES_PANTHER_LAKE
:
652 _decode_espi_frequency_800_series(freq
);
655 _decode_espi_frequency(freq
);
659 static void decode_component_density(unsigned int density
)
662 case COMPONENT_DENSITY_512KB
:
665 case COMPONENT_DENSITY_1MB
:
668 case COMPONENT_DENSITY_2MB
:
671 case COMPONENT_DENSITY_4MB
:
674 case COMPONENT_DENSITY_8MB
:
677 case COMPONENT_DENSITY_16MB
:
680 case COMPONENT_DENSITY_32MB
:
683 case COMPONENT_DENSITY_64MB
:
686 case COMPONENT_DENSITY_UNUSED
:
690 printf("unknown<%x>MB", density
);
694 static int is_platform_with_pch(void)
696 if (chipset
>= CHIPSET_5_SERIES_IBEX_PEAK
)
702 /* FLMAP0 register bit 24 onwards are reserved from SPT PCH */
703 static int is_platform_with_100x_series_pch(void)
705 if (chipset
>= CHIPSET_100_200_SERIES_SUNRISE_POINT
&&
706 chipset
<= CHIPSET_900_SERIES_PANTHER_LAKE
)
712 static void dump_fcba(const struct fcba
*fcba
, const struct fpsba
*fpsba
)
716 printf("\nFound Component Section\n");
717 printf("FLCOMP 0x%08x\n", fcba
->flcomp
);
718 printf(" Dual Output Fast Read Support: %ssupported\n",
719 (fcba
->flcomp
& (1 << 30)) ? "" : "not ");
720 printf(" Read ID/Read Status Clock Frequency: ");
721 decode_spi_frequency((fcba
->flcomp
>> 27) & 7);
722 printf("\n Write/Erase Clock Frequency: ");
723 decode_spi_frequency((fcba
->flcomp
>> 24) & 7);
724 printf("\n Fast Read Clock Frequency: ");
725 decode_spi_frequency((fcba
->flcomp
>> 21) & 7);
726 printf("\n Fast Read Support: %ssupported",
727 (fcba
->flcomp
& (1 << 20)) ? "" : "not ");
728 if (is_platform_with_100x_series_pch() &&
729 chipset
!= CHIPSET_100_200_SERIES_SUNRISE_POINT
) {
730 printf("\n Read eSPI/EC Bus Frequency: ");
731 if (chipset
== CHIPSET_500_600_SERIES_TIGER_ALDER_POINT
)
732 freq
= (fpsba
->pchstrp
[22] & 0x38) >> 3;
733 else if (chipset
== CHIPSET_800_SERIES_METEOR_LAKE
)
734 freq
= (fpsba
->pchstrp
[65] & 0x38) >> 3;
735 else if (chipset
== CHIPSET_900_SERIES_PANTHER_LAKE
)
736 freq
= (fpsba
->pchstrp
[119] & 0x38) >> 3;
738 freq
= (fcba
->flcomp
>> 17) & 7;
739 decode_espi_frequency(freq
);
741 printf("\n Quad I/O Read: %s",
742 (fcba
->flcomp
& (1 << 15)) ? "enabled" : "disabled");
743 printf("\n Quad Output Read: %s",
744 (fcba
->flcomp
& (1 << 14)) ? "enabled" : "disabled");
745 printf("\n Dual I/O Read: %s",
746 (fcba
->flcomp
& (1 << 13)) ? "enabled" : "disabled");
747 printf("\n Dual Output Read: %s",
748 (fcba
->flcomp
& (1 << 12)) ? "enabled" : "disabled");
750 printf("\n Read Clock Frequency: ");
751 decode_spi_frequency((fcba
->flcomp
>> 17) & 7);
754 switch (ifd_version
) {
756 printf("\n Component 2 Density: ");
757 decode_component_density((fcba
->flcomp
>> 3) & 7);
758 printf("\n Component 1 Density: ");
759 decode_component_density(fcba
->flcomp
& 7);
761 case IFD_VERSION_1_5
:
763 printf("\n Component 2 Density: ");
764 decode_component_density((fcba
->flcomp
>> 4) & 0xf);
765 printf("\n Component 1 Density: ");
766 decode_component_density(fcba
->flcomp
& 0xf);
771 printf("FLILL 0x%08x\n", fcba
->flill
);
772 printf(" Invalid Instruction 3: 0x%02x\n",
773 (fcba
->flill
>> 24) & 0xff);
774 printf(" Invalid Instruction 2: 0x%02x\n",
775 (fcba
->flill
>> 16) & 0xff);
776 printf(" Invalid Instruction 1: 0x%02x\n",
777 (fcba
->flill
>> 8) & 0xff);
778 printf(" Invalid Instruction 0: 0x%02x\n",
780 if (is_platform_with_100x_series_pch()) {
781 printf("FLILL1 0x%08x\n", fcba
->flpb
);
782 printf(" Invalid Instruction 7: 0x%02x\n",
783 (fcba
->flpb
>> 24) & 0xff);
784 printf(" Invalid Instruction 6: 0x%02x\n",
785 (fcba
->flpb
>> 16) & 0xff);
786 printf(" Invalid Instruction 5: 0x%02x\n",
787 (fcba
->flpb
>> 8) & 0xff);
788 printf(" Invalid Instruction 4: 0x%02x\n",
791 printf("FLPB 0x%08x\n", fcba
->flpb
);
792 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
793 (fcba
->flpb
& 0xfff) << 12);
797 static void dump_fpsba(const struct fdbar
*fdb
, const struct fpsba
*fpsba
)
800 /* SoC Straps, aka PSL, aka ISL */
801 unsigned int SS
= (fdb
->flmap1
>> 24) & 0xff;
803 printf("Found PCH Strap Section\n");
804 for (i
= 0; i
< SS
; i
++)
805 printf("PCHSTRP%-3u: 0x%08x\n", i
, fpsba
->pchstrp
[i
]);
807 if (ifd_version
>= IFD_VERSION_2
) {
808 printf("HAP bit is %sset\n",
809 fpsba
->pchstrp
[0] & (1 << 16) ? "" : "not ");
810 } else if (chipset
>= CHIPSET_ICH8
&& chipset
<= CHIPSET_ICH10
) {
811 printf("ICH_MeDisable bit is %sset\n",
812 fpsba
->pchstrp
[0] & 1 ? "" : "not ");
814 printf("AltMeDisable bit is %sset\n",
815 fpsba
->pchstrp
[10] & (1 << 7) ? "" : "not ");
821 static void decode_flmstr(uint32_t flmstr
)
823 int wr_shift
, rd_shift
;
824 if (ifd_version
>= IFD_VERSION_2
) {
825 wr_shift
= FLMSTR_WR_SHIFT_V2
;
826 rd_shift
= FLMSTR_RD_SHIFT_V2
;
828 wr_shift
= FLMSTR_WR_SHIFT_V1
;
829 rd_shift
= FLMSTR_RD_SHIFT_V1
;
832 /* EC region access only available on v2+ */
833 if (PLATFORM_HAS_EC_REGION
)
834 printf(" EC Region Write Access: %s\n",
835 (flmstr
& (1 << (wr_shift
+ 8))) ?
836 "enabled" : "disabled");
837 printf(" Platform Data Region Write Access: %s\n",
838 (flmstr
& (1 << (wr_shift
+ 4))) ? "enabled" : "disabled");
839 if (PLATFORM_HAS_GBE_REGION
) {
840 printf(" GbE Region Write Access: %s\n",
841 (flmstr
& (1 << (wr_shift
+ 3))) ? "enabled" : "disabled");
843 printf(" Intel ME Region Write Access: %s\n",
844 (flmstr
& (1 << (wr_shift
+ 2))) ? "enabled" : "disabled");
845 printf(" Host CPU/BIOS Region Write Access: %s\n",
846 (flmstr
& (1 << (wr_shift
+ 1))) ? "enabled" : "disabled");
847 printf(" Flash Descriptor Write Access: %s\n",
848 (flmstr
& (1 << wr_shift
)) ? "enabled" : "disabled");
849 if (PLATFORM_HAS_10GBE_0_REGION
) {
850 printf(" 10GbE_0 Write Access: %s\n",
851 (flmstr
& (1 << (wr_shift
+ 11))) ? "enabled" : "disabled");
853 if (PLATFORM_HAS_10GBE_1_REGION
) {
854 printf(" 10GbE_1 Write Access: %s\n",
855 (flmstr
& (1 << 4)) ? "enabled" : "disabled");
858 if (PLATFORM_HAS_EC_REGION
)
859 printf(" EC Region Read Access: %s\n",
860 (flmstr
& (1 << (rd_shift
+ 8))) ?
861 "enabled" : "disabled");
862 printf(" Platform Data Region Read Access: %s\n",
863 (flmstr
& (1 << (rd_shift
+ 4))) ? "enabled" : "disabled");
864 if (PLATFORM_HAS_GBE_REGION
) {
865 printf(" GbE Region Read Access: %s\n",
866 (flmstr
& (1 << (rd_shift
+ 3))) ? "enabled" : "disabled");
868 printf(" Intel ME Region Read Access: %s\n",
869 (flmstr
& (1 << (rd_shift
+ 2))) ? "enabled" : "disabled");
870 printf(" Host CPU/BIOS Region Read Access: %s\n",
871 (flmstr
& (1 << (rd_shift
+ 1))) ? "enabled" : "disabled");
872 printf(" Flash Descriptor Read Access: %s\n",
873 (flmstr
& (1 << rd_shift
)) ? "enabled" : "disabled");
874 if (PLATFORM_HAS_10GBE_0_REGION
) {
875 printf(" 10GbE_0 Read Access: %s\n",
876 (flmstr
& (1 << (rd_shift
+ 11))) ? "enabled" : "disabled");
878 if (PLATFORM_HAS_10GBE_1_REGION
) {
879 printf(" 10GbE_1 Read Access: %s\n",
880 (flmstr
& (1 << 0)) ? "enabled" : "disabled");
883 /* Requestor ID doesn't exist for ifd 2 */
884 if (ifd_version
< IFD_VERSION_2
)
885 printf(" Requester ID: 0x%04x\n\n",
889 static void dump_fmba(const struct fmba
*fmba
)
891 printf("Found Master Section\n");
892 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba
->flmstr1
);
893 decode_flmstr(fmba
->flmstr1
);
894 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba
->flmstr2
);
895 decode_flmstr(fmba
->flmstr2
);
896 if (PLATFORM_HAS_GBE_REGION
) {
897 printf("FLMSTR3: 0x%08x (GbE)\n", fmba
->flmstr3
);
898 decode_flmstr(fmba
->flmstr3
);
899 if (ifd_version
>= IFD_VERSION_2
) {
900 printf("FLMSTR5: 0x%08x (EC)\n", fmba
->flmstr5
);
901 decode_flmstr(fmba
->flmstr5
);
904 printf("FLMSTR6: 0x%08x (IE)\n", fmba
->flmstr6
);
905 decode_flmstr(fmba
->flmstr6
);
909 static void dump_fmsba(const struct fmsba
*fmsba
)
912 printf("Found Processor Strap Section\n");
913 for (i
= 0; i
< ARRAY_SIZE(fmsba
->data
); i
++)
914 printf("????: 0x%08x\n", fmsba
->data
[i
]);
916 if (chipset
>= CHIPSET_ICH8
&& chipset
<= CHIPSET_ICH10
) {
917 printf("MCH_MeDisable bit is %sset\n",
918 fmsba
->data
[0] & 1 ? "" : "not ");
919 printf("MCH_AltMeDisable bit is %sset\n",
920 fmsba
->data
[0] & (1 << 7) ? "" : "not ");
924 static void dump_jid(uint32_t jid
)
926 printf(" SPI Component Vendor ID: 0x%02x\n",
928 printf(" SPI Component Device ID 0: 0x%02x\n",
930 printf(" SPI Component Device ID 1: 0x%02x\n",
934 static void dump_vscc(uint32_t vscc
)
936 printf(" Lower Erase Opcode: 0x%02x\n",
938 printf(" Lower Write Enable on Write Status: 0x%02x\n",
939 vscc
& (1 << 20) ? 0x06 : 0x50);
940 printf(" Lower Write Status Required: %s\n",
941 vscc
& (1 << 19) ? "Yes" : "No");
942 printf(" Lower Write Granularity: %d bytes\n",
943 vscc
& (1 << 18) ? 64 : 1);
944 printf(" Lower Block / Sector Erase Size: ");
945 switch ((vscc
>> 16) & 0x3) {
947 printf("256 Byte\n");
960 printf(" Upper Erase Opcode: 0x%02x\n",
962 printf(" Upper Write Enable on Write Status: 0x%02x\n",
963 vscc
& (1 << 4) ? 0x06 : 0x50);
964 printf(" Upper Write Status Required: %s\n",
965 vscc
& (1 << 3) ? "Yes" : "No");
966 printf(" Upper Write Granularity: %d bytes\n",
967 vscc
& (1 << 2) ? 64 : 1);
968 printf(" Upper Block / Sector Erase Size: ");
969 switch (vscc
& 0x3) {
971 printf("256 Byte\n");
985 static void dump_vtba(const struct vtba
*vtba
, int vtl
)
988 int max_len
= sizeof(struct vtba
) / sizeof(struct vscc
);
989 int num
= (vtl
>> 1) < max_len
? (vtl
>> 1) : max_len
;
991 printf("ME VSCC table:\n");
992 for (i
= 0; i
< num
; i
++) {
993 printf(" JID%d: 0x%08x\n", i
, vtba
->entry
[i
].jid
);
994 dump_jid(vtba
->entry
[i
].jid
);
995 printf(" VSCC%d: 0x%08x\n", i
, vtba
->entry
[i
].vscc
);
996 dump_vscc(vtba
->entry
[i
].vscc
);
1001 static void dump_oem(const uint8_t *oem
)
1004 printf("OEM Section:\n");
1005 for (i
= 0; i
< 4; i
++) {
1006 printf("%02x:", i
<< 4);
1007 for (j
= 0; j
< 16; j
++)
1008 printf(" %02x", oem
[(i
<< 4) + j
]);
1014 static void dump_fd(char *image
, int size
)
1016 const struct fdbar
*fdb
= find_fd(image
, size
);
1020 printf("%s", is_platform_with_pch() ? "PCH" : "ICH");
1021 printf(" Revision: %s\n", ich_chipset_names
[chipset
]);
1022 printf("FLMAP0: 0x%08x\n", fdb
->flmap0
);
1023 if (!is_platform_with_100x_series_pch())
1024 printf(" NR: %d\n", (fdb
->flmap0
>> 24) & 7);
1025 printf(" FRBA: 0x%x\n", ((fdb
->flmap0
>> 16) & 0xff) << 4);
1026 printf(" NC: %d\n", ((fdb
->flmap0
>> 8) & 3) + 1);
1027 printf(" FCBA: 0x%x\n", ((fdb
->flmap0
) & 0xff) << 4);
1029 printf("FLMAP1: 0x%08x\n", fdb
->flmap1
);
1030 printf(" %s: ", is_platform_with_100x_series_pch() ? "PSL" : "ISL");
1031 printf("0x%02x\n", (fdb
->flmap1
>> 24) & 0xff);
1032 printf(" FPSBA: 0x%x\n", ((fdb
->flmap1
>> 16) & 0xff) << 4);
1033 printf(" NM: %d\n", (fdb
->flmap1
>> 8) & 3);
1034 printf(" FMBA: 0x%x\n", ((fdb
->flmap1
) & 0xff) << 4);
1036 if (!is_platform_with_100x_series_pch()) {
1037 printf("FLMAP2: 0x%08x\n", fdb
->flmap2
);
1038 printf(" PSL: 0x%04x\n", (fdb
->flmap2
>> 8) & 0xffff);
1039 printf(" FMSBA: 0x%x\n", ((fdb
->flmap2
) & 0xff) << 4);
1042 if (chipset
== CHIPSET_500_600_SERIES_TIGER_ALDER_POINT
||
1043 chipset
== CHIPSET_800_SERIES_METEOR_LAKE
||
1044 chipset
== CHIPSET_900_SERIES_PANTHER_LAKE
) {
1045 printf("FLMAP3: 0x%08x\n", fdb
->flmap3
);
1046 printf(" Minor Revision ID: 0x%04x\n", (fdb
->flmap3
>> 14) & 0x7f);
1047 printf(" Major Revision ID: 0x%04x\n", (fdb
->flmap3
>> 21) & 0x7ff);
1050 char *flumap
= find_flumap(image
, size
);
1051 uint32_t flumap1
= *(uint32_t *)flumap
;
1052 printf("FLUMAP1: 0x%08x\n", flumap1
);
1053 printf(" Intel ME VSCC Table Length (VTL): %d\n",
1054 (flumap1
>> 8) & 0xff);
1055 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
1056 (flumap1
& 0xff) << 4);
1057 dump_vtba((struct vtba
*)
1058 (image
+ ((flumap1
& 0xff) << 4)),
1059 (flumap1
>> 8) & 0xff);
1060 dump_oem((const uint8_t *)image
+ 0xf00);
1062 const struct frba
*frba
= find_frba(image
, size
);
1063 const struct fcba
*fcba
= find_fcba(image
, size
);
1064 const struct fpsba
*fpsba
= find_fpsba(image
, size
);
1065 const struct fmba
*fmba
= find_fmba(image
, size
);
1066 const struct fmsba
*fmsba
= find_fmsba(image
, size
);
1068 if (frba
&& fcba
&& fpsba
&& fmba
&& fmsba
) {
1070 dump_fcba(fcba
, fpsba
);
1071 dump_fpsba(fdb
, fpsba
);
1075 printf("FD is corrupted!\n");
1079 /* Takes an image containing an IFD and creates a Flashmap .fmd file template.
1080 * This flashmap will contain all IFD regions except the BIOS region.
1081 * The BIOS region is created by coreboot itself and 'should' match the IFD region
1082 * anyway (CONFIG_VALIDATE_INTEL_DESCRIPTOR should make sure). coreboot built system will use
1083 * this template to generate the final Flashmap file.
1085 static void create_fmap_template(char *image
, int size
, const char *layout_fname
)
1087 const struct frba
*frba
= find_frba(image
, size
);
1091 int layout_fd
= open(layout_fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1092 if (layout_fd
== -1) {
1093 perror("Could not open file");
1097 char *bbuf
= "FLASH@##ROM_BASE## ##ROM_SIZE## {\n";
1098 if (write(layout_fd
, bbuf
, strlen(bbuf
)) < 0) {
1099 perror("Could not write to file");
1103 /* fmaptool requires regions in .fmd to be sorted.
1104 * => We need to sort the regions by base address before writing them in .fmd File
1106 int count_regions
= 0;
1107 struct region sorted_regions
[MAX_REGIONS
] = { 0 };
1108 for (unsigned int i
= 0; i
< max_regions
; i
++) {
1109 struct region region
= get_region(frba
, i
);
1111 /* A region limit of 0 is an indicator of an unused region
1112 * A region base of 7FFFh is an indicator of a reserved region
1114 if (region
.limit
== 0 || region
.base
== 0x07FFF000)
1117 /* Is there an FMAP equivalent? IFD reserved regions are usually thrown out
1120 if (!region_names
[region
.type
].fmapname
) {
1121 printf("Skip IFD region: %s\n", region_names
[region
.type
].pretty
);
1125 /* Here we decide to use the coreboot generated FMAP BIOS region, instead of
1126 * the one specified in the IFD. The case when IFD and FMAP BIOS region do not
1127 * match cannot be caught here, therefore one should still validate IFD and
1128 * FMAP via CONFIG_VALIDATE_INTEL_DESCRIPTOR
1130 if (i
== REGION_BIOS
)
1133 sorted_regions
[count_regions
] = region
;
1134 // basically insertion sort
1135 for (int i
= count_regions
- 1; i
>= 0; i
--) {
1136 if (sorted_regions
[i
].base
> sorted_regions
[i
+ 1].base
) {
1137 struct region tmp
= sorted_regions
[i
];
1138 sorted_regions
[i
] = sorted_regions
[i
+ 1];
1139 sorted_regions
[i
+ 1] = tmp
;
1145 // Now write regions sorted by base address in the fmap file
1146 for (int i
= 0; i
< count_regions
; i
++) {
1147 struct region region
= sorted_regions
[i
];
1148 char buf
[LAYOUT_LINELEN
];
1149 snprintf(buf
, LAYOUT_LINELEN
, "\t%s@0x%X 0x%X\n", region_names
[region
.type
].fmapname
, region
.base
, region
.size
);
1150 if (write(layout_fd
, buf
, strlen(buf
)) < 0) {
1151 perror("Could not write to file");
1156 char *ebuf
= "\tSI_BIOS@##BIOS_BASE## ##BIOS_SIZE## {\n"
1157 "\t\t##CONSOLE_ENTRY##\n"
1158 "\t\t##MRC_CACHE_ENTRY##\n"
1159 "\t\t##SMMSTORE_ENTRY##\n"
1160 "\t\t##SPD_CACHE_ENTRY##\n"
1161 "\t\t##VPD_ENTRY##\n"
1162 "\t\tFMAP@##FMAP_BASE## ##FMAP_SIZE##\n"
1163 "\t\tCOREBOOT(CBFS)@##CBFS_BASE## ##CBFS_SIZE##\n"
1166 if (write(layout_fd
, ebuf
, strlen(ebuf
)) < 0) {
1167 perror("Could not write to file");
1172 printf("Wrote layout to %s\n", layout_fname
);
1175 static void write_regions(char *image
, int size
)
1178 const struct frba
*frba
= find_frba(image
, size
);
1183 for (i
= 0; i
< max_regions
; i
++) {
1184 struct region region
= get_region(frba
, i
);
1185 dump_region(i
, frba
);
1186 if (region
.size
> 0) {
1188 region_fd
= open(region_names
[i
].filename
,
1189 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1190 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
1191 if (region_fd
< 0) {
1192 perror("Error while trying to open file");
1195 if (write(region_fd
, image
+ region
.base
, region
.size
) != region
.size
)
1196 perror("Error while writing");
1202 static void validate_layout(char *image
, int size
)
1206 long int fmap_loc
= fmap_find((uint8_t *)image
, size
);
1207 const struct frba
*frba
= find_frba(image
, size
);
1209 if (fmap_loc
< 0 || !frba
) {
1210 printf("Could not find FMAP (%p) or Intel Flash Descriptor (%p)\n",
1211 (void *)fmap_loc
, frba
);
1215 fmap
= (struct fmap
*)(image
+ fmap_loc
);
1218 for (i
= 0; i
< max_regions
; i
++) {
1219 struct region region
= get_region(frba
, i
);
1220 if (region
.size
== 0)
1223 const struct fmap_area
*area
= fmap_find_area(fmap
, region_names
[i
].fmapname
);
1227 matches
++; // found a match between FMAP and IFD region
1229 if ((uint
)region
.base
!= area
->offset
|| (uint
)region
.size
!= area
->size
) {
1230 if (i
== REGION_BIOS
) {
1232 * BIOS FMAP region is a special case
1233 * coreboots FMAP BIOS region depends on the CONFIG_CBFS_SIZE
1234 * while the IFD BIOS region is always of static size.
1235 * Therefore we just make sure that the BIOS region of the FMAP
1236 * is inside the region specified by the IFD
1238 if ((uint
)region
.base
<= area
->offset
&&
1239 ((uint
)region
.base
+ region
.size
) >= (area
->offset
+ area
->size
)) {
1243 printf("Region mismatch between %s and %s\n", region_names
[i
].terse
, area
->name
);
1244 printf(" Descriptor region %s:\n", region_names
[i
].terse
);
1245 printf(" offset: 0x%08x\n", region
.base
);
1246 printf(" length: 0x%08x\n", region
.size
);
1247 printf(" FMAP area %s:\n", area
->name
);
1248 printf(" offset: 0x%08x\n", area
->offset
);
1249 printf(" length: 0x%08x\n", area
->size
);
1255 // At least a BIOS region should be present in both IFD and FMAP
1256 fprintf(stderr
, "Warning: Not a single IFD region found in FMAP\n");
1263 static void write_image(const char *filename
, char *image
, int size
)
1266 printf("Writing new image to %s\n", filename
);
1268 // Now write out new image
1269 new_fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1271 perror("Error while trying to open file");
1274 if (write(new_fd
, image
, size
) != size
)
1275 perror("Error while writing");
1279 static void set_spi_frequency(const char *filename
, char *image
, int size
,
1280 enum spi_frequency freq
)
1282 struct fcba
*fcba
= find_fcba(image
, size
);
1286 /* clear bits 21-29 */
1287 fcba
->flcomp
&= ~0x3fe00000;
1288 /* Read ID and Read Status Clock Frequency */
1289 fcba
->flcomp
|= freq
<< 27;
1290 /* Write and Erase Clock Frequency */
1291 fcba
->flcomp
|= freq
<< 24;
1292 /* Fast Read Clock Frequency */
1293 fcba
->flcomp
|= freq
<< 21;
1295 write_image(filename
, image
, size
);
1298 static void set_em100_mode(const char *filename
, char *image
, int size
)
1300 struct fcba
*fcba
= find_fcba(image
, size
);
1306 switch (ifd_version
) {
1308 case IFD_VERSION_1_5
:
1309 freq
= SPI_FREQUENCY_20MHZ
;
1312 freq
= SPI_FREQUENCY_17MHZ
;
1315 freq
= SPI_FREQUENCY_17MHZ
;
1319 fcba
->flcomp
&= ~(1 << 30);
1320 set_spi_frequency(filename
, image
, size
, freq
);
1323 static void set_chipdensity(const char *filename
, char *image
, int size
,
1324 unsigned int density
)
1326 struct fcba
*fcba
= find_fcba(image
, size
);
1327 uint8_t mask
, chip2_offset
;
1331 printf("Setting chip density to ");
1332 decode_component_density(density
);
1335 switch (ifd_version
) {
1337 /* fail if selected density is not supported by this version */
1338 if ( (density
== COMPONENT_DENSITY_32MB
) ||
1339 (density
== COMPONENT_DENSITY_64MB
) ||
1340 (density
== COMPONENT_DENSITY_UNUSED
) ) {
1341 printf("error: Selected density not supported in IFD version 1.\n");
1347 case IFD_VERSION_1_5
:
1353 printf("error: Unknown IFD version\n");
1358 /* clear chip density for corresponding chip */
1359 switch (selected_chip
) {
1361 fcba
->flcomp
&= ~mask
;
1364 fcba
->flcomp
&= ~(mask
<< chip2_offset
);
1366 default: /*both chips*/
1367 fcba
->flcomp
&= ~(mask
| (mask
<< chip2_offset
));
1371 /* set the new density */
1372 if (selected_chip
== 1 || selected_chip
== 0)
1373 fcba
->flcomp
|= (density
); /* first chip */
1374 if (selected_chip
== 2 || selected_chip
== 0)
1375 fcba
->flcomp
|= (density
<< chip2_offset
); /* second chip */
1377 write_image(filename
, image
, size
);
1380 static int check_region(const struct frba
*frba
, unsigned int region_type
)
1382 struct region region
;
1387 region
= get_region(frba
, region_type
);
1388 return !!((region
.base
< region
.limit
) && (region
.size
> 0));
1392 * Platforms from CNL onwards support up to 16 flash regions, not 12. The
1393 * permissions for regions [15:12] are stored in extended region read/write
1394 * access fields in the FLMSTR registers.
1396 * FLMSTR with extended regions:
1397 * 31:20 Region Write Access
1398 * 19:8 Region Read Access
1399 * 7:4 Extended Region Write Access
1400 * 3:0 Extended Region Read Access
1402 * FLMSTR without extended regions:
1403 * 31:20 Region Write Access
1404 * 19:8 Region Read Access
1407 static bool platform_has_extended_regions(void)
1422 static void lock_descriptor(const char *filename
, char *image
, int size
)
1424 int wr_shift
, rd_shift
;
1425 struct fmba
*fmba
= find_fmba(image
, size
);
1426 const struct frba
*frba
= find_frba(image
, size
);
1430 if (ifd_version
>= IFD_VERSION_2
) {
1431 wr_shift
= FLMSTR_WR_SHIFT_V2
;
1432 rd_shift
= FLMSTR_RD_SHIFT_V2
;
1435 * Clear all read/write access bits. See comment on
1436 * platform_has_extended_regions() for bitfields.
1438 if (platform_has_extended_regions()) {
1444 fmba
->flmstr1
&= 0xff;
1445 fmba
->flmstr2
&= 0xff;
1446 fmba
->flmstr3
&= 0xff;
1447 fmba
->flmstr5
&= 0xff;
1450 wr_shift
= FLMSTR_WR_SHIFT_V1
;
1451 rd_shift
= FLMSTR_RD_SHIFT_V1
;
1456 fmba
->flmstr3
= 0x118;
1462 /* CPU/BIOS can read descriptor and BIOS */
1463 fmba
->flmstr1
|= 0x3 << rd_shift
;
1464 /* CPU/BIOS can write BIOS */
1465 fmba
->flmstr1
|= 0x2 << wr_shift
;
1466 /* TXE can read descriptor, BIOS and Device Expansion */
1467 fmba
->flmstr2
|= 0x23 << rd_shift
;
1468 /* TXE can only write Device Expansion */
1469 fmba
->flmstr2
|= 0x20 << wr_shift
;
1473 case PLATFORM_SKLKBL
:
1481 /* CPU/BIOS can read descriptor and BIOS. */
1482 fmba
->flmstr1
|= (1 << REGION_DESC
) << rd_shift
;
1483 fmba
->flmstr1
|= (1 << REGION_BIOS
) << rd_shift
;
1484 /* CPU/BIOS can write BIOS. */
1485 fmba
->flmstr1
|= (1 << REGION_BIOS
) << wr_shift
;
1486 /* ME can read descriptor and ME. */
1487 fmba
->flmstr2
|= (1 << REGION_DESC
) << rd_shift
;
1488 fmba
->flmstr2
|= (1 << REGION_ME
) << rd_shift
;
1489 /* ME can write ME. */
1490 fmba
->flmstr2
|= (1 << REGION_ME
) << wr_shift
;
1491 if (check_region(frba
, REGION_GBE
)) {
1492 /* BIOS can read/write GbE. */
1493 fmba
->flmstr1
|= (1 << REGION_GBE
) << rd_shift
;
1494 fmba
->flmstr1
|= (1 << REGION_GBE
) << wr_shift
;
1495 /* ME can read GbE. */
1496 fmba
->flmstr2
|= (1 << REGION_GBE
) << rd_shift
;
1497 /* GbE can read descriptor and read/write GbE.. */
1498 fmba
->flmstr3
|= (1 << REGION_DESC
) << rd_shift
;
1499 fmba
->flmstr3
|= (1 << REGION_GBE
) << rd_shift
;
1500 fmba
->flmstr3
|= (1 << REGION_GBE
) << wr_shift
;
1502 if (check_region(frba
, REGION_PDR
)) {
1503 /* BIOS can read/write PDR. */
1504 fmba
->flmstr1
|= (1 << REGION_PDR
) << rd_shift
;
1505 fmba
->flmstr1
|= (1 << REGION_PDR
) << wr_shift
;
1507 if (check_region(frba
, REGION_EC
)) {
1508 /* BIOS can read EC. */
1509 fmba
->flmstr1
|= (1 << REGION_EC
) << rd_shift
;
1510 /* EC can read descriptor and read/write EC. */
1511 fmba
->flmstr5
|= (1 << REGION_DESC
) << rd_shift
;
1512 fmba
->flmstr5
|= (1 << REGION_EC
) << rd_shift
;
1513 fmba
->flmstr5
|= (1 << REGION_EC
) << wr_shift
;
1515 if (check_region(frba
, REGION_DEV_EXP2
)) {
1516 /* BIOS can read SPI device expansion 2 region. */
1517 fmba
->flmstr1
|= (1 << REGION_DEV_EXP2
) << rd_shift
;
1522 /* CPU/BIOS can read descriptor and BIOS. */
1523 fmba
->flmstr1
|= (1 << REGION_DESC
) << rd_shift
;
1524 fmba
->flmstr1
|= (1 << REGION_BIOS
) << rd_shift
;
1525 /* CPU/BIOS can write BIOS. */
1526 fmba
->flmstr1
|= (1 << REGION_BIOS
) << wr_shift
;
1527 /* ME can read descriptor and ME. */
1528 fmba
->flmstr2
|= (1 << REGION_DESC
) << rd_shift
;
1529 fmba
->flmstr2
|= (1 << REGION_ME
) << rd_shift
;
1530 /* ME can write ME. */
1531 fmba
->flmstr2
|= (1 << REGION_ME
) << wr_shift
;
1534 /* CPU/BIOS can read descriptor and BIOS. */
1535 fmba
->flmstr1
|= (1 << REGION_DESC
) << rd_shift
;
1536 fmba
->flmstr1
|= (1 << REGION_BIOS
) << rd_shift
;
1537 /* CPU/BIOS can write BIOS. */
1538 fmba
->flmstr1
|= (1 << REGION_BIOS
) << wr_shift
;
1539 /* ME can read descriptor and ME. */
1540 fmba
->flmstr2
|= (1 << REGION_DESC
) << rd_shift
;
1541 fmba
->flmstr2
|= (1 << REGION_ME
) << rd_shift
;
1542 /* ME can write ME. */
1543 fmba
->flmstr2
|= (1 << REGION_ME
) << wr_shift
;
1544 if (check_region(frba
, REGION_GBE
)) {
1545 /* BIOS can read GbE. */
1546 fmba
->flmstr1
|= (1 << REGION_GBE
) << rd_shift
;
1547 /* BIOS can write GbE. */
1548 fmba
->flmstr1
|= (1 << REGION_GBE
) << wr_shift
;
1549 /* ME can read GbE. */
1550 fmba
->flmstr2
|= (1 << REGION_GBE
) << rd_shift
;
1551 /* ME can write GbE. */
1552 fmba
->flmstr2
|= (1 << REGION_GBE
) << wr_shift
;
1553 /* GbE can write GbE. */
1554 fmba
->flmstr3
|= (1 << REGION_GBE
) << rd_shift
;
1555 /* GbE can read GbE. */
1556 fmba
->flmstr3
|= (1 << REGION_GBE
) << wr_shift
;
1561 write_image(filename
, image
, size
);
1564 static void enable_cpu_read_me(const char *filename
, char *image
, int size
)
1567 struct fmba
*fmba
= find_fmba(image
, size
);
1572 if (ifd_version
>= IFD_VERSION_2
)
1573 rd_shift
= FLMSTR_RD_SHIFT_V2
;
1575 rd_shift
= FLMSTR_RD_SHIFT_V1
;
1577 /* CPU/BIOS can read ME. */
1578 fmba
->flmstr1
|= (1 << REGION_ME
) << rd_shift
;
1580 write_image(filename
, image
, size
);
1583 static void unlock_descriptor(const char *filename
, char *image
, int size
)
1585 struct fmba
*fmba
= find_fmba(image
, size
);
1589 if (ifd_version
>= IFD_VERSION_2
) {
1591 * Set all read/write access bits. See comment on
1592 * platform_has_extended_regions() for bitfields.
1594 if (platform_has_extended_regions()) {
1595 fmba
->flmstr1
= 0xffffffff;
1596 fmba
->flmstr2
= 0xffffffff;
1597 fmba
->flmstr3
= 0xffffffff;
1598 fmba
->flmstr5
= 0xffffffff;
1600 fmba
->flmstr1
= 0xffffff00 | (fmba
->flmstr1
& 0xff);
1601 fmba
->flmstr2
= 0xffffff00 | (fmba
->flmstr2
& 0xff);
1602 fmba
->flmstr3
= 0xffffff00 | (fmba
->flmstr3
& 0xff);
1603 fmba
->flmstr5
= 0xffffff00 | (fmba
->flmstr5
& 0xff);
1606 fmba
->flmstr1
= 0xffff0000;
1607 fmba
->flmstr2
= 0xffff0000;
1608 /* Keep chipset specific Requester ID */
1609 fmba
->flmstr3
= 0x08080000 | (fmba
->flmstr3
& 0xffff);
1612 write_image(filename
, image
, size
);
1615 static void print_gpr0_range(union gprd reg
)
1617 printf("--------- GPR0 Protected Range --------------\n");
1618 printf("Start address = 0x%08x\n", reg
.data
.start
<< 12);
1619 printf("End address = 0x%08x\n", (reg
.data
.end
<< 12) | 0xfff);
1622 static uint8_t get_cse_data_partition_offset(void)
1624 uint8_t data_offset
= 0xff;
1644 static uint32_t get_gpr0_offset(void)
1646 /* Offset expressed as number of 32-bit fields from FPSBA */
1647 uint32_t gpr0_offset
= 0xffffffff;
1673 static void disable_gpr0(const char *filename
, char *image
, int size
)
1675 struct fpsba
*fpsba
= find_fpsba(image
, size
);
1679 uint32_t gpr0_offset
= get_gpr0_offset();
1680 if (gpr0_offset
== 0xffffffff) {
1681 fprintf(stderr
, "Disabling GPR0 not supported on this platform\n");
1686 /* If bit 31 is set then GPR0 protection is enable */
1687 reg
.value
= fpsba
->pchstrp
[gpr0_offset
];
1688 if (!reg
.data
.write_protect_en
) {
1689 printf("GPR0 protection is already disabled\n");
1693 printf("Value at GPRD offset (%d) is 0x%08x\n", gpr0_offset
, reg
.value
);
1694 print_gpr0_range(reg
);
1695 /* 0 means GPR0 protection is disabled */
1696 fpsba
->pchstrp
[gpr0_offset
] = 0;
1697 write_image(filename
, image
, size
);
1698 printf("GPR0 protection is now disabled\n");
1702 * Helper function to parse the FPT to retrieve the FITC start offset and size.
1703 * FITC is a sub-partition table inside CSE data partition known as FPT.
1706 * |-----> CSE Data Partition Offset
1707 * | |-------> FPT Entry
1708 * | | |-> Sub Partition 1
1709 * | | |-> Sub Partition 2
1711 * | | | | -> FITC Offset
1712 * | | | | -> FITC Length
1714 static int parse_fitc_table(struct cse_fpt
*fpt
, uint32_t *offset
,
1717 size_t num_part_header
= fpt
->count
;
1718 /* Move to the next structure which is FPT sub-partition entries */
1719 struct cse_fpt_sub_part
*fpt_sub_part
= (struct cse_fpt_sub_part
*)(fpt
+ 1);
1720 for (size_t index
= 0; index
< num_part_header
; index
++) {
1721 if (!strncmp(fpt_sub_part
->signature
, "FITC", 4)) {
1722 *offset
= fpt_sub_part
->offset
;
1723 *size
= fpt_sub_part
->length
;
1733 * Formula to calculate the GPR0 protection range as below:
1734 * Start: CSE Region Base Offset
1735 * End: Till the end of FITC sub-partition
1737 static int calculate_gpr0_range(char *image
, int size
,
1738 uint32_t *gpr0_start
, uint32_t *gpr0_end
)
1740 struct frba
*frba
= find_frba(image
, size
);
1744 struct region region
= get_region(frba
, REGION_ME
);
1745 if (region
.size
<= 0) {
1746 fprintf(stderr
, "Region %s is disabled in target\n",
1747 region_name(REGION_ME
));
1751 /* CSE Region Start */
1752 uint32_t cse_region_start
= region
.base
;
1753 /* Get CSE Data Partition Offset */
1754 uint8_t cse_data_offset
= get_cse_data_partition_offset();
1755 if (cse_data_offset
== 0xff) {
1756 fprintf(stderr
, "Unsupported platform\n");
1759 const uint32_t *data_part_offset_ptr
= (uint32_t *)(image
+ cse_region_start
+
1761 if (!PTR_IN_RANGE(data_part_offset_ptr
, image
, size
)) {
1762 fprintf(stderr
, "Data part offset %d exceeds image size %d\n",
1763 cse_region_start
+ cse_data_offset
, size
);
1766 uint32_t data_part_offset
= *data_part_offset_ptr
;
1768 /* Start reading the CSE Data Partition Table, also known as FPT */
1769 uint32_t data_part_start
= data_part_offset
+ cse_region_start
;
1770 struct cse_fpt
*fpt
= (struct cse_fpt
*)(image
+ data_part_start
);
1771 if (!PTR_IN_RANGE(fpt
, image
, size
)) {
1772 fprintf(stderr
, "FPT offset %d exceeds image size %d\n",
1773 data_part_start
, size
);
1777 uint32_t fitc_region_start
= 0;
1778 size_t fitc_region_size
= 0;
1780 * FPT holds entry for own FPT data structure also bunch of sub-partitions.
1781 * `FITC` is one of such sub-partition entry.
1783 if (parse_fitc_table(fpt
, &fitc_region_start
, &fitc_region_size
) < 0) {
1784 fprintf(stderr
, "Unable to find FITC entry\n");
1789 * GPR0 protection is configured to the following range:
1790 * start: CSE region base offset
1791 * end: Till the end of FITC sub-partition (i.e. CSE region + data partition offset +
1792 * FITC sub partition offset + FITC sub partition size)
1794 *gpr0_start
= cse_region_start
;
1795 *gpr0_end
= (cse_region_start
+ data_part_offset
+
1796 fitc_region_start
+ fitc_region_size
) - 1;
1801 static union gprd
get_enabled_gprd(char *image
, int size
)
1803 union gprd enabled_gprd_reg
;
1804 uint32_t gpr0_range_start
, gpr0_range_end
;
1805 enabled_gprd_reg
.value
= 0;
1806 if (calculate_gpr0_range(image
, size
, &gpr0_range_start
, &gpr0_range_end
))
1809 enabled_gprd_reg
.data
.start
= (gpr0_range_start
>> 12) & 0x7fff;
1810 enabled_gprd_reg
.data
.end
= (gpr0_range_end
>> 12) & 0x7fff;
1811 enabled_gprd_reg
.data
.read_protect_en
= 0;
1812 enabled_gprd_reg
.data
.write_protect_en
= 1;
1814 return enabled_gprd_reg
;
1817 static void enable_gpr0(const char *filename
, char *image
, int size
)
1819 struct fpsba
*fpsba
= find_fpsba(image
, size
);
1823 uint32_t gpr0_offset
= get_gpr0_offset();
1824 if (gpr0_offset
== 0xffffffff) {
1825 fprintf(stderr
, "Enabling GPR0 not supported on this platform\n");
1830 /* If bit 31 is set then GPR0 protection is enable */
1831 reg
.value
= fpsba
->pchstrp
[gpr0_offset
];
1832 if (reg
.data
.write_protect_en
) {
1833 printf("GPR0 protection is already enabled\n");
1834 print_gpr0_range(reg
);
1838 union gprd enabled_gprd
= get_enabled_gprd(image
, size
);
1840 fpsba
->pchstrp
[gpr0_offset
] = enabled_gprd
.value
;
1841 printf("Value at GPRD offset (%d) is 0x%08x\n", gpr0_offset
, enabled_gprd
.value
);
1842 print_gpr0_range(enabled_gprd
);
1843 write_image(filename
, image
, size
);
1844 printf("GPR0 protection is now enabled\n");
1847 static void is_gpr0_protected(char *image
, int size
)
1849 struct fpsba
*fpsba
= find_fpsba(image
, size
);
1853 uint32_t gpr0_offset
= get_gpr0_offset();
1854 if (gpr0_offset
== 0xffffffff) {
1855 fprintf(stderr
, "Checking GPR0 not supported on this platform\n");
1859 union gprd enabled_gprd
= get_enabled_gprd(image
, size
);
1860 reg
.value
= fpsba
->pchstrp
[gpr0_offset
];
1862 if (fpsba
->pchstrp
[gpr0_offset
] == enabled_gprd
.value
)
1863 printf("GPR0 status: Enabled\n\n");
1864 else if (fpsba
->pchstrp
[gpr0_offset
] == 0)
1865 printf("GPR0 status: Disabled\n\n");
1867 printf("ERROR: GPR0 setting is not expected\n\n");
1869 printf("Value at GPRD offset (%d) is 0x%08x\n", gpr0_offset
, fpsba
->pchstrp
[gpr0_offset
]);
1870 print_gpr0_range(reg
);
1873 static void set_pchstrap(struct fpsba
*fpsba
, const struct fdbar
*fdb
, const int strap
,
1874 const unsigned int value
)
1876 if (!fpsba
|| !fdb
) {
1877 fprintf(stderr
, "Internal error\n");
1881 /* SoC Strap, aka PSL, aka ISL */
1882 int SS
= (fdb
->flmap1
>> 24) & 0xff;
1884 fprintf(stderr
, "Strap index %d out of range (max: %d)\n", strap
, SS
);
1887 fpsba
->pchstrp
[strap
] = value
;
1890 /* Set the AltMeDisable (or HAP for >= IFD_VERSION_2) */
1891 static void fpsba_set_altmedisable(struct fpsba
*fpsba
, struct fmsba
*fmsba
, bool altmedisable
)
1893 if (ifd_version
>= IFD_VERSION_2
) {
1894 printf("%sting the HAP bit to %s Intel ME...\n",
1895 altmedisable
? "Set" : "Unset",
1896 altmedisable
? "disable" : "enable");
1898 fpsba
->pchstrp
[0] |= (1 << 16);
1900 fpsba
->pchstrp
[0] &= ~(1 << 16);
1902 if (chipset
>= CHIPSET_ICH8
&& chipset
<= CHIPSET_ICH10
) {
1903 printf("%sting the ICH_MeDisable, MCH_MeDisable, "
1904 "and MCH_AltMeDisable to %s Intel ME...\n",
1905 altmedisable
? "Set" : "Unset",
1906 altmedisable
? "disable" : "enable");
1909 fmsba
->data
[0] |= 1;
1910 /* MCH_AltMeDisable */
1911 fmsba
->data
[0] |= (1 << 7);
1913 fpsba
->pchstrp
[0] |= 1;
1915 fmsba
->data
[0] &= ~1;
1916 fmsba
->data
[0] &= ~(1 << 7);
1917 fpsba
->pchstrp
[0] &= ~1;
1920 printf("%sting the AltMeDisable to %s Intel ME...\n",
1921 altmedisable
? "Set" : "Unset",
1922 altmedisable
? "disable" : "enable");
1924 fpsba
->pchstrp
[10] |= (1 << 7);
1926 fpsba
->pchstrp
[10] &= ~(1 << 7);
1931 static void inject_region(const char *filename
, char *image
, int size
,
1932 unsigned int region_type
, const char *region_fname
)
1934 struct frba
*frba
= find_frba(image
, size
);
1938 struct region region
= get_region(frba
, region_type
);
1939 if (region
.size
<= 0xfff) {
1940 fprintf(stderr
, "Region %s is disabled in target. Not injecting.\n",
1941 region_name(region_type
));
1945 int region_fd
= open(region_fname
, O_RDONLY
| O_BINARY
);
1946 if (region_fd
== -1) {
1947 perror("Could not open file");
1951 if (fstat(region_fd
, &buf
) == -1) {
1952 perror("Could not stat file");
1955 int region_size
= buf
.st_size
;
1957 printf("File %s is %d bytes\n", region_fname
, region_size
);
1959 if (region_size
> region
.size
) {
1960 fprintf(stderr
, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
1961 " bytes. Not injecting.\n",
1962 region_name(region_type
), region
.size
,
1963 region
.size
, region_size
, region_size
);
1968 if ((region_type
== 1) && (region_size
< region
.size
)) {
1969 fprintf(stderr
, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
1970 " bytes. Padding before injecting.\n",
1971 region_name(region_type
), region
.size
,
1972 region
.size
, region_size
, region_size
);
1973 offset
= region
.size
- region_size
;
1974 memset(image
+ region
.base
, 0xff, offset
);
1977 if (size
< region
.base
+ offset
+ region_size
) {
1978 fprintf(stderr
, "Output file is too small. (%d < %d)\n",
1979 size
, region
.base
+ offset
+ region_size
);
1983 if (read(region_fd
, image
+ region
.base
+ offset
, region_size
) != region_size
) {
1984 perror("Could not read file");
1990 printf("Adding %s as the %s section of %s\n",
1991 region_fname
, region_name(region_type
), filename
);
1992 write_image(filename
, image
, size
);
1995 static unsigned int next_pow2(unsigned int x
)
2007 * Determine if two memory regions overlap.
2009 * @param r1, r2 Memory regions to compare.
2010 * @return 0 if the two regions are separate
2011 * @return 1 if the two regions overlap
2013 static int regions_collide(const struct region
*r1
, const struct region
*r2
)
2015 if ((r1
->size
== 0) || (r2
->size
== 0))
2018 /* r1 should be either completely below or completely above r2 */
2019 return !(r1
->limit
< r2
->base
|| r1
->base
> r2
->limit
);
2022 static void new_layout(const char *filename
, char *image
, int size
,
2023 const char *layout_fname
)
2027 char layout_region_name
[256];
2030 struct region current_regions
[MAX_REGIONS
];
2031 struct region new_regions
[MAX_REGIONS
];
2035 /* load current descriptor map and regions */
2036 struct frba
*frba
= find_frba(image
, size
);
2040 for (i
= 0; i
< max_regions
; i
++) {
2041 current_regions
[i
] = get_region(frba
, i
);
2042 new_regions
[i
] = get_region(frba
, i
);
2045 /* read new layout */
2046 romlayout
= fopen(layout_fname
, "r");
2049 perror("Could not read layout file.\n");
2053 while (!feof(romlayout
)) {
2054 char *tstr1
, *tstr2
;
2056 if (2 != fscanf(romlayout
, "%255s %255s\n", tempstr
,
2057 layout_region_name
))
2060 region_number
= region_num(layout_region_name
);
2061 if (region_number
< 0)
2064 tstr1
= strtok(tempstr
, ":");
2065 tstr2
= strtok(NULL
, ":");
2066 if (!tstr1
|| !tstr2
) {
2067 fprintf(stderr
, "Could not parse layout file.\n");
2070 new_regions
[region_number
].base
= strtol(tstr1
,
2072 new_regions
[region_number
].limit
= strtol(tstr2
,
2074 new_regions
[region_number
].size
=
2075 new_regions
[region_number
].limit
-
2076 new_regions
[region_number
].base
+ 1;
2078 if (new_regions
[region_number
].size
< 0)
2079 new_regions
[region_number
].size
= 0;
2083 /* check new layout */
2084 for (i
= 0; i
< max_regions
; i
++) {
2085 if (new_regions
[i
].size
== 0)
2088 if (new_regions
[i
].size
< current_regions
[i
].size
) {
2089 printf("DANGER: Region %s is shrinking.\n",
2091 printf(" The region will be truncated to fit.\n");
2092 printf(" This may result in an unusable image.\n");
2095 for (j
= i
+ 1; j
< max_regions
; j
++) {
2096 if (regions_collide(&new_regions
[i
], &new_regions
[j
])) {
2097 fprintf(stderr
, "Regions would overlap.\n");
2102 /* detect if the image size should grow */
2103 if (new_extent
< new_regions
[i
].limit
)
2104 new_extent
= new_regions
[i
].limit
;
2107 /* check if the image is actually a Flash Descriptor region */
2108 if (size
== new_regions
[0].size
) {
2109 printf("The image is a single Flash Descriptor:\n");
2110 printf(" Only the descriptor will be modified\n");
2113 new_extent
= next_pow2(new_extent
- 1);
2114 if (new_extent
!= size
) {
2115 printf("The image has changed in size.\n");
2116 printf("The old image is %d bytes.\n", size
);
2117 printf("The new image is %d bytes.\n", new_extent
);
2121 /* copy regions to a new image */
2122 new_image
= malloc(new_extent
);
2123 memset(new_image
, 0xff, new_extent
);
2124 for (i
= 0; i
< max_regions
; i
++) {
2125 int copy_size
= new_regions
[i
].size
;
2126 int offset_current
= 0, offset_new
= 0;
2127 const struct region
*current
= ¤t_regions
[i
];
2128 const struct region
*new = &new_regions
[i
];
2133 if (new->size
> current
->size
) {
2134 /* copy from the end of the current region */
2135 copy_size
= current
->size
;
2136 if (i
== REGION_BIOS
)
2137 offset_new
= new->size
- current
->size
;
2140 if ((i
== REGION_BIOS
) && (new->size
< current
->size
)) {
2141 /* copy BIOS region to the end of the new region */
2142 offset_current
= current
->size
- new->size
;
2145 if (size
< current
->base
+ offset_current
+ copy_size
) {
2146 printf("Skip descriptor %d (%s) (region missing in the old image)\n", i
,
2151 printf("Copy Descriptor %d (%s) (%d bytes)\n", i
,
2152 region_name(i
), copy_size
);
2153 printf(" from %08x+%08x:%08x (%10d)\n", current
->base
,
2154 offset_current
, current
->limit
, current
->size
);
2155 printf(" to %08x+%08x:%08x (%10d)\n", new->base
,
2156 offset_new
, new->limit
, new->size
);
2158 memcpy(new_image
+ new->base
+ offset_new
,
2159 image
+ current
->base
+ offset_current
,
2163 /* update new descriptor regions */
2164 frba
= find_frba(new_image
, new_extent
);
2168 printf("Modify Flash Descriptor regions\n");
2169 for (i
= 1; i
< max_regions
; i
++)
2170 set_region(frba
, i
, &new_regions
[i
]);
2172 write_image(filename
, new_image
, new_extent
);
2176 static void print_version(void)
2178 printf("ifdtool v%s -- ", IFDTOOL_VERSION
);
2179 printf("Copyright (C) 2011 Google Inc.\n\n");
2181 ("This program is free software: you can redistribute it and/or modify\n"
2182 "it under the terms of the GNU General Public License as published by\n"
2183 "the Free Software Foundation, version 2 of the License.\n\n"
2184 "This program is distributed in the hope that it will be useful,\n"
2185 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2186 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
2187 "GNU General Public License for more details.\n\n");
2190 static void print_usage(const char *name
)
2192 printf("usage: %s [-vhdix?] <filename>\n", name
);
2194 " -d | --dump: dump intel firmware descriptor\n"
2195 " -f | --layout <filename> dump regions into a flashrom layout file\n"
2196 " -F | --fmap-layout <filename> dump IFD regions into a fmap layout template (.fmd) file\n"
2197 " -t | --validate Validate that the firmware descriptor layout matches the fmap layout\n"
2198 " -x | --extract: extract intel fd modules\n"
2199 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
2200 " -n | --newlayout <filename> update regions using a flashrom layout file\n"
2201 " -O | --output <filename> output filename\n"
2202 " -s | --spifreq <17|20|30|33|48|50> set the SPI frequency\n"
2203 " -D | --density <512|1|2|4|8|16|32|64> set chip density (512 in KByte, others in MByte)\n"
2204 " -C | --chip <0|1|2> select spi chip on which to operate\n"
2205 " can only be used once per run:\n"
2206 " 0 - both chips (default), 1 - first chip, 2 - second chip\n"
2207 " -e | --em100 set SPI frequency to 20MHz and disable\n"
2208 " Dual Output Fast Read Support\n"
2209 " -l | --lock Lock firmware descriptor and ME region\n"
2210 " -r | --read Enable CPU/BIOS read access for ME region\n"
2211 " -u | --unlock Unlock firmware descriptor and ME region\n"
2212 " -g | --gpr0-disable Disable GPR0 (Global Protected Range) register\n"
2213 " -E | --gpr0-enable Enable GPR0 (Global Protected Range) register\n"
2214 " -c | --gpr0-status Checking GPR0 (Global Protected Range) register status\n"
2215 " -M | --altmedisable <0|1> Set the MeDisable and AltMeDisable (or HAP for skylake or newer platform)\n"
2216 " bits to disable ME\n"
2217 " -p | --platform Add platform-specific quirks\n"
2218 " adl - Alder Lake\n"
2219 " aplk - Apollo Lake\n"
2220 " cnl - Cannon Lake\n"
2221 " lbg - Lewisburg PCH\n"
2222 " dnv - Denverton\n"
2223 " ehl - Elkhart Lake\n"
2224 " glk - Gemini Lake\n"
2226 " ifd2 - IFDv2 Platform\n"
2227 " jsl - Jasper Lake\n"
2228 " mtl - Meteor Lake\n"
2229 " sklkbl - Sky Lake/Kaby Lake\n"
2230 " tgl - Tiger Lake\n"
2231 " wbg - Wellsburg\n"
2232 " -S | --setpchstrap Write a PCH strap\n"
2233 " -V | --newvalue The new value to write into PCH strap specified by -S\n"
2234 " -v | --version: print the version\n"
2235 " -h | --help: print this help\n\n"
2236 "<region> is one of Descriptor, BIOS, ME, GbE, Platform Data, Secondary BIOS, "
2237 "Device Exp1, EC, Device Exp2, IE, 10GbE_0, 10GbE_1, PTT\n"
2241 int main(int argc
, char *argv
[])
2243 int opt
, option_index
= 0;
2244 int mode_dump
= 0, mode_extract
= 0, mode_inject
= 0, mode_spifreq
= 0;
2245 int mode_em100
= 0, mode_locked
= 0, mode_unlocked
= 0, mode_validate
= 0;
2246 int mode_layout
= 0, mode_newlayout
= 0, mode_density
= 0, mode_setstrap
= 0;
2247 int mode_read
= 0, mode_altmedisable
= 0, altmedisable
= 0, mode_fmap_template
= 0;
2248 int mode_gpr0_disable
= 0, mode_gpr0_enable
= 0, mode_gpr0_status
= 0;
2249 char *region_type_string
= NULL
, *region_fname
= NULL
;
2250 const char *layout_fname
= NULL
;
2251 char *new_filename
= NULL
;
2252 int region_type
= -1, inputfreq
= 0;
2253 unsigned int value
= 0;
2254 unsigned int pchstrap
= 0;
2255 unsigned int new_density
= 0;
2256 enum spi_frequency spifreq
= SPI_FREQUENCY_20MHZ
;
2258 static const struct option long_options
[] = {
2259 {"dump", 0, NULL
, 'd'},
2260 {"layout", 1, NULL
, 'f'},
2261 {"fmap-template", 1, NULL
, 'F'},
2262 {"extract", 0, NULL
, 'x'},
2263 {"inject", 1, NULL
, 'i'},
2264 {"newlayout", 1, NULL
, 'n'},
2265 {"output", 1, NULL
, 'O'},
2266 {"spifreq", 1, NULL
, 's'},
2267 {"density", 1, NULL
, 'D'},
2268 {"chip", 1, NULL
, 'C'},
2269 {"altmedisable", 1, NULL
, 'M'},
2270 {"em100", 0, NULL
, 'e'},
2271 {"lock", 0, NULL
, 'l'},
2272 {"read", 0, NULL
, 'r'},
2273 {"unlock", 0, NULL
, 'u'},
2274 {"gpr0-disable", 0, NULL
, 'g'},
2275 {"gpr0-enable", 0, NULL
, 'E'},
2276 {"gpr0-status", 0, NULL
, 'c'},
2277 {"version", 0, NULL
, 'v'},
2278 {"help", 0, NULL
, 'h'},
2279 {"platform", 1, NULL
, 'p'},
2280 {"validate", 0, NULL
, 't'},
2281 {"setpchstrap", 1, NULL
, 'S'},
2282 {"newvalue", 1, NULL
, 'V'},
2286 while ((opt
= getopt_long(argc
, argv
, "S:V:df:F:D:C:M:xi:n:O:s:p:elrugEcvth?",
2287 long_options
, &option_index
)) != EOF
) {
2294 pchstrap
= strtoul(optarg
, NULL
, 0);
2297 value
= strtoul(optarg
, NULL
, 0);
2301 layout_fname
= strdup(optarg
);
2302 if (!layout_fname
) {
2303 fprintf(stderr
, "No layout file specified\n");
2304 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2309 mode_fmap_template
= 1;
2310 layout_fname
= strdup(optarg
);
2311 if (!layout_fname
) {
2312 fprintf(stderr
, "No layout file specified\n");
2313 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2321 // separate type and file name
2322 region_type_string
= strdup(optarg
);
2323 region_fname
= strchr(region_type_string
, ':');
2324 if (!region_fname
) {
2325 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2328 region_fname
[0] = '\0';
2330 // Descriptor, BIOS, ME, GbE, Platform
2332 if (!strcasecmp("Descriptor", region_type_string
))
2334 else if (!strcasecmp("BIOS", region_type_string
))
2336 else if (!strcasecmp("ME", region_type_string
))
2338 else if (!strcasecmp("GbE", region_type_string
))
2340 else if (!strcasecmp("Platform Data", region_type_string
))
2342 else if (!strcasecmp("Device Exp1", region_type_string
))
2344 else if (!strcasecmp("Secondary BIOS", region_type_string
))
2346 else if (!strcasecmp("Reserved", region_type_string
))
2348 else if (!strcasecmp("EC", region_type_string
))
2350 else if (!strcasecmp("Device Exp2", region_type_string
))
2352 else if (!strcasecmp("IE", region_type_string
))
2354 else if (!strcasecmp("10GbE_0", region_type_string
))
2356 else if (!strcasecmp("10GbE_1", region_type_string
))
2358 else if (!strcasecmp("PTT", region_type_string
))
2360 if (region_type
== -1) {
2361 fprintf(stderr
, "No such region type: '%s'\n\n",
2362 region_type_string
);
2363 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2370 layout_fname
= strdup(optarg
);
2371 if (!layout_fname
) {
2372 fprintf(stderr
, "No layout file specified\n");
2373 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2378 new_filename
= strdup(optarg
);
2379 if (!new_filename
) {
2380 fprintf(stderr
, "No output filename specified\n");
2381 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2387 new_density
= strtoul(optarg
, NULL
, 0);
2388 switch (new_density
) {
2390 new_density
= COMPONENT_DENSITY_512KB
;
2393 new_density
= COMPONENT_DENSITY_1MB
;
2396 new_density
= COMPONENT_DENSITY_2MB
;
2399 new_density
= COMPONENT_DENSITY_4MB
;
2402 new_density
= COMPONENT_DENSITY_8MB
;
2405 new_density
= COMPONENT_DENSITY_16MB
;
2408 new_density
= COMPONENT_DENSITY_32MB
;
2411 new_density
= COMPONENT_DENSITY_64MB
;
2414 new_density
= COMPONENT_DENSITY_UNUSED
;
2417 printf("error: Unknown density\n");
2418 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2423 selected_chip
= strtol(optarg
, NULL
, 0);
2424 if (selected_chip
> 2) {
2425 fprintf(stderr
, "error: Invalid chip selection\n");
2426 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2431 mode_altmedisable
= 1;
2432 altmedisable
= strtol(optarg
, NULL
, 0);
2433 if (altmedisable
> 1) {
2434 fprintf(stderr
, "error: Illegal value\n");
2435 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2440 // Parse the requested SPI frequency
2441 inputfreq
= strtol(optarg
, NULL
, 0);
2442 switch (inputfreq
) {
2444 spifreq
= SPI_FREQUENCY_17MHZ
;
2447 spifreq
= SPI_FREQUENCY_20MHZ
;
2450 spifreq
= SPI_FREQUENCY_50MHZ_30MHZ
;
2453 spifreq
= SPI_FREQUENCY_33MHZ
;
2456 spifreq
= SPI_FREQUENCY_48MHZ
;
2459 spifreq
= SPI_FREQUENCY_50MHZ_30MHZ
;
2462 fprintf(stderr
, "Invalid SPI Frequency: %d\n",
2464 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2474 if (mode_unlocked
== 1) {
2475 fprintf(stderr
, "Locking/Unlocking FD and ME are mutually exclusive\n");
2484 if (mode_locked
== 1) {
2485 fprintf(stderr
, "Locking/Unlocking FD and ME are mutually exclusive\n");
2490 mode_gpr0_disable
= 1;
2493 mode_gpr0_enable
= 1;
2496 mode_gpr0_status
= 1;
2499 if (!strcmp(optarg
, "aplk")) {
2500 platform
= PLATFORM_APL
;
2501 } else if (!strcmp(optarg
, "cnl")) {
2502 platform
= PLATFORM_CNL
;
2503 } else if (!strcmp(optarg
, "lbg")) {
2504 platform
= PLATFORM_LBG
;
2505 } else if (!strcmp(optarg
, "dnv")) {
2506 platform
= PLATFORM_DNV
;
2507 } else if (!strcmp(optarg
, "ehl")) {
2508 platform
= PLATFORM_EHL
;
2509 } else if (!strcmp(optarg
, "glk")) {
2510 platform
= PLATFORM_GLK
;
2511 } else if (!strcmp(optarg
, "icl")) {
2512 platform
= PLATFORM_ICL
;
2513 } else if (!strcmp(optarg
, "jsl")) {
2514 platform
= PLATFORM_JSL
;
2515 } else if (!strcmp(optarg
, "sklkbl")) {
2516 platform
= PLATFORM_SKLKBL
;
2517 } else if (!strcmp(optarg
, "tgl")) {
2518 platform
= PLATFORM_TGL
;
2519 } else if (!strcmp(optarg
, "adl")) {
2520 platform
= PLATFORM_ADL
;
2521 } else if (!strcmp(optarg
, "ifd2")) {
2522 platform
= PLATFORM_IFD2
;
2523 } else if (!strcmp(optarg
, "mtl")) {
2524 platform
= PLATFORM_MTL
;
2525 } else if (!strcmp(optarg
, "ptl")) {
2526 platform
= PLATFORM_PTL
;
2527 } else if (!strcmp(optarg
, "wbg")) {
2528 platform
= PLATFORM_WBG
;
2530 fprintf(stderr
, "Unknown platform: %s\n", optarg
);
2543 print_usage(argv
[0]);
2547 print_usage(argv
[0]);
2553 if ((mode_dump
+ mode_layout
+ mode_fmap_template
+ mode_extract
+ mode_inject
+
2554 mode_setstrap
+ mode_newlayout
+ (mode_spifreq
| mode_em100
|
2555 mode_unlocked
| mode_locked
) + mode_altmedisable
+ mode_validate
+
2556 (mode_gpr0_disable
| mode_gpr0_enable
) + mode_gpr0_status
) > 1) {
2557 fprintf(stderr
, "You may not specify more than one mode.\n\n");
2558 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2562 if ((mode_dump
+ mode_layout
+ mode_fmap_template
+ mode_extract
+ mode_inject
+
2563 mode_setstrap
+ mode_newlayout
+ mode_spifreq
+ mode_em100
+
2564 mode_locked
+ mode_unlocked
+ mode_density
+ mode_altmedisable
+
2565 mode_validate
+ (mode_gpr0_disable
| mode_gpr0_enable
) + mode_gpr0_status
) == 0) {
2566 fprintf(stderr
, "You need to specify a mode.\n\n");
2567 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2571 if (optind
+ 1 != argc
) {
2572 fprintf(stderr
, "You need to specify a file.\n\n");
2573 fprintf(stderr
, "run '%s -h' for usage\n", argv
[0]);
2578 fprintf(stderr
, "Warning: No platform specified. Output may be incomplete\n");
2580 char *filename
= argv
[optind
];
2581 int bios_fd
= open(filename
, O_RDONLY
| O_BINARY
);
2582 if (bios_fd
== -1) {
2583 perror("Could not open file");
2587 if (fstat(bios_fd
, &buf
) == -1) {
2588 perror("Could not stat file");
2591 int size
= buf
.st_size
;
2593 printf("File %s is %d bytes\n", filename
, size
);
2595 char *image
= malloc(size
);
2597 printf("Out of memory.\n");
2601 if (read(bios_fd
, image
, size
) != size
) {
2602 perror("Could not read file");
2608 // generate new filename
2609 if (new_filename
== NULL
) {
2610 new_filename
= (char *)malloc((strlen(filename
) + 5) * sizeof(char));
2611 if (!new_filename
) {
2612 printf("Out of memory.\n");
2615 // - 5: leave room for ".new\0"
2616 strcpy(new_filename
, filename
);
2617 strcat(new_filename
, ".new");
2620 check_ifd_version(image
, size
);
2623 dump_fd(image
, size
);
2626 dump_flashrom_layout(image
, size
, layout_fname
);
2628 if (mode_fmap_template
)
2629 create_fmap_template(image
, size
, layout_fname
);
2632 write_regions(image
, size
);
2635 validate_layout(image
, size
);
2638 inject_region(new_filename
, image
, size
, region_type
,
2642 new_layout(new_filename
, image
, size
, layout_fname
);
2645 set_spi_frequency(new_filename
, image
, size
, spifreq
);
2648 set_chipdensity(new_filename
, image
, size
, new_density
);
2651 set_em100_mode(new_filename
, image
, size
);
2654 lock_descriptor(new_filename
, image
, size
);
2657 enable_cpu_read_me(new_filename
, image
, size
);
2660 unlock_descriptor(new_filename
, image
, size
);
2662 if (mode_gpr0_disable
)
2663 disable_gpr0(new_filename
, image
, size
);
2665 if (mode_gpr0_enable
)
2666 enable_gpr0(new_filename
, image
, size
);
2668 if (mode_gpr0_status
)
2669 is_gpr0_protected(image
, size
);
2671 if (mode_setstrap
) {
2672 struct fpsba
*fpsba
= find_fpsba(image
, size
);
2673 const struct fdbar
*fdb
= find_fd(image
, size
);
2674 set_pchstrap(fpsba
, fdb
, pchstrap
, value
);
2675 write_image(new_filename
, image
, size
);
2678 if (mode_altmedisable
) {
2679 struct fpsba
*fpsba
= find_fpsba(image
, size
);
2680 struct fmsba
*fmsba
= find_fmsba(image
, size
);
2681 fpsba_set_altmedisable(fpsba
, fmsba
, altmedisable
);
2682 write_image(new_filename
, image
, size
);