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