cmd_pxe.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*
  2. * Copyright 2010-2011 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <common.h>
  18. #include <command.h>
  19. #include <malloc.h>
  20. #include <linux/string.h>
  21. #include <linux/ctype.h>
  22. #include <errno.h>
  23. #include <linux/list.h>
  24. #include "menu.h"
  25. #define MAX_TFTP_PATH_LEN 127
  26. const char *pxe_default_paths[] = {
  27. #ifdef CONFIG_SYS_SOC
  28. "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
  29. #endif
  30. "default-" CONFIG_SYS_ARCH,
  31. "default",
  32. NULL
  33. };
  34. /*
  35. * Like getenv, but prints an error if envvar isn't defined in the
  36. * environment. It always returns what getenv does, so it can be used in
  37. * place of getenv without changing error handling otherwise.
  38. */
  39. static char *from_env(const char *envvar)
  40. {
  41. char *ret;
  42. ret = getenv(envvar);
  43. if (!ret)
  44. printf("missing environment variable: %s\n", envvar);
  45. return ret;
  46. }
  47. /*
  48. * Convert an ethaddr from the environment to the format used by pxelinux
  49. * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
  50. * the beginning of the ethernet address to indicate a hardware type of
  51. * Ethernet. Also converts uppercase hex characters into lowercase, to match
  52. * pxelinux's behavior.
  53. *
  54. * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
  55. * environment, or some other value < 0 on error.
  56. */
  57. static int format_mac_pxe(char *outbuf, size_t outbuf_len)
  58. {
  59. uchar ethaddr[6];
  60. if (outbuf_len < 21) {
  61. printf("outbuf is too small (%d < 21)\n", outbuf_len);
  62. return -EINVAL;
  63. }
  64. if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
  65. ethaddr))
  66. return -ENOENT;
  67. sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
  68. ethaddr[0], ethaddr[1], ethaddr[2],
  69. ethaddr[3], ethaddr[4], ethaddr[5]);
  70. return 1;
  71. }
  72. /*
  73. * Returns the directory the file specified in the bootfile env variable is
  74. * in. If bootfile isn't defined in the environment, return NULL, which should
  75. * be interpreted as "don't prepend anything to paths".
  76. */
  77. static int get_bootfile_path(const char *file_path, char *bootfile_path,
  78. size_t bootfile_path_size)
  79. {
  80. char *bootfile, *last_slash;
  81. size_t path_len = 0;
  82. if (file_path[0] == '/')
  83. goto ret;
  84. bootfile = from_env("bootfile");
  85. if (!bootfile)
  86. goto ret;
  87. last_slash = strrchr(bootfile, '/');
  88. if (last_slash == NULL)
  89. goto ret;
  90. path_len = (last_slash - bootfile) + 1;
  91. if (bootfile_path_size < path_len) {
  92. printf("bootfile_path too small. (%d < %d)\n",
  93. bootfile_path_size, path_len);
  94. return -1;
  95. }
  96. strncpy(bootfile_path, bootfile, path_len);
  97. ret:
  98. bootfile_path[path_len] = '\0';
  99. return 1;
  100. }
  101. static int (*do_getfile)(const char *file_path, char *file_addr);
  102. static int do_get_tftp(const char *file_path, char *file_addr)
  103. {
  104. char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
  105. tftp_argv[1] = file_addr;
  106. tftp_argv[2] = (void *)file_path;
  107. if (do_tftpb(NULL, 0, 3, tftp_argv))
  108. return -ENOENT;
  109. return 1;
  110. }
  111. static char *fs_argv[5];
  112. static int do_get_ext2(const char *file_path, char *file_addr)
  113. {
  114. #ifdef CONFIG_CMD_EXT2
  115. fs_argv[0] = "ext2load";
  116. fs_argv[3] = file_addr;
  117. fs_argv[4] = (void *)file_path;
  118. if (!do_ext2load(NULL, 0, 5, fs_argv))
  119. return 1;
  120. #endif
  121. return -ENOENT;
  122. }
  123. static int do_get_fat(const char *file_path, char *file_addr)
  124. {
  125. #ifdef CONFIG_CMD_FAT
  126. fs_argv[0] = "fatload";
  127. fs_argv[3] = file_addr;
  128. fs_argv[4] = (void *)file_path;
  129. if (!do_fat_fsload(NULL, 0, 5, fs_argv))
  130. return 1;
  131. #endif
  132. return -ENOENT;
  133. }
  134. /*
  135. * As in pxelinux, paths to files referenced from files we retrieve are
  136. * relative to the location of bootfile. get_relfile takes such a path and
  137. * joins it with the bootfile path to get the full path to the target file. If
  138. * the bootfile path is NULL, we use file_path as is.
  139. *
  140. * Returns 1 for success, or < 0 on error.
  141. */
  142. static int get_relfile(const char *file_path, void *file_addr)
  143. {
  144. size_t path_len;
  145. char relfile[MAX_TFTP_PATH_LEN+1];
  146. char addr_buf[10];
  147. int err;
  148. err = get_bootfile_path(file_path, relfile, sizeof(relfile));
  149. if (err < 0)
  150. return err;
  151. path_len = strlen(file_path);
  152. path_len += strlen(relfile);
  153. if (path_len > MAX_TFTP_PATH_LEN) {
  154. printf("Base path too long (%s%s)\n",
  155. relfile,
  156. file_path);
  157. return -ENAMETOOLONG;
  158. }
  159. strcat(relfile, file_path);
  160. printf("Retrieving file: %s\n", relfile);
  161. sprintf(addr_buf, "%p", file_addr);
  162. return do_getfile(relfile, addr_buf);
  163. }
  164. /*
  165. * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
  166. * 'bootfile' was specified in the environment, the path to bootfile will be
  167. * prepended to 'file_path' and the resulting path will be used.
  168. *
  169. * Returns 1 on success, or < 0 for error.
  170. */
  171. static int get_pxe_file(const char *file_path, void *file_addr)
  172. {
  173. unsigned long config_file_size;
  174. char *tftp_filesize;
  175. int err;
  176. err = get_relfile(file_path, file_addr);
  177. if (err < 0)
  178. return err;
  179. /*
  180. * the file comes without a NUL byte at the end, so find out its size
  181. * and add the NUL byte.
  182. */
  183. tftp_filesize = from_env("filesize");
  184. if (!tftp_filesize)
  185. return -ENOENT;
  186. if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
  187. return -EINVAL;
  188. *(char *)(file_addr + config_file_size) = '\0';
  189. return 1;
  190. }
  191. #define PXELINUX_DIR "pxelinux.cfg/"
  192. /*
  193. * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
  194. * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
  195. * from the bootfile path, as described above.
  196. *
  197. * Returns 1 on success or < 0 on error.
  198. */
  199. static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
  200. {
  201. size_t base_len = strlen(PXELINUX_DIR);
  202. char path[MAX_TFTP_PATH_LEN+1];
  203. if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
  204. printf("path (%s%s) too long, skipping\n",
  205. PXELINUX_DIR, file);
  206. return -ENAMETOOLONG;
  207. }
  208. sprintf(path, PXELINUX_DIR "%s", file);
  209. return get_pxe_file(path, pxefile_addr_r);
  210. }
  211. /*
  212. * Looks for a pxe file with a name based on the pxeuuid environment variable.
  213. *
  214. * Returns 1 on success or < 0 on error.
  215. */
  216. static int pxe_uuid_path(void *pxefile_addr_r)
  217. {
  218. char *uuid_str;
  219. uuid_str = from_env("pxeuuid");
  220. if (!uuid_str)
  221. return -ENOENT;
  222. return get_pxelinux_path(uuid_str, pxefile_addr_r);
  223. }
  224. /*
  225. * Looks for a pxe file with a name based on the 'ethaddr' environment
  226. * variable.
  227. *
  228. * Returns 1 on success or < 0 on error.
  229. */
  230. static int pxe_mac_path(void *pxefile_addr_r)
  231. {
  232. char mac_str[21];
  233. int err;
  234. err = format_mac_pxe(mac_str, sizeof(mac_str));
  235. if (err < 0)
  236. return err;
  237. return get_pxelinux_path(mac_str, pxefile_addr_r);
  238. }
  239. /*
  240. * Looks for pxe files with names based on our IP address. See pxelinux
  241. * documentation for details on what these file names look like. We match
  242. * that exactly.
  243. *
  244. * Returns 1 on success or < 0 on error.
  245. */
  246. static int pxe_ipaddr_paths(void *pxefile_addr_r)
  247. {
  248. char ip_addr[9];
  249. int mask_pos, err;
  250. sprintf(ip_addr, "%08X", ntohl(NetOurIP));
  251. for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
  252. err = get_pxelinux_path(ip_addr, pxefile_addr_r);
  253. if (err > 0)
  254. return err;
  255. ip_addr[mask_pos] = '\0';
  256. }
  257. return -ENOENT;
  258. }
  259. /*
  260. * Entry point for the 'pxe get' command.
  261. * This Follows pxelinux's rules to download a config file from a tftp server.
  262. * The file is stored at the location given by the pxefile_addr_r environment
  263. * variable, which must be set.
  264. *
  265. * UUID comes from pxeuuid env variable, if defined
  266. * MAC addr comes from ethaddr env variable, if defined
  267. * IP
  268. *
  269. * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
  270. *
  271. * Returns 0 on success or 1 on error.
  272. */
  273. static int
  274. do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  275. {
  276. char *pxefile_addr_str;
  277. unsigned long pxefile_addr_r;
  278. int err, i = 0;
  279. do_getfile = do_get_tftp;
  280. if (argc != 1)
  281. return CMD_RET_USAGE;
  282. pxefile_addr_str = from_env("pxefile_addr_r");
  283. if (!pxefile_addr_str)
  284. return 1;
  285. err = strict_strtoul(pxefile_addr_str, 16,
  286. (unsigned long *)&pxefile_addr_r);
  287. if (err < 0)
  288. return 1;
  289. /*
  290. * Keep trying paths until we successfully get a file we're looking
  291. * for.
  292. */
  293. if (pxe_uuid_path((void *)pxefile_addr_r) > 0 ||
  294. pxe_mac_path((void *)pxefile_addr_r) > 0 ||
  295. pxe_ipaddr_paths((void *)pxefile_addr_r) > 0) {
  296. printf("Config file found\n");
  297. return 0;
  298. }
  299. while (pxe_default_paths[i]) {
  300. if (get_pxelinux_path(pxe_default_paths[i],
  301. (void *)pxefile_addr_r) > 0) {
  302. printf("Config file found\n");
  303. return 0;
  304. }
  305. i++;
  306. }
  307. printf("Config file not found\n");
  308. return 1;
  309. }
  310. /*
  311. * Wrapper to make it easier to store the file at file_path in the location
  312. * specified by envaddr_name. file_path will be joined to the bootfile path,
  313. * if any is specified.
  314. *
  315. * Returns 1 on success or < 0 on error.
  316. */
  317. static int get_relfile_envaddr(const char *file_path, const char *envaddr_name)
  318. {
  319. unsigned long file_addr;
  320. char *envaddr;
  321. envaddr = from_env(envaddr_name);
  322. if (!envaddr)
  323. return -ENOENT;
  324. if (strict_strtoul(envaddr, 16, &file_addr) < 0)
  325. return -EINVAL;
  326. return get_relfile(file_path, (void *)file_addr);
  327. }
  328. /*
  329. * A note on the pxe file parser.
  330. *
  331. * We're parsing files that use syslinux grammar, which has a few quirks.
  332. * String literals must be recognized based on context - there is no
  333. * quoting or escaping support. There's also nothing to explicitly indicate
  334. * when a label section completes. We deal with that by ending a label
  335. * section whenever we see a line that doesn't include.
  336. *
  337. * As with the syslinux family, this same file format could be reused in the
  338. * future for non pxe purposes. The only action it takes during parsing that
  339. * would throw this off is handling of include files. It assumes we're using
  340. * pxe, and does a tftp download of a file listed as an include file in the
  341. * middle of the parsing operation. That could be handled by refactoring it to
  342. * take a 'include file getter' function.
  343. */
  344. /*
  345. * Describes a single label given in a pxe file.
  346. *
  347. * Create these with the 'label_create' function given below.
  348. *
  349. * name - the name of the menu as given on the 'menu label' line.
  350. * kernel - the path to the kernel file to use for this label.
  351. * append - kernel command line to use when booting this label
  352. * initrd - path to the initrd to use for this label.
  353. * attempted - 0 if we haven't tried to boot this label, 1 if we have.
  354. * localboot - 1 if this label specified 'localboot', 0 otherwise.
  355. * list - lets these form a list, which a pxe_menu struct will hold.
  356. */
  357. struct pxe_label {
  358. char num[4];
  359. char *name;
  360. char *menu;
  361. char *kernel;
  362. char *append;
  363. char *initrd;
  364. char *fdt;
  365. int ipappend;
  366. int attempted;
  367. int localboot;
  368. int localboot_val;
  369. struct list_head list;
  370. };
  371. /*
  372. * Describes a pxe menu as given via pxe files.
  373. *
  374. * title - the name of the menu as given by a 'menu title' line.
  375. * default_label - the name of the default label, if any.
  376. * timeout - time in tenths of a second to wait for a user key-press before
  377. * booting the default label.
  378. * prompt - if 0, don't prompt for a choice unless the timeout period is
  379. * interrupted. If 1, always prompt for a choice regardless of
  380. * timeout.
  381. * labels - a list of labels defined for the menu.
  382. */
  383. struct pxe_menu {
  384. char *title;
  385. char *default_label;
  386. int timeout;
  387. int prompt;
  388. struct list_head labels;
  389. };
  390. /*
  391. * Allocates memory for and initializes a pxe_label. This uses malloc, so the
  392. * result must be free()'d to reclaim the memory.
  393. *
  394. * Returns NULL if malloc fails.
  395. */
  396. static struct pxe_label *label_create(void)
  397. {
  398. struct pxe_label *label;
  399. label = malloc(sizeof(struct pxe_label));
  400. if (!label)
  401. return NULL;
  402. memset(label, 0, sizeof(struct pxe_label));
  403. return label;
  404. }
  405. /*
  406. * Free the memory used by a pxe_label, including that used by its name,
  407. * kernel, append and initrd members, if they're non NULL.
  408. *
  409. * So - be sure to only use dynamically allocated memory for the members of
  410. * the pxe_label struct, unless you want to clean it up first. These are
  411. * currently only created by the pxe file parsing code.
  412. */
  413. static void label_destroy(struct pxe_label *label)
  414. {
  415. if (label->name)
  416. free(label->name);
  417. if (label->kernel)
  418. free(label->kernel);
  419. if (label->append)
  420. free(label->append);
  421. if (label->initrd)
  422. free(label->initrd);
  423. if (label->fdt)
  424. free(label->fdt);
  425. free(label);
  426. }
  427. /*
  428. * Print a label and its string members if they're defined.
  429. *
  430. * This is passed as a callback to the menu code for displaying each
  431. * menu entry.
  432. */
  433. static void label_print(void *data)
  434. {
  435. struct pxe_label *label = data;
  436. const char *c = label->menu ? label->menu : label->name;
  437. printf("%s:\t%s\n", label->num, c);
  438. }
  439. /*
  440. * Boot a label that specified 'localboot'. This requires that the 'localcmd'
  441. * environment variable is defined. Its contents will be executed as U-boot
  442. * command. If the label specified an 'append' line, its contents will be
  443. * used to overwrite the contents of the 'bootargs' environment variable prior
  444. * to running 'localcmd'.
  445. *
  446. * Returns 1 on success or < 0 on error.
  447. */
  448. static int label_localboot(struct pxe_label *label)
  449. {
  450. char *localcmd;
  451. localcmd = from_env("localcmd");
  452. if (!localcmd)
  453. return -ENOENT;
  454. if (label->append)
  455. setenv("bootargs", label->append);
  456. debug("running: %s\n", localcmd);
  457. return run_command_list(localcmd, strlen(localcmd), 0);
  458. }
  459. /*
  460. * Boot according to the contents of a pxe_label.
  461. *
  462. * If we can't boot for any reason, we return. A successful boot never
  463. * returns.
  464. *
  465. * The kernel will be stored in the location given by the 'kernel_addr_r'
  466. * environment variable.
  467. *
  468. * If the label specifies an initrd file, it will be stored in the location
  469. * given by the 'ramdisk_addr_r' environment variable.
  470. *
  471. * If the label specifies an 'append' line, its contents will overwrite that
  472. * of the 'bootargs' environment variable.
  473. */
  474. static int label_boot(struct pxe_label *label)
  475. {
  476. char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
  477. char initrd_str[22];
  478. char mac_str[29] = "";
  479. char ip_str[68] = "";
  480. char *bootargs;
  481. int bootm_argc = 3;
  482. int len = 0;
  483. label_print(label);
  484. label->attempted = 1;
  485. if (label->localboot) {
  486. if (label->localboot_val >= 0)
  487. label_localboot(label);
  488. return 0;
  489. }
  490. if (label->kernel == NULL) {
  491. printf("No kernel given, skipping %s\n",
  492. label->name);
  493. return 1;
  494. }
  495. if (label->initrd) {
  496. if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
  497. printf("Skipping %s for failure retrieving initrd\n",
  498. label->name);
  499. return 1;
  500. }
  501. bootm_argv[2] = initrd_str;
  502. strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
  503. strcat(bootm_argv[2], ":");
  504. strcat(bootm_argv[2], getenv("filesize"));
  505. } else {
  506. bootm_argv[2] = "-";
  507. }
  508. if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
  509. printf("Skipping %s for failure retrieving kernel\n",
  510. label->name);
  511. return 1;
  512. }
  513. if (label->ipappend & 0x1) {
  514. sprintf(ip_str, " ip=%s:%s:%s:%s",
  515. getenv("ipaddr"), getenv("serverip"),
  516. getenv("gatewayip"), getenv("netmask"));
  517. len += strlen(ip_str);
  518. }
  519. if (label->ipappend & 0x2) {
  520. int err;
  521. strcpy(mac_str, " BOOTIF=");
  522. err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
  523. if (err < 0)
  524. mac_str[0] = '\0';
  525. len += strlen(mac_str);
  526. }
  527. if (label->append)
  528. len += strlen(label->append);
  529. if (len) {
  530. bootargs = malloc(len + 1);
  531. if (!bootargs)
  532. return 1;
  533. bootargs[0] = '\0';
  534. if (label->append)
  535. strcpy(bootargs, label->append);
  536. strcat(bootargs, ip_str);
  537. strcat(bootargs, mac_str);
  538. setenv("bootargs", bootargs);
  539. printf("append: %s\n", bootargs);
  540. free(bootargs);
  541. }
  542. bootm_argv[1] = getenv("kernel_addr_r");
  543. /*
  544. * fdt usage is optional:
  545. * It handles the following scenarios. All scenarios are exclusive
  546. *
  547. * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
  548. * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
  549. * and adjust argc appropriately.
  550. *
  551. * Scenario 2: If there is an fdt_addr specified, pass it along to
  552. * bootm, and adjust argc appropriately.
  553. *
  554. * Scenario 3: fdt blob is not available.
  555. */
  556. bootm_argv[3] = getenv("fdt_addr_r");
  557. /* if fdt label is defined then get fdt from server */
  558. if (bootm_argv[3] && label->fdt) {
  559. if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
  560. printf("Skipping %s for failure retrieving fdt\n",
  561. label->name);
  562. return 1;
  563. }
  564. } else
  565. bootm_argv[3] = getenv("fdt_addr");
  566. if (bootm_argv[3])
  567. bootm_argc = 4;
  568. do_bootm(NULL, 0, bootm_argc, bootm_argv);
  569. #ifdef CONFIG_CMD_BOOTZ
  570. /* Try booting a zImage if do_bootm returns */
  571. do_bootz(NULL, 0, bootm_argc, bootm_argv);
  572. #endif
  573. return 1;
  574. }
  575. /*
  576. * Tokens for the pxe file parser.
  577. */
  578. enum token_type {
  579. T_EOL,
  580. T_STRING,
  581. T_EOF,
  582. T_MENU,
  583. T_TITLE,
  584. T_TIMEOUT,
  585. T_LABEL,
  586. T_KERNEL,
  587. T_LINUX,
  588. T_APPEND,
  589. T_INITRD,
  590. T_LOCALBOOT,
  591. T_DEFAULT,
  592. T_PROMPT,
  593. T_INCLUDE,
  594. T_FDT,
  595. T_ONTIMEOUT,
  596. T_IPAPPEND,
  597. T_INVALID
  598. };
  599. /*
  600. * A token - given by a value and a type.
  601. */
  602. struct token {
  603. char *val;
  604. enum token_type type;
  605. };
  606. /*
  607. * Keywords recognized.
  608. */
  609. static const struct token keywords[] = {
  610. {"menu", T_MENU},
  611. {"title", T_TITLE},
  612. {"timeout", T_TIMEOUT},
  613. {"default", T_DEFAULT},
  614. {"prompt", T_PROMPT},
  615. {"label", T_LABEL},
  616. {"kernel", T_KERNEL},
  617. {"linux", T_LINUX},
  618. {"localboot", T_LOCALBOOT},
  619. {"append", T_APPEND},
  620. {"initrd", T_INITRD},
  621. {"include", T_INCLUDE},
  622. {"fdt", T_FDT},
  623. {"ontimeout", T_ONTIMEOUT,},
  624. {"ipappend", T_IPAPPEND,},
  625. {NULL, T_INVALID}
  626. };
  627. /*
  628. * Since pxe(linux) files don't have a token to identify the start of a
  629. * literal, we have to keep track of when we're in a state where a literal is
  630. * expected vs when we're in a state a keyword is expected.
  631. */
  632. enum lex_state {
  633. L_NORMAL = 0,
  634. L_KEYWORD,
  635. L_SLITERAL
  636. };
  637. /*
  638. * get_string retrieves a string from *p and stores it as a token in
  639. * *t.
  640. *
  641. * get_string used for scanning both string literals and keywords.
  642. *
  643. * Characters from *p are copied into t-val until a character equal to
  644. * delim is found, or a NUL byte is reached. If delim has the special value of
  645. * ' ', any whitespace character will be used as a delimiter.
  646. *
  647. * If lower is unequal to 0, uppercase characters will be converted to
  648. * lowercase in the result. This is useful to make keywords case
  649. * insensitive.
  650. *
  651. * The location of *p is updated to point to the first character after the end
  652. * of the token - the ending delimiter.
  653. *
  654. * On success, the new value of t->val is returned. Memory for t->val is
  655. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  656. * memory is available, NULL is returned.
  657. */
  658. static char *get_string(char **p, struct token *t, char delim, int lower)
  659. {
  660. char *b, *e;
  661. size_t len, i;
  662. /*
  663. * b and e both start at the beginning of the input stream.
  664. *
  665. * e is incremented until we find the ending delimiter, or a NUL byte
  666. * is reached. Then, we take e - b to find the length of the token.
  667. */
  668. b = e = *p;
  669. while (*e) {
  670. if ((delim == ' ' && isspace(*e)) || delim == *e)
  671. break;
  672. e++;
  673. }
  674. len = e - b;
  675. /*
  676. * Allocate memory to hold the string, and copy it in, converting
  677. * characters to lowercase if lower is != 0.
  678. */
  679. t->val = malloc(len + 1);
  680. if (!t->val)
  681. return NULL;
  682. for (i = 0; i < len; i++, b++) {
  683. if (lower)
  684. t->val[i] = tolower(*b);
  685. else
  686. t->val[i] = *b;
  687. }
  688. t->val[len] = '\0';
  689. /*
  690. * Update *p so the caller knows where to continue scanning.
  691. */
  692. *p = e;
  693. t->type = T_STRING;
  694. return t->val;
  695. }
  696. /*
  697. * Populate a keyword token with a type and value.
  698. */
  699. static void get_keyword(struct token *t)
  700. {
  701. int i;
  702. for (i = 0; keywords[i].val; i++) {
  703. if (!strcmp(t->val, keywords[i].val)) {
  704. t->type = keywords[i].type;
  705. break;
  706. }
  707. }
  708. }
  709. /*
  710. * Get the next token. We have to keep track of which state we're in to know
  711. * if we're looking to get a string literal or a keyword.
  712. *
  713. * *p is updated to point at the first character after the current token.
  714. */
  715. static void get_token(char **p, struct token *t, enum lex_state state)
  716. {
  717. char *c = *p;
  718. t->type = T_INVALID;
  719. /* eat non EOL whitespace */
  720. while (isblank(*c))
  721. c++;
  722. /*
  723. * eat comments. note that string literals can't begin with #, but
  724. * can contain a # after their first character.
  725. */
  726. if (*c == '#') {
  727. while (*c && *c != '\n')
  728. c++;
  729. }
  730. if (*c == '\n') {
  731. t->type = T_EOL;
  732. c++;
  733. } else if (*c == '\0') {
  734. t->type = T_EOF;
  735. c++;
  736. } else if (state == L_SLITERAL) {
  737. get_string(&c, t, '\n', 0);
  738. } else if (state == L_KEYWORD) {
  739. /*
  740. * when we expect a keyword, we first get the next string
  741. * token delimited by whitespace, and then check if it
  742. * matches a keyword in our keyword list. if it does, it's
  743. * converted to a keyword token of the appropriate type, and
  744. * if not, it remains a string token.
  745. */
  746. get_string(&c, t, ' ', 1);
  747. get_keyword(t);
  748. }
  749. *p = c;
  750. }
  751. /*
  752. * Increment *c until we get to the end of the current line, or EOF.
  753. */
  754. static void eol_or_eof(char **c)
  755. {
  756. while (**c && **c != '\n')
  757. (*c)++;
  758. }
  759. /*
  760. * All of these parse_* functions share some common behavior.
  761. *
  762. * They finish with *c pointing after the token they parse, and return 1 on
  763. * success, or < 0 on error.
  764. */
  765. /*
  766. * Parse a string literal and store a pointer it at *dst. String literals
  767. * terminate at the end of the line.
  768. */
  769. static int parse_sliteral(char **c, char **dst)
  770. {
  771. struct token t;
  772. char *s = *c;
  773. get_token(c, &t, L_SLITERAL);
  774. if (t.type != T_STRING) {
  775. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  776. return -EINVAL;
  777. }
  778. *dst = t.val;
  779. return 1;
  780. }
  781. /*
  782. * Parse a base 10 (unsigned) integer and store it at *dst.
  783. */
  784. static int parse_integer(char **c, int *dst)
  785. {
  786. struct token t;
  787. char *s = *c;
  788. get_token(c, &t, L_SLITERAL);
  789. if (t.type != T_STRING) {
  790. printf("Expected string: %.*s\n", (int)(*c - s), s);
  791. return -EINVAL;
  792. }
  793. *dst = simple_strtol(t.val, NULL, 10);
  794. free(t.val);
  795. return 1;
  796. }
  797. static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
  798. /*
  799. * Parse an include statement, and retrieve and parse the file it mentions.
  800. *
  801. * base should point to a location where it's safe to store the file, and
  802. * nest_level should indicate how many nested includes have occurred. For this
  803. * include, nest_level has already been incremented and doesn't need to be
  804. * incremented here.
  805. */
  806. static int handle_include(char **c, char *base,
  807. struct pxe_menu *cfg, int nest_level)
  808. {
  809. char *include_path;
  810. char *s = *c;
  811. int err;
  812. err = parse_sliteral(c, &include_path);
  813. if (err < 0) {
  814. printf("Expected include path: %.*s\n",
  815. (int)(*c - s), s);
  816. return err;
  817. }
  818. err = get_pxe_file(include_path, base);
  819. if (err < 0) {
  820. printf("Couldn't retrieve %s\n", include_path);
  821. return err;
  822. }
  823. return parse_pxefile_top(base, cfg, nest_level);
  824. }
  825. /*
  826. * Parse lines that begin with 'menu'.
  827. *
  828. * b and nest are provided to handle the 'menu include' case.
  829. *
  830. * b should be the address where the file currently being parsed is stored.
  831. *
  832. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  833. * a file it includes, 3 when parsing a file included by that file, and so on.
  834. */
  835. static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
  836. {
  837. struct token t;
  838. char *s = *c;
  839. int err = 0;
  840. get_token(c, &t, L_KEYWORD);
  841. switch (t.type) {
  842. case T_TITLE:
  843. err = parse_sliteral(c, &cfg->title);
  844. break;
  845. case T_INCLUDE:
  846. err = handle_include(c, b + strlen(b) + 1, cfg,
  847. nest_level + 1);
  848. break;
  849. default:
  850. printf("Ignoring malformed menu command: %.*s\n",
  851. (int)(*c - s), s);
  852. }
  853. if (err < 0)
  854. return err;
  855. eol_or_eof(c);
  856. return 1;
  857. }
  858. /*
  859. * Handles parsing a 'menu line' when we're parsing a label.
  860. */
  861. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  862. struct pxe_label *label)
  863. {
  864. struct token t;
  865. char *s;
  866. s = *c;
  867. get_token(c, &t, L_KEYWORD);
  868. switch (t.type) {
  869. case T_DEFAULT:
  870. if (!cfg->default_label)
  871. cfg->default_label = strdup(label->name);
  872. if (!cfg->default_label)
  873. return -ENOMEM;
  874. break;
  875. case T_LABEL:
  876. parse_sliteral(c, &label->menu);
  877. break;
  878. default:
  879. printf("Ignoring malformed menu command: %.*s\n",
  880. (int)(*c - s), s);
  881. }
  882. eol_or_eof(c);
  883. return 0;
  884. }
  885. /*
  886. * Parses a label and adds it to the list of labels for a menu.
  887. *
  888. * A label ends when we either get to the end of a file, or
  889. * get some input we otherwise don't have a handler defined
  890. * for.
  891. *
  892. */
  893. static int parse_label(char **c, struct pxe_menu *cfg)
  894. {
  895. struct token t;
  896. int len;
  897. char *s = *c;
  898. struct pxe_label *label;
  899. int err;
  900. label = label_create();
  901. if (!label)
  902. return -ENOMEM;
  903. err = parse_sliteral(c, &label->name);
  904. if (err < 0) {
  905. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  906. label_destroy(label);
  907. return -EINVAL;
  908. }
  909. list_add_tail(&label->list, &cfg->labels);
  910. while (1) {
  911. s = *c;
  912. get_token(c, &t, L_KEYWORD);
  913. err = 0;
  914. switch (t.type) {
  915. case T_MENU:
  916. err = parse_label_menu(c, cfg, label);
  917. break;
  918. case T_KERNEL:
  919. case T_LINUX:
  920. err = parse_sliteral(c, &label->kernel);
  921. break;
  922. case T_APPEND:
  923. err = parse_sliteral(c, &label->append);
  924. if (label->initrd)
  925. break;
  926. s = strstr(label->append, "initrd=");
  927. if (!s)
  928. break;
  929. s += 7;
  930. len = (int)(strchr(s, ' ') - s);
  931. label->initrd = malloc(len + 1);
  932. strncpy(label->initrd, s, len);
  933. label->initrd[len] = '\0';
  934. break;
  935. case T_INITRD:
  936. if (!label->initrd)
  937. err = parse_sliteral(c, &label->initrd);
  938. break;
  939. case T_FDT:
  940. if (!label->fdt)
  941. err = parse_sliteral(c, &label->fdt);
  942. break;
  943. case T_LOCALBOOT:
  944. label->localboot = 1;
  945. err = parse_integer(c, &label->localboot_val);
  946. break;
  947. case T_IPAPPEND:
  948. err = parse_integer(c, &label->ipappend);
  949. break;
  950. case T_EOL:
  951. break;
  952. default:
  953. /*
  954. * put the token back! we don't want it - it's the end
  955. * of a label and whatever token this is, it's
  956. * something for the menu level context to handle.
  957. */
  958. *c = s;
  959. return 1;
  960. }
  961. if (err < 0)
  962. return err;
  963. }
  964. }
  965. /*
  966. * This 16 comes from the limit pxelinux imposes on nested includes.
  967. *
  968. * There is no reason at all we couldn't do more, but some limit helps prevent
  969. * infinite (until crash occurs) recursion if a file tries to include itself.
  970. */
  971. #define MAX_NEST_LEVEL 16
  972. /*
  973. * Entry point for parsing a menu file. nest_level indicates how many times
  974. * we've nested in includes. It will be 1 for the top level menu file.
  975. *
  976. * Returns 1 on success, < 0 on error.
  977. */
  978. static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
  979. {
  980. struct token t;
  981. char *s, *b, *label_name;
  982. int err;
  983. b = p;
  984. if (nest_level > MAX_NEST_LEVEL) {
  985. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  986. return -EMLINK;
  987. }
  988. while (1) {
  989. s = p;
  990. get_token(&p, &t, L_KEYWORD);
  991. err = 0;
  992. switch (t.type) {
  993. case T_MENU:
  994. cfg->prompt = 1;
  995. err = parse_menu(&p, cfg, b, nest_level);
  996. break;
  997. case T_TIMEOUT:
  998. err = parse_integer(&p, &cfg->timeout);
  999. break;
  1000. case T_LABEL:
  1001. err = parse_label(&p, cfg);
  1002. break;
  1003. case T_DEFAULT:
  1004. case T_ONTIMEOUT:
  1005. err = parse_sliteral(&p, &label_name);
  1006. if (label_name) {
  1007. if (cfg->default_label)
  1008. free(cfg->default_label);
  1009. cfg->default_label = label_name;
  1010. }
  1011. break;
  1012. case T_INCLUDE:
  1013. err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
  1014. nest_level + 1);
  1015. break;
  1016. case T_PROMPT:
  1017. eol_or_eof(&p);
  1018. break;
  1019. case T_EOL:
  1020. break;
  1021. case T_EOF:
  1022. return 1;
  1023. default:
  1024. printf("Ignoring unknown command: %.*s\n",
  1025. (int)(p - s), s);
  1026. eol_or_eof(&p);
  1027. }
  1028. if (err < 0)
  1029. return err;
  1030. }
  1031. }
  1032. /*
  1033. * Free the memory used by a pxe_menu and its labels.
  1034. */
  1035. static void destroy_pxe_menu(struct pxe_menu *cfg)
  1036. {
  1037. struct list_head *pos, *n;
  1038. struct pxe_label *label;
  1039. if (cfg->title)
  1040. free(cfg->title);
  1041. if (cfg->default_label)
  1042. free(cfg->default_label);
  1043. list_for_each_safe(pos, n, &cfg->labels) {
  1044. label = list_entry(pos, struct pxe_label, list);
  1045. label_destroy(label);
  1046. }
  1047. free(cfg);
  1048. }
  1049. /*
  1050. * Entry point for parsing a pxe file. This is only used for the top level
  1051. * file.
  1052. *
  1053. * Returns NULL if there is an error, otherwise, returns a pointer to a
  1054. * pxe_menu struct populated with the results of parsing the pxe file (and any
  1055. * files it includes). The resulting pxe_menu struct can be free()'d by using
  1056. * the destroy_pxe_menu() function.
  1057. */
  1058. static struct pxe_menu *parse_pxefile(char *menucfg)
  1059. {
  1060. struct pxe_menu *cfg;
  1061. cfg = malloc(sizeof(struct pxe_menu));
  1062. if (!cfg)
  1063. return NULL;
  1064. memset(cfg, 0, sizeof(struct pxe_menu));
  1065. INIT_LIST_HEAD(&cfg->labels);
  1066. if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
  1067. destroy_pxe_menu(cfg);
  1068. return NULL;
  1069. }
  1070. return cfg;
  1071. }
  1072. /*
  1073. * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
  1074. * menu code.
  1075. */
  1076. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1077. {
  1078. struct pxe_label *label;
  1079. struct list_head *pos;
  1080. struct menu *m;
  1081. int err;
  1082. int i = 1;
  1083. char *default_num = NULL;
  1084. /*
  1085. * Create a menu and add items for all the labels.
  1086. */
  1087. m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
  1088. NULL, NULL);
  1089. if (!m)
  1090. return NULL;
  1091. list_for_each(pos, &cfg->labels) {
  1092. label = list_entry(pos, struct pxe_label, list);
  1093. sprintf(label->num, "%d", i++);
  1094. if (menu_item_add(m, label->num, label) != 1) {
  1095. menu_destroy(m);
  1096. return NULL;
  1097. }
  1098. if (cfg->default_label &&
  1099. (strcmp(label->name, cfg->default_label) == 0))
  1100. default_num = label->num;
  1101. }
  1102. /*
  1103. * After we've created items for each label in the menu, set the
  1104. * menu's default label if one was specified.
  1105. */
  1106. if (default_num) {
  1107. err = menu_default_set(m, default_num);
  1108. if (err != 1) {
  1109. if (err != -ENOENT) {
  1110. menu_destroy(m);
  1111. return NULL;
  1112. }
  1113. printf("Missing default: %s\n", cfg->default_label);
  1114. }
  1115. }
  1116. return m;
  1117. }
  1118. /*
  1119. * Try to boot any labels we have yet to attempt to boot.
  1120. */
  1121. static void boot_unattempted_labels(struct pxe_menu *cfg)
  1122. {
  1123. struct list_head *pos;
  1124. struct pxe_label *label;
  1125. list_for_each(pos, &cfg->labels) {
  1126. label = list_entry(pos, struct pxe_label, list);
  1127. if (!label->attempted)
  1128. label_boot(label);
  1129. }
  1130. }
  1131. /*
  1132. * Boot the system as prescribed by a pxe_menu.
  1133. *
  1134. * Use the menu system to either get the user's choice or the default, based
  1135. * on config or user input. If there is no default or user's choice,
  1136. * attempted to boot labels in the order they were given in pxe files.
  1137. * If the default or user's choice fails to boot, attempt to boot other
  1138. * labels in the order they were given in pxe files.
  1139. *
  1140. * If this function returns, there weren't any labels that successfully
  1141. * booted, or the user interrupted the menu selection via ctrl+c.
  1142. */
  1143. static void handle_pxe_menu(struct pxe_menu *cfg)
  1144. {
  1145. void *choice;
  1146. struct menu *m;
  1147. int err;
  1148. m = pxe_menu_to_menu(cfg);
  1149. if (!m)
  1150. return;
  1151. err = menu_get_choice(m, &choice);
  1152. menu_destroy(m);
  1153. /*
  1154. * err == 1 means we got a choice back from menu_get_choice.
  1155. *
  1156. * err == -ENOENT if the menu was setup to select the default but no
  1157. * default was set. in that case, we should continue trying to boot
  1158. * labels that haven't been attempted yet.
  1159. *
  1160. * otherwise, the user interrupted or there was some other error and
  1161. * we give up.
  1162. */
  1163. if (err == 1) {
  1164. err = label_boot(choice);
  1165. if (!err)
  1166. return;
  1167. } else if (err != -ENOENT) {
  1168. return;
  1169. }
  1170. boot_unattempted_labels(cfg);
  1171. }
  1172. /*
  1173. * Boots a system using a pxe file
  1174. *
  1175. * Returns 0 on success, 1 on error.
  1176. */
  1177. static int
  1178. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1179. {
  1180. unsigned long pxefile_addr_r;
  1181. struct pxe_menu *cfg;
  1182. char *pxefile_addr_str;
  1183. do_getfile = do_get_tftp;
  1184. if (argc == 1) {
  1185. pxefile_addr_str = from_env("pxefile_addr_r");
  1186. if (!pxefile_addr_str)
  1187. return 1;
  1188. } else if (argc == 2) {
  1189. pxefile_addr_str = argv[1];
  1190. } else {
  1191. return CMD_RET_USAGE;
  1192. }
  1193. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1194. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1195. return 1;
  1196. }
  1197. cfg = parse_pxefile((char *)(pxefile_addr_r));
  1198. if (cfg == NULL) {
  1199. printf("Error parsing config file\n");
  1200. return 1;
  1201. }
  1202. handle_pxe_menu(cfg);
  1203. destroy_pxe_menu(cfg);
  1204. return 0;
  1205. }
  1206. static cmd_tbl_t cmd_pxe_sub[] = {
  1207. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  1208. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  1209. };
  1210. int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1211. {
  1212. cmd_tbl_t *cp;
  1213. if (argc < 2)
  1214. return CMD_RET_USAGE;
  1215. /* drop initial "pxe" arg */
  1216. argc--;
  1217. argv++;
  1218. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  1219. if (cp)
  1220. return cp->cmd(cmdtp, flag, argc, argv);
  1221. return CMD_RET_USAGE;
  1222. }
  1223. U_BOOT_CMD(
  1224. pxe, 3, 1, do_pxe,
  1225. "commands to get and boot from pxe files",
  1226. "get - try to retrieve a pxe file using tftp\npxe "
  1227. "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  1228. );
  1229. /*
  1230. * Boots a system using a local disk syslinux/extlinux file
  1231. *
  1232. * Returns 0 on success, 1 on error.
  1233. */
  1234. int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1235. {
  1236. unsigned long pxefile_addr_r;
  1237. struct pxe_menu *cfg;
  1238. char *pxefile_addr_str;
  1239. char *filename;
  1240. int prompt = 0;
  1241. if (strstr(argv[1], "-p")) {
  1242. prompt = 1;
  1243. argc--;
  1244. argv++;
  1245. }
  1246. if (argc < 4)
  1247. return cmd_usage(cmdtp);
  1248. if (argc < 5) {
  1249. pxefile_addr_str = from_env("pxefile_addr_r");
  1250. if (!pxefile_addr_str)
  1251. return 1;
  1252. } else {
  1253. pxefile_addr_str = argv[4];
  1254. }
  1255. if (argc < 6)
  1256. filename = getenv("bootfile");
  1257. else {
  1258. filename = argv[5];
  1259. setenv("bootfile", filename);
  1260. }
  1261. if (strstr(argv[3], "ext2"))
  1262. do_getfile = do_get_ext2;
  1263. else if (strstr(argv[3], "fat"))
  1264. do_getfile = do_get_fat;
  1265. else {
  1266. printf("Invalid filesystem: %s\n", argv[3]);
  1267. return 1;
  1268. }
  1269. fs_argv[1] = argv[1];
  1270. fs_argv[2] = argv[2];
  1271. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1272. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1273. return 1;
  1274. }
  1275. if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
  1276. printf("Error reading config file\n");
  1277. return 1;
  1278. }
  1279. cfg = parse_pxefile((char *)(pxefile_addr_r));
  1280. if (cfg == NULL) {
  1281. printf("Error parsing config file\n");
  1282. return 1;
  1283. }
  1284. if (prompt)
  1285. cfg->prompt = 1;
  1286. handle_pxe_menu(cfg);
  1287. destroy_pxe_menu(cfg);
  1288. return 0;
  1289. }
  1290. U_BOOT_CMD(
  1291. sysboot, 7, 1, do_sysboot,
  1292. "command to get and boot from syslinux files",
  1293. "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
  1294. " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
  1295. " filesystem on 'dev' on 'interface' to address 'addr'"
  1296. );