Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ifdtool - Manage Intel Firmware Descriptor information |
| 3 | * |
| 4 | * Copyright 2014 Google, Inc |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0 |
| 7 | * |
| 8 | * From Coreboot project, but it got a serious code clean-up |
| 9 | * and a few new features |
| 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <getopt.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 21 | #include <libfdt.h> |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 22 | #include "ifdtool.h" |
| 23 | |
| 24 | #undef DEBUG |
| 25 | |
| 26 | #ifdef DEBUG |
| 27 | #define debug(fmt, args...) printf(fmt, ##args) |
| 28 | #else |
| 29 | #define debug(fmt, args...) |
| 30 | #endif |
| 31 | |
| 32 | #define FD_SIGNATURE 0x0FF0A55A |
| 33 | #define FLREG_BASE(reg) ((reg & 0x00000fff) << 12); |
| 34 | #define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff); |
| 35 | |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 36 | enum input_file_type_t { |
| 37 | IF_normal, |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 38 | IF_fdt, |
| 39 | IF_uboot, |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct input_file { |
| 43 | char *fname; |
| 44 | unsigned int addr; |
| 45 | enum input_file_type_t type; |
| 46 | }; |
| 47 | |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 48 | /** |
| 49 | * find_fd() - Find the flash description in the ROM image |
| 50 | * |
| 51 | * @image: Pointer to image |
| 52 | * @size: Size of image in bytes |
| 53 | * @return pointer to structure, or NULL if not found |
| 54 | */ |
| 55 | static struct fdbar_t *find_fd(char *image, int size) |
| 56 | { |
| 57 | uint32_t *ptr, *end; |
| 58 | |
| 59 | /* Scan for FD signature */ |
| 60 | for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) { |
| 61 | if (*ptr == FD_SIGNATURE) |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (ptr == end) { |
| 66 | printf("No Flash Descriptor found in this image\n"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Simon Glass | 7e8ffa4 | 2014-12-14 17:15:35 -0700 | [diff] [blame] | 70 | debug("Found Flash Descriptor signature at 0x%08lx\n", |
| 71 | (char *)ptr - image); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 72 | |
| 73 | return (struct fdbar_t *)ptr; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * get_region() - Get information about the selected region |
| 78 | * |
| 79 | * @frba: Flash region list |
| 80 | * @region_type: Type of region (0..MAX_REGIONS-1) |
| 81 | * @region: Region information is written here |
| 82 | * @return 0 if OK, else -ve |
| 83 | */ |
| 84 | static int get_region(struct frba_t *frba, int region_type, |
| 85 | struct region_t *region) |
| 86 | { |
| 87 | if (region_type >= MAX_REGIONS) { |
| 88 | fprintf(stderr, "Invalid region type.\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | region->base = FLREG_BASE(frba->flreg[region_type]); |
| 93 | region->limit = FLREG_LIMIT(frba->flreg[region_type]); |
| 94 | region->size = region->limit - region->base + 1; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const char *region_name(int region_type) |
| 100 | { |
| 101 | static const char *const regions[] = { |
| 102 | "Flash Descriptor", |
| 103 | "BIOS", |
| 104 | "Intel ME", |
| 105 | "GbE", |
| 106 | "Platform Data" |
| 107 | }; |
| 108 | |
| 109 | assert(region_type < MAX_REGIONS); |
| 110 | |
| 111 | return regions[region_type]; |
| 112 | } |
| 113 | |
| 114 | static const char *region_filename(int region_type) |
| 115 | { |
| 116 | static const char *const region_filenames[] = { |
| 117 | "flashregion_0_flashdescriptor.bin", |
| 118 | "flashregion_1_bios.bin", |
| 119 | "flashregion_2_intel_me.bin", |
| 120 | "flashregion_3_gbe.bin", |
| 121 | "flashregion_4_platform_data.bin" |
| 122 | }; |
| 123 | |
| 124 | assert(region_type < MAX_REGIONS); |
| 125 | |
| 126 | return region_filenames[region_type]; |
| 127 | } |
| 128 | |
| 129 | static int dump_region(int num, struct frba_t *frba) |
| 130 | { |
| 131 | struct region_t region; |
| 132 | int ret; |
| 133 | |
| 134 | ret = get_region(frba, num, ®ion); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | printf(" Flash Region %d (%s): %08x - %08x %s\n", |
| 139 | num, region_name(num), region.base, region.limit, |
| 140 | region.size < 1 ? "(unused)" : ""); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static void dump_frba(struct frba_t *frba) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | printf("Found Region Section\n"); |
| 150 | for (i = 0; i < MAX_REGIONS; i++) { |
| 151 | printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]); |
| 152 | dump_region(i, frba); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void decode_spi_frequency(unsigned int freq) |
| 157 | { |
| 158 | switch (freq) { |
| 159 | case SPI_FREQUENCY_20MHZ: |
| 160 | printf("20MHz"); |
| 161 | break; |
| 162 | case SPI_FREQUENCY_33MHZ: |
| 163 | printf("33MHz"); |
| 164 | break; |
| 165 | case SPI_FREQUENCY_50MHZ: |
| 166 | printf("50MHz"); |
| 167 | break; |
| 168 | default: |
| 169 | printf("unknown<%x>MHz", freq); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void decode_component_density(unsigned int density) |
| 174 | { |
| 175 | switch (density) { |
| 176 | case COMPONENT_DENSITY_512KB: |
| 177 | printf("512KiB"); |
| 178 | break; |
| 179 | case COMPONENT_DENSITY_1MB: |
| 180 | printf("1MiB"); |
| 181 | break; |
| 182 | case COMPONENT_DENSITY_2MB: |
| 183 | printf("2MiB"); |
| 184 | break; |
| 185 | case COMPONENT_DENSITY_4MB: |
| 186 | printf("4MiB"); |
| 187 | break; |
| 188 | case COMPONENT_DENSITY_8MB: |
| 189 | printf("8MiB"); |
| 190 | break; |
| 191 | case COMPONENT_DENSITY_16MB: |
| 192 | printf("16MiB"); |
| 193 | break; |
| 194 | default: |
| 195 | printf("unknown<%x>MiB", density); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static void dump_fcba(struct fcba_t *fcba) |
| 200 | { |
| 201 | printf("\nFound Component Section\n"); |
| 202 | printf("FLCOMP 0x%08x\n", fcba->flcomp); |
| 203 | printf(" Dual Output Fast Read Support: %ssupported\n", |
| 204 | (fcba->flcomp & (1 << 30)) ? "" : "not "); |
| 205 | printf(" Read ID/Read Status Clock Frequency: "); |
| 206 | decode_spi_frequency((fcba->flcomp >> 27) & 7); |
| 207 | printf("\n Write/Erase Clock Frequency: "); |
| 208 | decode_spi_frequency((fcba->flcomp >> 24) & 7); |
| 209 | printf("\n Fast Read Clock Frequency: "); |
| 210 | decode_spi_frequency((fcba->flcomp >> 21) & 7); |
| 211 | printf("\n Fast Read Support: %ssupported", |
| 212 | (fcba->flcomp & (1 << 20)) ? "" : "not "); |
| 213 | printf("\n Read Clock Frequency: "); |
| 214 | decode_spi_frequency((fcba->flcomp >> 17) & 7); |
| 215 | printf("\n Component 2 Density: "); |
| 216 | decode_component_density((fcba->flcomp >> 3) & 7); |
| 217 | printf("\n Component 1 Density: "); |
| 218 | decode_component_density(fcba->flcomp & 7); |
| 219 | printf("\n"); |
| 220 | printf("FLILL 0x%08x\n", fcba->flill); |
| 221 | printf(" Invalid Instruction 3: 0x%02x\n", |
| 222 | (fcba->flill >> 24) & 0xff); |
| 223 | printf(" Invalid Instruction 2: 0x%02x\n", |
| 224 | (fcba->flill >> 16) & 0xff); |
| 225 | printf(" Invalid Instruction 1: 0x%02x\n", |
| 226 | (fcba->flill >> 8) & 0xff); |
| 227 | printf(" Invalid Instruction 0: 0x%02x\n", |
| 228 | fcba->flill & 0xff); |
| 229 | printf("FLPB 0x%08x\n", fcba->flpb); |
| 230 | printf(" Flash Partition Boundary Address: 0x%06x\n\n", |
| 231 | (fcba->flpb & 0xfff) << 12); |
| 232 | } |
| 233 | |
| 234 | static void dump_fpsba(struct fpsba_t *fpsba) |
| 235 | { |
| 236 | int i; |
| 237 | |
| 238 | printf("Found PCH Strap Section\n"); |
| 239 | for (i = 0; i < MAX_STRAPS; i++) |
| 240 | printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]); |
| 241 | } |
| 242 | |
| 243 | static const char *get_enabled(int flag) |
| 244 | { |
| 245 | return flag ? "enabled" : "disabled"; |
| 246 | } |
| 247 | |
| 248 | static void decode_flmstr(uint32_t flmstr) |
| 249 | { |
| 250 | printf(" Platform Data Region Write Access: %s\n", |
| 251 | get_enabled(flmstr & (1 << 28))); |
| 252 | printf(" GbE Region Write Access: %s\n", |
| 253 | get_enabled(flmstr & (1 << 27))); |
| 254 | printf(" Intel ME Region Write Access: %s\n", |
| 255 | get_enabled(flmstr & (1 << 26))); |
| 256 | printf(" Host CPU/BIOS Region Write Access: %s\n", |
| 257 | get_enabled(flmstr & (1 << 25))); |
| 258 | printf(" Flash Descriptor Write Access: %s\n", |
| 259 | get_enabled(flmstr & (1 << 24))); |
| 260 | |
| 261 | printf(" Platform Data Region Read Access: %s\n", |
| 262 | get_enabled(flmstr & (1 << 20))); |
| 263 | printf(" GbE Region Read Access: %s\n", |
| 264 | get_enabled(flmstr & (1 << 19))); |
| 265 | printf(" Intel ME Region Read Access: %s\n", |
| 266 | get_enabled(flmstr & (1 << 18))); |
| 267 | printf(" Host CPU/BIOS Region Read Access: %s\n", |
| 268 | get_enabled(flmstr & (1 << 17))); |
| 269 | printf(" Flash Descriptor Read Access: %s\n", |
| 270 | get_enabled(flmstr & (1 << 16))); |
| 271 | |
| 272 | printf(" Requester ID: 0x%04x\n\n", |
| 273 | flmstr & 0xffff); |
| 274 | } |
| 275 | |
| 276 | static void dump_fmba(struct fmba_t *fmba) |
| 277 | { |
| 278 | printf("Found Master Section\n"); |
| 279 | printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1); |
| 280 | decode_flmstr(fmba->flmstr1); |
| 281 | printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2); |
| 282 | decode_flmstr(fmba->flmstr2); |
| 283 | printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3); |
| 284 | decode_flmstr(fmba->flmstr3); |
| 285 | } |
| 286 | |
| 287 | static void dump_fmsba(struct fmsba_t *fmsba) |
| 288 | { |
| 289 | int i; |
| 290 | |
| 291 | printf("Found Processor Strap Section\n"); |
| 292 | for (i = 0; i < 4; i++) |
| 293 | printf("????: 0x%08x\n", fmsba->data[0]); |
| 294 | } |
| 295 | |
| 296 | static void dump_jid(uint32_t jid) |
| 297 | { |
| 298 | printf(" SPI Component Device ID 1: 0x%02x\n", |
| 299 | (jid >> 16) & 0xff); |
| 300 | printf(" SPI Component Device ID 0: 0x%02x\n", |
| 301 | (jid >> 8) & 0xff); |
| 302 | printf(" SPI Component Vendor ID: 0x%02x\n", |
| 303 | jid & 0xff); |
| 304 | } |
| 305 | |
| 306 | static void dump_vscc(uint32_t vscc) |
| 307 | { |
| 308 | printf(" Lower Erase Opcode: 0x%02x\n", |
| 309 | vscc >> 24); |
| 310 | printf(" Lower Write Enable on Write Status: 0x%02x\n", |
| 311 | vscc & (1 << 20) ? 0x06 : 0x50); |
| 312 | printf(" Lower Write Status Required: %s\n", |
| 313 | vscc & (1 << 19) ? "Yes" : "No"); |
| 314 | printf(" Lower Write Granularity: %d bytes\n", |
| 315 | vscc & (1 << 18) ? 64 : 1); |
| 316 | printf(" Lower Block / Sector Erase Size: "); |
| 317 | switch ((vscc >> 16) & 0x3) { |
| 318 | case 0: |
| 319 | printf("256 Byte\n"); |
| 320 | break; |
| 321 | case 1: |
| 322 | printf("4KB\n"); |
| 323 | break; |
| 324 | case 2: |
| 325 | printf("8KB\n"); |
| 326 | break; |
| 327 | case 3: |
| 328 | printf("64KB\n"); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | printf(" Upper Erase Opcode: 0x%02x\n", |
| 333 | (vscc >> 8) & 0xff); |
| 334 | printf(" Upper Write Enable on Write Status: 0x%02x\n", |
| 335 | vscc & (1 << 4) ? 0x06 : 0x50); |
| 336 | printf(" Upper Write Status Required: %s\n", |
| 337 | vscc & (1 << 3) ? "Yes" : "No"); |
| 338 | printf(" Upper Write Granularity: %d bytes\n", |
| 339 | vscc & (1 << 2) ? 64 : 1); |
| 340 | printf(" Upper Block / Sector Erase Size: "); |
| 341 | switch (vscc & 0x3) { |
| 342 | case 0: |
| 343 | printf("256 Byte\n"); |
| 344 | break; |
| 345 | case 1: |
| 346 | printf("4KB\n"); |
| 347 | break; |
| 348 | case 2: |
| 349 | printf("8KB\n"); |
| 350 | break; |
| 351 | case 3: |
| 352 | printf("64KB\n"); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static void dump_vtba(struct vtba_t *vtba, int vtl) |
| 358 | { |
| 359 | int i; |
| 360 | int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8; |
| 361 | |
| 362 | printf("ME VSCC table:\n"); |
| 363 | for (i = 0; i < num; i++) { |
| 364 | printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid); |
| 365 | dump_jid(vtba->entry[i].jid); |
| 366 | printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc); |
| 367 | dump_vscc(vtba->entry[i].vscc); |
| 368 | } |
| 369 | printf("\n"); |
| 370 | } |
| 371 | |
| 372 | static void dump_oem(uint8_t *oem) |
| 373 | { |
| 374 | int i, j; |
| 375 | printf("OEM Section:\n"); |
| 376 | for (i = 0; i < 4; i++) { |
| 377 | printf("%02x:", i << 4); |
| 378 | for (j = 0; j < 16; j++) |
| 379 | printf(" %02x", oem[(i<<4)+j]); |
| 380 | printf("\n"); |
| 381 | } |
| 382 | printf("\n"); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * dump_fd() - Display a dump of the full flash description |
| 387 | * |
| 388 | * @image: Pointer to image |
| 389 | * @size: Size of image in bytes |
| 390 | * @return 0 if OK, -1 on error |
| 391 | */ |
| 392 | static int dump_fd(char *image, int size) |
| 393 | { |
| 394 | struct fdbar_t *fdb = find_fd(image, size); |
| 395 | |
| 396 | if (!fdb) |
| 397 | return -1; |
| 398 | |
| 399 | printf("FLMAP0: 0x%08x\n", fdb->flmap0); |
| 400 | printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7); |
| 401 | printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4); |
| 402 | printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1); |
| 403 | printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4); |
| 404 | |
| 405 | printf("FLMAP1: 0x%08x\n", fdb->flmap1); |
| 406 | printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff); |
| 407 | printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4); |
| 408 | printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3); |
| 409 | printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4); |
| 410 | |
| 411 | printf("FLMAP2: 0x%08x\n", fdb->flmap2); |
| 412 | printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff); |
| 413 | printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4); |
| 414 | |
| 415 | printf("FLUMAP1: 0x%08x\n", fdb->flumap1); |
| 416 | printf(" Intel ME VSCC Table Length (VTL): %d\n", |
| 417 | (fdb->flumap1 >> 8) & 0xff); |
| 418 | printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n", |
| 419 | (fdb->flumap1 & 0xff) << 4); |
| 420 | dump_vtba((struct vtba_t *) |
| 421 | (image + ((fdb->flumap1 & 0xff) << 4)), |
| 422 | (fdb->flumap1 >> 8) & 0xff); |
| 423 | dump_oem((uint8_t *)image + 0xf00); |
| 424 | dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) |
| 425 | << 4))); |
| 426 | dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4))); |
| 427 | dump_fpsba((struct fpsba_t *) |
| 428 | (image + (((fdb->flmap1 >> 16) & 0xff) << 4))); |
| 429 | dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4))); |
| 430 | dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4))); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * write_regions() - Write each region from an image to its own file |
| 437 | * |
| 438 | * The filename to use in each case is fixed - see region_filename() |
| 439 | * |
| 440 | * @image: Pointer to image |
| 441 | * @size: Size of image in bytes |
| 442 | * @return 0 if OK, -ve on error |
| 443 | */ |
| 444 | static int write_regions(char *image, int size) |
| 445 | { |
| 446 | struct fdbar_t *fdb; |
| 447 | struct frba_t *frba; |
| 448 | int ret = 0; |
| 449 | int i; |
| 450 | |
| 451 | fdb = find_fd(image, size); |
| 452 | if (!fdb) |
| 453 | return -1; |
| 454 | |
| 455 | frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4)); |
| 456 | |
| 457 | for (i = 0; i < MAX_REGIONS; i++) { |
| 458 | struct region_t region; |
| 459 | int region_fd; |
| 460 | |
| 461 | ret = get_region(frba, i, ®ion); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | dump_region(i, frba); |
| 465 | if (region.size == 0) |
| 466 | continue; |
| 467 | region_fd = open(region_filename(i), |
| 468 | O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | |
| 469 | S_IWUSR | S_IRGRP | S_IROTH); |
| 470 | if (write(region_fd, image + region.base, region.size) != |
| 471 | region.size) { |
| 472 | perror("Error while writing"); |
| 473 | ret = -1; |
| 474 | } |
| 475 | close(region_fd); |
| 476 | } |
| 477 | |
| 478 | return ret; |
| 479 | } |
| 480 | |
Simon Glass | fa8d3b0 | 2014-12-14 17:15:36 -0700 | [diff] [blame] | 481 | static int perror_fname(const char *fmt, const char *fname) |
| 482 | { |
| 483 | char msg[strlen(fmt) + strlen(fname) + 1]; |
| 484 | |
| 485 | sprintf(msg, fmt, fname); |
| 486 | perror(msg); |
| 487 | |
| 488 | return -1; |
| 489 | } |
| 490 | |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 491 | /** |
| 492 | * write_image() - Write the image to a file |
| 493 | * |
| 494 | * @filename: Filename to use for the image |
| 495 | * @image: Pointer to image |
| 496 | * @size: Size of image in bytes |
| 497 | * @return 0 if OK, -ve on error |
| 498 | */ |
| 499 | static int write_image(char *filename, char *image, int size) |
| 500 | { |
| 501 | int new_fd; |
| 502 | |
| 503 | debug("Writing new image to %s\n", filename); |
| 504 | |
| 505 | new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | |
| 506 | S_IWUSR | S_IRGRP | S_IROTH); |
Simon Glass | fa8d3b0 | 2014-12-14 17:15:36 -0700 | [diff] [blame] | 507 | if (new_fd < 0) |
| 508 | return perror_fname("Could not open file '%s'", filename); |
| 509 | if (write(new_fd, image, size) != size) |
| 510 | return perror_fname("Could not write file '%s'", filename); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 511 | close(new_fd); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * set_spi_frequency() - Set the SPI frequency to use when booting |
| 518 | * |
| 519 | * Several frequencies are supported, some of which work with fast devices. |
| 520 | * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The |
| 521 | * Intel boot system uses this information somehow on boot. |
| 522 | * |
| 523 | * The image is updated with the supplied value |
| 524 | * |
| 525 | * @image: Pointer to image |
| 526 | * @size: Size of image in bytes |
| 527 | * @freq: SPI frequency to use |
| 528 | */ |
| 529 | static void set_spi_frequency(char *image, int size, enum spi_frequency freq) |
| 530 | { |
| 531 | struct fdbar_t *fdb = find_fd(image, size); |
| 532 | struct fcba_t *fcba; |
| 533 | |
| 534 | fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)); |
| 535 | |
| 536 | /* clear bits 21-29 */ |
| 537 | fcba->flcomp &= ~0x3fe00000; |
| 538 | /* Read ID and Read Status Clock Frequency */ |
| 539 | fcba->flcomp |= freq << 27; |
| 540 | /* Write and Erase Clock Frequency */ |
| 541 | fcba->flcomp |= freq << 24; |
| 542 | /* Fast Read Clock Frequency */ |
| 543 | fcba->flcomp |= freq << 21; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100 |
| 548 | * |
| 549 | * @image: Pointer to image |
| 550 | * @size: Size of image in bytes |
| 551 | */ |
| 552 | static void set_em100_mode(char *image, int size) |
| 553 | { |
| 554 | struct fdbar_t *fdb = find_fd(image, size); |
| 555 | struct fcba_t *fcba; |
| 556 | |
| 557 | fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)); |
| 558 | fcba->flcomp &= ~(1 << 30); |
| 559 | set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * lock_descriptor() - Lock the NE descriptor so it cannot be updated |
| 564 | * |
| 565 | * @image: Pointer to image |
| 566 | * @size: Size of image in bytes |
| 567 | */ |
| 568 | static void lock_descriptor(char *image, int size) |
| 569 | { |
| 570 | struct fdbar_t *fdb = find_fd(image, size); |
| 571 | struct fmba_t *fmba; |
| 572 | |
| 573 | /* |
| 574 | * TODO: Dynamically take Platform Data Region and GbE Region into |
| 575 | * account. |
| 576 | */ |
| 577 | fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)); |
| 578 | fmba->flmstr1 = 0x0a0b0000; |
| 579 | fmba->flmstr2 = 0x0c0d0000; |
| 580 | fmba->flmstr3 = 0x08080118; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * unlock_descriptor() - Lock the NE descriptor so it can be updated |
| 585 | * |
| 586 | * @image: Pointer to image |
| 587 | * @size: Size of image in bytes |
| 588 | */ |
| 589 | static void unlock_descriptor(char *image, int size) |
| 590 | { |
| 591 | struct fdbar_t *fdb = find_fd(image, size); |
| 592 | struct fmba_t *fmba; |
| 593 | |
| 594 | fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)); |
| 595 | fmba->flmstr1 = 0xffff0000; |
| 596 | fmba->flmstr2 = 0xffff0000; |
| 597 | fmba->flmstr3 = 0x08080118; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * open_for_read() - Open a file for reading |
| 602 | * |
| 603 | * @fname: Filename to open |
| 604 | * @sizep: Returns file size in bytes |
| 605 | * @return 0 if OK, -1 on error |
| 606 | */ |
| 607 | int open_for_read(const char *fname, int *sizep) |
| 608 | { |
| 609 | int fd = open(fname, O_RDONLY); |
| 610 | struct stat buf; |
| 611 | |
Simon Glass | fa8d3b0 | 2014-12-14 17:15:36 -0700 | [diff] [blame] | 612 | if (fd == -1) |
| 613 | return perror_fname("Could not open file '%s'", fname); |
| 614 | if (fstat(fd, &buf) == -1) |
| 615 | return perror_fname("Could not stat file '%s'", fname); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 616 | *sizep = buf.st_size; |
| 617 | debug("File %s is %d bytes\n", fname, *sizep); |
| 618 | |
| 619 | return fd; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * inject_region() - Add a file to an image region |
| 624 | * |
| 625 | * This puts a file into a particular region of the flash. Several pre-defined |
| 626 | * regions are used. |
| 627 | * |
| 628 | * @image: Pointer to image |
| 629 | * @size: Size of image in bytes |
| 630 | * @region_type: Region where the file should be added |
| 631 | * @region_fname: Filename to add to the image |
| 632 | * @return 0 if OK, -ve on error |
| 633 | */ |
| 634 | int inject_region(char *image, int size, int region_type, char *region_fname) |
| 635 | { |
| 636 | struct fdbar_t *fdb = find_fd(image, size); |
| 637 | struct region_t region; |
| 638 | struct frba_t *frba; |
| 639 | int region_size; |
| 640 | int offset = 0; |
| 641 | int region_fd; |
| 642 | int ret; |
| 643 | |
| 644 | if (!fdb) |
| 645 | exit(EXIT_FAILURE); |
| 646 | frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4)); |
| 647 | |
| 648 | ret = get_region(frba, region_type, ®ion); |
| 649 | if (ret) |
| 650 | return -1; |
| 651 | if (region.size <= 0xfff) { |
| 652 | fprintf(stderr, "Region %s is disabled in target. Not injecting.\n", |
| 653 | region_name(region_type)); |
| 654 | return -1; |
| 655 | } |
| 656 | |
| 657 | region_fd = open_for_read(region_fname, ®ion_size); |
| 658 | if (region_fd < 0) |
| 659 | return region_fd; |
| 660 | |
| 661 | if ((region_size > region.size) || |
| 662 | ((region_type != 1) && (region_size > region.size))) { |
| 663 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n", |
| 664 | region_name(region_type), region.size, |
| 665 | region.size, region_size, region_size); |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | if ((region_type == 1) && (region_size < region.size)) { |
| 670 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n", |
| 671 | region_name(region_type), region.size, |
| 672 | region.size, region_size, region_size); |
| 673 | offset = region.size - region_size; |
| 674 | memset(image + region.base, 0xff, offset); |
| 675 | } |
| 676 | |
| 677 | if (size < region.base + offset + region_size) { |
| 678 | fprintf(stderr, "Output file is too small. (%d < %d)\n", |
| 679 | size, region.base + offset + region_size); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | if (read(region_fd, image + region.base + offset, region_size) |
| 684 | != region_size) { |
| 685 | perror("Could not read file"); |
| 686 | return -1; |
| 687 | } |
| 688 | |
| 689 | close(region_fd); |
| 690 | |
| 691 | debug("Adding %s as the %s section\n", region_fname, |
| 692 | region_name(region_type)); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * write_data() - Write some raw data into a region |
| 699 | * |
| 700 | * This puts a file into a particular place in the flash, ignoring the |
| 701 | * regions. Be careful not to overwrite something important. |
| 702 | * |
| 703 | * @image: Pointer to image |
| 704 | * @size: Size of image in bytes |
| 705 | * @addr: x86 ROM address to put file. The ROM ends at |
| 706 | * 0xffffffff so use an address relative to that. For an |
| 707 | * 8MB ROM the start address is 0xfff80000. |
| 708 | * @write_fname: Filename to add to the image |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 709 | * @return number of bytes written if OK, -ve on error |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 710 | */ |
| 711 | static int write_data(char *image, int size, unsigned int addr, |
| 712 | const char *write_fname) |
| 713 | { |
| 714 | int write_fd, write_size; |
| 715 | int offset; |
| 716 | |
| 717 | write_fd = open_for_read(write_fname, &write_size); |
| 718 | if (write_fd < 0) |
| 719 | return write_fd; |
| 720 | |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 721 | offset = (uint32_t)(addr + size); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 722 | debug("Writing %s to offset %#x\n", write_fname, offset); |
| 723 | |
| 724 | if (offset < 0 || offset + write_size > size) { |
| 725 | fprintf(stderr, "Output file is too small. (%d < %d)\n", |
| 726 | size, offset + write_size); |
| 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | if (read(write_fd, image + offset, write_size) != write_size) { |
| 731 | perror("Could not read file"); |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | close(write_fd); |
| 736 | |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 737 | return write_size; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * write_uboot() - Write U-Boot, device tree and microcode pointer |
| 742 | * |
| 743 | * This writes U-Boot into a place in the flash, followed by its device tree. |
| 744 | * The microcode pointer is written so that U-Boot can find the microcode in |
| 745 | * the device tree very early in boot. |
| 746 | * |
| 747 | * @image: Pointer to image |
| 748 | * @size: Size of image in bytes |
| 749 | * @uboot: Input file information for u-boot.bin |
| 750 | * @fdt: Input file information for u-boot.dtb |
| 751 | * @ucode_ptr: Address in U-Boot where the microcode pointer should be placed |
| 752 | * @return 0 if OK, -ve on error |
| 753 | */ |
| 754 | static int write_uboot(char *image, int size, struct input_file *uboot, |
| 755 | struct input_file *fdt, unsigned int ucode_ptr) |
| 756 | { |
| 757 | const void *blob; |
| 758 | const char *data; |
| 759 | int uboot_size; |
| 760 | uint32_t *ptr; |
| 761 | int data_size; |
| 762 | int offset; |
| 763 | int node; |
| 764 | int ret; |
| 765 | |
| 766 | uboot_size = write_data(image, size, uboot->addr, uboot->fname); |
| 767 | if (uboot_size < 0) |
| 768 | return uboot_size; |
| 769 | fdt->addr = uboot->addr + uboot_size; |
| 770 | debug("U-Boot size %#x, FDT at %#x\n", uboot_size, fdt->addr); |
| 771 | ret = write_data(image, size, fdt->addr, fdt->fname); |
| 772 | if (ret < 0) |
| 773 | return ret; |
| 774 | |
| 775 | if (ucode_ptr) { |
| 776 | blob = (void *)image + (uint32_t)(fdt->addr + size); |
| 777 | debug("DTB at %lx\n", (char *)blob - image); |
| 778 | node = fdt_node_offset_by_compatible(blob, 0, |
| 779 | "intel,microcode"); |
| 780 | if (node < 0) { |
| 781 | debug("No microcode found in FDT: %s\n", |
| 782 | fdt_strerror(node)); |
| 783 | return -ENOENT; |
| 784 | } |
| 785 | data = fdt_getprop(blob, node, "data", &data_size); |
| 786 | if (!data) { |
| 787 | debug("No microcode data found in FDT: %s\n", |
| 788 | fdt_strerror(data_size)); |
| 789 | return -ENOENT; |
| 790 | } |
Simon Glass | 8a38808 | 2015-01-01 16:17:56 -0700 | [diff] [blame] | 791 | offset = (uint32_t)(ucode_ptr + size); |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 792 | ptr = (void *)image + offset; |
Simon Glass | 8a38808 | 2015-01-01 16:17:56 -0700 | [diff] [blame] | 793 | ptr[0] = (data - image) - size; |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 794 | ptr[1] = data_size; |
| 795 | debug("Wrote microcode pointer at %x: addr=%x, size=%x\n", |
| 796 | ucode_ptr, ptr[0], ptr[1]); |
| 797 | } |
| 798 | |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static void print_version(void) |
| 803 | { |
| 804 | printf("ifdtool v%s -- ", IFDTOOL_VERSION); |
| 805 | printf("Copyright (C) 2014 Google Inc.\n\n"); |
| 806 | printf("SPDX-License-Identifier: GPL-2.0+\n"); |
| 807 | } |
| 808 | |
| 809 | static void print_usage(const char *name) |
| 810 | { |
| 811 | printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name); |
| 812 | printf("\n" |
| 813 | " -d | --dump: dump intel firmware descriptor\n" |
| 814 | " -x | --extract: extract intel fd modules\n" |
| 815 | " -i | --inject <region>:<module> inject file <module> into region <region>\n" |
| 816 | " -w | --write <addr>:<file> write file to appear at memory address <addr>\n" |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 817 | " multiple files can be written simultaneously\n" |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 818 | " -s | --spifreq <20|33|50> set the SPI frequency\n" |
| 819 | " -e | --em100 set SPI frequency to 20MHz and disable\n" |
| 820 | " Dual Output Fast Read Support\n" |
| 821 | " -l | --lock Lock firmware descriptor and ME region\n" |
| 822 | " -u | --unlock Unlock firmware descriptor and ME region\n" |
| 823 | " -r | --romsize Specify ROM size\n" |
| 824 | " -D | --write-descriptor <file> Write descriptor at base\n" |
| 825 | " -c | --create Create a new empty image\n" |
| 826 | " -v | --version: print the version\n" |
| 827 | " -h | --help: print this help\n\n" |
| 828 | "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n" |
| 829 | "\n"); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * get_two_words() - Convert a string into two words separated by : |
| 834 | * |
| 835 | * The supplied string is split at ':', two substrings are allocated and |
| 836 | * returned. |
| 837 | * |
| 838 | * @str: String to split |
| 839 | * @firstp: Returns first string |
| 840 | * @secondp: Returns second string |
| 841 | * @return 0 if OK, -ve if @str does not have a : |
| 842 | */ |
| 843 | static int get_two_words(const char *str, char **firstp, char **secondp) |
| 844 | { |
| 845 | const char *p; |
| 846 | |
| 847 | p = strchr(str, ':'); |
| 848 | if (!p) |
| 849 | return -1; |
| 850 | *firstp = strdup(str); |
| 851 | (*firstp)[p - str] = '\0'; |
| 852 | *secondp = strdup(p + 1); |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | int main(int argc, char *argv[]) |
| 858 | { |
| 859 | int opt, option_index = 0; |
| 860 | int mode_dump = 0, mode_extract = 0, mode_inject = 0; |
| 861 | int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0; |
| 862 | int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0; |
| 863 | int create = 0; |
Simon Glass | 7dfb172 | 2014-12-13 22:25:46 -0700 | [diff] [blame] | 864 | char *region_type_string = NULL, *inject_fname = NULL; |
| 865 | char *desc_fname = NULL, *addr_str = NULL; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 866 | int region_type = -1, inputfreq = 0; |
| 867 | enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ; |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 868 | struct input_file input_file[WRITE_MAX], *ifile, *fdt = NULL; |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 869 | unsigned char wr_idx, wr_num = 0; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 870 | int rom_size = -1; |
| 871 | bool write_it; |
| 872 | char *filename; |
| 873 | char *outfile = NULL; |
| 874 | struct stat buf; |
| 875 | int size = 0; |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 876 | unsigned int ucode_ptr = 0; |
| 877 | bool have_uboot = false; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 878 | int bios_fd; |
| 879 | char *image; |
| 880 | int ret; |
| 881 | static struct option long_options[] = { |
| 882 | {"create", 0, NULL, 'c'}, |
| 883 | {"dump", 0, NULL, 'd'}, |
| 884 | {"descriptor", 1, NULL, 'D'}, |
| 885 | {"em100", 0, NULL, 'e'}, |
| 886 | {"extract", 0, NULL, 'x'}, |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 887 | {"fdt", 1, NULL, 'f'}, |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 888 | {"inject", 1, NULL, 'i'}, |
| 889 | {"lock", 0, NULL, 'l'}, |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 890 | {"microcode", 1, NULL, 'm'}, |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 891 | {"romsize", 1, NULL, 'r'}, |
| 892 | {"spifreq", 1, NULL, 's'}, |
| 893 | {"unlock", 0, NULL, 'u'}, |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 894 | {"uboot", 1, NULL, 'U'}, |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 895 | {"write", 1, NULL, 'w'}, |
| 896 | {"version", 0, NULL, 'v'}, |
| 897 | {"help", 0, NULL, 'h'}, |
| 898 | {0, 0, 0, 0} |
| 899 | }; |
| 900 | |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 901 | while ((opt = getopt_long(argc, argv, "cdD:ef:hi:lm:r:s:uU:vw:x?", |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 902 | long_options, &option_index)) != EOF) { |
| 903 | switch (opt) { |
| 904 | case 'c': |
| 905 | create = 1; |
| 906 | break; |
| 907 | case 'd': |
| 908 | mode_dump = 1; |
| 909 | break; |
| 910 | case 'D': |
| 911 | mode_write_descriptor = 1; |
Simon Glass | 7dfb172 | 2014-12-13 22:25:46 -0700 | [diff] [blame] | 912 | desc_fname = optarg; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 913 | break; |
| 914 | case 'e': |
| 915 | mode_em100 = 1; |
| 916 | break; |
| 917 | case 'i': |
| 918 | if (get_two_words(optarg, ®ion_type_string, |
Simon Glass | 7dfb172 | 2014-12-13 22:25:46 -0700 | [diff] [blame] | 919 | &inject_fname)) { |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 920 | print_usage(argv[0]); |
| 921 | exit(EXIT_FAILURE); |
| 922 | } |
| 923 | if (!strcasecmp("Descriptor", region_type_string)) |
| 924 | region_type = 0; |
| 925 | else if (!strcasecmp("BIOS", region_type_string)) |
| 926 | region_type = 1; |
| 927 | else if (!strcasecmp("ME", region_type_string)) |
| 928 | region_type = 2; |
| 929 | else if (!strcasecmp("GbE", region_type_string)) |
| 930 | region_type = 3; |
| 931 | else if (!strcasecmp("Platform", region_type_string)) |
| 932 | region_type = 4; |
| 933 | if (region_type == -1) { |
| 934 | fprintf(stderr, "No such region type: '%s'\n\n", |
| 935 | region_type_string); |
| 936 | print_usage(argv[0]); |
| 937 | exit(EXIT_FAILURE); |
| 938 | } |
| 939 | mode_inject = 1; |
| 940 | break; |
| 941 | case 'l': |
| 942 | mode_locked = 1; |
| 943 | break; |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 944 | case 'm': |
| 945 | ucode_ptr = strtoul(optarg, NULL, 0); |
| 946 | break; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 947 | case 'r': |
| 948 | rom_size = strtol(optarg, NULL, 0); |
| 949 | debug("ROM size %d\n", rom_size); |
| 950 | break; |
| 951 | case 's': |
| 952 | /* Parse the requested SPI frequency */ |
| 953 | inputfreq = strtol(optarg, NULL, 0); |
| 954 | switch (inputfreq) { |
| 955 | case 20: |
| 956 | spifreq = SPI_FREQUENCY_20MHZ; |
| 957 | break; |
| 958 | case 33: |
| 959 | spifreq = SPI_FREQUENCY_33MHZ; |
| 960 | break; |
| 961 | case 50: |
| 962 | spifreq = SPI_FREQUENCY_50MHZ; |
| 963 | break; |
| 964 | default: |
| 965 | fprintf(stderr, "Invalid SPI Frequency: %d\n", |
| 966 | inputfreq); |
| 967 | print_usage(argv[0]); |
| 968 | exit(EXIT_FAILURE); |
| 969 | } |
| 970 | mode_spifreq = 1; |
| 971 | break; |
| 972 | case 'u': |
| 973 | mode_unlocked = 1; |
| 974 | break; |
| 975 | case 'v': |
| 976 | print_version(); |
| 977 | exit(EXIT_SUCCESS); |
| 978 | break; |
| 979 | case 'w': |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 980 | case 'U': |
| 981 | case 'f': |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 982 | ifile = &input_file[wr_num]; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 983 | mode_write = 1; |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 984 | if (wr_num < WRITE_MAX) { |
| 985 | if (get_two_words(optarg, &addr_str, |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 986 | &ifile->fname)) { |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 987 | print_usage(argv[0]); |
| 988 | exit(EXIT_FAILURE); |
| 989 | } |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 990 | ifile->addr = strtol(optarg, NULL, 0); |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 991 | ifile->type = opt == 'f' ? IF_fdt : |
| 992 | opt == 'U' ? IF_uboot : IF_normal; |
| 993 | if (ifile->type == IF_fdt) |
| 994 | fdt = ifile; |
| 995 | else if (ifile->type == IF_uboot) |
| 996 | have_uboot = true; |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 997 | wr_num++; |
| 998 | } else { |
| 999 | fprintf(stderr, |
| 1000 | "The number of files to write simultaneously exceeds the limitation (%d)\n", |
| 1001 | WRITE_MAX); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1002 | } |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1003 | break; |
| 1004 | case 'x': |
| 1005 | mode_extract = 1; |
| 1006 | break; |
| 1007 | case 'h': |
| 1008 | case '?': |
| 1009 | default: |
| 1010 | print_usage(argv[0]); |
| 1011 | exit(EXIT_SUCCESS); |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (mode_locked == 1 && mode_unlocked == 1) { |
| 1017 | fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n"); |
| 1018 | exit(EXIT_FAILURE); |
| 1019 | } |
| 1020 | |
| 1021 | if (mode_inject == 1 && mode_write == 1) { |
| 1022 | fprintf(stderr, "Inject/Write are mutually exclusive\n"); |
| 1023 | exit(EXIT_FAILURE); |
| 1024 | } |
| 1025 | |
| 1026 | if ((mode_dump + mode_extract + mode_inject + |
| 1027 | (mode_spifreq | mode_em100 | mode_unlocked | |
| 1028 | mode_locked)) > 1) { |
| 1029 | fprintf(stderr, "You may not specify more than one mode.\n\n"); |
| 1030 | print_usage(argv[0]); |
| 1031 | exit(EXIT_FAILURE); |
| 1032 | } |
| 1033 | |
| 1034 | if ((mode_dump + mode_extract + mode_inject + mode_spifreq + |
| 1035 | mode_em100 + mode_locked + mode_unlocked + mode_write + |
Simon Glass | c03c951 | 2014-11-12 22:42:06 -0700 | [diff] [blame] | 1036 | mode_write_descriptor) == 0 && !create) { |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1037 | fprintf(stderr, "You need to specify a mode.\n\n"); |
| 1038 | print_usage(argv[0]); |
| 1039 | exit(EXIT_FAILURE); |
| 1040 | } |
| 1041 | |
| 1042 | if (create && rom_size == -1) { |
| 1043 | fprintf(stderr, "You need to specify a rom size when creating.\n\n"); |
| 1044 | exit(EXIT_FAILURE); |
| 1045 | } |
| 1046 | |
| 1047 | if (optind + 1 != argc) { |
| 1048 | fprintf(stderr, "You need to specify a file.\n\n"); |
| 1049 | print_usage(argv[0]); |
| 1050 | exit(EXIT_FAILURE); |
| 1051 | } |
| 1052 | |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 1053 | if (have_uboot && !fdt) { |
| 1054 | fprintf(stderr, |
| 1055 | "You must supply a device tree file for U-Boot\n\n"); |
| 1056 | print_usage(argv[0]); |
| 1057 | exit(EXIT_FAILURE); |
| 1058 | } |
| 1059 | |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1060 | filename = argv[optind]; |
| 1061 | if (optind + 2 != argc) |
| 1062 | outfile = argv[optind + 1]; |
| 1063 | |
| 1064 | if (create) |
| 1065 | bios_fd = open(filename, O_WRONLY | O_CREAT, 0666); |
| 1066 | else |
| 1067 | bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR); |
| 1068 | |
| 1069 | if (bios_fd == -1) { |
| 1070 | perror("Could not open file"); |
| 1071 | exit(EXIT_FAILURE); |
| 1072 | } |
| 1073 | |
| 1074 | if (!create) { |
| 1075 | if (fstat(bios_fd, &buf) == -1) { |
| 1076 | perror("Could not stat file"); |
| 1077 | exit(EXIT_FAILURE); |
| 1078 | } |
| 1079 | size = buf.st_size; |
| 1080 | } |
| 1081 | |
| 1082 | debug("File %s is %d bytes\n", filename, size); |
| 1083 | |
| 1084 | if (rom_size == -1) |
| 1085 | rom_size = size; |
| 1086 | |
| 1087 | image = malloc(rom_size); |
| 1088 | if (!image) { |
| 1089 | printf("Out of memory.\n"); |
| 1090 | exit(EXIT_FAILURE); |
| 1091 | } |
| 1092 | |
| 1093 | memset(image, '\xff', rom_size); |
| 1094 | if (!create && read(bios_fd, image, size) != size) { |
| 1095 | perror("Could not read file"); |
| 1096 | exit(EXIT_FAILURE); |
| 1097 | } |
| 1098 | if (size != rom_size) { |
| 1099 | debug("ROM size changed to %d bytes\n", rom_size); |
| 1100 | size = rom_size; |
| 1101 | } |
| 1102 | |
| 1103 | write_it = true; |
| 1104 | ret = 0; |
| 1105 | if (mode_dump) { |
| 1106 | ret = dump_fd(image, size); |
| 1107 | write_it = false; |
| 1108 | } |
| 1109 | |
| 1110 | if (mode_extract) { |
| 1111 | ret = write_regions(image, size); |
| 1112 | write_it = false; |
| 1113 | } |
| 1114 | |
| 1115 | if (mode_write_descriptor) |
Simon Glass | 7dfb172 | 2014-12-13 22:25:46 -0700 | [diff] [blame] | 1116 | ret = write_data(image, size, -size, desc_fname); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (mode_inject) |
Simon Glass | 7dfb172 | 2014-12-13 22:25:46 -0700 | [diff] [blame] | 1119 | ret = inject_region(image, size, region_type, inject_fname); |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1120 | |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 1121 | if (mode_write) { |
| 1122 | for (wr_idx = 0; wr_idx < wr_num; wr_idx++) { |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 1123 | ifile = &input_file[wr_idx]; |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 1124 | if (ifile->type == IF_fdt) { |
| 1125 | continue; |
| 1126 | } else if (ifile->type == IF_uboot) { |
| 1127 | ret = write_uboot(image, size, ifile, fdt, |
| 1128 | ucode_ptr); |
| 1129 | } else { |
| 1130 | ret = write_data(image, size, ifile->addr, |
Simon Glass | 65851fc | 2014-12-14 17:15:37 -0700 | [diff] [blame] | 1131 | ifile->fname); |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 1132 | } |
| 1133 | if (ret < 0) |
Bin Meng | 673ed2f | 2014-12-12 21:05:20 +0800 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | } |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (mode_spifreq) |
| 1139 | set_spi_frequency(image, size, spifreq); |
| 1140 | |
| 1141 | if (mode_em100) |
| 1142 | set_em100_mode(image, size); |
| 1143 | |
| 1144 | if (mode_locked) |
| 1145 | lock_descriptor(image, size); |
| 1146 | |
| 1147 | if (mode_unlocked) |
| 1148 | unlock_descriptor(image, size); |
| 1149 | |
| 1150 | if (write_it) { |
| 1151 | if (outfile) { |
| 1152 | ret = write_image(outfile, image, size); |
| 1153 | } else { |
| 1154 | if (lseek(bios_fd, 0, SEEK_SET)) { |
| 1155 | perror("Error while seeking"); |
| 1156 | ret = -1; |
| 1157 | } |
| 1158 | if (write(bios_fd, image, size) != size) { |
| 1159 | perror("Error while writing"); |
| 1160 | ret = -1; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | free(image); |
| 1166 | close(bios_fd); |
| 1167 | |
Simon Glass | 31eca69 | 2014-12-15 22:02:38 -0700 | [diff] [blame] | 1168 | return ret < 0 ? 1 : 0; |
Simon Glass | cd392fe | 2014-11-10 18:00:22 -0700 | [diff] [blame] | 1169 | } |