Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Elyes HAOUAS | 944da48 | 2021-02-01 21:30:13 +0100 | [diff] [blame] | 2 | |
Krishna P Bhat D | e3178a1 | 2022-04-21 16:40:06 +0530 | [diff] [blame] | 3 | #include <acpi/acpi.h> |
Krishna P Bhat D | e3178a1 | 2022-04-21 16:40:06 +0530 | [diff] [blame] | 4 | #include <bootstate.h> |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 5 | #include <cbfs.h> |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 6 | #include <commonlib/region.h> |
Elyes Haouas | def74aa | 2022-10-31 13:44:40 +0100 | [diff] [blame] | 7 | #include <console/console.h> |
| 8 | #include <cpu/cpu.h> |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 9 | #include <fmap.h> |
Elyes Haouas | def74aa | 2022-10-31 13:44:40 +0100 | [diff] [blame] | 10 | #include <intelbasecode/debug_feature.h> |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 11 | #include <intelblocks/cse.h> |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 12 | #include <intelblocks/cse_layout.h> |
Subrata Banik | da527ec | 2022-11-24 15:42:35 +0530 | [diff] [blame] | 13 | #include <intelblocks/spi.h> |
Sridhar Siricilla | 87e36c4 | 2020-05-03 19:08:18 +0530 | [diff] [blame] | 14 | #include <security/vboot/misc.h> |
Elyes Haouas | def74aa | 2022-10-31 13:44:40 +0100 | [diff] [blame] | 15 | #include <security/vboot/vboot_common.h> |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 16 | #include <soc/intel/common/reset.h> |
Krishna P Bhat D | e3178a1 | 2022-04-21 16:40:06 +0530 | [diff] [blame] | 17 | #include <timestamp.h> |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 18 | |
| 19 | #define BPDT_HEADER_SZ sizeof(struct bpdt_header) |
| 20 | #define BPDT_ENTRY_SZ sizeof(struct bpdt_entry) |
| 21 | #define SUBPART_HEADER_SZ sizeof(struct subpart_hdr) |
| 22 | #define SUBPART_ENTRY_SZ sizeof(struct subpart_entry) |
| 23 | #define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 24 | |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 25 | /* Converts bp index to boot partition string */ |
| 26 | #define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO") |
| 27 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 28 | /* CSE RW boot partition signature */ |
| 29 | #define CSE_RW_SIGNATURE 0x000055aa |
| 30 | |
| 31 | /* CSE RW boot partition signature size */ |
| 32 | #define CSE_RW_SIGN_SIZE sizeof(uint32_t) |
| 33 | |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 34 | /* |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 35 | * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 36 | * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT. |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 37 | * CSE Lite SKU Image Layout: |
Angel Pons | 2e8e060 | 2022-08-13 19:50:28 +0200 | [diff] [blame] | 38 | * +------------+ +----+------+----+ +-----+------+-----+ |
| 39 | * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 | |
| 40 | * +------------+ +----+------+----+ +-----+------+-----+ |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 41 | */ |
| 42 | #define CSE_MAX_BOOT_PARTITIONS 3 |
| 43 | |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 44 | /* CSE Lite SKU's valid bootable partition identifiers */ |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 45 | enum boot_partition_id { |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 46 | /* RO(BP1) contains recovery/minimal boot firmware */ |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 47 | RO = 0, |
| 48 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 49 | /* RW(BP2) contains fully functional CSE firmware */ |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 50 | RW = 1 |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * Boot partition status. |
| 55 | * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd. |
| 56 | */ |
| 57 | enum bp_status { |
| 58 | /* This value is returned when a partition has no errors */ |
| 59 | BP_STATUS_SUCCESS = 0, |
| 60 | |
| 61 | /* |
| 62 | * This value is returned when a partition should be present based on layout, but it is |
| 63 | * not valid. |
| 64 | */ |
| 65 | BP_STATUS_GENERAL_FAILURE = 1, |
| 66 | |
| 67 | /* This value is returned when a partition is not present per initial image layout */ |
| 68 | BP_STATUS_PARTITION_NOT_PRESENT = 2, |
| 69 | |
Sridhar Siricilla | 2f6d555 | 2020-04-19 23:39:02 +0530 | [diff] [blame] | 70 | /* |
| 71 | * This value is returned when unexpected issues are detected in CSE Data area |
| 72 | * and CSE TCB-SVN downgrade scenario. |
| 73 | */ |
| 74 | BP_STATUS_DATA_FAILURE = 3, |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Boot Partition Info Flags |
| 79 | * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd. |
| 80 | */ |
| 81 | enum bp_info_flags { |
| 82 | |
| 83 | /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */ |
| 84 | BP_INFO_REDUNDANCY_EN = 1 << 0, |
| 85 | |
| 86 | /* It indicates RO(BP1) supports Minimal Recovery Mode */ |
| 87 | BP_INFO_MIN_RECOV_MODE_EN = 1 << 1, |
| 88 | |
| 89 | /* |
| 90 | * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled. |
| 91 | * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled. |
| 92 | */ |
| 93 | BP_INFO_READ_ONLY_CFG = 1 << 2, |
| 94 | }; |
| 95 | |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 96 | /* CSE boot partition entry info */ |
| 97 | struct cse_bp_entry { |
| 98 | /* Boot partition version */ |
| 99 | struct fw_version fw_ver; |
| 100 | |
| 101 | /* Boot partition status */ |
| 102 | uint32_t status; |
| 103 | |
| 104 | /* Starting offset of the partition within CSE region */ |
| 105 | uint32_t start_offset; |
| 106 | |
| 107 | /* Ending offset of the partition within CSE region */ |
| 108 | uint32_t end_offset; |
| 109 | uint8_t reserved[12]; |
| 110 | } __packed; |
| 111 | |
| 112 | /* CSE boot partition info */ |
| 113 | struct cse_bp_info { |
| 114 | /* Number of boot partitions */ |
| 115 | uint8_t total_number_of_bp; |
| 116 | |
| 117 | /* Current boot partition */ |
| 118 | uint8_t current_bp; |
| 119 | |
| 120 | /* Next boot partition */ |
| 121 | uint8_t next_bp; |
| 122 | |
| 123 | /* Boot Partition Info Flags */ |
| 124 | uint8_t flags; |
| 125 | |
| 126 | /* Boot Partition Entry Info */ |
| 127 | struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS]; |
| 128 | } __packed; |
| 129 | |
| 130 | struct get_bp_info_rsp { |
| 131 | struct mkhi_hdr hdr; |
| 132 | struct cse_bp_info bp_info; |
| 133 | } __packed; |
| 134 | |
Sridhar Siricilla | 52479c7 | 2022-03-30 09:25:49 +0530 | [diff] [blame] | 135 | static const char * const cse_regions[] = {"RO", "RW"}; |
Bora Guvendik | f33c9bf | 2021-11-05 23:09:25 -0700 | [diff] [blame] | 136 | |
Subrata Banik | da527ec | 2022-11-24 15:42:35 +0530 | [diff] [blame] | 137 | void cse_log_ro_write_protection_info(bool mfg_mode) |
| 138 | { |
| 139 | bool cse_ro_wp_en = is_spi_wp_cse_ro_en(); |
| 140 | |
| 141 | printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n", |
| 142 | cse_ro_wp_en ? "YES" : "NO"); |
| 143 | |
| 144 | if (cse_ro_wp_en) { |
| 145 | uint32_t base, limit; |
| 146 | spi_get_wp_cse_ro_range(&base, &limit); |
| 147 | printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n", |
| 148 | base, limit); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * If manufacturing mode is disabled, but CSE RO is not write protected, |
| 153 | * log error. |
| 154 | */ |
| 155 | if (!mfg_mode && !cse_ro_wp_en) |
| 156 | printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n"); |
| 157 | } |
| 158 | |
Bora Guvendik | f33c9bf | 2021-11-05 23:09:25 -0700 | [diff] [blame] | 159 | bool cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp) |
| 160 | { |
| 161 | struct cse_boot_perf_req { |
| 162 | struct mkhi_hdr hdr; |
| 163 | uint32_t reserved; |
| 164 | } __packed; |
| 165 | |
| 166 | struct cse_boot_perf_req req = { |
| 167 | .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON, |
| 168 | .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA, |
| 169 | .reserved = 0, |
| 170 | }; |
| 171 | |
| 172 | size_t resp_size = sizeof(struct cse_boot_perf_rsp); |
| 173 | |
Sridhar Siricilla | 6836da2 | 2022-02-23 23:36:45 +0530 | [diff] [blame] | 174 | if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size, |
Bora Guvendik | f33c9bf | 2021-11-05 23:09:25 -0700 | [diff] [blame] | 175 | HECI_MKHI_ADDR)) { |
| 176 | printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n"); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | if (boot_perf_rsp->hdr.result) { |
| 181 | printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n", |
| 182 | boot_perf_rsp->hdr.result); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 190 | static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info) |
| 191 | { |
| 192 | return cse_bp_info->current_bp; |
| 193 | } |
| 194 | |
| 195 | static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp, |
| 196 | const struct cse_bp_info *cse_bp_info) |
| 197 | { |
| 198 | return &cse_bp_info->bp_entries[bp]; |
| 199 | } |
| 200 | |
| 201 | static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info) |
| 202 | { |
| 203 | const struct cse_bp_entry *cse_bp; |
| 204 | |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 205 | printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n", |
| 206 | cse_bp_info->total_number_of_bp); |
| 207 | printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n", |
| 208 | GET_BP_STR(cse_bp_info->current_bp)); |
| 209 | printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp)); |
| 210 | printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 211 | |
| 212 | /* Log version info of RO & RW partitions */ |
| 213 | cse_bp = cse_get_bp_entry(RO, cse_bp_info); |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 214 | printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n", |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 215 | GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor, |
| 216 | cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build, |
| 217 | cse_bp->status, cse_bp->start_offset, |
| 218 | cse_bp->end_offset); |
| 219 | |
| 220 | cse_bp = cse_get_bp_entry(RW, cse_bp_info); |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 221 | printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n", |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 222 | GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor, |
| 223 | cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build, |
| 224 | cse_bp->status, cse_bp->start_offset, |
| 225 | cse_bp->end_offset); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and |
| 230 | * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands. |
| 231 | * It allows execution of the Boot Partition commands in below scenarios: |
| 232 | * - When CSE boots from RW partition (COM: Normal and CWS: Normal) |
| 233 | * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal) |
| 234 | * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 235 | * The prerequisite check should be handled in cse_get_bp_info() and |
| 236 | * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these |
| 237 | * cmd handler calls. |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 238 | */ |
| 239 | static bool cse_is_bp_cmd_info_possible(void) |
| 240 | { |
| 241 | if (cse_is_hfs1_cws_normal()) { |
| 242 | if (cse_is_hfs1_com_normal()) |
| 243 | return true; |
| 244 | if (cse_is_hfs1_com_secover_mei_msg()) |
| 245 | return true; |
| 246 | if (cse_is_hfs1_com_soft_temp_disable()) |
| 247 | return true; |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 252 | static enum cb_err cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 253 | { |
| 254 | struct get_bp_info_req { |
| 255 | struct mkhi_hdr hdr; |
| 256 | uint8_t reserved[4]; |
| 257 | } __packed; |
| 258 | |
| 259 | struct get_bp_info_req info_req = { |
| 260 | .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON, |
| 261 | .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO, |
| 262 | .reserved = {0}, |
| 263 | }; |
| 264 | |
| 265 | if (!cse_is_bp_cmd_info_possible()) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 266 | printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n"); |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 267 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | size_t resp_size = sizeof(struct get_bp_info_rsp); |
| 271 | |
Sridhar Siricilla | 6836da2 | 2022-02-23 23:36:45 +0530 | [diff] [blame] | 272 | if (heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size, |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 273 | HECI_MKHI_ADDR)) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 274 | printk(BIOS_ERR, "cse_lite: Could not get partition info\n"); |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 275 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (bp_info_rsp->hdr.result) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 279 | printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n", |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 280 | bp_info_rsp->hdr.result); |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 281 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | cse_print_boot_partition_info(&bp_info_rsp->bp_info); |
| 285 | |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 286 | return CB_SUCCESS; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 287 | } |
| 288 | /* |
| 289 | * It sends HECI command to notify CSE about its next boot partition. When coreboot wants |
| 290 | * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used. |
| 291 | * The CSE's valid bootable partitions are BP1(RO) and BP2(RW). |
| 292 | * This function must be used before EOP. |
| 293 | * Returns false on failure and true on success. |
| 294 | */ |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 295 | static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 296 | { |
| 297 | struct set_boot_partition_info_req { |
| 298 | struct mkhi_hdr hdr; |
| 299 | uint8_t next_bp; |
| 300 | uint8_t reserved[3]; |
| 301 | } __packed; |
| 302 | |
| 303 | struct set_boot_partition_info_req switch_req = { |
| 304 | .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON, |
| 305 | .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO, |
| 306 | .next_bp = bp, |
| 307 | .reserved = {0}, |
| 308 | }; |
| 309 | |
| 310 | if (bp != RO && bp != RW) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 311 | printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp); |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 312 | return CB_ERR_ARG; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 313 | } |
| 314 | |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 315 | printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp)); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 316 | |
| 317 | if (!cse_is_bp_cmd_info_possible()) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 318 | printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n"); |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 319 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | struct mkhi_hdr switch_resp; |
| 323 | size_t sw_resp_sz = sizeof(struct mkhi_hdr); |
| 324 | |
Sridhar Siricilla | 6836da2 | 2022-02-23 23:36:45 +0530 | [diff] [blame] | 325 | if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz, |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 326 | HECI_MKHI_ADDR)) |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 327 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 328 | |
| 329 | if (switch_resp.result) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 330 | printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n", |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 331 | switch_resp.result); |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 332 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 333 | } |
| 334 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 335 | return CB_SUCCESS; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 336 | } |
| 337 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 338 | static enum cb_err cse_data_clear_request(const struct cse_bp_info *cse_bp_info) |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 339 | { |
| 340 | struct data_clr_request { |
| 341 | struct mkhi_hdr hdr; |
| 342 | uint8_t reserved[4]; |
| 343 | } __packed; |
| 344 | |
| 345 | struct data_clr_request data_clr_rq = { |
| 346 | .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON, |
| 347 | .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR, |
| 348 | .reserved = {0}, |
| 349 | }; |
| 350 | |
| 351 | if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() || |
| 352 | cse_get_current_bp(cse_bp_info) != RO) { |
| 353 | printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n"); |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 354 | return CB_ERR; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n"); |
| 358 | |
| 359 | struct mkhi_hdr data_clr_rsp; |
| 360 | size_t data_clr_rsp_sz = sizeof(data_clr_rsp); |
| 361 | |
Sridhar Siricilla | 6836da2 | 2022-02-23 23:36:45 +0530 | [diff] [blame] | 362 | if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp, |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 363 | &data_clr_rsp_sz, HECI_MKHI_ADDR)) { |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 364 | return CB_ERR; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (data_clr_rsp.result) { |
| 368 | printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n", |
| 369 | data_clr_rsp.result); |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 370 | return CB_ERR; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 371 | } |
| 372 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 373 | return CB_SUCCESS; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 374 | } |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 375 | |
Karthikeyan Ramasubramanian | f9cc637 | 2020-08-04 16:38:58 -0600 | [diff] [blame] | 376 | __weak void cse_board_reset(void) |
| 377 | { |
| 378 | /* Default weak implementation, does nothing. */ |
| 379 | } |
| 380 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 381 | /* Set the CSE's next boot partition and issues system reset */ |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 382 | static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 383 | { |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 384 | if (cse_set_next_boot_partition(bp) != CB_SUCCESS) |
| 385 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 386 | |
Karthikeyan Ramasubramanian | f9cc637 | 2020-08-04 16:38:58 -0600 | [diff] [blame] | 387 | /* Allow the board to perform a reset for CSE RO<->RW jump */ |
| 388 | cse_board_reset(); |
| 389 | |
| 390 | /* If board does not perform the reset, then perform global_reset */ |
Furquan Shaikh | b13bd1e | 2020-09-21 22:44:27 +0000 | [diff] [blame] | 391 | do_global_reset(); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 392 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 393 | die("cse_lite: Failed to reset the system\n"); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 394 | |
| 395 | /* Control never reaches here */ |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 396 | return CB_ERR; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 397 | } |
| 398 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 399 | static enum cb_err cse_boot_to_rw(const struct cse_bp_info *cse_bp_info) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 400 | { |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 401 | if (cse_get_current_bp(cse_bp_info) == RW) |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 402 | return CB_SUCCESS; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 403 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 404 | return cse_set_and_boot_from_next_bp(RW); |
| 405 | } |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 406 | |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 407 | /* Check if CSE RW data partition is valid or not */ |
| 408 | static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info) |
| 409 | { |
| 410 | const struct cse_bp_entry *rw_bp; |
| 411 | |
| 412 | rw_bp = cse_get_bp_entry(RW, cse_bp_info); |
| 413 | return rw_bp->status != BP_STATUS_DATA_FAILURE; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE |
| 418 | * otherwise false if any operation fails. |
| 419 | */ |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 420 | static enum cb_err cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info) |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 421 | { |
| 422 | /* |
| 423 | * If RW partition status indicates BP_STATUS_DATA_FAILURE, |
| 424 | * - Send DATA CLEAR HECI command to CSE |
| 425 | * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition |
| 426 | * - Issue GLOBAL RESET HECI command. |
| 427 | */ |
| 428 | if (cse_is_rw_dp_valid(cse_bp_info)) |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 429 | return CB_SUCCESS; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 430 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 431 | if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) |
| 432 | return CB_ERR; |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 433 | |
| 434 | return cse_boot_to_rw(cse_bp_info); |
| 435 | } |
| 436 | |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 437 | static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp, |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 438 | const struct cse_bp_info *bp_info) |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 439 | { |
| 440 | const struct cse_bp_entry *cse_bp; |
| 441 | |
| 442 | cse_bp = cse_get_bp_entry(bp, bp_info); |
| 443 | return &cse_bp->fw_ver; |
| 444 | } |
| 445 | |
| 446 | static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info) |
| 447 | { |
| 448 | return cse_get_bp_entry_version(RW, cse_bp_info); |
| 449 | } |
| 450 | |
| 451 | static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info, |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 452 | enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset) |
V Sowmya | f990552 | 2020-11-12 20:19:04 +0530 | [diff] [blame] | 453 | { |
| 454 | const struct cse_bp_entry *cse_bp; |
| 455 | |
| 456 | cse_bp = cse_get_bp_entry(bp, cse_bp_info); |
| 457 | |
| 458 | if (start_offset) |
| 459 | *start_offset = cse_bp->start_offset; |
| 460 | |
| 461 | if (end_offset) |
| 462 | *end_offset = cse_bp->end_offset; |
| 463 | |
| 464 | } |
| 465 | |
| 466 | static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info) |
| 467 | { |
| 468 | const struct cse_bp_entry *rw_bp; |
| 469 | |
| 470 | rw_bp = cse_get_bp_entry(RW, cse_bp_info); |
| 471 | |
| 472 | if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT || |
| 473 | rw_bp->status == BP_STATUS_GENERAL_FAILURE) { |
| 474 | printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status); |
| 475 | return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 480 | static bool cse_boot_to_ro(const struct cse_bp_info *cse_bp_info) |
| 481 | { |
| 482 | if (cse_get_current_bp(cse_bp_info) == RO) |
| 483 | return true; |
| 484 | |
| 485 | return cse_set_and_boot_from_next_bp(RO); |
| 486 | } |
| 487 | |
| 488 | static bool cse_get_rw_rdev(struct region_device *rdev) |
| 489 | { |
| 490 | if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) { |
| 491 | printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n", |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 492 | CONFIG_SOC_INTEL_CSE_FMAP_NAME); |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
| 496 | return true; |
| 497 | } |
| 498 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 499 | static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev) |
| 500 | { |
| 501 | uint32_t cse_bp_sign; |
| 502 | |
| 503 | if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) { |
| 504 | printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n"); |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | return cse_bp_sign == CSE_RW_SIGNATURE; |
| 509 | } |
| 510 | |
| 511 | static bool cse_get_target_rdev(const struct cse_bp_info *cse_bp_info, |
| 512 | struct region_device *target_rdev) |
| 513 | { |
| 514 | struct region_device cse_region_rdev; |
| 515 | size_t size; |
| 516 | uint32_t start_offset; |
| 517 | uint32_t end_offset; |
| 518 | |
| 519 | if (!cse_get_rw_rdev(&cse_region_rdev)) |
| 520 | return false; |
| 521 | |
| 522 | cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset); |
| 523 | size = end_offset + 1 - start_offset; |
| 524 | |
| 525 | if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size)) |
| 526 | return false; |
| 527 | |
| 528 | printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n", |
Elyes Haouas | 9018dee | 2022-11-18 15:07:33 +0100 | [diff] [blame] | 529 | (uint32_t)start_offset, (uint32_t)size); |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 534 | static const char *cse_get_source_rdev_fmap(void) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 535 | { |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 536 | struct vb2_context *ctx = vboot_get_context(); |
| 537 | if (ctx == NULL) |
| 538 | return NULL; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 539 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 540 | if (vboot_is_firmware_slot_a(ctx)) |
| 541 | return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME; |
| 542 | |
| 543 | return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME; |
| 544 | } |
| 545 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 546 | /* |
Sridhar Siricilla | b9277ba | 2021-11-27 13:57:40 +0530 | [diff] [blame] | 547 | * Compare versions of CSE CBFS sub-component and CSE sub-component partition |
| 548 | * In case of CSE component comparison: |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 549 | * If ver_cmp_status = 0, no update is required |
| 550 | * If ver_cmp_status < 0, coreboot downgrades CSE RW region |
| 551 | * If ver_cmp_status > 0, coreboot upgrades CSE RW region |
| 552 | */ |
Sridhar Siricilla | b9277ba | 2021-11-27 13:57:40 +0530 | [diff] [blame] | 553 | static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 554 | { |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 555 | if (a->major != b->major) |
| 556 | return a->major - b->major; |
| 557 | else if (a->minor != b->minor) |
| 558 | return a->minor - b->minor; |
| 559 | else if (a->hotfix != b->hotfix) |
| 560 | return a->hotfix - b->hotfix; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 561 | else |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 562 | return a->build - b->build; |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */ |
| 566 | static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha, |
| 567 | const void *rw_blob, const size_t rw_blob_sz) |
| 568 | |
| 569 | { |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 570 | struct vb2_hash calculated; |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 571 | |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 572 | if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz, |
| 573 | VB2_HASH_SHA256, &calculated)) { |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 574 | printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n"); |
| 575 | return false; |
| 576 | } |
| 577 | |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 578 | if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) { |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 579 | printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with" |
| 580 | "the provided SHA in the metadata\n"); |
| 581 | return false; |
| 582 | } |
| 583 | printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the" |
| 584 | " provided hash in the metadata\n"); |
| 585 | return true; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static bool cse_erase_rw_region(const struct region_device *target_rdev) |
| 589 | { |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 590 | if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) { |
| 591 | printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n"); |
| 592 | return false; |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 597 | static bool cse_copy_rw(const struct region_device *target_rdev, const void *buf, |
| 598 | size_t offset, size_t size) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 599 | { |
| 600 | if (rdev_writeat(target_rdev, buf, offset, size) < 0) { |
| 601 | printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n"); |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | return true; |
| 606 | } |
| 607 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 608 | enum cse_update_status { |
| 609 | CSE_UPDATE_NOT_REQUIRED, |
| 610 | CSE_UPDATE_UPGRADE, |
| 611 | CSE_UPDATE_DOWNGRADE, |
| 612 | CSE_UPDATE_CORRUPTED, |
| 613 | CSE_UPDATE_METADATA_ERROR, |
| 614 | }; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 615 | |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 616 | static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field) |
| 617 | { |
| 618 | if ((*curr - start) >= size) { |
| 619 | printk(BIOS_ERR, "cse_lite: Version string read overflow!\n"); |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | *ver_field = skip_atoi(curr); |
| 624 | (*curr)++; |
| 625 | return true; |
| 626 | } |
Sridhar Siricilla | 2f6d555 | 2020-04-19 23:39:02 +0530 | [diff] [blame] | 627 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 628 | static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info, |
| 629 | struct region_device *target_rdev) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 630 | { |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 631 | int ret; |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 632 | struct fw_version cbfs_rw_version; |
| 633 | char *version_str, *ptr; |
| 634 | size_t size; |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 635 | |
| 636 | if (!cse_is_rw_bp_sign_valid(target_rdev)) |
| 637 | return CSE_UPDATE_CORRUPTED; |
| 638 | |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 639 | ptr = version_str = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size); |
| 640 | if (!version_str) { |
| 641 | printk(BIOS_ERR, "cse_lite: Failed to get %s\n", |
| 642 | CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME); |
| 643 | return CSE_UPDATE_METADATA_ERROR; |
| 644 | } |
| 645 | |
| 646 | if (!read_ver_field(version_str, &ptr, size, &cbfs_rw_version.major) || |
| 647 | !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.minor) || |
| 648 | !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.hotfix) || |
| 649 | !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.build)) { |
| 650 | cbfs_unmap(version_str); |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 651 | return CSE_UPDATE_METADATA_ERROR; |
| 652 | } |
| 653 | |
| 654 | printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n", |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 655 | cbfs_rw_version.major, |
| 656 | cbfs_rw_version.minor, |
| 657 | cbfs_rw_version.hotfix, |
| 658 | cbfs_rw_version.build); |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 659 | |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 660 | cbfs_unmap(version_str); |
| 661 | |
Sridhar Siricilla | b9277ba | 2021-11-27 13:57:40 +0530 | [diff] [blame] | 662 | ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info)); |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 663 | if (ret == 0) |
| 664 | return CSE_UPDATE_NOT_REQUIRED; |
| 665 | else if (ret < 0) |
| 666 | return CSE_UPDATE_DOWNGRADE; |
| 667 | else |
| 668 | return CSE_UPDATE_UPGRADE; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static bool cse_write_rw_region(const struct region_device *target_rdev, |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 672 | const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 673 | { |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 674 | /* Points to CSE CBFS RW image after boot partition signature */ |
| 675 | uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE; |
| 676 | |
| 677 | /* Size of CSE CBFS RW image without boot partition signature */ |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 678 | uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 679 | |
| 680 | /* Update except CSE RW signature */ |
| 681 | if (!cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE, |
| 682 | cse_cbfs_rw_wo_sign_sz)) |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 683 | return false; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 684 | |
| 685 | /* Update CSE RW signature to indicate update is complete */ |
| 686 | if (!cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE)) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 687 | return false; |
| 688 | |
| 689 | printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n"); |
| 690 | return true; |
| 691 | } |
| 692 | |
Sridhar Siricilla | 0aa1ac4 | 2022-03-09 20:35:32 +0530 | [diff] [blame] | 693 | static bool is_cse_fw_update_enabled(void) |
| 694 | { |
| 695 | if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE)) |
| 696 | return false; |
| 697 | |
| 698 | if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE)) |
| 699 | return !is_debug_cse_fw_update_disable(); |
| 700 | |
| 701 | return true; |
| 702 | } |
| 703 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 704 | static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info, |
| 705 | const void *cse_cbfs_rw, const size_t cse_blob_sz, |
| 706 | struct region_device *target_rdev) |
| 707 | { |
Sridhar Siricilla | abeb688 | 2020-12-07 15:55:10 +0530 | [diff] [blame] | 708 | if (region_device_sz(target_rdev) < cse_blob_sz) { |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 709 | printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx," |
| 710 | "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz); |
Sridhar Siricilla | abeb688 | 2020-12-07 15:55:10 +0530 | [diff] [blame] | 711 | return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR; |
| 712 | } |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 713 | |
| 714 | if (!cse_erase_rw_region(target_rdev)) |
| 715 | return CSE_LITE_SKU_FW_UPDATE_ERROR; |
| 716 | |
| 717 | if (!cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz)) |
| 718 | return CSE_LITE_SKU_FW_UPDATE_ERROR; |
| 719 | |
Tim Wawrzynczak | e380a43 | 2021-06-18 09:54:55 -0600 | [diff] [blame] | 720 | return CSE_NO_ERROR; |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 721 | } |
| 722 | |
Sridhar Siricilla | 2f6d555 | 2020-04-19 23:39:02 +0530 | [diff] [blame] | 723 | static bool cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info, |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 724 | enum cse_update_status status) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 725 | { |
| 726 | /* |
| 727 | * To set CSE's operation mode to HMRFPO mode: |
| 728 | * 1. Ensure CSE to boot from RO(BP1) |
| 729 | * 2. Send HMRFPO_ENABLE command to CSE |
| 730 | */ |
| 731 | if (!cse_boot_to_ro(cse_bp_info)) |
| 732 | return false; |
| 733 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 734 | if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) { |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 735 | if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) { |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 736 | printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n"); |
| 737 | return false; |
| 738 | } |
Sridhar Siricilla | 2f6d555 | 2020-04-19 23:39:02 +0530 | [diff] [blame] | 739 | } |
| 740 | |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 741 | return cse_hmrfpo_enable(); |
| 742 | } |
| 743 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 744 | static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info, |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 745 | enum cse_update_status status, |
| 746 | struct region_device *target_rdev) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 747 | { |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 748 | enum csme_failure_reason rv; |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 749 | uint8_t *cbfs_rw_hash; |
Krishna P Bhat D | 75a423e | 2022-04-20 15:50:06 +0530 | [diff] [blame] | 750 | void *cse_cbfs_rw = NULL; |
Julius Werner | 18881f99 | 2021-04-13 15:28:12 -0700 | [diff] [blame] | 751 | size_t size; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 752 | |
Julius Werner | 18881f99 | 2021-04-13 15:28:12 -0700 | [diff] [blame] | 753 | const char *area_name = cse_get_source_rdev_fmap(); |
| 754 | if (!area_name) |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 755 | return CSE_LITE_SKU_RW_BLOB_NOT_FOUND; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 756 | |
Krishna P Bhat D | 75a423e | 2022-04-20 15:50:06 +0530 | [diff] [blame] | 757 | if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) { |
| 758 | cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name, |
| 759 | CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size); |
| 760 | } else { |
| 761 | cse_cbfs_rw = cbfs_unverified_area_map(area_name, |
| 762 | CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size); |
| 763 | } |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 764 | if (!cse_cbfs_rw) { |
| 765 | printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n"); |
| 766 | return CSE_LITE_SKU_RW_BLOB_NOT_FOUND; |
| 767 | } |
| 768 | |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 769 | cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL); |
| 770 | if (!cbfs_rw_hash) { |
| 771 | printk(BIOS_ERR, "cse_lite: Failed to get %s\n", |
| 772 | CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME); |
| 773 | rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND; |
| 774 | goto error_exit; |
| 775 | } |
| 776 | |
Julius Werner | 18881f99 | 2021-04-13 15:28:12 -0700 | [diff] [blame] | 777 | if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) { |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 778 | rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH; |
| 779 | goto error_exit; |
| 780 | } |
| 781 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 782 | if (!cse_prep_for_rw_update(cse_bp_info, status)) { |
Tim Wawrzynczak | e380a43 | 2021-06-18 09:54:55 -0600 | [diff] [blame] | 783 | rv = CSE_COMMUNICATION_ERROR; |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 784 | goto error_exit; |
| 785 | } |
| 786 | |
Julius Werner | 18881f99 | 2021-04-13 15:28:12 -0700 | [diff] [blame] | 787 | rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev); |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 788 | |
| 789 | error_exit: |
Furquan Shaikh | d2da870 | 2021-10-07 00:08:59 -0700 | [diff] [blame] | 790 | cbfs_unmap(cbfs_rw_hash); |
Julius Werner | 18881f99 | 2021-04-13 15:28:12 -0700 | [diff] [blame] | 791 | cbfs_unmap(cse_cbfs_rw); |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 792 | return rv; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 793 | } |
| 794 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 795 | static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info) |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 796 | { |
| 797 | struct region_device target_rdev; |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 798 | enum cse_update_status status; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 799 | |
| 800 | if (!cse_get_target_rdev(cse_bp_info, &target_rdev)) { |
| 801 | printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n"); |
| 802 | return CSE_LITE_SKU_RW_ACCESS_ERROR; |
| 803 | } |
| 804 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 805 | status = cse_check_update_status(cse_bp_info, &target_rdev); |
| 806 | if (status == CSE_UPDATE_NOT_REQUIRED) |
| 807 | return CSE_NO_ERROR; |
| 808 | if (status == CSE_UPDATE_METADATA_ERROR) |
| 809 | return CSE_LITE_SKU_RW_METADATA_NOT_FOUND; |
Rizwan Qureshi | ec32109 | 2019-09-06 20:28:43 +0530 | [diff] [blame] | 810 | |
Furquan Shaikh | c45e0be | 2021-10-06 23:28:03 -0700 | [diff] [blame] | 811 | printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n"); |
| 812 | return cse_trigger_fw_update(cse_bp_info, status, &target_rdev); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 813 | } |
| 814 | |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 815 | static const char *cse_sub_part_str(enum bpdt_entry_type type) |
| 816 | { |
| 817 | switch (type) { |
| 818 | case IOM_FW: |
| 819 | return "IOM"; |
| 820 | case NPHY_FW: |
| 821 | return "NPHY"; |
| 822 | default: |
| 823 | return "Unknown"; |
| 824 | } |
| 825 | } |
| 826 | |
Sridhar Siricilla | 52479c7 | 2022-03-30 09:25:49 +0530 | [diff] [blame] | 827 | static bool cse_locate_area_as_rdev_rw(const struct cse_bp_info *cse_bp_info, |
| 828 | size_t bp, struct region_device *cse_rdev) |
| 829 | { |
| 830 | struct region_device cse_region_rdev; |
| 831 | uint32_t size; |
| 832 | uint32_t start_offset; |
| 833 | uint32_t end_offset; |
| 834 | |
| 835 | if (!cse_get_rw_rdev(&cse_region_rdev)) |
| 836 | return false; |
| 837 | |
| 838 | if (!strcmp(cse_regions[bp], "RO")) |
| 839 | cse_get_bp_entry_range(cse_bp_info, RO, &start_offset, &end_offset); |
| 840 | else |
| 841 | cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset); |
| 842 | |
| 843 | size = end_offset + 1 - start_offset; |
| 844 | |
| 845 | if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size)) |
| 846 | return false; |
| 847 | |
| 848 | printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n", |
| 849 | cse_regions[bp], start_offset, size); |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | static bool cse_sub_part_get_target_rdev(const struct cse_bp_info *cse_bp_info, |
| 854 | struct region_device *target_rdev, size_t bp, enum bpdt_entry_type type) |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 855 | { |
| 856 | struct bpdt_header bpdt_hdr; |
| 857 | struct region_device cse_rdev; |
| 858 | struct bpdt_entry bpdt_entries[MAX_SUBPARTS]; |
| 859 | uint8_t i; |
| 860 | |
Sridhar Siricilla | 52479c7 | 2022-03-30 09:25:49 +0530 | [diff] [blame] | 861 | if (!cse_locate_area_as_rdev_rw(cse_bp_info, bp, &cse_rdev)) { |
| 862 | printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n", |
| 863 | cse_regions[bp]); |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | |
| 867 | if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) { |
| 868 | printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n"); |
| 869 | return false; |
| 870 | } |
| 871 | |
| 872 | if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ, |
| 873 | (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) != |
| 874 | (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) { |
| 875 | printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n"); |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | /* walk through BPDT entries to identify sub-partition's payload offset and size */ |
| 880 | for (i = 0; i < bpdt_hdr.descriptor_count; i++) { |
| 881 | if (bpdt_entries[i].type == type) { |
| 882 | printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x," |
| 883 | "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset, |
| 884 | bpdt_entries[i].size); |
| 885 | |
| 886 | if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset, |
| 887 | bpdt_entries[i].size)) |
| 888 | return false; |
| 889 | else |
| 890 | return true; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type)); |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | static bool cse_get_sub_part_fw_version(enum bpdt_entry_type type, |
| 899 | const struct region_device *rdev, |
| 900 | struct fw_version *fw_ver) |
| 901 | { |
| 902 | struct subpart_entry subpart_entry; |
| 903 | struct subpart_entry_manifest_header man_hdr; |
| 904 | |
| 905 | if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ)) |
| 906 | != SUBPART_ENTRY_SZ) { |
| 907 | printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n", |
| 908 | cse_sub_part_str(type)); |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ)) |
| 913 | != SUBPART_MANIFEST_HDR_SZ) { |
| 914 | printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n", |
| 915 | cse_sub_part_str(type)); |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | fw_ver->major = man_hdr.binary_version.major; |
| 920 | fw_ver->minor = man_hdr.binary_version.minor; |
| 921 | fw_ver->hotfix = man_hdr.binary_version.hotfix; |
| 922 | fw_ver->build = man_hdr.binary_version.build; |
| 923 | |
| 924 | return true; |
| 925 | } |
| 926 | |
| 927 | static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver) |
| 928 | { |
| 929 | uint8_t *ptr = (uint8_t *)subpart_cbfs_rw; |
| 930 | struct subpart_entry *subpart_entry; |
| 931 | struct subpart_entry_manifest_header *man_hdr; |
| 932 | |
Elyes Haouas | 9018dee | 2022-11-18 15:07:33 +0100 | [diff] [blame] | 933 | subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ); |
| 934 | man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes); |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 935 | |
| 936 | fw_ver->major = man_hdr->binary_version.major; |
| 937 | fw_ver->minor = man_hdr->binary_version.minor; |
| 938 | fw_ver->hotfix = man_hdr->binary_version.hotfix; |
| 939 | fw_ver->build = man_hdr->binary_version.build; |
| 940 | } |
| 941 | |
| 942 | static bool cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info) |
| 943 | { |
| 944 | /* |
| 945 | * To set CSE's operation mode to HMRFPO mode: |
| 946 | * 1. Ensure CSE to boot from RO(BP1) |
| 947 | * 2. Send HMRFPO_ENABLE command to CSE |
| 948 | */ |
| 949 | if (!cse_boot_to_ro(cse_bp_info)) |
| 950 | return false; |
| 951 | |
| 952 | return cse_hmrfpo_enable(); |
| 953 | } |
| 954 | |
Sridhar Siricilla | 0ae7a8b | 2023-01-10 17:12:01 +0530 | [diff] [blame] | 955 | static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type, |
| 956 | uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz, |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 957 | struct region_device *target_rdev) |
| 958 | { |
| 959 | if (region_device_sz(target_rdev) < blob_sz) { |
| 960 | printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, " |
| 961 | "smaller than blob size:%zx, abort update\n", |
| 962 | cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz); |
| 963 | return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR; |
| 964 | } |
| 965 | |
| 966 | /* Erase CSE Lite sub-partition */ |
| 967 | if (!cse_erase_rw_region(target_rdev)) |
| 968 | return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL; |
| 969 | |
| 970 | /* Update CSE Lite sub-partition */ |
| 971 | if (!cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz)) |
| 972 | return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL; |
| 973 | |
| 974 | printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp), |
| 975 | cse_sub_part_str(type)); |
| 976 | |
| 977 | return CSE_LITE_SKU_PART_UPDATE_SUCCESS; |
| 978 | } |
| 979 | |
Sridhar Siricilla | 0ae7a8b | 2023-01-10 17:12:01 +0530 | [diff] [blame] | 980 | static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv) |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 981 | { |
| 982 | switch (rv) { |
| 983 | case CSE_LITE_SKU_PART_UPDATE_SUCCESS: |
| 984 | case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ: |
| 985 | return rv; |
| 986 | default: |
| 987 | cse_trigger_vboot_recovery(rv); |
| 988 | } |
| 989 | /* Control never reaches here */ |
| 990 | return rv; |
| 991 | } |
| 992 | |
| 993 | static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type, |
| 994 | const struct cse_bp_info *cse_bp_info, const char *name) |
| 995 | { |
| 996 | struct region_device target_rdev; |
| 997 | struct fw_version target_fw_ver, source_fw_ver; |
| 998 | enum csme_failure_reason rv; |
| 999 | size_t size; |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1000 | |
| 1001 | void *subpart_cbfs_rw = cbfs_map(name, &size); |
| 1002 | if (!subpart_cbfs_rw) { |
| 1003 | printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n", |
| 1004 | cse_sub_part_str(type)); |
| 1005 | return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR; |
| 1006 | } |
| 1007 | |
| 1008 | cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver); |
| 1009 | printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type), |
| 1010 | source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix, |
| 1011 | source_fw_ver.build); |
| 1012 | |
| 1013 | /* Trigger sub-partition update in CSE RO and CSE RW */ |
| 1014 | for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) { |
Sridhar Siricilla | 52479c7 | 2022-03-30 09:25:49 +0530 | [diff] [blame] | 1015 | if (!cse_sub_part_get_target_rdev(cse_bp_info, &target_rdev, bp, type)) { |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1016 | rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR; |
| 1017 | goto error_exit; |
| 1018 | } |
| 1019 | |
| 1020 | if (!cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver)) { |
| 1021 | rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR; |
| 1022 | goto error_exit; |
| 1023 | } |
| 1024 | |
| 1025 | printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp], |
| 1026 | cse_sub_part_str(type), target_fw_ver.major, |
| 1027 | target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build); |
| 1028 | |
| 1029 | if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) { |
| 1030 | printk(BIOS_INFO, "cse_lite: %s %s update is not required\n", |
| 1031 | cse_regions[bp], cse_sub_part_str(type)); |
| 1032 | rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ; |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
| 1036 | printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp), |
| 1037 | cse_sub_part_str(type)); |
| 1038 | |
| 1039 | if (!cse_prep_for_component_update(cse_bp_info)) { |
| 1040 | rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR; |
| 1041 | goto error_exit; |
| 1042 | } |
| 1043 | |
| 1044 | rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw, |
| 1045 | size, &target_rdev); |
| 1046 | |
| 1047 | if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS) |
| 1048 | goto error_exit; |
| 1049 | } |
| 1050 | error_exit: |
| 1051 | cbfs_unmap(subpart_cbfs_rw); |
| 1052 | return rv; |
| 1053 | } |
| 1054 | |
Sridhar Siricilla | 0ae7a8b | 2023-01-10 17:12:01 +0530 | [diff] [blame] | 1055 | static enum csme_failure_reason cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info) |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1056 | { |
| 1057 | if (skip_cse_sub_part_update()) { |
| 1058 | printk(BIOS_INFO, "CSE Sub-partition update not required\n"); |
| 1059 | return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ; |
| 1060 | } |
| 1061 | |
Sridhar Siricilla | 0ae7a8b | 2023-01-10 17:12:01 +0530 | [diff] [blame] | 1062 | enum csme_failure_reason rv; |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1063 | rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info, |
| 1064 | CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME); |
| 1065 | |
| 1066 | handle_cse_sub_part_fw_update_rv(rv); |
| 1067 | |
| 1068 | rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info, |
| 1069 | CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME); |
| 1070 | |
| 1071 | return handle_cse_sub_part_fw_update_rv(rv); |
| 1072 | } |
| 1073 | |
Sridhar Siricilla | 1a2b702 | 2020-12-04 02:22:28 +0530 | [diff] [blame] | 1074 | void cse_fw_sync(void) |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1075 | { |
| 1076 | static struct get_bp_info_rsp cse_bp_info; |
| 1077 | |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1078 | /* |
| 1079 | * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update |
| 1080 | * is not enabled and continue to update CSE sub-partitions. |
| 1081 | */ |
| 1082 | if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 1083 | printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n"); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 1087 | /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */ |
| 1088 | if (!cse_is_hfs3_fw_sku_lite()) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 1089 | printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n"); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1090 | return; |
| 1091 | } |
| 1092 | |
Sridhar Siricilla | 4b6e8ca | 2023-01-10 14:43:18 +0530 | [diff] [blame] | 1093 | if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 1094 | printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n"); |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1095 | |
| 1096 | /* If system is in recovery mode, don't trigger recovery again */ |
| 1097 | if (!vboot_recovery_mode_enabled()) { |
| 1098 | cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR); |
| 1099 | } else { |
| 1100 | printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, " |
| 1101 | "so no action\n"); |
| 1102 | return; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * If system is in recovery mode, CSE Lite update has to be skipped but CSE |
| 1108 | * sub-partitions like NPHY and IOM have to to be updated. If CSE sub-parition update |
| 1109 | * fails during recovery, just continue to boot. |
| 1110 | */ |
| 1111 | if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) { |
| 1112 | if (cse_sub_part_fw_update(&cse_bp_info.bp_info) == |
| 1113 | CSE_LITE_SKU_PART_UPDATE_SUCCESS) { |
| 1114 | cse_board_reset(); |
| 1115 | do_global_reset(); |
| 1116 | die("ERROR: GLOBAL RESET Failed to reset the system\n"); |
| 1117 | } |
| 1118 | |
| 1119 | return; |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1120 | } |
| 1121 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 1122 | if (cse_fix_data_failure_err(&cse_bp_info.bp_info) != CB_SUCCESS) |
Tim Wawrzynczak | 09635f4 | 2021-06-18 10:08:47 -0600 | [diff] [blame] | 1123 | cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR); |
Sridhar Siricilla | 2f6d555 | 2020-04-19 23:39:02 +0530 | [diff] [blame] | 1124 | |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 1125 | /* |
Sridhar Siricilla | 0aa1ac4 | 2022-03-09 20:35:32 +0530 | [diff] [blame] | 1126 | * cse firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and |
| 1127 | * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS |
| 1128 | * RW metadata or CSE CBFS RW blob is not available. |
Sridhar Siricilla | 361e364 | 2020-10-18 20:14:07 +0530 | [diff] [blame] | 1129 | */ |
Sridhar Siricilla | 0aa1ac4 | 2022-03-09 20:35:32 +0530 | [diff] [blame] | 1130 | if (is_cse_fw_update_enabled()) { |
Sridhar Siricilla | 4c2890d | 2020-12-09 00:28:30 +0530 | [diff] [blame] | 1131 | uint8_t rv; |
| 1132 | rv = cse_fw_update(&cse_bp_info.bp_info); |
| 1133 | if (rv) |
Tim Wawrzynczak | 09635f4 | 2021-06-18 10:08:47 -0600 | [diff] [blame] | 1134 | cse_trigger_vboot_recovery(rv); |
Sridhar Siricilla | 4c2890d | 2020-12-09 00:28:30 +0530 | [diff] [blame] | 1135 | } |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1136 | |
Krishna Prasad Bhat | 333edcc | 2021-11-26 06:52:27 +0530 | [diff] [blame] | 1137 | if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) |
| 1138 | cse_sub_part_fw_update(&cse_bp_info.bp_info); |
| 1139 | |
Sridahr Siricilla | 54b0356 | 2021-06-18 10:59:30 +0530 | [diff] [blame] | 1140 | if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info)) |
Tim Wawrzynczak | f2801f4 | 2021-06-22 11:25:14 -0600 | [diff] [blame] | 1141 | cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR); |
Sridahr Siricilla | 54b0356 | 2021-06-18 10:59:30 +0530 | [diff] [blame] | 1142 | |
Sridhar Siricilla | 8f9c153 | 2023-01-10 14:38:51 +0530 | [diff] [blame^] | 1143 | if (cse_boot_to_rw(&cse_bp_info.bp_info) != CB_SUCCESS) { |
Sridhar Siricilla | 9f71b17 | 2020-06-01 14:50:52 +0530 | [diff] [blame] | 1144 | printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n"); |
Tim Wawrzynczak | 09635f4 | 2021-06-18 10:08:47 -0600 | [diff] [blame] | 1145 | cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR); |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1146 | } |
Sridhar Siricilla | f87ff33 | 2019-09-12 17:18:20 +0530 | [diff] [blame] | 1147 | } |
Krishna P Bhat D | e3178a1 | 2022-04-21 16:40:06 +0530 | [diff] [blame] | 1148 | |
| 1149 | static void ramstage_cse_fw_sync(void *unused) |
| 1150 | { |
| 1151 | bool s3wake; |
| 1152 | s3wake = acpi_get_sleep_type() == ACPI_S3; |
| 1153 | |
| 1154 | if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && !s3wake) { |
| 1155 | timestamp_add_now(TS_CSE_FW_SYNC_START); |
| 1156 | cse_fw_sync(); |
| 1157 | timestamp_add_now(TS_CSE_FW_SYNC_END); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_fw_sync, NULL); |