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>
17 
18 /**
19  * struct efi_mgr_priv - private info for the efi-mgr driver
20  *
21  * @fake_bootflow: Fake a valid bootflow for testing
22  */
23 struct efi_mgr_priv {
24 	bool fake_dev;
25 };
26 
sandbox_set_fake_efi_mgr_dev(struct udevice * dev,bool fake_dev)27 void sandbox_set_fake_efi_mgr_dev(struct udevice *dev, bool fake_dev)
28 {
29 	struct efi_mgr_priv *priv = dev_get_priv(dev);
30 
31 	priv->fake_dev = fake_dev;
32 }
33 
efi_mgr_check(struct udevice * dev,struct bootflow_iter * iter)34 static int efi_mgr_check(struct udevice *dev, struct bootflow_iter *iter)
35 {
36 	int ret;
37 
38 	/* Must be an bootstd device */
39 	ret = bootflow_iter_check_system(iter);
40 	if (ret)
41 		return log_msg_ret("net", ret);
42 
43 	return 0;
44 }
45 
efi_mgr_read_bootflow(struct udevice * dev,struct bootflow * bflow)46 static int efi_mgr_read_bootflow(struct udevice *dev, struct bootflow *bflow)
47 {
48 	struct efi_mgr_priv *priv = dev_get_priv(dev);
49 
50 	if (priv->fake_dev) {
51 		bflow->state = BOOTFLOWST_READY;
52 		return 0;
53 	}
54 
55 	/* To be implemented */
56 
57 	return -EINVAL;
58 }
59 
efi_mgr_read_file(struct udevice * dev,struct bootflow * bflow,const char * file_path,ulong addr,ulong * sizep)60 static int efi_mgr_read_file(struct udevice *dev, struct bootflow *bflow,
61 				const char *file_path, ulong addr, ulong *sizep)
62 {
63 	/* Files are loaded by the 'bootefi bootmgr' command */
64 
65 	return -ENOSYS;
66 }
67 
efi_mgr_boot(struct udevice * dev,struct bootflow * bflow)68 static int efi_mgr_boot(struct udevice *dev, struct bootflow *bflow)
69 {
70 	int ret;
71 
72 	/* Booting is handled by the 'bootefi bootmgr' command */
73 	ret = run_command("bootefi bootmgr", 0);
74 
75 	return 0;
76 }
77 
bootmeth_efi_mgr_bind(struct udevice * dev)78 static int bootmeth_efi_mgr_bind(struct udevice *dev)
79 {
80 	struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
81 
82 	plat->desc = "EFI bootmgr flow";
83 	plat->flags = BOOTMETHF_GLOBAL;
84 
85 	return 0;
86 }
87 
88 static struct bootmeth_ops efi_mgr_bootmeth_ops = {
89 	.check		= efi_mgr_check,
90 	.read_bootflow	= efi_mgr_read_bootflow,
91 	.read_file	= efi_mgr_read_file,
92 	.boot		= efi_mgr_boot,
93 };
94 
95 static const struct udevice_id efi_mgr_bootmeth_ids[] = {
96 	{ .compatible = "u-boot,efi-bootmgr" },
97 	{ }
98 };
99 
100 U_BOOT_DRIVER(bootmeth_efi_mgr) = {
101 	.name		= "bootmeth_efi_mgr",
102 	.id		= UCLASS_BOOTMETH,
103 	.of_match	= efi_mgr_bootmeth_ids,
104 	.ops		= &efi_mgr_bootmeth_ops,
105 	.bind		= bootmeth_efi_mgr_bind,
106 	.priv_auto	= sizeof(struct efi_mgr_priv),
107 };
108