bootstd: Support reading the device tree with EFI
[pandora-u-boot.git] / boot / bootmeth_efi.c
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 <net.h>
23 #include <pxe_utils.h>
24
25 #define EFI_DIRNAME     "efi/boot/"
26
27 /**
28  * get_efi_leafname() - Get the leaf name for the EFI file we expect
29  *
30  * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi".
31  *      Must have at least 16 bytes of space
32  * @max_len: Length of @str, must be >=16
33  */
34 static int get_efi_leafname(char *str, int max_len)
35 {
36         const char *base;
37
38         if (max_len < 16)
39                 return log_msg_ret("spc", -ENOSPC);
40         if (IS_ENABLED(CONFIG_ARM64))
41                 base = "bootaa64";
42         else if (IS_ENABLED(CONFIG_ARM))
43                 base = "bootarm";
44         else if (IS_ENABLED(CONFIG_X86_RUN_32BIT))
45                 base = "bootia32";
46         else if (IS_ENABLED(CONFIG_X86_RUN_64BIT))
47                 base = "bootx64";
48         else if (IS_ENABLED(CONFIG_ARCH_RV32I))
49                 base = "bootriscv32";
50         else if (IS_ENABLED(CONFIG_ARCH_RV64I))
51                 base = "bootriscv64";
52         else if (IS_ENABLED(CONFIG_SANDBOX))
53                 base = "bootsbox";
54         else
55                 return -EINVAL;
56
57         strcpy(str, base);
58         strcat(str, ".efi");
59
60         return 0;
61 }
62
63 static int get_efi_pxe_arch(void)
64 {
65         /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
66         if (IS_ENABLED(CONFIG_ARM64))
67                 return 0xb;
68         else if (IS_ENABLED(CONFIG_ARM))
69                 return 0xa;
70         else if (IS_ENABLED(CONFIG_X86_64))
71                 return 0x6;
72         else if (IS_ENABLED(CONFIG_X86))
73                 return 0x7;
74         else if (IS_ENABLED(CONFIG_ARCH_RV32I))
75                 return 0x19;
76         else if (IS_ENABLED(CONFIG_ARCH_RV64I))
77                 return 0x1b;
78         else if (IS_ENABLED(CONFIG_SANDBOX))
79                 return 0;       /* not used */
80
81         return -EINVAL;
82 }
83
84 static int get_efi_pxe_vci(char *str, int max_len)
85 {
86         int ret;
87
88         ret = get_efi_pxe_arch();
89         if (ret < 0)
90                 return ret;
91
92         snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
93
94         return 0;
95 }
96
97 static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow)
98 {
99         const struct udevice *media_dev;
100         int size = bflow->size;
101         const char *dev_name;
102         char devnum_str[9];
103         char dirname[200];
104         char *last_slash;
105         int ret;
106
107         ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000);
108         if (ret)
109                 return log_msg_ret("read", ret);
110
111         /*
112          * This is a horrible hack to tell EFI about this boot device. Once we
113          * unify EFI with the rest of U-Boot we can clean this up. The same hack
114          * exists in multiple places, e.g. in the fs, tftp and load commands.
115          *
116          * Once we can clean up the EFI code to make proper use of driver model,
117          * this can go away.
118          */
119         media_dev = dev_get_parent(bflow->dev);
120         snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev));
121
122         strlcpy(dirname, bflow->fname, sizeof(dirname));
123         last_slash = strrchr(dirname, '/');
124         if (last_slash)
125                 *last_slash = '\0';
126
127         log_debug("setting bootdev %s, %s, %s, %p, %x\n",
128                   dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
129                   bflow->buf, size);
130         dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
131                  "usb" : dev_get_uclass_name(media_dev);
132         efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
133
134         return 0;
135 }
136
137 static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
138 {
139         /* This only works on block and network devices */
140         if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
141                 return log_msg_ret("blk", -ENOTSUPP);
142
143         return 0;
144 }
145
146 static void distro_efi_get_fdt_name(char *fname, int size)
147 {
148         const char *fdt_fname;
149
150         fdt_fname = env_get("fdtfile");
151         if (fdt_fname) {
152                 snprintf(fname, size, "dtb/%s", fdt_fname);
153                 log_debug("Using device tree: %s\n", fname);
154         } else {
155                 const char *soc = env_get("soc");
156                 const char *board = env_get("board");
157                 const char *boardver = env_get("boardver");
158
159                 /* cf the code in label_boot() which seems very complex */
160                 snprintf(fname, size, "dtb/%s%s%s%s.dtb",
161                          soc ? soc : "", soc ? "-" : "", board ? board : "",
162                          boardver ? boardver : "");
163                 log_debug("Using default device tree: %s\n", fname);
164         }
165 }
166
167 static int distro_efi_read_bootflow_file(struct udevice *dev,
168                                          struct bootflow *bflow)
169 {
170         struct blk_desc *desc = NULL;
171         ulong fdt_addr, size;
172         char fname[256];
173         int ret;
174
175         /* We require a partition table */
176         if (!bflow->part)
177                 return -ENOENT;
178
179         strcpy(fname, EFI_DIRNAME);
180         ret = get_efi_leafname(fname + strlen(fname),
181                                sizeof(fname) - strlen(fname));
182         if (ret)
183                 return log_msg_ret("leaf", ret);
184
185         if (bflow->blk)
186                  desc = dev_get_uclass_plat(bflow->blk);
187         ret = bootmeth_try_file(bflow, desc, NULL, fname);
188         if (ret)
189                 return log_msg_ret("try", ret);
190
191         ret = efiload_read_file(desc, bflow);
192         if (ret)
193                 return log_msg_ret("read", -EINVAL);
194
195         distro_efi_get_fdt_name(fname, sizeof(fname));
196         bflow->fdt_fname = strdup(fname);
197         if (!bflow->fdt_fname)
198                 return log_msg_ret("fil", -ENOMEM);
199
200         fdt_addr = env_get_hex("fdt_addr_r", 0);
201         ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr, &size);
202         if (!ret) {
203                 bflow->fdt_size = size;
204                 bflow->fdt_addr = fdt_addr;
205
206                 /*
207                  * TODO: Apply extension overlay
208                  *
209                  * Here we need to load and apply the extension overlay. This is
210                  * not implemented. See do_extension_apply(). The extension
211                  * stuff needs an implementation in boot/extension.c so it is
212                  * separate from the command code. Really the extension stuff
213                  * should use the device tree and a uclass / driver interface
214                  * rather than implementing its own list
215                  */
216         } else {
217                 log_debug("No device tree available\n");
218         }
219
220         return 0;
221 }
222
223 static int distro_efi_read_bootflow_net(struct bootflow *bflow)
224 {
225         char file_addr[17], fname[256];
226         char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
227         struct cmd_tbl cmdtp = {};      /* dummy */
228         const char *addr_str, *fdt_addr_str;
229         int ret, arch, size;
230         ulong addr, fdt_addr;
231         char str[36];
232
233         ret = get_efi_pxe_vci(str, sizeof(str));
234         if (ret)
235                 return log_msg_ret("vci", ret);
236         ret = get_efi_pxe_arch();
237         if (ret < 0)
238                 return log_msg_ret("arc", ret);
239         arch = ret;
240
241         ret = env_set("bootp_vci", str);
242         if (ret)
243                 return log_msg_ret("vcs", ret);
244         ret = env_set_ulong("bootp_arch", arch);
245         if (ret)
246                 return log_msg_ret("ars", ret);
247
248         /* figure out the load address */
249         addr_str = env_get("kernel_addr_r");
250         addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
251
252         /* clear any previous bootfile */
253         env_set("bootfile", NULL);
254
255         /* read the kernel */
256         ret = dhcp_run(addr, NULL, true);
257         if (ret)
258                 return log_msg_ret("dhc", ret);
259
260         size = env_get_hex("filesize", -1);
261         if (size <= 0)
262                 return log_msg_ret("sz", -EINVAL);
263         bflow->size = size;
264
265         /* do the hideous EFI hack */
266         efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
267                         bflow->size);
268
269         /* read the DT file also */
270         fdt_addr_str = env_get("fdt_addr_r");
271         if (!fdt_addr_str)
272                 return log_msg_ret("fdt", -EINVAL);
273         fdt_addr = hextoul(fdt_addr_str, NULL);
274         sprintf(file_addr, "%lx", fdt_addr);
275
276         distro_efi_get_fdt_name(fname, sizeof(fname));
277         bflow->fdt_fname = strdup(fname);
278         if (!bflow->fdt_fname)
279                 return log_msg_ret("fil", -ENOMEM);
280
281         if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
282                 bflow->fdt_size = env_get_hex("filesize", 0);
283                 bflow->fdt_addr = fdt_addr;
284         } else {
285                 log_debug("No device tree available\n");
286         }
287
288         bflow->state = BOOTFLOWST_READY;
289
290         return 0;
291 }
292
293 static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
294 {
295         const struct udevice *media = dev_get_parent(bflow->dev);
296         int ret;
297
298         if (IS_ENABLED(CONFIG_CMD_DHCP) &&
299             device_get_uclass_id(media) == UCLASS_ETH) {
300                 /* we only support reading from one device, so ignore 'dev' */
301                 ret = distro_efi_read_bootflow_net(bflow);
302                 if (ret)
303                         return log_msg_ret("net", ret);
304         } else {
305                 ret = distro_efi_read_bootflow_file(dev, bflow);
306                 if (ret)
307                         return log_msg_ret("blk", ret);
308         }
309
310         return 0;
311 }
312
313 int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
314 {
315         ulong kernel, fdt;
316         char cmd[50];
317
318         /* A non-zero buffer indicates the kernel is there */
319         if (bflow->buf) {
320                 kernel = (ulong)map_to_sysmem(bflow->buf);
321
322                 /*
323                  * use the provided device tree if available, else fall back to
324                  * the control FDT
325                  */
326                 if (bflow->fdt_fname)
327                         fdt = bflow->fdt_addr;
328                 else
329                         fdt = (ulong)map_to_sysmem(gd->fdt_blob);
330         } else {
331                 /*
332                  * This doesn't actually work for network devices:
333                  *
334                  * do_bootefi_image() No UEFI binary known at 0x02080000
335                  *
336                  * But this is the same behaviour for distro boot, so it can be
337                  * fixed here.
338                  */
339                 kernel = env_get_hex("kernel_addr_r", 0);
340                 fdt = env_get_hex("fdt_addr_r", 0);
341         }
342
343         /*
344          * At some point we can add a real interface to bootefi so we can call
345          * this directly. For now, go through the CLI, like distro boot.
346          */
347         snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
348         if (run_command(cmd, 0))
349                 return log_msg_ret("run", -EINVAL);
350
351         return 0;
352 }
353
354 static int distro_bootmeth_efi_bind(struct udevice *dev)
355 {
356         struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
357
358         plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
359                 "EFI boot from an .efi file" : "EFI";
360
361         return 0;
362 }
363
364 static struct bootmeth_ops distro_efi_bootmeth_ops = {
365         .check          = distro_efi_check,
366         .read_bootflow  = distro_efi_read_bootflow,
367         .read_file      = bootmeth_common_read_file,
368         .boot           = distro_efi_boot,
369 };
370
371 static const struct udevice_id distro_efi_bootmeth_ids[] = {
372         { .compatible = "u-boot,distro-efi" },
373         { }
374 };
375
376 U_BOOT_DRIVER(bootmeth_efi) = {
377         .name           = "bootmeth_efi",
378         .id             = UCLASS_BOOTMETH,
379         .of_match       = distro_efi_bootmeth_ids,
380         .ops            = &distro_efi_bootmeth_ops,
381         .bind           = distro_bootmeth_efi_bind,
382 };