pxe.c 39 KB

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