blob: b94e46f06ac4755e4730f92153a79d296210a87a [file] [log] [blame]
Zheng Baoaddf3402021-06-03 15:46:53 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2
Zheng Baoc5e28ab2020-10-28 11:38:09 +08003#include <stdio.h>
4#include <regex.h>
5#include <string.h>
6#include <stdlib.h>
Idwer Vollering93df1d92020-12-30 00:01:59 +01007#include <stdint.h>
Zheng Baodac44612021-05-27 11:11:34 +08008#include <assert.h>
Zheng Baoc5e28ab2020-10-28 11:38:09 +08009
10#include "amdfwtool.h"
11
12/* TODO: a empty line does not matched. */
13static const char blank_or_comment_regex[] =
14 /* a blank line */
15 "(^[[:space:]]*$)"
16 "|" /* or ... */
17 /* a line consisting of: optional whitespace followed by */
18 "(^[[:space:]]*"
19 /* a '#' character and optionally, additional characters */
20 "#.*$)";
21static regex_t blank_or_comment_expr;
22
23static const char entries_line_regex[] =
24 /* optional whitespace */
25 "^[[:space:]]*"
26 /* followed by a chunk of nonwhitespace for macro field */
27 "([^[:space:]]+)"
28 /* followed by one or more whitespace characters */
29 "[[:space:]]+"
30 /* followed by a chunk of nonwhitespace for filename field */
31 "([^[:space:]]+)"
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -060032 /* followed by an optional whitespace + chunk of nonwhitespace for level field
Zheng Baob1fb8ce2021-09-13 18:00:09 +080033 1st char L: Indicator of field "level"
34 2nd char:
35 Directory level to be dropped in.
36 1: Level 1
37 2: Level 2
38 b: Level both 1&2
39 x: use default value hardcoded in table
40 3rd char:
41 For A/B recovery. Defined same as 2nd char.
42
43 Examples:
44 L2: Level 2 for normal mode
45 L12: Level 1 for normal mode, level 2 for A/B mode
46 Lx1: Use default value for normal mode, level 1 for A/B mode
47 */
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -060048 "([[:space:]]+([Ll][12bxBX]{1,2}))?"
Zheng Baob1fb8ce2021-09-13 18:00:09 +080049 /* followed by optional whitespace */
50 "[[:space:]]*$";
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -060051static regex_t entries_line_expr;
52
53enum match_id {
54 FW_TYPE = 1,
55 FW_FILE,
56 OPT_SPACE1,
57 OPT_LEVEL,
58 N_MATCHES,
59};
Zheng Baob1fb8ce2021-09-13 18:00:09 +080060
Zheng Baoc5e28ab2020-10-28 11:38:09 +080061void compile_reg_expr(int cflags, const char *expr, regex_t *reg)
62{
63 static const size_t ERROR_BUF_SIZE = 256;
64 char error_msg[ERROR_BUF_SIZE];
65 int result;
66
67 result = regcomp(reg, expr, cflags);
68 if (result != 0) {
69 regerror(result, reg, error_msg, ERROR_BUF_SIZE);
Zheng Bao77a2c672020-10-01 17:05:43 +080070 fprintf(stderr, "%s\n", error_msg);
Zheng Baoc5e28ab2020-10-28 11:38:09 +080071 }
72}
73
Zheng Bao010cc992023-01-25 22:58:49 +080074static enum platform identify_platform(char *soc_name)
75{
76 if (!strcasecmp(soc_name, "Stoneyridge"))
77 return PLATFORM_STONEYRIDGE;
78 else if (!strcasecmp(soc_name, "Carrizo"))
79 return PLATFORM_CARRIZO;
80 else if (!strcasecmp(soc_name, "Raven"))
81 return PLATFORM_RAVEN;
82 else if (!strcasecmp(soc_name, "Picasso"))
83 return PLATFORM_PICASSO;
84 else if (!strcasecmp(soc_name, "Cezanne"))
85 return PLATFORM_CEZANNE;
86 else if (!strcasecmp(soc_name, "Mendocino"))
87 return PLATFORM_MENDOCINO;
88 else if (!strcasecmp(soc_name, "Renoir"))
89 return PLATFORM_RENOIR;
90 else if (!strcasecmp(soc_name, "Lucienne"))
91 return PLATFORM_LUCIENNE;
92 else if (!strcasecmp(soc_name, "Phoenix"))
93 return PLATFORM_PHOENIX;
94 else if (!strcasecmp(soc_name, "Glinda"))
95 return PLATFORM_GLINDA;
96 else
97 return PLATFORM_UNKNOWN;
98}
99
Zheng Bao990d1542021-09-17 13:24:54 +0800100#define SET_LEVEL(tableptr, l, TABLE, ab) \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800101 do { \
102 switch ((l)) { \
103 case '1': \
Zheng Bao990d1542021-09-17 13:24:54 +0800104 (tableptr)->level = ab ? TABLE##_LVL1_AB : TABLE##_LVL1; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800105 break; \
106 case '2': \
Zheng Bao990d1542021-09-17 13:24:54 +0800107 (tableptr)->level = ab ? TABLE##_LVL2_AB : TABLE##_LVL2; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800108 break; \
109 case 'b': \
110 case 'B': \
Zheng Bao990d1542021-09-17 13:24:54 +0800111 (tableptr)->level = ab ? TABLE##_BOTH_AB : TABLE##_BOTH; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800112 break; \
113 default: \
114 /* use default value */ \
115 break; \
116 } \
117 } while (0)
118
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800119extern amd_fw_entry amd_psp_fw_table[];
120extern amd_bios_entry amd_bios_table[];
121
122static uint8_t find_register_fw_filename_psp_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800123 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800124{
125 amd_fw_type fw_type = AMD_FW_INVALID;
126 amd_fw_entry *psp_tableptr;
127 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800128 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800129
130 if (strcmp(fw_name, "PSPBTLDR_WL_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800131 if (cb_config->have_whitelist) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500132 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800133 subprog = 0;
134 } else {
135 fw_type = AMD_FW_SKIP;
136 }
Zheng Bao990d1542021-09-17 13:24:54 +0800137 } else if (strcmp(fw_name, "PSPBTLDR_AB_STAGE1_FILE") == 0) {
138 if (cb_config->recovery_ab) {
139 fw_type = AMD_FW_PSP_BOOTLOADER;
140 subprog = 0;
141 } else {
142 fw_type = AMD_FW_SKIP;
143 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800144 } else if (strcmp(fw_name, "PSPBTLDR_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800145 if (!cb_config->recovery_ab) {
146 fw_type = AMD_FW_PSP_BOOTLOADER;
147 subprog = 0;
148 } else {
149 fw_type = AMD_FW_SKIP;
150 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800151 } else if (strcmp(fw_name, "AMD_PUBKEY_FILE") == 0) {
152 fw_type = AMD_FW_PSP_PUBKEY;
153 subprog = 0;
154 } else if (strcmp(fw_name, "PSPRCVR_FILE") == 0) {
155 fw_type = AMD_FW_PSP_RECOVERY;
156 subprog = 0;
157 } else if (strcmp(fw_name, "PUBSIGNEDKEY_FILE") == 0) {
158 fw_type = AMD_FW_PSP_RTM_PUBKEY;
159 subprog = 0;
160 } else if (strcmp(fw_name, "PSPNVRAM_FILE") == 0) {
161 fw_type = AMD_FW_PSP_NVRAM;
162 subprog = 0;
163 } else if (strcmp(fw_name, "SMUSCS_FILE") == 0) {
164 fw_type = AMD_FW_PSP_SMUSCS;
165 subprog = 0;
166 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
167 fw_type = AMD_FW_PSP_TRUSTLETS;
168 subprog = 0;
169 } else if (strcmp(fw_name, "PSPSECUREDEBUG_FILE") == 0) {
170 fw_type = AMD_FW_PSP_SECURED_DEBUG;
171 subprog = 0;
172 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB0_FILE") == 0) {
173 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
174 subprog = 0;
Zheng Bao9bb62cb2023-03-07 19:48:11 +0800175 } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE_SUB0") == 0) {
Zheng Baobf29a0d2020-12-03 23:00:48 +0800176 fw_type = AMD_HW_IPCFG;
177 subprog = 0;
Zheng Bao9bb62cb2023-03-07 19:48:11 +0800178 } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE_SUB1") == 0) {
179 fw_type = AMD_HW_IPCFG;
180 subprog = 1;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800181 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB1_FILE") == 0) {
182 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
183 subprog = 1;
184 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB2_FILE") == 0) {
185 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
186 subprog = 2;
187 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB0_FILE") == 0) {
188 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
189 subprog = 0;
190 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB1_FILE") == 0) {
191 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
192 subprog = 1;
193 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB2_FILE") == 0) {
194 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
195 subprog = 2;
Zheng Bao8eba6622022-10-16 20:29:03 +0800196 } else if (strcmp(fw_name, "PSP_BOOT_DRIVER_FILE") == 0) {
197 fw_type = AMD_BOOT_DRIVER;
198 subprog = 0;
199 } else if (strcmp(fw_name, "PSP_SOC_DRIVER_FILE") == 0) {
200 fw_type = AMD_SOC_DRIVER;
201 subprog = 0;
202 } else if (strcmp(fw_name, "PSP_DEBUG_DRIVER_FILE") == 0) {
203 fw_type = AMD_DEBUG_DRIVER;
204 subprog = 0;
205 } else if (strcmp(fw_name, "PSP_INTERFACE_DRIVER_FILE") == 0) {
206 fw_type = AMD_INTERFACE_DRIVER;
207 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800208 } else if (strcmp(fw_name, "PSP_SEC_DBG_KEY_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800209 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800210 fw_type = AMD_FW_PSP_SECURED_DEBUG;
211 subprog = 0;
212 } else {
213 fw_type = AMD_FW_SKIP;
214 }
215 } else if (strcmp(fw_name, "PSP_SEC_DEBUG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800216 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800217 fw_type = AMD_DEBUG_UNLOCK;
218 subprog = 0;
219 } else {
220 fw_type = AMD_FW_SKIP;
221 }
222 } else if (strcmp(fw_name, "PSP_ABL0_FILE") == 0) {
223 fw_type = AMD_ABL0;
224 subprog = 0;
225 } else if (strcmp(fw_name, "PSP_ABL1_FILE") == 0) {
226 fw_type = AMD_ABL1;
227 subprog = 0;
228 } else if (strcmp(fw_name, "PSP_ABL2_FILE") == 0) {
229 fw_type = AMD_ABL2;
230 subprog = 0;
231 } else if (strcmp(fw_name, "PSP_ABL3_FILE") == 0) {
232 fw_type = AMD_ABL3;
233 subprog = 0;
234 } else if (strcmp(fw_name, "PSP_ABL4_FILE") == 0) {
235 fw_type = AMD_ABL4;
236 subprog = 0;
237 } else if (strcmp(fw_name, "PSP_ABL5_FILE") == 0) {
238 fw_type = AMD_ABL5;
239 subprog = 0;
240 } else if (strcmp(fw_name, "PSP_ABL6_FILE") == 0) {
241 fw_type = AMD_ABL6;
242 subprog = 0;
243 } else if (strcmp(fw_name, "PSP_ABL7_FILE") == 0) {
244 fw_type = AMD_ABL7;
245 subprog = 0;
246 } else if (strcmp(fw_name, "PSPSECUREOS_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800247 if (cb_config->use_secureos) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800248 fw_type = AMD_FW_PSP_SECURED_OS;
249 subprog = 0;
250 } else {
251 fw_type = AMD_FW_SKIP;
252 }
253 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
254 if (cb_config->use_secureos) {
255 fw_type = AMD_FW_PSP_TRUSTLETS;
256 subprog = 0;
257 } else {
258 fw_type = AMD_FW_SKIP;
259 }
260 } else if (strcmp(fw_name, "TRUSTLETKEY_FILE") == 0) {
261 if (cb_config->use_secureos) {
262 fw_type = AMD_FW_PSP_TRUSTLETKEY;
263 subprog = 0;
264 } else {
265 fw_type = AMD_FW_SKIP;
266 }
267 } else if (strcmp(fw_name, "PSP_IKEK_FILE") == 0) {
268 fw_type = AMD_WRAPPED_IKEK;
269 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800270 } else if (strcmp(fw_name, "PSP_SECG0_FILE") == 0) {
271 fw_type = AMD_SEC_GASKET;
272 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800273 } else if (strcmp(fw_name, "PSP_SECG1_FILE") == 0) {
274 fw_type = AMD_SEC_GASKET;
275 subprog = 1;
276 } else if (strcmp(fw_name, "PSP_SECG2_FILE") == 0) {
277 fw_type = AMD_SEC_GASKET;
278 subprog = 2;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800279 } else if (strcmp(fw_name, "PSP_MP2FW0_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800280 if (cb_config->load_mp2_fw) {
Zheng Baobf29a0d2020-12-03 23:00:48 +0800281 fw_type = AMD_MP2_FW;
282 subprog = 0;
283 } else {
284 fw_type = AMD_FW_SKIP;
285 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800286 } else if (strcmp(fw_name, "PSP_MP2FW1_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800287 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800288 fw_type = AMD_MP2_FW;
289 subprog = 1;
290 } else {
291 fw_type = AMD_FW_SKIP;
292 }
293 } else if (strcmp(fw_name, "PSP_MP2FW2_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800294 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800295 fw_type = AMD_MP2_FW;
296 subprog = 2;
297 } else {
298 fw_type = AMD_FW_SKIP;
299 }
Zheng Bao8eba6622022-10-16 20:29:03 +0800300 } else if (strcmp(fw_name, "PSP_C20MP_FILE") == 0) {
301 fw_type = AMD_FW_C20_MP;
302 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800303 } else if (strcmp(fw_name, "AMF_SRAM_FILE") == 0) {
304 fw_type = AMD_FW_AMF_SRAM;
305 subprog = 0;
306 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS0") == 0) {
307 fw_type = AMD_FW_AMF_DRAM;
308 subprog = 0;
309 instance = 0;
310 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS1") == 0) {
311 fw_type = AMD_FW_AMF_DRAM;
312 subprog = 0;
313 instance = 1;
314 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS0") == 0) {
315 fw_type = AMD_FW_AMF_WLAN;
316 subprog = 0;
317 instance = 0;
318 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS1") == 0) {
319 fw_type = AMD_FW_AMF_WLAN;
320 subprog = 0;
321 instance = 1;
322 } else if (strcmp(fw_name, "AMF_MFD_FILE") == 0) {
323 fw_type = AMD_FW_AMF_MFD;
324 subprog = 0;
325 } else if (strcmp(fw_name, "MPCCX_FILE") == 0) {
326 fw_type = AMD_FW_MPCCX;
327 subprog = 0;
328 } else if (strcmp(fw_name, "LSDMA_FILE") == 0) {
329 fw_type = AMD_FW_LSDMA;
330 subprog = 0;
331 } else if (strcmp(fw_name, "MINIMSMU_FILE") == 0) {
332 fw_type = AMD_FW_MINIMSMU;
333 instance = 0;
334 subprog = 0;
335 } else if (strcmp(fw_name, "MINIMSMU_FILE_INS1") == 0) {
336 fw_type = AMD_FW_MINIMSMU;
337 instance = 1;
338 subprog = 0;
339 } else if (strcmp(fw_name, "SRAM_FW_EXT_FILE") == 0) {
340 fw_type = AMD_FW_SRAM_FW_EXT;
341 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800342 } else if (strcmp(fw_name, "PSP_DRIVERS_FILE") == 0) {
343 fw_type = AMD_DRIVER_ENTRIES;
344 subprog = 0;
345 } else if (strcmp(fw_name, "PSP_S0I3_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800346 if (cb_config->s0i3) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800347 fw_type = AMD_S0I3_DRIVER;
348 subprog = 0;
349 } else {
350 fw_type = AMD_FW_SKIP;
351 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800352 } else if (strcmp(fw_name, "AMD_DRIVER_ENTRIES") == 0) {
353 fw_type = AMD_DRIVER_ENTRIES;
354 subprog = 0;
355 } else if (strcmp(fw_name, "VBIOS_BTLOADER_FILE") == 0) {
356 fw_type = AMD_VBIOS_BTLOADER;
357 subprog = 0;
358 } else if (strcmp(fw_name, "SECURE_POLICY_L1_FILE") == 0) {
359 fw_type = AMD_FW_TOS_SEC_POLICY;
360 subprog = 0;
361 } else if (strcmp(fw_name, "UNIFIEDUSB_FILE") == 0) {
362 fw_type = AMD_FW_USB_PHY;
363 subprog = 0;
364 } else if (strcmp(fw_name, "DRTMTA_FILE") == 0) {
365 fw_type = AMD_FW_DRTM_TA;
366 subprog = 0;
367 } else if (strcmp(fw_name, "KEYDBBL_FILE") == 0) {
368 fw_type = AMD_FW_KEYDB_BL;
369 subprog = 0;
370 } else if (strcmp(fw_name, "KEYDB_TOS_FILE") == 0) {
371 fw_type = AMD_FW_KEYDB_TOS;
372 subprog = 0;
Zheng Baoab84fd72022-01-27 22:38:27 +0800373 } else if (strcmp(fw_name, "SPL_TABLE_FILE") == 0) {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800374 if (cb_config->have_mb_spl) {
Felix Held11b0d362022-04-02 03:49:07 +0200375 fw_type = AMD_FW_SKIP;
376 } else {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800377 fw_type = AMD_FW_SPL;
378 subprog = 0;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800379 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800380 } else if (strcmp(fw_name, "DMCUERAMDCN21_FILE") == 0) {
381 fw_type = AMD_FW_DMCU_ERAM;
382 subprog = 0;
383 } else if (strcmp(fw_name, "DMCUINTVECTORSDCN21_FILE") == 0) {
384 fw_type = AMD_FW_DMCU_ISR;
385 subprog = 0;
Felix Held5f18bb72022-03-24 02:04:51 +0100386 } else if (strcmp(fw_name, "MSMU_FILE") == 0) {
387 fw_type = AMD_FW_MSMU;
388 subprog = 0;
389 } else if (strcmp(fw_name, "DMCUB_FILE") == 0) {
390 fw_type = AMD_FW_DMCUB;
391 subprog = 0;
392 } else if (strcmp(fw_name, "SPIROM_CONFIG_FILE") == 0) {
393 fw_type = AMD_FW_SPIROM_CFG;
394 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800395 } else if (strcmp(fw_name, "MPIO_FILE") == 0) {
396 fw_type = AMD_FW_MPIO;
397 subprog = 0;
398 } else if (strcmp(fw_name, "TPMLITE_FILE") == 0) {
Zheng Bao8eba6622022-10-16 20:29:03 +0800399 fw_type = AMD_FW_TPMLITE;
400 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800401 } else if (strcmp(fw_name, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) {
402 fw_type = AMD_FW_KVM_IMAGE;
403 subprog = 0;
404 } else if (strcmp(fw_name, "RPMC_FILE") == 0) {
405 fw_type = AMD_RPMC_NVRAM;
406 subprog = 0;
Zheng Baob993cb22021-02-02 18:48:23 +0800407 } else if (strcmp(fw_name, "PSPBTLDR_AB_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800408 if (!cb_config->have_whitelist || cb_config->recovery_ab) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500409 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
410 subprog = 0;
411 } else {
412 fw_type = AMD_FW_SKIP;
413 }
Karthikeyan Ramasubramanian0ab04d22022-05-03 18:16:34 -0600414 } else if (strcmp(fw_name, "TA_IKEK_FILE") == 0) {
415 fw_type = AMD_TA_IKEK;
416 subprog = 0;
Fred Reitbergerc4f3a332023-02-07 12:12:40 -0500417 } else if (strcmp(fw_name, "UMSMU_FILE") == 0) {
418 fw_type = AMD_FW_UMSMU;
419 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200420 } else if (strcmp(fw_name, "PSP_OEM_ABL_KEY_FILE") == 0) {
421 fw_type = AMD_FW_ABL_PUBKEY;
422 subprog = 0;
423 } else if (strcmp(fw_name, "PSP_MP5FW_SUB0_FILE") == 0) {
424 fw_type = AMD_FW_MP5;
425 subprog = 0;
426 } else if (strcmp(fw_name, "PSP_MP5FW_SUB1_FILE") == 0) {
427 fw_type = AMD_FW_MP5;
428 subprog = 1;
429 } else if (strcmp(fw_name, "PSP_MP5FW_SUB2_FILE") == 0) {
430 fw_type = AMD_FW_MP5;
431 subprog = 2;
432 } else if (strcmp(fw_name, "PSP_DXIOFW_FILE") == 0) {
433 fw_type = AMD_FW_DXIO;
434 subprog = 0;
435 } else if (strcmp(fw_name, "PSP_MPIOFW_FILE") == 0) {
436 fw_type = AMD_FW_MPIO;
437 subprog = 0;
Zheng Bao85ee1fd2023-01-30 13:52:30 +0800438 } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB0") == 0) {
Arthur Heymans1f05c802022-10-04 17:50:21 +0200439 fw_type = AMD_RIB;
440 subprog = 0;
Zheng Bao85ee1fd2023-01-30 13:52:30 +0800441 } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB1") == 0) {
442 fw_type = AMD_RIB;
443 subprog = 1;
Zheng Bao8eba6622022-10-16 20:29:03 +0800444 } else if (strcmp(fw_name, "FEATURE_TABLE_FILE") == 0) {
445 fw_type = AMD_FW_FCFG_TABLE;
446 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200447 } else if (strcmp(fw_name, "PSP_MPDMATFFW_FILE") == 0) {
448 fw_type = AMD_FW_MPDMA_TF;
449 subprog = 0;
450 } else if (strcmp(fw_name, "PSP_GMI3PHYFW_FILE") == 0) {
451 fw_type = AMD_FW_GMI3_PHY;
452 subprog = 0;
453 } else if (strcmp(fw_name, "PSP_MPDMAPMFW_FILE") == 0) {
454 fw_type = AMD_FW_MPDMA_PM;
455 subprog = 0;
456 } else if (strcmp(fw_name, "PSP_TOKEN_UNLOCK_FILE") == 0) {
457 fw_type = AMD_TOKEN_UNLOCK;
458 subprog = 0;
459 } else if (strcmp(fw_name, "SEV_DATA_FILE") == 0) {
460 fw_type = AMD_SEV_DATA;
461 subprog = 0;
462 } else if (strcmp(fw_name, "SEV_CODE_FILE") == 0) {
463 fw_type = AMD_SEV_CODE;
464 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800465 } else {
466 fw_type = AMD_FW_INVALID;
467 /* TODO: Add more */
468 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800469
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800470 /* Search and fill the filename */
471 psp_tableptr = &amd_psp_fw_table[0];
472 if (fw_type != AMD_FW_SKIP && fw_type != AMD_FW_INVALID) {
473 while (psp_tableptr->type != AMD_FW_INVALID) {
474 /* instance are not used in PSP table */
Zheng Bao5ca13432022-10-16 20:18:40 +0800475 if (psp_tableptr->type == fw_type && psp_tableptr->subprog == subprog
476 && psp_tableptr->inst == instance) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800477 psp_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800478 SET_LEVEL(psp_tableptr, level_to_set, PSP,
479 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800480 break;
481 }
482 psp_tableptr++;
483 }
484 }
485 if (fw_type == AMD_FW_INVALID)
486 return 0;
487 else
488 return 1;
489}
Zheng Bao1a9e5432022-02-17 17:48:27 +0800490#define PMUI_STR_BASE "PSP_PMUI_FILE"
491#define PMUD_STR_BASE "PSP_PMUD_FILE"
492#define PMU_STR_BASE_LEN strlen(PMUI_STR_BASE)
493#define PMU_STR_SUB_INDEX strlen(PMUI_STR_BASE"_SUB")
494#define PMU_STR_INS_INDEX strlen(PMUI_STR_BASE"_SUBx_INS")
Zheng Baofdb02942022-02-22 09:47:59 +0800495#define PMU_STR_ALL_LEN strlen(PMUI_STR_BASE"_SUBx_INSx")
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800496
497static uint8_t find_register_fw_filename_bios_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800498 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800499{
500 amd_bios_type fw_type = AMD_BIOS_INVALID;
501 amd_bios_entry *bhd_tableptr;
Felix Heldea3417b2020-11-20 20:08:42 +0100502 uint8_t subprog = 0;
503 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800504
505 (void) (cb_config); /* Remove warning and reserved for future. */
506
Zheng Bao1a9e5432022-02-17 17:48:27 +0800507 if (strncmp(fw_name, PMUI_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800508 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800509 fw_type = AMD_BIOS_PMUI;
Felix Held3dfb4852022-09-28 18:00:39 +0200510 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
511 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Bao1a9e5432022-02-17 17:48:27 +0800512 } else if (strncmp(fw_name, PMUD_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800513 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800514 fw_type = AMD_BIOS_PMUD;
Felix Held3dfb4852022-09-28 18:00:39 +0200515 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
516 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Baobf29a0d2020-12-03 23:00:48 +0800517 } else if (strcmp(fw_name, "RTM_PUBKEY_FILE") == 0) {
518 fw_type = AMD_BIOS_RTM_PUBKEY;
519 subprog = 0;
520 instance = 0;
Zheng Bao50143732020-11-14 21:54:06 +0800521 } else if (strcmp(fw_name, "PSP_MP2CFG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800522 if (cb_config->load_mp2_fw) {
Zheng Bao50143732020-11-14 21:54:06 +0800523 fw_type = AMD_BIOS_MP2_CFG;
524 subprog = 0;
525 } else {
Martin Rotha8e31ca2021-02-13 21:42:46 -0700526 fw_type = AMD_BIOS_SKIP;
Zheng Bao50143732020-11-14 21:54:06 +0800527 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800528 } else {
529 fw_type = AMD_BIOS_INVALID;
530 }
531
532 bhd_tableptr = amd_bios_table;
533
534 if (fw_type != AMD_BIOS_INVALID && fw_type != AMD_BIOS_SKIP) {
535 while (bhd_tableptr->type != AMD_BIOS_INVALID) {
536 if (bhd_tableptr->type == fw_type &&
537 bhd_tableptr->subpr == subprog &&
538 bhd_tableptr->inst == instance) {
539 bhd_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800540 SET_LEVEL(bhd_tableptr, level_to_set, BDT,
541 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800542 break;
543 }
544 bhd_tableptr++;
545 }
546 }
547 if (fw_type == AMD_BIOS_INVALID)
548 return 0;
549 else
550 return 1;
551}
552
553#define MAX_LINE_SIZE 1024
554
555int get_input_file_line(FILE *f, char line[], int line_buf_size)
556{
557 if (fgets(line, line_buf_size, f) == NULL)
558 return LINE_EOF;
559
560 /* If the file contains a line that is too long, then it's best
561 * to let the user know right away rather than passing back a
562 * truncated result that will lead to problems later on.
563 */
564 line[strlen(line) - 1] = '\0';
565
566 if (strlen(line) == ((size_t) (line_buf_size - 1))) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800567 fprintf(stderr, "The line size in config file should be lower than %d bytes.\n",
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800568 MAX_LINE_SIZE);
569 exit(1);
570 }
571
572 return OK;
573}
574
Zheng Bao4df5af82022-03-01 17:18:00 +0800575static int is_valid_entry(char *oneline, regmatch_t match[N_MATCHES])
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800576{
Zheng Bao4df5af82022-03-01 17:18:00 +0800577 int retval, index;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800578
Zheng Bao4df5af82022-03-01 17:18:00 +0800579 for (index = 0; index < N_MATCHES; index++) {
580 match[index].rm_so = -1;
581 match[index].rm_eo = -1;
582 }
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600583 if (regexec(&entries_line_expr, oneline, N_MATCHES, match, 0) == 0) {
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800584 /* match[1]: FW type
585 match[2]: FW filename
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600586 match[4]: Optional directory level to be dropped
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800587 */
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600588 oneline[match[FW_TYPE].rm_eo] = '\0';
589 oneline[match[FW_FILE].rm_eo] = '\0';
590 oneline[match[OPT_LEVEL].rm_eo] = '\0';
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800591 retval = 1;
592 } else {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800593 retval = 0;
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800594 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800595
596 return retval;
597}
598
599static int skip_comment_blank_line(char *oneline)
600{
601 int retval;
602
603 if (regexec(&blank_or_comment_expr, oneline, 0, NULL, 0) == 0) {
604 /* skip comment and blank */
605 retval = 1;
606 } else {
607 /* no match */
608 retval = 0;
609 }
610
611 return retval;
612}
613
Zheng Bao52a18982022-03-01 17:22:52 +0800614char get_level_from_config(char *line, regoff_t level_index, amd_cb_config *cb_config)
615{
616 char lvl = 'x';
617 /* If the optional level field is present,
618 extract the level char. */
619 if (level_index != -1) {
620 if (cb_config->recovery_ab == 0)
621 lvl = line[level_index + 1];
622 else if (strlen(&line[level_index]) >= 3)
623 lvl = line[level_index + 2];
624 }
625
626 assert(lvl == 'x' || lvl == 'X' ||
627 lvl == 'b' || lvl == 'B' ||
628 lvl == '1' || lvl == '2');
629
630 return lvl;
631}
632
Zheng Bao7db76422021-03-27 18:15:35 +0800633static uint8_t process_one_line(char *oneline, regmatch_t *match, char *dir,
Zheng Bao994ff522023-03-09 11:43:55 +0800634 amd_cb_config *cb_config)
Zheng Bao7db76422021-03-27 18:15:35 +0800635{
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600636 char *path_filename, *fn = &(oneline[match[FW_FILE].rm_so]);
637 char *fw_type_str = &(oneline[match[FW_TYPE].rm_so]);
Zheng Bao7db76422021-03-27 18:15:35 +0800638 char ch_lvl = 'x';
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600639 regoff_t ch_lvl_index = match[OPT_LEVEL].rm_so == match[OPT_LEVEL].rm_eo ?
640 -1 : match[OPT_LEVEL].rm_so;
Zheng Bao7db76422021-03-27 18:15:35 +0800641
642 /* If the optional level field is present,
643 extract the level char. */
644 ch_lvl = get_level_from_config(oneline, ch_lvl_index, cb_config);
645
646 path_filename = malloc(MAX_LINE_SIZE * 2 + 2);
Martin Roth6bb6ed92023-03-08 14:58:51 -0700647 if (strchr(fn, '/'))
648 snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s",
649 MAX_LINE_SIZE, fn);
650 else
651 snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s/%.*s",
652 MAX_LINE_SIZE, dir, MAX_LINE_SIZE, fn);
Zheng Bao7db76422021-03-27 18:15:35 +0800653
654 if (find_register_fw_filename_psp_dir(
655 fw_type_str, path_filename, ch_lvl, cb_config) == 0) {
656 if (find_register_fw_filename_bios_dir(
657 fw_type_str, path_filename, ch_lvl, cb_config) == 0) {
658 fprintf(stderr, "Module's name \"%s\" is not valid\n", fw_type_str);
659 return 0; /* Stop parsing. */
Zheng Bao7db76422021-03-27 18:15:35 +0800660 }
Zheng Bao7db76422021-03-27 18:15:35 +0800661 }
662 return 1;
663}
664
Zheng Bao010cc992023-01-25 22:58:49 +0800665static bool needs_ish(enum platform platform_type)
666{
667 if (platform_type == PLATFORM_MENDOCINO || platform_type == PLATFORM_PHOENIX || platform_type == PLATFORM_GLINDA)
668 return true;
669 else
670 return false;
671}
672
673static bool is_second_gen(enum platform platform_type)
674{
675 switch (platform_type) {
676 case PLATFORM_CARRIZO:
677 case PLATFORM_STONEYRIDGE:
678 case PLATFORM_RAVEN:
679 case PLATFORM_PICASSO:
680 return false;
681 case PLATFORM_RENOIR:
682 case PLATFORM_LUCIENNE:
683 case PLATFORM_CEZANNE:
684 case PLATFORM_MENDOCINO:
685 case PLATFORM_PHOENIX:
686 case PLATFORM_GLINDA:
687 return true;
688 case PLATFORM_UNKNOWN:
689 default:
690 fprintf(stderr, "Error: Invalid SOC name.\n\n");
691 return false;
692 }
693}
694
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600695#define FW_LOCATION "FIRMWARE_LOCATION"
696#define SOC_NAME "SOC_NAME"
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800697/*
698 return value:
699 0: The config file can not be parsed correctly.
700 1: The config file can be parsed correctly.
701 */
Zheng Bao994ff522023-03-09 11:43:55 +0800702uint8_t process_config(FILE *config, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800703{
Zheng Bao7db76422021-03-27 18:15:35 +0800704 char oneline[MAX_LINE_SIZE];
Zheng Bao4df5af82022-03-01 17:18:00 +0800705 regmatch_t match[N_MATCHES];
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800706 char dir[MAX_LINE_SIZE] = {'\0'};
Zheng Baodac44612021-05-27 11:11:34 +0800707 uint32_t dir_len;
Zheng Bao4df5af82022-03-01 17:18:00 +0800708 int index;
709
710 for (index = 0; index < N_MATCHES; index++) {
711 match[index].rm_so = -1;
712 match[index].rm_eo = -1;
713 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800714
715 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
716 blank_or_comment_regex, &blank_or_comment_expr);
717 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
718 entries_line_regex, &entries_line_expr);
719
720 /* Get a line */
Zheng Bao3384e4a2020-10-06 12:03:11 +0800721 /* Get FIRMWARE_LOCATION in the first loop */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800722 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
723 /* get a line */
724 if (skip_comment_blank_line(oneline))
725 continue;
726 if (is_valid_entry(oneline, match)) {
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600727 if (strcmp(&(oneline[match[FW_TYPE].rm_so]), FW_LOCATION) == 0) {
728 dir_len = match[FW_FILE].rm_eo - match[FW_FILE].rm_so;
Zheng Baodac44612021-05-27 11:11:34 +0800729 assert(dir_len < MAX_LINE_SIZE);
730 snprintf(dir, MAX_LINE_SIZE, "%.*s", dir_len,
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600731 &(oneline[match[FW_FILE].rm_so]));
732 } else if (strcmp(&(oneline[match[FW_TYPE].rm_so]), SOC_NAME) == 0) {
733 cb_config->soc_id = identify_platform(
734 &(oneline[match[FW_FILE].rm_so]));
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800735 }
736 }
737 }
738
Zheng Bao010cc992023-01-25 22:58:49 +0800739 cb_config->second_gen = is_second_gen(cb_config->soc_id);
740
741 if (needs_ish(cb_config->soc_id))
742 cb_config->need_ish = true;
743
744 if (cb_config->need_ish)
745 cb_config->recovery_ab = true;
746
747 if (cb_config->recovery_ab)
748 cb_config->multi_level = true;
749
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800750 if (dir[0] == '\0') {
751 fprintf(stderr, "No line with FIRMWARE_LOCATION\n");
752 return 0;
753 }
754
755 fseek(config, 0, SEEK_SET);
756 /* Get a line */
757 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
758 /* get a line */
759 if (skip_comment_blank_line(oneline))
760 continue;
761 if (is_valid_entry(oneline, match)) {
Karthikeyan Ramasubramaniandc498932023-05-02 22:10:57 -0600762 if (strcmp(&(oneline[match[FW_TYPE].rm_so]), FW_LOCATION) == 0 ||
763 strcmp(&(oneline[match[FW_TYPE].rm_so]), SOC_NAME) == 0) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800764 continue;
765 } else {
Zheng Bao994ff522023-03-09 11:43:55 +0800766 if (process_one_line(oneline, match, dir,
Zheng Bao7db76422021-03-27 18:15:35 +0800767 cb_config) == 0)
768 return 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800769 }
770 } else {
771 fprintf(stderr, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline);
772 return 0;
773 }
774 }
775 return 1;
776}