Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <command.h> |
| 5 | #include <env.h> |
| 6 | #include <fs.h> |
Simon Glass | 262cfb5 | 2021-10-14 12:48:00 -0600 | [diff] [blame] | 7 | #include <pxe_utils.h> |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 8 | |
| 9 | static char *fs_argv[5]; |
| 10 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 11 | static int do_get_ext2(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 12 | char *file_addr, ulong *sizep) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 13 | { |
| 14 | #ifdef CONFIG_CMD_EXT2 |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 15 | int ret; |
| 16 | |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 17 | fs_argv[0] = "ext2load"; |
| 18 | fs_argv[3] = file_addr; |
| 19 | fs_argv[4] = (void *)file_path; |
| 20 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 21 | if (!do_ext2load(ctx->cmdtp, 0, 5, fs_argv)) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 22 | return 1; |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 23 | ret = pxe_get_file_size(sizep); |
| 24 | if (ret) |
| 25 | return log_msg_ret("tftp", ret); |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 26 | #endif |
| 27 | return -ENOENT; |
| 28 | } |
| 29 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 30 | static int do_get_fat(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 31 | char *file_addr, ulong *sizep) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 32 | { |
| 33 | #ifdef CONFIG_CMD_FAT |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 34 | int ret; |
| 35 | |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 36 | fs_argv[0] = "fatload"; |
| 37 | fs_argv[3] = file_addr; |
| 38 | fs_argv[4] = (void *)file_path; |
| 39 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 40 | if (!do_fat_fsload(ctx->cmdtp, 0, 5, fs_argv)) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 41 | return 1; |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 42 | ret = pxe_get_file_size(sizep); |
| 43 | if (ret) |
| 44 | return log_msg_ret("tftp", ret); |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 45 | #endif |
| 46 | return -ENOENT; |
| 47 | } |
| 48 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 49 | static int do_get_any(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 50 | char *file_addr, ulong *sizep) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 51 | { |
| 52 | #ifdef CONFIG_CMD_FS_GENERIC |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 53 | int ret; |
| 54 | |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 55 | fs_argv[0] = "load"; |
| 56 | fs_argv[3] = file_addr; |
| 57 | fs_argv[4] = (void *)file_path; |
| 58 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 59 | if (!do_load(ctx->cmdtp, 0, 5, fs_argv, FS_TYPE_ANY)) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 60 | return 1; |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame^] | 61 | ret = pxe_get_file_size(sizep); |
| 62 | if (ret) |
| 63 | return log_msg_ret("tftp", ret); |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 64 | #endif |
| 65 | return -ENOENT; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Boots a system using a local disk syslinux/extlinux file |
| 70 | * |
| 71 | * Returns 0 on success, 1 on error. |
| 72 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 73 | static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 74 | char *const argv[]) |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 75 | { |
| 76 | unsigned long pxefile_addr_r; |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 77 | pxe_getfile_func getfile; |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 78 | struct pxe_context ctx; |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 79 | char *pxefile_addr_str; |
| 80 | char *filename; |
| 81 | int prompt = 0; |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 82 | int ret; |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 83 | |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 84 | if (argc > 1 && strstr(argv[1], "-p")) { |
| 85 | prompt = 1; |
| 86 | argc--; |
| 87 | argv++; |
| 88 | } |
| 89 | |
| 90 | if (argc < 4) |
| 91 | return cmd_usage(cmdtp); |
| 92 | |
| 93 | if (argc < 5) { |
| 94 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 95 | if (!pxefile_addr_str) |
| 96 | return 1; |
| 97 | } else { |
| 98 | pxefile_addr_str = argv[4]; |
| 99 | } |
| 100 | |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 101 | if (argc < 6) { |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 102 | filename = env_get("bootfile"); |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 103 | } else { |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 104 | filename = argv[5]; |
| 105 | env_set("bootfile", filename); |
| 106 | } |
| 107 | |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 108 | if (strstr(argv[3], "ext2")) { |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 109 | getfile = do_get_ext2; |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 110 | } else if (strstr(argv[3], "fat")) { |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 111 | getfile = do_get_fat; |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 112 | } else if (strstr(argv[3], "any")) { |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 113 | getfile = do_get_any; |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 114 | } else { |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 115 | printf("Invalid filesystem: %s\n", argv[3]); |
| 116 | return 1; |
| 117 | } |
| 118 | fs_argv[1] = argv[1]; |
| 119 | fs_argv[2] = argv[2]; |
| 120 | |
| 121 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 122 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 123 | return 1; |
| 124 | } |
| 125 | |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 126 | if (pxe_setup_ctx(&ctx, cmdtp, getfile, NULL, true, filename)) { |
| 127 | printf("Out of memory\n"); |
| 128 | return CMD_RET_FAILURE; |
| 129 | } |
| 130 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 131 | if (get_pxe_file(&ctx, filename, pxefile_addr_r) < 0) { |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 132 | printf("Error reading config file\n"); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 133 | pxe_destroy_ctx(&ctx); |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 134 | return 1; |
| 135 | } |
| 136 | |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 137 | ret = pxe_process(&ctx, pxefile_addr_r, prompt); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 138 | pxe_destroy_ctx(&ctx); |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 139 | if (ret) |
| 140 | return CMD_RET_FAILURE; |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Patrice Chotard | 51843ea | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 145 | U_BOOT_CMD(sysboot, 7, 1, do_sysboot, |
| 146 | "command to get and boot from syslinux files", |
| 147 | "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n" |
| 148 | " - load and parse syslinux menu file 'filename' from ext2, fat\n" |
| 149 | " or any filesystem on 'dev' on 'interface' to address 'addr'" |
Patrice Chotard | 993c912 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 150 | ); |