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