blob: 6a5d55bd341c203e8cc1e8b9b08a231e4a33711a [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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530138static struct get_bp_info_rsp cse_bp_info_rsp;
139
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000140enum cse_fw_state {
141 /* The CMOS and CBMEM have the current fw version. */
142 CSE_FW_WARM_BOOT,
143
144 /* The CMOS has the current fw version, and the CBMEM is wiped out. */
145 CSE_FW_COLD_BOOT,
146
147 /* The CMOS and CBMEM are not initialized or not same as running firmware version.*/
148 CSE_FW_INVALID,
149};
150
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530151static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700152
Subrata Banik65a6d172023-08-13 13:03:50 +0000153static struct cse_specific_info cse_info;
154
Subrata Banikda527ec2022-11-24 15:42:35 +0530155void cse_log_ro_write_protection_info(bool mfg_mode)
156{
157 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
158
159 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
160 cse_ro_wp_en ? "YES" : "NO");
161
162 if (cse_ro_wp_en) {
163 uint32_t base, limit;
164 spi_get_wp_cse_ro_range(&base, &limit);
165 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
166 base, limit);
167 }
168
169 /*
170 * If manufacturing mode is disabled, but CSE RO is not write protected,
171 * log error.
172 */
173 if (!mfg_mode && !cse_ro_wp_en)
174 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
175}
176
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530177enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700178{
179 struct cse_boot_perf_req {
180 struct mkhi_hdr hdr;
181 uint32_t reserved;
182 } __packed;
183
184 struct cse_boot_perf_req req = {
185 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
186 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
187 .reserved = 0,
188 };
189
190 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
191
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530192 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700193 HECI_MKHI_ADDR)) {
194 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530195 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700196 }
197
198 if (boot_perf_rsp->hdr.result) {
199 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
200 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530201 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700202 }
203
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530204 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700205}
206
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530207static const struct cse_bp_info *cse_get_bp_info_from_rsp(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530208{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530209 return &cse_bp_info_rsp.bp_info;
210}
211
212static uint8_t cse_get_current_bp(void)
213{
214 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530215 return cse_bp_info->current_bp;
216}
217
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530218static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530219{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530220 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530221 return &cse_bp_info->bp_entries[bp];
222}
223
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000224static bool is_cse_fpt_info_valid(const struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000225{
226 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
227
228 /*
229 * Authenticate the CBMEM persistent data.
230 *
231 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
232 * could change the values stored in this region has to also trigger the global
233 * reset. Hence, CBMEM persistent data won't be available any time after such
234 * event (global reset or cold reset) being initiated.
235 *
236 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
237 * want to override the existing data in CBMEM to avoid any additional boot latency.
238 */
239 if (info->crc != crc)
240 return false;
241
242 return true;
243}
244
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000245static void store_cse_info_crc(struct cse_specific_info *info)
Subrata Banik65a6d172023-08-13 13:03:50 +0000246{
247 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
248}
249
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000250static enum cse_fw_state get_cse_state(const struct fw_version *cur_cse_fw_ver,
251 struct fw_version *cmos_cse_fw_ver, const struct fw_version *cbmem_cse_fw_ver)
252{
253 enum cse_fw_state state = CSE_FW_WARM_BOOT;
254 size_t size = sizeof(struct fw_version);
255 /*
256 * Compare if stored CSE version (from the previous boot) is same as current
257 * running CSE version.
258 */
259 if (memcmp(cmos_cse_fw_ver, cur_cse_fw_ver, size)) {
260 /*
261 * CMOS CSE versioin is invalid, possibly two scenarios
262 * 1. CSE FW update
263 * 2. First boot
264 */
265 state = CSE_FW_INVALID;
266 } else {
267 /*
268 * Check if current running CSE version is same as previous stored CSE
269 * version aka CBMEM region is still valid.
270 */
271 if (memcmp(cbmem_cse_fw_ver, cur_cse_fw_ver, size))
272 state = CSE_FW_COLD_BOOT;
273 }
274 return state;
275}
276
Subrata Banik65a6d172023-08-13 13:03:50 +0000277/*
278 * Helper function that stores current CSE firmware version to CBMEM memory,
279 * except during recovery mode.
280 */
281static void cse_store_rw_fw_version(const struct cse_bp_entry *cse_bp)
282{
283 if (vboot_recovery_mode_enabled())
284 return;
285
286 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000287 /* update current CSE version and return */
288 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
289 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000290 return;
291 }
292
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000293 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
294 sizeof(*cse_info_in_cbmem));
295 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000296 return;
297
298 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000299 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000300 return;
301
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000302 struct cse_specific_info cse_info_in_cmos;
303 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000304
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000305 /* Get current cse firmware state */
306 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
307 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
308 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
309
310 /* Reset CBMEM data and update current CSE version */
311 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
312 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
313 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000314
315 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000316 store_cse_info_crc(cse_info_in_cbmem);
317
318 if (fw_state == CSE_FW_INVALID) {
319 /*
320 * Current CMOS data is outdated, which could be due to CSE update or
321 * rollback, hence, need to update CMOS with current CSE FPT versions.
322 */
323 cmos_write_fw_partition_info(cse_info_in_cbmem);
324 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000325}
326
327#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
328/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
329static void preram_cse_info_sync_to_cbmem(int is_recovery)
330{
331 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
332 return;
333
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000334 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
335 sizeof(*cse_info_in_cbmem));
336 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000337 return;
338
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000339 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
340 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000341 return;
342
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000343 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
344 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
345 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000346
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000347 struct cse_specific_info cse_info_in_cmos;
348 cmos_read_fw_partition_info(&cse_info_in_cmos);
349
350 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
351 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
352 sizeof(struct fw_version))) {
353 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
354 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
355 return;
356 }
357
358 /*
359 * Current CMOS data is outdated, which could be due to CSE update or
360 * rollback, hence, need to update CMOS with current CSE FPT versions.
361 */
362 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000363}
364
365CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
366#endif
367
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530368static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530369{
370 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530371 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530372
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530373 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
374 cse_bp_info->total_number_of_bp);
375 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
376 GET_BP_STR(cse_bp_info->current_bp));
377 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
378 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530379
380 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530381 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530382 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 +0530383 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
384 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
385 cse_bp->status, cse_bp->start_offset,
386 cse_bp->end_offset);
387
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530388 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530389 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 +0530390 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
391 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
392 cse_bp->status, cse_bp->start_offset,
393 cse_bp->end_offset);
Subrata Banik65a6d172023-08-13 13:03:50 +0000394
395 /* Store the CSE RW Firmware Version into CBMEM */
396 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
397 cse_store_rw_fw_version(cse_bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530398}
399
400/*
401 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
402 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
403 * It allows execution of the Boot Partition commands in below scenarios:
404 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
405 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
406 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530407 * The prerequisite check should be handled in cse_get_bp_info() and
408 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
409 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530410 */
411static bool cse_is_bp_cmd_info_possible(void)
412{
413 if (cse_is_hfs1_cws_normal()) {
414 if (cse_is_hfs1_com_normal())
415 return true;
416 if (cse_is_hfs1_com_secover_mei_msg())
417 return true;
418 if (cse_is_hfs1_com_soft_temp_disable())
419 return true;
420 }
421 return false;
422}
423
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530424static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530425{
426 struct get_bp_info_req {
427 struct mkhi_hdr hdr;
428 uint8_t reserved[4];
429 } __packed;
430
431 struct get_bp_info_req info_req = {
432 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
433 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
434 .reserved = {0},
435 };
436
437 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530438 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530439 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530440 }
441
442 size_t resp_size = sizeof(struct get_bp_info_rsp);
443
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530444 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530445 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530446 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530447 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530448 }
449
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530450 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530451 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530452 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530453 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530454 }
455
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530456 cse_print_boot_partition_info();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530457
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530458 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530459}
460/*
461 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
462 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
463 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
464 * This function must be used before EOP.
465 * Returns false on failure and true on success.
466 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530467static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530468{
469 struct set_boot_partition_info_req {
470 struct mkhi_hdr hdr;
471 uint8_t next_bp;
472 uint8_t reserved[3];
473 } __packed;
474
475 struct set_boot_partition_info_req switch_req = {
476 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
477 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
478 .next_bp = bp,
479 .reserved = {0},
480 };
481
482 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530483 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530484 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530485 }
486
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530487 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530488
489 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530490 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530491 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530492 }
493
494 struct mkhi_hdr switch_resp;
495 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
496
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530497 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530498 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530499 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530500
501 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530502 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530503 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530504 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530505 }
506
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530507 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530508}
509
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530510static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530511{
512 struct data_clr_request {
513 struct mkhi_hdr hdr;
514 uint8_t reserved[4];
515 } __packed;
516
517 struct data_clr_request data_clr_rq = {
518 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
519 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
520 .reserved = {0},
521 };
522
523 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530524 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530525 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530526 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530527 }
528
529 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
530
531 struct mkhi_hdr data_clr_rsp;
532 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
533
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530534 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530535 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530536 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530537 }
538
539 if (data_clr_rsp.result) {
540 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
541 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530542 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530543 }
544
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530545 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530546}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530547
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600548__weak void cse_board_reset(void)
549{
550 /* Default weak implementation, does nothing. */
551}
552
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700553__weak void cse_fw_update_misc_oper(void)
554{
555 /* Default weak implementation, does nothing. */
556}
557
Rizwan Qureshiec321092019-09-06 20:28:43 +0530558/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530559static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530560{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530561 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
562 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530563
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600564 /* Allow the board to perform a reset for CSE RO<->RW jump */
565 cse_board_reset();
566
567 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000568 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530569
Rizwan Qureshiec321092019-09-06 20:28:43 +0530570 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530571
572 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530573 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530574}
575
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530576static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530577{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530578 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530579 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530580
Rizwan Qureshiec321092019-09-06 20:28:43 +0530581 return cse_set_and_boot_from_next_bp(RW);
582}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530583
V Sowmyaf9905522020-11-12 20:19:04 +0530584/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530585static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530586{
587 const struct cse_bp_entry *rw_bp;
588
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530589 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530590 return rw_bp->status != BP_STATUS_DATA_FAILURE;
591}
592
593/*
594 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
595 * otherwise false if any operation fails.
596 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530597static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530598{
599 /*
600 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
601 * - Send DATA CLEAR HECI command to CSE
602 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
603 * - Issue GLOBAL RESET HECI command.
604 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530605 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530606 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530607
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530608 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530609 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530610
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530611 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530612}
613
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530614static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530615{
616 const struct cse_bp_entry *cse_bp;
617
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530618 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530619 return &cse_bp->fw_ver;
620}
621
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530622static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530623{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530624 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530625}
626
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530627static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
628 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530629{
630 const struct cse_bp_entry *cse_bp;
631
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530632 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530633
634 if (start_offset)
635 *start_offset = cse_bp->start_offset;
636
637 if (end_offset)
638 *end_offset = cse_bp->end_offset;
639
640}
641
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530642static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530643{
644 const struct cse_bp_entry *rw_bp;
645
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530646 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530647
648 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
649 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
650 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
651 return false;
652 }
653 return true;
654}
655
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530656static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530657{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530658 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530659 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530660
661 return cse_set_and_boot_from_next_bp(RO);
662}
663
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530664static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530665{
666 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
667 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530668 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530669 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530670 }
671
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530672 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530673}
674
Rizwan Qureshiec321092019-09-06 20:28:43 +0530675static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
676{
677 uint32_t cse_bp_sign;
678
679 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
680 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
681 return false;
682 }
683
684 return cse_bp_sign == CSE_RW_SIGNATURE;
685}
686
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530687static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530688{
689 struct region_device cse_region_rdev;
690 size_t size;
691 uint32_t start_offset;
692 uint32_t end_offset;
693
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530694 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
695 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530696
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530697 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530698 size = end_offset + 1 - start_offset;
699
700 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530701 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530702
703 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100704 (uint32_t)start_offset, (uint32_t)size);
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 Siricilla361e3642020-10-18 20:14:07 +0530709static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530710{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530711 struct vb2_context *ctx = vboot_get_context();
712 if (ctx == NULL)
713 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530714
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530715 if (vboot_is_firmware_slot_a(ctx))
716 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
717
718 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
719}
720
Rizwan Qureshiec321092019-09-06 20:28:43 +0530721/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530722 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
723 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530724 * If ver_cmp_status = 0, no update is required
725 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
726 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
727 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530728static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530729{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700730 if (a->major != b->major)
731 return a->major - b->major;
732 else if (a->minor != b->minor)
733 return a->minor - b->minor;
734 else if (a->hotfix != b->hotfix)
735 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530736 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700737 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530738}
739
740/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
741static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
742 const void *rw_blob, const size_t rw_blob_sz)
743
744{
Julius Wernerd96ca242022-08-08 18:08:35 -0700745 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530746
Julius Wernerd96ca242022-08-08 18:08:35 -0700747 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
748 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530749 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
750 return false;
751 }
752
Julius Wernerd96ca242022-08-08 18:08:35 -0700753 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530754 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
755 "the provided SHA in the metadata\n");
756 return false;
757 }
758 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
759 " provided hash in the metadata\n");
760 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530761}
762
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530763static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530764{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530765 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
766 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530767 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530768 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530769 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530770}
771
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530772static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530773 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530774{
775 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
776 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530777 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530778 }
779
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530780 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530781}
782
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700783enum cse_update_status {
784 CSE_UPDATE_NOT_REQUIRED,
785 CSE_UPDATE_UPGRADE,
786 CSE_UPDATE_DOWNGRADE,
787 CSE_UPDATE_CORRUPTED,
788 CSE_UPDATE_METADATA_ERROR,
789};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530790
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700791static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
792{
793 if ((*curr - start) >= size) {
794 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
795 return false;
796 }
797
798 *ver_field = skip_atoi(curr);
799 (*curr)++;
800 return true;
801}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530802
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530803static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
804{
805 char *version_str, *cbfs_ptr;
806 size_t size;
807
808 if (cbfs_rw_version == NULL)
809 return CB_ERR;
810
811 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
812 version_str = cbfs_ptr;
813 if (!version_str) {
814 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
815 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
816 return CB_ERR;
817 }
818
819 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
820 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
821 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
822 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
823 cbfs_unmap(version_str);
824 return CB_ERR;
825 }
826
827 cbfs_unmap(version_str);
828 return CB_SUCCESS;
829}
830
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530831static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530832{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700833 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700834 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700835
836 if (!cse_is_rw_bp_sign_valid(target_rdev))
837 return CSE_UPDATE_CORRUPTED;
838
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530839 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700840 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700841
842 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700843 cbfs_rw_version.major,
844 cbfs_rw_version.minor,
845 cbfs_rw_version.hotfix,
846 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700847
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530848 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700849 if (ret == 0)
850 return CSE_UPDATE_NOT_REQUIRED;
851 else if (ret < 0)
852 return CSE_UPDATE_DOWNGRADE;
853 else
854 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530855}
856
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530857static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530858 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530859{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530860 /* Points to CSE CBFS RW image after boot partition signature */
861 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
862
863 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530864 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530865
866 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530867 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
868 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
869 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530870
871 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530872 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
873 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530874
875 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530876 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530877}
878
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530879static bool is_cse_fw_update_enabled(void)
880{
881 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
882 return false;
883
884 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
885 return !is_debug_cse_fw_update_disable();
886
887 return true;
888}
889
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530890static 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 +0530891 struct region_device *target_rdev)
892{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530893 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530894 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
895 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530896 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
897 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530898
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530899 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530900 return CSE_LITE_SKU_FW_UPDATE_ERROR;
901
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530902 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530903 return CSE_LITE_SKU_FW_UPDATE_ERROR;
904
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600905 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530906}
907
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530908static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530909{
910 /*
911 * To set CSE's operation mode to HMRFPO mode:
912 * 1. Ensure CSE to boot from RO(BP1)
913 * 2. Send HMRFPO_ENABLE command to CSE
914 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530915 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530916 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530917
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700918 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530919 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700920 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530921 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700922 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530923 }
924
Rizwan Qureshiec321092019-09-06 20:28:43 +0530925 return cse_hmrfpo_enable();
926}
927
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530928static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
929 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530930{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530931 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700932 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530933 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700934 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530935
Julius Werner18881f992021-04-13 15:28:12 -0700936 const char *area_name = cse_get_source_rdev_fmap();
937 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530938 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530939
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530940 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
941 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
942 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
943 } else {
944 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
945 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
946 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530947 if (!cse_cbfs_rw) {
948 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
949 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
950 }
951
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700952 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
953 if (!cbfs_rw_hash) {
954 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
955 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
956 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
957 goto error_exit;
958 }
959
Julius Werner18881f992021-04-13 15:28:12 -0700960 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530961 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
962 goto error_exit;
963 }
964
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530965 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600966 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530967 goto error_exit;
968 }
969
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700970 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530971 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530972
973error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700974 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700975 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530976 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530977}
978
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530979static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530980{
981 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700982 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530983
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530984 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +0530985 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
986 return CSE_LITE_SKU_RW_ACCESS_ERROR;
987 }
988
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530989 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700990 if (status == CSE_UPDATE_NOT_REQUIRED)
991 return CSE_NO_ERROR;
992 if (status == CSE_UPDATE_METADATA_ERROR)
993 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530994
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700995 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530996 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530997}
998
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530999static const char *cse_sub_part_str(enum bpdt_entry_type type)
1000{
1001 switch (type) {
1002 case IOM_FW:
1003 return "IOM";
1004 case NPHY_FW:
1005 return "NPHY";
1006 default:
1007 return "Unknown";
1008 }
1009}
1010
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301011static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301012{
1013 struct region_device cse_region_rdev;
1014 uint32_t size;
1015 uint32_t start_offset;
1016 uint32_t end_offset;
1017
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301018 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
1019 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301020
1021 if (!strcmp(cse_regions[bp], "RO"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301022 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301023 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301024 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301025
1026 size = end_offset + 1 - start_offset;
1027
1028 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301029 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301030
1031 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
1032 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301033 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301034}
1035
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301036static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1037 enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301038{
1039 struct bpdt_header bpdt_hdr;
1040 struct region_device cse_rdev;
1041 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
1042 uint8_t i;
1043
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301044 if (cse_locate_area_as_rdev_rw(bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301045 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
1046 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301047 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301048 }
1049
1050 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
1051 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301052 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301053 }
1054
1055 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
1056 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
1057 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
1058 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301059 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301060 }
1061
1062 /* walk through BPDT entries to identify sub-partition's payload offset and size */
1063 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
1064 if (bpdt_entries[i].type == type) {
1065 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
1066 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
1067 bpdt_entries[i].size);
1068
1069 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
1070 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301071 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301072 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301073 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301074 }
1075 }
1076
1077 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301078 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301079}
1080
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301081static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301082 const struct region_device *rdev,
1083 struct fw_version *fw_ver)
1084{
1085 struct subpart_entry subpart_entry;
1086 struct subpart_entry_manifest_header man_hdr;
1087
1088 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1089 != SUBPART_ENTRY_SZ) {
1090 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1091 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301092 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301093 }
1094
1095 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1096 != SUBPART_MANIFEST_HDR_SZ) {
1097 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1098 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301099 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301100 }
1101
1102 fw_ver->major = man_hdr.binary_version.major;
1103 fw_ver->minor = man_hdr.binary_version.minor;
1104 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1105 fw_ver->build = man_hdr.binary_version.build;
1106
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301107 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301108}
1109
1110static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1111{
1112 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1113 struct subpart_entry *subpart_entry;
1114 struct subpart_entry_manifest_header *man_hdr;
1115
Elyes Haouas9018dee2022-11-18 15:07:33 +01001116 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1117 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301118
1119 fw_ver->major = man_hdr->binary_version.major;
1120 fw_ver->minor = man_hdr->binary_version.minor;
1121 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1122 fw_ver->build = man_hdr->binary_version.build;
1123}
1124
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301125static enum cb_err cse_prep_for_component_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301126{
1127 /*
1128 * To set CSE's operation mode to HMRFPO mode:
1129 * 1. Ensure CSE to boot from RO(BP1)
1130 * 2. Send HMRFPO_ENABLE command to CSE
1131 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301132 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301133 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301134
1135 return cse_hmrfpo_enable();
1136}
1137
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301138static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1139 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301140 struct region_device *target_rdev)
1141{
1142 if (region_device_sz(target_rdev) < blob_sz) {
1143 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1144 "smaller than blob size:%zx, abort update\n",
1145 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1146 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1147 }
1148
1149 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301150 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301151 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1152
1153 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301154 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301155 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1156
1157 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1158 cse_sub_part_str(type));
1159
1160 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1161}
1162
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301163static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301164{
1165 switch (rv) {
1166 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1167 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1168 return rv;
1169 default:
1170 cse_trigger_vboot_recovery(rv);
1171 }
1172 /* Control never reaches here */
1173 return rv;
1174}
1175
1176static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301177 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301178{
1179 struct region_device target_rdev;
1180 struct fw_version target_fw_ver, source_fw_ver;
1181 enum csme_failure_reason rv;
1182 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301183
1184 void *subpart_cbfs_rw = cbfs_map(name, &size);
1185 if (!subpart_cbfs_rw) {
1186 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1187 cse_sub_part_str(type));
1188 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1189 }
1190
1191 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1192 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1193 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1194 source_fw_ver.build);
1195
1196 /* Trigger sub-partition update in CSE RO and CSE RW */
1197 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301198 if (cse_sub_part_get_target_rdev(&target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301199 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1200 goto error_exit;
1201 }
1202
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301203 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301204 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1205 goto error_exit;
1206 }
1207
1208 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1209 cse_sub_part_str(type), target_fw_ver.major,
1210 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1211
1212 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1213 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1214 cse_regions[bp], cse_sub_part_str(type));
1215 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1216 continue;
1217 }
1218
1219 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1220 cse_sub_part_str(type));
1221
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301222 if (cse_prep_for_component_update() != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301223 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1224 goto error_exit;
1225 }
1226
1227 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1228 size, &target_rdev);
1229
1230 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1231 goto error_exit;
1232 }
1233error_exit:
1234 cbfs_unmap(subpart_cbfs_rw);
1235 return rv;
1236}
1237
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301238static enum csme_failure_reason cse_sub_part_fw_update(void)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301239{
1240 if (skip_cse_sub_part_update()) {
1241 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1242 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1243 }
1244
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301245 enum csme_failure_reason rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301246 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301247
1248 handle_cse_sub_part_fw_update_rv(rv);
1249
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301250 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301251
1252 return handle_cse_sub_part_fw_update_rv(rv);
1253}
1254
Subrata Banik5ff01182023-04-20 11:08:17 +05301255static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301256{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301257 /*
1258 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1259 * is not enabled and continue to update CSE sub-partitions.
1260 */
1261 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301262 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301263 return;
1264 }
1265
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301266 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1267 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301268 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301269 return;
1270 }
1271
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301272 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301273 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301274
1275 /* If system is in recovery mode, don't trigger recovery again */
1276 if (!vboot_recovery_mode_enabled()) {
1277 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1278 } else {
1279 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1280 "so no action\n");
1281 return;
1282 }
1283 }
1284
1285 /*
1286 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301287 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301288 * fails during recovery, just continue to boot.
1289 */
1290 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301291 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301292 cse_board_reset();
1293 do_global_reset();
1294 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1295 }
1296
1297 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301298 }
1299
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301300 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001301 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301302
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301303 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001304 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301305 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1306 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301307 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301308 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301309 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301310 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301311 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001312 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301313 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301314
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301315 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301316 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301317
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301318 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001319 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301320
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301321 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301322 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001323 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301324 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301325}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301326
Subrata Banik5ff01182023-04-20 11:08:17 +05301327void cse_fw_sync(void)
1328{
1329 timestamp_add_now(TS_CSE_FW_SYNC_START);
1330 do_cse_fw_sync();
1331 timestamp_add_now(TS_CSE_FW_SYNC_END);
1332}
1333
Subrata Banik7f66adb2023-04-14 00:35:38 +05301334static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1335 struct fw_version_resp *resp)
1336{
1337 enum cse_tx_rx_status ret;
1338 struct fw_version_msg {
1339 struct mkhi_hdr hdr;
1340 enum fpt_partition_id partition_id;
1341 } __packed msg = {
1342 .hdr = {
1343 .group_id = MKHI_GROUP_ID_GEN,
1344 .command = GEN_GET_IMAGE_FW_VERSION,
1345 },
1346 .partition_id = id,
1347 };
1348
1349 /*
1350 * Prerequisites:
1351 * 1) HFSTS1 CWS is Normal
1352 * 2) HFSTS1 COM is Normal
1353 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1354 */
1355
1356 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1357 !cse_is_hfs1_com_normal()) {
1358 printk(BIOS_ERR,
1359 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1360 return CB_ERR;
1361 }
1362
1363 size_t resp_size = sizeof(struct fw_version_resp);
1364 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1365
1366 if (ret || resp->hdr.result) {
1367 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1368 id, resp->hdr.result);
1369 return CB_ERR;
1370 }
1371
1372 return CB_SUCCESS;
1373}
1374
Subrata Banik044fc9f2023-04-14 02:34:37 +05301375static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1376 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301377{
1378 if (vboot_recovery_mode_enabled()) {
1379 printk(BIOS_WARNING,
1380 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1381 return CB_ERR;
1382 }
1383
1384 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1385 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1386 return CB_ERR;
1387 }
1388
1389 return send_get_fpt_partition_info_cmd(id, resp);
1390}
1391
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001392static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1393{
1394 const struct fw_version invalid_fw = {0, 0, 0, 0};
1395 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1396 return false;
1397 return true;
1398}
1399
Subrata Banikb1b7c532023-04-14 01:36:13 +05301400/*
1401 * Helper function to read ISH version from CSE FPT using HECI command.
1402 *
1403 * The HECI command only be executed after memory has been initialized.
1404 * This is because the command relies on resources that are not available
1405 * until DRAM initialization command has been sent.
1406 */
1407static void store_ish_version(void)
1408{
1409 if (!ENV_RAMSTAGE)
1410 return;
1411
1412 if (vboot_recovery_mode_enabled())
1413 return;
1414
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001415 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1416 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301417 return;
1418
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001419 struct cse_specific_info cse_info_in_cmos;
1420 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001421
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001422 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1423 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301424
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001425 /* Get current cse firmware state */
1426 enum cse_fw_state fw_state = get_cse_state(
1427 &(cbmem_version->cur_cse_fw_version),
1428 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1429 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301430
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001431 if (fw_state == CSE_FW_WARM_BOOT) {
1432 return;
1433 } else {
1434 if (fw_state == CSE_FW_COLD_BOOT &&
1435 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1436 /* CMOS data is persistent across cold boots */
1437 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1438 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1439 sizeof(struct cse_fw_ish_version_info));
1440 store_cse_info_crc(cse_info_in_cbmem);
1441 } else {
1442 /*
1443 * Current running CSE version is different than previous stored CSE version
1444 * which could be due to CSE update or rollback, hence, need to send ISHC
1445 * partition info cmd to know the currently running ISH version.
1446 */
1447 struct fw_version_resp resp;
1448 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1449 &resp) == CB_SUCCESS) {
1450 /* Update stored CSE version with current cse version */
1451 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1452 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001453
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001454 /* Retrieve and update current ish version */
1455 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1456 &(resp.manifest_data.version), sizeof(struct fw_version));
1457
1458 /* Update the CRC */
1459 store_cse_info_crc(cse_info_in_cbmem);
1460
1461 /* Update CMOS with current CSE FPT versions.*/
1462 cmos_write_fw_partition_info(cse_info_in_cbmem);
1463 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301464 }
1465 }
1466}
1467
Subrata Banikfc313d62023-04-14 01:31:29 +05301468static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301469{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301470 if (acpi_get_sleep_type() == ACPI_S3)
1471 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301472
Subrata Banik5ff01182023-04-20 11:08:17 +05301473 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301474 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301475
1476 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301477 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301478 * is available
1479 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301480 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301481 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301482}
1483
Subrata Banikfc313d62023-04-14 01:31:29 +05301484BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);