blob: 140a56d06fc5aabfafec2421610dc7017b48022f [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 */
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530281static void cse_store_rw_fw_version(void)
Subrata Banik65a6d172023-08-13 13:03:50 +0000282{
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +0530283 const struct cse_bp_entry *cse_bp;
284 cse_bp = cse_get_bp_entry(RW);
285
Subrata Banik65a6d172023-08-13 13:03:50 +0000286 if (vboot_recovery_mode_enabled())
287 return;
288
289 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000290 /* update current CSE version and return */
291 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version),
292 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000293 return;
294 }
295
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000296 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
297 sizeof(*cse_info_in_cbmem));
298 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000299 return;
300
301 /* Avoid CBMEM update if CBMEM already has persistent data */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000302 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000303 return;
304
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000305 struct cse_specific_info cse_info_in_cmos;
306 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik65a6d172023-08-13 13:03:50 +0000307
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000308 /* Get current cse firmware state */
309 enum cse_fw_state fw_state = get_cse_state(&(cse_bp->fw_ver),
310 &(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
311 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version));
312
313 /* Reset CBMEM data and update current CSE version */
314 memset(cse_info_in_cbmem, 0, sizeof(*cse_info_in_cbmem));
315 memcpy(&(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
316 &(cse_bp->fw_ver), sizeof(struct fw_version));
Subrata Banik65a6d172023-08-13 13:03:50 +0000317
318 /* Update the CRC */
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000319 store_cse_info_crc(cse_info_in_cbmem);
320
321 if (fw_state == CSE_FW_INVALID) {
322 /*
323 * Current CMOS data is outdated, which could be due to CSE update or
324 * rollback, hence, need to update CMOS with current CSE FPT versions.
325 */
326 cmos_write_fw_partition_info(cse_info_in_cbmem);
327 }
Subrata Banik65a6d172023-08-13 13:03:50 +0000328}
329
330#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
331/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
332static void preram_cse_info_sync_to_cbmem(int is_recovery)
333{
334 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
335 return;
336
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000337 struct cse_specific_info *cse_info_in_cbmem = cbmem_add(CBMEM_ID_CSE_INFO,
338 sizeof(*cse_info_in_cbmem));
339 if (!cse_info_in_cbmem)
Subrata Banik65a6d172023-08-13 13:03:50 +0000340 return;
341
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000342 /* Warm Reboot: Avoid sync into CBMEM if CBMEM already has persistent data */
343 if (is_cse_fpt_info_valid(cse_info_in_cbmem))
Subrata Banik65a6d172023-08-13 13:03:50 +0000344 return;
345
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000346 /* Update CBMEM with PRERAM CSE specific info and update the CRC */
347 memcpy(cse_info_in_cbmem, &cse_info, sizeof(struct cse_specific_info));
348 store_cse_info_crc(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000349
Dinesh Gehlota9232d82023-06-10 11:53:47 +0000350 struct cse_specific_info cse_info_in_cmos;
351 cmos_read_fw_partition_info(&cse_info_in_cmos);
352
353 if (!memcmp(&(cse_info_in_cmos.cse_fwp_version.cur_cse_fw_version),
354 &(cse_info_in_cbmem->cse_fwp_version.cur_cse_fw_version),
355 sizeof(struct fw_version))) {
356 /* Cold Reboot: Avoid sync into CMOS if CMOS already has persistent data */
357 if (is_cse_fpt_info_valid(&cse_info_in_cmos))
358 return;
359 }
360
361 /*
362 * Current CMOS data is outdated, which could be due to CSE update or
363 * rollback, hence, need to update CMOS with current CSE FPT versions.
364 */
365 cmos_write_fw_partition_info(cse_info_in_cbmem);
Subrata Banik65a6d172023-08-13 13:03:50 +0000366}
367
368CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
369#endif
370
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530371static void cse_print_boot_partition_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530372{
373 const struct cse_bp_entry *cse_bp;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530374 const struct cse_bp_info *cse_bp_info = cse_get_bp_info_from_rsp();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530375
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530376 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
377 cse_bp_info->total_number_of_bp);
378 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
379 GET_BP_STR(cse_bp_info->current_bp));
380 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
381 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530382
383 /* Log version info of RO & RW partitions */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530384 cse_bp = cse_get_bp_entry(RO);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530385 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530386 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
387 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
388 cse_bp->status, cse_bp->start_offset,
389 cse_bp->end_offset);
390
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530391 cse_bp = cse_get_bp_entry(RW);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530392 printk(BIOS_DEBUG, "cse_lite: %s version = %d.%d.%d.%d (Status=0x%x, Start=0x%x, End=0x%x)\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530393 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
394 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
395 cse_bp->status, cse_bp->start_offset,
396 cse_bp->end_offset);
397}
398
399/*
400 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
401 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
402 * It allows execution of the Boot Partition commands in below scenarios:
403 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
404 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
405 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530406 * The prerequisite check should be handled in cse_get_bp_info() and
407 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
408 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530409 */
410static bool cse_is_bp_cmd_info_possible(void)
411{
412 if (cse_is_hfs1_cws_normal()) {
413 if (cse_is_hfs1_com_normal())
414 return true;
415 if (cse_is_hfs1_com_secover_mei_msg())
416 return true;
417 if (cse_is_hfs1_com_soft_temp_disable())
418 return true;
419 }
420 return false;
421}
422
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530423static enum cb_err cse_get_bp_info(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530424{
425 struct get_bp_info_req {
426 struct mkhi_hdr hdr;
427 uint8_t reserved[4];
428 } __packed;
429
430 struct get_bp_info_req info_req = {
431 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
432 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
433 .reserved = {0},
434 };
435
436 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530437 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530438 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530439 }
440
441 size_t resp_size = sizeof(struct get_bp_info_rsp);
442
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530443 if (heci_send_receive(&info_req, sizeof(info_req), &cse_bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530444 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530445 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530446 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530447 }
448
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530449 if (cse_bp_info_rsp.hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530450 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530451 cse_bp_info_rsp.hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530452 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530453 }
454
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530455 cse_print_boot_partition_info();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530456
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530457 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530458}
459/*
460 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
461 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
462 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
463 * This function must be used before EOP.
464 * Returns false on failure and true on success.
465 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530466static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530467{
468 struct set_boot_partition_info_req {
469 struct mkhi_hdr hdr;
470 uint8_t next_bp;
471 uint8_t reserved[3];
472 } __packed;
473
474 struct set_boot_partition_info_req switch_req = {
475 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
476 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
477 .next_bp = bp,
478 .reserved = {0},
479 };
480
481 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530482 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530483 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530484 }
485
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530486 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530487
488 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530489 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530490 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530491 }
492
493 struct mkhi_hdr switch_resp;
494 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
495
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530496 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530497 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530498 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530499
500 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530501 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530502 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530503 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530504 }
505
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530506 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530507}
508
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530509static enum cb_err cse_data_clear_request(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530510{
511 struct data_clr_request {
512 struct mkhi_hdr hdr;
513 uint8_t reserved[4];
514 } __packed;
515
516 struct data_clr_request data_clr_rq = {
517 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
518 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
519 .reserved = {0},
520 };
521
522 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530523 cse_get_current_bp() != RO) {
V Sowmyaf9905522020-11-12 20:19:04 +0530524 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530525 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530526 }
527
528 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
529
530 struct mkhi_hdr data_clr_rsp;
531 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
532
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530533 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530534 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530535 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530536 }
537
538 if (data_clr_rsp.result) {
539 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
540 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530541 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530542 }
543
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530544 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530545}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530546
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600547__weak void cse_board_reset(void)
548{
549 /* Default weak implementation, does nothing. */
550}
551
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700552__weak void cse_fw_update_misc_oper(void)
553{
554 /* Default weak implementation, does nothing. */
555}
556
Rizwan Qureshiec321092019-09-06 20:28:43 +0530557/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530558static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530559{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530560 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
561 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530562
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600563 /* Allow the board to perform a reset for CSE RO<->RW jump */
564 cse_board_reset();
565
566 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000567 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530568
Rizwan Qureshiec321092019-09-06 20:28:43 +0530569 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530570
571 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530572 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530573}
574
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530575static enum cb_err cse_boot_to_rw(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530576{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530577 if (cse_get_current_bp() == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530578 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530579
Rizwan Qureshiec321092019-09-06 20:28:43 +0530580 return cse_set_and_boot_from_next_bp(RW);
581}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530582
V Sowmyaf9905522020-11-12 20:19:04 +0530583/* Check if CSE RW data partition is valid or not */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530584static bool cse_is_rw_dp_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530585{
586 const struct cse_bp_entry *rw_bp;
587
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530588 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530589 return rw_bp->status != BP_STATUS_DATA_FAILURE;
590}
591
592/*
593 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
594 * otherwise false if any operation fails.
595 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530596static enum cb_err cse_fix_data_failure_err(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530597{
598 /*
599 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
600 * - Send DATA CLEAR HECI command to CSE
601 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
602 * - Issue GLOBAL RESET HECI command.
603 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530604 if (cse_is_rw_dp_valid())
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530605 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530606
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530607 if (cse_data_clear_request() != CB_SUCCESS)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530608 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530609
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530610 return cse_boot_to_rw();
V Sowmyaf9905522020-11-12 20:19:04 +0530611}
612
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530613static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp)
V Sowmyaf9905522020-11-12 20:19:04 +0530614{
615 const struct cse_bp_entry *cse_bp;
616
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530617 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530618 return &cse_bp->fw_ver;
619}
620
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530621static const struct fw_version *cse_get_rw_version(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530622{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530623 return cse_get_bp_entry_version(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530624}
625
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530626static void cse_get_bp_entry_range(enum boot_partition_id bp, uint32_t *start_offset,
627 uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530628{
629 const struct cse_bp_entry *cse_bp;
630
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530631 cse_bp = cse_get_bp_entry(bp);
V Sowmyaf9905522020-11-12 20:19:04 +0530632
633 if (start_offset)
634 *start_offset = cse_bp->start_offset;
635
636 if (end_offset)
637 *end_offset = cse_bp->end_offset;
638
639}
640
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530641static bool cse_is_rw_bp_status_valid(void)
V Sowmyaf9905522020-11-12 20:19:04 +0530642{
643 const struct cse_bp_entry *rw_bp;
644
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530645 rw_bp = cse_get_bp_entry(RW);
V Sowmyaf9905522020-11-12 20:19:04 +0530646
647 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
648 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
649 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
650 return false;
651 }
652 return true;
653}
654
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530655static enum cb_err cse_boot_to_ro(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530656{
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530657 if (cse_get_current_bp() == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530658 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530659
660 return cse_set_and_boot_from_next_bp(RO);
661}
662
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530663static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530664{
665 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
666 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530667 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530668 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530669 }
670
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530671 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530672}
673
Rizwan Qureshiec321092019-09-06 20:28:43 +0530674static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
675{
676 uint32_t cse_bp_sign;
677
678 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
679 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
680 return false;
681 }
682
683 return cse_bp_sign == CSE_RW_SIGNATURE;
684}
685
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530686static enum cb_err cse_get_target_rdev(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530687{
688 struct region_device cse_region_rdev;
689 size_t size;
690 uint32_t start_offset;
691 uint32_t end_offset;
692
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530693 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
694 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530695
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530696 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530697 size = end_offset + 1 - start_offset;
698
699 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530700 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530701
702 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100703 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530704
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530705 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530706}
707
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530708static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530709{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530710 struct vb2_context *ctx = vboot_get_context();
711 if (ctx == NULL)
712 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530713
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530714 if (vboot_is_firmware_slot_a(ctx))
715 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
716
717 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
718}
719
Rizwan Qureshiec321092019-09-06 20:28:43 +0530720/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530721 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
722 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530723 * If ver_cmp_status = 0, no update is required
724 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
725 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
726 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530727static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530728{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700729 if (a->major != b->major)
730 return a->major - b->major;
731 else if (a->minor != b->minor)
732 return a->minor - b->minor;
733 else if (a->hotfix != b->hotfix)
734 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530735 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700736 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530737}
738
739/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
740static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
741 const void *rw_blob, const size_t rw_blob_sz)
742
743{
Julius Wernerd96ca242022-08-08 18:08:35 -0700744 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530745
Julius Wernerd96ca242022-08-08 18:08:35 -0700746 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
747 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530748 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
749 return false;
750 }
751
Julius Wernerd96ca242022-08-08 18:08:35 -0700752 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530753 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
754 "the provided SHA in the metadata\n");
755 return false;
756 }
757 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
758 " provided hash in the metadata\n");
759 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530760}
761
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530762static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530763{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530764 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
765 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530766 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530767 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530768 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530769}
770
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530771static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530772 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530773{
774 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
775 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530776 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530777 }
778
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530779 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530780}
781
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700782enum cse_update_status {
783 CSE_UPDATE_NOT_REQUIRED,
784 CSE_UPDATE_UPGRADE,
785 CSE_UPDATE_DOWNGRADE,
786 CSE_UPDATE_CORRUPTED,
787 CSE_UPDATE_METADATA_ERROR,
788};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530789
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700790static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
791{
792 if ((*curr - start) >= size) {
793 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
794 return false;
795 }
796
797 *ver_field = skip_atoi(curr);
798 (*curr)++;
799 return true;
800}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530801
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530802static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
803{
804 char *version_str, *cbfs_ptr;
805 size_t size;
806
807 if (cbfs_rw_version == NULL)
808 return CB_ERR;
809
810 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
811 version_str = cbfs_ptr;
812 if (!version_str) {
813 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
814 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
815 return CB_ERR;
816 }
817
818 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
819 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
820 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
821 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
822 cbfs_unmap(version_str);
823 return CB_ERR;
824 }
825
826 cbfs_unmap(version_str);
827 return CB_SUCCESS;
828}
829
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530830static enum cse_update_status cse_check_update_status(struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530831{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700832 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700833 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700834
835 if (!cse_is_rw_bp_sign_valid(target_rdev))
836 return CSE_UPDATE_CORRUPTED;
837
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530838 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700839 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700840
841 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700842 cbfs_rw_version.major,
843 cbfs_rw_version.minor,
844 cbfs_rw_version.hotfix,
845 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700846
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530847 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version());
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700848 if (ret == 0)
849 return CSE_UPDATE_NOT_REQUIRED;
850 else if (ret < 0)
851 return CSE_UPDATE_DOWNGRADE;
852 else
853 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530854}
855
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530856static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530857 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530858{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530859 /* Points to CSE CBFS RW image after boot partition signature */
860 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
861
862 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530863 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530864
865 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530866 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
867 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
868 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530869
870 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530871 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
872 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530873
874 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530875 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530876}
877
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530878static bool is_cse_fw_update_enabled(void)
879{
880 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
881 return false;
882
883 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
884 return !is_debug_cse_fw_update_disable();
885
886 return true;
887}
888
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530889static 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 +0530890 struct region_device *target_rdev)
891{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530892 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530893 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
894 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530895 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
896 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530897
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530898 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530899 return CSE_LITE_SKU_FW_UPDATE_ERROR;
900
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530901 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530902 return CSE_LITE_SKU_FW_UPDATE_ERROR;
903
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600904 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530905}
906
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530907static enum cb_err cse_prep_for_rw_update(enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530908{
909 /*
910 * To set CSE's operation mode to HMRFPO mode:
911 * 1. Ensure CSE to boot from RO(BP1)
912 * 2. Send HMRFPO_ENABLE command to CSE
913 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530914 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530915 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530916
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700917 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530918 if (cse_data_clear_request() != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700919 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530920 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700921 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530922 }
923
Rizwan Qureshiec321092019-09-06 20:28:43 +0530924 return cse_hmrfpo_enable();
925}
926
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530927static enum csme_failure_reason cse_trigger_fw_update(enum cse_update_status status,
928 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530929{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530930 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700931 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530932 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700933 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530934
Julius Werner18881f992021-04-13 15:28:12 -0700935 const char *area_name = cse_get_source_rdev_fmap();
936 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530937 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530938
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530939 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
940 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
941 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
942 } else {
943 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
944 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
945 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530946 if (!cse_cbfs_rw) {
947 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
948 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
949 }
950
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700951 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
952 if (!cbfs_rw_hash) {
953 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
954 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
955 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
956 goto error_exit;
957 }
958
Julius Werner18881f992021-04-13 15:28:12 -0700959 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530960 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
961 goto error_exit;
962 }
963
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530964 if (cse_prep_for_rw_update(status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600965 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530966 goto error_exit;
967 }
968
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700969 cse_fw_update_misc_oper();
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530970 rv = cse_update_rw(cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530971
972error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700973 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700974 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530975 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530976}
977
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530978static uint8_t cse_fw_update(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530979{
980 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700981 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530982
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530983 if (cse_get_target_rdev(&target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +0530984 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
985 return CSE_LITE_SKU_RW_ACCESS_ERROR;
986 }
987
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530988 status = cse_check_update_status(&target_rdev);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700989 if (status == CSE_UPDATE_NOT_REQUIRED)
990 return CSE_NO_ERROR;
991 if (status == CSE_UPDATE_METADATA_ERROR)
992 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530993
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700994 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +0530995 return cse_trigger_fw_update(status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530996}
997
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530998static const char *cse_sub_part_str(enum bpdt_entry_type type)
999{
1000 switch (type) {
1001 case IOM_FW:
1002 return "IOM";
1003 case NPHY_FW:
1004 return "NPHY";
1005 default:
1006 return "Unknown";
1007 }
1008}
1009
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301010static enum cb_err cse_locate_area_as_rdev_rw(size_t bp, struct region_device *cse_rdev)
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301011{
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"))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301021 cse_get_bp_entry_range(RO, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301022 else
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301023 cse_get_bp_entry_range(RW, &start_offset, &end_offset);
Sridhar Siricilla52479c72022-03-30 09:25:49 +05301024
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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301035static enum cb_err cse_sub_part_get_target_rdev(struct region_device *target_rdev, size_t bp,
1036 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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301043 if (cse_locate_area_as_rdev_rw(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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301124static enum cb_err cse_prep_for_component_update(void)
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 */
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301131 if (cse_boot_to_ro() != CB_SUCCESS)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301132 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,
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301176 const char *name)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301177{
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++) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301197 if (cse_sub_part_get_target_rdev(&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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301221 if (cse_prep_for_component_update() != 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
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301237static enum csme_failure_reason cse_sub_part_fw_update(void)
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 Bhatc3c71c372023-08-07 21:48:23 +05301245 rv = cse_sub_part_fw_component_update(IOM_FW, CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301246
1247 handle_cse_sub_part_fw_update_rv(rv);
1248
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301249 rv = cse_sub_part_fw_component_update(NPHY_FW, CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301250
1251 return handle_cse_sub_part_fw_update_rv(rv);
1252}
1253
Subrata Banik5ff01182023-04-20 11:08:17 +05301254static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301255{
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301256 /*
1257 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1258 * is not enabled and continue to update CSE sub-partitions.
1259 */
1260 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301261 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301262 return;
1263 }
1264
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301265 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1266 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301267 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301268 return;
1269 }
1270
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301271 if (cse_get_bp_info() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301272 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301273
1274 /* If system is in recovery mode, don't trigger recovery again */
1275 if (!vboot_recovery_mode_enabled()) {
1276 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1277 } else {
1278 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1279 "so no action\n");
1280 return;
1281 }
1282 }
1283
Krishna P Bhat D64ec6a72023-09-21 22:19:47 +05301284 /* Store the CSE RW Firmware Version into CBMEM */
1285 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1286 cse_store_rw_fw_version();
1287
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301288 /*
1289 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301290 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-partition 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()) {
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301294 if (cse_sub_part_fw_update() == CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301295 cse_board_reset();
1296 do_global_reset();
1297 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1298 }
1299
1300 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301301 }
1302
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301303 if (cse_fix_data_failure_err() != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001304 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301305
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301306 /*
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001307 * CSE firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301308 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1309 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301310 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301311 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301312 uint8_t rv;
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301313 rv = cse_fw_update();
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301314 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001315 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301316 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301317
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301318 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301319 cse_sub_part_fw_update();
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301320
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301321 if (!cse_is_rw_bp_status_valid())
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001322 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301323
Krishna Prasad Bhatc3c71c372023-08-07 21:48:23 +05301324 if (cse_boot_to_rw() != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301325 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001326 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301327 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301328}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301329
Subrata Banik5ff01182023-04-20 11:08:17 +05301330void cse_fw_sync(void)
1331{
1332 timestamp_add_now(TS_CSE_FW_SYNC_START);
1333 do_cse_fw_sync();
1334 timestamp_add_now(TS_CSE_FW_SYNC_END);
1335}
1336
Subrata Banik7f66adb2023-04-14 00:35:38 +05301337static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1338 struct fw_version_resp *resp)
1339{
1340 enum cse_tx_rx_status ret;
1341 struct fw_version_msg {
1342 struct mkhi_hdr hdr;
1343 enum fpt_partition_id partition_id;
1344 } __packed msg = {
1345 .hdr = {
1346 .group_id = MKHI_GROUP_ID_GEN,
1347 .command = GEN_GET_IMAGE_FW_VERSION,
1348 },
1349 .partition_id = id,
1350 };
1351
1352 /*
1353 * Prerequisites:
1354 * 1) HFSTS1 CWS is Normal
1355 * 2) HFSTS1 COM is Normal
1356 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1357 */
1358
1359 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1360 !cse_is_hfs1_com_normal()) {
1361 printk(BIOS_ERR,
1362 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1363 return CB_ERR;
1364 }
1365
1366 size_t resp_size = sizeof(struct fw_version_resp);
1367 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1368
1369 if (ret || resp->hdr.result) {
1370 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1371 id, resp->hdr.result);
1372 return CB_ERR;
1373 }
1374
1375 return CB_SUCCESS;
1376}
1377
Subrata Banik044fc9f2023-04-14 02:34:37 +05301378static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1379 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301380{
1381 if (vboot_recovery_mode_enabled()) {
1382 printk(BIOS_WARNING,
1383 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1384 return CB_ERR;
1385 }
1386
1387 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1388 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1389 return CB_ERR;
1390 }
1391
1392 return send_get_fpt_partition_info_cmd(id, resp);
1393}
1394
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001395static bool is_ish_version_valid(struct cse_fw_ish_version_info *version)
1396{
1397 const struct fw_version invalid_fw = {0, 0, 0, 0};
1398 if (!memcmp(&version->cur_ish_fw_version, &invalid_fw, sizeof(struct fw_version)))
1399 return false;
1400 return true;
1401}
1402
Subrata Banikb1b7c532023-04-14 01:36:13 +05301403/*
1404 * Helper function to read ISH version from CSE FPT using HECI command.
1405 *
1406 * The HECI command only be executed after memory has been initialized.
1407 * This is because the command relies on resources that are not available
1408 * until DRAM initialization command has been sent.
1409 */
1410static void store_ish_version(void)
1411{
1412 if (!ENV_RAMSTAGE)
1413 return;
1414
1415 if (vboot_recovery_mode_enabled())
1416 return;
1417
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001418 struct cse_specific_info *cse_info_in_cbmem = cbmem_find(CBMEM_ID_CSE_INFO);
1419 if (cse_info_in_cbmem == NULL)
Subrata Banikb1b7c532023-04-14 01:36:13 +05301420 return;
1421
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001422 struct cse_specific_info cse_info_in_cmos;
1423 cmos_read_fw_partition_info(&cse_info_in_cmos);
Subrata Banik88512b02023-08-17 15:44:52 +00001424
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001425 struct cse_fw_partition_info *cbmem_version = &(cse_info_in_cbmem->cse_fwp_version);
1426 struct cse_fw_partition_info *cmos_version = &(cse_info_in_cmos.cse_fwp_version);
Subrata Banikb1b7c532023-04-14 01:36:13 +05301427
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001428 /* Get current cse firmware state */
1429 enum cse_fw_state fw_state = get_cse_state(
1430 &(cbmem_version->cur_cse_fw_version),
1431 &(cmos_version->ish_partition_info.prev_cse_fw_version),
1432 &(cbmem_version->ish_partition_info.prev_cse_fw_version));
Subrata Banikb1b7c532023-04-14 01:36:13 +05301433
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001434 if (fw_state == CSE_FW_WARM_BOOT) {
1435 return;
1436 } else {
1437 if (fw_state == CSE_FW_COLD_BOOT &&
1438 is_ish_version_valid(&(cmos_version->ish_partition_info))) {
1439 /* CMOS data is persistent across cold boots */
1440 memcpy(&(cse_info_in_cbmem->cse_fwp_version.ish_partition_info),
1441 &(cse_info_in_cmos.cse_fwp_version.ish_partition_info),
1442 sizeof(struct cse_fw_ish_version_info));
1443 store_cse_info_crc(cse_info_in_cbmem);
1444 } else {
1445 /*
1446 * Current running CSE version is different than previous stored CSE version
1447 * which could be due to CSE update or rollback, hence, need to send ISHC
1448 * partition info cmd to know the currently running ISH version.
1449 */
1450 struct fw_version_resp resp;
1451 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC,
1452 &resp) == CB_SUCCESS) {
1453 /* Update stored CSE version with current cse version */
1454 memcpy(&(cbmem_version->ish_partition_info.prev_cse_fw_version),
1455 &(cbmem_version->cur_cse_fw_version), sizeof(struct fw_version));
Subrata Banik88512b02023-08-17 15:44:52 +00001456
Dinesh Gehlota9232d82023-06-10 11:53:47 +00001457 /* Retrieve and update current ish version */
1458 memcpy(&(cbmem_version->ish_partition_info.cur_ish_fw_version),
1459 &(resp.manifest_data.version), sizeof(struct fw_version));
1460
1461 /* Update the CRC */
1462 store_cse_info_crc(cse_info_in_cbmem);
1463
1464 /* Update CMOS with current CSE FPT versions.*/
1465 cmos_write_fw_partition_info(cse_info_in_cbmem);
1466 }
Subrata Banikb1b7c532023-04-14 01:36:13 +05301467 }
1468 }
1469}
1470
Subrata Banikfc313d62023-04-14 01:31:29 +05301471static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301472{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301473 if (acpi_get_sleep_type() == ACPI_S3)
1474 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301475
Subrata Banik5ff01182023-04-20 11:08:17 +05301476 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301477 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301478
1479 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301480 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301481 * is available
1482 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301483 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301484 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301485}
1486
Subrata Banikfc313d62023-04-14 01:31:29 +05301487BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);