blob: 4e3a44661d7cf706d713bc2854a78635208ce82a [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>
Kapil Porwal53e5d1f2024-06-17 11:32:58 +000015#include <intelblocks/cse_lite.h>
Subrata Banikda527ec2022-11-24 15:42:35 +053016#include <intelblocks/spi.h>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053017#include <security/vboot/misc.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010018#include <security/vboot/vboot_common.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053019#include <soc/intel/common/reset.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +053020#include <timestamp.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053021
Dinesh Gehlota9232d82023-06-10 11:53:47 +000022#include "cse_lite_cmos.h"
23
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +053024static struct get_bp_info_rsp cse_bp_info_rsp;
25
Dinesh Gehlota9232d82023-06-10 11:53:47 +000026enum cse_fw_state {
27 /* The CMOS and CBMEM have the current fw version. */
28 CSE_FW_WARM_BOOT,
29
30 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
31 CSE_FW_COLD_BOOT,
32
33 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
34 CSE_FW_INVALID,
35};
36
Sridhar Siricilla52479c72022-03-30 09:25:49 +053037static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070038
Subrata Banik65a6d172023-08-13 13:03:50 +000039static struct cse_specific_info cse_info;
40
Subrata Banikda527ec2022-11-24 15:42:35 +053041void cse_log_ro_write_protection_info(bool mfg_mode)
42{
43 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
44
45 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
46 cse_ro_wp_en ? "YES" : "NO");
47
48 if (cse_ro_wp_en) {
49 uint32_t base, limit;
50 spi_get_wp_cse_ro_range(&base, &limit);
51 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
52 base, limit);
53 }
54
55 /*
56 * If manufacturing mode is disabled, but CSE RO is not write protected,
57 * log error.
58 */
59 if (!mfg_mode && !cse_ro_wp_en)
60 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
61}
62
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +053063enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070064{
65 struct cse_boot_perf_req {
66 struct mkhi_hdr hdr;
67 uint32_t reserved;
68 } __packed;
69
70 struct cse_boot_perf_req req = {
71 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
72 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
73 .reserved = 0,
74 };
75
76 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
77
Sridhar Siricilla6836da22022-02-23 23:36:45 +053078 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070079 HECI_MKHI_ADDR)) {
80 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +053081 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070082 }
83
84 if (boot_perf_rsp->hdr.result) {
85 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
86 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +053087 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070088 }
89
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +053090 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -070091}
92
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +053093static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053094{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +053095 return &cse_bp_info_rsp.bp_info;
96}
97
98static uint8_t cse_get_current_bp(void)
99{
100 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530101 return cse_bp_info->current_bp;
102}
103
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530104static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530105{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530106 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530107 return &cse_bp_info->bp_entries[bp];
108}
109
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000110static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000111{
112 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
113
114 /*
115 * Authenticate the CBMEM persistent data.
116 *
117 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
118 * could change the values stored in this region has to also trigger the global
119 * reset. Hence, CBMEM persistent data won't be available any time after such
120 * event (global reset or cold reset) being initiated.
121 *
122 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
123 * want to override the existing data in CBMEM to avoid any additional boot latency.
124 */
125 if (info->crc != crc)
126 return false;
127
128 return true;
129}
130
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000131static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000132{
133 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
134}
135
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000136static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
137 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
138{
139 enum cse_fw_state state = CSE_FW_WARM_BOOT;
140 size_t size = sizeof(struct fw_version);
141 /*
142 * Compare if stored CSE version (from the previous boot) is same as current
143 * running CSE version.
144 */
145 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
146 /*
147 * CMOS CSE versioin is invalid, possibly two scenarios
148 * 1. CSE FW update
149 * 2. First boot
150 */
151 state = CSE_FW_INVALID;
152 } else {
153 /*
154 * Check if current running CSE version is same as previous stored CSE
155 * version aka CBMEM region is still valid.
156 */
157 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
158 state = CSE_FW_COLD_BOOT;
159 }
160 return state;
161}
162
Subrata Banik65a6d172023-08-13 13:03:50 +0000163/*
164 * Helper function that stores current CSE firmware version to CBMEM memory,
165 * except during recovery mode.
166 */
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530167static void cse_store_rw_fw_version(void)
Subrata Banik65a6d172023-08-13 13:03:50 +0000168{
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530169 const struct cse_bp_entry *cse_bp;
170 cse_bp = cse_get_bp_entry(RW);
171
Subrata Banik65a6d172023-08-13 13:03:50 +0000172 if (vboot_recovery_mode_enabled())
173 return;
174
175 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000176 /* update current CSE version and return */
177 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
178 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000179 return;
180 }
181
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000182 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
183 sizeof(*cse_info_in_cbmem));
184 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000185 return;
186
187 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000188 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000189 return;
190
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000191 struct cse_specific_info cse_info_in_cmos;
192 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000193
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000194 /* Get current cse firmware state */
195 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
196 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
197 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
198
199 /* Reset CBMEM data and update current CSE version */
200 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
201 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
202 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000203
204 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000205 store_cse_info_crc(cse_info_in_cbmem);
206
207 if (fw_state == CSE_FW_INVALID) {
208 /*
209 * Current CMOS data is outdated, which could be due to CSE update or
210 * rollback, hence, need to update CMOS with current CSE FPT versions.
211 */
212 cmos_write_fw_partition_info(cse_info_in_cbmem);
213 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000214}
215
216#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
217/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
218static void preram_cse_info_sync_to_cbmem(int is_recovery)
219{
Kapil Porwal53e5d1f2024-06-17 11:32:58 +0000220 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
221 return;
222
Subrata Banik65a6d172023-08-13 13:03:50 +0000223 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
224 return;
225
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000226 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
227 sizeof(*cse_info_in_cbmem));
228 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000229 return;
230
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000231 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
232 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000233 return;
234
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000235 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
236 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
237 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000238
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000239 struct cse_specific_info cse_info_in_cmos;
240 cmos_read_fw_partition_info(&cse_info_in_cmos);
241
242 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
243 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
244 sizeof(struct fw_version))) {
245 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
246 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
247 return;
248 }
249
250 /*
251 * Current CMOS data is outdated, which could be due to CSE update or
252 * rollback, hence, need to update CMOS with current CSE FPT versions.
253 */
254 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000255}
256
257CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
258#endif
259
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530260static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530261{
262 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530263 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530264
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530265 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
266 cse_bp_info->total_number_of_bp);
267 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
268 GET_BP_STR(cse_bp_info->current_bp));
269 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
270 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530271
272 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530273 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530274 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 +0530275 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
276 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
277 cse_bp->status, cse_bp->start_offset,
278 cse_bp->end_offset);
279
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530280 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530281 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 +0530282 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
283 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
284 cse_bp->status, cse_bp->start_offset,
285 cse_bp->end_offset);
286}
287
288/*
289 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
290 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
291 * It allows execution of the Boot Partition commands in below scenarios:
292 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
293 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
294 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530295 * The prerequisite check should be handled in cse_get_bp_info() and
296 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
297 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530298 */
299static bool cse_is_bp_cmd_info_possible(void)
300{
301 if (cse_is_hfs1_cws_normal()) {
302 if (cse_is_hfs1_com_normal())
303 return true;
304 if (cse_is_hfs1_com_secover_mei_msg())
305 return true;
306 if (cse_is_hfs1_com_soft_temp_disable())
307 return true;
308 }
309 return false;
310}
311
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530312static struct get_bp_info_rsp *sync_cse_bp_info_to_cbmem(void)
313{
314 struct get_bp_info_rsp *cse_bp_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_BP_INFO);
315
316 if (cse_bp_info_in_cbmem != NULL)
317 return cse_bp_info_in_cbmem;
318
319 cse_bp_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_BP_INFO,
320 sizeof(struct get_bp_info_rsp));
321
322 if (!cse_bp_info_in_cbmem) {
323 printk(BIOS_ERR, "Unable to store Boot Parition Info in cbmem\n");
324 return NULL;
325 }
326
327 /* Copy the CSE Boot Partition Info command response to cbmem */
328 memcpy(cse_bp_info_in_cbmem, &cse_bp_info_rsp, sizeof(struct get_bp_info_rsp));
329
330 return cse_bp_info_in_cbmem;
331}
332
333static bool is_cse_bp_info_valid(struct get_bp_info_rsp *bp_info_rsp)
334{
335 /*
336 * In case the cse_bp_info_rsp header group ID, command is incorrect or is_resp is 0,
337 * then return false to indicate cse_bp_info is not valid.
338 */
339 return (bp_info_rsp->hdr.group_id != MKHI_GROUP_ID_BUP_COMMON ||
340 bp_info_rsp->hdr.command != MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO ||
341 !bp_info_rsp->hdr.is_resp) ? false : true;
342}
343
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530344static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530345{
346 struct get_bp_info_req {
347 struct mkhi_hdr hdr;
348 uint8_t reserved[4];
349 } __packed;
350
351 struct get_bp_info_req info_req = {
352 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
353 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
354 .reserved = {0},
355 };
356
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530357 /*
358 * If SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE config is selected and memory has been
359 * initialized, check if there is cse bp info response stored in cbmem. Once the data
360 * is validated, copy it to the global cse_bp_info_rsp so that it can be used by other
361 * functions. In case, data is not available in cbmem or invalid, continue to send the
362 * GET_BOOT_PARTITION_INFO command, else return.
363 */
364 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && cbmem_online()) {
365 struct get_bp_info_rsp *cse_bp_info_in_cbmem = sync_cse_bp_info_to_cbmem();
366 if (cse_bp_info_in_cbmem) {
367 if (is_cse_bp_info_valid(cse_bp_info_in_cbmem)) {
368 memcpy(&cse_bp_info_rsp, cse_bp_info_in_cbmem,
369 sizeof(struct get_bp_info_rsp));
370 return CB_SUCCESS;
371 }
372 }
373 } else {
374 /*
375 * If SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE config is selected, check if the
376 * global cse bp info response stored in global cse_bp_info_rsp is valid.
377 * In case, it is not valid, continue to send the GET_BOOT_PARTITION_INFO
378 * command, else return.
379 */
380 if (is_cse_bp_info_valid(&cse_bp_info_rsp))
381 return CB_SUCCESS;
382 }
383
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530384 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530385 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530386 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530387 }
388
389 size_t resp_size = sizeof(struct get_bp_info_rsp);
390
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530391 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530392 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530393 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530394 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530395 }
396
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530397 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530398 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530399 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530400 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530401 }
402
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530403 cse_print_boot_partition_info();
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530404 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530405}
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530406
407void cse_fill_bp_info(void)
408{
Kapil Porwal53e5d1f2024-06-17 11:32:58 +0000409 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
410 return;
411
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530412 if (vboot_recovery_mode_enabled())
413 return;
414
415 if (cse_get_bp_info() != CB_SUCCESS)
416 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
417}
418
419/* Function to copy PRERAM CSE BP info to pertinent CBMEM. */
420static void preram_cse_bp_info_sync_to_cbmem(int is_recovery)
421{
Kapil Porwal53e5d1f2024-06-17 11:32:58 +0000422 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
423 return;
424
Krishna Prasad Bhat4f062ec2023-09-21 23:33:34 +0530425 if (vboot_recovery_mode_enabled())
426 return;
427
428 sync_cse_bp_info_to_cbmem();
429}
430
431CBMEM_CREATION_HOOK(preram_cse_bp_info_sync_to_cbmem);
432
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530433/*
434 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
435 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
436 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
437 * This function must be used before EOP.
438 * Returns false on failure and true on success.
439 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530440static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530441{
442 struct set_boot_partition_info_req {
443 struct mkhi_hdr hdr;
444 uint8_t next_bp;
445 uint8_t reserved[3];
446 } __packed;
447
448 struct set_boot_partition_info_req switch_req = {
449 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
450 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
451 .next_bp = bp,
452 .reserved = {0},
453 };
454
455 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530456 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530457 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530458 }
459
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530460 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530461
462 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530463 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530464 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530465 }
466
467 struct mkhi_hdr switch_resp;
468 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
469
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530470 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530471 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530472 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530473
474 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530475 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530476 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530477 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530478 }
479
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530480 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530481}
482
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530483static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530484{
485 struct data_clr_request {
486 struct mkhi_hdr hdr;
487 uint8_t reserved[4];
488 } __packed;
489
490 struct data_clr_request data_clr_rq = {
491 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
492 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
493 .reserved = {0},
494 };
495
496 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530497 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530498 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530499 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530500 }
501
502 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
503
504 struct mkhi_hdr data_clr_rsp;
505 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
506
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530507 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530508 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530509 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530510 }
511
512 if (data_clr_rsp.result) {
513 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
514 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530515 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530516 }
517
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530518 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530519}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530520
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600521__weak void cse_board_reset(void)
522{
523 /* Default weak implementation, does nothing. */
524}
525
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700526__weak void cse_fw_update_misc_oper(void)
527{
528 /* Default weak implementation, does nothing. */
529}
530
Rizwan Qureshiec321092019-09-06 20:28:43 +0530531/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530532static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530533{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530534 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
535 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530536
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600537 /* Allow the board to perform a reset for CSE RO<->RW jump */
538 cse_board_reset();
539
540 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000541 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530542
Rizwan Qureshiec321092019-09-06 20:28:43 +0530543 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530544
545 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530546 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530547}
548
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530549static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530550{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530551 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530552 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530553
Rizwan Qureshiec321092019-09-06 20:28:43 +0530554 return cse_set_and_boot_from_next_bp(RW);
555}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530556
V Sowmyaf9905522020-11-12 20:19:04 +0530557/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530558static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530559{
560 const struct cse_bp_entry *rw_bp;
561
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530562 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530563 return rw_bp->status != BP_STATUS_DATA_FAILURE;
564}
565
566/*
567 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
568 * otherwise false if any operation fails.
569 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530570static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530571{
572 /*
573 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
574 * - Send DATA CLEAR HECI command to CSE
575 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
576 * - Issue GLOBAL RESET HECI command.
577 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530578 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530579 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530580
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530581 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530582 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530583
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530584 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530585}
586
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530587static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530588{
589 const struct cse_bp_entry *cse_bp;
590
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530591 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530592 return &cse_bp->fw_ver;
593}
594
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530595static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530596{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530597 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530598}
599
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530600static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
601 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530602{
603 const struct cse_bp_entry *cse_bp;
604
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530605 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530606
607 if (start_offset)
608 *start_offset = cse_bp->start_offset;
609
610 if (end_offset)
611 *end_offset = cse_bp->end_offset;
V Sowmyaf9905522020-11-12 20:19:04 +0530612}
613
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530614static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530615{
616 const struct cse_bp_entry *rw_bp;
617
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530618 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530619
620 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
621 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
622 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
623 return false;
624 }
625 return true;
626}
627
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530628static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530629{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530630 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530631 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530632
633 return cse_set_and_boot_from_next_bp(RO);
634}
635
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530636static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530637{
638 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
639 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530640 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530641 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530642 }
643
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530644 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530645}
646
Rizwan Qureshiec321092019-09-06 20:28:43 +0530647static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
648{
649 uint32_t cse_bp_sign;
650
651 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
652 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
653 return false;
654 }
655
656 return cse_bp_sign == CSE_RW_SIGNATURE;
657}
658
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530659static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530660{
661 struct region_device cse_region_rdev;
662 size_t size;
663 uint32_t start_offset;
664 uint32_t end_offset;
665
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530666 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
667 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530668
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530669 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530670 size = end_offset + 1 - start_offset;
671
672 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530673 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530674
675 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100676 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530677
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530678 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530679}
680
Rizwan Qureshiec321092019-09-06 20:28:43 +0530681/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530682 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
683 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530684 * If ver_cmp_status = 0, no update is required
685 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
686 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
687 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530688static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530689{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700690 if (a->major != b->major)
691 return a->major - b->major;
692 else if (a->minor != b->minor)
693 return a->minor - b->minor;
694 else if (a->hotfix != b->hotfix)
695 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530696 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700697 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530698}
699
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530700static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530701{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530702 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
703 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530704 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530705 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530706 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530707}
708
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530709static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530710 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530711{
712 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
713 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530714 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530715 }
716
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530717 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530718}
719
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700720enum cse_update_status {
721 CSE_UPDATE_NOT_REQUIRED,
722 CSE_UPDATE_UPGRADE,
723 CSE_UPDATE_DOWNGRADE,
724 CSE_UPDATE_CORRUPTED,
725 CSE_UPDATE_METADATA_ERROR,
726};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530727
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700728static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
729{
730 if ((*curr - start) >= size) {
731 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
732 return false;
733 }
734
735 *ver_field = skip_atoi(curr);
736 (*curr)++;
737 return true;
738}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530739
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530740static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
741{
742 char *version_str, *cbfs_ptr;
743 size_t size;
744
745 if (cbfs_rw_version == NULL)
746 return CB_ERR;
747
748 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
749 version_str = cbfs_ptr;
750 if (!version_str) {
751 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
752 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
753 return CB_ERR;
754 }
755
756 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
757 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
758 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
759 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
760 cbfs_unmap(version_str);
761 return CB_ERR;
762 }
763
764 cbfs_unmap(version_str);
765 return CB_SUCCESS;
766}
767
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530768static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530769{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700770 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700771 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700772
773 if (!cse_is_rw_bp_sign_valid(target_rdev))
774 return CSE_UPDATE_CORRUPTED;
775
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530776 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700777 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700778
779 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700780 cbfs_rw_version.major,
781 cbfs_rw_version.minor,
782 cbfs_rw_version.hotfix,
783 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700784
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530785 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700786 if (ret == 0)
787 return CSE_UPDATE_NOT_REQUIRED;
788 else if (ret < 0)
789 return CSE_UPDATE_DOWNGRADE;
790 else
791 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530792}
793
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530794static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530795 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530796{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530797 /* Points to CSE CBFS RW image after boot partition signature */
798 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
799
800 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530801 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530802
803 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530804 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
805 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
806 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530807
808 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530809 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
810 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530811
812 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530813 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530814}
815
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530816static bool is_cse_fw_update_enabled(void)
817{
818 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
819 return false;
820
Kapil Porwal83cd6f92024-05-26 16:23:17 +0000821 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
822 return false;
823
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530824 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
825 return !is_debug_cse_fw_update_disable();
826
827 return true;
828}
829
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530830static 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 +0530831 struct region_device *target_rdev)
832{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530833 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530834 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
835 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530836 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
837 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530838
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530839 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530840 return CSE_LITE_SKU_FW_UPDATE_ERROR;
841
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530842 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530843 return CSE_LITE_SKU_FW_UPDATE_ERROR;
844
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600845 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530846}
847
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530848static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530849{
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530850 if (status == CSE_UPDATE_CORRUPTED)
851 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530852 /*
853 * To set CSE's operation mode to HMRFPO mode:
854 * 1. Ensure CSE to boot from RO(BP1)
855 * 2. Send HMRFPO_ENABLE command to CSE
856 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530857 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530858 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530859
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700860 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -0700861 /* Reset the PSR backup command status in CMOS */
862 if (CONFIG(SOC_INTEL_CSE_LITE_PSR))
863 update_psr_backup_status(PSR_BACKUP_PENDING);
864
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530865 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700866 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530867 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700868 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530869 }
870
Rizwan Qureshiec321092019-09-06 20:28:43 +0530871 return cse_hmrfpo_enable();
872}
873
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530874static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
875 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530876{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530877 enum csme_failure_reason rv;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530878 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700879 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530880
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530881 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530882 cse_cbfs_rw = cbfs_cbmem_alloc(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME,
883 CBMEM_ID_CSE_UPDATE, &size);
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530884 } else {
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530885 cse_cbfs_rw = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530886 }
Rizwan Qureshid81d80c2023-09-29 07:31:17 +0530887
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530888 if (!cse_cbfs_rw) {
889 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
890 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
891 }
892
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530893 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600894 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530895 goto error_exit;
896 }
897
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700898 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530899 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530900
901error_exit:
Julius Werner18881f992021-04-13 15:28:12 -0700902 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530903 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530904}
905
Anil Kumar7b2edc32023-04-18 11:03:34 -0700906static bool is_psr_data_backed_up(void)
907{
908 /* Track PSR backup status in CMOS */
909 return (get_psr_backup_status() == PSR_BACKUP_DONE);
910}
911
Anil Kumara2d10bb2023-05-01 11:44:47 -0700912static bool is_psr_supported(void)
913{
914 uint32_t feature_status;
915
916 /*
917 * Check if SoC has support for PSR feature typically PSR feature
918 * is only supported by vpro SKU
919 *
920 */
921 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
922 printk(BIOS_ERR, "cse_get_fw_feature_state command failed !\n");
923 return false;
924 }
925
926 if (!(feature_status & ME_FW_FEATURE_PSR)) {
927 printk(BIOS_DEBUG, "PSR is not supported in this SKU !\n");
928 return false;
929 }
930
931 return true;
932}
933
Anil Kumar7b2edc32023-04-18 11:03:34 -0700934/*
935 * PSR data needs to be backed up prior to downgrade. So switch the CSE boot mode to RW, send
936 * PSR back-up command to CSE and update the PSR back-up state in CMOS.
937 */
938static void backup_psr_data(void)
939{
940 printk(BIOS_DEBUG, "cse_lite: Initiate PSR data backup flow\n");
941 /* Switch CSE to RW to send PSR_HECI_FW_DOWNGRADE_BACKUP command */
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530942 if (cse_boot_to_rw() != CB_SUCCESS) {
943 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -0700944 goto update_and_exit;
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530945 }
Anil Kumar7b2edc32023-04-18 11:03:34 -0700946 /*
Anil Kumara2d10bb2023-05-01 11:44:47 -0700947 * The function to check for PSR feature support can only be called after
948 * switching to RW partition. The command MKHI_FWCAPS_GET_FW_FEATURE_STATE
949 * that gives feature state is supported by a process that is loaded only
950 * when CSE boots from RW.
951 *
952 */
953 if (!is_psr_supported())
954 goto update_and_exit;
955
956 /*
Anil Kumar7b2edc32023-04-18 11:03:34 -0700957 * Prerequisites:
958 * 1) HFSTS1 Current Working State is Normal
959 * 2) HFSTS1 Current Operation Mode is Normal
960 */
961 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
962 printk(BIOS_DEBUG, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
963 "prerequisites not met!\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530964 elog_add_event(ELOG_TYPE_PSR_DATA_LOST);
Anil Kumar7b2edc32023-04-18 11:03:34 -0700965 goto update_and_exit;
966 }
967
968 /* Send PSR_HECI_FW_DOWNGRADE_BACKUP command */
969 struct psr_heci_fw_downgrade_backup_req {
970 struct psr_heci_header header;
971 } __packed;
972
973 struct psr_heci_fw_downgrade_backup_req req = {
974 .header.command = PSR_HECI_FW_DOWNGRADE_BACKUP,
975 };
976
977 struct psr_heci_fw_downgrade_backup_res {
978 struct psr_heci_header header;
979 uint32_t status;
980 } __packed;
981
982 struct psr_heci_fw_downgrade_backup_res backup_psr_resp;
983 size_t resp_size = sizeof(backup_psr_resp);
984
985 printk(BIOS_DEBUG, "cse_lite: Send PSR_HECI_FW_DOWNGRADE_BACKUP command\n");
986 if (heci_send_receive(&req, sizeof(req),
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530987 &backup_psr_resp, &resp_size, HECI_PSR_ADDR)) {
Anil Kumar7b2edc32023-04-18 11:03:34 -0700988 printk(BIOS_ERR, "cse_lite: could not backup PSR data\n");
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530989 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP, ELOG_PSR_DATA_BACKUP_FAILED);
990 } else {
991 if (backup_psr_resp.status != PSR_STATUS_SUCCESS) {
Anil Kumar7b2edc32023-04-18 11:03:34 -0700992 printk(BIOS_ERR, "cse_lite: PSR_HECI_FW_DOWNGRADE_BACKUP command "
993 "returned %u\n", backup_psr_resp.status);
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +0530994 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
995 ELOG_PSR_DATA_BACKUP_FAILED);
996 } else {
997 elog_add_event_byte(ELOG_TYPE_PSR_DATA_BACKUP,
998 ELOG_PSR_DATA_BACKUP_SUCCESS);
999 }
1000 }
Anil Kumar7b2edc32023-04-18 11:03:34 -07001001
1002update_and_exit:
1003 /*
1004 * An attempt to send PSR back-up command has been made. Update this info in CMOS and
1005 * send success once backup_psr_data() has been called. We do not want to put the system
1006 * into recovery for PSR data backup command pre-requisites not being met.
Krishna Prasad Bhatb58fd2d2023-06-12 15:04:08 +05301007 * We cannot do much if CSE fails to backup the PSR data, except create an event log.
Anil Kumar7b2edc32023-04-18 11:03:34 -07001008 */
1009 update_psr_backup_status(PSR_BACKUP_DONE);
Anil Kumar7b2edc32023-04-18 11:03:34 -07001010}
1011
1012static void initiate_psr_data_backup(void)
1013{
1014 if (is_psr_data_backed_up())
1015 return;
1016
1017 backup_psr_data();
1018}
1019
Anil Kumare46af3f2023-10-03 21:31:53 -07001020/*
1021 * Check if a CSE Firmware update is required
1022 * returns true if an update is required, false otherwise
1023 */
1024bool is_cse_fw_update_required(void)
1025{
1026 struct fw_version cbfs_rw_version;
1027
1028 if (!is_cse_fw_update_enabled())
1029 return false;
1030
1031 /*
1032 * First, check if cse_bp_info_rsp global structure is populated.
1033 * If not, it implies that cse_fill_bp_info() function is not called.
1034 */
1035 if (!is_cse_bp_info_valid(&cse_bp_info_rsp))
1036 return false;
1037
1038 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
1039 return false;
1040
1041 return !!cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
1042}
1043
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301044static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +05301045{
1046 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001047 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +05301048
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301049 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +05301050 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
1051 return CSE_LITE_SKU_RW_ACCESS_ERROR;
1052 }
1053
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301054 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001055 if (status == CSE_UPDATE_NOT_REQUIRED)
1056 return CSE_NO_ERROR;
1057 if (status == CSE_UPDATE_METADATA_ERROR)
1058 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Anil Kumar7b2edc32023-04-18 11:03:34 -07001059 if (CONFIG(SOC_INTEL_CSE_LITE_PSR) && status == CSE_UPDATE_DOWNGRADE)
1060 initiate_psr_data_backup();
Rizwan Qureshiec321092019-09-06 20:28:43 +05301061
Furquan Shaikhc45e0be2021-10-06 23:28:03 -07001062 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301063 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301064}
1065
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301066static const char *cse_sub_part_str(enum bpdt_entry_type type)
1067{
1068 switch (type) {
1069 case IOM_FW:
1070 return "IOM";
1071 case NPHY_FW:
1072 return "NPHY";
1073 default:
1074 return "Unknown";
1075 }
1076}
1077
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301078static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301079{
1080 struct region_device cse_region_rdev;
1081 uint32_t size;
1082 uint32_t start_offset;
1083 uint32_t end_offset;
1084
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301085 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1086 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301087
1088 if (!strcmp(cse_regions[bp], "RO"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301089 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301090 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301091 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301092
1093 size = end_offset + 1 - start_offset;
1094
1095 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301096 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301097
1098 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1099 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301100 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301101}
1102
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301103static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1104 enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301105{
1106 struct bpdt_header bpdt_hdr;
1107 struct region_device cse_rdev;
1108 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1109 uint8_t i;
1110
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301111 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301112 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1113 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301114 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301115 }
1116
1117 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1118 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301119 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301120 }
1121
1122 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1123 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1124 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1125 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301126 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301127 }
1128
1129 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1130 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1131 if (bpdt_entries[i].type == type) {
1132 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1133 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1134 bpdt_entries[i].size);
1135
1136 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1137 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301138 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301139 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301140 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301141 }
1142 }
1143
1144 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301145 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301146}
1147
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301148static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301149 const struct region_device *rdev,
1150 struct fw_version *fw_ver)
1151{
1152 struct subpart_entry subpart_entry;
1153 struct subpart_entry_manifest_header man_hdr;
1154
1155 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1156 != SUBPART_ENTRY_SZ) {
1157 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1158 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301159 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301160 }
1161
1162 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1163 != SUBPART_MANIFEST_HDR_SZ) {
1164 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1165 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301166 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301167 }
1168
1169 fw_ver->major = man_hdr.binary_version.major;
1170 fw_ver->minor = man_hdr.binary_version.minor;
1171 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1172 fw_ver->build = man_hdr.binary_version.build;
1173
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301174 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301175}
1176
1177static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1178{
1179 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1180 struct subpart_entry *subpart_entry;
1181 struct subpart_entry_manifest_header *man_hdr;
1182
Elyes Haouas9018dee2022-11-18 15:07:33 +01001183 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1184 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301185
1186 fw_ver->major = man_hdr->binary_version.major;
1187 fw_ver->minor = man_hdr->binary_version.minor;
1188 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1189 fw_ver->build = man_hdr->binary_version.build;
1190}
1191
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301192static enum cb_err cse_prep_for_component_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301193{
1194 /*
1195 * To set CSE's operation mode to HMRFPO mode:
1196 * 1. Ensure CSE to boot from RO(BP1)
1197 * 2. Send HMRFPO_ENABLE command to CSE
1198 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301199 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301200 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301201
1202 return cse_hmrfpo_enable();
1203}
1204
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301205static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1206 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301207 struct region_device *target_rdev)
1208{
1209 if (region_device_sz(target_rdev) < blob_sz) {
1210 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1211 "smaller than blob size:%zx, abort update\n",
1212 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1213 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1214 }
1215
1216 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301217 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301218 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1219
1220 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301221 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301222 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1223
1224 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1225 cse_sub_part_str(type));
1226
1227 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1228}
1229
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301230static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301231{
1232 switch (rv) {
1233 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1234 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1235 return rv;
1236 default:
1237 cse_trigger_vboot_recovery(rv);
1238 }
1239 /* Control never reaches here */
1240 return rv;
1241}
1242
1243static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301244 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301245{
1246 struct region_device target_rdev;
1247 struct fw_version target_fw_ver, source_fw_ver;
1248 enum csme_failure_reason rv;
1249 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301250
1251 void *subpart_cbfs_rw = cbfs_map(name, &size);
1252 if (!subpart_cbfs_rw) {
1253 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1254 cse_sub_part_str(type));
1255 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1256 }
1257
1258 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1259 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1260 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1261 source_fw_ver.build);
1262
1263 /* Trigger sub-partition update in CSE RO and CSE RW */
1264 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301265 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301266 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1267 goto error_exit;
1268 }
1269
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301270 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301271 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1272 goto error_exit;
1273 }
1274
1275 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1276 cse_sub_part_str(type), target_fw_ver.major,
1277 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1278
1279 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1280 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1281 cse_regions[bp], cse_sub_part_str(type));
1282 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1283 continue;
1284 }
1285
1286 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1287 cse_sub_part_str(type));
1288
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301289 if (cse_prep_for_component_update() != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301290 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1291 goto error_exit;
1292 }
1293
1294 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1295 size, &target_rdev);
1296
1297 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1298 goto error_exit;
1299 }
1300error_exit:
1301 cbfs_unmap(subpart_cbfs_rw);
1302 return rv;
1303}
1304
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301305static enum csme_failure_reason cse_sub_part_fw_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301306{
1307 if (skip_cse_sub_part_update()) {
1308 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1309 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1310 }
1311
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301312 enum csme_failure_reason rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301313 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301314
1315 handle_cse_sub_part_fw_update_rv(rv);
1316
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301317 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301318
1319 return handle_cse_sub_part_fw_update_rv(rv);
1320}
1321
Subrata Banik5ff01182023-04-20 11:08:17 +05301322static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301323{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301324 /*
1325 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1326 * is not enabled and continue to update CSE sub-partitions.
1327 */
1328 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301329 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301330 return;
1331 }
1332
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301333 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1334 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301335 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301336 return;
1337 }
1338
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301339 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301340 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301341
1342 /* If system is in recovery mode, don't trigger recovery again */
1343 if (!vboot_recovery_mode_enabled()) {
1344 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1345 } else {
1346 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1347 "so no action\n");
1348 return;
1349 }
1350 }
1351
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +05301352 /* Store the CSE RW Firmware Version into CBMEM */
1353 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1354 cse_store_rw_fw_version();
1355
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301356 /*
1357 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301358 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301359 * fails during recovery, just continue to boot.
1360 */
1361 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301362 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301363 cse_board_reset();
1364 do_global_reset();
1365 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1366 }
1367
1368 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301369 }
1370
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301371 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001372 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301373
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301374 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001375 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301376 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1377 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301378 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301379 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301380 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301381 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301382 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001383 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301384 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301385
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301386 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301387 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301388
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301389 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001390 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301391
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301392 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301393 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001394 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301395 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301396}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301397
Subrata Banik5ff01182023-04-20 11:08:17 +05301398void cse_fw_sync(void)
1399{
Kapil Porwal83cd6f92024-05-26 16:23:17 +00001400 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1401 return;
1402
Subrata Banik5ff01182023-04-20 11:08:17 +05301403 timestamp_add_now(TS_CSE_FW_SYNC_START);
1404 do_cse_fw_sync();
1405 timestamp_add_now(TS_CSE_FW_SYNC_END);
1406}
1407
Subrata Banik7f66adb2023-04-14 00:35:38 +05301408static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1409 struct fw_version_resp *resp)
1410{
1411 enum cse_tx_rx_status ret;
1412 struct fw_version_msg {
1413 struct mkhi_hdr hdr;
1414 enum fpt_partition_id partition_id;
1415 } __packed msg = {
1416 .hdr = {
1417 .group_id = MKHI_GROUP_ID_GEN,
1418 .command = GEN_GET_IMAGE_FW_VERSION,
1419 },
1420 .partition_id = id,
1421 };
1422
1423 /*
1424 * Prerequisites:
1425 * 1) HFSTS1 CWS is Normal
1426 * 2) HFSTS1 COM is Normal
1427 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1428 */
1429
1430 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1431 !cse_is_hfs1_com_normal()) {
1432 printk(BIOS_ERR,
1433 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1434 return CB_ERR;
1435 }
1436
1437 size_t resp_size = sizeof(struct fw_version_resp);
1438 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1439
1440 if (ret || resp->hdr.result) {
1441 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1442 id, resp->hdr.result);
1443 return CB_ERR;
1444 }
1445
1446 return CB_SUCCESS;
1447}
1448
Subrata Banik044fc9f2023-04-14 02:34:37 +05301449static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1450 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301451{
1452 if (vboot_recovery_mode_enabled()) {
1453 printk(BIOS_WARNING,
1454 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1455 return CB_ERR;
1456 }
1457
1458 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1459 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1460 return CB_ERR;
1461 }
1462
1463 return send_get_fpt_partition_info_cmd(id, resp);
1464}
1465
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001466static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1467{
1468 const struct fw_version invalid_fw = {0, 0, 0, 0};
1469 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1470 return false;
1471 return true;
1472}
1473
Subrata Banikb1b7c532023-04-14 01:36:13 +05301474/*
1475 * Helper function to read ISH version from CSE FPT using HECI command.
1476 *
1477 * The HECI command only be executed after memory has been initialized.
1478 * This is because the command relies on resources that are not available
1479 * until DRAM initialization command has been sent.
1480 */
1481static void store_ish_version(void)
1482{
Kapil Porwal53e5d1f2024-06-17 11:32:58 +00001483 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1484 return;
1485
Subrata Banikb1b7c532023-04-14 01:36:13 +05301486 if (!ENV_RAMSTAGE)
1487 return;
1488
1489 if (vboot_recovery_mode_enabled())
1490 return;
1491
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001492 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1493 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301494 return;
1495
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001496 struct cse_specific_info cse_info_in_cmos;
1497 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001498
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001499 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1500 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301501
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001502 /* Get current cse firmware state */
1503 enum cse_fw_state fw_state = get_cse_state(
1504 &(cbmem_version->cur_cse_fw_version),
1505 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1506 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301507
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001508 if (fw_state == CSE_FW_WARM_BOOT) {
1509 return;
1510 } else {
1511 if (fw_state == CSE_FW_COLD_BOOT &&
1512 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1513 /* CMOS data is persistent across cold boots */
1514 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1515 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1516 sizeof(struct cse_fw_ish_version_info));
1517 store_cse_info_crc(cse_info_in_cbmem);
1518 } else {
1519 /*
1520 * Current running CSE version is different than previous stored CSE version
1521 * which could be due to CSE update or rollback, hence, need to send ISHC
1522 * partition info cmd to know the currently running ISH version.
1523 */
1524 struct fw_version_resp resp;
1525 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1526 &resp) == CB_SUCCESS) {
1527 /* Update stored CSE version with current cse version */
1528 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1529 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001530
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001531 /* Retrieve and update current ish version */
1532 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1533 &(resp.manifest_data.version), sizeof(struct fw_version));
1534
1535 /* Update the CRC */
1536 store_cse_info_crc(cse_info_in_cbmem);
1537
1538 /* Update CMOS with current CSE FPT versions.*/
1539 cmos_write_fw_partition_info(cse_info_in_cbmem);
1540 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301541 }
1542 }
1543}
1544
Kapil Porwal53e5d1f2024-06-17 11:32:58 +00001545static void preram_create_cbmem_cse_info(int is_recovery)
1546{
1547 if (!CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1548 return;
1549
1550 /*
1551 * CBMEM_ID_CSE_INFO will be used by the payload to -
1552 * 1. Avoid reading ISH firmware version on consecutive boots.
1553 * 2. Track state of PSR data during CSE downgrade operation.
1554 */
1555 void *temp = cbmem_add(CBMEM_ID_CSE_INFO, sizeof(struct cse_specific_info));
1556 if (!temp)
1557 printk(BIOS_ERR, "cse_lite: Couldn't create CBMEM_ID_CSE_INFO\n");
1558
1559 /*
1560 * CBMEM_ID_CSE_BP_INFO will be used by the payload to avoid reading CSE
1561 * boot partition information on consecutive boots.
1562 */
1563 temp = cbmem_add(CBMEM_ID_CSE_BP_INFO, sizeof(struct get_bp_info_rsp));
1564 if (!temp)
1565 printk(BIOS_ERR, "cse_lite: Couldn't create CBMEM_ID_CSE_BP_INFO\n");
1566}
1567
1568CBMEM_CREATION_HOOK(preram_create_cbmem_cse_info);
1569
Subrata Banikfc313d62023-04-14 01:31:29 +05301570static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301571{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301572 if (acpi_get_sleep_type() == ACPI_S3)
1573 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301574
Subrata Banik5ff01182023-04-20 11:08:17 +05301575 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301576 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301577
1578 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301579 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301580 * is available
1581 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301582 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301583 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301584}
1585
Subrata Banikfc313d62023-04-14 01:31:29 +05301586BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);