397e393a188fb5667b0d37c409aba9a9b0b44c31
[platform/kernel/u-boot.git] / lib / efi_loader / efi_capsule.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI Capsule
4  *
5  *  Copyright (c) 2018 Linaro Limited
6  *                      Author: AKASHI Takahiro
7  */
8
9 #define LOG_CATEGORY LOGC_EFI
10
11 #include <common.h>
12 #include <efi_loader.h>
13 #include <efi_variable.h>
14 #include <env.h>
15 #include <fdtdec.h>
16 #include <fs.h>
17 #include <hang.h>
18 #include <malloc.h>
19 #include <mapmem.h>
20 #include <sort.h>
21 #include <sysreset.h>
22 #include <asm/global_data.h>
23
24 #include <crypto/pkcs7.h>
25 #include <crypto/pkcs7_parser.h>
26 #include <linux/err.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
31 static const efi_guid_t efi_guid_firmware_management_capsule_id =
32                 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
33 const efi_guid_t efi_guid_firmware_management_protocol =
34                 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
35
36 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
37 /* for file system access */
38 static struct efi_file_handle *bootdev_root;
39 #endif
40
41 /**
42  * get_last_capsule - get the last capsule index
43  *
44  * Retrieve the index of the capsule invoked last time from "CapsuleLast"
45  * variable.
46  *
47  * Return:
48  * * > 0        - the last capsule index invoked
49  * * 0xffff     - on error, or no capsule invoked yet
50  */
51 static __maybe_unused unsigned int get_last_capsule(void)
52 {
53         u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
54         char value[5];
55         efi_uintn_t size;
56         unsigned long index = 0xffff;
57         efi_status_t ret;
58         int i;
59
60         size = sizeof(value16);
61         ret = efi_get_variable_int(u"CapsuleLast", &efi_guid_capsule_report,
62                                    NULL, &size, value16, NULL);
63         if (ret != EFI_SUCCESS || size != 22 ||
64             u16_strncmp(value16, u"Capsule", 7))
65                 goto err;
66         for (i = 0; i < 4; ++i) {
67                 u16 c = value16[i + 7];
68
69                 if (!c || c > 0x7f)
70                         goto err;
71                 value[i] = c;
72         }
73         value[4] = 0;
74         if (strict_strtoul(value, 16, &index))
75                 index = 0xffff;
76 err:
77         return index;
78 }
79
80 /**
81  * set_capsule_result - set a result variable
82  * @capsule:            Capsule
83  * @return_status:      Return status
84  *
85  * Create and set a result variable, "CapsuleXXXX", for the capsule,
86  * @capsule.
87  */
88 static __maybe_unused
89 void set_capsule_result(int index, struct efi_capsule_header *capsule,
90                         efi_status_t return_status)
91 {
92         u16 variable_name16[12];
93         struct efi_capsule_result_variable_header result;
94         struct efi_time time;
95         efi_status_t ret;
96
97         efi_create_indexed_name(variable_name16, sizeof(variable_name16),
98                                 "Capsule", index);
99         result.variable_total_size = sizeof(result);
100         result.capsule_guid = capsule->capsule_guid;
101         ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
102         if (ret == EFI_SUCCESS)
103                 memcpy(&result.capsule_processed, &time, sizeof(time));
104         else
105                 memset(&result.capsule_processed, 0, sizeof(time));
106         result.capsule_status = return_status;
107         ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
108                                    EFI_VARIABLE_NON_VOLATILE |
109                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
110                                    EFI_VARIABLE_RUNTIME_ACCESS,
111                                    sizeof(result), &result, false);
112         if (ret != EFI_SUCCESS) {
113                 log_err("Setting %ls failed\n", variable_name16);
114                 return;
115         }
116
117         /* Variable CapsuleLast must not include terminating 0x0000 */
118         ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report,
119                                    EFI_VARIABLE_READ_ONLY |
120                                    EFI_VARIABLE_NON_VOLATILE |
121                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
122                                    EFI_VARIABLE_RUNTIME_ACCESS,
123                                    22, variable_name16, false);
124         if (ret != EFI_SUCCESS)
125                 log_err("Setting %ls failed\n", u"CapsuleLast");
126 }
127
128 #ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
129 /**
130  * efi_fmp_find - search for Firmware Management Protocol drivers
131  * @image_type:         Image type guid
132  * @image_index:        Image Index
133  * @instance:           Instance number
134  * @handles:            Handles of FMP drivers
135  * @no_handles:         Number of handles
136  *
137  * Search for Firmware Management Protocol drivers, matching the image
138  * type, @image_type and the machine instance, @instance, from the list,
139  * @handles.
140  *
141  * Return:
142  * * Protocol instance  - on success
143  * * NULL               - on failure
144  */
145 static struct efi_firmware_management_protocol *
146 efi_fmp_find(efi_guid_t *image_type, u8 image_index, u64 instance,
147              efi_handle_t *handles, efi_uintn_t no_handles)
148 {
149         efi_handle_t *handle;
150         struct efi_firmware_management_protocol *fmp;
151         struct efi_firmware_image_descriptor *image_info, *desc;
152         efi_uintn_t info_size, descriptor_size;
153         u32 descriptor_version;
154         u8 descriptor_count;
155         u32 package_version;
156         u16 *package_version_name;
157         bool found = false;
158         int i, j;
159         efi_status_t ret;
160
161         for (i = 0, handle = handles; i < no_handles; i++, handle++) {
162                 struct efi_handler *fmp_handler;
163
164                 ret = efi_search_protocol(
165                                 *handle, &efi_guid_firmware_management_protocol,
166                                 &fmp_handler);
167                 if (ret != EFI_SUCCESS)
168                         continue;
169                 fmp = fmp_handler->protocol_interface;
170
171                 /* get device's image info */
172                 info_size = 0;
173                 image_info = NULL;
174                 descriptor_version = 0;
175                 descriptor_count = 0;
176                 descriptor_size = 0;
177                 package_version = 0;
178                 package_version_name = NULL;
179                 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
180                                                    image_info,
181                                                    &descriptor_version,
182                                                    &descriptor_count,
183                                                    &descriptor_size,
184                                                    &package_version,
185                                                    &package_version_name));
186                 if (ret != EFI_BUFFER_TOO_SMALL)
187                         goto skip;
188
189                 image_info = malloc(info_size);
190                 if (!image_info)
191                         goto skip;
192
193                 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
194                                                    image_info,
195                                                    &descriptor_version,
196                                                    &descriptor_count,
197                                                    &descriptor_size,
198                                                    &package_version,
199                                                    &package_version_name));
200                 if (ret != EFI_SUCCESS ||
201                     descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
202                         goto skip;
203
204                 /* matching */
205                 for (j = 0, desc = image_info; j < descriptor_count;
206                      j++, desc = (void *)desc + descriptor_size) {
207                         log_debug("+++ desc[%d] index: %d, name: %ls\n",
208                                   j, desc->image_index, desc->image_id_name);
209                         if (!guidcmp(&desc->image_type_id, image_type) &&
210                             (desc->image_index == image_index) &&
211                             (!instance ||
212                              !desc->hardware_instance ||
213                               desc->hardware_instance == instance))
214                                 found = true;
215                 }
216
217 skip:
218                 efi_free_pool(package_version_name);
219                 free(image_info);
220                 if (found)
221                         return fmp;
222         }
223
224         return NULL;
225 }
226
227 /**
228  * efi_remove_auth_hdr - remove authentication data from image
229  * @image:      Pointer to pointer to Image
230  * @image_size: Pointer to Image size
231  *
232  * Remove the authentication data from image if possible.
233  * Update @image and @image_size.
234  *
235  * Return:              status code
236  */
237 static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size)
238 {
239         struct efi_firmware_image_authentication *auth_hdr;
240         efi_status_t ret = EFI_INVALID_PARAMETER;
241
242         auth_hdr = (struct efi_firmware_image_authentication *)*image;
243         if (*image_size < sizeof(*auth_hdr))
244                 goto out;
245
246         if (auth_hdr->auth_info.hdr.dwLength <=
247             offsetof(struct win_certificate_uefi_guid, cert_data))
248                 goto out;
249
250         *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) +
251                 auth_hdr->auth_info.hdr.dwLength;
252         *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength -
253                 sizeof(auth_hdr->monotonic_count);
254
255         ret = EFI_SUCCESS;
256 out:
257         return ret;
258 }
259
260 #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
261 int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
262 {
263         const void *fdt_blob = gd->fdt_blob;
264         const void *blob;
265         const char *cnode_name = "capsule-key";
266         const char *snode_name = "signature";
267         int sig_node;
268         int len;
269
270         sig_node = fdt_subnode_offset(fdt_blob, 0, snode_name);
271         if (sig_node < 0) {
272                 log_err("Unable to get signature node offset\n");
273
274                 return -FDT_ERR_NOTFOUND;
275         }
276
277         blob = fdt_getprop(fdt_blob, sig_node, cnode_name, &len);
278
279         if (!blob || len < 0) {
280                 log_err("Unable to get capsule-key value\n");
281                 *pkey = NULL;
282                 *pkey_len = 0;
283
284                 return -FDT_ERR_NOTFOUND;
285         }
286
287         *pkey = (void *)blob;
288         *pkey_len = len;
289
290         return 0;
291 }
292
293 efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
294                                       void **image, efi_uintn_t *image_size)
295 {
296         u8 *buf;
297         int ret;
298         void *fdt_pkey, *pkey;
299         efi_uintn_t pkey_len;
300         uint64_t monotonic_count;
301         struct efi_signature_store *truststore;
302         struct pkcs7_message *capsule_sig;
303         struct efi_image_regions *regs;
304         struct efi_firmware_image_authentication *auth_hdr;
305         efi_status_t status;
306
307         status = EFI_SECURITY_VIOLATION;
308         capsule_sig = NULL;
309         truststore = NULL;
310         regs = NULL;
311
312         /* Sanity checks */
313         if (capsule == NULL || capsule_size == 0)
314                 goto out;
315
316         *image = (uint8_t *)capsule;
317         *image_size = capsule_size;
318         if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
319                 goto out;
320
321         auth_hdr = (struct efi_firmware_image_authentication *)capsule;
322         if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
323                 goto out;
324
325         memcpy(&monotonic_count, &auth_hdr->monotonic_count,
326                sizeof(monotonic_count));
327
328         /* data to be digested */
329         regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
330         if (!regs)
331                 goto out;
332
333         regs->max = 2;
334         efi_image_region_add(regs, (uint8_t *)*image,
335                              (uint8_t *)*image + *image_size, 1);
336
337         efi_image_region_add(regs, (uint8_t *)&monotonic_count,
338                              (uint8_t *)&monotonic_count + sizeof(monotonic_count),
339                              1);
340
341         capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
342                                              auth_hdr->auth_info.hdr.dwLength
343                                              - sizeof(auth_hdr->auth_info),
344                                              &buf);
345         if (IS_ERR(capsule_sig)) {
346                 debug("Parsing variable's pkcs7 header failed\n");
347                 capsule_sig = NULL;
348                 goto out;
349         }
350
351         ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
352         if (ret < 0)
353                 goto out;
354
355         pkey = malloc(pkey_len);
356         if (!pkey)
357                 goto out;
358
359         memcpy(pkey, fdt_pkey, pkey_len);
360         truststore = efi_build_signature_store(pkey, pkey_len);
361         if (!truststore)
362                 goto out;
363
364         /* verify signature */
365         if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
366                 debug("Verified\n");
367         } else {
368                 debug("Verifying variable's signature failed\n");
369                 goto out;
370         }
371
372         status = EFI_SUCCESS;
373
374 out:
375         efi_sigstore_free(truststore);
376         pkcs7_free_message(capsule_sig);
377         free(regs);
378
379         return status;
380 }
381 #else
382 efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
383                                       void **image, efi_uintn_t *image_size)
384 {
385         return EFI_UNSUPPORTED;
386 }
387 #endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
388
389
390 /**
391  * efi_capsule_update_firmware - update firmware from capsule
392  * @capsule_data:       Capsule
393  *
394  * Update firmware, using a capsule, @capsule_data. Loading any FMP
395  * drivers embedded in a capsule is not supported.
396  *
397  * Return:              status code
398  */
399 static efi_status_t efi_capsule_update_firmware(
400                 struct efi_capsule_header *capsule_data)
401 {
402         struct efi_firmware_management_capsule_header *capsule;
403         struct efi_firmware_management_capsule_image_header *image;
404         size_t capsule_size, image_binary_size;
405         void *image_binary, *vendor_code;
406         efi_handle_t *handles;
407         efi_uintn_t no_handles;
408         int item;
409         struct efi_firmware_management_protocol *fmp;
410         u16 *abort_reason;
411         efi_status_t ret = EFI_SUCCESS;
412
413         /* sanity check */
414         if (capsule_data->header_size < sizeof(*capsule) ||
415             capsule_data->header_size >= capsule_data->capsule_image_size)
416                 return EFI_INVALID_PARAMETER;
417
418         capsule = (void *)capsule_data + capsule_data->header_size;
419         capsule_size = capsule_data->capsule_image_size
420                         - capsule_data->header_size;
421
422         if (capsule->version != 0x00000001)
423                 return EFI_UNSUPPORTED;
424
425         handles = NULL;
426         ret = EFI_CALL(efi_locate_handle_buffer(
427                         BY_PROTOCOL,
428                         &efi_guid_firmware_management_protocol,
429                         NULL, &no_handles, (efi_handle_t **)&handles));
430         if (ret != EFI_SUCCESS)
431                 return EFI_UNSUPPORTED;
432
433         /* Payload */
434         for (item = capsule->embedded_driver_count;
435              item < capsule->embedded_driver_count
436                     + capsule->payload_item_count; item++) {
437                 /* sanity check */
438                 if ((capsule->item_offset_list[item] + sizeof(*image)
439                                  >= capsule_size)) {
440                         log_err("Capsule does not have enough data\n");
441                         ret = EFI_INVALID_PARAMETER;
442                         goto out;
443                 }
444
445                 image = (void *)capsule + capsule->item_offset_list[item];
446
447                 if (image->version != 0x00000003) {
448                         ret = EFI_UNSUPPORTED;
449                         goto out;
450                 }
451
452                 /* find a device for update firmware */
453                 fmp = efi_fmp_find(&image->update_image_type_id,
454                                    image->update_image_index,
455                                    image->update_hardware_instance,
456                                    handles, no_handles);
457                 if (!fmp) {
458                         log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
459                                 &image->update_image_type_id,
460                                 image->update_hardware_instance);
461                         ret = EFI_UNSUPPORTED;
462                         goto out;
463                 }
464
465                 /* do update */
466                 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
467                     !(image->image_capsule_support &
468                                 CAPSULE_SUPPORT_AUTHENTICATION)) {
469                         /* no signature */
470                         ret = EFI_SECURITY_VIOLATION;
471                         goto out;
472                 }
473
474                 image_binary = (void *)image + sizeof(*image);
475                 image_binary_size = image->update_image_size;
476                 vendor_code = image_binary + image_binary_size;
477                 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
478                     (image->image_capsule_support &
479                                 CAPSULE_SUPPORT_AUTHENTICATION)) {
480                         ret = efi_remove_auth_hdr(&image_binary,
481                                                   &image_binary_size);
482                         if (ret != EFI_SUCCESS)
483                                 goto out;
484                 }
485
486                 abort_reason = NULL;
487                 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
488                                               image_binary,
489                                               image_binary_size,
490                                               vendor_code, NULL,
491                                               &abort_reason));
492                 if (ret != EFI_SUCCESS) {
493                         log_err("Firmware update failed: %ls\n",
494                                 abort_reason);
495                         efi_free_pool(abort_reason);
496                         goto out;
497                 }
498         }
499
500 out:
501         efi_free_pool(handles);
502
503         return ret;
504 }
505 #else
506 static efi_status_t efi_capsule_update_firmware(
507                 struct efi_capsule_header *capsule_data)
508 {
509         return EFI_UNSUPPORTED;
510 }
511 #endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
512
513 /**
514  * efi_update_capsule() - process information from operating system
515  * @capsule_header_array:       Array of virtual address pointers
516  * @capsule_count:              Number of pointers in capsule_header_array
517  * @scatter_gather_list:        Array of physical address pointers
518  *
519  * This function implements the UpdateCapsule() runtime service.
520  *
521  * See the Unified Extensible Firmware Interface (UEFI) specification for
522  * details.
523  *
524  * Return:                      status code
525  */
526 efi_status_t EFIAPI efi_update_capsule(
527                 struct efi_capsule_header **capsule_header_array,
528                 efi_uintn_t capsule_count,
529                 u64 scatter_gather_list)
530 {
531         struct efi_capsule_header *capsule;
532         unsigned int i;
533         efi_status_t ret;
534
535         EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
536                   scatter_gather_list);
537
538         if (!capsule_count) {
539                 ret = EFI_INVALID_PARAMETER;
540                 goto out;
541         }
542
543         ret = EFI_SUCCESS;
544         for (i = 0, capsule = *capsule_header_array; i < capsule_count;
545              i++, capsule = *(++capsule_header_array)) {
546                 /* sanity check */
547                 if (capsule->header_size < sizeof(*capsule) ||
548                     capsule->capsule_image_size < sizeof(*capsule)) {
549                         log_err("Capsule does not have enough data\n");
550                         continue;
551                 }
552
553                 log_debug("Capsule[%d] (guid:%pUs)\n",
554                           i, &capsule->capsule_guid);
555                 if (!guidcmp(&capsule->capsule_guid,
556                              &efi_guid_firmware_management_capsule_id)) {
557                         ret  = efi_capsule_update_firmware(capsule);
558                 } else {
559                         log_err("Unsupported capsule type: %pUs\n",
560                                 &capsule->capsule_guid);
561                         ret = EFI_UNSUPPORTED;
562                 }
563
564                 if (ret != EFI_SUCCESS)
565                         goto out;
566         }
567
568         if (IS_ENABLED(CONFIG_EFI_ESRT)) {
569                 /* Rebuild the ESRT to reflect any updated FW images. */
570                 ret = efi_esrt_populate();
571                 if (ret != EFI_SUCCESS)
572                         log_warning("ESRT update failed\n");
573         }
574 out:
575
576         return EFI_EXIT(ret);
577 }
578
579 /**
580  * efi_query_capsule_caps() - check if capsule is supported
581  * @capsule_header_array:       Array of virtual pointers
582  * @capsule_count:              Number of pointers in capsule_header_array
583  * @maximum_capsule_size:       Maximum capsule size
584  * @reset_type:                 Type of reset needed for capsule update
585  *
586  * This function implements the QueryCapsuleCapabilities() runtime service.
587  *
588  * See the Unified Extensible Firmware Interface (UEFI) specification for
589  * details.
590  *
591  * Return:                      status code
592  */
593 efi_status_t EFIAPI efi_query_capsule_caps(
594                 struct efi_capsule_header **capsule_header_array,
595                 efi_uintn_t capsule_count,
596                 u64 *maximum_capsule_size,
597                 u32 *reset_type)
598 {
599         struct efi_capsule_header *capsule __attribute__((unused));
600         unsigned int i;
601         efi_status_t ret;
602
603         EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
604                   maximum_capsule_size, reset_type);
605
606         if (!maximum_capsule_size) {
607                 ret = EFI_INVALID_PARAMETER;
608                 goto out;
609         }
610
611         *maximum_capsule_size = U64_MAX;
612         *reset_type = EFI_RESET_COLD;
613
614         ret = EFI_SUCCESS;
615         for (i = 0, capsule = *capsule_header_array; i < capsule_count;
616              i++, capsule = *(++capsule_header_array)) {
617                 /* TODO */
618         }
619 out:
620         return EFI_EXIT(ret);
621 }
622
623 /**
624  * efi_load_capsule_drivers - initialize capsule drivers
625  *
626  * Generic FMP drivers backed by DFU
627  *
628  * Return:      status code
629  */
630 efi_status_t __weak efi_load_capsule_drivers(void)
631 {
632         __maybe_unused efi_handle_t handle;
633         efi_status_t ret = EFI_SUCCESS;
634
635         if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
636                 handle = NULL;
637                 ret = efi_install_multiple_protocol_interfaces(&handle,
638                                                                &efi_guid_firmware_management_protocol,
639                                                                &efi_fmp_fit,
640                                                                NULL);
641         }
642
643         if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
644                 handle = NULL;
645                 ret = efi_install_multiple_protocol_interfaces(&handle,
646                                                                &efi_guid_firmware_management_protocol,
647                                                                &efi_fmp_raw,
648                                                                NULL);
649         }
650
651         return ret;
652 }
653
654 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
655 /**
656  * get_dp_device - retrieve a device  path from boot variable
657  * @boot_var:   Boot variable name
658  * @device_dp   Device path
659  *
660  * Retrieve a device patch from boot variable, @boot_var.
661  *
662  * Return:      status code
663  */
664 static efi_status_t get_dp_device(u16 *boot_var,
665                                   struct efi_device_path **device_dp)
666 {
667         void *buf = NULL;
668         efi_uintn_t size;
669         struct efi_load_option lo;
670         struct efi_device_path *file_dp;
671         efi_status_t ret;
672
673         size = 0;
674         ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
675                                    NULL, &size, NULL, NULL);
676         if (ret == EFI_BUFFER_TOO_SMALL) {
677                 buf = malloc(size);
678                 if (!buf)
679                         return EFI_OUT_OF_RESOURCES;
680                 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
681                                            NULL, &size, buf, NULL);
682         }
683         if (ret != EFI_SUCCESS)
684                 return ret;
685
686         efi_deserialize_load_option(&lo, buf, &size);
687
688         if (lo.attributes & LOAD_OPTION_ACTIVE) {
689                 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
690                 efi_free_pool(file_dp);
691
692                 ret = EFI_SUCCESS;
693         } else {
694                 ret = EFI_NOT_FOUND;
695         }
696
697         free(buf);
698
699         return ret;
700 }
701
702 /**
703  * device_is_present_and_system_part - check if a device exists
704  *
705  * Check if a device pointed to by the device path, @dp, exists and is
706  * located in UEFI system partition.
707  *
708  * @dp          device path
709  * Return:      true - yes, false - no
710  */
711 static bool device_is_present_and_system_part(struct efi_device_path *dp)
712 {
713         efi_handle_t handle;
714         struct efi_device_path *rem;
715
716         /* Check device exists */
717         handle = efi_dp_find_obj(dp, NULL, NULL);
718         if (!handle)
719                 return false;
720
721         /* Check device is on system partition */
722         handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
723         if (!handle)
724                 return false;
725
726         return true;
727 }
728
729 /**
730  * find_boot_device - identify the boot device
731  *
732  * Identify the boot device from boot-related variables as UEFI
733  * specification describes and put its handle into bootdev_root.
734  *
735  * Return:      status code
736  */
737 static efi_status_t find_boot_device(void)
738 {
739         char boot_var[9];
740         u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
741         efi_uintn_t size;
742         int i, num;
743         struct efi_simple_file_system_protocol *volume;
744         struct efi_device_path *boot_dev = NULL;
745         efi_status_t ret;
746
747         /* find active boot device in BootNext */
748         bootnext = 0;
749         size = sizeof(bootnext);
750         ret = efi_get_variable_int(u"BootNext",
751                                    (efi_guid_t *)&efi_global_variable_guid,
752                                    NULL, &size, &bootnext, NULL);
753         if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
754                 /* BootNext does exist here */
755                 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
756                         log_err("BootNext must be 16-bit integer\n");
757                         goto skip;
758                 }
759                 sprintf((char *)boot_var, "Boot%04X", bootnext);
760                 p = boot_var16;
761                 utf8_utf16_strcpy(&p, boot_var);
762
763                 ret = get_dp_device(boot_var16, &boot_dev);
764                 if (ret == EFI_SUCCESS) {
765                         if (device_is_present_and_system_part(boot_dev)) {
766                                 goto found;
767                         } else {
768                                 efi_free_pool(boot_dev);
769                                 boot_dev = NULL;
770                         }
771                 }
772         }
773
774 skip:
775         /* find active boot device in BootOrder */
776         size = 0;
777         ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
778                                    NULL, &size, NULL, NULL);
779         if (ret == EFI_BUFFER_TOO_SMALL) {
780                 boot_order = malloc(size);
781                 if (!boot_order) {
782                         ret = EFI_OUT_OF_RESOURCES;
783                         goto out;
784                 }
785
786                 ret = efi_get_variable_int(u"BootOrder",
787                                            &efi_global_variable_guid,
788                                            NULL, &size, boot_order, NULL);
789         }
790         if (ret != EFI_SUCCESS)
791                 goto out;
792
793         /* check in higher order */
794         num = size / sizeof(u16);
795         for (i = 0; i < num; i++) {
796                 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
797                 p = boot_var16;
798                 utf8_utf16_strcpy(&p, boot_var);
799                 ret = get_dp_device(boot_var16, &boot_dev);
800                 if (ret != EFI_SUCCESS)
801                         continue;
802
803                 if (device_is_present_and_system_part(boot_dev))
804                         break;
805
806                 efi_free_pool(boot_dev);
807                 boot_dev = NULL;
808         }
809 found:
810         if (boot_dev) {
811                 log_debug("Boot device %pD\n", boot_dev);
812
813                 volume = efi_fs_from_path(boot_dev);
814                 if (!volume)
815                         ret = EFI_DEVICE_ERROR;
816                 else
817                         ret = EFI_CALL(volume->open_volume(volume,
818                                                            &bootdev_root));
819                 efi_free_pool(boot_dev);
820         } else {
821                 ret = EFI_NOT_FOUND;
822         }
823 out:
824         free(boot_order);
825
826         return ret;
827 }
828
829 /**
830  * efi_capsule_scan_dir - traverse a capsule directory in boot device
831  * @files:      Array of file names
832  * @num:        Number of elements in @files
833  *
834  * Traverse a capsule directory in boot device.
835  * Called by initialization code, and returns an array of capsule file
836  * names in @files.
837  *
838  * Return:      status code
839  */
840 static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
841 {
842         struct efi_file_handle *dirh;
843         struct efi_file_info *dirent;
844         efi_uintn_t dirent_size, tmp_size;
845         unsigned int count;
846         u16 **tmp_files;
847         efi_status_t ret;
848
849         ret = find_boot_device();
850         if (ret == EFI_NOT_FOUND) {
851                 log_debug("Boot device is not set\n");
852                 *num = 0;
853                 return EFI_SUCCESS;
854         } else if (ret != EFI_SUCCESS) {
855                 return EFI_DEVICE_ERROR;
856         }
857
858         /* count capsule files */
859         ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
860                                              EFI_CAPSULE_DIR,
861                                              EFI_FILE_MODE_READ, 0));
862         if (ret != EFI_SUCCESS) {
863                 *num = 0;
864                 return EFI_SUCCESS;
865         }
866
867         dirent_size = 256;
868         dirent = malloc(dirent_size);
869         if (!dirent)
870                 return EFI_OUT_OF_RESOURCES;
871
872         count = 0;
873         while (1) {
874                 tmp_size = dirent_size;
875                 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
876                 if (ret == EFI_BUFFER_TOO_SMALL) {
877                         struct efi_file_info *old_dirent = dirent;
878
879                         dirent = realloc(dirent, tmp_size);
880                         if (!dirent) {
881                                 dirent = old_dirent;
882                                 ret = EFI_OUT_OF_RESOURCES;
883                                 goto err;
884                         }
885                         dirent_size = tmp_size;
886                         ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
887                 }
888                 if (ret != EFI_SUCCESS)
889                         goto err;
890                 if (!tmp_size)
891                         break;
892
893                 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
894                         count++;
895         }
896
897         ret = EFI_CALL((*dirh->setpos)(dirh, 0));
898         if (ret != EFI_SUCCESS)
899                 goto err;
900
901         /* make a list */
902         tmp_files = malloc(count * sizeof(*tmp_files));
903         if (!tmp_files) {
904                 ret = EFI_OUT_OF_RESOURCES;
905                 goto err;
906         }
907
908         count = 0;
909         while (1) {
910                 tmp_size = dirent_size;
911                 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
912                 if (ret != EFI_SUCCESS)
913                         goto err;
914                 if (!tmp_size)
915                         break;
916
917                 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
918                     u16_strcmp(dirent->file_name, u".") &&
919                     u16_strcmp(dirent->file_name, u".."))
920                         tmp_files[count++] = u16_strdup(dirent->file_name);
921         }
922         /* ignore an error */
923         EFI_CALL((*dirh->close)(dirh));
924
925         /* in ascii order */
926         /* FIXME: u16 version of strcasecmp */
927         qsort(tmp_files, count, sizeof(*tmp_files),
928               (int (*)(const void *, const void *))strcasecmp);
929         *files = tmp_files;
930         *num = count;
931         ret = EFI_SUCCESS;
932 err:
933         free(dirent);
934
935         return ret;
936 }
937
938 /**
939  * efi_capsule_read_file - read in a capsule file
940  * @filename:   File name
941  * @capsule:    Pointer to buffer for capsule
942  *
943  * Read a capsule file and put its content in @capsule.
944  *
945  * Return:      status code
946  */
947 static efi_status_t efi_capsule_read_file(const u16 *filename,
948                                           struct efi_capsule_header **capsule)
949 {
950         struct efi_file_handle *dirh, *fh;
951         struct efi_file_info *file_info = NULL;
952         struct efi_capsule_header *buf = NULL;
953         efi_uintn_t size;
954         efi_status_t ret;
955
956         ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
957                                              EFI_CAPSULE_DIR,
958                                              EFI_FILE_MODE_READ, 0));
959         if (ret != EFI_SUCCESS)
960                 return ret;
961         ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
962                                      EFI_FILE_MODE_READ, 0));
963         /* ignore an error */
964         EFI_CALL((*dirh->close)(dirh));
965         if (ret != EFI_SUCCESS)
966                 return ret;
967
968         /* file size */
969         size = 0;
970         ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
971                                       &size, file_info));
972         if (ret == EFI_BUFFER_TOO_SMALL) {
973                 file_info = malloc(size);
974                 if (!file_info) {
975                         ret = EFI_OUT_OF_RESOURCES;
976                         goto err;
977                 }
978                 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
979                                               &size, file_info));
980         }
981         if (ret != EFI_SUCCESS)
982                 goto err;
983         size = file_info->file_size;
984         free(file_info);
985         buf = malloc(size);
986         if (!buf) {
987                 ret = EFI_OUT_OF_RESOURCES;
988                 goto err;
989         }
990
991         /* fetch data */
992         ret = EFI_CALL((*fh->read)(fh, &size, buf));
993         if (ret == EFI_SUCCESS) {
994                 if (size >= buf->capsule_image_size) {
995                         *capsule = buf;
996                 } else {
997                         free(buf);
998                         ret = EFI_INVALID_PARAMETER;
999                 }
1000         } else {
1001                 free(buf);
1002         }
1003 err:
1004         EFI_CALL((*fh->close)(fh));
1005
1006         return ret;
1007 }
1008
1009 /**
1010  * efi_capsule_delete_file - delete a capsule file
1011  * @filename:   File name
1012  *
1013  * Delete a capsule file from capsule directory.
1014  *
1015  * Return:      status code
1016  */
1017 static efi_status_t efi_capsule_delete_file(const u16 *filename)
1018 {
1019         struct efi_file_handle *dirh, *fh;
1020         efi_status_t ret;
1021
1022         ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1023                                              EFI_CAPSULE_DIR,
1024                                              EFI_FILE_MODE_READ, 0));
1025         if (ret != EFI_SUCCESS)
1026                 return ret;
1027         ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1028                                      EFI_FILE_MODE_READ, 0));
1029         /* ignore an error */
1030         EFI_CALL((*dirh->close)(dirh));
1031
1032         if (ret == EFI_SUCCESS)
1033                 ret = EFI_CALL((*fh->delete)(fh));
1034
1035         return ret;
1036 }
1037
1038 /**
1039  * efi_capsule_scan_done - reset a scan help function
1040  *
1041  * Reset a scan help function
1042  */
1043 static void efi_capsule_scan_done(void)
1044 {
1045         EFI_CALL((*bootdev_root->close)(bootdev_root));
1046         bootdev_root = NULL;
1047 }
1048
1049 /**
1050  * check_run_capsules() - check whether capsule update should run
1051  *
1052  * The spec says OsIndications must be set in order to run the capsule update
1053  * on-disk.  Since U-Boot doesn't support runtime SetVariable, allow capsules to
1054  * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
1055  *
1056  * Return:      EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
1057  */
1058 static efi_status_t check_run_capsules(void)
1059 {
1060         u64 os_indications = 0x0;
1061         efi_uintn_t size;
1062         efi_status_t r;
1063
1064         size = sizeof(os_indications);
1065         r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
1066                                  NULL, &size, &os_indications, NULL);
1067         if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1068             (r != EFI_SUCCESS || size != sizeof(os_indications)))
1069                 return EFI_NOT_FOUND;
1070
1071         if (os_indications &
1072             EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1073                 os_indications &=
1074                         ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
1075                 r = efi_set_variable_int(u"OsIndications",
1076                                          &efi_global_variable_guid,
1077                                          EFI_VARIABLE_NON_VOLATILE |
1078                                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1079                                          EFI_VARIABLE_RUNTIME_ACCESS,
1080                                          sizeof(os_indications),
1081                                          &os_indications, false);
1082                 if (r != EFI_SUCCESS)
1083                         log_err("Setting %ls failed\n", L"OsIndications");
1084                 return EFI_SUCCESS;
1085         } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1086                 return EFI_SUCCESS;
1087         } else {
1088                 return EFI_NOT_FOUND;
1089         }
1090 }
1091
1092 /**
1093  * efi_launch_capsule - launch capsules
1094  *
1095  * Launch all the capsules in system at boot time.
1096  * Called by efi init code
1097  *
1098  * Return:      status codde
1099  */
1100 efi_status_t efi_launch_capsules(void)
1101 {
1102         struct efi_capsule_header *capsule = NULL;
1103         u16 **files;
1104         unsigned int nfiles, index, i;
1105         efi_status_t ret;
1106
1107         if (check_run_capsules() != EFI_SUCCESS)
1108                 return EFI_SUCCESS;
1109
1110         index = get_last_capsule();
1111
1112         /*
1113          * Find capsules on disk.
1114          * All the capsules are collected at the beginning because
1115          * capsule files will be removed instantly.
1116          */
1117         nfiles = 0;
1118         files = NULL;
1119         ret = efi_capsule_scan_dir(&files, &nfiles);
1120         if (ret != EFI_SUCCESS)
1121                 return ret;
1122         if (!nfiles)
1123                 return EFI_SUCCESS;
1124
1125         /* Launch capsules */
1126         for (i = 0, ++index; i < nfiles; i++, index++) {
1127                 log_debug("Applying %ls\n", files[i]);
1128                 if (index > 0xffff)
1129                         index = 0;
1130                 ret = efi_capsule_read_file(files[i], &capsule);
1131                 if (ret == EFI_SUCCESS) {
1132                         ret = efi_capsule_update_firmware(capsule);
1133                         if (ret != EFI_SUCCESS)
1134                                 log_err("Applying capsule %ls failed.\n",
1135                                         files[i]);
1136                         else
1137                                 log_info("Applying capsule %ls succeeded.\n",
1138                                          files[i]);
1139
1140                         /* create CapsuleXXXX */
1141                         set_capsule_result(index, capsule, ret);
1142
1143                         free(capsule);
1144                 } else {
1145                         log_err("Reading capsule %ls failed\n", files[i]);
1146                 }
1147                 /* delete a capsule either in case of success or failure */
1148                 ret = efi_capsule_delete_file(files[i]);
1149                 if (ret != EFI_SUCCESS)
1150                         log_err("Deleting capsule %ls failed\n",
1151                                 files[i]);
1152         }
1153         efi_capsule_scan_done();
1154
1155         for (i = 0; i < nfiles; i++)
1156                 free(files[i]);
1157         free(files);
1158
1159         /*
1160          * UEFI spec requires to reset system after complete processing capsule
1161          * update on the storage.
1162          */
1163         log_info("Reboot after firmware update.\n");
1164         /* Cold reset is required for loading the new firmware. */
1165         sysreset_walk_halt(SYSRESET_COLD);
1166         hang();
1167         /* not reach here */
1168
1169         return 0;
1170 }
1171 #endif /* CONFIG_EFI_CAPSULE_ON_DISK */