Debian Codegraph @

util/amdfwtool/amdfwread.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #include <getopt.h>
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "amdfwtool.h"
10
11 #define FILE_REL_MASK 0xffffff
12
13 #define ERR(...) fprintf(stderr, __VA_ARGS__)
14
15 enum spi_frequency {
16 SPI_FREQUENCY_66_66MHZ = 0,
17 SPI_FREQUENCY_33_33MHZ = 1,
18 SPI_FREQUENCY_22_22MHZ = 2,
19 SPI_FREQUENCY_16_66MHZ = 3,
20 SPI_FREQUENCY_100MHZ = 4,
21 SPI_FREQUENCY_800KHZ = 5,
22 };
23
24 enum spi_read_mode {
25 SPI_READ_MODE_NORMAL_33 = 0,
26 SPI_READ_MODE_RESERVED = 1,
27 SPI_READ_MODE_DUAL_1_1_2 = 2,
28 SPI_READ_MODE_QUAD_1_1_4 = 3,
29 SPI_READ_MODE_DUAL_1_2_2 = 4,
30 SPI_READ_MODE_QUAD_1_4_4 = 5,
31 SPI_READ_MODE_NORMAL_66 = 6,
32 SPI_READ_MODE_FAST = 7,
33 };
34
35 /* Possible locations for the header */
36 const uint32_t fw_header_offsets[] = {
37 0xfa0000,
38 0xe20000,
39 0xc20000,
40 0x820000,
41 0x020000,
42 };
43
44 /* Converts addresses to be relative to the start of the file */
45 static uint64_t relative_offset(uint32_t header_offset, uint64_t addr, uint64_t mode)
46 {
47 switch (mode) {
48 /* Since this utility operates on the BIOS file, physical address is converted
49 relative to the start of the BIOS file. */
50 case AMD_ADDR_PHYSICAL:
51 if (addr < SPI_ROM_BASE || addr > (SPI_ROM_BASE + FILE_REL_MASK)) {
52 ERR("Invalid address(%lx) or mode(%lx)\n", addr, mode);
53 exit(1);
54 }
55 return addr & FILE_REL_MASK;
56
57 case AMD_ADDR_REL_BIOS:
58 if (addr > FILE_REL_MASK) {
59 ERR("Invalid address(%lx) or mode(%lx)\n", addr, mode);
60 exit(1);
61 }
62 return addr & FILE_REL_MASK;
63
64 case AMD_ADDR_REL_TAB:
65 return addr + header_offset;
66
67 default:
68 ERR("Unsupported mode %lu\n", mode);
69 exit(1);
70 }
71 }
72
73 static int read_header(FILE *fw, uint32_t offset, void *header, size_t header_size)
74 {
75 if (fseek(fw, offset, SEEK_SET) != 0) {
76 ERR("Failed to seek to file offset 0x%x\n", offset);
77 return 1;
78 }
79
80 if (fread(header, header_size, 1, fw) != 1) {
81 ERR("Failed to read header at 0x%x\n", offset);
82 return 1;
83 }
84
85 return 0;
86 }
87
88 static int read_fw_header(FILE *fw, uint32_t offset, embedded_firmware *fw_header)
89 {
90 if (read_header(fw, offset, fw_header, sizeof(embedded_firmware))) {
91 ERR("Failed to read fw header at 0x%x\n", offset);
92 return 1;
93 }
94
95 return fw_header->signature != EMBEDDED_FW_SIGNATURE;
96 }
97
98 static int read_psp_directory(FILE *fw, uint32_t offset, uint32_t expected_cookie,
99 psp_directory_header *header, psp_directory_entry **entries,
100 size_t *num_entries)
101 {
102 offset &= FILE_REL_MASK;
103
104 if (read_header(fw, offset, header, sizeof(psp_directory_header))) {
105 ERR("Failed to read PSP header\n");
106 return 1;
107 }
108
109 /* Ensure that we have a PSP directory */
110 if (header->cookie != expected_cookie) {
111 ERR("Invalid PSP header cookie value found: 0x%x, expected: 0x%x\n",
112 header->cookie, expected_cookie);
113 return 1;
114 }
115
116 /* Read the entries */
117 *num_entries = header->num_entries;
118 *entries = malloc(sizeof(psp_directory_entry) * header->num_entries);
119 if (fread(*entries, sizeof(psp_directory_entry), header->num_entries, fw)
120 != header->num_entries) {
121 ERR("Failed to read %d PSP entries\n", header->num_entries);
122 return 1;
123 }
124
125 return 0;
126 }
127
128 static int read_ish_directory(FILE *fw, uint32_t offset, ish_directory_table *table)
129 {
130 return read_header(fw, offset & FILE_REL_MASK, table, sizeof(*table));
131 }
132
133 static int read_bios_directory(FILE *fw, uint32_t offset, uint32_t expected_cookie,
134 bios_directory_hdr *header, bios_directory_entry **entries,
135 size_t *num_entries)
136 {
137 offset &= FILE_REL_MASK;
138
139 if (read_header(fw, offset, header, sizeof(bios_directory_hdr))) {
140 ERR("Failed to read BIOS header\n");
141 return 1;
142 }
143
144 /* Ensure that we have a BIOS directory */
145 if (header->cookie != expected_cookie) {
146 ERR("Invalid BIOS header cookie value found: 0x%x, expected: 0x%x\n",
147 header->cookie, expected_cookie);
148 return 1;
149 }
150
151 /* Read the entries */
152 *num_entries = header->num_entries;
153 *entries = malloc(sizeof(bios_directory_entry) * header->num_entries);
154 if (fread(*entries, sizeof(bios_directory_entry), header->num_entries, fw)
155 != header->num_entries) {
156 ERR("Failed to read %d BIOS entries\n", header->num_entries);
157 return 1;
158 }
159
160 return 0;
161 }
162
163 static int read_soft_fuse(FILE *fw, const embedded_firmware *fw_header)
164 {
165 psp_directory_entry *current_entries = NULL;
166 size_t num_current_entries = 0;
167
168 uint32_t psp_offset = 0;
169 /* 0xffffffff or 0x00000000 indicates that the offset is in new_psp_directory */
170 if (fw_header->psp_directory != 0xffffffff && fw_header->psp_directory != 0x00000000)
171 psp_offset = fw_header->psp_directory;
172 else
173 psp_offset = fw_header->new_psp_directory;
174
175 psp_directory_header header;
176 if (read_psp_directory(fw, psp_offset, PSP_COOKIE, &header,
177 &current_entries, &num_current_entries) != 0)
178 return 1;
179
180 while (1) {
181 uint32_t l2_dir_offset = 0;
182 uint32_t ish_dir_offset;
183 ish_directory_table ish_dir;
184
185 for (size_t i = 0; i < num_current_entries; i++) {
186 uint32_t type = current_entries[i].type;
187 uint64_t mode = current_entries[i].address_mode;
188 uint64_t addr = current_entries[i].addr;
189 uint64_t fuse;
190
191 switch (type) {
192 case AMD_PSP_FUSE_CHAIN:
193 fuse = mode << 62 | addr;
194
195 printf("Soft-fuse:0x%lx\n", fuse);
196 free(current_entries);
197 return 0;
198
199 case AMD_FW_L2_PTR:
200 /* There's a second level PSP directory to read */
201 if (l2_dir_offset != 0) {
202 ERR("Duplicate PSP L2 Entry, prior offset: %08x\n",
203 l2_dir_offset);
204 free(current_entries);
205 return 1;
206 }
207
208 l2_dir_offset = relative_offset(psp_offset, addr, mode);
209 break;
210
211 case AMD_FW_RECOVERYAB_A:
212 if (l2_dir_offset != 0) {
213 ERR("Duplicate PSP L2 Entry, prior offset: %08x\n",
214 l2_dir_offset);
215 free(current_entries);
216 return 1;
217 }
218
219 ish_dir_offset = relative_offset(psp_offset, addr, mode);
220 if (read_ish_directory(fw, ish_dir_offset, &ish_dir) != 0) {
221 ERR("Error reading ISH directory\n");
222 free(current_entries);
223 return 1;
224 }
225
226 l2_dir_offset = ish_dir.pl2_location;
227 break;
228
229 default:
230 /* No-op, continue to the next entry. */
231 break;
232 }
233 }
234
235 free(current_entries);
236
237 /* Didn't find an L2 PSP directory so we can stop */
238 if (l2_dir_offset == 0)
239 break;
240
241 /* Read the L2 PSP directory */
242 if (read_psp_directory(fw, l2_dir_offset, PSPL2_COOKIE, &header,
243 &current_entries, &num_current_entries) != 0)
244 break;
245 }
246
247 return 1;
248 }
249
250 #define MAX_NUM_LEVELS 10
251 #define MAX_INDENT_PER_LEVEL 4
252 #define MAX_INDENTATION_LEN (MAX_NUM_LEVELS * MAX_INDENT_PER_LEVEL + 1)
253 static void do_indentation_string(char *dest, uint8_t level)
254 {
255 for (uint8_t i = 0; i < level && i < MAX_NUM_LEVELS; i++)
256 strcat(dest, " ");
257 strcat(dest, "+-->");
258 }
259
260 static int amdfw_bios_dir_walk(FILE *fw, uint32_t bios_offset, uint32_t cookie, uint8_t level)
261 {
262 bios_directory_entry *current_entries = NULL;
263 size_t num_current_entries = 0;
264 bios_directory_hdr header;
265 uint32_t l2_dir_offset = 0;
266 uint64_t dir_mode = 0;
267 char indent[MAX_INDENTATION_LEN] = {0};
268
269 if (read_bios_directory(fw, bios_offset, cookie, &header,
270 &current_entries, &num_current_entries) != 0)
271 return 1;
272
273 if (header.additional_info_fields.version == 1)
274 dir_mode = header.additional_info_fields_v1.address_mode;
275 else
276 dir_mode = header.additional_info_fields.address_mode;
277
278 do_indentation_string(indent, level);
279 for (size_t i = 0; i < num_current_entries; i++) {
280 uint32_t type = current_entries[i].type;
281 uint64_t mode = current_entries[i].address_mode;
282 uint64_t addr = current_entries[i].source;
283
284 if (dir_mode < 2)
285 mode = dir_mode;
286
287 if (type == AMD_BIOS_APOB || type == AMD_BIOS_PSP_SHARED_MEM)
288 printf("%sBIOS%s: 0x%02x 0x%lx(DRAM-Address)\n",
289 indent, cookie == BHD_COOKIE ? "L1" : "L2",
290 type, current_entries[i].dest);
291 else
292 printf("%sBIOS%s: 0x%02x 0x%08lx 0x%08x\n",
293 indent, cookie == BHD_COOKIE ? "L1" : "L2",
294 type, relative_offset(bios_offset, addr, mode),
295 current_entries[i].size);
296
297 if (type == AMD_BIOS_L2_PTR) {
298 /* There's a second level BIOS directory to read */
299 if (l2_dir_offset != 0) {
300 ERR("Duplicate BIOS L2 Entry, prior offset: %08x\n",
301 l2_dir_offset);
302 free(current_entries);
303 return 1;
304 }
305
306 l2_dir_offset = relative_offset(bios_offset, addr, mode);
307 printf(" %sBIOSL2: Dir 0x%08x\n", indent, l2_dir_offset);
308 amdfw_bios_dir_walk(fw, l2_dir_offset, BHDL2_COOKIE, level + 2);
309 }
310 }
311
312 free(current_entries);
313 return 0;
314 }
315
316 static int amdfw_psp_dir_walk(FILE *fw, uint32_t psp_offset, uint32_t cookie, uint8_t level)
317 {
318 psp_directory_entry *current_entries = NULL;
319 size_t num_current_entries = 0;
320 psp_directory_header header;
321 uint32_t l2_dir_offset = 0;
322 uint32_t bios_dir_offset = 0;
323 uint32_t ish_dir_offset = 0;
324 ish_directory_table ish_dir;
325 uint64_t dir_mode = 0;
326 char indent[MAX_INDENTATION_LEN] = {0};
327
328 if (read_psp_directory(fw, psp_offset, cookie, &header,
329 &current_entries, &num_current_entries) != 0)
330 return 1;
331
332 if (header.additional_info_fields.version == 1)
333 dir_mode = header.additional_info_fields_v1.address_mode;
334 else
335 dir_mode = header.additional_info_fields.address_mode;
336
337 do_indentation_string(indent, level);
338 for (size_t i = 0; i < num_current_entries; i++) {
339 uint32_t type = current_entries[i].type;
340 uint64_t mode = current_entries[i].address_mode;
341 uint64_t addr = current_entries[i].addr;
342
343 if (dir_mode < 2)
344 mode = dir_mode;
345
346 if (type == AMD_PSP_FUSE_CHAIN)
347 printf("%sPSP%s: 0x%02x 0x%lx(Soft-fuse)\n",
348 indent, cookie == PSP_COOKIE ? "L1" : "L2",
349 type, (uint64_t)current_entries[i].address_mode << 62 | addr);
350 else
351 printf("%sPSP%s: 0x%02x 0x%08lx 0x%08x\n",
352 indent, cookie == PSP_COOKIE ? "L1" : "L2",
353 type, relative_offset(psp_offset, addr, mode),
354 current_entries[i].size);
355
356 switch (type) {
357 case AMD_FW_L2_PTR:
358 /* There's a second level PSP directory to read */
359 if (l2_dir_offset != 0) {
360 ERR("Duplicate PSP L2 Entry, prior offset: %08x\n",
361 l2_dir_offset);
362 free(current_entries);
363 return 1;
364 }
365
366 l2_dir_offset = relative_offset(psp_offset, addr, mode);
367 printf(" %sPSPL2: Dir 0x%08x\n", indent, l2_dir_offset);
368 amdfw_psp_dir_walk(fw, l2_dir_offset, PSPL2_COOKIE, level + 2);
369 break;
370
371 case AMD_FW_RECOVERYAB_A:
372 if (l2_dir_offset != 0) {
373 ERR("Duplicate PSP L2 Entry, prior offset: %08x\n",
374 l2_dir_offset);
375 free(current_entries);
376 return 1;
377 }
378
379 ish_dir_offset = relative_offset(psp_offset, addr, mode);
380 if (read_ish_directory(fw, ish_dir_offset, &ish_dir) != 0) {
381 ERR("Error reading ISH directory\n");
382 free(current_entries);
383 return 1;
384 }
385
386 l2_dir_offset = ish_dir.pl2_location;
387 printf(" %sPSPL2: Dir 0x%08x\n", indent, l2_dir_offset);
388 amdfw_psp_dir_walk(fw, l2_dir_offset, PSPL2_COOKIE, level + 2);
389 break;
390
391 case AMD_FW_BIOS_TABLE:
392 bios_dir_offset = relative_offset(psp_offset, addr, mode);
393 printf(" %sBIOSL2: Dir 0x%08x\n", indent, bios_dir_offset);
394 amdfw_bios_dir_walk(fw, bios_dir_offset, BHDL2_COOKIE, level + 2);
395 break;
396
397 default:
398 /* No additional processing required, continue to the next entry. */
399 break;
400 }
401 }
402
403 free(current_entries);
404 return 0;
405 }
406
407 static int list_amdfw_psp_dir(FILE *fw, const embedded_firmware *fw_header)
408 {
409 uint32_t psp_offset = 0;
410
411 /* 0xffffffff or 0x00000000 indicates that the offset is in new_psp_directory */
412 if (fw_header->psp_directory != 0xffffffff && fw_header->psp_directory != 0x00000000)
413 psp_offset = fw_header->psp_directory;
414 else
415 psp_offset = fw_header->new_psp_directory;
416
417 printf("PSPL1: Dir 0x%08x\n", psp_offset);
418 amdfw_psp_dir_walk(fw, psp_offset, PSP_COOKIE, 0);
419 return 0;
420 }
421
422 static int list_amdfw_bios_dir(FILE *fw, const embedded_firmware *fw_header)
423 {
424 /* 0xffffffff or 0x00000000 implies that the SoC uses recovery A/B
425 layout. Only BIOS L2 directory is present and that too as part of
426 PSP L2 directory. */
427 if (fw_header->bios3_entry != 0xffffffff && fw_header->bios3_entry != 0x00000000) {
428 printf("BIOSL1: Dir 0x%08x\n", fw_header->bios3_entry);
429 amdfw_bios_dir_walk(fw, fw_header->bios3_entry, BHD_COOKIE, 0);
430 }
431 return 0;
432 }
433
434
435 static int list_amdfw_ro(FILE *fw, const embedded_firmware *fw_header)
436 {
437 printf("Table: FW Offset Size\n");
438 list_amdfw_psp_dir(fw, fw_header);
439 list_amdfw_bios_dir(fw, fw_header);
440 return 0;
441 }
442
443 static void decode_spi_frequency(unsigned int freq)
444 {
445 switch (freq) {
446 case SPI_FREQUENCY_66_66MHZ:
447 printf("66.66MHz");
448 break;
449 case SPI_FREQUENCY_33_33MHZ:
450 printf("33.33MHz");
451 break;
452 case SPI_FREQUENCY_22_22MHZ:
453 printf("22.22MHz");
454 break;
455 case SPI_FREQUENCY_16_66MHZ:
456 printf("16.66MHz");
457 break;
458 case SPI_FREQUENCY_100MHZ:
459 printf("100MHz");
460 break;
461 case SPI_FREQUENCY_800KHZ:
462 printf("800kHz");
463 break;
464 default:
465 printf("unknown<%x>MHz", freq);
466 }
467 }
468
469 static void decode_spi_read_mode(unsigned int mode)
470 {
471 switch (mode) {
472 case SPI_READ_MODE_NORMAL_33:
473 printf("Normal read (up to 33M)");
474 break;
475 case SPI_READ_MODE_RESERVED:
476 printf("Reserved");
477 break;
478 case SPI_READ_MODE_DUAL_1_1_2:
479 printf("Dual IO (1-1-2)");
480 break;
481 case SPI_READ_MODE_QUAD_1_1_4:
482 printf("Quad IO (1-1-4)");
483 break;
484 case SPI_READ_MODE_DUAL_1_2_2:
485 printf("Dual IO (1-2-2)");
486 break;
487 case SPI_READ_MODE_QUAD_1_4_4:
488 printf("Quad IO (1-4-4)");
489 break;
490 case SPI_READ_MODE_NORMAL_66:
491 printf("Normal read (up to 66M)");
492 break;
493 case SPI_READ_MODE_FAST:
494 printf("Fast Read");
495 break;
496 default:
497 printf("unknown<%x>mode", mode);
498 }
499 }
500
501 static int dump_efw(const embedded_firmware *fw_header)
502 {
503 printf("EFS Generation: %s\n", fw_header->efs_gen.gen ? "first" : "second");
504
505 printf("\nFamily 15h Models 60h-6Fh");
506 printf("\n SPI Read Mode ");
507 decode_spi_read_mode(fw_header->spi_readmode_f15_mod_60_6f);
508 printf("\n SPI Frequency: ");
509 decode_spi_frequency(fw_header->fast_speed_new_f15_mod_60_6f);
510
511 printf("\n\nFamily 17h Models 00h-0Fh, 10h-1Fh");
512 printf("\n SPI Read Mode: ");
513 decode_spi_read_mode(fw_header->spi_readmode_f17_mod_00_2f);
514 printf("\n Fast Speed New: ");
515 decode_spi_frequency(fw_header->spi_fastspeed_f17_mod_00_2f);
516 printf("\n QPR_Dummy Cycle configure: 0x%02x\n", fw_header->qpr_dummy_cycle_f17_mod_00_2f);
517
518 printf("\nFamily 17h Models 30h-3Fh and later Families");
519 printf("\n SPI Read Mode: ");
520 decode_spi_read_mode(fw_header->spi_readmode_f17_mod_30_3f);
521 printf("\n SPI Fast Speed: ");
522 decode_spi_frequency(fw_header->spi_fastspeed_f17_mod_30_3f);
523 printf("\n Micron Detect Flag: 0x%02x\n", fw_header->micron_detect_f17_mod_30_3f);
524 return 0;
525 }
526
527 enum {
528 AMDFW_OPT_HELP = 'h',
529 AMDFW_OPT_DUMP = 'd',
530 AMDFW_OPT_SOFT_FUSE = 1UL << 0, /* Print Softfuse */
531 AMDFW_OPT_RO_LIST = 1UL << 1, /* List entries in AMDFW RO */
532 };
533
534 static const char optstring[] = {AMDFW_OPT_HELP, AMDFW_OPT_DUMP};
535
536 static struct option long_options[] = {
537 {"help", no_argument, 0, AMDFW_OPT_HELP},
538 {"dump", no_argument, 0, AMDFW_OPT_DUMP},
539 {"soft-fuse", no_argument, 0, AMDFW_OPT_SOFT_FUSE},
540 {"ro-list", no_argument, 0, AMDFW_OPT_RO_LIST},
541 };
542
543 static void print_usage(void)
544 {
545 printf("amdfwread: Examine AMD firmware images\n");
546 printf("Usage: amdfwread [options] <file>\n");
547 printf("-d | --dump Dump Embedded Firmware Structure\n");
548 printf("--soft-fuse Print soft fuse value\n");
549 printf("--ro-list List the programs under AMDFW in RO region\n");
550 }
551
552 int main(int argc, char **argv)
553 {
554 char *fw_file = NULL;
555 int mode_dump = 0;
556
557 int selected_functions = 0;
558 while (1) {
559 int opt = getopt_long(argc, argv, optstring, long_options, NULL);
560
561 if (opt == -1) {
562 if (optind != (argc - 1)) {
563 /* Print usage if one and only one option i.e. filename is
564 not found. */
565 print_usage();
566 return 0;
567 }
568
569 fw_file = argv[optind];
570 break;
571 }
572
573 switch (opt) {
574 case AMDFW_OPT_HELP:
575 print_usage();
576 return 0;
577 case AMDFW_OPT_DUMP:
578 mode_dump = 1;
579 break;
580
581 case AMDFW_OPT_SOFT_FUSE:
582 case AMDFW_OPT_RO_LIST:
583 selected_functions |= opt;
584 break;
585
586 default:
587 break;
588 }
589 }
590
591 FILE *fw = fopen(fw_file, "rb");
592 if (!fw) {
593 ERR("Failed to open FW file %s\n", fw_file);
594 return 1;
595 }
596
597 /* Find the FW header by checking each possible location */
598 embedded_firmware fw_header;
599 int found_header = 0;
600 for (size_t i = 0; i < ARRAY_SIZE(fw_header_offsets); i++) {
601 if (read_fw_header(fw, fw_header_offsets[i], &fw_header) == 0) {
602 found_header = 1;
603 break;
604 }
605 }
606
607 if (!found_header) {
608 ERR("Failed to find FW header\n");
609 fclose(fw);
610 return 1;
611 }
612
613 if (mode_dump)
614 dump_efw(&fw_header);
615
616 if (selected_functions & AMDFW_OPT_SOFT_FUSE) {
617 if (read_soft_fuse(fw, &fw_header) != 0) {
618 fclose(fw);
619 return 1;
620 }
621 }
622
623 if (selected_functions & AMDFW_OPT_RO_LIST) {
624 if (list_amdfw_ro(fw, &fw_header) != 0) {
625 fclose(fw);
626 return 1;
627 }
628 }
629
630 fclose(fw);
631 return 0;
632 }