Zheng Bao | addf340 | 2021-06-03 15:46:53 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <regex.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
Idwer Vollering | 93df1d9 | 2020-12-30 00:01:59 +0100 | [diff] [blame] | 7 | #include <stdint.h> |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 8 | #include <assert.h> |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 9 | |
| 10 | #include "amdfwtool.h" |
| 11 | |
| 12 | /* TODO: a empty line does not matched. */ |
| 13 | static 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 | "#.*$)"; |
| 21 | static regex_t blank_or_comment_expr; |
| 22 | |
| 23 | static 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:]]*$"; |
| 34 | static regex_t entries_line_expr; |
| 35 | |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 36 | static 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:]]*$"; |
| 66 | static regex_t entries_lvl_line_expr; |
| 67 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 68 | void 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 Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 77 | fprintf(stderr, "%s\n", error_msg); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 81 | #define SET_LEVEL(tableptr, l, TABLE) \ |
| 82 | do { \ |
| 83 | switch ((l)) { \ |
| 84 | case '1': \ |
| 85 | (tableptr)->level = TABLE##_LVL1;\ |
| 86 | break; \ |
| 87 | case '2': \ |
| 88 | (tableptr)->level = TABLE##_LVL2;\ |
| 89 | break; \ |
| 90 | case 'b': \ |
| 91 | case 'B': \ |
| 92 | (tableptr)->level = TABLE##_BOTH;\ |
| 93 | break; \ |
| 94 | default: \ |
| 95 | /* use default value */ \ |
| 96 | break; \ |
| 97 | } \ |
| 98 | } while (0) |
| 99 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 100 | extern amd_fw_entry amd_psp_fw_table[]; |
| 101 | extern amd_bios_entry amd_bios_table[]; |
| 102 | |
| 103 | static uint8_t find_register_fw_filename_psp_dir(char *fw_name, char *filename, |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 104 | char level_to_set, amd_cb_config *cb_config) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 105 | { |
| 106 | amd_fw_type fw_type = AMD_FW_INVALID; |
| 107 | amd_fw_entry *psp_tableptr; |
| 108 | uint8_t subprog; |
| 109 | |
| 110 | if (strcmp(fw_name, "PSPBTLDR_WL_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 111 | if (cb_config->have_whitelist) { |
Nikolai Vyssotski | 1965f650 | 2021-05-06 22:15:36 -0500 | [diff] [blame] | 112 | fw_type = AMD_FW_PSP_BOOTLOADER_AB; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 113 | subprog = 0; |
| 114 | } else { |
| 115 | fw_type = AMD_FW_SKIP; |
| 116 | } |
| 117 | } else if (strcmp(fw_name, "PSPBTLDR_FILE") == 0) { |
Nikolai Vyssotski | 1965f650 | 2021-05-06 22:15:36 -0500 | [diff] [blame] | 118 | fw_type = AMD_FW_PSP_BOOTLOADER; |
| 119 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 120 | } else if (strcmp(fw_name, "AMD_PUBKEY_FILE") == 0) { |
| 121 | fw_type = AMD_FW_PSP_PUBKEY; |
| 122 | subprog = 0; |
| 123 | } else if (strcmp(fw_name, "PSPRCVR_FILE") == 0) { |
| 124 | fw_type = AMD_FW_PSP_RECOVERY; |
| 125 | subprog = 0; |
| 126 | } else if (strcmp(fw_name, "PUBSIGNEDKEY_FILE") == 0) { |
| 127 | fw_type = AMD_FW_PSP_RTM_PUBKEY; |
| 128 | subprog = 0; |
| 129 | } else if (strcmp(fw_name, "PSPNVRAM_FILE") == 0) { |
| 130 | fw_type = AMD_FW_PSP_NVRAM; |
| 131 | subprog = 0; |
| 132 | } else if (strcmp(fw_name, "SMUSCS_FILE") == 0) { |
| 133 | fw_type = AMD_FW_PSP_SMUSCS; |
| 134 | subprog = 0; |
| 135 | } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) { |
| 136 | fw_type = AMD_FW_PSP_TRUSTLETS; |
| 137 | subprog = 0; |
| 138 | } else if (strcmp(fw_name, "PSPSECUREDEBUG_FILE") == 0) { |
| 139 | fw_type = AMD_FW_PSP_SECURED_DEBUG; |
| 140 | subprog = 0; |
| 141 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB0_FILE") == 0) { |
| 142 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 143 | subprog = 0; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 144 | } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE") == 0) { |
| 145 | fw_type = AMD_HW_IPCFG; |
| 146 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 147 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB1_FILE") == 0) { |
| 148 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 149 | subprog = 1; |
| 150 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB2_FILE") == 0) { |
| 151 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 152 | subprog = 2; |
| 153 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB0_FILE") == 0) { |
| 154 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 155 | subprog = 0; |
| 156 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB1_FILE") == 0) { |
| 157 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 158 | subprog = 1; |
| 159 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB2_FILE") == 0) { |
| 160 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 161 | subprog = 2; |
| 162 | } else if (strcmp(fw_name, "PSP_SEC_DBG_KEY_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 163 | if (cb_config->unlock_secure) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 164 | fw_type = AMD_FW_PSP_SECURED_DEBUG; |
| 165 | subprog = 0; |
| 166 | } else { |
| 167 | fw_type = AMD_FW_SKIP; |
| 168 | } |
| 169 | } else if (strcmp(fw_name, "PSP_SEC_DEBUG_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 170 | if (cb_config->unlock_secure) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 171 | fw_type = AMD_DEBUG_UNLOCK; |
| 172 | subprog = 0; |
| 173 | } else { |
| 174 | fw_type = AMD_FW_SKIP; |
| 175 | } |
| 176 | } else if (strcmp(fw_name, "PSP_ABL0_FILE") == 0) { |
| 177 | fw_type = AMD_ABL0; |
| 178 | subprog = 0; |
| 179 | } else if (strcmp(fw_name, "PSP_ABL1_FILE") == 0) { |
| 180 | fw_type = AMD_ABL1; |
| 181 | subprog = 0; |
| 182 | } else if (strcmp(fw_name, "PSP_ABL2_FILE") == 0) { |
| 183 | fw_type = AMD_ABL2; |
| 184 | subprog = 0; |
| 185 | } else if (strcmp(fw_name, "PSP_ABL3_FILE") == 0) { |
| 186 | fw_type = AMD_ABL3; |
| 187 | subprog = 0; |
| 188 | } else if (strcmp(fw_name, "PSP_ABL4_FILE") == 0) { |
| 189 | fw_type = AMD_ABL4; |
| 190 | subprog = 0; |
| 191 | } else if (strcmp(fw_name, "PSP_ABL5_FILE") == 0) { |
| 192 | fw_type = AMD_ABL5; |
| 193 | subprog = 0; |
| 194 | } else if (strcmp(fw_name, "PSP_ABL6_FILE") == 0) { |
| 195 | fw_type = AMD_ABL6; |
| 196 | subprog = 0; |
| 197 | } else if (strcmp(fw_name, "PSP_ABL7_FILE") == 0) { |
| 198 | fw_type = AMD_ABL7; |
| 199 | subprog = 0; |
| 200 | } else if (strcmp(fw_name, "PSPSECUREOS_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 201 | if (cb_config->use_secureos) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 202 | fw_type = AMD_FW_PSP_SECURED_OS; |
| 203 | subprog = 0; |
| 204 | } else { |
| 205 | fw_type = AMD_FW_SKIP; |
| 206 | } |
| 207 | } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) { |
| 208 | if (cb_config->use_secureos) { |
| 209 | fw_type = AMD_FW_PSP_TRUSTLETS; |
| 210 | subprog = 0; |
| 211 | } else { |
| 212 | fw_type = AMD_FW_SKIP; |
| 213 | } |
| 214 | } else if (strcmp(fw_name, "TRUSTLETKEY_FILE") == 0) { |
| 215 | if (cb_config->use_secureos) { |
| 216 | fw_type = AMD_FW_PSP_TRUSTLETKEY; |
| 217 | subprog = 0; |
| 218 | } else { |
| 219 | fw_type = AMD_FW_SKIP; |
| 220 | } |
| 221 | } else if (strcmp(fw_name, "PSP_IKEK_FILE") == 0) { |
| 222 | fw_type = AMD_WRAPPED_IKEK; |
| 223 | subprog = 0; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 224 | } else if (strcmp(fw_name, "PSP_SECG0_FILE") == 0) { |
| 225 | fw_type = AMD_SEC_GASKET; |
| 226 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 227 | } else if (strcmp(fw_name, "PSP_SECG1_FILE") == 0) { |
| 228 | fw_type = AMD_SEC_GASKET; |
| 229 | subprog = 1; |
| 230 | } else if (strcmp(fw_name, "PSP_SECG2_FILE") == 0) { |
| 231 | fw_type = AMD_SEC_GASKET; |
| 232 | subprog = 2; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 233 | } else if (strcmp(fw_name, "PSP_MP2FW0_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 234 | if (cb_config->load_mp2_fw) { |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 235 | fw_type = AMD_MP2_FW; |
| 236 | subprog = 0; |
| 237 | } else { |
| 238 | fw_type = AMD_FW_SKIP; |
| 239 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 240 | } else if (strcmp(fw_name, "PSP_MP2FW1_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 241 | if (cb_config->load_mp2_fw) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 242 | fw_type = AMD_MP2_FW; |
| 243 | subprog = 1; |
| 244 | } else { |
| 245 | fw_type = AMD_FW_SKIP; |
| 246 | } |
| 247 | } else if (strcmp(fw_name, "PSP_MP2FW2_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 248 | if (cb_config->load_mp2_fw) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 249 | fw_type = AMD_MP2_FW; |
| 250 | subprog = 2; |
| 251 | } else { |
| 252 | fw_type = AMD_FW_SKIP; |
| 253 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 254 | } else if (strcmp(fw_name, "PSP_DRIVERS_FILE") == 0) { |
| 255 | fw_type = AMD_DRIVER_ENTRIES; |
| 256 | subprog = 0; |
| 257 | } else if (strcmp(fw_name, "PSP_S0I3_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 258 | if (cb_config->s0i3) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 259 | fw_type = AMD_S0I3_DRIVER; |
| 260 | subprog = 0; |
| 261 | } else { |
| 262 | fw_type = AMD_FW_SKIP; |
| 263 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 264 | } else if (strcmp(fw_name, "AMD_DRIVER_ENTRIES") == 0) { |
| 265 | fw_type = AMD_DRIVER_ENTRIES; |
| 266 | subprog = 0; |
| 267 | } else if (strcmp(fw_name, "VBIOS_BTLOADER_FILE") == 0) { |
| 268 | fw_type = AMD_VBIOS_BTLOADER; |
| 269 | subprog = 0; |
| 270 | } else if (strcmp(fw_name, "SECURE_POLICY_L1_FILE") == 0) { |
| 271 | fw_type = AMD_FW_TOS_SEC_POLICY; |
| 272 | subprog = 0; |
| 273 | } else if (strcmp(fw_name, "UNIFIEDUSB_FILE") == 0) { |
| 274 | fw_type = AMD_FW_USB_PHY; |
| 275 | subprog = 0; |
| 276 | } else if (strcmp(fw_name, "DRTMTA_FILE") == 0) { |
| 277 | fw_type = AMD_FW_DRTM_TA; |
| 278 | subprog = 0; |
| 279 | } else if (strcmp(fw_name, "KEYDBBL_FILE") == 0) { |
| 280 | fw_type = AMD_FW_KEYDB_BL; |
| 281 | subprog = 0; |
| 282 | } else if (strcmp(fw_name, "KEYDB_TOS_FILE") == 0) { |
| 283 | fw_type = AMD_FW_KEYDB_TOS; |
| 284 | subprog = 0; |
| 285 | } else if (strcmp(fw_name, "DMCUERAMDCN21_FILE") == 0) { |
| 286 | fw_type = AMD_FW_DMCU_ERAM; |
| 287 | subprog = 0; |
| 288 | } else if (strcmp(fw_name, "DMCUINTVECTORSDCN21_FILE") == 0) { |
| 289 | fw_type = AMD_FW_DMCU_ISR; |
| 290 | subprog = 0; |
| 291 | } else if (strcmp(fw_name, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) { |
| 292 | fw_type = AMD_FW_KVM_IMAGE; |
| 293 | subprog = 0; |
| 294 | } else if (strcmp(fw_name, "RPMC_FILE") == 0) { |
| 295 | fw_type = AMD_RPMC_NVRAM; |
| 296 | subprog = 0; |
Zheng Bao | b993cb2 | 2021-02-02 18:48:23 +0800 | [diff] [blame] | 297 | } else if (strcmp(fw_name, "PSPBTLDR_AB_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 298 | if (!cb_config->have_whitelist) { |
Nikolai Vyssotski | 1965f650 | 2021-05-06 22:15:36 -0500 | [diff] [blame] | 299 | fw_type = AMD_FW_PSP_BOOTLOADER_AB; |
| 300 | subprog = 0; |
| 301 | } else { |
| 302 | fw_type = AMD_FW_SKIP; |
| 303 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 304 | } else { |
| 305 | fw_type = AMD_FW_INVALID; |
| 306 | /* TODO: Add more */ |
| 307 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 308 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 309 | /* Search and fill the filename */ |
| 310 | psp_tableptr = &amd_psp_fw_table[0]; |
| 311 | if (fw_type != AMD_FW_SKIP && fw_type != AMD_FW_INVALID) { |
| 312 | while (psp_tableptr->type != AMD_FW_INVALID) { |
| 313 | /* instance are not used in PSP table */ |
| 314 | if (psp_tableptr->type == fw_type && psp_tableptr->subprog == subprog) { |
| 315 | psp_tableptr->filename = filename; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 316 | SET_LEVEL(psp_tableptr, level_to_set, PSP); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | psp_tableptr++; |
| 320 | } |
| 321 | } |
| 322 | if (fw_type == AMD_FW_INVALID) |
| 323 | return 0; |
| 324 | else |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | static uint8_t find_register_fw_filename_bios_dir(char *fw_name, char *filename, |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 329 | char level_to_set, amd_cb_config *cb_config) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 330 | { |
| 331 | amd_bios_type fw_type = AMD_BIOS_INVALID; |
| 332 | amd_bios_entry *bhd_tableptr; |
Felix Held | ea3417b | 2020-11-20 20:08:42 +0100 | [diff] [blame] | 333 | uint8_t subprog = 0; |
| 334 | uint8_t instance = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 335 | |
| 336 | (void) (cb_config); /* Remove warning and reserved for future. */ |
| 337 | |
| 338 | if (strcmp(fw_name, "PSP_PMUI_FILE1") == 0) { |
| 339 | fw_type = AMD_BIOS_PMUI; |
| 340 | subprog = 0; |
| 341 | instance = 1; |
| 342 | } else if (strcmp(fw_name, "PSP_PMUI_FILE2") == 0) { |
| 343 | fw_type = AMD_BIOS_PMUI; |
| 344 | subprog = 0; |
| 345 | instance = 4; |
| 346 | } else if (strcmp(fw_name, "PSP_PMUI_FILE3") == 0) { |
| 347 | fw_type = AMD_BIOS_PMUI; |
| 348 | subprog = 1; |
| 349 | instance = 1; |
| 350 | } else if (strcmp(fw_name, "PSP_PMUI_FILE4") == 0) { |
| 351 | fw_type = AMD_BIOS_PMUI; |
| 352 | subprog = 1; |
| 353 | instance = 4; |
| 354 | } else if (strcmp(fw_name, "PSP_PMUD_FILE1") == 0) { |
| 355 | fw_type = AMD_BIOS_PMUD; |
| 356 | subprog = 0; |
| 357 | instance = 1; |
| 358 | } else if (strcmp(fw_name, "PSP_PMUD_FILE2") == 0) { |
| 359 | fw_type = AMD_BIOS_PMUD; |
| 360 | subprog = 0; |
| 361 | instance = 4; |
| 362 | } else if (strcmp(fw_name, "PSP_PMUD_FILE3") == 0) { |
| 363 | fw_type = AMD_BIOS_PMUD; |
| 364 | subprog = 1; |
| 365 | instance = 1; |
| 366 | } else if (strcmp(fw_name, "PSP_PMUD_FILE4") == 0) { |
| 367 | fw_type = AMD_BIOS_PMUD; |
| 368 | subprog = 1; |
| 369 | instance = 4; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 370 | } else if (strcmp(fw_name, "RTM_PUBKEY_FILE") == 0) { |
| 371 | fw_type = AMD_BIOS_RTM_PUBKEY; |
| 372 | subprog = 0; |
| 373 | instance = 0; |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 374 | } else if (strcmp(fw_name, "PSP_MP2CFG_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame^] | 375 | if (cb_config->load_mp2_fw) { |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 376 | fw_type = AMD_BIOS_MP2_CFG; |
| 377 | subprog = 0; |
| 378 | } else { |
Martin Roth | a8e31ca | 2021-02-13 21:42:46 -0700 | [diff] [blame] | 379 | fw_type = AMD_BIOS_SKIP; |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 380 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 381 | } else { |
| 382 | fw_type = AMD_BIOS_INVALID; |
| 383 | } |
| 384 | |
| 385 | bhd_tableptr = amd_bios_table; |
| 386 | |
| 387 | if (fw_type != AMD_BIOS_INVALID && fw_type != AMD_BIOS_SKIP) { |
| 388 | while (bhd_tableptr->type != AMD_BIOS_INVALID) { |
| 389 | if (bhd_tableptr->type == fw_type && |
| 390 | bhd_tableptr->subpr == subprog && |
| 391 | bhd_tableptr->inst == instance) { |
| 392 | bhd_tableptr->filename = filename; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 393 | SET_LEVEL(bhd_tableptr, level_to_set, BDT); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | bhd_tableptr++; |
| 397 | } |
| 398 | } |
| 399 | if (fw_type == AMD_BIOS_INVALID) |
| 400 | return 0; |
| 401 | else |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | #define MAX_LINE_SIZE 1024 |
| 406 | |
| 407 | int get_input_file_line(FILE *f, char line[], int line_buf_size) |
| 408 | { |
| 409 | if (fgets(line, line_buf_size, f) == NULL) |
| 410 | return LINE_EOF; |
| 411 | |
| 412 | /* If the file contains a line that is too long, then it's best |
| 413 | * to let the user know right away rather than passing back a |
| 414 | * truncated result that will lead to problems later on. |
| 415 | */ |
| 416 | line[strlen(line) - 1] = '\0'; |
| 417 | |
| 418 | if (strlen(line) == ((size_t) (line_buf_size - 1))) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 419 | fprintf(stderr, "The line size in config file should be lower than %d bytes.\n", |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 420 | MAX_LINE_SIZE); |
| 421 | exit(1); |
| 422 | } |
| 423 | |
| 424 | return OK; |
| 425 | } |
| 426 | |
| 427 | static int is_valid_entry(char *oneline, regmatch_t *match) |
| 428 | { |
| 429 | int retval; |
| 430 | |
| 431 | if (regexec(&entries_line_expr, oneline, 3, match, 0) == 0) { |
| 432 | oneline[match[1].rm_eo] = '\0'; |
| 433 | oneline[match[2].rm_eo] = '\0'; |
| 434 | retval = 1; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 435 | } else if (regexec(&entries_lvl_line_expr, oneline, 4, match, 0) == 0) { |
| 436 | /* match[1]: FW type |
| 437 | match[2]: FW filename |
| 438 | match[3]: Directory level to be dropped |
| 439 | */ |
| 440 | oneline[match[1].rm_eo] = '\0'; |
| 441 | oneline[match[2].rm_eo] = '\0'; |
| 442 | oneline[match[3].rm_eo] = '\0'; |
| 443 | retval = 1; |
| 444 | } else { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 445 | retval = 0; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 446 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 447 | |
| 448 | return retval; |
| 449 | } |
| 450 | |
| 451 | static int skip_comment_blank_line(char *oneline) |
| 452 | { |
| 453 | int retval; |
| 454 | |
| 455 | if (regexec(&blank_or_comment_expr, oneline, 0, NULL, 0) == 0) { |
| 456 | /* skip comment and blank */ |
| 457 | retval = 1; |
| 458 | } else { |
| 459 | /* no match */ |
| 460 | retval = 0; |
| 461 | } |
| 462 | |
| 463 | return retval; |
| 464 | } |
| 465 | |
| 466 | #define N_MATCHES 4 |
| 467 | /* |
| 468 | return value: |
| 469 | 0: The config file can not be parsed correctly. |
| 470 | 1: The config file can be parsed correctly. |
| 471 | */ |
| 472 | uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_deps) |
| 473 | { |
| 474 | char oneline[MAX_LINE_SIZE], *path_filename; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 475 | regmatch_t match[N_MATCHES] = {0}; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 476 | char dir[MAX_LINE_SIZE] = {'\0'}; |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 477 | uint32_t dir_len; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 478 | |
| 479 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, |
| 480 | blank_or_comment_regex, &blank_or_comment_expr); |
| 481 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, |
| 482 | entries_line_regex, &entries_line_expr); |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 483 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, |
| 484 | entries_lvl_line_regex, &entries_lvl_line_expr); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 485 | |
| 486 | /* Get a line */ |
Zheng Bao | 3384e4a | 2020-10-06 12:03:11 +0800 | [diff] [blame] | 487 | /* Get FIRMWARE_LOCATION in the first loop */ |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 488 | while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) { |
| 489 | /* get a line */ |
| 490 | if (skip_comment_blank_line(oneline)) |
| 491 | continue; |
| 492 | if (is_valid_entry(oneline, match)) { |
Zheng Bao | 3384e4a | 2020-10-06 12:03:11 +0800 | [diff] [blame] | 493 | if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0) { |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 494 | dir_len = match[2].rm_eo - match[2].rm_so; |
| 495 | assert(dir_len < MAX_LINE_SIZE); |
| 496 | snprintf(dir, MAX_LINE_SIZE, "%.*s", dir_len, |
| 497 | &(oneline[match[2].rm_so])); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (dir[0] == '\0') { |
| 504 | fprintf(stderr, "No line with FIRMWARE_LOCATION\n"); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | fseek(config, 0, SEEK_SET); |
| 509 | /* Get a line */ |
| 510 | while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) { |
| 511 | /* get a line */ |
| 512 | if (skip_comment_blank_line(oneline)) |
| 513 | continue; |
| 514 | if (is_valid_entry(oneline, match)) { |
Zheng Bao | 3384e4a | 2020-10-06 12:03:11 +0800 | [diff] [blame] | 515 | if (strcmp(&(oneline[match[1].rm_so]), "FIRMWARE_LOCATION") == 0) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 516 | continue; |
| 517 | } else { |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 518 | char ch_lvl = 'x'; |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 519 | path_filename = malloc(MAX_LINE_SIZE * 2 + 2); |
| 520 | snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s/%.*s", |
| 521 | MAX_LINE_SIZE, dir, MAX_LINE_SIZE, |
| 522 | &(oneline[match[2].rm_so])); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 523 | |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 524 | /* If the optional level field is present, |
| 525 | extract the level char. */ |
| 526 | if (match[3].rm_so != 0) { |
| 527 | ch_lvl = oneline[match[3].rm_so + 1]; |
| 528 | } |
| 529 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 530 | if (find_register_fw_filename_psp_dir( |
| 531 | &(oneline[match[1].rm_so]), |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 532 | path_filename, ch_lvl, cb_config) == 0) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 533 | if (find_register_fw_filename_bios_dir( |
| 534 | &(oneline[match[1].rm_so]), |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 535 | path_filename, ch_lvl, cb_config) |
| 536 | == 0) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 537 | fprintf(stderr, "Module's name \"%s\" is not valid\n", oneline); |
| 538 | return 0; /* Stop parsing. */ |
| 539 | } else { |
| 540 | if (print_deps) |
| 541 | printf(" %s ", path_filename); |
| 542 | } |
| 543 | } else { |
| 544 | if (print_deps) |
| 545 | printf(" %s ", path_filename); |
| 546 | } |
| 547 | } |
| 548 | } else { |
| 549 | fprintf(stderr, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline); |
| 550 | return 0; |
| 551 | } |
| 552 | } |
| 553 | return 1; |
| 554 | } |