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