blob: 9ea6c782254c8d9ae81be31d212ec971f010e065 [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>
Rizwan Qureshiec321092019-09-06 20:28:43 +053010#include <fmap.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010011#include <intelbasecode/debug_feature.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053012#include <intelblocks/cse.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053013#include <intelblocks/cse_layout.h>
Subrata Banikda527ec2022-11-24 15:42:35 +053014#include <intelblocks/spi.h>
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +053015#include <intelblocks/systemagent.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 {
86
87 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
88 BP_INFO_REDUNDANCY_EN = 1 << 0,
89
90 /* It indicates RO(BP1) supports Minimal Recovery Mode */
91 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
92
93 /*
94 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
95 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
96 */
97 BP_INFO_READ_ONLY_CFG = 1 << 2,
98};
99
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530100/* CSE boot partition entry info */
101struct cse_bp_entry {
102 /* Boot partition version */
103 struct fw_version fw_ver;
104
105 /* Boot partition status */
106 uint32_t status;
107
108 /* Starting offset of the partition within CSE region */
109 uint32_t start_offset;
110
111 /* Ending offset of the partition within CSE region */
112 uint32_t end_offset;
113 uint8_t reserved[12];
114} __packed;
115
116/* CSE boot partition info */
117struct cse_bp_info {
118 /* Number of boot partitions */
119 uint8_t total_number_of_bp;
120
121 /* Current boot partition */
122 uint8_t current_bp;
123
124 /* Next boot partition */
125 uint8_t next_bp;
126
127 /* Boot Partition Info Flags */
128 uint8_t flags;
129
130 /* Boot Partition Entry Info */
131 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
132} __packed;
133
134struct get_bp_info_rsp {
135 struct mkhi_hdr hdr;
136 struct cse_bp_info bp_info;
137} __packed;
138
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530139static struct get_bp_info_rsp cse_bp_info_rsp;
140
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000141enum cse_fw_state {
142 /* The CMOS and CBMEM have the current fw version. */
143 CSE_FW_WARM_BOOT,
144
145 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
146 CSE_FW_COLD_BOOT,
147
148 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
149 CSE_FW_INVALID,
150};
151
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530152static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700153
Subrata Banik65a6d172023-08-13 13:03:50 +0000154static struct cse_specific_info cse_info;
155
Subrata Banikda527ec2022-11-24 15:42:35 +0530156void cse_log_ro_write_protection_info(bool mfg_mode)
157{
158 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
159
160 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
161 cse_ro_wp_en ? "YES" : "NO");
162
163 if (cse_ro_wp_en) {
164 uint32_t base, limit;
165 spi_get_wp_cse_ro_range(&base, &limit);
166 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
167 base, limit);
168 }
169
170 /*
171 * If manufacturing mode is disabled, but CSE RO is not write protected,
172 * log error.
173 */
174 if (!mfg_mode && !cse_ro_wp_en)
175 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
176}
177
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530178enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700179{
180 struct cse_boot_perf_req {
181 struct mkhi_hdr hdr;
182 uint32_t reserved;
183 } __packed;
184
185 struct cse_boot_perf_req req = {
186 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
187 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
188 .reserved = 0,
189 };
190
191 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
192
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530193 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700194 HECI_MKHI_ADDR)) {
195 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530196 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700197 }
198
199 if (boot_perf_rsp->hdr.result) {
200 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
201 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530202 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700203 }
204
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530205 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700206}
207
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530208static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530209{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530210 return &cse_bp_info_rsp.bp_info;
211}
212
213static uint8_t cse_get_current_bp(void)
214{
215 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530216 return cse_bp_info->current_bp;
217}
218
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530219static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530220{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530221 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530222 return &cse_bp_info->bp_entries[bp];
223}
224
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000225static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000226{
227 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
228
229 /*
230 * Authenticate the CBMEM persistent data.
231 *
232 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
233 * could change the values stored in this region has to also trigger the global
234 * reset. Hence, CBMEM persistent data won't be available any time after such
235 * event (global reset or cold reset) being initiated.
236 *
237 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
238 * want to override the existing data in CBMEM to avoid any additional boot latency.
239 */
240 if (info->crc != crc)
241 return false;
242
243 return true;
244}
245
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000246static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000247{
248 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
249}
250
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000251static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
252 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
253{
254 enum cse_fw_state state = CSE_FW_WARM_BOOT;
255 size_t size = sizeof(struct fw_version);
256 /*
257 * Compare if stored CSE version (from the previous boot) is same as current
258 * running CSE version.
259 */
260 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
261 /*
262 * CMOS CSE versioin is invalid, possibly two scenarios
263 * 1. CSE FW update
264 * 2. First boot
265 */
266 state = CSE_FW_INVALID;
267 } else {
268 /*
269 * Check if current running CSE version is same as previous stored CSE
270 * version aka CBMEM region is still valid.
271 */
272 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
273 state = CSE_FW_COLD_BOOT;
274 }
275 return state;
276}
277
Subrata Banik65a6d172023-08-13 13:03:50 +0000278/*
279 * Helper function that stores current CSE firmware version to CBMEM memory,
280 * except during recovery mode.
281 */
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530282static void cse_store_rw_fw_version(void)
Subrata Banik65a6d172023-08-13 13:03:50 +0000283{
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530284 const struct cse_bp_entry *cse_bp;
285 cse_bp = cse_get_bp_entry(RW);
286
Subrata Banik65a6d172023-08-13 13:03:50 +0000287 if (vboot_recovery_mode_enabled())
288 return;
289
290 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000291 /* update current CSE version and return */
292 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
293 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000294 return;
295 }
296
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000297 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
298 sizeof(*cse_info_in_cbmem));
299 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000300 return;
301
302 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000303 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000304 return;
305
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000306 struct cse_specific_info cse_info_in_cmos;
307 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000308
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000309 /* Get current cse firmware state */
310 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
311 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
312 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
313
314 /* Reset CBMEM data and update current CSE version */
315 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
316 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
317 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000318
319 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000320 store_cse_info_crc(cse_info_in_cbmem);
321
322 if (fw_state == CSE_FW_INVALID) {
323 /*
324 * Current CMOS data is outdated, which could be due to CSE update or
325 * rollback, hence, need to update CMOS with current CSE FPT versions.
326 */
327 cmos_write_fw_partition_info(cse_info_in_cbmem);
328 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000329}
330
331#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
332/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
333static void preram_cse_info_sync_to_cbmem(int is_recovery)
334{
335 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
336 return;
337
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000338 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
339 sizeof(*cse_info_in_cbmem));
340 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000341 return;
342
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000343 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
344 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000345 return;
346
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000347 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
348 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
349 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000350
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000351 struct cse_specific_info cse_info_in_cmos;
352 cmos_read_fw_partition_info(&cse_info_in_cmos);
353
354 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
355 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
356 sizeof(struct fw_version))) {
357 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
358 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
359 return;
360 }
361
362 /*
363 * Current CMOS data is outdated, which could be due to CSE update or
364 * rollback, hence, need to update CMOS with current CSE FPT versions.
365 */
366 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000367}
368
369CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
370#endif
371
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530372static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530373{
374 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530375 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530376
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530377 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
378 cse_bp_info->total_number_of_bp);
379 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
380 GET_BP_STR(cse_bp_info->current_bp));
381 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
382 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530383
384 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530385 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530386 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 +0530387 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
388 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
389 cse_bp->status, cse_bp->start_offset,
390 cse_bp->end_offset);
391
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530392 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530393 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 +0530394 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
395 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
396 cse_bp->status, cse_bp->start_offset,
397 cse_bp->end_offset);
398}
399
400/*
401 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
402 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
403 * It allows execution of the Boot Partition commands in below scenarios:
404 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
405 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
406 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530407 * The prerequisite check should be handled in cse_get_bp_info() and
408 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
409 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530410 */
411static bool cse_is_bp_cmd_info_possible(void)
412{
413 if (cse_is_hfs1_cws_normal()) {
414 if (cse_is_hfs1_com_normal())
415 return true;
416 if (cse_is_hfs1_com_secover_mei_msg())
417 return true;
418 if (cse_is_hfs1_com_soft_temp_disable())
419 return true;
420 }
421 return false;
422}
423
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530424static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
425{
426 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
427
428 if (cse_bp_info_in_cbmem != NULL)
429 return cse_bp_info_in_cbmem;
430
431 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
432 sizeof(struct get_bp_info_rsp));
433
434 if (!cse_bp_info_in_cbmem) {
435 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
436 return NULL;
437 }
438
439 /* Copy the CSE Boot Partition Info command response to cbmem */
440 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
441
442 return cse_bp_info_in_cbmem;
443}
444
445static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
446{
447 /*
448 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
449 * then return false to indicate cse_bp_info is not valid.
450 */
451 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
452 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
453 !bp_info_rsp->hdr.is_resp) ? false : true;
454}
455
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530456static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530457{
458 struct get_bp_info_req {
459 struct mkhi_hdr hdr;
460 uint8_t reserved[4];
461 } __packed;
462
463 struct get_bp_info_req info_req = {
464 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
465 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
466 .reserved = {0},
467 };
468
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530469 /*
470 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
471 * initialized, check if there is cse bp info response stored in cbmem. Once the data
472 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
473 * functions. In case, data is not available in cbmem or invalid, continue to send the
474 * GET_BOOT_PARTITION_INFO command, else return.
475 */
476 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
477 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
478 if (cse_bp_info_in_cbmem) {
479 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
480 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
481 sizeof(struct get_bp_info_rsp));
482 return CB_SUCCESS;
483 }
484 }
485 } else {
486 /*
487 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
488 * global cse bp info response stored in global cse_bp_info_rsp is valid.
489 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
490 * command, else return.
491 */
492 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
493 return CB_SUCCESS;
494 }
495
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530496 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530497 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530498 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530499 }
500
501 size_t resp_size = sizeof(struct get_bp_info_rsp);
502
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530503 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530504 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530505 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530506 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530507 }
508
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530509 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530510 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530511 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530512 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530513 }
514
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530515 cse_print_boot_partition_info();
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530516 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530517}
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530518
519void cse_fill_bp_info(void)
520{
521 if (vboot_recovery_mode_enabled())
522 return;
523
524 if (cse_get_bp_info() != CB_SUCCESS)
525 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
526}
527
528/* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
529static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
530{
531 if (vboot_recovery_mode_enabled())
532 return;
533
534 sync_cse_bp_info_to_cbmem();
535}
536
537CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
538
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530539/*
540 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
541 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
542 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
543 * This function must be used before EOP.
544 * Returns false on failure and true on success.
545 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530546static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530547{
548 struct set_boot_partition_info_req {
549 struct mkhi_hdr hdr;
550 uint8_t next_bp;
551 uint8_t reserved[3];
552 } __packed;
553
554 struct set_boot_partition_info_req switch_req = {
555 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
556 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
557 .next_bp = bp,
558 .reserved = {0},
559 };
560
561 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530562 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530563 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530564 }
565
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530566 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530567
568 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530569 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530570 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530571 }
572
573 struct mkhi_hdr switch_resp;
574 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
575
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530576 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530577 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530578 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530579
580 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530581 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530582 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530583 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530584 }
585
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530586 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530587}
588
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530589static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530590{
591 struct data_clr_request {
592 struct mkhi_hdr hdr;
593 uint8_t reserved[4];
594 } __packed;
595
596 struct data_clr_request data_clr_rq = {
597 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
598 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
599 .reserved = {0},
600 };
601
602 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530603 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530604 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530605 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530606 }
607
608 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
609
610 struct mkhi_hdr data_clr_rsp;
611 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
612
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530613 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530614 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530615 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530616 }
617
618 if (data_clr_rsp.result) {
619 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
620 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530621 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530622 }
623
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530624 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530625}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530626
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600627__weak void cse_board_reset(void)
628{
629 /* Default weak implementation, does nothing. */
630}
631
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700632__weak void cse_fw_update_misc_oper(void)
633{
634 /* Default weak implementation, does nothing. */
635}
636
Rizwan Qureshiec321092019-09-06 20:28:43 +0530637/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530638static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530639{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530640 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
641 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530642
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600643 /* Allow the board to perform a reset for CSE RO<->RW jump */
644 cse_board_reset();
645
646 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000647 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530648
Rizwan Qureshiec321092019-09-06 20:28:43 +0530649 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530650
651 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530652 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530653}
654
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530655static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530656{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530657 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530658 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530659
Rizwan Qureshiec321092019-09-06 20:28:43 +0530660 return cse_set_and_boot_from_next_bp(RW);
661}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530662
V Sowmyaf9905522020-11-12 20:19:04 +0530663/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530664static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530665{
666 const struct cse_bp_entry *rw_bp;
667
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530668 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530669 return rw_bp->status != BP_STATUS_DATA_FAILURE;
670}
671
672/*
673 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
674 * otherwise false if any operation fails.
675 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530676static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530677{
678 /*
679 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
680 * - Send DATA CLEAR HECI command to CSE
681 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
682 * - Issue GLOBAL RESET HECI command.
683 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530684 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530685 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530686
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530687 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530688 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530689
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530690 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530691}
692
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530693static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530694{
695 const struct cse_bp_entry *cse_bp;
696
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530697 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530698 return &cse_bp->fw_ver;
699}
700
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530701static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530702{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530703 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530704}
705
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530706static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
707 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530708{
709 const struct cse_bp_entry *cse_bp;
710
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530711 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530712
713 if (start_offset)
714 *start_offset = cse_bp->start_offset;
715
716 if (end_offset)
717 *end_offset = cse_bp->end_offset;
718
719}
720
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530721static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530722{
723 const struct cse_bp_entry *rw_bp;
724
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530725 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530726
727 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
728 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
729 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
730 return false;
731 }
732 return true;
733}
734
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530735static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530736{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530737 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530738 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530739
740 return cse_set_and_boot_from_next_bp(RO);
741}
742
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530743static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530744{
745 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
746 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530747 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530748 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530749 }
750
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530751 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530752}
753
Rizwan Qureshiec321092019-09-06 20:28:43 +0530754static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
755{
756 uint32_t cse_bp_sign;
757
758 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
759 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
760 return false;
761 }
762
763 return cse_bp_sign == CSE_RW_SIGNATURE;
764}
765
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530766static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530767{
768 struct region_device cse_region_rdev;
769 size_t size;
770 uint32_t start_offset;
771 uint32_t end_offset;
772
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530773 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
774 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530775
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530776 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530777 size = end_offset + 1 - start_offset;
778
779 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530780 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530781
782 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100783 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530784
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530785 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530786}
787
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530788static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530789{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530790 struct vb2_context *ctx = vboot_get_context();
791 if (ctx == NULL)
792 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530793
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530794 if (vboot_is_firmware_slot_a(ctx))
795 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
796
797 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
798}
799
Rizwan Qureshiec321092019-09-06 20:28:43 +0530800/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530801 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
802 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530803 * If ver_cmp_status = 0, no update is required
804 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
805 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
806 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530807static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530808{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700809 if (a->major != b->major)
810 return a->major - b->major;
811 else if (a->minor != b->minor)
812 return a->minor - b->minor;
813 else if (a->hotfix != b->hotfix)
814 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530815 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700816 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530817}
818
819/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
820static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
821 const void *rw_blob, const size_t rw_blob_sz)
822
823{
Julius Wernerd96ca242022-08-08 18:08:35 -0700824 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530825
Julius Wernerd96ca242022-08-08 18:08:35 -0700826 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
827 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530828 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
829 return false;
830 }
831
Julius Wernerd96ca242022-08-08 18:08:35 -0700832 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530833 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
834 "the provided SHA in the metadata\n");
835 return false;
836 }
837 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
838 " provided hash in the metadata\n");
839 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530840}
841
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530842static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530843{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530844 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
845 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530846 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530847 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530848 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530849}
850
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530851static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530852 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530853{
854 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
855 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530856 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530857 }
858
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530859 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530860}
861
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700862enum cse_update_status {
863 CSE_UPDATE_NOT_REQUIRED,
864 CSE_UPDATE_UPGRADE,
865 CSE_UPDATE_DOWNGRADE,
866 CSE_UPDATE_CORRUPTED,
867 CSE_UPDATE_METADATA_ERROR,
868};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530869
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700870static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
871{
872 if ((*curr - start) >= size) {
873 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
874 return false;
875 }
876
877 *ver_field = skip_atoi(curr);
878 (*curr)++;
879 return true;
880}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530881
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530882static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
883{
884 char *version_str, *cbfs_ptr;
885 size_t size;
886
887 if (cbfs_rw_version == NULL)
888 return CB_ERR;
889
890 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
891 version_str = cbfs_ptr;
892 if (!version_str) {
893 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
894 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
895 return CB_ERR;
896 }
897
898 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
899 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
900 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
901 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
902 cbfs_unmap(version_str);
903 return CB_ERR;
904 }
905
906 cbfs_unmap(version_str);
907 return CB_SUCCESS;
908}
909
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530910static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530911{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700912 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700913 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700914
915 if (!cse_is_rw_bp_sign_valid(target_rdev))
916 return CSE_UPDATE_CORRUPTED;
917
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530918 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700919 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700920
921 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700922 cbfs_rw_version.major,
923 cbfs_rw_version.minor,
924 cbfs_rw_version.hotfix,
925 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700926
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530927 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700928 if (ret == 0)
929 return CSE_UPDATE_NOT_REQUIRED;
930 else if (ret < 0)
931 return CSE_UPDATE_DOWNGRADE;
932 else
933 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530934}
935
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530936static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530937 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530938{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530939 /* Points to CSE CBFS RW image after boot partition signature */
940 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
941
942 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530943 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530944
945 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530946 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
947 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
948 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530949
950 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530951 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
952 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530953
954 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530955 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530956}
957
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530958static bool is_cse_fw_update_enabled(void)
959{
960 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
961 return false;
962
963 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
964 return !is_debug_cse_fw_update_disable();
965
966 return true;
967}
968
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530969static 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 +0530970 struct region_device *target_rdev)
971{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530972 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530973 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
974 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530975 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
976 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530977
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530978 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530979 return CSE_LITE_SKU_FW_UPDATE_ERROR;
980
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530981 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530982 return CSE_LITE_SKU_FW_UPDATE_ERROR;
983
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600984 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530985}
986
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530987static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530988{
989 /*
990 * To set CSE's operation mode to HMRFPO mode:
991 * 1. Ensure CSE to boot from RO(BP1)
992 * 2. Send HMRFPO_ENABLE command to CSE
993 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530994 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530995 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530996
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700997 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530998 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700999 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +05301000 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001001 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301002 }
1003
Rizwan Qureshiec321092019-09-06 20:28:43 +05301004 return cse_hmrfpo_enable();
1005}
1006
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301007static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
1008 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301009{
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301010 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001011 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +05301012 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -07001013 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301014
Julius Werner18881f992021-04-13 15:28:12 -07001015 const char *area_name = cse_get_source_rdev_fmap();
1016 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301017 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301018
Krishna P Bhat D75a423e2022-04-20 15:50:06 +05301019 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
1020 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
1021 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
1022 } else {
1023 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
1024 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
1025 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301026 if (!cse_cbfs_rw) {
1027 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
1028 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
1029 }
1030
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001031 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
1032 if (!cbfs_rw_hash) {
1033 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
1034 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
1035 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
1036 goto error_exit;
1037 }
1038
Julius Werner18881f992021-04-13 15:28:12 -07001039 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301040 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
1041 goto error_exit;
1042 }
1043
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301044 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -06001045 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301046 goto error_exit;
1047 }
1048
Jeremy Compostella08b5200d2023-01-19 11:32:25 -07001049 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301050 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301051
1052error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -07001053 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -07001054 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301055 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301056}
1057
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301058static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301059{
1060 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001061 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301062
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301063 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +05301064 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1065 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1066 }
1067
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301068 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001069 if (status == CSE_UPDATE_NOT_REQUIRED)
1070 return CSE_NO_ERROR;
1071 if (status == CSE_UPDATE_METADATA_ERROR)
1072 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301073
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001074 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301075 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301076}
1077
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301078static const char *cse_sub_part_str(enum bpdt_entry_type type)
1079{
1080 switch (type) {
1081 case IOM_FW:
1082 return "IOM";
1083 case NPHY_FW:
1084 return "NPHY";
1085 default:
1086 return "Unknown";
1087 }
1088}
1089
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301090static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301091{
1092 struct region_device cse_region_rdev;
1093 uint32_t size;
1094 uint32_t start_offset;
1095 uint32_t end_offset;
1096
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301097 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1098 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301099
1100 if (!strcmp(cse_regions[bp], "RO"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301101 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301102 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301103 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301104
1105 size = end_offset + 1 - start_offset;
1106
1107 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301108 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301109
1110 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1111 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301112 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301113}
1114
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301115static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1116 enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301117{
1118 struct bpdt_header bpdt_hdr;
1119 struct region_device cse_rdev;
1120 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1121 uint8_t i;
1122
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301123 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301124 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1125 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301126 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301127 }
1128
1129 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1130 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301131 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301132 }
1133
1134 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1135 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1136 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1137 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301138 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301139 }
1140
1141 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1142 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1143 if (bpdt_entries[i].type == type) {
1144 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1145 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1146 bpdt_entries[i].size);
1147
1148 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1149 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301150 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301151 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301152 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301153 }
1154 }
1155
1156 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301157 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301158}
1159
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301160static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301161 const struct region_device *rdev,
1162 struct fw_version *fw_ver)
1163{
1164 struct subpart_entry subpart_entry;
1165 struct subpart_entry_manifest_header man_hdr;
1166
1167 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1168 != SUBPART_ENTRY_SZ) {
1169 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1170 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301171 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301172 }
1173
1174 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1175 != SUBPART_MANIFEST_HDR_SZ) {
1176 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1177 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301178 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301179 }
1180
1181 fw_ver->major = man_hdr.binary_version.major;
1182 fw_ver->minor = man_hdr.binary_version.minor;
1183 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1184 fw_ver->build = man_hdr.binary_version.build;
1185
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301186 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301187}
1188
1189static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1190{
1191 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1192 struct subpart_entry *subpart_entry;
1193 struct subpart_entry_manifest_header *man_hdr;
1194
Elyes Haouas9018dee2022-11-18 15:07:33 +01001195 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1196 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301197
1198 fw_ver->major = man_hdr->binary_version.major;
1199 fw_ver->minor = man_hdr->binary_version.minor;
1200 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1201 fw_ver->build = man_hdr->binary_version.build;
1202}
1203
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301204static enum cb_err cse_prep_for_component_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301205{
1206 /*
1207 * To set CSE's operation mode to HMRFPO mode:
1208 * 1. Ensure CSE to boot from RO(BP1)
1209 * 2. Send HMRFPO_ENABLE command to CSE
1210 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301211 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301212 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301213
1214 return cse_hmrfpo_enable();
1215}
1216
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301217static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1218 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301219 struct region_device *target_rdev)
1220{
1221 if (region_device_sz(target_rdev) < blob_sz) {
1222 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1223 "smaller than blob size:%zx, abort update\n",
1224 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1225 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1226 }
1227
1228 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301229 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301230 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1231
1232 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301233 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301234 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1235
1236 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1237 cse_sub_part_str(type));
1238
1239 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1240}
1241
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301242static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301243{
1244 switch (rv) {
1245 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1246 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1247 return rv;
1248 default:
1249 cse_trigger_vboot_recovery(rv);
1250 }
1251 /* Control never reaches here */
1252 return rv;
1253}
1254
1255static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301256 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301257{
1258 struct region_device target_rdev;
1259 struct fw_version target_fw_ver, source_fw_ver;
1260 enum csme_failure_reason rv;
1261 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301262
1263 void *subpart_cbfs_rw = cbfs_map(name, &size);
1264 if (!subpart_cbfs_rw) {
1265 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1266 cse_sub_part_str(type));
1267 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1268 }
1269
1270 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1271 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1272 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1273 source_fw_ver.build);
1274
1275 /* Trigger sub-partition update in CSE RO and CSE RW */
1276 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301277 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301278 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1279 goto error_exit;
1280 }
1281
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301282 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301283 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1284 goto error_exit;
1285 }
1286
1287 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1288 cse_sub_part_str(type), target_fw_ver.major,
1289 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1290
1291 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1292 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1293 cse_regions[bp], cse_sub_part_str(type));
1294 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1295 continue;
1296 }
1297
1298 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1299 cse_sub_part_str(type));
1300
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301301 if (cse_prep_for_component_update() != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301302 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1303 goto error_exit;
1304 }
1305
1306 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1307 size, &target_rdev);
1308
1309 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1310 goto error_exit;
1311 }
1312error_exit:
1313 cbfs_unmap(subpart_cbfs_rw);
1314 return rv;
1315}
1316
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301317static enum csme_failure_reason cse_sub_part_fw_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301318{
1319 if (skip_cse_sub_part_update()) {
1320 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1321 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1322 }
1323
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301324 enum csme_failure_reason rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301325 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301326
1327 handle_cse_sub_part_fw_update_rv(rv);
1328
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301329 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301330
1331 return handle_cse_sub_part_fw_update_rv(rv);
1332}
1333
Subrata Banik5ff01182023-04-20 11:08:17 +05301334static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301335{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301336 /*
1337 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1338 * is not enabled and continue to update CSE sub-partitions.
1339 */
1340 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301341 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301342 return;
1343 }
1344
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301345 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1346 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301347 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301348 return;
1349 }
1350
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301351 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301352 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301353
1354 /* If system is in recovery mode, don't trigger recovery again */
1355 if (!vboot_recovery_mode_enabled()) {
1356 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1357 } else {
1358 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1359 "so no action\n");
1360 return;
1361 }
1362 }
1363
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +05301364 /* Store the CSE RW Firmware Version into CBMEM */
1365 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1366 cse_store_rw_fw_version();
1367
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301368 /*
1369 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301370 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301371 * fails during recovery, just continue to boot.
1372 */
1373 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301374 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301375 cse_board_reset();
1376 do_global_reset();
1377 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1378 }
1379
1380 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301381 }
1382
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301383 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001384 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301385
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301386 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001387 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301388 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1389 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301390 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301391 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301392 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301393 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301394 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001395 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301396 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301397
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301398 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301399 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301400
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301401 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001402 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301403
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301404 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301405 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001406 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301407 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301408}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301409
Subrata Banik5ff01182023-04-20 11:08:17 +05301410void cse_fw_sync(void)
1411{
1412 timestamp_add_now(TS_CSE_FW_SYNC_START);
1413 do_cse_fw_sync();
1414 timestamp_add_now(TS_CSE_FW_SYNC_END);
1415}
1416
Subrata Banik7f66adb2023-04-14 00:35:38 +05301417static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1418 struct fw_version_resp *resp)
1419{
1420 enum cse_tx_rx_status ret;
1421 struct fw_version_msg {
1422 struct mkhi_hdr hdr;
1423 enum fpt_partition_id partition_id;
1424 } __packed msg = {
1425 .hdr = {
1426 .group_id = MKHI_GROUP_ID_GEN,
1427 .command = GEN_GET_IMAGE_FW_VERSION,
1428 },
1429 .partition_id = id,
1430 };
1431
1432 /*
1433 * Prerequisites:
1434 * 1) HFSTS1 CWS is Normal
1435 * 2) HFSTS1 COM is Normal
1436 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1437 */
1438
1439 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1440 !cse_is_hfs1_com_normal()) {
1441 printk(BIOS_ERR,
1442 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1443 return CB_ERR;
1444 }
1445
1446 size_t resp_size = sizeof(struct fw_version_resp);
1447 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1448
1449 if (ret || resp->hdr.result) {
1450 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1451 id, resp->hdr.result);
1452 return CB_ERR;
1453 }
1454
1455 return CB_SUCCESS;
1456}
1457
Subrata Banik044fc9f2023-04-14 02:34:37 +05301458static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1459 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301460{
1461 if (vboot_recovery_mode_enabled()) {
1462 printk(BIOS_WARNING,
1463 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1464 return CB_ERR;
1465 }
1466
1467 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1468 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1469 return CB_ERR;
1470 }
1471
1472 return send_get_fpt_partition_info_cmd(id, resp);
1473}
1474
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001475static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1476{
1477 const struct fw_version invalid_fw = {0, 0, 0, 0};
1478 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1479 return false;
1480 return true;
1481}
1482
Subrata Banikb1b7c532023-04-14 01:36:13 +05301483/*
1484 * Helper function to read ISH version from CSE FPT using HECI command.
1485 *
1486 * The HECI command only be executed after memory has been initialized.
1487 * This is because the command relies on resources that are not available
1488 * until DRAM initialization command has been sent.
1489 */
1490static void store_ish_version(void)
1491{
1492 if (!ENV_RAMSTAGE)
1493 return;
1494
1495 if (vboot_recovery_mode_enabled())
1496 return;
1497
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001498 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1499 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301500 return;
1501
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001502 struct cse_specific_info cse_info_in_cmos;
1503 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001504
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001505 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1506 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301507
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001508 /* Get current cse firmware state */
1509 enum cse_fw_state fw_state = get_cse_state(
1510 &(cbmem_version->cur_cse_fw_version),
1511 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1512 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301513
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001514 if (fw_state == CSE_FW_WARM_BOOT) {
1515 return;
1516 } else {
1517 if (fw_state == CSE_FW_COLD_BOOT &&
1518 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1519 /* CMOS data is persistent across cold boots */
1520 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1521 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1522 sizeof(struct cse_fw_ish_version_info));
1523 store_cse_info_crc(cse_info_in_cbmem);
1524 } else {
1525 /*
1526 * Current running CSE version is different than previous stored CSE version
1527 * which could be due to CSE update or rollback, hence, need to send ISHC
1528 * partition info cmd to know the currently running ISH version.
1529 */
1530 struct fw_version_resp resp;
1531 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1532 &resp) == CB_SUCCESS) {
1533 /* Update stored CSE version with current cse version */
1534 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1535 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001536
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001537 /* Retrieve and update current ish version */
1538 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1539 &(resp.manifest_data.version), sizeof(struct fw_version));
1540
1541 /* Update the CRC */
1542 store_cse_info_crc(cse_info_in_cbmem);
1543
1544 /* Update CMOS with current CSE FPT versions.*/
1545 cmos_write_fw_partition_info(cse_info_in_cbmem);
1546 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301547 }
1548 }
1549}
1550
Subrata Banikfc313d62023-04-14 01:31:29 +05301551static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301552{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301553 if (acpi_get_sleep_type() == ACPI_S3)
1554 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301555
Subrata Banik5ff01182023-04-20 11:08:17 +05301556 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301557 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301558
1559 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301560 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301561 * is available
1562 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301563 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301564 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301565}
1566
Subrata Banikfc313d62023-04-14 01:31:29 +05301567BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);