blob: 793fa9bf04eb96c7f60857c10957b4f07f9cf091 [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 Bao990d1542021-09-17 13:24:54 +080081#define SET_LEVEL(tableptr, l, TABLE, ab) \
Zheng Baob1fb8ce2021-09-13 18:00:09 +080082 do { \
83 switch ((l)) { \
84 case '1': \
Zheng Bao990d1542021-09-17 13:24:54 +080085 (tableptr)->level = ab ? TABLE##_LVL1_AB : TABLE##_LVL1; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +080086 break; \
87 case '2': \
Zheng Bao990d1542021-09-17 13:24:54 +080088 (tableptr)->level = ab ? TABLE##_LVL2_AB : TABLE##_LVL2; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +080089 break; \
90 case 'b': \
91 case 'B': \
Zheng Bao990d1542021-09-17 13:24:54 +080092 (tableptr)->level = ab ? TABLE##_BOTH_AB : TABLE##_BOTH; \
Zheng Baob1fb8ce2021-09-13 18:00:09 +080093 break; \
94 default: \
95 /* use default value */ \
96 break; \
97 } \
98 } while (0)
99
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800100extern amd_fw_entry amd_psp_fw_table[];
101extern amd_bios_entry amd_bios_table[];
102
103static uint8_t find_register_fw_filename_psp_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800104 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800105{
106 amd_fw_type fw_type = AMD_FW_INVALID;
107 amd_fw_entry *psp_tableptr;
108 uint8_t subprog;
Zheng Bao5ca13432022-10-16 20:18:40 +0800109 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800110
111 if (strcmp(fw_name, "PSPBTLDR_WL_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800112 if (cb_config->have_whitelist) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500113 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800114 subprog = 0;
115 } else {
116 fw_type = AMD_FW_SKIP;
117 }
Zheng Bao990d1542021-09-17 13:24:54 +0800118 } else if (strcmp(fw_name, "PSPBTLDR_AB_STAGE1_FILE") == 0) {
119 if (cb_config->recovery_ab) {
120 fw_type = AMD_FW_PSP_BOOTLOADER;
121 subprog = 0;
122 } else {
123 fw_type = AMD_FW_SKIP;
124 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800125 } else if (strcmp(fw_name, "PSPBTLDR_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800126 if (!cb_config->recovery_ab) {
127 fw_type = AMD_FW_PSP_BOOTLOADER;
128 subprog = 0;
129 } else {
130 fw_type = AMD_FW_SKIP;
131 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800132 } else if (strcmp(fw_name, "AMD_PUBKEY_FILE") == 0) {
133 fw_type = AMD_FW_PSP_PUBKEY;
134 subprog = 0;
135 } else if (strcmp(fw_name, "PSPRCVR_FILE") == 0) {
136 fw_type = AMD_FW_PSP_RECOVERY;
137 subprog = 0;
138 } else if (strcmp(fw_name, "PUBSIGNEDKEY_FILE") == 0) {
139 fw_type = AMD_FW_PSP_RTM_PUBKEY;
140 subprog = 0;
141 } else if (strcmp(fw_name, "PSPNVRAM_FILE") == 0) {
142 fw_type = AMD_FW_PSP_NVRAM;
143 subprog = 0;
144 } else if (strcmp(fw_name, "SMUSCS_FILE") == 0) {
145 fw_type = AMD_FW_PSP_SMUSCS;
146 subprog = 0;
147 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
148 fw_type = AMD_FW_PSP_TRUSTLETS;
149 subprog = 0;
150 } else if (strcmp(fw_name, "PSPSECUREDEBUG_FILE") == 0) {
151 fw_type = AMD_FW_PSP_SECURED_DEBUG;
152 subprog = 0;
153 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB0_FILE") == 0) {
154 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
155 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800156 } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE") == 0) {
157 fw_type = AMD_HW_IPCFG;
158 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800159 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB1_FILE") == 0) {
160 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
161 subprog = 1;
162 } else if (strcmp(fw_name, "PSP_SMUFW1_SUB2_FILE") == 0) {
163 fw_type = AMD_FW_PSP_SMU_FIRMWARE;
164 subprog = 2;
165 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB0_FILE") == 0) {
166 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
167 subprog = 0;
168 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB1_FILE") == 0) {
169 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
170 subprog = 1;
171 } else if (strcmp(fw_name, "PSP_SMUFW2_SUB2_FILE") == 0) {
172 fw_type = AMD_FW_PSP_SMU_FIRMWARE2;
173 subprog = 2;
Zheng Bao8eba6622022-10-16 20:29:03 +0800174 } else if (strcmp(fw_name, "PSP_BOOT_DRIVER_FILE") == 0) {
175 fw_type = AMD_BOOT_DRIVER;
176 subprog = 0;
177 } else if (strcmp(fw_name, "PSP_SOC_DRIVER_FILE") == 0) {
178 fw_type = AMD_SOC_DRIVER;
179 subprog = 0;
180 } else if (strcmp(fw_name, "PSP_DEBUG_DRIVER_FILE") == 0) {
181 fw_type = AMD_DEBUG_DRIVER;
182 subprog = 0;
183 } else if (strcmp(fw_name, "PSP_INTERFACE_DRIVER_FILE") == 0) {
184 fw_type = AMD_INTERFACE_DRIVER;
185 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800186 } else if (strcmp(fw_name, "PSP_SEC_DBG_KEY_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800187 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800188 fw_type = AMD_FW_PSP_SECURED_DEBUG;
189 subprog = 0;
190 } else {
191 fw_type = AMD_FW_SKIP;
192 }
193 } else if (strcmp(fw_name, "PSP_SEC_DEBUG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800194 if (cb_config->unlock_secure) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800195 fw_type = AMD_DEBUG_UNLOCK;
196 subprog = 0;
197 } else {
198 fw_type = AMD_FW_SKIP;
199 }
200 } else if (strcmp(fw_name, "PSP_ABL0_FILE") == 0) {
201 fw_type = AMD_ABL0;
202 subprog = 0;
203 } else if (strcmp(fw_name, "PSP_ABL1_FILE") == 0) {
204 fw_type = AMD_ABL1;
205 subprog = 0;
206 } else if (strcmp(fw_name, "PSP_ABL2_FILE") == 0) {
207 fw_type = AMD_ABL2;
208 subprog = 0;
209 } else if (strcmp(fw_name, "PSP_ABL3_FILE") == 0) {
210 fw_type = AMD_ABL3;
211 subprog = 0;
212 } else if (strcmp(fw_name, "PSP_ABL4_FILE") == 0) {
213 fw_type = AMD_ABL4;
214 subprog = 0;
215 } else if (strcmp(fw_name, "PSP_ABL5_FILE") == 0) {
216 fw_type = AMD_ABL5;
217 subprog = 0;
218 } else if (strcmp(fw_name, "PSP_ABL6_FILE") == 0) {
219 fw_type = AMD_ABL6;
220 subprog = 0;
221 } else if (strcmp(fw_name, "PSP_ABL7_FILE") == 0) {
222 fw_type = AMD_ABL7;
223 subprog = 0;
224 } else if (strcmp(fw_name, "PSPSECUREOS_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800225 if (cb_config->use_secureos) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800226 fw_type = AMD_FW_PSP_SECURED_OS;
227 subprog = 0;
228 } else {
229 fw_type = AMD_FW_SKIP;
230 }
231 } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) {
232 if (cb_config->use_secureos) {
233 fw_type = AMD_FW_PSP_TRUSTLETS;
234 subprog = 0;
235 } else {
236 fw_type = AMD_FW_SKIP;
237 }
238 } else if (strcmp(fw_name, "TRUSTLETKEY_FILE") == 0) {
239 if (cb_config->use_secureos) {
240 fw_type = AMD_FW_PSP_TRUSTLETKEY;
241 subprog = 0;
242 } else {
243 fw_type = AMD_FW_SKIP;
244 }
245 } else if (strcmp(fw_name, "PSP_IKEK_FILE") == 0) {
246 fw_type = AMD_WRAPPED_IKEK;
247 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800248 } else if (strcmp(fw_name, "PSP_SECG0_FILE") == 0) {
249 fw_type = AMD_SEC_GASKET;
250 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800251 } else if (strcmp(fw_name, "PSP_SECG1_FILE") == 0) {
252 fw_type = AMD_SEC_GASKET;
253 subprog = 1;
254 } else if (strcmp(fw_name, "PSP_SECG2_FILE") == 0) {
255 fw_type = AMD_SEC_GASKET;
256 subprog = 2;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800257 } else if (strcmp(fw_name, "PSP_MP2FW0_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800258 if (cb_config->load_mp2_fw) {
Zheng Baobf29a0d2020-12-03 23:00:48 +0800259 fw_type = AMD_MP2_FW;
260 subprog = 0;
261 } else {
262 fw_type = AMD_FW_SKIP;
263 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800264 } else if (strcmp(fw_name, "PSP_MP2FW1_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800265 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800266 fw_type = AMD_MP2_FW;
267 subprog = 1;
268 } else {
269 fw_type = AMD_FW_SKIP;
270 }
271 } else if (strcmp(fw_name, "PSP_MP2FW2_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800272 if (cb_config->load_mp2_fw) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800273 fw_type = AMD_MP2_FW;
274 subprog = 2;
275 } else {
276 fw_type = AMD_FW_SKIP;
277 }
Zheng Bao8eba6622022-10-16 20:29:03 +0800278 } else if (strcmp(fw_name, "PSP_C20MP_FILE") == 0) {
279 fw_type = AMD_FW_C20_MP;
280 subprog = 0;
281 } else if (strcmp(fw_name, "RIB_FILE") == 0) {
282 fw_type = AMD_RIB;
283 subprog = 0;
284 } else if (strcmp(fw_name, "AMF_SRAM_FILE") == 0) {
285 fw_type = AMD_FW_AMF_SRAM;
286 subprog = 0;
287 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS0") == 0) {
288 fw_type = AMD_FW_AMF_DRAM;
289 subprog = 0;
290 instance = 0;
291 } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS1") == 0) {
292 fw_type = AMD_FW_AMF_DRAM;
293 subprog = 0;
294 instance = 1;
295 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS0") == 0) {
296 fw_type = AMD_FW_AMF_WLAN;
297 subprog = 0;
298 instance = 0;
299 } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS1") == 0) {
300 fw_type = AMD_FW_AMF_WLAN;
301 subprog = 0;
302 instance = 1;
303 } else if (strcmp(fw_name, "AMF_MFD_FILE") == 0) {
304 fw_type = AMD_FW_AMF_MFD;
305 subprog = 0;
306 } else if (strcmp(fw_name, "MPCCX_FILE") == 0) {
307 fw_type = AMD_FW_MPCCX;
308 subprog = 0;
309 } else if (strcmp(fw_name, "LSDMA_FILE") == 0) {
310 fw_type = AMD_FW_LSDMA;
311 subprog = 0;
312 } else if (strcmp(fw_name, "MINIMSMU_FILE") == 0) {
313 fw_type = AMD_FW_MINIMSMU;
314 instance = 0;
315 subprog = 0;
316 } else if (strcmp(fw_name, "MINIMSMU_FILE_INS1") == 0) {
317 fw_type = AMD_FW_MINIMSMU;
318 instance = 1;
319 subprog = 0;
320 } else if (strcmp(fw_name, "SRAM_FW_EXT_FILE") == 0) {
321 fw_type = AMD_FW_SRAM_FW_EXT;
322 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800323 } else if (strcmp(fw_name, "PSP_DRIVERS_FILE") == 0) {
324 fw_type = AMD_DRIVER_ENTRIES;
325 subprog = 0;
326 } else if (strcmp(fw_name, "PSP_S0I3_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800327 if (cb_config->s0i3) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800328 fw_type = AMD_S0I3_DRIVER;
329 subprog = 0;
330 } else {
331 fw_type = AMD_FW_SKIP;
332 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800333 } else if (strcmp(fw_name, "AMD_DRIVER_ENTRIES") == 0) {
334 fw_type = AMD_DRIVER_ENTRIES;
335 subprog = 0;
336 } else if (strcmp(fw_name, "VBIOS_BTLOADER_FILE") == 0) {
337 fw_type = AMD_VBIOS_BTLOADER;
338 subprog = 0;
339 } else if (strcmp(fw_name, "SECURE_POLICY_L1_FILE") == 0) {
340 fw_type = AMD_FW_TOS_SEC_POLICY;
341 subprog = 0;
342 } else if (strcmp(fw_name, "UNIFIEDUSB_FILE") == 0) {
343 fw_type = AMD_FW_USB_PHY;
344 subprog = 0;
345 } else if (strcmp(fw_name, "DRTMTA_FILE") == 0) {
346 fw_type = AMD_FW_DRTM_TA;
347 subprog = 0;
348 } else if (strcmp(fw_name, "KEYDBBL_FILE") == 0) {
349 fw_type = AMD_FW_KEYDB_BL;
350 subprog = 0;
351 } else if (strcmp(fw_name, "KEYDB_TOS_FILE") == 0) {
352 fw_type = AMD_FW_KEYDB_TOS;
353 subprog = 0;
Zheng Baoab84fd72022-01-27 22:38:27 +0800354 } else if (strcmp(fw_name, "SPL_TABLE_FILE") == 0) {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800355 if (cb_config->have_mb_spl) {
Felix Held11b0d362022-04-02 03:49:07 +0200356 fw_type = AMD_FW_SKIP;
357 } else {
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800358 fw_type = AMD_FW_SPL;
359 subprog = 0;
Zheng Bao6c5ec8e2022-02-11 11:51:26 +0800360 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800361 } else if (strcmp(fw_name, "DMCUERAMDCN21_FILE") == 0) {
362 fw_type = AMD_FW_DMCU_ERAM;
363 subprog = 0;
364 } else if (strcmp(fw_name, "DMCUINTVECTORSDCN21_FILE") == 0) {
365 fw_type = AMD_FW_DMCU_ISR;
366 subprog = 0;
Felix Held5f18bb72022-03-24 02:04:51 +0100367 } else if (strcmp(fw_name, "MSMU_FILE") == 0) {
368 fw_type = AMD_FW_MSMU;
369 subprog = 0;
370 } else if (strcmp(fw_name, "DMCUB_FILE") == 0) {
371 fw_type = AMD_FW_DMCUB;
372 subprog = 0;
373 } else if (strcmp(fw_name, "SPIROM_CONFIG_FILE") == 0) {
374 fw_type = AMD_FW_SPIROM_CFG;
375 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800376 } else if (strcmp(fw_name, "MPIO_FILE") == 0) {
377 fw_type = AMD_FW_MPIO;
378 subprog = 0;
379 } else if (strcmp(fw_name, "TPMLITE_FILE") == 0) {
380 printf("TPMLITE\n");
381 fw_type = AMD_FW_TPMLITE;
382 subprog = 0;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800383 } else if (strcmp(fw_name, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) {
384 fw_type = AMD_FW_KVM_IMAGE;
385 subprog = 0;
386 } else if (strcmp(fw_name, "RPMC_FILE") == 0) {
387 fw_type = AMD_RPMC_NVRAM;
388 subprog = 0;
Zheng Baob993cb22021-02-02 18:48:23 +0800389 } else if (strcmp(fw_name, "PSPBTLDR_AB_FILE") == 0) {
Zheng Bao990d1542021-09-17 13:24:54 +0800390 if (!cb_config->have_whitelist || cb_config->recovery_ab) {
Nikolai Vyssotski1965f6502021-05-06 22:15:36 -0500391 fw_type = AMD_FW_PSP_BOOTLOADER_AB;
392 subprog = 0;
393 } else {
394 fw_type = AMD_FW_SKIP;
395 }
Karthikeyan Ramasubramanian0ab04d22022-05-03 18:16:34 -0600396 } else if (strcmp(fw_name, "TA_IKEK_FILE") == 0) {
397 fw_type = AMD_TA_IKEK;
398 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200399 } else if (strcmp(fw_name, "PSP_OEM_ABL_KEY_FILE") == 0) {
400 fw_type = AMD_FW_ABL_PUBKEY;
401 subprog = 0;
402 } else if (strcmp(fw_name, "PSP_MP5FW_SUB0_FILE") == 0) {
403 fw_type = AMD_FW_MP5;
404 subprog = 0;
405 } else if (strcmp(fw_name, "PSP_MP5FW_SUB1_FILE") == 0) {
406 fw_type = AMD_FW_MP5;
407 subprog = 1;
408 } else if (strcmp(fw_name, "PSP_MP5FW_SUB2_FILE") == 0) {
409 fw_type = AMD_FW_MP5;
410 subprog = 2;
411 } else if (strcmp(fw_name, "PSP_DXIOFW_FILE") == 0) {
412 fw_type = AMD_FW_DXIO;
413 subprog = 0;
414 } else if (strcmp(fw_name, "PSP_MPIOFW_FILE") == 0) {
415 fw_type = AMD_FW_MPIO;
416 subprog = 0;
417 } else if (strcmp(fw_name, "PSP_RIB_FILE") == 0) {
418 fw_type = AMD_RIB;
419 subprog = 0;
Zheng Bao8eba6622022-10-16 20:29:03 +0800420 } else if (strcmp(fw_name, "FEATURE_TABLE_FILE") == 0) {
421 fw_type = AMD_FW_FCFG_TABLE;
422 subprog = 0;
Arthur Heymans1f05c802022-10-04 17:50:21 +0200423 } else if (strcmp(fw_name, "PSP_MPDMATFFW_FILE") == 0) {
424 fw_type = AMD_FW_MPDMA_TF;
425 subprog = 0;
426 } else if (strcmp(fw_name, "PSP_GMI3PHYFW_FILE") == 0) {
427 fw_type = AMD_FW_GMI3_PHY;
428 subprog = 0;
429 } else if (strcmp(fw_name, "PSP_MPDMAPMFW_FILE") == 0) {
430 fw_type = AMD_FW_MPDMA_PM;
431 subprog = 0;
432 } else if (strcmp(fw_name, "PSP_TOKEN_UNLOCK_FILE") == 0) {
433 fw_type = AMD_TOKEN_UNLOCK;
434 subprog = 0;
435 } else if (strcmp(fw_name, "SEV_DATA_FILE") == 0) {
436 fw_type = AMD_SEV_DATA;
437 subprog = 0;
438 } else if (strcmp(fw_name, "SEV_CODE_FILE") == 0) {
439 fw_type = AMD_SEV_CODE;
440 subprog = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800441 } else {
442 fw_type = AMD_FW_INVALID;
443 /* TODO: Add more */
444 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800445
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800446 /* Search and fill the filename */
447 psp_tableptr = &amd_psp_fw_table[0];
448 if (fw_type != AMD_FW_SKIP && fw_type != AMD_FW_INVALID) {
449 while (psp_tableptr->type != AMD_FW_INVALID) {
450 /* instance are not used in PSP table */
Zheng Bao5ca13432022-10-16 20:18:40 +0800451 if (psp_tableptr->type == fw_type && psp_tableptr->subprog == subprog
452 && psp_tableptr->inst == instance) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800453 psp_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800454 SET_LEVEL(psp_tableptr, level_to_set, PSP,
455 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800456 break;
457 }
458 psp_tableptr++;
459 }
460 }
461 if (fw_type == AMD_FW_INVALID)
462 return 0;
463 else
464 return 1;
465}
Zheng Bao1a9e5432022-02-17 17:48:27 +0800466#define PMUI_STR_BASE "PSP_PMUI_FILE"
467#define PMUD_STR_BASE "PSP_PMUD_FILE"
468#define PMU_STR_BASE_LEN strlen(PMUI_STR_BASE)
469#define PMU_STR_SUB_INDEX strlen(PMUI_STR_BASE"_SUB")
470#define PMU_STR_INS_INDEX strlen(PMUI_STR_BASE"_SUBx_INS")
Zheng Baofdb02942022-02-22 09:47:59 +0800471#define PMU_STR_ALL_LEN strlen(PMUI_STR_BASE"_SUBx_INSx")
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800472
473static uint8_t find_register_fw_filename_bios_dir(char *fw_name, char *filename,
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800474 char level_to_set, amd_cb_config *cb_config)
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800475{
476 amd_bios_type fw_type = AMD_BIOS_INVALID;
477 amd_bios_entry *bhd_tableptr;
Felix Heldea3417b2020-11-20 20:08:42 +0100478 uint8_t subprog = 0;
479 uint8_t instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800480
481 (void) (cb_config); /* Remove warning and reserved for future. */
482
Zheng Bao1a9e5432022-02-17 17:48:27 +0800483 if (strncmp(fw_name, PMUI_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800484 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800485 fw_type = AMD_BIOS_PMUI;
Felix Held3dfb4852022-09-28 18:00:39 +0200486 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
487 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Bao1a9e5432022-02-17 17:48:27 +0800488 } else if (strncmp(fw_name, PMUD_STR_BASE, PMU_STR_BASE_LEN) == 0) {
Zheng Baofdb02942022-02-22 09:47:59 +0800489 assert(strlen(fw_name) == PMU_STR_ALL_LEN);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800490 fw_type = AMD_BIOS_PMUD;
Felix Held3dfb4852022-09-28 18:00:39 +0200491 subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16);
492 instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16);
Zheng Baobf29a0d2020-12-03 23:00:48 +0800493 } else if (strcmp(fw_name, "RTM_PUBKEY_FILE") == 0) {
494 fw_type = AMD_BIOS_RTM_PUBKEY;
495 subprog = 0;
496 instance = 0;
Zheng Bao50143732020-11-14 21:54:06 +0800497 } else if (strcmp(fw_name, "PSP_MP2CFG_FILE") == 0) {
Zheng Baoba3af5e2021-11-04 18:56:47 +0800498 if (cb_config->load_mp2_fw) {
Zheng Bao50143732020-11-14 21:54:06 +0800499 fw_type = AMD_BIOS_MP2_CFG;
500 subprog = 0;
501 } else {
Martin Rotha8e31ca2021-02-13 21:42:46 -0700502 fw_type = AMD_BIOS_SKIP;
Zheng Bao50143732020-11-14 21:54:06 +0800503 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800504 } else {
505 fw_type = AMD_BIOS_INVALID;
506 }
507
508 bhd_tableptr = amd_bios_table;
509
510 if (fw_type != AMD_BIOS_INVALID && fw_type != AMD_BIOS_SKIP) {
511 while (bhd_tableptr->type != AMD_BIOS_INVALID) {
512 if (bhd_tableptr->type == fw_type &&
513 bhd_tableptr->subpr == subprog &&
514 bhd_tableptr->inst == instance) {
515 bhd_tableptr->filename = filename;
Zheng Bao990d1542021-09-17 13:24:54 +0800516 SET_LEVEL(bhd_tableptr, level_to_set, BDT,
517 cb_config->recovery_ab);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800518 break;
519 }
520 bhd_tableptr++;
521 }
522 }
523 if (fw_type == AMD_BIOS_INVALID)
524 return 0;
525 else
526 return 1;
527}
528
529#define MAX_LINE_SIZE 1024
530
531int get_input_file_line(FILE *f, char line[], int line_buf_size)
532{
533 if (fgets(line, line_buf_size, f) == NULL)
534 return LINE_EOF;
535
536 /* If the file contains a line that is too long, then it's best
537 * to let the user know right away rather than passing back a
538 * truncated result that will lead to problems later on.
539 */
540 line[strlen(line) - 1] = '\0';
541
542 if (strlen(line) == ((size_t) (line_buf_size - 1))) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800543 fprintf(stderr, "The line size in config file should be lower than %d bytes.\n",
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800544 MAX_LINE_SIZE);
545 exit(1);
546 }
547
548 return OK;
549}
550
Zheng Bao4df5af82022-03-01 17:18:00 +0800551#define N_MATCHES 4
552static int is_valid_entry(char *oneline, regmatch_t match[N_MATCHES])
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800553{
Zheng Bao4df5af82022-03-01 17:18:00 +0800554 int retval, index;
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800555
Zheng Bao4df5af82022-03-01 17:18:00 +0800556 for (index = 0; index < N_MATCHES; index++) {
557 match[index].rm_so = -1;
558 match[index].rm_eo = -1;
559 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800560 if (regexec(&entries_line_expr, oneline, 3, match, 0) == 0) {
561 oneline[match[1].rm_eo] = '\0';
562 oneline[match[2].rm_eo] = '\0';
563 retval = 1;
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800564 } else if (regexec(&entries_lvl_line_expr, oneline, 4, match, 0) == 0) {
565 /* match[1]: FW type
566 match[2]: FW filename
567 match[3]: Directory level to be dropped
568 */
569 oneline[match[1].rm_eo] = '\0';
570 oneline[match[2].rm_eo] = '\0';
571 oneline[match[3].rm_eo] = '\0';
572 retval = 1;
573 } else {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800574 retval = 0;
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800575 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800576
577 return retval;
578}
579
580static int skip_comment_blank_line(char *oneline)
581{
582 int retval;
583
584 if (regexec(&blank_or_comment_expr, oneline, 0, NULL, 0) == 0) {
585 /* skip comment and blank */
586 retval = 1;
587 } else {
588 /* no match */
589 retval = 0;
590 }
591
592 return retval;
593}
594
Zheng Bao52a18982022-03-01 17:22:52 +0800595char get_level_from_config(char *line, regoff_t level_index, amd_cb_config *cb_config)
596{
597 char lvl = 'x';
598 /* If the optional level field is present,
599 extract the level char. */
600 if (level_index != -1) {
601 if (cb_config->recovery_ab == 0)
602 lvl = line[level_index + 1];
603 else if (strlen(&line[level_index]) >= 3)
604 lvl = line[level_index + 2];
605 }
606
607 assert(lvl == 'x' || lvl == 'X' ||
608 lvl == 'b' || lvl == 'B' ||
609 lvl == '1' || lvl == '2');
610
611 return lvl;
612}
613
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800614/*
615 return value:
616 0: The config file can not be parsed correctly.
617 1: The config file can be parsed correctly.
618 */
619uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps)
620{
621 char oneline[MAX_LINE_SIZE], *path_filename;
Zheng Bao4df5af82022-03-01 17:18:00 +0800622 regmatch_t match[N_MATCHES];
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800623 char dir[MAX_LINE_SIZE] = {'\0'};
Zheng Baodac44612021-05-27 11:11:34 +0800624 uint32_t dir_len;
Zheng Bao4df5af82022-03-01 17:18:00 +0800625 int index;
626
627 for (index = 0; index < N_MATCHES; index++) {
628 match[index].rm_so = -1;
629 match[index].rm_eo = -1;
630 }
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800631
632 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
633 blank_or_comment_regex, &blank_or_comment_expr);
634 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
635 entries_line_regex, &entries_line_expr);
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800636 compile_reg_expr(REG_EXTENDED | REG_NEWLINE,
637 entries_lvl_line_regex, &entries_lvl_line_expr);
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800638
639 /* Get a line */
Zheng Bao3384e4a2020-10-06 12:03:11 +0800640 /* Get FIRMWARE_LOCATION in the first loop */
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800641 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
642 /* get a line */
643 if (skip_comment_blank_line(oneline))
644 continue;
645 if (is_valid_entry(oneline, match)) {
Zheng Bao3384e4a2020-10-06 12:03:11 +0800646 if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0) {
Zheng Baodac44612021-05-27 11:11:34 +0800647 dir_len = match[2].rm_eo - match[2].rm_so;
648 assert(dir_len < MAX_LINE_SIZE);
649 snprintf(dir, MAX_LINE_SIZE, "%.*s", dir_len,
650 &(oneline[match[2].rm_so]));
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800651 break;
652 }
653 }
654 }
655
656 if (dir[0] == '\0') {
657 fprintf(stderr, "No line with FIRMWARE_LOCATION\n");
658 return 0;
659 }
660
661 fseek(config, 0, SEEK_SET);
662 /* Get a line */
663 while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) {
664 /* get a line */
665 if (skip_comment_blank_line(oneline))
666 continue;
667 if (is_valid_entry(oneline, match)) {
Zheng Bao3384e4a2020-10-06 12:03:11 +0800668 if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800669 continue;
670 } else {
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800671 char ch_lvl = 'x';
Zheng Baodac44612021-05-27 11:11:34 +0800672 path_filename = malloc(MAX_LINE_SIZE * 2 + 2);
673 snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s/%.*s",
674 MAX_LINE_SIZE, dir, MAX_LINE_SIZE,
675 &(oneline[match[2].rm_so]));
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800676
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800677 /* If the optional level field is present,
678 extract the level char. */
Zheng Bao52a18982022-03-01 17:22:52 +0800679 ch_lvl = get_level_from_config(oneline,
680 match[3].rm_so, cb_config);
Zheng Bao4df5af82022-03-01 17:18:00 +0800681
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800682 if (find_register_fw_filename_psp_dir(
683 &(oneline[match[1].rm_so]),
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800684 path_filename, ch_lvl, cb_config) == 0) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800685 if (find_register_fw_filename_bios_dir(
686 &(oneline[match[1].rm_so]),
Zheng Baob1fb8ce2021-09-13 18:00:09 +0800687 path_filename, ch_lvl, cb_config)
688 == 0) {
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800689 fprintf(stderr, "Module's name \"%s\" is not valid\n", oneline);
690 return 0; /* Stop parsing. */
691 } else {
692 if (print_deps)
693 printf(" %s ", path_filename);
694 }
695 } else {
696 if (print_deps)
697 printf(" %s ", path_filename);
698 }
699 }
700 } else {
701 fprintf(stderr, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline);
702 return 0;
703 }
704 }
705 return 1;
706}