blob: 2756814fdba9407ae0c3882ee2ad7470753d4872 [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
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700130
131bool cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
132{
133 struct cse_boot_perf_req {
134 struct mkhi_hdr hdr;
135 uint32_t reserved;
136 } __packed;
137
138 struct cse_boot_perf_req req = {
139 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
140 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
141 .reserved = 0,
142 };
143
144 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
145
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530146 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700147 HECI_MKHI_ADDR)) {
148 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
149 return false;
150 }
151
152 if (boot_perf_rsp->hdr.result) {
153 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
154 boot_perf_rsp->hdr.result);
155 return false;
156 }
157
158 return true;
159}
160
161
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530162static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
163{
164 return cse_bp_info->current_bp;
165}
166
167static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
168 const struct cse_bp_info *cse_bp_info)
169{
170 return &cse_bp_info->bp_entries[bp];
171}
172
173static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
174{
175 const struct cse_bp_entry *cse_bp;
176
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530177 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
178 cse_bp_info->total_number_of_bp);
179 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
180 GET_BP_STR(cse_bp_info->current_bp));
181 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
182 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530183
184 /* Log version info of RO & RW partitions */
185 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530186 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 +0530187 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
188 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
189 cse_bp->status, cse_bp->start_offset,
190 cse_bp->end_offset);
191
192 cse_bp = cse_get_bp_entry(RW, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530193 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 +0530194 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
195 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
196 cse_bp->status, cse_bp->start_offset,
197 cse_bp->end_offset);
198}
199
200/*
201 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
202 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
203 * It allows execution of the Boot Partition commands in below scenarios:
204 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
205 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
206 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530207 * The prerequisite check should be handled in cse_get_bp_info() and
208 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
209 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530210 */
211static bool cse_is_bp_cmd_info_possible(void)
212{
213 if (cse_is_hfs1_cws_normal()) {
214 if (cse_is_hfs1_com_normal())
215 return true;
216 if (cse_is_hfs1_com_secover_mei_msg())
217 return true;
218 if (cse_is_hfs1_com_soft_temp_disable())
219 return true;
220 }
221 return false;
222}
223
224static bool cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
225{
226 struct get_bp_info_req {
227 struct mkhi_hdr hdr;
228 uint8_t reserved[4];
229 } __packed;
230
231 struct get_bp_info_req info_req = {
232 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
233 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
234 .reserved = {0},
235 };
236
237 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530238 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530239 return false;
240 }
241
242 size_t resp_size = sizeof(struct get_bp_info_rsp);
243
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530244 if (heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530245 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530246 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530247 return false;
248 }
249
250 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530251 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530252 bp_info_rsp->hdr.result);
253 return false;
254 }
255
256 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
257
258 return true;
259}
260/*
261 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
262 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
263 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
264 * This function must be used before EOP.
265 * Returns false on failure and true on success.
266 */
267static bool cse_set_next_boot_partition(enum boot_partition_id bp)
268{
269 struct set_boot_partition_info_req {
270 struct mkhi_hdr hdr;
271 uint8_t next_bp;
272 uint8_t reserved[3];
273 } __packed;
274
275 struct set_boot_partition_info_req switch_req = {
276 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
277 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
278 .next_bp = bp,
279 .reserved = {0},
280 };
281
282 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530283 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530284 return false;
285 }
286
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530287 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530288
289 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530290 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530291 return false;
292 }
293
294 struct mkhi_hdr switch_resp;
295 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
296
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530297 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530298 HECI_MKHI_ADDR))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530299 return false;
300
301 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530302 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530303 switch_resp.result);
304 return false;
305 }
306
307 return true;
308}
309
V Sowmyaf9905522020-11-12 20:19:04 +0530310static bool cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
311{
312 struct data_clr_request {
313 struct mkhi_hdr hdr;
314 uint8_t reserved[4];
315 } __packed;
316
317 struct data_clr_request data_clr_rq = {
318 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
319 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
320 .reserved = {0},
321 };
322
323 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
324 cse_get_current_bp(cse_bp_info) != RO) {
325 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
326 return false;
327 }
328
329 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
330
331 struct mkhi_hdr data_clr_rsp;
332 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
333
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530334 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530335 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
V Sowmyaf9905522020-11-12 20:19:04 +0530336 return false;
337 }
338
339 if (data_clr_rsp.result) {
340 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
341 data_clr_rsp.result);
342 return false;
343 }
344
345 return true;
346}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530347
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600348__weak void cse_board_reset(void)
349{
350 /* Default weak implementation, does nothing. */
351}
352
Rizwan Qureshiec321092019-09-06 20:28:43 +0530353/* Set the CSE's next boot partition and issues system reset */
354static bool cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530355{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530356 if (!cse_set_next_boot_partition(bp))
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530357 return false;
358
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600359 /* Allow the board to perform a reset for CSE RO<->RW jump */
360 cse_board_reset();
361
362 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000363 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530364
Rizwan Qureshiec321092019-09-06 20:28:43 +0530365 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530366
367 /* Control never reaches here */
368 return false;
369}
370
Rizwan Qureshiec321092019-09-06 20:28:43 +0530371static bool cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530372{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530373 if (cse_get_current_bp(cse_bp_info) == RW)
374 return true;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530375
Rizwan Qureshiec321092019-09-06 20:28:43 +0530376 return cse_set_and_boot_from_next_bp(RW);
377}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530378
V Sowmyaf9905522020-11-12 20:19:04 +0530379/* Check if CSE RW data partition is valid or not */
380static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
381{
382 const struct cse_bp_entry *rw_bp;
383
384 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
385 return rw_bp->status != BP_STATUS_DATA_FAILURE;
386}
387
388/*
389 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
390 * otherwise false if any operation fails.
391 */
392static bool cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
393{
394 /*
395 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
396 * - Send DATA CLEAR HECI command to CSE
397 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
398 * - Issue GLOBAL RESET HECI command.
399 */
400 if (cse_is_rw_dp_valid(cse_bp_info))
401 return true;
402
403 if (!cse_data_clear_request(cse_bp_info))
404 return false;
405
406 return cse_boot_to_rw(cse_bp_info);
407}
408
V Sowmyaf9905522020-11-12 20:19:04 +0530409static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530410 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530411{
412 const struct cse_bp_entry *cse_bp;
413
414 cse_bp = cse_get_bp_entry(bp, bp_info);
415 return &cse_bp->fw_ver;
416}
417
418static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
419{
420 return cse_get_bp_entry_version(RW, cse_bp_info);
421}
422
423static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530424 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530425{
426 const struct cse_bp_entry *cse_bp;
427
428 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
429
430 if (start_offset)
431 *start_offset = cse_bp->start_offset;
432
433 if (end_offset)
434 *end_offset = cse_bp->end_offset;
435
436}
437
438static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
439{
440 const struct cse_bp_entry *rw_bp;
441
442 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
443
444 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
445 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
446 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
447 return false;
448 }
449 return true;
450}
451
Rizwan Qureshiec321092019-09-06 20:28:43 +0530452static bool cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
453{
454 if (cse_get_current_bp(cse_bp_info) == RO)
455 return true;
456
457 return cse_set_and_boot_from_next_bp(RO);
458}
459
460static bool cse_get_rw_rdev(struct region_device *rdev)
461{
462 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
463 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530464 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530465 return false;
466 }
467
468 return true;
469}
470
Rizwan Qureshiec321092019-09-06 20:28:43 +0530471static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
472{
473 uint32_t cse_bp_sign;
474
475 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
476 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
477 return false;
478 }
479
480 return cse_bp_sign == CSE_RW_SIGNATURE;
481}
482
483static bool cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
484 struct region_device *target_rdev)
485{
486 struct region_device cse_region_rdev;
487 size_t size;
488 uint32_t start_offset;
489 uint32_t end_offset;
490
491 if (!cse_get_rw_rdev(&cse_region_rdev))
492 return false;
493
494 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
495 size = end_offset + 1 - start_offset;
496
497 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
498 return false;
499
500 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
501 (uint32_t)start_offset, (uint32_t) size);
502
503 return true;
504}
505
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530506static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530507{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530508 struct vb2_context *ctx = vboot_get_context();
509 if (ctx == NULL)
510 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530511
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530512 if (vboot_is_firmware_slot_a(ctx))
513 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
514
515 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
516}
517
Rizwan Qureshiec321092019-09-06 20:28:43 +0530518/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530519 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
520 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530521 * If ver_cmp_status = 0, no update is required
522 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
523 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
524 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530525static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530526{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700527 if (a->major != b->major)
528 return a->major - b->major;
529 else if (a->minor != b->minor)
530 return a->minor - b->minor;
531 else if (a->hotfix != b->hotfix)
532 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530533 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700534 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530535}
536
537/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
538static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
539 const void *rw_blob, const size_t rw_blob_sz)
540
541{
542 uint8_t rw_comp_sha[VB2_SHA256_DIGEST_SIZE];
543
544 if (vb2_digest_buffer(rw_blob, rw_blob_sz, VB2_HASH_SHA256, rw_comp_sha,
545 VB2_SHA256_DIGEST_SIZE)) {
546 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
547 return false;
548 }
549
550 if (memcmp(expected_rw_blob_sha, rw_comp_sha, VB2_SHA256_DIGEST_SIZE)) {
551 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
552 "the provided SHA in the metadata\n");
553 return false;
554 }
555 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
556 " provided hash in the metadata\n");
557 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530558}
559
560static bool cse_erase_rw_region(const struct region_device *target_rdev)
561{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530562 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
563 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
564 return false;
565 }
566 return true;
567}
568
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530569static bool cse_copy_rw(const struct region_device *target_rdev, const void *buf,
570 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530571{
572 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
573 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
574 return false;
575 }
576
577 return true;
578}
579
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700580enum cse_update_status {
581 CSE_UPDATE_NOT_REQUIRED,
582 CSE_UPDATE_UPGRADE,
583 CSE_UPDATE_DOWNGRADE,
584 CSE_UPDATE_CORRUPTED,
585 CSE_UPDATE_METADATA_ERROR,
586};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530587
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700588static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
589{
590 if ((*curr - start) >= size) {
591 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
592 return false;
593 }
594
595 *ver_field = skip_atoi(curr);
596 (*curr)++;
597 return true;
598}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530599
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700600static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
601 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530602{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700603 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700604 struct fw_version cbfs_rw_version;
605 char *version_str, *ptr;
606 size_t size;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700607
608 if (!cse_is_rw_bp_sign_valid(target_rdev))
609 return CSE_UPDATE_CORRUPTED;
610
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700611 ptr = version_str = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
612 if (!version_str) {
613 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
614 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
615 return CSE_UPDATE_METADATA_ERROR;
616 }
617
618 if (!read_ver_field(version_str, &ptr, size, &cbfs_rw_version.major) ||
619 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.minor) ||
620 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.hotfix) ||
621 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.build)) {
622 cbfs_unmap(version_str);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700623 return CSE_UPDATE_METADATA_ERROR;
624 }
625
626 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700627 cbfs_rw_version.major,
628 cbfs_rw_version.minor,
629 cbfs_rw_version.hotfix,
630 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700631
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700632 cbfs_unmap(version_str);
633
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530634 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700635 if (ret == 0)
636 return CSE_UPDATE_NOT_REQUIRED;
637 else if (ret < 0)
638 return CSE_UPDATE_DOWNGRADE;
639 else
640 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530641}
642
643static bool cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530644 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530645{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530646 /* Points to CSE CBFS RW image after boot partition signature */
647 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
648
649 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530650 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530651
652 /* Update except CSE RW signature */
653 if (!cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
654 cse_cbfs_rw_wo_sign_sz))
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530655 return false;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530656
657 /* Update CSE RW signature to indicate update is complete */
658 if (!cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE))
Rizwan Qureshiec321092019-09-06 20:28:43 +0530659 return false;
660
661 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
662 return true;
663}
664
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530665static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
666 const void *cse_cbfs_rw, const size_t cse_blob_sz,
667 struct region_device *target_rdev)
668{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530669 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530670 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
671 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530672 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
673 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530674
675 if (!cse_erase_rw_region(target_rdev))
676 return CSE_LITE_SKU_FW_UPDATE_ERROR;
677
678 if (!cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz))
679 return CSE_LITE_SKU_FW_UPDATE_ERROR;
680
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600681 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530682}
683
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530684static bool cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700685 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530686{
687 /*
688 * To set CSE's operation mode to HMRFPO mode:
689 * 1. Ensure CSE to boot from RO(BP1)
690 * 2. Send HMRFPO_ENABLE command to CSE
691 */
692 if (!cse_boot_to_ro(cse_bp_info))
693 return false;
694
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700695 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
696 if (!cse_data_clear_request(cse_bp_info)) {
697 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
698 return false;
699 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530700 }
701
Rizwan Qureshiec321092019-09-06 20:28:43 +0530702 return cse_hmrfpo_enable();
703}
704
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530705static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700706 enum cse_update_status status,
707 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530708{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530709 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700710 uint8_t *cbfs_rw_hash;
Julius Werner18881f992021-04-13 15:28:12 -0700711 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530712
Julius Werner18881f992021-04-13 15:28:12 -0700713 const char *area_name = cse_get_source_rdev_fmap();
714 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530715 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530716
Julius Werner18881f992021-04-13 15:28:12 -0700717 void *cse_cbfs_rw = cbfs_unverified_area_map(area_name,
718 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530719 if (!cse_cbfs_rw) {
720 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
721 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
722 }
723
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700724 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
725 if (!cbfs_rw_hash) {
726 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
727 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
728 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
729 goto error_exit;
730 }
731
Julius Werner18881f992021-04-13 15:28:12 -0700732 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530733 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
734 goto error_exit;
735 }
736
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700737 if (!cse_prep_for_rw_update(cse_bp_info, status)) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600738 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530739 goto error_exit;
740 }
741
Julius Werner18881f992021-04-13 15:28:12 -0700742 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530743
744error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700745 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700746 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530747 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530748}
749
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530750static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530751{
752 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700753 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530754
755 if (!cse_get_target_rdev(cse_bp_info, &target_rdev)) {
756 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
757 return CSE_LITE_SKU_RW_ACCESS_ERROR;
758 }
759
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700760 status = cse_check_update_status(cse_bp_info, &target_rdev);
761 if (status == CSE_UPDATE_NOT_REQUIRED)
762 return CSE_NO_ERROR;
763 if (status == CSE_UPDATE_METADATA_ERROR)
764 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530765
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700766 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
767 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530768}
769
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530770static const char *cse_sub_part_str(enum bpdt_entry_type type)
771{
772 switch (type) {
773 case IOM_FW:
774 return "IOM";
775 case NPHY_FW:
776 return "NPHY";
777 default:
778 return "Unknown";
779 }
780}
781
782static bool cse_sub_part_get_target_rdev(struct region_device *target_rdev,
783 const char *region_name, enum bpdt_entry_type type)
784{
785 struct bpdt_header bpdt_hdr;
786 struct region_device cse_rdev;
787 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
788 uint8_t i;
789
790 if (fmap_locate_area_as_rdev_rw(region_name, &cse_rdev) < 0) {
791 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the FMAP\n", region_name);
792 return false;
793 }
794
795 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
796 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
797 return false;
798 }
799
800 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
801 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
802 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
803 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
804 return false;
805 }
806
807 /* walk through BPDT entries to identify sub-partition's payload offset and size */
808 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
809 if (bpdt_entries[i].type == type) {
810 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
811 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
812 bpdt_entries[i].size);
813
814 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
815 bpdt_entries[i].size))
816 return false;
817 else
818 return true;
819 }
820 }
821
822 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
823 return false;
824}
825
826static bool cse_get_sub_part_fw_version(enum bpdt_entry_type type,
827 const struct region_device *rdev,
828 struct fw_version *fw_ver)
829{
830 struct subpart_entry subpart_entry;
831 struct subpart_entry_manifest_header man_hdr;
832
833 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
834 != SUBPART_ENTRY_SZ) {
835 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
836 cse_sub_part_str(type));
837 return false;
838 }
839
840 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
841 != SUBPART_MANIFEST_HDR_SZ) {
842 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
843 cse_sub_part_str(type));
844 return false;
845 }
846
847 fw_ver->major = man_hdr.binary_version.major;
848 fw_ver->minor = man_hdr.binary_version.minor;
849 fw_ver->hotfix = man_hdr.binary_version.hotfix;
850 fw_ver->build = man_hdr.binary_version.build;
851
852 return true;
853}
854
855static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
856{
857 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
858 struct subpart_entry *subpart_entry;
859 struct subpart_entry_manifest_header *man_hdr;
860
861 subpart_entry = (struct subpart_entry *) (ptr + SUBPART_HEADER_SZ);
862 man_hdr = (struct subpart_entry_manifest_header *) (ptr + subpart_entry->offset_bytes);
863
864 fw_ver->major = man_hdr->binary_version.major;
865 fw_ver->minor = man_hdr->binary_version.minor;
866 fw_ver->hotfix = man_hdr->binary_version.hotfix;
867 fw_ver->build = man_hdr->binary_version.build;
868}
869
870static bool cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
871{
872 /*
873 * To set CSE's operation mode to HMRFPO mode:
874 * 1. Ensure CSE to boot from RO(BP1)
875 * 2. Send HMRFPO_ENABLE command to CSE
876 */
877 if (!cse_boot_to_ro(cse_bp_info))
878 return false;
879
880 return cse_hmrfpo_enable();
881}
882
883static uint8_t cse_sub_part_trigger_update(enum bpdt_entry_type type, uint8_t bp,
884 const void *subpart_cbfs_rw, const size_t blob_sz,
885 struct region_device *target_rdev)
886{
887 if (region_device_sz(target_rdev) < blob_sz) {
888 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
889 "smaller than blob size:%zx, abort update\n",
890 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
891 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
892 }
893
894 /* Erase CSE Lite sub-partition */
895 if (!cse_erase_rw_region(target_rdev))
896 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
897
898 /* Update CSE Lite sub-partition */
899 if (!cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz))
900 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
901
902 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
903 cse_sub_part_str(type));
904
905 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
906}
907
908static uint8_t handle_cse_sub_part_fw_update_rv(uint8_t rv)
909{
910 switch (rv) {
911 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
912 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
913 return rv;
914 default:
915 cse_trigger_vboot_recovery(rv);
916 }
917 /* Control never reaches here */
918 return rv;
919}
920
921static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
922 const struct cse_bp_info *cse_bp_info, const char *name)
923{
924 struct region_device target_rdev;
925 struct fw_version target_fw_ver, source_fw_ver;
926 enum csme_failure_reason rv;
927 size_t size;
928 static const char * const cse_regions[] = {"CSE_RO", "CSE_RW"};
929
930 void *subpart_cbfs_rw = cbfs_map(name, &size);
931 if (!subpart_cbfs_rw) {
932 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
933 cse_sub_part_str(type));
934 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
935 }
936
937 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
938 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
939 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
940 source_fw_ver.build);
941
942 /* Trigger sub-partition update in CSE RO and CSE RW */
943 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
944 if (!cse_sub_part_get_target_rdev(&target_rdev, cse_regions[bp], type)) {
945 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
946 goto error_exit;
947 }
948
949 if (!cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver)) {
950 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
951 goto error_exit;
952 }
953
954 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
955 cse_sub_part_str(type), target_fw_ver.major,
956 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
957
958 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
959 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
960 cse_regions[bp], cse_sub_part_str(type));
961 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
962 continue;
963 }
964
965 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
966 cse_sub_part_str(type));
967
968 if (!cse_prep_for_component_update(cse_bp_info)) {
969 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
970 goto error_exit;
971 }
972
973 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
974 size, &target_rdev);
975
976 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
977 goto error_exit;
978 }
979error_exit:
980 cbfs_unmap(subpart_cbfs_rw);
981 return rv;
982}
983
984static uint8_t cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info)
985{
986 if (skip_cse_sub_part_update()) {
987 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
988 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
989 }
990
991 int rv;
992 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
993 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
994
995 handle_cse_sub_part_fw_update_rv(rv);
996
997 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
998 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
999
1000 return handle_cse_sub_part_fw_update_rv(rv);
1001}
1002
Sridhar Siricilla1a2b7022020-12-04 02:22:28 +05301003void cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301004{
1005 static struct get_bp_info_rsp cse_bp_info;
1006
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301007 /*
1008 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1009 * is not enabled and continue to update CSE sub-partitions.
1010 */
1011 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301012 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301013 return;
1014 }
1015
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301016 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1017 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301018 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301019 return;
1020 }
1021
1022 if (!cse_get_bp_info(&cse_bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301023 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301024
1025 /* If system is in recovery mode, don't trigger recovery again */
1026 if (!vboot_recovery_mode_enabled()) {
1027 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1028 } else {
1029 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1030 "so no action\n");
1031 return;
1032 }
1033 }
1034
1035 /*
1036 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
1037 * sub-partitions like NPHY and IOM have to to be updated. If CSE sub-parition update
1038 * fails during recovery, just continue to boot.
1039 */
1040 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1041 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1042 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1043 cse_board_reset();
1044 do_global_reset();
1045 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1046 }
1047
1048 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301049 }
1050
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301051 if (!cse_fix_data_failure_err(&cse_bp_info.bp_info))
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001052 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301053
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301054 /*
1055 * If SOC_INTEL_CSE_RW_UPDATE is defined , then trigger CSE firmware update. The driver
1056 * triggers recovery if CSE CBFS RW metadata or CSE CBFS RW blob is not available.
1057 */
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301058 if (CONFIG(SOC_INTEL_CSE_RW_UPDATE)) {
1059 uint8_t rv;
1060 rv = cse_fw_update(&cse_bp_info.bp_info);
1061 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001062 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301063 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301064
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301065 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1066 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1067
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301068 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001069 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301070
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301071 if (!cse_boot_to_rw(&cse_bp_info.bp_info)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301072 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001073 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301074 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301075}