Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Simon Glass | 4157158 | 2023-07-12 09:04:32 -0600 | [diff] [blame] | 3 | * Bootdev for ethernet (uses PXE) |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
| 13 | #include <command.h> |
| 14 | #include <bootmeth.h> |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 16 | #include <extlinux.h> |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 17 | #include <init.h> |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 18 | #include <log.h> |
| 19 | #include <net.h> |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 20 | #include <test/test.h> |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 21 | |
| 22 | static 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 Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 29 | ret = bootflow_iter_check_net(iter); |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 30 | 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 Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 38 | * Like extlinux boot, this assumes there is only one Ethernet device. |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 39 | * 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 Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 47 | /* See distro_pxe_read_bootflow() for the standard impl of this */ |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 48 | log_debug("dhcp complete - reading bootflow with method '%s'\n", |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 49 | 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 | |
| 58 | static int eth_bootdev_bind(struct udevice *dev) |
| 59 | { |
| 60 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 61 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 62 | ucp->prio = BOOTDEVP_6_NET_BASE; |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 67 | static int eth_bootdev_hunt(struct bootdev_hunter *info, bool show) |
| 68 | { |
| 69 | int ret; |
| 70 | |
| 71 | if (!test_eth_enabled()) |
| 72 | return 0; |
| 73 | |
| 74 | /* init PCI first since this is often used to provide Ehternet */ |
| 75 | if (IS_ENABLED(CONFIG_PCI)) { |
| 76 | ret = pci_init(); |
| 77 | if (ret) |
| 78 | log_warning("Failed to init PCI (%dE)\n", ret); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Ethernet devices can also come from USB, but that is a higher |
| 83 | * priority (BOOTDEVP_5_SCAN_SLOW) than ethernet, so it should have been |
| 84 | * enumerated already. If something like 'bootflow scan dhcp' is used |
| 85 | * then the user will need to run 'usb start' first. |
| 86 | */ |
| 87 | if (IS_ENABLED(CONFIG_CMD_DHCP)) { |
| 88 | ret = dhcp_run(0, NULL, false); |
| 89 | if (ret) |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 96 | struct bootdev_ops eth_bootdev_ops = { |
| 97 | .get_bootflow = eth_get_bootflow, |
| 98 | }; |
| 99 | |
| 100 | static const struct udevice_id eth_bootdev_ids[] = { |
| 101 | { .compatible = "u-boot,bootdev-eth" }, |
| 102 | { } |
| 103 | }; |
| 104 | |
| 105 | U_BOOT_DRIVER(eth_bootdev) = { |
| 106 | .name = "eth_bootdev", |
| 107 | .id = UCLASS_BOOTDEV, |
| 108 | .ops = ð_bootdev_ops, |
| 109 | .bind = eth_bootdev_bind, |
| 110 | .of_match = eth_bootdev_ids, |
| 111 | }; |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 112 | |
| 113 | BOOTDEV_HUNTER(eth_bootdev_hunt) = { |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 114 | .prio = BOOTDEVP_6_NET_BASE, |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 115 | .uclass = UCLASS_ETH, |
| 116 | .hunt = eth_bootdev_hunt, |
| 117 | .drv = DM_DRIVER_REF(eth_bootdev), |
| 118 | }; |