e73d5f11ff790891f72735d4f4c31763b8e6dc80
[platform/kernel/u-boot.git] / lib / fwu_updates / fwu.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022, Linaro Limited
4  */
5
6 #include <dm.h>
7 #include <efi_loader.h>
8 #include <fwu.h>
9 #include <fwu_mdata.h>
10 #include <log.h>
11
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <u-boot/crc.h>
15
16 enum {
17         IMAGE_ACCEPT_SET = 1,
18         IMAGE_ACCEPT_CLEAR,
19 };
20
21 enum {
22         PRIMARY_PART = 1,
23         SECONDARY_PART,
24         BOTH_PARTS,
25 };
26
27 static int fwu_get_dev_mdata(struct udevice **dev, struct fwu_mdata *mdata)
28 {
29         int ret;
30
31         ret = uclass_first_device_err(UCLASS_FWU_MDATA, dev);
32         if (ret) {
33                 log_debug("Cannot find fwu device\n");
34                 return ret;
35         }
36
37         if (!mdata)
38                 return 0;
39
40         ret = fwu_get_mdata(*dev, mdata);
41         if (ret < 0)
42                 log_debug("Unable to get valid FWU metadata\n");
43
44         return ret;
45 }
46
47 static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id)
48 {
49         u8 index;
50         int i;
51         struct efi_fw_image *image;
52
53         index = *image_index;
54         image = update_info.images;
55         for (i = 0; i < num_image_type_guids; i++) {
56                 if (index == image[i].image_index) {
57                         guidcpy(image_type_id, &image[i].image_type_id);
58                         return 0;
59                 }
60         }
61
62         return -ENOENT;
63 }
64
65 /**
66  * fwu_verify_mdata() - Verify the FWU metadata
67  * @mdata: FWU metadata structure
68  * @pri_part: FWU metadata partition is primary or secondary
69  *
70  * Verify the FWU metadata by computing the CRC32 for the metadata
71  * structure and comparing it against the CRC32 value stored as part
72  * of the structure.
73  *
74  * Return: 0 if OK, -ve on error
75  *
76  */
77 int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
78 {
79         u32 calc_crc32;
80         void *buf;
81
82         buf = &mdata->version;
83         calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
84
85         if (calc_crc32 != mdata->crc32) {
86                 log_debug("crc32 check failed for %s FWU metadata partition\n",
87                           pri_part ? "primary" : "secondary");
88                 return -EINVAL;
89         }
90
91         return 0;
92 }
93
94 /**
95  * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
96  *
97  * Read both the metadata copies from the storage media, verify their checksum,
98  * and ascertain that both copies match. If one of the copies has gone bad,
99  * restore it from the good copy.
100  *
101  * Return: 0 if OK, -ve on error
102  *
103  */
104 int fwu_check_mdata_validity(void)
105 {
106         int ret;
107         struct udevice *dev;
108         struct fwu_mdata pri_mdata;
109         struct fwu_mdata secondary_mdata;
110         uint mdata_parts[2];
111         uint valid_partitions, invalid_partitions;
112
113         ret = fwu_get_dev_mdata(&dev, NULL);
114         if (ret)
115                 return ret;
116
117         /*
118          * Check if the platform has defined its own
119          * function to check the metadata partitions'
120          * validity. If so, that takes precedence.
121          */
122         ret = fwu_mdata_check(dev);
123         if (!ret || ret != -ENOSYS)
124                 return ret;
125
126         /*
127          * Two FWU metadata partitions are expected.
128          * If we don't have two, user needs to create
129          * them first
130          */
131         valid_partitions = 0;
132         ret = fwu_get_mdata_part_num(dev, mdata_parts);
133         if (ret < 0) {
134                 log_debug("Error getting the FWU metadata partitions\n");
135                 return -ENOENT;
136         }
137
138         ret = fwu_read_mdata_partition(dev, &pri_mdata, mdata_parts[0]);
139         if (!ret) {
140                 ret = fwu_verify_mdata(&pri_mdata, 1);
141                 if (!ret)
142                         valid_partitions |= PRIMARY_PART;
143         }
144
145         ret = fwu_read_mdata_partition(dev, &secondary_mdata, mdata_parts[1]);
146         if (!ret) {
147                 ret = fwu_verify_mdata(&secondary_mdata, 0);
148                 if (!ret)
149                         valid_partitions |= SECONDARY_PART;
150         }
151
152         if (valid_partitions == (PRIMARY_PART | SECONDARY_PART)) {
153                 /*
154                  * Before returning, check that both the
155                  * FWU metadata copies are the same. If not,
156                  * populate the secondary partition from the
157                  * primary partition copy.
158                  */
159                 if (!memcmp(&pri_mdata, &secondary_mdata,
160                             sizeof(struct fwu_mdata))) {
161                         ret = 0;
162                 } else {
163                         log_info("Both FWU metadata copies are valid but do not match.");
164                         log_info(" Restoring the secondary partition from the primary\n");
165                         ret = fwu_write_mdata_partition(dev, &pri_mdata,
166                                                         mdata_parts[1]);
167                         if (ret)
168                                 log_debug("Restoring secondary FWU metadata partition failed\n");
169                 }
170                 goto out;
171         }
172
173         if (!(valid_partitions & BOTH_PARTS)) {
174                 log_info("Both FWU metadata partitions invalid\n");
175                 ret = -EBADMSG;
176                 goto out;
177         }
178
179         invalid_partitions = valid_partitions ^ BOTH_PARTS;
180         ret = fwu_write_mdata_partition(dev,
181                                         (invalid_partitions == PRIMARY_PART) ?
182                                         &secondary_mdata : &pri_mdata,
183                                         (invalid_partitions == PRIMARY_PART) ?
184                                         mdata_parts[0] : mdata_parts[1]);
185
186         if (ret)
187                 log_debug("Restoring %s FWU metadata partition failed\n",
188                           (invalid_partitions == PRIMARY_PART) ?
189                           "primary" : "secondary");
190
191 out:
192         return ret;
193 }
194
195 /**
196  * fwu_get_active_index() - Get active_index from the FWU metadata
197  * @active_idx: active_index value to be read
198  *
199  * Read the active_index field from the FWU metadata and place it in
200  * the variable pointed to be the function argument.
201  *
202  * Return: 0 if OK, -ve on error
203  *
204  */
205 int fwu_get_active_index(uint *active_idx)
206 {
207         int ret;
208         struct udevice *dev;
209         struct fwu_mdata mdata = { 0 };
210
211         ret = fwu_get_dev_mdata(&dev, &mdata);
212         if (ret)
213                 return ret;
214
215         /*
216          * Found the FWU metadata partition, now read the active_index
217          * value
218          */
219         *active_idx = mdata.active_index;
220         if (*active_idx >= CONFIG_FWU_NUM_BANKS) {
221                 log_debug("Active index value read is incorrect\n");
222                 ret = -EINVAL;
223         }
224
225         return ret;
226 }
227
228 /**
229  * fwu_set_active_index() - Set active_index in the FWU metadata
230  * @active_idx: active_index value to be set
231  *
232  * Update the active_index field in the FWU metadata
233  *
234  * Return: 0 if OK, -ve on error
235  *
236  */
237 int fwu_set_active_index(uint active_idx)
238 {
239         int ret;
240         struct udevice *dev;
241         struct fwu_mdata mdata = { 0 };
242
243         if (active_idx >= CONFIG_FWU_NUM_BANKS) {
244                 log_debug("Invalid active index value\n");
245                 return -EINVAL;
246         }
247
248         ret = fwu_get_dev_mdata(&dev, &mdata);
249         if (ret)
250                 return ret;
251
252         /*
253          * Update the active index and previous_active_index fields
254          * in the FWU metadata
255          */
256         mdata.previous_active_index = mdata.active_index;
257         mdata.active_index = active_idx;
258
259         /*
260          * Now write this updated FWU metadata to both the
261          * FWU metadata partitions
262          */
263         ret = fwu_update_mdata(dev, &mdata);
264         if (ret) {
265                 log_debug("Failed to update FWU metadata partitions\n");
266                 ret = -EIO;
267         }
268
269         return ret;
270 }
271
272 /**
273  * fwu_get_image_index() - Get the Image Index to be used for capsule update
274  * @image_index: The Image Index for the image
275  *
276  * The FWU multi bank update feature computes the value of image_index at
277  * runtime, based on the bank to which the image needs to be written to.
278  * Derive the image_index value for the image.
279  *
280  * Currently, the capsule update driver uses the DFU framework for
281  * the updates. This function gets the DFU alt number which is to
282  * be used as the Image Index
283  *
284  * Return: 0 if OK, -ve on error
285  *
286  */
287 int fwu_get_image_index(u8 *image_index)
288 {
289         int ret, i;
290         u8 alt_num;
291         uint update_bank;
292         efi_guid_t *image_guid, image_type_id;
293         struct udevice *dev;
294         struct fwu_mdata mdata = { 0 };
295         struct fwu_image_entry *img_entry;
296         struct fwu_image_bank_info *img_bank_info;
297
298         ret = fwu_get_dev_mdata(&dev, &mdata);
299         if (ret)
300                 return ret;
301
302         ret = fwu_plat_get_update_index(&update_bank);
303         if (ret) {
304                 log_debug("Failed to get the FWU update bank\n");
305                 goto out;
306         }
307
308         ret = fwu_get_image_type_id(image_index, &image_type_id);
309         if (ret) {
310                 log_debug("Unable to get image_type_id for image_index %u\n",
311                           *image_index);
312                 goto out;
313         }
314
315         ret = -EINVAL;
316         /*
317          * The FWU metadata has been read. Now get the image_uuid for the
318          * image with the update_bank.
319          */
320         for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
321                 if (!guidcmp(&image_type_id,
322                              &mdata.img_entry[i].image_type_uuid)) {
323                         img_entry = &mdata.img_entry[i];
324                         img_bank_info = &img_entry->img_bank_info[update_bank];
325                         image_guid = &img_bank_info->image_uuid;
326                         ret = fwu_plat_get_alt_num(dev, image_guid, &alt_num);
327                         if (ret) {
328                                 log_debug("alt_num not found for partition with GUID %pUs\n",
329                                           image_guid);
330                         } else {
331                                 log_debug("alt_num %d for partition %pUs\n",
332                                           alt_num, image_guid);
333                                 *image_index = alt_num + 1;
334                         }
335
336                         goto out;
337                 }
338         }
339
340         log_debug("Partition with the image type %pUs not found\n",
341                   &image_type_id);
342
343 out:
344         return ret;
345 }
346
347 /**
348  * fwu_revert_boot_index() - Revert the active index in the FWU metadata
349  *
350  * Revert the active_index value in the FWU metadata, by swapping the values
351  * of active_index and previous_active_index in both copies of the
352  * FWU metadata.
353  *
354  * Return: 0 if OK, -ve on error
355  *
356  */
357 int fwu_revert_boot_index(void)
358 {
359         int ret;
360         u32 cur_active_index;
361         struct udevice *dev;
362         struct fwu_mdata mdata = { 0 };
363
364         ret = fwu_get_dev_mdata(&dev, &mdata);
365         if (ret)
366                 return ret;
367
368         /*
369          * Swap the active index and previous_active_index fields
370          * in the FWU metadata
371          */
372         cur_active_index = mdata.active_index;
373         mdata.active_index = mdata.previous_active_index;
374         mdata.previous_active_index = cur_active_index;
375
376         /*
377          * Now write this updated FWU metadata to both the
378          * FWU metadata partitions
379          */
380         ret = fwu_update_mdata(dev, &mdata);
381         if (ret) {
382                 log_debug("Failed to update FWU metadata partitions\n");
383                 ret = -EIO;
384         }
385
386         return ret;
387 }
388
389 /**
390  * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image
391  * @img_type_id: GUID of the image type for which the accepted bit is to be
392  *               set or cleared
393  * @bank: Bank of which the image's Accept bit is to be set or cleared
394  * @action: Action which specifies whether image's Accept bit is to be set or
395  *          cleared
396  *
397  * Set/Clear the accepted bit for the image specified by the img_guid parameter.
398  * This indicates acceptance or rejection of image for subsequent boots by some
399  * governing component like OS(or firmware).
400  *
401  * Return: 0 if OK, -ve on error
402  *
403  */
404 static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
405 {
406         int ret, i;
407         struct udevice *dev;
408         struct fwu_mdata mdata = { 0 };
409         struct fwu_image_entry *img_entry;
410         struct fwu_image_bank_info *img_bank_info;
411
412         ret = fwu_get_dev_mdata(&dev, &mdata);
413         if (ret)
414                 return ret;
415
416         img_entry = &mdata.img_entry[0];
417         for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
418                 if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) {
419                         img_bank_info = &img_entry[i].img_bank_info[bank];
420                         if (action == IMAGE_ACCEPT_SET)
421                                 img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
422                         else
423                                 img_bank_info->accepted = 0;
424
425                         ret = fwu_update_mdata(dev, &mdata);
426                         goto out;
427                 }
428         }
429
430         /* Image not found */
431         ret = -ENOENT;
432
433 out:
434         return ret;
435 }
436
437 /**
438  * fwu_accept_image() - Set the Acceptance bit for the image
439  * @img_type_id: GUID of the image type for which the accepted bit is to be
440  *               cleared
441  * @bank: Bank of which the image's Accept bit is to be set
442  *
443  * Set the accepted bit for the image specified by the img_guid parameter. This
444  * indicates acceptance of image for subsequent boots by some governing component
445  * like OS(or firmware).
446  *
447  * Return: 0 if OK, -ve on error
448  *
449  */
450 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
451 {
452         return fwu_clrset_image_accept(img_type_id, bank,
453                                        IMAGE_ACCEPT_SET);
454 }
455
456 /**
457  * fwu_clear_accept_image() - Clear the Acceptance bit for the image
458  * @img_type_id: GUID of the image type for which the accepted bit is to be
459  *               cleared
460  * @bank: Bank of which the image's Accept bit is to be cleared
461  *
462  * Clear the accepted bit for the image type specified by the img_type_id parameter.
463  * This function is called after the image has been updated. The accepted bit is
464  * cleared to be set subsequently after passing the image acceptance criteria, by
465  * either the OS(or firmware)
466  *
467  * Return: 0 if OK, -ve on error
468  *
469  */
470 int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
471 {
472         return fwu_clrset_image_accept(img_type_id, bank,
473                                        IMAGE_ACCEPT_CLEAR);
474 }
475
476 /**
477  * fwu_plat_get_update_index() - Get the value of the update bank
478  * @update_idx: Bank number to which images are to be updated
479  *
480  * Get the value of the bank(partition) to which the update needs to be
481  * made.
482  *
483  * Note: This is a weak function and platforms can override this with
484  * their own implementation for selection of the update bank.
485  *
486  * Return: 0 if OK, -ve on error
487  *
488  */
489 __weak int fwu_plat_get_update_index(uint *update_idx)
490 {
491         int ret;
492         u32 active_idx;
493
494         ret = fwu_get_active_index(&active_idx);
495         if (ret < 0)
496                 return -1;
497
498         *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS;
499
500         return ret;
501 }