sysboot.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <command.h>
  4. #include <env.h>
  5. #include <fs.h>
  6. #include <pxe_utils.h>
  7. /**
  8. * struct sysboot_info - useful information for sysboot helpers
  9. *
  10. * @fstype: Filesystem type (FS_TYPE_...)
  11. * @ifname: Interface name (e.g. "ide", "scsi")
  12. * @dev_part_str is in the format:
  13. * <dev>.<hw_part>:<part> where <dev> is the device number,
  14. * <hw_part> is the optional hardware partition number and
  15. * <part> is the partition number
  16. */
  17. struct sysboot_info {
  18. int fstype;
  19. const char *ifname;
  20. const char *dev_part_str;
  21. };
  22. static int sysboot_read_file(struct pxe_context *ctx, const char *file_path,
  23. char *file_addr, ulong *sizep)
  24. {
  25. struct sysboot_info *info = ctx->userdata;
  26. loff_t len_read;
  27. ulong addr;
  28. int ret;
  29. addr = simple_strtoul(file_addr, NULL, 16);
  30. ret = fs_set_blk_dev(info->ifname, info->dev_part_str, info->fstype);
  31. if (ret)
  32. return ret;
  33. ret = fs_read(file_path, addr, 0, 0, &len_read);
  34. if (ret)
  35. return ret;
  36. *sizep = len_read;
  37. return 0;
  38. }
  39. /*
  40. * Boots a system using a local disk syslinux/extlinux file
  41. *
  42. * Returns 0 on success, 1 on error.
  43. */
  44. static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc,
  45. char *const argv[])
  46. {
  47. unsigned long pxefile_addr_r;
  48. struct pxe_context ctx;
  49. char *pxefile_addr_str;
  50. struct sysboot_info info;
  51. char *filename;
  52. int prompt = 0;
  53. int ret;
  54. if (argc > 1 && strstr(argv[1], "-p")) {
  55. prompt = 1;
  56. argc--;
  57. argv++;
  58. }
  59. if (argc < 4)
  60. return cmd_usage(cmdtp);
  61. if (argc < 5) {
  62. pxefile_addr_str = from_env("pxefile_addr_r");
  63. if (!pxefile_addr_str)
  64. return 1;
  65. } else {
  66. pxefile_addr_str = argv[4];
  67. }
  68. if (argc < 6) {
  69. filename = env_get("bootfile");
  70. } else {
  71. filename = argv[5];
  72. env_set("bootfile", filename);
  73. }
  74. if (strstr(argv[3], "ext2")) {
  75. info.fstype = FS_TYPE_EXT;
  76. } else if (strstr(argv[3], "fat")) {
  77. info.fstype = FS_TYPE_FAT;
  78. } else if (strstr(argv[3], "any")) {
  79. info.fstype = FS_TYPE_ANY;
  80. } else {
  81. printf("Invalid filesystem: %s\n", argv[3]);
  82. return 1;
  83. }
  84. info.ifname = argv[1];
  85. info.dev_part_str = argv[2];
  86. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  87. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  88. return 1;
  89. }
  90. if (pxe_setup_ctx(&ctx, cmdtp, sysboot_read_file, &info, true,
  91. filename)) {
  92. printf("Out of memory\n");
  93. return CMD_RET_FAILURE;
  94. }
  95. if (get_pxe_file(&ctx, filename, pxefile_addr_r) < 0) {
  96. printf("Error reading config file\n");
  97. pxe_destroy_ctx(&ctx);
  98. return 1;
  99. }
  100. ret = pxe_process(&ctx, pxefile_addr_r, prompt);
  101. pxe_destroy_ctx(&ctx);
  102. if (ret)
  103. return CMD_RET_FAILURE;
  104. return 0;
  105. }
  106. U_BOOT_CMD(sysboot, 7, 1, do_sysboot,
  107. "command to get and boot from syslinux files",
  108. "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
  109. " - load and parse syslinux menu file 'filename' from ext2, fat\n"
  110. " or any filesystem on 'dev' on 'interface' to address 'addr'"
  111. );