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