Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2022, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <dm.h> |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 7 | #include <efi.h> |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 8 | #include <efi_loader.h> |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 9 | #include <efi_variable.h> |
| 10 | #include <event.h> |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 11 | #include <fwu.h> |
| 12 | #include <fwu_mdata.h> |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/types.h> |
| 17 | |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
| 19 | |
| 20 | static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */ |
| 21 | static struct udevice *g_dev; |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 22 | static u8 in_trial; |
| 23 | static u8 boottime_check; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 24 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 25 | enum { |
| 26 | IMAGE_ACCEPT_SET = 1, |
| 27 | IMAGE_ACCEPT_CLEAR, |
| 28 | }; |
| 29 | |
| 30 | enum { |
| 31 | PRIMARY_PART = 1, |
| 32 | SECONDARY_PART, |
| 33 | BOTH_PARTS, |
| 34 | }; |
| 35 | |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 36 | static int trial_counter_update(u16 *trial_state_ctr) |
| 37 | { |
| 38 | bool delete; |
| 39 | u32 var_attr; |
| 40 | efi_status_t status; |
| 41 | efi_uintn_t var_size; |
| 42 | |
| 43 | delete = !trial_state_ctr ? true : false; |
| 44 | var_size = !trial_state_ctr ? 0 : (efi_uintn_t)sizeof(*trial_state_ctr); |
| 45 | var_attr = !trial_state_ctr ? 0 : EFI_VARIABLE_NON_VOLATILE | |
| 46 | EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 47 | status = efi_set_variable_int(u"TrialStateCtr", |
| 48 | &efi_global_variable_guid, |
| 49 | var_attr, |
| 50 | var_size, trial_state_ctr, false); |
| 51 | |
| 52 | if ((delete && (status != EFI_NOT_FOUND && |
| 53 | status != EFI_SUCCESS)) || |
| 54 | (!delete && status != EFI_SUCCESS)) |
| 55 | return -1; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int trial_counter_read(u16 *trial_state_ctr) |
| 61 | { |
| 62 | efi_status_t status; |
| 63 | efi_uintn_t var_size; |
| 64 | |
| 65 | var_size = (efi_uintn_t)sizeof(trial_state_ctr); |
| 66 | status = efi_get_variable_int(u"TrialStateCtr", |
| 67 | &efi_global_variable_guid, |
| 68 | NULL, |
| 69 | &var_size, trial_state_ctr, |
| 70 | NULL); |
| 71 | if (status != EFI_SUCCESS) { |
| 72 | log_err("Unable to read TrialStateCtr variable\n"); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int fwu_trial_count_update(void) |
| 80 | { |
| 81 | int ret; |
| 82 | u16 trial_state_ctr; |
| 83 | |
| 84 | ret = trial_counter_read(&trial_state_ctr); |
| 85 | if (ret) { |
| 86 | log_debug("Unable to read trial_state_ctr\n"); |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | ++trial_state_ctr; |
| 91 | if (trial_state_ctr > CONFIG_FWU_TRIAL_STATE_CNT) { |
| 92 | log_info("Trial State count exceeded. Revert back to previous_active_index\n"); |
| 93 | ret = fwu_revert_boot_index(); |
| 94 | if (ret) |
| 95 | log_err("Unable to revert active_index\n"); |
| 96 | ret = 1; |
| 97 | } else { |
Michal Simek | b378fdd | 2023-07-14 10:47:06 +0200 | [diff] [blame] | 98 | log_info("Trial State count: attempt %d out of %d\n", |
| 99 | trial_state_ctr, CONFIG_FWU_TRIAL_STATE_CNT); |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 100 | ret = trial_counter_update(&trial_state_ctr); |
| 101 | if (ret) |
| 102 | log_err("Unable to increment TrialStateCtr variable\n"); |
| 103 | } |
| 104 | |
| 105 | out: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static int in_trial_state(struct fwu_mdata *mdata) |
| 110 | { |
| 111 | u32 i, active_bank; |
| 112 | struct fwu_image_entry *img_entry; |
| 113 | struct fwu_image_bank_info *img_bank_info; |
| 114 | |
| 115 | active_bank = mdata->active_index; |
| 116 | img_entry = &mdata->img_entry[0]; |
| 117 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 118 | img_bank_info = &img_entry[i].img_bank_info[active_bank]; |
| 119 | if (!img_bank_info->accepted) { |
| 120 | log_info("System booting in Trial State\n"); |
| 121 | return 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 128 | static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id) |
| 129 | { |
| 130 | u8 index; |
| 131 | int i; |
| 132 | struct efi_fw_image *image; |
| 133 | |
| 134 | index = *image_index; |
| 135 | image = update_info.images; |
Masahisa Kojima | cccea18 | 2023-06-07 14:41:51 +0900 | [diff] [blame] | 136 | for (i = 0; i < update_info.num_images; i++) { |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 137 | if (index == image[i].image_index) { |
| 138 | guidcpy(image_type_id, &image[i].image_type_id); |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return -ENOENT; |
| 144 | } |
| 145 | |
| 146 | /** |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 147 | * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided |
| 148 | * @mdata: FWU metadata structure |
| 149 | * @part: Bitmask of FWU metadata partitions to be written to |
| 150 | * |
| 151 | * Return: 0 if OK, -ve on error |
| 152 | */ |
| 153 | static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) |
| 154 | { |
| 155 | void *buf = &mdata->version; |
| 156 | int err; |
| 157 | |
| 158 | if (part == BOTH_PARTS) { |
| 159 | err = fwu_sync_mdata(mdata, SECONDARY_PART); |
| 160 | if (err) |
| 161 | return err; |
| 162 | part = PRIMARY_PART; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Calculate the crc32 for the updated FWU metadata |
| 167 | * and put the updated value in the FWU metadata crc32 |
| 168 | * field |
| 169 | */ |
| 170 | mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); |
| 171 | |
| 172 | err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART); |
| 173 | if (err) { |
| 174 | log_err("Unable to write %s mdata\n", |
| 175 | part == PRIMARY_PART ? "primary" : "secondary"); |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | /* update the cached copy of meta-data */ |
| 180 | memcpy(&g_mdata, mdata, sizeof(struct fwu_mdata)); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static inline int mdata_crc_check(struct fwu_mdata *mdata) |
| 186 | { |
| 187 | void *buf = &mdata->version; |
| 188 | u32 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); |
| 189 | |
| 190 | return calc_crc32 == mdata->crc32 ? 0 : -EINVAL; |
| 191 | } |
| 192 | |
| 193 | /** |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 194 | * fwu_get_mdata() - Read, verify and return the FWU metadata |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 195 | * @mdata: Output FWU metadata read or NULL |
| 196 | * |
| 197 | * Read both the metadata copies from the storage media, verify their checksum, |
| 198 | * and ascertain that both copies match. If one of the copies has gone bad, |
| 199 | * restore it from the good copy. |
| 200 | * |
| 201 | * Return: 0 if OK, -ve on error |
| 202 | */ |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 203 | int fwu_get_mdata(struct fwu_mdata *mdata) |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 204 | { |
| 205 | int err; |
| 206 | bool parts_ok[2] = { false }; |
| 207 | struct fwu_mdata s, *parts_mdata[2]; |
| 208 | |
| 209 | parts_mdata[0] = &g_mdata; |
| 210 | parts_mdata[1] = &s; |
| 211 | |
| 212 | /* if mdata already read and ready */ |
| 213 | err = mdata_crc_check(parts_mdata[0]); |
| 214 | if (!err) |
| 215 | goto ret_mdata; |
| 216 | /* else read, verify and, if needed, fix mdata */ |
| 217 | |
| 218 | for (int i = 0; i < 2; i++) { |
| 219 | parts_ok[i] = false; |
| 220 | err = fwu_read_mdata(g_dev, parts_mdata[i], !i); |
| 221 | if (!err) { |
| 222 | err = mdata_crc_check(parts_mdata[i]); |
| 223 | if (!err) |
| 224 | parts_ok[i] = true; |
| 225 | else |
| 226 | log_debug("mdata : %s crc32 failed\n", i ? "secondary" : "primary"); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (parts_ok[0] && parts_ok[1]) { |
| 231 | /* |
| 232 | * Before returning, check that both the |
| 233 | * FWU metadata copies are the same. |
| 234 | */ |
| 235 | err = memcmp(parts_mdata[0], parts_mdata[1], sizeof(struct fwu_mdata)); |
| 236 | if (!err) |
| 237 | goto ret_mdata; |
| 238 | |
| 239 | /* |
| 240 | * If not, populate the secondary partition from the |
| 241 | * primary partition copy. |
| 242 | */ |
| 243 | log_info("Both FWU metadata copies are valid but do not match."); |
| 244 | log_info(" Restoring the secondary partition from the primary\n"); |
| 245 | parts_ok[1] = false; |
| 246 | } |
| 247 | |
| 248 | for (int i = 0; i < 2; i++) { |
| 249 | if (parts_ok[i]) |
| 250 | continue; |
| 251 | |
| 252 | memcpy(parts_mdata[i], parts_mdata[1 - i], sizeof(struct fwu_mdata)); |
| 253 | err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART); |
| 254 | if (err) { |
| 255 | log_debug("mdata : %s write failed\n", i ? "secondary" : "primary"); |
| 256 | return err; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | ret_mdata: |
| 261 | if (!err && mdata) |
| 262 | memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata)); |
| 263 | |
| 264 | return err; |
| 265 | } |
| 266 | |
| 267 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 268 | * fwu_get_active_index() - Get active_index from the FWU metadata |
| 269 | * @active_idx: active_index value to be read |
| 270 | * |
| 271 | * Read the active_index field from the FWU metadata and place it in |
| 272 | * the variable pointed to be the function argument. |
| 273 | * |
| 274 | * Return: 0 if OK, -ve on error |
| 275 | * |
| 276 | */ |
| 277 | int fwu_get_active_index(uint *active_idx) |
| 278 | { |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 279 | int ret = 0; |
| 280 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * Found the FWU metadata partition, now read the active_index |
| 284 | * value |
| 285 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 286 | *active_idx = mdata->active_index; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 287 | if (*active_idx >= CONFIG_FWU_NUM_BANKS) { |
| 288 | log_debug("Active index value read is incorrect\n"); |
| 289 | ret = -EINVAL; |
| 290 | } |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * fwu_set_active_index() - Set active_index in the FWU metadata |
| 297 | * @active_idx: active_index value to be set |
| 298 | * |
| 299 | * Update the active_index field in the FWU metadata |
| 300 | * |
| 301 | * Return: 0 if OK, -ve on error |
| 302 | * |
| 303 | */ |
| 304 | int fwu_set_active_index(uint active_idx) |
| 305 | { |
| 306 | int ret; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 307 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 308 | |
| 309 | if (active_idx >= CONFIG_FWU_NUM_BANKS) { |
| 310 | log_debug("Invalid active index value\n"); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 314 | /* |
| 315 | * Update the active index and previous_active_index fields |
| 316 | * in the FWU metadata |
| 317 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 318 | mdata->previous_active_index = mdata->active_index; |
| 319 | mdata->active_index = active_idx; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Now write this updated FWU metadata to both the |
| 323 | * FWU metadata partitions |
| 324 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 325 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 326 | if (ret) { |
| 327 | log_debug("Failed to update FWU metadata partitions\n"); |
| 328 | ret = -EIO; |
| 329 | } |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * fwu_get_image_index() - Get the Image Index to be used for capsule update |
| 336 | * @image_index: The Image Index for the image |
| 337 | * |
| 338 | * The FWU multi bank update feature computes the value of image_index at |
| 339 | * runtime, based on the bank to which the image needs to be written to. |
| 340 | * Derive the image_index value for the image. |
| 341 | * |
| 342 | * Currently, the capsule update driver uses the DFU framework for |
| 343 | * the updates. This function gets the DFU alt number which is to |
| 344 | * be used as the Image Index |
| 345 | * |
| 346 | * Return: 0 if OK, -ve on error |
| 347 | * |
| 348 | */ |
| 349 | int fwu_get_image_index(u8 *image_index) |
| 350 | { |
| 351 | int ret, i; |
| 352 | u8 alt_num; |
| 353 | uint update_bank; |
| 354 | efi_guid_t *image_guid, image_type_id; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 355 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 356 | struct fwu_image_entry *img_entry; |
| 357 | struct fwu_image_bank_info *img_bank_info; |
| 358 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 359 | ret = fwu_plat_get_update_index(&update_bank); |
| 360 | if (ret) { |
| 361 | log_debug("Failed to get the FWU update bank\n"); |
| 362 | goto out; |
| 363 | } |
| 364 | |
| 365 | ret = fwu_get_image_type_id(image_index, &image_type_id); |
| 366 | if (ret) { |
| 367 | log_debug("Unable to get image_type_id for image_index %u\n", |
| 368 | *image_index); |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | ret = -EINVAL; |
| 373 | /* |
| 374 | * The FWU metadata has been read. Now get the image_uuid for the |
| 375 | * image with the update_bank. |
| 376 | */ |
| 377 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 378 | if (!guidcmp(&image_type_id, |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 379 | &mdata->img_entry[i].image_type_uuid)) { |
| 380 | img_entry = &mdata->img_entry[i]; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 381 | img_bank_info = &img_entry->img_bank_info[update_bank]; |
| 382 | image_guid = &img_bank_info->image_uuid; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 383 | ret = fwu_plat_get_alt_num(g_dev, image_guid, &alt_num); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 384 | if (ret) { |
| 385 | log_debug("alt_num not found for partition with GUID %pUs\n", |
| 386 | image_guid); |
| 387 | } else { |
| 388 | log_debug("alt_num %d for partition %pUs\n", |
| 389 | alt_num, image_guid); |
| 390 | *image_index = alt_num + 1; |
| 391 | } |
| 392 | |
| 393 | goto out; |
| 394 | } |
| 395 | } |
| 396 | |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 397 | log_err("Partition with the image type %pUs not found\n", |
| 398 | &image_type_id); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 399 | |
| 400 | out: |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * fwu_revert_boot_index() - Revert the active index in the FWU metadata |
| 406 | * |
| 407 | * Revert the active_index value in the FWU metadata, by swapping the values |
| 408 | * of active_index and previous_active_index in both copies of the |
| 409 | * FWU metadata. |
| 410 | * |
| 411 | * Return: 0 if OK, -ve on error |
| 412 | * |
| 413 | */ |
| 414 | int fwu_revert_boot_index(void) |
| 415 | { |
| 416 | int ret; |
| 417 | u32 cur_active_index; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 418 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Swap the active index and previous_active_index fields |
| 422 | * in the FWU metadata |
| 423 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 424 | cur_active_index = mdata->active_index; |
| 425 | mdata->active_index = mdata->previous_active_index; |
| 426 | mdata->previous_active_index = cur_active_index; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * Now write this updated FWU metadata to both the |
| 430 | * FWU metadata partitions |
| 431 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 432 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 433 | if (ret) { |
| 434 | log_debug("Failed to update FWU metadata partitions\n"); |
| 435 | ret = -EIO; |
| 436 | } |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image |
| 443 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 444 | * set or cleared |
| 445 | * @bank: Bank of which the image's Accept bit is to be set or cleared |
| 446 | * @action: Action which specifies whether image's Accept bit is to be set or |
| 447 | * cleared |
| 448 | * |
| 449 | * Set/Clear the accepted bit for the image specified by the img_guid parameter. |
| 450 | * This indicates acceptance or rejection of image for subsequent boots by some |
| 451 | * governing component like OS(or firmware). |
| 452 | * |
| 453 | * Return: 0 if OK, -ve on error |
| 454 | * |
| 455 | */ |
| 456 | static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action) |
| 457 | { |
| 458 | int ret, i; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 459 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 460 | struct fwu_image_entry *img_entry; |
| 461 | struct fwu_image_bank_info *img_bank_info; |
| 462 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 463 | img_entry = &mdata->img_entry[0]; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 464 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 465 | if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) { |
| 466 | img_bank_info = &img_entry[i].img_bank_info[bank]; |
| 467 | if (action == IMAGE_ACCEPT_SET) |
| 468 | img_bank_info->accepted |= FWU_IMAGE_ACCEPTED; |
| 469 | else |
| 470 | img_bank_info->accepted = 0; |
| 471 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 472 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 473 | goto out; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* Image not found */ |
| 478 | ret = -ENOENT; |
| 479 | |
| 480 | out: |
| 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * fwu_accept_image() - Set the Acceptance bit for the image |
| 486 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 487 | * cleared |
| 488 | * @bank: Bank of which the image's Accept bit is to be set |
| 489 | * |
| 490 | * Set the accepted bit for the image specified by the img_guid parameter. This |
| 491 | * indicates acceptance of image for subsequent boots by some governing component |
| 492 | * like OS(or firmware). |
| 493 | * |
| 494 | * Return: 0 if OK, -ve on error |
| 495 | * |
| 496 | */ |
| 497 | int fwu_accept_image(efi_guid_t *img_type_id, u32 bank) |
| 498 | { |
| 499 | return fwu_clrset_image_accept(img_type_id, bank, |
| 500 | IMAGE_ACCEPT_SET); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * fwu_clear_accept_image() - Clear the Acceptance bit for the image |
| 505 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 506 | * cleared |
| 507 | * @bank: Bank of which the image's Accept bit is to be cleared |
| 508 | * |
| 509 | * Clear the accepted bit for the image type specified by the img_type_id parameter. |
| 510 | * This function is called after the image has been updated. The accepted bit is |
| 511 | * cleared to be set subsequently after passing the image acceptance criteria, by |
| 512 | * either the OS(or firmware) |
| 513 | * |
| 514 | * Return: 0 if OK, -ve on error |
| 515 | * |
| 516 | */ |
| 517 | int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank) |
| 518 | { |
| 519 | return fwu_clrset_image_accept(img_type_id, bank, |
| 520 | IMAGE_ACCEPT_CLEAR); |
| 521 | } |
Sughosh Ganu | 7d6e2c5 | 2022-10-21 18:15:59 +0530 | [diff] [blame] | 522 | |
| 523 | /** |
| 524 | * fwu_plat_get_update_index() - Get the value of the update bank |
| 525 | * @update_idx: Bank number to which images are to be updated |
| 526 | * |
| 527 | * Get the value of the bank(partition) to which the update needs to be |
| 528 | * made. |
| 529 | * |
| 530 | * Note: This is a weak function and platforms can override this with |
| 531 | * their own implementation for selection of the update bank. |
| 532 | * |
| 533 | * Return: 0 if OK, -ve on error |
| 534 | * |
| 535 | */ |
| 536 | __weak int fwu_plat_get_update_index(uint *update_idx) |
| 537 | { |
| 538 | int ret; |
| 539 | u32 active_idx; |
| 540 | |
| 541 | ret = fwu_get_active_index(&active_idx); |
| 542 | if (ret < 0) |
| 543 | return -1; |
| 544 | |
| 545 | *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS; |
| 546 | |
| 547 | return ret; |
| 548 | } |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 549 | |
| 550 | /** |
Jassi Brar | a7e4541 | 2023-05-31 00:30:06 -0500 | [diff] [blame] | 551 | * fwu_plat_get_bootidx() - Get the value of the boot index |
| 552 | * @boot_idx: Boot index value |
| 553 | * |
| 554 | * Get the value of the bank(partition) from which the platform |
| 555 | * has booted. This value is passed to U-Boot from the earlier |
| 556 | * stage bootloader which loads and boots all the relevant |
| 557 | * firmware images |
| 558 | */ |
| 559 | __weak void fwu_plat_get_bootidx(uint *boot_idx) |
| 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | ret = fwu_get_active_index(boot_idx); |
| 564 | if (ret < 0) |
| 565 | *boot_idx = 0; /* Dummy value */ |
| 566 | } |
| 567 | |
| 568 | /** |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 569 | * fwu_update_checks_pass() - Check if FWU update can be done |
| 570 | * |
| 571 | * Check if the FWU update can be executed. The updates are |
| 572 | * allowed only when the platform is not in Trial State and |
| 573 | * the boot time checks have passed |
| 574 | * |
| 575 | * Return: 1 if OK, 0 if checks do not pass |
| 576 | * |
| 577 | */ |
| 578 | u8 fwu_update_checks_pass(void) |
| 579 | { |
| 580 | return !in_trial && boottime_check; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed |
| 585 | * |
| 586 | * Check if the empty capsule can be processed to either accept or revert |
| 587 | * an earlier executed update. The empty capsules need to be processed |
| 588 | * only when the platform is in Trial State and the boot time checks have |
| 589 | * passed |
| 590 | * |
| 591 | * Return: 1 if OK, 0 if not to be allowed |
| 592 | * |
| 593 | */ |
| 594 | u8 fwu_empty_capsule_checks_pass(void) |
| 595 | { |
| 596 | return in_trial && boottime_check; |
| 597 | } |
| 598 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 599 | /** |
| 600 | * fwu_trial_state_ctr_start() - Start the Trial State counter |
| 601 | * |
| 602 | * Start the counter to identify the platform booting in the |
| 603 | * Trial State. The counter is implemented as an EFI variable. |
| 604 | * |
| 605 | * Return: 0 if OK, -ve on error |
| 606 | * |
| 607 | */ |
| 608 | int fwu_trial_state_ctr_start(void) |
| 609 | { |
| 610 | int ret; |
| 611 | u16 trial_state_ctr; |
| 612 | |
| 613 | trial_state_ctr = 0; |
| 614 | ret = trial_counter_update(&trial_state_ctr); |
| 615 | if (ret) |
| 616 | log_err("Unable to initialise TrialStateCtr\n"); |
| 617 | |
| 618 | return ret; |
| 619 | } |
| 620 | |
Simon Glass | f72d0d4 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 621 | static int fwu_boottime_checks(void) |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 622 | { |
| 623 | int ret; |
| 624 | u32 boot_idx, active_idx; |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 625 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 626 | ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev); |
| 627 | if (ret) { |
| 628 | log_debug("Cannot find fwu device\n"); |
| 629 | return ret; |
| 630 | } |
| 631 | |
Marek Vasut | 95311f7 | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 632 | /* Don't have boot time checks on sandbox */ |
| 633 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
| 634 | boottime_check = 1; |
| 635 | return 0; |
| 636 | } |
| 637 | |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 638 | ret = fwu_get_mdata(NULL); |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 639 | if (ret) { |
| 640 | log_debug("Unable to read meta-data\n"); |
| 641 | return ret; |
| 642 | } |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Get the Boot Index, i.e. the bank from |
| 646 | * which the platform has booted. This value |
| 647 | * gets passed from the ealier stage bootloader |
| 648 | * which booted u-boot, e.g. tf-a. If the |
| 649 | * boot index is not the same as the |
| 650 | * active_index read from the FWU metadata, |
| 651 | * update the active_index. |
| 652 | */ |
| 653 | fwu_plat_get_bootidx(&boot_idx); |
| 654 | if (boot_idx >= CONFIG_FWU_NUM_BANKS) { |
| 655 | log_err("Received incorrect value of boot_index\n"); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | ret = fwu_get_active_index(&active_idx); |
| 660 | if (ret) { |
| 661 | log_err("Unable to read active_index\n"); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | if (boot_idx != active_idx) { |
| 666 | log_info("Boot idx %u is not matching active idx %u, changing active_idx\n", |
| 667 | boot_idx, active_idx); |
| 668 | ret = fwu_set_active_index(boot_idx); |
| 669 | if (!ret) |
| 670 | boottime_check = 1; |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (efi_init_obj_list() != EFI_SUCCESS) |
| 674 | return 0; |
| 675 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 676 | in_trial = in_trial_state(&g_mdata); |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 677 | if (!in_trial || (ret = fwu_trial_count_update()) > 0) |
| 678 | ret = trial_counter_update(NULL); |
| 679 | |
| 680 | if (!ret) |
| 681 | boottime_check = 1; |
| 682 | |
| 683 | return 0; |
| 684 | } |
Simon Glass | f72d0d4 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 685 | EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks); |