Patrice Chotard | 7b81c05 | 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> |
| 7 | #include "pxe_utils.h" |
| 8 | |
| 9 | static char *fs_argv[5]; |
| 10 | |
| 11 | static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
| 12 | { |
| 13 | #ifdef CONFIG_CMD_EXT2 |
| 14 | fs_argv[0] = "ext2load"; |
| 15 | fs_argv[3] = file_addr; |
| 16 | fs_argv[4] = (void *)file_path; |
| 17 | |
| 18 | if (!do_ext2load(cmdtp, 0, 5, fs_argv)) |
| 19 | return 1; |
| 20 | #endif |
| 21 | return -ENOENT; |
| 22 | } |
| 23 | |
| 24 | static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
| 25 | { |
| 26 | #ifdef CONFIG_CMD_FAT |
| 27 | fs_argv[0] = "fatload"; |
| 28 | fs_argv[3] = file_addr; |
| 29 | fs_argv[4] = (void *)file_path; |
| 30 | |
| 31 | if (!do_fat_fsload(cmdtp, 0, 5, fs_argv)) |
| 32 | return 1; |
| 33 | #endif |
| 34 | return -ENOENT; |
| 35 | } |
| 36 | |
| 37 | static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
| 38 | { |
| 39 | #ifdef CONFIG_CMD_FS_GENERIC |
| 40 | fs_argv[0] = "load"; |
| 41 | fs_argv[3] = file_addr; |
| 42 | fs_argv[4] = (void *)file_path; |
| 43 | |
| 44 | if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY)) |
| 45 | return 1; |
| 46 | #endif |
| 47 | return -ENOENT; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Boots a system using a local disk syslinux/extlinux file |
| 52 | * |
| 53 | * Returns 0 on success, 1 on error. |
| 54 | */ |
| 55 | static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 56 | { |
| 57 | unsigned long pxefile_addr_r; |
| 58 | struct pxe_menu *cfg; |
| 59 | char *pxefile_addr_str; |
| 60 | char *filename; |
| 61 | int prompt = 0; |
| 62 | |
| 63 | is_pxe = false; |
| 64 | |
| 65 | if (argc > 1 && strstr(argv[1], "-p")) { |
| 66 | prompt = 1; |
| 67 | argc--; |
| 68 | argv++; |
| 69 | } |
| 70 | |
| 71 | if (argc < 4) |
| 72 | return cmd_usage(cmdtp); |
| 73 | |
| 74 | if (argc < 5) { |
| 75 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 76 | if (!pxefile_addr_str) |
| 77 | return 1; |
| 78 | } else { |
| 79 | pxefile_addr_str = argv[4]; |
| 80 | } |
| 81 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 82 | if (argc < 6) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 83 | filename = env_get("bootfile"); |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 84 | } else { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 85 | filename = argv[5]; |
| 86 | env_set("bootfile", filename); |
| 87 | } |
| 88 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 89 | if (strstr(argv[3], "ext2")) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 90 | do_getfile = do_get_ext2; |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 91 | } else if (strstr(argv[3], "fat")) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 92 | do_getfile = do_get_fat; |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 93 | } else if (strstr(argv[3], "any")) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 94 | do_getfile = do_get_any; |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 95 | } else { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 96 | printf("Invalid filesystem: %s\n", argv[3]); |
| 97 | return 1; |
| 98 | } |
| 99 | fs_argv[1] = argv[1]; |
| 100 | fs_argv[2] = argv[2]; |
| 101 | |
| 102 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 103 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) { |
| 108 | printf("Error reading config file\n"); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | cfg = parse_pxefile(cmdtp, pxefile_addr_r); |
| 113 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 114 | if (!cfg) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 115 | printf("Error parsing config file\n"); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | if (prompt) |
| 120 | cfg->prompt = 1; |
| 121 | |
| 122 | handle_pxe_menu(cmdtp, cfg); |
| 123 | |
| 124 | destroy_pxe_menu(cfg); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 129 | U_BOOT_CMD(sysboot, 7, 1, do_sysboot, |
| 130 | "command to get and boot from syslinux files", |
| 131 | "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n" |
| 132 | " - load and parse syslinux menu file 'filename' from ext2, fat\n" |
| 133 | " or any filesystem on 'dev' on 'interface' to address 'addr'" |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 134 | ); |