blob: b381c48103144a085059fa1ee38201e48a6b9cda [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>
5#include <commonlib/cbfs.h>
6#include <commonlib/region.h>
7#include <fmap.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05308#include <intelblocks/cse.h>
9#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>
12
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053013/* Converts bp index to boot partition string */
14#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
15
Rizwan Qureshiec321092019-09-06 20:28:43 +053016/* CSE RW boot partition signature */
17#define CSE_RW_SIGNATURE 0x000055aa
18
19/* CSE RW boot partition signature size */
20#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
21
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053022/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053023 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053024 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053025 * CSE Lite SKU Image Layout:
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053026 * ------------- ------------------- ---------------------
27 * |CSE REGION | => | RO | RW | DATA | => | BP1 | BP2 | DATA |
28 * ------------- ------------------- ---------------------
29 */
30#define CSE_MAX_BOOT_PARTITIONS 3
31
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053032/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053033enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053034 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053035 RO = 0,
36
Rizwan Qureshiec321092019-09-06 20:28:43 +053037 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053038 RW = 1
39};
40
41/*
42 * Boot partition status.
43 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
44 */
45enum bp_status {
46 /* This value is returned when a partition has no errors */
47 BP_STATUS_SUCCESS = 0,
48
49 /*
50 * This value is returned when a partition should be present based on layout, but it is
51 * not valid.
52 */
53 BP_STATUS_GENERAL_FAILURE = 1,
54
55 /* This value is returned when a partition is not present per initial image layout */
56 BP_STATUS_PARTITION_NOT_PRESENT = 2,
57
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053058 /*
59 * This value is returned when unexpected issues are detected in CSE Data area
60 * and CSE TCB-SVN downgrade scenario.
61 */
62 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053063};
64
65/*
66 * Boot Partition Info Flags
67 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
68 */
69enum bp_info_flags {
70
71 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
72 BP_INFO_REDUNDANCY_EN = 1 << 0,
73
74 /* It indicates RO(BP1) supports Minimal Recovery Mode */
75 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
76
77 /*
78 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
79 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
80 */
81 BP_INFO_READ_ONLY_CFG = 1 << 2,
82};
83
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053084/* CSE boot partition entry info */
85struct cse_bp_entry {
86 /* Boot partition version */
87 struct fw_version fw_ver;
88
89 /* Boot partition status */
90 uint32_t status;
91
92 /* Starting offset of the partition within CSE region */
93 uint32_t start_offset;
94
95 /* Ending offset of the partition within CSE region */
96 uint32_t end_offset;
97 uint8_t reserved[12];
98} __packed;
99
100/* CSE boot partition info */
101struct cse_bp_info {
102 /* Number of boot partitions */
103 uint8_t total_number_of_bp;
104
105 /* Current boot partition */
106 uint8_t current_bp;
107
108 /* Next boot partition */
109 uint8_t next_bp;
110
111 /* Boot Partition Info Flags */
112 uint8_t flags;
113
114 /* Boot Partition Entry Info */
115 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
116} __packed;
117
118struct get_bp_info_rsp {
119 struct mkhi_hdr hdr;
120 struct cse_bp_info bp_info;
121} __packed;
122
123static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
124{
125 return cse_bp_info->current_bp;
126}
127
128static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
129 const struct cse_bp_info *cse_bp_info)
130{
131 return &cse_bp_info->bp_entries[bp];
132}
133
134static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
135{
136 const struct cse_bp_entry *cse_bp;
137
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530138 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
139 cse_bp_info->total_number_of_bp);
140 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
141 GET_BP_STR(cse_bp_info->current_bp));
142 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
143 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530144
145 /* Log version info of RO & RW partitions */
146 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530147 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 +0530148 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
149 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
150 cse_bp->status, cse_bp->start_offset,
151 cse_bp->end_offset);
152
153 cse_bp = cse_get_bp_entry(RW, 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(RW), 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
161/*
162 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
163 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
164 * It allows execution of the Boot Partition commands in below scenarios:
165 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
166 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
167 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530168 * The prerequisite check should be handled in cse_get_bp_info() and
169 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
170 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530171 */
172static bool cse_is_bp_cmd_info_possible(void)
173{
174 if (cse_is_hfs1_cws_normal()) {
175 if (cse_is_hfs1_com_normal())
176 return true;
177 if (cse_is_hfs1_com_secover_mei_msg())
178 return true;
179 if (cse_is_hfs1_com_soft_temp_disable())
180 return true;
181 }
182 return false;
183}
184
185static bool cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
186{
187 struct get_bp_info_req {
188 struct mkhi_hdr hdr;
189 uint8_t reserved[4];
190 } __packed;
191
192 struct get_bp_info_req info_req = {
193 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
194 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
195 .reserved = {0},
196 };
197
198 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530199 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530200 return false;
201 }
202
203 size_t resp_size = sizeof(struct get_bp_info_rsp);
204
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530205 if (!heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
206 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530207 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530208 return false;
209 }
210
211 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530212 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530213 bp_info_rsp->hdr.result);
214 return false;
215 }
216
217 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
218
219 return true;
220}
221/*
222 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
223 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
224 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
225 * This function must be used before EOP.
226 * Returns false on failure and true on success.
227 */
228static bool cse_set_next_boot_partition(enum boot_partition_id bp)
229{
230 struct set_boot_partition_info_req {
231 struct mkhi_hdr hdr;
232 uint8_t next_bp;
233 uint8_t reserved[3];
234 } __packed;
235
236 struct set_boot_partition_info_req switch_req = {
237 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
238 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
239 .next_bp = bp,
240 .reserved = {0},
241 };
242
243 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530244 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530245 return false;
246 }
247
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530248 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530249
250 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530251 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530252 return false;
253 }
254
255 struct mkhi_hdr switch_resp;
256 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
257
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530258 if (!heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
259 HECI_MKHI_ADDR))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530260 return false;
261
262 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530263 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530264 switch_resp.result);
265 return false;
266 }
267
268 return true;
269}
270
V Sowmyaf9905522020-11-12 20:19:04 +0530271static bool cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
272{
273 struct data_clr_request {
274 struct mkhi_hdr hdr;
275 uint8_t reserved[4];
276 } __packed;
277
278 struct data_clr_request data_clr_rq = {
279 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
280 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
281 .reserved = {0},
282 };
283
284 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
285 cse_get_current_bp(cse_bp_info) != RO) {
286 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
287 return false;
288 }
289
290 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
291
292 struct mkhi_hdr data_clr_rsp;
293 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
294
295 if (!heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530296 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
V Sowmyaf9905522020-11-12 20:19:04 +0530297 return false;
298 }
299
300 if (data_clr_rsp.result) {
301 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
302 data_clr_rsp.result);
303 return false;
304 }
305
306 return true;
307}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530308
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600309__weak void cse_board_reset(void)
310{
311 /* Default weak implementation, does nothing. */
312}
313
Rizwan Qureshiec321092019-09-06 20:28:43 +0530314/* Set the CSE's next boot partition and issues system reset */
315static bool cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530316{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530317 if (!cse_set_next_boot_partition(bp))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530318 return false;
319
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600320 /* Allow the board to perform a reset for CSE RO<->RW jump */
321 cse_board_reset();
322
323 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000324 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530325
Rizwan Qureshiec321092019-09-06 20:28:43 +0530326 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530327
328 /* Control never reaches here */
329 return false;
330}
331
Rizwan Qureshiec321092019-09-06 20:28:43 +0530332static bool cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530333{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530334 if (cse_get_current_bp(cse_bp_info) == RW)
335 return true;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530336
Rizwan Qureshiec321092019-09-06 20:28:43 +0530337 return cse_set_and_boot_from_next_bp(RW);
338}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530339
V Sowmyaf9905522020-11-12 20:19:04 +0530340/* Check if CSE RW data partition is valid or not */
341static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
342{
343 const struct cse_bp_entry *rw_bp;
344
345 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
346 return rw_bp->status != BP_STATUS_DATA_FAILURE;
347}
348
349/*
350 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
351 * otherwise false if any operation fails.
352 */
353static bool cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
354{
355 /*
356 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
357 * - Send DATA CLEAR HECI command to CSE
358 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
359 * - Issue GLOBAL RESET HECI command.
360 */
361 if (cse_is_rw_dp_valid(cse_bp_info))
362 return true;
363
364 if (!cse_data_clear_request(cse_bp_info))
365 return false;
366
367 return cse_boot_to_rw(cse_bp_info);
368}
369
V Sowmyaf9905522020-11-12 20:19:04 +0530370static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530371 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530372{
373 const struct cse_bp_entry *cse_bp;
374
375 cse_bp = cse_get_bp_entry(bp, bp_info);
376 return &cse_bp->fw_ver;
377}
378
379static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
380{
381 return cse_get_bp_entry_version(RW, cse_bp_info);
382}
383
384static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530385 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530386{
387 const struct cse_bp_entry *cse_bp;
388
389 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
390
391 if (start_offset)
392 *start_offset = cse_bp->start_offset;
393
394 if (end_offset)
395 *end_offset = cse_bp->end_offset;
396
397}
398
399static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
400{
401 const struct cse_bp_entry *rw_bp;
402
403 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
404
405 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
406 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
407 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
408 return false;
409 }
410 return true;
411}
412
Rizwan Qureshiec321092019-09-06 20:28:43 +0530413static bool cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
414{
415 if (cse_get_current_bp(cse_bp_info) == RO)
416 return true;
417
418 return cse_set_and_boot_from_next_bp(RO);
419}
420
421static bool cse_get_rw_rdev(struct region_device *rdev)
422{
423 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
424 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530425 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530426 return false;
427 }
428
429 return true;
430}
431
Rizwan Qureshiec321092019-09-06 20:28:43 +0530432static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
433{
434 uint32_t cse_bp_sign;
435
436 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
437 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
438 return false;
439 }
440
441 return cse_bp_sign == CSE_RW_SIGNATURE;
442}
443
444static bool cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
445 struct region_device *target_rdev)
446{
447 struct region_device cse_region_rdev;
448 size_t size;
449 uint32_t start_offset;
450 uint32_t end_offset;
451
452 if (!cse_get_rw_rdev(&cse_region_rdev))
453 return false;
454
455 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
456 size = end_offset + 1 - start_offset;
457
458 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
459 return false;
460
461 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
462 (uint32_t)start_offset, (uint32_t) size);
463
464 return true;
465}
466
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530467static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530468{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530469 struct vb2_context *ctx = vboot_get_context();
470 if (ctx == NULL)
471 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530472
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530473 if (vboot_is_firmware_slot_a(ctx))
474 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
475
476 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
477}
478
479static bool cse_get_source_rdev(struct region_device *rdev)
480{
481 const char *reg_name;
482 uint32_t cbfs_type = CBFS_TYPE_RAW;
483 struct cbfsf fh;
484
485 reg_name = cse_get_source_rdev_fmap();
486
487 if (reg_name == NULL)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530488 return false;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530489
490 if (cbfs_locate_file_in_region(&fh, reg_name, CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME,
491 &cbfs_type) < 0)
492 return false;
493
494 cbfs_file_data(rdev, &fh);
495
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530496 return true;
497}
498
Rizwan Qureshiec321092019-09-06 20:28:43 +0530499/*
500 * Compare versions of CSE CBFS RW and CSE RW partition
501 * If ver_cmp_status = 0, no update is required
502 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
503 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
504 */
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700505static int compare_cse_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530506{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700507 if (a->major != b->major)
508 return a->major - b->major;
509 else if (a->minor != b->minor)
510 return a->minor - b->minor;
511 else if (a->hotfix != b->hotfix)
512 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530513 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700514 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530515}
516
517/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
518static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
519 const void *rw_blob, const size_t rw_blob_sz)
520
521{
522 uint8_t rw_comp_sha[VB2_SHA256_DIGEST_SIZE];
523
524 if (vb2_digest_buffer(rw_blob, rw_blob_sz, VB2_HASH_SHA256, rw_comp_sha,
525 VB2_SHA256_DIGEST_SIZE)) {
526 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
527 return false;
528 }
529
530 if (memcmp(expected_rw_blob_sha, rw_comp_sha, VB2_SHA256_DIGEST_SIZE)) {
531 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
532 "the provided SHA in the metadata\n");
533 return false;
534 }
535 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
536 " provided hash in the metadata\n");
537 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530538}
539
540static bool cse_erase_rw_region(const struct region_device *target_rdev)
541{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530542 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
543 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
544 return false;
545 }
546 return true;
547}
548
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530549static bool cse_copy_rw(const struct region_device *target_rdev, const void *buf,
550 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530551{
552 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
553 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
554 return false;
555 }
556
557 return true;
558}
559
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700560enum cse_update_status {
561 CSE_UPDATE_NOT_REQUIRED,
562 CSE_UPDATE_UPGRADE,
563 CSE_UPDATE_DOWNGRADE,
564 CSE_UPDATE_CORRUPTED,
565 CSE_UPDATE_METADATA_ERROR,
566};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530567
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700568static struct cse_rw_metadata source_metadata;
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530569
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700570static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
571 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530572{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700573 int ret;
574
575 if (!cse_is_rw_bp_sign_valid(target_rdev))
576 return CSE_UPDATE_CORRUPTED;
577
578 if (cbfs_load(CONFIG_SOC_INTEL_CSE_RW_METADATA_CBFS_NAME, &source_metadata,
579 sizeof(source_metadata)) != sizeof(source_metadata)) {
580 printk(BIOS_ERR, "cse_lite: Failed to get CSE CBFS RW metadata\n");
581 return CSE_UPDATE_METADATA_ERROR;
582 }
583
584 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
585 source_metadata.version.major,
586 source_metadata.version.minor,
587 source_metadata.version.hotfix,
588 source_metadata.version.build);
589
590 ret = compare_cse_version(&source_metadata.version, cse_get_rw_version(cse_bp_info));
591 if (ret == 0)
592 return CSE_UPDATE_NOT_REQUIRED;
593 else if (ret < 0)
594 return CSE_UPDATE_DOWNGRADE;
595 else
596 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530597}
598
599static bool cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530600 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530601{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530602 /* Points to CSE CBFS RW image after boot partition signature */
603 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
604
605 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530606 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530607
608 /* Update except CSE RW signature */
609 if (!cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
610 cse_cbfs_rw_wo_sign_sz))
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530611 return false;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530612
613 /* Update CSE RW signature to indicate update is complete */
614 if (!cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE))
Rizwan Qureshiec321092019-09-06 20:28:43 +0530615 return false;
616
617 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
618 return true;
619}
620
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530621static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
622 const void *cse_cbfs_rw, const size_t cse_blob_sz,
623 struct region_device *target_rdev)
624{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530625 if (region_device_sz(target_rdev) < cse_blob_sz) {
626 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx, Update blob size:%zx\n",
627 region_device_sz(target_rdev), cse_blob_sz);
628 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
629 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530630
631 if (!cse_erase_rw_region(target_rdev))
632 return CSE_LITE_SKU_FW_UPDATE_ERROR;
633
634 if (!cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz))
635 return CSE_LITE_SKU_FW_UPDATE_ERROR;
636
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600637 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530638}
639
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530640static bool cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700641 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530642{
643 /*
644 * To set CSE's operation mode to HMRFPO mode:
645 * 1. Ensure CSE to boot from RO(BP1)
646 * 2. Send HMRFPO_ENABLE command to CSE
647 */
648 if (!cse_boot_to_ro(cse_bp_info))
649 return false;
650
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700651 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
652 if (!cse_data_clear_request(cse_bp_info)) {
653 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
654 return false;
655 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530656 }
657
Rizwan Qureshiec321092019-09-06 20:28:43 +0530658 return cse_hmrfpo_enable();
659}
660
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530661static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700662 enum cse_update_status status,
663 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530664{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530665 struct region_device source_rdev;
666 enum csme_failure_reason rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530667
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530668 if (!cse_get_source_rdev(&source_rdev))
669 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530670
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530671 void *cse_cbfs_rw = rdev_mmap_full(&source_rdev);
672
673 if (!cse_cbfs_rw) {
674 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
675 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
676 }
677
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700678 if (!cse_verify_cbfs_rw_sha256(source_metadata.sha256, cse_cbfs_rw,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530679 region_device_sz(&source_rdev))) {
680 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
681 goto error_exit;
682 }
683
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700684 if (!cse_prep_for_rw_update(cse_bp_info, status)) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600685 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530686 goto error_exit;
687 }
688
689 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, region_device_sz(&source_rdev),
690 target_rdev);
691
692error_exit:
693 rdev_munmap(&source_rdev, cse_cbfs_rw);
694 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530695}
696
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530697static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530698{
699 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700700 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530701
702 if (!cse_get_target_rdev(cse_bp_info, &target_rdev)) {
703 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
704 return CSE_LITE_SKU_RW_ACCESS_ERROR;
705 }
706
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700707 status = cse_check_update_status(cse_bp_info, &target_rdev);
708 if (status == CSE_UPDATE_NOT_REQUIRED)
709 return CSE_NO_ERROR;
710 if (status == CSE_UPDATE_METADATA_ERROR)
711 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530712
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700713 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
714 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530715}
716
Sridhar Siricilla1a2b7022020-12-04 02:22:28 +0530717void cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530718{
719 static struct get_bp_info_rsp cse_bp_info;
720
721 if (vboot_recovery_mode_enabled()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530722 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530723 return;
724 }
725
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530726 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
727 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530728 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530729 return;
730 }
731
732 if (!cse_get_bp_info(&cse_bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530733 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600734 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530735 }
736
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530737 if (!cse_fix_data_failure_err(&cse_bp_info.bp_info))
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600738 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530739
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530740 /*
741 * If SOC_INTEL_CSE_RW_UPDATE is defined , then trigger CSE firmware update. The driver
742 * triggers recovery if CSE CBFS RW metadata or CSE CBFS RW blob is not available.
743 */
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +0530744 if (CONFIG(SOC_INTEL_CSE_RW_UPDATE)) {
745 uint8_t rv;
746 rv = cse_fw_update(&cse_bp_info.bp_info);
747 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600748 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +0530749 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530750
Sridahr Siricilla54b03562021-06-18 10:59:30 +0530751 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -0600752 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +0530753
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530754 if (!cse_boot_to_rw(&cse_bp_info.bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530755 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600756 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530757 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530758}