blob: 6edbd843a4b4b6093609c27f3d91237071a8b823 [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
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05303#include <console/console.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05304#include <cbfs.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05305#include <commonlib/region.h>
6#include <fmap.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05307#include <intelblocks/cse.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05308#include <intelblocks/cse_layout.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05309#include <security/vboot/vboot_common.h>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053010#include <security/vboot/misc.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053011#include <soc/intel/common/reset.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053012#include <arch/cpu.h>
13
14#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
15#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
16#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
17#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
18#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053019
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053020/* Converts bp index to boot partition string */
21#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
22
Rizwan Qureshiec321092019-09-06 20:28:43 +053023/* CSE RW boot partition signature */
24#define CSE_RW_SIGNATURE 0x000055aa
25
26/* CSE RW boot partition signature size */
27#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
28
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053029/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053030 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053031 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053032 * CSE Lite SKU Image Layout:
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053033 * ------------- ------------------- ---------------------
34 * |CSE REGION | => | RO | RW | DATA | => | BP1 | BP2 | DATA |
35 * ------------- ------------------- ---------------------
36 */
37#define CSE_MAX_BOOT_PARTITIONS 3
38
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053039/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053040enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053041 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053042 RO = 0,
43
Rizwan Qureshiec321092019-09-06 20:28:43 +053044 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053045 RW = 1
46};
47
48/*
49 * Boot partition status.
50 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
51 */
52enum bp_status {
53 /* This value is returned when a partition has no errors */
54 BP_STATUS_SUCCESS = 0,
55
56 /*
57 * This value is returned when a partition should be present based on layout, but it is
58 * not valid.
59 */
60 BP_STATUS_GENERAL_FAILURE = 1,
61
62 /* This value is returned when a partition is not present per initial image layout */
63 BP_STATUS_PARTITION_NOT_PRESENT = 2,
64
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053065 /*
66 * This value is returned when unexpected issues are detected in CSE Data area
67 * and CSE TCB-SVN downgrade scenario.
68 */
69 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053070};
71
72/*
73 * Boot Partition Info Flags
74 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
75 */
76enum bp_info_flags {
77
78 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
79 BP_INFO_REDUNDANCY_EN = 1 << 0,
80
81 /* It indicates RO(BP1) supports Minimal Recovery Mode */
82 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
83
84 /*
85 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
86 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
87 */
88 BP_INFO_READ_ONLY_CFG = 1 << 2,
89};
90
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053091/* CSE boot partition entry info */
92struct cse_bp_entry {
93 /* Boot partition version */
94 struct fw_version fw_ver;
95
96 /* Boot partition status */
97 uint32_t status;
98
99 /* Starting offset of the partition within CSE region */
100 uint32_t start_offset;
101
102 /* Ending offset of the partition within CSE region */
103 uint32_t end_offset;
104 uint8_t reserved[12];
105} __packed;
106
107/* CSE boot partition info */
108struct cse_bp_info {
109 /* Number of boot partitions */
110 uint8_t total_number_of_bp;
111
112 /* Current boot partition */
113 uint8_t current_bp;
114
115 /* Next boot partition */
116 uint8_t next_bp;
117
118 /* Boot Partition Info Flags */
119 uint8_t flags;
120
121 /* Boot Partition Entry Info */
122 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
123} __packed;
124
125struct get_bp_info_rsp {
126 struct mkhi_hdr hdr;
127 struct cse_bp_info bp_info;
128} __packed;
129
130static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
131{
132 return cse_bp_info->current_bp;
133}
134
135static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
136 const struct cse_bp_info *cse_bp_info)
137{
138 return &cse_bp_info->bp_entries[bp];
139}
140
141static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
142{
143 const struct cse_bp_entry *cse_bp;
144
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530145 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
146 cse_bp_info->total_number_of_bp);
147 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
148 GET_BP_STR(cse_bp_info->current_bp));
149 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
150 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530151
152 /* Log version info of RO & RW partitions */
153 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530154 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 +0530155 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
156 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
157 cse_bp->status, cse_bp->start_offset,
158 cse_bp->end_offset);
159
160 cse_bp = cse_get_bp_entry(RW, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530161 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 +0530162 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
163 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
164 cse_bp->status, cse_bp->start_offset,
165 cse_bp->end_offset);
166}
167
168/*
169 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
170 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
171 * It allows execution of the Boot Partition commands in below scenarios:
172 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
173 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
174 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530175 * The prerequisite check should be handled in cse_get_bp_info() and
176 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
177 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530178 */
179static bool cse_is_bp_cmd_info_possible(void)
180{
181 if (cse_is_hfs1_cws_normal()) {
182 if (cse_is_hfs1_com_normal())
183 return true;
184 if (cse_is_hfs1_com_secover_mei_msg())
185 return true;
186 if (cse_is_hfs1_com_soft_temp_disable())
187 return true;
188 }
189 return false;
190}
191
192static bool cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
193{
194 struct get_bp_info_req {
195 struct mkhi_hdr hdr;
196 uint8_t reserved[4];
197 } __packed;
198
199 struct get_bp_info_req info_req = {
200 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
201 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
202 .reserved = {0},
203 };
204
205 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530206 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530207 return false;
208 }
209
210 size_t resp_size = sizeof(struct get_bp_info_rsp);
211
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530212 if (!heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
213 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530214 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530215 return false;
216 }
217
218 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530219 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530220 bp_info_rsp->hdr.result);
221 return false;
222 }
223
224 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
225
226 return true;
227}
228/*
229 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
230 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
231 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
232 * This function must be used before EOP.
233 * Returns false on failure and true on success.
234 */
235static bool cse_set_next_boot_partition(enum boot_partition_id bp)
236{
237 struct set_boot_partition_info_req {
238 struct mkhi_hdr hdr;
239 uint8_t next_bp;
240 uint8_t reserved[3];
241 } __packed;
242
243 struct set_boot_partition_info_req switch_req = {
244 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
245 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
246 .next_bp = bp,
247 .reserved = {0},
248 };
249
250 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530251 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530252 return false;
253 }
254
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530255 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530256
257 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530258 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530259 return false;
260 }
261
262 struct mkhi_hdr switch_resp;
263 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
264
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530265 if (!heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
266 HECI_MKHI_ADDR))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530267 return false;
268
269 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530270 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530271 switch_resp.result);
272 return false;
273 }
274
275 return true;
276}
277
V Sowmyaf9905522020-11-12 20:19:04 +0530278static bool cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
279{
280 struct data_clr_request {
281 struct mkhi_hdr hdr;
282 uint8_t reserved[4];
283 } __packed;
284
285 struct data_clr_request data_clr_rq = {
286 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
287 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
288 .reserved = {0},
289 };
290
291 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
292 cse_get_current_bp(cse_bp_info) != RO) {
293 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
294 return false;
295 }
296
297 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
298
299 struct mkhi_hdr data_clr_rsp;
300 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
301
302 if (!heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530303 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
V Sowmyaf9905522020-11-12 20:19:04 +0530304 return false;
305 }
306
307 if (data_clr_rsp.result) {
308 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
309 data_clr_rsp.result);
310 return false;
311 }
312
313 return true;
314}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530315
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600316__weak void cse_board_reset(void)
317{
318 /* Default weak implementation, does nothing. */
319}
320
Rizwan Qureshiec321092019-09-06 20:28:43 +0530321/* Set the CSE's next boot partition and issues system reset */
322static bool cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530323{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530324 if (!cse_set_next_boot_partition(bp))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530325 return false;
326
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600327 /* Allow the board to perform a reset for CSE RO<->RW jump */
328 cse_board_reset();
329
330 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000331 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530332
Rizwan Qureshiec321092019-09-06 20:28:43 +0530333 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530334
335 /* Control never reaches here */
336 return false;
337}
338
Rizwan Qureshiec321092019-09-06 20:28:43 +0530339static bool cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530340{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530341 if (cse_get_current_bp(cse_bp_info) == RW)
342 return true;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530343
Rizwan Qureshiec321092019-09-06 20:28:43 +0530344 return cse_set_and_boot_from_next_bp(RW);
345}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530346
V Sowmyaf9905522020-11-12 20:19:04 +0530347/* Check if CSE RW data partition is valid or not */
348static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
349{
350 const struct cse_bp_entry *rw_bp;
351
352 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
353 return rw_bp->status != BP_STATUS_DATA_FAILURE;
354}
355
356/*
357 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
358 * otherwise false if any operation fails.
359 */
360static bool cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
361{
362 /*
363 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
364 * - Send DATA CLEAR HECI command to CSE
365 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
366 * - Issue GLOBAL RESET HECI command.
367 */
368 if (cse_is_rw_dp_valid(cse_bp_info))
369 return true;
370
371 if (!cse_data_clear_request(cse_bp_info))
372 return false;
373
374 return cse_boot_to_rw(cse_bp_info);
375}
376
V Sowmyaf9905522020-11-12 20:19:04 +0530377static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530378 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530379{
380 const struct cse_bp_entry *cse_bp;
381
382 cse_bp = cse_get_bp_entry(bp, bp_info);
383 return &cse_bp->fw_ver;
384}
385
386static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
387{
388 return cse_get_bp_entry_version(RW, cse_bp_info);
389}
390
391static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530392 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530393{
394 const struct cse_bp_entry *cse_bp;
395
396 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
397
398 if (start_offset)
399 *start_offset = cse_bp->start_offset;
400
401 if (end_offset)
402 *end_offset = cse_bp->end_offset;
403
404}
405
406static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
407{
408 const struct cse_bp_entry *rw_bp;
409
410 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
411
412 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
413 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
414 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
415 return false;
416 }
417 return true;
418}
419
Rizwan Qureshiec321092019-09-06 20:28:43 +0530420static bool cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
421{
422 if (cse_get_current_bp(cse_bp_info) == RO)
423 return true;
424
425 return cse_set_and_boot_from_next_bp(RO);
426}
427
428static bool cse_get_rw_rdev(struct region_device *rdev)
429{
430 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
431 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530432 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530433 return false;
434 }
435
436 return true;
437}
438
Rizwan Qureshiec321092019-09-06 20:28:43 +0530439static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
440{
441 uint32_t cse_bp_sign;
442
443 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
444 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
445 return false;
446 }
447
448 return cse_bp_sign == CSE_RW_SIGNATURE;
449}
450
451static bool cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
452 struct region_device *target_rdev)
453{
454 struct region_device cse_region_rdev;
455 size_t size;
456 uint32_t start_offset;
457 uint32_t end_offset;
458
459 if (!cse_get_rw_rdev(&cse_region_rdev))
460 return false;
461
462 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
463 size = end_offset + 1 - start_offset;
464
465 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
466 return false;
467
468 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
469 (uint32_t)start_offset, (uint32_t) size);
470
471 return true;
472}
473
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530474static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530475{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530476 struct vb2_context *ctx = vboot_get_context();
477 if (ctx == NULL)
478 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530479
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530480 if (vboot_is_firmware_slot_a(ctx))
481 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
482
483 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
484}
485
Rizwan Qureshiec321092019-09-06 20:28:43 +0530486/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530487 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
488 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530489 * If ver_cmp_status = 0, no update is required
490 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
491 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
492 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530493static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530494{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700495 if (a->major != b->major)
496 return a->major - b->major;
497 else if (a->minor != b->minor)
498 return a->minor - b->minor;
499 else if (a->hotfix != b->hotfix)
500 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530501 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700502 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530503}
504
505/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
506static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
507 const void *rw_blob, const size_t rw_blob_sz)
508
509{
510 uint8_t rw_comp_sha[VB2_SHA256_DIGEST_SIZE];
511
512 if (vb2_digest_buffer(rw_blob, rw_blob_sz, VB2_HASH_SHA256, rw_comp_sha,
513 VB2_SHA256_DIGEST_SIZE)) {
514 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
515 return false;
516 }
517
518 if (memcmp(expected_rw_blob_sha, rw_comp_sha, VB2_SHA256_DIGEST_SIZE)) {
519 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
520 "the provided SHA in the metadata\n");
521 return false;
522 }
523 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
524 " provided hash in the metadata\n");
525 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530526}
527
528static bool cse_erase_rw_region(const struct region_device *target_rdev)
529{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530530 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
531 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
532 return false;
533 }
534 return true;
535}
536
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530537static bool cse_copy_rw(const struct region_device *target_rdev, const void *buf,
538 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530539{
540 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
541 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
542 return false;
543 }
544
545 return true;
546}
547
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700548enum cse_update_status {
549 CSE_UPDATE_NOT_REQUIRED,
550 CSE_UPDATE_UPGRADE,
551 CSE_UPDATE_DOWNGRADE,
552 CSE_UPDATE_CORRUPTED,
553 CSE_UPDATE_METADATA_ERROR,
554};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530555
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700556static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
557{
558 if ((*curr - start) >= size) {
559 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
560 return false;
561 }
562
563 *ver_field = skip_atoi(curr);
564 (*curr)++;
565 return true;
566}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530567
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700568static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
569 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530570{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700571 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700572 struct fw_version cbfs_rw_version;
573 char *version_str, *ptr;
574 size_t size;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700575
576 if (!cse_is_rw_bp_sign_valid(target_rdev))
577 return CSE_UPDATE_CORRUPTED;
578
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700579 ptr = version_str = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
580 if (!version_str) {
581 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
582 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
583 return CSE_UPDATE_METADATA_ERROR;
584 }
585
586 if (!read_ver_field(version_str, &ptr, size, &cbfs_rw_version.major) ||
587 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.minor) ||
588 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.hotfix) ||
589 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.build)) {
590 cbfs_unmap(version_str);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700591 return CSE_UPDATE_METADATA_ERROR;
592 }
593
594 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700595 cbfs_rw_version.major,
596 cbfs_rw_version.minor,
597 cbfs_rw_version.hotfix,
598 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700599
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700600 cbfs_unmap(version_str);
601
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530602 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700603 if (ret == 0)
604 return CSE_UPDATE_NOT_REQUIRED;
605 else if (ret < 0)
606 return CSE_UPDATE_DOWNGRADE;
607 else
608 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530609}
610
611static bool cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530612 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530613{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530614 /* Points to CSE CBFS RW image after boot partition signature */
615 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
616
617 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530618 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530619
620 /* Update except CSE RW signature */
621 if (!cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
622 cse_cbfs_rw_wo_sign_sz))
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530623 return false;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530624
625 /* Update CSE RW signature to indicate update is complete */
626 if (!cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE))
Rizwan Qureshiec321092019-09-06 20:28:43 +0530627 return false;
628
629 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
630 return true;
631}
632
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530633static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
634 const void *cse_cbfs_rw, const size_t cse_blob_sz,
635 struct region_device *target_rdev)
636{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530637 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530638 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
639 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530640 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
641 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530642
643 if (!cse_erase_rw_region(target_rdev))
644 return CSE_LITE_SKU_FW_UPDATE_ERROR;
645
646 if (!cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz))
647 return CSE_LITE_SKU_FW_UPDATE_ERROR;
648
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600649 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530650}
651
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530652static bool cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700653 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530654{
655 /*
656 * To set CSE's operation mode to HMRFPO mode:
657 * 1. Ensure CSE to boot from RO(BP1)
658 * 2. Send HMRFPO_ENABLE command to CSE
659 */
660 if (!cse_boot_to_ro(cse_bp_info))
661 return false;
662
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700663 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
664 if (!cse_data_clear_request(cse_bp_info)) {
665 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
666 return false;
667 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530668 }
669
Rizwan Qureshiec321092019-09-06 20:28:43 +0530670 return cse_hmrfpo_enable();
671}
672
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530673static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700674 enum cse_update_status status,
675 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530676{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530677 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700678 uint8_t *cbfs_rw_hash;
Julius Werner18881f992021-04-13 15:28:12 -0700679 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530680
Julius Werner18881f992021-04-13 15:28:12 -0700681 const char *area_name = cse_get_source_rdev_fmap();
682 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530683 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530684
Julius Werner18881f992021-04-13 15:28:12 -0700685 void *cse_cbfs_rw = cbfs_unverified_area_map(area_name,
686 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530687 if (!cse_cbfs_rw) {
688 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
689 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
690 }
691
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700692 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
693 if (!cbfs_rw_hash) {
694 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
695 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
696 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
697 goto error_exit;
698 }
699
Julius Werner18881f992021-04-13 15:28:12 -0700700 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530701 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
702 goto error_exit;
703 }
704
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700705 if (!cse_prep_for_rw_update(cse_bp_info, status)) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600706 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530707 goto error_exit;
708 }
709
Julius Werner18881f992021-04-13 15:28:12 -0700710 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530711
712error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700713 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700714 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530715 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530716}
717
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530718static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530719{
720 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700721 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530722
723 if (!cse_get_target_rdev(cse_bp_info, &target_rdev)) {
724 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
725 return CSE_LITE_SKU_RW_ACCESS_ERROR;
726 }
727
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700728 status = cse_check_update_status(cse_bp_info, &target_rdev);
729 if (status == CSE_UPDATE_NOT_REQUIRED)
730 return CSE_NO_ERROR;
731 if (status == CSE_UPDATE_METADATA_ERROR)
732 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530733
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700734 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
735 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530736}
737
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530738static const char *cse_sub_part_str(enum bpdt_entry_type type)
739{
740 switch (type) {
741 case IOM_FW:
742 return "IOM";
743 case NPHY_FW:
744 return "NPHY";
745 default:
746 return "Unknown";
747 }
748}
749
750static bool cse_sub_part_get_target_rdev(struct region_device *target_rdev,
751 const char *region_name, enum bpdt_entry_type type)
752{
753 struct bpdt_header bpdt_hdr;
754 struct region_device cse_rdev;
755 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
756 uint8_t i;
757
758 if (fmap_locate_area_as_rdev_rw(region_name, &cse_rdev) < 0) {
759 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the FMAP\n", region_name);
760 return false;
761 }
762
763 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
764 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
765 return false;
766 }
767
768 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
769 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
770 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
771 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
772 return false;
773 }
774
775 /* walk through BPDT entries to identify sub-partition's payload offset and size */
776 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
777 if (bpdt_entries[i].type == type) {
778 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
779 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
780 bpdt_entries[i].size);
781
782 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
783 bpdt_entries[i].size))
784 return false;
785 else
786 return true;
787 }
788 }
789
790 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
791 return false;
792}
793
794static bool cse_get_sub_part_fw_version(enum bpdt_entry_type type,
795 const struct region_device *rdev,
796 struct fw_version *fw_ver)
797{
798 struct subpart_entry subpart_entry;
799 struct subpart_entry_manifest_header man_hdr;
800
801 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
802 != SUBPART_ENTRY_SZ) {
803 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
804 cse_sub_part_str(type));
805 return false;
806 }
807
808 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
809 != SUBPART_MANIFEST_HDR_SZ) {
810 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
811 cse_sub_part_str(type));
812 return false;
813 }
814
815 fw_ver->major = man_hdr.binary_version.major;
816 fw_ver->minor = man_hdr.binary_version.minor;
817 fw_ver->hotfix = man_hdr.binary_version.hotfix;
818 fw_ver->build = man_hdr.binary_version.build;
819
820 return true;
821}
822
823static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
824{
825 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
826 struct subpart_entry *subpart_entry;
827 struct subpart_entry_manifest_header *man_hdr;
828
829 subpart_entry = (struct subpart_entry *) (ptr + SUBPART_HEADER_SZ);
830 man_hdr = (struct subpart_entry_manifest_header *) (ptr + subpart_entry->offset_bytes);
831
832 fw_ver->major = man_hdr->binary_version.major;
833 fw_ver->minor = man_hdr->binary_version.minor;
834 fw_ver->hotfix = man_hdr->binary_version.hotfix;
835 fw_ver->build = man_hdr->binary_version.build;
836}
837
838static bool cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
839{
840 /*
841 * To set CSE's operation mode to HMRFPO mode:
842 * 1. Ensure CSE to boot from RO(BP1)
843 * 2. Send HMRFPO_ENABLE command to CSE
844 */
845 if (!cse_boot_to_ro(cse_bp_info))
846 return false;
847
848 return cse_hmrfpo_enable();
849}
850
851static uint8_t cse_sub_part_trigger_update(enum bpdt_entry_type type, uint8_t bp,
852 const void *subpart_cbfs_rw, const size_t blob_sz,
853 struct region_device *target_rdev)
854{
855 if (region_device_sz(target_rdev) < blob_sz) {
856 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
857 "smaller than blob size:%zx, abort update\n",
858 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
859 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
860 }
861
862 /* Erase CSE Lite sub-partition */
863 if (!cse_erase_rw_region(target_rdev))
864 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
865
866 /* Update CSE Lite sub-partition */
867 if (!cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz))
868 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
869
870 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
871 cse_sub_part_str(type));
872
873 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
874}
875
876static uint8_t handle_cse_sub_part_fw_update_rv(uint8_t rv)
877{
878 switch (rv) {
879 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
880 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
881 return rv;
882 default:
883 cse_trigger_vboot_recovery(rv);
884 }
885 /* Control never reaches here */
886 return rv;
887}
888
889static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
890 const struct cse_bp_info *cse_bp_info, const char *name)
891{
892 struct region_device target_rdev;
893 struct fw_version target_fw_ver, source_fw_ver;
894 enum csme_failure_reason rv;
895 size_t size;
896 static const char * const cse_regions[] = {"CSE_RO", "CSE_RW"};
897
898 void *subpart_cbfs_rw = cbfs_map(name, &size);
899 if (!subpart_cbfs_rw) {
900 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
901 cse_sub_part_str(type));
902 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
903 }
904
905 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
906 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
907 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
908 source_fw_ver.build);
909
910 /* Trigger sub-partition update in CSE RO and CSE RW */
911 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
912 if (!cse_sub_part_get_target_rdev(&target_rdev, cse_regions[bp], type)) {
913 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
914 goto error_exit;
915 }
916
917 if (!cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver)) {
918 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
919 goto error_exit;
920 }
921
922 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
923 cse_sub_part_str(type), target_fw_ver.major,
924 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
925
926 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
927 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
928 cse_regions[bp], cse_sub_part_str(type));
929 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
930 continue;
931 }
932
933 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
934 cse_sub_part_str(type));
935
936 if (!cse_prep_for_component_update(cse_bp_info)) {
937 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
938 goto error_exit;
939 }
940
941 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
942 size, &target_rdev);
943
944 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
945 goto error_exit;
946 }
947error_exit:
948 cbfs_unmap(subpart_cbfs_rw);
949 return rv;
950}
951
952static uint8_t cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info)
953{
954 if (skip_cse_sub_part_update()) {
955 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
956 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
957 }
958
959 int rv;
960 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
961 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
962
963 handle_cse_sub_part_fw_update_rv(rv);
964
965 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
966 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
967
968 return handle_cse_sub_part_fw_update_rv(rv);
969}
970
Sridhar Siricilla1a2b7022020-12-04 02:22:28 +0530971void cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530972{
973 static struct get_bp_info_rsp cse_bp_info;
974
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530975 /*
976 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
977 * is not enabled and continue to update CSE sub-partitions.
978 */
979 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530980 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530981 return;
982 }
983
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530984 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
985 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530986 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530987 return;
988 }
989
990 if (!cse_get_bp_info(&cse_bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530991 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530992
993 /* If system is in recovery mode, don't trigger recovery again */
994 if (!vboot_recovery_mode_enabled()) {
995 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
996 } else {
997 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
998 "so no action\n");
999 return;
1000 }
1001 }
1002
1003 /*
1004 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
1005 * sub-partitions like NPHY and IOM have to to be updated. If CSE sub-parition update
1006 * fails during recovery, just continue to boot.
1007 */
1008 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1009 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1010 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1011 cse_board_reset();
1012 do_global_reset();
1013 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1014 }
1015
1016 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301017 }
1018
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301019 if (!cse_fix_data_failure_err(&cse_bp_info.bp_info))
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001020 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301021
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301022 /*
1023 * If SOC_INTEL_CSE_RW_UPDATE is defined , then trigger CSE firmware update. The driver
1024 * triggers recovery if CSE CBFS RW metadata or CSE CBFS RW blob is not available.
1025 */
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301026 if (CONFIG(SOC_INTEL_CSE_RW_UPDATE)) {
1027 uint8_t rv;
1028 rv = cse_fw_update(&cse_bp_info.bp_info);
1029 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001030 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301031 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301032
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301033 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1034 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1035
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301036 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001037 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301038
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301039 if (!cse_boot_to_rw(&cse_bp_info.bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301040 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001041 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301042 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301043}