blob: 7ee14df79e511009e9b8bf7deb3250d9547671f2 [file] [log] [blame]
Patrice Chotard993c9122019-11-25 09:07:38 +01001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <command.h>
5#include <env.h>
6#include <fs.h>
Simon Glass262cfb52021-10-14 12:48:00 -06007#include <pxe_utils.h>
Patrice Chotard993c9122019-11-25 09:07:38 +01008
9static char *fs_argv[5];
10
Simon Glassb1ead6b2021-10-14 12:47:57 -060011static int do_get_ext2(struct pxe_context *ctx, const char *file_path,
Simon Glass09140112020-05-10 11:40:03 -060012 char *file_addr)
Patrice Chotard993c9122019-11-25 09:07:38 +010013{
14#ifdef CONFIG_CMD_EXT2
15 fs_argv[0] = "ext2load";
16 fs_argv[3] = file_addr;
17 fs_argv[4] = (void *)file_path;
18
Simon Glassb1ead6b2021-10-14 12:47:57 -060019 if (!do_ext2load(ctx->cmdtp, 0, 5, fs_argv))
Patrice Chotard993c9122019-11-25 09:07:38 +010020 return 1;
21#endif
22 return -ENOENT;
23}
24
Simon Glassb1ead6b2021-10-14 12:47:57 -060025static int do_get_fat(struct pxe_context *ctx, const char *file_path,
Simon Glass09140112020-05-10 11:40:03 -060026 char *file_addr)
Patrice Chotard993c9122019-11-25 09:07:38 +010027{
28#ifdef CONFIG_CMD_FAT
29 fs_argv[0] = "fatload";
30 fs_argv[3] = file_addr;
31 fs_argv[4] = (void *)file_path;
32
Simon Glassb1ead6b2021-10-14 12:47:57 -060033 if (!do_fat_fsload(ctx->cmdtp, 0, 5, fs_argv))
Patrice Chotard993c9122019-11-25 09:07:38 +010034 return 1;
35#endif
36 return -ENOENT;
37}
38
Simon Glassb1ead6b2021-10-14 12:47:57 -060039static int do_get_any(struct pxe_context *ctx, const char *file_path,
Simon Glass09140112020-05-10 11:40:03 -060040 char *file_addr)
Patrice Chotard993c9122019-11-25 09:07:38 +010041{
42#ifdef CONFIG_CMD_FS_GENERIC
43 fs_argv[0] = "load";
44 fs_argv[3] = file_addr;
45 fs_argv[4] = (void *)file_path;
46
Simon Glassb1ead6b2021-10-14 12:47:57 -060047 if (!do_load(ctx->cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
Patrice Chotard993c9122019-11-25 09:07:38 +010048 return 1;
49#endif
50 return -ENOENT;
51}
52
53/*
54 * Boots a system using a local disk syslinux/extlinux file
55 *
56 * Returns 0 on success, 1 on error.
57 */
Simon Glass09140112020-05-10 11:40:03 -060058static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc,
59 char *const argv[])
Patrice Chotard993c9122019-11-25 09:07:38 +010060{
61 unsigned long pxefile_addr_r;
Simon Glassfd3fa5c2021-10-14 12:47:56 -060062 struct pxe_context ctx;
Patrice Chotard993c9122019-11-25 09:07:38 +010063 char *pxefile_addr_str;
64 char *filename;
65 int prompt = 0;
Simon Glass9e62e7c2021-10-14 12:48:03 -060066 int ret;
Patrice Chotard993c9122019-11-25 09:07:38 +010067
Patrice Chotard993c9122019-11-25 09:07:38 +010068 if (argc > 1 && strstr(argv[1], "-p")) {
69 prompt = 1;
70 argc--;
71 argv++;
72 }
73
74 if (argc < 4)
75 return cmd_usage(cmdtp);
76
77 if (argc < 5) {
78 pxefile_addr_str = from_env("pxefile_addr_r");
79 if (!pxefile_addr_str)
80 return 1;
81 } else {
82 pxefile_addr_str = argv[4];
83 }
84
Patrice Chotard51843ea2019-11-25 09:07:40 +010085 if (argc < 6) {
Patrice Chotard993c9122019-11-25 09:07:38 +010086 filename = env_get("bootfile");
Patrice Chotard51843ea2019-11-25 09:07:40 +010087 } else {
Patrice Chotard993c9122019-11-25 09:07:38 +010088 filename = argv[5];
89 env_set("bootfile", filename);
90 }
91
Simon Glass8018b9a2021-10-14 12:47:59 -060092 pxe_setup_ctx(&ctx, cmdtp, NULL, NULL, true);
Patrice Chotard51843ea2019-11-25 09:07:40 +010093 if (strstr(argv[3], "ext2")) {
Simon Glassb1ead6b2021-10-14 12:47:57 -060094 ctx.getfile = do_get_ext2;
Patrice Chotard51843ea2019-11-25 09:07:40 +010095 } else if (strstr(argv[3], "fat")) {
Simon Glassb1ead6b2021-10-14 12:47:57 -060096 ctx.getfile = do_get_fat;
Patrice Chotard51843ea2019-11-25 09:07:40 +010097 } else if (strstr(argv[3], "any")) {
Simon Glassb1ead6b2021-10-14 12:47:57 -060098 ctx.getfile = do_get_any;
Patrice Chotard51843ea2019-11-25 09:07:40 +010099 } else {
Patrice Chotard993c9122019-11-25 09:07:38 +0100100 printf("Invalid filesystem: %s\n", argv[3]);
101 return 1;
102 }
103 fs_argv[1] = argv[1];
104 fs_argv[2] = argv[2];
105
106 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
107 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
108 return 1;
109 }
110
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600111 if (get_pxe_file(&ctx, filename, pxefile_addr_r) < 0) {
Patrice Chotard993c9122019-11-25 09:07:38 +0100112 printf("Error reading config file\n");
113 return 1;
114 }
115
Simon Glass9e62e7c2021-10-14 12:48:03 -0600116 ret = pxe_process(&ctx, pxefile_addr_r, prompt);
117 if (ret)
118 return CMD_RET_FAILURE;
Patrice Chotard993c9122019-11-25 09:07:38 +0100119
120 return 0;
121}
122
Patrice Chotard51843ea2019-11-25 09:07:40 +0100123U_BOOT_CMD(sysboot, 7, 1, do_sysboot,
124 "command to get and boot from syslinux files",
125 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
126 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
127 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Patrice Chotard993c9122019-11-25 09:07:38 +0100128);