Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* ifdtool - dump Intel Firmware Descriptor information */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 3 | |
| 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 XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 12 | #include <commonlib/helpers.h> |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 13 | #include <fmap.h> |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 14 | #include "ifdtool.h" |
| 15 | |
Scott Duplichan | f2c9837 | 2014-12-12 21:03:06 -0600 | [diff] [blame] | 16 | #ifndef O_BINARY |
| 17 | #define O_BINARY 0 |
| 18 | #endif |
| 19 | |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 20 | /** |
| 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 Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 31 | /** |
| 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 Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 47 | static int max_regions_from_fdbar(const struct fdbar *fdb); |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 48 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 49 | static int ifd_version; |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 50 | static int chipset; |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 51 | static unsigned int max_regions = 0; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 52 | static int selected_chip = 0; |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 53 | static int platform = -1; |
Chris Douglass | e271865 | 2014-02-28 08:54:41 -0500 | [diff] [blame] | 54 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 55 | static const struct region_name region_names[MAX_REGIONS] = { |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 56 | { "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 Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 61 | { "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 King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 64 | { "EC", "ec", "flashregion_8_ec.bin", "SI_EC" }, |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 65 | { "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 Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 74 | /* port from flashrom */ |
| 75 | static const char *const ich_chipset_names[] = { |
| 76 | "Unknown ICH", |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 77 | "ICH8", |
| 78 | "ICH9", |
| 79 | "ICH10", |
Subrata Banik | 89db225 | 2020-08-26 14:49:17 +0530 | [diff] [blame] | 80 | "Unknown PCH", |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 81 | "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 Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 90 | "Apollo Lake: N3xxx, J3xxx", |
Subrata Banik | 89db225 | 2020-08-26 14:49:17 +0530 | [diff] [blame] | 91 | "Gemini Lake: N5xxx, J5xxx, N4xxx, J4xxx", |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 92 | "Jasper Lake: N6xxx, N51xx, N45xx", |
| 93 | "Elkhart Lake: x6000 series Atom", |
Subrata Banik | 89db225 | 2020-08-26 14:49:17 +0530 | [diff] [blame] | 94 | "100/200 series Sunrise Point", |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 95 | "300 series Cannon Point", |
| 96 | "400 series Ice Point", |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 97 | "500 series Tiger Point/ 600 series Alder Point", |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 98 | "C620 series Lewisburg", |
Patrick Rudolph | 1920900 | 2022-10-22 10:34:05 +0200 | [diff] [blame] | 99 | "Denverton: C39xx", |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 100 | NULL |
| 101 | }; |
| 102 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 103 | static struct fdbar *find_fd(char *image, int size) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 104 | { |
| 105 | int i, found = 0; |
| 106 | |
| 107 | /* Scan for FD signature */ |
| 108 | for (i = 0; i < (size - 4); i += 4) { |
| 109 | if (*(uint32_t *) (image + i) == 0x0FF0A55A) { |
| 110 | found = 1; |
| 111 | break; // signature found. |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!found) { |
| 116 | printf("No Flash Descriptor found in this image\n"); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 120 | struct fdbar *fdb = (struct fdbar *) (image + i); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 121 | return PTR_IN_RANGE(fdb, image, size) ? fdb : NULL; |
| 122 | } |
| 123 | |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 124 | static char *find_flumap(char *image, int size) |
| 125 | { |
| 126 | /* The upper map is located in the word before the 256B-long OEM section |
| 127 | * at the end of the 4kB-long flash descriptor. In the official |
| 128 | * documentation this is defined as FDBAR + 0xEFC. However, starting |
| 129 | * with B-Step of Ibex Peak (5 series) the signature (and thus FDBAR) |
| 130 | * has moved 16 bytes back to offset 0x10 of the image. Although |
| 131 | * official documentation still maintains the offset relative to FDBAR |
| 132 | * this is wrong and a simple fixed offset from the start of the image |
| 133 | * works. |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 134 | */ |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 135 | char *flumap = image + 4096 - 256 - 4; |
| 136 | return PTR_IN_RANGE(flumap, image, size) ? flumap : NULL; |
| 137 | } |
| 138 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 139 | static struct fcba *find_fcba(char *image, int size) |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 140 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 141 | struct fdbar *fdb = find_fd(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 142 | if (!fdb) |
| 143 | return NULL; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 144 | struct fcba *fcba = (struct fcba *) (image + ((fdb->flmap0 & 0xff) << 4)); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 145 | return PTR_IN_RANGE(fcba, image, size) ? fcba : NULL; |
| 146 | |
| 147 | } |
| 148 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 149 | static struct fmba *find_fmba(char *image, int size) |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 150 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 151 | struct fdbar *fdb = find_fd(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 152 | if (!fdb) |
| 153 | return NULL; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 154 | struct fmba *fmba = (struct fmba *) (image + ((fdb->flmap1 & 0xff) << 4)); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 155 | return PTR_IN_RANGE(fmba, image, size) ? fmba : NULL; |
| 156 | } |
| 157 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 158 | static struct frba *find_frba(char *image, int size) |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 159 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 160 | struct fdbar *fdb = find_fd(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 161 | if (!fdb) |
| 162 | return NULL; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 163 | struct frba *frba = |
| 164 | (struct frba *) (image + (((fdb->flmap0 >> 16) & 0xff) << 4)); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 165 | return PTR_IN_RANGE(frba, image, size) ? frba : NULL; |
| 166 | } |
| 167 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 168 | static struct fpsba *find_fpsba(char *image, int size) |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 169 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 170 | struct fdbar *fdb = find_fd(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 171 | if (!fdb) |
| 172 | return NULL; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 173 | struct fpsba *fpsba = |
| 174 | (struct fpsba *) (image + (((fdb->flmap1 >> 16) & 0xff) << 4)); |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 175 | |
| 176 | int SSL = ((fdb->flmap1 >> 24) & 0xff) * sizeof(uint32_t); |
| 177 | if ((((char *)fpsba) + SSL) >= (image + size)) |
| 178 | return NULL; |
| 179 | return fpsba; |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 182 | static struct fmsba *find_fmsba(char *image, int size) |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 183 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 184 | struct fdbar *fdb = find_fd(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 185 | if (!fdb) |
| 186 | return NULL; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 187 | struct fmsba *fmsba = (struct fmsba *) (image + ((fdb->flmap2 & 0xff) << 4)); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 188 | return PTR_IN_RANGE(fmsba, image, size) ? fmsba : NULL; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 191 | /* port from flashrom */ |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 192 | static enum ich_chipset ifd1_guess_chipset(char *image, int size) |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 193 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 194 | const struct fdbar *fdb = find_fd(image, size); |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 195 | if (!fdb) |
| 196 | exit(EXIT_FAILURE); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 197 | uint32_t iccriba = (fdb->flmap2 >> 16) & 0xff; |
| 198 | uint32_t msl = (fdb->flmap2 >> 8) & 0xff; |
| 199 | uint32_t isl = (fdb->flmap1 >> 24); |
Subrata Banik | 89db225 | 2020-08-26 14:49:17 +0530 | [diff] [blame] | 200 | |
| 201 | /* Rest for IFD1 chipset type */ |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 202 | if (iccriba == 0x00) { |
| 203 | if (msl == 0 && isl <= 2) |
| 204 | return CHIPSET_ICH8; |
| 205 | else if (isl <= 2) |
| 206 | return CHIPSET_ICH9; |
| 207 | else if (isl <= 10) |
| 208 | return CHIPSET_ICH10; |
| 209 | else if (isl <= 16) |
| 210 | return CHIPSET_5_SERIES_IBEX_PEAK; |
| 211 | printf("Peculiar firmware descriptor, assuming Ibex Peak compatibility.\n"); |
| 212 | return CHIPSET_5_SERIES_IBEX_PEAK; |
| 213 | } else if (iccriba < 0x31 && (fdb->flmap2 & 0xff) < 0x30) { |
| 214 | if (msl == 0 && isl <= 17) |
| 215 | return CHIPSET_BAYTRAIL; |
| 216 | else if (msl <= 1 && isl <= 18) |
| 217 | return CHIPSET_6_SERIES_COUGAR_POINT; |
| 218 | else if (msl <= 1 && isl <= 21) |
| 219 | return CHIPSET_8_SERIES_LYNX_POINT; |
| 220 | printf("Peculiar firmware descriptor, assuming Wildcat Point compatibility.\n"); |
| 221 | return CHIPSET_9_SERIES_WILDCAT_POINT; |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 222 | } |
Jan Tatje | b7eec4a | 2021-07-05 08:03:07 +0200 | [diff] [blame] | 223 | return CHIPSET_PCH_UNKNOWN; |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 224 | } |
| 225 | |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 226 | static enum ich_chipset ifd2_platform_to_chipset(const int pindex) |
| 227 | { |
| 228 | switch (pindex) { |
Subrata Banik | 6da003c | 2021-07-14 13:52:03 +0530 | [diff] [blame] | 229 | case PLATFORM_APL: |
| 230 | return CHIPSET_N_J_SERIES_APOLLO_LAKE; |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 231 | case PLATFORM_GLK: |
| 232 | return CHIPSET_N_J_SERIES_GEMINI_LAKE; |
| 233 | case PLATFORM_JSL: |
| 234 | return CHIPSET_N_SERIES_JASPER_LAKE; |
| 235 | case PLATFORM_EHL: |
| 236 | return CHIPSET_x6000_SERIES_ELKHART_LAKE; |
Jan Tatje | b7eec4a | 2021-07-05 08:03:07 +0200 | [diff] [blame] | 237 | case PLATFORM_SKLKBL: |
| 238 | return CHIPSET_100_200_SERIES_SUNRISE_POINT; |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 239 | case PLATFORM_CNL: |
| 240 | return CHIPSET_300_SERIES_CANNON_POINT; |
| 241 | case PLATFORM_TGL: |
| 242 | case PLATFORM_ADL: |
Wonkyu Kim | 3922aa5 | 2022-02-02 15:19:05 -0800 | [diff] [blame] | 243 | case PLATFORM_IFD2: |
Subrata Banik | ca82e61 | 2022-01-20 18:51:21 +0530 | [diff] [blame] | 244 | case PLATFORM_MTL: |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 245 | return CHIPSET_500_600_SERIES_TIGER_ALDER_POINT; |
| 246 | case PLATFORM_ICL: |
| 247 | return CHIPSET_400_SERIES_ICE_POINT; |
Johnny Lin | e273a02 | 2021-06-22 11:26:46 +0800 | [diff] [blame] | 248 | case PLATFORM_LBG: |
| 249 | return CHIPSET_C620_SERIES_LEWISBURG; |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 250 | case PLATFORM_DNV: |
| 251 | return CHIPSET_DENVERTON; |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 252 | case PLATFORM_WBG: |
| 253 | return CHIPSET_8_SERIES_WELLSBURG; |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 254 | default: |
| 255 | return CHIPSET_PCH_UNKNOWN; |
| 256 | } |
| 257 | } |
| 258 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 259 | /* |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 260 | * Some newer platforms have re-defined the FCBA field that was used to |
| 261 | * distinguish IFD v1 v/s v2. Define a list of platforms that we know do not |
| 262 | * have the required FCBA field, but are IFD v2 and return true if current |
| 263 | * platform is one of them. |
| 264 | */ |
| 265 | static int is_platform_ifd_2(void) |
| 266 | { |
| 267 | static const int ifd_2_platforms[] = { |
Subrata Banik | 6da003c | 2021-07-14 13:52:03 +0530 | [diff] [blame] | 268 | PLATFORM_APL, |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 269 | PLATFORM_GLK, |
| 270 | PLATFORM_CNL, |
Johnny Lin | e273a02 | 2021-06-22 11:26:46 +0800 | [diff] [blame] | 271 | PLATFORM_LBG, |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 272 | PLATFORM_DNV, |
Aamir Bohra | 1018be2 | 2018-06-29 15:08:50 +0530 | [diff] [blame] | 273 | PLATFORM_ICL, |
Ravi Sarawadi | 7d9d63b | 2019-10-22 13:45:36 -0700 | [diff] [blame] | 274 | PLATFORM_TGL, |
rkanabar | d64b046 | 2019-08-30 11:40:08 +0530 | [diff] [blame] | 275 | PLATFORM_JSL, |
Lean Sheng Tan | 0faba3c | 2021-06-09 07:52:24 -0700 | [diff] [blame] | 276 | PLATFORM_EHL, |
Subrata Banik | 46f8073 | 2020-03-14 15:01:42 +0530 | [diff] [blame] | 277 | PLATFORM_ADL, |
Jan Tatje | b7eec4a | 2021-07-05 08:03:07 +0200 | [diff] [blame] | 278 | PLATFORM_SKLKBL, |
Wonkyu Kim | 3922aa5 | 2022-02-02 15:19:05 -0800 | [diff] [blame] | 279 | PLATFORM_IFD2, |
Subrata Banik | ca82e61 | 2022-01-20 18:51:21 +0530 | [diff] [blame] | 280 | PLATFORM_MTL, |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 281 | PLATFORM_WBG, |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 282 | }; |
| 283 | unsigned int i; |
| 284 | |
| 285 | for (i = 0; i < ARRAY_SIZE(ifd_2_platforms); i++) { |
| 286 | if (platform == ifd_2_platforms[i]) |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 293 | static void check_ifd_version(char *image, int size) |
| 294 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 295 | const struct fdbar *fdb = find_fd(image, size); |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 296 | |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 297 | if (is_platform_ifd_2()) { |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 298 | chipset = ifd2_platform_to_chipset(platform); |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 299 | if (chipset == CHIPSET_8_SERIES_WELLSBURG) |
| 300 | ifd_version = IFD_VERSION_1_5; |
| 301 | else |
| 302 | ifd_version = IFD_VERSION_2; |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 303 | max_regions = MIN(max_regions_from_fdbar(fdb), MAX_REGIONS); |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 304 | } else { |
| 305 | ifd_version = IFD_VERSION_1; |
| 306 | chipset = ifd1_guess_chipset(image, size); |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 307 | max_regions = MIN(max_regions_from_fdbar(fdb), MAX_REGIONS_OLD); |
Subrata Banik | 8c082e5 | 2021-06-10 23:02:29 +0530 | [diff] [blame] | 308 | } |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 311 | static struct region get_region(const struct frba *frba, unsigned int region_type) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 312 | { |
Aaron Durbin | 0a31e59 | 2015-08-12 11:44:02 -0500 | [diff] [blame] | 313 | int base_mask; |
| 314 | int limit_mask; |
Ronald G. Minnich | 8db3c2a | 2017-05-25 10:48:57 -0700 | [diff] [blame] | 315 | uint32_t flreg; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 316 | struct region region; |
Aaron Durbin | 0a31e59 | 2015-08-12 11:44:02 -0500 | [diff] [blame] | 317 | |
| 318 | if (ifd_version >= IFD_VERSION_2) |
| 319 | base_mask = 0x7fff; |
| 320 | else |
| 321 | base_mask = 0xfff; |
| 322 | |
| 323 | limit_mask = base_mask << 16; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 324 | |
Youness Alaoui | f68e3ba | 2017-10-04 11:35:59 -0400 | [diff] [blame] | 325 | if (region_type >= max_regions) { |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 326 | fprintf(stderr, "Invalid region type %d.\n", region_type); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 327 | exit (EXIT_FAILURE); |
| 328 | } |
| 329 | |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 330 | flreg = frba->flreg[region_type]; |
Ronald G. Minnich | 8db3c2a | 2017-05-25 10:48:57 -0700 | [diff] [blame] | 331 | region.base = (flreg & base_mask) << 12; |
| 332 | region.limit = ((flreg & limit_mask) >> 4) | 0xfff; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 333 | region.size = region.limit - region.base + 1; |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 334 | region.type = region_type; |
Aaron Durbin | 0a31e59 | 2015-08-12 11:44:02 -0500 | [diff] [blame] | 335 | |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 336 | if (region.size < 0) |
| 337 | region.size = 0; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 338 | |
| 339 | return region; |
| 340 | } |
| 341 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 342 | static void set_region(struct frba *frba, unsigned int region_type, |
| 343 | const struct region *region) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 344 | { |
Youness Alaoui | f68e3ba | 2017-10-04 11:35:59 -0400 | [diff] [blame] | 345 | if (region_type >= max_regions) { |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 346 | fprintf(stderr, "Invalid region type %u.\n", region_type); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 347 | exit (EXIT_FAILURE); |
| 348 | } |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 349 | |
| 350 | frba->flreg[region_type] = |
| 351 | (((region->limit >> 12) & 0x7fff) << 16) | |
| 352 | ((region->base >> 12) & 0x7fff); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 355 | static const char *region_name(unsigned int region_type) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 356 | { |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 357 | if (region_type >= max_regions) { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 358 | fprintf(stderr, "Invalid region type.\n"); |
| 359 | exit (EXIT_FAILURE); |
| 360 | } |
| 361 | |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 362 | return region_names[region_type].pretty; |
| 363 | } |
| 364 | |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 365 | static int region_num(const char *name) |
| 366 | { |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 367 | unsigned int i; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 368 | |
Alexander Couzens | fd5c658 | 2016-10-08 00:37:24 +0200 | [diff] [blame] | 369 | for (i = 0; i < max_regions; i++) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 370 | if (strcasecmp(name, region_names[i].pretty) == 0) |
| 371 | return i; |
| 372 | if (strcasecmp(name, region_names[i].terse) == 0) |
| 373 | return i; |
| 374 | } |
| 375 | |
| 376 | return -1; |
| 377 | } |
| 378 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 379 | static void dump_region(unsigned int num, const struct frba *frba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 380 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 381 | struct region region = get_region(frba, num); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 382 | printf(" Flash Region %d (%s): %08x - %08x %s\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 383 | num, region_name(num), region.base, region.limit, |
| 384 | region.size < 1 ? "(unused)" : ""); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 385 | } |
| 386 | |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 387 | static int sort_compare(const void *a, const void *b) |
| 388 | { |
| 389 | return *(size_t *)a - *(size_t *)b; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * IFDv1 always has 8 regions, while IFDv2 always has 16 regions. |
| 394 | * |
| 395 | * It's platform specific which regions are used or are reserved. |
| 396 | * The 'SPI programming guide' as the name says is a guide only, |
| 397 | * not a specification what the hardware actually does. |
| 398 | * The best to do is not to rely on the guide, but detect how many |
| 399 | * regions are present in the IFD and expose them all. |
| 400 | * |
| 401 | * Very early IFDv2 chipsets, sometimes unofficially referred to as |
| 402 | * IFDv1.5 platforms, only have 8 regions. To not corrupt the IFD when |
| 403 | * operating on an IFDv1.5 detect how much space is actually present |
| 404 | * in the IFD. |
| 405 | */ |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 406 | static int max_regions_from_fdbar(const struct fdbar *fdb) |
Patrick Rudolph | 98ecaa4 | 2022-10-24 15:06:15 +0200 | [diff] [blame] | 407 | { |
| 408 | const size_t fcba = (fdb->flmap0 & 0xff) << 4; |
| 409 | const size_t fmba = (fdb->flmap1 & 0xff) << 4; |
| 410 | const size_t frba = ((fdb->flmap0 >> 16) & 0xff) << 4; |
| 411 | const size_t fpsba = ((fdb->flmap1 >> 16) & 0xff) << 4; |
| 412 | const size_t flumap = 4096 - 256 - 4; |
| 413 | size_t sorted[5] = {fcba, fmba, frba, fpsba, flumap}; |
| 414 | |
| 415 | qsort(sorted, ARRAY_SIZE(sorted), sizeof(size_t), sort_compare); |
| 416 | |
| 417 | for (size_t i = 0; i < 4; i++) { |
| 418 | /* |
| 419 | * Find FRBA in the sorted array and determine the size of the |
| 420 | * region by the start of the next region. Every region requires |
| 421 | * 4 bytes of space. |
| 422 | */ |
| 423 | if (sorted[i] == frba) |
| 424 | return MIN((sorted[i+1] - sorted[i])/4, MAX_REGIONS); |
| 425 | } |
| 426 | /* Never reaches this point */ |
| 427 | return 0; |
| 428 | } |
| 429 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 430 | static void dump_frba(const struct frba *frba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 431 | { |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 432 | unsigned int i; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 433 | struct region region; |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 434 | printf("Found Region Section\n"); |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 435 | for (i = 0; i < max_regions; i++) { |
Subrata Banik | d52df3cd | 2020-08-26 14:02:01 +0530 | [diff] [blame] | 436 | region = get_region(frba, i); |
| 437 | /* Skip unused & reserved Flash Region */ |
| 438 | if (region.size < 1 && !strcmp(region_name(i), "Reserved")) |
| 439 | continue; |
| 440 | |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 441 | printf("FLREG%u: 0x%08x\n", i, frba->flreg[i]); |
| 442 | dump_region(i, frba); |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 443 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 446 | static void dump_flashrom_layout(char *image, int size, const char *layout_fname) |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 447 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 448 | const struct frba *frba = find_frba(image, size); |
| 449 | if (!frba) |
| 450 | exit(EXIT_FAILURE); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 451 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 452 | int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 453 | if (layout_fd == -1) { |
| 454 | perror("Could not open file"); |
| 455 | exit(EXIT_FAILURE); |
| 456 | } |
| 457 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 458 | for (unsigned int i = 0; i < max_regions; i++) { |
| 459 | struct region region = get_region(frba, i); |
Maximilian Brune | 2c895aa | 2023-04-24 19:28:21 +0200 | [diff] [blame] | 460 | |
| 461 | /* A region limit of 0 is an indicator of an unused region |
| 462 | * A region base of 7FFFh is an indicator of a reserved region |
| 463 | */ |
| 464 | if (region.limit == 0 || region.base == 0x07FFF000) |
Alexander Couzens | eff596b | 2016-10-08 00:53:09 +0200 | [diff] [blame] | 465 | continue; |
| 466 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 467 | char buf[LAYOUT_LINELEN]; |
| 468 | snprintf(buf, LAYOUT_LINELEN, "%08x:%08x %s\n", region.base, region.limit, region_names[i].terse); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 469 | if (write(layout_fd, buf, strlen(buf)) < 0) { |
| 470 | perror("Could not write to file"); |
| 471 | exit(EXIT_FAILURE); |
| 472 | } |
| 473 | } |
| 474 | close(layout_fd); |
| 475 | printf("Wrote layout to %s\n", layout_fname); |
| 476 | } |
| 477 | |
Subrata Banik | d16ef4d | 2020-08-26 15:53:00 +0530 | [diff] [blame] | 478 | static void _decode_spi_frequency(unsigned int freq) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 479 | { |
| 480 | switch (freq) { |
| 481 | case SPI_FREQUENCY_20MHZ: |
| 482 | printf("20MHz"); |
| 483 | break; |
| 484 | case SPI_FREQUENCY_33MHZ: |
| 485 | printf("33MHz"); |
| 486 | break; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 487 | case SPI_FREQUENCY_48MHZ: |
| 488 | printf("48MHz"); |
| 489 | break; |
| 490 | case SPI_FREQUENCY_50MHZ_30MHZ: |
| 491 | switch (ifd_version) { |
| 492 | case IFD_VERSION_1: |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 493 | case IFD_VERSION_1_5: |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 494 | printf("50MHz"); |
| 495 | break; |
| 496 | case IFD_VERSION_2: |
| 497 | printf("30MHz"); |
| 498 | break; |
| 499 | } |
| 500 | break; |
| 501 | case SPI_FREQUENCY_17MHZ: |
| 502 | printf("17MHz"); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 503 | break; |
| 504 | default: |
| 505 | printf("unknown<%x>MHz", freq); |
| 506 | } |
| 507 | } |
| 508 | |
Subrata Banik | d16ef4d | 2020-08-26 15:53:00 +0530 | [diff] [blame] | 509 | static void _decode_spi_frequency_500_series(unsigned int freq) |
| 510 | { |
| 511 | switch (freq) { |
| 512 | case SPI_FREQUENCY_100MHZ: |
| 513 | printf("100MHz"); |
| 514 | break; |
| 515 | case SPI_FREQUENCY_50MHZ: |
| 516 | printf("50MHz"); |
| 517 | break; |
| 518 | case SPI_FREQUENCY_500SERIES_33MHZ: |
| 519 | printf("33MHz"); |
| 520 | break; |
| 521 | case SPI_FREQUENCY_25MHZ: |
| 522 | printf("25MHz"); |
| 523 | break; |
| 524 | case SPI_FREQUENCY_14MHZ: |
| 525 | printf("14MHz"); |
| 526 | break; |
| 527 | default: |
| 528 | printf("unknown<%x>MHz", freq); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static void decode_spi_frequency(unsigned int freq) |
| 533 | { |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 534 | if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT) |
Subrata Banik | d16ef4d | 2020-08-26 15:53:00 +0530 | [diff] [blame] | 535 | _decode_spi_frequency_500_series(freq); |
| 536 | else |
| 537 | _decode_spi_frequency(freq); |
| 538 | } |
| 539 | |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 540 | static void _decode_espi_frequency(unsigned int freq) |
| 541 | { |
| 542 | switch (freq) { |
| 543 | case ESPI_FREQUENCY_20MHZ: |
| 544 | printf("20MHz"); |
| 545 | break; |
| 546 | case ESPI_FREQUENCY_24MHZ: |
| 547 | printf("24MHz"); |
| 548 | break; |
| 549 | case ESPI_FREQUENCY_30MHZ: |
| 550 | printf("30MHz"); |
| 551 | break; |
| 552 | case ESPI_FREQUENCY_48MHZ: |
| 553 | printf("48MHz"); |
| 554 | break; |
| 555 | case ESPI_FREQUENCY_60MHZ: |
| 556 | printf("60MHz"); |
| 557 | break; |
| 558 | case ESPI_FREQUENCY_17MHZ: |
| 559 | printf("17MHz"); |
| 560 | break; |
| 561 | default: |
| 562 | printf("unknown<%x>MHz", freq); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static void _decode_espi_frequency_500_series(unsigned int freq) |
| 567 | { |
| 568 | switch (freq) { |
| 569 | case ESPI_FREQUENCY_500SERIES_20MHZ: |
| 570 | printf("20MHz"); |
| 571 | break; |
| 572 | case ESPI_FREQUENCY_500SERIES_24MHZ: |
| 573 | printf("24MHz"); |
| 574 | break; |
| 575 | case ESPI_FREQUENCY_500SERIES_25MHZ: |
| 576 | printf("25MHz"); |
| 577 | break; |
| 578 | case ESPI_FREQUENCY_500SERIES_48MHZ: |
| 579 | printf("48MHz"); |
| 580 | break; |
| 581 | case ESPI_FREQUENCY_500SERIES_60MHZ: |
| 582 | printf("60MHz"); |
| 583 | break; |
| 584 | default: |
| 585 | printf("unknown<%x>MHz", freq); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static void decode_espi_frequency(unsigned int freq) |
| 590 | { |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 591 | if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT) |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 592 | _decode_espi_frequency_500_series(freq); |
| 593 | else |
| 594 | _decode_espi_frequency(freq); |
| 595 | } |
| 596 | |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 597 | static void decode_component_density(unsigned int density) |
| 598 | { |
| 599 | switch (density) { |
| 600 | case COMPONENT_DENSITY_512KB: |
| 601 | printf("512KB"); |
| 602 | break; |
| 603 | case COMPONENT_DENSITY_1MB: |
| 604 | printf("1MB"); |
| 605 | break; |
| 606 | case COMPONENT_DENSITY_2MB: |
| 607 | printf("2MB"); |
| 608 | break; |
| 609 | case COMPONENT_DENSITY_4MB: |
| 610 | printf("4MB"); |
| 611 | break; |
| 612 | case COMPONENT_DENSITY_8MB: |
| 613 | printf("8MB"); |
| 614 | break; |
| 615 | case COMPONENT_DENSITY_16MB: |
| 616 | printf("16MB"); |
| 617 | break; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 618 | case COMPONENT_DENSITY_32MB: |
| 619 | printf("32MB"); |
| 620 | break; |
| 621 | case COMPONENT_DENSITY_64MB: |
| 622 | printf("64MB"); |
| 623 | break; |
| 624 | case COMPONENT_DENSITY_UNUSED: |
| 625 | printf("UNUSED"); |
| 626 | break; |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 627 | default: |
| 628 | printf("unknown<%x>MB", density); |
| 629 | } |
| 630 | } |
| 631 | |
Subrata Banik | 26058dc | 2020-08-26 15:12:16 +0530 | [diff] [blame] | 632 | static int is_platform_with_pch(void) |
| 633 | { |
| 634 | if (chipset >= CHIPSET_5_SERIES_IBEX_PEAK) |
| 635 | return 1; |
| 636 | |
| 637 | return 0; |
| 638 | } |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 639 | |
| 640 | /* FLMAP0 register bit 24 onwards are reserved from SPT PCH */ |
| 641 | static int is_platform_with_100x_series_pch(void) |
| 642 | { |
| 643 | if (chipset >= CHIPSET_100_200_SERIES_SUNRISE_POINT && |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 644 | chipset <= CHIPSET_500_600_SERIES_TIGER_ALDER_POINT) |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 645 | return 1; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 650 | static void dump_fcba(const struct fcba *fcba, const struct fpsba *fpsba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 651 | { |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 652 | unsigned int freq; |
| 653 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 654 | printf("\nFound Component Section\n"); |
| 655 | printf("FLCOMP 0x%08x\n", fcba->flcomp); |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 656 | printf(" Dual Output Fast Read Support: %ssupported\n", |
| 657 | (fcba->flcomp & (1 << 30))?"":"not "); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 658 | printf(" Read ID/Read Status Clock Frequency: "); |
| 659 | decode_spi_frequency((fcba->flcomp >> 27) & 7); |
| 660 | printf("\n Write/Erase Clock Frequency: "); |
| 661 | decode_spi_frequency((fcba->flcomp >> 24) & 7); |
| 662 | printf("\n Fast Read Clock Frequency: "); |
| 663 | decode_spi_frequency((fcba->flcomp >> 21) & 7); |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 664 | printf("\n Fast Read Support: %ssupported", |
| 665 | (fcba->flcomp & (1 << 20))?"":"not "); |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 666 | if (is_platform_with_100x_series_pch() && |
| 667 | chipset != CHIPSET_100_200_SERIES_SUNRISE_POINT) { |
| 668 | printf("\n Read eSPI/EC Bus Frequency: "); |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 669 | if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT) |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 670 | freq = (fpsba->pchstrp[22] & 0x38) >> 3; |
| 671 | else |
| 672 | freq = (fcba->flcomp >> 17) & 7; |
| 673 | decode_espi_frequency(freq); |
| 674 | } else { |
| 675 | printf("\n Read Clock Frequency: "); |
| 676 | decode_spi_frequency((fcba->flcomp >> 17) & 7); |
| 677 | } |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 678 | |
| 679 | switch (ifd_version) { |
| 680 | case IFD_VERSION_1: |
| 681 | printf("\n Component 2 Density: "); |
| 682 | decode_component_density((fcba->flcomp >> 3) & 7); |
| 683 | printf("\n Component 1 Density: "); |
| 684 | decode_component_density(fcba->flcomp & 7); |
| 685 | break; |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 686 | case IFD_VERSION_1_5: |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 687 | case IFD_VERSION_2: |
| 688 | printf("\n Component 2 Density: "); |
| 689 | decode_component_density((fcba->flcomp >> 4) & 0xf); |
| 690 | printf("\n Component 1 Density: "); |
| 691 | decode_component_density(fcba->flcomp & 0xf); |
| 692 | break; |
| 693 | } |
| 694 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 695 | printf("\n"); |
| 696 | printf("FLILL 0x%08x\n", fcba->flill); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 697 | printf(" Invalid Instruction 3: 0x%02x\n", |
| 698 | (fcba->flill >> 24) & 0xff); |
| 699 | printf(" Invalid Instruction 2: 0x%02x\n", |
| 700 | (fcba->flill >> 16) & 0xff); |
| 701 | printf(" Invalid Instruction 1: 0x%02x\n", |
| 702 | (fcba->flill >> 8) & 0xff); |
| 703 | printf(" Invalid Instruction 0: 0x%02x\n", |
| 704 | fcba->flill & 0xff); |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 705 | if (is_platform_with_100x_series_pch()) { |
| 706 | printf("FLILL1 0x%08x\n", fcba->flpb); |
| 707 | printf(" Invalid Instruction 7: 0x%02x\n", |
| 708 | (fcba->flpb >> 24) & 0xff); |
| 709 | printf(" Invalid Instruction 6: 0x%02x\n", |
| 710 | (fcba->flpb >> 16) & 0xff); |
| 711 | printf(" Invalid Instruction 5: 0x%02x\n", |
| 712 | (fcba->flpb >> 8) & 0xff); |
| 713 | printf(" Invalid Instruction 4: 0x%02x\n", |
| 714 | fcba->flpb & 0xff); |
| 715 | } else { |
| 716 | printf("FLPB 0x%08x\n", fcba->flpb); |
| 717 | printf(" Flash Partition Boundary Address: 0x%06x\n\n", |
| 718 | (fcba->flpb & 0xfff) << 12); |
| 719 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 722 | static void dump_fpsba(const struct fdbar *fdb, const struct fpsba *fpsba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 723 | { |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 724 | unsigned int i; |
Arthur Heymans | 0424a2c | 2021-06-22 11:14:24 +0200 | [diff] [blame] | 725 | /* SoC Straps, aka PSL, aka ISL */ |
| 726 | unsigned int SS = (fdb->flmap1 >> 24) & 0xff; |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 727 | |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 728 | printf("Found PCH Strap Section\n"); |
Arthur Heymans | 0424a2c | 2021-06-22 11:14:24 +0200 | [diff] [blame] | 729 | for (i = 0; i < SS; i++) |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 730 | printf("PCHSTRP%-3u: 0x%08x\n", i, fpsba->pchstrp[i]); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 731 | |
| 732 | if (ifd_version >= IFD_VERSION_2) { |
| 733 | printf("HAP bit is %sset\n", |
| 734 | fpsba->pchstrp[0] & (1 << 16) ? "" : "not "); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 735 | } else if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) { |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 736 | printf("ICH_MeDisable bit is %sset\n", |
| 737 | fpsba->pchstrp[0] & 1 ? "" : "not "); |
| 738 | } else { |
| 739 | printf("AltMeDisable bit is %sset\n", |
| 740 | fpsba->pchstrp[10] & (1 << 7) ? "" : "not "); |
| 741 | } |
| 742 | |
Bill XIE | 4651d45 | 2017-09-12 11:54:48 +0800 | [diff] [blame] | 743 | printf("\n"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static void decode_flmstr(uint32_t flmstr) |
| 747 | { |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 748 | int wr_shift, rd_shift; |
| 749 | if (ifd_version >= IFD_VERSION_2) { |
| 750 | wr_shift = FLMSTR_WR_SHIFT_V2; |
| 751 | rd_shift = FLMSTR_RD_SHIFT_V2; |
| 752 | } else { |
| 753 | wr_shift = FLMSTR_WR_SHIFT_V1; |
| 754 | rd_shift = FLMSTR_RD_SHIFT_V1; |
| 755 | } |
| 756 | |
| 757 | /* EC region access only available on v2+ */ |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 758 | if (PLATFORM_HAS_EC_REGION) |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 759 | printf(" EC Region Write Access: %s\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 760 | (flmstr & (1 << (wr_shift + 8))) ? |
| 761 | "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 762 | printf(" Platform Data Region Write Access: %s\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 763 | (flmstr & (1 << (wr_shift + 4))) ? "enabled" : "disabled"); |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 764 | if (PLATFORM_HAS_GBE_REGION) { |
| 765 | printf(" GbE Region Write Access: %s\n", |
| 766 | (flmstr & (1 << (wr_shift + 3))) ? "enabled" : "disabled"); |
| 767 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 768 | printf(" Intel ME Region Write Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 769 | (flmstr & (1 << (wr_shift + 2))) ? "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 770 | printf(" Host CPU/BIOS Region Write Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 771 | (flmstr & (1 << (wr_shift + 1))) ? "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 772 | printf(" Flash Descriptor Write Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 773 | (flmstr & (1 << wr_shift)) ? "enabled" : "disabled"); |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 774 | if (PLATFORM_HAS_10GBE_0_REGION) { |
| 775 | printf(" 10GbE_0 Write Access: %s\n", |
| 776 | (flmstr & (1 << (wr_shift + 11))) ? "enabled" : "disabled"); |
| 777 | } |
| 778 | if (PLATFORM_HAS_10GBE_1_REGION) { |
| 779 | printf(" 10GbE_1 Write Access: %s\n", |
| 780 | (flmstr & (1 << 4)) ? "enabled" : "disabled"); |
| 781 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 782 | |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 783 | if (PLATFORM_HAS_EC_REGION) |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 784 | printf(" EC Region Read Access: %s\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 785 | (flmstr & (1 << (rd_shift + 8))) ? |
| 786 | "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 787 | printf(" Platform Data Region Read Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 788 | (flmstr & (1 << (rd_shift + 4))) ? "enabled" : "disabled"); |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 789 | if (PLATFORM_HAS_GBE_REGION) { |
| 790 | printf(" GbE Region Read Access: %s\n", |
| 791 | (flmstr & (1 << (rd_shift + 3))) ? "enabled" : "disabled"); |
| 792 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 793 | printf(" Intel ME Region Read Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 794 | (flmstr & (1 << (rd_shift + 2))) ? "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 795 | printf(" Host CPU/BIOS Region Read Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 796 | (flmstr & (1 << (rd_shift + 1))) ? "enabled" : "disabled"); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 797 | printf(" Flash Descriptor Read Access: %s\n", |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 798 | (flmstr & (1 << rd_shift)) ? "enabled" : "disabled"); |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 799 | if (PLATFORM_HAS_10GBE_0_REGION) { |
| 800 | printf(" 10GbE_0 Read Access: %s\n", |
| 801 | (flmstr & (1 << (rd_shift + 11))) ? "enabled" : "disabled"); |
| 802 | } |
| 803 | if (PLATFORM_HAS_10GBE_1_REGION) { |
| 804 | printf(" 10GbE_1 Read Access: %s\n", |
| 805 | (flmstr & (1 << 0)) ? "enabled" : "disabled"); |
| 806 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 807 | |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 808 | /* Requestor ID doesn't exist for ifd 2 */ |
| 809 | if (ifd_version < IFD_VERSION_2) |
| 810 | printf(" Requester ID: 0x%04x\n\n", |
| 811 | flmstr & 0xffff); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 814 | static void dump_fmba(const struct fmba *fmba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 815 | { |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 816 | printf("Found Master Section\n"); |
| 817 | printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1); |
| 818 | decode_flmstr(fmba->flmstr1); |
| 819 | printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2); |
| 820 | decode_flmstr(fmba->flmstr2); |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 821 | if (PLATFORM_HAS_GBE_REGION) { |
| 822 | printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3); |
| 823 | decode_flmstr(fmba->flmstr3); |
| 824 | if (ifd_version >= IFD_VERSION_2) { |
| 825 | printf("FLMSTR5: 0x%08x (EC)\n", fmba->flmstr5); |
| 826 | decode_flmstr(fmba->flmstr5); |
| 827 | } |
| 828 | } else { |
| 829 | printf("FLMSTR6: 0x%08x (IE)\n", fmba->flmstr6); |
| 830 | decode_flmstr(fmba->flmstr6); |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 831 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 834 | static void dump_fmsba(const struct fmsba *fmsba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 835 | { |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 836 | unsigned int i; |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 837 | printf("Found Processor Strap Section\n"); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 838 | for (i = 0; i < ARRAY_SIZE(fmsba->data); i++) |
| 839 | printf("????: 0x%08x\n", fmsba->data[i]); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 840 | |
| 841 | if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) { |
| 842 | printf("MCH_MeDisable bit is %sset\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 843 | fmsba->data[0] & 1 ? "" : "not "); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 844 | printf("MCH_AltMeDisable bit is %sset\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 845 | fmsba->data[0] & (1 << 7) ? "" : "not "); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 846 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 849 | static void dump_jid(uint32_t jid) |
| 850 | { |
Elyes HAOUAS | 99e54fe | 2020-01-30 15:08:03 +0100 | [diff] [blame] | 851 | printf(" SPI Component Vendor ID: 0x%02x\n", |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 852 | jid & 0xff); |
Elyes HAOUAS | 99e54fe | 2020-01-30 15:08:03 +0100 | [diff] [blame] | 853 | printf(" SPI Component Device ID 0: 0x%02x\n", |
Stefan Tauner | 27bb066 | 2018-08-06 00:40:15 +0200 | [diff] [blame] | 854 | (jid >> 8) & 0xff); |
Elyes HAOUAS | 99e54fe | 2020-01-30 15:08:03 +0100 | [diff] [blame] | 855 | printf(" SPI Component Device ID 1: 0x%02x\n", |
Stefan Tauner | 27bb066 | 2018-08-06 00:40:15 +0200 | [diff] [blame] | 856 | (jid >> 16) & 0xff); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static void dump_vscc(uint32_t vscc) |
| 860 | { |
| 861 | printf(" Lower Erase Opcode: 0x%02x\n", |
| 862 | vscc >> 24); |
| 863 | printf(" Lower Write Enable on Write Status: 0x%02x\n", |
| 864 | vscc & (1 << 20) ? 0x06 : 0x50); |
| 865 | printf(" Lower Write Status Required: %s\n", |
| 866 | vscc & (1 << 19) ? "Yes" : "No"); |
| 867 | printf(" Lower Write Granularity: %d bytes\n", |
| 868 | vscc & (1 << 18) ? 64 : 1); |
| 869 | printf(" Lower Block / Sector Erase Size: "); |
| 870 | switch ((vscc >> 16) & 0x3) { |
| 871 | case 0: |
| 872 | printf("256 Byte\n"); |
| 873 | break; |
| 874 | case 1: |
| 875 | printf("4KB\n"); |
| 876 | break; |
| 877 | case 2: |
| 878 | printf("8KB\n"); |
| 879 | break; |
| 880 | case 3: |
| 881 | printf("64KB\n"); |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | printf(" Upper Erase Opcode: 0x%02x\n", |
| 886 | (vscc >> 8) & 0xff); |
| 887 | printf(" Upper Write Enable on Write Status: 0x%02x\n", |
| 888 | vscc & (1 << 4) ? 0x06 : 0x50); |
| 889 | printf(" Upper Write Status Required: %s\n", |
| 890 | vscc & (1 << 3) ? "Yes" : "No"); |
| 891 | printf(" Upper Write Granularity: %d bytes\n", |
| 892 | vscc & (1 << 2) ? 64 : 1); |
| 893 | printf(" Upper Block / Sector Erase Size: "); |
| 894 | switch (vscc & 0x3) { |
| 895 | case 0: |
| 896 | printf("256 Byte\n"); |
| 897 | break; |
| 898 | case 1: |
| 899 | printf("4KB\n"); |
| 900 | break; |
| 901 | case 2: |
| 902 | printf("8KB\n"); |
| 903 | break; |
| 904 | case 3: |
| 905 | printf("64KB\n"); |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 910 | static void dump_vtba(const struct vtba *vtba, int vtl) |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 911 | { |
| 912 | int i; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 913 | int max_len = sizeof(struct vtba)/sizeof(struct vscc); |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 914 | int num = (vtl >> 1) < max_len ? (vtl >> 1) : max_len; |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 915 | |
| 916 | printf("ME VSCC table:\n"); |
| 917 | for (i = 0; i < num; i++) { |
| 918 | printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid); |
| 919 | dump_jid(vtba->entry[i].jid); |
| 920 | printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc); |
| 921 | dump_vscc(vtba->entry[i].vscc); |
| 922 | } |
| 923 | printf("\n"); |
| 924 | } |
| 925 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 926 | static void dump_oem(const uint8_t *oem) |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 927 | { |
| 928 | int i, j; |
| 929 | printf("OEM Section:\n"); |
| 930 | for (i = 0; i < 4; i++) { |
| 931 | printf("%02x:", i << 4); |
| 932 | for (j = 0; j < 16; j++) |
| 933 | printf(" %02x", oem[(i<<4)+j]); |
| 934 | printf ("\n"); |
| 935 | } |
| 936 | printf ("\n"); |
| 937 | } |
| 938 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 939 | static void dump_fd(char *image, int size) |
| 940 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 941 | const struct fdbar *fdb = find_fd(image, size); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 942 | if (!fdb) |
| 943 | exit(EXIT_FAILURE); |
| 944 | |
Subrata Banik | 26058dc | 2020-08-26 15:12:16 +0530 | [diff] [blame] | 945 | printf("%s", is_platform_with_pch() ? "PCH" : "ICH"); |
| 946 | printf(" Revision: %s\n", ich_chipset_names[chipset]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 947 | printf("FLMAP0: 0x%08x\n", fdb->flmap0); |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 948 | if (!is_platform_with_100x_series_pch()) |
| 949 | printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 950 | printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4); |
| 951 | printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1); |
| 952 | printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4); |
| 953 | |
| 954 | printf("FLMAP1: 0x%08x\n", fdb->flmap1); |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 955 | printf(" %s: ", is_platform_with_100x_series_pch() ? "PSL" : "ISL"); |
| 956 | printf("0x%02x\n", (fdb->flmap1 >> 24) & 0xff); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 957 | printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4); |
| 958 | printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3); |
| 959 | printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4); |
| 960 | |
Subrata Banik | ac1b1dd | 2020-08-26 15:29:58 +0530 | [diff] [blame] | 961 | if (!is_platform_with_100x_series_pch()) { |
| 962 | printf("FLMAP2: 0x%08x\n", fdb->flmap2); |
| 963 | printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff); |
| 964 | printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4); |
| 965 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 966 | |
Subrata Banik | a5f4781 | 2020-09-29 11:43:01 +0530 | [diff] [blame] | 967 | if (chipset == CHIPSET_500_600_SERIES_TIGER_ALDER_POINT) { |
Subrata Banik | bd2da5a | 2020-08-26 15:43:51 +0530 | [diff] [blame] | 968 | printf("FLMAP3: 0x%08x\n", fdb->flmap3); |
| 969 | printf(" Minor Revision ID: 0x%04x\n", (fdb->flmap3 >> 14) & 0x7f); |
| 970 | printf(" Major Revision ID: 0x%04x\n", (fdb->flmap3 >> 21) & 0x7ff); |
| 971 | } |
| 972 | |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 973 | char *flumap = find_flumap(image, size); |
| 974 | uint32_t flumap1 = *(uint32_t *)flumap; |
| 975 | printf("FLUMAP1: 0x%08x\n", flumap1); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 976 | printf(" Intel ME VSCC Table Length (VTL): %d\n", |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 977 | (flumap1 >> 8) & 0xff); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 978 | printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n", |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 979 | (flumap1 & 0xff) << 4); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 980 | dump_vtba((struct vtba *) |
Stefan Tauner | 0d22614 | 2018-08-05 18:56:53 +0200 | [diff] [blame] | 981 | (image + ((flumap1 & 0xff) << 4)), |
| 982 | (flumap1 >> 8) & 0xff); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 983 | dump_oem((const uint8_t *)image + 0xf00); |
| 984 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 985 | const struct frba *frba = find_frba(image, size); |
| 986 | const struct fcba *fcba = find_fcba(image, size); |
| 987 | const struct fpsba *fpsba = find_fpsba(image, size); |
| 988 | const struct fmba *fmba = find_fmba(image, size); |
| 989 | const struct fmsba *fmsba = find_fmsba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 990 | |
| 991 | if (frba && fcba && fpsba && fmba && fmsba) { |
| 992 | dump_frba(frba); |
Subrata Banik | e5d3992 | 2020-08-26 16:01:42 +0530 | [diff] [blame] | 993 | dump_fcba(fcba, fpsba); |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 994 | dump_fpsba(fdb, fpsba); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 995 | dump_fmba(fmba); |
| 996 | dump_fmsba(fmsba); |
| 997 | } else { |
| 998 | printf("FD is corrupted!\n"); |
| 999 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1002 | /* Takes an image containing an IFD and creates a Flashmap .fmd file template. |
| 1003 | * This flashmap will contain all IFD regions except the BIOS region. |
| 1004 | * The BIOS region is created by coreboot itself and 'should' match the IFD region |
| 1005 | * anyway (CONFIG_VALIDATE_INTEL_DESCRIPTOR should make sure). coreboot built system will use |
| 1006 | * this template to generate the final Flashmap file. |
| 1007 | */ |
| 1008 | static void create_fmap_template(char *image, int size, const char *layout_fname) |
| 1009 | { |
| 1010 | const struct frba *frba = find_frba(image, size); |
| 1011 | if (!frba) |
| 1012 | exit(EXIT_FAILURE); |
| 1013 | |
| 1014 | int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1015 | if (layout_fd == -1) { |
| 1016 | perror("Could not open file"); |
| 1017 | exit(EXIT_FAILURE); |
| 1018 | } |
| 1019 | |
| 1020 | char *bbuf = "FLASH@##ROM_BASE## ##ROM_SIZE## {\n"; |
| 1021 | if (write(layout_fd, bbuf, strlen(bbuf)) < 0) { |
| 1022 | perror("Could not write to file"); |
| 1023 | exit(EXIT_FAILURE); |
| 1024 | } |
| 1025 | |
| 1026 | /* fmaptool requires regions in .fmd to be sorted. |
| 1027 | * => We need to sort the regions by base address before writing them in .fmd File |
| 1028 | */ |
| 1029 | int count_regions = 0; |
| 1030 | struct region sorted_regions[MAX_REGIONS] = { 0 }; |
| 1031 | for (unsigned int i = 0; i < max_regions; i++) { |
| 1032 | struct region region = get_region(frba, i); |
Maximilian Brune | 2c895aa | 2023-04-24 19:28:21 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* A region limit of 0 is an indicator of an unused region |
| 1035 | * A region base of 7FFFh is an indicator of a reserved region |
| 1036 | */ |
| 1037 | if (region.limit == 0 || region.base == 0x07FFF000) |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1038 | continue; |
| 1039 | |
| 1040 | /* Here we decide to use the coreboot generated FMAP BIOS region, instead of |
| 1041 | * the one specified in the IFD. The case when IFD and FMAP BIOS region do not |
| 1042 | * match cannot be caught here, therefore one should still validate IFD and |
| 1043 | * FMAP via CONFIG_VALIDATE_INTEL_DESCRIPTOR |
| 1044 | */ |
| 1045 | if (i == REGION_BIOS) |
| 1046 | continue; |
| 1047 | |
| 1048 | sorted_regions[count_regions] = region; |
| 1049 | // basically insertion sort |
| 1050 | for (int i = count_regions-1; i >= 0 ; i--) { |
| 1051 | if (sorted_regions[i].base > sorted_regions[i+1].base) { |
| 1052 | struct region tmp = sorted_regions[i]; |
| 1053 | sorted_regions[i] = sorted_regions[i+1]; |
| 1054 | sorted_regions[i+1] = tmp; |
| 1055 | } |
| 1056 | } |
| 1057 | count_regions++; |
| 1058 | } |
| 1059 | |
| 1060 | // Now write regions sorted by base address in the fmap file |
| 1061 | for (int i = 0; i < count_regions; i++) { |
| 1062 | struct region region = sorted_regions[i]; |
| 1063 | char buf[LAYOUT_LINELEN]; |
| 1064 | snprintf(buf, LAYOUT_LINELEN, "\t%s@0x%X 0x%X\n", region_names[region.type].fmapname, region.base, region.size); |
| 1065 | if (write(layout_fd, buf, strlen(buf)) < 0) { |
| 1066 | perror("Could not write to file"); |
| 1067 | exit(EXIT_FAILURE); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | char *ebuf = "\tSI_BIOS@##BIOS_BASE## ##BIOS_SIZE## {\n" |
| 1072 | "\t\t##CONSOLE_ENTRY##\n" |
| 1073 | "\t\t##MRC_CACHE_ENTRY##\n" |
| 1074 | "\t\t##SMMSTORE_ENTRY##\n" |
| 1075 | "\t\t##SPD_CACHE_ENTRY##\n" |
| 1076 | "\t\t##VPD_ENTRY##\n" |
| 1077 | "\t\tFMAP@##FMAP_BASE## ##FMAP_SIZE##\n" |
| 1078 | "\t\tCOREBOOT(CBFS)@##CBFS_BASE## ##CBFS_SIZE##\n" |
| 1079 | "\t}\n" |
| 1080 | "}\n"; |
| 1081 | if (write(layout_fd, ebuf, strlen(ebuf)) < 0) { |
| 1082 | perror("Could not write to file"); |
| 1083 | exit(EXIT_FAILURE); |
| 1084 | } |
| 1085 | |
| 1086 | close(layout_fd); |
| 1087 | printf("Wrote layout to %s\n", layout_fname); |
| 1088 | } |
| 1089 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1090 | static void write_regions(char *image, int size) |
| 1091 | { |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1092 | unsigned int i; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1093 | const struct frba *frba = find_frba(image, size); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1094 | |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1095 | if (!frba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1096 | exit(EXIT_FAILURE); |
| 1097 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1098 | for (i = 0; i < max_regions; i++) { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1099 | struct region region = get_region(frba, i); |
Stefan Reinauer | 4a17d29 | 2012-09-27 12:42:15 -0700 | [diff] [blame] | 1100 | dump_region(i, frba); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1101 | if (region.size > 0) { |
| 1102 | int region_fd; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1103 | region_fd = open(region_names[i].filename, |
Scott Duplichan | f2c9837 | 2014-12-12 21:03:06 -0600 | [diff] [blame] | 1104 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1105 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
Patrick Georgi | 38fa6ed | 2014-08-03 12:18:45 +0200 | [diff] [blame] | 1106 | if (region_fd < 0) { |
| 1107 | perror("Error while trying to open file"); |
| 1108 | exit(EXIT_FAILURE); |
| 1109 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1110 | if (write(region_fd, image + region.base, region.size) != region.size) |
Stefan Reinauer | a9f670a | 2012-06-18 16:02:20 -0700 | [diff] [blame] | 1111 | perror("Error while writing"); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1112 | close(region_fd); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1117 | static void validate_layout(char *image, int size) |
| 1118 | { |
| 1119 | uint i, errors = 0; |
| 1120 | struct fmap *fmap; |
| 1121 | long int fmap_loc = fmap_find((uint8_t *)image, size); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1122 | const struct frba *frba = find_frba(image, size); |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1123 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1124 | if (fmap_loc < 0 || !frba) { |
| 1125 | printf("Could not find FMAP (%p) or Intel Flash Descriptor (%p)\n", |
| 1126 | (void *)fmap_loc, frba); |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1127 | exit(EXIT_FAILURE); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1128 | } |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1129 | |
| 1130 | fmap = (struct fmap *)(image + fmap_loc); |
| 1131 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1132 | int matches = 0; |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1133 | for (i = 0; i < max_regions; i++) { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1134 | struct region region = get_region(frba, i); |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1135 | if (region.size == 0) |
| 1136 | continue; |
| 1137 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1138 | const struct fmap_area *area = fmap_find_area(fmap, region_names[i].fmapname); |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1139 | if (!area) |
| 1140 | continue; |
| 1141 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1142 | matches++; // found a match between FMAP and IFD region |
| 1143 | |
| 1144 | if ((uint)region.base != area->offset || (uint)region.size != area->size) { |
| 1145 | printf("Region mismatch between %s and %s\n", region_names[i].terse, area->name); |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1146 | printf(" Descriptor region %s:\n", region_names[i].terse); |
| 1147 | printf(" offset: 0x%08x\n", region.base); |
| 1148 | printf(" length: 0x%08x\n", region.size); |
| 1149 | printf(" FMAP area %s:\n", area->name); |
| 1150 | printf(" offset: 0x%08x\n", area->offset); |
| 1151 | printf(" length: 0x%08x\n", area->size); |
| 1152 | errors++; |
| 1153 | } |
| 1154 | } |
| 1155 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1156 | if (!matches) { |
| 1157 | // At least a BIOS region should be present in both IFD and FMAP |
| 1158 | fprintf(stderr, "Warning: Not a single IFD region found in FMAP\n"); |
| 1159 | } |
| 1160 | |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1161 | if (errors > 0) |
| 1162 | exit(EXIT_FAILURE); |
| 1163 | } |
| 1164 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1165 | static void write_image(const char *filename, char *image, int size) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1166 | { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1167 | int new_fd; |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1168 | printf("Writing new image to %s\n", filename); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1169 | |
| 1170 | // Now write out new image |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1171 | new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
Patrick Georgi | 38fa6ed | 2014-08-03 12:18:45 +0200 | [diff] [blame] | 1172 | if (new_fd < 0) { |
| 1173 | perror("Error while trying to open file"); |
| 1174 | exit(EXIT_FAILURE); |
| 1175 | } |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1176 | if (write(new_fd, image, size) != size) |
Stefan Reinauer | a9f670a | 2012-06-18 16:02:20 -0700 | [diff] [blame] | 1177 | perror("Error while writing"); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1178 | close(new_fd); |
| 1179 | } |
| 1180 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1181 | static void set_spi_frequency(const char *filename, char *image, int size, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1182 | enum spi_frequency freq) |
| 1183 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1184 | struct fcba *fcba = find_fcba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1185 | if (!fcba) |
| 1186 | exit(EXIT_FAILURE); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* clear bits 21-29 */ |
| 1189 | fcba->flcomp &= ~0x3fe00000; |
| 1190 | /* Read ID and Read Status Clock Frequency */ |
| 1191 | fcba->flcomp |= freq << 27; |
| 1192 | /* Write and Erase Clock Frequency */ |
| 1193 | fcba->flcomp |= freq << 24; |
| 1194 | /* Fast Read Clock Frequency */ |
| 1195 | fcba->flcomp |= freq << 21; |
| 1196 | |
| 1197 | write_image(filename, image, size); |
| 1198 | } |
| 1199 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1200 | static void set_em100_mode(const char *filename, char *image, int size) |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 1201 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1202 | struct fcba *fcba = find_fcba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1203 | if (!fcba) |
| 1204 | exit(EXIT_FAILURE); |
| 1205 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1206 | int freq; |
| 1207 | |
| 1208 | switch (ifd_version) { |
| 1209 | case IFD_VERSION_1: |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 1210 | case IFD_VERSION_1_5: |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1211 | freq = SPI_FREQUENCY_20MHZ; |
| 1212 | break; |
| 1213 | case IFD_VERSION_2: |
| 1214 | freq = SPI_FREQUENCY_17MHZ; |
| 1215 | break; |
Alexandru Gagniuc | 71a7ba2 | 2015-09-10 08:37:42 -0700 | [diff] [blame] | 1216 | default: |
| 1217 | freq = SPI_FREQUENCY_17MHZ; |
| 1218 | break; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1219 | } |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 1220 | |
| 1221 | fcba->flcomp &= ~(1 << 30); |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1222 | set_spi_frequency(filename, image, size, freq); |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1225 | static void set_chipdensity(const char *filename, char *image, int size, |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1226 | unsigned int density) |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1227 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1228 | struct fcba *fcba = find_fcba(image, size); |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1229 | uint8_t mask, chip2_offset; |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1230 | if (!fcba) |
| 1231 | exit(EXIT_FAILURE); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1232 | |
| 1233 | printf("Setting chip density to "); |
| 1234 | decode_component_density(density); |
| 1235 | printf("\n"); |
| 1236 | |
| 1237 | switch (ifd_version) { |
| 1238 | case IFD_VERSION_1: |
| 1239 | /* fail if selected density is not supported by this version */ |
| 1240 | if ( (density == COMPONENT_DENSITY_32MB) || |
| 1241 | (density == COMPONENT_DENSITY_64MB) || |
| 1242 | (density == COMPONENT_DENSITY_UNUSED) ) { |
| 1243 | printf("error: Selected density not supported in IFD version 1.\n"); |
| 1244 | exit(EXIT_FAILURE); |
| 1245 | } |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1246 | mask = 0x7; |
| 1247 | chip2_offset = 3; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1248 | break; |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 1249 | case IFD_VERSION_1_5: |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1250 | case IFD_VERSION_2: |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1251 | mask = 0xf; |
| 1252 | chip2_offset = 4; |
| 1253 | break; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1254 | default: |
| 1255 | printf("error: Unknown IFD version\n"); |
| 1256 | exit(EXIT_FAILURE); |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | /* clear chip density for corresponding chip */ |
| 1261 | switch (selected_chip) { |
| 1262 | case 1: |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1263 | fcba->flcomp &= ~mask; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1264 | break; |
| 1265 | case 2: |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1266 | fcba->flcomp &= ~(mask << chip2_offset); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1267 | break; |
| 1268 | default: /*both chips*/ |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1269 | fcba->flcomp &= ~(mask | (mask << chip2_offset)); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1270 | break; |
| 1271 | } |
| 1272 | |
| 1273 | /* set the new density */ |
| 1274 | if (selected_chip == 1 || selected_chip == 0) |
| 1275 | fcba->flcomp |= (density); /* first chip */ |
| 1276 | if (selected_chip == 2 || selected_chip == 0) |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1277 | fcba->flcomp |= (density << chip2_offset); /* second chip */ |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1278 | |
| 1279 | write_image(filename, image, size); |
| 1280 | } |
| 1281 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1282 | static int check_region(const struct frba *frba, unsigned int region_type) |
Duncan Laurie | 7775d67 | 2019-06-06 13:39:26 -0700 | [diff] [blame] | 1283 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1284 | struct region region; |
Duncan Laurie | 7775d67 | 2019-06-06 13:39:26 -0700 | [diff] [blame] | 1285 | |
| 1286 | if (!frba) |
| 1287 | return 0; |
| 1288 | |
| 1289 | region = get_region(frba, region_type); |
| 1290 | return !!((region.base < region.limit) && (region.size > 0)); |
| 1291 | } |
| 1292 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1293 | static void lock_descriptor(const char *filename, char *image, int size) |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1294 | { |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1295 | int wr_shift, rd_shift; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1296 | struct fmba *fmba = find_fmba(image, size); |
| 1297 | const struct frba *frba = find_frba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1298 | if (!fmba) |
| 1299 | exit(EXIT_FAILURE); |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1300 | |
| 1301 | if (ifd_version >= IFD_VERSION_2) { |
| 1302 | wr_shift = FLMSTR_WR_SHIFT_V2; |
| 1303 | rd_shift = FLMSTR_RD_SHIFT_V2; |
| 1304 | |
| 1305 | /* Clear non-reserved bits */ |
| 1306 | fmba->flmstr1 &= 0xff; |
| 1307 | fmba->flmstr2 &= 0xff; |
| 1308 | fmba->flmstr3 &= 0xff; |
Lijian Zhao | 9871b04 | 2019-01-26 16:48:38 -0800 | [diff] [blame] | 1309 | fmba->flmstr5 &= 0xff; |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1310 | } else { |
| 1311 | wr_shift = FLMSTR_WR_SHIFT_V1; |
| 1312 | rd_shift = FLMSTR_RD_SHIFT_V1; |
| 1313 | |
| 1314 | fmba->flmstr1 = 0; |
| 1315 | fmba->flmstr2 = 0; |
| 1316 | /* Requestor ID */ |
| 1317 | fmba->flmstr3 = 0x118; |
| 1318 | } |
| 1319 | |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 1320 | switch (platform) { |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 1321 | case PLATFORM_APL: |
| 1322 | case PLATFORM_GLK: |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 1323 | /* CPU/BIOS can read descriptor and BIOS */ |
| 1324 | fmba->flmstr1 |= 0x3 << rd_shift; |
| 1325 | /* CPU/BIOS can write BIOS */ |
| 1326 | fmba->flmstr1 |= 0x2 << wr_shift; |
| 1327 | /* TXE can read descriptor, BIOS and Device Expansion */ |
| 1328 | fmba->flmstr2 |= 0x23 << rd_shift; |
| 1329 | /* TXE can only write Device Expansion */ |
| 1330 | fmba->flmstr2 |= 0x20 << wr_shift; |
| 1331 | break; |
Lijian Zhao | 3e88b18 | 2019-01-25 23:20:16 -0800 | [diff] [blame] | 1332 | case PLATFORM_CNL: |
| 1333 | case PLATFORM_ICL: |
Furquan Shaikh | 088b6e8 | 2018-03-21 10:42:37 -0700 | [diff] [blame] | 1334 | case PLATFORM_SKLKBL: |
Ravi Sarawadi | 7d9d63b | 2019-10-22 13:45:36 -0700 | [diff] [blame] | 1335 | case PLATFORM_TGL: |
Subrata Banik | a717e2f | 2020-07-30 11:22:53 +0530 | [diff] [blame] | 1336 | case PLATFORM_JSL: |
Lean Sheng Tan | 0faba3c | 2021-06-09 07:52:24 -0700 | [diff] [blame] | 1337 | case PLATFORM_EHL: |
Subrata Banik | 46f8073 | 2020-03-14 15:01:42 +0530 | [diff] [blame] | 1338 | case PLATFORM_ADL: |
Wonkyu Kim | 3922aa5 | 2022-02-02 15:19:05 -0800 | [diff] [blame] | 1339 | case PLATFORM_IFD2: |
Subrata Banik | ca82e61 | 2022-01-20 18:51:21 +0530 | [diff] [blame] | 1340 | case PLATFORM_MTL: |
Duncan Laurie | 7775d67 | 2019-06-06 13:39:26 -0700 | [diff] [blame] | 1341 | /* CPU/BIOS can read descriptor and BIOS. */ |
| 1342 | fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift; |
| 1343 | fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift; |
| 1344 | /* CPU/BIOS can write BIOS. */ |
| 1345 | fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift; |
| 1346 | /* ME can read descriptor and ME. */ |
| 1347 | fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift; |
| 1348 | fmba->flmstr2 |= (1 << REGION_ME) << rd_shift; |
Furquan Shaikh | 088b6e8 | 2018-03-21 10:42:37 -0700 | [diff] [blame] | 1349 | /* ME can write ME. */ |
Duncan Laurie | 7775d67 | 2019-06-06 13:39:26 -0700 | [diff] [blame] | 1350 | fmba->flmstr2 |= (1 << REGION_ME) << wr_shift; |
| 1351 | if (check_region(frba, REGION_GBE)) { |
| 1352 | /* BIOS can read/write GbE. */ |
| 1353 | fmba->flmstr1 |= (1 << REGION_GBE) << rd_shift; |
| 1354 | fmba->flmstr1 |= (1 << REGION_GBE) << wr_shift; |
| 1355 | /* ME can read GbE. */ |
| 1356 | fmba->flmstr2 |= (1 << REGION_GBE) << rd_shift; |
| 1357 | /* GbE can read descriptor and read/write GbE.. */ |
| 1358 | fmba->flmstr3 |= (1 << REGION_DESC) << rd_shift; |
| 1359 | fmba->flmstr3 |= (1 << REGION_GBE) << rd_shift; |
| 1360 | fmba->flmstr3 |= (1 << REGION_GBE) << wr_shift; |
| 1361 | } |
| 1362 | if (check_region(frba, REGION_PDR)) { |
| 1363 | /* BIOS can read/write PDR. */ |
| 1364 | fmba->flmstr1 |= (1 << REGION_PDR) << rd_shift; |
| 1365 | fmba->flmstr1 |= (1 << REGION_PDR) << wr_shift; |
| 1366 | } |
| 1367 | if (check_region(frba, REGION_EC)) { |
| 1368 | /* BIOS can read EC. */ |
| 1369 | fmba->flmstr1 |= (1 << REGION_EC) << rd_shift; |
| 1370 | /* EC can read descriptor and read/write EC. */ |
| 1371 | fmba->flmstr5 |= (1 << REGION_DESC) << rd_shift; |
| 1372 | fmba->flmstr5 |= (1 << REGION_EC) << rd_shift; |
| 1373 | fmba->flmstr5 |= (1 << REGION_EC) << wr_shift; |
| 1374 | } |
Furquan Shaikh | 088b6e8 | 2018-03-21 10:42:37 -0700 | [diff] [blame] | 1375 | break; |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 1376 | case PLATFORM_DNV: |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 1377 | case PLATFORM_WBG: |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 1378 | /* CPU/BIOS can read descriptor and BIOS. */ |
| 1379 | fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift; |
| 1380 | fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift; |
| 1381 | /* CPU/BIOS can write BIOS. */ |
| 1382 | fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift; |
| 1383 | /* ME can read descriptor and ME. */ |
| 1384 | fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift; |
| 1385 | fmba->flmstr2 |= (1 << REGION_ME) << rd_shift; |
| 1386 | /* ME can write ME. */ |
| 1387 | fmba->flmstr2 |= (1 << REGION_ME) << wr_shift; |
| 1388 | break; |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 1389 | default: |
Duncan Laurie | 7775d67 | 2019-06-06 13:39:26 -0700 | [diff] [blame] | 1390 | /* CPU/BIOS can read descriptor and BIOS. */ |
| 1391 | fmba->flmstr1 |= (1 << REGION_DESC) << rd_shift; |
| 1392 | fmba->flmstr1 |= (1 << REGION_BIOS) << rd_shift; |
| 1393 | /* CPU/BIOS can write BIOS. */ |
| 1394 | fmba->flmstr1 |= (1 << REGION_BIOS) << wr_shift; |
| 1395 | /* ME can read descriptor and ME. */ |
| 1396 | fmba->flmstr2 |= (1 << REGION_DESC) << rd_shift; |
| 1397 | fmba->flmstr2 |= (1 << REGION_ME) << rd_shift; |
| 1398 | /* ME can write ME. */ |
| 1399 | fmba->flmstr2 |= (1 << REGION_ME) << wr_shift; |
| 1400 | if (check_region(frba, REGION_GBE)) { |
| 1401 | /* BIOS can read GbE. */ |
| 1402 | fmba->flmstr1 |= (1 << REGION_GBE) << rd_shift; |
| 1403 | /* BIOS can write GbE. */ |
| 1404 | fmba->flmstr1 |= (1 << REGION_GBE) << wr_shift; |
| 1405 | /* ME can read GbE. */ |
| 1406 | fmba->flmstr2 |= (1 << REGION_GBE) << rd_shift; |
| 1407 | /* ME can write GbE. */ |
| 1408 | fmba->flmstr2 |= (1 << REGION_GBE) << wr_shift; |
| 1409 | /* GbE can write GbE. */ |
| 1410 | fmba->flmstr3 |= (1 << REGION_GBE) << rd_shift; |
| 1411 | /* GbE can read GbE. */ |
| 1412 | fmba->flmstr3 |= (1 << REGION_GBE) << wr_shift; |
| 1413 | } |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1416 | |
| 1417 | write_image(filename, image, size); |
| 1418 | } |
| 1419 | |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 1420 | static void enable_cpu_read_me(const char *filename, char *image, int size) |
| 1421 | { |
| 1422 | int rd_shift; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1423 | struct fmba *fmba = find_fmba(image, size); |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 1424 | |
| 1425 | if (!fmba) |
| 1426 | exit(EXIT_FAILURE); |
| 1427 | |
| 1428 | if (ifd_version >= IFD_VERSION_2) |
| 1429 | rd_shift = FLMSTR_RD_SHIFT_V2; |
| 1430 | else |
| 1431 | rd_shift = FLMSTR_RD_SHIFT_V1; |
| 1432 | |
| 1433 | /* CPU/BIOS can read ME. */ |
| 1434 | fmba->flmstr1 |= (1 << REGION_ME) << rd_shift; |
| 1435 | |
| 1436 | write_image(filename, image, size); |
| 1437 | } |
| 1438 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1439 | static void unlock_descriptor(const char *filename, char *image, int size) |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1440 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1441 | struct fmba *fmba = find_fmba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1442 | if (!fmba) |
| 1443 | exit(EXIT_FAILURE); |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1444 | |
| 1445 | if (ifd_version >= IFD_VERSION_2) { |
| 1446 | /* Access bits for each region are read: 19:8 write: 31:20 */ |
| 1447 | fmba->flmstr1 = 0xffffff00 | (fmba->flmstr1 & 0xff); |
| 1448 | fmba->flmstr2 = 0xffffff00 | (fmba->flmstr2 & 0xff); |
| 1449 | fmba->flmstr3 = 0xffffff00 | (fmba->flmstr3 & 0xff); |
Lijian Zhao | 9871b04 | 2019-01-26 16:48:38 -0800 | [diff] [blame] | 1450 | fmba->flmstr5 = 0xffffff00 | (fmba->flmstr5 & 0xff); |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1451 | } else { |
| 1452 | fmba->flmstr1 = 0xffff0000; |
| 1453 | fmba->flmstr2 = 0xffff0000; |
Patrick Rudolph | 8a06cc7 | 2017-01-08 09:57:47 +0100 | [diff] [blame] | 1454 | /* Keep chipset specific Requester ID */ |
| 1455 | fmba->flmstr3 = 0x08080000 | (fmba->flmstr3 & 0xffff); |
Shawn Nematbakhsh | d2cb118 | 2015-09-10 19:07:13 -0700 | [diff] [blame] | 1456 | } |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1457 | |
| 1458 | write_image(filename, image, size); |
| 1459 | } |
| 1460 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1461 | static void set_pchstrap(struct fpsba *fpsba, const struct fdbar *fdb, const int strap, |
| 1462 | const unsigned int value) |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1463 | { |
| 1464 | if (!fpsba || !fdb) { |
| 1465 | fprintf(stderr, "Internal error\n"); |
| 1466 | exit(EXIT_FAILURE); |
| 1467 | } |
| 1468 | |
Arthur Heymans | 0424a2c | 2021-06-22 11:14:24 +0200 | [diff] [blame] | 1469 | /* SoC Strap, aka PSL, aka ISL */ |
| 1470 | int SS = (fdb->flmap1 >> 24) & 0xff; |
| 1471 | if (strap >= SS) { |
| 1472 | fprintf(stderr, "Strap index %d out of range (max: %d)\n", strap, SS); |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1473 | exit(EXIT_FAILURE); |
| 1474 | } |
| 1475 | fpsba->pchstrp[strap] = value; |
| 1476 | } |
| 1477 | |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 1478 | /* Set the AltMeDisable (or HAP for >= IFD_VERSION_2) */ |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1479 | static void fpsba_set_altmedisable(struct fpsba *fpsba, struct fmsba *fmsba, bool altmedisable) |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 1480 | { |
| 1481 | if (ifd_version >= IFD_VERSION_2) { |
| 1482 | printf("%sting the HAP bit to %s Intel ME...\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1483 | altmedisable?"Set":"Unset", |
| 1484 | altmedisable?"disable":"enable"); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 1485 | if (altmedisable) |
| 1486 | fpsba->pchstrp[0] |= (1 << 16); |
| 1487 | else |
| 1488 | fpsba->pchstrp[0] &= ~(1 << 16); |
| 1489 | } else { |
| 1490 | if (chipset >= CHIPSET_ICH8 && chipset <= CHIPSET_ICH10) { |
| 1491 | printf("%sting the ICH_MeDisable, MCH_MeDisable, " |
| 1492 | "and MCH_AltMeDisable to %s Intel ME...\n", |
| 1493 | altmedisable?"Set":"Unset", |
| 1494 | altmedisable?"disable":"enable"); |
| 1495 | if (altmedisable) { |
| 1496 | /* MCH_MeDisable */ |
| 1497 | fmsba->data[0] |= 1; |
| 1498 | /* MCH_AltMeDisable */ |
| 1499 | fmsba->data[0] |= (1 << 7); |
| 1500 | /* ICH_MeDisable */ |
| 1501 | fpsba->pchstrp[0] |= 1; |
| 1502 | } else { |
| 1503 | fmsba->data[0] &= ~1; |
| 1504 | fmsba->data[0] &= ~(1 << 7); |
| 1505 | fpsba->pchstrp[0] &= ~1; |
| 1506 | } |
| 1507 | } else { |
| 1508 | printf("%sting the AltMeDisable to %s Intel ME...\n", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1509 | altmedisable?"Set":"Unset", |
| 1510 | altmedisable?"disable":"enable"); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 1511 | if (altmedisable) |
| 1512 | fpsba->pchstrp[10] |= (1 << 7); |
| 1513 | else |
| 1514 | fpsba->pchstrp[10] &= ~(1 << 7); |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
Jacob Garber | 595d926 | 2019-06-27 17:33:10 -0600 | [diff] [blame] | 1519 | static void inject_region(const char *filename, char *image, int size, |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1520 | unsigned int region_type, const char *region_fname) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1521 | { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1522 | struct frba *frba = find_frba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1523 | if (!frba) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1524 | exit(EXIT_FAILURE); |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 1525 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1526 | struct region region = get_region(frba, region_type); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1527 | if (region.size <= 0xfff) { |
| 1528 | fprintf(stderr, "Region %s is disabled in target. Not injecting.\n", |
| 1529 | region_name(region_type)); |
| 1530 | exit(EXIT_FAILURE); |
| 1531 | } |
| 1532 | |
Scott Duplichan | f2c9837 | 2014-12-12 21:03:06 -0600 | [diff] [blame] | 1533 | int region_fd = open(region_fname, O_RDONLY | O_BINARY); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1534 | if (region_fd == -1) { |
| 1535 | perror("Could not open file"); |
| 1536 | exit(EXIT_FAILURE); |
| 1537 | } |
| 1538 | struct stat buf; |
| 1539 | if (fstat(region_fd, &buf) == -1) { |
| 1540 | perror("Could not stat file"); |
| 1541 | exit(EXIT_FAILURE); |
| 1542 | } |
| 1543 | int region_size = buf.st_size; |
| 1544 | |
| 1545 | printf("File %s is %d bytes\n", region_fname, region_size); |
| 1546 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1547 | if (region_size > region.size) { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1548 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)" |
| 1549 | " bytes. Not injecting.\n", |
| 1550 | region_name(region_type), region.size, |
| 1551 | region.size, region_size, region_size); |
| 1552 | exit(EXIT_FAILURE); |
| 1553 | } |
| 1554 | |
| 1555 | int offset = 0; |
| 1556 | if ((region_type == 1) && (region_size < region.size)) { |
| 1557 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)" |
| 1558 | " bytes. Padding before injecting.\n", |
| 1559 | region_name(region_type), region.size, |
| 1560 | region.size, region_size, region_size); |
| 1561 | offset = region.size - region_size; |
| 1562 | memset(image + region.base, 0xff, offset); |
| 1563 | } |
| 1564 | |
Stefan Reinauer | 5e93b37 | 2012-09-25 13:30:48 -0700 | [diff] [blame] | 1565 | if (size < region.base + offset + region_size) { |
| 1566 | fprintf(stderr, "Output file is too small. (%d < %d)\n", |
| 1567 | size, region.base + offset + region_size); |
| 1568 | exit(EXIT_FAILURE); |
| 1569 | } |
| 1570 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1571 | if (read(region_fd, image + region.base + offset, region_size) != region_size) { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1572 | perror("Could not read file"); |
| 1573 | exit(EXIT_FAILURE); |
| 1574 | } |
| 1575 | |
| 1576 | close(region_fd); |
| 1577 | |
| 1578 | printf("Adding %s as the %s section of %s\n", |
| 1579 | region_fname, region_name(region_type), filename); |
| 1580 | write_image(filename, image, size); |
| 1581 | } |
| 1582 | |
Jacob Garber | 595d926 | 2019-06-27 17:33:10 -0600 | [diff] [blame] | 1583 | static unsigned int next_pow2(unsigned int x) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1584 | { |
| 1585 | unsigned int y = 1; |
| 1586 | if (x == 0) |
| 1587 | return 0; |
| 1588 | while (y <= x) |
| 1589 | y = y << 1; |
| 1590 | |
| 1591 | return y; |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * Determine if two memory regions overlap. |
| 1596 | * |
| 1597 | * @param r1, r2 Memory regions to compare. |
Elyes HAOUAS | a1ccaed | 2018-08-24 07:58:00 +0200 | [diff] [blame] | 1598 | * @return 0 if the two regions are separate |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1599 | * @return 1 if the two regions overlap |
| 1600 | */ |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1601 | static int regions_collide(const struct region *r1, const struct region *r2) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1602 | { |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1603 | if ((r1->size == 0) || (r2->size == 0)) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1604 | return 0; |
| 1605 | |
Nico Huber | 844eda0 | 2019-01-05 00:06:19 +0100 | [diff] [blame] | 1606 | /* r1 should be either completely below or completely above r2 */ |
| 1607 | return !(r1->limit < r2->base || r1->base > r2->limit); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1608 | } |
| 1609 | |
Jacob Garber | 595d926 | 2019-06-27 17:33:10 -0600 | [diff] [blame] | 1610 | static void new_layout(const char *filename, char *image, int size, |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1611 | const char *layout_fname) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1612 | { |
| 1613 | FILE *romlayout; |
| 1614 | char tempstr[256]; |
| 1615 | char layout_region_name[256]; |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1616 | unsigned int i, j; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1617 | int region_number; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1618 | struct region current_regions[MAX_REGIONS]; |
| 1619 | struct region new_regions[MAX_REGIONS]; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1620 | int new_extent = 0; |
| 1621 | char *new_image; |
| 1622 | |
| 1623 | /* load current descriptor map and regions */ |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1624 | struct frba *frba = find_frba(image, size); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1625 | if (!frba) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1626 | exit(EXIT_FAILURE); |
| 1627 | |
Alexander Couzens | fd5c658 | 2016-10-08 00:37:24 +0200 | [diff] [blame] | 1628 | for (i = 0; i < max_regions; i++) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1629 | current_regions[i] = get_region(frba, i); |
| 1630 | new_regions[i] = get_region(frba, i); |
| 1631 | } |
| 1632 | |
| 1633 | /* read new layout */ |
| 1634 | romlayout = fopen(layout_fname, "r"); |
| 1635 | |
| 1636 | if (!romlayout) { |
| 1637 | perror("Could not read layout file.\n"); |
| 1638 | exit(EXIT_FAILURE); |
| 1639 | } |
| 1640 | |
| 1641 | while (!feof(romlayout)) { |
| 1642 | char *tstr1, *tstr2; |
| 1643 | |
Patrick Georgi | 802ad52 | 2014-08-09 17:12:23 +0200 | [diff] [blame] | 1644 | if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1645 | layout_region_name)) |
| 1646 | continue; |
| 1647 | |
| 1648 | region_number = region_num(layout_region_name); |
| 1649 | if (region_number < 0) |
| 1650 | continue; |
| 1651 | |
| 1652 | tstr1 = strtok(tempstr, ":"); |
| 1653 | tstr2 = strtok(NULL, ":"); |
| 1654 | if (!tstr1 || !tstr2) { |
| 1655 | fprintf(stderr, "Could not parse layout file.\n"); |
| 1656 | exit(EXIT_FAILURE); |
| 1657 | } |
| 1658 | new_regions[region_number].base = strtol(tstr1, |
| 1659 | (char **)NULL, 16); |
| 1660 | new_regions[region_number].limit = strtol(tstr2, |
| 1661 | (char **)NULL, 16); |
| 1662 | new_regions[region_number].size = |
| 1663 | new_regions[region_number].limit - |
| 1664 | new_regions[region_number].base + 1; |
| 1665 | |
| 1666 | if (new_regions[region_number].size < 0) |
| 1667 | new_regions[region_number].size = 0; |
| 1668 | } |
| 1669 | fclose(romlayout); |
| 1670 | |
| 1671 | /* check new layout */ |
Alexander Couzens | fd5c658 | 2016-10-08 00:37:24 +0200 | [diff] [blame] | 1672 | for (i = 0; i < max_regions; i++) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1673 | if (new_regions[i].size == 0) |
| 1674 | continue; |
| 1675 | |
| 1676 | if (new_regions[i].size < current_regions[i].size) { |
| 1677 | printf("DANGER: Region %s is shrinking.\n", |
| 1678 | region_name(i)); |
| 1679 | printf(" The region will be truncated to fit.\n"); |
| 1680 | printf(" This may result in an unusable image.\n"); |
| 1681 | } |
| 1682 | |
Alexander Couzens | fd5c658 | 2016-10-08 00:37:24 +0200 | [diff] [blame] | 1683 | for (j = i + 1; j < max_regions; j++) { |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1684 | if (regions_collide(&new_regions[i], &new_regions[j])) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1685 | fprintf(stderr, "Regions would overlap.\n"); |
| 1686 | exit(EXIT_FAILURE); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /* detect if the image size should grow */ |
| 1691 | if (new_extent < new_regions[i].limit) |
| 1692 | new_extent = new_regions[i].limit; |
| 1693 | } |
| 1694 | |
Marcello Sylvester Bauer | 6f9a778 | 2020-02-04 17:20:50 +0100 | [diff] [blame] | 1695 | /* check if the image is actually a Flash Descriptor region */ |
| 1696 | if (size == new_regions[0].size) { |
| 1697 | printf("The image is a single Flash Descriptor:\n"); |
| 1698 | printf(" Only the descriptor will be modified\n"); |
| 1699 | new_extent = size; |
| 1700 | } else { |
| 1701 | new_extent = next_pow2(new_extent - 1); |
| 1702 | if (new_extent != size) { |
| 1703 | printf("The image has changed in size.\n"); |
| 1704 | printf("The old image is %d bytes.\n", size); |
| 1705 | printf("The new image is %d bytes.\n", new_extent); |
| 1706 | } |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /* copy regions to a new image */ |
| 1710 | new_image = malloc(new_extent); |
| 1711 | memset(new_image, 0xff, new_extent); |
Alexander Couzens | fd5c658 | 2016-10-08 00:37:24 +0200 | [diff] [blame] | 1712 | for (i = 0; i < max_regions; i++) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1713 | int copy_size = new_regions[i].size; |
| 1714 | int offset_current = 0, offset_new = 0; |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1715 | const struct region *current = ¤t_regions[i]; |
| 1716 | const struct region *new = &new_regions[i]; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1717 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1718 | if (new->size == 0) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1719 | continue; |
| 1720 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1721 | if (new->size > current->size) { |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1722 | /* copy from the end of the current region */ |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1723 | copy_size = current->size; |
Wim Vervoorn | 05bc9b3 | 2020-01-17 13:48:00 +0100 | [diff] [blame] | 1724 | if (i == REGION_BIOS) |
| 1725 | offset_new = new->size - current->size; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
Wim Vervoorn | 05bc9b3 | 2020-01-17 13:48:00 +0100 | [diff] [blame] | 1728 | if ((i == REGION_BIOS) && (new->size < current->size)) { |
| 1729 | /* copy BIOS region to the end of the new region */ |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1730 | offset_current = current->size - new->size; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1731 | } |
| 1732 | |
Marcello Sylvester Bauer | 6f9a778 | 2020-02-04 17:20:50 +0100 | [diff] [blame] | 1733 | if (size < current->base + offset_current + copy_size) { |
| 1734 | printf("Skip descriptor %d (%s) (region missing in the old image)\n", i, |
| 1735 | region_name(i)); |
| 1736 | continue; |
| 1737 | }; |
| 1738 | |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1739 | printf("Copy Descriptor %d (%s) (%d bytes)\n", i, |
| 1740 | region_name(i), copy_size); |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1741 | printf(" from %08x+%08x:%08x (%10d)\n", current->base, |
| 1742 | offset_current, current->limit, current->size); |
| 1743 | printf(" to %08x+%08x:%08x (%10d)\n", new->base, |
| 1744 | offset_new, new->limit, new->size); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1745 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1746 | memcpy(new_image + new->base + offset_new, |
| 1747 | image + current->base + offset_current, |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1748 | copy_size); |
| 1749 | } |
| 1750 | |
| 1751 | /* update new descriptor regions */ |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1752 | frba = find_frba(new_image, new_extent); |
| 1753 | if (!frba) |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1754 | exit(EXIT_FAILURE); |
| 1755 | |
Marcello Sylvester Bauer | 6f9a778 | 2020-02-04 17:20:50 +0100 | [diff] [blame] | 1756 | printf("Modify Flash Descriptor regions\n"); |
Bill XIE | 612ec0e | 2017-08-30 16:10:27 +0800 | [diff] [blame] | 1757 | for (i = 1; i < max_regions; i++) |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1758 | set_region(frba, i, &new_regions[i]); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1759 | |
| 1760 | write_image(filename, new_image, new_extent); |
| 1761 | free(new_image); |
| 1762 | } |
| 1763 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1764 | static void print_version(void) |
| 1765 | { |
| 1766 | printf("ifdtool v%s -- ", IFDTOOL_VERSION); |
| 1767 | printf("Copyright (C) 2011 Google Inc.\n\n"); |
| 1768 | printf |
| 1769 | ("This program is free software: you can redistribute it and/or modify\n" |
| 1770 | "it under the terms of the GNU General Public License as published by\n" |
| 1771 | "the Free Software Foundation, version 2 of the License.\n\n" |
| 1772 | "This program is distributed in the hope that it will be useful,\n" |
| 1773 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 1774 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
Martin Roth | eb20e60 | 2016-01-12 13:30:50 -0700 | [diff] [blame] | 1775 | "GNU General Public License for more details.\n\n"); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | static void print_usage(const char *name) |
| 1779 | { |
| 1780 | printf("usage: %s [-vhdix?] <filename>\n", name); |
| 1781 | printf("\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1782 | " -d | --dump: dump intel firmware descriptor\n" |
| 1783 | " -f | --layout <filename> dump regions into a flashrom layout file\n" |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1784 | " -F | --fmap-layout <filename> dump IFD regions into a fmap layout template (.fmd) file\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1785 | " -t | --validate Validate that the firmware descriptor layout matches the fmap layout\n" |
| 1786 | " -x | --extract: extract intel fd modules\n" |
| 1787 | " -i | --inject <region>:<module> inject file <module> into region <region>\n" |
| 1788 | " -n | --newlayout <filename> update regions using a flashrom layout file\n" |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1789 | " -O | --output <filename> output filename\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1790 | " -s | --spifreq <17|20|30|33|48|50> set the SPI frequency\n" |
| 1791 | " -D | --density <512|1|2|4|8|16|32|64> set chip density (512 in KByte, others in MByte)\n" |
| 1792 | " -C | --chip <0|1|2> select spi chip on which to operate\n" |
| 1793 | " can only be used once per run:\n" |
| 1794 | " 0 - both chips (default), 1 - first chip, 2 - second chip\n" |
| 1795 | " -e | --em100 set SPI frequency to 20MHz and disable\n" |
| 1796 | " Dual Output Fast Read Support\n" |
| 1797 | " -l | --lock Lock firmware descriptor and ME region\n" |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 1798 | " -r | --read Enable CPU/BIOS read access for ME region\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1799 | " -u | --unlock Unlock firmware descriptor and ME region\n" |
Evgeny Zinoviev | 8c4c370 | 2020-02-16 20:34:26 +0300 | [diff] [blame] | 1800 | " -M | --altmedisable <0|1> Set the MeDisable and AltMeDisable (or HAP for skylake or newer platform)\n" |
| 1801 | " bits to disable ME\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1802 | " -p | --platform Add platform-specific quirks\n" |
Subrata Banik | 15da174 | 2020-08-26 13:27:21 +0530 | [diff] [blame] | 1803 | " adl - Alder Lake\n" |
| 1804 | " aplk - Apollo Lake\n" |
| 1805 | " cnl - Cannon Lake\n" |
Johnny Lin | e273a02 | 2021-06-22 11:26:46 +0800 | [diff] [blame] | 1806 | " lbg - Lewisburg PCH\n" |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 1807 | " dnv - Denverton\n" |
Lean Sheng Tan | 0faba3c | 2021-06-09 07:52:24 -0700 | [diff] [blame] | 1808 | " ehl - Elkhart Lake\n" |
Subrata Banik | 15da174 | 2020-08-26 13:27:21 +0530 | [diff] [blame] | 1809 | " glk - Gemini Lake\n" |
| 1810 | " icl - Ice Lake\n" |
Wonkyu Kim | 3922aa5 | 2022-02-02 15:19:05 -0800 | [diff] [blame] | 1811 | " ifd2 - IFDv2 Platform\n" |
Subrata Banik | 15da174 | 2020-08-26 13:27:21 +0530 | [diff] [blame] | 1812 | " jsl - Jasper Lake\n" |
| 1813 | " sklkbl - Sky Lake/Kaby Lake\n" |
| 1814 | " tgl - Tiger Lake\n" |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 1815 | " wbg - Wellsburg\n" |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1816 | " -S | --setpchstrap Write a PCH strap\n" |
| 1817 | " -V | --newvalue The new value to write into PCH strap specified by -S\n" |
Arthur Heymans | 4cb888e | 2019-10-17 19:37:45 +0200 | [diff] [blame] | 1818 | " -v | --version: print the version\n" |
| 1819 | " -h | --help: print this help\n\n" |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1820 | "<region> is one of Descriptor, BIOS, ME, GbE, Platform Data, Secondary BIOS, " |
| 1821 | "Device Exp1, EC, Device Exp2, IE, 10GbE_0, 10GbE_1, PTT\n" |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1822 | "\n"); |
| 1823 | } |
| 1824 | |
| 1825 | int main(int argc, char *argv[]) |
| 1826 | { |
| 1827 | int opt, option_index = 0; |
| 1828 | int mode_dump = 0, mode_extract = 0, mode_inject = 0, mode_spifreq = 0; |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1829 | int mode_em100 = 0, mode_locked = 0, mode_unlocked = 0, mode_validate = 0; |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1830 | int mode_layout = 0, mode_newlayout = 0, mode_density = 0, mode_setstrap = 0; |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1831 | int mode_read = 0, mode_altmedisable = 0, altmedisable = 0, mode_fmap_template = 0; |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1832 | char *region_type_string = NULL, *region_fname = NULL; |
| 1833 | const char *layout_fname = NULL; |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1834 | char *new_filename = NULL; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1835 | int region_type = -1, inputfreq = 0; |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1836 | unsigned int value = 0; |
| 1837 | unsigned int pchstrap = 0; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1838 | unsigned int new_density = 0; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1839 | enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ; |
| 1840 | |
Bill XIE | fa5f994 | 2017-09-12 11:22:29 +0800 | [diff] [blame] | 1841 | static const struct option long_options[] = { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1842 | {"dump", 0, NULL, 'd'}, |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 1843 | {"layout", 1, NULL, 'f'}, |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1844 | {"fmap-template", 1, NULL, 'F'}, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1845 | {"extract", 0, NULL, 'x'}, |
| 1846 | {"inject", 1, NULL, 'i'}, |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1847 | {"newlayout", 1, NULL, 'n'}, |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1848 | {"output", 1, NULL, 'O'}, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1849 | {"spifreq", 1, NULL, 's'}, |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1850 | {"density", 1, NULL, 'D'}, |
| 1851 | {"chip", 1, NULL, 'C'}, |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 1852 | {"altmedisable", 1, NULL, 'M'}, |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 1853 | {"em100", 0, NULL, 'e'}, |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1854 | {"lock", 0, NULL, 'l'}, |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 1855 | {"read", 0, NULL, 'r'}, |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 1856 | {"unlock", 0, NULL, 'u'}, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1857 | {"version", 0, NULL, 'v'}, |
| 1858 | {"help", 0, NULL, 'h'}, |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 1859 | {"platform", 0, NULL, 'p'}, |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 1860 | {"validate", 0, NULL, 't'}, |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1861 | {"setpchstrap", 1, NULL, 'S'}, |
| 1862 | {"newvalue", 1, NULL, 'V'}, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1863 | {0, 0, 0, 0} |
| 1864 | }; |
| 1865 | |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1866 | while ((opt = getopt_long(argc, argv, "S:V:df:F:D:C:M:xi:n:O:s:p:elruvth?", |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1867 | long_options, &option_index)) != EOF) { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1868 | switch (opt) { |
| 1869 | case 'd': |
| 1870 | mode_dump = 1; |
| 1871 | break; |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 1872 | case 'S': |
| 1873 | mode_setstrap = 1; |
| 1874 | pchstrap = strtoul(optarg, NULL, 0); |
| 1875 | break; |
| 1876 | case 'V': |
| 1877 | value = strtoul(optarg, NULL, 0); |
| 1878 | break; |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 1879 | case 'f': |
| 1880 | mode_layout = 1; |
| 1881 | layout_fname = strdup(optarg); |
| 1882 | if (!layout_fname) { |
| 1883 | fprintf(stderr, "No layout file specified\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1884 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 1885 | exit(EXIT_FAILURE); |
| 1886 | } |
| 1887 | break; |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 1888 | case 'F': |
| 1889 | mode_fmap_template = 1; |
| 1890 | layout_fname = strdup(optarg); |
| 1891 | if (!layout_fname) { |
| 1892 | fprintf(stderr, "No layout file specified\n"); |
| 1893 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
| 1894 | exit(EXIT_FAILURE); |
| 1895 | } |
| 1896 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1897 | case 'x': |
| 1898 | mode_extract = 1; |
| 1899 | break; |
| 1900 | case 'i': |
| 1901 | // separate type and file name |
| 1902 | region_type_string = strdup(optarg); |
| 1903 | region_fname = strchr(region_type_string, ':'); |
| 1904 | if (!region_fname) { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1905 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1906 | exit(EXIT_FAILURE); |
| 1907 | } |
| 1908 | region_fname[0] = '\0'; |
| 1909 | region_fname++; |
| 1910 | // Descriptor, BIOS, ME, GbE, Platform |
| 1911 | // valid type? |
| 1912 | if (!strcasecmp("Descriptor", region_type_string)) |
| 1913 | region_type = 0; |
| 1914 | else if (!strcasecmp("BIOS", region_type_string)) |
| 1915 | region_type = 1; |
| 1916 | else if (!strcasecmp("ME", region_type_string)) |
| 1917 | region_type = 2; |
| 1918 | else if (!strcasecmp("GbE", region_type_string)) |
| 1919 | region_type = 3; |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1920 | else if (!strcasecmp("Platform Data", region_type_string)) |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1921 | region_type = 4; |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1922 | else if (!strcasecmp("Device Exp1", region_type_string)) |
Stefan Reinauer | e4e08f2 | 2020-10-26 10:15:42 -0700 | [diff] [blame] | 1923 | region_type = 5; |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1924 | else if (!strcasecmp("Secondary BIOS", region_type_string)) |
Stefan Reinauer | e4e08f2 | 2020-10-26 10:15:42 -0700 | [diff] [blame] | 1925 | region_type = 6; |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1926 | else if (!strcasecmp("Reserved", region_type_string)) |
Stefan Reinauer | e4e08f2 | 2020-10-26 10:15:42 -0700 | [diff] [blame] | 1927 | region_type = 7; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 1928 | else if (!strcasecmp("EC", region_type_string)) |
| 1929 | region_type = 8; |
Jeff Daly | 3623eca | 2022-01-05 23:51:40 -0500 | [diff] [blame] | 1930 | else if (!strcasecmp("Device Exp2", region_type_string)) |
| 1931 | region_type = 9; |
| 1932 | else if (!strcasecmp("IE", region_type_string)) |
| 1933 | region_type = 10; |
| 1934 | else if (!strcasecmp("10GbE_0", region_type_string)) |
| 1935 | region_type = 11; |
| 1936 | else if (!strcasecmp("10GbE_1", region_type_string)) |
| 1937 | region_type = 12; |
| 1938 | else if (!strcasecmp("PTT", region_type_string)) |
| 1939 | region_type = 15; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1940 | if (region_type == -1) { |
| 1941 | fprintf(stderr, "No such region type: '%s'\n\n", |
| 1942 | region_type_string); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1943 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 1944 | exit(EXIT_FAILURE); |
| 1945 | } |
| 1946 | mode_inject = 1; |
| 1947 | break; |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1948 | case 'n': |
| 1949 | mode_newlayout = 1; |
| 1950 | layout_fname = strdup(optarg); |
| 1951 | if (!layout_fname) { |
| 1952 | fprintf(stderr, "No layout file specified\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1953 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 1954 | exit(EXIT_FAILURE); |
| 1955 | } |
| 1956 | break; |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1957 | case 'O': |
| 1958 | new_filename = strdup(optarg); |
| 1959 | if (!new_filename) { |
| 1960 | fprintf(stderr, "No output filename specified\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1961 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 1962 | exit(EXIT_FAILURE); |
| 1963 | } |
| 1964 | break; |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1965 | case 'D': |
| 1966 | mode_density = 1; |
| 1967 | new_density = strtoul(optarg, NULL, 0); |
| 1968 | switch (new_density) { |
| 1969 | case 512: |
| 1970 | new_density = COMPONENT_DENSITY_512KB; |
| 1971 | break; |
| 1972 | case 1: |
| 1973 | new_density = COMPONENT_DENSITY_1MB; |
| 1974 | break; |
| 1975 | case 2: |
| 1976 | new_density = COMPONENT_DENSITY_2MB; |
| 1977 | break; |
| 1978 | case 4: |
| 1979 | new_density = COMPONENT_DENSITY_4MB; |
| 1980 | break; |
| 1981 | case 8: |
| 1982 | new_density = COMPONENT_DENSITY_8MB; |
| 1983 | break; |
| 1984 | case 16: |
| 1985 | new_density = COMPONENT_DENSITY_16MB; |
| 1986 | break; |
| 1987 | case 32: |
| 1988 | new_density = COMPONENT_DENSITY_32MB; |
| 1989 | break; |
| 1990 | case 64: |
| 1991 | new_density = COMPONENT_DENSITY_64MB; |
| 1992 | break; |
| 1993 | case 0: |
| 1994 | new_density = COMPONENT_DENSITY_UNUSED; |
| 1995 | break; |
| 1996 | default: |
| 1997 | printf("error: Unknown density\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 1998 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 1999 | exit(EXIT_FAILURE); |
| 2000 | } |
| 2001 | break; |
| 2002 | case 'C': |
| 2003 | selected_chip = strtol(optarg, NULL, 0); |
| 2004 | if (selected_chip > 2) { |
| 2005 | fprintf(stderr, "error: Invalid chip selection\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2006 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 2007 | exit(EXIT_FAILURE); |
| 2008 | } |
| 2009 | break; |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 2010 | case 'M': |
| 2011 | mode_altmedisable = 1; |
| 2012 | altmedisable = strtol(optarg, NULL, 0); |
| 2013 | if (altmedisable > 1) { |
| 2014 | fprintf(stderr, "error: Illegal value\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2015 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 2016 | exit(EXIT_FAILURE); |
| 2017 | } |
| 2018 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2019 | case 's': |
| 2020 | // Parse the requested SPI frequency |
| 2021 | inputfreq = strtol(optarg, NULL, 0); |
| 2022 | switch (inputfreq) { |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 2023 | case 17: |
| 2024 | spifreq = SPI_FREQUENCY_17MHZ; |
| 2025 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2026 | case 20: |
| 2027 | spifreq = SPI_FREQUENCY_20MHZ; |
| 2028 | break; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 2029 | case 30: |
| 2030 | spifreq = SPI_FREQUENCY_50MHZ_30MHZ; |
| 2031 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2032 | case 33: |
| 2033 | spifreq = SPI_FREQUENCY_33MHZ; |
| 2034 | break; |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 2035 | case 48: |
| 2036 | spifreq = SPI_FREQUENCY_48MHZ; |
| 2037 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2038 | case 50: |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 2039 | spifreq = SPI_FREQUENCY_50MHZ_30MHZ; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2040 | break; |
| 2041 | default: |
| 2042 | fprintf(stderr, "Invalid SPI Frequency: %d\n", |
| 2043 | inputfreq); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2044 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2045 | exit(EXIT_FAILURE); |
| 2046 | } |
| 2047 | mode_spifreq = 1; |
| 2048 | break; |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 2049 | case 'e': |
| 2050 | mode_em100 = 1; |
| 2051 | break; |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2052 | case 'l': |
| 2053 | mode_locked = 1; |
| 2054 | if (mode_unlocked == 1) { |
| 2055 | fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n"); |
| 2056 | exit(EXIT_FAILURE); |
| 2057 | } |
| 2058 | break; |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 2059 | case 'r': |
| 2060 | mode_read = 1; |
| 2061 | break; |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2062 | case 'u': |
| 2063 | mode_unlocked = 1; |
| 2064 | if (mode_locked == 1) { |
| 2065 | fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n"); |
| 2066 | exit(EXIT_FAILURE); |
| 2067 | } |
| 2068 | break; |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 2069 | case 'p': |
| 2070 | if (!strcmp(optarg, "aplk")) { |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 2071 | platform = PLATFORM_APL; |
| 2072 | } else if (!strcmp(optarg, "cnl")) { |
| 2073 | platform = PLATFORM_CNL; |
Johnny Lin | e273a02 | 2021-06-22 11:26:46 +0800 | [diff] [blame] | 2074 | } else if (!strcmp(optarg, "lbg")) { |
| 2075 | platform = PLATFORM_LBG; |
Jeff Daly | abd4b96 | 2022-01-06 00:52:30 -0500 | [diff] [blame] | 2076 | } else if (!strcmp(optarg, "dnv")) { |
| 2077 | platform = PLATFORM_DNV; |
Lean Sheng Tan | 0faba3c | 2021-06-09 07:52:24 -0700 | [diff] [blame] | 2078 | } else if (!strcmp(optarg, "ehl")) { |
| 2079 | platform = PLATFORM_EHL; |
Furquan Shaikh | c0257dd | 2018-05-02 23:29:04 -0700 | [diff] [blame] | 2080 | } else if (!strcmp(optarg, "glk")) { |
| 2081 | platform = PLATFORM_GLK; |
Aamir Bohra | 1018be2 | 2018-06-29 15:08:50 +0530 | [diff] [blame] | 2082 | } else if (!strcmp(optarg, "icl")) { |
| 2083 | platform = PLATFORM_ICL; |
rkanabar | d64b046 | 2019-08-30 11:40:08 +0530 | [diff] [blame] | 2084 | } else if (!strcmp(optarg, "jsl")) { |
Lean Sheng Tan | 0faba3c | 2021-06-09 07:52:24 -0700 | [diff] [blame] | 2085 | platform = PLATFORM_JSL; |
Furquan Shaikh | 088b6e8 | 2018-03-21 10:42:37 -0700 | [diff] [blame] | 2086 | } else if (!strcmp(optarg, "sklkbl")) { |
| 2087 | platform = PLATFORM_SKLKBL; |
Ravi Sarawadi | 7d9d63b | 2019-10-22 13:45:36 -0700 | [diff] [blame] | 2088 | } else if (!strcmp(optarg, "tgl")) { |
| 2089 | platform = PLATFORM_TGL; |
Subrata Banik | 46f8073 | 2020-03-14 15:01:42 +0530 | [diff] [blame] | 2090 | } else if (!strcmp(optarg, "adl")) { |
| 2091 | platform = PLATFORM_ADL; |
Wonkyu Kim | 3922aa5 | 2022-02-02 15:19:05 -0800 | [diff] [blame] | 2092 | } else if (!strcmp(optarg, "ifd2")) { |
| 2093 | platform = PLATFORM_IFD2; |
Subrata Banik | ca82e61 | 2022-01-20 18:51:21 +0530 | [diff] [blame] | 2094 | } else if (!strcmp(optarg, "mtl")) { |
| 2095 | platform = PLATFORM_MTL; |
Patrick Rudolph | 1659874 | 2022-10-21 15:13:43 +0200 | [diff] [blame] | 2096 | } else if (!strcmp(optarg, "wbg")) { |
| 2097 | platform = PLATFORM_WBG; |
Andrey Petrov | 96ecb77 | 2016-10-31 19:31:54 -0700 | [diff] [blame] | 2098 | } else { |
| 2099 | fprintf(stderr, "Unknown platform: %s\n", optarg); |
| 2100 | exit(EXIT_FAILURE); |
| 2101 | } |
| 2102 | break; |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 2103 | case 't': |
| 2104 | mode_validate = 1; |
| 2105 | break; |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2106 | case 'v': |
| 2107 | print_version(); |
| 2108 | exit(EXIT_SUCCESS); |
| 2109 | break; |
| 2110 | case 'h': |
| 2111 | case '?': |
| 2112 | default: |
| 2113 | print_usage(argv[0]); |
| 2114 | exit(EXIT_SUCCESS); |
| 2115 | break; |
| 2116 | } |
| 2117 | } |
| 2118 | |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 2119 | if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject + |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2120 | mode_setstrap + mode_newlayout + (mode_spifreq | mode_em100 | |
| 2121 | mode_unlocked | mode_locked) + mode_altmedisable + mode_validate) > 1) { |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2122 | fprintf(stderr, "You may not specify more than one mode.\n\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2123 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2124 | exit(EXIT_FAILURE); |
| 2125 | } |
| 2126 | |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 2127 | if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject + |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2128 | mode_setstrap + mode_newlayout + mode_spifreq + mode_em100 + |
| 2129 | mode_locked + mode_unlocked + mode_density + mode_altmedisable + mode_validate) == 0) { |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2130 | fprintf(stderr, "You need to specify a mode.\n\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2131 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2132 | exit(EXIT_FAILURE); |
| 2133 | } |
| 2134 | |
| 2135 | if (optind + 1 != argc) { |
| 2136 | fprintf(stderr, "You need to specify a file.\n\n"); |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2137 | fprintf(stderr, "run '%s -h' for usage\n", argv[0]); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2138 | exit(EXIT_FAILURE); |
| 2139 | } |
| 2140 | |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2141 | if (platform == -1) |
| 2142 | fprintf(stderr, "Warning: No platform specified. Output may be incomplete\n"); |
| 2143 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2144 | char *filename = argv[optind]; |
Scott Duplichan | f2c9837 | 2014-12-12 21:03:06 -0600 | [diff] [blame] | 2145 | int bios_fd = open(filename, O_RDONLY | O_BINARY); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2146 | if (bios_fd == -1) { |
| 2147 | perror("Could not open file"); |
| 2148 | exit(EXIT_FAILURE); |
| 2149 | } |
| 2150 | struct stat buf; |
| 2151 | if (fstat(bios_fd, &buf) == -1) { |
| 2152 | perror("Could not stat file"); |
| 2153 | exit(EXIT_FAILURE); |
| 2154 | } |
| 2155 | int size = buf.st_size; |
| 2156 | |
| 2157 | printf("File %s is %d bytes\n", filename, size); |
| 2158 | |
| 2159 | char *image = malloc(size); |
| 2160 | if (!image) { |
| 2161 | printf("Out of memory.\n"); |
| 2162 | exit(EXIT_FAILURE); |
| 2163 | } |
| 2164 | |
| 2165 | if (read(bios_fd, image, size) != size) { |
| 2166 | perror("Could not read file"); |
| 2167 | exit(EXIT_FAILURE); |
| 2168 | } |
| 2169 | |
| 2170 | close(bios_fd); |
| 2171 | |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2172 | // generate new filename |
| 2173 | if (new_filename == NULL) { |
| 2174 | new_filename = (char *) malloc((strlen(filename) + 5) * sizeof(char)); |
| 2175 | if (!new_filename) { |
| 2176 | printf("Out of memory.\n"); |
| 2177 | exit(EXIT_FAILURE); |
| 2178 | } |
| 2179 | // - 5: leave room for ".new\0" |
| 2180 | strcpy(new_filename, filename); |
| 2181 | strcat(new_filename, ".new"); |
| 2182 | } |
| 2183 | |
Duncan Laurie | 1f7fd72 | 2015-06-22 11:14:48 -0700 | [diff] [blame] | 2184 | check_ifd_version(image, size); |
| 2185 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2186 | if (mode_dump) |
| 2187 | dump_fd(image, size); |
| 2188 | |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 2189 | if (mode_layout) |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2190 | dump_flashrom_layout(image, size, layout_fname); |
Chris Douglass | 03ce014 | 2014-02-26 13:30:13 -0500 | [diff] [blame] | 2191 | |
Maximilian Brune | 347596a | 2023-03-05 20:55:32 +0100 | [diff] [blame] | 2192 | if (mode_fmap_template) |
| 2193 | create_fmap_template(image, size, layout_fname); |
| 2194 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2195 | if (mode_extract) |
| 2196 | write_regions(image, size); |
| 2197 | |
Mathew King | c7ddc99 | 2019-08-08 14:59:25 -0600 | [diff] [blame] | 2198 | if (mode_validate) |
| 2199 | validate_layout(image, size); |
| 2200 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2201 | if (mode_inject) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2202 | inject_region(new_filename, image, size, region_type, |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2203 | region_fname); |
| 2204 | |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 2205 | if (mode_newlayout) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2206 | new_layout(new_filename, image, size, layout_fname); |
Chris Douglass | 4eabe1e | 2014-02-27 09:25:19 -0500 | [diff] [blame] | 2207 | |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2208 | if (mode_spifreq) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2209 | set_spi_frequency(new_filename, image, size, spifreq); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2210 | |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 2211 | if (mode_density) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2212 | set_chipdensity(new_filename, image, size, new_density); |
Jan Tatje | fa31751 | 2016-03-11 00:52:07 +0100 | [diff] [blame] | 2213 | |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 2214 | if (mode_em100) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2215 | set_em100_mode(new_filename, image, size); |
Stefan Reinauer | 1b1309f | 2012-05-11 15:53:43 -0700 | [diff] [blame] | 2216 | |
Alexander Couzens | d12ea11 | 2016-09-10 13:33:05 +0200 | [diff] [blame] | 2217 | if (mode_locked) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2218 | lock_descriptor(new_filename, image, size); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2219 | |
Usha P | 412679d | 2020-10-15 11:25:08 +0530 | [diff] [blame] | 2220 | if (mode_read) |
| 2221 | enable_cpu_read_me(new_filename, image, size); |
| 2222 | |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2223 | if (mode_unlocked) |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2224 | unlock_descriptor(new_filename, image, size); |
Stefan Reinauer | 3c53d33 | 2012-09-26 17:33:39 -0700 | [diff] [blame] | 2225 | |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 2226 | if (mode_setstrap) { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2227 | struct fpsba *fpsba = find_fpsba(image, size); |
| 2228 | const struct fdbar *fdb = find_fd(image, size); |
Patrick Rudolph | 802cbee | 2020-05-25 12:18:11 +0200 | [diff] [blame] | 2229 | set_pchstrap(fpsba, fdb, pchstrap, value); |
| 2230 | write_image(new_filename, image, size); |
| 2231 | } |
| 2232 | |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 2233 | if (mode_altmedisable) { |
Maximilian Brune | ab0e680 | 2023-03-05 04:34:40 +0100 | [diff] [blame] | 2234 | struct fpsba *fpsba = find_fpsba(image, size); |
| 2235 | struct fmsba *fmsba = find_fmsba(image, size); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 2236 | fpsba_set_altmedisable(fpsba, fmsba, altmedisable); |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2237 | write_image(new_filename, image, size); |
Bill XIE | b3e15a2 | 2017-09-07 18:34:50 +0800 | [diff] [blame] | 2238 | } |
| 2239 | |
Marcello Sylvester Bauer | dc1596c | 2020-02-11 13:31:38 +0100 | [diff] [blame] | 2240 | free(new_filename); |
Stefan Reinauer | 1c795ad1 | 2011-10-14 12:49:41 -0700 | [diff] [blame] | 2241 | free(image); |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |