blob: b0fca6e8313d796ac1048b0edf85d69278e7f820 [file] [log] [blame]
Simon Glass4f840a22022-04-24 23:31:15 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glass41571582023-07-12 09:04:32 -06003 * Bootdev for ethernet (uses PXE)
Simon Glass4f840a22022-04-24 23:31:15 -06004 *
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 <bootdev.h>
12#include <bootflow.h>
13#include <command.h>
14#include <bootmeth.h>
Simon Glass4f840a22022-04-24 23:31:15 -060015#include <dm.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060016#include <extlinux.h>
Simon Glass5ea0a8d2023-01-17 10:47:40 -070017#include <init.h>
Simon Glass4f840a22022-04-24 23:31:15 -060018#include <log.h>
19#include <net.h>
Simon Glass5ea0a8d2023-01-17 10:47:40 -070020#include <test/test.h>
Simon Glass4f840a22022-04-24 23:31:15 -060021
22static int eth_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
23 struct bootflow *bflow)
24{
25 char name[60];
26 int ret;
27
28 /* Must be an Ethernet device */
Simon Glass18c50402023-01-17 10:47:54 -070029 ret = bootflow_iter_check_net(iter);
Simon Glass4f840a22022-04-24 23:31:15 -060030 if (ret)
31 return log_msg_ret("net", ret);
32
33 ret = bootmeth_check(bflow->method, iter);
34 if (ret)
35 return log_msg_ret("check", ret);
36
37 /*
Simon Glassb71d7f72023-05-10 16:34:46 -060038 * Like extlinux boot, this assumes there is only one Ethernet device.
Simon Glass4f840a22022-04-24 23:31:15 -060039 * In this case, that means that @eth is ignored
40 */
41
42 snprintf(name, sizeof(name), "%s.%d", dev->name, iter->part);
43 bflow->name = strdup(name);
44 if (!bflow->name)
45 return log_msg_ret("name", -ENOMEM);
46
Simon Glass4f840a22022-04-24 23:31:15 -060047 /* See distro_pxe_read_bootflow() for the standard impl of this */
Simon Glass5ea0a8d2023-01-17 10:47:40 -070048 log_debug("dhcp complete - reading bootflow with method '%s'\n",
Simon Glass4f840a22022-04-24 23:31:15 -060049 bflow->method->name);
50 ret = bootmeth_read_bootflow(bflow->method, bflow);
51 log_debug("reading bootflow returned %d\n", ret);
52 if (ret)
53 return log_msg_ret("method", ret);
54
55 return 0;
56}
57
58static int eth_bootdev_bind(struct udevice *dev)
59{
60 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
61
Simon Glass7e1f6a42023-01-17 10:48:08 -070062 ucp->prio = BOOTDEVP_6_NET_BASE;
Simon Glass4f840a22022-04-24 23:31:15 -060063
64 return 0;
65}
66
Heinrich Schuchardt08ff75e2024-11-27 08:06:29 +010067/**
68 * eth_bootdev_hunt() - probe all network devices
69 *
70 * Network devices can also come from USB, but that is a higher
71 * priority (BOOTDEVP_5_SCAN_SLOW) than network, so it should have been
72 * enumerated already. If something like 'bootflow scan dhcp' is used,
73 * then the user will need to run 'usb start' first.
74 *
75 * @info: info structure describing this hunter
76 * @show: true to show information from the hunter
77 *
78 * Return: 0 if device found, -EINVAL otherwise
79 */
Simon Glass5ea0a8d2023-01-17 10:47:40 -070080static int eth_bootdev_hunt(struct bootdev_hunter *info, bool show)
81{
82 int ret;
Heinrich Schuchardt08ff75e2024-11-27 08:06:29 +010083 struct udevice *dev = NULL;
Simon Glass5ea0a8d2023-01-17 10:47:40 -070084
85 if (!test_eth_enabled())
86 return 0;
87
88 /* init PCI first since this is often used to provide Ehternet */
89 if (IS_ENABLED(CONFIG_PCI)) {
90 ret = pci_init();
91 if (ret)
92 log_warning("Failed to init PCI (%dE)\n", ret);
93 }
94
Heinrich Schuchardt08ff75e2024-11-27 08:06:29 +010095 ret = -EINVAL;
96 uclass_foreach_dev_probe(UCLASS_ETH, dev)
97 ret = 0;
Simon Glass5ea0a8d2023-01-17 10:47:40 -070098
Heinrich Schuchardt08ff75e2024-11-27 08:06:29 +010099 return ret;
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700100}
101
Simon Glass4f840a22022-04-24 23:31:15 -0600102struct bootdev_ops eth_bootdev_ops = {
103 .get_bootflow = eth_get_bootflow,
104};
105
106static const struct udevice_id eth_bootdev_ids[] = {
107 { .compatible = "u-boot,bootdev-eth" },
108 { }
109};
110
111U_BOOT_DRIVER(eth_bootdev) = {
112 .name = "eth_bootdev",
113 .id = UCLASS_BOOTDEV,
114 .ops = &eth_bootdev_ops,
115 .bind = eth_bootdev_bind,
116 .of_match = eth_bootdev_ids,
117};
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700118
119BOOTDEV_HUNTER(eth_bootdev_hunt) = {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700120 .prio = BOOTDEVP_6_NET_BASE,
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700121 .uclass = UCLASS_ETH,
122 .hunt = eth_bootdev_hunt,
123 .drv = DM_DRIVER_REF(eth_bootdev),
124};