blob: 774c2e4968de214aa6868541325bf92948dc16fc [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,
Felix Held9f5a5ee2023-02-01 19:21:11 +010071 AMD_FW_TPMLITE = 0x5f, /* family 17h & 19h */
72 AMD_FW_PSP_SMUSCS = 0x5f, /* family 15h & 16h */
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,
Fred Reitbergerc4f3a332023-02-07 12:12:40 -050090 AMD_FW_UMSMU = 0xa2,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080091 AMD_FW_IMC = 0x200, /* Large enough to be larger than the top BHD entry type. */
92 AMD_FW_GEC,
93 AMD_FW_XHCI,
94 AMD_FW_INVALID, /* Real last one to detect the last entry in table. */
95 AMD_FW_SKIP /* This is for non-applicable options. */
96} amd_fw_type;
97
98typedef enum _amd_bios_type {
Ritul Guru9a321f32022-07-29 11:06:40 +053099 AMD_BIOS_RTM_PUBKEY = 0x05,
100 AMD_BIOS_SIG = 0x07,
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800101 AMD_BIOS_APCB = 0x60,
102 AMD_BIOS_APOB = 0x61,
103 AMD_BIOS_BIN = 0x62,
104 AMD_BIOS_APOB_NV = 0x63,
105 AMD_BIOS_PMUI = 0x64,
106 AMD_BIOS_PMUD = 0x65,
107 AMD_BIOS_UCODE = 0x66,
108 AMD_BIOS_APCB_BK = 0x68,
109 AMD_BIOS_MP2_CFG = 0x6a,
110 AMD_BIOS_PSP_SHARED_MEM = 0x6b,
111 AMD_BIOS_L2_PTR = 0x70,
112 AMD_BIOS_INVALID,
113 AMD_BIOS_SKIP
114} amd_bios_type;
115
Robert Zieba29bc79f2022-03-14 15:59:12 -0600116typedef enum _amd_addr_mode {
117 AMD_ADDR_PHYSICAL = 0, /* Physical address */
118 AMD_ADDR_REL_BIOS, /* Relative to beginning of image */
119 AMD_ADDR_REL_TAB, /* Relative to table */
120 AMD_ADDR_REL_SLOT, /* Relative to slot */
121} amd_addr_mode;
122
Zheng Bao6be1ab62021-05-26 10:16:33 +0800123struct second_gen_efs { /* todo: expand for Server products */
124 int gen:1; /* Client products only use bit 0 */
125 int reserved:31;
126} __attribute__((packed));
127
128#define EFS_SECOND_GEN 0
Zheng Bao487d0452022-04-03 12:50:07 +0800129#define EFS_BEFORE_SECOND_GEN 1
Zheng Bao6be1ab62021-05-26 10:16:33 +0800130
131typedef struct _embedded_firmware {
132 uint32_t signature; /* 0x55aa55aa */
133 uint32_t imc_entry;
134 uint32_t gec_entry;
135 uint32_t xhci_entry;
Felix Heldad68b072021-10-18 14:00:35 +0200136 uint32_t psp_directory;
Zheng Baob749d3f2021-10-23 20:20:21 +0800137 union {
138 uint32_t new_psp_directory;
139 uint32_t combo_psp_directory;
140 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800141 uint32_t bios0_entry; /* todo: add way to select correct entry */
142 uint32_t bios1_entry;
143 uint32_t bios2_entry;
144 struct second_gen_efs efs_gen;
145 uint32_t bios3_entry;
146 uint32_t reserved_2Ch;
147 uint32_t promontory_fw_ptr;
148 uint32_t lp_promontory_fw_ptr;
149 uint32_t reserved_38h;
150 uint32_t reserved_3Ch;
151 uint8_t spi_readmode_f15_mod_60_6f;
152 uint8_t fast_speed_new_f15_mod_60_6f;
153 uint8_t reserved_42h;
154 uint8_t spi_readmode_f17_mod_00_2f;
155 uint8_t spi_fastspeed_f17_mod_00_2f;
156 uint8_t qpr_dummy_cycle_f17_mod_00_2f;
157 uint8_t reserved_46h;
158 uint8_t spi_readmode_f17_mod_30_3f;
159 uint8_t spi_fastspeed_f17_mod_30_3f;
160 uint8_t micron_detect_f17_mod_30_3f;
161 uint8_t reserved_4Ah;
162 uint8_t reserved_4Bh;
163 uint32_t reserved_4Ch;
164} __attribute__((packed, aligned(16))) embedded_firmware;
165
166typedef struct _psp_directory_header {
167 uint32_t cookie;
168 uint32_t checksum;
169 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800170 union {
171 uint32_t additional_info;
172 struct {
173 uint32_t dir_size:10;
174 uint32_t spi_block_size:4;
175 uint32_t base_addr:15;
176 uint32_t address_mode:2;
177 uint32_t not_used:1;
178 } __attribute__((packed)) additional_info_fields;
179 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800180} __attribute__((packed, aligned(16))) psp_directory_header;
181
182typedef struct _psp_directory_entry {
183 uint8_t type;
184 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800185 union {
186 uint16_t rsvd;
187 struct {
188 uint8_t rom_id:2;
189 uint8_t writable:1;
190 uint8_t inst:4;
191 uint8_t rsvd_1:1;
192 uint8_t rsvd_2:8;
193 } __attribute__((packed));
194 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800195 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800196 uint64_t addr:62; /* or a value in some cases */
197 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800198} __attribute__((packed)) psp_directory_entry;
199
200typedef struct _psp_directory_table {
201 psp_directory_header header;
202 psp_directory_entry entries[];
203} __attribute__((packed, aligned(16))) psp_directory_table;
204
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500205#define MAX_PSP_ENTRIES 0x2f
Zheng Bao6be1ab62021-05-26 10:16:33 +0800206
207typedef struct _psp_combo_header {
208 uint32_t cookie;
209 uint32_t checksum;
210 uint32_t num_entries;
211 uint32_t lookup;
212 uint64_t reserved[2];
213} __attribute__((packed, aligned(16))) psp_combo_header;
214
215typedef struct _psp_combo_entry {
216 uint32_t id_sel;
217 uint32_t id;
218 uint64_t lvl2_addr;
219} __attribute__((packed)) psp_combo_entry;
220
221typedef struct _psp_combo_directory {
222 psp_combo_header header;
223 psp_combo_entry entries[];
224} __attribute__((packed, aligned(16))) psp_combo_directory;
225
226#define MAX_COMBO_ENTRIES 1
227
228typedef struct _bios_directory_hdr {
229 uint32_t cookie;
230 uint32_t checksum;
231 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800232 union {
233 uint32_t additional_info;
234 struct {
235 uint32_t dir_size:10;
236 uint32_t spi_block_size:4;
237 uint32_t base_addr:15;
238 uint32_t address_mode:2;
239 uint32_t not_used:1;
240 } __attribute__((packed)) additional_info_fields;
241 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800242} __attribute__((packed, aligned(16))) bios_directory_hdr;
243
244typedef struct _bios_directory_entry {
245 uint8_t type;
246 uint8_t region_type;
247 int reset:1;
248 int copy:1;
249 int ro:1;
250 int compressed:1;
251 int inst:4;
252 uint8_t subprog; /* b[7:3] reserved */
253 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800254 uint64_t source:62;
255 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800256 uint64_t dest;
257} __attribute__((packed)) bios_directory_entry;
258
259typedef struct _bios_directory_table {
260 bios_directory_hdr header;
261 bios_directory_entry entries[];
262} bios_directory_table;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800263
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500264#define MAX_BIOS_ENTRIES 0x2f
265
Zheng Bao33351332021-10-30 16:53:23 +0800266#define BDT_LVL1 (1 << 0)
267#define BDT_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800268#define BDT_LVL1_AB (1 << 2)
269#define BDT_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800270#define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800271#define BDT_BOTH_AB (BDT_LVL1_AB | BDT_LVL2_AB)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800272typedef struct _amd_bios_entry {
273 amd_bios_type type;
274 char *filename;
275 int subpr;
276 int region_type;
277 int reset;
278 int copy;
279 int ro;
280 int zlib;
281 int inst;
282 uint64_t src;
283 uint64_t dest;
284 size_t size;
285 int level;
286} amd_bios_entry;
287
Zheng Baofdd47ef2021-09-17 13:30:08 +0800288typedef struct _ish_directory_table {
289 uint32_t checksum;
290 uint32_t boot_priority;
291 uint32_t update_retry_count;
292 uint8_t glitch_retry_count;
293 uint8_t glitch_higherbits_reserved[3];
294 uint32_t pl2_location;
295 uint32_t psp_id;
296 uint32_t slot_max_size;
297 uint32_t reserved;
298} __attribute__((packed)) ish_directory_table;
299
Zheng Bao6be1ab62021-05-26 10:16:33 +0800300#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
301#define PSP_COOKIE 0x50535024 /* 'PSP$' */
302#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
303#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Zheng Bao96a33712021-06-11 15:54:40 +0800304#define BHD_COOKIE 0x44484224 /* 'DHB$ */
305#define BHDL2_COOKIE 0x324c4224 /* '2LB$ */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800306
Zheng Bao33351332021-10-30 16:53:23 +0800307#define PSP_LVL1 (1 << 0)
308#define PSP_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800309#define PSP_LVL1_AB (1 << 2)
310#define PSP_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800311#define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800312#define PSP_BOTH_AB (PSP_LVL1_AB | PSP_LVL2_AB)
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100313
314typedef struct _amd_fw_entry_hash {
315 uint16_t fw_id;
316 uint16_t subtype;
317 uint32_t sha_len;
318 uint8_t sha[SHA384_DIGEST_LENGTH];
319} amd_fw_entry_hash;
320
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800321typedef struct _amd_fw_entry {
322 amd_fw_type type;
Kangheui Won3c164e12021-12-03 20:25:05 +1100323 /* Mendocino and later SoCs use fw_id instead of fw_type. fw_type is still around
324 for backwards compatibility. fw_id can be populated from the PSP binary file. */
325 uint16_t fw_id;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800326 char *filename;
327 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800328 uint8_t inst;
Ritul Gurua2cb3402022-08-29 00:51:08 +0530329 uint64_t dest;
330 size_t size;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800331 int level;
332 uint64_t other;
Kangheui Won3c164e12021-12-03 20:25:05 +1100333 /* If the binary is signed and the tool is invoked to keep the signed binaries separate,
334 then this field is populated with the offset of the concerned PSP binary (relative to
335 BIOS or PSP Directory table). */
336 uint64_t addr_signed;
337 uint32_t file_size;
338 /* Some files that don't have amd_fw_header have to be skipped from hashing. These files
339 include but not limited to: *iKek*, *.tkn, *.stkn */
340 bool skip_hashing;
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100341 uint32_t num_hash_entries;
342 amd_fw_entry_hash *hash_entries;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800343} amd_fw_entry;
344
Kangheui Won3c164e12021-12-03 20:25:05 +1100345/* Most PSP binaries, if not all, have the following header format. */
346struct amd_fw_header {
347 uint8_t reserved_0[20];
348 uint32_t fw_size_signed;
349 uint8_t reserved_18[24];
350 /* 1 if the image is signed, 0 otherwise */
351 uint32_t sig_opt;
352 uint32_t sig_id;
353 uint8_t sig_param[16];
354 uint32_t comp_opt;
355 uint8_t reserved_4c[4];
356 uint32_t uncomp_size;
357 uint32_t comp_size;
358 /* Starting MDN fw_id is populated instead of fw_type. */
359 uint16_t fw_id;
360 uint8_t reserved_5a[18];
361 uint32_t size_total;
362 uint8_t reserved_70[12];
363 /* Starting MDN fw_id is populated instead of fw_type. fw_type will still be around
364 for backwards compatibility. */
365 uint8_t fw_type;
366 uint8_t fw_subtype;
367 uint8_t fw_subprog;
368 uint8_t reserved_7f;
369 uint8_t reserved_80[128];
370} __packed;
371
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100372struct psp_fw_hash_table {
373 uint16_t version;
374 uint16_t no_of_entries_256;
375 uint16_t no_of_entries_384;
376 /* The next 2 elements are pointers to arrays of SHA256 and SHA384 entries. */
377 /* It does not make sense to store pointers in the CBFS file */
378} __packed;
379
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800380typedef struct _amd_cb_config {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800381 bool have_whitelist;
382 bool unlock_secure;
383 bool use_secureos;
384 bool load_mp2_fw;
385 bool multi_level;
386 bool s0i3;
Zheng Baoc3007f32022-04-03 12:53:51 +0800387 bool second_gen;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800388 bool have_mb_spl;
Zheng Bao990d1542021-09-17 13:24:54 +0800389 bool recovery_ab;
Karthikeyan Ramasubramanianad06bae2022-04-08 14:19:55 -0600390 bool recovery_ab_single_copy;
Zheng Baofdd47ef2021-09-17 13:30:08 +0800391 bool need_ish;
Zheng Bao993b43f2021-11-10 12:21:46 +0800392 bool use_combo;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800393} amd_cb_config;
394
395void register_fw_fuse(char *str);
396uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps);
397
398#define OK 0
399
400#define LINE_EOF (1)
401#define LINE_TOO_LONG (2)
402
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800403#endif /* _AMD_FW_TOOL_H_ */