blob: 908b5de1fef51e2d513857585b7decfceaadbd2f [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
20#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
21#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
22#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
23#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
24#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053025
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053026/* Converts bp index to boot partition string */
27#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
28
Rizwan Qureshiec321092019-09-06 20:28:43 +053029/* CSE RW boot partition signature */
30#define CSE_RW_SIGNATURE 0x000055aa
31
32/* CSE RW boot partition signature size */
33#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
34
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053035/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053036 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053037 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053038 * CSE Lite SKU Image Layout:
Angel Pons2e8e0602022-08-13 19:50:28 +020039 * +------------+ +----+------+----+ +-----+------+-----+
40 * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 |
41 * +------------+ +----+------+----+ +-----+------+-----+
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053042 */
43#define CSE_MAX_BOOT_PARTITIONS 3
44
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053045/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053046enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053047 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053048 RO = 0,
49
Rizwan Qureshiec321092019-09-06 20:28:43 +053050 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053051 RW = 1
52};
53
54/*
55 * Boot partition status.
56 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
57 */
58enum bp_status {
59 /* This value is returned when a partition has no errors */
60 BP_STATUS_SUCCESS = 0,
61
62 /*
63 * This value is returned when a partition should be present based on layout, but it is
64 * not valid.
65 */
66 BP_STATUS_GENERAL_FAILURE = 1,
67
68 /* This value is returned when a partition is not present per initial image layout */
69 BP_STATUS_PARTITION_NOT_PRESENT = 2,
70
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053071 /*
72 * This value is returned when unexpected issues are detected in CSE Data area
73 * and CSE TCB-SVN downgrade scenario.
74 */
75 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053076};
77
78/*
79 * Boot Partition Info Flags
80 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
81 */
82enum bp_info_flags {
83
84 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
85 BP_INFO_REDUNDANCY_EN = 1 << 0,
86
87 /* It indicates RO(BP1) supports Minimal Recovery Mode */
88 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
89
90 /*
91 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
92 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
93 */
94 BP_INFO_READ_ONLY_CFG = 1 << 2,
95};
96
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053097/* CSE boot partition entry info */
98struct cse_bp_entry {
99 /* Boot partition version */
100 struct fw_version fw_ver;
101
102 /* Boot partition status */
103 uint32_t status;
104
105 /* Starting offset of the partition within CSE region */
106 uint32_t start_offset;
107
108 /* Ending offset of the partition within CSE region */
109 uint32_t end_offset;
110 uint8_t reserved[12];
111} __packed;
112
113/* CSE boot partition info */
114struct cse_bp_info {
115 /* Number of boot partitions */
116 uint8_t total_number_of_bp;
117
118 /* Current boot partition */
119 uint8_t current_bp;
120
121 /* Next boot partition */
122 uint8_t next_bp;
123
124 /* Boot Partition Info Flags */
125 uint8_t flags;
126
127 /* Boot Partition Entry Info */
128 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
129} __packed;
130
131struct get_bp_info_rsp {
132 struct mkhi_hdr hdr;
133 struct cse_bp_info bp_info;
134} __packed;
135
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530136static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700137
Subrata Banik65a6d172023-08-13 13:03:50 +0000138static struct cse_specific_info cse_info;
139
Subrata Banikda527ec2022-11-24 15:42:35 +0530140void cse_log_ro_write_protection_info(bool mfg_mode)
141{
142 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
143
144 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
145 cse_ro_wp_en ? "YES" : "NO");
146
147 if (cse_ro_wp_en) {
148 uint32_t base, limit;
149 spi_get_wp_cse_ro_range(&base, &limit);
150 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
151 base, limit);
152 }
153
154 /*
155 * If manufacturing mode is disabled, but CSE RO is not write protected,
156 * log error.
157 */
158 if (!mfg_mode && !cse_ro_wp_en)
159 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
160}
161
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530162enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700163{
164 struct cse_boot_perf_req {
165 struct mkhi_hdr hdr;
166 uint32_t reserved;
167 } __packed;
168
169 struct cse_boot_perf_req req = {
170 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
171 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
172 .reserved = 0,
173 };
174
175 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
176
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530177 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700178 HECI_MKHI_ADDR)) {
179 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530180 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700181 }
182
183 if (boot_perf_rsp->hdr.result) {
184 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
185 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530186 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700187 }
188
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530189 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700190}
191
192
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530193static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
194{
195 return cse_bp_info->current_bp;
196}
197
198static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
199 const struct cse_bp_info *cse_bp_info)
200{
201 return &cse_bp_info->bp_entries[bp];
202}
203
Subrata Banik65a6d172023-08-13 13:03:50 +0000204static bool is_cbmem_cse_info_valid(const struct cse_specific_info *info)
205{
206 uint32_t crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
207
208 /*
209 * Authenticate the CBMEM persistent data.
210 *
211 * The underlying assumption is that an event (i.e., CSE upgrade/downgrade) which
212 * could change the values stored in this region has to also trigger the global
213 * reset. Hence, CBMEM persistent data won't be available any time after such
214 * event (global reset or cold reset) being initiated.
215 *
216 * During warm boot scenarios CBMEM contents remain persistent hence, we don't
217 * want to override the existing data in CBMEM to avoid any additional boot latency.
218 */
219 if (info->crc != crc)
220 return false;
221
222 return true;
223}
224
225static void cbmem_store_cse_info_crc(struct cse_specific_info *info)
226{
227 info->crc = ~CRC(info, offsetof(struct cse_specific_info, crc), crc32_byte);
228}
229
230/*
231 * Helper function that stores current CSE firmware version to CBMEM memory,
232 * except during recovery mode.
233 */
234static void cse_store_rw_fw_version(const struct cse_bp_entry *cse_bp)
235{
236 if (vboot_recovery_mode_enabled())
237 return;
238
239 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)) {
240 memcpy(&(cse_info.cse_fwp_version.cur_cse_fw_version), &(cse_bp->fw_ver),
241 sizeof(struct fw_version));
242 return;
243 }
244
245 struct cse_specific_info *info = cbmem_add(CBMEM_ID_CSE_INFO, sizeof(*info));
246 if (!info)
247 return;
248
249 /* Avoid CBMEM update if CBMEM already has persistent data */
250 if (is_cbmem_cse_info_valid(info))
251 return;
252
253 memset(info, 0, sizeof(*info));
254
255 memcpy(&(info->cse_fwp_version.cur_cse_fw_version), &(cse_bp->fw_ver),
256 sizeof(struct fw_version));
257
258 /* Update the CRC */
259 cbmem_store_cse_info_crc(info);
260}
261
262#if CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_ROMSTAGE)
263/* Function to copy PRERAM CSE specific info to pertinent CBMEM. */
264static void preram_cse_info_sync_to_cbmem(int is_recovery)
265{
266 if (vboot_recovery_mode_enabled() || !CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
267 return;
268
269 struct cse_specific_info *info = cbmem_add(CBMEM_ID_CSE_INFO, sizeof(*info));
270 if (!info)
271 return;
272
273 /* Avoid sync into CBMEM if CBMEM already has persistent data */
274 if (is_cbmem_cse_info_valid(info))
275 return;
276
277 memcpy(info, &cse_info, sizeof(struct cse_specific_info));
278
279 /* Update the CRC */
280 cbmem_store_cse_info_crc(info);
281}
282
283CBMEM_CREATION_HOOK(preram_cse_info_sync_to_cbmem);
284#endif
285
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530286static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
287{
288 const struct cse_bp_entry *cse_bp;
289
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530290 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
291 cse_bp_info->total_number_of_bp);
292 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
293 GET_BP_STR(cse_bp_info->current_bp));
294 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
295 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530296
297 /* Log version info of RO & RW partitions */
298 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530299 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 +0530300 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
301 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
302 cse_bp->status, cse_bp->start_offset,
303 cse_bp->end_offset);
304
305 cse_bp = cse_get_bp_entry(RW, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530306 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 +0530307 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
308 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
309 cse_bp->status, cse_bp->start_offset,
310 cse_bp->end_offset);
Subrata Banik65a6d172023-08-13 13:03:50 +0000311
312 /* Store the CSE RW Firmware Version into CBMEM */
313 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
314 cse_store_rw_fw_version(cse_bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530315}
316
317/*
318 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
319 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
320 * It allows execution of the Boot Partition commands in below scenarios:
321 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
322 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
323 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530324 * The prerequisite check should be handled in cse_get_bp_info() and
325 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
326 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530327 */
328static bool cse_is_bp_cmd_info_possible(void)
329{
330 if (cse_is_hfs1_cws_normal()) {
331 if (cse_is_hfs1_com_normal())
332 return true;
333 if (cse_is_hfs1_com_secover_mei_msg())
334 return true;
335 if (cse_is_hfs1_com_soft_temp_disable())
336 return true;
337 }
338 return false;
339}
340
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530341static enum cb_err cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530342{
343 struct get_bp_info_req {
344 struct mkhi_hdr hdr;
345 uint8_t reserved[4];
346 } __packed;
347
348 struct get_bp_info_req info_req = {
349 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
350 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
351 .reserved = {0},
352 };
353
354 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530355 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530356 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530357 }
358
359 size_t resp_size = sizeof(struct get_bp_info_rsp);
360
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530361 if (heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530362 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530363 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530364 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530365 }
366
367 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530368 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530369 bp_info_rsp->hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530370 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530371 }
372
373 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
374
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530375 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530376}
377/*
378 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
379 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
380 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
381 * This function must be used before EOP.
382 * Returns false on failure and true on success.
383 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530384static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530385{
386 struct set_boot_partition_info_req {
387 struct mkhi_hdr hdr;
388 uint8_t next_bp;
389 uint8_t reserved[3];
390 } __packed;
391
392 struct set_boot_partition_info_req switch_req = {
393 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
394 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
395 .next_bp = bp,
396 .reserved = {0},
397 };
398
399 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530400 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530401 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530402 }
403
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530404 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530405
406 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530407 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530408 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530409 }
410
411 struct mkhi_hdr switch_resp;
412 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
413
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530414 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530415 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530416 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530417
418 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530419 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530420 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530421 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530422 }
423
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530424 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530425}
426
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530427static enum cb_err cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530428{
429 struct data_clr_request {
430 struct mkhi_hdr hdr;
431 uint8_t reserved[4];
432 } __packed;
433
434 struct data_clr_request data_clr_rq = {
435 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
436 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
437 .reserved = {0},
438 };
439
440 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
441 cse_get_current_bp(cse_bp_info) != RO) {
442 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530443 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530444 }
445
446 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
447
448 struct mkhi_hdr data_clr_rsp;
449 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
450
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530451 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530452 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530453 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530454 }
455
456 if (data_clr_rsp.result) {
457 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
458 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530459 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530460 }
461
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530462 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530463}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530464
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600465__weak void cse_board_reset(void)
466{
467 /* Default weak implementation, does nothing. */
468}
469
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700470__weak void cse_fw_update_misc_oper(void)
471{
472 /* Default weak implementation, does nothing. */
473}
474
Rizwan Qureshiec321092019-09-06 20:28:43 +0530475/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530476static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530477{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530478 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
479 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530480
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600481 /* Allow the board to perform a reset for CSE RO<->RW jump */
482 cse_board_reset();
483
484 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000485 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530486
Rizwan Qureshiec321092019-09-06 20:28:43 +0530487 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530488
489 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530490 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530491}
492
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530493static enum cb_err cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530494{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530495 if (cse_get_current_bp(cse_bp_info) == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530496 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530497
Rizwan Qureshiec321092019-09-06 20:28:43 +0530498 return cse_set_and_boot_from_next_bp(RW);
499}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530500
V Sowmyaf9905522020-11-12 20:19:04 +0530501/* Check if CSE RW data partition is valid or not */
502static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
503{
504 const struct cse_bp_entry *rw_bp;
505
506 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
507 return rw_bp->status != BP_STATUS_DATA_FAILURE;
508}
509
510/*
511 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
512 * otherwise false if any operation fails.
513 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530514static enum cb_err cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530515{
516 /*
517 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
518 * - Send DATA CLEAR HECI command to CSE
519 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
520 * - Issue GLOBAL RESET HECI command.
521 */
522 if (cse_is_rw_dp_valid(cse_bp_info))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530523 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530524
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530525 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS)
526 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530527
528 return cse_boot_to_rw(cse_bp_info);
529}
530
V Sowmyaf9905522020-11-12 20:19:04 +0530531static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530532 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530533{
534 const struct cse_bp_entry *cse_bp;
535
536 cse_bp = cse_get_bp_entry(bp, bp_info);
537 return &cse_bp->fw_ver;
538}
539
540static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
541{
542 return cse_get_bp_entry_version(RW, cse_bp_info);
543}
544
545static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530546 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530547{
548 const struct cse_bp_entry *cse_bp;
549
550 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
551
552 if (start_offset)
553 *start_offset = cse_bp->start_offset;
554
555 if (end_offset)
556 *end_offset = cse_bp->end_offset;
557
558}
559
560static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
561{
562 const struct cse_bp_entry *rw_bp;
563
564 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
565
566 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
567 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
568 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
569 return false;
570 }
571 return true;
572}
573
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530574static enum cb_err cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530575{
576 if (cse_get_current_bp(cse_bp_info) == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530577 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530578
579 return cse_set_and_boot_from_next_bp(RO);
580}
581
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530582static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530583{
584 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
585 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530586 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530587 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530588 }
589
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530590 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530591}
592
Rizwan Qureshiec321092019-09-06 20:28:43 +0530593static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
594{
595 uint32_t cse_bp_sign;
596
597 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
598 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
599 return false;
600 }
601
602 return cse_bp_sign == CSE_RW_SIGNATURE;
603}
604
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530605static enum cb_err cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Rizwan Qureshiec321092019-09-06 20:28:43 +0530606 struct region_device *target_rdev)
607{
608 struct region_device cse_region_rdev;
609 size_t size;
610 uint32_t start_offset;
611 uint32_t end_offset;
612
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530613 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
614 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530615
616 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
617 size = end_offset + 1 - start_offset;
618
619 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530620 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530621
622 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100623 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530624
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530625 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530626}
627
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530628static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530629{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530630 struct vb2_context *ctx = vboot_get_context();
631 if (ctx == NULL)
632 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530633
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530634 if (vboot_is_firmware_slot_a(ctx))
635 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
636
637 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
638}
639
Rizwan Qureshiec321092019-09-06 20:28:43 +0530640/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530641 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
642 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530643 * If ver_cmp_status = 0, no update is required
644 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
645 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
646 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530647static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530648{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700649 if (a->major != b->major)
650 return a->major - b->major;
651 else if (a->minor != b->minor)
652 return a->minor - b->minor;
653 else if (a->hotfix != b->hotfix)
654 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530655 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700656 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530657}
658
659/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
660static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
661 const void *rw_blob, const size_t rw_blob_sz)
662
663{
Julius Wernerd96ca242022-08-08 18:08:35 -0700664 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530665
Julius Wernerd96ca242022-08-08 18:08:35 -0700666 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
667 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530668 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
669 return false;
670 }
671
Julius Wernerd96ca242022-08-08 18:08:35 -0700672 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530673 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
674 "the provided SHA in the metadata\n");
675 return false;
676 }
677 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
678 " provided hash in the metadata\n");
679 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530680}
681
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530682static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530683{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530684 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
685 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530686 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530687 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530688 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530689}
690
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530691static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530692 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530693{
694 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
695 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530696 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530697 }
698
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530699 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530700}
701
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700702enum cse_update_status {
703 CSE_UPDATE_NOT_REQUIRED,
704 CSE_UPDATE_UPGRADE,
705 CSE_UPDATE_DOWNGRADE,
706 CSE_UPDATE_CORRUPTED,
707 CSE_UPDATE_METADATA_ERROR,
708};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530709
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700710static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
711{
712 if ((*curr - start) >= size) {
713 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
714 return false;
715 }
716
717 *ver_field = skip_atoi(curr);
718 (*curr)++;
719 return true;
720}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530721
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530722static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
723{
724 char *version_str, *cbfs_ptr;
725 size_t size;
726
727 if (cbfs_rw_version == NULL)
728 return CB_ERR;
729
730 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
731 version_str = cbfs_ptr;
732 if (!version_str) {
733 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
734 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
735 return CB_ERR;
736 }
737
738 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
739 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
740 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
741 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
742 cbfs_unmap(version_str);
743 return CB_ERR;
744 }
745
746 cbfs_unmap(version_str);
747 return CB_SUCCESS;
748}
749
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700750static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530751 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530752{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700753 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700754 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700755
756 if (!cse_is_rw_bp_sign_valid(target_rdev))
757 return CSE_UPDATE_CORRUPTED;
758
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530759 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700760 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700761
762 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700763 cbfs_rw_version.major,
764 cbfs_rw_version.minor,
765 cbfs_rw_version.hotfix,
766 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700767
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530768 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700769 if (ret == 0)
770 return CSE_UPDATE_NOT_REQUIRED;
771 else if (ret < 0)
772 return CSE_UPDATE_DOWNGRADE;
773 else
774 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530775}
776
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530777static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530778 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530779{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530780 /* Points to CSE CBFS RW image after boot partition signature */
781 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
782
783 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530784 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530785
786 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530787 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
788 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
789 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530790
791 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530792 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
793 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530794
795 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530796 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530797}
798
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530799static bool is_cse_fw_update_enabled(void)
800{
801 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
802 return false;
803
804 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
805 return !is_debug_cse_fw_update_disable();
806
807 return true;
808}
809
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530810static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
811 const void *cse_cbfs_rw, const size_t cse_blob_sz,
812 struct region_device *target_rdev)
813{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530814 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530815 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
816 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530817 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
818 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530819
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530820 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530821 return CSE_LITE_SKU_FW_UPDATE_ERROR;
822
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530823 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530824 return CSE_LITE_SKU_FW_UPDATE_ERROR;
825
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600826 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530827}
828
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530829static enum cb_err cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700830 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530831{
832 /*
833 * To set CSE's operation mode to HMRFPO mode:
834 * 1. Ensure CSE to boot from RO(BP1)
835 * 2. Send HMRFPO_ENABLE command to CSE
836 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530837 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
838 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530839
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700840 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530841 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700842 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530843 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700844 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530845 }
846
Rizwan Qureshiec321092019-09-06 20:28:43 +0530847 return cse_hmrfpo_enable();
848}
849
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530850static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700851 enum cse_update_status status,
852 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530853{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530854 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700855 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530856 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700857 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530858
Julius Werner18881f992021-04-13 15:28:12 -0700859 const char *area_name = cse_get_source_rdev_fmap();
860 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530861 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530862
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530863 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
864 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
865 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
866 } else {
867 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
868 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
869 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530870 if (!cse_cbfs_rw) {
871 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
872 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
873 }
874
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700875 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
876 if (!cbfs_rw_hash) {
877 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
878 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
879 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
880 goto error_exit;
881 }
882
Julius Werner18881f992021-04-13 15:28:12 -0700883 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530884 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
885 goto error_exit;
886 }
887
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530888 if (cse_prep_for_rw_update(cse_bp_info, status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600889 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530890 goto error_exit;
891 }
892
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700893 cse_fw_update_misc_oper();
Julius Werner18881f992021-04-13 15:28:12 -0700894 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530895
896error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700897 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700898 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530899 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530900}
901
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530902static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530903{
904 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700905 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530906
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530907 if (cse_get_target_rdev(cse_bp_info, &target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +0530908 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
909 return CSE_LITE_SKU_RW_ACCESS_ERROR;
910 }
911
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700912 status = cse_check_update_status(cse_bp_info, &target_rdev);
913 if (status == CSE_UPDATE_NOT_REQUIRED)
914 return CSE_NO_ERROR;
915 if (status == CSE_UPDATE_METADATA_ERROR)
916 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530917
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700918 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
919 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530920}
921
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530922static const char *cse_sub_part_str(enum bpdt_entry_type type)
923{
924 switch (type) {
925 case IOM_FW:
926 return "IOM";
927 case NPHY_FW:
928 return "NPHY";
929 default:
930 return "Unknown";
931 }
932}
933
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530934static enum cb_err cse_locate_area_as_rdev_rw(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530935 size_t bp, struct region_device *cse_rdev)
936{
937 struct region_device cse_region_rdev;
938 uint32_t size;
939 uint32_t start_offset;
940 uint32_t end_offset;
941
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530942 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
943 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530944
945 if (!strcmp(cse_regions[bp], "RO"))
946 cse_get_bp_entry_range(cse_bp_info, RO, &start_offset, &end_offset);
947 else
948 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
949
950 size = end_offset + 1 - start_offset;
951
952 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530953 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530954
955 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
956 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530957 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530958}
959
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530960static enum cb_err cse_sub_part_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530961 struct region_device *target_rdev, size_t bp, enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530962{
963 struct bpdt_header bpdt_hdr;
964 struct region_device cse_rdev;
965 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
966 uint8_t i;
967
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530968 if (cse_locate_area_as_rdev_rw(cse_bp_info, bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530969 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
970 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530971 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530972 }
973
974 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
975 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530976 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530977 }
978
979 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
980 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
981 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
982 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530983 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530984 }
985
986 /* walk through BPDT entries to identify sub-partition's payload offset and size */
987 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
988 if (bpdt_entries[i].type == type) {
989 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
990 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
991 bpdt_entries[i].size);
992
993 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
994 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530995 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530996 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530997 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530998 }
999 }
1000
1001 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301002 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301003}
1004
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301005static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301006 const struct region_device *rdev,
1007 struct fw_version *fw_ver)
1008{
1009 struct subpart_entry subpart_entry;
1010 struct subpart_entry_manifest_header man_hdr;
1011
1012 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
1013 != SUBPART_ENTRY_SZ) {
1014 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
1015 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301016 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301017 }
1018
1019 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
1020 != SUBPART_MANIFEST_HDR_SZ) {
1021 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
1022 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301023 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301024 }
1025
1026 fw_ver->major = man_hdr.binary_version.major;
1027 fw_ver->minor = man_hdr.binary_version.minor;
1028 fw_ver->hotfix = man_hdr.binary_version.hotfix;
1029 fw_ver->build = man_hdr.binary_version.build;
1030
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301031 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301032}
1033
1034static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
1035{
1036 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
1037 struct subpart_entry *subpart_entry;
1038 struct subpart_entry_manifest_header *man_hdr;
1039
Elyes Haouas9018dee2022-11-18 15:07:33 +01001040 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
1041 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301042
1043 fw_ver->major = man_hdr->binary_version.major;
1044 fw_ver->minor = man_hdr->binary_version.minor;
1045 fw_ver->hotfix = man_hdr->binary_version.hotfix;
1046 fw_ver->build = man_hdr->binary_version.build;
1047}
1048
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301049static enum cb_err cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301050{
1051 /*
1052 * To set CSE's operation mode to HMRFPO mode:
1053 * 1. Ensure CSE to boot from RO(BP1)
1054 * 2. Send HMRFPO_ENABLE command to CSE
1055 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301056 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
1057 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301058
1059 return cse_hmrfpo_enable();
1060}
1061
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301062static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
1063 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301064 struct region_device *target_rdev)
1065{
1066 if (region_device_sz(target_rdev) < blob_sz) {
1067 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
1068 "smaller than blob size:%zx, abort update\n",
1069 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
1070 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
1071 }
1072
1073 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301074 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301075 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1076
1077 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +05301078 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301079 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
1080
1081 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
1082 cse_sub_part_str(type));
1083
1084 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
1085}
1086
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301087static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301088{
1089 switch (rv) {
1090 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1091 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1092 return rv;
1093 default:
1094 cse_trigger_vboot_recovery(rv);
1095 }
1096 /* Control never reaches here */
1097 return rv;
1098}
1099
1100static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
1101 const struct cse_bp_info *cse_bp_info, const char *name)
1102{
1103 struct region_device target_rdev;
1104 struct fw_version target_fw_ver, source_fw_ver;
1105 enum csme_failure_reason rv;
1106 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301107
1108 void *subpart_cbfs_rw = cbfs_map(name, &size);
1109 if (!subpart_cbfs_rw) {
1110 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1111 cse_sub_part_str(type));
1112 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1113 }
1114
1115 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1116 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1117 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1118 source_fw_ver.build);
1119
1120 /* Trigger sub-partition update in CSE RO and CSE RW */
1121 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301122 if (cse_sub_part_get_target_rdev(cse_bp_info, &target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301123 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1124 goto error_exit;
1125 }
1126
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301127 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301128 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1129 goto error_exit;
1130 }
1131
1132 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1133 cse_sub_part_str(type), target_fw_ver.major,
1134 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1135
1136 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1137 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1138 cse_regions[bp], cse_sub_part_str(type));
1139 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1140 continue;
1141 }
1142
1143 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1144 cse_sub_part_str(type));
1145
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301146 if (cse_prep_for_component_update(cse_bp_info) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301147 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1148 goto error_exit;
1149 }
1150
1151 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1152 size, &target_rdev);
1153
1154 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1155 goto error_exit;
1156 }
1157error_exit:
1158 cbfs_unmap(subpart_cbfs_rw);
1159 return rv;
1160}
1161
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301162static enum csme_failure_reason cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301163{
1164 if (skip_cse_sub_part_update()) {
1165 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1166 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1167 }
1168
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301169 enum csme_failure_reason rv;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301170 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
1171 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1172
1173 handle_cse_sub_part_fw_update_rv(rv);
1174
1175 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
1176 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1177
1178 return handle_cse_sub_part_fw_update_rv(rv);
1179}
1180
Subrata Banik5ff01182023-04-20 11:08:17 +05301181static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301182{
1183 static struct get_bp_info_rsp cse_bp_info;
1184
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301185 /*
1186 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1187 * is not enabled and continue to update CSE sub-partitions.
1188 */
1189 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301190 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301191 return;
1192 }
1193
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301194 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1195 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301196 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301197 return;
1198 }
1199
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +05301200 if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301201 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301202
1203 /* If system is in recovery mode, don't trigger recovery again */
1204 if (!vboot_recovery_mode_enabled()) {
1205 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1206 } else {
1207 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1208 "so no action\n");
1209 return;
1210 }
1211 }
1212
1213 /*
1214 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Alexander Goncharov893c3ae82023-02-04 15:20:37 +04001215 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-parition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301216 * fails during recovery, just continue to boot.
1217 */
1218 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1219 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1220 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1221 cse_board_reset();
1222 do_global_reset();
1223 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1224 }
1225
1226 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301227 }
1228
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301229 if (cse_fix_data_failure_err(&cse_bp_info.bp_info) != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001230 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301231
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301232 /*
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301233 * cse firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
1234 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1235 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301236 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301237 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301238 uint8_t rv;
1239 rv = cse_fw_update(&cse_bp_info.bp_info);
1240 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001241 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301242 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301243
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301244 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1245 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1246
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301247 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001248 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301249
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301250 if (cse_boot_to_rw(&cse_bp_info.bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301251 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001252 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301253 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301254}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301255
Subrata Banik5ff01182023-04-20 11:08:17 +05301256void cse_fw_sync(void)
1257{
1258 timestamp_add_now(TS_CSE_FW_SYNC_START);
1259 do_cse_fw_sync();
1260 timestamp_add_now(TS_CSE_FW_SYNC_END);
1261}
1262
Subrata Banik7f66adb2023-04-14 00:35:38 +05301263static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1264 struct fw_version_resp *resp)
1265{
1266 enum cse_tx_rx_status ret;
1267 struct fw_version_msg {
1268 struct mkhi_hdr hdr;
1269 enum fpt_partition_id partition_id;
1270 } __packed msg = {
1271 .hdr = {
1272 .group_id = MKHI_GROUP_ID_GEN,
1273 .command = GEN_GET_IMAGE_FW_VERSION,
1274 },
1275 .partition_id = id,
1276 };
1277
1278 /*
1279 * Prerequisites:
1280 * 1) HFSTS1 CWS is Normal
1281 * 2) HFSTS1 COM is Normal
1282 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1283 */
1284
1285 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1286 !cse_is_hfs1_com_normal()) {
1287 printk(BIOS_ERR,
1288 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1289 return CB_ERR;
1290 }
1291
1292 size_t resp_size = sizeof(struct fw_version_resp);
1293 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1294
1295 if (ret || resp->hdr.result) {
1296 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1297 id, resp->hdr.result);
1298 return CB_ERR;
1299 }
1300
1301 return CB_SUCCESS;
1302}
1303
Subrata Banik044fc9f2023-04-14 02:34:37 +05301304static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1305 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301306{
1307 if (vboot_recovery_mode_enabled()) {
1308 printk(BIOS_WARNING,
1309 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1310 return CB_ERR;
1311 }
1312
1313 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1314 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1315 return CB_ERR;
1316 }
1317
1318 return send_get_fpt_partition_info_cmd(id, resp);
1319}
1320
Subrata Banikb1b7c532023-04-14 01:36:13 +05301321/*
1322 * Helper function to read ISH version from CSE FPT using HECI command.
1323 *
1324 * The HECI command only be executed after memory has been initialized.
1325 * This is because the command relies on resources that are not available
1326 * until DRAM initialization command has been sent.
1327 */
1328static void store_ish_version(void)
1329{
1330 if (!ENV_RAMSTAGE)
1331 return;
1332
1333 if (vboot_recovery_mode_enabled())
1334 return;
1335
1336 struct cse_fw_partition_info *version;
1337 size_t size = sizeof(struct fw_version);
1338 version = cbmem_find(CBMEM_ID_CSE_PARTITION_VERSION);
1339 if (version == NULL)
1340 return;
1341
1342 /*
1343 * Compare if stored cse version (from the previous boot) is same as current
1344 * running cse version.
1345 */
1346 if (memcmp(&version->ish_partition_info.prev_cse_fw_version,
1347 &version->cur_cse_fw_version, sizeof(struct fw_version))) {
1348 /*
1349 * Current running CSE version is different than previous stored CSE version
1350 * which could be due to CSE update or rollback, hence, need to send ISHC
1351 * partition info cmd to know the currently running ISH version.
1352 */
1353
1354 struct fw_version_resp resp;
1355 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC, &resp) == CB_SUCCESS) {
1356 /* Update stored cse version with current version */
1357 memcpy(&(version->ish_partition_info.prev_cse_fw_version),
1358 &(version->cur_cse_fw_version), size);
1359
1360 /* Since cse version has been updated, ish version needs to be updated. */
1361 memcpy(&(version->ish_partition_info.cur_ish_fw_version),
1362 &(resp.manifest_data.version), size);
1363 }
1364 }
1365}
1366
Subrata Banikfc313d62023-04-14 01:31:29 +05301367static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301368{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301369 if (acpi_get_sleep_type() == ACPI_S3)
1370 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301371
Subrata Banik5ff01182023-04-20 11:08:17 +05301372 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301373 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301374
1375 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301376 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301377 * is available
1378 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301379 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301380 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301381}
1382
Subrata Banikfc313d62023-04-14 01:31:29 +05301383BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);