blob: 6e5b451c124978f31f6ede409b4676f875dfa3be [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>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053016#include <security/vboot/misc.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010017#include <security/vboot/vboot_common.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053018#include <soc/intel/common/reset.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +053019#include <timestamp.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053020
Dinesh Gehlota9232d82023-06-10 11:53:47 +000021#include "cse_lite_cmos.h"
22
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053023#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
24#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
25#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
26#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
27#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053028
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053029/* Converts bp index to boot partition string */
30#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
31
Rizwan Qureshiec321092019-09-06 20:28:43 +053032/* CSE RW boot partition signature */
33#define CSE_RW_SIGNATURE 0x000055aa
34
35/* CSE RW boot partition signature size */
36#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
37
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053038/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053039 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053040 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053041 * CSE Lite SKU Image Layout:
Angel Pons2e8e0602022-08-13 19:50:28 +020042 * +------------+ +----+------+----+ +-----+------+-----+
43 * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 |
44 * +------------+ +----+------+----+ +-----+------+-----+
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053045 */
46#define CSE_MAX_BOOT_PARTITIONS 3
47
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053048/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053049enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053050 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053051 RO = 0,
52
Rizwan Qureshiec321092019-09-06 20:28:43 +053053 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053054 RW = 1
55};
56
57/*
58 * Boot partition status.
59 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
60 */
61enum bp_status {
62 /* This value is returned when a partition has no errors */
63 BP_STATUS_SUCCESS = 0,
64
65 /*
66 * This value is returned when a partition should be present based on layout, but it is
67 * not valid.
68 */
69 BP_STATUS_GENERAL_FAILURE = 1,
70
71 /* This value is returned when a partition is not present per initial image layout */
72 BP_STATUS_PARTITION_NOT_PRESENT = 2,
73
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053074 /*
75 * This value is returned when unexpected issues are detected in CSE Data area
76 * and CSE TCB-SVN downgrade scenario.
77 */
78 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053079};
80
81/*
82 * Boot Partition Info Flags
83 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
84 */
85enum bp_info_flags {
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053086 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
87 BP_INFO_REDUNDANCY_EN = 1 << 0,
88
89 /* It indicates RO(BP1) supports Minimal Recovery Mode */
90 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
91
92 /*
93 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
94 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
95 */
96 BP_INFO_READ_ONLY_CFG = 1 << 2,
97};
98
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053099/* CSE boot partition entry info */
100struct cse_bp_entry {
101 /* Boot partition version */
102 struct fw_version fw_ver;
103
104 /* Boot partition status */
105 uint32_t status;
106
107 /* Starting offset of the partition within CSE region */
108 uint32_t start_offset;
109
110 /* Ending offset of the partition within CSE region */
111 uint32_t end_offset;
112 uint8_t reserved[12];
113} __packed;
114
115/* CSE boot partition info */
116struct cse_bp_info {
117 /* Number of boot partitions */
118 uint8_t total_number_of_bp;
119
120 /* Current boot partition */
121 uint8_t current_bp;
122
123 /* Next boot partition */
124 uint8_t next_bp;
125
126 /* Boot Partition Info Flags */
127 uint8_t flags;
128
129 /* Boot Partition Entry Info */
130 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
131} __packed;
132
133struct get_bp_info_rsp {
134 struct mkhi_hdr hdr;
135 struct cse_bp_info bp_info;
136} __packed;
137
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530138static struct get_bp_info_rsp cse_bp_info_rsp;
139
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000140enum cse_fw_state {
141 /* The CMOS and CBMEM have the current fw version. */
142 CSE_FW_WARM_BOOT,
143
144 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
145 CSE_FW_COLD_BOOT,
146
147 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
148 CSE_FW_INVALID,
149};
150
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530151static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700152
Subrata Banik65a6d172023-08-13 13:03:50 +0000153static struct cse_specific_info cse_info;
154
Subrata Banikda527ec2022-11-24 15:42:35 +0530155void cse_log_ro_write_protection_info(bool mfg_mode)
156{
157 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
158
159 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
160 cse_ro_wp_en ? "YES" : "NO");
161
162 if (cse_ro_wp_en) {
163 uint32_t base, limit;
164 spi_get_wp_cse_ro_range(&base, &limit);
165 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
166 base, limit);
167 }
168
169 /*
170 * If manufacturing mode is disabled, but CSE RO is not write protected,
171 * log error.
172 */
173 if (!mfg_mode && !cse_ro_wp_en)
174 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
175}
176
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530177enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700178{
179 struct cse_boot_perf_req {
180 struct mkhi_hdr hdr;
181 uint32_t reserved;
182 } __packed;
183
184 struct cse_boot_perf_req req = {
185 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
186 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
187 .reserved = 0,
188 };
189
190 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
191
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530192 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700193 HECI_MKHI_ADDR)) {
194 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530195 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700196 }
197
198 if (boot_perf_rsp->hdr.result) {
199 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
200 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530201 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700202 }
203
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530204 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700205}
206
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530207static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530208{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530209 return &cse_bp_info_rsp.bp_info;
210}
211
212static uint8_t cse_get_current_bp(void)
213{
214 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530215 return cse_bp_info->current_bp;
216}
217
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530218static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530219{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530220 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530221 return &cse_bp_info->bp_entries[bp];
222}
223
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000224static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000225{
226 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
227
228 /*
229 * Authenticate the CBMEM persistent data.
230 *
231 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
232 * could change the values stored in this region has to also trigger the global
233 * reset. Hence, CBMEM persistent data won't be available any time after such
234 * event (global reset or cold reset) being initiated.
235 *
236 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
237 * want to override the existing data in CBMEM to avoid any additional boot latency.
238 */
239 if (info->crc != crc)
240 return false;
241
242 return true;
243}
244
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000245static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000246{
247 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
248}
249
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000250static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
251 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
252{
253 enum cse_fw_state state = CSE_FW_WARM_BOOT;
254 size_t size = sizeof(struct fw_version);
255 /*
256 * Compare if stored CSE version (from the previous boot) is same as current
257 * running CSE version.
258 */
259 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
260 /*
261 * CMOS CSE versioin is invalid, possibly two scenarios
262 * 1. CSE FW update
263 * 2. First boot
264 */
265 state = CSE_FW_INVALID;
266 } else {
267 /*
268 * Check if current running CSE version is same as previous stored CSE
269 * version aka CBMEM region is still valid.
270 */
271 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
272 state = CSE_FW_COLD_BOOT;
273 }
274 return state;
275}
276
Subrata Banik65a6d172023-08-13 13:03:50 +0000277/*
278 * Helper function that stores current CSE firmware version to CBMEM memory,
279 * except during recovery mode.
280 */
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530281static void cse_store_rw_fw_version(void)
Subrata Banik65a6d172023-08-13 13:03:50 +0000282{
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530283 const struct cse_bp_entry *cse_bp;
284 cse_bp = cse_get_bp_entry(RW);
285
Subrata Banik65a6d172023-08-13 13:03:50 +0000286 if (vboot_recovery_mode_enabled())
287 return;
288
289 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000290 /* update current CSE version and return */
291 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
292 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000293 return;
294 }
295
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000296 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
297 sizeof(*cse_info_in_cbmem));
298 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000299 return;
300
301 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000302 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000303 return;
304
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000305 struct cse_specific_info cse_info_in_cmos;
306 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000307
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000308 /* Get current cse firmware state */
309 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
310 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
311 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
312
313 /* Reset CBMEM data and update current CSE version */
314 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
315 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
316 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000317
318 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000319 store_cse_info_crc(cse_info_in_cbmem);
320
321 if (fw_state == CSE_FW_INVALID) {
322 /*
323 * Current CMOS data is outdated, which could be due to CSE update or
324 * rollback, hence, need to update CMOS with current CSE FPT versions.
325 */
326 cmos_write_fw_partition_info(cse_info_in_cbmem);
327 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000328}
329
330#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
331/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
332static void preram_cse_info_sync_to_cbmem(int is_recovery)
333{
334 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
335 return;
336
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000337 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
338 sizeof(*cse_info_in_cbmem));
339 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000340 return;
341
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000342 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
343 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000344 return;
345
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000346 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
347 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
348 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000349
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000350 struct cse_specific_info cse_info_in_cmos;
351 cmos_read_fw_partition_info(&cse_info_in_cmos);
352
353 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
354 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
355 sizeof(struct fw_version))) {
356 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
357 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
358 return;
359 }
360
361 /*
362 * Current CMOS data is outdated, which could be due to CSE update or
363 * rollback, hence, need to update CMOS with current CSE FPT versions.
364 */
365 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000366}
367
368CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
369#endif
370
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530371static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530372{
373 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530374 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530375
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530376 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
377 cse_bp_info->total_number_of_bp);
378 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
379 GET_BP_STR(cse_bp_info->current_bp));
380 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
381 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530382
383 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530384 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530385 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 +0530386 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
387 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
388 cse_bp->status, cse_bp->start_offset,
389 cse_bp->end_offset);
390
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530391 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530392 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 +0530393 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
394 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
395 cse_bp->status, cse_bp->start_offset,
396 cse_bp->end_offset);
397}
398
399/*
400 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
401 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
402 * It allows execution of the Boot Partition commands in below scenarios:
403 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
404 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
405 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530406 * The prerequisite check should be handled in cse_get_bp_info() and
407 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
408 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530409 */
410static bool cse_is_bp_cmd_info_possible(void)
411{
412 if (cse_is_hfs1_cws_normal()) {
413 if (cse_is_hfs1_com_normal())
414 return true;
415 if (cse_is_hfs1_com_secover_mei_msg())
416 return true;
417 if (cse_is_hfs1_com_soft_temp_disable())
418 return true;
419 }
420 return false;
421}
422
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530423static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
424{
425 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
426
427 if (cse_bp_info_in_cbmem != NULL)
428 return cse_bp_info_in_cbmem;
429
430 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
431 sizeof(struct get_bp_info_rsp));
432
433 if (!cse_bp_info_in_cbmem) {
434 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
435 return NULL;
436 }
437
438 /* Copy the CSE Boot Partition Info command response to cbmem */
439 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
440
441 return cse_bp_info_in_cbmem;
442}
443
444static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
445{
446 /*
447 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
448 * then return false to indicate cse_bp_info is not valid.
449 */
450 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
451 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
452 !bp_info_rsp->hdr.is_resp) ? false : true;
453}
454
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530455static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530456{
457 struct get_bp_info_req {
458 struct mkhi_hdr hdr;
459 uint8_t reserved[4];
460 } __packed;
461
462 struct get_bp_info_req info_req = {
463 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
464 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
465 .reserved = {0},
466 };
467
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530468 /*
469 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
470 * initialized, check if there is cse bp info response stored in cbmem. Once the data
471 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
472 * functions. In case, data is not available in cbmem or invalid, continue to send the
473 * GET_BOOT_PARTITION_INFO command, else return.
474 */
475 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
476 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
477 if (cse_bp_info_in_cbmem) {
478 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
479 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
480 sizeof(struct get_bp_info_rsp));
481 return CB_SUCCESS;
482 }
483 }
484 } else {
485 /*
486 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
487 * global cse bp info response stored in global cse_bp_info_rsp is valid.
488 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
489 * command, else return.
490 */
491 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
492 return CB_SUCCESS;
493 }
494
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530495 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530496 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530497 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530498 }
499
500 size_t resp_size = sizeof(struct get_bp_info_rsp);
501
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530502 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530503 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530504 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530505 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530506 }
507
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530508 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530509 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530510 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530511 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530512 }
513
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530514 cse_print_boot_partition_info();
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530515 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530516}
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530517
518void cse_fill_bp_info(void)
519{
520 if (vboot_recovery_mode_enabled())
521 return;
522
523 if (cse_get_bp_info() != CB_SUCCESS)
524 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
525}
526
527/* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
528static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
529{
530 if (vboot_recovery_mode_enabled())
531 return;
532
533 sync_cse_bp_info_to_cbmem();
534}
535
536CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
537
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530538/*
539 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
540 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
541 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
542 * This function must be used before EOP.
543 * Returns false on failure and true on success.
544 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530545static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530546{
547 struct set_boot_partition_info_req {
548 struct mkhi_hdr hdr;
549 uint8_t next_bp;
550 uint8_t reserved[3];
551 } __packed;
552
553 struct set_boot_partition_info_req switch_req = {
554 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
555 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
556 .next_bp = bp,
557 .reserved = {0},
558 };
559
560 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530561 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530562 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530563 }
564
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530565 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530566
567 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530568 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530569 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530570 }
571
572 struct mkhi_hdr switch_resp;
573 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
574
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530575 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530576 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530577 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530578
579 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530580 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530581 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530582 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530583 }
584
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530585 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530586}
587
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530588static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530589{
590 struct data_clr_request {
591 struct mkhi_hdr hdr;
592 uint8_t reserved[4];
593 } __packed;
594
595 struct data_clr_request data_clr_rq = {
596 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
597 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
598 .reserved = {0},
599 };
600
601 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530602 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530603 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530604 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530605 }
606
607 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
608
609 struct mkhi_hdr data_clr_rsp;
610 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
611
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530612 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530613 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530614 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530615 }
616
617 if (data_clr_rsp.result) {
618 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
619 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530620 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530621 }
622
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530623 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530624}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530625
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600626__weak void cse_board_reset(void)
627{
628 /* Default weak implementation, does nothing. */
629}
630
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700631__weak void cse_fw_update_misc_oper(void)
632{
633 /* Default weak implementation, does nothing. */
634}
635
Rizwan Qureshiec321092019-09-06 20:28:43 +0530636/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530637static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530638{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530639 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
640 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530641
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600642 /* Allow the board to perform a reset for CSE RO<->RW jump */
643 cse_board_reset();
644
645 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000646 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530647
Rizwan Qureshiec321092019-09-06 20:28:43 +0530648 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530649
650 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530651 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530652}
653
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530654static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530655{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530656 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530657 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530658
Rizwan Qureshiec321092019-09-06 20:28:43 +0530659 return cse_set_and_boot_from_next_bp(RW);
660}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530661
V Sowmyaf9905522020-11-12 20:19:04 +0530662/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530663static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530664{
665 const struct cse_bp_entry *rw_bp;
666
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530667 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530668 return rw_bp->status != BP_STATUS_DATA_FAILURE;
669}
670
671/*
672 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
673 * otherwise false if any operation fails.
674 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530675static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530676{
677 /*
678 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
679 * - Send DATA CLEAR HECI command to CSE
680 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
681 * - Issue GLOBAL RESET HECI command.
682 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530683 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530684 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530685
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530686 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530687 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530688
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530689 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530690}
691
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530692static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530693{
694 const struct cse_bp_entry *cse_bp;
695
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530696 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530697 return &cse_bp->fw_ver;
698}
699
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530700static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530701{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530702 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530703}
704
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530705static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
706 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530707{
708 const struct cse_bp_entry *cse_bp;
709
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530710 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530711
712 if (start_offset)
713 *start_offset = cse_bp->start_offset;
714
715 if (end_offset)
716 *end_offset = cse_bp->end_offset;
V Sowmyaf9905522020-11-12 20:19:04 +0530717}
718
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530719static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530720{
721 const struct cse_bp_entry *rw_bp;
722
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530723 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530724
725 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
726 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
727 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
728 return false;
729 }
730 return true;
731}
732
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530733static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530734{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530735 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530736 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530737
738 return cse_set_and_boot_from_next_bp(RO);
739}
740
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530741static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530742{
743 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
744 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530745 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530746 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530747 }
748
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530749 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530750}
751
Rizwan Qureshiec321092019-09-06 20:28:43 +0530752static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
753{
754 uint32_t cse_bp_sign;
755
756 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
757 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
758 return false;
759 }
760
761 return cse_bp_sign == CSE_RW_SIGNATURE;
762}
763
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530764static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530765{
766 struct region_device cse_region_rdev;
767 size_t size;
768 uint32_t start_offset;
769 uint32_t end_offset;
770
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530771 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
772 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530773
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530774 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530775 size = end_offset + 1 - start_offset;
776
777 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530778 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530779
780 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100781 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530782
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530783 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530784}
785
Rizwan Qureshiec321092019-09-06 20:28:43 +0530786/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530787 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
788 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530789 * If ver_cmp_status = 0, no update is required
790 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
791 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
792 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530793static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530794{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700795 if (a->major != b->major)
796 return a->major - b->major;
797 else if (a->minor != b->minor)
798 return a->minor - b->minor;
799 else if (a->hotfix != b->hotfix)
800 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530801 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700802 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530803}
804
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530805static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530806{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530807 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
808 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530809 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530810 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530811 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530812}
813
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530814static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530815 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530816{
817 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
818 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530819 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530820 }
821
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530822 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530823}
824
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700825enum cse_update_status {
826 CSE_UPDATE_NOT_REQUIRED,
827 CSE_UPDATE_UPGRADE,
828 CSE_UPDATE_DOWNGRADE,
829 CSE_UPDATE_CORRUPTED,
830 CSE_UPDATE_METADATA_ERROR,
831};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530832
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700833static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
834{
835 if ((*curr - start) >= size) {
836 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
837 return false;
838 }
839
840 *ver_field = skip_atoi(curr);
841 (*curr)++;
842 return true;
843}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530844
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530845static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
846{
847 char *version_str, *cbfs_ptr;
848 size_t size;
849
850 if (cbfs_rw_version == NULL)
851 return CB_ERR;
852
853 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
854 version_str = cbfs_ptr;
855 if (!version_str) {
856 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
857 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
858 return CB_ERR;
859 }
860
861 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
862 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
863 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
864 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
865 cbfs_unmap(version_str);
866 return CB_ERR;
867 }
868
869 cbfs_unmap(version_str);
870 return CB_SUCCESS;
871}
872
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530873static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530874{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700875 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700876 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700877
878 if (!cse_is_rw_bp_sign_valid(target_rdev))
879 return CSE_UPDATE_CORRUPTED;
880
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530881 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700882 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700883
884 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700885 cbfs_rw_version.major,
886 cbfs_rw_version.minor,
887 cbfs_rw_version.hotfix,
888 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700889
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530890 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700891 if (ret == 0)
892 return CSE_UPDATE_NOT_REQUIRED;
893 else if (ret < 0)
894 return CSE_UPDATE_DOWNGRADE;
895 else
896 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530897}
898
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530899static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530900 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530901{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530902 /* Points to CSE CBFS RW image after boot partition signature */
903 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
904
905 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530906 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530907
908 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530909 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
910 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
911 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530912
913 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530914 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
915 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530916
917 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530918 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530919}
920
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530921static bool is_cse_fw_update_enabled(void)
922{
923 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
924 return false;
925
Kapil Porwal83cd6f92024-05-26 16:23:17 +0000926 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
927 return false;
928
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530929 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
930 return !is_debug_cse_fw_update_disable();
931
932 return true;
933}
934
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530935static 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 +0530936 struct region_device *target_rdev)
937{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530938 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530939 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
940 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530941 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
942 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530943
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530944 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530945 return CSE_LITE_SKU_FW_UPDATE_ERROR;
946
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530947 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530948 return CSE_LITE_SKU_FW_UPDATE_ERROR;
949
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600950 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530951}
952
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530953static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530954{
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530955 if (status == CSE_UPDATE_CORRUPTED)
956 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530957 /*
958 * To set CSE's operation mode to HMRFPO mode:
959 * 1. Ensure CSE to boot from RO(BP1)
960 * 2. Send HMRFPO_ENABLE command to CSE
961 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530962 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530963 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530964
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700965 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -0700966 /* Reset the PSR backup command status in CMOS */
967 if (CONFIG(SOC_INTEL_CSE_LITE_PSR))
968 update_psr_backup_status(PSR_BACKUP_PENDING);
969
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530970 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700971 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530972 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700973 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530974 }
975
Rizwan Qureshiec321092019-09-06 20:28:43 +0530976 return cse_hmrfpo_enable();
977}
978
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530979static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
980 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530981{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530982 enum csme_failure_reason rv;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530983 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700984 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530985
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530986 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530987 cse_cbfs_rw = cbfs_cbmem_alloc(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME,
988 CBMEM_ID_CSE_UPDATE, &size);
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530989 } else {
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530990 cse_cbfs_rw = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530991 }
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530992
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530993 if (!cse_cbfs_rw) {
994 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
995 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
996 }
997
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530998 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600999 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301000 goto error_exit;
1001 }
1002
Jeremy Compostella08b5200d2023-01-19 11:32:25 -07001003 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301004 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301005
1006error_exit:
Julius Werner18881f992021-04-13 15:28:12 -07001007 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301008 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301009}
1010
Anil Kumar7b2edc32023-04-18 11:03:34 -07001011static bool is_psr_data_backed_up(void)
1012{
1013 /* Track PSR backup status in CMOS */
1014 return (get_psr_backup_status() == PSR_BACKUP_DONE);
1015}
1016
Anil Kumara2d10bb2023-05-01 11:44:47 -07001017static bool is_psr_supported(void)
1018{
1019 uint32_t feature_status;
1020
1021 /*
1022 * Check if SoC has support for PSR feature typically PSR feature
1023 * is only supported by vpro SKU
1024 *
1025 */
1026 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
1027 printk(BIOS_ERR, "cse_get_fw_feature_state command failed !\n");
1028 return false;
1029 }
1030
1031 if (!(feature_status & ME_FW_FEATURE_PSR)) {
1032 printk(BIOS_DEBUG, "PSR is not supported in this SKU !\n");
1033 return false;
1034 }
1035
1036 return true;
1037}
1038
Anil Kumar7b2edc32023-04-18 11:03:34 -07001039/*
1040 * PSR data needs to be backed up prior to downgrade. So switch the CSE boot mode to RW, send
1041 * PSR back-up command to CSE and update the PSR back-up state in CMOS.
1042 */
1043static void backup_psr_data(void)
1044{
1045 printk(BIOS_DEBUG, "cse_lite: Initiate PSR data backup flow\n");
1046 /* Switch CSE to RW to send PSR_HECI_FW_DOWNGRADE_BACKUP command */
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301047 if (cse_boot_to_rw() != CB_SUCCESS) {
1048 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001049 goto update_and_exit;
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301050 }
Anil Kumar7b2edc32023-04-18 11:03:34 -07001051 /*
Anil Kumara2d10bb2023-05-01 11:44:47 -07001052 * The function to check for PSR feature support can only be called after
1053 * switching to RW partition. The command MKHI_FWCAPS_GET_FW_FEATURE_STATE
1054 * that gives feature state is supported by a process that is loaded only
1055 * when CSE boots from RW.
1056 *
1057 */
1058 if (!is_psr_supported())
1059 goto update_and_exit;
1060
1061 /*
Anil Kumar7b2edc32023-04-18 11:03:34 -07001062 * Prerequisites:
1063 * 1) HFSTS1 Current Working State is Normal
1064 * 2) HFSTS1 Current Operation Mode is Normal
1065 */
1066 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
1067 printk(BIOS_DEBUG, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1068 "prerequisites not met!\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301069 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001070 goto update_and_exit;
1071 }
1072
1073 /* Send PSR_HECI_FW_DOWNGRADE_BACKUP command */
1074 struct psr_heci_fw_downgrade_backup_req {
1075 struct psr_heci_header header;
1076 } __packed;
1077
1078 struct psr_heci_fw_downgrade_backup_req req = {
1079 .header.command = PSR_HECI_FW_DOWNGRADE_BACKUP,
1080 };
1081
1082 struct psr_heci_fw_downgrade_backup_res {
1083 struct psr_heci_header header;
1084 uint32_t status;
1085 } __packed;
1086
1087 struct psr_heci_fw_downgrade_backup_res backup_psr_resp;
1088 size_t resp_size = sizeof(backup_psr_resp);
1089
1090 printk(BIOS_DEBUG, "cse_lite: Send PSR_HECI_FW_DOWNGRADE_BACKUP command\n");
1091 if (heci_send_receive(&req, sizeof(req),
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301092 &backup_psr_resp, &resp_size, HECI_PSR_ADDR)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -07001093 printk(BIOS_ERR, "cse_lite: could not backup PSR data\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301094 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP, ELOG_PSR_DATA_BACKUP_FAILED);
1095 } else {
1096 if (backup_psr_resp.status != PSR_STATUS_SUCCESS) {
Anil Kumar7b2edc32023-04-18 11:03:34 -07001097 printk(BIOS_ERR, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
1098 "returned %u\n", backup_psr_resp.status);
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301099 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1100 ELOG_PSR_DATA_BACKUP_FAILED);
1101 } else {
1102 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
1103 ELOG_PSR_DATA_BACKUP_SUCCESS);
1104 }
1105 }
Anil Kumar7b2edc32023-04-18 11:03:34 -07001106
1107update_and_exit:
1108 /*
1109 * An attempt to send PSR back-up command has been made. Update this info in CMOS and
1110 * send success once backup_psr_data() has been called. We do not want to put the system
1111 * into recovery for PSR data backup command pre-requisites not being met.
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301112 * We cannot do much if CSE fails to backup the PSR data, except create an event log.
Anil Kumar7b2edc32023-04-18 11:03:34 -07001113 */
1114 update_psr_backup_status(PSR_BACKUP_DONE);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001115}
1116
1117static void initiate_psr_data_backup(void)
1118{
1119 if (is_psr_data_backed_up())
1120 return;
1121
1122 backup_psr_data();
1123}
1124
Anil Kumare46af3f2023-10-03 21:31:53 -07001125/*
1126 * Check if a CSE Firmware update is required
1127 * returns true if an update is required, false otherwise
1128 */
1129bool is_cse_fw_update_required(void)
1130{
1131 struct fw_version cbfs_rw_version;
1132
1133 if (!is_cse_fw_update_enabled())
1134 return false;
1135
1136 /*
1137 * First, check if cse_bp_info_rsp global structure is populated.
1138 * If not, it implies that cse_fill_bp_info() function is not called.
1139 */
1140 if (!is_cse_bp_info_valid(&cse_bp_info_rsp))
1141 return false;
1142
1143 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
1144 return false;
1145
1146 return !!cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
1147}
1148
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301149static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301150{
1151 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001152 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301153
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301154 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +05301155 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1156 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1157 }
1158
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301159 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001160 if (status == CSE_UPDATE_NOT_REQUIRED)
1161 return CSE_NO_ERROR;
1162 if (status == CSE_UPDATE_METADATA_ERROR)
1163 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Anil Kumar7b2edc32023-04-18 11:03:34 -07001164 if (CONFIG(SOC_INTEL_CSE_LITE_PSR) && status == CSE_UPDATE_DOWNGRADE)
1165 initiate_psr_data_backup();
Rizwan Qureshiec321092019-09-06 20:28:43 +05301166
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001167 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301168 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301169}
1170
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301171static const char *cse_sub_part_str(enum bpdt_entry_type type)
1172{
1173 switch (type) {
1174 case IOM_FW:
1175 return "IOM";
1176 case NPHY_FW:
1177 return "NPHY";
1178 default:
1179 return "Unknown";
1180 }
1181}
1182
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301183static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301184{
1185 struct region_device cse_region_rdev;
1186 uint32_t size;
1187 uint32_t start_offset;
1188 uint32_t end_offset;
1189
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301190 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1191 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301192
1193 if (!strcmp(cse_regions[bp], "RO"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301194 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301195 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301196 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301197
1198 size = end_offset + 1 - start_offset;
1199
1200 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301201 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301202
1203 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1204 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301205 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301206}
1207
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301208static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1209 enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301210{
1211 struct bpdt_header bpdt_hdr;
1212 struct region_device cse_rdev;
1213 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1214 uint8_t i;
1215
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301216 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301217 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1218 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301219 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301220 }
1221
1222 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1223 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301224 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301225 }
1226
1227 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1228 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1229 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1230 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301231 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301232 }
1233
1234 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1235 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1236 if (bpdt_entries[i].type == type) {
1237 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1238 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1239 bpdt_entries[i].size);
1240
1241 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1242 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301243 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301244 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301245 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301246 }
1247 }
1248
1249 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301250 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301251}
1252
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301253static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301254 const struct region_device *rdev,
1255 struct fw_version *fw_ver)
1256{
1257 struct subpart_entry subpart_entry;
1258 struct subpart_entry_manifest_header man_hdr;
1259
1260 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1261 != SUBPART_ENTRY_SZ) {
1262 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1263 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301264 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301265 }
1266
1267 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1268 != SUBPART_MANIFEST_HDR_SZ) {
1269 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1270 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301271 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301272 }
1273
1274 fw_ver->major = man_hdr.binary_version.major;
1275 fw_ver->minor = man_hdr.binary_version.minor;
1276 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1277 fw_ver->build = man_hdr.binary_version.build;
1278
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301279 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301280}
1281
1282static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1283{
1284 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1285 struct subpart_entry *subpart_entry;
1286 struct subpart_entry_manifest_header *man_hdr;
1287
Elyes Haouas9018dee2022-11-18 15:07:33 +01001288 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1289 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301290
1291 fw_ver->major = man_hdr->binary_version.major;
1292 fw_ver->minor = man_hdr->binary_version.minor;
1293 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1294 fw_ver->build = man_hdr->binary_version.build;
1295}
1296
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301297static enum cb_err cse_prep_for_component_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301298{
1299 /*
1300 * To set CSE's operation mode to HMRFPO mode:
1301 * 1. Ensure CSE to boot from RO(BP1)
1302 * 2. Send HMRFPO_ENABLE command to CSE
1303 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301304 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301305 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301306
1307 return cse_hmrfpo_enable();
1308}
1309
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301310static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1311 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301312 struct region_device *target_rdev)
1313{
1314 if (region_device_sz(target_rdev) < blob_sz) {
1315 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1316 "smaller than blob size:%zx, abort update\n",
1317 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1318 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1319 }
1320
1321 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301322 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301323 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1324
1325 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301326 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301327 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1328
1329 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1330 cse_sub_part_str(type));
1331
1332 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1333}
1334
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301335static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301336{
1337 switch (rv) {
1338 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1339 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1340 return rv;
1341 default:
1342 cse_trigger_vboot_recovery(rv);
1343 }
1344 /* Control never reaches here */
1345 return rv;
1346}
1347
1348static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301349 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301350{
1351 struct region_device target_rdev;
1352 struct fw_version target_fw_ver, source_fw_ver;
1353 enum csme_failure_reason rv;
1354 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301355
1356 void *subpart_cbfs_rw = cbfs_map(name, &size);
1357 if (!subpart_cbfs_rw) {
1358 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1359 cse_sub_part_str(type));
1360 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1361 }
1362
1363 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1364 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1365 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1366 source_fw_ver.build);
1367
1368 /* Trigger sub-partition update in CSE RO and CSE RW */
1369 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301370 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301371 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1372 goto error_exit;
1373 }
1374
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301375 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301376 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1377 goto error_exit;
1378 }
1379
1380 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1381 cse_sub_part_str(type), target_fw_ver.major,
1382 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1383
1384 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1385 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1386 cse_regions[bp], cse_sub_part_str(type));
1387 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1388 continue;
1389 }
1390
1391 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1392 cse_sub_part_str(type));
1393
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301394 if (cse_prep_for_component_update() != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301395 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1396 goto error_exit;
1397 }
1398
1399 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1400 size, &target_rdev);
1401
1402 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1403 goto error_exit;
1404 }
1405error_exit:
1406 cbfs_unmap(subpart_cbfs_rw);
1407 return rv;
1408}
1409
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301410static enum csme_failure_reason cse_sub_part_fw_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301411{
1412 if (skip_cse_sub_part_update()) {
1413 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1414 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1415 }
1416
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301417 enum csme_failure_reason rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301418 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301419
1420 handle_cse_sub_part_fw_update_rv(rv);
1421
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301422 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301423
1424 return handle_cse_sub_part_fw_update_rv(rv);
1425}
1426
Subrata Banik5ff01182023-04-20 11:08:17 +05301427static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301428{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301429 /*
1430 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1431 * is not enabled and continue to update CSE sub-partitions.
1432 */
1433 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301434 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301435 return;
1436 }
1437
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301438 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1439 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301440 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301441 return;
1442 }
1443
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301444 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301445 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301446
1447 /* If system is in recovery mode, don't trigger recovery again */
1448 if (!vboot_recovery_mode_enabled()) {
1449 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1450 } else {
1451 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1452 "so no action\n");
1453 return;
1454 }
1455 }
1456
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +05301457 /* Store the CSE RW Firmware Version into CBMEM */
1458 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1459 cse_store_rw_fw_version();
1460
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301461 /*
1462 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301463 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301464 * fails during recovery, just continue to boot.
1465 */
1466 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301467 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301468 cse_board_reset();
1469 do_global_reset();
1470 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1471 }
1472
1473 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301474 }
1475
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301476 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001477 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301478
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301479 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001480 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301481 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1482 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301483 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301484 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301485 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301486 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301487 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001488 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301489 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301490
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301491 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301492 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301493
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301494 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001495 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301496
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301497 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301498 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001499 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301500 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301501}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301502
Subrata Banik5ff01182023-04-20 11:08:17 +05301503void cse_fw_sync(void)
1504{
Kapil Porwal83cd6f92024-05-26 16:23:17 +00001505 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1506 return;
1507
Subrata Banik5ff01182023-04-20 11:08:17 +05301508 timestamp_add_now(TS_CSE_FW_SYNC_START);
1509 do_cse_fw_sync();
1510 timestamp_add_now(TS_CSE_FW_SYNC_END);
1511}
1512
Subrata Banik7f66adb2023-04-14 00:35:38 +05301513static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1514 struct fw_version_resp *resp)
1515{
1516 enum cse_tx_rx_status ret;
1517 struct fw_version_msg {
1518 struct mkhi_hdr hdr;
1519 enum fpt_partition_id partition_id;
1520 } __packed msg = {
1521 .hdr = {
1522 .group_id = MKHI_GROUP_ID_GEN,
1523 .command = GEN_GET_IMAGE_FW_VERSION,
1524 },
1525 .partition_id = id,
1526 };
1527
1528 /*
1529 * Prerequisites:
1530 * 1) HFSTS1 CWS is Normal
1531 * 2) HFSTS1 COM is Normal
1532 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1533 */
1534
1535 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1536 !cse_is_hfs1_com_normal()) {
1537 printk(BIOS_ERR,
1538 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1539 return CB_ERR;
1540 }
1541
1542 size_t resp_size = sizeof(struct fw_version_resp);
1543 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1544
1545 if (ret || resp->hdr.result) {
1546 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1547 id, resp->hdr.result);
1548 return CB_ERR;
1549 }
1550
1551 return CB_SUCCESS;
1552}
1553
Subrata Banik044fc9f2023-04-14 02:34:37 +05301554static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1555 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301556{
1557 if (vboot_recovery_mode_enabled()) {
1558 printk(BIOS_WARNING,
1559 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1560 return CB_ERR;
1561 }
1562
1563 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1564 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1565 return CB_ERR;
1566 }
1567
1568 return send_get_fpt_partition_info_cmd(id, resp);
1569}
1570
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001571static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1572{
1573 const struct fw_version invalid_fw = {0, 0, 0, 0};
1574 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1575 return false;
1576 return true;
1577}
1578
Subrata Banikb1b7c532023-04-14 01:36:13 +05301579/*
1580 * Helper function to read ISH version from CSE FPT using HECI command.
1581 *
1582 * The HECI command only be executed after memory has been initialized.
1583 * This is because the command relies on resources that are not available
1584 * until DRAM initialization command has been sent.
1585 */
1586static void store_ish_version(void)
1587{
1588 if (!ENV_RAMSTAGE)
1589 return;
1590
1591 if (vboot_recovery_mode_enabled())
1592 return;
1593
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001594 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1595 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301596 return;
1597
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001598 struct cse_specific_info cse_info_in_cmos;
1599 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001600
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001601 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1602 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301603
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001604 /* Get current cse firmware state */
1605 enum cse_fw_state fw_state = get_cse_state(
1606 &(cbmem_version->cur_cse_fw_version),
1607 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1608 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301609
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001610 if (fw_state == CSE_FW_WARM_BOOT) {
1611 return;
1612 } else {
1613 if (fw_state == CSE_FW_COLD_BOOT &&
1614 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1615 /* CMOS data is persistent across cold boots */
1616 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1617 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1618 sizeof(struct cse_fw_ish_version_info));
1619 store_cse_info_crc(cse_info_in_cbmem);
1620 } else {
1621 /*
1622 * Current running CSE version is different than previous stored CSE version
1623 * which could be due to CSE update or rollback, hence, need to send ISHC
1624 * partition info cmd to know the currently running ISH version.
1625 */
1626 struct fw_version_resp resp;
1627 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1628 &resp) == CB_SUCCESS) {
1629 /* Update stored CSE version with current cse version */
1630 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1631 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001632
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001633 /* Retrieve and update current ish version */
1634 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1635 &(resp.manifest_data.version), sizeof(struct fw_version));
1636
1637 /* Update the CRC */
1638 store_cse_info_crc(cse_info_in_cbmem);
1639
1640 /* Update CMOS with current CSE FPT versions.*/
1641 cmos_write_fw_partition_info(cse_info_in_cbmem);
1642 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301643 }
1644 }
1645}
1646
Subrata Banikfc313d62023-04-14 01:31:29 +05301647static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301648{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301649 if (acpi_get_sleep_type() == ACPI_S3)
1650 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301651
Subrata Banik5ff01182023-04-20 11:08:17 +05301652 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301653 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301654
1655 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301656 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301657 * is available
1658 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301659 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301660 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301661}
1662
Subrata Banikfc313d62023-04-14 01:31:29 +05301663BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);