cmd_pxe.c 34 KB

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