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:]]+)" |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 32 | /* followed by an optional whitespace + chunk of nonwhitespace for level field |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 33 | 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 Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 48 | "([[:space:]]+([Ll][12bxBX]{1,2}))?" |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 49 | /* followed by an optional whitespace + chunk of nonwhitespace for hash table field |
| 50 | 1st char H: Indicator of field "Hash Table ID" |
| 51 | 2nd char: |
| 52 | Table ID to be dropped in. |
| 53 | 0: Table 0 / Default Unified Table |
| 54 | 1: Table 1 |
| 55 | ... |
| 56 | 9: Table 9 |
| 57 | |
| 58 | Examples: |
| 59 | H2: Put the hash for the concerned entry in Hash Table 2 |
| 60 | */ |
| 61 | "([[:space:]]+([Hh][0-9]+))?" |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 62 | /* followed by an optional whitespace + "UUID" to indicate the binary is using 16 bytes |
| 63 | UUID as firmware identity. In the absence of this field, the binary is using 2 bytes |
| 64 | FWID as firmware identity. |
| 65 | */ |
| 66 | "([[:space:]]+(UUID))?" |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 67 | /* followed by optional whitespace */ |
| 68 | "[[:space:]]*$"; |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 69 | static regex_t entries_line_expr; |
| 70 | |
| 71 | enum match_id { |
| 72 | FW_TYPE = 1, |
| 73 | FW_FILE, |
| 74 | OPT_SPACE1, |
| 75 | OPT_LEVEL, |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 76 | OPT_SPACE2, |
| 77 | OPT_HASH_TABLE_ID, |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 78 | OPT_SPACE3, |
| 79 | OPT_FWID_TYPE, |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 80 | N_MATCHES, |
| 81 | }; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 82 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 83 | void compile_reg_expr(int cflags, const char *expr, regex_t *reg) |
| 84 | { |
| 85 | static const size_t ERROR_BUF_SIZE = 256; |
| 86 | char error_msg[ERROR_BUF_SIZE]; |
| 87 | int result; |
| 88 | |
| 89 | result = regcomp(reg, expr, cflags); |
| 90 | if (result != 0) { |
| 91 | regerror(result, reg, error_msg, ERROR_BUF_SIZE); |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 92 | fprintf(stderr, "%s\n", error_msg); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Zheng Bao | 010cc99 | 2023-01-25 22:58:49 +0800 | [diff] [blame] | 96 | static enum platform identify_platform(char *soc_name) |
| 97 | { |
| 98 | if (!strcasecmp(soc_name, "Stoneyridge")) |
| 99 | return PLATFORM_STONEYRIDGE; |
| 100 | else if (!strcasecmp(soc_name, "Carrizo")) |
| 101 | return PLATFORM_CARRIZO; |
| 102 | else if (!strcasecmp(soc_name, "Raven")) |
| 103 | return PLATFORM_RAVEN; |
| 104 | else if (!strcasecmp(soc_name, "Picasso")) |
| 105 | return PLATFORM_PICASSO; |
| 106 | else if (!strcasecmp(soc_name, "Cezanne")) |
| 107 | return PLATFORM_CEZANNE; |
| 108 | else if (!strcasecmp(soc_name, "Mendocino")) |
| 109 | return PLATFORM_MENDOCINO; |
| 110 | else if (!strcasecmp(soc_name, "Renoir")) |
| 111 | return PLATFORM_RENOIR; |
| 112 | else if (!strcasecmp(soc_name, "Lucienne")) |
| 113 | return PLATFORM_LUCIENNE; |
| 114 | else if (!strcasecmp(soc_name, "Phoenix")) |
| 115 | return PLATFORM_PHOENIX; |
| 116 | else if (!strcasecmp(soc_name, "Glinda")) |
| 117 | return PLATFORM_GLINDA; |
| 118 | else |
| 119 | return PLATFORM_UNKNOWN; |
| 120 | } |
| 121 | |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 122 | #define SET_LEVEL(tableptr, l, TABLE, ab) \ |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 123 | do { \ |
| 124 | switch ((l)) { \ |
| 125 | case '1': \ |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 126 | (tableptr)->level = ab ? TABLE##_LVL1_AB : TABLE##_LVL1; \ |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 127 | break; \ |
| 128 | case '2': \ |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 129 | (tableptr)->level = ab ? TABLE##_LVL2_AB : TABLE##_LVL2; \ |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 130 | break; \ |
| 131 | case 'b': \ |
| 132 | case 'B': \ |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 133 | (tableptr)->level = ab ? TABLE##_BOTH_AB : TABLE##_BOTH; \ |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 134 | break; \ |
| 135 | default: \ |
| 136 | /* use default value */ \ |
| 137 | break; \ |
| 138 | } \ |
| 139 | } while (0) |
| 140 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 141 | extern amd_fw_entry amd_psp_fw_table[]; |
| 142 | extern amd_bios_entry amd_bios_table[]; |
| 143 | |
| 144 | static uint8_t find_register_fw_filename_psp_dir(char *fw_name, char *filename, |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 145 | char level_to_set, uint8_t hash_tbl_id, fwid_type_t fwid_type, |
| 146 | amd_cb_config *cb_config) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 147 | { |
| 148 | amd_fw_type fw_type = AMD_FW_INVALID; |
| 149 | amd_fw_entry *psp_tableptr; |
| 150 | uint8_t subprog; |
Zheng Bao | 5ca1343 | 2022-10-16 20:18:40 +0800 | [diff] [blame] | 151 | uint8_t instance = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 152 | |
| 153 | if (strcmp(fw_name, "PSPBTLDR_WL_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 154 | if (cb_config->have_whitelist) { |
Nikolai Vyssotski | 1965f650 | 2021-05-06 22:15:36 -0500 | [diff] [blame] | 155 | fw_type = AMD_FW_PSP_BOOTLOADER_AB; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 156 | subprog = 0; |
| 157 | } else { |
| 158 | fw_type = AMD_FW_SKIP; |
| 159 | } |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 160 | } else if (strcmp(fw_name, "PSPBTLDR_AB_STAGE1_FILE") == 0) { |
| 161 | if (cb_config->recovery_ab) { |
| 162 | fw_type = AMD_FW_PSP_BOOTLOADER; |
| 163 | subprog = 0; |
| 164 | } else { |
| 165 | fw_type = AMD_FW_SKIP; |
| 166 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 167 | } else if (strcmp(fw_name, "PSPBTLDR_FILE") == 0) { |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 168 | if (!cb_config->recovery_ab) { |
| 169 | fw_type = AMD_FW_PSP_BOOTLOADER; |
| 170 | subprog = 0; |
| 171 | } else { |
| 172 | fw_type = AMD_FW_SKIP; |
| 173 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 174 | } else if (strcmp(fw_name, "AMD_PUBKEY_FILE") == 0) { |
| 175 | fw_type = AMD_FW_PSP_PUBKEY; |
| 176 | subprog = 0; |
| 177 | } else if (strcmp(fw_name, "PSPRCVR_FILE") == 0) { |
| 178 | fw_type = AMD_FW_PSP_RECOVERY; |
| 179 | subprog = 0; |
| 180 | } else if (strcmp(fw_name, "PUBSIGNEDKEY_FILE") == 0) { |
| 181 | fw_type = AMD_FW_PSP_RTM_PUBKEY; |
| 182 | subprog = 0; |
| 183 | } else if (strcmp(fw_name, "PSPNVRAM_FILE") == 0) { |
| 184 | fw_type = AMD_FW_PSP_NVRAM; |
| 185 | subprog = 0; |
| 186 | } else if (strcmp(fw_name, "SMUSCS_FILE") == 0) { |
| 187 | fw_type = AMD_FW_PSP_SMUSCS; |
| 188 | subprog = 0; |
| 189 | } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) { |
| 190 | fw_type = AMD_FW_PSP_TRUSTLETS; |
| 191 | subprog = 0; |
| 192 | } else if (strcmp(fw_name, "PSPSECUREDEBUG_FILE") == 0) { |
| 193 | fw_type = AMD_FW_PSP_SECURED_DEBUG; |
| 194 | subprog = 0; |
| 195 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB0_FILE") == 0) { |
| 196 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 197 | subprog = 0; |
Zheng Bao | 9bb62cb | 2023-03-07 19:48:11 +0800 | [diff] [blame] | 198 | } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE_SUB0") == 0) { |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 199 | fw_type = AMD_HW_IPCFG; |
| 200 | subprog = 0; |
Zheng Bao | 9bb62cb | 2023-03-07 19:48:11 +0800 | [diff] [blame] | 201 | } else if (strcmp(fw_name, "PSP_HW_IPCFG_FILE_SUB1") == 0) { |
| 202 | fw_type = AMD_HW_IPCFG; |
| 203 | subprog = 1; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 204 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB1_FILE") == 0) { |
| 205 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 206 | subprog = 1; |
| 207 | } else if (strcmp(fw_name, "PSP_SMUFW1_SUB2_FILE") == 0) { |
| 208 | fw_type = AMD_FW_PSP_SMU_FIRMWARE; |
| 209 | subprog = 2; |
| 210 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB0_FILE") == 0) { |
| 211 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 212 | subprog = 0; |
| 213 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB1_FILE") == 0) { |
| 214 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 215 | subprog = 1; |
| 216 | } else if (strcmp(fw_name, "PSP_SMUFW2_SUB2_FILE") == 0) { |
| 217 | fw_type = AMD_FW_PSP_SMU_FIRMWARE2; |
| 218 | subprog = 2; |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 219 | } else if (strcmp(fw_name, "PSP_BOOT_DRIVER_FILE") == 0) { |
| 220 | fw_type = AMD_BOOT_DRIVER; |
| 221 | subprog = 0; |
| 222 | } else if (strcmp(fw_name, "PSP_SOC_DRIVER_FILE") == 0) { |
| 223 | fw_type = AMD_SOC_DRIVER; |
| 224 | subprog = 0; |
| 225 | } else if (strcmp(fw_name, "PSP_DEBUG_DRIVER_FILE") == 0) { |
| 226 | fw_type = AMD_DEBUG_DRIVER; |
| 227 | subprog = 0; |
| 228 | } else if (strcmp(fw_name, "PSP_INTERFACE_DRIVER_FILE") == 0) { |
| 229 | fw_type = AMD_INTERFACE_DRIVER; |
| 230 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 231 | } else if (strcmp(fw_name, "PSP_SEC_DBG_KEY_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 232 | if (cb_config->unlock_secure) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 233 | fw_type = AMD_FW_PSP_SECURED_DEBUG; |
| 234 | subprog = 0; |
| 235 | } else { |
| 236 | fw_type = AMD_FW_SKIP; |
| 237 | } |
| 238 | } else if (strcmp(fw_name, "PSP_SEC_DEBUG_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 239 | if (cb_config->unlock_secure) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 240 | fw_type = AMD_DEBUG_UNLOCK; |
| 241 | subprog = 0; |
| 242 | } else { |
| 243 | fw_type = AMD_FW_SKIP; |
| 244 | } |
| 245 | } else if (strcmp(fw_name, "PSP_ABL0_FILE") == 0) { |
| 246 | fw_type = AMD_ABL0; |
| 247 | subprog = 0; |
| 248 | } else if (strcmp(fw_name, "PSP_ABL1_FILE") == 0) { |
| 249 | fw_type = AMD_ABL1; |
| 250 | subprog = 0; |
| 251 | } else if (strcmp(fw_name, "PSP_ABL2_FILE") == 0) { |
| 252 | fw_type = AMD_ABL2; |
| 253 | subprog = 0; |
| 254 | } else if (strcmp(fw_name, "PSP_ABL3_FILE") == 0) { |
| 255 | fw_type = AMD_ABL3; |
| 256 | subprog = 0; |
| 257 | } else if (strcmp(fw_name, "PSP_ABL4_FILE") == 0) { |
| 258 | fw_type = AMD_ABL4; |
| 259 | subprog = 0; |
| 260 | } else if (strcmp(fw_name, "PSP_ABL5_FILE") == 0) { |
| 261 | fw_type = AMD_ABL5; |
| 262 | subprog = 0; |
| 263 | } else if (strcmp(fw_name, "PSP_ABL6_FILE") == 0) { |
| 264 | fw_type = AMD_ABL6; |
| 265 | subprog = 0; |
| 266 | } else if (strcmp(fw_name, "PSP_ABL7_FILE") == 0) { |
| 267 | fw_type = AMD_ABL7; |
| 268 | subprog = 0; |
| 269 | } else if (strcmp(fw_name, "PSPSECUREOS_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 270 | if (cb_config->use_secureos) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 271 | fw_type = AMD_FW_PSP_SECURED_OS; |
| 272 | subprog = 0; |
| 273 | } else { |
| 274 | fw_type = AMD_FW_SKIP; |
| 275 | } |
| 276 | } else if (strcmp(fw_name, "PSPTRUSTLETS_FILE") == 0) { |
| 277 | if (cb_config->use_secureos) { |
| 278 | fw_type = AMD_FW_PSP_TRUSTLETS; |
| 279 | subprog = 0; |
| 280 | } else { |
| 281 | fw_type = AMD_FW_SKIP; |
| 282 | } |
| 283 | } else if (strcmp(fw_name, "TRUSTLETKEY_FILE") == 0) { |
| 284 | if (cb_config->use_secureos) { |
| 285 | fw_type = AMD_FW_PSP_TRUSTLETKEY; |
| 286 | subprog = 0; |
| 287 | } else { |
| 288 | fw_type = AMD_FW_SKIP; |
| 289 | } |
| 290 | } else if (strcmp(fw_name, "PSP_IKEK_FILE") == 0) { |
| 291 | fw_type = AMD_WRAPPED_IKEK; |
| 292 | subprog = 0; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 293 | } else if (strcmp(fw_name, "PSP_SECG0_FILE") == 0) { |
| 294 | fw_type = AMD_SEC_GASKET; |
| 295 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 296 | } else if (strcmp(fw_name, "PSP_SECG1_FILE") == 0) { |
| 297 | fw_type = AMD_SEC_GASKET; |
| 298 | subprog = 1; |
| 299 | } else if (strcmp(fw_name, "PSP_SECG2_FILE") == 0) { |
| 300 | fw_type = AMD_SEC_GASKET; |
| 301 | subprog = 2; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 302 | } else if (strcmp(fw_name, "PSP_MP2FW0_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 303 | if (cb_config->load_mp2_fw) { |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 304 | fw_type = AMD_MP2_FW; |
| 305 | subprog = 0; |
| 306 | } else { |
| 307 | fw_type = AMD_FW_SKIP; |
| 308 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 309 | } else if (strcmp(fw_name, "PSP_MP2FW1_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 310 | if (cb_config->load_mp2_fw) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 311 | fw_type = AMD_MP2_FW; |
| 312 | subprog = 1; |
| 313 | } else { |
| 314 | fw_type = AMD_FW_SKIP; |
| 315 | } |
| 316 | } else if (strcmp(fw_name, "PSP_MP2FW2_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 317 | if (cb_config->load_mp2_fw) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 318 | fw_type = AMD_MP2_FW; |
| 319 | subprog = 2; |
| 320 | } else { |
| 321 | fw_type = AMD_FW_SKIP; |
| 322 | } |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 323 | } else if (strcmp(fw_name, "PSP_C20MP_FILE") == 0) { |
| 324 | fw_type = AMD_FW_C20_MP; |
| 325 | subprog = 0; |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 326 | } else if (strcmp(fw_name, "AMF_SRAM_FILE") == 0) { |
| 327 | fw_type = AMD_FW_AMF_SRAM; |
| 328 | subprog = 0; |
| 329 | } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS0") == 0) { |
| 330 | fw_type = AMD_FW_AMF_DRAM; |
| 331 | subprog = 0; |
| 332 | instance = 0; |
| 333 | } else if (strcmp(fw_name, "AMF_DRAM_FILE_INS1") == 0) { |
| 334 | fw_type = AMD_FW_AMF_DRAM; |
| 335 | subprog = 0; |
| 336 | instance = 1; |
| 337 | } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS0") == 0) { |
| 338 | fw_type = AMD_FW_AMF_WLAN; |
| 339 | subprog = 0; |
| 340 | instance = 0; |
| 341 | } else if (strcmp(fw_name, "AMF_WLAN_FILE_INS1") == 0) { |
| 342 | fw_type = AMD_FW_AMF_WLAN; |
| 343 | subprog = 0; |
| 344 | instance = 1; |
| 345 | } else if (strcmp(fw_name, "AMF_MFD_FILE") == 0) { |
| 346 | fw_type = AMD_FW_AMF_MFD; |
| 347 | subprog = 0; |
| 348 | } else if (strcmp(fw_name, "MPCCX_FILE") == 0) { |
| 349 | fw_type = AMD_FW_MPCCX; |
| 350 | subprog = 0; |
| 351 | } else if (strcmp(fw_name, "LSDMA_FILE") == 0) { |
| 352 | fw_type = AMD_FW_LSDMA; |
| 353 | subprog = 0; |
| 354 | } else if (strcmp(fw_name, "MINIMSMU_FILE") == 0) { |
| 355 | fw_type = AMD_FW_MINIMSMU; |
| 356 | instance = 0; |
| 357 | subprog = 0; |
| 358 | } else if (strcmp(fw_name, "MINIMSMU_FILE_INS1") == 0) { |
| 359 | fw_type = AMD_FW_MINIMSMU; |
| 360 | instance = 1; |
| 361 | subprog = 0; |
| 362 | } else if (strcmp(fw_name, "SRAM_FW_EXT_FILE") == 0) { |
| 363 | fw_type = AMD_FW_SRAM_FW_EXT; |
| 364 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 365 | } else if (strcmp(fw_name, "PSP_DRIVERS_FILE") == 0) { |
| 366 | fw_type = AMD_DRIVER_ENTRIES; |
| 367 | subprog = 0; |
| 368 | } else if (strcmp(fw_name, "PSP_S0I3_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 369 | if (cb_config->s0i3) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 370 | fw_type = AMD_S0I3_DRIVER; |
| 371 | subprog = 0; |
| 372 | } else { |
| 373 | fw_type = AMD_FW_SKIP; |
| 374 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 375 | } else if (strcmp(fw_name, "AMD_DRIVER_ENTRIES") == 0) { |
| 376 | fw_type = AMD_DRIVER_ENTRIES; |
| 377 | subprog = 0; |
| 378 | } else if (strcmp(fw_name, "VBIOS_BTLOADER_FILE") == 0) { |
| 379 | fw_type = AMD_VBIOS_BTLOADER; |
| 380 | subprog = 0; |
| 381 | } else if (strcmp(fw_name, "SECURE_POLICY_L1_FILE") == 0) { |
| 382 | fw_type = AMD_FW_TOS_SEC_POLICY; |
| 383 | subprog = 0; |
| 384 | } else if (strcmp(fw_name, "UNIFIEDUSB_FILE") == 0) { |
| 385 | fw_type = AMD_FW_USB_PHY; |
| 386 | subprog = 0; |
| 387 | } else if (strcmp(fw_name, "DRTMTA_FILE") == 0) { |
| 388 | fw_type = AMD_FW_DRTM_TA; |
| 389 | subprog = 0; |
| 390 | } else if (strcmp(fw_name, "KEYDBBL_FILE") == 0) { |
| 391 | fw_type = AMD_FW_KEYDB_BL; |
| 392 | subprog = 0; |
| 393 | } else if (strcmp(fw_name, "KEYDB_TOS_FILE") == 0) { |
| 394 | fw_type = AMD_FW_KEYDB_TOS; |
| 395 | subprog = 0; |
Zheng Bao | ab84fd7 | 2022-01-27 22:38:27 +0800 | [diff] [blame] | 396 | } else if (strcmp(fw_name, "SPL_TABLE_FILE") == 0) { |
Zheng Bao | 6c5ec8e | 2022-02-11 11:51:26 +0800 | [diff] [blame] | 397 | if (cb_config->have_mb_spl) { |
Felix Held | 11b0d36 | 2022-04-02 03:49:07 +0200 | [diff] [blame] | 398 | fw_type = AMD_FW_SKIP; |
| 399 | } else { |
Zheng Bao | 6c5ec8e | 2022-02-11 11:51:26 +0800 | [diff] [blame] | 400 | fw_type = AMD_FW_SPL; |
| 401 | subprog = 0; |
Zheng Bao | 6c5ec8e | 2022-02-11 11:51:26 +0800 | [diff] [blame] | 402 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 403 | } else if (strcmp(fw_name, "DMCUERAMDCN21_FILE") == 0) { |
| 404 | fw_type = AMD_FW_DMCU_ERAM; |
| 405 | subprog = 0; |
| 406 | } else if (strcmp(fw_name, "DMCUINTVECTORSDCN21_FILE") == 0) { |
| 407 | fw_type = AMD_FW_DMCU_ISR; |
| 408 | subprog = 0; |
Felix Held | 5f18bb7 | 2022-03-24 02:04:51 +0100 | [diff] [blame] | 409 | } else if (strcmp(fw_name, "MSMU_FILE") == 0) { |
| 410 | fw_type = AMD_FW_MSMU; |
| 411 | subprog = 0; |
| 412 | } else if (strcmp(fw_name, "DMCUB_FILE") == 0) { |
| 413 | fw_type = AMD_FW_DMCUB; |
| 414 | subprog = 0; |
| 415 | } else if (strcmp(fw_name, "SPIROM_CONFIG_FILE") == 0) { |
| 416 | fw_type = AMD_FW_SPIROM_CFG; |
| 417 | subprog = 0; |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 418 | } else if (strcmp(fw_name, "MPIO_FILE") == 0) { |
| 419 | fw_type = AMD_FW_MPIO; |
| 420 | subprog = 0; |
| 421 | } else if (strcmp(fw_name, "TPMLITE_FILE") == 0) { |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 422 | fw_type = AMD_FW_TPMLITE; |
| 423 | subprog = 0; |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 424 | } else if (strcmp(fw_name, "PSP_KVM_ENGINE_DUMMY_FILE") == 0) { |
| 425 | fw_type = AMD_FW_KVM_IMAGE; |
| 426 | subprog = 0; |
| 427 | } else if (strcmp(fw_name, "RPMC_FILE") == 0) { |
| 428 | fw_type = AMD_RPMC_NVRAM; |
| 429 | subprog = 0; |
Zheng Bao | b993cb2 | 2021-02-02 18:48:23 +0800 | [diff] [blame] | 430 | } else if (strcmp(fw_name, "PSPBTLDR_AB_FILE") == 0) { |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 431 | if (!cb_config->have_whitelist || cb_config->recovery_ab) { |
Nikolai Vyssotski | 1965f650 | 2021-05-06 22:15:36 -0500 | [diff] [blame] | 432 | fw_type = AMD_FW_PSP_BOOTLOADER_AB; |
| 433 | subprog = 0; |
| 434 | } else { |
| 435 | fw_type = AMD_FW_SKIP; |
| 436 | } |
Karthikeyan Ramasubramanian | 0ab04d2 | 2022-05-03 18:16:34 -0600 | [diff] [blame] | 437 | } else if (strcmp(fw_name, "TA_IKEK_FILE") == 0) { |
| 438 | fw_type = AMD_TA_IKEK; |
| 439 | subprog = 0; |
Fred Reitberger | c4f3a33 | 2023-02-07 12:12:40 -0500 | [diff] [blame] | 440 | } else if (strcmp(fw_name, "UMSMU_FILE") == 0) { |
| 441 | fw_type = AMD_FW_UMSMU; |
| 442 | subprog = 0; |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 443 | } else if (strcmp(fw_name, "PSP_OEM_ABL_KEY_FILE") == 0) { |
| 444 | fw_type = AMD_FW_ABL_PUBKEY; |
| 445 | subprog = 0; |
| 446 | } else if (strcmp(fw_name, "PSP_MP5FW_SUB0_FILE") == 0) { |
| 447 | fw_type = AMD_FW_MP5; |
| 448 | subprog = 0; |
| 449 | } else if (strcmp(fw_name, "PSP_MP5FW_SUB1_FILE") == 0) { |
| 450 | fw_type = AMD_FW_MP5; |
| 451 | subprog = 1; |
| 452 | } else if (strcmp(fw_name, "PSP_MP5FW_SUB2_FILE") == 0) { |
| 453 | fw_type = AMD_FW_MP5; |
| 454 | subprog = 2; |
| 455 | } else if (strcmp(fw_name, "PSP_DXIOFW_FILE") == 0) { |
| 456 | fw_type = AMD_FW_DXIO; |
| 457 | subprog = 0; |
| 458 | } else if (strcmp(fw_name, "PSP_MPIOFW_FILE") == 0) { |
| 459 | fw_type = AMD_FW_MPIO; |
| 460 | subprog = 0; |
Zheng Bao | 85ee1fd | 2023-01-30 13:52:30 +0800 | [diff] [blame] | 461 | } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB0") == 0) { |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 462 | fw_type = AMD_RIB; |
| 463 | subprog = 0; |
Zheng Bao | 85ee1fd | 2023-01-30 13:52:30 +0800 | [diff] [blame] | 464 | } else if (strcmp(fw_name, "PSP_RIB_FILE_SUB1") == 0) { |
| 465 | fw_type = AMD_RIB; |
| 466 | subprog = 1; |
Zheng Bao | 8eba662 | 2022-10-16 20:29:03 +0800 | [diff] [blame] | 467 | } else if (strcmp(fw_name, "FEATURE_TABLE_FILE") == 0) { |
| 468 | fw_type = AMD_FW_FCFG_TABLE; |
| 469 | subprog = 0; |
Arthur Heymans | 1f05c80 | 2022-10-04 17:50:21 +0200 | [diff] [blame] | 470 | } else if (strcmp(fw_name, "PSP_MPDMATFFW_FILE") == 0) { |
| 471 | fw_type = AMD_FW_MPDMA_TF; |
| 472 | subprog = 0; |
| 473 | } else if (strcmp(fw_name, "PSP_GMI3PHYFW_FILE") == 0) { |
| 474 | fw_type = AMD_FW_GMI3_PHY; |
| 475 | subprog = 0; |
| 476 | } else if (strcmp(fw_name, "PSP_MPDMAPMFW_FILE") == 0) { |
| 477 | fw_type = AMD_FW_MPDMA_PM; |
| 478 | subprog = 0; |
| 479 | } else if (strcmp(fw_name, "PSP_TOKEN_UNLOCK_FILE") == 0) { |
| 480 | fw_type = AMD_TOKEN_UNLOCK; |
| 481 | subprog = 0; |
| 482 | } else if (strcmp(fw_name, "SEV_DATA_FILE") == 0) { |
| 483 | fw_type = AMD_SEV_DATA; |
| 484 | subprog = 0; |
| 485 | } else if (strcmp(fw_name, "SEV_CODE_FILE") == 0) { |
| 486 | fw_type = AMD_SEV_CODE; |
| 487 | subprog = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 488 | } else { |
| 489 | fw_type = AMD_FW_INVALID; |
| 490 | /* TODO: Add more */ |
| 491 | } |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 492 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 493 | /* Search and fill the filename */ |
| 494 | psp_tableptr = &amd_psp_fw_table[0]; |
| 495 | if (fw_type != AMD_FW_SKIP && fw_type != AMD_FW_INVALID) { |
| 496 | while (psp_tableptr->type != AMD_FW_INVALID) { |
| 497 | /* instance are not used in PSP table */ |
Zheng Bao | 5ca1343 | 2022-10-16 20:18:40 +0800 | [diff] [blame] | 498 | if (psp_tableptr->type == fw_type && psp_tableptr->subprog == subprog |
| 499 | && psp_tableptr->inst == instance) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 500 | psp_tableptr->filename = filename; |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 501 | SET_LEVEL(psp_tableptr, level_to_set, PSP, |
| 502 | cb_config->recovery_ab); |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 503 | psp_tableptr->hash_tbl_id = hash_tbl_id; |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 504 | psp_tableptr->fwid_type = fwid_type; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | psp_tableptr++; |
| 508 | } |
| 509 | } |
| 510 | if (fw_type == AMD_FW_INVALID) |
| 511 | return 0; |
| 512 | else |
| 513 | return 1; |
| 514 | } |
Zheng Bao | 1a9e543 | 2022-02-17 17:48:27 +0800 | [diff] [blame] | 515 | #define PMUI_STR_BASE "PSP_PMUI_FILE" |
| 516 | #define PMUD_STR_BASE "PSP_PMUD_FILE" |
| 517 | #define PMU_STR_BASE_LEN strlen(PMUI_STR_BASE) |
| 518 | #define PMU_STR_SUB_INDEX strlen(PMUI_STR_BASE"_SUB") |
| 519 | #define PMU_STR_INS_INDEX strlen(PMUI_STR_BASE"_SUBx_INS") |
Zheng Bao | fdb0294 | 2022-02-22 09:47:59 +0800 | [diff] [blame] | 520 | #define PMU_STR_ALL_LEN strlen(PMUI_STR_BASE"_SUBx_INSx") |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 521 | |
| 522 | 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] | 523 | char level_to_set, amd_cb_config *cb_config) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 524 | { |
| 525 | amd_bios_type fw_type = AMD_BIOS_INVALID; |
| 526 | amd_bios_entry *bhd_tableptr; |
Felix Held | ea3417b | 2020-11-20 20:08:42 +0100 | [diff] [blame] | 527 | uint8_t subprog = 0; |
| 528 | uint8_t instance = 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 529 | |
| 530 | (void) (cb_config); /* Remove warning and reserved for future. */ |
| 531 | |
Zheng Bao | 1a9e543 | 2022-02-17 17:48:27 +0800 | [diff] [blame] | 532 | if (strncmp(fw_name, PMUI_STR_BASE, PMU_STR_BASE_LEN) == 0) { |
Zheng Bao | fdb0294 | 2022-02-22 09:47:59 +0800 | [diff] [blame] | 533 | assert(strlen(fw_name) == PMU_STR_ALL_LEN); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 534 | fw_type = AMD_BIOS_PMUI; |
Felix Held | 3dfb485 | 2022-09-28 18:00:39 +0200 | [diff] [blame] | 535 | subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16); |
| 536 | instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16); |
Zheng Bao | 1a9e543 | 2022-02-17 17:48:27 +0800 | [diff] [blame] | 537 | } else if (strncmp(fw_name, PMUD_STR_BASE, PMU_STR_BASE_LEN) == 0) { |
Zheng Bao | fdb0294 | 2022-02-22 09:47:59 +0800 | [diff] [blame] | 538 | assert(strlen(fw_name) == PMU_STR_ALL_LEN); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 539 | fw_type = AMD_BIOS_PMUD; |
Felix Held | 3dfb485 | 2022-09-28 18:00:39 +0200 | [diff] [blame] | 540 | subprog = strtol(&fw_name[PMU_STR_SUB_INDEX], NULL, 16); |
| 541 | instance = strtol(&fw_name[PMU_STR_INS_INDEX], NULL, 16); |
Zheng Bao | bf29a0d | 2020-12-03 23:00:48 +0800 | [diff] [blame] | 542 | } else if (strcmp(fw_name, "RTM_PUBKEY_FILE") == 0) { |
| 543 | fw_type = AMD_BIOS_RTM_PUBKEY; |
| 544 | subprog = 0; |
| 545 | instance = 0; |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 546 | } else if (strcmp(fw_name, "PSP_MP2CFG_FILE") == 0) { |
Zheng Bao | ba3af5e | 2021-11-04 18:56:47 +0800 | [diff] [blame] | 547 | if (cb_config->load_mp2_fw) { |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 548 | fw_type = AMD_BIOS_MP2_CFG; |
| 549 | subprog = 0; |
| 550 | } else { |
Martin Roth | a8e31ca | 2021-02-13 21:42:46 -0700 | [diff] [blame] | 551 | fw_type = AMD_BIOS_SKIP; |
Zheng Bao | 5014373 | 2020-11-14 21:54:06 +0800 | [diff] [blame] | 552 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 553 | } else { |
| 554 | fw_type = AMD_BIOS_INVALID; |
| 555 | } |
| 556 | |
| 557 | bhd_tableptr = amd_bios_table; |
| 558 | |
| 559 | if (fw_type != AMD_BIOS_INVALID && fw_type != AMD_BIOS_SKIP) { |
| 560 | while (bhd_tableptr->type != AMD_BIOS_INVALID) { |
| 561 | if (bhd_tableptr->type == fw_type && |
| 562 | bhd_tableptr->subpr == subprog && |
| 563 | bhd_tableptr->inst == instance) { |
| 564 | bhd_tableptr->filename = filename; |
Zheng Bao | 990d154 | 2021-09-17 13:24:54 +0800 | [diff] [blame] | 565 | SET_LEVEL(bhd_tableptr, level_to_set, BDT, |
| 566 | cb_config->recovery_ab); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | bhd_tableptr++; |
| 570 | } |
| 571 | } |
| 572 | if (fw_type == AMD_BIOS_INVALID) |
| 573 | return 0; |
| 574 | else |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | #define MAX_LINE_SIZE 1024 |
| 579 | |
| 580 | int get_input_file_line(FILE *f, char line[], int line_buf_size) |
| 581 | { |
| 582 | if (fgets(line, line_buf_size, f) == NULL) |
| 583 | return LINE_EOF; |
| 584 | |
| 585 | /* If the file contains a line that is too long, then it's best |
| 586 | * to let the user know right away rather than passing back a |
| 587 | * truncated result that will lead to problems later on. |
| 588 | */ |
| 589 | line[strlen(line) - 1] = '\0'; |
| 590 | |
| 591 | if (strlen(line) == ((size_t) (line_buf_size - 1))) { |
Zheng Bao | 77a2c67 | 2020-10-01 17:05:43 +0800 | [diff] [blame] | 592 | 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] | 593 | MAX_LINE_SIZE); |
| 594 | exit(1); |
| 595 | } |
| 596 | |
| 597 | return OK; |
| 598 | } |
| 599 | |
Zheng Bao | 4df5af8 | 2022-03-01 17:18:00 +0800 | [diff] [blame] | 600 | static int is_valid_entry(char *oneline, regmatch_t match[N_MATCHES]) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 601 | { |
Zheng Bao | 4df5af8 | 2022-03-01 17:18:00 +0800 | [diff] [blame] | 602 | int retval, index; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 603 | |
Zheng Bao | 4df5af8 | 2022-03-01 17:18:00 +0800 | [diff] [blame] | 604 | for (index = 0; index < N_MATCHES; index++) { |
| 605 | match[index].rm_so = -1; |
| 606 | match[index].rm_eo = -1; |
| 607 | } |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 608 | if (regexec(&entries_line_expr, oneline, N_MATCHES, match, 0) == 0) { |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 609 | /* match[1]: FW type |
| 610 | match[2]: FW filename |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 611 | match[4]: Optional directory level to be dropped |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 612 | match[6]: Optional hash table ID to put the hash for the entry |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 613 | */ |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 614 | oneline[match[FW_TYPE].rm_eo] = '\0'; |
| 615 | oneline[match[FW_FILE].rm_eo] = '\0'; |
| 616 | oneline[match[OPT_LEVEL].rm_eo] = '\0'; |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 617 | oneline[match[OPT_HASH_TABLE_ID].rm_eo] = '\0'; |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 618 | oneline[match[OPT_FWID_TYPE].rm_eo] = '\0'; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 619 | retval = 1; |
| 620 | } else { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 621 | retval = 0; |
Zheng Bao | b1fb8ce | 2021-09-13 18:00:09 +0800 | [diff] [blame] | 622 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 623 | |
| 624 | return retval; |
| 625 | } |
| 626 | |
| 627 | static int skip_comment_blank_line(char *oneline) |
| 628 | { |
| 629 | int retval; |
| 630 | |
| 631 | if (regexec(&blank_or_comment_expr, oneline, 0, NULL, 0) == 0) { |
| 632 | /* skip comment and blank */ |
| 633 | retval = 1; |
| 634 | } else { |
| 635 | /* no match */ |
| 636 | retval = 0; |
| 637 | } |
| 638 | |
| 639 | return retval; |
| 640 | } |
| 641 | |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 642 | static char get_level_from_config(char *line, regoff_t level_index, amd_cb_config *cb_config) |
Zheng Bao | 52a1898 | 2022-03-01 17:22:52 +0800 | [diff] [blame] | 643 | { |
| 644 | char lvl = 'x'; |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 645 | /* If the optional level field is present, extract the level char. */ |
Zheng Bao | 52a1898 | 2022-03-01 17:22:52 +0800 | [diff] [blame] | 646 | if (level_index != -1) { |
| 647 | if (cb_config->recovery_ab == 0) |
| 648 | lvl = line[level_index + 1]; |
| 649 | else if (strlen(&line[level_index]) >= 3) |
| 650 | lvl = line[level_index + 2]; |
| 651 | } |
| 652 | |
| 653 | assert(lvl == 'x' || lvl == 'X' || |
| 654 | lvl == 'b' || lvl == 'B' || |
| 655 | lvl == '1' || lvl == '2'); |
| 656 | |
| 657 | return lvl; |
| 658 | } |
| 659 | |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 660 | static uint8_t get_hash_tbl_id(char *line, regoff_t hash_tbl_index) |
| 661 | { |
| 662 | uint8_t tbl = 0; |
| 663 | /* If the optional hash table field is present, extract the table id char. */ |
| 664 | if (hash_tbl_index != -1) |
| 665 | tbl = (uint8_t)atoi(&line[hash_tbl_index + 1]); |
| 666 | |
| 667 | assert(tbl < MAX_NUM_HASH_TABLES); |
| 668 | return tbl; |
| 669 | } |
| 670 | |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 671 | static fwid_type_t get_fwid_type(char *line, regoff_t fwid_type_index) |
| 672 | { |
| 673 | if (fwid_type_index != -1 && !strncmp(&line[fwid_type_index], "UUID", strlen("UUID"))) |
| 674 | return FWID_TYPE_UUID; |
| 675 | |
| 676 | return FWID_TYPE_FWID; |
| 677 | } |
| 678 | |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 679 | static uint8_t process_one_line(char *oneline, regmatch_t *match, char *dir, |
Zheng Bao | 994ff52 | 2023-03-09 11:43:55 +0800 | [diff] [blame] | 680 | amd_cb_config *cb_config) |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 681 | { |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 682 | char *path_filename, *fn = &(oneline[match[FW_FILE].rm_so]); |
| 683 | char *fw_type_str = &(oneline[match[FW_TYPE].rm_so]); |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 684 | regoff_t ch_lvl_index = match[OPT_LEVEL].rm_so == match[OPT_LEVEL].rm_eo ? |
| 685 | -1 : match[OPT_LEVEL].rm_so; |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 686 | regoff_t ch_hash_tbl_index = |
| 687 | match[OPT_HASH_TABLE_ID].rm_so == match[OPT_HASH_TABLE_ID].rm_eo ? |
| 688 | -1 : match[OPT_HASH_TABLE_ID].rm_so; |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 689 | regoff_t ch_fwid_type_index = match[OPT_FWID_TYPE].rm_so == match[OPT_FWID_TYPE].rm_eo ? |
| 690 | -1 : match[OPT_FWID_TYPE].rm_so; |
Karthikeyan Ramasubramanian | d7a5d9e | 2023-05-03 13:34:41 -0600 | [diff] [blame] | 691 | char ch_lvl = get_level_from_config(oneline, ch_lvl_index, cb_config); |
| 692 | uint8_t ch_hash_tbl = get_hash_tbl_id(oneline, ch_hash_tbl_index); |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 693 | fwid_type_t ch_fwid_type = get_fwid_type(oneline, ch_fwid_type_index); |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 694 | |
| 695 | path_filename = malloc(MAX_LINE_SIZE * 2 + 2); |
Martin Roth | 6bb6ed9 | 2023-03-08 14:58:51 -0700 | [diff] [blame] | 696 | if (strchr(fn, '/')) |
| 697 | snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s", |
| 698 | MAX_LINE_SIZE, fn); |
| 699 | else |
| 700 | snprintf(path_filename, MAX_LINE_SIZE * 2 + 2, "%.*s/%.*s", |
| 701 | MAX_LINE_SIZE, dir, MAX_LINE_SIZE, fn); |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 702 | |
Karthikeyan Ramasubramanian | 24b5227 | 2023-07-13 11:11:04 -0600 | [diff] [blame] | 703 | if (find_register_fw_filename_psp_dir(fw_type_str, path_filename, |
| 704 | ch_lvl, ch_hash_tbl, ch_fwid_type, cb_config) == 0) { |
| 705 | if (find_register_fw_filename_bios_dir(fw_type_str, path_filename, |
| 706 | ch_lvl, cb_config) == 0) { |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 707 | fprintf(stderr, "Module's name \"%s\" is not valid\n", fw_type_str); |
| 708 | return 0; /* Stop parsing. */ |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 709 | } |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 710 | } |
| 711 | return 1; |
| 712 | } |
| 713 | |
Zheng Bao | 010cc99 | 2023-01-25 22:58:49 +0800 | [diff] [blame] | 714 | static bool needs_ish(enum platform platform_type) |
| 715 | { |
| 716 | if (platform_type == PLATFORM_MENDOCINO || platform_type == PLATFORM_PHOENIX || platform_type == PLATFORM_GLINDA) |
| 717 | return true; |
| 718 | else |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | static bool is_second_gen(enum platform platform_type) |
| 723 | { |
| 724 | switch (platform_type) { |
| 725 | case PLATFORM_CARRIZO: |
| 726 | case PLATFORM_STONEYRIDGE: |
| 727 | case PLATFORM_RAVEN: |
| 728 | case PLATFORM_PICASSO: |
| 729 | return false; |
| 730 | case PLATFORM_RENOIR: |
| 731 | case PLATFORM_LUCIENNE: |
| 732 | case PLATFORM_CEZANNE: |
| 733 | case PLATFORM_MENDOCINO: |
| 734 | case PLATFORM_PHOENIX: |
| 735 | case PLATFORM_GLINDA: |
| 736 | return true; |
| 737 | case PLATFORM_UNKNOWN: |
| 738 | default: |
| 739 | fprintf(stderr, "Error: Invalid SOC name.\n\n"); |
| 740 | return false; |
| 741 | } |
| 742 | } |
| 743 | |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 744 | #define FW_LOCATION "FIRMWARE_LOCATION" |
| 745 | #define SOC_NAME "SOC_NAME" |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 746 | /* |
| 747 | return value: |
| 748 | 0: The config file can not be parsed correctly. |
| 749 | 1: The config file can be parsed correctly. |
| 750 | */ |
Zheng Bao | 994ff52 | 2023-03-09 11:43:55 +0800 | [diff] [blame] | 751 | uint8_t process_config(FILE *config, amd_cb_config *cb_config) |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 752 | { |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 753 | char oneline[MAX_LINE_SIZE]; |
Zheng Bao | 4df5af8 | 2022-03-01 17:18:00 +0800 | [diff] [blame] | 754 | regmatch_t match[N_MATCHES]; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 755 | char dir[MAX_LINE_SIZE] = {'\0'}; |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 756 | uint32_t dir_len; |
Zheng Bao | 4df5af8 | 2022-03-01 17:18:00 +0800 | [diff] [blame] | 757 | int index; |
| 758 | |
| 759 | for (index = 0; index < N_MATCHES; index++) { |
| 760 | match[index].rm_so = -1; |
| 761 | match[index].rm_eo = -1; |
| 762 | } |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 763 | |
| 764 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, |
| 765 | blank_or_comment_regex, &blank_or_comment_expr); |
| 766 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, |
| 767 | entries_line_regex, &entries_line_expr); |
| 768 | |
| 769 | /* Get a line */ |
Zheng Bao | 3384e4a | 2020-10-06 12:03:11 +0800 | [diff] [blame] | 770 | /* Get FIRMWARE_LOCATION in the first loop */ |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 771 | while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) { |
| 772 | /* get a line */ |
| 773 | if (skip_comment_blank_line(oneline)) |
| 774 | continue; |
| 775 | if (is_valid_entry(oneline, match)) { |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 776 | if (strcmp(&(oneline[match[FW_TYPE].rm_so]), FW_LOCATION) == 0) { |
| 777 | dir_len = match[FW_FILE].rm_eo - match[FW_FILE].rm_so; |
Zheng Bao | dac4461 | 2021-05-27 11:11:34 +0800 | [diff] [blame] | 778 | assert(dir_len < MAX_LINE_SIZE); |
| 779 | snprintf(dir, MAX_LINE_SIZE, "%.*s", dir_len, |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 780 | &(oneline[match[FW_FILE].rm_so])); |
| 781 | } else if (strcmp(&(oneline[match[FW_TYPE].rm_so]), SOC_NAME) == 0) { |
| 782 | cb_config->soc_id = identify_platform( |
| 783 | &(oneline[match[FW_FILE].rm_so])); |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Zheng Bao | 010cc99 | 2023-01-25 22:58:49 +0800 | [diff] [blame] | 788 | cb_config->second_gen = is_second_gen(cb_config->soc_id); |
| 789 | |
| 790 | if (needs_ish(cb_config->soc_id)) |
| 791 | cb_config->need_ish = true; |
| 792 | |
| 793 | if (cb_config->need_ish) |
| 794 | cb_config->recovery_ab = true; |
| 795 | |
| 796 | if (cb_config->recovery_ab) |
| 797 | cb_config->multi_level = true; |
| 798 | |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 799 | if (dir[0] == '\0') { |
| 800 | fprintf(stderr, "No line with FIRMWARE_LOCATION\n"); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | fseek(config, 0, SEEK_SET); |
| 805 | /* Get a line */ |
| 806 | while (get_input_file_line(config, oneline, MAX_LINE_SIZE) == OK) { |
| 807 | /* get a line */ |
| 808 | if (skip_comment_blank_line(oneline)) |
| 809 | continue; |
| 810 | if (is_valid_entry(oneline, match)) { |
Karthikeyan Ramasubramanian | dc49893 | 2023-05-02 22:10:57 -0600 | [diff] [blame] | 811 | if (strcmp(&(oneline[match[FW_TYPE].rm_so]), FW_LOCATION) == 0 || |
| 812 | strcmp(&(oneline[match[FW_TYPE].rm_so]), SOC_NAME) == 0) { |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 813 | continue; |
| 814 | } else { |
Zheng Bao | 994ff52 | 2023-03-09 11:43:55 +0800 | [diff] [blame] | 815 | if (process_one_line(oneline, match, dir, |
Zheng Bao | 7db7642 | 2021-03-27 18:15:35 +0800 | [diff] [blame] | 816 | cb_config) == 0) |
| 817 | return 0; |
Zheng Bao | c5e28ab | 2020-10-28 11:38:09 +0800 | [diff] [blame] | 818 | } |
| 819 | } else { |
| 820 | fprintf(stderr, "AMDFWTOOL config file line can't be parsed \"%s\"\n", oneline); |
| 821 | return 0; |
| 822 | } |
| 823 | } |
| 824 | return 1; |
| 825 | } |