/src/u-boot/cmd/sysboot.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0+ |
2 | | |
3 | | #include <command.h> |
4 | | #include <env.h> |
5 | | #include <fs.h> |
6 | | #include <pxe_utils.h> |
7 | | #include <vsprintf.h> |
8 | | |
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 | | }; |
24 | | |
25 | | static int sysboot_read_file(struct pxe_context *ctx, const char *file_path, |
26 | | char *file_addr, enum bootflow_img_t type, |
27 | | ulong *sizep) |
28 | 0 | { |
29 | 0 | struct sysboot_info *info = ctx->userdata; |
30 | 0 | loff_t len_read; |
31 | 0 | ulong addr; |
32 | 0 | int ret; |
33 | |
|
34 | 0 | addr = simple_strtoul(file_addr, NULL, 16); |
35 | 0 | ret = fs_set_blk_dev(info->ifname, info->dev_part_str, info->fstype); |
36 | 0 | if (ret) |
37 | 0 | return ret; |
38 | 0 | ret = fs_read(file_path, addr, 0, 0, &len_read); |
39 | 0 | if (ret) |
40 | 0 | return ret; |
41 | 0 | *sizep = len_read; |
42 | |
|
43 | 0 | return 0; |
44 | 0 | } |
45 | | |
46 | | /* |
47 | | * Boots a system using a local disk syslinux/extlinux file |
48 | | * |
49 | | * Returns 0 on success, 1 on error. |
50 | | */ |
51 | | static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc, |
52 | | char *const argv[]) |
53 | 0 | { |
54 | 0 | unsigned long pxefile_addr_r; |
55 | 0 | struct pxe_context ctx; |
56 | 0 | char *pxefile_addr_str; |
57 | 0 | struct sysboot_info info; |
58 | 0 | char *filename; |
59 | 0 | int prompt = 0; |
60 | 0 | int ret; |
61 | |
|
62 | 0 | if (argc > 1 && strstr(argv[1], "-p")) { |
63 | 0 | prompt = 1; |
64 | 0 | argc--; |
65 | 0 | argv++; |
66 | 0 | } |
67 | |
|
68 | 0 | if (argc < 4) |
69 | 0 | return cmd_usage(cmdtp); |
70 | | |
71 | 0 | if (argc < 5) { |
72 | 0 | pxefile_addr_str = from_env("pxefile_addr_r"); |
73 | 0 | if (!pxefile_addr_str) |
74 | 0 | return 1; |
75 | 0 | } else { |
76 | 0 | pxefile_addr_str = argv[4]; |
77 | 0 | } |
78 | | |
79 | 0 | if (argc < 6) { |
80 | 0 | filename = env_get("bootfile"); |
81 | 0 | if (!filename) { |
82 | 0 | printf("Specify a filename or set the ${bootfile} environment variable\n"); |
83 | 0 | return 1; |
84 | 0 | } |
85 | 0 | } else { |
86 | 0 | filename = argv[5]; |
87 | 0 | env_set("bootfile", filename); |
88 | 0 | } |
89 | | |
90 | 0 | if (strstr(argv[3], "ext2")) { |
91 | 0 | info.fstype = FS_TYPE_EXT; |
92 | 0 | } else if (strstr(argv[3], "fat")) { |
93 | 0 | info.fstype = FS_TYPE_FAT; |
94 | 0 | } else if (strstr(argv[3], "any")) { |
95 | 0 | info.fstype = FS_TYPE_ANY; |
96 | 0 | } else { |
97 | 0 | printf("Invalid filesystem: %s\n", argv[3]); |
98 | 0 | return 1; |
99 | 0 | } |
100 | 0 | info.ifname = argv[1]; |
101 | 0 | info.dev_part_str = argv[2]; |
102 | |
|
103 | 0 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
104 | 0 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
105 | 0 | return 1; |
106 | 0 | } |
107 | | |
108 | 0 | if (pxe_setup_ctx(&ctx, cmdtp, sysboot_read_file, &info, true, |
109 | 0 | filename, false, false)) { |
110 | 0 | printf("Out of memory\n"); |
111 | 0 | return CMD_RET_FAILURE; |
112 | 0 | } |
113 | | |
114 | 0 | if (get_pxe_file(&ctx, filename, pxefile_addr_r) |
115 | 0 | < 0) { |
116 | 0 | printf("Error reading config file\n"); |
117 | 0 | pxe_destroy_ctx(&ctx); |
118 | 0 | return 1; |
119 | 0 | } |
120 | | |
121 | 0 | ret = pxe_process(&ctx, pxefile_addr_r, prompt); |
122 | 0 | pxe_destroy_ctx(&ctx); |
123 | 0 | if (ret) |
124 | 0 | return CMD_RET_FAILURE; |
125 | | |
126 | 0 | return 0; |
127 | 0 | } |
128 | | |
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'" |
134 | | ); |