blob: 5dfea2a4d8d6ef09d5fe9dbb9e02d2422be4c44e [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 Ganuac62e0b2024-03-22 16:27:21 +053013#include <log.h>
Sughosh Ganu7e9814c2022-10-21 18:16:02 +053014#include <malloc.h>
15
16#include <linux/errno.h>
17#include <linux/types.h>
18
Jassi Brar167994f2023-03-06 17:18:28 -060019#include <u-boot/crc.h>
20
Sughosh Ganuac62e0b2024-03-22 16:27:21 +053021struct fwu_data g_fwu_data;
Jassi Brar167994f2023-03-06 17:18:28 -060022static struct udevice *g_dev;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +053023static u8 in_trial;
24static u8 boottime_check;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053025
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053026enum {
27 IMAGE_ACCEPT_SET = 1,
28 IMAGE_ACCEPT_CLEAR,
29};
30
Sughosh Ganu7e9814c2022-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 Simekb378fdd2023-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 Ganu7e9814c2022-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 Ganuac62e0b2024-03-22 16:27:21 +0530104static u32 in_trial_state(void)
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530105{
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530106 return g_fwu_data.trial_state;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530107}
108
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900109static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530110{
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530111 int i;
112 struct efi_fw_image *image;
113
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530114 image = update_info.images;
Masahisa Kojimacccea182023-06-07 14:41:51 +0900115 for (i = 0; i < update_info.num_images; i++) {
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900116 if (image_index == image[i].image_index) {
Sughosh Ganu2eaedc92022-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 Ganuac62e0b2024-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
165/**
166 * fwu_get_dev() - Return the FWU metadata device
167 *
168 * Return the pointer to the FWU metadata device.
169 *
170 * Return: Pointer to the FWU metadata dev
171 */
172struct udevice *fwu_get_dev(void)
173{
174 return g_dev;
175}
176
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530177/**
Jassi Brar167994f2023-03-06 17:18:28 -0600178 * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530179 * @data: FWU Data structure
Jassi Brar167994f2023-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 Ganuac62e0b2024-03-22 16:27:21 +0530184int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
Jassi Brar167994f2023-03-06 17:18:28 -0600185{
Jassi Brar167994f2023-03-06 17:18:28 -0600186 int err;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530187 uint mdata_size;
188 void *buf = &mdata->version;
Jassi Brar167994f2023-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 Ganuac62e0b2024-03-22 16:27:21 +0530197 err = fwu_get_mdata_size(&mdata_size);
198 if (err)
199 return err;
200
Jassi Brar167994f2023-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 Ganuac62e0b2024-03-22 16:27:21 +0530206 mdata->crc32 = crc32(0, buf, mdata_size - sizeof(u32));
207 fwu_data_crc_update(mdata->crc32);
Jassi Brar167994f2023-03-06 17:18:28 -0600208
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530209 err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART, mdata_size);
Jassi Brar167994f2023-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 Brar167994f2023-03-06 17:18:28 -0600216 return 0;
217}
218
Sughosh Ganuac62e0b2024-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 Brar167994f2023-03-06 17:18:28 -0600229{
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530230 if (g_fwu_data.fwu_mdata)
231 return 0;
Jassi Brar167994f2023-03-06 17:18:28 -0600232
Sughosh Ganuac62e0b2024-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 Brar167994f2023-03-06 17:18:28 -0600244}
245
246/**
Jassi Brar1e917a62023-03-06 17:18:48 -0600247 * fwu_get_mdata() - Read, verify and return the FWU metadata
Jassi Brar167994f2023-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 Brar1e917a62023-03-06 17:18:48 -0600256int fwu_get_mdata(struct fwu_mdata *mdata)
Jassi Brar167994f2023-03-06 17:18:28 -0600257{
258 int err;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530259 uint32_t mdata_size;
Jassi Brar167994f2023-03-06 17:18:28 -0600260 bool parts_ok[2] = { false };
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530261 struct fwu_mdata *parts_mdata[2];
Jassi Brar167994f2023-03-06 17:18:28 -0600262
Sughosh Ganuac62e0b2024-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 Brar167994f2023-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 Brar167994f2023-03-06 17:18:28 -0600280
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530281
282 /* else read, verify and, if needed, fix mdata */
Jassi Brar167994f2023-03-06 17:18:28 -0600283 for (int i = 0; i < 2; i++) {
284 parts_ok[i] = false;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530285 err = fwu_read_mdata(g_dev, parts_mdata[i], !i, mdata_size);
Jassi Brar167994f2023-03-06 17:18:28 -0600286 if (!err) {
287 err = mdata_crc_check(parts_mdata[i]);
288 if (!err)
289 parts_ok[i] = true;
290 else
291 log_debug("mdata : %s crc32 failed\n", i ? "secondary" : "primary");
292 }
293 }
294
295 if (parts_ok[0] && parts_ok[1]) {
296 /*
297 * Before returning, check that both the
298 * FWU metadata copies are the same.
299 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530300 err = memcmp(parts_mdata[0], parts_mdata[1], mdata_size);
Jassi Brar167994f2023-03-06 17:18:28 -0600301 if (!err)
302 goto ret_mdata;
303
304 /*
305 * If not, populate the secondary partition from the
306 * primary partition copy.
307 */
308 log_info("Both FWU metadata copies are valid but do not match.");
309 log_info(" Restoring the secondary partition from the primary\n");
310 parts_ok[1] = false;
311 }
312
313 for (int i = 0; i < 2; i++) {
314 if (parts_ok[i])
315 continue;
316
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530317 memcpy(parts_mdata[i], parts_mdata[1 - i], mdata_size);
Jassi Brar167994f2023-03-06 17:18:28 -0600318 err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART);
319 if (err) {
320 log_debug("mdata : %s write failed\n", i ? "secondary" : "primary");
321 return err;
322 }
323 }
324
325ret_mdata:
326 if (!err && mdata)
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530327 memcpy(mdata, parts_mdata[0], mdata_size);
Jassi Brar167994f2023-03-06 17:18:28 -0600328
329 return err;
330}
331
332/**
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530333 * fwu_get_active_index() - Get active_index from the FWU metadata
334 * @active_idx: active_index value to be read
335 *
336 * Read the active_index field from the FWU metadata and place it in
337 * the variable pointed to be the function argument.
338 *
339 * Return: 0 if OK, -ve on error
340 *
341 */
342int fwu_get_active_index(uint *active_idx)
343{
Jassi Brar246ec2a2023-03-06 17:18:41 -0600344 int ret = 0;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530345 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530346
347 /*
348 * Found the FWU metadata partition, now read the active_index
349 * value
350 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530351 *active_idx = data->active_index;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530352 if (*active_idx >= CONFIG_FWU_NUM_BANKS) {
353 log_debug("Active index value read is incorrect\n");
354 ret = -EINVAL;
355 }
356
357 return ret;
358}
359
360/**
361 * fwu_set_active_index() - Set active_index in the FWU metadata
362 * @active_idx: active_index value to be set
363 *
364 * Update the active_index field in the FWU metadata
365 *
366 * Return: 0 if OK, -ve on error
367 *
368 */
369int fwu_set_active_index(uint active_idx)
370{
371 int ret;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530372 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530373
374 if (active_idx >= CONFIG_FWU_NUM_BANKS) {
375 log_debug("Invalid active index value\n");
376 return -EINVAL;
377 }
378
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530379 /*
380 * Update the active index and previous_active_index fields
381 * in the FWU metadata
382 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530383 data->previous_active_index = data->active_index;
384 data->active_index = active_idx;
385
386 fwu_populate_mdata_bank_index(data);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530387
388 /*
389 * Now write this updated FWU metadata to both the
390 * FWU metadata partitions
391 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530392 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530393 if (ret) {
394 log_debug("Failed to update FWU metadata partitions\n");
395 ret = -EIO;
396 }
397
398 return ret;
399}
400
401/**
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900402 * fwu_get_dfu_alt_num() - Get the dfu_alt_num to be used for capsule update
403 * @image_index: The Image Index for the image
404 * @alt_num: pointer to store dfu_alt_num
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530405 *
406 * Currently, the capsule update driver uses the DFU framework for
407 * the updates. This function gets the DFU alt number which is to
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900408 * be used for capsule update.
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530409 *
410 * Return: 0 if OK, -ve on error
411 *
412 */
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900413int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530414{
415 int ret, i;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530416 uint update_bank;
417 efi_guid_t *image_guid, image_type_id;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530418 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530419 struct fwu_image_entry *img_entry;
420 struct fwu_image_bank_info *img_bank_info;
421
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530422 ret = fwu_plat_get_update_index(&update_bank);
423 if (ret) {
424 log_debug("Failed to get the FWU update bank\n");
425 goto out;
426 }
427
428 ret = fwu_get_image_type_id(image_index, &image_type_id);
429 if (ret) {
430 log_debug("Unable to get image_type_id for image_index %u\n",
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900431 image_index);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530432 goto out;
433 }
434
435 ret = -EINVAL;
436 /*
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530437 * The FWU metadata has been read. Now get the image_guid for the
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530438 * image with the update_bank.
439 */
440 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
441 if (!guidcmp(&image_type_id,
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530442 &data->fwu_images[i].image_type_guid)) {
443 img_entry = &data->fwu_images[i];
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530444 img_bank_info = &img_entry->img_bank_info[update_bank];
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530445 image_guid = &img_bank_info->image_guid;
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900446 ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num);
447 if (ret)
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530448 log_debug("alt_num not found for partition with GUID %pUs\n",
449 image_guid);
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900450 else
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530451 log_debug("alt_num %d for partition %pUs\n",
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900452 *alt_num, image_guid);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530453
454 goto out;
455 }
456 }
457
Jassi Brar167994f2023-03-06 17:18:28 -0600458 log_err("Partition with the image type %pUs not found\n",
459 &image_type_id);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530460
461out:
462 return ret;
463}
464
465/**
466 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
467 *
468 * Revert the active_index value in the FWU metadata, by swapping the values
469 * of active_index and previous_active_index in both copies of the
470 * FWU metadata.
471 *
472 * Return: 0 if OK, -ve on error
473 *
474 */
475int fwu_revert_boot_index(void)
476{
477 int ret;
478 u32 cur_active_index;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530479 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530480
481 /*
482 * Swap the active index and previous_active_index fields
483 * in the FWU metadata
484 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530485 cur_active_index = data->active_index;
486 data->active_index = data->previous_active_index;
487 data->previous_active_index = cur_active_index;
488
489 fwu_populate_mdata_bank_index(data);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530490
491 /*
492 * Now write this updated FWU metadata to both the
493 * FWU metadata partitions
494 */
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530495 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530496 if (ret) {
497 log_debug("Failed to update FWU metadata partitions\n");
498 ret = -EIO;
499 }
500
501 return ret;
502}
503
504/**
505 * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image
506 * @img_type_id: GUID of the image type for which the accepted bit is to be
507 * set or cleared
508 * @bank: Bank of which the image's Accept bit is to be set or cleared
509 * @action: Action which specifies whether image's Accept bit is to be set or
510 * cleared
511 *
512 * Set/Clear the accepted bit for the image specified by the img_guid parameter.
513 * This indicates acceptance or rejection of image for subsequent boots by some
514 * governing component like OS(or firmware).
515 *
516 * Return: 0 if OK, -ve on error
517 *
518 */
519static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
520{
521 int ret, i;
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530522 struct fwu_data *data = &g_fwu_data;
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530523 struct fwu_image_entry *img_entry;
524 struct fwu_image_bank_info *img_bank_info;
525
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530526 img_entry = &data->fwu_images[0];
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530527 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530528 if (!guidcmp(&img_entry[i].image_type_guid, img_type_id)) {
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530529 img_bank_info = &img_entry[i].img_bank_info[bank];
530 if (action == IMAGE_ACCEPT_SET)
531 img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
532 else
533 img_bank_info->accepted = 0;
534
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530535 fwu_populate_mdata_image_info(data);
536 ret = fwu_sync_mdata(data->fwu_mdata, BOTH_PARTS);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530537 goto out;
538 }
539 }
540
541 /* Image not found */
542 ret = -ENOENT;
543
544out:
545 return ret;
546}
547
548/**
549 * fwu_accept_image() - Set the Acceptance bit for the image
550 * @img_type_id: GUID of the image type for which the accepted bit is to be
551 * cleared
552 * @bank: Bank of which the image's Accept bit is to be set
553 *
554 * Set the accepted bit for the image specified by the img_guid parameter. This
555 * indicates acceptance of image for subsequent boots by some governing component
556 * like OS(or firmware).
557 *
558 * Return: 0 if OK, -ve on error
559 *
560 */
561int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
562{
563 return fwu_clrset_image_accept(img_type_id, bank,
564 IMAGE_ACCEPT_SET);
565}
566
567/**
568 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
569 * @img_type_id: GUID of the image type for which the accepted bit is to be
570 * cleared
571 * @bank: Bank of which the image's Accept bit is to be cleared
572 *
573 * Clear the accepted bit for the image type specified by the img_type_id parameter.
574 * This function is called after the image has been updated. The accepted bit is
575 * cleared to be set subsequently after passing the image acceptance criteria, by
576 * either the OS(or firmware)
577 *
578 * Return: 0 if OK, -ve on error
579 *
580 */
581int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
582{
583 return fwu_clrset_image_accept(img_type_id, bank,
584 IMAGE_ACCEPT_CLEAR);
585}
Sughosh Ganu7d6e2c52022-10-21 18:15:59 +0530586
587/**
588 * fwu_plat_get_update_index() - Get the value of the update bank
589 * @update_idx: Bank number to which images are to be updated
590 *
591 * Get the value of the bank(partition) to which the update needs to be
592 * made.
593 *
594 * Note: This is a weak function and platforms can override this with
595 * their own implementation for selection of the update bank.
596 *
597 * Return: 0 if OK, -ve on error
598 *
599 */
600__weak int fwu_plat_get_update_index(uint *update_idx)
601{
602 int ret;
603 u32 active_idx;
604
605 ret = fwu_get_active_index(&active_idx);
606 if (ret < 0)
607 return -1;
608
609 *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS;
610
611 return ret;
612}
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530613
614/**
Jassi Brara7e45412023-05-31 00:30:06 -0500615 * fwu_plat_get_bootidx() - Get the value of the boot index
616 * @boot_idx: Boot index value
617 *
618 * Get the value of the bank(partition) from which the platform
619 * has booted. This value is passed to U-Boot from the earlier
620 * stage bootloader which loads and boots all the relevant
621 * firmware images
622 */
623__weak void fwu_plat_get_bootidx(uint *boot_idx)
624{
625 int ret;
626
627 ret = fwu_get_active_index(boot_idx);
628 if (ret < 0)
629 *boot_idx = 0; /* Dummy value */
630}
631
632/**
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530633 * fwu_update_checks_pass() - Check if FWU update can be done
634 *
635 * Check if the FWU update can be executed. The updates are
636 * allowed only when the platform is not in Trial State and
637 * the boot time checks have passed
638 *
639 * Return: 1 if OK, 0 if checks do not pass
640 *
641 */
642u8 fwu_update_checks_pass(void)
643{
644 return !in_trial && boottime_check;
645}
646
647/**
648 * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed
649 *
650 * Check if the empty capsule can be processed to either accept or revert
651 * an earlier executed update. The empty capsules need to be processed
652 * only when the platform is in Trial State and the boot time checks have
653 * passed
654 *
655 * Return: 1 if OK, 0 if not to be allowed
656 *
657 */
658u8 fwu_empty_capsule_checks_pass(void)
659{
660 return in_trial && boottime_check;
661}
662
Sughosh Ganu86794052022-10-21 18:16:03 +0530663/**
664 * fwu_trial_state_ctr_start() - Start the Trial State counter
665 *
666 * Start the counter to identify the platform booting in the
667 * Trial State. The counter is implemented as an EFI variable.
668 *
669 * Return: 0 if OK, -ve on error
670 *
671 */
672int fwu_trial_state_ctr_start(void)
673{
674 int ret;
675 u16 trial_state_ctr;
676
677 trial_state_ctr = 0;
678 ret = trial_counter_update(&trial_state_ctr);
679 if (ret)
680 log_err("Unable to initialise TrialStateCtr\n");
681
682 return ret;
683}
684
Simon Glassf72d0d42023-08-21 21:16:56 -0600685static int fwu_boottime_checks(void)
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530686{
687 int ret;
688 u32 boot_idx, active_idx;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530689
Jassi Brar246ec2a2023-03-06 17:18:41 -0600690 ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev);
691 if (ret) {
692 log_debug("Cannot find fwu device\n");
693 return ret;
694 }
695
Marek Vasut95311f72023-08-23 02:16:52 +0200696 /* Don't have boot time checks on sandbox */
697 if (IS_ENABLED(CONFIG_SANDBOX)) {
698 boottime_check = 1;
699 return 0;
700 }
701
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530702 ret = fwu_init();
Jassi Brar246ec2a2023-03-06 17:18:41 -0600703 if (ret) {
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530704 log_debug("fwu_init() failed\n");
Jassi Brar246ec2a2023-03-06 17:18:41 -0600705 return ret;
706 }
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530707
708 /*
709 * Get the Boot Index, i.e. the bank from
710 * which the platform has booted. This value
711 * gets passed from the ealier stage bootloader
712 * which booted u-boot, e.g. tf-a. If the
713 * boot index is not the same as the
714 * active_index read from the FWU metadata,
715 * update the active_index.
716 */
717 fwu_plat_get_bootidx(&boot_idx);
718 if (boot_idx >= CONFIG_FWU_NUM_BANKS) {
719 log_err("Received incorrect value of boot_index\n");
720 return 0;
721 }
722
723 ret = fwu_get_active_index(&active_idx);
724 if (ret) {
725 log_err("Unable to read active_index\n");
726 return 0;
727 }
728
729 if (boot_idx != active_idx) {
730 log_info("Boot idx %u is not matching active idx %u, changing active_idx\n",
731 boot_idx, active_idx);
732 ret = fwu_set_active_index(boot_idx);
733 if (!ret)
734 boottime_check = 1;
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530735 }
736
737 if (efi_init_obj_list() != EFI_SUCCESS)
738 return 0;
739
Sughosh Ganuac62e0b2024-03-22 16:27:21 +0530740 in_trial = in_trial_state();
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530741 if (!in_trial || (ret = fwu_trial_count_update()) > 0)
742 ret = trial_counter_update(NULL);
743
744 if (!ret)
745 boottime_check = 1;
746
747 return 0;
748}
Simon Glassf72d0d42023-08-21 21:16:56 -0600749EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks);