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