blob: 93d4a4008308f9c66c4810fdc4af8c6885795889 [file] [log] [blame]
Patrice Chotard7b81c052019-11-25 09:07:38 +01001// SPDX-License-Identifier: GPL-2.0+
2
Patrice Chotard7b81c052019-11-25 09:07:38 +01003#include <command.h>
4#include <env.h>
5#include <fs.h>
Simon Glass0666fcb2021-10-14 12:48:00 -06006#include <pxe_utils.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <vsprintf.h>
Patrice Chotard7b81c052019-11-25 09:07:38 +01008
Simon Glass086b0802021-10-14 12:48:09 -06009/**
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 */
19struct sysboot_info {
20 int fstype;
21 const char *ifname;
22 const char *dev_part_str;
23};
Patrice Chotard7b81c052019-11-25 09:07:38 +010024
Simon Glass086b0802021-10-14 12:48:09 -060025static int sysboot_read_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -070026 char *file_addr, enum bootflow_img_t type,
27 ulong *sizep)
Patrice Chotard7b81c052019-11-25 09:07:38 +010028{
Simon Glass086b0802021-10-14 12:48:09 -060029 struct sysboot_info *info = ctx->userdata;
30 loff_t len_read;
31 ulong addr;
Simon Glassa9401b92021-10-14 12:48:08 -060032 int ret;
33
Simon Glass086b0802021-10-14 12:48:09 -060034 addr = simple_strtoul(file_addr, NULL, 16);
35 ret = fs_set_blk_dev(info->ifname, info->dev_part_str, info->fstype);
Simon Glassa9401b92021-10-14 12:48:08 -060036 if (ret)
Simon Glass086b0802021-10-14 12:48:09 -060037 return ret;
38 ret = fs_read(file_path, addr, 0, 0, &len_read);
Simon Glassa9401b92021-10-14 12:48:08 -060039 if (ret)
Simon Glass086b0802021-10-14 12:48:09 -060040 return ret;
41 *sizep = len_read;
Patrice Chotard7b81c052019-11-25 09:07:38 +010042
Simon Glass086b0802021-10-14 12:48:09 -060043 return 0;
Patrice Chotard7b81c052019-11-25 09:07:38 +010044}
45
46/*
47 * Boots a system using a local disk syslinux/extlinux file
48 *
49 * Returns 0 on success, 1 on error.
50 */
Simon Glassed38aef2020-05-10 11:40:03 -060051static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc,
52 char *const argv[])
Patrice Chotard7b81c052019-11-25 09:07:38 +010053{
54 unsigned long pxefile_addr_r;
Simon Glassb0d08db2021-10-14 12:47:56 -060055 struct pxe_context ctx;
Patrice Chotard7b81c052019-11-25 09:07:38 +010056 char *pxefile_addr_str;
Simon Glass086b0802021-10-14 12:48:09 -060057 struct sysboot_info info;
Patrice Chotard7b81c052019-11-25 09:07:38 +010058 char *filename;
59 int prompt = 0;
Simon Glass791bbfe2021-10-14 12:48:03 -060060 int ret;
Patrice Chotard7b81c052019-11-25 09:07:38 +010061
Patrice Chotard7b81c052019-11-25 09:07:38 +010062 if (argc > 1 && strstr(argv[1], "-p")) {
63 prompt = 1;
64 argc--;
65 argv++;
66 }
67
68 if (argc < 4)
69 return cmd_usage(cmdtp);
70
71 if (argc < 5) {
72 pxefile_addr_str = from_env("pxefile_addr_r");
73 if (!pxefile_addr_str)
74 return 1;
75 } else {
76 pxefile_addr_str = argv[4];
77 }
78
Patrice Chotard1918e732019-11-25 09:07:40 +010079 if (argc < 6) {
Patrice Chotard7b81c052019-11-25 09:07:38 +010080 filename = env_get("bootfile");
Caleb Connollya1198332024-03-18 23:16:36 +000081 if (!filename) {
82 printf("Specify a filename or set the ${bootfile} environment variable\n");
83 return 1;
84 }
Patrice Chotard1918e732019-11-25 09:07:40 +010085 } else {
Patrice Chotard7b81c052019-11-25 09:07:38 +010086 filename = argv[5];
87 env_set("bootfile", filename);
88 }
89
Patrice Chotard1918e732019-11-25 09:07:40 +010090 if (strstr(argv[3], "ext2")) {
Simon Glass086b0802021-10-14 12:48:09 -060091 info.fstype = FS_TYPE_EXT;
Patrice Chotard1918e732019-11-25 09:07:40 +010092 } else if (strstr(argv[3], "fat")) {
Simon Glass086b0802021-10-14 12:48:09 -060093 info.fstype = FS_TYPE_FAT;
Patrice Chotard1918e732019-11-25 09:07:40 +010094 } else if (strstr(argv[3], "any")) {
Simon Glass086b0802021-10-14 12:48:09 -060095 info.fstype = FS_TYPE_ANY;
Patrice Chotard1918e732019-11-25 09:07:40 +010096 } else {
Patrice Chotard7b81c052019-11-25 09:07:38 +010097 printf("Invalid filesystem: %s\n", argv[3]);
98 return 1;
99 }
Simon Glass086b0802021-10-14 12:48:09 -0600100 info.ifname = argv[1];
101 info.dev_part_str = argv[2];
Patrice Chotard7b81c052019-11-25 09:07:38 +0100102
103 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
104 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
105 return 1;
106 }
107
Simon Glass086b0802021-10-14 12:48:09 -0600108 if (pxe_setup_ctx(&ctx, cmdtp, sysboot_read_file, &info, true,
Martyn Welch2c47aac2024-10-09 14:15:39 +0100109 filename, false, false)) {
Simon Glasse719fe02021-10-14 12:48:04 -0600110 printf("Out of memory\n");
111 return CMD_RET_FAILURE;
112 }
113
Simon Glassa686ba62024-11-15 16:19:19 -0700114 if (get_pxe_file(&ctx, filename, pxefile_addr_r)
115 < 0) {
Patrice Chotard7b81c052019-11-25 09:07:38 +0100116 printf("Error reading config file\n");
Simon Glasse719fe02021-10-14 12:48:04 -0600117 pxe_destroy_ctx(&ctx);
Patrice Chotard7b81c052019-11-25 09:07:38 +0100118 return 1;
119 }
120
Simon Glass791bbfe2021-10-14 12:48:03 -0600121 ret = pxe_process(&ctx, pxefile_addr_r, prompt);
Simon Glasse719fe02021-10-14 12:48:04 -0600122 pxe_destroy_ctx(&ctx);
Simon Glass791bbfe2021-10-14 12:48:03 -0600123 if (ret)
124 return CMD_RET_FAILURE;
Patrice Chotard7b81c052019-11-25 09:07:38 +0100125
126 return 0;
127}
128
Patrice Chotard1918e732019-11-25 09:07:40 +0100129U_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 Chotard7b81c052019-11-25 09:07:38 +0100134);