Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 2 | |
| 3 | /* |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 4 | * ROMSIG At ROMBASE + 0x[0,2,4,8]20000: |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 5 | * 0 4 8 C |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 6 | * +------------+---------------+----------------+------------+ |
| 7 | * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM | |
| 8 | * +------------+---------------+----------------+------------+ |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 9 | * | PSPDIR ADDR|PSPDIR ADDR(C) | BDT ADDR 0 | BDT ADDR 1 | |
| 10 | * +------------+---------------+----------------+------------+ |
| 11 | * | BDT ADDR 2 | | BDT ADDR 3(C) | | |
| 12 | * +------------+---------------+----------------+------------+ |
| 13 | * (C): Could be a combo header |
| 14 | * |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 15 | * EC ROM should be 64K aligned. |
| 16 | * |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 17 | * PSP directory (Where "PSPDIR ADDR" points) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 18 | * +------------+---------------+----------------+------------+ |
| 19 | * | 'PSP$' | Fletcher | Count | Reserved | |
| 20 | * +------------+---------------+----------------+------------+ |
| 21 | * | 0 | size | Base address | Reserved | Pubkey |
| 22 | * +------------+---------------+----------------+------------+ |
| 23 | * | 1 | size | Base address | Reserved | Bootloader |
| 24 | * +------------+---------------+----------------+------------+ |
| 25 | * | 8 | size | Base address | Reserved | Smu Firmware |
| 26 | * +------------+---------------+----------------+------------+ |
| 27 | * | 3 | size | Base address | Reserved | Recovery Firmware |
| 28 | * +------------+---------------+----------------+------------+ |
| 29 | * | | |
| 30 | * | | |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 31 | * | Other PSP Firmware | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 32 | * | | |
| 33 | * +------------+---------------+----------------+------------+ |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 34 | * | 40 | size | Base address | Reserved |---+ |
| 35 | * +------------+---------------+----------------+------------+ | |
| 36 | * :or 48(A/B A): size : Base address : Reserved : | |
| 37 | * + - - + - - + - - + - - + | |
| 38 | * : 4A(A/B B): size : Base address : Reserved : | |
| 39 | * +------------+---------------+----------------+------------+ | |
| 40 | * (A/B A) & (A/B B): Similar as 40, pointing to PSP level 2 | |
| 41 | * for A/B recovery | |
| 42 | * | |
| 43 | * | |
| 44 | * +------------+---------------+----------------+------------+ | |
| 45 | * | '2LP$' | Fletcher | Count | Reserved |<--+ |
| 46 | * +------------+---------------+----------------+------------+ |
| 47 | * | | |
| 48 | * | | |
| 49 | * | PSP Firmware | |
| 50 | * | (2nd-level is not required on all families) | |
| 51 | * | | |
| 52 | * +------------+---------------+----------------+------------+ |
| 53 | * BIOS Directory Table (BDT) is similar |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 54 | * |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 55 | * PSP Combo directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 56 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 57 | * | 'PSP2' | Fletcher | Count |Look up mode| |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 58 | * +------------+---------------+----------------+------------+ |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 59 | * | R e s e r v e d | |
| 60 | * +------------+---------------+----------------+------------+ |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 61 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 1st PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 62 | * +------------+---------------+----------------+------------+ |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 63 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 64 | * +------------+---------------+----------------+------------+ |
| 65 | * | | |
| 66 | * | Other PSP | |
| 67 | * | | |
| 68 | * +------------+---------------+----------------+------------+ |
Zheng Bao | dd4c542 | 2021-10-14 16:14:09 +0800 | [diff] [blame] | 69 | * BDT Combo is similar |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 70 | */ |
| 71 | |
Elyes Haouas | 7d67a19 | 2022-10-14 09:58:29 +0200 | [diff] [blame] | 72 | #include <commonlib/bsd/helpers.h> |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 73 | #include <fcntl.h> |
| 74 | #include <errno.h> |
Karthikeyan Ramasubramanian | ecb4e31 | 2022-11-02 16:53:54 -0600 | [diff] [blame] | 75 | #include <limits.h> |
Martin Roth | 37305e7 | 2020-04-07 14:16:39 -0600 | [diff] [blame] | 76 | #include <stdbool.h> |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 77 | #include <stdio.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <sys/types.h> |
| 80 | #include <unistd.h> |
| 81 | #include <string.h> |
| 82 | #include <stdlib.h> |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 83 | #include <libgen.h> |
Idwer Vollering | 93df1d9 | 2020-12-30 00:01:59 +0100 | [diff] [blame] | 84 | #include <stdint.h> |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 85 | |
| 86 | #include "amdfwtool.h" |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 87 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 88 | #define AMD_ROMSIG_OFFSET 0x20000 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 89 | |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 90 | #define _MAX(A, B) (((A) > (B)) ? (A) : (B)) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 91 | |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 92 | static void output_manifest(int manifest_fd, amd_fw_entry *fw_entry); |
| 93 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 94 | /* |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 95 | * Beginning with Family 15h Models 70h-7F, a.k.a Stoney Ridge, the PSP |
| 96 | * can support an optional "combo" implementation. If the PSP sees the |
| 97 | * PSP2 cookie, it interprets the table as a roadmap to additional PSP |
| 98 | * tables. Using this, support for multiple product generations may be |
| 99 | * built into one image. If the PSP$ cookie is found, the table is a |
| 100 | * normal directory table. |
| 101 | * |
| 102 | * Modern generations supporting the combo directories require the |
| 103 | * pointer to be at offset 0x14 of the Embedded Firmware Structure, |
Zheng Bao | c91867a | 2023-02-26 12:31:31 +0800 | [diff] [blame] | 104 | * regardless of the type of directory used. The --use-combo |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 105 | * argument enforces this placement. |
| 106 | * |
| 107 | * TODO: Future work may require fully implementing the PSP_COMBO feature. |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 108 | */ |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 109 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 110 | /* |
| 111 | * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3. |
| 112 | * The checksum field of the passed PDU does not need to be reset to zero. |
| 113 | * |
| 114 | * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of |
| 115 | * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an |
| 116 | * alternative to cyclical redundancy checks because it provides error- |
| 117 | * detection properties similar to cyclical redundancy checks but at the |
| 118 | * cost of a simple summation technique. Its characteristics were first |
| 119 | * published in IEEE Transactions on Communications in January 1982. One |
| 120 | * version has been adopted by ISO for use in the class-4 transport layer |
| 121 | * of the network protocol. |
| 122 | * |
| 123 | * This program expects: |
| 124 | * stdin: The input file to compute a checksum for. The input file |
| 125 | * not be longer than 256 bytes. |
| 126 | * stdout: Copied from the input file with the Fletcher's Checksum |
| 127 | * inserted 8 bytes after the beginning of the file. |
| 128 | * stderr: Used to print out error messages. |
| 129 | */ |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 130 | static uint32_t fletcher32(const void *data, int length) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 131 | { |
| 132 | uint32_t c0; |
| 133 | uint32_t c1; |
| 134 | uint32_t checksum; |
| 135 | int index; |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 136 | const uint16_t *pptr = data; |
| 137 | |
| 138 | length /= 2; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 139 | |
| 140 | c0 = 0xFFFF; |
| 141 | c1 = 0xFFFF; |
| 142 | |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 143 | while (length) { |
| 144 | index = length >= 359 ? 359 : length; |
| 145 | length -= index; |
Zheng Bao | c88f2b5 | 2021-10-14 16:15:11 +0800 | [diff] [blame] | 146 | do { |
| 147 | c0 += *(pptr++); |
| 148 | c1 += c0; |
| 149 | } while (--index); |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 150 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 151 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 154 | /* Sums[0,1] mod 64K + overflow */ |
| 155 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 156 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 157 | checksum = (c1 << 16) | c0; |
| 158 | |
| 159 | return checksum; |
| 160 | } |
| 161 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 162 | amd_fw_entry amd_psp_fw_table[] = { |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 163 | { .type = AMD_FW_PSP_PUBKEY, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true }, |
Matt DeVillier | fdb4503 | 2023-07-31 20:08:43 +0000 | [diff] [blame] | 164 | { .type = AMD_FW_PSP_BOOTLOADER, .level = PSP_BOTH | PSP_LVL2_AB, |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 165 | .generate_manifest = true }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 166 | { .type = AMD_FW_PSP_SECURED_OS, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 167 | { .type = AMD_FW_PSP_RECOVERY, .level = PSP_LVL1 }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 168 | { .type = AMD_FW_PSP_NVRAM, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 169 | { .type = AMD_FW_PSP_RTM_PUBKEY, .level = PSP_BOTH }, |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 170 | { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB, |
| 171 | .generate_manifest = true }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 172 | { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 173 | { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 2, .level = PSP_BOTH | PSP_LVL2_AB }, |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 174 | { .type = AMD_FW_PSP_SECURED_DEBUG, .level = PSP_LVL2 | PSP_LVL2_AB, |
| 175 | .skip_hashing = true }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 176 | { .type = AMD_FW_ABL_PUBKEY, .level = PSP_BOTH | PSP_BOTH_AB }, |
Matt DeVillier | fdb4503 | 2023-07-31 20:08:43 +0000 | [diff] [blame] | 177 | { .type = AMD_PSP_FUSE_CHAIN, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 178 | { .type = AMD_FW_PSP_TRUSTLETS, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 179 | { .type = AMD_FW_PSP_TRUSTLETKEY, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Matt DeVillier | fdb4503 | 2023-07-31 20:08:43 +0000 | [diff] [blame] | 180 | { .type = AMD_FW_PSP_SMU_FIRMWARE2, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 181 | { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 182 | { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 2, .level = PSP_BOTH | PSP_LVL2_AB }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 183 | { .type = AMD_BOOT_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 184 | { .type = AMD_SOC_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 185 | { .type = AMD_DEBUG_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 186 | { .type = AMD_INTERFACE_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB }, |
Matt DeVillier | fdb4503 | 2023-07-31 20:08:43 +0000 | [diff] [blame] | 187 | { .type = AMD_DEBUG_UNLOCK, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Zheng Bao | 9bb62cb | 2023-03-07 19:48:11 +0800 | [diff] [blame] | 188 | { .type = AMD_HW_IPCFG, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 189 | { .type = AMD_HW_IPCFG, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 190 | { .type = AMD_WRAPPED_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 191 | { .type = AMD_TOKEN_UNLOCK, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 192 | { .type = AMD_SEC_GASKET, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 193 | { .type = AMD_SEC_GASKET, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 194 | { .type = AMD_SEC_GASKET, .subprog = 2, .level = PSP_BOTH | PSP_LVL2_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 195 | { .type = AMD_MP2_FW, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 196 | { .type = AMD_MP2_FW, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 197 | { .type = AMD_MP2_FW, .subprog = 2, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 198 | { .type = AMD_DRIVER_ENTRIES, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 199 | { .type = AMD_FW_KVM_IMAGE, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 200 | { .type = AMD_FW_MP5, .subprog = 0, .level = PSP_BOTH | PSP_BOTH_AB }, |
| 201 | { .type = AMD_FW_MP5, .subprog = 1, .level = PSP_BOTH | PSP_BOTH_AB }, |
| 202 | { .type = AMD_FW_MP5, .subprog = 2, .level = PSP_BOTH | PSP_BOTH_AB }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 203 | { .type = AMD_S0I3_DRIVER, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 204 | { .type = AMD_ABL0, .level = PSP_BOTH | PSP_LVL2_AB, |
| 205 | .generate_manifest = true }, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 206 | { .type = AMD_ABL1, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 207 | { .type = AMD_ABL2, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 208 | { .type = AMD_ABL3, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 209 | { .type = AMD_ABL4, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 210 | { .type = AMD_ABL5, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 211 | { .type = AMD_ABL6, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 212 | { .type = AMD_ABL7, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 213 | { .type = AMD_SEV_DATA, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 214 | { .type = AMD_SEV_CODE, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Karthikeyan Ramasubramanian | 51f914d | 2022-07-28 16:42:12 -0600 | [diff] [blame] | 215 | { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 216 | { .type = AMD_VBIOS_BTLOADER, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 217 | { .type = AMD_FW_DXIO, .level = PSP_BOTH | PSP_BOTH_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 218 | { .type = AMD_FW_USB_PHY, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 219 | { .type = AMD_FW_TOS_SEC_POLICY, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 220 | { .type = AMD_FW_DRTM_TA, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 221 | { .type = AMD_FW_KEYDB_BL, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 222 | { .type = AMD_FW_KEYDB_TOS, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Karthikeyan Ramasubramanian | 234e370 | 2022-07-25 09:49:24 -0600 | [diff] [blame] | 223 | { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 224 | { .type = AMD_FW_VERSTAGE_SIG, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 225 | { .type = AMD_RPMC_NVRAM, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Matt DeVillier | fdb4503 | 2023-07-31 20:08:43 +0000 | [diff] [blame] | 226 | { .type = AMD_FW_SPL, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 227 | { .type = AMD_FW_DMCU_ERAM, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 228 | { .type = AMD_FW_DMCU_ISR, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 229 | { .type = AMD_FW_MSMU, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 230 | { .type = AMD_FW_SPIROM_CFG, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Nikolai Vyssotski | bfc9ca7 | 2023-03-07 15:09:09 -0600 | [diff] [blame] | 231 | { .type = AMD_FW_MPIO, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 232 | { .type = AMD_FW_PSP_SMUSCS, .level = PSP_BOTH | PSP_LVL2_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 233 | { .type = AMD_FW_DMCUB, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 234 | { .type = AMD_FW_PSP_BOOTLOADER_AB, .level = PSP_LVL2 | PSP_LVL2_AB, |
| 235 | .generate_manifest = true }, |
Zheng Bao | 85ee1fd | 2023-01-30 13:52:30 +0800 | [diff] [blame] | 236 | { .type = AMD_RIB, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 237 | { .type = AMD_RIB, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 238 | { .type = AMD_FW_MPDMA_TF, .level = PSP_BOTH | PSP_BOTH_AB }, |
Arthur Heymans | aafbe13 | 2022-09-30 08:33:28 +0200 | [diff] [blame] | 239 | { .type = AMD_TA_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true }, |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 240 | { .type = AMD_FW_GMI3_PHY, .level = PSP_BOTH | PSP_BOTH_AB }, |
| 241 | { .type = AMD_FW_MPDMA_PM, .level = PSP_BOTH | PSP_BOTH_AB }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 242 | { .type = AMD_FW_AMF_SRAM, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 243 | { .type = AMD_FW_AMF_DRAM, .inst = 0, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 244 | { .type = AMD_FW_AMF_DRAM, .inst = 1, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 245 | { .type = AMD_FW_FCFG_TABLE, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 246 | { .type = AMD_FW_AMF_WLAN, .inst = 0, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 247 | { .type = AMD_FW_AMF_WLAN, .inst = 1, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 248 | { .type = AMD_FW_AMF_MFD, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 249 | { .type = AMD_TA_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true }, |
| 250 | { .type = AMD_FW_MPCCX, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 251 | { .type = AMD_FW_LSDMA, .level = PSP_LVL2 | PSP_LVL2_AB }, |
| 252 | { .type = AMD_FW_C20_MP, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 253 | { .type = AMD_FW_MINIMSMU, .inst = 0, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 254 | { .type = AMD_FW_MINIMSMU, .inst = 1, .level = PSP_BOTH | PSP_LVL2_AB }, |
| 255 | { .type = AMD_FW_SRAM_FW_EXT, .level = PSP_LVL2 | PSP_LVL2_AB }, |
Fred Reitberger | c4f3a33 | 2023-02-07 12:12:40 -0500 | [diff] [blame] | 256 | { .type = AMD_FW_UMSMU, .level = PSP_LVL2 | PSP_LVL2_AB }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 257 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 258 | }; |
| 259 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 260 | amd_fw_entry amd_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 261 | { .type = AMD_FW_XHCI }, |
| 262 | { .type = AMD_FW_IMC }, |
| 263 | { .type = AMD_FW_GEC }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 264 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 265 | }; |
| 266 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 267 | amd_bios_entry amd_bios_table[] = { |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 268 | { .type = AMD_BIOS_RTM_PUBKEY, .inst = 0, .level = BDT_BOTH }, |
Ritul Guru | 9a321f3 | 2022-07-29 11:06:40 +0530 | [diff] [blame] | 269 | { .type = AMD_BIOS_SIG, .inst = 0, .level = BDT_BOTH }, |
Marshall Dawson | 0581bf6 | 2019-09-25 11:03:53 -0600 | [diff] [blame] | 270 | { .type = AMD_BIOS_APCB, .inst = 0, .level = BDT_BOTH }, |
| 271 | { .type = AMD_BIOS_APCB, .inst = 1, .level = BDT_BOTH }, |
| 272 | { .type = AMD_BIOS_APCB, .inst = 2, .level = BDT_BOTH }, |
| 273 | { .type = AMD_BIOS_APCB, .inst = 3, .level = BDT_BOTH }, |
| 274 | { .type = AMD_BIOS_APCB, .inst = 4, .level = BDT_BOTH }, |
Rob Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 275 | { .type = AMD_BIOS_APCB, .inst = 5, .level = BDT_BOTH }, |
| 276 | { .type = AMD_BIOS_APCB, .inst = 6, .level = BDT_BOTH }, |
| 277 | { .type = AMD_BIOS_APCB, .inst = 7, .level = BDT_BOTH }, |
| 278 | { .type = AMD_BIOS_APCB, .inst = 8, .level = BDT_BOTH }, |
| 279 | { .type = AMD_BIOS_APCB, .inst = 9, .level = BDT_BOTH }, |
| 280 | { .type = AMD_BIOS_APCB, .inst = 10, .level = BDT_BOTH }, |
| 281 | { .type = AMD_BIOS_APCB, .inst = 11, .level = BDT_BOTH }, |
| 282 | { .type = AMD_BIOS_APCB, .inst = 12, .level = BDT_BOTH }, |
| 283 | { .type = AMD_BIOS_APCB, .inst = 13, .level = BDT_BOTH }, |
| 284 | { .type = AMD_BIOS_APCB, .inst = 14, .level = BDT_BOTH }, |
| 285 | { .type = AMD_BIOS_APCB, .inst = 15, .level = BDT_BOTH }, |
Marshall Dawson | 2dd3b5c | 2020-01-03 17:57:48 -0700 | [diff] [blame] | 286 | { .type = AMD_BIOS_APCB_BK, .inst = 0, .level = BDT_BOTH }, |
| 287 | { .type = AMD_BIOS_APCB_BK, .inst = 1, .level = BDT_BOTH }, |
| 288 | { .type = AMD_BIOS_APCB_BK, .inst = 2, .level = BDT_BOTH }, |
| 289 | { .type = AMD_BIOS_APCB_BK, .inst = 3, .level = BDT_BOTH }, |
| 290 | { .type = AMD_BIOS_APCB_BK, .inst = 4, .level = BDT_BOTH }, |
Rob Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 291 | { .type = AMD_BIOS_APCB_BK, .inst = 5, .level = BDT_BOTH }, |
| 292 | { .type = AMD_BIOS_APCB_BK, .inst = 6, .level = BDT_BOTH }, |
| 293 | { .type = AMD_BIOS_APCB_BK, .inst = 7, .level = BDT_BOTH }, |
| 294 | { .type = AMD_BIOS_APCB_BK, .inst = 8, .level = BDT_BOTH }, |
| 295 | { .type = AMD_BIOS_APCB_BK, .inst = 9, .level = BDT_BOTH }, |
| 296 | { .type = AMD_BIOS_APCB_BK, .inst = 10, .level = BDT_BOTH }, |
| 297 | { .type = AMD_BIOS_APCB_BK, .inst = 11, .level = BDT_BOTH }, |
| 298 | { .type = AMD_BIOS_APCB_BK, .inst = 12, .level = BDT_BOTH }, |
| 299 | { .type = AMD_BIOS_APCB_BK, .inst = 13, .level = BDT_BOTH }, |
| 300 | { .type = AMD_BIOS_APCB_BK, .inst = 14, .level = BDT_BOTH }, |
| 301 | { .type = AMD_BIOS_APCB_BK, .inst = 15, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 302 | { .type = AMD_BIOS_APOB, .level = BDT_BOTH }, |
| 303 | { .type = AMD_BIOS_BIN, |
Zheng Bao | 3d426f3 | 2022-10-16 20:34:57 +0800 | [diff] [blame] | 304 | .reset = 1, .copy = 1, .zlib = 1, .inst = 0, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 305 | { .type = AMD_BIOS_APOB_NV, .level = BDT_LVL2 }, |
| 306 | { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 0, .level = BDT_BOTH }, |
| 307 | { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 0, .level = BDT_BOTH }, |
Zheng Bao | e220faa | 2022-02-17 17:22:15 +0800 | [diff] [blame] | 308 | { .type = AMD_BIOS_PMUI, .inst = 2, .subpr = 0, .level = BDT_BOTH }, |
| 309 | { .type = AMD_BIOS_PMUD, .inst = 2, .subpr = 0, .level = BDT_BOTH }, |
Arthur Heymans | a83c502 | 2022-10-04 20:37:10 +0200 | [diff] [blame] | 310 | { .type = AMD_BIOS_PMUI, .inst = 3, .subpr = 0, .level = BDT_BOTH }, |
| 311 | { .type = AMD_BIOS_PMUD, .inst = 3, .subpr = 0, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 312 | { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 0, .level = BDT_BOTH }, |
| 313 | { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 0, .level = BDT_BOTH }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 314 | { .type = AMD_BIOS_PMUI, .inst = 5, .subpr = 0, .level = BDT_BOTH }, |
| 315 | { .type = AMD_BIOS_PMUD, .inst = 5, .subpr = 0, .level = BDT_BOTH }, |
| 316 | { .type = AMD_BIOS_PMUI, .inst = 6, .subpr = 0, .level = BDT_BOTH }, |
| 317 | { .type = AMD_BIOS_PMUD, .inst = 6, .subpr = 0, .level = BDT_BOTH }, |
| 318 | { .type = AMD_BIOS_PMUI, .inst = 7, .subpr = 0, .level = BDT_BOTH }, |
| 319 | { .type = AMD_BIOS_PMUD, .inst = 7, .subpr = 0, .level = BDT_BOTH }, |
Arthur Heymans | a83c502 | 2022-10-04 20:37:10 +0200 | [diff] [blame] | 320 | { .type = AMD_BIOS_PMUI, .inst = 9, .subpr = 0, .level = BDT_BOTH }, |
| 321 | { .type = AMD_BIOS_PMUD, .inst = 9, .subpr = 0, .level = BDT_BOTH }, |
| 322 | { .type = AMD_BIOS_PMUI, .inst = 10, .subpr = 0, .level = BDT_BOTH }, |
| 323 | { .type = AMD_BIOS_PMUD, .inst = 10, .subpr = 0, .level = BDT_BOTH }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 324 | { .type = AMD_BIOS_PMUI, .inst = 11, .subpr = 0, .level = BDT_BOTH }, |
| 325 | { .type = AMD_BIOS_PMUD, .inst = 11, .subpr = 0, .level = BDT_BOTH }, |
| 326 | { .type = AMD_BIOS_PMUI, .inst = 12, .subpr = 0, .level = BDT_BOTH }, |
| 327 | { .type = AMD_BIOS_PMUD, .inst = 12, .subpr = 0, .level = BDT_BOTH }, |
| 328 | { .type = AMD_BIOS_PMUI, .inst = 13, .subpr = 0, .level = BDT_BOTH }, |
| 329 | { .type = AMD_BIOS_PMUD, .inst = 13, .subpr = 0, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 330 | { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 1, .level = BDT_BOTH }, |
| 331 | { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 1, .level = BDT_BOTH }, |
Zheng Bao | e220faa | 2022-02-17 17:22:15 +0800 | [diff] [blame] | 332 | { .type = AMD_BIOS_PMUI, .inst = 2, .subpr = 1, .level = BDT_BOTH }, |
| 333 | { .type = AMD_BIOS_PMUD, .inst = 2, .subpr = 1, .level = BDT_BOTH }, |
Arthur Heymans | a83c502 | 2022-10-04 20:37:10 +0200 | [diff] [blame] | 334 | { .type = AMD_BIOS_PMUI, .inst = 3, .subpr = 1, .level = BDT_BOTH }, |
| 335 | { .type = AMD_BIOS_PMUD, .inst = 3, .subpr = 1, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 336 | { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 1, .level = BDT_BOTH }, |
| 337 | { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 1, .level = BDT_BOTH }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 338 | { .type = AMD_BIOS_PMUI, .inst = 5, .subpr = 1, .level = BDT_BOTH }, |
| 339 | { .type = AMD_BIOS_PMUD, .inst = 5, .subpr = 1, .level = BDT_BOTH }, |
| 340 | { .type = AMD_BIOS_PMUI, .inst = 6, .subpr = 1, .level = BDT_BOTH }, |
| 341 | { .type = AMD_BIOS_PMUD, .inst = 6, .subpr = 1, .level = BDT_BOTH }, |
| 342 | { .type = AMD_BIOS_PMUI, .inst = 7, .subpr = 1, .level = BDT_BOTH }, |
| 343 | { .type = AMD_BIOS_PMUD, .inst = 7, .subpr = 1, .level = BDT_BOTH }, |
Arthur Heymans | a83c502 | 2022-10-04 20:37:10 +0200 | [diff] [blame] | 344 | { .type = AMD_BIOS_PMUI, .inst = 9, .subpr = 1, .level = BDT_BOTH }, |
| 345 | { .type = AMD_BIOS_PMUD, .inst = 9, .subpr = 1, .level = BDT_BOTH }, |
| 346 | { .type = AMD_BIOS_PMUI, .inst = 10, .subpr = 1, .level = BDT_BOTH }, |
| 347 | { .type = AMD_BIOS_PMUD, .inst = 10, .subpr = 1, .level = BDT_BOTH }, |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 348 | { .type = AMD_BIOS_PMUI, .inst = 11, .subpr = 1, .level = BDT_BOTH }, |
| 349 | { .type = AMD_BIOS_PMUD, .inst = 11, .subpr = 1, .level = BDT_BOTH }, |
| 350 | { .type = AMD_BIOS_PMUI, .inst = 12, .subpr = 1, .level = BDT_BOTH }, |
| 351 | { .type = AMD_BIOS_PMUD, .inst = 12, .subpr = 1, .level = BDT_BOTH }, |
| 352 | { .type = AMD_BIOS_PMUI, .inst = 13, .subpr = 1, .level = BDT_BOTH }, |
| 353 | { .type = AMD_BIOS_PMUD, .inst = 13, .subpr = 1, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 354 | { .type = AMD_BIOS_UCODE, .inst = 0, .level = BDT_LVL2 }, |
| 355 | { .type = AMD_BIOS_UCODE, .inst = 1, .level = BDT_LVL2 }, |
| 356 | { .type = AMD_BIOS_UCODE, .inst = 2, .level = BDT_LVL2 }, |
Arthur Heymans | a83c502 | 2022-10-04 20:37:10 +0200 | [diff] [blame] | 357 | { .type = AMD_BIOS_UCODE, .inst = 3, .level = BDT_LVL2 }, |
| 358 | { .type = AMD_BIOS_UCODE, .inst = 4, .level = BDT_LVL2 }, |
| 359 | { .type = AMD_BIOS_UCODE, .inst = 5, .level = BDT_LVL2 }, |
| 360 | { .type = AMD_BIOS_UCODE, .inst = 6, .level = BDT_LVL2 }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 361 | { .type = AMD_BIOS_MP2_CFG, .level = BDT_LVL2 }, |
Martin Roth | 9455474 | 2020-04-14 14:59:36 -0600 | [diff] [blame] | 362 | { .type = AMD_BIOS_PSP_SHARED_MEM, .inst = 0, .level = BDT_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 363 | { .type = AMD_BIOS_INVALID }, |
| 364 | }; |
| 365 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 366 | #define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 367 | #define RUN_OFFSET_MODE(ctx, offset, mode) \ |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 368 | ((mode) == AMD_ADDR_PHYSICAL ? RUN_BASE(ctx) + (offset) : \ |
| 369 | ((mode) == AMD_ADDR_REL_BIOS ? (offset) : \ |
Zheng Bao | c38f764 | 2023-02-21 10:43:08 +0800 | [diff] [blame] | 370 | ((mode) == AMD_ADDR_REL_TAB ? (offset) - (ctx).current_table : (offset)))) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 371 | #define RUN_OFFSET(ctx, offset) RUN_OFFSET_MODE((ctx), (offset), (ctx).address_mode) |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 372 | #define RUN_TO_OFFSET(ctx, run) ((ctx).address_mode == AMD_ADDR_PHYSICAL ? \ |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 373 | (run) - RUN_BASE(ctx) : (run)) /* TODO: */ |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 374 | #define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 375 | /* The mode in entry can not be higher than the header's. |
| 376 | For example, if table mode is 0, all the entry mode will be 0. */ |
| 377 | #define RUN_CURRENT_MODE(ctx, mode) RUN_OFFSET_MODE((ctx), (ctx).current, \ |
| 378 | (ctx).address_mode < (mode) ? (ctx).address_mode : (mode)) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 379 | #define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset))) |
| 380 | #define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current) |
| 381 | #define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom)) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 382 | #define BUFF_TO_RUN_MODE(ctx, ptr, mode) RUN_OFFSET_MODE((ctx), ((char *)(ptr) - (ctx).rom), \ |
| 383 | (ctx).address_mode < (mode) ? (ctx).address_mode : (mode)) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 384 | #define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 385 | /* Only set the address mode in entry if the table is mode 2. */ |
| 386 | #define SET_ADDR_MODE(table, mode) \ |
| 387 | ((table)->header.additional_info_fields.address_mode == \ |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 388 | AMD_ADDR_REL_TAB ? (mode) : 0) |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 389 | #define SET_ADDR_MODE_BY_TABLE(table) \ |
| 390 | SET_ADDR_MODE((table), (table)->header.additional_info_fields.address_mode) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 391 | |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 392 | |
| 393 | static void free_psp_firmware_filenames(amd_fw_entry *fw_table) |
| 394 | { |
| 395 | amd_fw_entry *index; |
| 396 | |
| 397 | for (index = fw_table; index->type != AMD_FW_INVALID; index++) { |
| 398 | if (index->filename && |
| 399 | index->type != AMD_FW_VERSTAGE_SIG && |
| 400 | index->type != AMD_FW_PSP_VERSTAGE && |
| 401 | index->type != AMD_FW_SPL && |
| 402 | index->type != AMD_FW_PSP_WHITELIST) { |
| 403 | free(index->filename); |
| 404 | index->filename = NULL; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | static void free_bdt_firmware_filenames(amd_bios_entry *fw_table) |
| 410 | { |
| 411 | amd_bios_entry *index; |
| 412 | |
| 413 | for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) { |
| 414 | if (index->filename && |
| 415 | index->type != AMD_BIOS_APCB && |
| 416 | index->type != AMD_BIOS_BIN && |
| 417 | index->type != AMD_BIOS_APCB_BK && |
| 418 | index->type != AMD_BIOS_UCODE) { |
| 419 | free(index->filename); |
| 420 | index->filename = NULL; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void amdfwtool_cleanup(context *ctx) |
| 426 | { |
| 427 | free(ctx->rom); |
| 428 | ctx->rom = NULL; |
| 429 | |
| 430 | /* Free the filename. */ |
| 431 | free_psp_firmware_filenames(amd_psp_fw_table); |
| 432 | free_bdt_firmware_filenames(amd_bios_table); |
Zheng Bao | 8dd34bd | 2023-03-09 21:09:58 +0800 | [diff] [blame] | 433 | |
| 434 | free(ctx->amd_psp_fw_table_clean); |
| 435 | ctx->amd_psp_fw_table_clean = NULL; |
| 436 | free(ctx->amd_bios_table_clean); |
| 437 | ctx->amd_bios_table_clean = NULL; |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Zheng Bao | 5164e4b | 2021-10-30 12:09:07 +0800 | [diff] [blame] | 440 | void assert_fw_entry(uint32_t count, uint32_t max, context *ctx) |
| 441 | { |
| 442 | if (count >= max) { |
| 443 | fprintf(stderr, "Error: BIOS entries (%d) exceeds max allowed items " |
| 444 | "(%d)\n", count, max); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 445 | amdfwtool_cleanup(ctx); |
Zheng Bao | 5164e4b | 2021-10-30 12:09:07 +0800 | [diff] [blame] | 446 | exit(1); |
| 447 | } |
| 448 | } |
| 449 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 450 | static void set_current_pointer(context *ctx, uint32_t value) |
| 451 | { |
| 452 | if (ctx->current_pointer_saved != 0xFFFFFFFF && |
| 453 | ctx->current_pointer_saved != ctx->current) { |
| 454 | fprintf(stderr, "Error: The pointer is changed elsewhere\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 455 | amdfwtool_cleanup(ctx); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 456 | exit(1); |
| 457 | } |
| 458 | |
| 459 | ctx->current = value; |
| 460 | |
| 461 | if (ctx->current > ctx->rom_size) { |
| 462 | fprintf(stderr, "Error: Packing data causes overflow\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 463 | amdfwtool_cleanup(ctx); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 464 | exit(1); |
| 465 | } |
| 466 | |
| 467 | ctx->current_pointer_saved = ctx->current; |
| 468 | } |
| 469 | |
| 470 | static void adjust_current_pointer(context *ctx, uint32_t add, uint32_t align) |
| 471 | { |
| 472 | /* Get */ |
| 473 | set_current_pointer(ctx, ALIGN_UP(ctx->current + add, align)); |
| 474 | } |
| 475 | |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 476 | static void *new_psp_dir(context *ctx, int multi) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 477 | { |
| 478 | void *ptr; |
| 479 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 480 | /* |
| 481 | * Force both onto boundary when multi. Primary table is after |
| 482 | * updatable table, so alignment ensures primary can stay intact |
| 483 | * if secondary is reprogrammed. |
| 484 | */ |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 485 | if (multi) |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 486 | adjust_current_pointer(ctx, 0, TABLE_ERASE_ALIGNMENT); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 487 | else |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 488 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 489 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 490 | ptr = BUFF_CURRENT(*ctx); |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 491 | ((psp_directory_header *)ptr)->num_entries = 0; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 492 | ((psp_directory_header *)ptr)->additional_info = 0; |
| 493 | ((psp_directory_header *)ptr)->additional_info_fields.address_mode = ctx->address_mode; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 494 | adjust_current_pointer(ctx, |
| 495 | sizeof(psp_directory_header) + MAX_PSP_ENTRIES * sizeof(psp_directory_entry), |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 496 | 1); |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 497 | return ptr; |
| 498 | } |
| 499 | |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 500 | static void *new_ish_dir(context *ctx) |
| 501 | { |
| 502 | void *ptr; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 503 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 504 | ptr = BUFF_CURRENT(*ctx); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 505 | adjust_current_pointer(ctx, TABLE_ALIGNMENT, 1); |
| 506 | |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 507 | return ptr; |
| 508 | } |
| 509 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 510 | static void *new_combo_dir(context *ctx) |
| 511 | { |
| 512 | void *ptr; |
| 513 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 514 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 515 | ptr = BUFF_CURRENT(*ctx); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 516 | adjust_current_pointer(ctx, |
| 517 | sizeof(psp_combo_header) + MAX_COMBO_ENTRIES * sizeof(psp_combo_entry), |
| 518 | 1); |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 519 | return ptr; |
| 520 | } |
| 521 | |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 522 | static void fill_dir_header(void *directory, uint32_t count, uint32_t cookie, context *ctx) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 523 | { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 524 | psp_combo_directory *cdir = directory; |
| 525 | psp_directory_table *dir = directory; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 526 | bios_directory_table *bdir = directory; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 527 | uint32_t table_size = 0; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 528 | |
Zheng Bao | b035f58 | 2021-05-27 11:26:12 +0800 | [diff] [blame] | 529 | if (ctx == NULL || directory == NULL) { |
| 530 | fprintf(stderr, "Calling %s with NULL pointers\n", __func__); |
| 531 | return; |
| 532 | } |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 533 | |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 534 | /* The table size needs to be 0x1000 aligned. So align the end of table. */ |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 535 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 536 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 537 | switch (cookie) { |
| 538 | case PSP2_COOKIE: |
Zheng Bao | 84fb9ea | 2022-08-18 15:54:47 +0800 | [diff] [blame] | 539 | case BHD2_COOKIE: |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 540 | cdir->header.cookie = cookie; |
Zheng Bao | fd51af6 | 2022-08-18 15:26:39 +0800 | [diff] [blame] | 541 | /* lookup mode is hardcoded for now. */ |
| 542 | cdir->header.lookup = 1; |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 543 | cdir->header.num_entries = count; |
| 544 | cdir->header.reserved[0] = 0; |
| 545 | cdir->header.reserved[1] = 0; |
| 546 | /* checksum everything that comes after the Checksum field */ |
| 547 | cdir->header.checksum = fletcher32(&cdir->header.num_entries, |
| 548 | count * sizeof(psp_combo_entry) |
| 549 | + sizeof(cdir->header.num_entries) |
| 550 | + sizeof(cdir->header.lookup) |
| 551 | + 2 * sizeof(cdir->header.reserved[0])); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 552 | break; |
| 553 | case PSP_COOKIE: |
| 554 | case PSPL2_COOKIE: |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 555 | /* The table size is only set once. Later calls only update |
| 556 | * the count and fletcher. So does the BIOS table. */ |
| 557 | if (dir->header.additional_info_fields.dir_size == 0) { |
Zheng Bao | fc3fcf2 | 2023-10-12 11:28:05 +0800 | [diff] [blame] | 558 | table_size = ctx->current - ctx->current_table; |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 559 | if ((table_size % TABLE_ALIGNMENT) != 0 && |
| 560 | (table_size / TABLE_ALIGNMENT) != 0) { |
| 561 | fprintf(stderr, "The PSP table size should be 4K aligned\n"); |
| 562 | amdfwtool_cleanup(ctx); |
| 563 | exit(1); |
| 564 | } |
| 565 | dir->header.additional_info_fields.dir_size = |
| 566 | table_size / TABLE_ALIGNMENT; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 567 | } |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 568 | dir->header.cookie = cookie; |
| 569 | dir->header.num_entries = count; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 570 | dir->header.additional_info_fields.spi_block_size = 1; |
| 571 | dir->header.additional_info_fields.base_addr = 0; |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 572 | /* checksum everything that comes after the Checksum field */ |
| 573 | dir->header.checksum = fletcher32(&dir->header.num_entries, |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 574 | count * sizeof(psp_directory_entry) |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 575 | + sizeof(dir->header.num_entries) |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 576 | + sizeof(dir->header.additional_info)); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 577 | break; |
Zheng Bao | 96a3371 | 2021-06-11 15:54:40 +0800 | [diff] [blame] | 578 | case BHD_COOKIE: |
| 579 | case BHDL2_COOKIE: |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 580 | if (bdir->header.additional_info_fields.dir_size == 0) { |
| 581 | table_size = ctx->current - ctx->current_table; |
| 582 | if ((table_size % TABLE_ALIGNMENT) != 0 && |
| 583 | table_size / TABLE_ALIGNMENT != 0) { |
| 584 | fprintf(stderr, "The BIOS table size should be 4K aligned\n"); |
| 585 | amdfwtool_cleanup(ctx); |
| 586 | exit(1); |
| 587 | } |
| 588 | bdir->header.additional_info_fields.dir_size = |
| 589 | table_size / TABLE_ALIGNMENT; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 590 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 591 | bdir->header.cookie = cookie; |
| 592 | bdir->header.num_entries = count; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 593 | bdir->header.additional_info_fields.spi_block_size = 1; |
| 594 | bdir->header.additional_info_fields.base_addr = 0; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 595 | /* checksum everything that comes after the Checksum field */ |
| 596 | bdir->header.checksum = fletcher32(&bdir->header.num_entries, |
| 597 | count * sizeof(bios_directory_entry) |
| 598 | + sizeof(bdir->header.num_entries) |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 599 | + sizeof(bdir->header.additional_info)); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 600 | break; |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 601 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 602 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 603 | } |
| 604 | |
Zheng Bao | 2f6b7d5 | 2023-02-11 22:27:49 +0800 | [diff] [blame] | 605 | static void fill_psp_directory_to_efs(embedded_firmware *amd_romsig, void *pspdir, |
| 606 | context *ctx, amd_cb_config *cb_config) |
| 607 | { |
| 608 | switch (cb_config->soc_id) { |
| 609 | case PLATFORM_UNKNOWN: |
| 610 | amd_romsig->psp_directory = |
| 611 | BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS); |
| 612 | break; |
| 613 | case PLATFORM_CEZANNE: |
| 614 | case PLATFORM_MENDOCINO: |
| 615 | case PLATFORM_PHOENIX: |
| 616 | case PLATFORM_GLINDA: |
| 617 | case PLATFORM_CARRIZO: |
| 618 | case PLATFORM_STONEYRIDGE: |
| 619 | case PLATFORM_RAVEN: |
| 620 | case PLATFORM_PICASSO: |
| 621 | case PLATFORM_LUCIENNE: |
| 622 | case PLATFORM_RENOIR: |
Arthur Heymans | 563f7af | 2023-07-13 11:40:08 +0200 | [diff] [blame] | 623 | case PLATFORM_GENOA: |
Zheng Bao | 2f6b7d5 | 2023-02-11 22:27:49 +0800 | [diff] [blame] | 624 | default: |
| 625 | /* for combo, it is also combo_psp_directory */ |
| 626 | amd_romsig->new_psp_directory = |
| 627 | BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS); |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static void fill_bios_directory_to_efs(embedded_firmware *amd_romsig, void *biosdir, |
| 633 | context *ctx, amd_cb_config *cb_config) |
| 634 | { |
| 635 | switch (cb_config->soc_id) { |
| 636 | case PLATFORM_RENOIR: |
| 637 | case PLATFORM_LUCIENNE: |
| 638 | case PLATFORM_CEZANNE: |
Arthur Heymans | 563f7af | 2023-07-13 11:40:08 +0200 | [diff] [blame] | 639 | case PLATFORM_GENOA: |
Zheng Bao | 2f6b7d5 | 2023-02-11 22:27:49 +0800 | [diff] [blame] | 640 | if (!cb_config->recovery_ab) |
| 641 | amd_romsig->bios3_entry = |
| 642 | BUFF_TO_RUN_MODE(*ctx, biosdir, AMD_ADDR_REL_BIOS); |
| 643 | break; |
| 644 | case PLATFORM_MENDOCINO: |
| 645 | case PLATFORM_PHOENIX: |
| 646 | case PLATFORM_GLINDA: |
| 647 | break; |
| 648 | case PLATFORM_CARRIZO: |
| 649 | case PLATFORM_STONEYRIDGE: |
| 650 | case PLATFORM_RAVEN: |
| 651 | case PLATFORM_PICASSO: |
| 652 | default: |
| 653 | amd_romsig->bios1_entry = |
| 654 | BUFF_TO_RUN_MODE(*ctx, biosdir, AMD_ADDR_REL_BIOS); |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
Zheng Bao | eb0404e | 2021-10-14 15:09:09 +0800 | [diff] [blame] | 659 | static uint32_t get_psp_id(enum platform soc_id) |
| 660 | { |
| 661 | uint32_t psp_id; |
| 662 | switch (soc_id) { |
| 663 | case PLATFORM_RAVEN: |
| 664 | case PLATFORM_PICASSO: |
| 665 | psp_id = 0xBC0A0000; |
| 666 | break; |
| 667 | case PLATFORM_RENOIR: |
| 668 | case PLATFORM_LUCIENNE: |
| 669 | psp_id = 0xBC0C0000; |
| 670 | break; |
| 671 | case PLATFORM_CEZANNE: |
| 672 | psp_id = 0xBC0C0140; |
| 673 | break; |
| 674 | case PLATFORM_MENDOCINO: |
| 675 | psp_id = 0xBC0D0900; |
| 676 | break; |
| 677 | case PLATFORM_STONEYRIDGE: |
| 678 | psp_id = 0x10220B00; |
| 679 | break; |
Zheng Bao | de6f198 | 2022-10-16 20:32:43 +0800 | [diff] [blame] | 680 | case PLATFORM_GLINDA: |
| 681 | psp_id = 0xBC0E0200; |
| 682 | break; |
| 683 | case PLATFORM_PHOENIX: |
| 684 | psp_id = 0xBC0D0400; |
| 685 | break; |
Arthur Heymans | 563f7af | 2023-07-13 11:40:08 +0200 | [diff] [blame] | 686 | case PLATFORM_GENOA: |
| 687 | psp_id = 0xBC0C0111; |
| 688 | break; |
Zheng Bao | 3d7623f | 2022-08-17 11:52:30 +0800 | [diff] [blame] | 689 | case PLATFORM_CARRIZO: |
Zheng Bao | eb0404e | 2021-10-14 15:09:09 +0800 | [diff] [blame] | 690 | default: |
| 691 | psp_id = 0; |
| 692 | break; |
| 693 | } |
| 694 | return psp_id; |
| 695 | } |
| 696 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 697 | static void integrate_firmwares(context *ctx, |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 698 | embedded_firmware *romsig, |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 699 | amd_fw_entry *fw_table) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 700 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 701 | ssize_t bytes; |
Zheng Bao | 6d402ac | 2020-10-01 16:16:30 +0800 | [diff] [blame] | 702 | uint32_t i; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 703 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 704 | adjust_current_pointer(ctx, 0, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 705 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 706 | for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 707 | if (fw_table[i].filename != NULL) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 708 | switch (fw_table[i].type) { |
| 709 | case AMD_FW_IMC: |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 710 | adjust_current_pointer(ctx, 0, 0x10000U); |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 711 | romsig->imc_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 712 | break; |
| 713 | case AMD_FW_GEC: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 714 | romsig->gec_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 715 | break; |
| 716 | case AMD_FW_XHCI: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 717 | romsig->xhci_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 718 | break; |
| 719 | default: |
| 720 | /* Error */ |
| 721 | break; |
| 722 | } |
| 723 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 724 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 725 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
Marshall Dawson | 02bd773 | 2019-03-13 14:43:17 -0600 | [diff] [blame] | 726 | if (bytes < 0) { |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 727 | amdfwtool_cleanup(ctx); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 728 | exit(1); |
| 729 | } |
| 730 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 731 | adjust_current_pointer(ctx, bytes, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 732 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 733 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 734 | } |
| 735 | |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 736 | static void output_manifest(int manifest_fd, amd_fw_entry *fw_entry) |
| 737 | { |
| 738 | struct amd_fw_header hdr; |
| 739 | int blob_fd; |
| 740 | ssize_t bytes; |
| 741 | |
| 742 | blob_fd = open(fw_entry->filename, O_RDONLY); |
| 743 | if (blob_fd < 0) { |
| 744 | fprintf(stderr, "Error opening file: %s: %s\n", |
| 745 | fw_entry->filename, strerror(errno)); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | bytes = read(blob_fd, &hdr, sizeof(hdr)); |
| 750 | if (bytes != sizeof(hdr)) { |
| 751 | close(blob_fd); |
| 752 | fprintf(stderr, "Error while reading %s\n", fw_entry->filename); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | dprintf(manifest_fd, "type: 0x%02x ver:%02x.%02x.%02x.%02x\n", |
| 757 | fw_entry->type, hdr.version[3], hdr.version[2], |
| 758 | hdr.version[1], hdr.version[0]); |
| 759 | |
| 760 | close(blob_fd); |
| 761 | |
| 762 | } |
| 763 | |
| 764 | static void dump_blob_version(char *manifest_file, amd_fw_entry *fw_table) |
| 765 | { |
| 766 | amd_fw_entry *index; |
| 767 | int manifest_fd; |
| 768 | |
| 769 | manifest_fd = open(manifest_file, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 770 | if (manifest_fd < 0) { |
| 771 | fprintf(stderr, "Error opening file: %s: %s\n", |
| 772 | manifest_file, strerror(errno)); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | for (index = fw_table; index->type != AMD_FW_INVALID; index++) { |
| 777 | if (!(index->filename)) |
| 778 | continue; |
| 779 | |
| 780 | if (index->generate_manifest == true) |
| 781 | output_manifest(manifest_fd, index); |
| 782 | } |
| 783 | |
| 784 | close(manifest_fd); |
| 785 | } |
| 786 | |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 787 | /* For debugging */ |
| 788 | static void dump_psp_firmwares(amd_fw_entry *fw_table) |
| 789 | { |
| 790 | amd_fw_entry *index; |
| 791 | |
Zheng Bao | 2951447 | 2023-08-25 19:12:47 +0800 | [diff] [blame] | 792 | printf("PSP firmware components:\n"); |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 793 | for (index = fw_table; index->type != AMD_FW_INVALID; index++) { |
Zheng Bao | 730c3ba | 2023-08-25 19:20:37 +0800 | [diff] [blame] | 794 | if (index->type == AMD_PSP_FUSE_CHAIN) |
| 795 | printf(" %2x: level=%x, subprog=%x, inst=%x\n", |
| 796 | index->type, index->level, index->subprog, index->inst); |
| 797 | else if (index->filename) |
Zheng Bao | 2951447 | 2023-08-25 19:12:47 +0800 | [diff] [blame] | 798 | printf(" %2x: level=%x, subprog=%x, inst=%x, %s\n", |
| 799 | index->type, index->level, index->subprog, index->inst, |
| 800 | index->filename); |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
| 804 | static void dump_bdt_firmwares(amd_bios_entry *fw_table) |
| 805 | { |
| 806 | amd_bios_entry *index; |
| 807 | |
Zheng Bao | 2951447 | 2023-08-25 19:12:47 +0800 | [diff] [blame] | 808 | printf("BIOS Directory Table (BDT) components:\n"); |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 809 | for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) { |
| 810 | if (index->filename) |
Zheng Bao | 2951447 | 2023-08-25 19:12:47 +0800 | [diff] [blame] | 811 | printf(" %2x: level=%x, %s\n", |
| 812 | index->type, index->level, index->filename); |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 816 | static void dump_image_addresses(context *ctx) |
| 817 | { |
| 818 | printf("romsig offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->amd_romsig_ptr)); |
| 819 | printf("PSP L1 offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir)); |
| 820 | if (ctx->pspdir2 != NULL) |
| 821 | printf("PSP L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir2)); |
| 822 | if (ctx->ish_a_dir != NULL) |
| 823 | printf("ISHA offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->ish_a_dir)); |
| 824 | if (ctx->ish_b_dir != NULL) |
| 825 | printf("ISHB offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->ish_b_dir)); |
| 826 | if (ctx->pspdir2_b != NULL) |
| 827 | printf("PSP L2B offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir2_b)); |
| 828 | if (ctx->biosdir != NULL) |
| 829 | printf("BHD offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir)); |
| 830 | if (ctx->biosdir2 != NULL) |
| 831 | printf("BHD L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir2)); |
| 832 | if (ctx->biosdir2_b != NULL) |
| 833 | printf("BHD L2B offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir2_b)); |
| 834 | if (ctx->psp_combo_dir != NULL) |
| 835 | printf("PSP combo offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->psp_combo_dir)); |
| 836 | if (ctx->bhd_combo_dir != NULL) |
| 837 | printf("BHD combo offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->bhd_combo_dir)); |
| 838 | } |
| 839 | |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 840 | static void integrate_psp_ab(context *ctx, psp_directory_table *pspdir, |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 841 | psp_directory_table *pspdir2, ish_directory_table *ish, |
| 842 | amd_fw_type ab, enum platform soc_id) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 843 | { |
| 844 | uint32_t count; |
| 845 | uint32_t current_table_save; |
| 846 | |
| 847 | current_table_save = ctx->current_table; |
| 848 | ctx->current_table = (char *)pspdir - ctx->rom; |
| 849 | count = pspdir->header.num_entries; |
| 850 | assert_fw_entry(count, MAX_PSP_ENTRIES, ctx); |
| 851 | pspdir->entries[count].type = (uint8_t)ab; |
| 852 | pspdir->entries[count].subprog = 0; |
| 853 | pspdir->entries[count].rsvd = 0; |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 854 | if (ish != NULL) { |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 855 | ish->pl2_location = BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 856 | ish->boot_priority = ab == AMD_FW_RECOVERYAB_A ? 0xFFFFFFFF : 1; |
| 857 | ish->update_retry_count = 2; |
| 858 | ish->glitch_retry_count = 0; |
| 859 | ish->psp_id = get_psp_id(soc_id); |
| 860 | ish->checksum = fletcher32(&ish->boot_priority, |
| 861 | sizeof(ish_directory_table) - sizeof(uint32_t)); |
| 862 | pspdir->entries[count].addr = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 863 | BUFF_TO_RUN_MODE(*ctx, ish, AMD_ADDR_REL_BIOS); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 864 | pspdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 865 | SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 866 | pspdir->entries[count].size = TABLE_ALIGNMENT; |
| 867 | } else { |
| 868 | pspdir->entries[count].addr = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 869 | BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 870 | pspdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 871 | SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS); |
Zheng Bao | 948c0b7 | 2023-05-11 10:03:46 +0800 | [diff] [blame] | 872 | pspdir->entries[count].size = _MAX(TABLE_ALIGNMENT, |
| 873 | pspdir2->header.num_entries * |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 874 | sizeof(psp_directory_entry) + |
Zheng Bao | 948c0b7 | 2023-05-11 10:03:46 +0800 | [diff] [blame] | 875 | sizeof(psp_directory_header)); |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 876 | } |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 877 | |
| 878 | count++; |
| 879 | pspdir->header.num_entries = count; |
| 880 | ctx->current_table = current_table_save; |
| 881 | } |
| 882 | |
Zheng Bao | f360095 | 2024-03-14 13:35:38 +0800 | [diff] [blame] | 883 | static void integrate_psp_levels(context *ctx, |
| 884 | amd_cb_config *cb_config) |
| 885 | { |
| 886 | uint32_t current_table_save; |
| 887 | bool recovery_ab = cb_config->recovery_ab; |
| 888 | unsigned int count; |
| 889 | psp_directory_table *pspdir, *pspdir2, *pspdir2_b; |
| 890 | bool use_only_a = (cb_config->soc_id == PLATFORM_PHOENIX); /* TODO: b:285390041 */ |
| 891 | |
| 892 | pspdir = ctx->pspdir; |
| 893 | pspdir2 = ctx->pspdir2; |
| 894 | pspdir2_b = ctx->pspdir2_b; |
| 895 | count = pspdir->header.num_entries; |
| 896 | |
| 897 | current_table_save = ctx->current_table; |
| 898 | ctx->current_table = BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS); |
| 899 | if (recovery_ab && (pspdir2 != NULL)) { |
| 900 | if (cb_config->need_ish) { /* Need ISH */ |
| 901 | ctx->ish_a_dir = new_ish_dir(ctx); |
| 902 | if (pspdir2_b != NULL) |
| 903 | ctx->ish_b_dir = new_ish_dir(ctx); |
| 904 | } |
| 905 | pspdir->header.num_entries = count; |
| 906 | integrate_psp_ab(ctx, pspdir, pspdir2, ctx->ish_a_dir, |
| 907 | AMD_FW_RECOVERYAB_A, cb_config->soc_id); |
| 908 | if (pspdir2_b != NULL) |
| 909 | integrate_psp_ab(ctx, pspdir, pspdir2_b, ctx->ish_b_dir, |
| 910 | use_only_a ? AMD_FW_RECOVERYAB_A : AMD_FW_RECOVERYAB_B, |
| 911 | cb_config->soc_id); |
| 912 | else |
| 913 | integrate_psp_ab(ctx, pspdir, pspdir2, ctx->ish_a_dir, |
| 914 | use_only_a ? AMD_FW_RECOVERYAB_A : AMD_FW_RECOVERYAB_B, |
| 915 | cb_config->soc_id); |
| 916 | |
| 917 | count = pspdir->header.num_entries; |
| 918 | } else if (pspdir2 != NULL) { |
| 919 | assert_fw_entry(count, MAX_PSP_ENTRIES, ctx); |
| 920 | pspdir->entries[count].type = AMD_FW_L2_PTR; |
| 921 | pspdir->entries[count].subprog = 0; |
| 922 | pspdir->entries[count].rsvd = 0; |
| 923 | pspdir->entries[count].size = sizeof(pspdir2->header) |
| 924 | + pspdir2->header.num_entries |
| 925 | * sizeof(psp_directory_entry); |
| 926 | |
| 927 | pspdir->entries[count].addr = |
| 928 | BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS); |
| 929 | pspdir->entries[count].address_mode = |
| 930 | SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS); |
| 931 | count++; |
| 932 | } |
| 933 | fill_dir_header(pspdir, count, PSP_COOKIE, ctx); |
| 934 | ctx->current_table = current_table_save; |
| 935 | } |
| 936 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 937 | static void integrate_psp_firmwares(context *ctx, |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 938 | amd_fw_entry *fw_table, |
Zheng Bao | 2079589 | 2021-08-20 14:58:22 +0800 | [diff] [blame] | 939 | uint32_t cookie, |
| 940 | amd_cb_config *cb_config) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 941 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 942 | ssize_t bytes; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 943 | unsigned int i, count; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 944 | int level; |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 945 | uint32_t size; |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 946 | psp_directory_table *pspdir; |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 947 | uint64_t addr; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 948 | uint32_t current_table_save; |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 949 | bool recovery_ab = cb_config->recovery_ab; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 950 | |
| 951 | /* This function can create a primary table, a secondary table, or a |
| 952 | * flattened table which contains all applicable types. These if-else |
| 953 | * statements infer what the caller intended. If a 2nd-level cookie |
| 954 | * is passed, clearly a 2nd-level table is intended. However, a |
Zheng Bao | e079379 | 2024-03-13 23:35:29 +0800 | [diff] [blame] | 955 | * 1st-level cookie may indicate level 1 or flattened. |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 956 | */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 957 | pspdir = new_psp_dir(ctx, cb_config->multi_level); |
| 958 | |
| 959 | if (cookie == PSP_COOKIE) |
| 960 | ctx->pspdir = pspdir; |
| 961 | else if (cookie == PSPL2_COOKIE) { |
| 962 | if (ctx->pspdir2 == NULL) |
| 963 | ctx->pspdir2 = pspdir; |
| 964 | else if (ctx->pspdir2_b == NULL) |
| 965 | ctx->pspdir2_b = pspdir; |
| 966 | } |
| 967 | |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 968 | if (!cb_config->multi_level) |
Zheng Bao | 2079589 | 2021-08-20 14:58:22 +0800 | [diff] [blame] | 969 | level = PSP_BOTH; |
| 970 | else if (cookie == PSPL2_COOKIE) |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 971 | level = PSP_LVL2; |
Zheng Bao | e079379 | 2024-03-13 23:35:29 +0800 | [diff] [blame] | 972 | else if (cookie == PSP_COOKIE) |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 973 | level = PSP_LVL1; |
| 974 | else |
| 975 | level = PSP_BOTH; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 976 | |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 977 | if (recovery_ab) { |
| 978 | if (cookie == PSPL2_COOKIE) |
| 979 | level = PSP_LVL2_AB; |
Zheng Bao | e079379 | 2024-03-13 23:35:29 +0800 | [diff] [blame] | 980 | else if (cookie == PSP_COOKIE) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 981 | level = PSP_LVL1_AB; |
| 982 | else |
| 983 | level = PSP_BOTH_AB; |
| 984 | } |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 985 | current_table_save = ctx->current_table; |
| 986 | ctx->current_table = (char *)pspdir - ctx->rom; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 987 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 988 | |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 989 | for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 990 | if (!(fw_table[i].level & level)) |
| 991 | continue; |
| 992 | |
Zheng Bao | 5164e4b | 2021-10-30 12:09:07 +0800 | [diff] [blame] | 993 | assert_fw_entry(count, MAX_PSP_ENTRIES, ctx); |
| 994 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 995 | if (fw_table[i].type == AMD_TOKEN_UNLOCK) { |
| 996 | if (!fw_table[i].other) |
| 997 | continue; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 998 | adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 999 | pspdir->entries[count].type = fw_table[i].type; |
| 1000 | pspdir->entries[count].size = 4096; /* TODO: doc? */ |
| 1001 | pspdir->entries[count].addr = RUN_CURRENT(*ctx); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1002 | pspdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(pspdir); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1003 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 1004 | pspdir->entries[count].rsvd = 0; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1005 | adjust_current_pointer(ctx, 4096, 0x100U); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1006 | count++; |
| 1007 | } else if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) { |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1008 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1009 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 1010 | pspdir->entries[count].rsvd = 0; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1011 | pspdir->entries[count].size = 0xFFFFFFFF; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1012 | pspdir->entries[count].addr = fw_table[i].other; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1013 | pspdir->entries[count].address_mode = 0; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 1014 | count++; |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 1015 | } else if (fw_table[i].type == AMD_FW_PSP_NVRAM) { |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1016 | if (fw_table[i].filename == NULL) { |
| 1017 | if (fw_table[i].size == 0) |
| 1018 | continue; |
| 1019 | size = fw_table[i].size; |
| 1020 | addr = fw_table[i].dest; |
Elyes Haouas | 7d67a19 | 2022-10-14 09:58:29 +0200 | [diff] [blame] | 1021 | if (addr != ALIGN_UP(addr, ERASE_ALIGNMENT)) { |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1022 | fprintf(stderr, |
| 1023 | "Error: PSP NVRAM section not aligned with erase block size.\n\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1024 | amdfwtool_cleanup(ctx); |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1025 | exit(1); |
| 1026 | } |
| 1027 | } else { |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1028 | adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT); |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1029 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 1030 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
| 1031 | if (bytes <= 0) { |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1032 | amdfwtool_cleanup(ctx); |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1033 | exit(1); |
| 1034 | } |
| 1035 | |
Elyes Haouas | 7d67a19 | 2022-10-14 09:58:29 +0200 | [diff] [blame] | 1036 | size = ALIGN_UP(bytes, ERASE_ALIGNMENT); |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1037 | addr = RUN_CURRENT(*ctx); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1038 | adjust_current_pointer(ctx, bytes, BLOB_ERASE_ALIGNMENT); |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | pspdir->entries[count].type = fw_table[i].type; |
| 1042 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 1043 | pspdir->entries[count].rsvd = 0; |
Ritul Guru | a2cb340 | 2022-08-29 00:51:08 +0530 | [diff] [blame] | 1044 | pspdir->entries[count].size = size; |
| 1045 | pspdir->entries[count].addr = addr; |
| 1046 | |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1047 | pspdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1048 | SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS); |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 1049 | |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 1050 | count++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 1051 | } else if (fw_table[i].filename != NULL) { |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 1052 | if (fw_table[i].addr_signed) { |
| 1053 | pspdir->entries[count].addr = |
| 1054 | RUN_OFFSET(*ctx, fw_table[i].addr_signed); |
| 1055 | pspdir->entries[count].address_mode = |
| 1056 | SET_ADDR_MODE_BY_TABLE(pspdir); |
| 1057 | bytes = fw_table[i].file_size; |
| 1058 | } else { |
| 1059 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 1060 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
| 1061 | if (bytes < 0) { |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1062 | amdfwtool_cleanup(ctx); |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 1063 | exit(1); |
| 1064 | } |
| 1065 | pspdir->entries[count].addr = RUN_CURRENT(*ctx); |
| 1066 | pspdir->entries[count].address_mode = |
| 1067 | SET_ADDR_MODE_BY_TABLE(pspdir); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1068 | adjust_current_pointer(ctx, bytes, BLOB_ALIGNMENT); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1071 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1072 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 1073 | pspdir->entries[count].rsvd = 0; |
Fred Reitberger | 75191be | 2023-03-07 11:00:49 -0500 | [diff] [blame] | 1074 | pspdir->entries[count].inst = fw_table[i].inst; |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 1075 | pspdir->entries[count].size = (uint32_t)bytes; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1076 | |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 1077 | count++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 1078 | } else { |
| 1079 | /* This APU doesn't have this firmware. */ |
| 1080 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1081 | } |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1082 | |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 1083 | fill_dir_header(pspdir, count, cookie, ctx); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1084 | ctx->current_table = current_table_save; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 1087 | static void add_psp_firmware_entry(context *ctx, |
| 1088 | psp_directory_table *pspdir, |
| 1089 | void *table, amd_fw_type type, uint32_t size) |
| 1090 | { |
| 1091 | uint32_t count = pspdir->header.num_entries; |
| 1092 | uint32_t index; |
| 1093 | uint32_t current_table_save; |
| 1094 | |
| 1095 | current_table_save = ctx->current_table; |
| 1096 | ctx->current_table = (char *)pspdir - ctx->rom; |
| 1097 | |
| 1098 | /* If there is an entry of "type", replace it. */ |
| 1099 | for (index = 0; index < count; index++) { |
| 1100 | if (pspdir->entries[index].type == (uint8_t)type) |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | assert_fw_entry(count, MAX_PSP_ENTRIES, ctx); |
| 1105 | pspdir->entries[index].type = (uint8_t)type; |
| 1106 | pspdir->entries[index].subprog = 0; |
| 1107 | pspdir->entries[index].rsvd = 0; |
| 1108 | pspdir->entries[index].addr = BUFF_TO_RUN(*ctx, table); |
| 1109 | pspdir->entries[index].address_mode = SET_ADDR_MODE_BY_TABLE(pspdir); |
| 1110 | pspdir->entries[index].size = size; |
| 1111 | if (index == count) |
| 1112 | count++; |
| 1113 | |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 1114 | fill_dir_header(pspdir, count, pspdir->header.cookie, ctx); |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 1115 | ctx->current_table = current_table_save; |
| 1116 | } |
| 1117 | |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 1118 | static void *new_bios_dir(context *ctx, bool multi) |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1119 | { |
| 1120 | void *ptr; |
| 1121 | |
| 1122 | /* |
| 1123 | * Force both onto boundary when multi. Primary table is after |
| 1124 | * updatable table, so alignment ensures primary can stay intact |
| 1125 | * if secondary is reprogrammed. |
| 1126 | */ |
| 1127 | if (multi) |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1128 | adjust_current_pointer(ctx, 0, TABLE_ERASE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1129 | else |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1130 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1131 | ptr = BUFF_CURRENT(*ctx); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1132 | ((bios_directory_hdr *) ptr)->additional_info = 0; |
| 1133 | ((bios_directory_hdr *) ptr)->additional_info_fields.address_mode = ctx->address_mode; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1134 | adjust_current_pointer(ctx, |
| 1135 | sizeof(bios_directory_hdr) + MAX_BIOS_ENTRIES * sizeof(bios_directory_entry), |
| 1136 | 1); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1137 | return ptr; |
| 1138 | } |
| 1139 | |
| 1140 | static int locate_bdt2_bios(bios_directory_table *level2, |
| 1141 | uint64_t *source, uint32_t *size) |
| 1142 | { |
Zheng Bao | 6d402ac | 2020-10-01 16:16:30 +0800 | [diff] [blame] | 1143 | uint32_t i; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1144 | |
| 1145 | *source = 0; |
| 1146 | *size = 0; |
| 1147 | if (!level2) |
| 1148 | return 0; |
| 1149 | |
| 1150 | for (i = 0 ; i < level2->header.num_entries ; i++) { |
| 1151 | if (level2->entries[i].type == AMD_BIOS_BIN) { |
| 1152 | *source = level2->entries[i].source; |
| 1153 | *size = level2->entries[i].size; |
| 1154 | return 1; |
| 1155 | } |
| 1156 | } |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | static int have_bios_tables(amd_bios_entry *table) |
| 1161 | { |
| 1162 | int i; |
| 1163 | |
| 1164 | for (i = 0 ; table[i].type != AMD_BIOS_INVALID; i++) { |
| 1165 | if (table[i].level & BDT_LVL1 && table[i].filename) |
| 1166 | return 1; |
| 1167 | } |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1171 | int find_bios_entry(amd_bios_type type) |
Marshall Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 1172 | { |
| 1173 | int i; |
| 1174 | |
| 1175 | for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) { |
| 1176 | if (amd_bios_table[i].type == type) |
| 1177 | return i; |
| 1178 | } |
| 1179 | return -1; |
| 1180 | } |
| 1181 | |
Karthikeyan Ramasubramanian | 8d88561 | 2023-03-09 17:39:31 -0700 | [diff] [blame] | 1182 | static void add_bios_apcb_bk_entry(bios_directory_table *biosdir, unsigned int idx, |
| 1183 | int inst, uint32_t size, uint64_t source) |
| 1184 | { |
| 1185 | int i; |
| 1186 | |
| 1187 | for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) { |
| 1188 | if (amd_bios_table[i].type == AMD_BIOS_APCB_BK && |
| 1189 | amd_bios_table[i].inst == inst) |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | if (amd_bios_table[i].type != AMD_BIOS_APCB_BK) |
| 1194 | return; |
| 1195 | |
| 1196 | biosdir->entries[idx].type = amd_bios_table[i].type; |
| 1197 | biosdir->entries[idx].region_type = amd_bios_table[i].region_type; |
| 1198 | biosdir->entries[idx].dest = amd_bios_table[i].dest ? |
| 1199 | amd_bios_table[i].dest : (uint64_t)-1; |
| 1200 | biosdir->entries[idx].reset = amd_bios_table[i].reset; |
| 1201 | biosdir->entries[idx].copy = amd_bios_table[i].copy; |
| 1202 | biosdir->entries[idx].ro = amd_bios_table[i].ro; |
| 1203 | biosdir->entries[idx].compressed = amd_bios_table[i].zlib; |
| 1204 | biosdir->entries[idx].inst = amd_bios_table[i].inst; |
| 1205 | biosdir->entries[idx].subprog = amd_bios_table[i].subpr; |
| 1206 | biosdir->entries[idx].size = size; |
| 1207 | biosdir->entries[idx].source = source; |
| 1208 | biosdir->entries[idx].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir); |
| 1209 | } |
| 1210 | |
Zheng Bao | f360095 | 2024-03-14 13:35:38 +0800 | [diff] [blame] | 1211 | static void integrate_bios_levels(context *ctx) |
| 1212 | { |
| 1213 | unsigned int count = ctx->biosdir->header.num_entries; |
| 1214 | uint32_t current_table_save; |
| 1215 | |
| 1216 | current_table_save = ctx->current_table; |
| 1217 | ctx->current_table = (char *)ctx->biosdir - ctx->rom; |
| 1218 | |
| 1219 | if (ctx->biosdir2) { |
| 1220 | assert_fw_entry(count, MAX_BIOS_ENTRIES, ctx); |
| 1221 | ctx->biosdir->entries[count].type = AMD_BIOS_L2_PTR; |
| 1222 | ctx->biosdir->entries[count].region_type = 0; |
| 1223 | ctx->biosdir->entries[count].size = |
| 1224 | + MAX_BIOS_ENTRIES |
| 1225 | * sizeof(bios_directory_entry); |
| 1226 | ctx->biosdir->entries[count].source = |
| 1227 | BUFF_TO_RUN(*ctx, ctx->biosdir2); |
| 1228 | ctx->biosdir->entries[count].address_mode = |
| 1229 | SET_ADDR_MODE(ctx->biosdir, AMD_ADDR_REL_BIOS); |
| 1230 | ctx->biosdir->entries[count].subprog = 0; |
| 1231 | ctx->biosdir->entries[count].inst = 0; |
| 1232 | ctx->biosdir->entries[count].copy = 0; |
| 1233 | ctx->biosdir->entries[count].compressed = 0; |
| 1234 | ctx->biosdir->entries[count].dest = -1; |
| 1235 | ctx->biosdir->entries[count].reset = 0; |
| 1236 | ctx->biosdir->entries[count].ro = 0; |
| 1237 | count++; |
| 1238 | } |
| 1239 | fill_dir_header(ctx->biosdir, count, BHD_COOKIE, ctx); |
| 1240 | ctx->current_table = current_table_save; |
| 1241 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1242 | static void integrate_bios_firmwares(context *ctx, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1243 | amd_bios_entry *fw_table, |
Zheng Bao | 2079589 | 2021-08-20 14:58:22 +0800 | [diff] [blame] | 1244 | uint32_t cookie, |
| 1245 | amd_cb_config *cb_config) |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1246 | { |
| 1247 | ssize_t bytes; |
Martin Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 1248 | unsigned int i, count; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1249 | int level; |
Marshall Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 1250 | int apob_idx; |
Martin Roth | eca423b | 2020-09-01 10:54:11 -0600 | [diff] [blame] | 1251 | uint32_t size; |
| 1252 | uint64_t source; |
Zheng Bao | a640b12 | 2023-10-09 17:36:08 +0800 | [diff] [blame] | 1253 | uint32_t current_table_save; |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1254 | bios_directory_table *biosdir; |
| 1255 | |
| 1256 | biosdir = new_bios_dir(ctx, cb_config->multi_level); |
| 1257 | |
| 1258 | if (cookie == BHD_COOKIE) |
| 1259 | ctx->biosdir = biosdir; |
| 1260 | else if (cookie == BHDL2_COOKIE) { |
| 1261 | if (ctx->biosdir2 == NULL) |
| 1262 | ctx->biosdir2 = biosdir; |
| 1263 | else if (ctx->biosdir2_b == NULL) |
| 1264 | ctx->biosdir2_b = biosdir; |
| 1265 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1266 | |
| 1267 | /* This function can create a primary table, a secondary table, or a |
| 1268 | * flattened table which contains all applicable types. These if-else |
| 1269 | * statements infer what the caller intended. If a 2nd-level cookie |
| 1270 | * is passed, clearly a 2nd-level table is intended. However, a |
Zheng Bao | e079379 | 2024-03-13 23:35:29 +0800 | [diff] [blame] | 1271 | * 1st-level cookie may indicate level 1 or flattened. |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1272 | */ |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 1273 | if (!cb_config->multi_level) |
Zheng Bao | 2079589 | 2021-08-20 14:58:22 +0800 | [diff] [blame] | 1274 | level = BDT_BOTH; |
Zheng Bao | 96a3371 | 2021-06-11 15:54:40 +0800 | [diff] [blame] | 1275 | else if (cookie == BHDL2_COOKIE) |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1276 | level = BDT_LVL2; |
Zheng Bao | e079379 | 2024-03-13 23:35:29 +0800 | [diff] [blame] | 1277 | else if (cookie == BHD_COOKIE) |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1278 | level = BDT_LVL1; |
| 1279 | else |
| 1280 | level = BDT_BOTH; |
| 1281 | |
Zheng Bao | a640b12 | 2023-10-09 17:36:08 +0800 | [diff] [blame] | 1282 | current_table_save = ctx->current_table; |
| 1283 | ctx->current_table = (char *)biosdir - ctx->rom; |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1284 | adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1285 | |
| 1286 | for (i = 0, count = 0; fw_table[i].type != AMD_BIOS_INVALID; i++) { |
| 1287 | if (!(fw_table[i].level & level)) |
| 1288 | continue; |
| 1289 | if (fw_table[i].filename == NULL && ( |
Ritul Guru | 9a321f3 | 2022-07-29 11:06:40 +0530 | [diff] [blame] | 1290 | fw_table[i].type != AMD_BIOS_SIG && |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1291 | fw_table[i].type != AMD_BIOS_APOB && |
| 1292 | fw_table[i].type != AMD_BIOS_APOB_NV && |
| 1293 | fw_table[i].type != AMD_BIOS_L2_PTR && |
Martin Roth | 9455474 | 2020-04-14 14:59:36 -0600 | [diff] [blame] | 1294 | fw_table[i].type != AMD_BIOS_BIN && |
| 1295 | fw_table[i].type != AMD_BIOS_PSP_SHARED_MEM)) |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1296 | continue; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1297 | |
| 1298 | /* BIOS Directory items may have additional requirements */ |
| 1299 | |
Ritul Guru | 9a321f3 | 2022-07-29 11:06:40 +0530 | [diff] [blame] | 1300 | /* SIG needs a size, else no choice but to skip */ |
| 1301 | if (fw_table[i].type == AMD_BIOS_SIG && !fw_table[i].size) |
| 1302 | continue; |
| 1303 | |
Martin Roth | 48dd9fe | 2020-07-29 16:32:25 -0600 | [diff] [blame] | 1304 | /* Check APOB_NV requirements */ |
| 1305 | if (fw_table[i].type == AMD_BIOS_APOB_NV) { |
| 1306 | if (!fw_table[i].size && !fw_table[i].src) |
| 1307 | continue; /* APOB_NV not used */ |
| 1308 | if (fw_table[i].src && !fw_table[i].size) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1309 | fprintf(stderr, "Error: APOB NV address provided, but no size\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1310 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1311 | exit(1); |
| 1312 | } |
Martin Roth | 48dd9fe | 2020-07-29 16:32:25 -0600 | [diff] [blame] | 1313 | /* If the APOB isn't used, APOB_NV isn't used either */ |
Marshall Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 1314 | apob_idx = find_bios_entry(AMD_BIOS_APOB); |
Martin Roth | 48dd9fe | 2020-07-29 16:32:25 -0600 | [diff] [blame] | 1315 | if (apob_idx < 0 || !fw_table[apob_idx].dest) |
| 1316 | continue; /* APOV NV not supported */ |
Marshall Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 1317 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1318 | |
| 1319 | /* APOB_DATA needs destination */ |
| 1320 | if (fw_table[i].type == AMD_BIOS_APOB && !fw_table[i].dest) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1321 | fprintf(stderr, "Error: APOB destination not provided\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1322 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1323 | exit(1); |
| 1324 | } |
| 1325 | |
| 1326 | /* BIOS binary must have destination and uncompressed size. If |
| 1327 | * no filename given, then user must provide a source address. |
| 1328 | */ |
| 1329 | if (fw_table[i].type == AMD_BIOS_BIN) { |
| 1330 | if (!fw_table[i].dest || !fw_table[i].size) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1331 | fprintf(stderr, "Error: BIOS binary destination and uncompressed size are required\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1332 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1333 | exit(1); |
| 1334 | } |
| 1335 | if (!fw_table[i].filename && !fw_table[i].src) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1336 | fprintf(stderr, "Error: BIOS binary assumed outside amdfw.rom but no source address given\n"); |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1337 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1338 | exit(1); |
| 1339 | } |
| 1340 | } |
| 1341 | |
Martin Roth | 9455474 | 2020-04-14 14:59:36 -0600 | [diff] [blame] | 1342 | /* PSP_SHARED_MEM needs a destination and size */ |
| 1343 | if (fw_table[i].type == AMD_BIOS_PSP_SHARED_MEM && |
| 1344 | (!fw_table[i].dest || !fw_table[i].size)) |
| 1345 | continue; |
Zheng Bao | 5164e4b | 2021-10-30 12:09:07 +0800 | [diff] [blame] | 1346 | assert_fw_entry(count, MAX_BIOS_ENTRIES, ctx); |
Martin Roth | 9455474 | 2020-04-14 14:59:36 -0600 | [diff] [blame] | 1347 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1348 | biosdir->entries[count].type = fw_table[i].type; |
| 1349 | biosdir->entries[count].region_type = fw_table[i].region_type; |
| 1350 | biosdir->entries[count].dest = fw_table[i].dest ? |
| 1351 | fw_table[i].dest : (uint64_t)-1; |
| 1352 | biosdir->entries[count].reset = fw_table[i].reset; |
| 1353 | biosdir->entries[count].copy = fw_table[i].copy; |
| 1354 | biosdir->entries[count].ro = fw_table[i].ro; |
| 1355 | biosdir->entries[count].compressed = fw_table[i].zlib; |
| 1356 | biosdir->entries[count].inst = fw_table[i].inst; |
| 1357 | biosdir->entries[count].subprog = fw_table[i].subpr; |
| 1358 | |
| 1359 | switch (fw_table[i].type) { |
Ritul Guru | 9a321f3 | 2022-07-29 11:06:40 +0530 | [diff] [blame] | 1360 | case AMD_BIOS_SIG: |
| 1361 | /* Reserve size bytes within amdfw.rom */ |
| 1362 | biosdir->entries[count].size = fw_table[i].size; |
| 1363 | biosdir->entries[count].source = RUN_CURRENT(*ctx); |
| 1364 | biosdir->entries[count].address_mode = |
| 1365 | SET_ADDR_MODE_BY_TABLE(biosdir); |
| 1366 | memset(BUFF_CURRENT(*ctx), 0xff, |
| 1367 | biosdir->entries[count].size); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1368 | adjust_current_pointer(ctx, biosdir->entries[count].size, 0x100U); |
Ritul Guru | 9a321f3 | 2022-07-29 11:06:40 +0530 | [diff] [blame] | 1369 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1370 | case AMD_BIOS_APOB: |
| 1371 | biosdir->entries[count].size = fw_table[i].size; |
| 1372 | biosdir->entries[count].source = fw_table[i].src; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1373 | biosdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1374 | break; |
| 1375 | case AMD_BIOS_APOB_NV: |
| 1376 | if (fw_table[i].src) { |
| 1377 | /* If source is given, use that and its size */ |
| 1378 | biosdir->entries[count].source = fw_table[i].src; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1379 | biosdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1380 | SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1381 | biosdir->entries[count].size = fw_table[i].size; |
| 1382 | } else { |
| 1383 | /* Else reserve size bytes within amdfw.rom */ |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1384 | adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1385 | biosdir->entries[count].source = RUN_CURRENT(*ctx); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1386 | biosdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1387 | SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS); |
Elyes Haouas | 7d67a19 | 2022-10-14 09:58:29 +0200 | [diff] [blame] | 1388 | biosdir->entries[count].size = ALIGN_UP( |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1389 | fw_table[i].size, ERASE_ALIGNMENT); |
| 1390 | memset(BUFF_CURRENT(*ctx), 0xff, |
| 1391 | biosdir->entries[count].size); |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1392 | adjust_current_pointer(ctx, biosdir->entries[count].size, 1); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1393 | } |
| 1394 | break; |
| 1395 | case AMD_BIOS_BIN: |
| 1396 | /* Don't make a 2nd copy, point to the same one */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1397 | if (level == BDT_LVL1 && locate_bdt2_bios(ctx->biosdir2, &source, &size)) { |
Martin Roth | eca423b | 2020-09-01 10:54:11 -0600 | [diff] [blame] | 1398 | biosdir->entries[count].source = source; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1399 | biosdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1400 | SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS); |
Martin Roth | eca423b | 2020-09-01 10:54:11 -0600 | [diff] [blame] | 1401 | biosdir->entries[count].size = size; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1402 | break; |
Martin Roth | eca423b | 2020-09-01 10:54:11 -0600 | [diff] [blame] | 1403 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1404 | |
| 1405 | /* level 2, or level 1 and no copy found in level 2 */ |
| 1406 | biosdir->entries[count].source = fw_table[i].src; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1407 | biosdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1408 | SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1409 | biosdir->entries[count].dest = fw_table[i].dest; |
| 1410 | biosdir->entries[count].size = fw_table[i].size; |
| 1411 | |
| 1412 | if (!fw_table[i].filename) |
| 1413 | break; |
| 1414 | |
| 1415 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 1416 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
| 1417 | if (bytes <= 0) { |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1418 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1419 | exit(1); |
| 1420 | } |
| 1421 | |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1422 | biosdir->entries[count].source = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1423 | RUN_CURRENT_MODE(*ctx, AMD_ADDR_REL_BIOS); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1424 | biosdir->entries[count].address_mode = |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1425 | SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1426 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1427 | adjust_current_pointer(ctx, bytes, 0x100U); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1428 | break; |
Martin Roth | 9455474 | 2020-04-14 14:59:36 -0600 | [diff] [blame] | 1429 | case AMD_BIOS_PSP_SHARED_MEM: |
| 1430 | biosdir->entries[count].dest = fw_table[i].dest; |
| 1431 | biosdir->entries[count].size = fw_table[i].size; |
| 1432 | break; |
| 1433 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1434 | default: /* everything else is copied from input */ |
| 1435 | if (fw_table[i].type == AMD_BIOS_APCB || |
| 1436 | fw_table[i].type == AMD_BIOS_APCB_BK) |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1437 | adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1438 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 1439 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
| 1440 | if (bytes <= 0) { |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1441 | amdfwtool_cleanup(ctx); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1442 | exit(1); |
| 1443 | } |
| 1444 | |
| 1445 | biosdir->entries[count].size = (uint32_t)bytes; |
| 1446 | biosdir->entries[count].source = RUN_CURRENT(*ctx); |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1447 | biosdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1448 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1449 | adjust_current_pointer(ctx, bytes, 0x100U); |
Karthikeyan Ramasubramanian | 8d88561 | 2023-03-09 17:39:31 -0700 | [diff] [blame] | 1450 | if (fw_table[i].type == AMD_BIOS_APCB && !cb_config->have_apcb_bk) { |
| 1451 | size = biosdir->entries[count].size; |
| 1452 | source = biosdir->entries[count].source; |
| 1453 | count++; |
| 1454 | add_bios_apcb_bk_entry(biosdir, count, fw_table[i].inst, size, source); |
| 1455 | } |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1456 | break; |
| 1457 | } |
| 1458 | |
| 1459 | count++; |
| 1460 | } |
| 1461 | |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 1462 | fill_dir_header(biosdir, count, cookie, ctx); |
Zheng Bao | a640b12 | 2023-10-09 17:36:08 +0800 | [diff] [blame] | 1463 | ctx->current_table = current_table_save; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1464 | } |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1465 | |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 1466 | static int set_efs_table(uint8_t soc_id, amd_cb_config *cb_config, |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1467 | embedded_firmware *amd_romsig) |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1468 | { |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1469 | if ((cb_config->efs_spi_readmode == 0xFF) || (cb_config->efs_spi_speed == 0xFF)) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1470 | fprintf(stderr, "Error: EFS read mode and SPI speed must be set\n"); |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1471 | return 1; |
| 1472 | } |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 1473 | |
| 1474 | /* amd_romsig->efs_gen introduced after RAVEN/PICASSO. |
| 1475 | * Leave as 0xffffffff for first gen */ |
| 1476 | if (cb_config->second_gen) { |
| 1477 | amd_romsig->efs_gen.gen = EFS_SECOND_GEN; |
| 1478 | amd_romsig->efs_gen.reserved = 0; |
| 1479 | } else { |
Zheng Bao | 487d045 | 2022-04-03 12:50:07 +0800 | [diff] [blame] | 1480 | amd_romsig->efs_gen.gen = EFS_BEFORE_SECOND_GEN; |
| 1481 | amd_romsig->efs_gen.reserved = ~0; |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | switch (soc_id) { |
Zheng Bao | 3d7623f | 2022-08-17 11:52:30 +0800 | [diff] [blame] | 1485 | case PLATFORM_CARRIZO: |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 1486 | case PLATFORM_STONEYRIDGE: |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1487 | amd_romsig->spi_readmode_f15_mod_60_6f = cb_config->efs_spi_readmode; |
| 1488 | amd_romsig->fast_speed_new_f15_mod_60_6f = cb_config->efs_spi_speed; |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1489 | break; |
| 1490 | case PLATFORM_RAVEN: |
| 1491 | case PLATFORM_PICASSO: |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1492 | amd_romsig->spi_readmode_f17_mod_00_2f = cb_config->efs_spi_readmode; |
| 1493 | amd_romsig->spi_fastspeed_f17_mod_00_2f = cb_config->efs_spi_speed; |
| 1494 | switch (cb_config->efs_spi_micron_flag) { |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1495 | case 0: |
| 1496 | amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xff; |
| 1497 | break; |
| 1498 | case 1: |
| 1499 | amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xa; |
| 1500 | break; |
| 1501 | default: |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1502 | fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n"); |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1503 | return 1; |
| 1504 | } |
| 1505 | break; |
| 1506 | case PLATFORM_RENOIR: |
| 1507 | case PLATFORM_LUCIENNE: |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 1508 | case PLATFORM_CEZANNE: |
Zheng Bao | 535ec53 | 2021-08-12 16:30:19 +0800 | [diff] [blame] | 1509 | case PLATFORM_MENDOCINO: |
Zheng Bao | de6f198 | 2022-10-16 20:32:43 +0800 | [diff] [blame] | 1510 | case PLATFORM_PHOENIX: |
| 1511 | case PLATFORM_GLINDA: |
Arthur Heymans | 563f7af | 2023-07-13 11:40:08 +0200 | [diff] [blame] | 1512 | case PLATFORM_GENOA: |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1513 | amd_romsig->spi_readmode_f17_mod_30_3f = cb_config->efs_spi_readmode; |
| 1514 | amd_romsig->spi_fastspeed_f17_mod_30_3f = cb_config->efs_spi_speed; |
| 1515 | switch (cb_config->efs_spi_micron_flag) { |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1516 | case 0: |
| 1517 | amd_romsig->micron_detect_f17_mod_30_3f = 0xff; |
| 1518 | break; |
| 1519 | case 1: |
| 1520 | amd_romsig->micron_detect_f17_mod_30_3f = 0xaa; |
| 1521 | break; |
| 1522 | case 2: |
| 1523 | amd_romsig->micron_detect_f17_mod_30_3f = 0x55; |
| 1524 | break; |
| 1525 | default: |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1526 | fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n"); |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1527 | return 1; |
| 1528 | } |
| 1529 | break; |
| 1530 | case PLATFORM_UNKNOWN: |
| 1531 | default: |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1532 | fprintf(stderr, "Error: Invalid SOC name.\n\n"); |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1533 | return 1; |
| 1534 | } |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
Zheng Bao | 18cf3f7 | 2024-03-09 12:38:16 +0800 | [diff] [blame] | 1538 | void open_process_config(char *config, amd_cb_config *cb_config) |
Zheng Bao | 39cae56 | 2023-03-07 18:37:43 +0800 | [diff] [blame] | 1539 | { |
| 1540 | FILE *config_handle; |
| 1541 | |
| 1542 | if (config) { |
| 1543 | config_handle = fopen(config, "r"); |
| 1544 | if (config_handle == NULL) { |
| 1545 | fprintf(stderr, "Can not open file %s for reading: %s\n", |
| 1546 | config, strerror(errno)); |
| 1547 | exit(1); |
| 1548 | } |
Zheng Bao | 994ff52 | 2023-03-09 11:43:55 +0800 | [diff] [blame] | 1549 | if (process_config(config_handle, cb_config) == 0) { |
Zheng Bao | 39cae56 | 2023-03-07 18:37:43 +0800 | [diff] [blame] | 1550 | fprintf(stderr, "Configuration file %s parsing error\n", |
| 1551 | config); |
| 1552 | fclose(config_handle); |
| 1553 | exit(1); |
| 1554 | } |
| 1555 | fclose(config_handle); |
| 1556 | } |
| 1557 | |
| 1558 | /* For debug. */ |
Zheng Bao | 18cf3f7 | 2024-03-09 12:38:16 +0800 | [diff] [blame] | 1559 | if (cb_config->debug) { |
Zheng Bao | 39cae56 | 2023-03-07 18:37:43 +0800 | [diff] [blame] | 1560 | dump_psp_firmwares(amd_psp_fw_table); |
| 1561 | dump_bdt_firmwares(amd_bios_table); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Karthikeyan Ramasubramanian | 225b4b3 | 2023-03-08 10:24:50 -0700 | [diff] [blame] | 1565 | static bool is_initial_alignment_required(enum platform soc_id) |
| 1566 | { |
| 1567 | switch (soc_id) { |
| 1568 | case PLATFORM_MENDOCINO: |
| 1569 | case PLATFORM_PHOENIX: |
| 1570 | case PLATFORM_GLINDA: |
| 1571 | return false; |
| 1572 | default: |
| 1573 | return true; |
| 1574 | } |
| 1575 | } |
| 1576 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1577 | int main(int argc, char **argv) |
| 1578 | { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1579 | int retval = 0; |
Zheng Bao | 0e3d18b | 2023-03-07 15:28:57 +0800 | [diff] [blame] | 1580 | int combo_index = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1581 | int targetfd; |
Zheng Bao | 9c8ce3e | 2020-09-28 10:36:29 +0800 | [diff] [blame] | 1582 | context ctx = { 0 }; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1583 | uint32_t romsig_offset; |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1584 | amd_cb_config cb_config = { |
| 1585 | .efs_spi_readmode = 0xff, .efs_spi_speed = 0xff, .efs_spi_micron_flag = 0xff |
| 1586 | }; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 1587 | |
Zheng Bao | c26108f | 2023-02-17 11:01:07 +0800 | [diff] [blame] | 1588 | ctx.current_pointer_saved = 0xFFFFFFFF; |
| 1589 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1590 | retval = amdfwtool_getopt(argc, argv, &cb_config, &ctx); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1591 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1592 | if (retval) { |
| 1593 | return retval; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1594 | } |
| 1595 | |
Zheng Bao | 8dd34bd | 2023-03-09 21:09:58 +0800 | [diff] [blame] | 1596 | if (cb_config.use_combo) { |
| 1597 | ctx.amd_psp_fw_table_clean = malloc(sizeof(amd_psp_fw_table)); |
| 1598 | ctx.amd_bios_table_clean = malloc(sizeof(amd_bios_table)); |
| 1599 | memcpy(ctx.amd_psp_fw_table_clean, amd_psp_fw_table, sizeof(amd_psp_fw_table)); |
| 1600 | memcpy(ctx.amd_bios_table_clean, amd_bios_table, sizeof(amd_bios_table)); |
| 1601 | } |
| 1602 | |
Zheng Bao | 18cf3f7 | 2024-03-09 12:38:16 +0800 | [diff] [blame] | 1603 | open_process_config(cb_config.config, &cb_config); |
Zheng Bao | 9e90807 | 2020-10-28 11:39:13 +0800 | [diff] [blame] | 1604 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1605 | ctx.rom = malloc(ctx.rom_size); |
| 1606 | if (!ctx.rom) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1607 | fprintf(stderr, "Error: Failed to allocate memory\n"); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1608 | return 1; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1609 | } |
| 1610 | memset(ctx.rom, 0xFF, ctx.rom_size); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1611 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1612 | romsig_offset = cb_config.efs_location ? cb_config.efs_location : AMD_ROMSIG_OFFSET; |
Zheng Bao | 6095cd1 | 2023-02-21 10:52:47 +0800 | [diff] [blame] | 1613 | set_current_pointer(&ctx, romsig_offset); |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1614 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1615 | ctx.amd_romsig_ptr = BUFF_OFFSET(ctx, romsig_offset); |
| 1616 | ctx.amd_romsig_ptr->signature = EMBEDDED_FW_SIGNATURE; |
| 1617 | ctx.amd_romsig_ptr->imc_entry = 0; |
| 1618 | ctx.amd_romsig_ptr->gec_entry = 0; |
| 1619 | ctx.amd_romsig_ptr->xhci_entry = 0; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1620 | |
Zheng Bao | 4bf6f49 | 2023-01-25 22:37:29 +0800 | [diff] [blame] | 1621 | if (cb_config.soc_id != PLATFORM_UNKNOWN) { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1622 | retval = set_efs_table(cb_config.soc_id, &cb_config, ctx.amd_romsig_ptr); |
Zheng Bao | 570645d | 2021-11-03 10:25:03 +0800 | [diff] [blame] | 1623 | if (retval) { |
| 1624 | fprintf(stderr, "ERROR: Failed to initialize EFS table!\n"); |
| 1625 | return retval; |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1626 | } |
| 1627 | } else { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 1628 | fprintf(stderr, "WARNING: No SOC name specified.\n"); |
Matt Papageorge | be4376c | 2020-06-15 11:18:15 -0500 | [diff] [blame] | 1629 | } |
| 1630 | |
Felix Held | 21a8e38 | 2022-03-29 23:10:45 +0200 | [diff] [blame] | 1631 | if (cb_config.need_ish) |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1632 | ctx.address_mode = AMD_ADDR_REL_TAB; |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 1633 | else if (cb_config.second_gen) |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1634 | ctx.address_mode = AMD_ADDR_REL_BIOS; |
Zheng Bao | da83d2c | 2021-06-04 19:03:10 +0800 | [diff] [blame] | 1635 | else |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 1636 | ctx.address_mode = AMD_ADDR_PHYSICAL; |
Zheng Bao | da83d2c | 2021-06-04 19:03:10 +0800 | [diff] [blame] | 1637 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1638 | if (cb_config.efs_location != cb_config.body_location) |
| 1639 | set_current_pointer(&ctx, cb_config.body_location); |
Zheng Bao | 6095cd1 | 2023-02-21 10:52:47 +0800 | [diff] [blame] | 1640 | else |
| 1641 | set_current_pointer(&ctx, romsig_offset + sizeof(embedded_firmware)); |
| 1642 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1643 | integrate_firmwares(&ctx, ctx.amd_romsig_ptr, amd_fw_table); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1644 | |
Karthikeyan Ramasubramanian | 225b4b3 | 2023-03-08 10:24:50 -0700 | [diff] [blame] | 1645 | if (is_initial_alignment_required(cb_config.soc_id)) { |
| 1646 | /* TODO: Check for older platforms. */ |
| 1647 | adjust_current_pointer(&ctx, 0, 0x10000U); |
| 1648 | } |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 1649 | ctx.current_table = 0; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1650 | |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 1651 | /* If the tool is invoked with command-line options to keep the signed PSP |
| 1652 | binaries separate, process the signed binaries first. */ |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1653 | if (cb_config.signed_output_file && cb_config.signed_start_addr) |
| 1654 | process_signed_psp_firmwares(cb_config.signed_output_file, |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 1655 | amd_psp_fw_table, |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1656 | cb_config.signed_start_addr, |
Zheng Bao | 4bf6f49 | 2023-01-25 22:37:29 +0800 | [diff] [blame] | 1657 | cb_config.soc_id); |
Kangheui Won | 3c164e1 | 2021-12-03 20:25:05 +1100 | [diff] [blame] | 1658 | |
Zheng Bao | b2ae6a5 | 2022-08-18 15:45:27 +0800 | [diff] [blame] | 1659 | if (cb_config.use_combo) { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1660 | ctx.psp_combo_dir = new_combo_dir(&ctx); |
Zheng Bao | 84fb9ea | 2022-08-18 15:54:47 +0800 | [diff] [blame] | 1661 | |
| 1662 | adjust_current_pointer(&ctx, 0, 0x1000U); |
| 1663 | |
Zheng Bao | d22c2c8 | 2024-03-09 10:56:06 +0800 | [diff] [blame] | 1664 | if (!cb_config.recovery_ab) |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1665 | ctx.bhd_combo_dir = new_combo_dir(&ctx); |
Zheng Bao | b2ae6a5 | 2022-08-18 15:45:27 +0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Zheng Bao | 0e3d18b | 2023-03-07 15:28:57 +0800 | [diff] [blame] | 1668 | combo_index = 0; |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1669 | if (cb_config.config) |
| 1670 | cb_config.combo_config[0] = cb_config.config; |
Zheng Bao | 0e3d18b | 2023-03-07 15:28:57 +0800 | [diff] [blame] | 1671 | |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1672 | do { |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1673 | if (cb_config.use_combo && cb_config.debug) |
Zheng Bao | 3e7008d | 2023-03-15 16:15:13 +0800 | [diff] [blame] | 1674 | printf("Processing %dth combo entry\n", combo_index); |
| 1675 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1676 | ctx.pspdir = NULL; |
| 1677 | ctx.pspdir2 = NULL; |
| 1678 | ctx.pspdir2_b = NULL; |
| 1679 | ctx.biosdir = NULL; |
| 1680 | ctx.biosdir2 = NULL; |
| 1681 | ctx.biosdir2_b = NULL; |
| 1682 | ctx.ish_a_dir = NULL; |
| 1683 | ctx.ish_b_dir = NULL; |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1684 | /* for non-combo image, combo_config[0] == config, and |
| 1685 | * it already is processed. Actually "combo_index > |
| 1686 | * 0" is enough. Put both of them here to make sure |
| 1687 | * and make it clear this will not affect non-combo |
| 1688 | * case. |
| 1689 | */ |
| 1690 | if (cb_config.use_combo && combo_index > 0) { |
Zheng Bao | 8dd34bd | 2023-03-09 21:09:58 +0800 | [diff] [blame] | 1691 | /* Restore the table as clean data. */ |
| 1692 | memcpy(amd_psp_fw_table, ctx.amd_psp_fw_table_clean, |
| 1693 | sizeof(amd_psp_fw_table)); |
| 1694 | memcpy(amd_bios_table, ctx.amd_bios_table_clean, |
| 1695 | sizeof(amd_bios_table)); |
Zheng Bao | 7391722 | 2023-03-15 16:14:03 +0800 | [diff] [blame] | 1696 | assert_fw_entry(combo_index, MAX_COMBO_ENTRIES, &ctx); |
Zheng Bao | 18cf3f7 | 2024-03-09 12:38:16 +0800 | [diff] [blame] | 1697 | open_process_config(cb_config.combo_config[combo_index], &cb_config); |
Zheng Bao | 0e3d18b | 2023-03-07 15:28:57 +0800 | [diff] [blame] | 1698 | |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1699 | /* In most cases, the address modes are same. */ |
| 1700 | if (cb_config.need_ish) |
| 1701 | ctx.address_mode = AMD_ADDR_REL_TAB; |
| 1702 | else if (cb_config.second_gen) |
| 1703 | ctx.address_mode = AMD_ADDR_REL_BIOS; |
| 1704 | else |
| 1705 | ctx.address_mode = AMD_ADDR_PHYSICAL; |
Zheng Bao | fa25954 | 2021-10-26 19:46:55 +0800 | [diff] [blame] | 1706 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1707 | register_apcb_combo(&cb_config, combo_index, &ctx); |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 1708 | } |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1709 | |
Zheng Bao | 481661e | 2021-08-20 14:47:46 +0800 | [diff] [blame] | 1710 | if (cb_config.multi_level) { |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1711 | /* Do 2nd PSP directory followed by 1st */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1712 | integrate_psp_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1713 | amd_psp_fw_table, PSPL2_COOKIE, &cb_config); |
| 1714 | if (cb_config.recovery_ab && !cb_config.recovery_ab_single_copy) { |
| 1715 | /* Create a copy of PSP Directory 2 in the backup slot B. |
| 1716 | Related biosdir2_b copy will be created later. */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1717 | integrate_psp_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1718 | amd_psp_fw_table, PSPL2_COOKIE, &cb_config); |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 1719 | } else { |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1720 | /* |
| 1721 | * Either the platform is using only |
| 1722 | * one slot or B is same as above |
| 1723 | * directories for A. Skip creating |
| 1724 | * pspdir2_b here to save flash space. |
| 1725 | * Related biosdir2_b will be skipped |
| 1726 | * automatically. |
| 1727 | */ |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1728 | ctx.pspdir2_b = NULL; /* More explicitly */ |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 1729 | } |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1730 | integrate_psp_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1731 | amd_psp_fw_table, PSP_COOKIE, &cb_config); |
Zheng Bao | f360095 | 2024-03-14 13:35:38 +0800 | [diff] [blame] | 1732 | integrate_psp_levels(&ctx, &cb_config); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1733 | } else { |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1734 | /* flat: PSP 1 cookie and no pointer to 2nd table */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1735 | integrate_psp_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1736 | amd_psp_fw_table, PSP_COOKIE, &cb_config); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1737 | } |
Zheng Bao | 84fb9ea | 2022-08-18 15:54:47 +0800 | [diff] [blame] | 1738 | |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1739 | if (!cb_config.use_combo) { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1740 | fill_psp_directory_to_efs(ctx.amd_romsig_ptr, ctx.pspdir, &ctx, &cb_config); |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1741 | } else { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1742 | fill_psp_directory_to_efs(ctx.amd_romsig_ptr, ctx.psp_combo_dir, &ctx, &cb_config); |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1743 | /* 0 -Compare PSP ID, 1 -Compare chip family ID */ |
| 1744 | assert_fw_entry(combo_index, MAX_COMBO_ENTRIES, &ctx); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1745 | ctx.psp_combo_dir->entries[combo_index].id_sel = 0; |
| 1746 | ctx.psp_combo_dir->entries[combo_index].id = get_psp_id(cb_config.soc_id); |
| 1747 | ctx.psp_combo_dir->entries[combo_index].lvl2_addr = |
| 1748 | BUFF_TO_RUN_MODE(ctx, ctx.pspdir, AMD_ADDR_REL_BIOS); |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1749 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1750 | fill_dir_header(ctx.psp_combo_dir, combo_index + 1, PSP2_COOKIE, &ctx); |
Zheng Bao | 84fb9ea | 2022-08-18 15:54:47 +0800 | [diff] [blame] | 1751 | } |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1752 | |
| 1753 | if (have_bios_tables(amd_bios_table)) { |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1754 | if (cb_config.multi_level) { |
| 1755 | /* Do 2nd level BIOS directory followed by 1st */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1756 | integrate_bios_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1757 | amd_bios_table, BHDL2_COOKIE, &cb_config); |
| 1758 | if (cb_config.recovery_ab) { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1759 | if (ctx.pspdir2_b != NULL) { |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1760 | integrate_bios_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1761 | amd_bios_table, BHDL2_COOKIE, |
| 1762 | &cb_config); |
| 1763 | } |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1764 | add_psp_firmware_entry(&ctx, ctx.pspdir2, ctx.biosdir2, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1765 | AMD_FW_BIOS_TABLE, TABLE_ALIGNMENT); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1766 | if (ctx.pspdir2_b != NULL) |
| 1767 | add_psp_firmware_entry(&ctx, ctx.pspdir2_b, |
| 1768 | ctx.biosdir2_b, AMD_FW_BIOS_TABLE, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1769 | TABLE_ALIGNMENT); |
| 1770 | } else { |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1771 | integrate_bios_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1772 | amd_bios_table, BHD_COOKIE, &cb_config); |
Zheng Bao | f360095 | 2024-03-14 13:35:38 +0800 | [diff] [blame] | 1773 | integrate_bios_levels(&ctx); |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1774 | } |
| 1775 | } else { |
| 1776 | /* flat: BHD1 cookie and no pointer to 2nd table */ |
Zheng Bao | eea834b | 2024-03-14 13:17:05 +0800 | [diff] [blame^] | 1777 | integrate_bios_firmwares(&ctx, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1778 | amd_bios_table, BHD_COOKIE, &cb_config); |
| 1779 | } |
| 1780 | if (!cb_config.use_combo) { |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1781 | fill_bios_directory_to_efs(ctx.amd_romsig_ptr, ctx.biosdir, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1782 | &ctx, &cb_config); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1783 | } else if (ctx.bhd_combo_dir != NULL) { |
Zheng Bao | d22c2c8 | 2024-03-09 10:56:06 +0800 | [diff] [blame] | 1784 | /* In recovery A/B mode, there isn't a BHD combo directory. |
| 1785 | * Instead, the BIOS tables level 2 are linked by PSP tables. |
| 1786 | */ |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1787 | fill_bios_directory_to_efs(ctx.amd_romsig_ptr, ctx.bhd_combo_dir, |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1788 | &ctx, &cb_config); |
| 1789 | assert_fw_entry(combo_index, MAX_COMBO_ENTRIES, &ctx); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1790 | ctx.bhd_combo_dir->entries[combo_index].id_sel = 0; |
| 1791 | ctx.bhd_combo_dir->entries[combo_index].id = |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1792 | get_psp_id(cb_config.soc_id); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1793 | ctx.bhd_combo_dir->entries[combo_index].lvl2_addr = |
| 1794 | BUFF_TO_RUN_MODE(ctx, ctx.biosdir, AMD_ADDR_REL_BIOS); |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1795 | |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1796 | fill_dir_header(ctx.bhd_combo_dir, combo_index + 1, |
Zheng Bao | 07e0508 | 2024-03-15 13:46:51 +0800 | [diff] [blame] | 1797 | BHD2_COOKIE, &ctx); |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1798 | |
Zheng Bao | 4b6aa19 | 2023-03-09 11:28:47 +0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1801 | if (cb_config.debug) |
| 1802 | dump_image_addresses(&ctx); |
Zheng Bao | 17551ae | 2023-03-11 10:29:56 +0800 | [diff] [blame] | 1803 | } while (cb_config.use_combo && ++combo_index < MAX_COMBO_ENTRIES && |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1804 | cb_config.combo_config[combo_index] != NULL); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1805 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1806 | targetfd = open(cb_config.output, O_RDWR | O_CREAT | O_TRUNC, 0666); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1807 | if (targetfd >= 0) { |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1808 | uint32_t offset = cb_config.efs_location; |
| 1809 | uint32_t bytes = cb_config.efs_location == cb_config.body_location ? |
Zheng Bao | e4214b7 | 2024-03-13 22:51:24 +0800 | [diff] [blame] | 1810 | ctx.current - offset : sizeof(embedded_firmware); |
Zheng Bao | 69ea83c | 2023-01-22 21:08:18 +0800 | [diff] [blame] | 1811 | uint32_t ret_bytes; |
Karthikeyan Ramasubramanian | ecb4e31 | 2022-11-02 16:53:54 -0600 | [diff] [blame] | 1812 | |
Zheng Bao | c25d593 | 2023-03-22 12:51:47 +0800 | [diff] [blame] | 1813 | ret_bytes = write_from_buf_to_file(targetfd, BUFF_OFFSET(ctx, offset), bytes); |
Zheng Bao | 69ea83c | 2023-01-22 21:08:18 +0800 | [diff] [blame] | 1814 | if (bytes != ret_bytes) { |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1815 | fprintf(stderr, "Error: Writing to file %s failed\n", cb_config.output); |
Zheng Bao | 4739691 | 2020-09-29 17:33:17 +0800 | [diff] [blame] | 1816 | retval = 1; |
| 1817 | } |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1818 | close(targetfd); |
| 1819 | } else { |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1820 | fprintf(stderr, "Error: could not open file: %s\n", cb_config.output); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1821 | retval = 1; |
| 1822 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1823 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1824 | if (cb_config.efs_location != cb_config.body_location) { |
Karthikeyan Ramasubramanian | ecb4e31 | 2022-11-02 16:53:54 -0600 | [diff] [blame] | 1825 | ssize_t bytes; |
| 1826 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1827 | bytes = write_body(cb_config.output, BUFF_OFFSET(ctx, cb_config.body_location), |
| 1828 | ctx.current - cb_config.body_location); |
| 1829 | if (bytes != ctx.current - cb_config.body_location) { |
Zheng Bao | 69ea83c | 2023-01-22 21:08:18 +0800 | [diff] [blame] | 1830 | fprintf(stderr, "Error: Writing body\n"); |
Karthikeyan Ramasubramanian | ecb4e31 | 2022-11-02 16:53:54 -0600 | [diff] [blame] | 1831 | retval = 1; |
| 1832 | } |
| 1833 | } |
| 1834 | |
Zheng Bao | e35c502 | 2023-10-11 16:08:44 +0800 | [diff] [blame] | 1835 | if (cb_config.manifest_file) { |
| 1836 | dump_blob_version(cb_config.manifest_file, amd_psp_fw_table); |
Grzegorz Bernacki | dfdf81c | 2023-04-05 09:35:42 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Zheng Bao | 7c5ad88 | 2023-02-19 13:02:52 +0800 | [diff] [blame] | 1839 | amdfwtool_cleanup(&ctx); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1840 | return retval; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1841 | } |