Debian Codegraph @

util/amdfwtool/amdfwtool.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef _AMD_FW_TOOL_H_
4 #define _AMD_FW_TOOL_H_
5
6 #include <commonlib/bsd/compiler.h>
7 #include <commonlib/bsd/helpers.h>
8 #include <openssl/sha.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11
12 /* An address can be relative to the image/file start but it can also be the address when
13 * the image is mapped at 0xff000000. Used to ensure that we only attempt to read within
14 * the limits of the file. */
15 #define SPI_ROM_BASE 0xff000000
16
17 #define ERASE_ALIGNMENT 0x1000U
18 #define TABLE_ALIGNMENT 0x1000U
19 #define TABLE_L2_SIZE_MAX 0x400U
20 #define BLOB_ALIGNMENT 0x100U
21 #define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT)
22 #define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT)
23
24 enum platform {
25 PLATFORM_UNKNOWN,
26 PLATFORM_CARRIZO,
27 PLATFORM_STONEYRIDGE,
28 PLATFORM_RAVEN,
29 PLATFORM_PICASSO,
30 PLATFORM_RENOIR,
31 PLATFORM_CEZANNE,
32 PLATFORM_MENDOCINO,
33 PLATFORM_LUCIENNE,
34 PLATFORM_PHOENIX,
35 PLATFORM_GLINDA,
36 PLATFORM_GENOA,
37 PLATFORM_FAEGAN,
38 };
39
40 typedef enum _amd_fw_type {
41 AMD_FW_PSP_PUBKEY = 0x00,
42 AMD_FW_PSP_BOOTLOADER = 0x01,
43 AMD_FW_PSP_SECURED_OS = 0x02,
44 AMD_FW_PSP_RECOVERY = 0x03,
45 AMD_FW_PSP_NVRAM = 0x04,
46 AMD_FW_PSP_RTM_PUBKEY = 0x05,
47 AMD_FW_PSP_SMU_FIRMWARE = 0x08,
48 AMD_FW_PSP_SECURED_DEBUG = 0x09,
49 AMD_FW_ABL_PUBKEY = 0x0a,
50 AMD_PSP_FUSE_CHAIN = 0x0b,
51 AMD_FW_PSP_TRUSTLETS = 0x0c,
52 AMD_FW_PSP_TRUSTLETKEY = 0x0d,
53 AMD_FW_PSP_SMU_FIRMWARE2 = 0x12,
54 AMD_DEBUG_UNLOCK = 0x13,
55 AMD_FW_PSP_TEEIPKEY = 0x15,
56 AMD_BOOT_DRIVER = 0x1b,
57 AMD_SOC_DRIVER = 0x1c,
58 AMD_DEBUG_DRIVER = 0x1d,
59 AMD_INTERFACE_DRIVER = 0x1f,
60 AMD_HW_IPCFG = 0x20,
61 AMD_WRAPPED_IKEK = 0x21,
62 AMD_TOKEN_UNLOCK = 0x22,
63 AMD_SEC_GASKET = 0x24,
64 AMD_MP2_FW = 0x25,
65 AMD_DRIVER_ENTRIES = 0x28,
66 AMD_FW_KVM_IMAGE = 0x29,
67 AMD_FW_MP5 = 0x2a,
68 AMD_S0I3_DRIVER = 0x2d,
69 AMD_ABL0 = 0x30,
70 AMD_ABL1 = 0x31,
71 AMD_ABL2 = 0x32,
72 AMD_ABL3 = 0x33,
73 AMD_ABL4 = 0x34,
74 AMD_ABL5 = 0x35,
75 AMD_ABL6 = 0x36,
76 AMD_ABL7 = 0x37,
77 AMD_SEV_DATA = 0x38,
78 AMD_SEV_CODE = 0x39,
79 AMD_FW_PSP_WHITELIST = 0x3a,
80 AMD_VBIOS_BTLOADER = 0x3c,
81 AMD_FW_L2_PTR = 0x40,
82 AMD_FW_DXIO = 0x42,
83 AMD_FW_USB_PHY = 0x44,
84 AMD_FW_TOS_SEC_POLICY = 0x45,
85 AMD_FW_DRTM_TA = 0x47,
86 AMD_FW_RECOVERYAB_A = 0x48,
87 AMD_FW_RECOVERYAB_B = 0x4A,
88 AMD_FW_BIOS_TABLE = 0x49,
89 AMD_FW_KEYDB_BL = 0x50,
90 AMD_FW_KEYDB_TOS = 0x51,
91 AMD_FW_PSP_VERSTAGE = 0x52,
92 AMD_FW_VERSTAGE_SIG = 0x53,
93 AMD_RPMC_NVRAM = 0x54,
94 AMD_FW_SPL = 0x55,
95 AMD_FW_DMCU_ERAM = 0x58,
96 AMD_FW_DMCU_ISR = 0x59,
97 AMD_FW_MSMU = 0x5a,
98 AMD_FW_SPIROM_CFG = 0x5c,
99 AMD_FW_MPIO = 0x5d,
100 AMD_FW_TPMLITE = 0x5f, /* family 17h & 19h */
101 AMD_FW_PSP_SMUSCS = 0x5f, /* family 15h & 16h */
102 AMD_FW_DMCUB = 0x71,
103 AMD_FW_PSP_BOOTLOADER_AB = 0x73,
104 AMD_RIB = 0x76,
105 AMD_FW_AMF_SRAM = 0x85,
106 AMD_FW_AMF_DRAM = 0x86,
107 AMD_FW_MFD_MPM = 0x87,
108 AMD_FW_AMF_WLAN = 0x88,
109 AMD_FW_AMF_MFD = 0x89,
110 AMD_FW_MPDMA_TF = 0x8c,
111 AMD_TA_IKEK = 0x8d,
112 AMD_FW_MPCCX = 0x90,
113 AMD_FW_GMI3_PHY = 0x91,
114 AMD_FW_MPDMA_PM = 0x92,
115 AMD_FW_LSDMA = 0x94,
116 AMD_FW_C20_MP = 0x95,
117 AMD_FW_FCFG_TABLE = 0x98,
118 AMD_FW_MINIMSMU = 0x9a,
119 AMD_FW_GFXIMU_0 = 0x9b,
120 AMD_FW_GFXIMU_1 = 0x9c,
121 AMD_FW_GFXIMU_2 = 0x9d,
122 AMD_FW_SRAM_FW_EXT = 0x9d,
123 AMD_FW_UMSMU = 0xa2,
124 AMD_FW_S3IMG = 0xa0,
125 AMD_FW_USBDP = 0xa4,
126 AMD_FW_USBSS = 0xa5,
127 AMD_FW_USB4 = 0xa6,
128 AMD_FW_IMC = 0x200, /* Large enough to be larger than the top BHD entry type. */
129 AMD_FW_GEC,
130 AMD_FW_XHCI,
131 AMD_FW_INVALID, /* Real last one to detect the last entry in table. */
132 AMD_FW_SKIP /* This is for non-applicable options. */
133 } amd_fw_type;
134
135 typedef enum _amd_bios_type {
136 AMD_BIOS_RTM_PUBKEY = 0x05,
137 AMD_BIOS_SIG = 0x07,
138 AMD_BIOS_APCB = 0x60,
139 AMD_BIOS_APOB = 0x61,
140 AMD_BIOS_BIN = 0x62,
141 AMD_BIOS_APOB_NV = 0x63,
142 AMD_BIOS_PMUI = 0x64,
143 AMD_BIOS_PMUD = 0x65,
144 AMD_BIOS_UCODE = 0x66,
145 AMD_BIOS_APCB_BK = 0x68,
146 AMD_BIOS_EARLY_VGA = 0x69,
147 AMD_BIOS_MP2_CFG = 0x6a,
148 AMD_BIOS_PSP_SHARED_MEM = 0x6b,
149 AMD_BIOS_NV_ST = 0x6d,
150 AMD_BIOS_L2_PTR = 0x70,
151 AMD_BIOS_INVALID,
152 AMD_BIOS_SKIP
153 } amd_bios_type;
154
155 typedef enum _amd_addr_mode {
156 AMD_ADDR_PHYSICAL = 0, /* Physical address */
157 AMD_ADDR_REL_BIOS, /* Relative to beginning of image */
158 AMD_ADDR_REL_TAB, /* Relative to table */
159 AMD_ADDR_REL_SLOT, /* Relative to slot */
160 } amd_addr_mode;
161
162 struct second_gen_efs { /* todo: expand for Server products */
163 uint32_t gen:1; /* Client products only use bit 0 */
164 uint32_t reserved:31;
165 } __attribute__((packed));
166
167 #define EFS_SECOND_GEN 0
168 #define EFS_BEFORE_SECOND_GEN 1
169
170 typedef struct _embedded_firmware {
171 uint32_t signature; /* 0x55aa55aa */
172 uint32_t imc_entry;
173 uint32_t gec_entry;
174 uint32_t xhci_entry;
175 uint32_t psp_directory;
176 uint32_t new_psp_directory; /* also used as combo_psp_directory */
177 uint32_t bios0_entry; /* todo: add way to select correct entry */
178 uint32_t bios1_entry;
179 uint32_t bios2_entry;
180 struct second_gen_efs efs_gen;
181 uint32_t bios3_entry;
182 uint32_t psp_bak_directory;
183 uint32_t promontory_fw_ptr;
184 uint32_t lp_promontory_fw_ptr;
185 uint32_t reserved_38h;
186 uint32_t reserved_3Ch;
187 uint8_t spi_readmode_f15_mod_60_6f;
188 uint8_t fast_speed_new_f15_mod_60_6f;
189 uint8_t reserved_42h;
190 uint8_t spi_readmode_f17_mod_00_2f;
191 uint8_t spi_fastspeed_f17_mod_00_2f;
192 uint8_t qpr_dummy_cycle_f17_mod_00_2f;
193 uint8_t reserved_46h;
194 uint8_t spi_readmode_f17_mod_30_3f;
195 uint8_t spi_fastspeed_f17_mod_30_3f;
196 uint8_t micron_detect_f17_mod_30_3f;
197 uint8_t reserved_4Ah;
198 uint8_t reserved_4Bh;
199 uint32_t reserved_4Ch;
200 } __attribute__((packed, aligned(16))) embedded_firmware;
201
202 typedef struct _psp_directory_header {
203 uint32_t cookie;
204 uint32_t checksum;
205 uint32_t num_entries;
206 union {
207 uint32_t additional_info;
208 struct {
209 uint32_t dir_size:10;
210 uint32_t spi_block_size:4;
211 uint32_t base_addr:15;
212 uint32_t address_mode:2;
213 uint32_t version:1;
214 } __attribute__((packed)) additional_info_fields;
215 struct {
216 uint32_t dir_size:16;
217 uint32_t spi_block_size:4;
218 uint32_t dir_header_size:4;
219 uint32_t address_mode:2;
220 uint32_t reserved:5;
221 uint32_t version:1;
222 } __attribute__((packed)) additional_info_fields_v1;
223 };
224 } __attribute__((packed, aligned(16))) psp_directory_header;
225
226 typedef struct _psp_directory_entry {
227 uint8_t type;
228 uint8_t subprog;
229 union {
230 uint16_t rsvd;
231 struct {
232 uint8_t rom_id:2;
233 uint8_t writable:1;
234 uint8_t inst:4;
235 uint8_t rsvd_1:1;
236 uint8_t rsvd_2:8;
237 } __attribute__((packed));
238 };
239 uint32_t size;
240 uint64_t addr:62; /* or a value in some cases */
241 uint64_t address_mode:2;
242 } __attribute__((packed)) psp_directory_entry;
243
244 typedef struct _psp_directory_table {
245 psp_directory_header header;
246 psp_directory_entry entries[];
247 } __attribute__((packed, aligned(16))) psp_directory_table;
248
249 #define MAX_PSP_ENTRIES 0xff
250
251 typedef struct _psp_combo_header {
252 uint32_t cookie;
253 uint32_t checksum;
254 uint32_t num_entries;
255 uint32_t lookup;
256 uint64_t reserved[2];
257 } __attribute__((packed, aligned(16))) psp_combo_header;
258
259 typedef struct _psp_combo_entry {
260 uint32_t id_sel;
261 uint32_t id;
262 uint64_t lvl2_addr;
263 } __attribute__((packed)) psp_combo_entry;
264
265 typedef struct _psp_combo_directory {
266 psp_combo_header header;
267 psp_combo_entry entries[];
268 } __attribute__((packed, aligned(16))) psp_combo_directory;
269
270 #define MAX_COMBO_ENTRIES 2
271
272 typedef struct _bios_directory_hdr {
273 uint32_t cookie;
274 uint32_t checksum;
275 uint32_t num_entries;
276 union {
277 uint32_t additional_info;
278 struct {
279 uint32_t dir_size:10;
280 uint32_t spi_block_size:4;
281 uint32_t base_addr:15;
282 uint32_t address_mode:2;
283 uint32_t version:1;
284 } __attribute__((packed)) additional_info_fields;
285 struct {
286 uint32_t dir_size:16;
287 uint32_t spi_block_size:4;
288 uint32_t dir_header_size:4;
289 uint32_t address_mode:2;
290 uint32_t reserved:5;
291 uint32_t version:1;
292 } __attribute__((packed)) additional_info_fields_v1;
293 };
294 } __attribute__((packed, aligned(16))) bios_directory_hdr;
295
296 typedef struct _bios_directory_entry {
297 uint8_t type;
298 uint8_t region_type;
299 int reset:1;
300 int copy:1;
301 int ro:1;
302 int compressed:1;
303 int inst:4;
304 uint8_t subprog; /* b[7:3] reserved */
305 uint32_t size;
306 uint64_t source:62;
307 uint64_t address_mode:2;
308 uint64_t dest;
309 } __attribute__((packed)) bios_directory_entry;
310
311 typedef struct _bios_directory_table {
312 bios_directory_hdr header;
313 bios_directory_entry entries[];
314 } bios_directory_table;
315
316 #define MAX_BIOS_ENTRIES 0x2f
317
318 #define BDT_LVL1 (1 << 0)
319 #define BDT_LVL2 (1 << 1)
320 #define BDT_LVL1_AB (1 << 2)
321 #define BDT_LVL2_AB (1 << 3)
322 #define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
323 #define BDT_BOTH_AB (BDT_LVL1_AB | BDT_LVL2_AB)
324 typedef struct _amd_bios_entry {
325 amd_bios_type type;
326 char *filename;
327 int subpr;
328 int region_type;
329 int reset;
330 int copy;
331 int ro;
332 int zlib;
333 int inst;
334 uint64_t src;
335 uint64_t dest;
336 size_t size;
337 int level;
338 } amd_bios_entry;
339
340 typedef struct _ish_directory_table {
341 uint32_t checksum;
342 uint32_t boot_priority;
343 uint32_t update_retry_count;
344 uint8_t glitch_retry_count;
345 uint8_t glitch_higherbits_reserved[3];
346 uint32_t pl2_location;
347 uint32_t psp_id;
348 uint32_t slot_max_size;
349 uint32_t reserved;
350 } __attribute__((packed)) ish_directory_table;
351
352 #define EMBEDDED_FW_SIGNATURE 0x55aa55aa
353 #define PSP_COOKIE 0x50535024 /* 'PSP$' */
354 #define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
355 #define PSP2_COOKIE 0x50535032 /* 'PSP2' */
356 #define BHD_COOKIE 0x44484224 /* 'DHB$ */
357 #define BHDL2_COOKIE 0x324c4224 /* '2LB$ */
358 #define BHD2_COOKIE 0x44484232 /* 'DHB2' */
359
360 #define PSP_LVL1 (1 << 0)
361 #define PSP_LVL2 (1 << 1)
362 #define PSP_LVL1_AB (1 << 2)
363 #define PSP_LVL2_AB (1 << 3)
364 #define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
365 #define PSP_BOTH_AB (PSP_LVL1_AB | PSP_LVL2_AB)
366
367 typedef enum _fwid_type {
368 FWID_TYPE_FWID = 0,
369 FWID_TYPE_UUID,
370 } fwid_type_t;
371
372 #define UUID_LEN_BYTES 16
373 typedef struct _amd_fw_entry_hash {
374 fwid_type_t fwid_type;
375 union {
376 uint16_t fw_id;
377 uint8_t uuid[UUID_LEN_BYTES];
378 };
379 uint16_t subtype;
380 uint32_t sha_len;
381 uint8_t sha[SHA384_DIGEST_LENGTH];
382 } amd_fw_entry_hash;
383
384 typedef struct _amd_fw_entry {
385 amd_fw_type type;
386 char *filename;
387 uint8_t subprog;
388 uint8_t inst;
389 uint64_t dest;
390 size_t size;
391 int level;
392 uint64_t other;
393 /* If the binary is signed and the tool is invoked to keep the signed binaries separate,
394 then this field is populated with the offset of the concerned PSP binary (relative to
395 BIOS or PSP Directory table). */
396 uint64_t addr_signed;
397 uint32_t file_size;
398 /* Some files that don't have amd_fw_header have to be skipped from hashing. These files
399 include but not limited to: *iKek*, *.tkn, *.stkn */
400 bool skip_hashing;
401 uint8_t hash_tbl_id;
402 fwid_type_t fwid_type;
403 uint32_t num_hash_entries;
404 amd_fw_entry_hash *hash_entries;
405 bool generate_manifest;
406 } amd_fw_entry;
407
408 /* Most PSP binaries, if not all, have the following header format. */
409 struct amd_fw_header {
410 uint8_t reserved_0[20];
411 uint32_t fw_size_signed;
412 uint8_t reserved_18[24];
413 /* 1 if the image is signed, 0 otherwise */
414 uint32_t sig_opt;
415 uint32_t sig_id;
416 uint8_t sig_param[16];
417 uint32_t comp_opt;
418 uint8_t reserved_4c[4];
419 uint32_t uncomp_size;
420 uint32_t comp_size;
421 /* Starting MDN fw_id is populated instead of fw_type. */
422 uint16_t fw_id;
423 uint8_t reserved_5a[6];
424 uint8_t version[4];
425 uint8_t reserved_64[8];
426 uint32_t size_total;
427 uint8_t reserved_70[12];
428 /* Starting MDN fw_id is populated instead of fw_type. fw_type will still be around
429 for backwards compatibility. */
430 uint8_t fw_type;
431 uint8_t fw_subtype;
432 uint8_t fw_subprog;
433 uint8_t reserved_7f;
434 uint8_t reserved_80[128];
435 } __packed;
436
437 /* Based on the available PSP resources and increasing number of signed PSP binaries,
438 AMD recommends to split the hash table into 3 parts for now. */
439 #define MAX_NUM_HASH_TABLES 3
440 struct psp_fw_hash_table {
441 uint16_t version;
442 uint16_t no_of_entries_256;
443 uint16_t no_of_entries_384;
444 /* The next 2 elements are pointers to arrays of SHA256 and SHA384 entries. */
445 /* It does not make sense to store pointers in the CBFS file */
446 } __packed;
447
448 typedef struct _amd_cb_config {
449 bool have_whitelist;
450 bool unlock_secure;
451 bool use_secureos;
452 bool load_mp2_fw;
453 bool multi_level;
454 bool s0i3;
455 bool second_gen;
456 bool have_mb_spl;
457 bool recovery_ab;
458 bool recovery_ab_single_copy;
459 bool need_ish;
460 bool use_combo;
461 bool combo_new_rab; /* new combo layout for recovery A/B */
462 bool have_apcb_bk;
463 enum platform soc_id;
464
465 uint8_t efs_spi_readmode, efs_spi_speed, efs_spi_micron_flag;
466 uint32_t body_location, efs_location;
467 uint64_t signed_start_addr;
468 char *manifest_file;
469 const char *signed_output_file;
470 char *output, *config;
471 char *combo_config[MAX_COMBO_ENTRIES];
472 int debug;
473 } amd_cb_config;
474
475 typedef struct _context {
476 char *rom; /* target buffer, size of flash device */
477 uint32_t rom_size; /* size of flash device */
478 uint32_t address_mode; /* 0:abs address; 1:relative to flash; 2: relative to table 3: relative to partition */
479 uint32_t current; /* pointer within flash & proxy buffer */
480 uint32_t current_pointer_saved;
481 uint32_t current_table;
482 uint32_t combo_index;
483 void *amd_psp_fw_table_clean;
484 void *amd_bios_table_clean;
485 struct _combo_apcb {
486 char *filename;
487 uint8_t ins;
488 uint8_t sub;
489 } combo_apcb[MAX_COMBO_ENTRIES], combo_apcb_bk[MAX_COMBO_ENTRIES];
490 embedded_firmware *amd_romsig_ptr;
491 psp_directory_table *pspdir, *pspdir_bak, *pspdir2, *pspdir2_b;
492 bios_directory_table *biosdir, *biosdir2, *biosdir2_b;
493 psp_combo_directory *psp_combo_dir, *bhd_combo_dir;
494 ish_directory_table *ish_a_dir, *ish_b_dir;
495 } context;
496
497 uint8_t process_config(FILE *config, amd_cb_config *cb_config);
498 void process_signed_psp_firmwares(const char *signed_rom,
499 amd_fw_entry *fw_table,
500 uint64_t signed_start_addr,
501 enum platform soc_id);
502 int find_bios_entry(amd_bios_type type);
503 bool needs_ish(enum platform platform_type);
504
505 #define EFS_FILE_SUFFIX ".efs"
506 #define TMP_FILE_SUFFIX ".tmp"
507 #define BODY_FILE_SUFFIX ".body"
508
509 void write_or_fail(int fd, void *ptr, size_t size);
510 ssize_t read_from_file_to_buf(int fd, void *buf, size_t buf_size);
511 ssize_t write_from_buf_to_file(int fd, const void *buf, size_t buf_size);
512 ssize_t write_body(char *output, void *body_offset, ssize_t body_size);
513 ssize_t copy_blob(void *dest, const char *src_file, size_t room);
514 #define OK 0
515
516 #define LINE_EOF (1)
517 #define LINE_TOO_LONG (2)
518
519 int amdfwtool_getopt(int argc, char *argv[], amd_cb_config *cb_config, context *ctx);
520 void register_apcb_combo(amd_cb_config *cb_config, int combo_index, context *ctx);
521
522 #endif /* _AMD_FW_TOOL_H_ */