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