Debian Codegraph @

src/soc/intel/common/block/cse/cse_lite.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
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>
21
22 #include "cse_lite_cmos.h"
23
24 static struct get_bp_info_rsp cse_bp_info_rsp;
25
26 enum cse_fw_state {
27 /* The CMOS and CBMEM have the current fw version. */
28 CSE_FW_WARM_BOOT,
29
30 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
31 CSE_FW_COLD_BOOT,
32
33 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
34 CSE_FW_INVALID,
35 };
36
37 static const char * const cse_regions[] = {"RO", "RW"};
38
39 static struct cse_specific_info cse_info;
40
41 void cse_log_ro_write_protection_info(bool mfg_mode)
42 {
43 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
44
45 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
46 cse_ro_wp_en ? "YES" : "NO");
47
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);
53 }
54
55 /*
56 * If manufacturing mode is disabled, but CSE RO is not write protected,
57 * log error.
58 */
59 if (!mfg_mode && !cse_ro_wp_en)
60 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
61 }
62
63
64 static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
65 {
66 return &cse_bp_info_rsp.bp_info;
67 }
68
69 static uint8_t cse_get_current_bp(void)
70 {
71 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
72 return cse_bp_info->current_bp;
73 }
74
75 static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
76 {
77 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
78 return &cse_bp_info->bp_entries[bp];
79 }
80
81 static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
82 {
83 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
84
85 /*
86 * Authenticate the CBMEM persistent data.
87 *
88 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
89 * could change the values stored in this region has to also trigger the global
90 * reset. Hence, CBMEM persistent data won't be available any time after such
91 * event (global reset or cold reset) being initiated.
92 *
93 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
94 * want to override the existing data in CBMEM to avoid any additional boot latency.
95 */
96 if (info->crc != crc)
97 return false;
98
99 return true;
100 }
101
102 static void store_cse_info_crc(struct cse_specific_info *info)
103 {
104 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
105 }
106
107 static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
108 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
109 {
110 enum cse_fw_state state = CSE_FW_WARM_BOOT;
111 size_t size = sizeof(struct fw_version);
112 /*
113 * Compare if stored CSE version (from the previous boot) is same as current
114 * running CSE version.
115 */
116 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
117 /*
118 * CMOS CSE versioin is invalid, possibly two scenarios
119 * 1. CSE FW update
120 * 2. First boot
121 */
122 state = CSE_FW_INVALID;
123 } else {
124 /*
125 * Check if current running CSE version is same as previous stored CSE
126 * version aka CBMEM region is still valid.
127 */
128 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
129 state = CSE_FW_COLD_BOOT;
130 }
131 return state;
132 }
133
134 /*
135 * Helper function that stores current CSE firmware version to CBMEM memory,
136 * except during recovery mode.
137 */
138 static void cse_store_rw_fw_version(void)
139 {
140 const struct cse_bp_entry *cse_bp;
141 cse_bp = cse_get_bp_entry(RW);
142
143 if (vboot_recovery_mode_enabled())
144 return;
145
146 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
147 /* update current CSE version and return */
148 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
149 &(cse_bp->fw_ver), sizeof(struct fw_version));
150 return;
151 }
152
153 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
154 sizeof(*cse_info_in_cbmem));
155 if (!cse_info_in_cbmem)
156 return;
157
158 /* Avoid CBMEM update if CBMEM already has persistent data */
159 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
160 return;
161
162 struct cse_specific_info cse_info_in_cmos;
163 cmos_read_fw_partition_info(&cse_info_in_cmos);
164
165 /* Get current cse firmware state */
166 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
167 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
168 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
169
170 /* Reset CBMEM data and update current CSE version */
171 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
172 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
173 &(cse_bp->fw_ver), sizeof(struct fw_version));
174
175 /* Update CBMEM cse sync status flag with CMOS stored information */
176 cse_info_in_cbmem->cse_sync_status = cse_info_in_cmos.cse_sync_status;
177
178 /* Update the CRC */
179 store_cse_info_crc(cse_info_in_cbmem);
180
181 if (fw_state == CSE_FW_INVALID) {
182 /*
183 * Current CMOS data is outdated, which could be due to CSE update or
184 * rollback, hence, need to update CMOS with current CSE FPT versions.
185 */
186 cmos_write_fw_partition_info(cse_info_in_cbmem);
187 }
188 }
189
190 #if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
191 /* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
192 static void preram_cse_info_sync_to_cbmem(int is_recovery)
193 {
194 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
195 return;
196
197 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
198 return;
199
200 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
201 sizeof(*cse_info_in_cbmem));
202 if (!cse_info_in_cbmem)
203 return;
204
205 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
206 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
207 return;
208
209 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
210 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
211 store_cse_info_crc(cse_info_in_cbmem);
212
213 struct cse_specific_info cse_info_in_cmos;
214 cmos_read_fw_partition_info(&cse_info_in_cmos);
215
216 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
217 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
218 sizeof(struct fw_version))) {
219 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
220 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
221 return;
222 }
223
224 /*
225 * Current CMOS data is outdated, which could be due to CSE update or
226 * rollback, hence, need to update CMOS with current CSE FPT versions.
227 */
228 cmos_write_fw_partition_info(cse_info_in_cbmem);
229 }
230
231 CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
232 #endif
233
234 static void cse_print_boot_partition_info(void)
235 {
236 const struct cse_bp_entry *cse_bp;
237 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
238
239 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
240 cse_bp_info->total_number_of_bp);
241 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
242 GET_BP_STR(cse_bp_info->current_bp));
243 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
244 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
245
246 /* Log version info of RO & RW partitions */
247 cse_bp = cse_get_bp_entry(RO);
248 if (cse_bp->status == BP_STATUS_SUCCESS)
249 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Start=0x%x, End=0x%x)\n",
250 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
251 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
252 cse_bp->start_offset, cse_bp->end_offset);
253 else
254 printk(BIOS_ERR, "cse_lite: %s status=0x%x\n", GET_BP_STR(RO), cse_bp->status);
255
256 cse_bp = cse_get_bp_entry(RW);
257 if (cse_bp->status == BP_STATUS_SUCCESS)
258 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Start=0x%x, End=0x%x)\n",
259 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
260 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
261 cse_bp->start_offset, cse_bp->end_offset);
262 else
263 printk(BIOS_ERR, "cse_lite: %s status=0x%x\n", GET_BP_STR(RW), cse_bp->status);
264 }
265
266 /*
267 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
268 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
269 * It allows execution of the Boot Partition commands in below scenarios:
270 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
271 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
272 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
273 * - When CSE boots from RW partition (COM: Soft Temp Disable and CWS: M3 w/o UMA)
274 * The prerequisite check should be handled in cse_get_bp_info() and
275 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
276 * cmd handler calls.
277 */
278 static bool cse_is_bp_cmd_info_possible(void)
279 {
280 if (cse_is_hfs1_cws_normal()) {
281 if (cse_is_hfs1_com_normal())
282 return true;
283 if (cse_is_hfs1_com_secover_mei_msg())
284 return true;
285 if (cse_is_hfs1_com_soft_temp_disable())
286 return true;
287 } else if (cse_is_hfs1_cws_m3_no_uma() && cse_is_hfs1_com_soft_temp_disable()) {
288 return true;
289 }
290 return false;
291 }
292
293 static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
294 {
295 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
296
297 if (cse_bp_info_in_cbmem != NULL)
298 return cse_bp_info_in_cbmem;
299
300 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
301 sizeof(struct get_bp_info_rsp));
302
303 if (!cse_bp_info_in_cbmem) {
304 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
305 return NULL;
306 }
307
308 /* Copy the CSE Boot Partition Info command response to cbmem */
309 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
310
311 return cse_bp_info_in_cbmem;
312 }
313
314 static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
315 {
316 /*
317 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
318 * then return false to indicate cse_bp_info is not valid.
319 */
320 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
321 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
322 !bp_info_rsp->hdr.is_resp) ? false : true;
323 }
324
325 static enum cb_err cse_get_bp_info(void)
326 {
327 struct get_bp_info_req {
328 struct mkhi_hdr hdr;
329 uint8_t reserved[4];
330 } __packed;
331
332 struct get_bp_info_req info_req = {
333 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
334 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
335 .reserved = {0},
336 };
337
338 /*
339 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
340 * initialized, check if there is cse bp info response stored in cbmem. Once the data
341 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
342 * functions. In case, data is not available in cbmem or invalid, continue to send the
343 * GET_BOOT_PARTITION_INFO command, else return.
344 */
345 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
346 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
347 if (cse_bp_info_in_cbmem) {
348 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
349 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
350 sizeof(struct get_bp_info_rsp));
351 return CB_SUCCESS;
352 }
353 }
354 } else {
355 /*
356 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
357 * global cse bp info response stored in global cse_bp_info_rsp is valid.
358 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
359 * command, else return.
360 */
361 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
362 return CB_SUCCESS;
363 }
364
365 if (!cse_is_bp_cmd_info_possible()) {
366 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
367 return CB_ERR;
368 }
369
370 size_t resp_size = sizeof(struct get_bp_info_rsp);
371
372 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
373 HECI_MKHI_ADDR)) {
374 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
375 return CB_ERR;
376 }
377
378 if (cse_bp_info_rsp.hdr.result) {
379 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
380 cse_bp_info_rsp.hdr.result);
381 return CB_ERR;
382 }
383
384 cse_print_boot_partition_info();
385 return CB_SUCCESS;
386 }
387
388 void cse_fill_bp_info(void)
389 {
390 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
391 return;
392
393 if (vboot_recovery_mode_enabled())
394 return;
395
396 if (cse_get_bp_info() != CB_SUCCESS)
397 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
398 }
399
400 /* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
401 static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
402 {
403 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
404 return;
405
406 if (vboot_recovery_mode_enabled())
407 return;
408
409 sync_cse_bp_info_to_cbmem();
410 }
411
412 CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
413
414 /*
415 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
416 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
417 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
418 * This function must be used before EOP.
419 * Returns false on failure and true on success.
420 */
421 static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
422 {
423 struct set_boot_partition_info_req {
424 struct mkhi_hdr hdr;
425 uint8_t next_bp;
426 uint8_t reserved[3];
427 } __packed;
428
429 struct set_boot_partition_info_req switch_req = {
430 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
431 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
432 .next_bp = bp,
433 .reserved = {0},
434 };
435
436 if (bp != RO && bp != RW) {
437 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
438 return CB_ERR_ARG;
439 }
440
441 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
442
443 if (!cse_is_bp_cmd_info_possible()) {
444 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
445 return CB_ERR;
446 }
447
448 struct mkhi_hdr switch_resp;
449 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
450
451 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
452 HECI_MKHI_ADDR))
453 return CB_ERR;
454
455 if (switch_resp.result) {
456 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
457 switch_resp.result);
458 return CB_ERR;
459 }
460
461 return CB_SUCCESS;
462 }
463
464 static enum cb_err cse_data_clear_request(void)
465 {
466 struct data_clr_request {
467 struct mkhi_hdr hdr;
468 uint8_t reserved[4];
469 } __packed;
470
471 struct data_clr_request data_clr_rq = {
472 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
473 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
474 .reserved = {0},
475 };
476
477 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
478 cse_get_current_bp() != RO) {
479 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
480 return CB_ERR;
481 }
482
483 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
484
485 struct mkhi_hdr data_clr_rsp;
486 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
487
488 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
489 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
490 return CB_ERR;
491 }
492
493 if (data_clr_rsp.result) {
494 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
495 data_clr_rsp.result);
496 return CB_ERR;
497 }
498
499 return CB_SUCCESS;
500 }
501
502 __weak void cse_board_reset(void)
503 {
504 /* Default weak implementation, does nothing. */
505 }
506
507 __weak void cse_fw_update_misc_oper(void)
508 {
509 /* Default weak implementation, does nothing. */
510 }
511
512 /* Set the CSE's next boot partition and issues system reset */
513 static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
514 {
515 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
516 return CB_ERR;
517
518 /* Allow the board to perform a reset for CSE RO<->RW jump */
519 cse_board_reset();
520
521 /* If board does not perform the reset, then perform global_reset */
522 do_global_reset();
523
524 die("cse_lite: Failed to reset the system\n");
525
526 /* Control never reaches here */
527 return CB_ERR;
528 }
529
530 static enum cb_err cse_boot_to_rw(void)
531 {
532 if (cse_get_current_bp() == RW)
533 return CB_SUCCESS;
534
535 return cse_set_and_boot_from_next_bp(RW);
536 }
537
538 /* Check if CSE RW data partition is valid or not */
539 static bool cse_is_rw_dp_valid(void)
540 {
541 const struct cse_bp_entry *rw_bp;
542
543 rw_bp = cse_get_bp_entry(RW);
544 return rw_bp->status != BP_STATUS_DATA_FAILURE;
545 }
546
547 /*
548 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
549 * otherwise false if any operation fails.
550 */
551 static enum cb_err cse_fix_data_failure_err(void)
552 {
553 /*
554 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
555 * - Send DATA CLEAR HECI command to CSE
556 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
557 * - Issue GLOBAL RESET HECI command.
558 */
559 if (cse_is_rw_dp_valid())
560 return CB_SUCCESS;
561
562 if (cse_data_clear_request() != CB_SUCCESS)
563 return CB_ERR;
564
565 return cse_boot_to_rw();
566 }
567
568 static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
569 {
570 const struct cse_bp_entry *cse_bp;
571
572 cse_bp = cse_get_bp_entry(bp);
573 return &cse_bp->fw_ver;
574 }
575
576 static const struct fw_version *cse_get_rw_version(void)
577 {
578 return cse_get_bp_entry_version(RW);
579 }
580
581 static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
582 uint32_t *end_offset)
583 {
584 const struct cse_bp_entry *cse_bp;
585
586 cse_bp = cse_get_bp_entry(bp);
587
588 if (start_offset)
589 *start_offset = cse_bp->start_offset;
590
591 if (end_offset)
592 *end_offset = cse_bp->end_offset;
593 }
594
595 static bool cse_is_rw_bp_status_valid(void)
596 {
597 const struct cse_bp_entry *rw_bp;
598
599 rw_bp = cse_get_bp_entry(RW);
600
601 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
602 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
603 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
604 return false;
605 }
606 return true;
607 }
608
609 static enum cb_err cse_boot_to_ro(void)
610 {
611 if (cse_get_current_bp() == RO)
612 return CB_SUCCESS;
613
614 return cse_set_and_boot_from_next_bp(RO);
615 }
616
617 static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
618 {
619 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
620 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
621 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
622 return CB_ERR;
623 }
624
625 return CB_SUCCESS;
626 }
627
628 static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
629 {
630 uint32_t cse_bp_sign;
631
632 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
633 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
634 return false;
635 }
636
637 return cse_bp_sign == CSE_RW_SIGNATURE;
638 }
639
640 static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
641 {
642 struct region_device cse_region_rdev;
643 size_t size;
644 uint32_t start_offset;
645 uint32_t end_offset;
646
647 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
648 return CB_ERR;
649
650 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
651 size = end_offset + 1 - start_offset;
652
653 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
654 return CB_ERR;
655
656 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
657 (uint32_t)start_offset, (uint32_t)size);
658
659 return CB_SUCCESS;
660 }
661
662 /*
663 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
664 * In case of CSE component comparison:
665 * If ver_cmp_status = 0, no update is required
666 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
667 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
668 */
669 static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
670 {
671 if (a->major != b->major)
672 return a->major - b->major;
673 else if (a->minor != b->minor)
674 return a->minor - b->minor;
675 else if (a->hotfix != b->hotfix)
676 return a->hotfix - b->hotfix;
677 else
678 return a->build - b->build;
679 }
680
681 static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
682 {
683 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
684 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
685 return CB_ERR;
686 }
687 return CB_SUCCESS;
688 }
689
690 static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
691 size_t offset, size_t size)
692 {
693 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
694 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
695 return CB_ERR;
696 }
697
698 return CB_SUCCESS;
699 }
700
701 enum cse_update_status {
702 CSE_UPDATE_NOT_REQUIRED,
703 CSE_UPDATE_UPGRADE,
704 CSE_UPDATE_DOWNGRADE,
705 CSE_UPDATE_CORRUPTED,
706 CSE_UPDATE_METADATA_ERROR,
707 };
708
709 static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
710 {
711 if ((*curr - start) >= size) {
712 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
713 return false;
714 }
715
716 *ver_field = skip_atoi(curr);
717 (*curr)++;
718 return true;
719 }
720
721 static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
722 {
723 char *version_str, *cbfs_ptr;
724 size_t size;
725
726 if (cbfs_rw_version == NULL)
727 return CB_ERR;
728
729 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
730 version_str = cbfs_ptr;
731 if (!version_str) {
732 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
733 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
734 return CB_ERR;
735 }
736
737 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
738 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
739 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
740 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
741 cbfs_unmap(version_str);
742 return CB_ERR;
743 }
744
745 cbfs_unmap(version_str);
746 return CB_SUCCESS;
747 }
748
749 static void cmos_update_cse_sync_performed(void)
750 {
751 if (!CONFIG(VBOOT) || !vboot_is_gbb_flag_set(VB2_GBB_FLAG_FORCE_CSE_SYNC))
752 return;
753
754 struct cse_specific_info cse_info_in_cmos;
755 cmos_read_fw_partition_info(&cse_info_in_cmos);
756
757 /* Update CSE sync as performed */
758 cse_info_in_cmos.cse_sync_status |= CSE_ENFORCED_SYNC_PERFORMED;
759
760 /* Update the CRC */
761 store_cse_info_crc(&cse_info_in_cmos);
762
763 /* Update CSE specific info at CMOS */
764 cmos_write_fw_partition_info(&cse_info_in_cmos);
765 }
766
767 static void cmos_update_cse_sync_status(void)
768 {
769 if (!CONFIG(VBOOT) || !vboot_is_gbb_flag_set(VB2_GBB_FLAG_FORCE_CSE_SYNC))
770 return;
771
772 /*
773 * If the pertinent GBB flag is set, the Enforce CSE sync sequence proceeds as follows:
774 * 1) Request forced sync (set CSE_FORCED_SYNC_REQUEST):
775 * CSE_FORCED_SYNC_REQUEST is set during the "CSE sync status check" only if CSE
776 * sync has not yet been performed.
777 * It is possible to have duplicate CSE status checks, for example, if eSOL tries
778 * to make an early decision to display a screen for the CSE update, or if the
779 * device switches from an RW partition to RO before sync. These duplicate
780 * requests will set the request flag again, effectively will not alter the state
781 * machines.
782 * 2) Sync is performed (set CSE_FORCED_SYNC_PERFORMED):
783 * AP will set CSE_FORCED_SYNC_PERFORMED upon successful CSE sync.
784 * 3) Reset forced sync Request (reset CSE_FORCED_SYNC_REQUEST):
785 * Once the CSE sync is performed and the CSE_FORCED_SYNC_PERFORMED flag is set.
786 * For eSOL decision, AP will request a "CSE sync status check" on the subsequent
787 * boot. As the CSE sync request is honoured, CSE_FORCED_SYNC_REQUEST is reset.
788 * 4) Resert Sync performed flag (reset CSE_FORCED_SYNC_PERFORMED):
789 * Another "CSE sync status check" occurs during the CSE sync process itself. The
790 * AP then resets CSE_FORCED_SYNC_PERFORMED to signify the completion of the
791 * entire CSE sync cycle.
792 *
793 * It is important to avoid clearing both flags in step 3, as the process is not yet
794 * finished. Doing so would cause boot loop as the AP will interpret a subsequent "CSE
795 * sync status check" as a fresh sync request.
796 */
797
798 struct cse_specific_info cse_info_in_cmos;
799 cmos_read_fw_partition_info(&cse_info_in_cmos);
800
801 /* Reset flags if Enforced CSE sync is performed; else, request sync.*/
802 if (cse_info_in_cmos.cse_sync_status & CSE_ENFORCED_SYNC_PERFORMED) {
803 if (cse_info_in_cmos.cse_sync_status & CSE_ENFORCED_SYNC_REQUEST)
804 cse_info_in_cmos.cse_sync_status &= ~(CSE_ENFORCED_SYNC_REQUEST);
805 else
806 cse_info_in_cmos.cse_sync_status &= ~(CSE_ENFORCED_SYNC_PERFORMED);
807 } else {
808 cse_info_in_cmos.cse_sync_status |= CSE_ENFORCED_SYNC_REQUEST;
809 }
810
811 /* Update the CRC */
812 store_cse_info_crc(&cse_info_in_cmos);
813
814 /* Update CSE specific info at CMOS */
815 cmos_write_fw_partition_info(&cse_info_in_cmos);
816 }
817
818 static bool is_cse_sync_enforced(void)
819 {
820 if (CONFIG(VBOOT) && vboot_is_gbb_flag_set(VB2_GBB_FLAG_FORCE_CSE_SYNC)) {
821
822 struct cse_specific_info cse_info_in_cmos;
823 cmos_read_fw_partition_info(&cse_info_in_cmos);
824
825 return cse_info_in_cmos.cse_sync_status & CSE_ENFORCED_SYNC_REQUEST;
826 }
827 return false;
828 }
829
830 static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
831 {
832 int ret;
833 struct fw_version cbfs_rw_version;
834
835 if (!cse_is_rw_bp_sign_valid(target_rdev))
836 return CSE_UPDATE_CORRUPTED;
837
838 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
839 return CSE_UPDATE_METADATA_ERROR;
840
841 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
842 cbfs_rw_version.major,
843 cbfs_rw_version.minor,
844 cbfs_rw_version.hotfix,
845 cbfs_rw_version.build);
846
847 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
848 if (ret == 0) {
849 /* Update the CMOS status flag enforced CSE sync */
850 cmos_update_cse_sync_status();
851
852 if (is_cse_sync_enforced()) {
853 printk(BIOS_WARNING, "Force CSE Firmware upgrade for Autotest\n");
854 return CSE_UPDATE_UPGRADE;
855 }
856 return CSE_UPDATE_NOT_REQUIRED;
857 } else {
858 if (ret < 0)
859 return CSE_UPDATE_DOWNGRADE;
860 else
861 return CSE_UPDATE_UPGRADE;
862 }
863 }
864
865 static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
866 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
867 {
868 /* Points to CSE CBFS RW image after boot partition signature */
869 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
870
871 /* Size of CSE CBFS RW image without boot partition signature */
872 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
873
874 /* Update except CSE RW signature */
875 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
876 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
877 return CB_ERR;
878
879 /* Update CSE RW signature to indicate update is complete */
880 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
881 return CB_ERR;
882
883 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
884 elog_add_event_byte(ELOG_TYPE_FW_CSE_SYNC, ENV_RAMSTAGE ? ELOG_FW_POST_RAM_CSE_SYNC :
885 ELOG_FW_PRE_RAM_CSE_SYNC);
886 return CB_SUCCESS;
887 }
888
889 static bool is_cse_fw_update_enabled(void)
890 {
891 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
892 return false;
893
894 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
895 return false;
896
897 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
898 return !is_debug_cse_fw_update_disable();
899
900 return true;
901 }
902
903 static enum csme_failure_reason cse_update_rw(const void *cse_cbfs_rw, const size_t cse_blob_sz,
904 struct region_device *target_rdev)
905 {
906 if (region_device_sz(target_rdev) < cse_blob_sz) {
907 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
908 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
909 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
910 }
911
912 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
913 return CSE_LITE_SKU_FW_UPDATE_ERROR;
914
915 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
916 return CSE_LITE_SKU_FW_UPDATE_ERROR;
917
918 cmos_update_cse_sync_performed();
919 return CSE_NO_ERROR;
920 }
921
922 static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
923 {
924 if (status == CSE_UPDATE_CORRUPTED)
925 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
926 /*
927 * To set CSE's operation mode to HMRFPO mode:
928 * 1. Ensure CSE to boot from RO(BP1)
929 * 2. Send HMRFPO_ENABLE command to CSE
930 */
931 if (cse_boot_to_ro() != CB_SUCCESS)
932 return CB_ERR;
933
934 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
935 /* Reset the PSR backup command status in CMOS */
936 if (CONFIG(SOC_INTEL_CSE_LITE_PSR))
937 update_psr_backup_status(PSR_BACKUP_PENDING);
938
939 if (cse_data_clear_request() != CB_SUCCESS) {
940 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
941 return CB_ERR;
942 }
943 }
944
945 return cse_hmrfpo_enable();
946 }
947
948 static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
949 struct region_device *target_rdev)
950 {
951 enum csme_failure_reason rv;
952 void *cse_cbfs_rw = NULL;
953 size_t size;
954
955 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
956 cse_cbfs_rw = cbfs_cbmem_alloc(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME,
957 CBMEM_ID_CSE_UPDATE, &size);
958 } else {
959 cse_cbfs_rw = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
960 }
961
962 if (!cse_cbfs_rw) {
963 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
964 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
965 }
966
967 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
968 rv = CSE_COMMUNICATION_ERROR;
969 goto error_exit;
970 }
971
972 cse_fw_update_misc_oper();
973 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
974
975 error_exit:
976 cbfs_unmap(cse_cbfs_rw);
977 return rv;
978 }
979
980 static bool is_psr_data_backed_up(void)
981 {
982 /* Track PSR backup status in CMOS */
983 return (get_psr_backup_status() == PSR_BACKUP_DONE);
984 }
985
986 static bool is_psr_supported(void)
987 {
988 uint32_t feature_status;
989
990 /*
991 * Check if SoC has support for PSR feature typically PSR feature
992 * is only supported by vpro SKU
993 *
994 */
995 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
996 printk(BIOS_ERR, "cse_get_fw_feature_state command failed !\n");
997 return false;
998 }
999
1000 if (!(feature_status & ME_FW_FEATURE_PSR)) {
1001 printk(BIOS_DEBUG, "PSR is not supported in this SKU !\n");
1002 return false;
1003 }
1004
1005 return true;
1006 }
1007
1008 /*
1009 * PSR data needs to be backed up prior to downgrade. So switch the CSE boot mode to RW, send
1010 * PSR back-up command to CSE and update the PSR back-up state in CMOS.
1011 */
1012 static void backup_psr_data(void)
1013 {
1014 printk(BIOS_DEBUG, "cse_lite: Initiate PSR data backup flow\n");
1015 /* Switch CSE to RW to send PSR_HECI_FW_DOWNGRADE_BACKUP command */
1016 if (cse_boot_to_rw() != CB_SUCCESS) {
1017 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
1018 goto update_and_exit;
1019 }
1020 /*
1021 * The function to check for PSR feature support can only be called after
1022 * switching to RW partition. The command MKHI_FWCAPS_GET_FW_FEATURE_STATE
1023 * that gives feature state is supported by a process that is loaded only
1024 * when CSE boots from RW.
1025 *
1026 */
1027 if (!is_psr_supported())
1028 goto update_and_exit;
1029
1030 /*
1031 * Prerequisites:
1032 * 1) HFSTS1 Current Working State is Normal
1033 * 2) HFSTS1 Current Operation Mode is Normal
1034 */
1035 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
1036 printk(BIOS_DEBUG, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1037 "prerequisites not met!\n");
1038 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
1039 goto update_and_exit;
1040 }
1041
1042 /* Send PSR_HECI_FW_DOWNGRADE_BACKUP command */
1043 struct psr_heci_fw_downgrade_backup_req {
1044 struct psr_heci_header header;
1045 } __packed;
1046
1047 struct psr_heci_fw_downgrade_backup_req req = {
1048 .header.command = PSR_HECI_FW_DOWNGRADE_BACKUP,
1049 };
1050
1051 struct psr_heci_fw_downgrade_backup_res {
1052 struct psr_heci_header header;
1053 uint32_t status;
1054 } __packed;
1055
1056 struct psr_heci_fw_downgrade_backup_res backup_psr_resp;
1057 size_t resp_size = sizeof(backup_psr_resp);
1058
1059 printk(BIOS_DEBUG, "cse_lite: Send PSR_HECI_FW_DOWNGRADE_BACKUP command\n");
1060 if (heci_send_receive(&req, sizeof(req),
1061 &backup_psr_resp, &resp_size, HECI_PSR_ADDR)) {
1062 printk(BIOS_ERR, "cse_lite: could not backup PSR data\n");
1063 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP, ELOG_PSR_DATA_BACKUP_FAILED);
1064 } else {
1065 if (backup_psr_resp.status != PSR_STATUS_SUCCESS) {
1066 printk(BIOS_ERR, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1067 "returned %u\n", backup_psr_resp.status);
1068 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1069 ELOG_PSR_DATA_BACKUP_FAILED);
1070 } else {
1071 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1072 ELOG_PSR_DATA_BACKUP_SUCCESS);
1073 }
1074 }
1075
1076 update_and_exit:
1077 /*
1078 * An attempt to send PSR back-up command has been made. Update this info in CMOS and
1079 * send success once backup_psr_data() has been called. We do not want to put the system
1080 * into recovery for PSR data backup command pre-requisites not being met.
1081 * We cannot do much if CSE fails to backup the PSR data, except create an event log.
1082 */
1083 update_psr_backup_status(PSR_BACKUP_DONE);
1084 }
1085
1086 static void initiate_psr_data_backup(void)
1087 {
1088 if (is_psr_data_backed_up())
1089 return;
1090
1091 backup_psr_data();
1092 }
1093
1094 /*
1095 * Check if a CSE Firmware update is required
1096 * returns true if an update is required, false otherwise
1097 */
1098 bool is_cse_fw_update_required(void)
1099 {
1100 struct fw_version cbfs_rw_version;
1101
1102 if (!is_cse_fw_update_enabled())
1103 return false;
1104
1105 /*
1106 * First, check if cse_bp_info_rsp global structure is populated.
1107 * If not, it implies that cse_fill_bp_info() function is not called.
1108 */
1109 if (!is_cse_bp_info_valid(&cse_bp_info_rsp))
1110 return false;
1111
1112 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
1113 return false;
1114
1115 /* Update the CMOS status flag enforced CSE sync */
1116 cmos_update_cse_sync_status();
1117
1118 /* Check if CSE sync is enforced */
1119 if (is_cse_sync_enforced()) {
1120 return true;
1121 }
1122 return !!cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
1123 }
1124
1125 bool is_cse_boot_to_rw(void)
1126 {
1127 if (cse_get_bp_info() != CB_SUCCESS) {
1128 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
1129 return false;
1130 }
1131
1132 if (cse_get_current_bp() == RW)
1133 return true;
1134
1135 return false;
1136 }
1137
1138 static uint8_t cse_fw_update(void)
1139 {
1140 struct region_device target_rdev;
1141 enum cse_update_status status;
1142
1143 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
1144 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1145 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1146 }
1147
1148 status = cse_check_update_status(&target_rdev);
1149 if (status == CSE_UPDATE_NOT_REQUIRED)
1150 return CSE_NO_ERROR;
1151 if (status == CSE_UPDATE_METADATA_ERROR)
1152 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
1153 if (CONFIG(SOC_INTEL_CSE_LITE_PSR) && status == CSE_UPDATE_DOWNGRADE)
1154 initiate_psr_data_backup();
1155
1156 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
1157 return cse_trigger_fw_update(status, &target_rdev);
1158 }
1159
1160 static const char *cse_sub_part_str(enum bpdt_entry_type type)
1161 {
1162 switch (type) {
1163 case IOM_FW:
1164 return "IOM";
1165 case NPHY_FW:
1166 return "NPHY";
1167 default:
1168 return "Unknown";
1169 }
1170 }
1171
1172 static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
1173 {
1174 struct region_device cse_region_rdev;
1175 uint32_t size;
1176 uint32_t start_offset;
1177 uint32_t end_offset;
1178
1179 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1180 return CB_ERR;
1181
1182 if (!strcmp(cse_regions[bp], "RO"))
1183 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
1184 else
1185 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
1186
1187 size = end_offset + 1 - start_offset;
1188
1189 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
1190 return CB_ERR;
1191
1192 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1193 cse_regions[bp], start_offset, size);
1194 return CB_SUCCESS;
1195 }
1196
1197 static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1198 enum bpdt_entry_type type)
1199 {
1200 struct bpdt_header bpdt_hdr;
1201 struct region_device cse_rdev;
1202 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1203 uint8_t i;
1204
1205 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
1206 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1207 cse_regions[bp]);
1208 return CB_ERR;
1209 }
1210
1211 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1212 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
1213 return CB_ERR;
1214 }
1215
1216 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1217 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1218 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1219 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
1220 return CB_ERR;
1221 }
1222
1223 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1224 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1225 if (bpdt_entries[i].type == type) {
1226 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1227 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1228 bpdt_entries[i].size);
1229
1230 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1231 bpdt_entries[i].size))
1232 return CB_ERR;
1233 else
1234 return CB_SUCCESS;
1235 }
1236 }
1237
1238 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
1239 return CB_ERR;
1240 }
1241
1242 static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
1243 const struct region_device *rdev,
1244 struct fw_version *fw_ver)
1245 {
1246 struct subpart_entry subpart_entry;
1247 struct subpart_entry_manifest_header man_hdr;
1248
1249 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1250 != SUBPART_ENTRY_SZ) {
1251 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1252 cse_sub_part_str(type));
1253 return CB_ERR;
1254 }
1255
1256 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1257 != SUBPART_MANIFEST_HDR_SZ) {
1258 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1259 cse_sub_part_str(type));
1260 return CB_ERR;
1261 }
1262
1263 fw_ver->major = man_hdr.binary_version.major;
1264 fw_ver->minor = man_hdr.binary_version.minor;
1265 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1266 fw_ver->build = man_hdr.binary_version.build;
1267
1268 return CB_SUCCESS;
1269 }
1270
1271 static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1272 {
1273 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1274 struct subpart_entry *subpart_entry;
1275 struct subpart_entry_manifest_header *man_hdr;
1276
1277 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1278 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
1279
1280 fw_ver->major = man_hdr->binary_version.major;
1281 fw_ver->minor = man_hdr->binary_version.minor;
1282 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1283 fw_ver->build = man_hdr->binary_version.build;
1284 }
1285
1286 static enum cb_err cse_prep_for_component_update(void)
1287 {
1288 /*
1289 * To set CSE's operation mode to HMRFPO mode:
1290 * 1. Ensure CSE to boot from RO(BP1)
1291 * 2. Send HMRFPO_ENABLE command to CSE
1292 */
1293 if (cse_boot_to_ro() != CB_SUCCESS)
1294 return CB_ERR;
1295
1296 return cse_hmrfpo_enable();
1297 }
1298
1299 static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1300 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
1301 struct region_device *target_rdev)
1302 {
1303 if (region_device_sz(target_rdev) < blob_sz) {
1304 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1305 "smaller than blob size:%zx, abort update\n",
1306 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1307 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1308 }
1309
1310 /* Erase CSE Lite sub-partition */
1311 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
1312 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1313
1314 /* Update CSE Lite sub-partition */
1315 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
1316 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1317
1318 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1319 cse_sub_part_str(type));
1320
1321 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1322 }
1323
1324 static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
1325 {
1326 switch (rv) {
1327 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1328 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1329 return rv;
1330 default:
1331 cse_trigger_vboot_recovery(rv);
1332 }
1333 /* Control never reaches here */
1334 return rv;
1335 }
1336
1337 static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
1338 const char *name)
1339 {
1340 struct region_device target_rdev;
1341 struct fw_version target_fw_ver, source_fw_ver;
1342 enum csme_failure_reason rv;
1343 size_t size;
1344
1345 void *subpart_cbfs_rw = cbfs_map(name, &size);
1346 if (!subpart_cbfs_rw) {
1347 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1348 cse_sub_part_str(type));
1349 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1350 }
1351
1352 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1353 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1354 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1355 source_fw_ver.build);
1356
1357 /* Trigger sub-partition update in CSE RO and CSE RW */
1358 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
1359 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
1360 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1361 goto error_exit;
1362 }
1363
1364 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
1365 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1366 goto error_exit;
1367 }
1368
1369 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1370 cse_sub_part_str(type), target_fw_ver.major,
1371 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1372
1373 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1374 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1375 cse_regions[bp], cse_sub_part_str(type));
1376 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1377 continue;
1378 }
1379
1380 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1381 cse_sub_part_str(type));
1382
1383 if (cse_prep_for_component_update() != CB_SUCCESS) {
1384 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1385 goto error_exit;
1386 }
1387
1388 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1389 size, &target_rdev);
1390
1391 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1392 goto error_exit;
1393 }
1394 error_exit:
1395 cbfs_unmap(subpart_cbfs_rw);
1396 return rv;
1397 }
1398
1399 static enum csme_failure_reason cse_sub_part_fw_update(void)
1400 {
1401 if (skip_cse_sub_part_update()) {
1402 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1403 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1404 }
1405
1406 enum csme_failure_reason rv;
1407 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1408
1409 handle_cse_sub_part_fw_update_rv(rv);
1410
1411 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1412
1413 return handle_cse_sub_part_fw_update_rv(rv);
1414 }
1415
1416 static void do_cse_fw_sync(void)
1417 {
1418 /*
1419 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1420 * is not enabled and continue to update CSE sub-partitions.
1421 */
1422 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
1423 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
1424 return;
1425 }
1426
1427 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1428 if (!cse_is_hfs3_fw_sku_lite()) {
1429 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
1430 return;
1431 }
1432
1433 if (cse_get_bp_info() != CB_SUCCESS) {
1434 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
1435
1436 /* If system is in recovery mode, don't trigger recovery again */
1437 if (!vboot_recovery_mode_enabled()) {
1438 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1439 } else {
1440 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1441 "so no action\n");
1442 return;
1443 }
1444 }
1445
1446 /* Store the CSE RW Firmware Version into CBMEM */
1447 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1448 cse_store_rw_fw_version();
1449
1450 /*
1451 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
1452 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
1453 * fails during recovery, just continue to boot.
1454 */
1455 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1456 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1457 cse_board_reset();
1458 do_global_reset();
1459 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1460 }
1461
1462 return;
1463 }
1464
1465 if (cse_fix_data_failure_err() != CB_SUCCESS)
1466 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
1467
1468 /*
1469 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
1470 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1471 * RW metadata or CSE CBFS RW blob is not available.
1472 */
1473 if (is_cse_fw_update_enabled()) {
1474 uint8_t rv;
1475 rv = cse_fw_update();
1476 if (rv)
1477 cse_trigger_vboot_recovery(rv);
1478 }
1479
1480 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1481 cse_sub_part_fw_update();
1482
1483 if (!cse_is_rw_bp_status_valid())
1484 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
1485
1486 if (cse_boot_to_rw() != CB_SUCCESS) {
1487 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
1488 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
1489 }
1490 }
1491
1492 void cse_fw_sync(void)
1493 {
1494 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1495 return;
1496
1497 timestamp_add_now(TS_CSE_FW_SYNC_START);
1498 do_cse_fw_sync();
1499 timestamp_add_now(TS_CSE_FW_SYNC_END);
1500 }
1501
1502 static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1503 struct fw_version_resp *resp)
1504 {
1505 enum cse_tx_rx_status ret;
1506 struct fw_version_msg {
1507 struct mkhi_hdr hdr;
1508 enum fpt_partition_id partition_id;
1509 } __packed msg = {
1510 .hdr = {
1511 .group_id = MKHI_GROUP_ID_GEN,
1512 .command = GEN_GET_IMAGE_FW_VERSION,
1513 },
1514 .partition_id = id,
1515 };
1516
1517 /*
1518 * Prerequisites:
1519 * 1) HFSTS1 CWS is Normal
1520 * 2) HFSTS1 COM is Normal
1521 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1522 */
1523
1524 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1525 !cse_is_hfs1_com_normal()) {
1526 printk(BIOS_ERR,
1527 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1528 return CB_ERR;
1529 }
1530
1531 size_t resp_size = sizeof(struct fw_version_resp);
1532 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1533
1534 if (ret || resp->hdr.result) {
1535 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1536 id, resp->hdr.result);
1537 return CB_ERR;
1538 }
1539
1540 return CB_SUCCESS;
1541 }
1542
1543 static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1544 struct fw_version_resp *resp)
1545 {
1546 if (vboot_recovery_mode_enabled()) {
1547 printk(BIOS_WARNING,
1548 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1549 return CB_ERR;
1550 }
1551
1552 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1553 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1554 return CB_ERR;
1555 }
1556
1557 return send_get_fpt_partition_info_cmd(id, resp);
1558 }
1559
1560 static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1561 {
1562 const struct fw_version invalid_fw = {0, 0, 0, 0};
1563 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1564 return false;
1565 return true;
1566 }
1567
1568 /*
1569 * Helper function to read ISH version from CSE FPT using HECI command.
1570 *
1571 * The HECI command only be executed after memory has been initialized.
1572 * This is because the command relies on resources that are not available
1573 * until DRAM initialization command has been sent.
1574 */
1575 static void store_ish_version(void)
1576 {
1577 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1578 return;
1579
1580 if (!ENV_RAMSTAGE)
1581 return;
1582
1583 if (vboot_recovery_mode_enabled())
1584 return;
1585
1586 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1587 if (cse_info_in_cbmem == NULL)
1588 return;
1589
1590 struct cse_specific_info cse_info_in_cmos;
1591 cmos_read_fw_partition_info(&cse_info_in_cmos);
1592
1593 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1594 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
1595
1596 /* Get current cse firmware state */
1597 enum cse_fw_state fw_state = get_cse_state(
1598 &(cbmem_version->cur_cse_fw_version),
1599 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1600 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
1601
1602 if (fw_state == CSE_FW_WARM_BOOT) {
1603 return;
1604 } else {
1605 if (fw_state == CSE_FW_COLD_BOOT &&
1606 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1607 /* CMOS data is persistent across cold boots */
1608 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1609 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1610 sizeof(struct cse_fw_ish_version_info));
1611 store_cse_info_crc(cse_info_in_cbmem);
1612 } else {
1613 /*
1614 * Current running CSE version is different than previous stored CSE version
1615 * which could be due to CSE update or rollback, hence, need to send ISHC
1616 * partition info cmd to know the currently running ISH version.
1617 */
1618 struct fw_version_resp resp;
1619 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1620 &resp) == CB_SUCCESS) {
1621 /* Update stored CSE version with current cse version */
1622 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1623 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
1624
1625 /* Retrieve and update current ish version */
1626 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1627 &(resp.manifest_data.version), sizeof(struct fw_version));
1628
1629 /* Update the CRC */
1630 store_cse_info_crc(cse_info_in_cbmem);
1631
1632 /* Update CMOS with current CSE FPT versions.*/
1633 cmos_write_fw_partition_info(cse_info_in_cbmem);
1634 }
1635 }
1636 }
1637 }
1638
1639 static void ramstage_cse_misc_ops(void *unused)
1640 {
1641 if (acpi_get_sleep_type() == ACPI_S3)
1642 return;
1643
1644 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
1645 cse_fw_sync();
1646
1647 /*
1648 * Store the ISH RW Firmware Version into CBMEM if ISH partition
1649 * is available
1650 */
1651 if (!CONFIG(DRIVER_INTEL_ISH_HAS_MAIN_FW) && soc_is_ish_partition_enabled())
1652 store_ish_version();
1653 }
1654
1655 BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);