blob: fdf48f0013110fdfc4217a61fdcf80103677b61e [file] [log] [blame]
Simon Glass4f840a22022-04-24 23:31:15 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Bootdevice for ethernet (uses PXE)
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
Simon Glass5ea0a8d2023-01-17 10:47:40 -07009#define LOG_CATEGORY UCLASS_BOOTSTD
10
Simon Glass4f840a22022-04-24 23:31:15 -060011#include <common.h>
12#include <bootdev.h>
13#include <bootflow.h>
14#include <command.h>
15#include <bootmeth.h>
16#include <distro.h>
17#include <dm.h>
Simon Glass5ea0a8d2023-01-17 10:47:40 -070018#include <init.h>
Simon Glass4f840a22022-04-24 23:31:15 -060019#include <log.h>
20#include <net.h>
Simon Glass5ea0a8d2023-01-17 10:47:40 -070021#include <test/test.h>
Simon Glass4f840a22022-04-24 23:31:15 -060022
23static int eth_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
24 struct bootflow *bflow)
25{
26 char name[60];
27 int ret;
28
29 /* Must be an Ethernet device */
30 ret = bootflow_iter_uses_network(iter);
31 if (ret)
32 return log_msg_ret("net", ret);
33
34 ret = bootmeth_check(bflow->method, iter);
35 if (ret)
36 return log_msg_ret("check", ret);
37
38 /*
39 * Like distro boot, this assumes there is only one Ethernet device.
40 * In this case, that means that @eth is ignored
41 */
42
43 snprintf(name, sizeof(name), "%s.%d", dev->name, iter->part);
44 bflow->name = strdup(name);
45 if (!bflow->name)
46 return log_msg_ret("name", -ENOMEM);
47
Simon Glass4f840a22022-04-24 23:31:15 -060048 /* See distro_pxe_read_bootflow() for the standard impl of this */
Simon Glass5ea0a8d2023-01-17 10:47:40 -070049 log_debug("dhcp complete - reading bootflow with method '%s'\n",
Simon Glass4f840a22022-04-24 23:31:15 -060050 bflow->method->name);
51 ret = bootmeth_read_bootflow(bflow->method, bflow);
52 log_debug("reading bootflow returned %d\n", ret);
53 if (ret)
54 return log_msg_ret("method", ret);
55
56 return 0;
57}
58
59static int eth_bootdev_bind(struct udevice *dev)
60{
61 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
62
63 ucp->prio = BOOTDEVP_4_NET_BASE;
64
65 return 0;
66}
67
Simon Glass5ea0a8d2023-01-17 10:47:40 -070068static int eth_bootdev_hunt(struct bootdev_hunter *info, bool show)
69{
70 int ret;
71
72 if (!test_eth_enabled())
73 return 0;
74
75 /* init PCI first since this is often used to provide Ehternet */
76 if (IS_ENABLED(CONFIG_PCI)) {
77 ret = pci_init();
78 if (ret)
79 log_warning("Failed to init PCI (%dE)\n", ret);
80 }
81
82 /*
83 * Ethernet devices can also come from USB, but that is a higher
84 * priority (BOOTDEVP_5_SCAN_SLOW) than ethernet, so it should have been
85 * enumerated already. If something like 'bootflow scan dhcp' is used
86 * then the user will need to run 'usb start' first.
87 */
88 if (IS_ENABLED(CONFIG_CMD_DHCP)) {
89 ret = dhcp_run(0, NULL, false);
90 if (ret)
91 return -EINVAL;
92 }
93
94 return 0;
95}
96
Simon Glass4f840a22022-04-24 23:31:15 -060097struct bootdev_ops eth_bootdev_ops = {
98 .get_bootflow = eth_get_bootflow,
99};
100
101static const struct udevice_id eth_bootdev_ids[] = {
102 { .compatible = "u-boot,bootdev-eth" },
103 { }
104};
105
106U_BOOT_DRIVER(eth_bootdev) = {
107 .name = "eth_bootdev",
108 .id = UCLASS_BOOTDEV,
109 .ops = &eth_bootdev_ops,
110 .bind = eth_bootdev_bind,
111 .of_match = eth_bootdev_ids,
112};
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700113
114BOOTDEV_HUNTER(eth_bootdev_hunt) = {
115 .prio = BOOTDEVP_4_NET_BASE,
116 .uclass = UCLASS_ETH,
117 .hunt = eth_bootdev_hunt,
118 .drv = DM_DRIVER_REF(eth_bootdev),
119};