blob: 43451b165f9d1a77bb537468b37fd0d0473d03b8 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* ifdtool - dump Intel Firmware Descriptor information */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07003
4#include <unistd.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <getopt.h>
9#include <fcntl.h>
10#include <sys/types.h>
11#include <sys/stat.h>
Bill XIE4651d452017-09-12 11:54:48 +080012#include <commonlib/helpers.h>
Mathew Kingc7ddc992019-08-08 14:59:25 -060013#include <fmap.h>
Stefan Reinauer1c795ad12011-10-14 12:49:41 -070014#include "ifdtool.h"
15
Scott Duplichanf2c98372014-12-12 21:03:06 -060016#ifndef O_BINARY
17#define O_BINARY 0
18#endif
19
Bill XIE612ec0e2017-08-30 16:10:27 +080020/**
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.
25 *
26 */
27#define PTR_IN_RANGE(ptr, base, limit) \
28 ((const char *)(ptr) >= (base) && \
29 (const char *)&(ptr)[1] <= (base) + (limit))
30
Jeff Dalyabd4b962022-01-06 00:52:30 -050031/**
32 * PLATFORM_HAS_GBE_REGION - some platforms do not support the PCH GbE LAN region
33 */
34#define PLATFORM_HAS_GBE_REGION (platform != PLATFORM_DNV)
35
36/*
37 * PLATFORM_HAS_EC_REGION - some platforms do not support the EC region
38 */
39#define PLATFORM_HAS_EC_REGION (ifd_version >= IFD_VERSION_2 && platform != PLATFORM_DNV)
40
41/*
42 * PLATFORM_HAS_10GBE_X_REGION - some platforms have 1 or more 10GbE LAN regions
43 */
44#define PLATFORM_HAS_10GBE_0_REGION (platform == PLATFORM_DNV)
45#define PLATFORM_HAS_10GBE_1_REGION (platform == PLATFORM_DNV)
46
Maximilian Bruneab0e6802023-03-05 04:34:40 +010047static int max_regions_from_fdbar(const struct fdbar *fdb);
Patrick Rudolph98ecaa42022-10-24 15:06:15 +020048
Duncan Laurie1f7fd722015-06-22 11:14:48 -070049static int ifd_version;
Bill XIEb3e15a22017-09-07 18:34:50 +080050static int chipset;
Bill XIEfa5f9942017-09-12 11:22:29 +080051static unsigned int max_regions = 0;
Jan Tatjefa317512016-03-11 00:52:07 +010052static int selected_chip = 0;
Andrey Petrov96ecb772016-10-31 19:31:54 -070053static int platform = -1;
Chris Douglasse2718652014-02-28 08:54:41 -050054
Duncan Laurie1f7fd722015-06-22 11:14:48 -070055static const struct region_name region_names[MAX_REGIONS] = {
Mathew Kingc7ddc992019-08-08 14:59:25 -060056 { "Flash Descriptor", "fd", "flashregion_0_flashdescriptor.bin", "SI_DESC" },
57 { "BIOS", "bios", "flashregion_1_bios.bin", "SI_BIOS" },
58 { "Intel ME", "me", "flashregion_2_intel_me.bin", "SI_ME" },
59 { "GbE", "gbe", "flashregion_3_gbe.bin", "SI_GBE" },
60 { "Platform Data", "pd", "flashregion_4_platform_data.bin", "SI_PDR" },
Jeff Daly3623eca2022-01-05 23:51:40 -050061 { "Device Exp1", "devexp", "flashregion_5_device_exp.bin", "SI_DEVICEEXT" },
62 { "Secondary BIOS", "bios2", "flashregion_6_bios2.bin", "SI_BIOS2" },
63 { "Reserved", "res7", "flashregion_7_reserved.bin", NULL },
Mathew Kingc7ddc992019-08-08 14:59:25 -060064 { "EC", "ec", "flashregion_8_ec.bin", "SI_EC" },
Jeff Daly3623eca2022-01-05 23:51:40 -050065 { "Device Exp2", "devexp2", "flashregion_9_device_exp.bin", "SI_DEVICEEXT2" },
66 { "IE", "ie", "flashregion_10_ie.bin", "SI_IE" },
67 { "10GbE_0", "10gbe_0", "flashregion_11_10gbe0.bin", "SI_10GBE0" },
68 { "10GbE_1", "10gbe_1", "flashregion_12_10gbe1.bin", "SI_10GBE1" },
69 { "Reserved", "res13", "flashregion_13_reserved.bin", NULL },
70 { "Reserved", "res14", "flashregion_14_reserved.bin", NULL },
71 { "PTT", "ptt", "flashregion_15_ptt.bin", "SI_PTT" },
Chris Douglass03ce0142014-02-26 13:30:13 -050072};
73
Bill XIEb3e15a22017-09-07 18:34:50 +080074/* port from flashrom */
75static const char *const ich_chipset_names[] = {
76 "Unknown ICH",
Bill XIEb3e15a22017-09-07 18:34:50 +080077 "ICH8",
78 "ICH9",
79 "ICH10",
Subrata Banik89db2252020-08-26 14:49:17 +053080 "Unknown PCH",
Bill XIEb3e15a22017-09-07 18:34:50 +080081 "5 series Ibex Peak",
82 "6 series Cougar Point",
83 "7 series Panther Point",
84 "8 series Lynx Point",
85 "Baytrail",
86 "8 series Lynx Point LP",
87 "8 series Wellsburg",
88 "9 series Wildcat Point",
89 "9 series Wildcat Point LP",
Subrata Banik8c082e52021-06-10 23:02:29 +053090 "Apollo Lake: N3xxx, J3xxx",
Subrata Banik89db2252020-08-26 14:49:17 +053091 "Gemini Lake: N5xxx, J5xxx, N4xxx, J4xxx",
Subrata Banik8c082e52021-06-10 23:02:29 +053092 "Jasper Lake: N6xxx, N51xx, N45xx",
93 "Elkhart Lake: x6000 series Atom",
Subrata Banik89db2252020-08-26 14:49:17 +053094 "100/200 series Sunrise Point",
Subrata Banik8c082e52021-06-10 23:02:29 +053095 "300 series Cannon Point",
96 "400 series Ice Point",
Subrata Banika5f47812020-09-29 11:43:01 +053097 "500 series Tiger Point/ 600 series Alder Point",
Subrata Banik9cd85d02023-05-24 23:18:49 +053098 "800 series Meteor Lake",
Bill XIEb3e15a22017-09-07 18:34:50 +080099 "C620 series Lewisburg",
Patrick Rudolph19209002022-10-22 10:34:05 +0200100 "Denverton: C39xx",
Bill XIEb3e15a22017-09-07 18:34:50 +0800101 NULL
102};
103
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100104static struct fdbar *find_fd(char *image, int size)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700105{
106 int i, found = 0;
107
108 /* Scan for FD signature */
109 for (i = 0; i < (size - 4); i += 4) {
110 if (*(uint32_t *) (image + i) == 0x0FF0A55A) {
111 found = 1;
112 break; // signature found.
113 }
114 }
115
116 if (!found) {
117 printf("No Flash Descriptor found in this image\n");
118 return NULL;
119 }
120
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100121 struct fdbar *fdb = (struct fdbar *) (image + i);
Bill XIE612ec0e2017-08-30 16:10:27 +0800122 return PTR_IN_RANGE(fdb, image, size) ? fdb : NULL;
123}
124
Stefan Tauner0d226142018-08-05 18:56:53 +0200125static char *find_flumap(char *image, int size)
126{
127 /* The upper map is located in the word before the 256B-long OEM section
128 * at the end of the 4kB-long flash descriptor. In the official
129 * documentation this is defined as FDBAR + 0xEFC. However, starting
130 * with B-Step of Ibex Peak (5 series) the signature (and thus FDBAR)
131 * has moved 16 bytes back to offset 0x10 of the image. Although
132 * official documentation still maintains the offset relative to FDBAR
133 * this is wrong and a simple fixed offset from the start of the image
134 * works.
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100135 */
Stefan Tauner0d226142018-08-05 18:56:53 +0200136 char *flumap = image + 4096 - 256 - 4;
137 return PTR_IN_RANGE(flumap, image, size) ? flumap : NULL;
138}
139
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100140static struct fcba *find_fcba(char *image, int size)
Bill XIE612ec0e2017-08-30 16:10:27 +0800141{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100142 struct fdbar *fdb = find_fd(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +0800143 if (!fdb)
144 return NULL;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100145 struct fcba *fcba = (struct fcba *) (image + ((fdb->flmap0 & 0xff) << 4));
Bill XIE612ec0e2017-08-30 16:10:27 +0800146 return PTR_IN_RANGE(fcba, image, size) ? fcba : NULL;
147
148}
149
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100150static struct fmba *find_fmba(char *image, int size)
Bill XIE612ec0e2017-08-30 16:10:27 +0800151{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100152 struct fdbar *fdb = find_fd(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +0800153 if (!fdb)
154 return NULL;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100155 struct fmba *fmba = (struct fmba *) (image + ((fdb->flmap1 & 0xff) << 4));
Bill XIE612ec0e2017-08-30 16:10:27 +0800156 return PTR_IN_RANGE(fmba, image, size) ? fmba : NULL;
157}
158
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100159static struct frba *find_frba(char *image, int size)
Bill XIE612ec0e2017-08-30 16:10:27 +0800160{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100161 struct fdbar *fdb = find_fd(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +0800162 if (!fdb)
163 return NULL;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100164 struct frba *frba =
165 (struct frba *) (image + (((fdb->flmap0 >> 16) & 0xff) << 4));
Bill XIE612ec0e2017-08-30 16:10:27 +0800166 return PTR_IN_RANGE(frba, image, size) ? frba : NULL;
167}
168
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100169static struct fpsba *find_fpsba(char *image, int size)
Bill XIE612ec0e2017-08-30 16:10:27 +0800170{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100171 struct fdbar *fdb = find_fd(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +0800172 if (!fdb)
173 return NULL;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100174 struct fpsba *fpsba =
175 (struct fpsba *) (image + (((fdb->flmap1 >> 16) & 0xff) << 4));
Patrick Rudolph802cbee2020-05-25 12:18:11 +0200176
177 int SSL = ((fdb->flmap1 >> 24) & 0xff) * sizeof(uint32_t);
178 if ((((char *)fpsba) + SSL) >= (image + size))
179 return NULL;
180 return fpsba;
Bill XIE612ec0e2017-08-30 16:10:27 +0800181}
182
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100183static struct fmsba *find_fmsba(char *image, int size)
Bill XIE612ec0e2017-08-30 16:10:27 +0800184{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100185 struct fdbar *fdb = find_fd(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +0800186 if (!fdb)
187 return NULL;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100188 struct fmsba *fmsba = (struct fmsba *) (image + ((fdb->flmap2 & 0xff) << 4));
Bill XIE612ec0e2017-08-30 16:10:27 +0800189 return PTR_IN_RANGE(fmsba, image, size) ? fmsba : NULL;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700190}
191
Bill XIEb3e15a22017-09-07 18:34:50 +0800192/* port from flashrom */
Subrata Banik8c082e52021-06-10 23:02:29 +0530193static enum ich_chipset ifd1_guess_chipset(char *image, int size)
Bill XIEb3e15a22017-09-07 18:34:50 +0800194{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100195 const struct fdbar *fdb = find_fd(image, size);
Subrata Banik8c082e52021-06-10 23:02:29 +0530196 if (!fdb)
197 exit(EXIT_FAILURE);
Bill XIEb3e15a22017-09-07 18:34:50 +0800198 uint32_t iccriba = (fdb->flmap2 >> 16) & 0xff;
199 uint32_t msl = (fdb->flmap2 >> 8) & 0xff;
200 uint32_t isl = (fdb->flmap1 >> 24);
Subrata Banik89db2252020-08-26 14:49:17 +0530201
202 /* Rest for IFD1 chipset type */
Bill XIEb3e15a22017-09-07 18:34:50 +0800203 if (iccriba == 0x00) {
204 if (msl == 0 && isl <= 2)
205 return CHIPSET_ICH8;
206 else if (isl <= 2)
207 return CHIPSET_ICH9;
208 else if (isl <= 10)
209 return CHIPSET_ICH10;
210 else if (isl <= 16)
211 return CHIPSET_5_SERIES_IBEX_PEAK;
212 printf("Peculiar firmware descriptor, assuming Ibex Peak compatibility.\n");
213 return CHIPSET_5_SERIES_IBEX_PEAK;
214 } else if (iccriba < 0x31 && (fdb->flmap2 & 0xff) < 0x30) {
215 if (msl == 0 && isl <= 17)
216 return CHIPSET_BAYTRAIL;
217 else if (msl <= 1 && isl <= 18)
218 return CHIPSET_6_SERIES_COUGAR_POINT;
219 else if (msl <= 1 && isl <= 21)
220 return CHIPSET_8_SERIES_LYNX_POINT;
221 printf("Peculiar firmware descriptor, assuming Wildcat Point compatibility.\n");
222 return CHIPSET_9_SERIES_WILDCAT_POINT;
Bill XIEb3e15a22017-09-07 18:34:50 +0800223 }
Jan Tatjeb7eec4a2021-07-05 08:03:07 +0200224 return CHIPSET_PCH_UNKNOWN;
Bill XIEb3e15a22017-09-07 18:34:50 +0800225}
226
Subrata Banik8c082e52021-06-10 23:02:29 +0530227static enum ich_chipset ifd2_platform_to_chipset(const int pindex)
228{
229 switch (pindex) {
Subrata Banik6da003c2021-07-14 13:52:03 +0530230 case PLATFORM_APL:
231 return CHIPSET_N_J_SERIES_APOLLO_LAKE;
Subrata Banik8c082e52021-06-10 23:02:29 +0530232 case PLATFORM_GLK:
233 return CHIPSET_N_J_SERIES_GEMINI_LAKE;
234 case PLATFORM_JSL:
235 return CHIPSET_N_SERIES_JASPER_LAKE;
236 case PLATFORM_EHL:
237 return CHIPSET_x6000_SERIES_ELKHART_LAKE;
Jan Tatjeb7eec4a2021-07-05 08:03:07 +0200238 case PLATFORM_SKLKBL:
239 return CHIPSET_100_200_SERIES_SUNRISE_POINT;
Subrata Banik8c082e52021-06-10 23:02:29 +0530240 case PLATFORM_CNL:
241 return CHIPSET_300_SERIES_CANNON_POINT;
242 case PLATFORM_TGL:
243 case PLATFORM_ADL:
Wonkyu Kim3922aa52022-02-02 15:19:05 -0800244 case PLATFORM_IFD2:
Subrata Banik8c082e52021-06-10 23:02:29 +0530245 return CHIPSET_500_600_SERIES_TIGER_ALDER_POINT;
Subrata Banik9cd85d02023-05-24 23:18:49 +0530246 case PLATFORM_MTL:
247 return CHIPSET_800_SERIES_METEOR_LAKE;
Subrata Banik8c082e52021-06-10 23:02:29 +0530248 case PLATFORM_ICL:
249 return CHIPSET_400_SERIES_ICE_POINT;
Johnny Line273a022021-06-22 11:26:46 +0800250 case PLATFORM_LBG:
251 return CHIPSET_C620_SERIES_LEWISBURG;
Jeff Dalyabd4b962022-01-06 00:52:30 -0500252 case PLATFORM_DNV:
253 return CHIPSET_DENVERTON;
Patrick Rudolph16598742022-10-21 15:13:43 +0200254 case PLATFORM_WBG:
255 return CHIPSET_8_SERIES_WELLSBURG;
Subrata Banik8c082e52021-06-10 23:02:29 +0530256 default:
257 return CHIPSET_PCH_UNKNOWN;
258 }
259}
260
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700261/*
Furquan Shaikhc0257dd2018-05-02 23:29:04 -0700262 * Some newer platforms have re-defined the FCBA field that was used to
263 * distinguish IFD v1 v/s v2. Define a list of platforms that we know do not
264 * have the required FCBA field, but are IFD v2 and return true if current
265 * platform is one of them.
266 */
267static int is_platform_ifd_2(void)
268{
269 static const int ifd_2_platforms[] = {
Subrata Banik6da003c2021-07-14 13:52:03 +0530270 PLATFORM_APL,
Furquan Shaikhc0257dd2018-05-02 23:29:04 -0700271 PLATFORM_GLK,
272 PLATFORM_CNL,
Johnny Line273a022021-06-22 11:26:46 +0800273 PLATFORM_LBG,
Jeff Dalyabd4b962022-01-06 00:52:30 -0500274 PLATFORM_DNV,
Aamir Bohra1018be22018-06-29 15:08:50 +0530275 PLATFORM_ICL,
Ravi Sarawadi7d9d63b2019-10-22 13:45:36 -0700276 PLATFORM_TGL,
rkanabard64b0462019-08-30 11:40:08 +0530277 PLATFORM_JSL,
Lean Sheng Tan0faba3c2021-06-09 07:52:24 -0700278 PLATFORM_EHL,
Subrata Banik46f80732020-03-14 15:01:42 +0530279 PLATFORM_ADL,
Jan Tatjeb7eec4a2021-07-05 08:03:07 +0200280 PLATFORM_SKLKBL,
Wonkyu Kim3922aa52022-02-02 15:19:05 -0800281 PLATFORM_IFD2,
Subrata Banikca82e612022-01-20 18:51:21 +0530282 PLATFORM_MTL,
Patrick Rudolph16598742022-10-21 15:13:43 +0200283 PLATFORM_WBG,
Furquan Shaikhc0257dd2018-05-02 23:29:04 -0700284 };
285 unsigned int i;
286
287 for (i = 0; i < ARRAY_SIZE(ifd_2_platforms); i++) {
288 if (platform == ifd_2_platforms[i])
289 return 1;
290 }
291
292 return 0;
293}
294
Furquan Shaikhc0257dd2018-05-02 23:29:04 -0700295static void check_ifd_version(char *image, int size)
296{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100297 const struct fdbar *fdb = find_fd(image, size);
Patrick Rudolph98ecaa42022-10-24 15:06:15 +0200298
Subrata Banik8c082e52021-06-10 23:02:29 +0530299 if (is_platform_ifd_2()) {
Subrata Banik8c082e52021-06-10 23:02:29 +0530300 chipset = ifd2_platform_to_chipset(platform);
Patrick Rudolph16598742022-10-21 15:13:43 +0200301 if (chipset == CHIPSET_8_SERIES_WELLSBURG)
302 ifd_version = IFD_VERSION_1_5;
303 else
304 ifd_version = IFD_VERSION_2;
Patrick Rudolph98ecaa42022-10-24 15:06:15 +0200305 max_regions = MIN(max_regions_from_fdbar(fdb), MAX_REGIONS);
Subrata Banik8c082e52021-06-10 23:02:29 +0530306 } else {
307 ifd_version = IFD_VERSION_1;
308 chipset = ifd1_guess_chipset(image, size);
Patrick Rudolph98ecaa42022-10-24 15:06:15 +0200309 max_regions = MIN(max_regions_from_fdbar(fdb), MAX_REGIONS_OLD);
Subrata Banik8c082e52021-06-10 23:02:29 +0530310 }
Furquan Shaikhc0257dd2018-05-02 23:29:04 -0700311}
312
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100313static struct region get_region(const struct frba *frba, unsigned int region_type)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700314{
Aaron Durbin0a31e592015-08-12 11:44:02 -0500315 int base_mask;
316 int limit_mask;
Ronald G. Minnich8db3c2a2017-05-25 10:48:57 -0700317 uint32_t flreg;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100318 struct region region;
Aaron Durbin0a31e592015-08-12 11:44:02 -0500319
320 if (ifd_version >= IFD_VERSION_2)
321 base_mask = 0x7fff;
322 else
323 base_mask = 0xfff;
324
325 limit_mask = base_mask << 16;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700326
Youness Alaouif68e3ba2017-10-04 11:35:59 -0400327 if (region_type >= max_regions) {
Bill XIE4651d452017-09-12 11:54:48 +0800328 fprintf(stderr, "Invalid region type %d.\n", region_type);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700329 exit (EXIT_FAILURE);
330 }
331
Bill XIE4651d452017-09-12 11:54:48 +0800332 flreg = frba->flreg[region_type];
Ronald G. Minnich8db3c2a2017-05-25 10:48:57 -0700333 region.base = (flreg & base_mask) << 12;
334 region.limit = ((flreg & limit_mask) >> 4) | 0xfff;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700335 region.size = region.limit - region.base + 1;
Maximilian Brune347596a2023-03-05 20:55:32 +0100336 region.type = region_type;
Aaron Durbin0a31e592015-08-12 11:44:02 -0500337
Chris Douglass03ce0142014-02-26 13:30:13 -0500338 if (region.size < 0)
339 region.size = 0;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700340
341 return region;
342}
343
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100344static void set_region(struct frba *frba, unsigned int region_type,
345 const struct region *region)
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500346{
Youness Alaouif68e3ba2017-10-04 11:35:59 -0400347 if (region_type >= max_regions) {
Bill XIE4651d452017-09-12 11:54:48 +0800348 fprintf(stderr, "Invalid region type %u.\n", region_type);
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500349 exit (EXIT_FAILURE);
350 }
Bill XIE4651d452017-09-12 11:54:48 +0800351
352 frba->flreg[region_type] =
353 (((region->limit >> 12) & 0x7fff) << 16) |
354 ((region->base >> 12) & 0x7fff);
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500355}
356
Bill XIEfa5f9942017-09-12 11:22:29 +0800357static const char *region_name(unsigned int region_type)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700358{
Bill XIEfa5f9942017-09-12 11:22:29 +0800359 if (region_type >= max_regions) {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700360 fprintf(stderr, "Invalid region type.\n");
361 exit (EXIT_FAILURE);
362 }
363
Chris Douglass03ce0142014-02-26 13:30:13 -0500364 return region_names[region_type].pretty;
365}
366
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500367static int region_num(const char *name)
368{
Bill XIEfa5f9942017-09-12 11:22:29 +0800369 unsigned int i;
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500370
Alexander Couzensfd5c6582016-10-08 00:37:24 +0200371 for (i = 0; i < max_regions; i++) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -0500372 if (strcasecmp(name, region_names[i].pretty) == 0)
373 return i;
374 if (strcasecmp(name, region_names[i].terse) == 0)
375 return i;
376 }
377
378 return -1;
379}
380
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100381static void dump_region(unsigned int num, const struct frba *frba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700382{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100383 struct region region = get_region(frba, num);
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700384 printf(" Flash Region %d (%s): %08x - %08x %s\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100385 num, region_name(num), region.base, region.limit,
386 region.size < 1 ? "(unused)" : "");
Chris Douglass03ce0142014-02-26 13:30:13 -0500387}
388
Patrick Rudolph98ecaa42022-10-24 15:06:15 +0200389static int sort_compare(const void *a, const void *b)
390{
391 return *(size_t *)a - *(size_t *)b;
392}
393
394/*
395 * IFDv1 always has 8 regions, while IFDv2 always has 16 regions.
396 *
397 * It's platform specific which regions are used or are reserved.
398 * The 'SPI programming guide' as the name says is a guide only,
399 * not a specification what the hardware actually does.
400 * The best to do is not to rely on the guide, but detect how many
401 * regions are present in the IFD and expose them all.
402 *
403 * Very early IFDv2 chipsets, sometimes unofficially referred to as
404 * IFDv1.5 platforms, only have 8 regions. To not corrupt the IFD when
405 * operating on an IFDv1.5 detect how much space is actually present
406 * in the IFD.
407 */
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100408static int max_regions_from_fdbar(const struct fdbar *fdb)
Patrick Rudolph98ecaa42022-10-24 15:06:15 +0200409{
410 const size_t fcba = (fdb->flmap0 & 0xff) << 4;
411 const size_t fmba = (fdb->flmap1 & 0xff) << 4;
412 const size_t frba = ((fdb->flmap0 >> 16) & 0xff) << 4;
413 const size_t fpsba = ((fdb->flmap1 >> 16) & 0xff) << 4;
414 const size_t flumap = 4096 - 256 - 4;
415 size_t sorted[5] = {fcba, fmba, frba, fpsba, flumap};
416
417 qsort(sorted, ARRAY_SIZE(sorted), sizeof(size_t), sort_compare);
418
419 for (size_t i = 0; i < 4; i++) {
420 /*
421 * Find FRBA in the sorted array and determine the size of the
422 * region by the start of the next region. Every region requires
423 * 4 bytes of space.
424 */
425 if (sorted[i] == frba)
426 return MIN((sorted[i+1] - sorted[i])/4, MAX_REGIONS);
427 }
428 /* Never reaches this point */
429 return 0;
430}
431
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100432static void dump_frba(const struct frba *frba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700433{
Bill XIE4651d452017-09-12 11:54:48 +0800434 unsigned int i;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100435 struct region region;
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700436 printf("Found Region Section\n");
Bill XIE4651d452017-09-12 11:54:48 +0800437 for (i = 0; i < max_regions; i++) {
Subrata Banikd52df3cd2020-08-26 14:02:01 +0530438 region = get_region(frba, i);
439 /* Skip unused & reserved Flash Region */
440 if (region.size < 1 && !strcmp(region_name(i), "Reserved"))
441 continue;
442
Bill XIE4651d452017-09-12 11:54:48 +0800443 printf("FLREG%u: 0x%08x\n", i, frba->flreg[i]);
444 dump_region(i, frba);
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700445 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700446}
447
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100448static void dump_flashrom_layout(char *image, int size, const char *layout_fname)
Chris Douglass03ce0142014-02-26 13:30:13 -0500449{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100450 const struct frba *frba = find_frba(image, size);
451 if (!frba)
452 exit(EXIT_FAILURE);
Chris Douglass03ce0142014-02-26 13:30:13 -0500453
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100454 int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Chris Douglass03ce0142014-02-26 13:30:13 -0500455 if (layout_fd == -1) {
456 perror("Could not open file");
457 exit(EXIT_FAILURE);
458 }
459
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100460 for (unsigned int i = 0; i < max_regions; i++) {
461 struct region region = get_region(frba, i);
Maximilian Brune2c895aa2023-04-24 19:28:21 +0200462
463 /* A region limit of 0 is an indicator of an unused region
464 * A region base of 7FFFh is an indicator of a reserved region
465 */
466 if (region.limit == 0 || region.base == 0x07FFF000)
Alexander Couzenseff596b2016-10-08 00:53:09 +0200467 continue;
468
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100469 char buf[LAYOUT_LINELEN];
470 snprintf(buf, LAYOUT_LINELEN, "%08x:%08x %s\n", region.base, region.limit, region_names[i].terse);
Chris Douglass03ce0142014-02-26 13:30:13 -0500471 if (write(layout_fd, buf, strlen(buf)) < 0) {
472 perror("Could not write to file");
473 exit(EXIT_FAILURE);
474 }
475 }
476 close(layout_fd);
477 printf("Wrote layout to %s\n", layout_fname);
478}
479
Subrata Banikd16ef4d2020-08-26 15:53:00 +0530480static void _decode_spi_frequency(unsigned int freq)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700481{
482 switch (freq) {
483 case SPI_FREQUENCY_20MHZ:
484 printf("20MHz");
485 break;
486 case SPI_FREQUENCY_33MHZ:
487 printf("33MHz");
488 break;
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700489 case SPI_FREQUENCY_48MHZ:
490 printf("48MHz");
491 break;
492 case SPI_FREQUENCY_50MHZ_30MHZ:
493 switch (ifd_version) {
494 case IFD_VERSION_1:
Patrick Rudolph16598742022-10-21 15:13:43 +0200495 case IFD_VERSION_1_5:
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700496 printf("50MHz");
497 break;
498 case IFD_VERSION_2:
499 printf("30MHz");
500 break;
501 }
502 break;
503 case SPI_FREQUENCY_17MHZ:
504 printf("17MHz");
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700505 break;
506 default:
507 printf("unknown<%x>MHz", freq);
508 }
509}
510
Subrata Banikd16ef4d2020-08-26 15:53:00 +0530511static void _decode_spi_frequency_500_series(unsigned int freq)
512{
513 switch (freq) {
514 case SPI_FREQUENCY_100MHZ:
515 printf("100MHz");
516 break;
517 case SPI_FREQUENCY_50MHZ:
518 printf("50MHz");
519 break;
520 case SPI_FREQUENCY_500SERIES_33MHZ:
521 printf("33MHz");
522 break;
523 case SPI_FREQUENCY_25MHZ:
524 printf("25MHz");
525 break;
526 case SPI_FREQUENCY_14MHZ:
527 printf("14MHz");
528 break;
529 default:
530 printf("unknown<%x>MHz", freq);
531 }
532}
533
534static void decode_spi_frequency(unsigned int freq)
535{
Subrata Banik9cd85d02023-05-24 23:18:49 +0530536 switch (chipset) {
537 case CHIPSET_500_600_SERIES_TIGER_ALDER_POINT:
538 case CHIPSET_800_SERIES_METEOR_LAKE:
Subrata Banikd16ef4d2020-08-26 15:53:00 +0530539 _decode_spi_frequency_500_series(freq);
Subrata Banik9cd85d02023-05-24 23:18:49 +0530540 break;
541 default:
Subrata Banikd16ef4d2020-08-26 15:53:00 +0530542 _decode_spi_frequency(freq);
Subrata Banik9cd85d02023-05-24 23:18:49 +0530543 }
Subrata Banikd16ef4d2020-08-26 15:53:00 +0530544}
545
Subrata Banike5d39922020-08-26 16:01:42 +0530546static void _decode_espi_frequency(unsigned int freq)
547{
548 switch (freq) {
549 case ESPI_FREQUENCY_20MHZ:
550 printf("20MHz");
551 break;
552 case ESPI_FREQUENCY_24MHZ:
553 printf("24MHz");
554 break;
555 case ESPI_FREQUENCY_30MHZ:
556 printf("30MHz");
557 break;
558 case ESPI_FREQUENCY_48MHZ:
559 printf("48MHz");
560 break;
561 case ESPI_FREQUENCY_60MHZ:
562 printf("60MHz");
563 break;
564 case ESPI_FREQUENCY_17MHZ:
565 printf("17MHz");
566 break;
567 default:
568 printf("unknown<%x>MHz", freq);
569 }
570}
571
572static void _decode_espi_frequency_500_series(unsigned int freq)
573{
574 switch (freq) {
575 case ESPI_FREQUENCY_500SERIES_20MHZ:
576 printf("20MHz");
577 break;
578 case ESPI_FREQUENCY_500SERIES_24MHZ:
579 printf("24MHz");
580 break;
581 case ESPI_FREQUENCY_500SERIES_25MHZ:
582 printf("25MHz");
583 break;
584 case ESPI_FREQUENCY_500SERIES_48MHZ:
585 printf("48MHz");
586 break;
587 case ESPI_FREQUENCY_500SERIES_60MHZ:
588 printf("60MHz");
589 break;
590 default:
591 printf("unknown<%x>MHz", freq);
592 }
593}
594
Subrata Banik9cd85d02023-05-24 23:18:49 +0530595static void _decode_espi_frequency_800_series(unsigned int freq)
596{
597 switch (freq) {
598 case ESPI_FREQUENCY_800SERIES_20MHZ:
599 printf("20MHz");
600 break;
601 case ESPI_FREQUENCY_800SERIES_25MHZ:
602 printf("25MHz");
603 break;
604 case ESPI_FREQUENCY_800SERIES_33MHZ:
605 printf("33MHz");
606 break;
607 case ESPI_FREQUENCY_800SERIES_50MHZ:
608 printf("50MHz");
609 break;
610 default:
611 printf("unknown<%x>MHz", freq);
612 }
613}
614
Subrata Banike5d39922020-08-26 16:01:42 +0530615static void decode_espi_frequency(unsigned int freq)
616{
Subrata Banika5f47812020-09-29 11:43:01 +0530617 if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT)
Subrata Banike5d39922020-08-26 16:01:42 +0530618 _decode_espi_frequency_500_series(freq);
Subrata Banik9cd85d02023-05-24 23:18:49 +0530619 else if (chipset == CHIPSET_800_SERIES_METEOR_LAKE)
620 _decode_espi_frequency_800_series(freq);
Subrata Banike5d39922020-08-26 16:01:42 +0530621 else
622 _decode_espi_frequency(freq);
623}
624
Stefan Reinauer1b1309f2012-05-11 15:53:43 -0700625static void decode_component_density(unsigned int density)
626{
627 switch (density) {
628 case COMPONENT_DENSITY_512KB:
629 printf("512KB");
630 break;
631 case COMPONENT_DENSITY_1MB:
632 printf("1MB");
633 break;
634 case COMPONENT_DENSITY_2MB:
635 printf("2MB");
636 break;
637 case COMPONENT_DENSITY_4MB:
638 printf("4MB");
639 break;
640 case COMPONENT_DENSITY_8MB:
641 printf("8MB");
642 break;
643 case COMPONENT_DENSITY_16MB:
644 printf("16MB");
645 break;
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700646 case COMPONENT_DENSITY_32MB:
647 printf("32MB");
648 break;
649 case COMPONENT_DENSITY_64MB:
650 printf("64MB");
651 break;
652 case COMPONENT_DENSITY_UNUSED:
653 printf("UNUSED");
654 break;
Stefan Reinauer1b1309f2012-05-11 15:53:43 -0700655 default:
656 printf("unknown<%x>MB", density);
657 }
658}
659
Subrata Banik26058dc2020-08-26 15:12:16 +0530660static int is_platform_with_pch(void)
661{
662 if (chipset >= CHIPSET_5_SERIES_IBEX_PEAK)
663 return 1;
664
665 return 0;
666}
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530667
668/* FLMAP0 register bit 24 onwards are reserved from SPT PCH */
669static int is_platform_with_100x_series_pch(void)
670{
671 if (chipset >= CHIPSET_100_200_SERIES_SUNRISE_POINT &&
Subrata Banik9cd85d02023-05-24 23:18:49 +0530672 chipset <= CHIPSET_800_SERIES_METEOR_LAKE)
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530673 return 1;
674
675 return 0;
676}
677
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100678static void dump_fcba(const struct fcba *fcba, const struct fpsba *fpsba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700679{
Subrata Banike5d39922020-08-26 16:01:42 +0530680 unsigned int freq;
681
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700682 printf("\nFound Component Section\n");
683 printf("FLCOMP 0x%08x\n", fcba->flcomp);
Stefan Reinauer1b1309f2012-05-11 15:53:43 -0700684 printf(" Dual Output Fast Read Support: %ssupported\n",
685 (fcba->flcomp & (1 << 30))?"":"not ");
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700686 printf(" Read ID/Read Status Clock Frequency: ");
687 decode_spi_frequency((fcba->flcomp >> 27) & 7);
688 printf("\n Write/Erase Clock Frequency: ");
689 decode_spi_frequency((fcba->flcomp >> 24) & 7);
690 printf("\n Fast Read Clock Frequency: ");
691 decode_spi_frequency((fcba->flcomp >> 21) & 7);
Stefan Reinauer1b1309f2012-05-11 15:53:43 -0700692 printf("\n Fast Read Support: %ssupported",
693 (fcba->flcomp & (1 << 20))?"":"not ");
Subrata Banike5d39922020-08-26 16:01:42 +0530694 if (is_platform_with_100x_series_pch() &&
695 chipset != CHIPSET_100_200_SERIES_SUNRISE_POINT) {
696 printf("\n Read eSPI/EC Bus Frequency: ");
Subrata Banika5f47812020-09-29 11:43:01 +0530697 if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT)
Subrata Banike5d39922020-08-26 16:01:42 +0530698 freq = (fpsba->pchstrp[22] & 0x38) >> 3;
Subrata Banik9cd85d02023-05-24 23:18:49 +0530699 else if (chipset == CHIPSET_800_SERIES_METEOR_LAKE)
700 freq = (fpsba->pchstrp[65] & 0x38) >> 3;
Subrata Banike5d39922020-08-26 16:01:42 +0530701 else
702 freq = (fcba->flcomp >> 17) & 7;
703 decode_espi_frequency(freq);
704 } else {
705 printf("\n Read Clock Frequency: ");
706 decode_spi_frequency((fcba->flcomp >> 17) & 7);
707 }
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700708
709 switch (ifd_version) {
710 case IFD_VERSION_1:
711 printf("\n Component 2 Density: ");
712 decode_component_density((fcba->flcomp >> 3) & 7);
713 printf("\n Component 1 Density: ");
714 decode_component_density(fcba->flcomp & 7);
715 break;
Patrick Rudolph16598742022-10-21 15:13:43 +0200716 case IFD_VERSION_1_5:
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700717 case IFD_VERSION_2:
718 printf("\n Component 2 Density: ");
719 decode_component_density((fcba->flcomp >> 4) & 0xf);
720 printf("\n Component 1 Density: ");
721 decode_component_density(fcba->flcomp & 0xf);
722 break;
723 }
724
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700725 printf("\n");
726 printf("FLILL 0x%08x\n", fcba->flill);
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700727 printf(" Invalid Instruction 3: 0x%02x\n",
728 (fcba->flill >> 24) & 0xff);
729 printf(" Invalid Instruction 2: 0x%02x\n",
730 (fcba->flill >> 16) & 0xff);
731 printf(" Invalid Instruction 1: 0x%02x\n",
732 (fcba->flill >> 8) & 0xff);
733 printf(" Invalid Instruction 0: 0x%02x\n",
734 fcba->flill & 0xff);
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530735 if (is_platform_with_100x_series_pch()) {
736 printf("FLILL1 0x%08x\n", fcba->flpb);
737 printf(" Invalid Instruction 7: 0x%02x\n",
738 (fcba->flpb >> 24) & 0xff);
739 printf(" Invalid Instruction 6: 0x%02x\n",
740 (fcba->flpb >> 16) & 0xff);
741 printf(" Invalid Instruction 5: 0x%02x\n",
742 (fcba->flpb >> 8) & 0xff);
743 printf(" Invalid Instruction 4: 0x%02x\n",
744 fcba->flpb & 0xff);
745 } else {
746 printf("FLPB 0x%08x\n", fcba->flpb);
747 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
748 (fcba->flpb & 0xfff) << 12);
749 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700750}
751
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100752static void dump_fpsba(const struct fdbar *fdb, const struct fpsba *fpsba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700753{
Bill XIE4651d452017-09-12 11:54:48 +0800754 unsigned int i;
Arthur Heymans0424a2c2021-06-22 11:14:24 +0200755 /* SoC Straps, aka PSL, aka ISL */
756 unsigned int SS = (fdb->flmap1 >> 24) & 0xff;
Patrick Rudolph802cbee2020-05-25 12:18:11 +0200757
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700758 printf("Found PCH Strap Section\n");
Arthur Heymans0424a2c2021-06-22 11:14:24 +0200759 for (i = 0; i < SS; i++)
Patrick Rudolph802cbee2020-05-25 12:18:11 +0200760 printf("PCHSTRP%-3u: 0x%08x\n", i, fpsba->pchstrp[i]);
Bill XIEb3e15a22017-09-07 18:34:50 +0800761
762 if (ifd_version >= IFD_VERSION_2) {
763 printf("HAP bit is %sset\n",
764 fpsba->pchstrp[0] & (1 << 16) ? "" : "not ");
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100765 } else if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) {
Bill XIEb3e15a22017-09-07 18:34:50 +0800766 printf("ICH_MeDisable bit is %sset\n",
767 fpsba->pchstrp[0] & 1 ? "" : "not ");
768 } else {
769 printf("AltMeDisable bit is %sset\n",
770 fpsba->pchstrp[10] & (1 << 7) ? "" : "not ");
771 }
772
Bill XIE4651d452017-09-12 11:54:48 +0800773 printf("\n");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700774}
775
776static void decode_flmstr(uint32_t flmstr)
777{
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700778 int wr_shift, rd_shift;
779 if (ifd_version >= IFD_VERSION_2) {
780 wr_shift = FLMSTR_WR_SHIFT_V2;
781 rd_shift = FLMSTR_RD_SHIFT_V2;
782 } else {
783 wr_shift = FLMSTR_WR_SHIFT_V1;
784 rd_shift = FLMSTR_RD_SHIFT_V1;
785 }
786
787 /* EC region access only available on v2+ */
Jeff Dalyabd4b962022-01-06 00:52:30 -0500788 if (PLATFORM_HAS_EC_REGION)
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700789 printf(" EC Region Write Access: %s\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100790 (flmstr & (1 << (wr_shift + 8))) ?
791 "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700792 printf(" Platform Data Region Write Access: %s\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100793 (flmstr & (1 << (wr_shift + 4))) ? "enabled" : "disabled");
Jeff Dalyabd4b962022-01-06 00:52:30 -0500794 if (PLATFORM_HAS_GBE_REGION) {
795 printf(" GbE Region Write Access: %s\n",
796 (flmstr & (1 << (wr_shift + 3))) ? "enabled" : "disabled");
797 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700798 printf(" Intel ME Region Write Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700799 (flmstr & (1 << (wr_shift + 2))) ? "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700800 printf(" Host CPU/BIOS Region Write Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700801 (flmstr & (1 << (wr_shift + 1))) ? "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700802 printf(" Flash Descriptor Write Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700803 (flmstr & (1 << wr_shift)) ? "enabled" : "disabled");
Jeff Dalyabd4b962022-01-06 00:52:30 -0500804 if (PLATFORM_HAS_10GBE_0_REGION) {
805 printf(" 10GbE_0 Write Access: %s\n",
806 (flmstr & (1 << (wr_shift + 11))) ? "enabled" : "disabled");
807 }
808 if (PLATFORM_HAS_10GBE_1_REGION) {
809 printf(" 10GbE_1 Write Access: %s\n",
810 (flmstr & (1 << 4)) ? "enabled" : "disabled");
811 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700812
Jeff Dalyabd4b962022-01-06 00:52:30 -0500813 if (PLATFORM_HAS_EC_REGION)
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700814 printf(" EC Region Read Access: %s\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100815 (flmstr & (1 << (rd_shift + 8))) ?
816 "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700817 printf(" Platform Data Region Read Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700818 (flmstr & (1 << (rd_shift + 4))) ? "enabled" : "disabled");
Jeff Dalyabd4b962022-01-06 00:52:30 -0500819 if (PLATFORM_HAS_GBE_REGION) {
820 printf(" GbE Region Read Access: %s\n",
821 (flmstr & (1 << (rd_shift + 3))) ? "enabled" : "disabled");
822 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700823 printf(" Intel ME Region Read Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700824 (flmstr & (1 << (rd_shift + 2))) ? "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700825 printf(" Host CPU/BIOS Region Read Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700826 (flmstr & (1 << (rd_shift + 1))) ? "enabled" : "disabled");
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700827 printf(" Flash Descriptor Read Access: %s\n",
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700828 (flmstr & (1 << rd_shift)) ? "enabled" : "disabled");
Jeff Dalyabd4b962022-01-06 00:52:30 -0500829 if (PLATFORM_HAS_10GBE_0_REGION) {
830 printf(" 10GbE_0 Read Access: %s\n",
831 (flmstr & (1 << (rd_shift + 11))) ? "enabled" : "disabled");
832 }
833 if (PLATFORM_HAS_10GBE_1_REGION) {
834 printf(" 10GbE_1 Read Access: %s\n",
835 (flmstr & (1 << 0)) ? "enabled" : "disabled");
836 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700837
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -0700838 /* Requestor ID doesn't exist for ifd 2 */
839 if (ifd_version < IFD_VERSION_2)
840 printf(" Requester ID: 0x%04x\n\n",
841 flmstr & 0xffff);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700842}
843
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100844static void dump_fmba(const struct fmba *fmba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700845{
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700846 printf("Found Master Section\n");
847 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
848 decode_flmstr(fmba->flmstr1);
849 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
850 decode_flmstr(fmba->flmstr2);
Jeff Dalyabd4b962022-01-06 00:52:30 -0500851 if (PLATFORM_HAS_GBE_REGION) {
852 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
853 decode_flmstr(fmba->flmstr3);
854 if (ifd_version >= IFD_VERSION_2) {
855 printf("FLMSTR5: 0x%08x (EC)\n", fmba->flmstr5);
856 decode_flmstr(fmba->flmstr5);
857 }
858 } else {
859 printf("FLMSTR6: 0x%08x (IE)\n", fmba->flmstr6);
860 decode_flmstr(fmba->flmstr6);
Duncan Laurie1f7fd722015-06-22 11:14:48 -0700861 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700862}
863
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100864static void dump_fmsba(const struct fmsba *fmsba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700865{
Bill XIE612ec0e2017-08-30 16:10:27 +0800866 unsigned int i;
Stefan Reinauer3c53d332012-09-26 17:33:39 -0700867 printf("Found Processor Strap Section\n");
Bill XIE612ec0e2017-08-30 16:10:27 +0800868 for (i = 0; i < ARRAY_SIZE(fmsba->data); i++)
869 printf("????: 0x%08x\n", fmsba->data[i]);
Bill XIEb3e15a22017-09-07 18:34:50 +0800870
871 if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) {
872 printf("MCH_MeDisable bit is %sset\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100873 fmsba->data[0] & 1 ? "" : "not ");
Bill XIEb3e15a22017-09-07 18:34:50 +0800874 printf("MCH_AltMeDisable bit is %sset\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100875 fmsba->data[0] & (1 << 7) ? "" : "not ");
Bill XIEb3e15a22017-09-07 18:34:50 +0800876 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700877}
878
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700879static void dump_jid(uint32_t jid)
880{
Elyes HAOUAS99e54fe2020-01-30 15:08:03 +0100881 printf(" SPI Component Vendor ID: 0x%02x\n",
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700882 jid & 0xff);
Elyes HAOUAS99e54fe2020-01-30 15:08:03 +0100883 printf(" SPI Component Device ID 0: 0x%02x\n",
Stefan Tauner27bb0662018-08-06 00:40:15 +0200884 (jid >> 8) & 0xff);
Elyes HAOUAS99e54fe2020-01-30 15:08:03 +0100885 printf(" SPI Component Device ID 1: 0x%02x\n",
Stefan Tauner27bb0662018-08-06 00:40:15 +0200886 (jid >> 16) & 0xff);
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700887}
888
889static void dump_vscc(uint32_t vscc)
890{
891 printf(" Lower Erase Opcode: 0x%02x\n",
892 vscc >> 24);
893 printf(" Lower Write Enable on Write Status: 0x%02x\n",
894 vscc & (1 << 20) ? 0x06 : 0x50);
895 printf(" Lower Write Status Required: %s\n",
896 vscc & (1 << 19) ? "Yes" : "No");
897 printf(" Lower Write Granularity: %d bytes\n",
898 vscc & (1 << 18) ? 64 : 1);
899 printf(" Lower Block / Sector Erase Size: ");
900 switch ((vscc >> 16) & 0x3) {
901 case 0:
902 printf("256 Byte\n");
903 break;
904 case 1:
905 printf("4KB\n");
906 break;
907 case 2:
908 printf("8KB\n");
909 break;
910 case 3:
911 printf("64KB\n");
912 break;
913 }
914
915 printf(" Upper Erase Opcode: 0x%02x\n",
916 (vscc >> 8) & 0xff);
917 printf(" Upper Write Enable on Write Status: 0x%02x\n",
918 vscc & (1 << 4) ? 0x06 : 0x50);
919 printf(" Upper Write Status Required: %s\n",
920 vscc & (1 << 3) ? "Yes" : "No");
921 printf(" Upper Write Granularity: %d bytes\n",
922 vscc & (1 << 2) ? 64 : 1);
923 printf(" Upper Block / Sector Erase Size: ");
924 switch (vscc & 0x3) {
925 case 0:
926 printf("256 Byte\n");
927 break;
928 case 1:
929 printf("4KB\n");
930 break;
931 case 2:
932 printf("8KB\n");
933 break;
934 case 3:
935 printf("64KB\n");
936 break;
937 }
938}
939
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100940static void dump_vtba(const struct vtba *vtba, int vtl)
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700941{
942 int i;
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100943 int max_len = sizeof(struct vtba)/sizeof(struct vscc);
Stefan Tauner0d226142018-08-05 18:56:53 +0200944 int num = (vtl >> 1) < max_len ? (vtl >> 1) : max_len;
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700945
946 printf("ME VSCC table:\n");
947 for (i = 0; i < num; i++) {
948 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
949 dump_jid(vtba->entry[i].jid);
950 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
951 dump_vscc(vtba->entry[i].vscc);
952 }
953 printf("\n");
954}
955
Bill XIEfa5f9942017-09-12 11:22:29 +0800956static void dump_oem(const uint8_t *oem)
Stefan Reinauer4a17d292012-09-27 12:42:15 -0700957{
958 int i, j;
959 printf("OEM Section:\n");
960 for (i = 0; i < 4; i++) {
961 printf("%02x:", i << 4);
962 for (j = 0; j < 16; j++)
963 printf(" %02x", oem[(i<<4)+j]);
964 printf ("\n");
965 }
966 printf ("\n");
967}
968
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700969static void dump_fd(char *image, int size)
970{
Maximilian Bruneab0e6802023-03-05 04:34:40 +0100971 const struct fdbar *fdb = find_fd(image, size);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700972 if (!fdb)
973 exit(EXIT_FAILURE);
974
Subrata Banik26058dc2020-08-26 15:12:16 +0530975 printf("%s", is_platform_with_pch() ? "PCH" : "ICH");
976 printf(" Revision: %s\n", ich_chipset_names[chipset]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700977 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530978 if (!is_platform_with_100x_series_pch())
979 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700980 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
981 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
982 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
983
984 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530985 printf(" %s: ", is_platform_with_100x_series_pch() ? "PSL" : "ISL");
986 printf("0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700987 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
988 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
989 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
990
Subrata Banikac1b1dd2020-08-26 15:29:58 +0530991 if (!is_platform_with_100x_series_pch()) {
992 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
993 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
994 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
995 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -0700996
Subrata Banik9cd85d02023-05-24 23:18:49 +0530997 if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT ||
998 chipset == CHIPSET_800_SERIES_METEOR_LAKE) {
Subrata Banikbd2da5a2020-08-26 15:43:51 +0530999 printf("FLMAP3: 0x%08x\n", fdb->flmap3);
1000 printf(" Minor Revision ID: 0x%04x\n", (fdb->flmap3 >> 14) & 0x7f);
1001 printf(" Major Revision ID: 0x%04x\n", (fdb->flmap3 >> 21) & 0x7ff);
1002 }
1003
Stefan Tauner0d226142018-08-05 18:56:53 +02001004 char *flumap = find_flumap(image, size);
1005 uint32_t flumap1 = *(uint32_t *)flumap;
1006 printf("FLUMAP1: 0x%08x\n", flumap1);
Stefan Reinauer4a17d292012-09-27 12:42:15 -07001007 printf(" Intel ME VSCC Table Length (VTL): %d\n",
Stefan Tauner0d226142018-08-05 18:56:53 +02001008 (flumap1 >> 8) & 0xff);
Stefan Reinauer4a17d292012-09-27 12:42:15 -07001009 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
Stefan Tauner0d226142018-08-05 18:56:53 +02001010 (flumap1 & 0xff) << 4);
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001011 dump_vtba((struct vtba *)
Stefan Tauner0d226142018-08-05 18:56:53 +02001012 (image + ((flumap1 & 0xff) << 4)),
1013 (flumap1 >> 8) & 0xff);
Bill XIE612ec0e2017-08-30 16:10:27 +08001014 dump_oem((const uint8_t *)image + 0xf00);
1015
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001016 const struct frba *frba = find_frba(image, size);
1017 const struct fcba *fcba = find_fcba(image, size);
1018 const struct fpsba *fpsba = find_fpsba(image, size);
1019 const struct fmba *fmba = find_fmba(image, size);
1020 const struct fmsba *fmsba = find_fmsba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001021
1022 if (frba && fcba && fpsba && fmba && fmsba) {
1023 dump_frba(frba);
Subrata Banike5d39922020-08-26 16:01:42 +05301024 dump_fcba(fcba, fpsba);
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001025 dump_fpsba(fdb, fpsba);
Bill XIE612ec0e2017-08-30 16:10:27 +08001026 dump_fmba(fmba);
1027 dump_fmsba(fmsba);
1028 } else {
1029 printf("FD is corrupted!\n");
1030 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001031}
1032
Maximilian Brune347596a2023-03-05 20:55:32 +01001033/* Takes an image containing an IFD and creates a Flashmap .fmd file template.
1034 * This flashmap will contain all IFD regions except the BIOS region.
1035 * The BIOS region is created by coreboot itself and 'should' match the IFD region
1036 * anyway (CONFIG_VALIDATE_INTEL_DESCRIPTOR should make sure). coreboot built system will use
1037 * this template to generate the final Flashmap file.
1038 */
1039static void create_fmap_template(char *image, int size, const char *layout_fname)
1040{
1041 const struct frba *frba = find_frba(image, size);
1042 if (!frba)
1043 exit(EXIT_FAILURE);
1044
1045 int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1046 if (layout_fd == -1) {
1047 perror("Could not open file");
1048 exit(EXIT_FAILURE);
1049 }
1050
1051 char *bbuf = "FLASH@##ROM_BASE## ##ROM_SIZE## {\n";
1052 if (write(layout_fd, bbuf, strlen(bbuf)) < 0) {
1053 perror("Could not write to file");
1054 exit(EXIT_FAILURE);
1055 }
1056
1057 /* fmaptool requires regions in .fmd to be sorted.
1058 * => We need to sort the regions by base address before writing them in .fmd File
1059 */
1060 int count_regions = 0;
1061 struct region sorted_regions[MAX_REGIONS] = { 0 };
1062 for (unsigned int i = 0; i < max_regions; i++) {
1063 struct region region = get_region(frba, i);
Maximilian Brune2c895aa2023-04-24 19:28:21 +02001064
1065 /* A region limit of 0 is an indicator of an unused region
1066 * A region base of 7FFFh is an indicator of a reserved region
1067 */
1068 if (region.limit == 0 || region.base == 0x07FFF000)
Maximilian Brune347596a2023-03-05 20:55:32 +01001069 continue;
1070
Maximilian Brune794d1d72023-11-13 02:07:01 +01001071 /* Is there an FMAP equivalent? IFD reserved regions are usually thrown out
1072 * of the FMAP here
1073 */
1074 if (!region_names[region.type].fmapname) {
1075 printf("Skip IFD region: %s\n", region_names[region.type].pretty);
1076 continue;
1077 }
1078
Maximilian Brune347596a2023-03-05 20:55:32 +01001079 /* Here we decide to use the coreboot generated FMAP BIOS region, instead of
1080 * the one specified in the IFD. The case when IFD and FMAP BIOS region do not
1081 * match cannot be caught here, therefore one should still validate IFD and
1082 * FMAP via CONFIG_VALIDATE_INTEL_DESCRIPTOR
1083 */
1084 if (i == REGION_BIOS)
1085 continue;
1086
1087 sorted_regions[count_regions] = region;
1088 // basically insertion sort
1089 for (int i = count_regions-1; i >= 0 ; i--) {
1090 if (sorted_regions[i].base > sorted_regions[i+1].base) {
1091 struct region tmp = sorted_regions[i];
1092 sorted_regions[i] = sorted_regions[i+1];
1093 sorted_regions[i+1] = tmp;
1094 }
1095 }
1096 count_regions++;
1097 }
1098
1099 // Now write regions sorted by base address in the fmap file
1100 for (int i = 0; i < count_regions; i++) {
1101 struct region region = sorted_regions[i];
1102 char buf[LAYOUT_LINELEN];
1103 snprintf(buf, LAYOUT_LINELEN, "\t%s@0x%X 0x%X\n", region_names[region.type].fmapname, region.base, region.size);
1104 if (write(layout_fd, buf, strlen(buf)) < 0) {
1105 perror("Could not write to file");
1106 exit(EXIT_FAILURE);
1107 }
1108 }
1109
1110 char *ebuf = "\tSI_BIOS@##BIOS_BASE## ##BIOS_SIZE## {\n"
1111 "\t\t##CONSOLE_ENTRY##\n"
1112 "\t\t##MRC_CACHE_ENTRY##\n"
1113 "\t\t##SMMSTORE_ENTRY##\n"
1114 "\t\t##SPD_CACHE_ENTRY##\n"
1115 "\t\t##VPD_ENTRY##\n"
1116 "\t\tFMAP@##FMAP_BASE## ##FMAP_SIZE##\n"
1117 "\t\tCOREBOOT(CBFS)@##CBFS_BASE## ##CBFS_SIZE##\n"
1118 "\t}\n"
1119 "}\n";
1120 if (write(layout_fd, ebuf, strlen(ebuf)) < 0) {
1121 perror("Could not write to file");
1122 exit(EXIT_FAILURE);
1123 }
1124
1125 close(layout_fd);
1126 printf("Wrote layout to %s\n", layout_fname);
1127}
1128
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001129static void write_regions(char *image, int size)
1130{
Bill XIEfa5f9942017-09-12 11:22:29 +08001131 unsigned int i;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001132 const struct frba *frba = find_frba(image, size);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001133
Bill XIE612ec0e2017-08-30 16:10:27 +08001134 if (!frba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001135 exit(EXIT_FAILURE);
1136
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001137 for (i = 0; i < max_regions; i++) {
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001138 struct region region = get_region(frba, i);
Stefan Reinauer4a17d292012-09-27 12:42:15 -07001139 dump_region(i, frba);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001140 if (region.size > 0) {
1141 int region_fd;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001142 region_fd = open(region_names[i].filename,
Scott Duplichanf2c98372014-12-12 21:03:06 -06001143 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001144 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Patrick Georgi38fa6ed2014-08-03 12:18:45 +02001145 if (region_fd < 0) {
1146 perror("Error while trying to open file");
1147 exit(EXIT_FAILURE);
1148 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001149 if (write(region_fd, image + region.base, region.size) != region.size)
Stefan Reinauera9f670a2012-06-18 16:02:20 -07001150 perror("Error while writing");
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001151 close(region_fd);
1152 }
1153 }
1154}
1155
Mathew Kingc7ddc992019-08-08 14:59:25 -06001156static void validate_layout(char *image, int size)
1157{
1158 uint i, errors = 0;
1159 struct fmap *fmap;
1160 long int fmap_loc = fmap_find((uint8_t *)image, size);
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001161 const struct frba *frba = find_frba(image, size);
Mathew Kingc7ddc992019-08-08 14:59:25 -06001162
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001163 if (fmap_loc < 0 || !frba) {
1164 printf("Could not find FMAP (%p) or Intel Flash Descriptor (%p)\n",
1165 (void *)fmap_loc, frba);
Mathew Kingc7ddc992019-08-08 14:59:25 -06001166 exit(EXIT_FAILURE);
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001167 }
Mathew Kingc7ddc992019-08-08 14:59:25 -06001168
1169 fmap = (struct fmap *)(image + fmap_loc);
1170
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001171 int matches = 0;
Mathew Kingc7ddc992019-08-08 14:59:25 -06001172 for (i = 0; i < max_regions; i++) {
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001173 struct region region = get_region(frba, i);
Mathew Kingc7ddc992019-08-08 14:59:25 -06001174 if (region.size == 0)
1175 continue;
1176
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001177 const struct fmap_area *area = fmap_find_area(fmap, region_names[i].fmapname);
Mathew Kingc7ddc992019-08-08 14:59:25 -06001178 if (!area)
1179 continue;
1180
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001181 matches++; // found a match between FMAP and IFD region
1182
1183 if ((uint)region.base != area->offset || (uint)region.size != area->size) {
1184 printf("Region mismatch between %s and %s\n", region_names[i].terse, area->name);
Mathew Kingc7ddc992019-08-08 14:59:25 -06001185 printf(" Descriptor region %s:\n", region_names[i].terse);
1186 printf(" offset: 0x%08x\n", region.base);
1187 printf(" length: 0x%08x\n", region.size);
1188 printf(" FMAP area %s:\n", area->name);
1189 printf(" offset: 0x%08x\n", area->offset);
1190 printf(" length: 0x%08x\n", area->size);
1191 errors++;
1192 }
1193 }
1194
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001195 if (!matches) {
1196 // At least a BIOS region should be present in both IFD and FMAP
1197 fprintf(stderr, "Warning: Not a single IFD region found in FMAP\n");
1198 }
1199
Mathew Kingc7ddc992019-08-08 14:59:25 -06001200 if (errors > 0)
1201 exit(EXIT_FAILURE);
1202}
1203
Bill XIEfa5f9942017-09-12 11:22:29 +08001204static void write_image(const char *filename, char *image, int size)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001205{
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001206 int new_fd;
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01001207 printf("Writing new image to %s\n", filename);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001208
1209 // Now write out new image
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001210 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
Patrick Georgi38fa6ed2014-08-03 12:18:45 +02001211 if (new_fd < 0) {
1212 perror("Error while trying to open file");
1213 exit(EXIT_FAILURE);
1214 }
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001215 if (write(new_fd, image, size) != size)
Stefan Reinauera9f670a2012-06-18 16:02:20 -07001216 perror("Error while writing");
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001217 close(new_fd);
1218}
1219
Bill XIEfa5f9942017-09-12 11:22:29 +08001220static void set_spi_frequency(const char *filename, char *image, int size,
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001221 enum spi_frequency freq)
1222{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001223 struct fcba *fcba = find_fcba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001224 if (!fcba)
1225 exit(EXIT_FAILURE);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001226
1227 /* clear bits 21-29 */
1228 fcba->flcomp &= ~0x3fe00000;
1229 /* Read ID and Read Status Clock Frequency */
1230 fcba->flcomp |= freq << 27;
1231 /* Write and Erase Clock Frequency */
1232 fcba->flcomp |= freq << 24;
1233 /* Fast Read Clock Frequency */
1234 fcba->flcomp |= freq << 21;
1235
1236 write_image(filename, image, size);
1237}
1238
Bill XIEfa5f9942017-09-12 11:22:29 +08001239static void set_em100_mode(const char *filename, char *image, int size)
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07001240{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001241 struct fcba *fcba = find_fcba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001242 if (!fcba)
1243 exit(EXIT_FAILURE);
1244
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001245 int freq;
1246
1247 switch (ifd_version) {
1248 case IFD_VERSION_1:
Patrick Rudolph16598742022-10-21 15:13:43 +02001249 case IFD_VERSION_1_5:
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001250 freq = SPI_FREQUENCY_20MHZ;
1251 break;
1252 case IFD_VERSION_2:
1253 freq = SPI_FREQUENCY_17MHZ;
1254 break;
Alexandru Gagniuc71a7ba22015-09-10 08:37:42 -07001255 default:
1256 freq = SPI_FREQUENCY_17MHZ;
1257 break;
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001258 }
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07001259
1260 fcba->flcomp &= ~(1 << 30);
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001261 set_spi_frequency(filename, image, size, freq);
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07001262}
1263
Bill XIEfa5f9942017-09-12 11:22:29 +08001264static void set_chipdensity(const char *filename, char *image, int size,
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001265 unsigned int density)
Jan Tatjefa317512016-03-11 00:52:07 +01001266{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001267 struct fcba *fcba = find_fcba(image, size);
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001268 uint8_t mask, chip2_offset;
Bill XIE612ec0e2017-08-30 16:10:27 +08001269 if (!fcba)
1270 exit(EXIT_FAILURE);
Jan Tatjefa317512016-03-11 00:52:07 +01001271
1272 printf("Setting chip density to ");
1273 decode_component_density(density);
1274 printf("\n");
1275
1276 switch (ifd_version) {
1277 case IFD_VERSION_1:
1278 /* fail if selected density is not supported by this version */
1279 if ( (density == COMPONENT_DENSITY_32MB) ||
1280 (density == COMPONENT_DENSITY_64MB) ||
1281 (density == COMPONENT_DENSITY_UNUSED) ) {
1282 printf("error: Selected density not supported in IFD version 1.\n");
1283 exit(EXIT_FAILURE);
1284 }
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001285 mask = 0x7;
1286 chip2_offset = 3;
Jan Tatjefa317512016-03-11 00:52:07 +01001287 break;
Patrick Rudolph16598742022-10-21 15:13:43 +02001288 case IFD_VERSION_1_5:
Jan Tatjefa317512016-03-11 00:52:07 +01001289 case IFD_VERSION_2:
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001290 mask = 0xf;
1291 chip2_offset = 4;
1292 break;
Jan Tatjefa317512016-03-11 00:52:07 +01001293 default:
1294 printf("error: Unknown IFD version\n");
1295 exit(EXIT_FAILURE);
1296 break;
1297 }
1298
1299 /* clear chip density for corresponding chip */
1300 switch (selected_chip) {
1301 case 1:
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001302 fcba->flcomp &= ~mask;
Jan Tatjefa317512016-03-11 00:52:07 +01001303 break;
1304 case 2:
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001305 fcba->flcomp &= ~(mask << chip2_offset);
Jan Tatjefa317512016-03-11 00:52:07 +01001306 break;
1307 default: /*both chips*/
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001308 fcba->flcomp &= ~(mask | (mask << chip2_offset));
Jan Tatjefa317512016-03-11 00:52:07 +01001309 break;
1310 }
1311
1312 /* set the new density */
1313 if (selected_chip == 1 || selected_chip == 0)
1314 fcba->flcomp |= (density); /* first chip */
1315 if (selected_chip == 2 || selected_chip == 0)
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001316 fcba->flcomp |= (density << chip2_offset); /* second chip */
Jan Tatjefa317512016-03-11 00:52:07 +01001317
1318 write_image(filename, image, size);
1319}
1320
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001321static int check_region(const struct frba *frba, unsigned int region_type)
Duncan Laurie7775d672019-06-06 13:39:26 -07001322{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001323 struct region region;
Duncan Laurie7775d672019-06-06 13:39:26 -07001324
1325 if (!frba)
1326 return 0;
1327
1328 region = get_region(frba, region_type);
1329 return !!((region.base < region.limit) && (region.size > 0));
1330}
1331
Bill XIEfa5f9942017-09-12 11:22:29 +08001332static void lock_descriptor(const char *filename, char *image, int size)
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001333{
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001334 int wr_shift, rd_shift;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001335 struct fmba *fmba = find_fmba(image, size);
1336 const struct frba *frba = find_frba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001337 if (!fmba)
1338 exit(EXIT_FAILURE);
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001339
1340 if (ifd_version >= IFD_VERSION_2) {
1341 wr_shift = FLMSTR_WR_SHIFT_V2;
1342 rd_shift = FLMSTR_RD_SHIFT_V2;
1343
1344 /* Clear non-reserved bits */
1345 fmba->flmstr1 &= 0xff;
1346 fmba->flmstr2 &= 0xff;
1347 fmba->flmstr3 &= 0xff;
Lijian Zhao9871b042019-01-26 16:48:38 -08001348 fmba->flmstr5 &= 0xff;
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001349 } else {
1350 wr_shift = FLMSTR_WR_SHIFT_V1;
1351 rd_shift = FLMSTR_RD_SHIFT_V1;
1352
1353 fmba->flmstr1 = 0;
1354 fmba->flmstr2 = 0;
1355 /* Requestor ID */
1356 fmba->flmstr3 = 0x118;
1357 }
1358
Andrey Petrov96ecb772016-10-31 19:31:54 -07001359 switch (platform) {
Furquan Shaikhc0257dd2018-05-02 23:29:04 -07001360 case PLATFORM_APL:
1361 case PLATFORM_GLK:
Andrey Petrov96ecb772016-10-31 19:31:54 -07001362 /* CPU/BIOS can read descriptor and BIOS */
1363 fmba->flmstr1 |= 0x3 << rd_shift;
1364 /* CPU/BIOS can write BIOS */
1365 fmba->flmstr1 |= 0x2 << wr_shift;
1366 /* TXE can read descriptor, BIOS and Device Expansion */
1367 fmba->flmstr2 |= 0x23 << rd_shift;
1368 /* TXE can only write Device Expansion */
1369 fmba->flmstr2 |= 0x20 << wr_shift;
1370 break;
Lijian Zhao3e88b182019-01-25 23:20:16 -08001371 case PLATFORM_CNL:
1372 case PLATFORM_ICL:
Furquan Shaikh088b6e82018-03-21 10:42:37 -07001373 case PLATFORM_SKLKBL:
Ravi Sarawadi7d9d63b2019-10-22 13:45:36 -07001374 case PLATFORM_TGL:
Subrata Banika717e2f2020-07-30 11:22:53 +05301375 case PLATFORM_JSL:
Lean Sheng Tan0faba3c2021-06-09 07:52:24 -07001376 case PLATFORM_EHL:
Subrata Banik46f80732020-03-14 15:01:42 +05301377 case PLATFORM_ADL:
Wonkyu Kim3922aa52022-02-02 15:19:05 -08001378 case PLATFORM_IFD2:
Subrata Banikca82e612022-01-20 18:51:21 +05301379 case PLATFORM_MTL:
Duncan Laurie7775d672019-06-06 13:39:26 -07001380 /* CPU/BIOS can read descriptor and BIOS. */
1381 fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift;
1382 fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift;
1383 /* CPU/BIOS can write BIOS. */
1384 fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift;
1385 /* ME can read descriptor and ME. */
1386 fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift;
1387 fmba->flmstr2 |= (1 << REGION_ME) << rd_shift;
Furquan Shaikh088b6e82018-03-21 10:42:37 -07001388 /* ME can write ME. */
Duncan Laurie7775d672019-06-06 13:39:26 -07001389 fmba->flmstr2 |= (1 << REGION_ME) << wr_shift;
1390 if (check_region(frba, REGION_GBE)) {
1391 /* BIOS can read/write GbE. */
1392 fmba->flmstr1 |= (1 << REGION_GBE) << rd_shift;
1393 fmba->flmstr1 |= (1 << REGION_GBE) << wr_shift;
1394 /* ME can read GbE. */
1395 fmba->flmstr2 |= (1 << REGION_GBE) << rd_shift;
1396 /* GbE can read descriptor and read/write GbE.. */
1397 fmba->flmstr3 |= (1 << REGION_DESC) << rd_shift;
1398 fmba->flmstr3 |= (1 << REGION_GBE) << rd_shift;
1399 fmba->flmstr3 |= (1 << REGION_GBE) << wr_shift;
1400 }
1401 if (check_region(frba, REGION_PDR)) {
1402 /* BIOS can read/write PDR. */
1403 fmba->flmstr1 |= (1 << REGION_PDR) << rd_shift;
1404 fmba->flmstr1 |= (1 << REGION_PDR) << wr_shift;
1405 }
1406 if (check_region(frba, REGION_EC)) {
1407 /* BIOS can read EC. */
1408 fmba->flmstr1 |= (1 << REGION_EC) << rd_shift;
1409 /* EC can read descriptor and read/write EC. */
1410 fmba->flmstr5 |= (1 << REGION_DESC) << rd_shift;
1411 fmba->flmstr5 |= (1 << REGION_EC) << rd_shift;
1412 fmba->flmstr5 |= (1 << REGION_EC) << wr_shift;
1413 }
Furquan Shaikh088b6e82018-03-21 10:42:37 -07001414 break;
Jeff Dalyabd4b962022-01-06 00:52:30 -05001415 case PLATFORM_DNV:
Patrick Rudolph16598742022-10-21 15:13:43 +02001416 case PLATFORM_WBG:
Jeff Dalyabd4b962022-01-06 00:52:30 -05001417 /* CPU/BIOS can read descriptor and BIOS. */
1418 fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift;
1419 fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift;
1420 /* CPU/BIOS can write BIOS. */
1421 fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift;
1422 /* ME can read descriptor and ME. */
1423 fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift;
1424 fmba->flmstr2 |= (1 << REGION_ME) << rd_shift;
1425 /* ME can write ME. */
1426 fmba->flmstr2 |= (1 << REGION_ME) << wr_shift;
1427 break;
Andrey Petrov96ecb772016-10-31 19:31:54 -07001428 default:
Duncan Laurie7775d672019-06-06 13:39:26 -07001429 /* CPU/BIOS can read descriptor and BIOS. */
1430 fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift;
1431 fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift;
1432 /* CPU/BIOS can write BIOS. */
1433 fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift;
1434 /* ME can read descriptor and ME. */
1435 fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift;
1436 fmba->flmstr2 |= (1 << REGION_ME) << rd_shift;
1437 /* ME can write ME. */
1438 fmba->flmstr2 |= (1 << REGION_ME) << wr_shift;
1439 if (check_region(frba, REGION_GBE)) {
1440 /* BIOS can read GbE. */
1441 fmba->flmstr1 |= (1 << REGION_GBE) << rd_shift;
1442 /* BIOS can write GbE. */
1443 fmba->flmstr1 |= (1 << REGION_GBE) << wr_shift;
1444 /* ME can read GbE. */
1445 fmba->flmstr2 |= (1 << REGION_GBE) << rd_shift;
1446 /* ME can write GbE. */
1447 fmba->flmstr2 |= (1 << REGION_GBE) << wr_shift;
1448 /* GbE can write GbE. */
1449 fmba->flmstr3 |= (1 << REGION_GBE) << rd_shift;
1450 /* GbE can read GbE. */
1451 fmba->flmstr3 |= (1 << REGION_GBE) << wr_shift;
1452 }
Andrey Petrov96ecb772016-10-31 19:31:54 -07001453 break;
1454 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001455
1456 write_image(filename, image, size);
1457}
1458
Usha P412679d2020-10-15 11:25:08 +05301459static void enable_cpu_read_me(const char *filename, char *image, int size)
1460{
1461 int rd_shift;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001462 struct fmba *fmba = find_fmba(image, size);
Usha P412679d2020-10-15 11:25:08 +05301463
1464 if (!fmba)
1465 exit(EXIT_FAILURE);
1466
1467 if (ifd_version >= IFD_VERSION_2)
1468 rd_shift = FLMSTR_RD_SHIFT_V2;
1469 else
1470 rd_shift = FLMSTR_RD_SHIFT_V1;
1471
1472 /* CPU/BIOS can read ME. */
1473 fmba->flmstr1 |= (1 << REGION_ME) << rd_shift;
1474
1475 write_image(filename, image, size);
1476}
1477
Bill XIEfa5f9942017-09-12 11:22:29 +08001478static void unlock_descriptor(const char *filename, char *image, int size)
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001479{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001480 struct fmba *fmba = find_fmba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001481 if (!fmba)
1482 exit(EXIT_FAILURE);
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001483
1484 if (ifd_version >= IFD_VERSION_2) {
1485 /* Access bits for each region are read: 19:8 write: 31:20 */
1486 fmba->flmstr1 = 0xffffff00 | (fmba->flmstr1 & 0xff);
1487 fmba->flmstr2 = 0xffffff00 | (fmba->flmstr2 & 0xff);
1488 fmba->flmstr3 = 0xffffff00 | (fmba->flmstr3 & 0xff);
Lijian Zhao9871b042019-01-26 16:48:38 -08001489 fmba->flmstr5 = 0xffffff00 | (fmba->flmstr5 & 0xff);
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001490 } else {
1491 fmba->flmstr1 = 0xffff0000;
1492 fmba->flmstr2 = 0xffff0000;
Patrick Rudolph8a06cc72017-01-08 09:57:47 +01001493 /* Keep chipset specific Requester ID */
1494 fmba->flmstr3 = 0x08080000 | (fmba->flmstr3 & 0xffff);
Shawn Nematbakhshd2cb1182015-09-10 19:07:13 -07001495 }
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001496
1497 write_image(filename, image, size);
1498}
1499
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001500static void set_pchstrap(struct fpsba *fpsba, const struct fdbar *fdb, const int strap,
1501 const unsigned int value)
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001502{
1503 if (!fpsba || !fdb) {
1504 fprintf(stderr, "Internal error\n");
1505 exit(EXIT_FAILURE);
1506 }
1507
Arthur Heymans0424a2c2021-06-22 11:14:24 +02001508 /* SoC Strap, aka PSL, aka ISL */
1509 int SS = (fdb->flmap1 >> 24) & 0xff;
1510 if (strap >= SS) {
1511 fprintf(stderr, "Strap index %d out of range (max: %d)\n", strap, SS);
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001512 exit(EXIT_FAILURE);
1513 }
1514 fpsba->pchstrp[strap] = value;
1515}
1516
Bill XIEb3e15a22017-09-07 18:34:50 +08001517/* Set the AltMeDisable (or HAP for >= IFD_VERSION_2) */
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001518static void fpsba_set_altmedisable(struct fpsba *fpsba, struct fmsba *fmsba, bool altmedisable)
Bill XIEb3e15a22017-09-07 18:34:50 +08001519{
1520 if (ifd_version >= IFD_VERSION_2) {
1521 printf("%sting the HAP bit to %s Intel ME...\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001522 altmedisable?"Set":"Unset",
1523 altmedisable?"disable":"enable");
Bill XIEb3e15a22017-09-07 18:34:50 +08001524 if (altmedisable)
1525 fpsba->pchstrp[0] |= (1 << 16);
1526 else
1527 fpsba->pchstrp[0] &= ~(1 << 16);
1528 } else {
1529 if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) {
1530 printf("%sting the ICH_MeDisable, MCH_MeDisable, "
1531 "and MCH_AltMeDisable to %s Intel ME...\n",
1532 altmedisable?"Set":"Unset",
1533 altmedisable?"disable":"enable");
1534 if (altmedisable) {
1535 /* MCH_MeDisable */
1536 fmsba->data[0] |= 1;
1537 /* MCH_AltMeDisable */
1538 fmsba->data[0] |= (1 << 7);
1539 /* ICH_MeDisable */
1540 fpsba->pchstrp[0] |= 1;
1541 } else {
1542 fmsba->data[0] &= ~1;
1543 fmsba->data[0] &= ~(1 << 7);
1544 fpsba->pchstrp[0] &= ~1;
1545 }
1546 } else {
1547 printf("%sting the AltMeDisable to %s Intel ME...\n",
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001548 altmedisable?"Set":"Unset",
1549 altmedisable?"disable":"enable");
Bill XIEb3e15a22017-09-07 18:34:50 +08001550 if (altmedisable)
1551 fpsba->pchstrp[10] |= (1 << 7);
1552 else
1553 fpsba->pchstrp[10] &= ~(1 << 7);
1554 }
1555 }
1556}
1557
Jacob Garber595d9262019-06-27 17:33:10 -06001558static void inject_region(const char *filename, char *image, int size,
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001559 unsigned int region_type, const char *region_fname)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001560{
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001561 struct frba *frba = find_frba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001562 if (!frba)
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001563 exit(EXIT_FAILURE);
Stefan Reinauer5ff7c132011-10-31 12:56:45 -07001564
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001565 struct region region = get_region(frba, region_type);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001566 if (region.size <= 0xfff) {
1567 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
1568 region_name(region_type));
1569 exit(EXIT_FAILURE);
1570 }
1571
Scott Duplichanf2c98372014-12-12 21:03:06 -06001572 int region_fd = open(region_fname, O_RDONLY | O_BINARY);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001573 if (region_fd == -1) {
1574 perror("Could not open file");
1575 exit(EXIT_FAILURE);
1576 }
1577 struct stat buf;
1578 if (fstat(region_fd, &buf) == -1) {
1579 perror("Could not stat file");
1580 exit(EXIT_FAILURE);
1581 }
1582 int region_size = buf.st_size;
1583
1584 printf("File %s is %d bytes\n", region_fname, region_size);
1585
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001586 if (region_size > region.size) {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001587 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
1588 " bytes. Not injecting.\n",
1589 region_name(region_type), region.size,
1590 region.size, region_size, region_size);
1591 exit(EXIT_FAILURE);
1592 }
1593
1594 int offset = 0;
1595 if ((region_type == 1) && (region_size < region.size)) {
1596 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)"
1597 " bytes. Padding before injecting.\n",
1598 region_name(region_type), region.size,
1599 region.size, region_size, region_size);
1600 offset = region.size - region_size;
1601 memset(image + region.base, 0xff, offset);
1602 }
1603
Stefan Reinauer5e93b372012-09-25 13:30:48 -07001604 if (size < region.base + offset + region_size) {
1605 fprintf(stderr, "Output file is too small. (%d < %d)\n",
1606 size, region.base + offset + region_size);
1607 exit(EXIT_FAILURE);
1608 }
1609
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001610 if (read(region_fd, image + region.base + offset, region_size) != region_size) {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001611 perror("Could not read file");
1612 exit(EXIT_FAILURE);
1613 }
1614
1615 close(region_fd);
1616
1617 printf("Adding %s as the %s section of %s\n",
1618 region_fname, region_name(region_type), filename);
1619 write_image(filename, image, size);
1620}
1621
Jacob Garber595d9262019-06-27 17:33:10 -06001622static unsigned int next_pow2(unsigned int x)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001623{
1624 unsigned int y = 1;
1625 if (x == 0)
1626 return 0;
1627 while (y <= x)
1628 y = y << 1;
1629
1630 return y;
1631}
1632
1633/**
1634 * Determine if two memory regions overlap.
1635 *
1636 * @param r1, r2 Memory regions to compare.
Elyes HAOUASa1ccaed2018-08-24 07:58:00 +02001637 * @return 0 if the two regions are separate
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001638 * @return 1 if the two regions overlap
1639 */
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001640static int regions_collide(const struct region *r1, const struct region *r2)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001641{
Bill XIEfa5f9942017-09-12 11:22:29 +08001642 if ((r1->size == 0) || (r2->size == 0))
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001643 return 0;
1644
Nico Huber844eda02019-01-05 00:06:19 +01001645 /* r1 should be either completely below or completely above r2 */
1646 return !(r1->limit < r2->base || r1->base > r2->limit);
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001647}
1648
Jacob Garber595d9262019-06-27 17:33:10 -06001649static void new_layout(const char *filename, char *image, int size,
Bill XIEfa5f9942017-09-12 11:22:29 +08001650 const char *layout_fname)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001651{
1652 FILE *romlayout;
1653 char tempstr[256];
1654 char layout_region_name[256];
Bill XIEfa5f9942017-09-12 11:22:29 +08001655 unsigned int i, j;
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001656 int region_number;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001657 struct region current_regions[MAX_REGIONS];
1658 struct region new_regions[MAX_REGIONS];
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001659 int new_extent = 0;
1660 char *new_image;
1661
1662 /* load current descriptor map and regions */
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001663 struct frba *frba = find_frba(image, size);
Bill XIE612ec0e2017-08-30 16:10:27 +08001664 if (!frba)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001665 exit(EXIT_FAILURE);
1666
Alexander Couzensfd5c6582016-10-08 00:37:24 +02001667 for (i = 0; i < max_regions; i++) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001668 current_regions[i] = get_region(frba, i);
1669 new_regions[i] = get_region(frba, i);
1670 }
1671
1672 /* read new layout */
1673 romlayout = fopen(layout_fname, "r");
1674
1675 if (!romlayout) {
1676 perror("Could not read layout file.\n");
1677 exit(EXIT_FAILURE);
1678 }
1679
1680 while (!feof(romlayout)) {
1681 char *tstr1, *tstr2;
1682
Patrick Georgi802ad522014-08-09 17:12:23 +02001683 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr,
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001684 layout_region_name))
1685 continue;
1686
1687 region_number = region_num(layout_region_name);
1688 if (region_number < 0)
1689 continue;
1690
1691 tstr1 = strtok(tempstr, ":");
1692 tstr2 = strtok(NULL, ":");
1693 if (!tstr1 || !tstr2) {
1694 fprintf(stderr, "Could not parse layout file.\n");
1695 exit(EXIT_FAILURE);
1696 }
1697 new_regions[region_number].base = strtol(tstr1,
1698 (char **)NULL, 16);
1699 new_regions[region_number].limit = strtol(tstr2,
1700 (char **)NULL, 16);
1701 new_regions[region_number].size =
1702 new_regions[region_number].limit -
1703 new_regions[region_number].base + 1;
1704
1705 if (new_regions[region_number].size < 0)
1706 new_regions[region_number].size = 0;
1707 }
1708 fclose(romlayout);
1709
1710 /* check new layout */
Alexander Couzensfd5c6582016-10-08 00:37:24 +02001711 for (i = 0; i < max_regions; i++) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001712 if (new_regions[i].size == 0)
1713 continue;
1714
1715 if (new_regions[i].size < current_regions[i].size) {
1716 printf("DANGER: Region %s is shrinking.\n",
1717 region_name(i));
1718 printf(" The region will be truncated to fit.\n");
1719 printf(" This may result in an unusable image.\n");
1720 }
1721
Alexander Couzensfd5c6582016-10-08 00:37:24 +02001722 for (j = i + 1; j < max_regions; j++) {
Bill XIEfa5f9942017-09-12 11:22:29 +08001723 if (regions_collide(&new_regions[i], &new_regions[j])) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001724 fprintf(stderr, "Regions would overlap.\n");
1725 exit(EXIT_FAILURE);
1726 }
1727 }
1728
1729 /* detect if the image size should grow */
1730 if (new_extent < new_regions[i].limit)
1731 new_extent = new_regions[i].limit;
1732 }
1733
Marcello Sylvester Bauer6f9a7782020-02-04 17:20:50 +01001734 /* check if the image is actually a Flash Descriptor region */
1735 if (size == new_regions[0].size) {
1736 printf("The image is a single Flash Descriptor:\n");
1737 printf(" Only the descriptor will be modified\n");
1738 new_extent = size;
1739 } else {
1740 new_extent = next_pow2(new_extent - 1);
1741 if (new_extent != size) {
1742 printf("The image has changed in size.\n");
1743 printf("The old image is %d bytes.\n", size);
1744 printf("The new image is %d bytes.\n", new_extent);
1745 }
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001746 }
1747
1748 /* copy regions to a new image */
1749 new_image = malloc(new_extent);
1750 memset(new_image, 0xff, new_extent);
Alexander Couzensfd5c6582016-10-08 00:37:24 +02001751 for (i = 0; i < max_regions; i++) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001752 int copy_size = new_regions[i].size;
1753 int offset_current = 0, offset_new = 0;
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001754 const struct region *current = &current_regions[i];
1755 const struct region *new = &new_regions[i];
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001756
Bill XIEfa5f9942017-09-12 11:22:29 +08001757 if (new->size == 0)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001758 continue;
1759
Bill XIEfa5f9942017-09-12 11:22:29 +08001760 if (new->size > current->size) {
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001761 /* copy from the end of the current region */
Bill XIEfa5f9942017-09-12 11:22:29 +08001762 copy_size = current->size;
Wim Vervoorn05bc9b32020-01-17 13:48:00 +01001763 if (i == REGION_BIOS)
1764 offset_new = new->size - current->size;
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001765 }
1766
Wim Vervoorn05bc9b32020-01-17 13:48:00 +01001767 if ((i == REGION_BIOS) && (new->size < current->size)) {
1768 /* copy BIOS region to the end of the new region */
Bill XIEfa5f9942017-09-12 11:22:29 +08001769 offset_current = current->size - new->size;
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001770 }
1771
Marcello Sylvester Bauer6f9a7782020-02-04 17:20:50 +01001772 if (size < current->base + offset_current + copy_size) {
1773 printf("Skip descriptor %d (%s) (region missing in the old image)\n", i,
1774 region_name(i));
1775 continue;
1776 };
1777
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001778 printf("Copy Descriptor %d (%s) (%d bytes)\n", i,
1779 region_name(i), copy_size);
Bill XIEfa5f9942017-09-12 11:22:29 +08001780 printf(" from %08x+%08x:%08x (%10d)\n", current->base,
1781 offset_current, current->limit, current->size);
1782 printf(" to %08x+%08x:%08x (%10d)\n", new->base,
1783 offset_new, new->limit, new->size);
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001784
Bill XIEfa5f9942017-09-12 11:22:29 +08001785 memcpy(new_image + new->base + offset_new,
1786 image + current->base + offset_current,
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001787 copy_size);
1788 }
1789
1790 /* update new descriptor regions */
Bill XIE612ec0e2017-08-30 16:10:27 +08001791 frba = find_frba(new_image, new_extent);
1792 if (!frba)
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001793 exit(EXIT_FAILURE);
1794
Marcello Sylvester Bauer6f9a7782020-02-04 17:20:50 +01001795 printf("Modify Flash Descriptor regions\n");
Bill XIE612ec0e2017-08-30 16:10:27 +08001796 for (i = 1; i < max_regions; i++)
Bill XIEfa5f9942017-09-12 11:22:29 +08001797 set_region(frba, i, &new_regions[i]);
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001798
1799 write_image(filename, new_image, new_extent);
1800 free(new_image);
1801}
1802
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001803static void print_version(void)
1804{
1805 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
1806 printf("Copyright (C) 2011 Google Inc.\n\n");
1807 printf
1808 ("This program is free software: you can redistribute it and/or modify\n"
1809 "it under the terms of the GNU General Public License as published by\n"
1810 "the Free Software Foundation, version 2 of the License.\n\n"
1811 "This program is distributed in the hope that it will be useful,\n"
1812 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1813 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -07001814 "GNU General Public License for more details.\n\n");
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001815}
1816
1817static void print_usage(const char *name)
1818{
1819 printf("usage: %s [-vhdix?] <filename>\n", name);
1820 printf("\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001821 " -d | --dump: dump intel firmware descriptor\n"
1822 " -f | --layout <filename> dump regions into a flashrom layout file\n"
Maximilian Brune347596a2023-03-05 20:55:32 +01001823 " -F | --fmap-layout <filename> dump IFD regions into a fmap layout template (.fmd) file\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001824 " -t | --validate Validate that the firmware descriptor layout matches the fmap layout\n"
1825 " -x | --extract: extract intel fd modules\n"
1826 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
1827 " -n | --newlayout <filename> update regions using a flashrom layout file\n"
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01001828 " -O | --output <filename> output filename\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001829 " -s | --spifreq <17|20|30|33|48|50> set the SPI frequency\n"
1830 " -D | --density <512|1|2|4|8|16|32|64> set chip density (512 in KByte, others in MByte)\n"
1831 " -C | --chip <0|1|2> select spi chip on which to operate\n"
1832 " can only be used once per run:\n"
1833 " 0 - both chips (default), 1 - first chip, 2 - second chip\n"
1834 " -e | --em100 set SPI frequency to 20MHz and disable\n"
1835 " Dual Output Fast Read Support\n"
1836 " -l | --lock Lock firmware descriptor and ME region\n"
Usha P412679d2020-10-15 11:25:08 +05301837 " -r | --read Enable CPU/BIOS read access for ME region\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001838 " -u | --unlock Unlock firmware descriptor and ME region\n"
Evgeny Zinoviev8c4c3702020-02-16 20:34:26 +03001839 " -M | --altmedisable <0|1> Set the MeDisable and AltMeDisable (or HAP for skylake or newer platform)\n"
1840 " bits to disable ME\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001841 " -p | --platform Add platform-specific quirks\n"
Subrata Banik15da1742020-08-26 13:27:21 +05301842 " adl - Alder Lake\n"
1843 " aplk - Apollo Lake\n"
1844 " cnl - Cannon Lake\n"
Johnny Line273a022021-06-22 11:26:46 +08001845 " lbg - Lewisburg PCH\n"
Jeff Dalyabd4b962022-01-06 00:52:30 -05001846 " dnv - Denverton\n"
Lean Sheng Tan0faba3c2021-06-09 07:52:24 -07001847 " ehl - Elkhart Lake\n"
Subrata Banik15da1742020-08-26 13:27:21 +05301848 " glk - Gemini Lake\n"
1849 " icl - Ice Lake\n"
Wonkyu Kim3922aa52022-02-02 15:19:05 -08001850 " ifd2 - IFDv2 Platform\n"
Subrata Banik15da1742020-08-26 13:27:21 +05301851 " jsl - Jasper Lake\n"
Subrata Banik9cd85d02023-05-24 23:18:49 +05301852 " mtl - Meteor Lake\n"
Subrata Banik15da1742020-08-26 13:27:21 +05301853 " sklkbl - Sky Lake/Kaby Lake\n"
1854 " tgl - Tiger Lake\n"
Patrick Rudolph16598742022-10-21 15:13:43 +02001855 " wbg - Wellsburg\n"
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001856 " -S | --setpchstrap Write a PCH strap\n"
1857 " -V | --newvalue The new value to write into PCH strap specified by -S\n"
Arthur Heymans4cb888e2019-10-17 19:37:45 +02001858 " -v | --version: print the version\n"
1859 " -h | --help: print this help\n\n"
Jeff Daly3623eca2022-01-05 23:51:40 -05001860 "<region> is one of Descriptor, BIOS, ME, GbE, Platform Data, Secondary BIOS, "
1861 "Device Exp1, EC, Device Exp2, IE, 10GbE_0, 10GbE_1, PTT\n"
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001862 "\n");
1863}
1864
1865int main(int argc, char *argv[])
1866{
1867 int opt, option_index = 0;
1868 int mode_dump = 0, mode_extract = 0, mode_inject = 0, mode_spifreq = 0;
Mathew Kingc7ddc992019-08-08 14:59:25 -06001869 int mode_em100 = 0, mode_locked = 0, mode_unlocked = 0, mode_validate = 0;
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001870 int mode_layout = 0, mode_newlayout = 0, mode_density = 0, mode_setstrap = 0;
Maximilian Brune347596a2023-03-05 20:55:32 +01001871 int mode_read = 0, mode_altmedisable = 0, altmedisable = 0, mode_fmap_template = 0;
Bill XIEfa5f9942017-09-12 11:22:29 +08001872 char *region_type_string = NULL, *region_fname = NULL;
1873 const char *layout_fname = NULL;
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01001874 char *new_filename = NULL;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001875 int region_type = -1, inputfreq = 0;
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001876 unsigned int value = 0;
1877 unsigned int pchstrap = 0;
Jan Tatjefa317512016-03-11 00:52:07 +01001878 unsigned int new_density = 0;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001879 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
1880
Bill XIEfa5f9942017-09-12 11:22:29 +08001881 static const struct option long_options[] = {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001882 {"dump", 0, NULL, 'd'},
Chris Douglass03ce0142014-02-26 13:30:13 -05001883 {"layout", 1, NULL, 'f'},
Maximilian Brune347596a2023-03-05 20:55:32 +01001884 {"fmap-template", 1, NULL, 'F'},
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001885 {"extract", 0, NULL, 'x'},
1886 {"inject", 1, NULL, 'i'},
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001887 {"newlayout", 1, NULL, 'n'},
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01001888 {"output", 1, NULL, 'O'},
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001889 {"spifreq", 1, NULL, 's'},
Jan Tatjefa317512016-03-11 00:52:07 +01001890 {"density", 1, NULL, 'D'},
1891 {"chip", 1, NULL, 'C'},
Bill XIEb3e15a22017-09-07 18:34:50 +08001892 {"altmedisable", 1, NULL, 'M'},
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07001893 {"em100", 0, NULL, 'e'},
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001894 {"lock", 0, NULL, 'l'},
Usha P412679d2020-10-15 11:25:08 +05301895 {"read", 0, NULL, 'r'},
Stefan Reinauer3c53d332012-09-26 17:33:39 -07001896 {"unlock", 0, NULL, 'u'},
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001897 {"version", 0, NULL, 'v'},
1898 {"help", 0, NULL, 'h'},
Andrey Petrov96ecb772016-10-31 19:31:54 -07001899 {"platform", 0, NULL, 'p'},
Mathew Kingc7ddc992019-08-08 14:59:25 -06001900 {"validate", 0, NULL, 't'},
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001901 {"setpchstrap", 1, NULL, 'S'},
1902 {"newvalue", 1, NULL, 'V'},
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001903 {0, 0, 0, 0}
1904 };
1905
Maximilian Brune347596a2023-03-05 20:55:32 +01001906 while ((opt = getopt_long(argc, argv, "S:V:df:F:D:C:M:xi:n:O:s:p:elruvth?",
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001907 long_options, &option_index)) != EOF) {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001908 switch (opt) {
1909 case 'd':
1910 mode_dump = 1;
1911 break;
Patrick Rudolph802cbee2020-05-25 12:18:11 +02001912 case 'S':
1913 mode_setstrap = 1;
1914 pchstrap = strtoul(optarg, NULL, 0);
1915 break;
1916 case 'V':
1917 value = strtoul(optarg, NULL, 0);
1918 break;
Chris Douglass03ce0142014-02-26 13:30:13 -05001919 case 'f':
1920 mode_layout = 1;
1921 layout_fname = strdup(optarg);
1922 if (!layout_fname) {
1923 fprintf(stderr, "No layout file specified\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001924 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Chris Douglass03ce0142014-02-26 13:30:13 -05001925 exit(EXIT_FAILURE);
1926 }
1927 break;
Maximilian Brune347596a2023-03-05 20:55:32 +01001928 case 'F':
1929 mode_fmap_template = 1;
1930 layout_fname = strdup(optarg);
1931 if (!layout_fname) {
1932 fprintf(stderr, "No layout file specified\n");
1933 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
1934 exit(EXIT_FAILURE);
1935 }
1936 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001937 case 'x':
1938 mode_extract = 1;
1939 break;
1940 case 'i':
1941 // separate type and file name
1942 region_type_string = strdup(optarg);
1943 region_fname = strchr(region_type_string, ':');
1944 if (!region_fname) {
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001945 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001946 exit(EXIT_FAILURE);
1947 }
1948 region_fname[0] = '\0';
1949 region_fname++;
1950 // Descriptor, BIOS, ME, GbE, Platform
1951 // valid type?
1952 if (!strcasecmp("Descriptor", region_type_string))
1953 region_type = 0;
1954 else if (!strcasecmp("BIOS", region_type_string))
1955 region_type = 1;
1956 else if (!strcasecmp("ME", region_type_string))
1957 region_type = 2;
1958 else if (!strcasecmp("GbE", region_type_string))
1959 region_type = 3;
Jeff Daly3623eca2022-01-05 23:51:40 -05001960 else if (!strcasecmp("Platform Data", region_type_string))
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001961 region_type = 4;
Jeff Daly3623eca2022-01-05 23:51:40 -05001962 else if (!strcasecmp("Device Exp1", region_type_string))
Stefan Reinauere4e08f22020-10-26 10:15:42 -07001963 region_type = 5;
Jeff Daly3623eca2022-01-05 23:51:40 -05001964 else if (!strcasecmp("Secondary BIOS", region_type_string))
Stefan Reinauere4e08f22020-10-26 10:15:42 -07001965 region_type = 6;
Jeff Daly3623eca2022-01-05 23:51:40 -05001966 else if (!strcasecmp("Reserved", region_type_string))
Stefan Reinauere4e08f22020-10-26 10:15:42 -07001967 region_type = 7;
Duncan Laurie1f7fd722015-06-22 11:14:48 -07001968 else if (!strcasecmp("EC", region_type_string))
1969 region_type = 8;
Jeff Daly3623eca2022-01-05 23:51:40 -05001970 else if (!strcasecmp("Device Exp2", region_type_string))
1971 region_type = 9;
1972 else if (!strcasecmp("IE", region_type_string))
1973 region_type = 10;
1974 else if (!strcasecmp("10GbE_0", region_type_string))
1975 region_type = 11;
1976 else if (!strcasecmp("10GbE_1", region_type_string))
1977 region_type = 12;
1978 else if (!strcasecmp("PTT", region_type_string))
1979 region_type = 15;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001980 if (region_type == -1) {
1981 fprintf(stderr, "No such region type: '%s'\n\n",
1982 region_type_string);
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001983 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07001984 exit(EXIT_FAILURE);
1985 }
1986 mode_inject = 1;
1987 break;
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001988 case 'n':
1989 mode_newlayout = 1;
1990 layout_fname = strdup(optarg);
1991 if (!layout_fname) {
1992 fprintf(stderr, "No layout file specified\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01001993 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Chris Douglass4eabe1e2014-02-27 09:25:19 -05001994 exit(EXIT_FAILURE);
1995 }
1996 break;
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01001997 case 'O':
1998 new_filename = strdup(optarg);
1999 if (!new_filename) {
2000 fprintf(stderr, "No output filename specified\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002001 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002002 exit(EXIT_FAILURE);
2003 }
2004 break;
Jan Tatjefa317512016-03-11 00:52:07 +01002005 case 'D':
2006 mode_density = 1;
2007 new_density = strtoul(optarg, NULL, 0);
2008 switch (new_density) {
2009 case 512:
2010 new_density = COMPONENT_DENSITY_512KB;
2011 break;
2012 case 1:
2013 new_density = COMPONENT_DENSITY_1MB;
2014 break;
2015 case 2:
2016 new_density = COMPONENT_DENSITY_2MB;
2017 break;
2018 case 4:
2019 new_density = COMPONENT_DENSITY_4MB;
2020 break;
2021 case 8:
2022 new_density = COMPONENT_DENSITY_8MB;
2023 break;
2024 case 16:
2025 new_density = COMPONENT_DENSITY_16MB;
2026 break;
2027 case 32:
2028 new_density = COMPONENT_DENSITY_32MB;
2029 break;
2030 case 64:
2031 new_density = COMPONENT_DENSITY_64MB;
2032 break;
2033 case 0:
2034 new_density = COMPONENT_DENSITY_UNUSED;
2035 break;
2036 default:
2037 printf("error: Unknown density\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002038 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Jan Tatjefa317512016-03-11 00:52:07 +01002039 exit(EXIT_FAILURE);
2040 }
2041 break;
2042 case 'C':
2043 selected_chip = strtol(optarg, NULL, 0);
2044 if (selected_chip > 2) {
2045 fprintf(stderr, "error: Invalid chip selection\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002046 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Jan Tatjefa317512016-03-11 00:52:07 +01002047 exit(EXIT_FAILURE);
2048 }
2049 break;
Bill XIEb3e15a22017-09-07 18:34:50 +08002050 case 'M':
2051 mode_altmedisable = 1;
2052 altmedisable = strtol(optarg, NULL, 0);
2053 if (altmedisable > 1) {
2054 fprintf(stderr, "error: Illegal value\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002055 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Bill XIEb3e15a22017-09-07 18:34:50 +08002056 exit(EXIT_FAILURE);
2057 }
2058 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002059 case 's':
2060 // Parse the requested SPI frequency
2061 inputfreq = strtol(optarg, NULL, 0);
2062 switch (inputfreq) {
Duncan Laurie1f7fd722015-06-22 11:14:48 -07002063 case 17:
2064 spifreq = SPI_FREQUENCY_17MHZ;
2065 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002066 case 20:
2067 spifreq = SPI_FREQUENCY_20MHZ;
2068 break;
Duncan Laurie1f7fd722015-06-22 11:14:48 -07002069 case 30:
2070 spifreq = SPI_FREQUENCY_50MHZ_30MHZ;
2071 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002072 case 33:
2073 spifreq = SPI_FREQUENCY_33MHZ;
2074 break;
Duncan Laurie1f7fd722015-06-22 11:14:48 -07002075 case 48:
2076 spifreq = SPI_FREQUENCY_48MHZ;
2077 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002078 case 50:
Duncan Laurie1f7fd722015-06-22 11:14:48 -07002079 spifreq = SPI_FREQUENCY_50MHZ_30MHZ;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002080 break;
2081 default:
2082 fprintf(stderr, "Invalid SPI Frequency: %d\n",
2083 inputfreq);
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002084 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002085 exit(EXIT_FAILURE);
2086 }
2087 mode_spifreq = 1;
2088 break;
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07002089 case 'e':
2090 mode_em100 = 1;
2091 break;
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002092 case 'l':
2093 mode_locked = 1;
2094 if (mode_unlocked == 1) {
2095 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
2096 exit(EXIT_FAILURE);
2097 }
2098 break;
Usha P412679d2020-10-15 11:25:08 +05302099 case 'r':
2100 mode_read = 1;
2101 break;
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002102 case 'u':
2103 mode_unlocked = 1;
2104 if (mode_locked == 1) {
2105 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
2106 exit(EXIT_FAILURE);
2107 }
2108 break;
Andrey Petrov96ecb772016-10-31 19:31:54 -07002109 case 'p':
2110 if (!strcmp(optarg, "aplk")) {
Furquan Shaikhc0257dd2018-05-02 23:29:04 -07002111 platform = PLATFORM_APL;
2112 } else if (!strcmp(optarg, "cnl")) {
2113 platform = PLATFORM_CNL;
Johnny Line273a022021-06-22 11:26:46 +08002114 } else if (!strcmp(optarg, "lbg")) {
2115 platform = PLATFORM_LBG;
Jeff Dalyabd4b962022-01-06 00:52:30 -05002116 } else if (!strcmp(optarg, "dnv")) {
2117 platform = PLATFORM_DNV;
Lean Sheng Tan0faba3c2021-06-09 07:52:24 -07002118 } else if (!strcmp(optarg, "ehl")) {
2119 platform = PLATFORM_EHL;
Furquan Shaikhc0257dd2018-05-02 23:29:04 -07002120 } else if (!strcmp(optarg, "glk")) {
2121 platform = PLATFORM_GLK;
Aamir Bohra1018be22018-06-29 15:08:50 +05302122 } else if (!strcmp(optarg, "icl")) {
2123 platform = PLATFORM_ICL;
rkanabard64b0462019-08-30 11:40:08 +05302124 } else if (!strcmp(optarg, "jsl")) {
Lean Sheng Tan0faba3c2021-06-09 07:52:24 -07002125 platform = PLATFORM_JSL;
Furquan Shaikh088b6e82018-03-21 10:42:37 -07002126 } else if (!strcmp(optarg, "sklkbl")) {
2127 platform = PLATFORM_SKLKBL;
Ravi Sarawadi7d9d63b2019-10-22 13:45:36 -07002128 } else if (!strcmp(optarg, "tgl")) {
2129 platform = PLATFORM_TGL;
Subrata Banik46f80732020-03-14 15:01:42 +05302130 } else if (!strcmp(optarg, "adl")) {
2131 platform = PLATFORM_ADL;
Wonkyu Kim3922aa52022-02-02 15:19:05 -08002132 } else if (!strcmp(optarg, "ifd2")) {
2133 platform = PLATFORM_IFD2;
Subrata Banikca82e612022-01-20 18:51:21 +05302134 } else if (!strcmp(optarg, "mtl")) {
2135 platform = PLATFORM_MTL;
Patrick Rudolph16598742022-10-21 15:13:43 +02002136 } else if (!strcmp(optarg, "wbg")) {
2137 platform = PLATFORM_WBG;
Andrey Petrov96ecb772016-10-31 19:31:54 -07002138 } else {
2139 fprintf(stderr, "Unknown platform: %s\n", optarg);
2140 exit(EXIT_FAILURE);
2141 }
2142 break;
Mathew Kingc7ddc992019-08-08 14:59:25 -06002143 case 't':
2144 mode_validate = 1;
2145 break;
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002146 case 'v':
2147 print_version();
2148 exit(EXIT_SUCCESS);
2149 break;
2150 case 'h':
2151 case '?':
2152 default:
2153 print_usage(argv[0]);
2154 exit(EXIT_SUCCESS);
2155 break;
2156 }
2157 }
2158
Maximilian Brune347596a2023-03-05 20:55:32 +01002159 if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject +
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002160 mode_setstrap + mode_newlayout + (mode_spifreq | mode_em100 |
2161 mode_unlocked | mode_locked) + mode_altmedisable + mode_validate) > 1) {
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002162 fprintf(stderr, "You may not specify more than one mode.\n\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002163 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002164 exit(EXIT_FAILURE);
2165 }
2166
Maximilian Brune347596a2023-03-05 20:55:32 +01002167 if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject +
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002168 mode_setstrap + mode_newlayout + mode_spifreq + mode_em100 +
2169 mode_locked + mode_unlocked + mode_density + mode_altmedisable + mode_validate) == 0) {
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002170 fprintf(stderr, "You need to specify a mode.\n\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002171 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002172 exit(EXIT_FAILURE);
2173 }
2174
2175 if (optind + 1 != argc) {
2176 fprintf(stderr, "You need to specify a file.\n\n");
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002177 fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002178 exit(EXIT_FAILURE);
2179 }
2180
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002181 if (platform == -1)
2182 fprintf(stderr, "Warning: No platform specified. Output may be incomplete\n");
2183
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002184 char *filename = argv[optind];
Scott Duplichanf2c98372014-12-12 21:03:06 -06002185 int bios_fd = open(filename, O_RDONLY | O_BINARY);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002186 if (bios_fd == -1) {
2187 perror("Could not open file");
2188 exit(EXIT_FAILURE);
2189 }
2190 struct stat buf;
2191 if (fstat(bios_fd, &buf) == -1) {
2192 perror("Could not stat file");
2193 exit(EXIT_FAILURE);
2194 }
2195 int size = buf.st_size;
2196
2197 printf("File %s is %d bytes\n", filename, size);
2198
2199 char *image = malloc(size);
2200 if (!image) {
2201 printf("Out of memory.\n");
2202 exit(EXIT_FAILURE);
2203 }
2204
2205 if (read(bios_fd, image, size) != size) {
2206 perror("Could not read file");
2207 exit(EXIT_FAILURE);
2208 }
2209
2210 close(bios_fd);
2211
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002212 // generate new filename
2213 if (new_filename == NULL) {
2214 new_filename = (char *) malloc((strlen(filename) + 5) * sizeof(char));
2215 if (!new_filename) {
2216 printf("Out of memory.\n");
2217 exit(EXIT_FAILURE);
2218 }
2219 // - 5: leave room for ".new\0"
2220 strcpy(new_filename, filename);
2221 strcat(new_filename, ".new");
2222 }
2223
Duncan Laurie1f7fd722015-06-22 11:14:48 -07002224 check_ifd_version(image, size);
2225
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002226 if (mode_dump)
2227 dump_fd(image, size);
2228
Chris Douglass03ce0142014-02-26 13:30:13 -05002229 if (mode_layout)
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002230 dump_flashrom_layout(image, size, layout_fname);
Chris Douglass03ce0142014-02-26 13:30:13 -05002231
Maximilian Brune347596a2023-03-05 20:55:32 +01002232 if (mode_fmap_template)
2233 create_fmap_template(image, size, layout_fname);
2234
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002235 if (mode_extract)
2236 write_regions(image, size);
2237
Mathew Kingc7ddc992019-08-08 14:59:25 -06002238 if (mode_validate)
2239 validate_layout(image, size);
2240
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002241 if (mode_inject)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002242 inject_region(new_filename, image, size, region_type,
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002243 region_fname);
2244
Chris Douglass4eabe1e2014-02-27 09:25:19 -05002245 if (mode_newlayout)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002246 new_layout(new_filename, image, size, layout_fname);
Chris Douglass4eabe1e2014-02-27 09:25:19 -05002247
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002248 if (mode_spifreq)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002249 set_spi_frequency(new_filename, image, size, spifreq);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002250
Jan Tatjefa317512016-03-11 00:52:07 +01002251 if (mode_density)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002252 set_chipdensity(new_filename, image, size, new_density);
Jan Tatjefa317512016-03-11 00:52:07 +01002253
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07002254 if (mode_em100)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002255 set_em100_mode(new_filename, image, size);
Stefan Reinauer1b1309f2012-05-11 15:53:43 -07002256
Alexander Couzensd12ea112016-09-10 13:33:05 +02002257 if (mode_locked)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002258 lock_descriptor(new_filename, image, size);
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002259
Usha P412679d2020-10-15 11:25:08 +05302260 if (mode_read)
2261 enable_cpu_read_me(new_filename, image, size);
2262
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002263 if (mode_unlocked)
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002264 unlock_descriptor(new_filename, image, size);
Stefan Reinauer3c53d332012-09-26 17:33:39 -07002265
Patrick Rudolph802cbee2020-05-25 12:18:11 +02002266 if (mode_setstrap) {
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002267 struct fpsba *fpsba = find_fpsba(image, size);
2268 const struct fdbar *fdb = find_fd(image, size);
Patrick Rudolph802cbee2020-05-25 12:18:11 +02002269 set_pchstrap(fpsba, fdb, pchstrap, value);
2270 write_image(new_filename, image, size);
2271 }
2272
Bill XIEb3e15a22017-09-07 18:34:50 +08002273 if (mode_altmedisable) {
Maximilian Bruneab0e6802023-03-05 04:34:40 +01002274 struct fpsba *fpsba = find_fpsba(image, size);
2275 struct fmsba *fmsba = find_fmsba(image, size);
Bill XIEb3e15a22017-09-07 18:34:50 +08002276 fpsba_set_altmedisable(fpsba, fmsba, altmedisable);
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002277 write_image(new_filename, image, size);
Bill XIEb3e15a22017-09-07 18:34:50 +08002278 }
2279
Marcello Sylvester Bauerdc1596c2020-02-11 13:31:38 +01002280 free(new_filename);
Stefan Reinauer1c795ad12011-10-14 12:49:41 -07002281 free(image);
2282
2283 return 0;
2284}