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