Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 3 | #include <command.h> |
| 4 | #include <env.h> |
| 5 | #include <fs.h> |
Simon Glass | 0666fcb | 2021-10-14 12:48:00 -0600 | [diff] [blame] | 6 | #include <pxe_utils.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 7 | #include <vsprintf.h> |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 8 | |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 9 | /** |
| 10 | * struct sysboot_info - useful information for sysboot helpers |
| 11 | * |
| 12 | * @fstype: Filesystem type (FS_TYPE_...) |
| 13 | * @ifname: Interface name (e.g. "ide", "scsi") |
| 14 | * @dev_part_str is in the format: |
| 15 | * <dev>.<hw_part>:<part> where <dev> is the device number, |
| 16 | * <hw_part> is the optional hardware partition number and |
| 17 | * <part> is the partition number |
| 18 | */ |
| 19 | struct sysboot_info { |
| 20 | int fstype; |
| 21 | const char *ifname; |
| 22 | const char *dev_part_str; |
| 23 | }; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 24 | |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 25 | static int sysboot_read_file(struct pxe_context *ctx, const char *file_path, |
| 26 | char *file_addr, ulong *sizep) |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 27 | { |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 28 | struct sysboot_info *info = ctx->userdata; |
| 29 | loff_t len_read; |
| 30 | ulong addr; |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 31 | int ret; |
| 32 | |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 33 | addr = simple_strtoul(file_addr, NULL, 16); |
| 34 | ret = fs_set_blk_dev(info->ifname, info->dev_part_str, info->fstype); |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 35 | if (ret) |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 36 | return ret; |
| 37 | ret = fs_read(file_path, addr, 0, 0, &len_read); |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 38 | if (ret) |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 39 | return ret; |
| 40 | *sizep = len_read; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 41 | |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 42 | return 0; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Boots a system using a local disk syslinux/extlinux file |
| 47 | * |
| 48 | * Returns 0 on success, 1 on error. |
| 49 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 50 | static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 51 | char *const argv[]) |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 52 | { |
| 53 | unsigned long pxefile_addr_r; |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 54 | struct pxe_context ctx; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 55 | char *pxefile_addr_str; |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 56 | struct sysboot_info info; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 57 | char *filename; |
| 58 | int prompt = 0; |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 59 | int ret; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 60 | |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 61 | if (argc > 1 && strstr(argv[1], "-p")) { |
| 62 | prompt = 1; |
| 63 | argc--; |
| 64 | argv++; |
| 65 | } |
| 66 | |
| 67 | if (argc < 4) |
| 68 | return cmd_usage(cmdtp); |
| 69 | |
| 70 | if (argc < 5) { |
| 71 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 72 | if (!pxefile_addr_str) |
| 73 | return 1; |
| 74 | } else { |
| 75 | pxefile_addr_str = argv[4]; |
| 76 | } |
| 77 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 78 | if (argc < 6) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 79 | filename = env_get("bootfile"); |
Caleb Connolly | a119833 | 2024-03-18 23:16:36 +0000 | [diff] [blame] | 80 | if (!filename) { |
| 81 | printf("Specify a filename or set the ${bootfile} environment variable\n"); |
| 82 | return 1; |
| 83 | } |
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")) { |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 90 | info.fstype = FS_TYPE_EXT; |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 91 | } else if (strstr(argv[3], "fat")) { |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 92 | info.fstype = FS_TYPE_FAT; |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 93 | } else if (strstr(argv[3], "any")) { |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 94 | info.fstype = FS_TYPE_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 | } |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 99 | info.ifname = argv[1]; |
| 100 | info.dev_part_str = argv[2]; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 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 | |
Simon Glass | 086b080 | 2021-10-14 12:48:09 -0600 | [diff] [blame] | 107 | if (pxe_setup_ctx(&ctx, cmdtp, sysboot_read_file, &info, true, |
Sean Edmond | ba80286 | 2023-04-11 10:48:47 -0700 | [diff] [blame] | 108 | filename, false)) { |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 109 | printf("Out of memory\n"); |
| 110 | return CMD_RET_FAILURE; |
| 111 | } |
| 112 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 113 | if (get_pxe_file(&ctx, filename, pxefile_addr_r) < 0) { |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 114 | printf("Error reading config file\n"); |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 115 | pxe_destroy_ctx(&ctx); |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 119 | ret = pxe_process(&ctx, pxefile_addr_r, prompt); |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 120 | pxe_destroy_ctx(&ctx); |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 121 | if (ret) |
| 122 | return CMD_RET_FAILURE; |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Patrice Chotard | 1918e73 | 2019-11-25 09:07:40 +0100 | [diff] [blame] | 127 | U_BOOT_CMD(sysboot, 7, 1, do_sysboot, |
| 128 | "command to get and boot from syslinux files", |
| 129 | "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n" |
| 130 | " - load and parse syslinux menu file 'filename' from ext2, fat\n" |
| 131 | " or any filesystem on 'dev' on 'interface' to address 'addr'" |
Patrice Chotard | 7b81c05 | 2019-11-25 09:07:38 +0100 | [diff] [blame] | 132 | ); |