Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Bootmethod for distro boot via EFI |
| 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> |
| 17 | #include <efi_loader.h> |
| 18 | #include <fs.h> |
| 19 | #include <malloc.h> |
| 20 | #include <mapmem.h> |
| 21 | #include <mmc.h> |
| 22 | #include <pxe_utils.h> |
| 23 | |
| 24 | #define EFI_DIRNAME "efi/boot/" |
| 25 | |
| 26 | /** |
| 27 | * get_efi_leafname() - Get the leaf name for the EFI file we expect |
| 28 | * |
| 29 | * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi". |
| 30 | * Must have at least 16 bytes of space |
| 31 | * @max_len: Length of @str, must be >=16 |
| 32 | */ |
| 33 | static int get_efi_leafname(char *str, int max_len) |
| 34 | { |
| 35 | const char *base; |
| 36 | |
| 37 | if (max_len < 16) |
| 38 | return log_msg_ret("spc", -ENOSPC); |
| 39 | if (IS_ENABLED(CONFIG_ARM64)) |
| 40 | base = "bootaa64"; |
| 41 | else if (IS_ENABLED(CONFIG_ARM)) |
| 42 | base = "bootarm"; |
| 43 | else if (IS_ENABLED(CONFIG_X86_RUN_32BIT)) |
| 44 | base = "bootia32"; |
| 45 | else if (IS_ENABLED(CONFIG_X86_RUN_64BIT)) |
| 46 | base = "bootx64"; |
| 47 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) |
| 48 | base = "bootriscv32"; |
| 49 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) |
| 50 | base = "bootriscv64"; |
| 51 | else if (IS_ENABLED(CONFIG_SANDBOX)) |
| 52 | base = "bootsbox"; |
| 53 | else |
| 54 | return -EINVAL; |
| 55 | |
| 56 | strcpy(str, base); |
| 57 | strcat(str, ".efi"); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow) |
| 63 | { |
| 64 | const struct udevice *media_dev; |
| 65 | int size = bflow->size; |
| 66 | const char *dev_name; |
| 67 | char devnum_str[9]; |
| 68 | char dirname[200]; |
| 69 | char *last_slash; |
| 70 | int ret; |
| 71 | |
| 72 | ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000); |
| 73 | if (ret) |
| 74 | return log_msg_ret("read", ret); |
| 75 | |
| 76 | /* |
| 77 | * This is a horrible hack to tell EFI about this boot device. Once we |
| 78 | * unify EFI with the rest of U-Boot we can clean this up. The same hack |
| 79 | * exists in multiple places, e.g. in the fs, tftp and load commands. |
| 80 | * |
| 81 | * Once we can clean up the EFI code to make proper use of driver model, |
| 82 | * this can go away. |
| 83 | */ |
| 84 | media_dev = dev_get_parent(bflow->dev); |
| 85 | snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev)); |
| 86 | |
| 87 | strlcpy(dirname, bflow->fname, sizeof(dirname)); |
| 88 | last_slash = strrchr(dirname, '/'); |
| 89 | if (last_slash) |
| 90 | *last_slash = '\0'; |
| 91 | |
| 92 | log_debug("setting bootdev %s, %s, %s, %p, %x\n", |
| 93 | dev_get_uclass_name(media_dev), devnum_str, bflow->fname, |
| 94 | bflow->buf, size); |
| 95 | dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ? |
| 96 | "usb" : dev_get_uclass_name(media_dev); |
| 97 | efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) |
| 103 | { |
| 104 | int ret; |
| 105 | |
| 106 | /* This only works on block devices */ |
| 107 | ret = bootflow_iter_uses_blk_dev(iter); |
| 108 | if (ret) |
| 109 | return log_msg_ret("blk", ret); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 115 | { |
| 116 | struct blk_desc *desc = NULL; |
| 117 | char fname[sizeof(EFI_DIRNAME) + 16]; |
| 118 | int ret; |
| 119 | |
| 120 | /* We require a partition table */ |
| 121 | if (!bflow->part) |
| 122 | return -ENOENT; |
| 123 | |
| 124 | strcpy(fname, EFI_DIRNAME); |
| 125 | ret = get_efi_leafname(fname + strlen(fname), |
| 126 | sizeof(fname) - strlen(fname)); |
| 127 | if (ret) |
| 128 | return log_msg_ret("leaf", ret); |
| 129 | |
| 130 | if (bflow->blk) |
| 131 | desc = dev_get_uclass_plat(bflow->blk); |
| 132 | ret = bootmeth_try_file(bflow, desc, NULL, fname); |
| 133 | if (ret) |
| 134 | return log_msg_ret("try", ret); |
| 135 | |
| 136 | ret = efiload_read_file(desc, bflow); |
| 137 | if (ret) |
| 138 | return log_msg_ret("read", -EINVAL); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) |
| 144 | { |
| 145 | char cmd[50]; |
| 146 | |
| 147 | /* |
| 148 | * At some point we can add a real interface to bootefi so we can call |
| 149 | * this directly. For now, go through the CLI like distro boot. |
| 150 | */ |
| 151 | snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", |
| 152 | (ulong)map_to_sysmem(bflow->buf), |
| 153 | (ulong)map_to_sysmem(gd->fdt_blob)); |
| 154 | if (run_command(cmd, 0)) |
| 155 | return log_msg_ret("run", -EINVAL); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int distro_bootmeth_efi_bind(struct udevice *dev) |
| 161 | { |
| 162 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 163 | |
| 164 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 165 | "EFI boot from an .efi file" : "EFI"; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static struct bootmeth_ops distro_efi_bootmeth_ops = { |
| 171 | .check = distro_efi_check, |
| 172 | .read_bootflow = distro_efi_read_bootflow, |
| 173 | .read_file = bootmeth_common_read_file, |
| 174 | .boot = distro_efi_boot, |
| 175 | }; |
| 176 | |
| 177 | static const struct udevice_id distro_efi_bootmeth_ids[] = { |
| 178 | { .compatible = "u-boot,distro-efi" }, |
| 179 | { } |
| 180 | }; |
| 181 | |
| 182 | U_BOOT_DRIVER(bootmeth_efi) = { |
| 183 | .name = "bootmeth_efi", |
| 184 | .id = UCLASS_BOOTMETH, |
| 185 | .of_match = distro_efi_bootmeth_ids, |
| 186 | .ops = &distro_efi_bootmeth_ops, |
| 187 | .bind = distro_bootmeth_efi_bind, |
| 188 | }; |