blob: bd06bbf0bae7336041b939e5c7484225a79ad500 [file] [log] [blame]
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301/* SPDX-License-Identifier: GPL-2.0-only */
Elyes HAOUAS944da482021-02-01 21:30:13 +01002
Krishna P Bhat De3178a12022-04-21 16:40:06 +05303#include <acpi/acpi.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +05304#include <bootstate.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05305#include <cbfs.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05306#include <commonlib/region.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +01007#include <console/console.h>
8#include <cpu/cpu.h>
Subrata Banik65a6d172023-08-13 13:03:50 +00009#include <crc_byte.h>
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +053010#include <elog.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053011#include <fmap.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010012#include <intelbasecode/debug_feature.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053013#include <intelblocks/cse.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053014#include <intelblocks/cse_layout.h>
Subrata Banikda527ec2022-11-24 15:42:35 +053015#include <intelblocks/spi.h>
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +053016#include <intelblocks/systemagent.h>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053017#include <security/vboot/misc.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010018#include <security/vboot/vboot_common.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053019#include <soc/intel/common/reset.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +053020#include <timestamp.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053021
Dinesh Gehlota9232d82023-06-10 11:53:47 +000022#include "cse_lite_cmos.h"
23
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053024#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
25#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
26#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
27#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
28#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053029
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053030/* Converts bp index to boot partition string */
31#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
32
Rizwan Qureshiec321092019-09-06 20:28:43 +053033/* CSE RW boot partition signature */
34#define CSE_RW_SIGNATURE 0x000055aa
35
36/* CSE RW boot partition signature size */
37#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
38
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053039/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053040 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053041 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053042 * CSE Lite SKU Image Layout:
Angel Pons2e8e0602022-08-13 19:50:28 +020043 * +------------+ +----+------+----+ +-----+------+-----+
44 * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 |
45 * +------------+ +----+------+----+ +-----+------+-----+
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053046 */
47#define CSE_MAX_BOOT_PARTITIONS 3
48
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053049/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053050enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053051 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053052 RO = 0,
53
Rizwan Qureshiec321092019-09-06 20:28:43 +053054 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053055 RW = 1
56};
57
58/*
59 * Boot partition status.
60 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
61 */
62enum bp_status {
63 /* This value is returned when a partition has no errors */
64 BP_STATUS_SUCCESS = 0,
65
66 /*
67 * This value is returned when a partition should be present based on layout, but it is
68 * not valid.
69 */
70 BP_STATUS_GENERAL_FAILURE = 1,
71
72 /* This value is returned when a partition is not present per initial image layout */
73 BP_STATUS_PARTITION_NOT_PRESENT = 2,
74
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053075 /*
76 * This value is returned when unexpected issues are detected in CSE Data area
77 * and CSE TCB-SVN downgrade scenario.
78 */
79 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053080};
81
82/*
83 * Boot Partition Info Flags
84 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
85 */
86enum bp_info_flags {
87
88 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
89 BP_INFO_REDUNDANCY_EN = 1 << 0,
90
91 /* It indicates RO(BP1) supports Minimal Recovery Mode */
92 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
93
94 /*
95 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
96 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
97 */
98 BP_INFO_READ_ONLY_CFG = 1 << 2,
99};
100
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530101/* CSE boot partition entry info */
102struct cse_bp_entry {
103 /* Boot partition version */
104 struct fw_version fw_ver;
105
106 /* Boot partition status */
107 uint32_t status;
108
109 /* Starting offset of the partition within CSE region */
110 uint32_t start_offset;
111
112 /* Ending offset of the partition within CSE region */
113 uint32_t end_offset;
114 uint8_t reserved[12];
115} __packed;
116
117/* CSE boot partition info */
118struct cse_bp_info {
119 /* Number of boot partitions */
120 uint8_t total_number_of_bp;
121
122 /* Current boot partition */
123 uint8_t current_bp;
124
125 /* Next boot partition */
126 uint8_t next_bp;
127
128 /* Boot Partition Info Flags */
129 uint8_t flags;
130
131 /* Boot Partition Entry Info */
132 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
133} __packed;
134
135struct get_bp_info_rsp {
136 struct mkhi_hdr hdr;
137 struct cse_bp_info bp_info;
138} __packed;
139
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530140static struct get_bp_info_rsp cse_bp_info_rsp;
141
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000142enum cse_fw_state {
143 /* The CMOS and CBMEM have the current fw version. */
144 CSE_FW_WARM_BOOT,
145
146 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
147 CSE_FW_COLD_BOOT,
148
149 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
150 CSE_FW_INVALID,
151};
152
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530153static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700154
Subrata Banik65a6d172023-08-13 13:03:50 +0000155static struct cse_specific_info cse_info;
156
Subrata Banikda527ec2022-11-24 15:42:35 +0530157void cse_log_ro_write_protection_info(bool mfg_mode)
158{
159 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
160
161 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
162 cse_ro_wp_en ? "YES" : "NO");
163
164 if (cse_ro_wp_en) {
165 uint32_t base, limit;
166 spi_get_wp_cse_ro_range(&base, &limit);
167 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
168 base, limit);
169 }
170
171 /*
172 * If manufacturing mode is disabled, but CSE RO is not write protected,
173 * log error.
174 */
175 if (!mfg_mode && !cse_ro_wp_en)
176 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
177}
178
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530179enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700180{
181 struct cse_boot_perf_req {
182 struct mkhi_hdr hdr;
183 uint32_t reserved;
184 } __packed;
185
186 struct cse_boot_perf_req req = {
187 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
188 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
189 .reserved = 0,
190 };
191
192 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
193
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530194 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700195 HECI_MKHI_ADDR)) {
196 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530197 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700198 }
199
200 if (boot_perf_rsp->hdr.result) {
201 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
202 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530203 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700204 }
205
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530206 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700207}
208
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530209static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530210{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530211 return &cse_bp_info_rsp.bp_info;
212}
213
214static uint8_t cse_get_current_bp(void)
215{
216 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530217 return cse_bp_info->current_bp;
218}
219
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530220static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530221{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530222 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530223 return &cse_bp_info->bp_entries[bp];
224}
225
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000226static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000227{
228 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
229
230 /*
231 * Authenticate the CBMEM persistent data.
232 *
233 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
234 * could change the values stored in this region has to also trigger the global
235 * reset. Hence, CBMEM persistent data won't be available any time after such
236 * event (global reset or cold reset) being initiated.
237 *
238 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
239 * want to override the existing data in CBMEM to avoid any additional boot latency.
240 */
241 if (info->crc != crc)
242 return false;
243
244 return true;
245}
246
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000247static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000248{
249 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
250}
251
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000252static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
253 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
254{
255 enum cse_fw_state state = CSE_FW_WARM_BOOT;
256 size_t size = sizeof(struct fw_version);
257 /*
258 * Compare if stored CSE version (from the previous boot) is same as current
259 * running CSE version.
260 */
261 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
262 /*
263 * CMOS CSE versioin is invalid, possibly two scenarios
264 * 1. CSE FW update
265 * 2. First boot
266 */
267 state = CSE_FW_INVALID;
268 } else {
269 /*
270 * Check if current running CSE version is same as previous stored CSE
271 * version aka CBMEM region is still valid.
272 */
273 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
274 state = CSE_FW_COLD_BOOT;
275 }
276 return state;
277}
278
Subrata Banik65a6d172023-08-13 13:03:50 +0000279/*
280 * Helper function that stores current CSE firmware version to CBMEM memory,
281 * except during recovery mode.
282 */
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530283static void cse_store_rw_fw_version(void)
Subrata Banik65a6d172023-08-13 13:03:50 +0000284{
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530285 const struct cse_bp_entry *cse_bp;
286 cse_bp = cse_get_bp_entry(RW);
287
Subrata Banik65a6d172023-08-13 13:03:50 +0000288 if (vboot_recovery_mode_enabled())
289 return;
290
291 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000292 /* update current CSE version and return */
293 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
294 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000295 return;
296 }
297
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000298 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
299 sizeof(*cse_info_in_cbmem));
300 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000301 return;
302
303 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000304 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000305 return;
306
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000307 struct cse_specific_info cse_info_in_cmos;
308 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000309
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000310 /* Get current cse firmware state */
311 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
312 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
313 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
314
315 /* Reset CBMEM data and update current CSE version */
316 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
317 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
318 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000319
320 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000321 store_cse_info_crc(cse_info_in_cbmem);
322
323 if (fw_state == CSE_FW_INVALID) {
324 /*
325 * Current CMOS data is outdated, which could be due to CSE update or
326 * rollback, hence, need to update CMOS with current CSE FPT versions.
327 */
328 cmos_write_fw_partition_info(cse_info_in_cbmem);
329 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000330}
331
332#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
333/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
334static void preram_cse_info_sync_to_cbmem(int is_recovery)
335{
336 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
337 return;
338
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000339 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
340 sizeof(*cse_info_in_cbmem));
341 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000342 return;
343
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000344 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
345 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000346 return;
347
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000348 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
349 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
350 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000351
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000352 struct cse_specific_info cse_info_in_cmos;
353 cmos_read_fw_partition_info(&cse_info_in_cmos);
354
355 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
356 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
357 sizeof(struct fw_version))) {
358 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
359 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
360 return;
361 }
362
363 /*
364 * Current CMOS data is outdated, which could be due to CSE update or
365 * rollback, hence, need to update CMOS with current CSE FPT versions.
366 */
367 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000368}
369
370CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
371#endif
372
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530373static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530374{
375 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530376 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530377
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530378 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
379 cse_bp_info->total_number_of_bp);
380 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
381 GET_BP_STR(cse_bp_info->current_bp));
382 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
383 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530384
385 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530386 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530387 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530388 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
389 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
390 cse_bp->status, cse_bp->start_offset,
391 cse_bp->end_offset);
392
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530393 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530394 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530395 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
396 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
397 cse_bp->status, cse_bp->start_offset,
398 cse_bp->end_offset);
399}
400
401/*
402 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
403 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
404 * It allows execution of the Boot Partition commands in below scenarios:
405 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
406 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
407 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530408 * The prerequisite check should be handled in cse_get_bp_info() and
409 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
410 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530411 */
412static bool cse_is_bp_cmd_info_possible(void)
413{
414 if (cse_is_hfs1_cws_normal()) {
415 if (cse_is_hfs1_com_normal())
416 return true;
417 if (cse_is_hfs1_com_secover_mei_msg())
418 return true;
419 if (cse_is_hfs1_com_soft_temp_disable())
420 return true;
421 }
422 return false;
423}
424
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530425static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
426{
427 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
428
429 if (cse_bp_info_in_cbmem != NULL)
430 return cse_bp_info_in_cbmem;
431
432 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
433 sizeof(struct get_bp_info_rsp));
434
435 if (!cse_bp_info_in_cbmem) {
436 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
437 return NULL;
438 }
439
440 /* Copy the CSE Boot Partition Info command response to cbmem */
441 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
442
443 return cse_bp_info_in_cbmem;
444}
445
446static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
447{
448 /*
449 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
450 * then return false to indicate cse_bp_info is not valid.
451 */
452 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
453 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
454 !bp_info_rsp->hdr.is_resp) ? false : true;
455}
456
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530457static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530458{
459 struct get_bp_info_req {
460 struct mkhi_hdr hdr;
461 uint8_t reserved[4];
462 } __packed;
463
464 struct get_bp_info_req info_req = {
465 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
466 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
467 .reserved = {0},
468 };
469
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530470 /*
471 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
472 * initialized, check if there is cse bp info response stored in cbmem. Once the data
473 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
474 * functions. In case, data is not available in cbmem or invalid, continue to send the
475 * GET_BOOT_PARTITION_INFO command, else return.
476 */
477 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
478 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
479 if (cse_bp_info_in_cbmem) {
480 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
481 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
482 sizeof(struct get_bp_info_rsp));
483 return CB_SUCCESS;
484 }
485 }
486 } else {
487 /*
488 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
489 * global cse bp info response stored in global cse_bp_info_rsp is valid.
490 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
491 * command, else return.
492 */
493 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
494 return CB_SUCCESS;
495 }
496
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530497 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530498 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530499 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530500 }
501
502 size_t resp_size = sizeof(struct get_bp_info_rsp);
503
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530504 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530505 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530506 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530507 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530508 }
509
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530510 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530511 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530512 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530513 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530514 }
515
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530516 cse_print_boot_partition_info();
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530517 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530518}
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530519
520void cse_fill_bp_info(void)
521{
522 if (vboot_recovery_mode_enabled())
523 return;
524
525 if (cse_get_bp_info() != CB_SUCCESS)
526 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
527}
528
529/* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
530static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
531{
532 if (vboot_recovery_mode_enabled())
533 return;
534
535 sync_cse_bp_info_to_cbmem();
536}
537
538CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
539
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530540/*
541 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
542 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
543 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
544 * This function must be used before EOP.
545 * Returns false on failure and true on success.
546 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530547static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530548{
549 struct set_boot_partition_info_req {
550 struct mkhi_hdr hdr;
551 uint8_t next_bp;
552 uint8_t reserved[3];
553 } __packed;
554
555 struct set_boot_partition_info_req switch_req = {
556 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
557 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
558 .next_bp = bp,
559 .reserved = {0},
560 };
561
562 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530563 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530564 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530565 }
566
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530567 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530568
569 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530570 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530571 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530572 }
573
574 struct mkhi_hdr switch_resp;
575 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
576
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530577 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530578 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530579 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530580
581 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530582 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530583 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530584 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530585 }
586
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530587 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530588}
589
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530590static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530591{
592 struct data_clr_request {
593 struct mkhi_hdr hdr;
594 uint8_t reserved[4];
595 } __packed;
596
597 struct data_clr_request data_clr_rq = {
598 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
599 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
600 .reserved = {0},
601 };
602
603 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530604 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530605 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530606 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530607 }
608
609 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
610
611 struct mkhi_hdr data_clr_rsp;
612 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
613
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530614 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530615 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530616 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530617 }
618
619 if (data_clr_rsp.result) {
620 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
621 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530622 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530623 }
624
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530625 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530626}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530627
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600628__weak void cse_board_reset(void)
629{
630 /* Default weak implementation, does nothing. */
631}
632
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700633__weak void cse_fw_update_misc_oper(void)
634{
635 /* Default weak implementation, does nothing. */
636}
637
Rizwan Qureshiec321092019-09-06 20:28:43 +0530638/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530639static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530640{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530641 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
642 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530643
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600644 /* Allow the board to perform a reset for CSE RO<->RW jump */
645 cse_board_reset();
646
647 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000648 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530649
Rizwan Qureshiec321092019-09-06 20:28:43 +0530650 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530651
652 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530653 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530654}
655
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530656static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530657{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530658 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530659 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530660
Rizwan Qureshiec321092019-09-06 20:28:43 +0530661 return cse_set_and_boot_from_next_bp(RW);
662}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530663
V Sowmyaf9905522020-11-12 20:19:04 +0530664/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530665static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530666{
667 const struct cse_bp_entry *rw_bp;
668
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530669 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530670 return rw_bp->status != BP_STATUS_DATA_FAILURE;
671}
672
673/*
674 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
675 * otherwise false if any operation fails.
676 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530677static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530678{
679 /*
680 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
681 * - Send DATA CLEAR HECI command to CSE
682 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
683 * - Issue GLOBAL RESET HECI command.
684 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530685 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530686 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530687
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530688 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530689 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530690
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530691 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530692}
693
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530694static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530695{
696 const struct cse_bp_entry *cse_bp;
697
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530698 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530699 return &cse_bp->fw_ver;
700}
701
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530702static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530703{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530704 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530705}
706
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530707static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
708 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530709{
710 const struct cse_bp_entry *cse_bp;
711
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530712 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530713
714 if (start_offset)
715 *start_offset = cse_bp->start_offset;
716
717 if (end_offset)
718 *end_offset = cse_bp->end_offset;
719
720}
721
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530722static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530723{
724 const struct cse_bp_entry *rw_bp;
725
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530726 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530727
728 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
729 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
730 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
731 return false;
732 }
733 return true;
734}
735
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530736static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530737{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530738 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530739 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530740
741 return cse_set_and_boot_from_next_bp(RO);
742}
743
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530744static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530745{
746 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
747 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530748 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530749 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530750 }
751
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530752 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530753}
754
Rizwan Qureshiec321092019-09-06 20:28:43 +0530755static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
756{
757 uint32_t cse_bp_sign;
758
759 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
760 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
761 return false;
762 }
763
764 return cse_bp_sign == CSE_RW_SIGNATURE;
765}
766
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530767static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530768{
769 struct region_device cse_region_rdev;
770 size_t size;
771 uint32_t start_offset;
772 uint32_t end_offset;
773
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530774 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
775 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530776
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530777 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530778 size = end_offset + 1 - start_offset;
779
780 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530781 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530782
783 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100784 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530785
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530786 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530787}
788
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530789static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530790{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530791 struct vb2_context *ctx = vboot_get_context();
792 if (ctx == NULL)
793 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530794
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530795 if (vboot_is_firmware_slot_a(ctx))
796 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
797
798 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
799}
800
Rizwan Qureshiec321092019-09-06 20:28:43 +0530801/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530802 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
803 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530804 * If ver_cmp_status = 0, no update is required
805 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
806 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
807 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530808static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530809{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700810 if (a->major != b->major)
811 return a->major - b->major;
812 else if (a->minor != b->minor)
813 return a->minor - b->minor;
814 else if (a->hotfix != b->hotfix)
815 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530816 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700817 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530818}
819
820/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
821static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
822 const void *rw_blob, const size_t rw_blob_sz)
823
824{
Julius Wernerd96ca242022-08-08 18:08:35 -0700825 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530826
Julius Wernerd96ca242022-08-08 18:08:35 -0700827 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
828 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530829 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
830 return false;
831 }
832
Julius Wernerd96ca242022-08-08 18:08:35 -0700833 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530834 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
835 "the provided SHA in the metadata\n");
836 return false;
837 }
838 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
839 " provided hash in the metadata\n");
840 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530841}
842
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530843static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530844{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530845 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
846 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530847 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530848 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530849 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530850}
851
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530852static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530853 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530854{
855 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
856 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530857 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530858 }
859
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530860 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530861}
862
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700863enum cse_update_status {
864 CSE_UPDATE_NOT_REQUIRED,
865 CSE_UPDATE_UPGRADE,
866 CSE_UPDATE_DOWNGRADE,
867 CSE_UPDATE_CORRUPTED,
868 CSE_UPDATE_METADATA_ERROR,
869};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530870
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700871static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
872{
873 if ((*curr - start) >= size) {
874 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
875 return false;
876 }
877
878 *ver_field = skip_atoi(curr);
879 (*curr)++;
880 return true;
881}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530882
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530883static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
884{
885 char *version_str, *cbfs_ptr;
886 size_t size;
887
888 if (cbfs_rw_version == NULL)
889 return CB_ERR;
890
891 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
892 version_str = cbfs_ptr;
893 if (!version_str) {
894 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
895 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
896 return CB_ERR;
897 }
898
899 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
900 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
901 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
902 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
903 cbfs_unmap(version_str);
904 return CB_ERR;
905 }
906
907 cbfs_unmap(version_str);
908 return CB_SUCCESS;
909}
910
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530911static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530912{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700913 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700914 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700915
916 if (!cse_is_rw_bp_sign_valid(target_rdev))
917 return CSE_UPDATE_CORRUPTED;
918
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530919 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700920 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700921
922 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700923 cbfs_rw_version.major,
924 cbfs_rw_version.minor,
925 cbfs_rw_version.hotfix,
926 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700927
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530928 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700929 if (ret == 0)
930 return CSE_UPDATE_NOT_REQUIRED;
931 else if (ret < 0)
932 return CSE_UPDATE_DOWNGRADE;
933 else
934 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530935}
936
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530937static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530938 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530939{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530940 /* Points to CSE CBFS RW image after boot partition signature */
941 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
942
943 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530944 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530945
946 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530947 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
948 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
949 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530950
951 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530952 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
953 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530954
955 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530956 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530957}
958
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530959static bool is_cse_fw_update_enabled(void)
960{
961 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
962 return false;
963
964 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
965 return !is_debug_cse_fw_update_disable();
966
967 return true;
968}
969
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530970static enum csme_failure_reason cse_update_rw(const void *cse_cbfs_rw, const size_t cse_blob_sz,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530971 struct region_device *target_rdev)
972{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530973 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530974 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
975 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530976 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
977 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530978
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530979 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530980 return CSE_LITE_SKU_FW_UPDATE_ERROR;
981
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530982 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530983 return CSE_LITE_SKU_FW_UPDATE_ERROR;
984
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600985 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530986}
987
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530988static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530989{
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530990 if (status == CSE_UPDATE_CORRUPTED)
991 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530992 /*
993 * To set CSE's operation mode to HMRFPO mode:
994 * 1. Ensure CSE to boot from RO(BP1)
995 * 2. Send HMRFPO_ENABLE command to CSE
996 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530997 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530998 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530999
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001000 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -07001001 /* Reset the PSR backup command status in CMOS */
1002 if (CONFIG(SOC_INTEL_CSE_LITE_PSR))
1003 update_psr_backup_status(PSR_BACKUP_PENDING);
1004
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301005 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001006 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +05301007 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001008 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301009 }
1010
Rizwan Qureshiec321092019-09-06 20:28:43 +05301011 return cse_hmrfpo_enable();
1012}
1013
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301014static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
1015 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301016{
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301017 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001018 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +05301019 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -07001020 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301021
Julius Werner18881f992021-04-13 15:28:12 -07001022 const char *area_name = cse_get_source_rdev_fmap();
1023 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301024 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301025
Krishna P Bhat D75a423e2022-04-20 15:50:06 +05301026 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
1027 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
1028 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
1029 } else {
1030 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
1031 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
1032 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301033 if (!cse_cbfs_rw) {
1034 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
1035 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
1036 }
1037
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001038 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
1039 if (!cbfs_rw_hash) {
1040 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
1041 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
1042 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
1043 goto error_exit;
1044 }
1045
Julius Werner18881f992021-04-13 15:28:12 -07001046 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301047 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
1048 goto error_exit;
1049 }
1050
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301051 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -06001052 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301053 goto error_exit;
1054 }
1055
Jeremy Compostella08b5200d2023-01-19 11:32:25 -07001056 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301057 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301058
1059error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001060 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -07001061 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301062 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301063}
1064
Anil Kumar7b2edc32023-04-18 11:03:34 -07001065static bool is_psr_data_backed_up(void)
1066{
1067 /* Track PSR backup status in CMOS */
1068 return (get_psr_backup_status() == PSR_BACKUP_DONE);
1069}
1070
Anil Kumara2d10bb2023-05-01 11:44:47 -07001071static bool is_psr_supported(void)
1072{
1073 uint32_t feature_status;
1074
1075 /*
1076 * Check if SoC has support for PSR feature typically PSR feature
1077 * is only supported by vpro SKU
1078 *
1079 */
1080 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
1081 printk(BIOS_ERR, "cse_get_fw_feature_state command failed !\n");
1082 return false;
1083 }
1084
1085 if (!(feature_status & ME_FW_FEATURE_PSR)) {
1086 printk(BIOS_DEBUG, "PSR is not supported in this SKU !\n");
1087 return false;
1088 }
1089
1090 return true;
1091}
1092
Anil Kumar7b2edc32023-04-18 11:03:34 -07001093/*
1094 * PSR data needs to be backed up prior to downgrade. So switch the CSE boot mode to RW, send
1095 * PSR back-up command to CSE and update the PSR back-up state in CMOS.
1096 */
1097static void backup_psr_data(void)
1098{
1099 printk(BIOS_DEBUG, "cse_lite: Initiate PSR data backup flow\n");
1100 /* Switch CSE to RW to send PSR_HECI_FW_DOWNGRADE_BACKUP command */
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301101 if (cse_boot_to_rw() != CB_SUCCESS) {
1102 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001103 goto update_and_exit;
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301104 }
Anil Kumar7b2edc32023-04-18 11:03:34 -07001105 /*
Anil Kumara2d10bb2023-05-01 11:44:47 -07001106 * The function to check for PSR feature support can only be called after
1107 * switching to RW partition. The command MKHI_FWCAPS_GET_FW_FEATURE_STATE
1108 * that gives feature state is supported by a process that is loaded only
1109 * when CSE boots from RW.
1110 *
1111 */
1112 if (!is_psr_supported())
1113 goto update_and_exit;
1114
1115 /*
Anil Kumar7b2edc32023-04-18 11:03:34 -07001116 * Prerequisites:
1117 * 1) HFSTS1 Current Working State is Normal
1118 * 2) HFSTS1 Current Operation Mode is Normal
1119 */
1120 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
1121 printk(BIOS_DEBUG, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1122 "prerequisites not met!\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301123 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001124 goto update_and_exit;
1125 }
1126
1127 /* Send PSR_HECI_FW_DOWNGRADE_BACKUP command */
1128 struct psr_heci_fw_downgrade_backup_req {
1129 struct psr_heci_header header;
1130 } __packed;
1131
1132 struct psr_heci_fw_downgrade_backup_req req = {
1133 .header.command = PSR_HECI_FW_DOWNGRADE_BACKUP,
1134 };
1135
1136 struct psr_heci_fw_downgrade_backup_res {
1137 struct psr_heci_header header;
1138 uint32_t status;
1139 } __packed;
1140
1141 struct psr_heci_fw_downgrade_backup_res backup_psr_resp;
1142 size_t resp_size = sizeof(backup_psr_resp);
1143
1144 printk(BIOS_DEBUG, "cse_lite: Send PSR_HECI_FW_DOWNGRADE_BACKUP command\n");
1145 if (heci_send_receive(&req, sizeof(req),
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301146 &backup_psr_resp, &resp_size, HECI_PSR_ADDR)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -07001147 printk(BIOS_ERR, "cse_lite: could not backup PSR data\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301148 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP, ELOG_PSR_DATA_BACKUP_FAILED);
1149 } else {
1150 if (backup_psr_resp.status != PSR_STATUS_SUCCESS) {
Anil Kumar7b2edc32023-04-18 11:03:34 -07001151 printk(BIOS_ERR, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1152 "returned %u\n", backup_psr_resp.status);
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301153 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1154 ELOG_PSR_DATA_BACKUP_FAILED);
1155 } else {
1156 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1157 ELOG_PSR_DATA_BACKUP_SUCCESS);
1158 }
1159 }
Anil Kumar7b2edc32023-04-18 11:03:34 -07001160
1161update_and_exit:
1162 /*
1163 * An attempt to send PSR back-up command has been made. Update this info in CMOS and
1164 * send success once backup_psr_data() has been called. We do not want to put the system
1165 * into recovery for PSR data backup command pre-requisites not being met.
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301166 * We cannot do much if CSE fails to backup the PSR data, except create an event log.
Anil Kumar7b2edc32023-04-18 11:03:34 -07001167 */
1168 update_psr_backup_status(PSR_BACKUP_DONE);
1169 return;
1170}
1171
1172static void initiate_psr_data_backup(void)
1173{
1174 if (is_psr_data_backed_up())
1175 return;
1176
1177 backup_psr_data();
1178}
1179
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301180static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301181{
1182 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001183 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301184
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301185 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +05301186 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1187 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1188 }
1189
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301190 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001191 if (status == CSE_UPDATE_NOT_REQUIRED)
1192 return CSE_NO_ERROR;
1193 if (status == CSE_UPDATE_METADATA_ERROR)
1194 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Anil Kumar7b2edc32023-04-18 11:03:34 -07001195 if (CONFIG(SOC_INTEL_CSE_LITE_PSR) && status == CSE_UPDATE_DOWNGRADE)
1196 initiate_psr_data_backup();
Rizwan Qureshiec321092019-09-06 20:28:43 +05301197
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001198 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301199 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301200}
1201
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301202static const char *cse_sub_part_str(enum bpdt_entry_type type)
1203{
1204 switch (type) {
1205 case IOM_FW:
1206 return "IOM";
1207 case NPHY_FW:
1208 return "NPHY";
1209 default:
1210 return "Unknown";
1211 }
1212}
1213
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301214static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301215{
1216 struct region_device cse_region_rdev;
1217 uint32_t size;
1218 uint32_t start_offset;
1219 uint32_t end_offset;
1220
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301221 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1222 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301223
1224 if (!strcmp(cse_regions[bp], "RO"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301225 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301226 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301227 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301228
1229 size = end_offset + 1 - start_offset;
1230
1231 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301232 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301233
1234 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1235 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301236 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301237}
1238
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301239static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1240 enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301241{
1242 struct bpdt_header bpdt_hdr;
1243 struct region_device cse_rdev;
1244 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1245 uint8_t i;
1246
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301247 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301248 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1249 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301250 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301251 }
1252
1253 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1254 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301255 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301256 }
1257
1258 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1259 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1260 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1261 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301262 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301263 }
1264
1265 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1266 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1267 if (bpdt_entries[i].type == type) {
1268 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1269 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1270 bpdt_entries[i].size);
1271
1272 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1273 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301274 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301275 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301276 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301277 }
1278 }
1279
1280 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301281 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301282}
1283
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301284static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301285 const struct region_device *rdev,
1286 struct fw_version *fw_ver)
1287{
1288 struct subpart_entry subpart_entry;
1289 struct subpart_entry_manifest_header man_hdr;
1290
1291 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1292 != SUBPART_ENTRY_SZ) {
1293 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1294 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301295 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301296 }
1297
1298 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1299 != SUBPART_MANIFEST_HDR_SZ) {
1300 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1301 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301302 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301303 }
1304
1305 fw_ver->major = man_hdr.binary_version.major;
1306 fw_ver->minor = man_hdr.binary_version.minor;
1307 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1308 fw_ver->build = man_hdr.binary_version.build;
1309
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301310 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301311}
1312
1313static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1314{
1315 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1316 struct subpart_entry *subpart_entry;
1317 struct subpart_entry_manifest_header *man_hdr;
1318
Elyes Haouas9018dee2022-11-18 15:07:33 +01001319 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1320 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301321
1322 fw_ver->major = man_hdr->binary_version.major;
1323 fw_ver->minor = man_hdr->binary_version.minor;
1324 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1325 fw_ver->build = man_hdr->binary_version.build;
1326}
1327
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301328static enum cb_err cse_prep_for_component_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301329{
1330 /*
1331 * To set CSE's operation mode to HMRFPO mode:
1332 * 1. Ensure CSE to boot from RO(BP1)
1333 * 2. Send HMRFPO_ENABLE command to CSE
1334 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301335 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301336 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301337
1338 return cse_hmrfpo_enable();
1339}
1340
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301341static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1342 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301343 struct region_device *target_rdev)
1344{
1345 if (region_device_sz(target_rdev) < blob_sz) {
1346 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1347 "smaller than blob size:%zx, abort update\n",
1348 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1349 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1350 }
1351
1352 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301353 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301354 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1355
1356 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301357 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301358 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1359
1360 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1361 cse_sub_part_str(type));
1362
1363 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1364}
1365
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301366static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301367{
1368 switch (rv) {
1369 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1370 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1371 return rv;
1372 default:
1373 cse_trigger_vboot_recovery(rv);
1374 }
1375 /* Control never reaches here */
1376 return rv;
1377}
1378
1379static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301380 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301381{
1382 struct region_device target_rdev;
1383 struct fw_version target_fw_ver, source_fw_ver;
1384 enum csme_failure_reason rv;
1385 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301386
1387 void *subpart_cbfs_rw = cbfs_map(name, &size);
1388 if (!subpart_cbfs_rw) {
1389 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1390 cse_sub_part_str(type));
1391 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1392 }
1393
1394 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1395 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1396 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1397 source_fw_ver.build);
1398
1399 /* Trigger sub-partition update in CSE RO and CSE RW */
1400 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301401 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301402 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1403 goto error_exit;
1404 }
1405
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301406 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301407 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1408 goto error_exit;
1409 }
1410
1411 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1412 cse_sub_part_str(type), target_fw_ver.major,
1413 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1414
1415 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1416 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1417 cse_regions[bp], cse_sub_part_str(type));
1418 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1419 continue;
1420 }
1421
1422 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1423 cse_sub_part_str(type));
1424
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301425 if (cse_prep_for_component_update() != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301426 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1427 goto error_exit;
1428 }
1429
1430 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1431 size, &target_rdev);
1432
1433 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1434 goto error_exit;
1435 }
1436error_exit:
1437 cbfs_unmap(subpart_cbfs_rw);
1438 return rv;
1439}
1440
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301441static enum csme_failure_reason cse_sub_part_fw_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301442{
1443 if (skip_cse_sub_part_update()) {
1444 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1445 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1446 }
1447
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301448 enum csme_failure_reason rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301449 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301450
1451 handle_cse_sub_part_fw_update_rv(rv);
1452
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301453 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301454
1455 return handle_cse_sub_part_fw_update_rv(rv);
1456}
1457
Subrata Banik5ff01182023-04-20 11:08:17 +05301458static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301459{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301460 /*
1461 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1462 * is not enabled and continue to update CSE sub-partitions.
1463 */
1464 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301465 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301466 return;
1467 }
1468
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301469 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1470 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301471 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301472 return;
1473 }
1474
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301475 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301476 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301477
1478 /* If system is in recovery mode, don't trigger recovery again */
1479 if (!vboot_recovery_mode_enabled()) {
1480 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1481 } else {
1482 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1483 "so no action\n");
1484 return;
1485 }
1486 }
1487
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +05301488 /* Store the CSE RW Firmware Version into CBMEM */
1489 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1490 cse_store_rw_fw_version();
1491
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301492 /*
1493 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301494 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301495 * fails during recovery, just continue to boot.
1496 */
1497 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301498 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301499 cse_board_reset();
1500 do_global_reset();
1501 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1502 }
1503
1504 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301505 }
1506
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301507 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001508 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301509
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301510 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001511 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301512 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1513 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301514 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301515 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301516 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301517 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301518 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001519 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301520 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301521
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301522 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301523 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301524
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301525 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001526 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301527
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301528 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301529 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001530 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301531 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301532}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301533
Subrata Banik5ff01182023-04-20 11:08:17 +05301534void cse_fw_sync(void)
1535{
1536 timestamp_add_now(TS_CSE_FW_SYNC_START);
1537 do_cse_fw_sync();
1538 timestamp_add_now(TS_CSE_FW_SYNC_END);
1539}
1540
Subrata Banik7f66adb2023-04-14 00:35:38 +05301541static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1542 struct fw_version_resp *resp)
1543{
1544 enum cse_tx_rx_status ret;
1545 struct fw_version_msg {
1546 struct mkhi_hdr hdr;
1547 enum fpt_partition_id partition_id;
1548 } __packed msg = {
1549 .hdr = {
1550 .group_id = MKHI_GROUP_ID_GEN,
1551 .command = GEN_GET_IMAGE_FW_VERSION,
1552 },
1553 .partition_id = id,
1554 };
1555
1556 /*
1557 * Prerequisites:
1558 * 1) HFSTS1 CWS is Normal
1559 * 2) HFSTS1 COM is Normal
1560 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1561 */
1562
1563 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1564 !cse_is_hfs1_com_normal()) {
1565 printk(BIOS_ERR,
1566 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1567 return CB_ERR;
1568 }
1569
1570 size_t resp_size = sizeof(struct fw_version_resp);
1571 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1572
1573 if (ret || resp->hdr.result) {
1574 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1575 id, resp->hdr.result);
1576 return CB_ERR;
1577 }
1578
1579 return CB_SUCCESS;
1580}
1581
Subrata Banik044fc9f2023-04-14 02:34:37 +05301582static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1583 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301584{
1585 if (vboot_recovery_mode_enabled()) {
1586 printk(BIOS_WARNING,
1587 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1588 return CB_ERR;
1589 }
1590
1591 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1592 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1593 return CB_ERR;
1594 }
1595
1596 return send_get_fpt_partition_info_cmd(id, resp);
1597}
1598
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001599static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1600{
1601 const struct fw_version invalid_fw = {0, 0, 0, 0};
1602 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1603 return false;
1604 return true;
1605}
1606
Subrata Banikb1b7c532023-04-14 01:36:13 +05301607/*
1608 * Helper function to read ISH version from CSE FPT using HECI command.
1609 *
1610 * The HECI command only be executed after memory has been initialized.
1611 * This is because the command relies on resources that are not available
1612 * until DRAM initialization command has been sent.
1613 */
1614static void store_ish_version(void)
1615{
1616 if (!ENV_RAMSTAGE)
1617 return;
1618
1619 if (vboot_recovery_mode_enabled())
1620 return;
1621
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001622 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1623 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301624 return;
1625
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001626 struct cse_specific_info cse_info_in_cmos;
1627 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001628
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001629 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1630 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301631
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001632 /* Get current cse firmware state */
1633 enum cse_fw_state fw_state = get_cse_state(
1634 &(cbmem_version->cur_cse_fw_version),
1635 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1636 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301637
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001638 if (fw_state == CSE_FW_WARM_BOOT) {
1639 return;
1640 } else {
1641 if (fw_state == CSE_FW_COLD_BOOT &&
1642 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1643 /* CMOS data is persistent across cold boots */
1644 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1645 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1646 sizeof(struct cse_fw_ish_version_info));
1647 store_cse_info_crc(cse_info_in_cbmem);
1648 } else {
1649 /*
1650 * Current running CSE version is different than previous stored CSE version
1651 * which could be due to CSE update or rollback, hence, need to send ISHC
1652 * partition info cmd to know the currently running ISH version.
1653 */
1654 struct fw_version_resp resp;
1655 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1656 &resp) == CB_SUCCESS) {
1657 /* Update stored CSE version with current cse version */
1658 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1659 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001660
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001661 /* Retrieve and update current ish version */
1662 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1663 &(resp.manifest_data.version), sizeof(struct fw_version));
1664
1665 /* Update the CRC */
1666 store_cse_info_crc(cse_info_in_cbmem);
1667
1668 /* Update CMOS with current CSE FPT versions.*/
1669 cmos_write_fw_partition_info(cse_info_in_cbmem);
1670 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301671 }
1672 }
1673}
1674
Subrata Banikfc313d62023-04-14 01:31:29 +05301675static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301676{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301677 if (acpi_get_sleep_type() == ACPI_S3)
1678 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301679
Subrata Banik5ff01182023-04-20 11:08:17 +05301680 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301681 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301682
1683 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301684 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301685 * is available
1686 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301687 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301688 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301689}
1690
Subrata Banikfc313d62023-04-14 01:31:29 +05301691BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);