blob: c4ca41b9fec5472f7ce15c800166ebaf90979fc1 [file] [log] [blame]
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301/* SPDX-License-Identifier: GPL-2.0-only */
Elyes HAOUAS944da482021-02-01 21:30:13 +01002
Krishna P Bhat De3178a12022-04-21 16:40:06 +05303#include <acpi/acpi.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +05304#include <bootstate.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05305#include <cbfs.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05306#include <commonlib/region.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +01007#include <console/console.h>
8#include <cpu/cpu.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +05309#include <fmap.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010010#include <intelbasecode/debug_feature.h>
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053011#include <intelblocks/cse.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053012#include <intelblocks/cse_layout.h>
Subrata Banikda527ec2022-11-24 15:42:35 +053013#include <intelblocks/spi.h>
Sridhar Siricilla87e36c42020-05-03 19:08:18 +053014#include <security/vboot/misc.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010015#include <security/vboot/vboot_common.h>
Rizwan Qureshiec321092019-09-06 20:28:43 +053016#include <soc/intel/common/reset.h>
Krishna P Bhat De3178a12022-04-21 16:40:06 +053017#include <timestamp.h>
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +053018
19#define BPDT_HEADER_SZ sizeof(struct bpdt_header)
20#define BPDT_ENTRY_SZ sizeof(struct bpdt_entry)
21#define SUBPART_HEADER_SZ sizeof(struct subpart_hdr)
22#define SUBPART_ENTRY_SZ sizeof(struct subpart_entry)
23#define SUBPART_MANIFEST_HDR_SZ sizeof(struct subpart_entry_manifest_header)
Rizwan Qureshiec321092019-09-06 20:28:43 +053024
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053025/* Converts bp index to boot partition string */
26#define GET_BP_STR(bp_index) (bp_index ? "RW" : "RO")
27
Rizwan Qureshiec321092019-09-06 20:28:43 +053028/* CSE RW boot partition signature */
29#define CSE_RW_SIGNATURE 0x000055aa
30
31/* CSE RW boot partition signature size */
32#define CSE_RW_SIGN_SIZE sizeof(uint32_t)
33
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053034/*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053035 * CSE Firmware supports 3 boot partitions. For CSE Lite SKU, only 2 boot partitions are
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053036 * used and 3rd boot partition is set to BP_STATUS_PARTITION_NOT_PRESENT.
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053037 * CSE Lite SKU Image Layout:
Angel Pons2e8e0602022-08-13 19:50:28 +020038 * +------------+ +----+------+----+ +-----+------+-----+
39 * | CSE REGION | => | RO | DATA | RW | => | BP1 | DATA | BP2 |
40 * +------------+ +----+------+----+ +-----+------+-----+
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053041 */
42#define CSE_MAX_BOOT_PARTITIONS 3
43
Sridhar Siricilla99dbca32020-05-12 21:05:04 +053044/* CSE Lite SKU's valid bootable partition identifiers */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053045enum boot_partition_id {
Rizwan Qureshiec321092019-09-06 20:28:43 +053046 /* RO(BP1) contains recovery/minimal boot firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053047 RO = 0,
48
Rizwan Qureshiec321092019-09-06 20:28:43 +053049 /* RW(BP2) contains fully functional CSE firmware */
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053050 RW = 1
51};
52
53/*
54 * Boot partition status.
55 * The status is returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
56 */
57enum bp_status {
58 /* This value is returned when a partition has no errors */
59 BP_STATUS_SUCCESS = 0,
60
61 /*
62 * This value is returned when a partition should be present based on layout, but it is
63 * not valid.
64 */
65 BP_STATUS_GENERAL_FAILURE = 1,
66
67 /* This value is returned when a partition is not present per initial image layout */
68 BP_STATUS_PARTITION_NOT_PRESENT = 2,
69
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +053070 /*
71 * This value is returned when unexpected issues are detected in CSE Data area
72 * and CSE TCB-SVN downgrade scenario.
73 */
74 BP_STATUS_DATA_FAILURE = 3,
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053075};
76
77/*
78 * Boot Partition Info Flags
79 * The flags are returned in response to MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO cmd.
80 */
81enum bp_info_flags {
82
83 /* Redundancy Enabled: It indicates CSE supports RO(BP1) and RW(BP2) regions */
84 BP_INFO_REDUNDANCY_EN = 1 << 0,
85
86 /* It indicates RO(BP1) supports Minimal Recovery Mode */
87 BP_INFO_MIN_RECOV_MODE_EN = 1 << 1,
88
89 /*
90 * Read-only Config Enabled: It indicates HW protection to CSE RO region is enabled.
91 * The option is relevant only if the BP_INFO_MIN_RECOV_MODE_EN flag is enabled.
92 */
93 BP_INFO_READ_ONLY_CFG = 1 << 2,
94};
95
Sridhar Siricillaf87ff332019-09-12 17:18:20 +053096/* CSE boot partition entry info */
97struct cse_bp_entry {
98 /* Boot partition version */
99 struct fw_version fw_ver;
100
101 /* Boot partition status */
102 uint32_t status;
103
104 /* Starting offset of the partition within CSE region */
105 uint32_t start_offset;
106
107 /* Ending offset of the partition within CSE region */
108 uint32_t end_offset;
109 uint8_t reserved[12];
110} __packed;
111
112/* CSE boot partition info */
113struct cse_bp_info {
114 /* Number of boot partitions */
115 uint8_t total_number_of_bp;
116
117 /* Current boot partition */
118 uint8_t current_bp;
119
120 /* Next boot partition */
121 uint8_t next_bp;
122
123 /* Boot Partition Info Flags */
124 uint8_t flags;
125
126 /* Boot Partition Entry Info */
127 struct cse_bp_entry bp_entries[CSE_MAX_BOOT_PARTITIONS];
128} __packed;
129
130struct get_bp_info_rsp {
131 struct mkhi_hdr hdr;
132 struct cse_bp_info bp_info;
133} __packed;
134
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530135static const char * const cse_regions[] = {"RO", "RW"};
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700136
Subrata Banikda527ec2022-11-24 15:42:35 +0530137void cse_log_ro_write_protection_info(bool mfg_mode)
138{
139 bool cse_ro_wp_en = is_spi_wp_cse_ro_en();
140
141 printk(BIOS_DEBUG, "ME: WP for RO is enabled : %s\n",
142 cse_ro_wp_en ? "YES" : "NO");
143
144 if (cse_ro_wp_en) {
145 uint32_t base, limit;
146 spi_get_wp_cse_ro_range(&base, &limit);
147 printk(BIOS_DEBUG, "ME: RO write protection scope - Start=0x%X, End=0x%X\n",
148 base, limit);
149 }
150
151 /*
152 * If manufacturing mode is disabled, but CSE RO is not write protected,
153 * log error.
154 */
155 if (!mfg_mode && !cse_ro_wp_en)
156 printk(BIOS_ERR, "ME: Write protection for CSE RO is not enabled\n");
157}
158
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530159enum cb_err cse_get_boot_performance_data(struct cse_boot_perf_rsp *boot_perf_rsp)
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700160{
161 struct cse_boot_perf_req {
162 struct mkhi_hdr hdr;
163 uint32_t reserved;
164 } __packed;
165
166 struct cse_boot_perf_req req = {
167 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
168 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PERF_DATA,
169 .reserved = 0,
170 };
171
172 size_t resp_size = sizeof(struct cse_boot_perf_rsp);
173
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530174 if (heci_send_receive(&req, sizeof(req), boot_perf_rsp, &resp_size,
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700175 HECI_MKHI_ADDR)) {
176 printk(BIOS_ERR, "cse_lite: Could not get boot performance data\n");
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530177 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700178 }
179
180 if (boot_perf_rsp->hdr.result) {
181 printk(BIOS_ERR, "cse_lite: Get boot performance data resp failed: %d\n",
182 boot_perf_rsp->hdr.result);
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530183 return CB_ERR;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700184 }
185
Sridhar Siricilladd7d51d2023-01-10 15:05:07 +0530186 return CB_SUCCESS;
Bora Guvendikf33c9bf2021-11-05 23:09:25 -0700187}
188
189
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530190static uint8_t cse_get_current_bp(const struct cse_bp_info *cse_bp_info)
191{
192 return cse_bp_info->current_bp;
193}
194
195static const struct cse_bp_entry *cse_get_bp_entry(enum boot_partition_id bp,
196 const struct cse_bp_info *cse_bp_info)
197{
198 return &cse_bp_info->bp_entries[bp];
199}
200
201static void cse_print_boot_partition_info(const struct cse_bp_info *cse_bp_info)
202{
203 const struct cse_bp_entry *cse_bp;
204
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530205 printk(BIOS_DEBUG, "cse_lite: Number of partitions = %d\n",
206 cse_bp_info->total_number_of_bp);
207 printk(BIOS_DEBUG, "cse_lite: Current partition = %s\n",
208 GET_BP_STR(cse_bp_info->current_bp));
209 printk(BIOS_DEBUG, "cse_lite: Next partition = %s\n", GET_BP_STR(cse_bp_info->next_bp));
210 printk(BIOS_DEBUG, "cse_lite: Flags = 0x%x\n", cse_bp_info->flags);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530211
212 /* Log version info of RO & RW partitions */
213 cse_bp = cse_get_bp_entry(RO, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530214 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 +0530215 GET_BP_STR(RO), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
216 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
217 cse_bp->status, cse_bp->start_offset,
218 cse_bp->end_offset);
219
220 cse_bp = cse_get_bp_entry(RW, cse_bp_info);
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530221 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 +0530222 GET_BP_STR(RW), cse_bp->fw_ver.major, cse_bp->fw_ver.minor,
223 cse_bp->fw_ver.hotfix, cse_bp->fw_ver.build,
224 cse_bp->status, cse_bp->start_offset,
225 cse_bp->end_offset);
226}
227
228/*
229 * Checks prerequisites for MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO and
230 * MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO HECI commands.
231 * It allows execution of the Boot Partition commands in below scenarios:
232 * - When CSE boots from RW partition (COM: Normal and CWS: Normal)
233 * - When CSE boots from RO partition (COM: Soft Temp Disable and CWS: Normal)
234 * - After HMRFPO_ENABLE command is issued to CSE (COM: SECOVER_MEI_MSG and CWS: Normal)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530235 * The prerequisite check should be handled in cse_get_bp_info() and
236 * cse_set_next_boot_partition() since the CSE's current operation mode is changed between these
237 * cmd handler calls.
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530238 */
239static bool cse_is_bp_cmd_info_possible(void)
240{
241 if (cse_is_hfs1_cws_normal()) {
242 if (cse_is_hfs1_com_normal())
243 return true;
244 if (cse_is_hfs1_com_secover_mei_msg())
245 return true;
246 if (cse_is_hfs1_com_soft_temp_disable())
247 return true;
248 }
249 return false;
250}
251
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530252static enum cb_err cse_get_bp_info(struct get_bp_info_rsp *bp_info_rsp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530253{
254 struct get_bp_info_req {
255 struct mkhi_hdr hdr;
256 uint8_t reserved[4];
257 } __packed;
258
259 struct get_bp_info_req info_req = {
260 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
261 .hdr.command = MKHI_BUP_COMMON_GET_BOOT_PARTITION_INFO,
262 .reserved = {0},
263 };
264
265 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530266 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530267 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530268 }
269
270 size_t resp_size = sizeof(struct get_bp_info_rsp);
271
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530272 if (heci_send_receive(&info_req, sizeof(info_req), bp_info_rsp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530273 HECI_MKHI_ADDR)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530274 printk(BIOS_ERR, "cse_lite: Could not get partition info\n");
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530275 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530276 }
277
278 if (bp_info_rsp->hdr.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530279 printk(BIOS_ERR, "cse_lite: Get partition info resp failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530280 bp_info_rsp->hdr.result);
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530281 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530282 }
283
284 cse_print_boot_partition_info(&bp_info_rsp->bp_info);
285
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +0530286 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530287}
288/*
289 * It sends HECI command to notify CSE about its next boot partition. When coreboot wants
290 * CSE to boot from certain partition (BP1 <RO> or BP2 <RW>), then this command can be used.
291 * The CSE's valid bootable partitions are BP1(RO) and BP2(RW).
292 * This function must be used before EOP.
293 * Returns false on failure and true on success.
294 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530295static enum cb_err cse_set_next_boot_partition(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530296{
297 struct set_boot_partition_info_req {
298 struct mkhi_hdr hdr;
299 uint8_t next_bp;
300 uint8_t reserved[3];
301 } __packed;
302
303 struct set_boot_partition_info_req switch_req = {
304 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
305 .hdr.command = MKHI_BUP_COMMON_SET_BOOT_PARTITION_INFO,
306 .next_bp = bp,
307 .reserved = {0},
308 };
309
310 if (bp != RO && bp != RW) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530311 printk(BIOS_ERR, "cse_lite: Incorrect partition id(%d) is provided", bp);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530312 return CB_ERR_ARG;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530313 }
314
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530315 printk(BIOS_INFO, "cse_lite: Set Boot Partition Info Command (%s)\n", GET_BP_STR(bp));
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530316
317 if (!cse_is_bp_cmd_info_possible()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530318 printk(BIOS_ERR, "cse_lite: CSE does not meet prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530319 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530320 }
321
322 struct mkhi_hdr switch_resp;
323 size_t sw_resp_sz = sizeof(struct mkhi_hdr);
324
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530325 if (heci_send_receive(&switch_req, sizeof(switch_req), &switch_resp, &sw_resp_sz,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530326 HECI_MKHI_ADDR))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530327 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530328
329 if (switch_resp.result) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +0530330 printk(BIOS_ERR, "cse_lite: Set Boot Partition Info Response Failed: %d\n",
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530331 switch_resp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530332 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530333 }
334
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530335 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530336}
337
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530338static enum cb_err cse_data_clear_request(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530339{
340 struct data_clr_request {
341 struct mkhi_hdr hdr;
342 uint8_t reserved[4];
343 } __packed;
344
345 struct data_clr_request data_clr_rq = {
346 .hdr.group_id = MKHI_GROUP_ID_BUP_COMMON,
347 .hdr.command = MKHI_BUP_COMMON_DATA_CLEAR,
348 .reserved = {0},
349 };
350
351 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_soft_temp_disable() ||
352 cse_get_current_bp(cse_bp_info) != RO) {
353 printk(BIOS_ERR, "cse_lite: CSE doesn't meet DATA CLEAR cmd prerequisites\n");
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530354 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530355 }
356
357 printk(BIOS_DEBUG, "cse_lite: Sending DATA CLEAR HECI command\n");
358
359 struct mkhi_hdr data_clr_rsp;
360 size_t data_clr_rsp_sz = sizeof(data_clr_rsp);
361
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530362 if (heci_send_receive(&data_clr_rq, sizeof(data_clr_rq), &data_clr_rsp,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530363 &data_clr_rsp_sz, HECI_MKHI_ADDR)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530364 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530365 }
366
367 if (data_clr_rsp.result) {
368 printk(BIOS_ERR, "cse_lite: CSE DATA CLEAR command response failed: %d\n",
369 data_clr_rsp.result);
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530370 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530371 }
372
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530373 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530374}
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530375
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600376__weak void cse_board_reset(void)
377{
378 /* Default weak implementation, does nothing. */
379}
380
Rizwan Qureshiec321092019-09-06 20:28:43 +0530381/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530382static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530383{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530384 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
385 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530386
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600387 /* Allow the board to perform a reset for CSE RO<->RW jump */
388 cse_board_reset();
389
390 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000391 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530392
Rizwan Qureshiec321092019-09-06 20:28:43 +0530393 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530394
395 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530396 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530397}
398
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530399static enum cb_err cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530400{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530401 if (cse_get_current_bp(cse_bp_info) == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530402 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530403
Rizwan Qureshiec321092019-09-06 20:28:43 +0530404 return cse_set_and_boot_from_next_bp(RW);
405}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530406
V Sowmyaf9905522020-11-12 20:19:04 +0530407/* Check if CSE RW data partition is valid or not */
408static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
409{
410 const struct cse_bp_entry *rw_bp;
411
412 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
413 return rw_bp->status != BP_STATUS_DATA_FAILURE;
414}
415
416/*
417 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
418 * otherwise false if any operation fails.
419 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530420static enum cb_err cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530421{
422 /*
423 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
424 * - Send DATA CLEAR HECI command to CSE
425 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
426 * - Issue GLOBAL RESET HECI command.
427 */
428 if (cse_is_rw_dp_valid(cse_bp_info))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530429 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530430
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530431 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS)
432 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530433
434 return cse_boot_to_rw(cse_bp_info);
435}
436
V Sowmyaf9905522020-11-12 20:19:04 +0530437static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530438 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530439{
440 const struct cse_bp_entry *cse_bp;
441
442 cse_bp = cse_get_bp_entry(bp, bp_info);
443 return &cse_bp->fw_ver;
444}
445
446static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
447{
448 return cse_get_bp_entry_version(RW, cse_bp_info);
449}
450
451static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530452 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530453{
454 const struct cse_bp_entry *cse_bp;
455
456 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
457
458 if (start_offset)
459 *start_offset = cse_bp->start_offset;
460
461 if (end_offset)
462 *end_offset = cse_bp->end_offset;
463
464}
465
466static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
467{
468 const struct cse_bp_entry *rw_bp;
469
470 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
471
472 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
473 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
474 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
475 return false;
476 }
477 return true;
478}
479
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530480static enum cb_err cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530481{
482 if (cse_get_current_bp(cse_bp_info) == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530483 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530484
485 return cse_set_and_boot_from_next_bp(RO);
486}
487
488static bool cse_get_rw_rdev(struct region_device *rdev)
489{
490 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
491 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530492 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530493 return false;
494 }
495
496 return true;
497}
498
Rizwan Qureshiec321092019-09-06 20:28:43 +0530499static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
500{
501 uint32_t cse_bp_sign;
502
503 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
504 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
505 return false;
506 }
507
508 return cse_bp_sign == CSE_RW_SIGNATURE;
509}
510
511static bool cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
512 struct region_device *target_rdev)
513{
514 struct region_device cse_region_rdev;
515 size_t size;
516 uint32_t start_offset;
517 uint32_t end_offset;
518
519 if (!cse_get_rw_rdev(&cse_region_rdev))
520 return false;
521
522 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
523 size = end_offset + 1 - start_offset;
524
525 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
526 return false;
527
528 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100529 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530530
531 return true;
532}
533
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530534static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530535{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530536 struct vb2_context *ctx = vboot_get_context();
537 if (ctx == NULL)
538 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530539
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530540 if (vboot_is_firmware_slot_a(ctx))
541 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
542
543 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
544}
545
Rizwan Qureshiec321092019-09-06 20:28:43 +0530546/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530547 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
548 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530549 * If ver_cmp_status = 0, no update is required
550 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
551 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
552 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530553static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530554{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700555 if (a->major != b->major)
556 return a->major - b->major;
557 else if (a->minor != b->minor)
558 return a->minor - b->minor;
559 else if (a->hotfix != b->hotfix)
560 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530561 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700562 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530563}
564
565/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
566static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
567 const void *rw_blob, const size_t rw_blob_sz)
568
569{
Julius Wernerd96ca242022-08-08 18:08:35 -0700570 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530571
Julius Wernerd96ca242022-08-08 18:08:35 -0700572 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
573 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530574 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
575 return false;
576 }
577
Julius Wernerd96ca242022-08-08 18:08:35 -0700578 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530579 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
580 "the provided SHA in the metadata\n");
581 return false;
582 }
583 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
584 " provided hash in the metadata\n");
585 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530586}
587
588static bool cse_erase_rw_region(const struct region_device *target_rdev)
589{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530590 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
591 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
592 return false;
593 }
594 return true;
595}
596
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530597static bool cse_copy_rw(const struct region_device *target_rdev, const void *buf,
598 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530599{
600 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
601 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
602 return false;
603 }
604
605 return true;
606}
607
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700608enum cse_update_status {
609 CSE_UPDATE_NOT_REQUIRED,
610 CSE_UPDATE_UPGRADE,
611 CSE_UPDATE_DOWNGRADE,
612 CSE_UPDATE_CORRUPTED,
613 CSE_UPDATE_METADATA_ERROR,
614};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530615
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700616static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
617{
618 if ((*curr - start) >= size) {
619 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
620 return false;
621 }
622
623 *ver_field = skip_atoi(curr);
624 (*curr)++;
625 return true;
626}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530627
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700628static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
629 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530630{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700631 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700632 struct fw_version cbfs_rw_version;
633 char *version_str, *ptr;
634 size_t size;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700635
636 if (!cse_is_rw_bp_sign_valid(target_rdev))
637 return CSE_UPDATE_CORRUPTED;
638
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700639 ptr = version_str = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
640 if (!version_str) {
641 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
642 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
643 return CSE_UPDATE_METADATA_ERROR;
644 }
645
646 if (!read_ver_field(version_str, &ptr, size, &cbfs_rw_version.major) ||
647 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.minor) ||
648 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.hotfix) ||
649 !read_ver_field(version_str, &ptr, size, &cbfs_rw_version.build)) {
650 cbfs_unmap(version_str);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700651 return CSE_UPDATE_METADATA_ERROR;
652 }
653
654 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700655 cbfs_rw_version.major,
656 cbfs_rw_version.minor,
657 cbfs_rw_version.hotfix,
658 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700659
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700660 cbfs_unmap(version_str);
661
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530662 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700663 if (ret == 0)
664 return CSE_UPDATE_NOT_REQUIRED;
665 else if (ret < 0)
666 return CSE_UPDATE_DOWNGRADE;
667 else
668 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530669}
670
671static bool cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530672 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530673{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530674 /* Points to CSE CBFS RW image after boot partition signature */
675 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
676
677 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530678 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530679
680 /* Update except CSE RW signature */
681 if (!cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
682 cse_cbfs_rw_wo_sign_sz))
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530683 return false;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530684
685 /* Update CSE RW signature to indicate update is complete */
686 if (!cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE))
Rizwan Qureshiec321092019-09-06 20:28:43 +0530687 return false;
688
689 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
690 return true;
691}
692
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530693static bool is_cse_fw_update_enabled(void)
694{
695 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
696 return false;
697
698 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
699 return !is_debug_cse_fw_update_disable();
700
701 return true;
702}
703
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530704static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
705 const void *cse_cbfs_rw, const size_t cse_blob_sz,
706 struct region_device *target_rdev)
707{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530708 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530709 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
710 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530711 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
712 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530713
714 if (!cse_erase_rw_region(target_rdev))
715 return CSE_LITE_SKU_FW_UPDATE_ERROR;
716
717 if (!cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz))
718 return CSE_LITE_SKU_FW_UPDATE_ERROR;
719
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600720 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530721}
722
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530723static enum cb_err cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700724 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530725{
726 /*
727 * To set CSE's operation mode to HMRFPO mode:
728 * 1. Ensure CSE to boot from RO(BP1)
729 * 2. Send HMRFPO_ENABLE command to CSE
730 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530731 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
732 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530733
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700734 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530735 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700736 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530737 return CB_SUCCESS;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700738 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530739 }
740
Rizwan Qureshiec321092019-09-06 20:28:43 +0530741 return cse_hmrfpo_enable();
742}
743
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530744static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700745 enum cse_update_status status,
746 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530747{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530748 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700749 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530750 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700751 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530752
Julius Werner18881f992021-04-13 15:28:12 -0700753 const char *area_name = cse_get_source_rdev_fmap();
754 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530755 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530756
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530757 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
758 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
759 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
760 } else {
761 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
762 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
763 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530764 if (!cse_cbfs_rw) {
765 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
766 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
767 }
768
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700769 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
770 if (!cbfs_rw_hash) {
771 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
772 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
773 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
774 goto error_exit;
775 }
776
Julius Werner18881f992021-04-13 15:28:12 -0700777 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530778 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
779 goto error_exit;
780 }
781
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530782 if (cse_prep_for_rw_update(cse_bp_info, status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600783 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530784 goto error_exit;
785 }
786
Julius Werner18881f992021-04-13 15:28:12 -0700787 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530788
789error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700790 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700791 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530792 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530793}
794
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530795static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530796{
797 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700798 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530799
800 if (!cse_get_target_rdev(cse_bp_info, &target_rdev)) {
801 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
802 return CSE_LITE_SKU_RW_ACCESS_ERROR;
803 }
804
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700805 status = cse_check_update_status(cse_bp_info, &target_rdev);
806 if (status == CSE_UPDATE_NOT_REQUIRED)
807 return CSE_NO_ERROR;
808 if (status == CSE_UPDATE_METADATA_ERROR)
809 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530810
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700811 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
812 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530813}
814
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530815static const char *cse_sub_part_str(enum bpdt_entry_type type)
816{
817 switch (type) {
818 case IOM_FW:
819 return "IOM";
820 case NPHY_FW:
821 return "NPHY";
822 default:
823 return "Unknown";
824 }
825}
826
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530827static bool cse_locate_area_as_rdev_rw(const struct cse_bp_info *cse_bp_info,
828 size_t bp, struct region_device *cse_rdev)
829{
830 struct region_device cse_region_rdev;
831 uint32_t size;
832 uint32_t start_offset;
833 uint32_t end_offset;
834
835 if (!cse_get_rw_rdev(&cse_region_rdev))
836 return false;
837
838 if (!strcmp(cse_regions[bp], "RO"))
839 cse_get_bp_entry_range(cse_bp_info, RO, &start_offset, &end_offset);
840 else
841 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
842
843 size = end_offset + 1 - start_offset;
844
845 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
846 return false;
847
848 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
849 cse_regions[bp], start_offset, size);
850 return true;
851}
852
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530853static enum cb_err cse_sub_part_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530854 struct region_device *target_rdev, size_t bp, enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530855{
856 struct bpdt_header bpdt_hdr;
857 struct region_device cse_rdev;
858 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
859 uint8_t i;
860
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530861 if (!cse_locate_area_as_rdev_rw(cse_bp_info, bp, &cse_rdev)) {
862 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
863 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530864 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530865 }
866
867 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
868 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530869 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530870 }
871
872 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
873 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
874 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
875 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530876 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530877 }
878
879 /* walk through BPDT entries to identify sub-partition's payload offset and size */
880 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
881 if (bpdt_entries[i].type == type) {
882 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
883 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
884 bpdt_entries[i].size);
885
886 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
887 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530888 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530889 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530890 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530891 }
892 }
893
894 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530895 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530896}
897
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530898static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530899 const struct region_device *rdev,
900 struct fw_version *fw_ver)
901{
902 struct subpart_entry subpart_entry;
903 struct subpart_entry_manifest_header man_hdr;
904
905 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
906 != SUBPART_ENTRY_SZ) {
907 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
908 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530909 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530910 }
911
912 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
913 != SUBPART_MANIFEST_HDR_SZ) {
914 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
915 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530916 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530917 }
918
919 fw_ver->major = man_hdr.binary_version.major;
920 fw_ver->minor = man_hdr.binary_version.minor;
921 fw_ver->hotfix = man_hdr.binary_version.hotfix;
922 fw_ver->build = man_hdr.binary_version.build;
923
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530924 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530925}
926
927static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
928{
929 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
930 struct subpart_entry *subpart_entry;
931 struct subpart_entry_manifest_header *man_hdr;
932
Elyes Haouas9018dee2022-11-18 15:07:33 +0100933 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
934 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530935
936 fw_ver->major = man_hdr->binary_version.major;
937 fw_ver->minor = man_hdr->binary_version.minor;
938 fw_ver->hotfix = man_hdr->binary_version.hotfix;
939 fw_ver->build = man_hdr->binary_version.build;
940}
941
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530942static enum cb_err cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530943{
944 /*
945 * To set CSE's operation mode to HMRFPO mode:
946 * 1. Ensure CSE to boot from RO(BP1)
947 * 2. Send HMRFPO_ENABLE command to CSE
948 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530949 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
950 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530951
952 return cse_hmrfpo_enable();
953}
954
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +0530955static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
956 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530957 struct region_device *target_rdev)
958{
959 if (region_device_sz(target_rdev) < blob_sz) {
960 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
961 "smaller than blob size:%zx, abort update\n",
962 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
963 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
964 }
965
966 /* Erase CSE Lite sub-partition */
967 if (!cse_erase_rw_region(target_rdev))
968 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
969
970 /* Update CSE Lite sub-partition */
971 if (!cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz))
972 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
973
974 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
975 cse_sub_part_str(type));
976
977 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
978}
979
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +0530980static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530981{
982 switch (rv) {
983 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
984 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
985 return rv;
986 default:
987 cse_trigger_vboot_recovery(rv);
988 }
989 /* Control never reaches here */
990 return rv;
991}
992
993static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
994 const struct cse_bp_info *cse_bp_info, const char *name)
995{
996 struct region_device target_rdev;
997 struct fw_version target_fw_ver, source_fw_ver;
998 enum csme_failure_reason rv;
999 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301000
1001 void *subpart_cbfs_rw = cbfs_map(name, &size);
1002 if (!subpart_cbfs_rw) {
1003 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1004 cse_sub_part_str(type));
1005 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1006 }
1007
1008 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1009 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1010 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1011 source_fw_ver.build);
1012
1013 /* Trigger sub-partition update in CSE RO and CSE RW */
1014 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301015 if (cse_sub_part_get_target_rdev(cse_bp_info, &target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301016 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1017 goto error_exit;
1018 }
1019
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301020 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301021 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1022 goto error_exit;
1023 }
1024
1025 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1026 cse_sub_part_str(type), target_fw_ver.major,
1027 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1028
1029 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1030 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1031 cse_regions[bp], cse_sub_part_str(type));
1032 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1033 continue;
1034 }
1035
1036 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1037 cse_sub_part_str(type));
1038
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301039 if (cse_prep_for_component_update(cse_bp_info) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301040 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1041 goto error_exit;
1042 }
1043
1044 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1045 size, &target_rdev);
1046
1047 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1048 goto error_exit;
1049 }
1050error_exit:
1051 cbfs_unmap(subpart_cbfs_rw);
1052 return rv;
1053}
1054
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301055static enum csme_failure_reason cse_sub_part_fw_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301056{
1057 if (skip_cse_sub_part_update()) {
1058 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1059 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1060 }
1061
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301062 enum csme_failure_reason rv;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301063 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
1064 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1065
1066 handle_cse_sub_part_fw_update_rv(rv);
1067
1068 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
1069 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1070
1071 return handle_cse_sub_part_fw_update_rv(rv);
1072}
1073
Sridhar Siricilla1a2b7022020-12-04 02:22:28 +05301074void cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301075{
1076 static struct get_bp_info_rsp cse_bp_info;
1077
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301078 /*
1079 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1080 * is not enabled and continue to update CSE sub-partitions.
1081 */
1082 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301083 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301084 return;
1085 }
1086
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301087 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1088 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301089 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301090 return;
1091 }
1092
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +05301093 if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301094 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301095
1096 /* If system is in recovery mode, don't trigger recovery again */
1097 if (!vboot_recovery_mode_enabled()) {
1098 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1099 } else {
1100 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1101 "so no action\n");
1102 return;
1103 }
1104 }
1105
1106 /*
1107 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
1108 * sub-partitions like NPHY and IOM have to to be updated. If CSE sub-parition update
1109 * fails during recovery, just continue to boot.
1110 */
1111 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1112 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1113 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1114 cse_board_reset();
1115 do_global_reset();
1116 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1117 }
1118
1119 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301120 }
1121
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301122 if (cse_fix_data_failure_err(&cse_bp_info.bp_info) != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001123 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301124
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301125 /*
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301126 * cse firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
1127 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1128 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301129 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301130 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301131 uint8_t rv;
1132 rv = cse_fw_update(&cse_bp_info.bp_info);
1133 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001134 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301135 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301136
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301137 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1138 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1139
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301140 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001141 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301142
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301143 if (cse_boot_to_rw(&cse_bp_info.bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301144 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001145 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301146 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301147}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301148
1149static void ramstage_cse_fw_sync(void *unused)
1150{
1151 bool s3wake;
1152 s3wake = acpi_get_sleep_type() == ACPI_S3;
1153
1154 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE) && !s3wake) {
1155 timestamp_add_now(TS_CSE_FW_SYNC_START);
1156 cse_fw_sync();
1157 timestamp_add_now(TS_CSE_FW_SYNC_END);
1158 }
1159}
1160
1161BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_fw_sync, NULL);