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