pxe.c 37 KB

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