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