blob: 6203baaa4595bde8b553a2ec8d63f8c5eac18217 [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
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700381__weak void cse_fw_update_misc_oper(void)
382{
383 /* Default weak implementation, does nothing. */
384}
385
Rizwan Qureshiec321092019-09-06 20:28:43 +0530386/* Set the CSE's next boot partition and issues system reset */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530387static enum cb_err cse_set_and_boot_from_next_bp(enum boot_partition_id bp)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530388{
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530389 if (cse_set_next_boot_partition(bp) != CB_SUCCESS)
390 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530391
Karthikeyan Ramasubramanianf9cc6372020-08-04 16:38:58 -0600392 /* Allow the board to perform a reset for CSE RO<->RW jump */
393 cse_board_reset();
394
395 /* If board does not perform the reset, then perform global_reset */
Furquan Shaikhb13bd1e2020-09-21 22:44:27 +0000396 do_global_reset();
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530397
Rizwan Qureshiec321092019-09-06 20:28:43 +0530398 die("cse_lite: Failed to reset the system\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530399
400 /* Control never reaches here */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530401 return CB_ERR;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530402}
403
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530404static enum cb_err cse_boot_to_rw(const struct cse_bp_info *cse_bp_info)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530405{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530406 if (cse_get_current_bp(cse_bp_info) == RW)
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530407 return CB_SUCCESS;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530408
Rizwan Qureshiec321092019-09-06 20:28:43 +0530409 return cse_set_and_boot_from_next_bp(RW);
410}
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530411
V Sowmyaf9905522020-11-12 20:19:04 +0530412/* Check if CSE RW data partition is valid or not */
413static bool cse_is_rw_dp_valid(const struct cse_bp_info *cse_bp_info)
414{
415 const struct cse_bp_entry *rw_bp;
416
417 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
418 return rw_bp->status != BP_STATUS_DATA_FAILURE;
419}
420
421/*
422 * It returns true if RW partition doesn't indicate BP_STATUS_DATA_FAILURE
423 * otherwise false if any operation fails.
424 */
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530425static enum cb_err cse_fix_data_failure_err(const struct cse_bp_info *cse_bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530426{
427 /*
428 * If RW partition status indicates BP_STATUS_DATA_FAILURE,
429 * - Send DATA CLEAR HECI command to CSE
430 * - Send SET BOOT PARTITION INFO(RW) command to set CSE's next partition
431 * - Issue GLOBAL RESET HECI command.
432 */
433 if (cse_is_rw_dp_valid(cse_bp_info))
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530434 return CB_SUCCESS;
V Sowmyaf9905522020-11-12 20:19:04 +0530435
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530436 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS)
437 return CB_ERR;
V Sowmyaf9905522020-11-12 20:19:04 +0530438
439 return cse_boot_to_rw(cse_bp_info);
440}
441
V Sowmyaf9905522020-11-12 20:19:04 +0530442static const struct fw_version *cse_get_bp_entry_version(enum boot_partition_id bp,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530443 const struct cse_bp_info *bp_info)
V Sowmyaf9905522020-11-12 20:19:04 +0530444{
445 const struct cse_bp_entry *cse_bp;
446
447 cse_bp = cse_get_bp_entry(bp, bp_info);
448 return &cse_bp->fw_ver;
449}
450
451static const struct fw_version *cse_get_rw_version(const struct cse_bp_info *cse_bp_info)
452{
453 return cse_get_bp_entry_version(RW, cse_bp_info);
454}
455
456static void cse_get_bp_entry_range(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530457 enum boot_partition_id bp, uint32_t *start_offset, uint32_t *end_offset)
V Sowmyaf9905522020-11-12 20:19:04 +0530458{
459 const struct cse_bp_entry *cse_bp;
460
461 cse_bp = cse_get_bp_entry(bp, cse_bp_info);
462
463 if (start_offset)
464 *start_offset = cse_bp->start_offset;
465
466 if (end_offset)
467 *end_offset = cse_bp->end_offset;
468
469}
470
471static bool cse_is_rw_bp_status_valid(const struct cse_bp_info *cse_bp_info)
472{
473 const struct cse_bp_entry *rw_bp;
474
475 rw_bp = cse_get_bp_entry(RW, cse_bp_info);
476
477 if (rw_bp->status == BP_STATUS_PARTITION_NOT_PRESENT ||
478 rw_bp->status == BP_STATUS_GENERAL_FAILURE) {
479 printk(BIOS_ERR, "cse_lite: RW BP (status:%u) is not valid\n", rw_bp->status);
480 return false;
481 }
482 return true;
483}
484
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530485static enum cb_err cse_boot_to_ro(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530486{
487 if (cse_get_current_bp(cse_bp_info) == RO)
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530488 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530489
490 return cse_set_and_boot_from_next_bp(RO);
491}
492
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530493static enum cb_err cse_get_rw_rdev(struct region_device *rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530494{
495 if (fmap_locate_area_as_rdev_rw(CONFIG_SOC_INTEL_CSE_FMAP_NAME, rdev) < 0) {
496 printk(BIOS_ERR, "cse_lite: Failed to locate %s in FMAP\n",
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530497 CONFIG_SOC_INTEL_CSE_FMAP_NAME);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530498 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530499 }
500
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530501 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530502}
503
Rizwan Qureshiec321092019-09-06 20:28:43 +0530504static bool cse_is_rw_bp_sign_valid(const struct region_device *target_rdev)
505{
506 uint32_t cse_bp_sign;
507
508 if (rdev_readat(target_rdev, &cse_bp_sign, 0, CSE_RW_SIGN_SIZE) != CSE_RW_SIGN_SIZE) {
509 printk(BIOS_ERR, "cse_lite: Failed to read RW boot partition signature\n");
510 return false;
511 }
512
513 return cse_bp_sign == CSE_RW_SIGNATURE;
514}
515
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530516static enum cb_err cse_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Rizwan Qureshiec321092019-09-06 20:28:43 +0530517 struct region_device *target_rdev)
518{
519 struct region_device cse_region_rdev;
520 size_t size;
521 uint32_t start_offset;
522 uint32_t end_offset;
523
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530524 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
525 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530526
527 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
528 size = end_offset + 1 - start_offset;
529
530 if (rdev_chain(target_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530531 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530532
533 printk(BIOS_DEBUG, "cse_lite: CSE RW partition: offset = 0x%x, size = 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100534 (uint32_t)start_offset, (uint32_t)size);
Rizwan Qureshiec321092019-09-06 20:28:43 +0530535
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530536 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530537}
538
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530539static const char *cse_get_source_rdev_fmap(void)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530540{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530541 struct vb2_context *ctx = vboot_get_context();
542 if (ctx == NULL)
543 return NULL;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530544
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530545 if (vboot_is_firmware_slot_a(ctx))
546 return CONFIG_SOC_INTEL_CSE_RW_A_FMAP_NAME;
547
548 return CONFIG_SOC_INTEL_CSE_RW_B_FMAP_NAME;
549}
550
Rizwan Qureshiec321092019-09-06 20:28:43 +0530551/*
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530552 * Compare versions of CSE CBFS sub-component and CSE sub-component partition
553 * In case of CSE component comparison:
Rizwan Qureshiec321092019-09-06 20:28:43 +0530554 * If ver_cmp_status = 0, no update is required
555 * If ver_cmp_status < 0, coreboot downgrades CSE RW region
556 * If ver_cmp_status > 0, coreboot upgrades CSE RW region
557 */
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530558static int cse_compare_sub_part_version(const struct fw_version *a, const struct fw_version *b)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530559{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700560 if (a->major != b->major)
561 return a->major - b->major;
562 else if (a->minor != b->minor)
563 return a->minor - b->minor;
564 else if (a->hotfix != b->hotfix)
565 return a->hotfix - b->hotfix;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530566 else
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700567 return a->build - b->build;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530568}
569
570/* The function calculates SHA-256 of CSE RW blob and compares it with the provided SHA value */
571static bool cse_verify_cbfs_rw_sha256(const uint8_t *expected_rw_blob_sha,
572 const void *rw_blob, const size_t rw_blob_sz)
573
574{
Julius Wernerd96ca242022-08-08 18:08:35 -0700575 struct vb2_hash calculated;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530576
Julius Wernerd96ca242022-08-08 18:08:35 -0700577 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), rw_blob, rw_blob_sz,
578 VB2_HASH_SHA256, &calculated)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530579 printk(BIOS_ERR, "cse_lite: CSE CBFS RW's SHA-256 calculation has failed\n");
580 return false;
581 }
582
Julius Wernerd96ca242022-08-08 18:08:35 -0700583 if (memcmp(expected_rw_blob_sha, calculated.sha256, sizeof(calculated.sha256))) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530584 printk(BIOS_ERR, "cse_lite: Computed CBFS RW's SHA-256 does not match with"
585 "the provided SHA in the metadata\n");
586 return false;
587 }
588 printk(BIOS_SPEW, "cse_lite: Computed SHA of CSE CBFS RW Image matches the"
589 " provided hash in the metadata\n");
590 return true;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530591}
592
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530593static enum cb_err cse_erase_rw_region(const struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530594{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530595 if (rdev_eraseat(target_rdev, 0, region_device_sz(target_rdev)) < 0) {
596 printk(BIOS_ERR, "cse_lite: CSE RW partition could not be erased\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530597 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530598 }
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530599 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530600}
601
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530602static enum cb_err cse_copy_rw(const struct region_device *target_rdev, const void *buf,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530603 size_t offset, size_t size)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530604{
605 if (rdev_writeat(target_rdev, buf, offset, size) < 0) {
606 printk(BIOS_ERR, "cse_lite: Failed to update CSE firmware\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530607 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530608 }
609
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530610 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530611}
612
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700613enum cse_update_status {
614 CSE_UPDATE_NOT_REQUIRED,
615 CSE_UPDATE_UPGRADE,
616 CSE_UPDATE_DOWNGRADE,
617 CSE_UPDATE_CORRUPTED,
618 CSE_UPDATE_METADATA_ERROR,
619};
Rizwan Qureshiec321092019-09-06 20:28:43 +0530620
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700621static bool read_ver_field(const char *start, char **curr, size_t size, uint16_t *ver_field)
622{
623 if ((*curr - start) >= size) {
624 printk(BIOS_ERR, "cse_lite: Version string read overflow!\n");
625 return false;
626 }
627
628 *ver_field = skip_atoi(curr);
629 (*curr)++;
630 return true;
631}
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530632
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530633static enum cb_err get_cse_ver_from_cbfs(struct fw_version *cbfs_rw_version)
634{
635 char *version_str, *cbfs_ptr;
636 size_t size;
637
638 if (cbfs_rw_version == NULL)
639 return CB_ERR;
640
641 cbfs_ptr = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME, &size);
642 version_str = cbfs_ptr;
643 if (!version_str) {
644 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
645 CONFIG_SOC_INTEL_CSE_RW_VERSION_CBFS_NAME);
646 return CB_ERR;
647 }
648
649 if (!read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->major) ||
650 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->minor) ||
651 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->hotfix) ||
652 !read_ver_field(version_str, &cbfs_ptr, size, &cbfs_rw_version->build)) {
653 cbfs_unmap(version_str);
654 return CB_ERR;
655 }
656
657 cbfs_unmap(version_str);
658 return CB_SUCCESS;
659}
660
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700661static enum cse_update_status cse_check_update_status(const struct cse_bp_info *cse_bp_info,
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530662 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530663{
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700664 int ret;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700665 struct fw_version cbfs_rw_version;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700666
667 if (!cse_is_rw_bp_sign_valid(target_rdev))
668 return CSE_UPDATE_CORRUPTED;
669
Ashish Kumar Mishra2ee71622023-04-25 17:23:21 +0530670 if (get_cse_ver_from_cbfs(&cbfs_rw_version) == CB_ERR)
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700671 return CSE_UPDATE_METADATA_ERROR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700672
673 printk(BIOS_DEBUG, "cse_lite: CSE CBFS RW version : %d.%d.%d.%d\n",
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700674 cbfs_rw_version.major,
675 cbfs_rw_version.minor,
676 cbfs_rw_version.hotfix,
677 cbfs_rw_version.build);
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700678
Sridhar Siricillab9277ba2021-11-27 13:57:40 +0530679 ret = cse_compare_sub_part_version(&cbfs_rw_version, cse_get_rw_version(cse_bp_info));
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700680 if (ret == 0)
681 return CSE_UPDATE_NOT_REQUIRED;
682 else if (ret < 0)
683 return CSE_UPDATE_DOWNGRADE;
684 else
685 return CSE_UPDATE_UPGRADE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530686}
687
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530688static enum cb_err cse_write_rw_region(const struct region_device *target_rdev,
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530689 const void *cse_cbfs_rw, const size_t cse_cbfs_rw_sz)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530690{
Rizwan Qureshiec321092019-09-06 20:28:43 +0530691 /* Points to CSE CBFS RW image after boot partition signature */
692 uint8_t *cse_cbfs_rw_wo_sign = (uint8_t *)cse_cbfs_rw + CSE_RW_SIGN_SIZE;
693
694 /* Size of CSE CBFS RW image without boot partition signature */
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530695 uint32_t cse_cbfs_rw_wo_sign_sz = cse_cbfs_rw_sz - CSE_RW_SIGN_SIZE;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530696
697 /* Update except CSE RW signature */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530698 if (cse_copy_rw(target_rdev, cse_cbfs_rw_wo_sign, CSE_RW_SIGN_SIZE,
699 cse_cbfs_rw_wo_sign_sz) != CB_SUCCESS)
700 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530701
702 /* Update CSE RW signature to indicate update is complete */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530703 if (cse_copy_rw(target_rdev, (void *)cse_cbfs_rw, 0, CSE_RW_SIGN_SIZE) != CB_SUCCESS)
704 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530705
706 printk(BIOS_INFO, "cse_lite: CSE RW Update Successful\n");
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530707 return CB_SUCCESS;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530708}
709
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +0530710static bool is_cse_fw_update_enabled(void)
711{
712 if (!CONFIG(SOC_INTEL_CSE_RW_UPDATE))
713 return false;
714
715 if (CONFIG(SOC_INTEL_COMMON_BASECODE_DEBUG_FEATURE))
716 return !is_debug_cse_fw_update_disable();
717
718 return true;
719}
720
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530721static enum csme_failure_reason cse_update_rw(const struct cse_bp_info *cse_bp_info,
722 const void *cse_cbfs_rw, const size_t cse_blob_sz,
723 struct region_device *target_rdev)
724{
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530725 if (region_device_sz(target_rdev) < cse_blob_sz) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530726 printk(BIOS_ERR, "RW update does not fit. CSE RW flash region size: %zx,"
727 "Update blob size:%zx\n", region_device_sz(target_rdev), cse_blob_sz);
Sridhar Siricillaabeb6882020-12-07 15:55:10 +0530728 return CSE_LITE_SKU_LAYOUT_MISMATCH_ERROR;
729 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530730
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530731 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530732 return CSE_LITE_SKU_FW_UPDATE_ERROR;
733
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530734 if (cse_write_rw_region(target_rdev, cse_cbfs_rw, cse_blob_sz) != CB_SUCCESS)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530735 return CSE_LITE_SKU_FW_UPDATE_ERROR;
736
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600737 return CSE_NO_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530738}
739
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530740static enum cb_err cse_prep_for_rw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700741 enum cse_update_status status)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530742{
743 /*
744 * To set CSE's operation mode to HMRFPO mode:
745 * 1. Ensure CSE to boot from RO(BP1)
746 * 2. Send HMRFPO_ENABLE command to CSE
747 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530748 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
749 return CB_ERR;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530750
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700751 if ((status == CSE_UPDATE_DOWNGRADE) || (status == CSE_UPDATE_CORRUPTED)) {
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +0530752 if (cse_data_clear_request(cse_bp_info) != CB_SUCCESS) {
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700753 printk(BIOS_ERR, "cse_lite: CSE data clear failed!\n");
Krishna Prasad Bhat6ba83482023-08-03 12:15:32 +0530754 return CB_ERR;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700755 }
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +0530756 }
757
Rizwan Qureshiec321092019-09-06 20:28:43 +0530758 return cse_hmrfpo_enable();
759}
760
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530761static enum csme_failure_reason cse_trigger_fw_update(const struct cse_bp_info *cse_bp_info,
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700762 enum cse_update_status status,
763 struct region_device *target_rdev)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530764{
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530765 enum csme_failure_reason rv;
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700766 uint8_t *cbfs_rw_hash;
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530767 void *cse_cbfs_rw = NULL;
Julius Werner18881f992021-04-13 15:28:12 -0700768 size_t size;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530769
Julius Werner18881f992021-04-13 15:28:12 -0700770 const char *area_name = cse_get_source_rdev_fmap();
771 if (!area_name)
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530772 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530773
Krishna P Bhat D75a423e2022-04-20 15:50:06 +0530774 if (CONFIG(SOC_INTEL_CSE_LITE_COMPRESS_ME_RW)) {
775 cse_cbfs_rw = cbfs_unverified_area_cbmem_alloc(area_name,
776 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, CBMEM_ID_CSE_UPDATE, &size);
777 } else {
778 cse_cbfs_rw = cbfs_unverified_area_map(area_name,
779 CONFIG_SOC_INTEL_CSE_RW_CBFS_NAME, &size);
780 }
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530781 if (!cse_cbfs_rw) {
782 printk(BIOS_ERR, "cse_lite: CSE CBFS RW blob could not be mapped\n");
783 return CSE_LITE_SKU_RW_BLOB_NOT_FOUND;
784 }
785
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700786 cbfs_rw_hash = cbfs_map(CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME, NULL);
787 if (!cbfs_rw_hash) {
788 printk(BIOS_ERR, "cse_lite: Failed to get %s\n",
789 CONFIG_SOC_INTEL_CSE_RW_HASH_CBFS_NAME);
790 rv = CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
791 goto error_exit;
792 }
793
Julius Werner18881f992021-04-13 15:28:12 -0700794 if (!cse_verify_cbfs_rw_sha256(cbfs_rw_hash, cse_cbfs_rw, size)) {
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530795 rv = CSE_LITE_SKU_RW_BLOB_SHA256_MISMATCH;
796 goto error_exit;
797 }
798
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530799 if (cse_prep_for_rw_update(cse_bp_info, status) != CB_SUCCESS) {
Tim Wawrzynczake380a432021-06-18 09:54:55 -0600800 rv = CSE_COMMUNICATION_ERROR;
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530801 goto error_exit;
802 }
803
Jeremy Compostella08b5200d2023-01-19 11:32:25 -0700804 cse_fw_update_misc_oper();
Julius Werner18881f992021-04-13 15:28:12 -0700805 rv = cse_update_rw(cse_bp_info, cse_cbfs_rw, size, target_rdev);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530806
807error_exit:
Furquan Shaikhd2da8702021-10-07 00:08:59 -0700808 cbfs_unmap(cbfs_rw_hash);
Julius Werner18881f992021-04-13 15:28:12 -0700809 cbfs_unmap(cse_cbfs_rw);
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530810 return rv;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530811}
812
Sridhar Siricilla361e3642020-10-18 20:14:07 +0530813static uint8_t cse_fw_update(const struct cse_bp_info *cse_bp_info)
Rizwan Qureshiec321092019-09-06 20:28:43 +0530814{
815 struct region_device target_rdev;
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700816 enum cse_update_status status;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530817
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530818 if (cse_get_target_rdev(cse_bp_info, &target_rdev) != CB_SUCCESS) {
Rizwan Qureshiec321092019-09-06 20:28:43 +0530819 printk(BIOS_ERR, "cse_lite: Failed to get CSE RW Partition\n");
820 return CSE_LITE_SKU_RW_ACCESS_ERROR;
821 }
822
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700823 status = cse_check_update_status(cse_bp_info, &target_rdev);
824 if (status == CSE_UPDATE_NOT_REQUIRED)
825 return CSE_NO_ERROR;
826 if (status == CSE_UPDATE_METADATA_ERROR)
827 return CSE_LITE_SKU_RW_METADATA_NOT_FOUND;
Rizwan Qureshiec321092019-09-06 20:28:43 +0530828
Furquan Shaikhc45e0be2021-10-06 23:28:03 -0700829 printk(BIOS_DEBUG, "cse_lite: CSE RW update is initiated\n");
830 return cse_trigger_fw_update(cse_bp_info, status, &target_rdev);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +0530831}
832
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530833static const char *cse_sub_part_str(enum bpdt_entry_type type)
834{
835 switch (type) {
836 case IOM_FW:
837 return "IOM";
838 case NPHY_FW:
839 return "NPHY";
840 default:
841 return "Unknown";
842 }
843}
844
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530845static enum cb_err cse_locate_area_as_rdev_rw(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530846 size_t bp, struct region_device *cse_rdev)
847{
848 struct region_device cse_region_rdev;
849 uint32_t size;
850 uint32_t start_offset;
851 uint32_t end_offset;
852
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530853 if (cse_get_rw_rdev(&cse_region_rdev) != CB_SUCCESS)
854 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530855
856 if (!strcmp(cse_regions[bp], "RO"))
857 cse_get_bp_entry_range(cse_bp_info, RO, &start_offset, &end_offset);
858 else
859 cse_get_bp_entry_range(cse_bp_info, RW, &start_offset, &end_offset);
860
861 size = end_offset + 1 - start_offset;
862
863 if (rdev_chain(cse_rdev, &cse_region_rdev, start_offset, size))
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530864 return CB_ERR;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530865
866 printk(BIOS_DEBUG, "cse_lite: CSE %s partition: offset = 0x%x, size = 0x%x\n",
867 cse_regions[bp], start_offset, size);
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530868 return CB_SUCCESS;
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530869}
870
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530871static enum cb_err cse_sub_part_get_target_rdev(const struct cse_bp_info *cse_bp_info,
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530872 struct region_device *target_rdev, size_t bp, enum bpdt_entry_type type)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530873{
874 struct bpdt_header bpdt_hdr;
875 struct region_device cse_rdev;
876 struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
877 uint8_t i;
878
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530879 if (cse_locate_area_as_rdev_rw(cse_bp_info, bp, &cse_rdev) != CB_SUCCESS) {
Sridhar Siricilla52479c72022-03-30 09:25:49 +0530880 printk(BIOS_ERR, "cse_lite: Failed to locate %s in the CSE Region\n",
881 cse_regions[bp]);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530882 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530883 }
884
885 if ((rdev_readat(&cse_rdev, &bpdt_hdr, 0, BPDT_HEADER_SZ)) != BPDT_HEADER_SZ) {
886 printk(BIOS_ERR, "cse_lite: Failed to read BPDT header from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530887 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530888 }
889
890 if ((rdev_readat(&cse_rdev, bpdt_entries, BPDT_HEADER_SZ,
891 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ))) !=
892 (bpdt_hdr.descriptor_count * BPDT_ENTRY_SZ)) {
893 printk(BIOS_ERR, "cse_lite: Failed to read BPDT entries from CSE region\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530894 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530895 }
896
897 /* walk through BPDT entries to identify sub-partition's payload offset and size */
898 for (i = 0; i < bpdt_hdr.descriptor_count; i++) {
899 if (bpdt_entries[i].type == type) {
900 printk(BIOS_INFO, "cse_lite: Sub-partition %s- offset = 0x%x,"
901 "size = 0x%x\n", cse_sub_part_str(type), bpdt_entries[i].offset,
902 bpdt_entries[i].size);
903
904 if (rdev_chain(target_rdev, &cse_rdev, bpdt_entries[i].offset,
905 bpdt_entries[i].size))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530906 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530907 else
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530908 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530909 }
910 }
911
912 printk(BIOS_ERR, "cse_lite: Sub-partition %s is not found\n", cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530913 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530914}
915
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530916static enum cb_err cse_get_sub_part_fw_version(enum bpdt_entry_type type,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530917 const struct region_device *rdev,
918 struct fw_version *fw_ver)
919{
920 struct subpart_entry subpart_entry;
921 struct subpart_entry_manifest_header man_hdr;
922
923 if ((rdev_readat(rdev, &subpart_entry, SUBPART_HEADER_SZ, SUBPART_ENTRY_SZ))
924 != SUBPART_ENTRY_SZ) {
925 printk(BIOS_ERR, "cse_lite: Failed to read %s sub partition entry\n",
926 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530927 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530928 }
929
930 if ((rdev_readat(rdev, &man_hdr, subpart_entry.offset_bytes, SUBPART_MANIFEST_HDR_SZ))
931 != SUBPART_MANIFEST_HDR_SZ) {
932 printk(BIOS_ERR, "cse_lite: Failed to read %s Sub part entry #0 manifest\n",
933 cse_sub_part_str(type));
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530934 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530935 }
936
937 fw_ver->major = man_hdr.binary_version.major;
938 fw_ver->minor = man_hdr.binary_version.minor;
939 fw_ver->hotfix = man_hdr.binary_version.hotfix;
940 fw_ver->build = man_hdr.binary_version.build;
941
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530942 return CB_SUCCESS;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530943}
944
945static void cse_sub_part_get_source_fw_version(void *subpart_cbfs_rw, struct fw_version *fw_ver)
946{
947 uint8_t *ptr = (uint8_t *)subpart_cbfs_rw;
948 struct subpart_entry *subpart_entry;
949 struct subpart_entry_manifest_header *man_hdr;
950
Elyes Haouas9018dee2022-11-18 15:07:33 +0100951 subpart_entry = (struct subpart_entry *)(ptr + SUBPART_HEADER_SZ);
952 man_hdr = (struct subpart_entry_manifest_header *)(ptr + subpart_entry->offset_bytes);
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530953
954 fw_ver->major = man_hdr->binary_version.major;
955 fw_ver->minor = man_hdr->binary_version.minor;
956 fw_ver->hotfix = man_hdr->binary_version.hotfix;
957 fw_ver->build = man_hdr->binary_version.build;
958}
959
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530960static enum cb_err cse_prep_for_component_update(const struct cse_bp_info *cse_bp_info)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530961{
962 /*
963 * To set CSE's operation mode to HMRFPO mode:
964 * 1. Ensure CSE to boot from RO(BP1)
965 * 2. Send HMRFPO_ENABLE command to CSE
966 */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530967 if (cse_boot_to_ro(cse_bp_info) != CB_SUCCESS)
968 return CB_ERR;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530969
970 return cse_hmrfpo_enable();
971}
972
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +0530973static enum csme_failure_reason cse_sub_part_trigger_update(enum bpdt_entry_type type,
974 uint8_t bp, const void *subpart_cbfs_rw, const size_t blob_sz,
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530975 struct region_device *target_rdev)
976{
977 if (region_device_sz(target_rdev) < blob_sz) {
978 printk(BIOS_ERR, "cse_lite: %s Target sub-partition size: %zx, "
979 "smaller than blob size:%zx, abort update\n",
980 cse_sub_part_str(type), region_device_sz(target_rdev), blob_sz);
981 return CSE_LITE_SKU_SUB_PART_LAYOUT_MISMATCH_ERROR;
982 }
983
984 /* Erase CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530985 if (cse_erase_rw_region(target_rdev) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530986 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
987
988 /* Update CSE Lite sub-partition */
Sridhar Siricilla2e6c5592023-01-10 16:16:57 +0530989 if (cse_copy_rw(target_rdev, (void *)subpart_cbfs_rw, 0, blob_sz) != CB_SUCCESS)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530990 return CSE_LITE_SKU_SUB_PART_UPDATE_FAIL;
991
992 printk(BIOS_INFO, "cse_lite: CSE %s %s Update successful\n", GET_BP_STR(bp),
993 cse_sub_part_str(type));
994
995 return CSE_LITE_SKU_PART_UPDATE_SUCCESS;
996}
997
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +0530998static enum csme_failure_reason handle_cse_sub_part_fw_update_rv(enum csme_failure_reason rv)
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +0530999{
1000 switch (rv) {
1001 case CSE_LITE_SKU_PART_UPDATE_SUCCESS:
1002 case CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ:
1003 return rv;
1004 default:
1005 cse_trigger_vboot_recovery(rv);
1006 }
1007 /* Control never reaches here */
1008 return rv;
1009}
1010
1011static enum csme_failure_reason cse_sub_part_fw_component_update(enum bpdt_entry_type type,
1012 const struct cse_bp_info *cse_bp_info, const char *name)
1013{
1014 struct region_device target_rdev;
1015 struct fw_version target_fw_ver, source_fw_ver;
1016 enum csme_failure_reason rv;
1017 size_t size;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301018
1019 void *subpart_cbfs_rw = cbfs_map(name, &size);
1020 if (!subpart_cbfs_rw) {
1021 printk(BIOS_ERR, "cse_lite: Not able to map %s CBFS file\n",
1022 cse_sub_part_str(type));
1023 return CSE_LITE_SKU_SUB_PART_BLOB_ACCESS_ERR;
1024 }
1025
1026 cse_sub_part_get_source_fw_version(subpart_cbfs_rw, &source_fw_ver);
1027 printk(BIOS_INFO, "cse_lite: CBFS %s FW Version: %x.%x.%x.%x\n", cse_sub_part_str(type),
1028 source_fw_ver.major, source_fw_ver.minor, source_fw_ver.hotfix,
1029 source_fw_ver.build);
1030
1031 /* Trigger sub-partition update in CSE RO and CSE RW */
1032 for (size_t bp = 0; bp < ARRAY_SIZE(cse_regions); bp++) {
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301033 if (cse_sub_part_get_target_rdev(cse_bp_info, &target_rdev, bp, type) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301034 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1035 goto error_exit;
1036 }
1037
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301038 if (cse_get_sub_part_fw_version(type, &target_rdev, &target_fw_ver) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301039 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1040 goto error_exit;
1041 }
1042
1043 printk(BIOS_INFO, "cse_lite: %s %s FW Version: %x.%x.%x.%x\n", cse_regions[bp],
1044 cse_sub_part_str(type), target_fw_ver.major,
1045 target_fw_ver.minor, target_fw_ver.hotfix, target_fw_ver.build);
1046
1047 if (!cse_compare_sub_part_version(&target_fw_ver, &source_fw_ver)) {
1048 printk(BIOS_INFO, "cse_lite: %s %s update is not required\n",
1049 cse_regions[bp], cse_sub_part_str(type));
1050 rv = CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1051 continue;
1052 }
1053
1054 printk(BIOS_INFO, "CSE %s %s Update initiated\n", GET_BP_STR(bp),
1055 cse_sub_part_str(type));
1056
Sridhar Siricillaad6d3122023-01-10 14:59:35 +05301057 if (cse_prep_for_component_update(cse_bp_info) != CB_SUCCESS) {
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301058 rv = CSE_LITE_SKU_SUB_PART_ACCESS_ERR;
1059 goto error_exit;
1060 }
1061
1062 rv = cse_sub_part_trigger_update(type, bp, subpart_cbfs_rw,
1063 size, &target_rdev);
1064
1065 if (rv != CSE_LITE_SKU_PART_UPDATE_SUCCESS)
1066 goto error_exit;
1067 }
1068error_exit:
1069 cbfs_unmap(subpart_cbfs_rw);
1070 return rv;
1071}
1072
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301073static 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 +05301074{
1075 if (skip_cse_sub_part_update()) {
1076 printk(BIOS_INFO, "CSE Sub-partition update not required\n");
1077 return CSE_LITE_SKU_SUB_PART_UPDATE_NOT_REQ;
1078 }
1079
Sridhar Siricilla0ae7a8b2023-01-10 17:12:01 +05301080 enum csme_failure_reason rv;
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301081 rv = cse_sub_part_fw_component_update(IOM_FW, cse_bp_info,
1082 CONFIG_SOC_INTEL_CSE_IOM_CBFS_NAME);
1083
1084 handle_cse_sub_part_fw_update_rv(rv);
1085
1086 rv = cse_sub_part_fw_component_update(NPHY_FW, cse_bp_info,
1087 CONFIG_SOC_INTEL_CSE_NPHY_CBFS_NAME);
1088
1089 return handle_cse_sub_part_fw_update_rv(rv);
1090}
1091
Subrata Banik5ff01182023-04-20 11:08:17 +05301092static void do_cse_fw_sync(void)
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301093{
1094 static struct get_bp_info_rsp cse_bp_info;
1095
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301096 /*
1097 * If system is in recovery mode, skip CSE Lite update if CSE sub-partition update
1098 * is not enabled and continue to update CSE sub-partitions.
1099 */
1100 if (vboot_recovery_mode_enabled() && !CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE)) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301101 printk(BIOS_DEBUG, "cse_lite: Skip switching to RW in the recovery path\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301102 return;
1103 }
1104
Sridhar Siricilla99dbca32020-05-12 21:05:04 +05301105 /* If CSE SKU type is not Lite, skip enabling CSE Lite SKU */
1106 if (!cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301107 printk(BIOS_ERR, "cse_lite: Not a CSE Lite SKU\n");
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301108 return;
1109 }
1110
Sridhar Siricilla4b6e8ca2023-01-10 14:43:18 +05301111 if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301112 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301113
1114 /* If system is in recovery mode, don't trigger recovery again */
1115 if (!vboot_recovery_mode_enabled()) {
1116 cse_trigger_vboot_recovery(CSE_COMMUNICATION_ERROR);
1117 } else {
1118 printk(BIOS_ERR, "cse_lite: System is already in Recovery Mode, "
1119 "so no action\n");
1120 return;
1121 }
1122 }
1123
1124 /*
1125 * If system is in recovery mode, CSE Lite update has to be skipped but CSE
Alexander Goncharov893c3ae82023-02-04 15:20:37 +04001126 * sub-partitions like NPHY and IOM have to be updated. If CSE sub-parition update
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301127 * fails during recovery, just continue to boot.
1128 */
1129 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE) && vboot_recovery_mode_enabled()) {
1130 if (cse_sub_part_fw_update(&cse_bp_info.bp_info) ==
1131 CSE_LITE_SKU_PART_UPDATE_SUCCESS) {
1132 cse_board_reset();
1133 do_global_reset();
1134 die("ERROR: GLOBAL RESET Failed to reset the system\n");
1135 }
1136
1137 return;
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301138 }
1139
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301140 if (cse_fix_data_failure_err(&cse_bp_info.bp_info) != CB_SUCCESS)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001141 cse_trigger_vboot_recovery(CSE_LITE_SKU_DATA_WIPE_ERROR);
Sridhar Siricilla2f6d5552020-04-19 23:39:02 +05301142
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301143 /*
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301144 * cse firmware update is skipped if SOC_INTEL_CSE_RW_UPDATE is not defined and
1145 * runtime debug control flag is not enabled. The driver triggers recovery if CSE CBFS
1146 * RW metadata or CSE CBFS RW blob is not available.
Sridhar Siricilla361e3642020-10-18 20:14:07 +05301147 */
Sridhar Siricilla0aa1ac42022-03-09 20:35:32 +05301148 if (is_cse_fw_update_enabled()) {
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301149 uint8_t rv;
1150 rv = cse_fw_update(&cse_bp_info.bp_info);
1151 if (rv)
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001152 cse_trigger_vboot_recovery(rv);
Sridhar Siricilla4c2890d2020-12-09 00:28:30 +05301153 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301154
Krishna Prasad Bhat333edcc2021-11-26 06:52:27 +05301155 if (CONFIG(SOC_INTEL_CSE_SUB_PART_UPDATE))
1156 cse_sub_part_fw_update(&cse_bp_info.bp_info);
1157
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301158 if (!cse_is_rw_bp_status_valid(&cse_bp_info.bp_info))
Tim Wawrzynczakf2801f42021-06-22 11:25:14 -06001159 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_JUMP_ERROR);
Sridahr Siricilla54b03562021-06-18 10:59:30 +05301160
Sridhar Siricilla8f9c1532023-01-10 14:38:51 +05301161 if (cse_boot_to_rw(&cse_bp_info.bp_info) != CB_SUCCESS) {
Sridhar Siricilla9f71b172020-06-01 14:50:52 +05301162 printk(BIOS_ERR, "cse_lite: Failed to switch to RW\n");
Tim Wawrzynczak09635f42021-06-18 10:08:47 -06001163 cse_trigger_vboot_recovery(CSE_LITE_SKU_RW_SWITCH_ERROR);
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301164 }
Sridhar Siricillaf87ff332019-09-12 17:18:20 +05301165}
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301166
Subrata Banik5ff01182023-04-20 11:08:17 +05301167void cse_fw_sync(void)
1168{
1169 timestamp_add_now(TS_CSE_FW_SYNC_START);
1170 do_cse_fw_sync();
1171 timestamp_add_now(TS_CSE_FW_SYNC_END);
1172}
1173
Subrata Banikfc313d62023-04-14 01:31:29 +05301174/*
1175 * Helper function that stores current CSE firmware version to CBMEM memory,
1176 * except during recovery mode.
1177 */
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301178static void cse_store_rw_fw_version(void)
Subrata Banikfc313d62023-04-14 01:31:29 +05301179{
1180 if (vboot_recovery_mode_enabled())
1181 return;
1182
1183 struct get_bp_info_rsp cse_bp_info;
Dinesh Gehlot8c53e6a2023-05-08 05:15:53 +00001184 if (cse_get_bp_info(&cse_bp_info) != CB_SUCCESS) {
Subrata Banikfc313d62023-04-14 01:31:29 +05301185 printk(BIOS_ERR, "cse_lite: Failed to get CSE boot partition info\n");
1186 return;
1187 }
1188 const struct cse_bp_entry *cse_bp = cse_get_bp_entry(RW, &cse_bp_info.bp_info);
1189 struct cse_fw_partition_info *version;
1190 version = cbmem_add(CBMEM_ID_CSE_PARTITION_VERSION, sizeof(*version));
1191 memcpy(&(version->cur_cse_fw_version), &(cse_bp->fw_ver), sizeof(struct fw_version));
1192}
1193
Subrata Banik7f66adb2023-04-14 00:35:38 +05301194static enum cb_err send_get_fpt_partition_info_cmd(enum fpt_partition_id id,
1195 struct fw_version_resp *resp)
1196{
1197 enum cse_tx_rx_status ret;
1198 struct fw_version_msg {
1199 struct mkhi_hdr hdr;
1200 enum fpt_partition_id partition_id;
1201 } __packed msg = {
1202 .hdr = {
1203 .group_id = MKHI_GROUP_ID_GEN,
1204 .command = GEN_GET_IMAGE_FW_VERSION,
1205 },
1206 .partition_id = id,
1207 };
1208
1209 /*
1210 * Prerequisites:
1211 * 1) HFSTS1 CWS is Normal
1212 * 2) HFSTS1 COM is Normal
1213 * 3) Only sent after DID (accomplished by compiling this into ramstage)
1214 */
1215
1216 if (cse_is_hfs1_com_soft_temp_disable() || !cse_is_hfs1_cws_normal() ||
1217 !cse_is_hfs1_com_normal()) {
1218 printk(BIOS_ERR,
1219 "HECI: Prerequisites not met for Get Image Firmware Version command\n");
1220 return CB_ERR;
1221 }
1222
1223 size_t resp_size = sizeof(struct fw_version_resp);
1224 ret = heci_send_receive(&msg, sizeof(msg), resp, &resp_size, HECI_MKHI_ADDR);
1225
1226 if (ret || resp->hdr.result) {
1227 printk(BIOS_ERR, "CSE: Failed to get partition information for %d: 0x%x\n",
1228 id, resp->hdr.result);
1229 return CB_ERR;
1230 }
1231
1232 return CB_SUCCESS;
1233}
1234
Subrata Banik044fc9f2023-04-14 02:34:37 +05301235static enum cb_err cse_get_fpt_partition_info(enum fpt_partition_id id,
1236 struct fw_version_resp *resp)
Subrata Banik7f66adb2023-04-14 00:35:38 +05301237{
1238 if (vboot_recovery_mode_enabled()) {
1239 printk(BIOS_WARNING,
1240 "CSE: Skip sending Get Image Info command during recovery mode!\n");
1241 return CB_ERR;
1242 }
1243
1244 if (id == FPT_PARTITION_NAME_ISHC && !CONFIG(DRIVERS_INTEL_ISH)) {
1245 printk(BIOS_WARNING, "CSE: Info request denied, no ISH partition\n");
1246 return CB_ERR;
1247 }
1248
1249 return send_get_fpt_partition_info_cmd(id, resp);
1250}
1251
Subrata Banikb1b7c532023-04-14 01:36:13 +05301252/*
1253 * Helper function to read ISH version from CSE FPT using HECI command.
1254 *
1255 * The HECI command only be executed after memory has been initialized.
1256 * This is because the command relies on resources that are not available
1257 * until DRAM initialization command has been sent.
1258 */
1259static void store_ish_version(void)
1260{
1261 if (!ENV_RAMSTAGE)
1262 return;
1263
1264 if (vboot_recovery_mode_enabled())
1265 return;
1266
1267 struct cse_fw_partition_info *version;
1268 size_t size = sizeof(struct fw_version);
1269 version = cbmem_find(CBMEM_ID_CSE_PARTITION_VERSION);
1270 if (version == NULL)
1271 return;
1272
1273 /*
1274 * Compare if stored cse version (from the previous boot) is same as current
1275 * running cse version.
1276 */
1277 if (memcmp(&version->ish_partition_info.prev_cse_fw_version,
1278 &version->cur_cse_fw_version, sizeof(struct fw_version))) {
1279 /*
1280 * Current running CSE version is different than previous stored CSE version
1281 * which could be due to CSE update or rollback, hence, need to send ISHC
1282 * partition info cmd to know the currently running ISH version.
1283 */
1284
1285 struct fw_version_resp resp;
1286 if (cse_get_fpt_partition_info(FPT_PARTITION_NAME_ISHC, &resp) == CB_SUCCESS) {
1287 /* Update stored cse version with current version */
1288 memcpy(&(version->ish_partition_info.prev_cse_fw_version),
1289 &(version->cur_cse_fw_version), size);
1290
1291 /* Since cse version has been updated, ish version needs to be updated. */
1292 memcpy(&(version->ish_partition_info.cur_ish_fw_version),
1293 &(resp.manifest_data.version), size);
1294 }
1295 }
1296}
1297
Subrata Banikfc313d62023-04-14 01:31:29 +05301298static void ramstage_cse_misc_ops(void *unused)
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301299{
Subrata Banikdb7b35a2023-04-19 20:48:01 +05301300 if (acpi_get_sleep_type() == ACPI_S3)
1301 return;
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301302
Subrata Banik5ff01182023-04-20 11:08:17 +05301303 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_IN_RAMSTAGE))
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301304 cse_fw_sync();
Subrata Banikfc313d62023-04-14 01:31:29 +05301305
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301306 /* Store the CSE RW Firmware Version into CBMEM */
Subrata Banik2172a632023-06-22 01:00:06 +05301307 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1308 cse_store_rw_fw_version();
Subrata Banikfc313d62023-04-14 01:31:29 +05301309 /*
Subrata Banik3c06f1e2023-06-13 02:12:30 +05301310 * Store the ISH RW Firmware Version into CBMEM if ISH partition
Subrata Banikfc313d62023-04-14 01:31:29 +05301311 * is available
1312 */
Subrata Banikf27a41f2023-06-13 10:32:09 +05301313 if (soc_is_ish_partition_enabled())
Subrata Banikb1b7c532023-04-14 01:36:13 +05301314 store_ish_version();
Krishna P Bhat De3178a12022-04-21 16:40:06 +05301315}
1316
Subrata Banikfc313d62023-04-14 01:31:29 +05301317BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, ramstage_cse_misc_ops, NULL);