blob: 86518108c2d55a4e03ab0005699edf5a00857d6a [file] [log] [blame]
Sughosh Ganu2eaedc92022-10-21 18:15:55 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2022, Linaro Limited
4 */
5
6#include <dm.h>
Sughosh Ganu7e9814c2022-10-21 18:16:02 +05307#include <efi.h>
Sughosh Ganu2eaedc92022-10-21 18:15:55 +05308#include <efi_loader.h>
Sughosh Ganu7e9814c2022-10-21 18:16:02 +05309#include <efi_variable.h>
10#include <event.h>
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053011#include <fwu.h>
12#include <fwu_mdata.h>
Sughosh Ganu7e9814c2022-10-21 18:16:02 +053013#include <malloc.h>
14
15#include <linux/errno.h>
16#include <linux/types.h>
17
Jassi Brar167994f2023-03-06 17:18:28 -060018#include <u-boot/crc.h>
19
20static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */
21static struct udevice *g_dev;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +053022static u8 in_trial;
23static u8 boottime_check;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053024
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053025enum {
26 IMAGE_ACCEPT_SET = 1,
27 IMAGE_ACCEPT_CLEAR,
28};
29
30enum {
31 PRIMARY_PART = 1,
32 SECONDARY_PART,
33 BOTH_PARTS,
34};
35
Sughosh Ganu7e9814c2022-10-21 18:16:02 +053036static 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
60static 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
79static 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 Simekb378fdd2023-07-14 10:47:06 +020098 log_info("Trial State count: attempt %d out of %d\n",
99 trial_state_ctr, CONFIG_FWU_TRIAL_STATE_CNT);
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530100 ret = trial_counter_update(&trial_state_ctr);
101 if (ret)
102 log_err("Unable to increment TrialStateCtr variable\n");
103 }
104
105out:
106 return ret;
107}
108
109static 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 Kojimaaf7a34a2024-01-11 14:35:39 +0900128static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530129{
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530130 int i;
131 struct efi_fw_image *image;
132
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530133 image = update_info.images;
Masahisa Kojimacccea182023-06-07 14:41:51 +0900134 for (i = 0; i < update_info.num_images; i++) {
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900135 if (image_index == image[i].image_index) {
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530136 guidcpy(image_type_id, &image[i].image_type_id);
137 return 0;
138 }
139 }
140
141 return -ENOENT;
142}
143
144/**
Jassi Brar167994f2023-03-06 17:18:28 -0600145 * 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 */
151static 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
183static 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 Brar1e917a62023-03-06 17:18:48 -0600192 * fwu_get_mdata() - Read, verify and return the FWU metadata
Jassi Brar167994f2023-03-06 17:18:28 -0600193 * @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 Brar1e917a62023-03-06 17:18:48 -0600201int fwu_get_mdata(struct fwu_mdata *mdata)
Jassi Brar167994f2023-03-06 17:18:28 -0600202{
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
258ret_mdata:
259 if (!err && mdata)
260 memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata));
261
262 return err;
263}
264
265/**
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530266 * 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 */
275int fwu_get_active_index(uint *active_idx)
276{
Jassi Brar246ec2a2023-03-06 17:18:41 -0600277 int ret = 0;
278 struct fwu_mdata *mdata = &g_mdata;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530279
280 /*
281 * Found the FWU metadata partition, now read the active_index
282 * value
283 */
Jassi Brar246ec2a2023-03-06 17:18:41 -0600284 *active_idx = mdata->active_index;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530285 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 */
302int fwu_set_active_index(uint active_idx)
303{
304 int ret;
Jassi Brar246ec2a2023-03-06 17:18:41 -0600305 struct fwu_mdata *mdata = &g_mdata;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530306
307 if (active_idx >= CONFIG_FWU_NUM_BANKS) {
308 log_debug("Invalid active index value\n");
309 return -EINVAL;
310 }
311
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530312 /*
313 * Update the active index and previous_active_index fields
314 * in the FWU metadata
315 */
Jassi Brar246ec2a2023-03-06 17:18:41 -0600316 mdata->previous_active_index = mdata->active_index;
317 mdata->active_index = active_idx;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530318
319 /*
320 * Now write this updated FWU metadata to both the
321 * FWU metadata partitions
322 */
Jassi Brar246ec2a2023-03-06 17:18:41 -0600323 ret = fwu_sync_mdata(mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530324 if (ret) {
325 log_debug("Failed to update FWU metadata partitions\n");
326 ret = -EIO;
327 }
328
329 return ret;
330}
331
332/**
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900333 * 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 Ganu2eaedc92022-10-21 18:15:55 +0530336 *
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 Kojimaaf7a34a2024-01-11 14:35:39 +0900339 * be used for capsule update.
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530340 *
341 * Return: 0 if OK, -ve on error
342 *
343 */
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900344int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530345{
346 int ret, i;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530347 uint update_bank;
348 efi_guid_t *image_guid, image_type_id;
Jassi Brar246ec2a2023-03-06 17:18:41 -0600349 struct fwu_mdata *mdata = &g_mdata;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530350 struct fwu_image_entry *img_entry;
351 struct fwu_image_bank_info *img_bank_info;
352
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530353 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 Kojimaaf7a34a2024-01-11 14:35:39 +0900362 image_index);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530363 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 Brar246ec2a2023-03-06 17:18:41 -0600373 &mdata->img_entry[i].image_type_uuid)) {
374 img_entry = &mdata->img_entry[i];
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530375 img_bank_info = &img_entry->img_bank_info[update_bank];
376 image_guid = &img_bank_info->image_uuid;
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900377 ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num);
378 if (ret)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530379 log_debug("alt_num not found for partition with GUID %pUs\n",
380 image_guid);
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900381 else
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530382 log_debug("alt_num %d for partition %pUs\n",
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900383 *alt_num, image_guid);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530384
385 goto out;
386 }
387 }
388
Jassi Brar167994f2023-03-06 17:18:28 -0600389 log_err("Partition with the image type %pUs not found\n",
390 &image_type_id);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530391
392out:
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 */
406int fwu_revert_boot_index(void)
407{
408 int ret;
409 u32 cur_active_index;
Jassi Brar246ec2a2023-03-06 17:18:41 -0600410 struct fwu_mdata *mdata = &g_mdata;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530411
412 /*
413 * Swap the active index and previous_active_index fields
414 * in the FWU metadata
415 */
Jassi Brar246ec2a2023-03-06 17:18:41 -0600416 cur_active_index = mdata->active_index;
417 mdata->active_index = mdata->previous_active_index;
418 mdata->previous_active_index = cur_active_index;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530419
420 /*
421 * Now write this updated FWU metadata to both the
422 * FWU metadata partitions
423 */
Jassi Brar246ec2a2023-03-06 17:18:41 -0600424 ret = fwu_sync_mdata(mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530425 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 */
448static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
449{
450 int ret, i;
Jassi Brar246ec2a2023-03-06 17:18:41 -0600451 struct fwu_mdata *mdata = &g_mdata;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530452 struct fwu_image_entry *img_entry;
453 struct fwu_image_bank_info *img_bank_info;
454
Jassi Brar246ec2a2023-03-06 17:18:41 -0600455 img_entry = &mdata->img_entry[0];
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530456 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 Brar246ec2a2023-03-06 17:18:41 -0600464 ret = fwu_sync_mdata(mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530465 goto out;
466 }
467 }
468
469 /* Image not found */
470 ret = -ENOENT;
471
472out:
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 */
489int 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 */
509int 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 Ganu7d6e2c52022-10-21 18:15:59 +0530514
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 Ganu7e9814c2022-10-21 18:16:02 +0530541
542/**
Jassi Brara7e45412023-05-31 00:30:06 -0500543 * 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 Ganu7e9814c2022-10-21 18:16:02 +0530561 * 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 */
570u8 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 */
586u8 fwu_empty_capsule_checks_pass(void)
587{
588 return in_trial && boottime_check;
589}
590
Sughosh Ganu86794052022-10-21 18:16:03 +0530591/**
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 */
600int 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 Glassf72d0d42023-08-21 21:16:56 -0600613static int fwu_boottime_checks(void)
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530614{
615 int ret;
616 u32 boot_idx, active_idx;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530617
Jassi Brar246ec2a2023-03-06 17:18:41 -0600618 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 Vasut95311f72023-08-23 02:16:52 +0200624 /* Don't have boot time checks on sandbox */
625 if (IS_ENABLED(CONFIG_SANDBOX)) {
626 boottime_check = 1;
627 return 0;
628 }
629
Jassi Brar1e917a62023-03-06 17:18:48 -0600630 ret = fwu_get_mdata(NULL);
Jassi Brar246ec2a2023-03-06 17:18:41 -0600631 if (ret) {
632 log_debug("Unable to read meta-data\n");
633 return ret;
634 }
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530635
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 Ganu7e9814c2022-10-21 18:16:02 +0530663 }
664
665 if (efi_init_obj_list() != EFI_SUCCESS)
666 return 0;
667
Jassi Brar246ec2a2023-03-06 17:18:41 -0600668 in_trial = in_trial_state(&g_mdata);
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530669 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 Glassf72d0d42023-08-21 21:16:56 -0600677EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks);