Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #ifndef _AMD_FW_TOOL_H_ |
| 4 | #define _AMD_FW_TOOL_H_ |
| 5 | |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 6 | #include <stdint.h> |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 7 | #include <stdbool.h> |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 8 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 9 | typedef enum _amd_fw_type { |
| 10 | AMD_FW_PSP_PUBKEY = 0, |
| 11 | AMD_FW_PSP_BOOTLOADER = 1, |
| 12 | AMD_FW_PSP_SMU_FIRMWARE = 8, |
| 13 | AMD_FW_PSP_RECOVERY = 3, |
| 14 | AMD_FW_PSP_RTM_PUBKEY = 5, |
| 15 | AMD_FW_PSP_SECURED_OS = 2, |
| 16 | AMD_FW_PSP_NVRAM = 4, |
| 17 | AMD_FW_PSP_SECURED_DEBUG = 9, |
| 18 | AMD_FW_PSP_TRUSTLETS = 12, |
| 19 | AMD_FW_PSP_TRUSTLETKEY = 13, |
| 20 | AMD_FW_PSP_SMU_FIRMWARE2 = 18, |
| 21 | AMD_PSP_FUSE_CHAIN = 11, |
| 22 | AMD_FW_PSP_SMUSCS = 95, |
| 23 | AMD_DEBUG_UNLOCK = 0x13, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 24 | AMD_HW_IPCFG = 0x20, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 25 | AMD_WRAPPED_IKEK = 0x21, |
| 26 | AMD_TOKEN_UNLOCK = 0x22, |
| 27 | AMD_SEC_GASKET = 0x24, |
| 28 | AMD_MP2_FW = 0x25, |
| 29 | AMD_DRIVER_ENTRIES = 0x28, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 30 | AMD_FW_KVM_IMAGE = 0x29, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 31 | AMD_S0I3_DRIVER = 0x2d, |
| 32 | AMD_ABL0 = 0x30, |
| 33 | AMD_ABL1 = 0x31, |
| 34 | AMD_ABL2 = 0x32, |
| 35 | AMD_ABL3 = 0x33, |
| 36 | AMD_ABL4 = 0x34, |
| 37 | AMD_ABL5 = 0x35, |
| 38 | AMD_ABL6 = 0x36, |
| 39 | AMD_ABL7 = 0x37, |
| 40 | AMD_FW_PSP_WHITELIST = 0x3a, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 41 | AMD_VBIOS_BTLOADER = 0x3c, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 42 | AMD_FW_L2_PTR = 0x40, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 43 | AMD_FW_USB_PHY = 0x44, |
| 44 | AMD_FW_TOS_SEC_POLICY = 0x45, |
| 45 | AMD_FW_DRTM_TA = 0x47, |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 46 | AMD_FW_RECOVERYAB_A = 0x48, |
| 47 | AMD_FW_RECOVERYAB_B = 0x4A, |
| 48 | AMD_FW_BIOS_TABLE = 0x49, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 49 | AMD_FW_KEYDB_BL = 0x50, |
| 50 | AMD_FW_KEYDB_TOS = 0x51, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 51 | AMD_FW_PSP_VERSTAGE = 0x52, |
| 52 | AMD_FW_VERSTAGE_SIG = 0x53, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 53 | AMD_RPMC_NVRAM = 0x54, |
Zheng Bao | ab84fd7 | 2022-01-27 22:38:27 +0800 | [diff] [blame] | 54 | AMD_FW_SPL = 0x55, |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 55 | AMD_FW_DMCU_ERAM = 0x58, |
| 56 | AMD_FW_DMCU_ISR = 0x59, |
Felix Held | 5f18bb7 | 2022-03-24 02:04:51 +0100 | [diff] [blame] | 57 | AMD_FW_MSMU = 0x5a, |
| 58 | AMD_FW_SPIROM_CFG = 0x5c, |
| 59 | AMD_FW_DMCUB = 0x71, |
Zheng Bao | b993cb2 | 2021-02-02 18:48:23 +0800 | [diff] [blame] | 60 | AMD_FW_PSP_BOOTLOADER_AB = 0x73, |
Karthikeyan Ramasubramanian | 0ab04d2 | 2022-05-03 18:16:34 -0600 | [diff] [blame] | 61 | AMD_TA_IKEK = 0x8d, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 62 | AMD_FW_IMC = 0x200, /* Large enough to be larger than the top BHD entry type. */ |
| 63 | AMD_FW_GEC, |
| 64 | AMD_FW_XHCI, |
| 65 | AMD_FW_INVALID, /* Real last one to detect the last entry in table. */ |
| 66 | AMD_FW_SKIP /* This is for non-applicable options. */ |
| 67 | } amd_fw_type; |
| 68 | |
| 69 | typedef enum _amd_bios_type { |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 70 | AMD_BIOS_RTM_PUBKEY = 5, |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 71 | AMD_BIOS_APCB = 0x60, |
| 72 | AMD_BIOS_APOB = 0x61, |
| 73 | AMD_BIOS_BIN = 0x62, |
| 74 | AMD_BIOS_APOB_NV = 0x63, |
| 75 | AMD_BIOS_PMUI = 0x64, |
| 76 | AMD_BIOS_PMUD = 0x65, |
| 77 | AMD_BIOS_UCODE = 0x66, |
| 78 | AMD_BIOS_APCB_BK = 0x68, |
| 79 | AMD_BIOS_MP2_CFG = 0x6a, |
| 80 | AMD_BIOS_PSP_SHARED_MEM = 0x6b, |
| 81 | AMD_BIOS_L2_PTR = 0x70, |
| 82 | AMD_BIOS_INVALID, |
| 83 | AMD_BIOS_SKIP |
| 84 | } amd_bios_type; |
| 85 | |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 86 | typedef enum _amd_addr_mode { |
| 87 | AMD_ADDR_PHYSICAL = 0, /* Physical address */ |
| 88 | AMD_ADDR_REL_BIOS, /* Relative to beginning of image */ |
| 89 | AMD_ADDR_REL_TAB, /* Relative to table */ |
| 90 | AMD_ADDR_REL_SLOT, /* Relative to slot */ |
| 91 | } amd_addr_mode; |
| 92 | |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 93 | struct second_gen_efs { /* todo: expand for Server products */ |
| 94 | int gen:1; /* Client products only use bit 0 */ |
| 95 | int reserved:31; |
| 96 | } __attribute__((packed)); |
| 97 | |
| 98 | #define EFS_SECOND_GEN 0 |
Zheng Bao | 487d045 | 2022-04-03 12:50:07 +0800 | [diff] [blame] | 99 | #define EFS_BEFORE_SECOND_GEN 1 |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 100 | |
| 101 | typedef struct _embedded_firmware { |
| 102 | uint32_t signature; /* 0x55aa55aa */ |
| 103 | uint32_t imc_entry; |
| 104 | uint32_t gec_entry; |
| 105 | uint32_t xhci_entry; |
Felix Held | ad68b07 | 2021-10-18 14:00:35 +0200 | [diff] [blame] | 106 | uint32_t psp_directory; |
Zheng Bao | b749d3f | 2021-10-23 20:20:21 +0800 | [diff] [blame] | 107 | union { |
| 108 | uint32_t new_psp_directory; |
| 109 | uint32_t combo_psp_directory; |
| 110 | }; |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 111 | uint32_t bios0_entry; /* todo: add way to select correct entry */ |
| 112 | uint32_t bios1_entry; |
| 113 | uint32_t bios2_entry; |
| 114 | struct second_gen_efs efs_gen; |
| 115 | uint32_t bios3_entry; |
| 116 | uint32_t reserved_2Ch; |
| 117 | uint32_t promontory_fw_ptr; |
| 118 | uint32_t lp_promontory_fw_ptr; |
| 119 | uint32_t reserved_38h; |
| 120 | uint32_t reserved_3Ch; |
| 121 | uint8_t spi_readmode_f15_mod_60_6f; |
| 122 | uint8_t fast_speed_new_f15_mod_60_6f; |
| 123 | uint8_t reserved_42h; |
| 124 | uint8_t spi_readmode_f17_mod_00_2f; |
| 125 | uint8_t spi_fastspeed_f17_mod_00_2f; |
| 126 | uint8_t qpr_dummy_cycle_f17_mod_00_2f; |
| 127 | uint8_t reserved_46h; |
| 128 | uint8_t spi_readmode_f17_mod_30_3f; |
| 129 | uint8_t spi_fastspeed_f17_mod_30_3f; |
| 130 | uint8_t micron_detect_f17_mod_30_3f; |
| 131 | uint8_t reserved_4Ah; |
| 132 | uint8_t reserved_4Bh; |
| 133 | uint32_t reserved_4Ch; |
| 134 | } __attribute__((packed, aligned(16))) embedded_firmware; |
| 135 | |
| 136 | typedef struct _psp_directory_header { |
| 137 | uint32_t cookie; |
| 138 | uint32_t checksum; |
| 139 | uint32_t num_entries; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 140 | union { |
| 141 | uint32_t additional_info; |
| 142 | struct { |
| 143 | uint32_t dir_size:10; |
| 144 | uint32_t spi_block_size:4; |
| 145 | uint32_t base_addr:15; |
| 146 | uint32_t address_mode:2; |
| 147 | uint32_t not_used:1; |
| 148 | } __attribute__((packed)) additional_info_fields; |
| 149 | }; |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 150 | } __attribute__((packed, aligned(16))) psp_directory_header; |
| 151 | |
| 152 | typedef struct _psp_directory_entry { |
| 153 | uint8_t type; |
| 154 | uint8_t subprog; |
| 155 | uint16_t rsvd; |
| 156 | uint32_t size; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 157 | uint64_t addr:62; /* or a value in some cases */ |
| 158 | uint64_t address_mode:2; |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 159 | } __attribute__((packed)) psp_directory_entry; |
| 160 | |
| 161 | typedef struct _psp_directory_table { |
| 162 | psp_directory_header header; |
| 163 | psp_directory_entry entries[]; |
| 164 | } __attribute__((packed, aligned(16))) psp_directory_table; |
| 165 | |
| 166 | #define MAX_PSP_ENTRIES 0x1f |
| 167 | |
| 168 | typedef struct _psp_combo_header { |
| 169 | uint32_t cookie; |
| 170 | uint32_t checksum; |
| 171 | uint32_t num_entries; |
| 172 | uint32_t lookup; |
| 173 | uint64_t reserved[2]; |
| 174 | } __attribute__((packed, aligned(16))) psp_combo_header; |
| 175 | |
| 176 | typedef struct _psp_combo_entry { |
| 177 | uint32_t id_sel; |
| 178 | uint32_t id; |
| 179 | uint64_t lvl2_addr; |
| 180 | } __attribute__((packed)) psp_combo_entry; |
| 181 | |
| 182 | typedef struct _psp_combo_directory { |
| 183 | psp_combo_header header; |
| 184 | psp_combo_entry entries[]; |
| 185 | } __attribute__((packed, aligned(16))) psp_combo_directory; |
| 186 | |
| 187 | #define MAX_COMBO_ENTRIES 1 |
| 188 | |
| 189 | typedef struct _bios_directory_hdr { |
| 190 | uint32_t cookie; |
| 191 | uint32_t checksum; |
| 192 | uint32_t num_entries; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 193 | union { |
| 194 | uint32_t additional_info; |
| 195 | struct { |
| 196 | uint32_t dir_size:10; |
| 197 | uint32_t spi_block_size:4; |
| 198 | uint32_t base_addr:15; |
| 199 | uint32_t address_mode:2; |
| 200 | uint32_t not_used:1; |
| 201 | } __attribute__((packed)) additional_info_fields; |
| 202 | }; |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 203 | } __attribute__((packed, aligned(16))) bios_directory_hdr; |
| 204 | |
| 205 | typedef struct _bios_directory_entry { |
| 206 | uint8_t type; |
| 207 | uint8_t region_type; |
| 208 | int reset:1; |
| 209 | int copy:1; |
| 210 | int ro:1; |
| 211 | int compressed:1; |
| 212 | int inst:4; |
| 213 | uint8_t subprog; /* b[7:3] reserved */ |
| 214 | uint32_t size; |
Zheng Bao | 6fff249 | 2021-11-15 19:53:21 +0800 | [diff] [blame] | 215 | uint64_t source:62; |
| 216 | uint64_t address_mode:2; |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 217 | uint64_t dest; |
| 218 | } __attribute__((packed)) bios_directory_entry; |
| 219 | |
| 220 | typedef struct _bios_directory_table { |
| 221 | bios_directory_hdr header; |
| 222 | bios_directory_entry entries[]; |
| 223 | } bios_directory_table; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 224 | |
Zheng Bao | 3335133 | 2021-10-30 16:53:23 +0800 | [diff] [blame] | 225 | #define BDT_LVL1 (1 << 0) |
| 226 | #define BDT_LVL2 (1 << 1) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 227 | #define BDT_LVL1_AB (1 << 2) |
| 228 | #define BDT_LVL2_AB (1 << 3) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 229 | #define BDT_BOTH (BDT_LVL1 | BDT_LVL2) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 230 | #define BDT_BOTH_AB (BDT_LVL1_AB | BDT_LVL2_AB) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 231 | typedef struct _amd_bios_entry { |
| 232 | amd_bios_type type; |
| 233 | char *filename; |
| 234 | int subpr; |
| 235 | int region_type; |
| 236 | int reset; |
| 237 | int copy; |
| 238 | int ro; |
| 239 | int zlib; |
| 240 | int inst; |
| 241 | uint64_t src; |
| 242 | uint64_t dest; |
| 243 | size_t size; |
| 244 | int level; |
| 245 | } amd_bios_entry; |
| 246 | |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 247 | typedef struct _ish_directory_table { |
| 248 | uint32_t checksum; |
| 249 | uint32_t boot_priority; |
| 250 | uint32_t update_retry_count; |
| 251 | uint8_t glitch_retry_count; |
| 252 | uint8_t glitch_higherbits_reserved[3]; |
| 253 | uint32_t pl2_location; |
| 254 | uint32_t psp_id; |
| 255 | uint32_t slot_max_size; |
| 256 | uint32_t reserved; |
| 257 | } __attribute__((packed)) ish_directory_table; |
| 258 | |
Zheng Bao | 6be1ab6 | 2021-05-26 10:16:33 +0800 | [diff] [blame] | 259 | #define EMBEDDED_FW_SIGNATURE 0x55aa55aa |
| 260 | #define PSP_COOKIE 0x50535024 /* 'PSP$' */ |
| 261 | #define PSPL2_COOKIE 0x324c5024 /* '2LP$' */ |
| 262 | #define PSP2_COOKIE 0x50535032 /* 'PSP2' */ |
Zheng Bao | 96a3371 | 2021-06-11 15:54:40 +0800 | [diff] [blame] | 263 | #define BHD_COOKIE 0x44484224 /* 'DHB$ */ |
| 264 | #define BHDL2_COOKIE 0x324c4224 /* '2LB$ */ |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 265 | |
Zheng Bao | 3335133 | 2021-10-30 16:53:23 +0800 | [diff] [blame] | 266 | #define PSP_LVL1 (1 << 0) |
| 267 | #define PSP_LVL2 (1 << 1) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 268 | #define PSP_LVL1_AB (1 << 2) |
| 269 | #define PSP_LVL2_AB (1 << 3) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 270 | #define PSP_BOTH (PSP_LVL1 | PSP_LVL2) |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 271 | #define PSP_BOTH_AB (PSP_LVL1_AB | PSP_LVL2_AB) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 272 | typedef struct _amd_fw_entry { |
| 273 | amd_fw_type type; |
| 274 | char *filename; |
| 275 | uint8_t subprog; |
| 276 | int level; |
| 277 | uint64_t other; |
| 278 | } amd_fw_entry; |
| 279 | |
| 280 | typedef struct _amd_cb_config { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 281 | bool have_whitelist; |
| 282 | bool unlock_secure; |
| 283 | bool use_secureos; |
| 284 | bool load_mp2_fw; |
| 285 | bool multi_level; |
| 286 | bool s0i3; |
Zheng Bao | c3007f3 | 2022-04-03 12:53:51 +0800 | [diff] [blame] | 287 | bool second_gen; |
Zheng Bao | 6c5ec8e | 2022-02-11 11:51:26 +0800 | [diff] [blame] | 288 | bool have_mb_spl; |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 289 | bool recovery_ab; |
Karthikeyan Ramasubramanian | ad06bae | 2022-04-08 14:19:55 -0600 | [diff] [blame] | 290 | bool recovery_ab_single_copy; |
Zheng Bao | fdd47ef | 2021-09-17 13:30:08 +0800 | [diff] [blame] | 291 | bool need_ish; |
Zheng Bao | 993b43f | 2021-11-10 12:21:46 +0800 | [diff] [blame] | 292 | bool use_combo; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 293 | } amd_cb_config; |
| 294 | |
| 295 | void register_fw_fuse(char *str); |
| 296 | uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps); |
| 297 | |
| 298 | #define OK 0 |
| 299 | |
| 300 | #define LINE_EOF (1) |
| 301 | #define LINE_TOO_LONG (2) |
| 302 | |
Robert Zieba | 29bc79f | 2022-03-14 15:59:12 -0600 | [diff] [blame] | 303 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 304 | |
| 305 | #endif /* _AMD_FW_TOOL_H_ */ |