blob: 7b31a74c68afcfbd3beb81fa2e346f33e7a3697c [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>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053015#include <security/vboot/misc.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010016#include <security/vboot/vboot_common.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053017#include <soc/intel/common/reset.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +053018#include <timestamp.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053019
Dinesh Gehlota9232d82023-06-10 11:53:47 +000020#include "cse_lite_cmos.h"
21
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053022#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
23#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
24#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
25#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
26#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053027
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053028/* Converts bp index to boot partition string */
29#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
30
Rizwan Qureshiec321092019-09-06 20:28:43 +053031/* CSE RW boot partition signature */
32#define CSE_RW_SIGNATURE 0x000055aa
33
34/* CSE RW boot partition signature size */
35#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
36
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053037/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053038 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053039 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053040 * CSE Lite SKU Image Layout:
Angel Pons2e8e0602022-08-13 19:50:28 +020041 * +------------+ +----+------+----+ +-----+------+-----+
42 * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 |
43 * +------------+ +----+------+----+ +-----+------+-----+
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053044 */
45#define CSE_MAX_BOOT_PARTITIONS 3
46
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053047/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053048enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053049 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053050 RO = 0,
51
Rizwan Qureshiec321092019-09-06 20:28:43 +053052 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053053 RW = 1
54};
55
56/*
57 * Boot partition status.
58 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
59 */
60enum bp_status {
61 /* This value is returned when a partition has no errors */
62 BP_STATUS_SUCCESS = 0,
63
64 /*
65 * This value is returned when a partition should be present based on layout, but it is
66 * not valid.
67 */
68 BP_STATUS_GENERAL_FAILURE = 1,
69
70 /* This value is returned when a partition is not present per initial image layout */
71 BP_STATUS_PARTITION_NOT_PRESENT = 2,
72
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053073 /*
74 * This value is returned when unexpected issues are detected in CSE Data area
75 * and CSE TCB-SVN downgrade scenario.
76 */
77 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053078};
79
80/*
81 * Boot Partition Info Flags
82 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
83 */
84enum bp_info_flags {
85
86 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
87 BP_INFO_REDUNDANCY_EN = 1 << 0,
88
89 /* It indicates RO(BP1) supports Minimal Recovery Mode */
90 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
91
92 /*
93 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
94 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
95 */
96 BP_INFO_READ_ONLY_CFG = 1 << 2,
97};
98
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053099/* CSE boot partition entry info */
100struct cse_bp_entry {
101 /* Boot partition version */
102 struct fw_version fw_ver;
103
104 /* Boot partition status */
105 uint32_t status;
106
107 /* Starting offset of the partition within CSE region */
108 uint32_t start_offset;
109
110 /* Ending offset of the partition within CSE region */
111 uint32_t end_offset;
112 uint8_t reserved[12];
113} __packed;
114
115/* CSE boot partition info */
116struct cse_bp_info {
117 /* Number of boot partitions */
118 uint8_t total_number_of_bp;
119
120 /* Current boot partition */
121 uint8_t current_bp;
122
123 /* Next boot partition */
124 uint8_t next_bp;
125
126 /* Boot Partition Info Flags */
127 uint8_t flags;
128
129 /* Boot Partition Entry Info */
130 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
131} __packed;
132
133struct get_bp_info_rsp {
134 struct mkhi_hdr hdr;
135 struct cse_bp_info bp_info;
136} __packed;
137
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000138enum cse_fw_state {
139 /* The CMOS and CBMEM have the current fw version. */
140 CSE_FW_WARM_BOOT,
141
142 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
143 CSE_FW_COLD_BOOT,
144
145 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
146 CSE_FW_INVALID,
147};
148
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530149static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700150
Subrata Banik65a6d172023-08-13 13:03:50 +0000151static struct cse_specific_info cse_info;
152
Subrata Banikda527ec2022-11-24 15:42:35 +0530153void cse_log_ro_write_protection_info(bool mfg_mode)
154{
155 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
156
157 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
158 cse_ro_wp_en ? "YES" : "NO");
159
160 if (cse_ro_wp_en) {
161 uint32_t base, limit;
162 spi_get_wp_cse_ro_range(&base, &limit);
163 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
164 base, limit);
165 }
166
167 /*
168 * If manufacturing mode is disabled, but CSE RO is not write protected,
169 * log error.
170 */
171 if (!mfg_mode && !cse_ro_wp_en)
172 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
173}
174
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530175enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700176{
177 struct cse_boot_perf_req {
178 struct mkhi_hdr hdr;
179 uint32_t reserved;
180 } __packed;
181
182 struct cse_boot_perf_req req = {
183 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
184 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
185 .reserved = 0,
186 };
187
188 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
189
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530190 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700191 HECI_MKHI_ADDR)) {
192 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530193 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700194 }
195
196 if (boot_perf_rsp->hdr.result) {
197 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
198 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530199 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700200 }
201
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530202 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700203}
204
205
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530206static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
207{
208 return cse_bp_info->current_bp;
209}
210
211static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
212 const struct cse_bp_info *cse_bp_info)
213{
214 return &cse_bp_info->bp_entries[bp];
215}
216
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000217static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000218{
219 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
220
221 /*
222 * Authenticate the CBMEM persistent data.
223 *
224 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
225 * could change the values stored in this region has to also trigger the global
226 * reset. Hence, CBMEM persistent data won't be available any time after such
227 * event (global reset or cold reset) being initiated.
228 *
229 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
230 * want to override the existing data in CBMEM to avoid any additional boot latency.
231 */
232 if (info->crc != crc)
233 return false;
234
235 return true;
236}
237
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000238static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000239{
240 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
241}
242
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000243static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
244 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
245{
246 enum cse_fw_state state = CSE_FW_WARM_BOOT;
247 size_t size = sizeof(struct fw_version);
248 /*
249 * Compare if stored CSE version (from the previous boot) is same as current
250 * running CSE version.
251 */
252 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
253 /*
254 * CMOS CSE versioin is invalid, possibly two scenarios
255 * 1. CSE FW update
256 * 2. First boot
257 */
258 state = CSE_FW_INVALID;
259 } else {
260 /*
261 * Check if current running CSE version is same as previous stored CSE
262 * version aka CBMEM region is still valid.
263 */
264 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
265 state = CSE_FW_COLD_BOOT;
266 }
267 return state;
268}
269
Subrata Banik65a6d172023-08-13 13:03:50 +0000270/*
271 * Helper function that stores current CSE firmware version to CBMEM memory,
272 * except during recovery mode.
273 */
274static void cse_store_rw_fw_version(const struct cse_bp_entry *cse_bp)
275{
276 if (vboot_recovery_mode_enabled())
277 return;
278
279 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000280 /* update current CSE version and return */
281 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
282 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000283 return;
284 }
285
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000286 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
287 sizeof(*cse_info_in_cbmem));
288 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000289 return;
290
291 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000292 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000293 return;
294
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000295 struct cse_specific_info cse_info_in_cmos;
296 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000297
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000298 /* Get current cse firmware state */
299 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
300 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
301 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
302
303 /* Reset CBMEM data and update current CSE version */
304 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
305 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
306 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000307
308 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000309 store_cse_info_crc(cse_info_in_cbmem);
310
311 if (fw_state == CSE_FW_INVALID) {
312 /*
313 * Current CMOS data is outdated, which could be due to CSE update or
314 * rollback, hence, need to update CMOS with current CSE FPT versions.
315 */
316 cmos_write_fw_partition_info(cse_info_in_cbmem);
317 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000318}
319
320#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
321/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
322static void preram_cse_info_sync_to_cbmem(int is_recovery)
323{
324 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
325 return;
326
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000327 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
328 sizeof(*cse_info_in_cbmem));
329 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000330 return;
331
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000332 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
333 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000334 return;
335
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000336 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
337 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
338 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000339
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000340 struct cse_specific_info cse_info_in_cmos;
341 cmos_read_fw_partition_info(&cse_info_in_cmos);
342
343 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
344 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
345 sizeof(struct fw_version))) {
346 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
347 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
348 return;
349 }
350
351 /*
352 * Current CMOS data is outdated, which could be due to CSE update or
353 * rollback, hence, need to update CMOS with current CSE FPT versions.
354 */
355 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000356}
357
358CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
359#endif
360
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530361static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
362{
363 const struct cse_bp_entry *cse_bp;
364
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530365 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
366 cse_bp_info->total_number_of_bp);
367 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
368 GET_BP_STR(cse_bp_info->current_bp));
369 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
370 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530371
372 /* Log version info of RO & RW partitions */
373 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530374 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 +0530375 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
376 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
377 cse_bp->status, cse_bp->start_offset,
378 cse_bp->end_offset);
379
380 cse_bp = cse_get_bp_entry(RW, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530381 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 +0530382 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
383 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
384 cse_bp->status, cse_bp->start_offset,
385 cse_bp->end_offset);
Subrata Banik65a6d172023-08-13 13:03:50 +0000386
387 /* Store the CSE RW Firmware Version into CBMEM */
388 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
389 cse_store_rw_fw_version(cse_bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530390}
391
392/*
393 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
394 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
395 * It allows execution of the Boot Partition commands in below scenarios:
396 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
397 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
398 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530399 * The prerequisite check should be handled in cse_get_bp_info() and
400 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
401 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530402 */
403static bool cse_is_bp_cmd_info_possible(void)
404{
405 if (cse_is_hfs1_cws_normal()) {
406 if (cse_is_hfs1_com_normal())
407 return true;
408 if (cse_is_hfs1_com_secover_mei_msg())
409 return true;
410 if (cse_is_hfs1_com_soft_temp_disable())
411 return true;
412 }
413 return false;
414}
415
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530416static enum cb_err cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530417{
418 struct get_bp_info_req {
419 struct mkhi_hdr hdr;
420 uint8_t reserved[4];
421 } __packed;
422
423 struct get_bp_info_req info_req = {
424 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
425 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
426 .reserved = {0},
427 };
428
429 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530430 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530431 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530432 }
433
434 size_t resp_size = sizeof(struct get_bp_info_rsp);
435
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530436 if (heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530437 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530438 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530439 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530440 }
441
442 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530443 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530444 bp_info_rsp->hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530445 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530446 }
447
448 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
449
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530450 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530451}
452/*
453 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
454 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
455 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
456 * This function must be used before EOP.
457 * Returns false on failure and true on success.
458 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530459static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530460{
461 struct set_boot_partition_info_req {
462 struct mkhi_hdr hdr;
463 uint8_t next_bp;
464 uint8_t reserved[3];
465 } __packed;
466
467 struct set_boot_partition_info_req switch_req = {
468 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
469 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
470 .next_bp = bp,
471 .reserved = {0},
472 };
473
474 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530475 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530476 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530477 }
478
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530479 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530480
481 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530482 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530483 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530484 }
485
486 struct mkhi_hdr switch_resp;
487 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
488
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530489 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530490 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530491 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530492
493 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530494 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530495 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530496 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530497 }
498
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530499 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530500}
501
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530502static enum cb_err cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530503{
504 struct data_clr_request {
505 struct mkhi_hdr hdr;
506 uint8_t reserved[4];
507 } __packed;
508
509 struct data_clr_request data_clr_rq = {
510 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
511 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
512 .reserved = {0},
513 };
514
515 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
516 cse_get_current_bp(cse_bp_info) != RO) {
517 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530518 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530519 }
520
521 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
522
523 struct mkhi_hdr data_clr_rsp;
524 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
525
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530526 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530527 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530528 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530529 }
530
531 if (data_clr_rsp.result) {
532 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
533 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530534 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530535 }
536
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530537 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530538}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530539
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600540__weak void cse_board_reset(void)
541{
542 /* Default weak implementation, does nothing. */
543}
544
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700545__weak void cse_fw_update_misc_oper(void)
546{
547 /* Default weak implementation, does nothing. */
548}
549
Rizwan Qureshiec321092019-09-06 20:28:43 +0530550/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530551static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530552{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530553 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
554 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530555
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600556 /* Allow the board to perform a reset for CSE RO<->RW jump */
557 cse_board_reset();
558
559 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000560 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530561
Rizwan Qureshiec321092019-09-06 20:28:43 +0530562 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530563
564 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530565 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530566}
567
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530568static enum cb_err cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530569{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530570 if (cse_get_current_bp(cse_bp_info) == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530571 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530572
Rizwan Qureshiec321092019-09-06 20:28:43 +0530573 return cse_set_and_boot_from_next_bp(RW);
574}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530575
V Sowmyaf9905522020-11-12 20:19:04 +0530576/* Check if CSE RW data partition is valid or not */
577static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
578{
579 const struct cse_bp_entry *rw_bp;
580
581 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
582 return rw_bp->status != BP_STATUS_DATA_FAILURE;
583}
584
585/*
586 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
587 * otherwise false if any operation fails.
588 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530589static enum cb_err cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530590{
591 /*
592 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
593 * - Send DATA CLEAR HECI command to CSE
594 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
595 * - Issue GLOBAL RESET HECI command.
596 */
597 if (cse_is_rw_dp_valid(cse_bp_info))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530598 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530599
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530600 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS)
601 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530602
603 return cse_boot_to_rw(cse_bp_info);
604}
605
V Sowmyaf9905522020-11-12 20:19:04 +0530606static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530607 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530608{
609 const struct cse_bp_entry *cse_bp;
610
611 cse_bp = cse_get_bp_entry(bp, bp_info);
612 return &cse_bp->fw_ver;
613}
614
615static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
616{
617 return cse_get_bp_entry_version(RW, cse_bp_info);
618}
619
620static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530621 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530622{
623 const struct cse_bp_entry *cse_bp;
624
625 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
626
627 if (start_offset)
628 *start_offset = cse_bp->start_offset;
629
630 if (end_offset)
631 *end_offset = cse_bp->end_offset;
632
633}
634
635static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
636{
637 const struct cse_bp_entry *rw_bp;
638
639 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
640
641 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
642 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
643 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
644 return false;
645 }
646 return true;
647}
648
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530649static enum cb_err cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530650{
651 if (cse_get_current_bp(cse_bp_info) == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530652 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530653
654 return cse_set_and_boot_from_next_bp(RO);
655}
656
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530657static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530658{
659 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
660 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530661 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530662 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530663 }
664
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530665 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530666}
667
Rizwan Qureshiec321092019-09-06 20:28:43 +0530668static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
669{
670 uint32_t cse_bp_sign;
671
672 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
673 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
674 return false;
675 }
676
677 return cse_bp_sign == CSE_RW_SIGNATURE;
678}
679
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530680static enum cb_err cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Rizwan Qureshiec321092019-09-06 20:28:43 +0530681 struct region_device *target_rdev)
682{
683 struct region_device cse_region_rdev;
684 size_t size;
685 uint32_t start_offset;
686 uint32_t end_offset;
687
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530688 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
689 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530690
691 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
692 size = end_offset + 1 - start_offset;
693
694 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530695 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530696
697 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100698 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530699
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530700 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530701}
702
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530703static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530704{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530705 struct vb2_context *ctx = vboot_get_context();
706 if (ctx == NULL)
707 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530708
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530709 if (vboot_is_firmware_slot_a(ctx))
710 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
711
712 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
713}
714
Rizwan Qureshiec321092019-09-06 20:28:43 +0530715/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530716 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
717 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530718 * If ver_cmp_status = 0, no update is required
719 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
720 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
721 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530722static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530723{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700724 if (a->major != b->major)
725 return a->major - b->major;
726 else if (a->minor != b->minor)
727 return a->minor - b->minor;
728 else if (a->hotfix != b->hotfix)
729 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530730 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700731 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530732}
733
734/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
735static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
736 const void *rw_blob, const size_t rw_blob_sz)
737
738{
Julius Wernerd96ca242022-08-08 18:08:35 -0700739 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530740
Julius Wernerd96ca242022-08-08 18:08:35 -0700741 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
742 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530743 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
744 return false;
745 }
746
Julius Wernerd96ca242022-08-08 18:08:35 -0700747 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530748 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
749 "the provided SHA in the metadata\n");
750 return false;
751 }
752 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
753 " provided hash in the metadata\n");
754 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530755}
756
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530757static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530758{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530759 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
760 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530761 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530762 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530763 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530764}
765
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530766static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530767 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530768{
769 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
770 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530771 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530772 }
773
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530774 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530775}
776
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700777enum cse_update_status {
778 CSE_UPDATE_NOT_REQUIRED,
779 CSE_UPDATE_UPGRADE,
780 CSE_UPDATE_DOWNGRADE,
781 CSE_UPDATE_CORRUPTED,
782 CSE_UPDATE_METADATA_ERROR,
783};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530784
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700785static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
786{
787 if ((*curr - start) >= size) {
788 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
789 return false;
790 }
791
792 *ver_field = skip_atoi(curr);
793 (*curr)++;
794 return true;
795}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530796
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530797static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
798{
799 char *version_str, *cbfs_ptr;
800 size_t size;
801
802 if (cbfs_rw_version == NULL)
803 return CB_ERR;
804
805 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
806 version_str = cbfs_ptr;
807 if (!version_str) {
808 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
809 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
810 return CB_ERR;
811 }
812
813 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
814 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
815 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
816 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
817 cbfs_unmap(version_str);
818 return CB_ERR;
819 }
820
821 cbfs_unmap(version_str);
822 return CB_SUCCESS;
823}
824
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700825static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530826 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530827{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700828 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700829 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700830
831 if (!cse_is_rw_bp_sign_valid(target_rdev))
832 return CSE_UPDATE_CORRUPTED;
833
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530834 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700835 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700836
837 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700838 cbfs_rw_version.major,
839 cbfs_rw_version.minor,
840 cbfs_rw_version.hotfix,
841 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700842
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530843 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700844 if (ret == 0)
845 return CSE_UPDATE_NOT_REQUIRED;
846 else if (ret < 0)
847 return CSE_UPDATE_DOWNGRADE;
848 else
849 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530850}
851
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530852static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530853 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530854{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530855 /* Points to CSE CBFS RW image after boot partition signature */
856 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
857
858 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530859 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530860
861 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530862 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
863 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
864 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530865
866 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530867 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
868 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530869
870 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530871 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530872}
873
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530874static bool is_cse_fw_update_enabled(void)
875{
876 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
877 return false;
878
879 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
880 return !is_debug_cse_fw_update_disable();
881
882 return true;
883}
884
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530885static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
886 const void *cse_cbfs_rw, const size_t cse_blob_sz,
887 struct region_device *target_rdev)
888{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530889 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530890 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
891 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530892 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
893 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530894
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530895 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530896 return CSE_LITE_SKU_FW_UPDATE_ERROR;
897
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530898 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530899 return CSE_LITE_SKU_FW_UPDATE_ERROR;
900
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600901 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530902}
903
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530904static enum cb_err cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700905 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530906{
907 /*
908 * To set CSE's operation mode to HMRFPO mode:
909 * 1. Ensure CSE to boot from RO(BP1)
910 * 2. Send HMRFPO_ENABLE command to CSE
911 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530912 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
913 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530914
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700915 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530916 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700917 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530918 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700919 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530920 }
921
Rizwan Qureshiec321092019-09-06 20:28:43 +0530922 return cse_hmrfpo_enable();
923}
924
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530925static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700926 enum cse_update_status status,
927 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530928{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530929 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700930 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530931 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700932 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530933
Julius Werner18881f992021-04-13 15:28:12 -0700934 const char *area_name = cse_get_source_rdev_fmap();
935 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530936 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530937
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530938 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
939 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
940 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
941 } else {
942 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
943 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
944 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530945 if (!cse_cbfs_rw) {
946 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
947 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
948 }
949
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700950 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
951 if (!cbfs_rw_hash) {
952 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
953 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
954 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
955 goto error_exit;
956 }
957
Julius Werner18881f992021-04-13 15:28:12 -0700958 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530959 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
960 goto error_exit;
961 }
962
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530963 if (cse_prep_for_rw_update(cse_bp_info, status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600964 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530965 goto error_exit;
966 }
967
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700968 cse_fw_update_misc_oper();
Julius Werner18881f992021-04-13 15:28:12 -0700969 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530970
971error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700972 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700973 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530974 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530975}
976
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530977static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530978{
979 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700980 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530981
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530982 if (cse_get_target_rdev(cse_bp_info, &target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +0530983 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
984 return CSE_LITE_SKU_RW_ACCESS_ERROR;
985 }
986
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700987 status = cse_check_update_status(cse_bp_info, &target_rdev);
988 if (status == CSE_UPDATE_NOT_REQUIRED)
989 return CSE_NO_ERROR;
990 if (status == CSE_UPDATE_METADATA_ERROR)
991 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530992
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700993 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
994 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530995}
996
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530997static const char *cse_sub_part_str(enum bpdt_entry_type type)
998{
999 switch (type) {
1000 case IOM_FW:
1001 return "IOM";
1002 case NPHY_FW:
1003 return "NPHY";
1004 default:
1005 return "Unknown";
1006 }
1007}
1008
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301009static enum cb_err cse_locate_area_as_rdev_rw(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301010 size_t bp, struct region_device *cse_rdev)
1011{
1012 struct region_device cse_region_rdev;
1013 uint32_t size;
1014 uint32_t start_offset;
1015 uint32_t end_offset;
1016
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301017 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1018 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301019
1020 if (!strcmp(cse_regions[bp], "RO"))
1021 cse_get_bp_entry_range(cse_bp_info, RO, &start_offset, &end_offset);
1022 else
1023 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
1024
1025 size = end_offset + 1 - start_offset;
1026
1027 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301028 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301029
1030 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1031 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301032 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301033}
1034
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301035static enum cb_err cse_sub_part_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301036 struct region_device *target_rdev, size_t bp, enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301037{
1038 struct bpdt_header bpdt_hdr;
1039 struct region_device cse_rdev;
1040 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1041 uint8_t i;
1042
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301043 if (cse_locate_area_as_rdev_rw(cse_bp_info, bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301044 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1045 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301046 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301047 }
1048
1049 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1050 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301051 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301052 }
1053
1054 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1055 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1056 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1057 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301058 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301059 }
1060
1061 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1062 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1063 if (bpdt_entries[i].type == type) {
1064 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1065 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1066 bpdt_entries[i].size);
1067
1068 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1069 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301070 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301071 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301072 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301073 }
1074 }
1075
1076 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301077 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301078}
1079
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301080static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301081 const struct region_device *rdev,
1082 struct fw_version *fw_ver)
1083{
1084 struct subpart_entry subpart_entry;
1085 struct subpart_entry_manifest_header man_hdr;
1086
1087 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1088 != SUBPART_ENTRY_SZ) {
1089 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1090 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301091 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301092 }
1093
1094 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1095 != SUBPART_MANIFEST_HDR_SZ) {
1096 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1097 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301098 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301099 }
1100
1101 fw_ver->major = man_hdr.binary_version.major;
1102 fw_ver->minor = man_hdr.binary_version.minor;
1103 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1104 fw_ver->build = man_hdr.binary_version.build;
1105
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301106 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301107}
1108
1109static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1110{
1111 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1112 struct subpart_entry *subpart_entry;
1113 struct subpart_entry_manifest_header *man_hdr;
1114
Elyes Haouas9018dee2022-11-18 15:07:33 +01001115 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1116 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301117
1118 fw_ver->major = man_hdr->binary_version.major;
1119 fw_ver->minor = man_hdr->binary_version.minor;
1120 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1121 fw_ver->build = man_hdr->binary_version.build;
1122}
1123
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301124static enum cb_err cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301125{
1126 /*
1127 * To set CSE's operation mode to HMRFPO mode:
1128 * 1. Ensure CSE to boot from RO(BP1)
1129 * 2. Send HMRFPO_ENABLE command to CSE
1130 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301131 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
1132 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301133
1134 return cse_hmrfpo_enable();
1135}
1136
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301137static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1138 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301139 struct region_device *target_rdev)
1140{
1141 if (region_device_sz(target_rdev) < blob_sz) {
1142 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1143 "smaller than blob size:%zx, abort update\n",
1144 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1145 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1146 }
1147
1148 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301149 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301150 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1151
1152 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301153 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301154 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1155
1156 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1157 cse_sub_part_str(type));
1158
1159 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1160}
1161
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301162static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301163{
1164 switch (rv) {
1165 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1166 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1167 return rv;
1168 default:
1169 cse_trigger_vboot_recovery(rv);
1170 }
1171 /* Control never reaches here */
1172 return rv;
1173}
1174
1175static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
1176 const struct cse_bp_info *cse_bp_info, const char *name)
1177{
1178 struct region_device target_rdev;
1179 struct fw_version target_fw_ver, source_fw_ver;
1180 enum csme_failure_reason rv;
1181 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301182
1183 void *subpart_cbfs_rw = cbfs_map(name, &size);
1184 if (!subpart_cbfs_rw) {
1185 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1186 cse_sub_part_str(type));
1187 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1188 }
1189
1190 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1191 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1192 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1193 source_fw_ver.build);
1194
1195 /* Trigger sub-partition update in CSE RO and CSE RW */
1196 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301197 if (cse_sub_part_get_target_rdev(cse_bp_info, &target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301198 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1199 goto error_exit;
1200 }
1201
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301202 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301203 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1204 goto error_exit;
1205 }
1206
1207 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1208 cse_sub_part_str(type), target_fw_ver.major,
1209 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1210
1211 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1212 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1213 cse_regions[bp], cse_sub_part_str(type));
1214 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1215 continue;
1216 }
1217
1218 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1219 cse_sub_part_str(type));
1220
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301221 if (cse_prep_for_component_update(cse_bp_info) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301222 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1223 goto error_exit;
1224 }
1225
1226 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1227 size, &target_rdev);
1228
1229 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1230 goto error_exit;
1231 }
1232error_exit:
1233 cbfs_unmap(subpart_cbfs_rw);
1234 return rv;
1235}
1236
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301237static enum csme_failure_reason cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301238{
1239 if (skip_cse_sub_part_update()) {
1240 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1241 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1242 }
1243
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301244 enum csme_failure_reason rv;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301245 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
1246 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1247
1248 handle_cse_sub_part_fw_update_rv(rv);
1249
1250 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
1251 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1252
1253 return handle_cse_sub_part_fw_update_rv(rv);
1254}
1255
Subrata Banik5ff01182023-04-20 11:08:17 +05301256static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301257{
1258 static struct get_bp_info_rsp cse_bp_info;
1259
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301260 /*
1261 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1262 * is not enabled and continue to update CSE sub-partitions.
1263 */
1264 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301265 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301266 return;
1267 }
1268
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301269 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1270 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301271 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301272 return;
1273 }
1274
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +05301275 if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301276 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301277
1278 /* If system is in recovery mode, don't trigger recovery again */
1279 if (!vboot_recovery_mode_enabled()) {
1280 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1281 } else {
1282 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1283 "so no action\n");
1284 return;
1285 }
1286 }
1287
1288 /*
1289 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Alexander Goncharov893c3ae82023-02-04 15:20:37 +04001290 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-parition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301291 * fails during recovery, just continue to boot.
1292 */
1293 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1294 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1295 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1296 cse_board_reset();
1297 do_global_reset();
1298 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1299 }
1300
1301 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301302 }
1303
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301304 if (cse_fix_data_failure_err(&cse_bp_info.bp_info) != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001305 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301306
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301307 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001308 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301309 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1310 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301311 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301312 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301313 uint8_t rv;
1314 rv = cse_fw_update(&cse_bp_info.bp_info);
1315 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001316 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301317 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301318
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301319 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1320 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1321
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301322 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001323 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301324
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301325 if (cse_boot_to_rw(&cse_bp_info.bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301326 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001327 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301328 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301329}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301330
Subrata Banik5ff01182023-04-20 11:08:17 +05301331void cse_fw_sync(void)
1332{
1333 timestamp_add_now(TS_CSE_FW_SYNC_START);
1334 do_cse_fw_sync();
1335 timestamp_add_now(TS_CSE_FW_SYNC_END);
1336}
1337
Subrata Banik7f66adb2023-04-14 00:35:38 +05301338static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1339 struct fw_version_resp *resp)
1340{
1341 enum cse_tx_rx_status ret;
1342 struct fw_version_msg {
1343 struct mkhi_hdr hdr;
1344 enum fpt_partition_id partition_id;
1345 } __packed msg = {
1346 .hdr = {
1347 .group_id = MKHI_GROUP_ID_GEN,
1348 .command = GEN_GET_IMAGE_FW_VERSION,
1349 },
1350 .partition_id = id,
1351 };
1352
1353 /*
1354 * Prerequisites:
1355 * 1) HFSTS1 CWS is Normal
1356 * 2) HFSTS1 COM is Normal
1357 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1358 */
1359
1360 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1361 !cse_is_hfs1_com_normal()) {
1362 printk(BIOS_ERR,
1363 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1364 return CB_ERR;
1365 }
1366
1367 size_t resp_size = sizeof(struct fw_version_resp);
1368 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1369
1370 if (ret || resp->hdr.result) {
1371 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1372 id, resp->hdr.result);
1373 return CB_ERR;
1374 }
1375
1376 return CB_SUCCESS;
1377}
1378
Subrata Banik044fc9f2023-04-14 02:34:37 +05301379static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1380 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301381{
1382 if (vboot_recovery_mode_enabled()) {
1383 printk(BIOS_WARNING,
1384 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1385 return CB_ERR;
1386 }
1387
1388 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1389 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1390 return CB_ERR;
1391 }
1392
1393 return send_get_fpt_partition_info_cmd(id, resp);
1394}
1395
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001396static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1397{
1398 const struct fw_version invalid_fw = {0, 0, 0, 0};
1399 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1400 return false;
1401 return true;
1402}
1403
Subrata Banikb1b7c532023-04-14 01:36:13 +05301404/*
1405 * Helper function to read ISH version from CSE FPT using HECI command.
1406 *
1407 * The HECI command only be executed after memory has been initialized.
1408 * This is because the command relies on resources that are not available
1409 * until DRAM initialization command has been sent.
1410 */
1411static void store_ish_version(void)
1412{
1413 if (!ENV_RAMSTAGE)
1414 return;
1415
1416 if (vboot_recovery_mode_enabled())
1417 return;
1418
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001419 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1420 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301421 return;
1422
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001423 struct cse_specific_info cse_info_in_cmos;
1424 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001425
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001426 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1427 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301428
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001429 /* Get current cse firmware state */
1430 enum cse_fw_state fw_state = get_cse_state(
1431 &(cbmem_version->cur_cse_fw_version),
1432 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1433 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301434
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001435 if (fw_state == CSE_FW_WARM_BOOT) {
1436 return;
1437 } else {
1438 if (fw_state == CSE_FW_COLD_BOOT &&
1439 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1440 /* CMOS data is persistent across cold boots */
1441 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1442 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1443 sizeof(struct cse_fw_ish_version_info));
1444 store_cse_info_crc(cse_info_in_cbmem);
1445 } else {
1446 /*
1447 * Current running CSE version is different than previous stored CSE version
1448 * which could be due to CSE update or rollback, hence, need to send ISHC
1449 * partition info cmd to know the currently running ISH version.
1450 */
1451 struct fw_version_resp resp;
1452 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1453 &resp) == CB_SUCCESS) {
1454 /* Update stored CSE version with current cse version */
1455 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1456 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001457
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001458 /* Retrieve and update current ish version */
1459 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1460 &(resp.manifest_data.version), sizeof(struct fw_version));
1461
1462 /* Update the CRC */
1463 store_cse_info_crc(cse_info_in_cbmem);
1464
1465 /* Update CMOS with current CSE FPT versions.*/
1466 cmos_write_fw_partition_info(cse_info_in_cbmem);
1467 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301468 }
1469 }
1470}
1471
Subrata Banikfc313d62023-04-14 01:31:29 +05301472static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301473{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301474 if (acpi_get_sleep_type() == ACPI_S3)
1475 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301476
Subrata Banik5ff01182023-04-20 11:08:17 +05301477 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301478 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301479
1480 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301481 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301482 * is available
1483 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301484 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301485 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301486}
1487
Subrata Banikfc313d62023-04-14 01:31:29 +05301488BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);