blob: 0af23df3a4a551adc230469041eadac885be7424 [file] [log] [blame]
Simon Glass66f62552022-04-24 23:31:17 -06001// 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
Simon Glass66f62552022-04-24 23:31:17 -060011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
14#include <command.h>
15#include <dm.h>
Simon Glass3b1e60b2024-11-07 14:31:43 -070016#include <efi.h>
Simon Glass66f62552022-04-24 23:31:17 -060017#include <efi_loader.h>
Tom Rinic31301c2025-05-15 17:31:50 -060018#include <env.h>
Simon Glass66f62552022-04-24 23:31:17 -060019#include <fs.h>
20#include <malloc.h>
21#include <mapmem.h>
22#include <mmc.h>
Simon Glass9e08caa2023-01-17 10:47:55 -070023#include <net.h>
Simon Glass66f62552022-04-24 23:31:17 -060024#include <pxe_utils.h>
Simon Glass02fafec2023-07-26 21:01:24 -060025#include <linux/sizes.h>
Simon Glass66f62552022-04-24 23:31:17 -060026
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020027#define EFI_DIRNAME "/EFI/BOOT/"
Simon Glass66f62552022-04-24 23:31:17 -060028
Simon Glass9e08caa2023-01-17 10:47:55 -070029static int get_efi_pxe_vci(char *str, int max_len)
30{
31 int ret;
32
Simon Glass1824bf62024-11-07 14:31:44 -070033 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -070034 if (ret < 0)
35 return ret;
36
37 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
38
39 return 0;
40}
41
Simon Glass47fc7342023-07-26 21:01:22 -060042/**
43 * bootmeth_uses_network() - check if the media device is Ethernet
44 *
45 * @bflow: Bootflow to check
46 * Returns: true if the media device is Ethernet, else false
47 */
48static bool bootmeth_uses_network(struct bootflow *bflow)
49{
50 const struct udevice *media = dev_get_parent(bflow->dev);
51
52 return IS_ENABLED(CONFIG_CMD_DHCP) &&
53 device_get_uclass_id(media) == UCLASS_ETH;
54}
55
Simon Glass4aec1902023-07-26 21:01:23 -060056static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassacad3622023-04-24 13:49:46 +120057{
Simon Glass4aec1902023-07-26 21:01:23 -060058 struct blk_desc *desc = NULL;
Simon Glass5f27d162024-11-15 16:19:16 -070059 ulong size;
Simon Glassacad3622023-04-24 13:49:46 +120060 int ret;
61
Simon Glass4aec1902023-07-26 21:01:23 -060062 if (bflow->blk)
63 desc = dev_get_uclass_plat(bflow->blk);
Simon Glass4aec1902023-07-26 21:01:23 -060064
Simon Glass5f27d162024-11-15 16:19:16 -070065 size = SZ_1G;
66 ret = bootmeth_common_read_file(bflow->method, bflow, bflow->fname,
Simon Glassf39b5592024-11-15 16:19:17 -070067 addr, BFI_EFI, &size);
Simon Glassacad3622023-04-24 13:49:46 +120068 if (ret)
Simon Glass5f27d162024-11-15 16:19:16 -070069 return log_msg_ret("rdf", ret);
Simon Glass4aec1902023-07-26 21:01:23 -060070 bflow->buf = map_sysmem(addr, bflow->size);
71
Simon Glass66f62552022-04-24 23:31:17 -060072 return 0;
73}
74
75static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
76{
Simon Glass9e08caa2023-01-17 10:47:55 -070077 /* This only works on block and network devices */
78 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
79 return log_msg_ret("blk", -ENOTSUPP);
Simon Glass66f62552022-04-24 23:31:17 -060080
Simon Glasse22fe922023-01-17 10:48:05 -070081 /* This works on block devices and network devices */
82 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
83 return log_msg_ret("pxe", -ENOTSUPP);
84
Simon Glass66f62552022-04-24 23:31:17 -060085 return 0;
86}
87
Simon Glass4aec1902023-07-26 21:01:23 -060088/*
89 * distro_efi_try_bootflow_files() - Check that files are present
90 *
91 * This reads any FDT file and checks whether the bootflow file is present, for
92 * later reading. We avoid reading the bootflow now, since it is likely large,
93 * it may take a long time and we want to avoid needing to allocate memory for
94 * it
95 *
96 * @dev: bootmeth device to use
97 * @bflow: bootflow to update
98 */
99static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass9e08caa2023-01-17 10:47:55 -0700100 struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600101{
102 struct blk_desc *desc = NULL;
Simon Glass56bb4e42023-01-17 10:47:57 -0700103 ulong fdt_addr, size;
Simon Glass9e08caa2023-01-17 10:47:55 -0700104 char fname[256];
Simon Glassca84dc82023-02-22 12:17:04 -0700105 int ret, seq;
Simon Glass66f62552022-04-24 23:31:17 -0600106
107 /* We require a partition table */
Simon Glassb3445e02024-09-26 23:59:37 +0200108 if (!bflow->part) {
109 log_debug("no partitions\n");
Simon Glass66f62552022-04-24 23:31:17 -0600110 return -ENOENT;
Simon Glassb3445e02024-09-26 23:59:37 +0200111 }
Simon Glass66f62552022-04-24 23:31:17 -0600112
113 strcpy(fname, EFI_DIRNAME);
Simon Glass3b1e60b2024-11-07 14:31:43 -0700114 strcat(fname, efi_get_basename());
Simon Glass66f62552022-04-24 23:31:17 -0600115
116 if (bflow->blk)
117 desc = dev_get_uclass_plat(bflow->blk);
118 ret = bootmeth_try_file(bflow, desc, NULL, fname);
Simon Glassb3445e02024-09-26 23:59:37 +0200119 if (ret) {
120 log_debug("File '%s' not found\n", fname);
Simon Glass66f62552022-04-24 23:31:17 -0600121 return log_msg_ret("try", ret);
Simon Glassb3445e02024-09-26 23:59:37 +0200122 }
Simon Glass66f62552022-04-24 23:31:17 -0600123
Simon Glass4aec1902023-07-26 21:01:23 -0600124 /* Since we can access the file, let's call it ready */
125 bflow->state = BOOTFLOWST_READY;
Simon Glass66f62552022-04-24 23:31:17 -0600126
Simon Glassca84dc82023-02-22 12:17:04 -0700127 fdt_addr = env_get_hex("fdt_addr_r", 0);
128
129 /* try the various available names */
130 ret = -ENOENT;
Simon Glass40516be2023-04-24 13:49:44 +1200131 *fname = '\0';
132 for (seq = 0; ret == -ENOENT; seq++) {
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200133 ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
Simon Glass40516be2023-04-24 13:49:44 +1200134 if (ret == -EALREADY)
Simon Glassca84dc82023-02-22 12:17:04 -0700135 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass02fafec2023-07-26 21:01:24 -0600136 if (!ret) {
137 /* Limit FDT files to 4MB */
138 size = SZ_4M;
Simon Glass40516be2023-04-24 13:49:44 +1200139 ret = bootmeth_common_read_file(dev, bflow, fname,
Simon Glassf39b5592024-11-15 16:19:17 -0700140 fdt_addr, (enum bootflow_img_t)IH_TYPE_FLATDT,
141 &size);
Simon Glass02fafec2023-07-26 21:01:24 -0600142 }
Simon Glassca84dc82023-02-22 12:17:04 -0700143 }
144
Simon Glass40516be2023-04-24 13:49:44 +1200145 if (*fname) {
146 bflow->fdt_fname = strdup(fname);
147 if (!bflow->fdt_fname)
148 return log_msg_ret("fil", -ENOMEM);
149 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700150
Simon Glass56bb4e42023-01-17 10:47:57 -0700151 if (!ret) {
152 bflow->fdt_size = size;
153 bflow->fdt_addr = fdt_addr;
154
155 /*
156 * TODO: Apply extension overlay
157 *
158 * Here we need to load and apply the extension overlay. This is
159 * not implemented. See do_extension_apply(). The extension
160 * stuff needs an implementation in boot/extension.c so it is
161 * separate from the command code. Really the extension stuff
162 * should use the device tree and a uclass / driver interface
163 * rather than implementing its own list
164 */
165 } else {
166 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000167 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700168 }
169
Simon Glass66f62552022-04-24 23:31:17 -0600170 return 0;
171}
172
Simon Glass9e08caa2023-01-17 10:47:55 -0700173static int distro_efi_read_bootflow_net(struct bootflow *bflow)
174{
Simon Glass56bb4e42023-01-17 10:47:57 -0700175 char file_addr[17], fname[256];
176 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
177 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000178 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass9e08caa2023-01-17 10:47:55 -0700179 int ret, arch, size;
Simon Glass56bb4e42023-01-17 10:47:57 -0700180 ulong addr, fdt_addr;
Simon Glass9e08caa2023-01-17 10:47:55 -0700181 char str[36];
Simon Glass9e08caa2023-01-17 10:47:55 -0700182
183 ret = get_efi_pxe_vci(str, sizeof(str));
184 if (ret)
185 return log_msg_ret("vci", ret);
Simon Glass1824bf62024-11-07 14:31:44 -0700186 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -0700187 if (ret < 0)
188 return log_msg_ret("arc", ret);
189 arch = ret;
190
191 ret = env_set("bootp_vci", str);
192 if (ret)
193 return log_msg_ret("vcs", ret);
Shantur Rathoreb4c12522023-11-19 16:54:58 +0000194 ret = env_set_hex("bootp_arch", arch);
Simon Glass9e08caa2023-01-17 10:47:55 -0700195 if (ret)
196 return log_msg_ret("ars", ret);
197
198 /* figure out the load address */
199 addr_str = env_get("kernel_addr_r");
200 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
201
202 /* clear any previous bootfile */
203 env_set("bootfile", NULL);
204
205 /* read the kernel */
206 ret = dhcp_run(addr, NULL, true);
207 if (ret)
208 return log_msg_ret("dhc", ret);
209
210 size = env_get_hex("filesize", -1);
211 if (size <= 0)
212 return log_msg_ret("sz", -EINVAL);
213 bflow->size = size;
Simon Glassff055b02025-01-23 15:07:24 -0700214 bflow->buf = map_sysmem(addr, size);
Simon Glass9e08caa2023-01-17 10:47:55 -0700215
Simon Glass5b0d9142024-07-17 09:31:03 +0100216 /* bootfile should be setup by dhcp */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000217 bootfile_name = env_get("bootfile");
218 if (!bootfile_name)
219 return log_msg_ret("bootfile_name", ret);
220 bflow->fname = strdup(bootfile_name);
Simon Glass58648912024-11-15 16:19:18 -0700221 if (!bflow->fname)
222 return log_msg_ret("fi0", -ENOMEM);
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000223
Simon Glass56bb4e42023-01-17 10:47:57 -0700224 /* read the DT file also */
225 fdt_addr_str = env_get("fdt_addr_r");
226 if (!fdt_addr_str)
227 return log_msg_ret("fdt", -EINVAL);
228 fdt_addr = hextoul(fdt_addr_str, NULL);
229 sprintf(file_addr, "%lx", fdt_addr);
230
Simon Glassca84dc82023-02-22 12:17:04 -0700231 /* We only allow the first prefix with PXE */
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200232 ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
Simon Glassca84dc82023-02-22 12:17:04 -0700233 if (ret)
234 return log_msg_ret("nam", ret);
235
Simon Glass56bb4e42023-01-17 10:47:57 -0700236 bflow->fdt_fname = strdup(fname);
237 if (!bflow->fdt_fname)
238 return log_msg_ret("fil", -ENOMEM);
239
240 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
241 bflow->fdt_size = env_get_hex("filesize", 0);
242 bflow->fdt_addr = fdt_addr;
243 } else {
244 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000245 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700246 }
247
Simon Glass9e08caa2023-01-17 10:47:55 -0700248 bflow->state = BOOTFLOWST_READY;
249
250 return 0;
251}
252
253static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
254{
Simon Glass9e08caa2023-01-17 10:47:55 -0700255 int ret;
256
Simon Glassb3445e02024-09-26 23:59:37 +0200257 log_debug("dev='%s', part=%d\n", bflow->dev->name, bflow->part);
258
Shantur Rathoreb45d7e22023-11-19 16:55:01 +0000259 /*
260 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
261 * set flag to avoid freeing static buffer.
262 */
263 bflow->flags |= BOOTFLOWF_STATIC_BUF;
264
Simon Glass47fc7342023-07-26 21:01:22 -0600265 if (bootmeth_uses_network(bflow)) {
Simon Glass9e08caa2023-01-17 10:47:55 -0700266 /* we only support reading from one device, so ignore 'dev' */
267 ret = distro_efi_read_bootflow_net(bflow);
268 if (ret)
269 return log_msg_ret("net", ret);
270 } else {
Simon Glass4aec1902023-07-26 21:01:23 -0600271 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass9e08caa2023-01-17 10:47:55 -0700272 if (ret)
273 return log_msg_ret("blk", ret);
274 }
275
276 return 0;
277}
278
Bin Meng79eb5f72023-08-03 17:30:05 +0800279static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600280{
Simon Glass56bb4e42023-01-17 10:47:57 -0700281 ulong kernel, fdt;
Simon Glass4aec1902023-07-26 21:01:23 -0600282 int ret;
Simon Glass66f62552022-04-24 23:31:17 -0600283
Simon Glassb3445e02024-09-26 23:59:37 +0200284 log_debug("distro EFI boot\n");
Simon Glass4aec1902023-07-26 21:01:23 -0600285 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass47fc7342023-07-26 21:01:22 -0600286 if (!bootmeth_uses_network(bflow)) {
Simon Glass4aec1902023-07-26 21:01:23 -0600287 ret = efiload_read_file(bflow, kernel);
288 if (ret)
289 return log_msg_ret("read", ret);
Simon Glass56bb4e42023-01-17 10:47:57 -0700290
291 /*
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000292 * use the provided device tree if not using the built-in fdt
Simon Glass56bb4e42023-01-17 10:47:57 -0700293 */
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000294 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glass56bb4e42023-01-17 10:47:57 -0700295 fdt = bflow->fdt_addr;
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000296
Simon Glass56bb4e42023-01-17 10:47:57 -0700297 }
298
Simon Glassfce57772025-01-23 15:07:23 -0700299 if (efi_bootflow_run(bflow))
300 return log_msg_ret("run", -EINVAL);
Simon Glass66f62552022-04-24 23:31:17 -0600301
302 return 0;
303}
304
305static int distro_bootmeth_efi_bind(struct udevice *dev)
306{
307 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
308
309 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
310 "EFI boot from an .efi file" : "EFI";
311
312 return 0;
313}
314
315static struct bootmeth_ops distro_efi_bootmeth_ops = {
316 .check = distro_efi_check,
317 .read_bootflow = distro_efi_read_bootflow,
318 .read_file = bootmeth_common_read_file,
319 .boot = distro_efi_boot,
320};
321
322static const struct udevice_id distro_efi_bootmeth_ids[] = {
323 { .compatible = "u-boot,distro-efi" },
324 { }
325};
326
Simon Glasse5cb0182024-07-17 09:30:59 +0100327/* Put a number before 'efi' to provide a default ordering */
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200328U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glass66f62552022-04-24 23:31:17 -0600329 .name = "bootmeth_efi",
330 .id = UCLASS_BOOTMETH,
331 .of_match = distro_efi_bootmeth_ids,
332 .ops = &distro_efi_bootmeth_ops,
333 .bind = distro_bootmeth_efi_bind,
334};