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