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