blob: 0c9b4c3d59dc6505c05ccf5c2fdd02f401a0f048 [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>
18#include <fs.h>
19#include <malloc.h>
20#include <mapmem.h>
21#include <mmc.h>
Simon Glass9e08caa2023-01-17 10:47:55 -070022#include <net.h>
Simon Glass66f62552022-04-24 23:31:17 -060023#include <pxe_utils.h>
Simon Glass02fafec2023-07-26 21:01:24 -060024#include <linux/sizes.h>
Simon Glass66f62552022-04-24 23:31:17 -060025
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020026#define EFI_DIRNAME "/EFI/BOOT/"
Simon Glass66f62552022-04-24 23:31:17 -060027
Simon Glass9e08caa2023-01-17 10:47:55 -070028static int get_efi_pxe_vci(char *str, int max_len)
29{
30 int ret;
31
Simon Glass1824bf62024-11-07 14:31:44 -070032 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -070033 if (ret < 0)
34 return ret;
35
36 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
37
38 return 0;
39}
40
Simon Glass47fc7342023-07-26 21:01:22 -060041/**
42 * bootmeth_uses_network() - check if the media device is Ethernet
43 *
44 * @bflow: Bootflow to check
45 * Returns: true if the media device is Ethernet, else false
46 */
47static bool bootmeth_uses_network(struct bootflow *bflow)
48{
49 const struct udevice *media = dev_get_parent(bflow->dev);
50
51 return IS_ENABLED(CONFIG_CMD_DHCP) &&
52 device_get_uclass_id(media) == UCLASS_ETH;
53}
54
Simon Glass4aec1902023-07-26 21:01:23 -060055static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassacad3622023-04-24 13:49:46 +120056{
Simon Glass4aec1902023-07-26 21:01:23 -060057 struct blk_desc *desc = NULL;
Simon Glass5f27d162024-11-15 16:19:16 -070058 ulong size;
Simon Glassacad3622023-04-24 13:49:46 +120059 int ret;
60
Simon Glass4aec1902023-07-26 21:01:23 -060061 if (bflow->blk)
62 desc = dev_get_uclass_plat(bflow->blk);
Simon Glass4aec1902023-07-26 21:01:23 -060063
Simon Glass5f27d162024-11-15 16:19:16 -070064 size = SZ_1G;
65 ret = bootmeth_common_read_file(bflow->method, bflow, bflow->fname,
Simon Glassf39b5592024-11-15 16:19:17 -070066 addr, BFI_EFI, &size);
Simon Glassacad3622023-04-24 13:49:46 +120067 if (ret)
Simon Glass5f27d162024-11-15 16:19:16 -070068 return log_msg_ret("rdf", ret);
Simon Glass4aec1902023-07-26 21:01:23 -060069 bflow->buf = map_sysmem(addr, bflow->size);
70
Simon Glass66f62552022-04-24 23:31:17 -060071 return 0;
72}
73
74static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
75{
Simon Glass9e08caa2023-01-17 10:47:55 -070076 /* This only works on block and network devices */
77 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
78 return log_msg_ret("blk", -ENOTSUPP);
Simon Glass66f62552022-04-24 23:31:17 -060079
Simon Glasse22fe922023-01-17 10:48:05 -070080 /* This works on block devices and network devices */
81 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
82 return log_msg_ret("pxe", -ENOTSUPP);
83
Simon Glass66f62552022-04-24 23:31:17 -060084 return 0;
85}
86
Simon Glass4aec1902023-07-26 21:01:23 -060087/*
88 * distro_efi_try_bootflow_files() - Check that files are present
89 *
90 * This reads any FDT file and checks whether the bootflow file is present, for
91 * later reading. We avoid reading the bootflow now, since it is likely large,
92 * it may take a long time and we want to avoid needing to allocate memory for
93 * it
94 *
95 * @dev: bootmeth device to use
96 * @bflow: bootflow to update
97 */
98static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass9e08caa2023-01-17 10:47:55 -070099 struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600100{
101 struct blk_desc *desc = NULL;
Simon Glass56bb4e42023-01-17 10:47:57 -0700102 ulong fdt_addr, size;
Simon Glass9e08caa2023-01-17 10:47:55 -0700103 char fname[256];
Simon Glassca84dc82023-02-22 12:17:04 -0700104 int ret, seq;
Simon Glass66f62552022-04-24 23:31:17 -0600105
106 /* We require a partition table */
Simon Glassb3445e02024-09-26 23:59:37 +0200107 if (!bflow->part) {
108 log_debug("no partitions\n");
Simon Glass66f62552022-04-24 23:31:17 -0600109 return -ENOENT;
Simon Glassb3445e02024-09-26 23:59:37 +0200110 }
Simon Glass66f62552022-04-24 23:31:17 -0600111
112 strcpy(fname, EFI_DIRNAME);
Simon Glass3b1e60b2024-11-07 14:31:43 -0700113 strcat(fname, efi_get_basename());
Simon Glass66f62552022-04-24 23:31:17 -0600114
115 if (bflow->blk)
116 desc = dev_get_uclass_plat(bflow->blk);
117 ret = bootmeth_try_file(bflow, desc, NULL, fname);
Simon Glassb3445e02024-09-26 23:59:37 +0200118 if (ret) {
119 log_debug("File '%s' not found\n", fname);
Simon Glass66f62552022-04-24 23:31:17 -0600120 return log_msg_ret("try", ret);
Simon Glassb3445e02024-09-26 23:59:37 +0200121 }
Simon Glass66f62552022-04-24 23:31:17 -0600122
Simon Glass4aec1902023-07-26 21:01:23 -0600123 /* Since we can access the file, let's call it ready */
124 bflow->state = BOOTFLOWST_READY;
Simon Glass66f62552022-04-24 23:31:17 -0600125
Simon Glassca84dc82023-02-22 12:17:04 -0700126 fdt_addr = env_get_hex("fdt_addr_r", 0);
127
128 /* try the various available names */
129 ret = -ENOENT;
Simon Glass40516be2023-04-24 13:49:44 +1200130 *fname = '\0';
131 for (seq = 0; ret == -ENOENT; seq++) {
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200132 ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
Simon Glass40516be2023-04-24 13:49:44 +1200133 if (ret == -EALREADY)
Simon Glassca84dc82023-02-22 12:17:04 -0700134 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass02fafec2023-07-26 21:01:24 -0600135 if (!ret) {
136 /* Limit FDT files to 4MB */
137 size = SZ_4M;
Simon Glass40516be2023-04-24 13:49:44 +1200138 ret = bootmeth_common_read_file(dev, bflow, fname,
Simon Glassf39b5592024-11-15 16:19:17 -0700139 fdt_addr, (enum bootflow_img_t)IH_TYPE_FLATDT,
140 &size);
Simon Glass02fafec2023-07-26 21:01:24 -0600141 }
Simon Glassca84dc82023-02-22 12:17:04 -0700142 }
143
Simon Glass40516be2023-04-24 13:49:44 +1200144 if (*fname) {
145 bflow->fdt_fname = strdup(fname);
146 if (!bflow->fdt_fname)
147 return log_msg_ret("fil", -ENOMEM);
148 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700149
Simon Glass56bb4e42023-01-17 10:47:57 -0700150 if (!ret) {
151 bflow->fdt_size = size;
152 bflow->fdt_addr = fdt_addr;
153
154 /*
155 * TODO: Apply extension overlay
156 *
157 * Here we need to load and apply the extension overlay. This is
158 * not implemented. See do_extension_apply(). The extension
159 * stuff needs an implementation in boot/extension.c so it is
160 * separate from the command code. Really the extension stuff
161 * should use the device tree and a uclass / driver interface
162 * rather than implementing its own list
163 */
164 } else {
165 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000166 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700167 }
168
Simon Glass66f62552022-04-24 23:31:17 -0600169 return 0;
170}
171
Simon Glass9e08caa2023-01-17 10:47:55 -0700172static int distro_efi_read_bootflow_net(struct bootflow *bflow)
173{
Simon Glass56bb4e42023-01-17 10:47:57 -0700174 char file_addr[17], fname[256];
175 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
176 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000177 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass9e08caa2023-01-17 10:47:55 -0700178 int ret, arch, size;
Simon Glass56bb4e42023-01-17 10:47:57 -0700179 ulong addr, fdt_addr;
Simon Glass9e08caa2023-01-17 10:47:55 -0700180 char str[36];
Simon Glass9e08caa2023-01-17 10:47:55 -0700181
182 ret = get_efi_pxe_vci(str, sizeof(str));
183 if (ret)
184 return log_msg_ret("vci", ret);
Simon Glass1824bf62024-11-07 14:31:44 -0700185 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -0700186 if (ret < 0)
187 return log_msg_ret("arc", ret);
188 arch = ret;
189
190 ret = env_set("bootp_vci", str);
191 if (ret)
192 return log_msg_ret("vcs", ret);
Shantur Rathoreb4c12522023-11-19 16:54:58 +0000193 ret = env_set_hex("bootp_arch", arch);
Simon Glass9e08caa2023-01-17 10:47:55 -0700194 if (ret)
195 return log_msg_ret("ars", ret);
196
197 /* figure out the load address */
198 addr_str = env_get("kernel_addr_r");
199 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
200
201 /* clear any previous bootfile */
202 env_set("bootfile", NULL);
203
204 /* read the kernel */
205 ret = dhcp_run(addr, NULL, true);
206 if (ret)
207 return log_msg_ret("dhc", ret);
208
209 size = env_get_hex("filesize", -1);
210 if (size <= 0)
211 return log_msg_ret("sz", -EINVAL);
212 bflow->size = size;
Simon Glassff055b02025-01-23 15:07:24 -0700213 bflow->buf = map_sysmem(addr, size);
Simon Glass9e08caa2023-01-17 10:47:55 -0700214
Simon Glass5b0d9142024-07-17 09:31:03 +0100215 /* bootfile should be setup by dhcp */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000216 bootfile_name = env_get("bootfile");
217 if (!bootfile_name)
218 return log_msg_ret("bootfile_name", ret);
219 bflow->fname = strdup(bootfile_name);
Simon Glass58648912024-11-15 16:19:18 -0700220 if (!bflow->fname)
221 return log_msg_ret("fi0", -ENOMEM);
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000222
Simon Glass56bb4e42023-01-17 10:47:57 -0700223 /* read the DT file also */
224 fdt_addr_str = env_get("fdt_addr_r");
225 if (!fdt_addr_str)
226 return log_msg_ret("fdt", -EINVAL);
227 fdt_addr = hextoul(fdt_addr_str, NULL);
228 sprintf(file_addr, "%lx", fdt_addr);
229
Simon Glassca84dc82023-02-22 12:17:04 -0700230 /* We only allow the first prefix with PXE */
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200231 ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
Simon Glassca84dc82023-02-22 12:17:04 -0700232 if (ret)
233 return log_msg_ret("nam", ret);
234
Simon Glass56bb4e42023-01-17 10:47:57 -0700235 bflow->fdt_fname = strdup(fname);
236 if (!bflow->fdt_fname)
237 return log_msg_ret("fil", -ENOMEM);
238
239 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
240 bflow->fdt_size = env_get_hex("filesize", 0);
241 bflow->fdt_addr = fdt_addr;
242 } else {
243 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000244 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700245 }
246
Simon Glass9e08caa2023-01-17 10:47:55 -0700247 bflow->state = BOOTFLOWST_READY;
248
249 return 0;
250}
251
252static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
253{
Simon Glass9e08caa2023-01-17 10:47:55 -0700254 int ret;
255
Simon Glassb3445e02024-09-26 23:59:37 +0200256 log_debug("dev='%s', part=%d\n", bflow->dev->name, bflow->part);
257
Shantur Rathoreb45d7e22023-11-19 16:55:01 +0000258 /*
259 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
260 * set flag to avoid freeing static buffer.
261 */
262 bflow->flags |= BOOTFLOWF_STATIC_BUF;
263
Simon Glass47fc7342023-07-26 21:01:22 -0600264 if (bootmeth_uses_network(bflow)) {
Simon Glass9e08caa2023-01-17 10:47:55 -0700265 /* we only support reading from one device, so ignore 'dev' */
266 ret = distro_efi_read_bootflow_net(bflow);
267 if (ret)
268 return log_msg_ret("net", ret);
269 } else {
Simon Glass4aec1902023-07-26 21:01:23 -0600270 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass9e08caa2023-01-17 10:47:55 -0700271 if (ret)
272 return log_msg_ret("blk", ret);
273 }
274
275 return 0;
276}
277
Bin Meng79eb5f72023-08-03 17:30:05 +0800278static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600279{
Simon Glass56bb4e42023-01-17 10:47:57 -0700280 ulong kernel, fdt;
Simon Glass4aec1902023-07-26 21:01:23 -0600281 int ret;
Simon Glass66f62552022-04-24 23:31:17 -0600282
Simon Glassb3445e02024-09-26 23:59:37 +0200283 log_debug("distro EFI boot\n");
Simon Glass4aec1902023-07-26 21:01:23 -0600284 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass47fc7342023-07-26 21:01:22 -0600285 if (!bootmeth_uses_network(bflow)) {
Simon Glass4aec1902023-07-26 21:01:23 -0600286 ret = efiload_read_file(bflow, kernel);
287 if (ret)
288 return log_msg_ret("read", ret);
Simon Glass56bb4e42023-01-17 10:47:57 -0700289
290 /*
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000291 * use the provided device tree if not using the built-in fdt
Simon Glass56bb4e42023-01-17 10:47:57 -0700292 */
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000293 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glass56bb4e42023-01-17 10:47:57 -0700294 fdt = bflow->fdt_addr;
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000295
Simon Glass56bb4e42023-01-17 10:47:57 -0700296 }
297
Simon Glassfce57772025-01-23 15:07:23 -0700298 if (efi_bootflow_run(bflow))
299 return log_msg_ret("run", -EINVAL);
Simon Glass66f62552022-04-24 23:31:17 -0600300
301 return 0;
302}
303
304static int distro_bootmeth_efi_bind(struct udevice *dev)
305{
306 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
307
308 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
309 "EFI boot from an .efi file" : "EFI";
310
311 return 0;
312}
313
314static struct bootmeth_ops distro_efi_bootmeth_ops = {
315 .check = distro_efi_check,
316 .read_bootflow = distro_efi_read_bootflow,
317 .read_file = bootmeth_common_read_file,
318 .boot = distro_efi_boot,
319};
320
321static const struct udevice_id distro_efi_bootmeth_ids[] = {
322 { .compatible = "u-boot,distro-efi" },
323 { }
324};
325
Simon Glasse5cb0182024-07-17 09:30:59 +0100326/* Put a number before 'efi' to provide a default ordering */
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200327U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glass66f62552022-04-24 23:31:17 -0600328 .name = "bootmeth_efi",
329 .id = UCLASS_BOOTMETH,
330 .of_match = distro_efi_bootmeth_ids,
331 .ops = &distro_efi_bootmeth_ops,
332 .bind = distro_bootmeth_efi_bind,
333};