blob: a4ebbcd974d1777a944daebe59958df55f5a8668 [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 Baobf29a0d2020-12-03 23:00:48 +080027 AMD_HW_IPCFG = 0x20,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080028 AMD_WRAPPED_IKEK = 0x21,
29 AMD_TOKEN_UNLOCK = 0x22,
30 AMD_SEC_GASKET = 0x24,
31 AMD_MP2_FW = 0x25,
32 AMD_DRIVER_ENTRIES = 0x28,
Zheng Baobf29a0d2020-12-03 23:00:48 +080033 AMD_FW_KVM_IMAGE = 0x29,
Arthur Heymans1f05c802022-10-04 17:50:21 +020034 AMD_FW_MP5 = 0x2a,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080035 AMD_S0I3_DRIVER = 0x2d,
36 AMD_ABL0 = 0x30,
37 AMD_ABL1 = 0x31,
38 AMD_ABL2 = 0x32,
39 AMD_ABL3 = 0x33,
40 AMD_ABL4 = 0x34,
41 AMD_ABL5 = 0x35,
42 AMD_ABL6 = 0x36,
43 AMD_ABL7 = 0x37,
Arthur Heymans1f05c802022-10-04 17:50:21 +020044 AMD_SEV_DATA = 0x38,
45 AMD_SEV_CODE = 0x39,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080046 AMD_FW_PSP_WHITELIST = 0x3a,
Zheng Baobf29a0d2020-12-03 23:00:48 +080047 AMD_VBIOS_BTLOADER = 0x3c,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080048 AMD_FW_L2_PTR = 0x40,
Arthur Heymans1f05c802022-10-04 17:50:21 +020049 AMD_FW_DXIO = 0x42,
Zheng Baobf29a0d2020-12-03 23:00:48 +080050 AMD_FW_USB_PHY = 0x44,
51 AMD_FW_TOS_SEC_POLICY = 0x45,
52 AMD_FW_DRTM_TA = 0x47,
Zheng Bao990d1542021-09-17 13:24:54 +080053 AMD_FW_RECOVERYAB_A = 0x48,
54 AMD_FW_RECOVERYAB_B = 0x4A,
55 AMD_FW_BIOS_TABLE = 0x49,
Zheng Baobf29a0d2020-12-03 23:00:48 +080056 AMD_FW_KEYDB_BL = 0x50,
57 AMD_FW_KEYDB_TOS = 0x51,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080058 AMD_FW_PSP_VERSTAGE = 0x52,
59 AMD_FW_VERSTAGE_SIG = 0x53,
Zheng Baobf29a0d2020-12-03 23:00:48 +080060 AMD_RPMC_NVRAM = 0x54,
Zheng Baoab84fd72022-01-27 22:38:27 +080061 AMD_FW_SPL = 0x55,
Zheng Baobf29a0d2020-12-03 23:00:48 +080062 AMD_FW_DMCU_ERAM = 0x58,
63 AMD_FW_DMCU_ISR = 0x59,
Felix Held5f18bb72022-03-24 02:04:51 +010064 AMD_FW_MSMU = 0x5a,
65 AMD_FW_SPIROM_CFG = 0x5c,
Arthur Heymans1f05c802022-10-04 17:50:21 +020066 AMD_FW_MPIO = 0x5d,
Arthur Heymansaafbe132022-09-30 08:33:28 +020067 AMD_FW_PSP_SMUSCS = 0x5f,
Felix Held5f18bb72022-03-24 02:04:51 +010068 AMD_FW_DMCUB = 0x71,
Zheng Baob993cb22021-02-02 18:48:23 +080069 AMD_FW_PSP_BOOTLOADER_AB = 0x73,
Arthur Heymans1f05c802022-10-04 17:50:21 +020070 AMD_RIB = 0x76,
71 AMD_FW_MPDMA_TF = 0x8c,
Karthikeyan Ramasubramanian0ab04d22022-05-03 18:16:34 -060072 AMD_TA_IKEK = 0x8d,
Arthur Heymans1f05c802022-10-04 17:50:21 +020073 AMD_FW_GMI3_PHY = 0x91,
74 AMD_FW_MPDMA_PM = 0x92,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080075 AMD_FW_IMC = 0x200, /* Large enough to be larger than the top BHD entry type. */
76 AMD_FW_GEC,
77 AMD_FW_XHCI,
78 AMD_FW_INVALID, /* Real last one to detect the last entry in table. */
79 AMD_FW_SKIP /* This is for non-applicable options. */
80} amd_fw_type;
81
82typedef enum _amd_bios_type {
Ritul Guru9a321f32022-07-29 11:06:40 +053083 AMD_BIOS_RTM_PUBKEY = 0x05,
84 AMD_BIOS_SIG = 0x07,
Zheng Baoc5e28ab2020-10-28 11:38:09 +080085 AMD_BIOS_APCB = 0x60,
86 AMD_BIOS_APOB = 0x61,
87 AMD_BIOS_BIN = 0x62,
88 AMD_BIOS_APOB_NV = 0x63,
89 AMD_BIOS_PMUI = 0x64,
90 AMD_BIOS_PMUD = 0x65,
91 AMD_BIOS_UCODE = 0x66,
92 AMD_BIOS_APCB_BK = 0x68,
93 AMD_BIOS_MP2_CFG = 0x6a,
94 AMD_BIOS_PSP_SHARED_MEM = 0x6b,
95 AMD_BIOS_L2_PTR = 0x70,
96 AMD_BIOS_INVALID,
97 AMD_BIOS_SKIP
98} amd_bios_type;
99
Robert Zieba29bc79f2022-03-14 15:59:12 -0600100typedef enum _amd_addr_mode {
101 AMD_ADDR_PHYSICAL = 0, /* Physical address */
102 AMD_ADDR_REL_BIOS, /* Relative to beginning of image */
103 AMD_ADDR_REL_TAB, /* Relative to table */
104 AMD_ADDR_REL_SLOT, /* Relative to slot */
105} amd_addr_mode;
106
Zheng Bao6be1ab62021-05-26 10:16:33 +0800107struct second_gen_efs { /* todo: expand for Server products */
108 int gen:1; /* Client products only use bit 0 */
109 int reserved:31;
110} __attribute__((packed));
111
112#define EFS_SECOND_GEN 0
Zheng Bao487d0452022-04-03 12:50:07 +0800113#define EFS_BEFORE_SECOND_GEN 1
Zheng Bao6be1ab62021-05-26 10:16:33 +0800114
115typedef struct _embedded_firmware {
116 uint32_t signature; /* 0x55aa55aa */
117 uint32_t imc_entry;
118 uint32_t gec_entry;
119 uint32_t xhci_entry;
Felix Heldad68b072021-10-18 14:00:35 +0200120 uint32_t psp_directory;
Zheng Baob749d3f2021-10-23 20:20:21 +0800121 union {
122 uint32_t new_psp_directory;
123 uint32_t combo_psp_directory;
124 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800125 uint32_t bios0_entry; /* todo: add way to select correct entry */
126 uint32_t bios1_entry;
127 uint32_t bios2_entry;
128 struct second_gen_efs efs_gen;
129 uint32_t bios3_entry;
130 uint32_t reserved_2Ch;
131 uint32_t promontory_fw_ptr;
132 uint32_t lp_promontory_fw_ptr;
133 uint32_t reserved_38h;
134 uint32_t reserved_3Ch;
135 uint8_t spi_readmode_f15_mod_60_6f;
136 uint8_t fast_speed_new_f15_mod_60_6f;
137 uint8_t reserved_42h;
138 uint8_t spi_readmode_f17_mod_00_2f;
139 uint8_t spi_fastspeed_f17_mod_00_2f;
140 uint8_t qpr_dummy_cycle_f17_mod_00_2f;
141 uint8_t reserved_46h;
142 uint8_t spi_readmode_f17_mod_30_3f;
143 uint8_t spi_fastspeed_f17_mod_30_3f;
144 uint8_t micron_detect_f17_mod_30_3f;
145 uint8_t reserved_4Ah;
146 uint8_t reserved_4Bh;
147 uint32_t reserved_4Ch;
148} __attribute__((packed, aligned(16))) embedded_firmware;
149
150typedef struct _psp_directory_header {
151 uint32_t cookie;
152 uint32_t checksum;
153 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800154 union {
155 uint32_t additional_info;
156 struct {
157 uint32_t dir_size:10;
158 uint32_t spi_block_size:4;
159 uint32_t base_addr:15;
160 uint32_t address_mode:2;
161 uint32_t not_used:1;
162 } __attribute__((packed)) additional_info_fields;
163 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800164} __attribute__((packed, aligned(16))) psp_directory_header;
165
166typedef struct _psp_directory_entry {
167 uint8_t type;
168 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800169 union {
170 uint16_t rsvd;
171 struct {
172 uint8_t rom_id:2;
173 uint8_t writable:1;
174 uint8_t inst:4;
175 uint8_t rsvd_1:1;
176 uint8_t rsvd_2:8;
177 } __attribute__((packed));
178 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800179 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800180 uint64_t addr:62; /* or a value in some cases */
181 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800182} __attribute__((packed)) psp_directory_entry;
183
184typedef struct _psp_directory_table {
185 psp_directory_header header;
186 psp_directory_entry entries[];
187} __attribute__((packed, aligned(16))) psp_directory_table;
188
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500189#define MAX_PSP_ENTRIES 0x2f
Zheng Bao6be1ab62021-05-26 10:16:33 +0800190
191typedef struct _psp_combo_header {
192 uint32_t cookie;
193 uint32_t checksum;
194 uint32_t num_entries;
195 uint32_t lookup;
196 uint64_t reserved[2];
197} __attribute__((packed, aligned(16))) psp_combo_header;
198
199typedef struct _psp_combo_entry {
200 uint32_t id_sel;
201 uint32_t id;
202 uint64_t lvl2_addr;
203} __attribute__((packed)) psp_combo_entry;
204
205typedef struct _psp_combo_directory {
206 psp_combo_header header;
207 psp_combo_entry entries[];
208} __attribute__((packed, aligned(16))) psp_combo_directory;
209
210#define MAX_COMBO_ENTRIES 1
211
212typedef struct _bios_directory_hdr {
213 uint32_t cookie;
214 uint32_t checksum;
215 uint32_t num_entries;
Zheng Bao6fff2492021-11-15 19:53:21 +0800216 union {
217 uint32_t additional_info;
218 struct {
219 uint32_t dir_size:10;
220 uint32_t spi_block_size:4;
221 uint32_t base_addr:15;
222 uint32_t address_mode:2;
223 uint32_t not_used:1;
224 } __attribute__((packed)) additional_info_fields;
225 };
Zheng Bao6be1ab62021-05-26 10:16:33 +0800226} __attribute__((packed, aligned(16))) bios_directory_hdr;
227
228typedef struct _bios_directory_entry {
229 uint8_t type;
230 uint8_t region_type;
231 int reset:1;
232 int copy:1;
233 int ro:1;
234 int compressed:1;
235 int inst:4;
236 uint8_t subprog; /* b[7:3] reserved */
237 uint32_t size;
Zheng Bao6fff2492021-11-15 19:53:21 +0800238 uint64_t source:62;
239 uint64_t address_mode:2;
Zheng Bao6be1ab62021-05-26 10:16:33 +0800240 uint64_t dest;
241} __attribute__((packed)) bios_directory_entry;
242
243typedef struct _bios_directory_table {
244 bios_directory_hdr header;
245 bios_directory_entry entries[];
246} bios_directory_table;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800247
Altamshali Hirani8915abe2022-03-17 13:26:31 -0500248#define MAX_BIOS_ENTRIES 0x2f
249
Zheng Bao33351332021-10-30 16:53:23 +0800250#define BDT_LVL1 (1 << 0)
251#define BDT_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800252#define BDT_LVL1_AB (1 << 2)
253#define BDT_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800254#define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800255#define BDT_BOTH_AB (BDT_LVL1_AB | BDT_LVL2_AB)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800256typedef struct _amd_bios_entry {
257 amd_bios_type type;
258 char *filename;
259 int subpr;
260 int region_type;
261 int reset;
262 int copy;
263 int ro;
264 int zlib;
265 int inst;
266 uint64_t src;
267 uint64_t dest;
268 size_t size;
269 int level;
270} amd_bios_entry;
271
Zheng Baofdd47ef2021-09-17 13:30:08 +0800272typedef struct _ish_directory_table {
273 uint32_t checksum;
274 uint32_t boot_priority;
275 uint32_t update_retry_count;
276 uint8_t glitch_retry_count;
277 uint8_t glitch_higherbits_reserved[3];
278 uint32_t pl2_location;
279 uint32_t psp_id;
280 uint32_t slot_max_size;
281 uint32_t reserved;
282} __attribute__((packed)) ish_directory_table;
283
Zheng Bao6be1ab62021-05-26 10:16:33 +0800284#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
285#define PSP_COOKIE 0x50535024 /* 'PSP$' */
286#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
287#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Zheng Bao96a33712021-06-11 15:54:40 +0800288#define BHD_COOKIE 0x44484224 /* 'DHB$ */
289#define BHDL2_COOKIE 0x324c4224 /* '2LB$ */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800290
Zheng Bao33351332021-10-30 16:53:23 +0800291#define PSP_LVL1 (1 << 0)
292#define PSP_LVL2 (1 << 1)
Zheng Bao990d1542021-09-17 13:24:54 +0800293#define PSP_LVL1_AB (1 << 2)
294#define PSP_LVL2_AB (1 << 3)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800295#define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
Zheng Bao990d1542021-09-17 13:24:54 +0800296#define PSP_BOTH_AB (PSP_LVL1_AB | PSP_LVL2_AB)
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100297
298typedef struct _amd_fw_entry_hash {
299 uint16_t fw_id;
300 uint16_t subtype;
301 uint32_t sha_len;
302 uint8_t sha[SHA384_DIGEST_LENGTH];
303} amd_fw_entry_hash;
304
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800305typedef struct _amd_fw_entry {
306 amd_fw_type type;
Kangheui Won3c164e12021-12-03 20:25:05 +1100307 /* Mendocino and later SoCs use fw_id instead of fw_type. fw_type is still around
308 for backwards compatibility. fw_id can be populated from the PSP binary file. */
309 uint16_t fw_id;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800310 char *filename;
311 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800312 uint8_t inst;
Ritul Gurua2cb3402022-08-29 00:51:08 +0530313 uint64_t dest;
314 size_t size;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800315 int level;
316 uint64_t other;
Kangheui Won3c164e12021-12-03 20:25:05 +1100317 /* If the binary is signed and the tool is invoked to keep the signed binaries separate,
318 then this field is populated with the offset of the concerned PSP binary (relative to
319 BIOS or PSP Directory table). */
320 uint64_t addr_signed;
321 uint32_t file_size;
322 /* Some files that don't have amd_fw_header have to be skipped from hashing. These files
323 include but not limited to: *iKek*, *.tkn, *.stkn */
324 bool skip_hashing;
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100325 uint32_t num_hash_entries;
326 amd_fw_entry_hash *hash_entries;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800327} amd_fw_entry;
328
Kangheui Won3c164e12021-12-03 20:25:05 +1100329/* Most PSP binaries, if not all, have the following header format. */
330struct amd_fw_header {
331 uint8_t reserved_0[20];
332 uint32_t fw_size_signed;
333 uint8_t reserved_18[24];
334 /* 1 if the image is signed, 0 otherwise */
335 uint32_t sig_opt;
336 uint32_t sig_id;
337 uint8_t sig_param[16];
338 uint32_t comp_opt;
339 uint8_t reserved_4c[4];
340 uint32_t uncomp_size;
341 uint32_t comp_size;
342 /* Starting MDN fw_id is populated instead of fw_type. */
343 uint16_t fw_id;
344 uint8_t reserved_5a[18];
345 uint32_t size_total;
346 uint8_t reserved_70[12];
347 /* Starting MDN fw_id is populated instead of fw_type. fw_type will still be around
348 for backwards compatibility. */
349 uint8_t fw_type;
350 uint8_t fw_subtype;
351 uint8_t fw_subprog;
352 uint8_t reserved_7f;
353 uint8_t reserved_80[128];
354} __packed;
355
Kangheui Won5b84dfd2021-12-21 15:45:06 +1100356struct psp_fw_hash_table {
357 uint16_t version;
358 uint16_t no_of_entries_256;
359 uint16_t no_of_entries_384;
360 /* The next 2 elements are pointers to arrays of SHA256 and SHA384 entries. */
361 /* It does not make sense to store pointers in the CBFS file */
362} __packed;
363
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800364typedef struct _amd_cb_config {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800365 bool have_whitelist;
366 bool unlock_secure;
367 bool use_secureos;
368 bool load_mp2_fw;
369 bool multi_level;
370 bool s0i3;
Zheng Baoc3007f32022-04-03 12:53:51 +0800371 bool second_gen;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800372 bool have_mb_spl;
Zheng Bao990d1542021-09-17 13:24:54 +0800373 bool recovery_ab;
Karthikeyan Ramasubramanianad06bae2022-04-08 14:19:55 -0600374 bool recovery_ab_single_copy;
Zheng Baofdd47ef2021-09-17 13:30:08 +0800375 bool need_ish;
Zheng Bao993b43f2021-11-10 12:21:46 +0800376 bool use_combo;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800377} amd_cb_config;
378
379void register_fw_fuse(char *str);
380uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps);
381
382#define OK 0
383
384#define LINE_EOF (1)
385#define LINE_TOO_LONG (2)
386
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800387#endif /* _AMD_FW_TOOL_H_ */