1 /* SPDX-License-Identifier: GPL-2.0-only */
10 #include "amdfwtool.h"
12 /* TODO: a empty line does not matched. */
13 static const char blank_or_comment_regex
[] =
17 /* a line consisting of: optional whitespace followed by */
19 /* a '#' character and optionally, additional characters */
21 static regex_t blank_or_comment_expr
;
23 static const char entries_line_regex
[] =
24 /* optional whitespace */
26 /* followed by a chunk of nonwhitespace for macro field */
28 /* followed by one or more whitespace characters */
30 /* followed by a chunk of nonwhitespace for filename field */
32 /* followed by optional whitespace */
34 static regex_t entries_line_expr
;
36 static const char entries_lvl_line_regex
[] =
37 /* optional whitespace */
39 /* followed by a chunk of nonwhitespace for macro field */
41 /* followed by one or more whitespace characters */
43 /* followed by a chunk of nonwhitespace for filename field */
45 /* followed by one or more whitespace characters */
47 /* followed by a chunk of nonwhitespace for level field
48 1st char L: Indicator of field "level"
50 Directory level to be dropped in.
54 x: use default value hardcoded in table
56 For A/B recovery. Defined same as 2nd char.
59 L2: Level 2 for normal mode
60 L12: Level 1 for normal mode, level 2 for A/B mode
61 Lx1: Use default value for normal mode, level 1 for A/B mode
64 /* followed by optional whitespace */
66 static regex_t entries_lvl_line_expr
;
68 void compile_reg_expr(int cflags
, const char *expr
, regex_t
*reg
)
70 static const size_t ERROR_BUF_SIZE
= 256;
71 char error_msg
[ERROR_BUF_SIZE
];
74 result
= regcomp(reg
, expr
, cflags
);
76 regerror(result
, reg
, error_msg
, ERROR_BUF_SIZE
);
77 fprintf(stderr
, "%s\n", error_msg
);
81 #define SET_LEVEL(tableptr, l, TABLE, ab) \
85 (tableptr)->level = ab ? TABLE##_LVL1_AB : TABLE##_LVL1; \
88 (tableptr)->level = ab ? TABLE##_LVL2_AB : TABLE##_LVL2; \
92 (tableptr)->level = ab ? TABLE##_BOTH_AB : TABLE##_BOTH; \
95 /* use default value */ \
100 extern amd_fw_entry amd_psp_fw_table
[];
101 extern amd_bios_entry amd_bios_table
[];
103 static uint8_t find_register_fw_filename_psp_dir(char *fw_name
, char *filename
,
104 char level_to_set
, amd_cb_config
*cb_config
)
106 amd_fw_type fw_type
= AMD_FW_INVALID
;
107 amd_fw_entry
*psp_tableptr
;
110 if (strcmp(fw_name
, "PSPBTLDR_WL_FILE") == 0) {
111 if (cb_config
->have_whitelist
) {
112 fw_type
= AMD_FW_PSP_BOOTLOADER_AB
;
115 fw_type
= AMD_FW_SKIP
;
117 } else if (strcmp(fw_name
, "PSPBTLDR_AB_STAGE1_FILE") == 0) {
118 if (cb_config
->recovery_ab
) {
119 fw_type
= AMD_FW_PSP_BOOTLOADER
;
122 fw_type
= AMD_FW_SKIP
;
124 } else if (strcmp(fw_name
, "PSPBTLDR_FILE") == 0) {
125 if (!cb_config
->recovery_ab
) {
126 fw_type
= AMD_FW_PSP_BOOTLOADER
;
129 fw_type
= AMD_FW_SKIP
;
131 } else if (strcmp(fw_name
, "AMD_PUBKEY_FILE") == 0) {
132 fw_type
= AMD_FW_PSP_PUBKEY
;
134 } else if (strcmp(fw_name
, "PSPRCVR_FILE") == 0) {
135 fw_type
= AMD_FW_PSP_RECOVERY
;
137 } else if (strcmp(fw_name
, "PUBSIGNEDKEY_FILE") == 0) {
138 fw_type
= AMD_FW_PSP_RTM_PUBKEY
;
140 } else if (strcmp(fw_name
, "PSPNVRAM_FILE") == 0) {
141 fw_type
= AMD_FW_PSP_NVRAM
;
143 } else if (strcmp(fw_name
, "SMUSCS_FILE") == 0) {
144 fw_type
= AMD_FW_PSP_SMUSCS
;
146 } else if (strcmp(fw_name
, "PSPTRUSTLETS_FILE") == 0) {
147 fw_type
= AMD_FW_PSP_TRUSTLETS
;
149 } else if (strcmp(fw_name
, "PSPSECUREDEBUG_FILE") == 0) {
150 fw_type
= AMD_FW_PSP_SECURED_DEBUG
;
152 } else if (strcmp(fw_name
, "PSP_SMUFW1_SUB0_FILE") == 0) {
153 fw_type
= AMD_FW_PSP_SMU_FIRMWARE
;
155 } else if (strcmp(fw_name
, "PSP_HW_IPCFG_FILE") == 0) {
156 fw_type
= AMD_HW_IPCFG
;
158 } else if (strcmp(fw_name
, "PSP_SMUFW1_SUB1_FILE") == 0) {
159 fw_type
= AMD_FW_PSP_SMU_FIRMWARE
;
161 } else if (strcmp(fw_name
, "PSP_SMUFW1_SUB2_FILE") == 0) {
162 fw_type
= AMD_FW_PSP_SMU_FIRMWARE
;
164 } else if (strcmp(fw_name
, "PSP_SMUFW2_SUB0_FILE") == 0) {
165 fw_type
= AMD_FW_PSP_SMU_FIRMWARE2
;
167 } else if (strcmp(fw_name
, "PSP_SMUFW2_SUB1_FILE") == 0) {
168 fw_type
= AMD_FW_PSP_SMU_FIRMWARE2
;
170 } else if (strcmp(fw_name
, "PSP_SMUFW2_SUB2_FILE") == 0) {
171 fw_type
= AMD_FW_PSP_SMU_FIRMWARE2
;
173 } else if (strcmp(fw_name
, "PSP_SEC_DBG_KEY_FILE") == 0) {
174 if (cb_config
->unlock_secure
) {
175 fw_type
= AMD_FW_PSP_SECURED_DEBUG
;
178 fw_type
= AMD_FW_SKIP
;
180 } else if (strcmp(fw_name
, "PSP_SEC_DEBUG_FILE") == 0) {
181 if (cb_config
->unlock_secure
) {
182 fw_type
= AMD_DEBUG_UNLOCK
;
185 fw_type
= AMD_FW_SKIP
;
187 } else if (strcmp(fw_name
, "PSP_ABL0_FILE") == 0) {
190 } else if (strcmp(fw_name
, "PSP_ABL1_FILE") == 0) {
193 } else if (strcmp(fw_name
, "PSP_ABL2_FILE") == 0) {
196 } else if (strcmp(fw_name
, "PSP_ABL3_FILE") == 0) {
199 } else if (strcmp(fw_name
, "PSP_ABL4_FILE") == 0) {
202 } else if (strcmp(fw_name
, "PSP_ABL5_FILE") == 0) {
205 } else if (strcmp(fw_name
, "PSP_ABL6_FILE") == 0) {
208 } else if (strcmp(fw_name
, "PSP_ABL7_FILE") == 0) {
211 } else if (strcmp(fw_name
, "PSPSECUREOS_FILE") == 0) {
212 if (cb_config
->use_secureos
) {
213 fw_type
= AMD_FW_PSP_SECURED_OS
;
216 fw_type
= AMD_FW_SKIP
;
218 } else if (strcmp(fw_name
, "PSPTRUSTLETS_FILE") == 0) {
219 if (cb_config
->use_secureos
) {
220 fw_type
= AMD_FW_PSP_TRUSTLETS
;
223 fw_type
= AMD_FW_SKIP
;
225 } else if (strcmp(fw_name
, "TRUSTLETKEY_FILE") == 0) {
226 if (cb_config
->use_secureos
) {
227 fw_type
= AMD_FW_PSP_TRUSTLETKEY
;
230 fw_type
= AMD_FW_SKIP
;
232 } else if (strcmp(fw_name
, "PSP_IKEK_FILE") == 0) {
233 fw_type
= AMD_WRAPPED_IKEK
;
235 } else if (strcmp(fw_name
, "PSP_SECG0_FILE") == 0) {
236 fw_type
= AMD_SEC_GASKET
;
238 } else if (strcmp(fw_name
, "PSP_SECG1_FILE") == 0) {
239 fw_type
= AMD_SEC_GASKET
;
241 } else if (strcmp(fw_name
, "PSP_SECG2_FILE") == 0) {
242 fw_type
= AMD_SEC_GASKET
;
244 } else if (strcmp(fw_name
, "PSP_MP2FW0_FILE") == 0) {
245 if (cb_config
->load_mp2_fw
) {
246 fw_type
= AMD_MP2_FW
;
249 fw_type
= AMD_FW_SKIP
;
251 } else if (strcmp(fw_name
, "PSP_MP2FW1_FILE") == 0) {
252 if (cb_config
->load_mp2_fw
) {
253 fw_type
= AMD_MP2_FW
;
256 fw_type
= AMD_FW_SKIP
;
258 } else if (strcmp(fw_name
, "PSP_MP2FW2_FILE") == 0) {
259 if (cb_config
->load_mp2_fw
) {
260 fw_type
= AMD_MP2_FW
;
263 fw_type
= AMD_FW_SKIP
;
265 } else if (strcmp(fw_name
, "PSP_DRIVERS_FILE") == 0) {
266 fw_type
= AMD_DRIVER_ENTRIES
;
268 } else if (strcmp(fw_name
, "PSP_S0I3_FILE") == 0) {
269 if (cb_config
->s0i3
) {
270 fw_type
= AMD_S0I3_DRIVER
;
273 fw_type
= AMD_FW_SKIP
;
275 } else if (strcmp(fw_name
, "AMD_DRIVER_ENTRIES") == 0) {
276 fw_type
= AMD_DRIVER_ENTRIES
;
278 } else if (strcmp(fw_name
, "VBIOS_BTLOADER_FILE") == 0) {
279 fw_type
= AMD_VBIOS_BTLOADER
;
281 } else if (strcmp(fw_name
, "SECURE_POLICY_L1_FILE") == 0) {
282 fw_type
= AMD_FW_TOS_SEC_POLICY
;
284 } else if (strcmp(fw_name
, "UNIFIEDUSB_FILE") == 0) {
285 fw_type
= AMD_FW_USB_PHY
;
287 } else if (strcmp(fw_name
, "DRTMTA_FILE") == 0) {
288 fw_type
= AMD_FW_DRTM_TA
;
290 } else if (strcmp(fw_name
, "KEYDBBL_FILE") == 0) {
291 fw_type
= AMD_FW_KEYDB_BL
;
293 } else if (strcmp(fw_name
, "KEYDB_TOS_FILE") == 0) {
294 fw_type
= AMD_FW_KEYDB_TOS
;
296 } else if (strcmp(fw_name
, "SPL_TABLE_FILE") == 0) {
297 if (cb_config
->have_mb_spl
) {
298 fw_type
= AMD_FW_SKIP
;
300 fw_type
= AMD_FW_SPL
;
303 } else if (strcmp(fw_name
, "DMCUERAMDCN21_FILE") == 0) {
304 fw_type
= AMD_FW_DMCU_ERAM
;
306 } else if (strcmp(fw_name
, "DMCUINTVECTORSDCN21_FILE") == 0) {
307 fw_type
= AMD_FW_DMCU_ISR
;
309 } else if (strcmp(fw_name
, "MSMU_FILE") == 0) {
310 fw_type
= AMD_FW_MSMU
;
312 } else if (strcmp(fw_name
, "DMCUB_FILE") == 0) {
313 fw_type
= AMD_FW_DMCUB
;
315 } else if (strcmp(fw_name
, "SPIROM_CONFIG_FILE") == 0) {
316 fw_type
= AMD_FW_SPIROM_CFG
;
318 } else if (strcmp(fw_name
, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) {
319 fw_type
= AMD_FW_KVM_IMAGE
;
321 } else if (strcmp(fw_name
, "RPMC_FILE") == 0) {
322 fw_type
= AMD_RPMC_NVRAM
;
324 } else if (strcmp(fw_name
, "PSPBTLDR_AB_FILE") == 0) {
325 if (!cb_config
->have_whitelist
|| cb_config
->recovery_ab
) {
326 fw_type
= AMD_FW_PSP_BOOTLOADER_AB
;
329 fw_type
= AMD_FW_SKIP
;
331 } else if (strcmp(fw_name
, "TA_IKEK_FILE") == 0) {
332 fw_type
= AMD_TA_IKEK
;
335 fw_type
= AMD_FW_INVALID
;
339 /* Search and fill the filename */
340 psp_tableptr
= &amd_psp_fw_table
[0];
341 if (fw_type
!= AMD_FW_SKIP
&& fw_type
!= AMD_FW_INVALID
) {
342 while (psp_tableptr
->type
!= AMD_FW_INVALID
) {
343 /* instance are not used in PSP table */
344 if (psp_tableptr
->type
== fw_type
&& psp_tableptr
->subprog
== subprog
) {
345 psp_tableptr
->filename
= filename
;
346 SET_LEVEL(psp_tableptr
, level_to_set
, PSP
,
347 cb_config
->recovery_ab
);
353 if (fw_type
== AMD_FW_INVALID
)
358 #define PMUI_STR_BASE "PSP_PMUI_FILE"
359 #define PMUD_STR_BASE "PSP_PMUD_FILE"
360 #define PMU_STR_BASE_LEN strlen(PMUI_STR_BASE)
361 #define PMU_STR_SUB_INDEX strlen(PMUI_STR_BASE"_SUB")
362 #define PMU_STR_INS_INDEX strlen(PMUI_STR_BASE"_SUBx_INS")
363 #define PMU_STR_ALL_LEN strlen(PMUI_STR_BASE"_SUBx_INSx")
365 static uint8_t find_register_fw_filename_bios_dir(char *fw_name
, char *filename
,
366 char level_to_set
, amd_cb_config
*cb_config
)
368 amd_bios_type fw_type
= AMD_BIOS_INVALID
;
369 amd_bios_entry
*bhd_tableptr
;
371 uint8_t instance
= 0;
373 (void) (cb_config
); /* Remove warning and reserved for future. */
375 if (strncmp(fw_name
, PMUI_STR_BASE
, PMU_STR_BASE_LEN
) == 0) {
376 assert(strlen(fw_name
) == PMU_STR_ALL_LEN
);
377 fw_type
= AMD_BIOS_PMUI
;
378 subprog
= fw_name
[PMU_STR_SUB_INDEX
] - '0';
379 instance
= fw_name
[PMU_STR_INS_INDEX
] - '0';
380 } else if (strncmp(fw_name
, PMUD_STR_BASE
, PMU_STR_BASE_LEN
) == 0) {
381 assert(strlen(fw_name
) == PMU_STR_ALL_LEN
);
382 fw_type
= AMD_BIOS_PMUD
;
383 subprog
= fw_name
[PMU_STR_SUB_INDEX
] - '0';
384 instance
= fw_name
[PMU_STR_INS_INDEX
] - '0';
385 } else if (strcmp(fw_name
, "RTM_PUBKEY_FILE") == 0) {
386 fw_type
= AMD_BIOS_RTM_PUBKEY
;
389 } else if (strcmp(fw_name
, "PSP_MP2CFG_FILE") == 0) {
390 if (cb_config
->load_mp2_fw
) {
391 fw_type
= AMD_BIOS_MP2_CFG
;
394 fw_type
= AMD_BIOS_SKIP
;
397 fw_type
= AMD_BIOS_INVALID
;
400 bhd_tableptr
= amd_bios_table
;
402 if (fw_type
!= AMD_BIOS_INVALID
&& fw_type
!= AMD_BIOS_SKIP
) {
403 while (bhd_tableptr
->type
!= AMD_BIOS_INVALID
) {
404 if (bhd_tableptr
->type
== fw_type
&&
405 bhd_tableptr
->subpr
== subprog
&&
406 bhd_tableptr
->inst
== instance
) {
407 bhd_tableptr
->filename
= filename
;
408 SET_LEVEL(bhd_tableptr
, level_to_set
, BDT
,
409 cb_config
->recovery_ab
);
415 if (fw_type
== AMD_BIOS_INVALID
)
421 #define MAX_LINE_SIZE 1024
423 int get_input_file_line(FILE *f
, char line
[], int line_buf_size
)
425 if (fgets(line
, line_buf_size
, f
) == NULL
)
428 /* If the file contains a line that is too long, then it's best
429 * to let the user know right away rather than passing back a
430 * truncated result that will lead to problems later on.
432 line
[strlen(line
) - 1] = '\0';
434 if (strlen(line
) == ((size_t) (line_buf_size
- 1))) {
435 fprintf(stderr
, "The line size in config file should be lower than %d bytes.\n",
444 static int is_valid_entry(char *oneline
, regmatch_t match
[N_MATCHES
])
448 for (index
= 0; index
< N_MATCHES
; index
++) {
449 match
[index
].rm_so
= -1;
450 match
[index
].rm_eo
= -1;
452 if (regexec(&entries_line_expr
, oneline
, 3, match
, 0) == 0) {
453 oneline
[match
[1].rm_eo
] = '\0';
454 oneline
[match
[2].rm_eo
] = '\0';
456 } else if (regexec(&entries_lvl_line_expr
, oneline
, 4, match
, 0) == 0) {
458 match[2]: FW filename
459 match[3]: Directory level to be dropped
461 oneline
[match
[1].rm_eo
] = '\0';
462 oneline
[match
[2].rm_eo
] = '\0';
463 oneline
[match
[3].rm_eo
] = '\0';
472 static int skip_comment_blank_line(char *oneline
)
476 if (regexec(&blank_or_comment_expr
, oneline
, 0, NULL
, 0) == 0) {
477 /* skip comment and blank */
487 char get_level_from_config(char *line
, regoff_t level_index
, amd_cb_config
*cb_config
)
490 /* If the optional level field is present,
491 extract the level char. */
492 if (level_index
!= -1) {
493 if (cb_config
->recovery_ab
== 0)
494 lvl
= line
[level_index
+ 1];
495 else if (strlen(&line
[level_index
]) >= 3)
496 lvl
= line
[level_index
+ 2];
499 assert(lvl
== 'x' || lvl
== 'X' ||
500 lvl
== 'b' || lvl
== 'B' ||
501 lvl
== '1' || lvl
== '2');
508 0: The config file can not be parsed correctly.
509 1: The config file can be parsed correctly.
511 uint8_t process_config(FILE *config
, amd_cb_config
*cb_config
, uint8_t print_deps
)
513 char oneline
[MAX_LINE_SIZE
], *path_filename
;
514 regmatch_t match
[N_MATCHES
];
515 char dir
[MAX_LINE_SIZE
] = {'\0'};
519 for (index
= 0; index
< N_MATCHES
; index
++) {
520 match
[index
].rm_so
= -1;
521 match
[index
].rm_eo
= -1;
524 compile_reg_expr(REG_EXTENDED
| REG_NEWLINE
,
525 blank_or_comment_regex
, &blank_or_comment_expr
);
526 compile_reg_expr(REG_EXTENDED
| REG_NEWLINE
,
527 entries_line_regex
, &entries_line_expr
);
528 compile_reg_expr(REG_EXTENDED
| REG_NEWLINE
,
529 entries_lvl_line_regex
, &entries_lvl_line_expr
);
532 /* Get FIRMWARE_LOCATION in the first loop */
533 while (get_input_file_line(config
, oneline
, MAX_LINE_SIZE
) == OK
) {
535 if (skip_comment_blank_line(oneline
))
537 if (is_valid_entry(oneline
, match
)) {
538 if (strcmp(&(oneline
[match
[1].rm_so
]), "FIRMWARE_LOCATION") == 0) {
539 dir_len
= match
[2].rm_eo
- match
[2].rm_so
;
540 assert(dir_len
< MAX_LINE_SIZE
);
541 snprintf(dir
, MAX_LINE_SIZE
, "%.*s", dir_len
,
542 &(oneline
[match
[2].rm_so
]));
548 if (dir
[0] == '\0') {
549 fprintf(stderr
, "No line with FIRMWARE_LOCATION\n");
553 fseek(config
, 0, SEEK_SET
);
555 while (get_input_file_line(config
, oneline
, MAX_LINE_SIZE
) == OK
) {
557 if (skip_comment_blank_line(oneline
))
559 if (is_valid_entry(oneline
, match
)) {
560 if (strcmp(&(oneline
[match
[1].rm_so
]), "FIRMWARE_LOCATION") == 0) {
564 path_filename
= malloc(MAX_LINE_SIZE
* 2 + 2);
565 snprintf(path_filename
, MAX_LINE_SIZE
* 2 + 2, "%.*s/%.*s",
566 MAX_LINE_SIZE
, dir
, MAX_LINE_SIZE
,
567 &(oneline
[match
[2].rm_so
]));
569 /* If the optional level field is present,
570 extract the level char. */
571 ch_lvl
= get_level_from_config(oneline
,
572 match
[3].rm_so
, cb_config
);
574 if (find_register_fw_filename_psp_dir(
575 &(oneline
[match
[1].rm_so
]),
576 path_filename
, ch_lvl
, cb_config
) == 0) {
577 if (find_register_fw_filename_bios_dir(
578 &(oneline
[match
[1].rm_so
]),
579 path_filename
, ch_lvl
, cb_config
)
581 fprintf(stderr
, "Module's name \"%s\" is not valid\n", oneline
);
582 return 0; /* Stop parsing. */
585 printf(" %s ", path_filename
);
589 printf(" %s ", path_filename
);
593 fprintf(stderr
, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline
);