blob: 2f338309ed3da54b36fa32ef6897228eabc5c138 [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:]]+)"
32 /* followed by optional whitespace */
33 "[[:space:]]*$";
34static regex_t entries_line_expr;
35
Zheng Baob1fb8ce2021-09-13 18:00:09 +080036static const char entries_lvl_line_regex[] =
37 /* optional whitespace */
38 "^[[:space:]]*"
39 /* followed by a chunk of nonwhitespace for macro field */
40 "([^[:space:]]+)"
41 /* followed by one or more whitespace characters */
42 "[[:space:]]+"
43 /* followed by a chunk of nonwhitespace for filename field */
44 "([^[:space:]]+)"
45 /* followed by one or more whitespace characters */
46 "[[:space:]]+"
47 /* followed by a chunk of nonwhitespace for level field
48 1st char L: Indicator of field "level"
49 2nd char:
50 Directory level to be dropped in.
51 1: Level 1
52 2: Level 2
53 b: Level both 1&2
54 x: use default value hardcoded in table
55 3rd char:
56 For A/B recovery. Defined same as 2nd char.
57
58 Examples:
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
62 */
63 "([Ll][12bxBX]{1,2})"
64 /* followed by optional whitespace */
65 "[[:space:]]*$";
66static regex_t entries_lvl_line_expr;
67
Zheng Baoc5e28ab2020-10-28 11:38:09 +080068void compile_reg_expr(int cflags, const char *expr, regex_t *reg)
69{
70 static const size_t ERROR_BUF_SIZE = 256;
71 char error_msg[ERROR_BUF_SIZE];
72 int result;
73
74 result = regcomp(reg, expr, cflags);
75 if (result != 0) {
76 regerror(result, reg, error_msg, ERROR_BUF_SIZE);
Zheng Bao77a2c672020-10-01 17:05:43 +080077 fprintf(stderr, "%s\n", error_msg);
Zheng Baoc5e28ab2020-10-28 11:38:09 +080078 }
79}
80
Zheng Bao010cc992023-01-25 22:58:49 +080081static enum platform identify_platform(char *soc_name)
82{
83 if (!strcasecmp(soc_name, "Stoneyridge"))
84 return PLATFORM_STONEYRIDGE;
85 else if (!strcasecmp(soc_name, "Carrizo"))
86 return PLATFORM_CARRIZO;
87 else if (!strcasecmp(soc_name, "Raven"))
88 return PLATFORM_RAVEN;
89 else if (!strcasecmp(soc_name, "Picasso"))
90 return PLATFORM_PICASSO;
91 else if (!strcasecmp(soc_name, "Cezanne"))
92 return PLATFORM_CEZANNE;
93 else if (!strcasecmp(soc_name, "Mendocino"))
94 return PLATFORM_MENDOCINO;
95 else if (!strcasecmp(soc_name, "Renoir"))
96 return PLATFORM_RENOIR;
97 else if (!strcasecmp(soc_name, "Lucienne"))
98 return PLATFORM_LUCIENNE;
99 else if (!strcasecmp(soc_name, "Phoenix"))
100 return PLATFORM_PHOENIX;
101 else if (!strcasecmp(soc_name, "Glinda"))
102 return PLATFORM_GLINDA;
103 else
104 return PLATFORM_UNKNOWN;
105}
106
Zheng Bao990d1542021-09-17 13:24:54 +0800107#define SET_LEVEL(tableptr, l, TABLE, ab) \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800108 do { \
109 switch ((l)) { \
110 case '1': \
Zheng Bao990d1542021-09-17 13:24:54 +0800111 (tableptr)->level = ab ? TABLE##_LVL1_AB : TABLE##_LVL1; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800112 break; \
113 case '2': \
Zheng Bao990d1542021-09-17 13:24:54 +0800114 (tableptr)->level = ab ? TABLE##_LVL2_AB : TABLE##_LVL2; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800115 break; \
116 case 'b': \
117 case 'B': \
Zheng Bao990d1542021-09-17 13:24:54 +0800118 (tableptr)->level = ab ? TABLE##_BOTH_AB : TABLE##_BOTH; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800119 break; \
120 default: \
121 /* use default value */ \
122 break; \
123 } \
124 } while (0)
125
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800126extern amd_fw_entry amd_psp_fw_table[];
127extern amd_bios_entry amd_bios_table[];
128
129static uint8_t find_register_fw_filename_psp_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800130 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800131{
132 amd_fw_type fw_type = AMD_FW_INVALID;
133 amd_fw_entry *psp_tableptr;
134 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800135 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800136
137 if (strcmp(fw_name, "PSPBTLDR_WL_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800138 if (cb_config->have_whitelist) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500139 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800140 subprog = 0;
141 } else {
142 fw_type = AMD_FW_SKIP;
143 }
Zheng Bao990d1542021-09-17 13:24:54 +0800144 } else if (strcmp(fw_name, "PSPBTLDR_AB_STAGE1_FILE") == 0) {
145 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, "PSPBTLDR_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800152 if (!cb_config->recovery_ab) {
153 fw_type = AMD_FW_PSP_BOOTLOADER;
154 subprog = 0;
155 } else {
156 fw_type = AMD_FW_SKIP;
157 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800158 } else if (strcmp(fw_name, "AMD_PUBKEY_FILE") == 0) {
159 fw_type = AMD_FW_PSP_PUBKEY;
160 subprog = 0;
161 } else if (strcmp(fw_name, "PSPRCVR_FILE") == 0) {
162 fw_type = AMD_FW_PSP_RECOVERY;
163 subprog = 0;
164 } else if (strcmp(fw_name, "PUBSIGNEDKEY_FILE") == 0) {
165 fw_type = AMD_FW_PSP_RTM_PUBKEY;
166 subprog = 0;
167 } else if (strcmp(fw_name, "PSPNVRAM_FILE") == 0) {
168 fw_type = AMD_FW_PSP_NVRAM;
169 subprog = 0;
170 } else if (strcmp(fw_name, "SMUSCS_FILE") == 0) {
171 fw_type = AMD_FW_PSP_SMUSCS;
172 subprog = 0;
173 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
174 fw_type = AMD_FW_PSP_TRUSTLETS;
175 subprog = 0;
176 } else if (strcmp(fw_name, "PSPSECUREDEBUG_FILE") == 0) {
177 fw_type = AMD_FW_PSP_SECURED_DEBUG;
178 subprog = 0;
179 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB0_FILE") == 0) {
180 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
181 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800182 } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE") == 0) {
183 fw_type = AMD_HW_IPCFG;
184 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800185 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB1_FILE") == 0) {
186 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
187 subprog = 1;
188 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB2_FILE") == 0) {
189 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
190 subprog = 2;
191 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB0_FILE") == 0) {
192 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
193 subprog = 0;
194 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB1_FILE") == 0) {
195 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
196 subprog = 1;
197 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB2_FILE") == 0) {
198 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
199 subprog = 2;
Zheng Bao8eba6622022-10-16 20:29:03 +0800200 } else if (strcmp(fw_name, "PSP_BOOT_DRIVER_FILE") == 0) {
201 fw_type = AMD_BOOT_DRIVER;
202 subprog = 0;
203 } else if (strcmp(fw_name, "PSP_SOC_DRIVER_FILE") == 0) {
204 fw_type = AMD_SOC_DRIVER;
205 subprog = 0;
206 } else if (strcmp(fw_name, "PSP_DEBUG_DRIVER_FILE") == 0) {
207 fw_type = AMD_DEBUG_DRIVER;
208 subprog = 0;
209 } else if (strcmp(fw_name, "PSP_INTERFACE_DRIVER_FILE") == 0) {
210 fw_type = AMD_INTERFACE_DRIVER;
211 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800212 } else if (strcmp(fw_name, "PSP_SEC_DBG_KEY_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800213 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800214 fw_type = AMD_FW_PSP_SECURED_DEBUG;
215 subprog = 0;
216 } else {
217 fw_type = AMD_FW_SKIP;
218 }
219 } else if (strcmp(fw_name, "PSP_SEC_DEBUG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800220 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800221 fw_type = AMD_DEBUG_UNLOCK;
222 subprog = 0;
223 } else {
224 fw_type = AMD_FW_SKIP;
225 }
226 } else if (strcmp(fw_name, "PSP_ABL0_FILE") == 0) {
227 fw_type = AMD_ABL0;
228 subprog = 0;
229 } else if (strcmp(fw_name, "PSP_ABL1_FILE") == 0) {
230 fw_type = AMD_ABL1;
231 subprog = 0;
232 } else if (strcmp(fw_name, "PSP_ABL2_FILE") == 0) {
233 fw_type = AMD_ABL2;
234 subprog = 0;
235 } else if (strcmp(fw_name, "PSP_ABL3_FILE") == 0) {
236 fw_type = AMD_ABL3;
237 subprog = 0;
238 } else if (strcmp(fw_name, "PSP_ABL4_FILE") == 0) {
239 fw_type = AMD_ABL4;
240 subprog = 0;
241 } else if (strcmp(fw_name, "PSP_ABL5_FILE") == 0) {
242 fw_type = AMD_ABL5;
243 subprog = 0;
244 } else if (strcmp(fw_name, "PSP_ABL6_FILE") == 0) {
245 fw_type = AMD_ABL6;
246 subprog = 0;
247 } else if (strcmp(fw_name, "PSP_ABL7_FILE") == 0) {
248 fw_type = AMD_ABL7;
249 subprog = 0;
250 } else if (strcmp(fw_name, "PSPSECUREOS_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800251 if (cb_config->use_secureos) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800252 fw_type = AMD_FW_PSP_SECURED_OS;
253 subprog = 0;
254 } else {
255 fw_type = AMD_FW_SKIP;
256 }
257 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
258 if (cb_config->use_secureos) {
259 fw_type = AMD_FW_PSP_TRUSTLETS;
260 subprog = 0;
261 } else {
262 fw_type = AMD_FW_SKIP;
263 }
264 } else if (strcmp(fw_name, "TRUSTLETKEY_FILE") == 0) {
265 if (cb_config->use_secureos) {
266 fw_type = AMD_FW_PSP_TRUSTLETKEY;
267 subprog = 0;
268 } else {
269 fw_type = AMD_FW_SKIP;
270 }
271 } else if (strcmp(fw_name, "PSP_IKEK_FILE") == 0) {
272 fw_type = AMD_WRAPPED_IKEK;
273 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800274 } else if (strcmp(fw_name, "PSP_SECG0_FILE") == 0) {
275 fw_type = AMD_SEC_GASKET;
276 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800277 } else if (strcmp(fw_name, "PSP_SECG1_FILE") == 0) {
278 fw_type = AMD_SEC_GASKET;
279 subprog = 1;
280 } else if (strcmp(fw_name, "PSP_SECG2_FILE") == 0) {
281 fw_type = AMD_SEC_GASKET;
282 subprog = 2;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800283 } else if (strcmp(fw_name, "PSP_MP2FW0_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800284 if (cb_config->load_mp2_fw) {
Zheng Baobf29a0d2020-12-03 23:00:48 +0800285 fw_type = AMD_MP2_FW;
286 subprog = 0;
287 } else {
288 fw_type = AMD_FW_SKIP;
289 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800290 } else if (strcmp(fw_name, "PSP_MP2FW1_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800291 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800292 fw_type = AMD_MP2_FW;
293 subprog = 1;
294 } else {
295 fw_type = AMD_FW_SKIP;
296 }
297 } else if (strcmp(fw_name, "PSP_MP2FW2_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800298 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800299 fw_type = AMD_MP2_FW;
300 subprog = 2;
301 } else {
302 fw_type = AMD_FW_SKIP;
303 }
Zheng Bao8eba6622022-10-16 20:29:03 +0800304 } else if (strcmp(fw_name, "PSP_C20MP_FILE") == 0) {
305 fw_type = AMD_FW_C20_MP;
306 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800307 } else if (strcmp(fw_name, "AMF_SRAM_FILE") == 0) {
308 fw_type = AMD_FW_AMF_SRAM;
309 subprog = 0;
310 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS0") == 0) {
311 fw_type = AMD_FW_AMF_DRAM;
312 subprog = 0;
313 instance = 0;
314 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS1") == 0) {
315 fw_type = AMD_FW_AMF_DRAM;
316 subprog = 0;
317 instance = 1;
318 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS0") == 0) {
319 fw_type = AMD_FW_AMF_WLAN;
320 subprog = 0;
321 instance = 0;
322 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS1") == 0) {
323 fw_type = AMD_FW_AMF_WLAN;
324 subprog = 0;
325 instance = 1;
326 } else if (strcmp(fw_name, "AMF_MFD_FILE") == 0) {
327 fw_type = AMD_FW_AMF_MFD;
328 subprog = 0;
329 } else if (strcmp(fw_name, "MPCCX_FILE") == 0) {
330 fw_type = AMD_FW_MPCCX;
331 subprog = 0;
332 } else if (strcmp(fw_name, "LSDMA_FILE") == 0) {
333 fw_type = AMD_FW_LSDMA;
334 subprog = 0;
335 } else if (strcmp(fw_name, "MINIMSMU_FILE") == 0) {
336 fw_type = AMD_FW_MINIMSMU;
337 instance = 0;
338 subprog = 0;
339 } else if (strcmp(fw_name, "MINIMSMU_FILE_INS1") == 0) {
340 fw_type = AMD_FW_MINIMSMU;
341 instance = 1;
342 subprog = 0;
343 } else if (strcmp(fw_name, "SRAM_FW_EXT_FILE") == 0) {
344 fw_type = AMD_FW_SRAM_FW_EXT;
345 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800346 } else if (strcmp(fw_name, "PSP_DRIVERS_FILE") == 0) {
347 fw_type = AMD_DRIVER_ENTRIES;
348 subprog = 0;
349 } else if (strcmp(fw_name, "PSP_S0I3_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800350 if (cb_config->s0i3) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800351 fw_type = AMD_S0I3_DRIVER;
352 subprog = 0;
353 } else {
354 fw_type = AMD_FW_SKIP;
355 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800356 } else if (strcmp(fw_name, "AMD_DRIVER_ENTRIES") == 0) {
357 fw_type = AMD_DRIVER_ENTRIES;
358 subprog = 0;
359 } else if (strcmp(fw_name, "VBIOS_BTLOADER_FILE") == 0) {
360 fw_type = AMD_VBIOS_BTLOADER;
361 subprog = 0;
362 } else if (strcmp(fw_name, "SECURE_POLICY_L1_FILE") == 0) {
363 fw_type = AMD_FW_TOS_SEC_POLICY;
364 subprog = 0;
365 } else if (strcmp(fw_name, "UNIFIEDUSB_FILE") == 0) {
366 fw_type = AMD_FW_USB_PHY;
367 subprog = 0;
368 } else if (strcmp(fw_name, "DRTMTA_FILE") == 0) {
369 fw_type = AMD_FW_DRTM_TA;
370 subprog = 0;
371 } else if (strcmp(fw_name, "KEYDBBL_FILE") == 0) {
372 fw_type = AMD_FW_KEYDB_BL;
373 subprog = 0;
374 } else if (strcmp(fw_name, "KEYDB_TOS_FILE") == 0) {
375 fw_type = AMD_FW_KEYDB_TOS;
376 subprog = 0;
Zheng Baoab84fd72022-01-27 22:38:27 +0800377 } else if (strcmp(fw_name, "SPL_TABLE_FILE") == 0) {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800378 if (cb_config->have_mb_spl) {
Felix Held11b0d362022-04-02 03:49:07 +0200379 fw_type = AMD_FW_SKIP;
380 } else {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800381 fw_type = AMD_FW_SPL;
382 subprog = 0;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800383 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800384 } else if (strcmp(fw_name, "DMCUERAMDCN21_FILE") == 0) {
385 fw_type = AMD_FW_DMCU_ERAM;
386 subprog = 0;
387 } else if (strcmp(fw_name, "DMCUINTVECTORSDCN21_FILE") == 0) {
388 fw_type = AMD_FW_DMCU_ISR;
389 subprog = 0;
Felix Held5f18bb72022-03-24 02:04:51 +0100390 } else if (strcmp(fw_name, "MSMU_FILE") == 0) {
391 fw_type = AMD_FW_MSMU;
392 subprog = 0;
393 } else if (strcmp(fw_name, "DMCUB_FILE") == 0) {
394 fw_type = AMD_FW_DMCUB;
395 subprog = 0;
396 } else if (strcmp(fw_name, "SPIROM_CONFIG_FILE") == 0) {
397 fw_type = AMD_FW_SPIROM_CFG;
398 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800399 } else if (strcmp(fw_name, "MPIO_FILE") == 0) {
400 fw_type = AMD_FW_MPIO;
401 subprog = 0;
402 } else if (strcmp(fw_name, "TPMLITE_FILE") == 0) {
Zheng Bao8eba6622022-10-16 20:29:03 +0800403 fw_type = AMD_FW_TPMLITE;
404 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800405 } else if (strcmp(fw_name, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) {
406 fw_type = AMD_FW_KVM_IMAGE;
407 subprog = 0;
408 } else if (strcmp(fw_name, "RPMC_FILE") == 0) {
409 fw_type = AMD_RPMC_NVRAM;
410 subprog = 0;
Zheng Baob993cb22021-02-02 18:48:23 +0800411 } else if (strcmp(fw_name, "PSPBTLDR_AB_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800412 if (!cb_config->have_whitelist || cb_config->recovery_ab) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500413 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
414 subprog = 0;
415 } else {
416 fw_type = AMD_FW_SKIP;
417 }
Karthikeyan Ramasubramanian0ab04d22022-05-03 18:16:34 -0600418 } else if (strcmp(fw_name, "TA_IKEK_FILE") == 0) {
419 fw_type = AMD_TA_IKEK;
420 subprog = 0;
Fred Reitbergerc4f3a332023-02-07 12:12:40 -0500421 } else if (strcmp(fw_name, "UMSMU_FILE") == 0) {
422 fw_type = AMD_FW_UMSMU;
423 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200424 } else if (strcmp(fw_name, "PSP_OEM_ABL_KEY_FILE") == 0) {
425 fw_type = AMD_FW_ABL_PUBKEY;
426 subprog = 0;
427 } else if (strcmp(fw_name, "PSP_MP5FW_SUB0_FILE") == 0) {
428 fw_type = AMD_FW_MP5;
429 subprog = 0;
430 } else if (strcmp(fw_name, "PSP_MP5FW_SUB1_FILE") == 0) {
431 fw_type = AMD_FW_MP5;
432 subprog = 1;
433 } else if (strcmp(fw_name, "PSP_MP5FW_SUB2_FILE") == 0) {
434 fw_type = AMD_FW_MP5;
435 subprog = 2;
436 } else if (strcmp(fw_name, "PSP_DXIOFW_FILE") == 0) {
437 fw_type = AMD_FW_DXIO;
438 subprog = 0;
439 } else if (strcmp(fw_name, "PSP_MPIOFW_FILE") == 0) {
440 fw_type = AMD_FW_MPIO;
441 subprog = 0;
Zheng Bao85ee1fd2023-01-30 13:52:30 +0800442 } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB0") == 0) {
Arthur Heymans1f05c802022-10-04 17:50:21 +0200443 fw_type = AMD_RIB;
444 subprog = 0;
Zheng Bao85ee1fd2023-01-30 13:52:30 +0800445 } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB1") == 0) {
446 fw_type = AMD_RIB;
447 subprog = 1;
Zheng Bao8eba6622022-10-16 20:29:03 +0800448 } else if (strcmp(fw_name, "FEATURE_TABLE_FILE") == 0) {
449 fw_type = AMD_FW_FCFG_TABLE;
450 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200451 } else if (strcmp(fw_name, "PSP_MPDMATFFW_FILE") == 0) {
452 fw_type = AMD_FW_MPDMA_TF;
453 subprog = 0;
454 } else if (strcmp(fw_name, "PSP_GMI3PHYFW_FILE") == 0) {
455 fw_type = AMD_FW_GMI3_PHY;
456 subprog = 0;
457 } else if (strcmp(fw_name, "PSP_MPDMAPMFW_FILE") == 0) {
458 fw_type = AMD_FW_MPDMA_PM;
459 subprog = 0;
460 } else if (strcmp(fw_name, "PSP_TOKEN_UNLOCK_FILE") == 0) {
461 fw_type = AMD_TOKEN_UNLOCK;
462 subprog = 0;
463 } else if (strcmp(fw_name, "SEV_DATA_FILE") == 0) {
464 fw_type = AMD_SEV_DATA;
465 subprog = 0;
466 } else if (strcmp(fw_name, "SEV_CODE_FILE") == 0) {
467 fw_type = AMD_SEV_CODE;
468 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800469 } else {
470 fw_type = AMD_FW_INVALID;
471 /* TODO: Add more */
472 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800473
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800474 /* Search and fill the filename */
475 psp_tableptr = &amd_psp_fw_table[0];
476 if (fw_type != AMD_FW_SKIP && fw_type != AMD_FW_INVALID) {
477 while (psp_tableptr->type != AMD_FW_INVALID) {
478 /* instance are not used in PSP table */
Zheng Bao5ca13432022-10-16 20:18:40 +0800479 if (psp_tableptr->type == fw_type && psp_tableptr->subprog == subprog
480 && psp_tableptr->inst == instance) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800481 psp_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800482 SET_LEVEL(psp_tableptr, level_to_set, PSP,
483 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800484 break;
485 }
486 psp_tableptr++;
487 }
488 }
489 if (fw_type == AMD_FW_INVALID)
490 return 0;
491 else
492 return 1;
493}
Zheng Bao1a9e5432022-02-17 17:48:27 +0800494#define PMUI_STR_BASE "PSP_PMUI_FILE"
495#define PMUD_STR_BASE "PSP_PMUD_FILE"
496#define PMU_STR_BASE_LEN strlen(PMUI_STR_BASE)
497#define PMU_STR_SUB_INDEX strlen(PMUI_STR_BASE"_SUB")
498#define PMU_STR_INS_INDEX strlen(PMUI_STR_BASE"_SUBx_INS")
Zheng Baofdb02942022-02-22 09:47:59 +0800499#define PMU_STR_ALL_LEN strlen(PMUI_STR_BASE"_SUBx_INSx")
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800500
501static uint8_t find_register_fw_filename_bios_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800502 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800503{
504 amd_bios_type fw_type = AMD_BIOS_INVALID;
505 amd_bios_entry *bhd_tableptr;
Felix Heldea3417b2020-11-20 20:08:42 +0100506 uint8_t subprog = 0;
507 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800508
509 (void) (cb_config); /* Remove warning and reserved for future. */
510
Zheng Bao1a9e5432022-02-17 17:48:27 +0800511 if (strncmp(fw_name, PMUI_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800512 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800513 fw_type = AMD_BIOS_PMUI;
Felix Held3dfb4852022-09-28 18:00:39 +0200514 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
515 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Bao1a9e5432022-02-17 17:48:27 +0800516 } else if (strncmp(fw_name, PMUD_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800517 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800518 fw_type = AMD_BIOS_PMUD;
Felix Held3dfb4852022-09-28 18:00:39 +0200519 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
520 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Baobf29a0d2020-12-03 23:00:48 +0800521 } else if (strcmp(fw_name, "RTM_PUBKEY_FILE") == 0) {
522 fw_type = AMD_BIOS_RTM_PUBKEY;
523 subprog = 0;
524 instance = 0;
Zheng Bao50143732020-11-14 21:54:06 +0800525 } else if (strcmp(fw_name, "PSP_MP2CFG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800526 if (cb_config->load_mp2_fw) {
Zheng Bao50143732020-11-14 21:54:06 +0800527 fw_type = AMD_BIOS_MP2_CFG;
528 subprog = 0;
529 } else {
Martin Rotha8e31ca2021-02-13 21:42:46 -0700530 fw_type = AMD_BIOS_SKIP;
Zheng Bao50143732020-11-14 21:54:06 +0800531 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800532 } else {
533 fw_type = AMD_BIOS_INVALID;
534 }
535
536 bhd_tableptr = amd_bios_table;
537
538 if (fw_type != AMD_BIOS_INVALID && fw_type != AMD_BIOS_SKIP) {
539 while (bhd_tableptr->type != AMD_BIOS_INVALID) {
540 if (bhd_tableptr->type == fw_type &&
541 bhd_tableptr->subpr == subprog &&
542 bhd_tableptr->inst == instance) {
543 bhd_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800544 SET_LEVEL(bhd_tableptr, level_to_set, BDT,
545 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800546 break;
547 }
548 bhd_tableptr++;
549 }
550 }
551 if (fw_type == AMD_BIOS_INVALID)
552 return 0;
553 else
554 return 1;
555}
556
557#define MAX_LINE_SIZE 1024
558
559int get_input_file_line(FILE *f, char line[], int line_buf_size)
560{
561 if (fgets(line, line_buf_size, f) == NULL)
562 return LINE_EOF;
563
564 /* If the file contains a line that is too long, then it's best
565 * to let the user know right away rather than passing back a
566 * truncated result that will lead to problems later on.
567 */
568 line[strlen(line) - 1] = '\0';
569
570 if (strlen(line) == ((size_t) (line_buf_size - 1))) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800571 fprintf(stderr, "The line size in config file should be lower than %d bytes.\n",
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800572 MAX_LINE_SIZE);
573 exit(1);
574 }
575
576 return OK;
577}
578
Zheng Bao4df5af82022-03-01 17:18:00 +0800579#define N_MATCHES 4
580static int is_valid_entry(char *oneline, regmatch_t match[N_MATCHES])
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800581{
Zheng Bao4df5af82022-03-01 17:18:00 +0800582 int retval, index;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800583
Zheng Bao4df5af82022-03-01 17:18:00 +0800584 for (index = 0; index < N_MATCHES; index++) {
585 match[index].rm_so = -1;
586 match[index].rm_eo = -1;
587 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800588 if (regexec(&entries_line_expr, oneline, 3, match, 0) == 0) {
589 oneline[match[1].rm_eo] = '\0';
590 oneline[match[2].rm_eo] = '\0';
591 retval = 1;
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800592 } else if (regexec(&entries_lvl_line_expr, oneline, 4, match, 0) == 0) {
593 /* match[1]: FW type
594 match[2]: FW filename
595 match[3]: Directory level to be dropped
596 */
597 oneline[match[1].rm_eo] = '\0';
598 oneline[match[2].rm_eo] = '\0';
599 oneline[match[3].rm_eo] = '\0';
600 retval = 1;
601 } else {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800602 retval = 0;
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800603 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800604
605 return retval;
606}
607
608static int skip_comment_blank_line(char *oneline)
609{
610 int retval;
611
612 if (regexec(&blank_or_comment_expr, oneline, 0, NULL, 0) == 0) {
613 /* skip comment and blank */
614 retval = 1;
615 } else {
616 /* no match */
617 retval = 0;
618 }
619
620 return retval;
621}
622
Zheng Bao52a18982022-03-01 17:22:52 +0800623char get_level_from_config(char *line, regoff_t level_index, amd_cb_config *cb_config)
624{
625 char lvl = 'x';
626 /* If the optional level field is present,
627 extract the level char. */
628 if (level_index != -1) {
629 if (cb_config->recovery_ab == 0)
630 lvl = line[level_index + 1];
631 else if (strlen(&line[level_index]) >= 3)
632 lvl = line[level_index + 2];
633 }
634
635 assert(lvl == 'x' || lvl == 'X' ||
636 lvl == 'b' || lvl == 'B' ||
637 lvl == '1' || lvl == '2');
638
639 return lvl;
640}
641
Zheng Bao7db76422021-03-27 18:15:35 +0800642static uint8_t process_one_line(char *oneline, regmatch_t *match, char *dir,
643 uint8_t print_deps, amd_cb_config *cb_config)
644{
645 char *path_filename, *fn = &(oneline[match[2].rm_so]);
646 char *fw_type_str = &(oneline[match[1].rm_so]);
647 char ch_lvl = 'x';
648 regoff_t ch_lvl_index = match[3].rm_so;
649
650 /* If the optional level field is present,
651 extract the level char. */
652 ch_lvl = get_level_from_config(oneline, ch_lvl_index, cb_config);
653
654 path_filename = malloc(MAX_LINE_SIZE * 2 + 2);
655 snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s/%.*s",
656 MAX_LINE_SIZE, dir, MAX_LINE_SIZE, fn);
657
658 if (find_register_fw_filename_psp_dir(
659 fw_type_str, path_filename, ch_lvl, cb_config) == 0) {
660 if (find_register_fw_filename_bios_dir(
661 fw_type_str, path_filename, ch_lvl, cb_config) == 0) {
662 fprintf(stderr, "Module's name \"%s\" is not valid\n", fw_type_str);
663 return 0; /* Stop parsing. */
664 } else {
665 if (print_deps)
666 printf(" %s ", path_filename);
667 }
668 } else {
669 if (print_deps)
670 printf(" %s ", path_filename);
671 }
672 return 1;
673}
674
Zheng Bao010cc992023-01-25 22:58:49 +0800675static bool needs_ish(enum platform platform_type)
676{
677 if (platform_type == PLATFORM_MENDOCINO || platform_type == PLATFORM_PHOENIX || platform_type == PLATFORM_GLINDA)
678 return true;
679 else
680 return false;
681}
682
683static bool is_second_gen(enum platform platform_type)
684{
685 switch (platform_type) {
686 case PLATFORM_CARRIZO:
687 case PLATFORM_STONEYRIDGE:
688 case PLATFORM_RAVEN:
689 case PLATFORM_PICASSO:
690 return false;
691 case PLATFORM_RENOIR:
692 case PLATFORM_LUCIENNE:
693 case PLATFORM_CEZANNE:
694 case PLATFORM_MENDOCINO:
695 case PLATFORM_PHOENIX:
696 case PLATFORM_GLINDA:
697 return true;
698 case PLATFORM_UNKNOWN:
699 default:
700 fprintf(stderr, "Error: Invalid SOC name.\n\n");
701 return false;
702 }
703}
704
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800705/*
706 return value:
707 0: The config file can not be parsed correctly.
708 1: The config file can be parsed correctly.
709 */
710uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps)
711{
Zheng Bao7db76422021-03-27 18:15:35 +0800712 char oneline[MAX_LINE_SIZE];
Zheng Bao4df5af82022-03-01 17:18:00 +0800713 regmatch_t match[N_MATCHES];
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800714 char dir[MAX_LINE_SIZE] = {'\0'};
Zheng Baodac44612021-05-27 11:11:34 +0800715 uint32_t dir_len;
Zheng Bao4df5af82022-03-01 17:18:00 +0800716 int index;
717
718 for (index = 0; index < N_MATCHES; index++) {
719 match[index].rm_so = -1;
720 match[index].rm_eo = -1;
721 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800722
723 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
724 blank_or_comment_regex, &blank_or_comment_expr);
725 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
726 entries_line_regex, &entries_line_expr);
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800727 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
728 entries_lvl_line_regex, &entries_lvl_line_expr);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800729
730 /* Get a line */
Zheng Bao3384e4a2020-10-06 12:03:11 +0800731 /* Get FIRMWARE_LOCATION in the first loop */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800732 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
733 /* get a line */
734 if (skip_comment_blank_line(oneline))
735 continue;
736 if (is_valid_entry(oneline, match)) {
Zheng Bao3384e4a2020-10-06 12:03:11 +0800737 if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0) {
Zheng Baodac44612021-05-27 11:11:34 +0800738 dir_len = match[2].rm_eo - match[2].rm_so;
739 assert(dir_len < MAX_LINE_SIZE);
740 snprintf(dir, MAX_LINE_SIZE, "%.*s", dir_len,
741 &(oneline[match[2].rm_so]));
Zheng Bao010cc992023-01-25 22:58:49 +0800742 } else if (strcmp(&(oneline[match[1].rm_so]), "SOC_NAME") == 0) {
743 cb_config->soc_id =
744 identify_platform(&(oneline[match[2].rm_so]));
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800745 }
746 }
747 }
748
Zheng Bao010cc992023-01-25 22:58:49 +0800749 cb_config->second_gen = is_second_gen(cb_config->soc_id);
750
751 if (needs_ish(cb_config->soc_id))
752 cb_config->need_ish = true;
753
754 if (cb_config->need_ish)
755 cb_config->recovery_ab = true;
756
757 if (cb_config->recovery_ab)
758 cb_config->multi_level = true;
759
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800760 if (dir[0] == '\0') {
761 fprintf(stderr, "No line with FIRMWARE_LOCATION\n");
762 return 0;
763 }
764
765 fseek(config, 0, SEEK_SET);
766 /* Get a line */
767 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
768 /* get a line */
769 if (skip_comment_blank_line(oneline))
770 continue;
771 if (is_valid_entry(oneline, match)) {
Zheng Bao29063fb2023-01-25 22:46:57 +0800772 if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0 ||
773 strcmp(&(oneline[match[1].rm_so]), "SOC_NAME") == 0) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800774 continue;
775 } else {
Zheng Bao7db76422021-03-27 18:15:35 +0800776 if (process_one_line(oneline, match, dir, print_deps,
777 cb_config) == 0)
778 return 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800779 }
780 } else {
781 fprintf(stderr, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline);
782 return 0;
783 }
784 }
785 return 1;
786}