Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010-2011 Calxeda, Inc. |
Bryan Wu | a1d715b | 2014-07-31 17:39:59 -0700 | [diff] [blame] | 4 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 5 | */ |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <command.h> |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 9 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 10 | #include "pxe_utils.h" |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 11 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 12 | #ifdef CONFIG_CMD_NET |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 13 | const char *pxe_default_paths[] = { |
Joe Hershberger | 921a71d | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 14 | #ifdef CONFIG_SYS_SOC |
Marek BehĂșn | 016ed13 | 2019-05-02 15:29:12 +0200 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_BOARD |
| 16 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD, |
| 17 | #endif |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 18 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
Joe Hershberger | 921a71d | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 19 | #endif |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 20 | "default-" CONFIG_SYS_ARCH, |
| 21 | "default", |
| 22 | NULL |
| 23 | }; |
| 24 | |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 25 | static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 26 | { |
| 27 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; |
| 28 | |
| 29 | tftp_argv[1] = file_addr; |
Rob Herring | 824901c | 2012-12-02 21:00:21 -0600 | [diff] [blame] | 30 | tftp_argv[2] = (void *)file_path; |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 31 | |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 32 | if (do_tftpb(cmdtp, 0, 3, tftp_argv)) |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 33 | return -ENOENT; |
| 34 | |
| 35 | return 1; |
| 36 | } |
Stephen Warren | 857291b | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 37 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 38 | /* |
| 39 | * Looks for a pxe file with a name based on the pxeuuid environment variable. |
| 40 | * |
| 41 | * Returns 1 on success or < 0 on error. |
| 42 | */ |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 43 | static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 44 | { |
| 45 | char *uuid_str; |
| 46 | |
| 47 | uuid_str = from_env("pxeuuid"); |
| 48 | |
| 49 | if (!uuid_str) |
| 50 | return -ENOENT; |
| 51 | |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 52 | return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Looks for a pxe file with a name based on the 'ethaddr' environment |
| 57 | * variable. |
| 58 | * |
| 59 | * Returns 1 on success or < 0 on error. |
| 60 | */ |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 61 | static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 62 | { |
| 63 | char mac_str[21]; |
| 64 | int err; |
| 65 | |
| 66 | err = format_mac_pxe(mac_str, sizeof(mac_str)); |
| 67 | |
| 68 | if (err < 0) |
| 69 | return err; |
| 70 | |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 71 | return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Looks for pxe files with names based on our IP address. See pxelinux |
| 76 | * documentation for details on what these file names look like. We match |
| 77 | * that exactly. |
| 78 | * |
| 79 | * Returns 1 on success or < 0 on error. |
| 80 | */ |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 81 | static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 82 | { |
| 83 | char ip_addr[9]; |
| 84 | int mask_pos, err; |
| 85 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 86 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 87 | |
| 88 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 89 | err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 90 | |
| 91 | if (err > 0) |
| 92 | return err; |
| 93 | |
| 94 | ip_addr[mask_pos] = '\0'; |
| 95 | } |
| 96 | |
| 97 | return -ENOENT; |
| 98 | } |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 99 | /* |
| 100 | * Entry point for the 'pxe get' command. |
| 101 | * This Follows pxelinux's rules to download a config file from a tftp server. |
| 102 | * The file is stored at the location given by the pxefile_addr_r environment |
| 103 | * variable, which must be set. |
| 104 | * |
| 105 | * UUID comes from pxeuuid env variable, if defined |
| 106 | * MAC addr comes from ethaddr env variable, if defined |
| 107 | * IP |
| 108 | * |
| 109 | * see http://syslinux.zytor.com/wiki/index.php/PXELINUX |
| 110 | * |
| 111 | * Returns 0 on success or 1 on error. |
| 112 | */ |
| 113 | static int |
| 114 | do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 115 | { |
| 116 | char *pxefile_addr_str; |
Jason Hobbs | 9135c79 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 117 | unsigned long pxefile_addr_r; |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 118 | int err, i = 0; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 119 | |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 120 | do_getfile = do_get_tftp; |
| 121 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 122 | if (argc != 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 123 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 124 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 125 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 126 | |
| 127 | if (!pxefile_addr_str) |
| 128 | return 1; |
| 129 | |
| 130 | err = strict_strtoul(pxefile_addr_str, 16, |
Patrice Chotard | 7f87165 | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 131 | (unsigned long *)&pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 132 | if (err < 0) |
| 133 | return 1; |
| 134 | |
| 135 | /* |
| 136 | * Keep trying paths until we successfully get a file we're looking |
| 137 | * for. |
| 138 | */ |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 139 | if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 || |
| 140 | pxe_mac_path(cmdtp, pxefile_addr_r) > 0 || |
| 141 | pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) { |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 142 | printf("Config file found\n"); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 147 | while (pxe_default_paths[i]) { |
Steven Falco | 7de0fa8 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 148 | if (get_pxelinux_path(cmdtp, pxe_default_paths[i], |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 149 | pxefile_addr_r) > 0) { |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 150 | printf("Config file found\n"); |
| 151 | return 0; |
| 152 | } |
| 153 | i++; |
| 154 | } |
| 155 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 156 | printf("Config file not found\n"); |
| 157 | |
Rob Herring | 58d7f43 | 2012-12-02 21:00:22 -0600 | [diff] [blame] | 158 | return 1; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 162 | * Boots a system using a pxe file |
| 163 | * |
| 164 | * Returns 0 on success, 1 on error. |
| 165 | */ |
| 166 | static int |
| 167 | do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 168 | { |
| 169 | unsigned long pxefile_addr_r; |
| 170 | struct pxe_menu *cfg; |
| 171 | char *pxefile_addr_str; |
| 172 | |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 173 | do_getfile = do_get_tftp; |
| 174 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 175 | if (argc == 1) { |
| 176 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 177 | if (!pxefile_addr_str) |
| 178 | return 1; |
| 179 | |
| 180 | } else if (argc == 2) { |
| 181 | pxefile_addr_str = argv[1]; |
| 182 | } else { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 183 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 187 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 188 | return 1; |
| 189 | } |
| 190 | |
Sjoerd Simons | 45af07c | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 191 | cfg = parse_pxefile(cmdtp, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 192 | |
Patrice Chotard | 7f87165 | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 193 | if (!cfg) { |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 194 | printf("Error parsing config file\n"); |
| 195 | return 1; |
| 196 | } |
| 197 | |
Tom Rini | 5b5b176 | 2013-09-24 09:05:08 -0400 | [diff] [blame] | 198 | handle_pxe_menu(cmdtp, cfg); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 199 | |
| 200 | destroy_pxe_menu(cfg); |
| 201 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 202 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
Stephen Warren | c24cbc2 | 2014-07-22 18:06:46 -0600 | [diff] [blame] | 203 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static cmd_tbl_t cmd_pxe_sub[] = { |
| 208 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), |
| 209 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") |
| 210 | }; |
| 211 | |
Jeroen Hofstee | f384fbf | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 212 | static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 213 | { |
| 214 | cmd_tbl_t *cp; |
| 215 | |
| 216 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 217 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 218 | |
Rob Herring | 759456e | 2013-10-18 13:04:42 -0500 | [diff] [blame] | 219 | is_pxe = true; |
| 220 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 221 | /* drop initial "pxe" arg */ |
| 222 | argc--; |
| 223 | argv++; |
| 224 | |
| 225 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 226 | |
| 227 | if (cp) |
| 228 | return cp->cmd(cmdtp, flag, argc, argv); |
| 229 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 230 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Patrice Chotard | 7f87165 | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 233 | U_BOOT_CMD(pxe, 3, 1, do_pxe, |
| 234 | "commands to get and boot from pxe files", |
| 235 | "get - try to retrieve a pxe file using tftp\n" |
| 236 | "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 237 | ); |
Stephen Warren | 857291b | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 238 | #endif |