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