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