blob: d3d686669ef6bf37937c06be431cc640c4f060f8 [file] [log] [blame]
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#ifndef _AMD_FW_TOOL_H_
4#define _AMD_FW_TOOL_H_
5
Karthikeyan Ramasubramanian236245e2022-09-06 14:02:41 -06006#include <commonlib/bsd/compiler.h>
Elyes Haouas7d67a192022-10-14 09:58:29 +02007#include <commonlib/bsd/helpers.h>
Kangheui Won5b84dfd2021-12-21 15:45:06 +11008#include <openssl/sha.h>
Zheng Bao6be1ab62021-05-26 10:16:33 +08009#include <stdint.h>
Zheng Baoba3af5e2021-11-04 18:56:47 +080010#include <stdbool.h>
Zheng Bao6be1ab62021-05-26 10:16:33 +080011
Zheng Baoc5e28ab2020-10-28 11:38:09 +080012typedef enum _amd_fw_type {
Arthur Heymansaafbe132022-09-30 08:33:28 +020013 AMD_FW_PSP_PUBKEY = 0x00,
14 AMD_FW_PSP_BOOTLOADER = 0x01,
15 AMD_FW_PSP_SECURED_OS = 0x02,
16 AMD_FW_PSP_RECOVERY = 0x03,
17 AMD_FW_PSP_NVRAM = 0x04,
18 AMD_FW_PSP_RTM_PUBKEY = 0x05,
19 AMD_FW_PSP_SMU_FIRMWARE = 0x08,
20 AMD_FW_PSP_SECURED_DEBUG = 0x09,
Arthur Heymans1f05c802022-10-04 17:50:21 +020021 AMD_FW_ABL_PUBKEY = 0x0a,
Arthur Heymansaafbe132022-09-30 08:33:28 +020022 AMD_PSP_FUSE_CHAIN = 0x0b,
23 AMD_FW_PSP_TRUSTLETS = 0x0c,
24 AMD_FW_PSP_TRUSTLETKEY = 0x0d,
25 AMD_FW_PSP_SMU_FIRMWARE2 = 0x12,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080026 AMD_DEBUG_UNLOCK = 0x13,
Zheng Bao8eba6622022-10-16 20:29:03 +080027 AMD_BOOT_DRIVER = 0x1b,
28 AMD_SOC_DRIVER = 0x1c,
29 AMD_DEBUG_DRIVER = 0x1d,
30 AMD_INTERFACE_DRIVER = 0x1f,
Zheng Baobf29a0d2020-12-03 23:00:48 +080031 AMD_HW_IPCFG = 0x20,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080032 AMD_WRAPPED_IKEK = 0x21,
33 AMD_TOKEN_UNLOCK = 0x22,
34 AMD_SEC_GASKET = 0x24,
35 AMD_MP2_FW = 0x25,
36 AMD_DRIVER_ENTRIES = 0x28,
Zheng Baobf29a0d2020-12-03 23:00:48 +080037 AMD_FW_KVM_IMAGE = 0x29,
Arthur Heymans1f05c802022-10-04 17:50:21 +020038 AMD_FW_MP5 = 0x2a,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080039 AMD_S0I3_DRIVER = 0x2d,
40 AMD_ABL0 = 0x30,
41 AMD_ABL1 = 0x31,
42 AMD_ABL2 = 0x32,
43 AMD_ABL3 = 0x33,
44 AMD_ABL4 = 0x34,
45 AMD_ABL5 = 0x35,
46 AMD_ABL6 = 0x36,
47 AMD_ABL7 = 0x37,
Arthur Heymans1f05c802022-10-04 17:50:21 +020048 AMD_SEV_DATA = 0x38,
49 AMD_SEV_CODE = 0x39,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080050 AMD_FW_PSP_WHITELIST = 0x3a,
Zheng Baobf29a0d2020-12-03 23:00:48 +080051 AMD_VBIOS_BTLOADER = 0x3c,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080052 AMD_FW_L2_PTR = 0x40,
Arthur Heymans1f05c802022-10-04 17:50:21 +020053 AMD_FW_DXIO = 0x42,
Zheng Baobf29a0d2020-12-03 23:00:48 +080054 AMD_FW_USB_PHY = 0x44,
55 AMD_FW_TOS_SEC_POLICY = 0x45,
56 AMD_FW_DRTM_TA = 0x47,
Zheng Bao990d1542021-09-17 13:24:54 +080057 AMD_FW_RECOVERYAB_A = 0x48,
58 AMD_FW_RECOVERYAB_B = 0x4A,
59 AMD_FW_BIOS_TABLE = 0x49,
Zheng Baobf29a0d2020-12-03 23:00:48 +080060 AMD_FW_KEYDB_BL = 0x50,
61 AMD_FW_KEYDB_TOS = 0x51,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080062 AMD_FW_PSP_VERSTAGE = 0x52,
63 AMD_FW_VERSTAGE_SIG = 0x53,
Zheng Baobf29a0d2020-12-03 23:00:48 +080064 AMD_RPMC_NVRAM = 0x54,
Zheng Baoab84fd72022-01-27 22:38:27 +080065 AMD_FW_SPL = 0x55,
Zheng Baobf29a0d2020-12-03 23:00:48 +080066 AMD_FW_DMCU_ERAM = 0x58,
67 AMD_FW_DMCU_ISR = 0x59,
Felix Held5f18bb72022-03-24 02:04:51 +010068 AMD_FW_MSMU = 0x5a,
69 AMD_FW_SPIROM_CFG = 0x5c,
Arthur Heymans1f05c802022-10-04 17:50:21 +020070 AMD_FW_MPIO = 0x5d,
Zheng Bao8eba6622022-10-16 20:29:03 +080071 AMD_FW_TPMLITE = 0x5f,
Arthur Heymansaafbe132022-09-30 08:33:28 +020072 AMD_FW_PSP_SMUSCS = 0x5f,
Felix Held5f18bb72022-03-24 02:04:51 +010073 AMD_FW_DMCUB = 0x71,
Zheng Baob993cb22021-02-02 18:48:23 +080074 AMD_FW_PSP_BOOTLOADER_AB = 0x73,
Arthur Heymans1f05c802022-10-04 17:50:21 +020075 AMD_RIB = 0x76,
Zheng Bao8eba6622022-10-16 20:29:03 +080076 AMD_FW_AMF_SRAM = 0x85,
77 AMD_FW_AMF_DRAM = 0x86,
78 AMD_FW_AMF_WLAN = 0x88,
79 AMD_FW_AMF_MFD = 0x89,
Arthur Heymans1f05c802022-10-04 17:50:21 +020080 AMD_FW_MPDMA_TF = 0x8c,
Karthikeyan Ramasubramanian0ab04d22022-05-03 18:16:34 -060081 AMD_TA_IKEK = 0x8d,
Zheng Bao8eba6622022-10-16 20:29:03 +080082 AMD_FW_MPCCX = 0x90,
Arthur Heymans1f05c802022-10-04 17:50:21 +020083 AMD_FW_GMI3_PHY = 0x91,
84 AMD_FW_MPDMA_PM = 0x92,
Zheng Bao8eba6622022-10-16 20:29:03 +080085 AMD_FW_LSDMA = 0x94,
86 AMD_FW_C20_MP = 0x95,
87 AMD_FW_FCFG_TABLE = 0x98,
88 AMD_FW_MINIMSMU = 0x9a,
89 AMD_FW_SRAM_FW_EXT = 0x9d,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080090 AMD_FW_IMC = 0x200, /* Large enough to be larger than the top BHD entry type. */
91 AMD_FW_GEC,
92 AMD_FW_XHCI,
93 AMD_FW_INVALID, /* Real last one to detect the last entry in table. */
94 AMD_FW_SKIP /* This is for non-applicable options. */
95} amd_fw_type;
96
97typedef enum _amd_bios_type {
Ritul Guru9a321f32022-07-29 11:06:40 +053098 AMD_BIOS_RTM_PUBKEY = 0x05,
99 AMD_BIOS_SIG = 0x07,
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800100 AMD_BIOS_APCB = 0x60,
101 AMD_BIOS_APOB = 0x61,
102 AMD_BIOS_BIN = 0x62,
103 AMD_BIOS_APOB_NV = 0x63,
104 AMD_BIOS_PMUI = 0x64,
105 AMD_BIOS_PMUD = 0x65,
106 AMD_BIOS_UCODE = 0x66,
107 AMD_BIOS_APCB_BK = 0x68,
108 AMD_BIOS_MP2_CFG = 0x6a,
109 AMD_BIOS_PSP_SHARED_MEM = 0x6b,
110 AMD_BIOS_L2_PTR = 0x70,
111 AMD_BIOS_INVALID,
112 AMD_BIOS_SKIP
113} amd_bios_type;
114
Robert Zieba29bc79f2022-03-14 15:59:12 -0600115typedef enum _amd_addr_mode {
116 AMD_ADDR_PHYSICAL = 0, /* Physical address */
117 AMD_ADDR_REL_BIOS, /* Relative to beginning of image */
118 AMD_ADDR_REL_TAB, /* Relative to table */
119 AMD_ADDR_REL_SLOT, /* Relative to slot */
120} amd_addr_mode;
121
Zheng Bao6be1ab62021-05-26 10:16:33 +0800122struct second_gen_efs { /* todo: expand for Server products */
123 int gen:1; /* Client products only use bit 0 */
124 int reserved:31;
125} __attribute__((packed));
126
127#define EFS_SECOND_GEN 0
Zheng Bao487d0452022-04-03 12:50:07 +0800128#define EFS_BEFORE_SECOND_GEN 1
Zheng Bao6be1ab62021-05-26 10:16:33 +0800129
130typedef struct _embedded_firmware {
131 uint32_t signature; /* 0x55aa55aa */
132 uint32_t imc_entry;
133 uint32_t gec_entry;
134 uint32_t xhci_entry;
Felix Heldad68b072021-10-18 14:00:35 +0200135 uint32_t psp_directory;
Zheng Baob749d3f2021-10-23 20:20:21 +0800136 union {
137 uint32_t new_psp_directory;
138 uint32_t combo_psp_directory;
139 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800140 uint32_t bios0_entry; /* todo: add way to select correct entry */
141 uint32_t bios1_entry;
142 uint32_t bios2_entry;
143 struct second_gen_efs efs_gen;
144 uint32_t bios3_entry;
145 uint32_t reserved_2Ch;
146 uint32_t promontory_fw_ptr;
147 uint32_t lp_promontory_fw_ptr;
148 uint32_t reserved_38h;
149 uint32_t reserved_3Ch;
150 uint8_t spi_readmode_f15_mod_60_6f;
151 uint8_t fast_speed_new_f15_mod_60_6f;
152 uint8_t reserved_42h;
153 uint8_t spi_readmode_f17_mod_00_2f;
154 uint8_t spi_fastspeed_f17_mod_00_2f;
155 uint8_t qpr_dummy_cycle_f17_mod_00_2f;
156 uint8_t reserved_46h;
157 uint8_t spi_readmode_f17_mod_30_3f;
158 uint8_t spi_fastspeed_f17_mod_30_3f;
159 uint8_t micron_detect_f17_mod_30_3f;
160 uint8_t reserved_4Ah;
161 uint8_t reserved_4Bh;
162 uint32_t reserved_4Ch;
163} __attribute__((packed, aligned(16))) embedded_firmware;
164
165typedef struct _psp_directory_header {
166 uint32_t cookie;
167 uint32_t checksum;
168 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800169 union {
170 uint32_t additional_info;
171 struct {
172 uint32_t dir_size:10;
173 uint32_t spi_block_size:4;
174 uint32_t base_addr:15;
175 uint32_t address_mode:2;
176 uint32_t not_used:1;
177 } __attribute__((packed)) additional_info_fields;
178 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800179} __attribute__((packed, aligned(16))) psp_directory_header;
180
181typedef struct _psp_directory_entry {
182 uint8_t type;
183 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800184 union {
185 uint16_t rsvd;
186 struct {
187 uint8_t rom_id:2;
188 uint8_t writable:1;
189 uint8_t inst:4;
190 uint8_t rsvd_1:1;
191 uint8_t rsvd_2:8;
192 } __attribute__((packed));
193 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800194 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800195 uint64_t addr:62; /* or a value in some cases */
196 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800197} __attribute__((packed)) psp_directory_entry;
198
199typedef struct _psp_directory_table {
200 psp_directory_header header;
201 psp_directory_entry entries[];
202} __attribute__((packed, aligned(16))) psp_directory_table;
203
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500204#define MAX_PSP_ENTRIES 0x2f
Zheng Bao6be1ab62021-05-26 10:16:33 +0800205
206typedef struct _psp_combo_header {
207 uint32_t cookie;
208 uint32_t checksum;
209 uint32_t num_entries;
210 uint32_t lookup;
211 uint64_t reserved[2];
212} __attribute__((packed, aligned(16))) psp_combo_header;
213
214typedef struct _psp_combo_entry {
215 uint32_t id_sel;
216 uint32_t id;
217 uint64_t lvl2_addr;
218} __attribute__((packed)) psp_combo_entry;
219
220typedef struct _psp_combo_directory {
221 psp_combo_header header;
222 psp_combo_entry entries[];
223} __attribute__((packed, aligned(16))) psp_combo_directory;
224
225#define MAX_COMBO_ENTRIES 1
226
227typedef struct _bios_directory_hdr {
228 uint32_t cookie;
229 uint32_t checksum;
230 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800231 union {
232 uint32_t additional_info;
233 struct {
234 uint32_t dir_size:10;
235 uint32_t spi_block_size:4;
236 uint32_t base_addr:15;
237 uint32_t address_mode:2;
238 uint32_t not_used:1;
239 } __attribute__((packed)) additional_info_fields;
240 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800241} __attribute__((packed, aligned(16))) bios_directory_hdr;
242
243typedef struct _bios_directory_entry {
244 uint8_t type;
245 uint8_t region_type;
246 int reset:1;
247 int copy:1;
248 int ro:1;
249 int compressed:1;
250 int inst:4;
251 uint8_t subprog; /* b[7:3] reserved */
252 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800253 uint64_t source:62;
254 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800255 uint64_t dest;
256} __attribute__((packed)) bios_directory_entry;
257
258typedef struct _bios_directory_table {
259 bios_directory_hdr header;
260 bios_directory_entry entries[];
261} bios_directory_table;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800262
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500263#define MAX_BIOS_ENTRIES 0x2f
264
Zheng Bao33351332021-10-30 16:53:23 +0800265#define BDT_LVL1 (1 << 0)
266#define BDT_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800267#define BDT_LVL1_AB (1 << 2)
268#define BDT_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800269#define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800270#define BDT_BOTH_AB (BDT_LVL1_AB | BDT_LVL2_AB)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800271typedef struct _amd_bios_entry {
272 amd_bios_type type;
273 char *filename;
274 int subpr;
275 int region_type;
276 int reset;
277 int copy;
278 int ro;
279 int zlib;
280 int inst;
281 uint64_t src;
282 uint64_t dest;
283 size_t size;
284 int level;
285} amd_bios_entry;
286
Zheng Baofdd47ef2021-09-17 13:30:08 +0800287typedef struct _ish_directory_table {
288 uint32_t checksum;
289 uint32_t boot_priority;
290 uint32_t update_retry_count;
291 uint8_t glitch_retry_count;
292 uint8_t glitch_higherbits_reserved[3];
293 uint32_t pl2_location;
294 uint32_t psp_id;
295 uint32_t slot_max_size;
296 uint32_t reserved;
297} __attribute__((packed)) ish_directory_table;
298
Zheng Bao6be1ab62021-05-26 10:16:33 +0800299#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
300#define PSP_COOKIE 0x50535024 /* 'PSP$' */
301#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
302#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Zheng Bao96a33712021-06-11 15:54:40 +0800303#define BHD_COOKIE 0x44484224 /* 'DHB$ */
304#define BHDL2_COOKIE 0x324c4224 /* '2LB$ */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800305
Zheng Bao33351332021-10-30 16:53:23 +0800306#define PSP_LVL1 (1 << 0)
307#define PSP_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800308#define PSP_LVL1_AB (1 << 2)
309#define PSP_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800310#define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800311#define PSP_BOTH_AB (PSP_LVL1_AB | PSP_LVL2_AB)
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100312
313typedef struct _amd_fw_entry_hash {
314 uint16_t fw_id;
315 uint16_t subtype;
316 uint32_t sha_len;
317 uint8_t sha[SHA384_DIGEST_LENGTH];
318} amd_fw_entry_hash;
319
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800320typedef struct _amd_fw_entry {
321 amd_fw_type type;
Kangheui Won3c164e12021-12-03 20:25:05 +1100322 /* Mendocino and later SoCs use fw_id instead of fw_type. fw_type is still around
323 for backwards compatibility. fw_id can be populated from the PSP binary file. */
324 uint16_t fw_id;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800325 char *filename;
326 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800327 uint8_t inst;
Ritul Gurua2cb3402022-08-29 00:51:08 +0530328 uint64_t dest;
329 size_t size;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800330 int level;
331 uint64_t other;
Kangheui Won3c164e12021-12-03 20:25:05 +1100332 /* If the binary is signed and the tool is invoked to keep the signed binaries separate,
333 then this field is populated with the offset of the concerned PSP binary (relative to
334 BIOS or PSP Directory table). */
335 uint64_t addr_signed;
336 uint32_t file_size;
337 /* Some files that don't have amd_fw_header have to be skipped from hashing. These files
338 include but not limited to: *iKek*, *.tkn, *.stkn */
339 bool skip_hashing;
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100340 uint32_t num_hash_entries;
341 amd_fw_entry_hash *hash_entries;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800342} amd_fw_entry;
343
Kangheui Won3c164e12021-12-03 20:25:05 +1100344/* Most PSP binaries, if not all, have the following header format. */
345struct amd_fw_header {
346 uint8_t reserved_0[20];
347 uint32_t fw_size_signed;
348 uint8_t reserved_18[24];
349 /* 1 if the image is signed, 0 otherwise */
350 uint32_t sig_opt;
351 uint32_t sig_id;
352 uint8_t sig_param[16];
353 uint32_t comp_opt;
354 uint8_t reserved_4c[4];
355 uint32_t uncomp_size;
356 uint32_t comp_size;
357 /* Starting MDN fw_id is populated instead of fw_type. */
358 uint16_t fw_id;
359 uint8_t reserved_5a[18];
360 uint32_t size_total;
361 uint8_t reserved_70[12];
362 /* Starting MDN fw_id is populated instead of fw_type. fw_type will still be around
363 for backwards compatibility. */
364 uint8_t fw_type;
365 uint8_t fw_subtype;
366 uint8_t fw_subprog;
367 uint8_t reserved_7f;
368 uint8_t reserved_80[128];
369} __packed;
370
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100371struct psp_fw_hash_table {
372 uint16_t version;
373 uint16_t no_of_entries_256;
374 uint16_t no_of_entries_384;
375 /* The next 2 elements are pointers to arrays of SHA256 and SHA384 entries. */
376 /* It does not make sense to store pointers in the CBFS file */
377} __packed;
378
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800379typedef struct _amd_cb_config {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800380 bool have_whitelist;
381 bool unlock_secure;
382 bool use_secureos;
383 bool load_mp2_fw;
384 bool multi_level;
385 bool s0i3;
Zheng Baoc3007f32022-04-03 12:53:51 +0800386 bool second_gen;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800387 bool have_mb_spl;
Zheng Bao990d1542021-09-17 13:24:54 +0800388 bool recovery_ab;
Karthikeyan Ramasubramanianad06bae2022-04-08 14:19:55 -0600389 bool recovery_ab_single_copy;
Zheng Baofdd47ef2021-09-17 13:30:08 +0800390 bool need_ish;
Zheng Bao993b43f2021-11-10 12:21:46 +0800391 bool use_combo;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800392} amd_cb_config;
393
394void register_fw_fuse(char *str);
395uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps);
396
397#define OK 0
398
399#define LINE_EOF (1)
400#define LINE_TOO_LONG (2)
401
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800402#endif /* _AMD_FW_TOOL_H_ */