blob: a94a769a2b6bd12af8230a26b63d4e421916367f [file] [log] [blame]
Sughosh Ganu0f9399a2022-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 Ganu2a0592a2022-10-21 18:16:02 +05307#include <efi.h>
Sughosh Ganu0f9399a2022-10-21 18:15:55 +05308#include <efi_loader.h>
Sughosh Ganu2a0592a2022-10-21 18:16:02 +05309#include <efi_variable.h>
10#include <event.h>
Sughosh Ganu0f9399a2022-10-21 18:15:55 +053011#include <fwu.h>
12#include <fwu_mdata.h>
Sughosh Ganu64a83a62024-03-22 16:27:21 +053013#include <log.h>
Sughosh Ganu2a0592a2022-10-21 18:16:02 +053014#include <malloc.h>
Sughosh Ganu0f9399a2022-10-21 18:15:55 +053015
16#include <linux/errno.h>
17#include <linux/types.h>
Sughosh Ganu2a0592a2022-10-21 18:16:02 +053018
Jassi Brar821e4622023-03-06 17:18:28 -060019#include <u-boot/crc.h>
20
Sughosh Ganu64a83a62024-03-22 16:27:21 +053021struct fwu_data g_fwu_data;
Jassi Brar821e4622023-03-06 17:18:28 -060022static struct udevice *g_dev;
Sughosh Ganu2a0592a2022-10-21 18:16:02 +053023static u8 in_trial;
24static u8 boottime_check;
25
Sughosh Ganu0f9399a2022-10-21 18:15:55 +053026enum {
27 IMAGE_ACCEPT_SET = 1,
28 IMAGE_ACCEPT_CLEAR,
29};
30
Sughosh Ganu4c63b7e2024-09-09 16:50:18 +053031/**
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 */
40bool 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 Ganu2a0592a2022-10-21 18:16:02 +053056static 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
80static 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
99static 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 Simeke9b59bf2023-07-14 10:47:06 +0200118 log_info("Trial State count: attempt %d out of %d\n",
119 trial_state_ctr, CONFIG_FWU_TRIAL_STATE_CNT);
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530120 ret = trial_counter_update(&trial_state_ctr);
121 if (ret)
122 log_err("Unable to increment TrialStateCtr variable\n");
123 }
124
125out:
126 return ret;
127}
128
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530129static u32 in_trial_state(void)
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530130{
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530131 return g_fwu_data.trial_state;
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530132}
133
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900134static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530135{
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530136 int i;
137 struct efi_fw_image *image;
138
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530139 image = update_info.images;
Masahisa Kojima5d2438b2023-06-07 14:41:51 +0900140 for (i = 0; i < update_info.num_images; i++) {
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900141 if (image_index == image[i].image_index) {
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530142 guidcpy(image_type_id, &image[i].image_type_id);
143 return 0;
144 }
145 }
146
147 return -ENOENT;
148}
149
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530150static 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
165static 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 */
177struct fwu_data *fwu_get_data(void)
178{
179 return &g_fwu_data;
180}
181
182static 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 Ganu0f9399a2022-10-21 18:15:55 +0530190/**
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530191 * 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 */
197struct udevice *fwu_get_dev(void)
198{
199 return g_dev;
200}
201
202/**
Jassi Brar821e4622023-03-06 17:18:28 -0600203 * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530204 * @data: FWU Data structure
Jassi Brar821e4622023-03-06 17:18:28 -0600205 * @part: Bitmask of FWU metadata partitions to be written to
206 *
207 * Return: 0 if OK, -ve on error
208 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530209int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
Jassi Brar821e4622023-03-06 17:18:28 -0600210{
Jassi Brar821e4622023-03-06 17:18:28 -0600211 int err;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530212 uint mdata_size;
213 void *buf = &mdata->version;
Jassi Brar821e4622023-03-06 17:18:28 -0600214
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 Ganu64a83a62024-03-22 16:27:21 +0530222 err = fwu_get_mdata_size(&mdata_size);
223 if (err)
224 return err;
225
Jassi Brar821e4622023-03-06 17:18:28 -0600226 /*
227 * Calculate the crc32 for the updated FWU metadata
228 * and put the updated value in the FWU metadata crc32
229 * field
230 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530231 mdata->crc32 = crc32(0, buf, mdata_size - sizeof(u32));
232 fwu_data_crc_update(mdata->crc32);
Jassi Brar821e4622023-03-06 17:18:28 -0600233
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530234 err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART, mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600235 if (err) {
236 log_err("Unable to write %s mdata\n",
237 part == PRIMARY_PART ? "primary" : "secondary");
238 return err;
239 }
240
Jassi Brar821e4622023-03-06 17:18:28 -0600241 return 0;
242}
243
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530244/**
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 */
253int fwu_mdata_copies_allocate(u32 mdata_size)
Jassi Brar821e4622023-03-06 17:18:28 -0600254{
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530255 if (g_fwu_data.fwu_mdata)
256 return 0;
Jassi Brar821e4622023-03-06 17:18:28 -0600257
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530258 /*
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 Brar821e4622023-03-06 17:18:28 -0600269}
270
271/**
Jassi Brarf7522552023-03-06 17:18:48 -0600272 * fwu_get_mdata() - Read, verify and return the FWU metadata
Jassi Brar821e4622023-03-06 17:18:28 -0600273 * @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 Brarf7522552023-03-06 17:18:48 -0600281int fwu_get_mdata(struct fwu_mdata *mdata)
Jassi Brar821e4622023-03-06 17:18:28 -0600282{
283 int err;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530284 uint32_t mdata_size;
Jassi Brar821e4622023-03-06 17:18:28 -0600285 bool parts_ok[2] = { false };
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530286 struct fwu_mdata *parts_mdata[2];
Jassi Brar821e4622023-03-06 17:18:28 -0600287
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530288 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 Brar821e4622023-03-06 17:18:28 -0600300
301 /* if mdata already read and ready */
302 err = mdata_crc_check(parts_mdata[0]);
303 if (!err)
304 goto ret_mdata;
Jassi Brar821e4622023-03-06 17:18:28 -0600305
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530306 /* else read, verify and, if needed, fix mdata */
Jassi Brar821e4622023-03-06 17:18:28 -0600307 for (int i = 0; i < 2; i++) {
308 parts_ok[i] = false;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530309 err = fwu_read_mdata(g_dev, parts_mdata[i], !i, mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600310 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 Ganu64a83a62024-03-22 16:27:21 +0530324 err = memcmp(parts_mdata[0], parts_mdata[1], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600325 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 Ganu64a83a62024-03-22 16:27:21 +0530341 memcpy(parts_mdata[i], parts_mdata[1 - i], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600342 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
349ret_mdata:
350 if (!err && mdata)
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530351 memcpy(mdata, parts_mdata[0], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600352
353 return err;
354}
355
356/**
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530357 * 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 */
366int fwu_get_active_index(uint *active_idx)
367{
Jassi Brara1df12f2023-03-06 17:18:41 -0600368 int ret = 0;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530369 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530370
371 /*
372 * Found the FWU metadata partition, now read the active_index
373 * value
374 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530375 *active_idx = data->active_index;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530376 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 */
393int fwu_set_active_index(uint active_idx)
394{
395 int ret;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530396 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530397
398 if (active_idx >= CONFIG_FWU_NUM_BANKS) {
399 log_debug("Invalid active index value\n");
400 return -EINVAL;
401 }
402
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530403 /*
404 * Update the active index and previous_active_index fields
405 * in the FWU metadata
406 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530407 data->previous_active_index = data->active_index;
408 data->active_index = active_idx;
409
410 fwu_populate_mdata_bank_index(data);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530411
412 /*
413 * Now write this updated FWU metadata to both the
414 * FWU metadata partitions
415 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530416 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530417 if (ret) {
418 log_debug("Failed to update FWU metadata partitions\n");
419 ret = -EIO;
420 }
421
422 return ret;
423}
424
425/**
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900426 * 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 Ganu0f9399a2022-10-21 18:15:55 +0530429 *
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 Kojimac1c48e42024-01-11 14:35:39 +0900432 * be used for capsule update.
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530433 *
434 * Return: 0 if OK, -ve on error
435 *
436 */
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900437int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530438{
439 int ret, i;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530440 uint update_bank;
441 efi_guid_t *image_guid, image_type_id;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530442 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530443 struct fwu_image_entry *img_entry;
444 struct fwu_image_bank_info *img_bank_info;
445
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530446 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 Kojimac1c48e42024-01-11 14:35:39 +0900455 image_index);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530456 goto out;
457 }
458
459 ret = -EINVAL;
460 /*
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530461 * The FWU metadata has been read. Now get the image_guid for the
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530462 * 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 Ganu64a83a62024-03-22 16:27:21 +0530466 &data->fwu_images[i].image_type_guid)) {
467 img_entry = &data->fwu_images[i];
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530468 img_bank_info = &img_entry->img_bank_info[update_bank];
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530469 image_guid = &img_bank_info->image_guid;
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900470 ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num);
471 if (ret)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530472 log_debug("alt_num not found for partition with GUID %pUs\n",
473 image_guid);
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900474 else
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530475 log_debug("alt_num %d for partition %pUs\n",
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900476 *alt_num, image_guid);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530477
478 goto out;
479 }
480 }
481
Jassi Brar821e4622023-03-06 17:18:28 -0600482 log_err("Partition with the image type %pUs not found\n",
483 &image_type_id);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530484
485out:
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 */
499int fwu_revert_boot_index(void)
500{
501 int ret;
502 u32 cur_active_index;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530503 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530504
505 /*
506 * Swap the active index and previous_active_index fields
507 * in the FWU metadata
508 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530509 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 Ganu0f9399a2022-10-21 18:15:55 +0530514
515 /*
516 * Now write this updated FWU metadata to both the
517 * FWU metadata partitions
518 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530519 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530520 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 */
543static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
544{
545 int ret, i;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530546 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530547 struct fwu_image_entry *img_entry;
548 struct fwu_image_bank_info *img_bank_info;
549
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530550 img_entry = &data->fwu_images[0];
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530551 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530552 if (!guidcmp(&img_entry[i].image_type_guid, img_type_id)) {
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530553 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 Ganu64a83a62024-03-22 16:27:21 +0530559 fwu_populate_mdata_image_info(data);
560 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530561 goto out;
562 }
563 }
564
565 /* Image not found */
566 ret = -ENOENT;
567
568out:
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 */
585int 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 */
605int 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 Ganu60475ca2022-10-21 18:15:59 +0530610
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 Ganu2a0592a2022-10-21 18:16:02 +0530637
638/**
Jassi Brare22576d2023-05-31 00:30:06 -0500639 * 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 Ganu2a0592a2022-10-21 18:16:02 +0530657 * 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 */
666u8 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 */
682u8 fwu_empty_capsule_checks_pass(void)
683{
684 return in_trial && boottime_check;
685}
686
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530687/**
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 */
696int 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 Glassb8357c12023-08-21 21:16:56 -0600709static int fwu_boottime_checks(void)
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530710{
711 int ret;
712 u32 boot_idx, active_idx;
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530713
Jassi Brara1df12f2023-03-06 17:18:41 -0600714 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 Vasut4ede2df2023-08-23 02:16:52 +0200720 /* Don't have boot time checks on sandbox */
721 if (IS_ENABLED(CONFIG_SANDBOX)) {
722 boottime_check = 1;
723 return 0;
724 }
725
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530726 ret = fwu_init();
Jassi Brara1df12f2023-03-06 17:18:41 -0600727 if (ret) {
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530728 log_debug("fwu_init() failed\n");
Jassi Brara1df12f2023-03-06 17:18:41 -0600729 return ret;
730 }
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530731
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 Ganu2a0592a2022-10-21 18:16:02 +0530759 }
760
761 if (efi_init_obj_list() != EFI_SUCCESS)
762 return 0;
763
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530764 in_trial = in_trial_state();
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530765 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 Glassb8357c12023-08-21 21:16:56 -0600773EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks);