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