Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 3 | * Bootmethod for extlinux boot using PXE (network boot) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
| 13 | #include <bootmeth.h> |
| 14 | #include <command.h> |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 15 | #include <dm.h> |
Tom Rini | c31301c | 2025-05-15 17:31:50 -0600 | [diff] [blame] | 16 | #include <env.h> |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 17 | #include <extlinux.h> |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 18 | #include <fs.h> |
| 19 | #include <log.h> |
| 20 | #include <malloc.h> |
| 21 | #include <mapmem.h> |
| 22 | #include <mmc.h> |
| 23 | #include <net.h> |
| 24 | #include <pxe_utils.h> |
| 25 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 26 | static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path, |
Simon Glass | a686ba6 | 2024-11-15 16:19:19 -0700 | [diff] [blame] | 27 | char *file_addr, enum bootflow_img_t type, |
| 28 | ulong *sizep) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 29 | { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 30 | struct extlinux_info *info = ctx->userdata; |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 31 | ulong addr; |
| 32 | int ret; |
| 33 | |
| 34 | addr = simple_strtoul(file_addr, NULL, 16); |
Simon Glass | 1dee4eb | 2023-07-26 21:01:25 -0600 | [diff] [blame] | 35 | |
| 36 | /* Allow up to 1GB */ |
| 37 | *sizep = 1 << 30; |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 38 | ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr, |
Simon Glass | a686ba6 | 2024-11-15 16:19:19 -0700 | [diff] [blame] | 39 | type, sizep); |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 40 | if (ret) |
| 41 | return log_msg_ret("read", ret); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 46 | static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 47 | { |
| 48 | int ret; |
| 49 | |
| 50 | /* This only works on network devices */ |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 51 | ret = bootflow_iter_check_net(iter); |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 52 | if (ret) |
| 53 | return log_msg_ret("net", ret); |
| 54 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 55 | if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY) |
| 56 | return log_msg_ret("dhcp", -ENOTSUPP); |
| 57 | |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 61 | static int extlinux_pxe_read_bootflow(struct udevice *dev, |
| 62 | struct bootflow *bflow) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 63 | { |
| 64 | const char *addr_str; |
| 65 | char fname[200]; |
| 66 | char *bootdir; |
| 67 | ulong addr; |
| 68 | ulong size; |
| 69 | char *buf; |
| 70 | int ret; |
| 71 | |
| 72 | addr_str = env_get("pxefile_addr_r"); |
| 73 | if (!addr_str) |
| 74 | return log_msg_ret("pxeb", -EPERM); |
| 75 | addr = simple_strtoul(addr_str, NULL, 16); |
| 76 | |
Heiko Stuebner | 33df5cd | 2025-04-02 23:50:25 +0200 | [diff] [blame] | 77 | ret = dhcp_run(addr, NULL, false); |
| 78 | if (ret) |
| 79 | return log_msg_ret("dhc", ret); |
| 80 | |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 81 | log_debug("calling pxe_get()\n"); |
Sean Edmond | ba80286 | 2023-04-11 10:48:47 -0700 | [diff] [blame] | 82 | ret = pxe_get(addr, &bootdir, &size, false); |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 83 | log_debug("pxe_get() returned %d\n", ret); |
| 84 | if (ret) |
| 85 | return log_msg_ret("pxeb", ret); |
| 86 | bflow->size = size; |
| 87 | |
| 88 | /* Use the directory of the dhcp bootdir as our subdir, if provided */ |
| 89 | if (bootdir) { |
| 90 | const char *last_slash; |
| 91 | int path_len; |
| 92 | |
| 93 | last_slash = strrchr(bootdir, '/'); |
| 94 | if (last_slash) { |
| 95 | path_len = (last_slash - bootdir) + 1; |
| 96 | bflow->subdir = malloc(path_len + 1); |
| 97 | memcpy(bflow->subdir, bootdir, path_len); |
| 98 | bflow->subdir[path_len] = '\0'; |
| 99 | } |
| 100 | } |
| 101 | snprintf(fname, sizeof(fname), "%s%s", |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 102 | bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME); |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 103 | |
| 104 | bflow->fname = strdup(fname); |
| 105 | if (!bflow->fname) |
| 106 | return log_msg_ret("name", -ENOMEM); |
| 107 | |
| 108 | bflow->state = BOOTFLOWST_READY; |
| 109 | |
| 110 | /* Allocate the buffer, including the \0 byte added by get_pxe_file() */ |
| 111 | buf = malloc(size + 1); |
| 112 | if (!buf) |
| 113 | return log_msg_ret("buf", -ENOMEM); |
| 114 | memcpy(buf, map_sysmem(addr, 0), size + 1); |
| 115 | bflow->buf = buf; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 120 | static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow, |
| 121 | const char *file_path, ulong addr, |
Simon Glass | f39b559 | 2024-11-15 16:19:17 -0700 | [diff] [blame] | 122 | enum bootflow_img_t type, ulong *sizep) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 123 | { |
| 124 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; |
| 125 | struct pxe_context *ctx = dev_get_priv(dev); |
| 126 | char file_addr[17]; |
| 127 | ulong size; |
| 128 | int ret; |
| 129 | |
| 130 | sprintf(file_addr, "%lx", addr); |
| 131 | tftp_argv[1] = file_addr; |
| 132 | tftp_argv[2] = (void *)file_path; |
| 133 | |
| 134 | if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv)) |
| 135 | return -ENOENT; |
| 136 | ret = pxe_get_file_size(&size); |
| 137 | if (ret) |
| 138 | return log_msg_ret("tftp", ret); |
| 139 | if (size > *sizep) |
| 140 | return log_msg_ret("spc", -ENOSPC); |
| 141 | *sizep = size; |
| 142 | |
Simon Glass | 392eac4 | 2024-11-15 16:19:20 -0700 | [diff] [blame] | 143 | if (!bootflow_img_add(bflow, file_path, type, addr, size)) |
| 144 | return log_msg_ret("pxi", -ENOMEM); |
| 145 | |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 149 | static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 150 | { |
| 151 | struct pxe_context *ctx = dev_get_priv(dev); |
| 152 | struct cmd_tbl cmdtp = {}; /* dummy */ |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 153 | struct extlinux_info info; |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 154 | ulong addr; |
| 155 | int ret; |
| 156 | |
| 157 | addr = map_to_sysmem(bflow->buf); |
| 158 | info.dev = dev; |
| 159 | info.bflow = bflow; |
| 160 | info.cmdtp = &cmdtp; |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 161 | ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false, |
Martyn Welch | 2c47aac | 2024-10-09 14:15:39 +0100 | [diff] [blame] | 162 | bflow->subdir, false, false); |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 163 | if (ret) |
| 164 | return log_msg_ret("ctx", -EINVAL); |
| 165 | |
| 166 | ret = pxe_process(ctx, addr, false); |
| 167 | if (ret) |
| 168 | return log_msg_ret("bread", -EINVAL); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 173 | static int extlinux_bootmeth_pxe_bind(struct udevice *dev) |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 174 | { |
| 175 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 176 | |
| 177 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 178 | "PXE boot from a network device" : "PXE"; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 183 | static struct bootmeth_ops extlinux_bootmeth_pxe_ops = { |
| 184 | .check = extlinux_pxe_check, |
| 185 | .read_bootflow = extlinux_pxe_read_bootflow, |
| 186 | .read_file = extlinux_pxe_read_file, |
| 187 | .boot = extlinux_pxe_boot, |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 188 | }; |
| 189 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 190 | static const struct udevice_id extlinux_bootmeth_pxe_ids[] = { |
| 191 | { .compatible = "u-boot,extlinux-pxe" }, |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 192 | { } |
| 193 | }; |
| 194 | |
Heinrich Schuchardt | dc1f006 | 2024-04-03 20:34:15 +0200 | [diff] [blame] | 195 | U_BOOT_DRIVER(bootmeth_zpxe) = { |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 196 | .name = "bootmeth_pxe", |
| 197 | .id = UCLASS_BOOTMETH, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 198 | .of_match = extlinux_bootmeth_pxe_ids, |
| 199 | .ops = &extlinux_bootmeth_pxe_ops, |
| 200 | .bind = extlinux_bootmeth_pxe_bind, |
Simon Glass | 8314461 | 2022-04-24 23:31:16 -0600 | [diff] [blame] | 201 | .priv_auto = sizeof(struct pxe_context), |
| 202 | }; |