blob: c7fc8987beb0e0669afef47d32c1c388c8f1b03e [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 Ganu2a0592a2022-10-21 18:16:02 +053031static 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
55static 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
74static 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 Simeke9b59bf2023-07-14 10:47:06 +020093 log_info("Trial State count: attempt %d out of %d\n",
94 trial_state_ctr, CONFIG_FWU_TRIAL_STATE_CNT);
Sughosh Ganu2a0592a2022-10-21 18:16:02 +053095 ret = trial_counter_update(&trial_state_ctr);
96 if (ret)
97 log_err("Unable to increment TrialStateCtr variable\n");
98 }
99
100out:
101 return ret;
102}
103
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530104static u32 in_trial_state(void)
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530105{
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530106 return g_fwu_data.trial_state;
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530107}
108
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900109static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530110{
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530111 int i;
112 struct efi_fw_image *image;
113
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530114 image = update_info.images;
Masahisa Kojima5d2438b2023-06-07 14:41:51 +0900115 for (i = 0; i < update_info.num_images; i++) {
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900116 if (image_index == image[i].image_index) {
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530117 guidcpy(image_type_id, &image[i].image_type_id);
118 return 0;
119 }
120 }
121
122 return -ENOENT;
123}
124
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530125static 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
140static 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 */
152struct fwu_data *fwu_get_data(void)
153{
154 return &g_fwu_data;
155}
156
157static 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 Ganu0f9399a2022-10-21 18:15:55 +0530165/**
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530166 * 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 */
172struct udevice *fwu_get_dev(void)
173{
174 return g_dev;
175}
176
177/**
Jassi Brar821e4622023-03-06 17:18:28 -0600178 * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530179 * @data: FWU Data structure
Jassi Brar821e4622023-03-06 17:18:28 -0600180 * @part: Bitmask of FWU metadata partitions to be written to
181 *
182 * Return: 0 if OK, -ve on error
183 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530184int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
Jassi Brar821e4622023-03-06 17:18:28 -0600185{
Jassi Brar821e4622023-03-06 17:18:28 -0600186 int err;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530187 uint mdata_size;
188 void *buf = &mdata->version;
Jassi Brar821e4622023-03-06 17:18:28 -0600189
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 Ganu64a83a62024-03-22 16:27:21 +0530197 err = fwu_get_mdata_size(&mdata_size);
198 if (err)
199 return err;
200
Jassi Brar821e4622023-03-06 17:18:28 -0600201 /*
202 * Calculate the crc32 for the updated FWU metadata
203 * and put the updated value in the FWU metadata crc32
204 * field
205 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530206 mdata->crc32 = crc32(0, buf, mdata_size - sizeof(u32));
207 fwu_data_crc_update(mdata->crc32);
Jassi Brar821e4622023-03-06 17:18:28 -0600208
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530209 err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART, mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600210 if (err) {
211 log_err("Unable to write %s mdata\n",
212 part == PRIMARY_PART ? "primary" : "secondary");
213 return err;
214 }
215
Jassi Brar821e4622023-03-06 17:18:28 -0600216 return 0;
217}
218
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530219/**
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 */
228int fwu_mdata_copies_allocate(u32 mdata_size)
Jassi Brar821e4622023-03-06 17:18:28 -0600229{
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530230 if (g_fwu_data.fwu_mdata)
231 return 0;
Jassi Brar821e4622023-03-06 17:18:28 -0600232
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530233 /*
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 Brar821e4622023-03-06 17:18:28 -0600244}
245
246/**
Jassi Brarf7522552023-03-06 17:18:48 -0600247 * fwu_get_mdata() - Read, verify and return the FWU metadata
Jassi Brar821e4622023-03-06 17:18:28 -0600248 * @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 Brarf7522552023-03-06 17:18:48 -0600256int fwu_get_mdata(struct fwu_mdata *mdata)
Jassi Brar821e4622023-03-06 17:18:28 -0600257{
258 int err;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530259 uint32_t mdata_size;
Jassi Brar821e4622023-03-06 17:18:28 -0600260 bool parts_ok[2] = { false };
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530261 struct fwu_mdata *parts_mdata[2];
Jassi Brar821e4622023-03-06 17:18:28 -0600262
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530263 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 Brar821e4622023-03-06 17:18:28 -0600275
276 /* if mdata already read and ready */
277 err = mdata_crc_check(parts_mdata[0]);
278 if (!err)
279 goto ret_mdata;
Jassi Brar821e4622023-03-06 17:18:28 -0600280
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530281 /* else read, verify and, if needed, fix mdata */
Jassi Brar821e4622023-03-06 17:18:28 -0600282 for (int i = 0; i < 2; i++) {
283 parts_ok[i] = false;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530284 err = fwu_read_mdata(g_dev, parts_mdata[i], !i, mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600285 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 Ganu64a83a62024-03-22 16:27:21 +0530299 err = memcmp(parts_mdata[0], parts_mdata[1], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600300 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 Ganu64a83a62024-03-22 16:27:21 +0530316 memcpy(parts_mdata[i], parts_mdata[1 - i], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600317 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
324ret_mdata:
325 if (!err && mdata)
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530326 memcpy(mdata, parts_mdata[0], mdata_size);
Jassi Brar821e4622023-03-06 17:18:28 -0600327
328 return err;
329}
330
331/**
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530332 * 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 */
341int fwu_get_active_index(uint *active_idx)
342{
Jassi Brara1df12f2023-03-06 17:18:41 -0600343 int ret = 0;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530344 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530345
346 /*
347 * Found the FWU metadata partition, now read the active_index
348 * value
349 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530350 *active_idx = data->active_index;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530351 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 */
368int fwu_set_active_index(uint active_idx)
369{
370 int ret;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530371 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530372
373 if (active_idx >= CONFIG_FWU_NUM_BANKS) {
374 log_debug("Invalid active index value\n");
375 return -EINVAL;
376 }
377
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530378 /*
379 * Update the active index and previous_active_index fields
380 * in the FWU metadata
381 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530382 data->previous_active_index = data->active_index;
383 data->active_index = active_idx;
384
385 fwu_populate_mdata_bank_index(data);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530386
387 /*
388 * Now write this updated FWU metadata to both the
389 * FWU metadata partitions
390 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530391 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530392 if (ret) {
393 log_debug("Failed to update FWU metadata partitions\n");
394 ret = -EIO;
395 }
396
397 return ret;
398}
399
400/**
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900401 * 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 Ganu0f9399a2022-10-21 18:15:55 +0530404 *
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 Kojimac1c48e42024-01-11 14:35:39 +0900407 * be used for capsule update.
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530408 *
409 * Return: 0 if OK, -ve on error
410 *
411 */
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900412int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530413{
414 int ret, i;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530415 uint update_bank;
416 efi_guid_t *image_guid, image_type_id;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530417 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530418 struct fwu_image_entry *img_entry;
419 struct fwu_image_bank_info *img_bank_info;
420
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530421 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 Kojimac1c48e42024-01-11 14:35:39 +0900430 image_index);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530431 goto out;
432 }
433
434 ret = -EINVAL;
435 /*
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530436 * The FWU metadata has been read. Now get the image_guid for the
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530437 * 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 Ganu64a83a62024-03-22 16:27:21 +0530441 &data->fwu_images[i].image_type_guid)) {
442 img_entry = &data->fwu_images[i];
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530443 img_bank_info = &img_entry->img_bank_info[update_bank];
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530444 image_guid = &img_bank_info->image_guid;
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900445 ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num);
446 if (ret)
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530447 log_debug("alt_num not found for partition with GUID %pUs\n",
448 image_guid);
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900449 else
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530450 log_debug("alt_num %d for partition %pUs\n",
Masahisa Kojimac1c48e42024-01-11 14:35:39 +0900451 *alt_num, image_guid);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530452
453 goto out;
454 }
455 }
456
Jassi Brar821e4622023-03-06 17:18:28 -0600457 log_err("Partition with the image type %pUs not found\n",
458 &image_type_id);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530459
460out:
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 */
474int fwu_revert_boot_index(void)
475{
476 int ret;
477 u32 cur_active_index;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530478 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530479
480 /*
481 * Swap the active index and previous_active_index fields
482 * in the FWU metadata
483 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530484 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 Ganu0f9399a2022-10-21 18:15:55 +0530489
490 /*
491 * Now write this updated FWU metadata to both the
492 * FWU metadata partitions
493 */
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530494 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530495 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 */
518static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
519{
520 int ret, i;
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530521 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530522 struct fwu_image_entry *img_entry;
523 struct fwu_image_bank_info *img_bank_info;
524
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530525 img_entry = &data->fwu_images[0];
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530526 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530527 if (!guidcmp(&img_entry[i].image_type_guid, img_type_id)) {
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530528 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 Ganu64a83a62024-03-22 16:27:21 +0530534 fwu_populate_mdata_image_info(data);
535 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu0f9399a2022-10-21 18:15:55 +0530536 goto out;
537 }
538 }
539
540 /* Image not found */
541 ret = -ENOENT;
542
543out:
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 */
560int 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 */
580int 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 Ganu60475ca2022-10-21 18:15:59 +0530585
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 Ganu2a0592a2022-10-21 18:16:02 +0530612
613/**
Jassi Brare22576d2023-05-31 00:30:06 -0500614 * 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 Ganu2a0592a2022-10-21 18:16:02 +0530632 * 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 */
641u8 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 */
657u8 fwu_empty_capsule_checks_pass(void)
658{
659 return in_trial && boottime_check;
660}
661
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530662/**
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 */
671int 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 Glassb8357c12023-08-21 21:16:56 -0600684static int fwu_boottime_checks(void)
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530685{
686 int ret;
687 u32 boot_idx, active_idx;
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530688
Jassi Brara1df12f2023-03-06 17:18:41 -0600689 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 Vasut4ede2df2023-08-23 02:16:52 +0200695 /* Don't have boot time checks on sandbox */
696 if (IS_ENABLED(CONFIG_SANDBOX)) {
697 boottime_check = 1;
698 return 0;
699 }
700
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530701 ret = fwu_init();
Jassi Brara1df12f2023-03-06 17:18:41 -0600702 if (ret) {
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530703 log_debug("fwu_init() failed\n");
Jassi Brara1df12f2023-03-06 17:18:41 -0600704 return ret;
705 }
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530706
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 Ganu2a0592a2022-10-21 18:16:02 +0530734 }
735
736 if (efi_init_obj_list() != EFI_SUCCESS)
737 return 0;
738
Sughosh Ganu64a83a62024-03-22 16:27:21 +0530739 in_trial = in_trial_state();
Sughosh Ganu2a0592a2022-10-21 18:16:02 +0530740 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 Glassb8357c12023-08-21 21:16:56 -0600748EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks);