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 | |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 128 | static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id) |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 129 | { |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 130 | int i; |
| 131 | struct efi_fw_image *image; |
| 132 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 133 | image = update_info.images; |
Masahisa Kojima | cccea18 | 2023-06-07 14:41:51 +0900 | [diff] [blame] | 134 | for (i = 0; i < update_info.num_images; i++) { |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 135 | if (image_index == image[i].image_index) { |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 136 | guidcpy(image_type_id, &image[i].image_type_id); |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return -ENOENT; |
| 142 | } |
| 143 | |
| 144 | /** |
Jassi Brar | 167994f | 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 | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 192 | * fwu_get_mdata() - Read, verify and return the FWU metadata |
Jassi Brar | 167994f | 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 | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 201 | int fwu_get_mdata(struct fwu_mdata *mdata) |
Jassi Brar | 167994f | 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 | 2eaedc9 | 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 | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 277 | int ret = 0; |
| 278 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 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 | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 284 | *active_idx = mdata->active_index; |
Sughosh Ganu | 2eaedc9 | 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 | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 305 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 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 | 2eaedc9 | 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 | 246ec2a | 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 | 2eaedc9 | 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 | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 323 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 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 | /** |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 333 | * fwu_get_dfu_alt_num() - Get the dfu_alt_num to be used for capsule update |
| 334 | * @image_index: The Image Index for the image |
| 335 | * @alt_num: pointer to store dfu_alt_num |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 336 | * |
| 337 | * Currently, the capsule update driver uses the DFU framework for |
| 338 | * the updates. This function gets the DFU alt number which is to |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 339 | * be used for capsule update. |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 340 | * |
| 341 | * Return: 0 if OK, -ve on error |
| 342 | * |
| 343 | */ |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 344 | int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num) |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 345 | { |
| 346 | int ret, i; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 347 | uint update_bank; |
| 348 | efi_guid_t *image_guid, image_type_id; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 349 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 350 | struct fwu_image_entry *img_entry; |
| 351 | struct fwu_image_bank_info *img_bank_info; |
| 352 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 353 | ret = fwu_plat_get_update_index(&update_bank); |
| 354 | if (ret) { |
| 355 | log_debug("Failed to get the FWU update bank\n"); |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | ret = fwu_get_image_type_id(image_index, &image_type_id); |
| 360 | if (ret) { |
| 361 | log_debug("Unable to get image_type_id for image_index %u\n", |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 362 | image_index); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 363 | goto out; |
| 364 | } |
| 365 | |
| 366 | ret = -EINVAL; |
| 367 | /* |
| 368 | * The FWU metadata has been read. Now get the image_uuid for the |
| 369 | * image with the update_bank. |
| 370 | */ |
| 371 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 372 | if (!guidcmp(&image_type_id, |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 373 | &mdata->img_entry[i].image_type_uuid)) { |
| 374 | img_entry = &mdata->img_entry[i]; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 375 | img_bank_info = &img_entry->img_bank_info[update_bank]; |
| 376 | image_guid = &img_bank_info->image_uuid; |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 377 | ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num); |
| 378 | if (ret) |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 379 | log_debug("alt_num not found for partition with GUID %pUs\n", |
| 380 | image_guid); |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 381 | else |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 382 | log_debug("alt_num %d for partition %pUs\n", |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 383 | *alt_num, image_guid); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 384 | |
| 385 | goto out; |
| 386 | } |
| 387 | } |
| 388 | |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 389 | log_err("Partition with the image type %pUs not found\n", |
| 390 | &image_type_id); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 391 | |
| 392 | out: |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * fwu_revert_boot_index() - Revert the active index in the FWU metadata |
| 398 | * |
| 399 | * Revert the active_index value in the FWU metadata, by swapping the values |
| 400 | * of active_index and previous_active_index in both copies of the |
| 401 | * FWU metadata. |
| 402 | * |
| 403 | * Return: 0 if OK, -ve on error |
| 404 | * |
| 405 | */ |
| 406 | int fwu_revert_boot_index(void) |
| 407 | { |
| 408 | int ret; |
| 409 | u32 cur_active_index; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 410 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Swap the active index and previous_active_index fields |
| 414 | * in the FWU metadata |
| 415 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 416 | cur_active_index = mdata->active_index; |
| 417 | mdata->active_index = mdata->previous_active_index; |
| 418 | mdata->previous_active_index = cur_active_index; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Now write this updated FWU metadata to both the |
| 422 | * FWU metadata partitions |
| 423 | */ |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 424 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 425 | if (ret) { |
| 426 | log_debug("Failed to update FWU metadata partitions\n"); |
| 427 | ret = -EIO; |
| 428 | } |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image |
| 435 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 436 | * set or cleared |
| 437 | * @bank: Bank of which the image's Accept bit is to be set or cleared |
| 438 | * @action: Action which specifies whether image's Accept bit is to be set or |
| 439 | * cleared |
| 440 | * |
| 441 | * Set/Clear the accepted bit for the image specified by the img_guid parameter. |
| 442 | * This indicates acceptance or rejection of image for subsequent boots by some |
| 443 | * governing component like OS(or firmware). |
| 444 | * |
| 445 | * Return: 0 if OK, -ve on error |
| 446 | * |
| 447 | */ |
| 448 | static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action) |
| 449 | { |
| 450 | int ret, i; |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 451 | struct fwu_mdata *mdata = &g_mdata; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 452 | struct fwu_image_entry *img_entry; |
| 453 | struct fwu_image_bank_info *img_bank_info; |
| 454 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 455 | img_entry = &mdata->img_entry[0]; |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 456 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 457 | if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) { |
| 458 | img_bank_info = &img_entry[i].img_bank_info[bank]; |
| 459 | if (action == IMAGE_ACCEPT_SET) |
| 460 | img_bank_info->accepted |= FWU_IMAGE_ACCEPTED; |
| 461 | else |
| 462 | img_bank_info->accepted = 0; |
| 463 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 464 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 465 | goto out; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* Image not found */ |
| 470 | ret = -ENOENT; |
| 471 | |
| 472 | out: |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * fwu_accept_image() - Set the Acceptance bit for the image |
| 478 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 479 | * cleared |
| 480 | * @bank: Bank of which the image's Accept bit is to be set |
| 481 | * |
| 482 | * Set the accepted bit for the image specified by the img_guid parameter. This |
| 483 | * indicates acceptance of image for subsequent boots by some governing component |
| 484 | * like OS(or firmware). |
| 485 | * |
| 486 | * Return: 0 if OK, -ve on error |
| 487 | * |
| 488 | */ |
| 489 | int fwu_accept_image(efi_guid_t *img_type_id, u32 bank) |
| 490 | { |
| 491 | return fwu_clrset_image_accept(img_type_id, bank, |
| 492 | IMAGE_ACCEPT_SET); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * fwu_clear_accept_image() - Clear the Acceptance bit for the image |
| 497 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 498 | * cleared |
| 499 | * @bank: Bank of which the image's Accept bit is to be cleared |
| 500 | * |
| 501 | * Clear the accepted bit for the image type specified by the img_type_id parameter. |
| 502 | * This function is called after the image has been updated. The accepted bit is |
| 503 | * cleared to be set subsequently after passing the image acceptance criteria, by |
| 504 | * either the OS(or firmware) |
| 505 | * |
| 506 | * Return: 0 if OK, -ve on error |
| 507 | * |
| 508 | */ |
| 509 | int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank) |
| 510 | { |
| 511 | return fwu_clrset_image_accept(img_type_id, bank, |
| 512 | IMAGE_ACCEPT_CLEAR); |
| 513 | } |
Sughosh Ganu | 7d6e2c5 | 2022-10-21 18:15:59 +0530 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * fwu_plat_get_update_index() - Get the value of the update bank |
| 517 | * @update_idx: Bank number to which images are to be updated |
| 518 | * |
| 519 | * Get the value of the bank(partition) to which the update needs to be |
| 520 | * made. |
| 521 | * |
| 522 | * Note: This is a weak function and platforms can override this with |
| 523 | * their own implementation for selection of the update bank. |
| 524 | * |
| 525 | * Return: 0 if OK, -ve on error |
| 526 | * |
| 527 | */ |
| 528 | __weak int fwu_plat_get_update_index(uint *update_idx) |
| 529 | { |
| 530 | int ret; |
| 531 | u32 active_idx; |
| 532 | |
| 533 | ret = fwu_get_active_index(&active_idx); |
| 534 | if (ret < 0) |
| 535 | return -1; |
| 536 | |
| 537 | *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS; |
| 538 | |
| 539 | return ret; |
| 540 | } |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 541 | |
| 542 | /** |
Jassi Brar | a7e4541 | 2023-05-31 00:30:06 -0500 | [diff] [blame] | 543 | * fwu_plat_get_bootidx() - Get the value of the boot index |
| 544 | * @boot_idx: Boot index value |
| 545 | * |
| 546 | * Get the value of the bank(partition) from which the platform |
| 547 | * has booted. This value is passed to U-Boot from the earlier |
| 548 | * stage bootloader which loads and boots all the relevant |
| 549 | * firmware images |
| 550 | */ |
| 551 | __weak void fwu_plat_get_bootidx(uint *boot_idx) |
| 552 | { |
| 553 | int ret; |
| 554 | |
| 555 | ret = fwu_get_active_index(boot_idx); |
| 556 | if (ret < 0) |
| 557 | *boot_idx = 0; /* Dummy value */ |
| 558 | } |
| 559 | |
| 560 | /** |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 561 | * fwu_update_checks_pass() - Check if FWU update can be done |
| 562 | * |
| 563 | * Check if the FWU update can be executed. The updates are |
| 564 | * allowed only when the platform is not in Trial State and |
| 565 | * the boot time checks have passed |
| 566 | * |
| 567 | * Return: 1 if OK, 0 if checks do not pass |
| 568 | * |
| 569 | */ |
| 570 | u8 fwu_update_checks_pass(void) |
| 571 | { |
| 572 | return !in_trial && boottime_check; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed |
| 577 | * |
| 578 | * Check if the empty capsule can be processed to either accept or revert |
| 579 | * an earlier executed update. The empty capsules need to be processed |
| 580 | * only when the platform is in Trial State and the boot time checks have |
| 581 | * passed |
| 582 | * |
| 583 | * Return: 1 if OK, 0 if not to be allowed |
| 584 | * |
| 585 | */ |
| 586 | u8 fwu_empty_capsule_checks_pass(void) |
| 587 | { |
| 588 | return in_trial && boottime_check; |
| 589 | } |
| 590 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 591 | /** |
| 592 | * fwu_trial_state_ctr_start() - Start the Trial State counter |
| 593 | * |
| 594 | * Start the counter to identify the platform booting in the |
| 595 | * Trial State. The counter is implemented as an EFI variable. |
| 596 | * |
| 597 | * Return: 0 if OK, -ve on error |
| 598 | * |
| 599 | */ |
| 600 | int fwu_trial_state_ctr_start(void) |
| 601 | { |
| 602 | int ret; |
| 603 | u16 trial_state_ctr; |
| 604 | |
| 605 | trial_state_ctr = 0; |
| 606 | ret = trial_counter_update(&trial_state_ctr); |
| 607 | if (ret) |
| 608 | log_err("Unable to initialise TrialStateCtr\n"); |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
Simon Glass | f72d0d4 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 613 | static int fwu_boottime_checks(void) |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 614 | { |
| 615 | int ret; |
| 616 | u32 boot_idx, active_idx; |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 617 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 618 | ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev); |
| 619 | if (ret) { |
| 620 | log_debug("Cannot find fwu device\n"); |
| 621 | return ret; |
| 622 | } |
| 623 | |
Marek Vasut | 95311f7 | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 624 | /* Don't have boot time checks on sandbox */ |
| 625 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
| 626 | boottime_check = 1; |
| 627 | return 0; |
| 628 | } |
| 629 | |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 630 | ret = fwu_get_mdata(NULL); |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 631 | if (ret) { |
| 632 | log_debug("Unable to read meta-data\n"); |
| 633 | return ret; |
| 634 | } |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | * Get the Boot Index, i.e. the bank from |
| 638 | * which the platform has booted. This value |
| 639 | * gets passed from the ealier stage bootloader |
| 640 | * which booted u-boot, e.g. tf-a. If the |
| 641 | * boot index is not the same as the |
| 642 | * active_index read from the FWU metadata, |
| 643 | * update the active_index. |
| 644 | */ |
| 645 | fwu_plat_get_bootidx(&boot_idx); |
| 646 | if (boot_idx >= CONFIG_FWU_NUM_BANKS) { |
| 647 | log_err("Received incorrect value of boot_index\n"); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | ret = fwu_get_active_index(&active_idx); |
| 652 | if (ret) { |
| 653 | log_err("Unable to read active_index\n"); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | if (boot_idx != active_idx) { |
| 658 | log_info("Boot idx %u is not matching active idx %u, changing active_idx\n", |
| 659 | boot_idx, active_idx); |
| 660 | ret = fwu_set_active_index(boot_idx); |
| 661 | if (!ret) |
| 662 | boottime_check = 1; |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (efi_init_obj_list() != EFI_SUCCESS) |
| 666 | return 0; |
| 667 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 668 | in_trial = in_trial_state(&g_mdata); |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 669 | if (!in_trial || (ret = fwu_trial_count_update()) > 0) |
| 670 | ret = trial_counter_update(NULL); |
| 671 | |
| 672 | if (!ret) |
| 673 | boottime_check = 1; |
| 674 | |
| 675 | return 0; |
| 676 | } |
Simon Glass | f72d0d4 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 677 | EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks); |