bootmeth_pxe.c 4.4 KB

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