soc/intel/pantherlake: Remove soc_info.[hc] interface
[coreboot2.git] / src / soc / intel / common / block / cse / cse_lite.c
blobc5f0560d2a6a7b5a3719bc4f920433fc0fc5ebbb
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpi.h>
4 #include <bootstate.h>
5 #include <cbfs.h>
6 #include <commonlib/region.h>
7 #include <console/console.h>
8 #include <cpu/cpu.h>
9 #include <crc_byte.h>
10 #include <elog.h>
11 #include <fmap.h>
12 #include <intelbasecode/debug_feature.h>
13 #include <intelblocks/cse.h>
14 #include <intelblocks/cse_layout.h>
15 #include <intelblocks/cse_lite.h>
16 #include <intelblocks/spi.h>
17 #include <security/vboot/misc.h>
18 #include <security/vboot/vboot_common.h>
19 #include <soc/intel/common/reset.h>
20 #include <timestamp.h>
22 #include "cse_lite_cmos.h"
24 static struct get_bp_info_rsp cse_bp_info_rsp;
26 enum cse_fw_state {
27 /* The CMOS and CBMEM have the current fw version. */
28 CSE_FW_WARM_BOOT,
30 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
31 CSE_FW_COLD_BOOT,
33 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
34 CSE_FW_INVALID,
37 static const char * const cse_regions[] = {"RO", "RW"};
39 static struct cse_specific_info cse_info;
41 void cse_log_ro_write_protection_info(bool mfg_mode)
43 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
45 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
46 cse_ro_wp_en ? "YES" : "NO");
48 if (cse_ro_wp_en) {
49 uint32_t base, limit;
50 spi_get_wp_cse_ro_range(&base, &limit);
51 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
52 base, limit);
56 * If manufacturing mode is disabled, but CSE RO is not write protected,
57 * log error.
59 if (!mfg_mode && !cse_ro_wp_en)
60 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
63 enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
65 struct cse_boot_perf_req {
66 struct mkhi_hdr hdr;
67 uint32_t reserved;
68 } __packed;
70 struct cse_boot_perf_req req = {
71 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
72 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
73 .reserved = 0,
76 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
78 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
79 HECI_MKHI_ADDR)) {
80 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
81 return CB_ERR;
84 if (boot_perf_rsp->hdr.result) {
85 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
86 boot_perf_rsp->hdr.result);
87 return CB_ERR;
90 return CB_SUCCESS;
93 static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
95 return &cse_bp_info_rsp.bp_info;
98 static uint8_t cse_get_current_bp(void)
100 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
101 return cse_bp_info->current_bp;
104 static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
106 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
107 return &cse_bp_info->bp_entries[bp];
110 static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
112 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
115 * Authenticate the CBMEM persistent data.
117 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
118 * could change the values stored in this region has to also trigger the global
119 * reset. Hence, CBMEM persistent data won't be available any time after such
120 * event (global reset or cold reset) being initiated.
122 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
123 * want to override the existing data in CBMEM to avoid any additional boot latency.
125 if (info->crc != crc)
126 return false;
128 return true;
131 static void store_cse_info_crc(struct cse_specific_info *info)
133 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
136 static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
137 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
139 enum cse_fw_state state = CSE_FW_WARM_BOOT;
140 size_t size = sizeof(struct fw_version);
142 * Compare if stored CSE version (from the previous boot) is same as current
143 * running CSE version.
145 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
147 * CMOS CSE versioin is invalid, possibly two scenarios
148 * 1. CSE FW update
149 * 2. First boot
151 state = CSE_FW_INVALID;
152 } else {
154 * Check if current running CSE version is same as previous stored CSE
155 * version aka CBMEM region is still valid.
157 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
158 state = CSE_FW_COLD_BOOT;
160 return state;
164 * Helper function that stores current CSE firmware version to CBMEM memory,
165 * except during recovery mode.
167 static void cse_store_rw_fw_version(void)
169 const struct cse_bp_entry *cse_bp;
170 cse_bp = cse_get_bp_entry(RW);
172 if (vboot_recovery_mode_enabled())
173 return;
175 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
176 /* update current CSE version and return */
177 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
178 &(cse_bp->fw_ver), sizeof(struct fw_version));
179 return;
182 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
183 sizeof(*cse_info_in_cbmem));
184 if (!cse_info_in_cbmem)
185 return;
187 /* Avoid CBMEM update if CBMEM already has persistent data */
188 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
189 return;
191 struct cse_specific_info cse_info_in_cmos;
192 cmos_read_fw_partition_info(&cse_info_in_cmos);
194 /* Get current cse firmware state */
195 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
196 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
197 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
199 /* Reset CBMEM data and update current CSE version */
200 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
201 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
202 &(cse_bp->fw_ver), sizeof(struct fw_version));
204 /* Update the CRC */
205 store_cse_info_crc(cse_info_in_cbmem);
207 if (fw_state == CSE_FW_INVALID) {
209 * Current CMOS data is outdated, which could be due to CSE update or
210 * rollback, hence, need to update CMOS with current CSE FPT versions.
212 cmos_write_fw_partition_info(cse_info_in_cbmem);
216 #if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
217 /* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
218 static void preram_cse_info_sync_to_cbmem(int is_recovery)
220 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
221 return;
223 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
224 return;
226 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
227 sizeof(*cse_info_in_cbmem));
228 if (!cse_info_in_cbmem)
229 return;
231 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
232 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
233 return;
235 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
236 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
237 store_cse_info_crc(cse_info_in_cbmem);
239 struct cse_specific_info cse_info_in_cmos;
240 cmos_read_fw_partition_info(&cse_info_in_cmos);
242 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
243 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
244 sizeof(struct fw_version))) {
245 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
246 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
247 return;
251 * Current CMOS data is outdated, which could be due to CSE update or
252 * rollback, hence, need to update CMOS with current CSE FPT versions.
254 cmos_write_fw_partition_info(cse_info_in_cbmem);
257 CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
258 #endif
260 static void cse_print_boot_partition_info(void)
262 const struct cse_bp_entry *cse_bp;
263 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
265 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
266 cse_bp_info->total_number_of_bp);
267 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
268 GET_BP_STR(cse_bp_info->current_bp));
269 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
270 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
272 /* Log version info of RO & RW partitions */
273 cse_bp = cse_get_bp_entry(RO);
274 if (cse_bp->status == BP_STATUS_SUCCESS)
275 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Start=0x%x, End=0x%x)\n",
276 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
277 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
278 cse_bp->start_offset, cse_bp->end_offset);
279 else
280 printk(BIOS_ERR, "cse_lite: %s status=0x%x\n", GET_BP_STR(RO), cse_bp->status);
282 cse_bp = cse_get_bp_entry(RW);
283 if (cse_bp->status == BP_STATUS_SUCCESS)
284 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Start=0x%x, End=0x%x)\n",
285 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
286 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
287 cse_bp->start_offset, cse_bp->end_offset);
288 else
289 printk(BIOS_ERR, "cse_lite: %s status=0x%x\n", GET_BP_STR(RW), cse_bp->status);
293 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
294 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
295 * It allows execution of the Boot Partition commands in below scenarios:
296 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
297 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
298 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
299 * The prerequisite check should be handled in cse_get_bp_info() and
300 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
301 * cmd handler calls.
303 static bool cse_is_bp_cmd_info_possible(void)
305 if (cse_is_hfs1_cws_normal()) {
306 if (cse_is_hfs1_com_normal())
307 return true;
308 if (cse_is_hfs1_com_secover_mei_msg())
309 return true;
310 if (cse_is_hfs1_com_soft_temp_disable())
311 return true;
313 return false;
316 static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
318 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
320 if (cse_bp_info_in_cbmem != NULL)
321 return cse_bp_info_in_cbmem;
323 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
324 sizeof(struct get_bp_info_rsp));
326 if (!cse_bp_info_in_cbmem) {
327 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
328 return NULL;
331 /* Copy the CSE Boot Partition Info command response to cbmem */
332 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
334 return cse_bp_info_in_cbmem;
337 static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
340 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
341 * then return false to indicate cse_bp_info is not valid.
343 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
344 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
345 !bp_info_rsp->hdr.is_resp) ? false : true;
348 static enum cb_err cse_get_bp_info(void)
350 struct get_bp_info_req {
351 struct mkhi_hdr hdr;
352 uint8_t reserved[4];
353 } __packed;
355 struct get_bp_info_req info_req = {
356 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
357 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
358 .reserved = {0},
362 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
363 * initialized, check if there is cse bp info response stored in cbmem. Once the data
364 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
365 * functions. In case, data is not available in cbmem or invalid, continue to send the
366 * GET_BOOT_PARTITION_INFO command, else return.
368 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
369 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
370 if (cse_bp_info_in_cbmem) {
371 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
372 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
373 sizeof(struct get_bp_info_rsp));
374 return CB_SUCCESS;
377 } else {
379 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
380 * global cse bp info response stored in global cse_bp_info_rsp is valid.
381 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
382 * command, else return.
384 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
385 return CB_SUCCESS;
388 if (!cse_is_bp_cmd_info_possible()) {
389 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
390 return CB_ERR;
393 size_t resp_size = sizeof(struct get_bp_info_rsp);
395 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
396 HECI_MKHI_ADDR)) {
397 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
398 return CB_ERR;
401 if (cse_bp_info_rsp.hdr.result) {
402 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
403 cse_bp_info_rsp.hdr.result);
404 return CB_ERR;
407 cse_print_boot_partition_info();
408 return CB_SUCCESS;
411 void cse_fill_bp_info(void)
413 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
414 return;
416 if (vboot_recovery_mode_enabled())
417 return;
419 if (cse_get_bp_info() != CB_SUCCESS)
420 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
423 /* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
424 static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
426 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
427 return;
429 if (vboot_recovery_mode_enabled())
430 return;
432 sync_cse_bp_info_to_cbmem();
435 CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
438 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
439 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
440 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
441 * This function must be used before EOP.
442 * Returns false on failure and true on success.
444 static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
446 struct set_boot_partition_info_req {
447 struct mkhi_hdr hdr;
448 uint8_t next_bp;
449 uint8_t reserved[3];
450 } __packed;
452 struct set_boot_partition_info_req switch_req = {
453 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
454 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
455 .next_bp = bp,
456 .reserved = {0},
459 if (bp != RO && bp != RW) {
460 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
461 return CB_ERR_ARG;
464 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
466 if (!cse_is_bp_cmd_info_possible()) {
467 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
468 return CB_ERR;
471 struct mkhi_hdr switch_resp;
472 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
474 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
475 HECI_MKHI_ADDR))
476 return CB_ERR;
478 if (switch_resp.result) {
479 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
480 switch_resp.result);
481 return CB_ERR;
484 return CB_SUCCESS;
487 static enum cb_err cse_data_clear_request(void)
489 struct data_clr_request {
490 struct mkhi_hdr hdr;
491 uint8_t reserved[4];
492 } __packed;
494 struct data_clr_request data_clr_rq = {
495 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
496 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
497 .reserved = {0},
500 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
501 cse_get_current_bp() != RO) {
502 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
503 return CB_ERR;
506 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
508 struct mkhi_hdr data_clr_rsp;
509 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
511 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
512 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
513 return CB_ERR;
516 if (data_clr_rsp.result) {
517 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
518 data_clr_rsp.result);
519 return CB_ERR;
522 return CB_SUCCESS;
525 __weak void cse_board_reset(void)
527 /* Default weak implementation, does nothing. */
530 __weak void cse_fw_update_misc_oper(void)
532 /* Default weak implementation, does nothing. */
535 /* Set the CSE's next boot partition and issues system reset */
536 static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
538 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
539 return CB_ERR;
541 /* Allow the board to perform a reset for CSE RO<->RW jump */
542 cse_board_reset();
544 /* If board does not perform the reset, then perform global_reset */
545 do_global_reset();
547 die("cse_lite: Failed to reset the system\n");
549 /* Control never reaches here */
550 return CB_ERR;
553 static enum cb_err cse_boot_to_rw(void)
555 if (cse_get_current_bp() == RW)
556 return CB_SUCCESS;
558 return cse_set_and_boot_from_next_bp(RW);
561 /* Check if CSE RW data partition is valid or not */
562 static bool cse_is_rw_dp_valid(void)
564 const struct cse_bp_entry *rw_bp;
566 rw_bp = cse_get_bp_entry(RW);
567 return rw_bp->status != BP_STATUS_DATA_FAILURE;
571 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
572 * otherwise false if any operation fails.
574 static enum cb_err cse_fix_data_failure_err(void)
577 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
578 * - Send DATA CLEAR HECI command to CSE
579 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
580 * - Issue GLOBAL RESET HECI command.
582 if (cse_is_rw_dp_valid())
583 return CB_SUCCESS;
585 if (cse_data_clear_request() != CB_SUCCESS)
586 return CB_ERR;
588 return cse_boot_to_rw();
591 static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
593 const struct cse_bp_entry *cse_bp;
595 cse_bp = cse_get_bp_entry(bp);
596 return &cse_bp->fw_ver;
599 static const struct fw_version *cse_get_rw_version(void)
601 return cse_get_bp_entry_version(RW);
604 static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
605 uint32_t *end_offset)
607 const struct cse_bp_entry *cse_bp;
609 cse_bp = cse_get_bp_entry(bp);
611 if (start_offset)
612 *start_offset = cse_bp->start_offset;
614 if (end_offset)
615 *end_offset = cse_bp->end_offset;
618 static bool cse_is_rw_bp_status_valid(void)
620 const struct cse_bp_entry *rw_bp;
622 rw_bp = cse_get_bp_entry(RW);
624 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
625 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
626 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
627 return false;
629 return true;
632 static enum cb_err cse_boot_to_ro(void)
634 if (cse_get_current_bp() == RO)
635 return CB_SUCCESS;
637 return cse_set_and_boot_from_next_bp(RO);
640 static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
642 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
643 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
644 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
645 return CB_ERR;
648 return CB_SUCCESS;
651 static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
653 uint32_t cse_bp_sign;
655 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
656 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
657 return false;
660 return cse_bp_sign == CSE_RW_SIGNATURE;
663 static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
665 struct region_device cse_region_rdev;
666 size_t size;
667 uint32_t start_offset;
668 uint32_t end_offset;
670 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
671 return CB_ERR;
673 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
674 size = end_offset + 1 - start_offset;
676 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
677 return CB_ERR;
679 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
680 (uint32_t)start_offset, (uint32_t)size);
682 return CB_SUCCESS;
686 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
687 * In case of CSE component comparison:
688 * If ver_cmp_status = 0, no update is required
689 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
690 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
692 static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
694 if (a->major != b->major)
695 return a->major - b->major;
696 else if (a->minor != b->minor)
697 return a->minor - b->minor;
698 else if (a->hotfix != b->hotfix)
699 return a->hotfix - b->hotfix;
700 else
701 return a->build - b->build;
704 static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
706 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
707 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
708 return CB_ERR;
710 return CB_SUCCESS;
713 static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
714 size_t offset, size_t size)
716 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
717 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
718 return CB_ERR;
721 return CB_SUCCESS;
724 enum cse_update_status {
725 CSE_UPDATE_NOT_REQUIRED,
726 CSE_UPDATE_UPGRADE,
727 CSE_UPDATE_DOWNGRADE,
728 CSE_UPDATE_CORRUPTED,
729 CSE_UPDATE_METADATA_ERROR,
732 static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
734 if ((*curr - start) >= size) {
735 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
736 return false;
739 *ver_field = skip_atoi(curr);
740 (*curr)++;
741 return true;
744 static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
746 char *version_str, *cbfs_ptr;
747 size_t size;
749 if (cbfs_rw_version == NULL)
750 return CB_ERR;
752 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
753 version_str = cbfs_ptr;
754 if (!version_str) {
755 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
756 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
757 return CB_ERR;
760 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
761 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
762 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
763 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
764 cbfs_unmap(version_str);
765 return CB_ERR;
768 cbfs_unmap(version_str);
769 return CB_SUCCESS;
772 static bool is_cse_sync_enforced(void)
775 * Force test CSE firmware update scenario if below conditions are being met:
776 * - VB2_GBB_FLAG_FORCE_CSE_SYNC flag is set
777 * - CSE FW is in RO
779 struct vb2_context *ctx = vboot_get_context();
780 if ((vb2api_gbb_get_flags(ctx) & VB2_GBB_FLAG_FORCE_CSE_SYNC) &&
781 cse_get_current_bp() == RO) {
782 return true;
784 return false;
787 static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
789 int ret;
790 struct fw_version cbfs_rw_version;
792 if (!cse_is_rw_bp_sign_valid(target_rdev))
793 return CSE_UPDATE_CORRUPTED;
795 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
796 return CSE_UPDATE_METADATA_ERROR;
798 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
799 cbfs_rw_version.major,
800 cbfs_rw_version.minor,
801 cbfs_rw_version.hotfix,
802 cbfs_rw_version.build);
804 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
805 if (ret == 0) {
806 if (is_cse_sync_enforced()) {
807 printk(BIOS_WARNING, "Force CSE Firmware upgrade for Autotest\n");
808 return CSE_UPDATE_UPGRADE;
810 return CSE_UPDATE_NOT_REQUIRED;
811 } else {
812 if (ret < 0)
813 return CSE_UPDATE_DOWNGRADE;
814 else
815 return CSE_UPDATE_UPGRADE;
819 static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
820 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
822 /* Points to CSE CBFS RW image after boot partition signature */
823 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
825 /* Size of CSE CBFS RW image without boot partition signature */
826 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
828 /* Update except CSE RW signature */
829 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
830 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
831 return CB_ERR;
833 /* Update CSE RW signature to indicate update is complete */
834 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
835 return CB_ERR;
837 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
838 elog_add_event_byte(ELOG_TYPE_FW_EARLY_SOL, ELOG_FW_EARLY_SOL_CSE_SYNC);
839 return CB_SUCCESS;
842 static bool is_cse_fw_update_enabled(void)
844 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
845 return false;
847 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
848 return false;
850 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
851 return !is_debug_cse_fw_update_disable();
853 return true;
856 static enum csme_failure_reason cse_update_rw(const void *cse_cbfs_rw, const size_t cse_blob_sz,
857 struct region_device *target_rdev)
859 if (region_device_sz(target_rdev) < cse_blob_sz) {
860 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
861 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
862 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
865 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
866 return CSE_LITE_SKU_FW_UPDATE_ERROR;
868 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
869 return CSE_LITE_SKU_FW_UPDATE_ERROR;
871 return CSE_NO_ERROR;
874 static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
876 if (status == CSE_UPDATE_CORRUPTED)
877 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
879 * To set CSE's operation mode to HMRFPO mode:
880 * 1. Ensure CSE to boot from RO(BP1)
881 * 2. Send HMRFPO_ENABLE command to CSE
883 if (cse_boot_to_ro() != CB_SUCCESS)
884 return CB_ERR;
886 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
887 /* Reset the PSR backup command status in CMOS */
888 if (CONFIG(SOC_INTEL_CSE_LITE_PSR))
889 update_psr_backup_status(PSR_BACKUP_PENDING);
891 if (cse_data_clear_request() != CB_SUCCESS) {
892 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
893 return CB_ERR;
897 return cse_hmrfpo_enable();
900 static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
901 struct region_device *target_rdev)
903 enum csme_failure_reason rv;
904 void *cse_cbfs_rw = NULL;
905 size_t size;
907 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
908 cse_cbfs_rw = cbfs_cbmem_alloc(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME,
909 CBMEM_ID_CSE_UPDATE, &size);
910 } else {
911 cse_cbfs_rw = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
914 if (!cse_cbfs_rw) {
915 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
916 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
919 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
920 rv = CSE_COMMUNICATION_ERROR;
921 goto error_exit;
924 cse_fw_update_misc_oper();
925 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
927 error_exit:
928 cbfs_unmap(cse_cbfs_rw);
929 return rv;
932 static bool is_psr_data_backed_up(void)
934 /* Track PSR backup status in CMOS */
935 return (get_psr_backup_status() == PSR_BACKUP_DONE);
938 static bool is_psr_supported(void)
940 uint32_t feature_status;
943 * Check if SoC has support for PSR feature typically PSR feature
944 * is only supported by vpro SKU
947 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
948 printk(BIOS_ERR, "cse_get_fw_feature_state command failed !\n");
949 return false;
952 if (!(feature_status & ME_FW_FEATURE_PSR)) {
953 printk(BIOS_DEBUG, "PSR is not supported in this SKU !\n");
954 return false;
957 return true;
961 * PSR data needs to be backed up prior to downgrade. So switch the CSE boot mode to RW, send
962 * PSR back-up command to CSE and update the PSR back-up state in CMOS.
964 static void backup_psr_data(void)
966 printk(BIOS_DEBUG, "cse_lite: Initiate PSR data backup flow\n");
967 /* Switch CSE to RW to send PSR_HECI_FW_DOWNGRADE_BACKUP command */
968 if (cse_boot_to_rw() != CB_SUCCESS) {
969 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
970 goto update_and_exit;
973 * The function to check for PSR feature support can only be called after
974 * switching to RW partition. The command MKHI_FWCAPS_GET_FW_FEATURE_STATE
975 * that gives feature state is supported by a process that is loaded only
976 * when CSE boots from RW.
979 if (!is_psr_supported())
980 goto update_and_exit;
983 * Prerequisites:
984 * 1) HFSTS1 Current Working State is Normal
985 * 2) HFSTS1 Current Operation Mode is Normal
987 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
988 printk(BIOS_DEBUG, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
989 "prerequisites not met!\n");
990 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
991 goto update_and_exit;
994 /* Send PSR_HECI_FW_DOWNGRADE_BACKUP command */
995 struct psr_heci_fw_downgrade_backup_req {
996 struct psr_heci_header header;
997 } __packed;
999 struct psr_heci_fw_downgrade_backup_req req = {
1000 .header.command = PSR_HECI_FW_DOWNGRADE_BACKUP,
1003 struct psr_heci_fw_downgrade_backup_res {
1004 struct psr_heci_header header;
1005 uint32_t status;
1006 } __packed;
1008 struct psr_heci_fw_downgrade_backup_res backup_psr_resp;
1009 size_t resp_size = sizeof(backup_psr_resp);
1011 printk(BIOS_DEBUG, "cse_lite: Send PSR_HECI_FW_DOWNGRADE_BACKUP command\n");
1012 if (heci_send_receive(&req, sizeof(req),
1013 &backup_psr_resp, &resp_size, HECI_PSR_ADDR)) {
1014 printk(BIOS_ERR, "cse_lite: could not backup PSR data\n");
1015 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP, ELOG_PSR_DATA_BACKUP_FAILED);
1016 } else {
1017 if (backup_psr_resp.status != PSR_STATUS_SUCCESS) {
1018 printk(BIOS_ERR, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1019 "returned %u\n", backup_psr_resp.status);
1020 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1021 ELOG_PSR_DATA_BACKUP_FAILED);
1022 } else {
1023 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1024 ELOG_PSR_DATA_BACKUP_SUCCESS);
1028 update_and_exit:
1030 * An attempt to send PSR back-up command has been made. Update this info in CMOS and
1031 * send success once backup_psr_data() has been called. We do not want to put the system
1032 * into recovery for PSR data backup command pre-requisites not being met.
1033 * We cannot do much if CSE fails to backup the PSR data, except create an event log.
1035 update_psr_backup_status(PSR_BACKUP_DONE);
1038 static void initiate_psr_data_backup(void)
1040 if (is_psr_data_backed_up())
1041 return;
1043 backup_psr_data();
1047 * Check if a CSE Firmware update is required
1048 * returns true if an update is required, false otherwise
1050 bool is_cse_fw_update_required(void)
1052 struct fw_version cbfs_rw_version;
1054 if (!is_cse_fw_update_enabled())
1055 return false;
1058 * First, check if cse_bp_info_rsp global structure is populated.
1059 * If not, it implies that cse_fill_bp_info() function is not called.
1061 if (!is_cse_bp_info_valid(&cse_bp_info_rsp))
1062 return false;
1064 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
1065 return false;
1067 /* Check if CSE sync is enforced */
1068 if (is_cse_sync_enforced()) {
1069 return true;
1071 return !!cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
1074 static uint8_t cse_fw_update(void)
1076 struct region_device target_rdev;
1077 enum cse_update_status status;
1079 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
1080 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1081 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1084 status = cse_check_update_status(&target_rdev);
1085 if (status == CSE_UPDATE_NOT_REQUIRED)
1086 return CSE_NO_ERROR;
1087 if (status == CSE_UPDATE_METADATA_ERROR)
1088 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
1089 if (CONFIG(SOC_INTEL_CSE_LITE_PSR) && status == CSE_UPDATE_DOWNGRADE)
1090 initiate_psr_data_backup();
1092 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
1093 return cse_trigger_fw_update(status, &target_rdev);
1096 static const char *cse_sub_part_str(enum bpdt_entry_type type)
1098 switch (type) {
1099 case IOM_FW:
1100 return "IOM";
1101 case NPHY_FW:
1102 return "NPHY";
1103 default:
1104 return "Unknown";
1108 static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
1110 struct region_device cse_region_rdev;
1111 uint32_t size;
1112 uint32_t start_offset;
1113 uint32_t end_offset;
1115 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1116 return CB_ERR;
1118 if (!strcmp(cse_regions[bp], "RO"))
1119 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
1120 else
1121 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
1123 size = end_offset + 1 - start_offset;
1125 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
1126 return CB_ERR;
1128 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1129 cse_regions[bp], start_offset, size);
1130 return CB_SUCCESS;
1133 static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1134 enum bpdt_entry_type type)
1136 struct bpdt_header bpdt_hdr;
1137 struct region_device cse_rdev;
1138 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1139 uint8_t i;
1141 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
1142 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1143 cse_regions[bp]);
1144 return CB_ERR;
1147 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1148 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
1149 return CB_ERR;
1152 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1153 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1154 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1155 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
1156 return CB_ERR;
1159 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1160 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1161 if (bpdt_entries[i].type == type) {
1162 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1163 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1164 bpdt_entries[i].size);
1166 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1167 bpdt_entries[i].size))
1168 return CB_ERR;
1169 else
1170 return CB_SUCCESS;
1174 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
1175 return CB_ERR;
1178 static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
1179 const struct region_device *rdev,
1180 struct fw_version *fw_ver)
1182 struct subpart_entry subpart_entry;
1183 struct subpart_entry_manifest_header man_hdr;
1185 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1186 != SUBPART_ENTRY_SZ) {
1187 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1188 cse_sub_part_str(type));
1189 return CB_ERR;
1192 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1193 != SUBPART_MANIFEST_HDR_SZ) {
1194 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1195 cse_sub_part_str(type));
1196 return CB_ERR;
1199 fw_ver->major = man_hdr.binary_version.major;
1200 fw_ver->minor = man_hdr.binary_version.minor;
1201 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1202 fw_ver->build = man_hdr.binary_version.build;
1204 return CB_SUCCESS;
1207 static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1209 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1210 struct subpart_entry *subpart_entry;
1211 struct subpart_entry_manifest_header *man_hdr;
1213 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1214 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
1216 fw_ver->major = man_hdr->binary_version.major;
1217 fw_ver->minor = man_hdr->binary_version.minor;
1218 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1219 fw_ver->build = man_hdr->binary_version.build;
1222 static enum cb_err cse_prep_for_component_update(void)
1225 * To set CSE's operation mode to HMRFPO mode:
1226 * 1. Ensure CSE to boot from RO(BP1)
1227 * 2. Send HMRFPO_ENABLE command to CSE
1229 if (cse_boot_to_ro() != CB_SUCCESS)
1230 return CB_ERR;
1232 return cse_hmrfpo_enable();
1235 static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1236 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
1237 struct region_device *target_rdev)
1239 if (region_device_sz(target_rdev) < blob_sz) {
1240 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1241 "smaller than blob size:%zx, abort update\n",
1242 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1243 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1246 /* Erase CSE Lite sub-partition */
1247 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
1248 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1250 /* Update CSE Lite sub-partition */
1251 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
1252 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1254 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1255 cse_sub_part_str(type));
1257 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1260 static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
1262 switch (rv) {
1263 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1264 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1265 return rv;
1266 default:
1267 cse_trigger_vboot_recovery(rv);
1269 /* Control never reaches here */
1270 return rv;
1273 static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
1274 const char *name)
1276 struct region_device target_rdev;
1277 struct fw_version target_fw_ver, source_fw_ver;
1278 enum csme_failure_reason rv;
1279 size_t size;
1281 void *subpart_cbfs_rw = cbfs_map(name, &size);
1282 if (!subpart_cbfs_rw) {
1283 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1284 cse_sub_part_str(type));
1285 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1288 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1289 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1290 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1291 source_fw_ver.build);
1293 /* Trigger sub-partition update in CSE RO and CSE RW */
1294 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
1295 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
1296 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1297 goto error_exit;
1300 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
1301 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1302 goto error_exit;
1305 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1306 cse_sub_part_str(type), target_fw_ver.major,
1307 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1309 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1310 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1311 cse_regions[bp], cse_sub_part_str(type));
1312 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1313 continue;
1316 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1317 cse_sub_part_str(type));
1319 if (cse_prep_for_component_update() != CB_SUCCESS) {
1320 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1321 goto error_exit;
1324 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1325 size, &target_rdev);
1327 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1328 goto error_exit;
1330 error_exit:
1331 cbfs_unmap(subpart_cbfs_rw);
1332 return rv;
1335 static enum csme_failure_reason cse_sub_part_fw_update(void)
1337 if (skip_cse_sub_part_update()) {
1338 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1339 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1342 enum csme_failure_reason rv;
1343 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1345 handle_cse_sub_part_fw_update_rv(rv);
1347 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1349 return handle_cse_sub_part_fw_update_rv(rv);
1352 static void do_cse_fw_sync(void)
1355 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1356 * is not enabled and continue to update CSE sub-partitions.
1358 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
1359 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
1360 return;
1363 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1364 if (!cse_is_hfs3_fw_sku_lite()) {
1365 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
1366 return;
1369 if (cse_get_bp_info() != CB_SUCCESS) {
1370 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
1372 /* If system is in recovery mode, don't trigger recovery again */
1373 if (!vboot_recovery_mode_enabled()) {
1374 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1375 } else {
1376 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1377 "so no action\n");
1378 return;
1382 /* Store the CSE RW Firmware Version into CBMEM */
1383 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1384 cse_store_rw_fw_version();
1387 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
1388 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
1389 * fails during recovery, just continue to boot.
1391 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1392 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1393 cse_board_reset();
1394 do_global_reset();
1395 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1398 return;
1401 if (cse_fix_data_failure_err() != CB_SUCCESS)
1402 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
1405 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
1406 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1407 * RW metadata or CSE CBFS RW blob is not available.
1409 if (is_cse_fw_update_enabled()) {
1410 uint8_t rv;
1411 rv = cse_fw_update();
1412 if (rv)
1413 cse_trigger_vboot_recovery(rv);
1416 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1417 cse_sub_part_fw_update();
1419 if (!cse_is_rw_bp_status_valid())
1420 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
1422 if (cse_boot_to_rw() != CB_SUCCESS) {
1423 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
1424 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
1428 void cse_fw_sync(void)
1430 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1431 return;
1433 timestamp_add_now(TS_CSE_FW_SYNC_START);
1434 do_cse_fw_sync();
1435 timestamp_add_now(TS_CSE_FW_SYNC_END);
1438 static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1439 struct fw_version_resp *resp)
1441 enum cse_tx_rx_status ret;
1442 struct fw_version_msg {
1443 struct mkhi_hdr hdr;
1444 enum fpt_partition_id partition_id;
1445 } __packed msg = {
1446 .hdr = {
1447 .group_id = MKHI_GROUP_ID_GEN,
1448 .command = GEN_GET_IMAGE_FW_VERSION,
1450 .partition_id = id,
1454 * Prerequisites:
1455 * 1) HFSTS1 CWS is Normal
1456 * 2) HFSTS1 COM is Normal
1457 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1460 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1461 !cse_is_hfs1_com_normal()) {
1462 printk(BIOS_ERR,
1463 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1464 return CB_ERR;
1467 size_t resp_size = sizeof(struct fw_version_resp);
1468 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1470 if (ret || resp->hdr.result) {
1471 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1472 id, resp->hdr.result);
1473 return CB_ERR;
1476 return CB_SUCCESS;
1479 static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1480 struct fw_version_resp *resp)
1482 if (vboot_recovery_mode_enabled()) {
1483 printk(BIOS_WARNING,
1484 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1485 return CB_ERR;
1488 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1489 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1490 return CB_ERR;
1493 return send_get_fpt_partition_info_cmd(id, resp);
1496 static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1498 const struct fw_version invalid_fw = {0, 0, 0, 0};
1499 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1500 return false;
1501 return true;
1505 * Helper function to read ISH version from CSE FPT using HECI command.
1507 * The HECI command only be executed after memory has been initialized.
1508 * This is because the command relies on resources that are not available
1509 * until DRAM initialization command has been sent.
1511 static void store_ish_version(void)
1513 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1514 return;
1516 if (!ENV_RAMSTAGE)
1517 return;
1519 if (vboot_recovery_mode_enabled())
1520 return;
1522 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1523 if (cse_info_in_cbmem == NULL)
1524 return;
1526 struct cse_specific_info cse_info_in_cmos;
1527 cmos_read_fw_partition_info(&cse_info_in_cmos);
1529 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1530 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
1532 /* Get current cse firmware state */
1533 enum cse_fw_state fw_state = get_cse_state(
1534 &(cbmem_version->cur_cse_fw_version),
1535 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1536 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
1538 if (fw_state == CSE_FW_WARM_BOOT) {
1539 return;
1540 } else {
1541 if (fw_state == CSE_FW_COLD_BOOT &&
1542 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1543 /* CMOS data is persistent across cold boots */
1544 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1545 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1546 sizeof(struct cse_fw_ish_version_info));
1547 store_cse_info_crc(cse_info_in_cbmem);
1548 } else {
1550 * Current running CSE version is different than previous stored CSE version
1551 * which could be due to CSE update or rollback, hence, need to send ISHC
1552 * partition info cmd to know the currently running ISH version.
1554 struct fw_version_resp resp;
1555 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1556 &resp) == CB_SUCCESS) {
1557 /* Update stored CSE version with current cse version */
1558 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1559 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
1561 /* Retrieve and update current ish version */
1562 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1563 &(resp.manifest_data.version), sizeof(struct fw_version));
1565 /* Update the CRC */
1566 store_cse_info_crc(cse_info_in_cbmem);
1568 /* Update CMOS with current CSE FPT versions.*/
1569 cmos_write_fw_partition_info(cse_info_in_cbmem);
1575 static void ramstage_cse_misc_ops(void *unused)
1577 if (acpi_get_sleep_type() == ACPI_S3)
1578 return;
1580 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
1581 cse_fw_sync();
1584 * Store the ISH RW Firmware Version into CBMEM if ISH partition
1585 * is available
1587 if (!CONFIG(DRIVER_INTEL_ISH_HAS_MAIN_FW) && soc_is_ish_partition_enabled())
1588 store_ish_version();
1591 BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);