Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Bootmethod for EFI boot manager |
| 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <bootdev.h> |
| 13 | #include <bootflow.h> |
| 14 | #include <bootmeth.h> |
| 15 | #include <command.h> |
| 16 | #include <dm.h> |
Mark Kettenis | f98bb0b | 2023-09-03 22:40:00 +0200 | [diff] [blame] | 17 | #include <efi_loader.h> |
| 18 | #include <efi_variable.h> |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 19 | |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 20 | /** |
| 21 | * struct efi_mgr_priv - private info for the efi-mgr driver |
| 22 | * |
| 23 | * @fake_bootflow: Fake a valid bootflow for testing |
| 24 | */ |
| 25 | struct efi_mgr_priv { |
| 26 | bool fake_dev; |
| 27 | }; |
| 28 | |
| 29 | void sandbox_set_fake_efi_mgr_dev(struct udevice *dev, bool fake_dev) |
| 30 | { |
| 31 | struct efi_mgr_priv *priv = dev_get_priv(dev); |
| 32 | |
| 33 | priv->fake_dev = fake_dev; |
| 34 | } |
| 35 | |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 36 | static int efi_mgr_check(struct udevice *dev, struct bootflow_iter *iter) |
| 37 | { |
| 38 | int ret; |
| 39 | |
| 40 | /* Must be an bootstd device */ |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 41 | ret = bootflow_iter_check_system(iter); |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 42 | if (ret) |
| 43 | return log_msg_ret("net", ret); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int efi_mgr_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 49 | { |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 50 | struct efi_mgr_priv *priv = dev_get_priv(dev); |
Mark Kettenis | f98bb0b | 2023-09-03 22:40:00 +0200 | [diff] [blame] | 51 | efi_status_t ret; |
| 52 | efi_uintn_t size; |
| 53 | u16 *bootorder; |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 54 | |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 55 | if (priv->fake_dev) { |
| 56 | bflow->state = BOOTFLOWST_READY; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Mark Kettenis | f98bb0b | 2023-09-03 22:40:00 +0200 | [diff] [blame] | 60 | ret = efi_init_obj_list(); |
| 61 | if (ret) |
| 62 | return log_msg_ret("init", ret); |
| 63 | |
| 64 | /* Enable this method if the "BootOrder" UEFI exists. */ |
| 65 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, |
| 66 | &size); |
| 67 | if (bootorder) { |
| 68 | bflow->state = BOOTFLOWST_READY; |
| 69 | return 0; |
| 70 | } |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 71 | |
| 72 | return -EINVAL; |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int efi_mgr_read_file(struct udevice *dev, struct bootflow *bflow, |
| 76 | const char *file_path, ulong addr, ulong *sizep) |
| 77 | { |
| 78 | /* Files are loaded by the 'bootefi bootmgr' command */ |
| 79 | |
| 80 | return -ENOSYS; |
| 81 | } |
| 82 | |
| 83 | static int efi_mgr_boot(struct udevice *dev, struct bootflow *bflow) |
| 84 | { |
| 85 | int ret; |
| 86 | |
| 87 | /* Booting is handled by the 'bootefi bootmgr' command */ |
| 88 | ret = run_command("bootefi bootmgr", 0); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int bootmeth_efi_mgr_bind(struct udevice *dev) |
| 94 | { |
| 95 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 96 | |
| 97 | plat->desc = "EFI bootmgr flow"; |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 98 | plat->flags = BOOTMETHF_GLOBAL; |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static struct bootmeth_ops efi_mgr_bootmeth_ops = { |
| 104 | .check = efi_mgr_check, |
| 105 | .read_bootflow = efi_mgr_read_bootflow, |
| 106 | .read_file = efi_mgr_read_file, |
| 107 | .boot = efi_mgr_boot, |
| 108 | }; |
| 109 | |
| 110 | static const struct udevice_id efi_mgr_bootmeth_ids[] = { |
| 111 | { .compatible = "u-boot,efi-bootmgr" }, |
| 112 | { } |
| 113 | }; |
| 114 | |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 115 | U_BOOT_DRIVER(bootmeth_efi_mgr) = { |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 116 | .name = "bootmeth_efi_mgr", |
| 117 | .id = UCLASS_BOOTMETH, |
| 118 | .of_match = efi_mgr_bootmeth_ids, |
| 119 | .ops = &efi_mgr_bootmeth_ops, |
| 120 | .bind = bootmeth_efi_mgr_bind, |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 121 | .priv_auto = sizeof(struct efi_mgr_priv), |
Simon Glass | 309e6ec | 2022-04-24 23:31:19 -0600 | [diff] [blame] | 122 | }; |