cmd_pxe.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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 = 3;
  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. } else {
  532. bootm_argv[2] = "-";
  533. }
  534. if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
  535. printf("Skipping %s for failure retrieving kernel\n",
  536. label->name);
  537. return 1;
  538. }
  539. if (label->ipappend & 0x1) {
  540. sprintf(ip_str, " ip=%s:%s:%s:%s",
  541. getenv("ipaddr"), getenv("serverip"),
  542. getenv("gatewayip"), getenv("netmask"));
  543. }
  544. #ifdef CONFIG_CMD_NET
  545. if (label->ipappend & 0x2) {
  546. int err;
  547. strcpy(mac_str, " BOOTIF=");
  548. err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
  549. if (err < 0)
  550. mac_str[0] = '\0';
  551. }
  552. #endif
  553. if ((label->ipappend & 0x3) || label->append) {
  554. char bootargs[CONFIG_SYS_CBSIZE] = "";
  555. char finalbootargs[CONFIG_SYS_CBSIZE];
  556. if (strlen(label->append ?: "") +
  557. strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
  558. printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
  559. strlen(label->append ?: ""),
  560. strlen(ip_str), strlen(mac_str),
  561. sizeof(bootargs));
  562. return 1;
  563. }
  564. if (label->append)
  565. strcpy(bootargs, label->append);
  566. strcat(bootargs, ip_str);
  567. strcat(bootargs, mac_str);
  568. cli_simple_process_macros(bootargs, finalbootargs);
  569. setenv("bootargs", finalbootargs);
  570. printf("append: %s\n", finalbootargs);
  571. }
  572. bootm_argv[1] = getenv("kernel_addr_r");
  573. /*
  574. * fdt usage is optional:
  575. * It handles the following scenarios. All scenarios are exclusive
  576. *
  577. * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
  578. * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
  579. * and adjust argc appropriately.
  580. *
  581. * Scenario 2: If there is an fdt_addr specified, pass it along to
  582. * bootm, and adjust argc appropriately.
  583. *
  584. * Scenario 3: fdt blob is not available.
  585. */
  586. bootm_argv[3] = getenv("fdt_addr_r");
  587. /* if fdt label is defined then get fdt from server */
  588. if (bootm_argv[3]) {
  589. char *fdtfile = NULL;
  590. char *fdtfilefree = NULL;
  591. if (label->fdt) {
  592. fdtfile = label->fdt;
  593. } else if (label->fdtdir) {
  594. char *f1, *f2, *f3, *f4, *slash;
  595. f1 = getenv("fdtfile");
  596. if (f1) {
  597. f2 = "";
  598. f3 = "";
  599. f4 = "";
  600. } else {
  601. /*
  602. * For complex cases where this code doesn't
  603. * generate the correct filename, the board
  604. * code should set $fdtfile during early boot,
  605. * or the boot scripts should set $fdtfile
  606. * before invoking "pxe" or "sysboot".
  607. */
  608. f1 = getenv("soc");
  609. f2 = "-";
  610. f3 = getenv("board");
  611. f4 = ".dtb";
  612. }
  613. len = strlen(label->fdtdir);
  614. if (!len)
  615. slash = "./";
  616. else if (label->fdtdir[len - 1] != '/')
  617. slash = "/";
  618. else
  619. slash = "";
  620. len = strlen(label->fdtdir) + strlen(slash) +
  621. strlen(f1) + strlen(f2) + strlen(f3) +
  622. strlen(f4) + 1;
  623. fdtfilefree = malloc(len);
  624. if (!fdtfilefree) {
  625. printf("malloc fail (FDT filename)\n");
  626. return 1;
  627. }
  628. snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
  629. label->fdtdir, slash, f1, f2, f3, f4);
  630. fdtfile = fdtfilefree;
  631. }
  632. if (fdtfile) {
  633. int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
  634. free(fdtfilefree);
  635. if (err < 0) {
  636. printf("Skipping %s for failure retrieving fdt\n",
  637. label->name);
  638. return 1;
  639. }
  640. } else {
  641. bootm_argv[3] = NULL;
  642. }
  643. }
  644. if (!bootm_argv[3])
  645. bootm_argv[3] = getenv("fdt_addr");
  646. if (bootm_argv[3])
  647. bootm_argc = 4;
  648. kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
  649. buf = map_sysmem(kernel_addr, 0);
  650. /* Try bootm for legacy and FIT format image */
  651. if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
  652. do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
  653. #ifdef CONFIG_CMD_BOOTZ
  654. /* Try booting a zImage */
  655. else
  656. do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
  657. #endif
  658. unmap_sysmem(buf);
  659. return 1;
  660. }
  661. /*
  662. * Tokens for the pxe file parser.
  663. */
  664. enum token_type {
  665. T_EOL,
  666. T_STRING,
  667. T_EOF,
  668. T_MENU,
  669. T_TITLE,
  670. T_TIMEOUT,
  671. T_LABEL,
  672. T_KERNEL,
  673. T_LINUX,
  674. T_APPEND,
  675. T_INITRD,
  676. T_LOCALBOOT,
  677. T_DEFAULT,
  678. T_PROMPT,
  679. T_INCLUDE,
  680. T_FDT,
  681. T_FDTDIR,
  682. T_ONTIMEOUT,
  683. T_IPAPPEND,
  684. T_INVALID
  685. };
  686. /*
  687. * A token - given by a value and a type.
  688. */
  689. struct token {
  690. char *val;
  691. enum token_type type;
  692. };
  693. /*
  694. * Keywords recognized.
  695. */
  696. static const struct token keywords[] = {
  697. {"menu", T_MENU},
  698. {"title", T_TITLE},
  699. {"timeout", T_TIMEOUT},
  700. {"default", T_DEFAULT},
  701. {"prompt", T_PROMPT},
  702. {"label", T_LABEL},
  703. {"kernel", T_KERNEL},
  704. {"linux", T_LINUX},
  705. {"localboot", T_LOCALBOOT},
  706. {"append", T_APPEND},
  707. {"initrd", T_INITRD},
  708. {"include", T_INCLUDE},
  709. {"devicetree", T_FDT},
  710. {"fdt", T_FDT},
  711. {"devicetreedir", T_FDTDIR},
  712. {"fdtdir", T_FDTDIR},
  713. {"ontimeout", T_ONTIMEOUT,},
  714. {"ipappend", T_IPAPPEND,},
  715. {NULL, T_INVALID}
  716. };
  717. /*
  718. * Since pxe(linux) files don't have a token to identify the start of a
  719. * literal, we have to keep track of when we're in a state where a literal is
  720. * expected vs when we're in a state a keyword is expected.
  721. */
  722. enum lex_state {
  723. L_NORMAL = 0,
  724. L_KEYWORD,
  725. L_SLITERAL
  726. };
  727. /*
  728. * get_string retrieves a string from *p and stores it as a token in
  729. * *t.
  730. *
  731. * get_string used for scanning both string literals and keywords.
  732. *
  733. * Characters from *p are copied into t-val until a character equal to
  734. * delim is found, or a NUL byte is reached. If delim has the special value of
  735. * ' ', any whitespace character will be used as a delimiter.
  736. *
  737. * If lower is unequal to 0, uppercase characters will be converted to
  738. * lowercase in the result. This is useful to make keywords case
  739. * insensitive.
  740. *
  741. * The location of *p is updated to point to the first character after the end
  742. * of the token - the ending delimiter.
  743. *
  744. * On success, the new value of t->val is returned. Memory for t->val is
  745. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  746. * memory is available, NULL is returned.
  747. */
  748. static char *get_string(char **p, struct token *t, char delim, int lower)
  749. {
  750. char *b, *e;
  751. size_t len, i;
  752. /*
  753. * b and e both start at the beginning of the input stream.
  754. *
  755. * e is incremented until we find the ending delimiter, or a NUL byte
  756. * is reached. Then, we take e - b to find the length of the token.
  757. */
  758. b = e = *p;
  759. while (*e) {
  760. if ((delim == ' ' && isspace(*e)) || delim == *e)
  761. break;
  762. e++;
  763. }
  764. len = e - b;
  765. /*
  766. * Allocate memory to hold the string, and copy it in, converting
  767. * characters to lowercase if lower is != 0.
  768. */
  769. t->val = malloc(len + 1);
  770. if (!t->val)
  771. return NULL;
  772. for (i = 0; i < len; i++, b++) {
  773. if (lower)
  774. t->val[i] = tolower(*b);
  775. else
  776. t->val[i] = *b;
  777. }
  778. t->val[len] = '\0';
  779. /*
  780. * Update *p so the caller knows where to continue scanning.
  781. */
  782. *p = e;
  783. t->type = T_STRING;
  784. return t->val;
  785. }
  786. /*
  787. * Populate a keyword token with a type and value.
  788. */
  789. static void get_keyword(struct token *t)
  790. {
  791. int i;
  792. for (i = 0; keywords[i].val; i++) {
  793. if (!strcmp(t->val, keywords[i].val)) {
  794. t->type = keywords[i].type;
  795. break;
  796. }
  797. }
  798. }
  799. /*
  800. * Get the next token. We have to keep track of which state we're in to know
  801. * if we're looking to get a string literal or a keyword.
  802. *
  803. * *p is updated to point at the first character after the current token.
  804. */
  805. static void get_token(char **p, struct token *t, enum lex_state state)
  806. {
  807. char *c = *p;
  808. t->type = T_INVALID;
  809. /* eat non EOL whitespace */
  810. while (isblank(*c))
  811. c++;
  812. /*
  813. * eat comments. note that string literals can't begin with #, but
  814. * can contain a # after their first character.
  815. */
  816. if (*c == '#') {
  817. while (*c && *c != '\n')
  818. c++;
  819. }
  820. if (*c == '\n') {
  821. t->type = T_EOL;
  822. c++;
  823. } else if (*c == '\0') {
  824. t->type = T_EOF;
  825. c++;
  826. } else if (state == L_SLITERAL) {
  827. get_string(&c, t, '\n', 0);
  828. } else if (state == L_KEYWORD) {
  829. /*
  830. * when we expect a keyword, we first get the next string
  831. * token delimited by whitespace, and then check if it
  832. * matches a keyword in our keyword list. if it does, it's
  833. * converted to a keyword token of the appropriate type, and
  834. * if not, it remains a string token.
  835. */
  836. get_string(&c, t, ' ', 1);
  837. get_keyword(t);
  838. }
  839. *p = c;
  840. }
  841. /*
  842. * Increment *c until we get to the end of the current line, or EOF.
  843. */
  844. static void eol_or_eof(char **c)
  845. {
  846. while (**c && **c != '\n')
  847. (*c)++;
  848. }
  849. /*
  850. * All of these parse_* functions share some common behavior.
  851. *
  852. * They finish with *c pointing after the token they parse, and return 1 on
  853. * success, or < 0 on error.
  854. */
  855. /*
  856. * Parse a string literal and store a pointer it at *dst. String literals
  857. * terminate at the end of the line.
  858. */
  859. static int parse_sliteral(char **c, char **dst)
  860. {
  861. struct token t;
  862. char *s = *c;
  863. get_token(c, &t, L_SLITERAL);
  864. if (t.type != T_STRING) {
  865. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  866. return -EINVAL;
  867. }
  868. *dst = t.val;
  869. return 1;
  870. }
  871. /*
  872. * Parse a base 10 (unsigned) integer and store it at *dst.
  873. */
  874. static int parse_integer(char **c, int *dst)
  875. {
  876. struct token t;
  877. char *s = *c;
  878. get_token(c, &t, L_SLITERAL);
  879. if (t.type != T_STRING) {
  880. printf("Expected string: %.*s\n", (int)(*c - s), s);
  881. return -EINVAL;
  882. }
  883. *dst = simple_strtol(t.val, NULL, 10);
  884. free(t.val);
  885. return 1;
  886. }
  887. static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
  888. struct pxe_menu *cfg, int nest_level);
  889. /*
  890. * Parse an include statement, and retrieve and parse the file it mentions.
  891. *
  892. * base should point to a location where it's safe to store the file, and
  893. * nest_level should indicate how many nested includes have occurred. For this
  894. * include, nest_level has already been incremented and doesn't need to be
  895. * incremented here.
  896. */
  897. static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
  898. struct pxe_menu *cfg, int nest_level)
  899. {
  900. char *include_path;
  901. char *s = *c;
  902. int err;
  903. char *buf;
  904. int ret;
  905. err = parse_sliteral(c, &include_path);
  906. if (err < 0) {
  907. printf("Expected include path: %.*s\n",
  908. (int)(*c - s), s);
  909. return err;
  910. }
  911. err = get_pxe_file(cmdtp, include_path, base);
  912. if (err < 0) {
  913. printf("Couldn't retrieve %s\n", include_path);
  914. return err;
  915. }
  916. buf = map_sysmem(base, 0);
  917. ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
  918. unmap_sysmem(buf);
  919. return ret;
  920. }
  921. /*
  922. * Parse lines that begin with 'menu'.
  923. *
  924. * base and nest are provided to handle the 'menu include' case.
  925. *
  926. * base should point to a location where it's safe to store the included file.
  927. *
  928. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  929. * a file it includes, 3 when parsing a file included by that file, and so on.
  930. */
  931. static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
  932. unsigned long base, int nest_level)
  933. {
  934. struct token t;
  935. char *s = *c;
  936. int err = 0;
  937. get_token(c, &t, L_KEYWORD);
  938. switch (t.type) {
  939. case T_TITLE:
  940. err = parse_sliteral(c, &cfg->title);
  941. break;
  942. case T_INCLUDE:
  943. err = handle_include(cmdtp, c, base, cfg,
  944. nest_level + 1);
  945. break;
  946. default:
  947. printf("Ignoring malformed menu command: %.*s\n",
  948. (int)(*c - s), s);
  949. }
  950. if (err < 0)
  951. return err;
  952. eol_or_eof(c);
  953. return 1;
  954. }
  955. /*
  956. * Handles parsing a 'menu line' when we're parsing a label.
  957. */
  958. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  959. struct pxe_label *label)
  960. {
  961. struct token t;
  962. char *s;
  963. s = *c;
  964. get_token(c, &t, L_KEYWORD);
  965. switch (t.type) {
  966. case T_DEFAULT:
  967. if (!cfg->default_label)
  968. cfg->default_label = strdup(label->name);
  969. if (!cfg->default_label)
  970. return -ENOMEM;
  971. break;
  972. case T_LABEL:
  973. parse_sliteral(c, &label->menu);
  974. break;
  975. default:
  976. printf("Ignoring malformed menu command: %.*s\n",
  977. (int)(*c - s), s);
  978. }
  979. eol_or_eof(c);
  980. return 0;
  981. }
  982. /*
  983. * Parses a label and adds it to the list of labels for a menu.
  984. *
  985. * A label ends when we either get to the end of a file, or
  986. * get some input we otherwise don't have a handler defined
  987. * for.
  988. *
  989. */
  990. static int parse_label(char **c, struct pxe_menu *cfg)
  991. {
  992. struct token t;
  993. int len;
  994. char *s = *c;
  995. struct pxe_label *label;
  996. int err;
  997. label = label_create();
  998. if (!label)
  999. return -ENOMEM;
  1000. err = parse_sliteral(c, &label->name);
  1001. if (err < 0) {
  1002. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  1003. label_destroy(label);
  1004. return -EINVAL;
  1005. }
  1006. list_add_tail(&label->list, &cfg->labels);
  1007. while (1) {
  1008. s = *c;
  1009. get_token(c, &t, L_KEYWORD);
  1010. err = 0;
  1011. switch (t.type) {
  1012. case T_MENU:
  1013. err = parse_label_menu(c, cfg, label);
  1014. break;
  1015. case T_KERNEL:
  1016. case T_LINUX:
  1017. err = parse_sliteral(c, &label->kernel);
  1018. break;
  1019. case T_APPEND:
  1020. err = parse_sliteral(c, &label->append);
  1021. if (label->initrd)
  1022. break;
  1023. s = strstr(label->append, "initrd=");
  1024. if (!s)
  1025. break;
  1026. s += 7;
  1027. len = (int)(strchr(s, ' ') - s);
  1028. label->initrd = malloc(len + 1);
  1029. strncpy(label->initrd, s, len);
  1030. label->initrd[len] = '\0';
  1031. break;
  1032. case T_INITRD:
  1033. if (!label->initrd)
  1034. err = parse_sliteral(c, &label->initrd);
  1035. break;
  1036. case T_FDT:
  1037. if (!label->fdt)
  1038. err = parse_sliteral(c, &label->fdt);
  1039. break;
  1040. case T_FDTDIR:
  1041. if (!label->fdtdir)
  1042. err = parse_sliteral(c, &label->fdtdir);
  1043. break;
  1044. case T_LOCALBOOT:
  1045. label->localboot = 1;
  1046. err = parse_integer(c, &label->localboot_val);
  1047. break;
  1048. case T_IPAPPEND:
  1049. err = parse_integer(c, &label->ipappend);
  1050. break;
  1051. case T_EOL:
  1052. break;
  1053. default:
  1054. /*
  1055. * put the token back! we don't want it - it's the end
  1056. * of a label and whatever token this is, it's
  1057. * something for the menu level context to handle.
  1058. */
  1059. *c = s;
  1060. return 1;
  1061. }
  1062. if (err < 0)
  1063. return err;
  1064. }
  1065. }
  1066. /*
  1067. * This 16 comes from the limit pxelinux imposes on nested includes.
  1068. *
  1069. * There is no reason at all we couldn't do more, but some limit helps prevent
  1070. * infinite (until crash occurs) recursion if a file tries to include itself.
  1071. */
  1072. #define MAX_NEST_LEVEL 16
  1073. /*
  1074. * Entry point for parsing a menu file. nest_level indicates how many times
  1075. * we've nested in includes. It will be 1 for the top level menu file.
  1076. *
  1077. * Returns 1 on success, < 0 on error.
  1078. */
  1079. static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
  1080. struct pxe_menu *cfg, int nest_level)
  1081. {
  1082. struct token t;
  1083. char *s, *b, *label_name;
  1084. int err;
  1085. b = p;
  1086. if (nest_level > MAX_NEST_LEVEL) {
  1087. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  1088. return -EMLINK;
  1089. }
  1090. while (1) {
  1091. s = p;
  1092. get_token(&p, &t, L_KEYWORD);
  1093. err = 0;
  1094. switch (t.type) {
  1095. case T_MENU:
  1096. cfg->prompt = 1;
  1097. err = parse_menu(cmdtp, &p, cfg,
  1098. base + ALIGN(strlen(b) + 1, 4),
  1099. nest_level);
  1100. break;
  1101. case T_TIMEOUT:
  1102. err = parse_integer(&p, &cfg->timeout);
  1103. break;
  1104. case T_LABEL:
  1105. err = parse_label(&p, cfg);
  1106. break;
  1107. case T_DEFAULT:
  1108. case T_ONTIMEOUT:
  1109. err = parse_sliteral(&p, &label_name);
  1110. if (label_name) {
  1111. if (cfg->default_label)
  1112. free(cfg->default_label);
  1113. cfg->default_label = label_name;
  1114. }
  1115. break;
  1116. case T_INCLUDE:
  1117. err = handle_include(cmdtp, &p,
  1118. base + ALIGN(strlen(b), 4), cfg,
  1119. nest_level + 1);
  1120. break;
  1121. case T_PROMPT:
  1122. eol_or_eof(&p);
  1123. break;
  1124. case T_EOL:
  1125. break;
  1126. case T_EOF:
  1127. return 1;
  1128. default:
  1129. printf("Ignoring unknown command: %.*s\n",
  1130. (int)(p - s), s);
  1131. eol_or_eof(&p);
  1132. }
  1133. if (err < 0)
  1134. return err;
  1135. }
  1136. }
  1137. /*
  1138. * Free the memory used by a pxe_menu and its labels.
  1139. */
  1140. static void destroy_pxe_menu(struct pxe_menu *cfg)
  1141. {
  1142. struct list_head *pos, *n;
  1143. struct pxe_label *label;
  1144. if (cfg->title)
  1145. free(cfg->title);
  1146. if (cfg->default_label)
  1147. free(cfg->default_label);
  1148. list_for_each_safe(pos, n, &cfg->labels) {
  1149. label = list_entry(pos, struct pxe_label, list);
  1150. label_destroy(label);
  1151. }
  1152. free(cfg);
  1153. }
  1154. /*
  1155. * Entry point for parsing a pxe file. This is only used for the top level
  1156. * file.
  1157. *
  1158. * Returns NULL if there is an error, otherwise, returns a pointer to a
  1159. * pxe_menu struct populated with the results of parsing the pxe file (and any
  1160. * files it includes). The resulting pxe_menu struct can be free()'d by using
  1161. * the destroy_pxe_menu() function.
  1162. */
  1163. static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
  1164. {
  1165. struct pxe_menu *cfg;
  1166. char *buf;
  1167. int r;
  1168. cfg = malloc(sizeof(struct pxe_menu));
  1169. if (!cfg)
  1170. return NULL;
  1171. memset(cfg, 0, sizeof(struct pxe_menu));
  1172. INIT_LIST_HEAD(&cfg->labels);
  1173. buf = map_sysmem(menucfg, 0);
  1174. r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
  1175. unmap_sysmem(buf);
  1176. if (r < 0) {
  1177. destroy_pxe_menu(cfg);
  1178. return NULL;
  1179. }
  1180. return cfg;
  1181. }
  1182. /*
  1183. * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
  1184. * menu code.
  1185. */
  1186. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1187. {
  1188. struct pxe_label *label;
  1189. struct list_head *pos;
  1190. struct menu *m;
  1191. int err;
  1192. int i = 1;
  1193. char *default_num = NULL;
  1194. /*
  1195. * Create a menu and add items for all the labels.
  1196. */
  1197. m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
  1198. NULL, NULL);
  1199. if (!m)
  1200. return NULL;
  1201. list_for_each(pos, &cfg->labels) {
  1202. label = list_entry(pos, struct pxe_label, list);
  1203. sprintf(label->num, "%d", i++);
  1204. if (menu_item_add(m, label->num, label) != 1) {
  1205. menu_destroy(m);
  1206. return NULL;
  1207. }
  1208. if (cfg->default_label &&
  1209. (strcmp(label->name, cfg->default_label) == 0))
  1210. default_num = label->num;
  1211. }
  1212. /*
  1213. * After we've created items for each label in the menu, set the
  1214. * menu's default label if one was specified.
  1215. */
  1216. if (default_num) {
  1217. err = menu_default_set(m, default_num);
  1218. if (err != 1) {
  1219. if (err != -ENOENT) {
  1220. menu_destroy(m);
  1221. return NULL;
  1222. }
  1223. printf("Missing default: %s\n", cfg->default_label);
  1224. }
  1225. }
  1226. return m;
  1227. }
  1228. /*
  1229. * Try to boot any labels we have yet to attempt to boot.
  1230. */
  1231. static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
  1232. {
  1233. struct list_head *pos;
  1234. struct pxe_label *label;
  1235. list_for_each(pos, &cfg->labels) {
  1236. label = list_entry(pos, struct pxe_label, list);
  1237. if (!label->attempted)
  1238. label_boot(cmdtp, label);
  1239. }
  1240. }
  1241. /*
  1242. * Boot the system as prescribed by a pxe_menu.
  1243. *
  1244. * Use the menu system to either get the user's choice or the default, based
  1245. * on config or user input. If there is no default or user's choice,
  1246. * attempted to boot labels in the order they were given in pxe files.
  1247. * If the default or user's choice fails to boot, attempt to boot other
  1248. * labels in the order they were given in pxe files.
  1249. *
  1250. * If this function returns, there weren't any labels that successfully
  1251. * booted, or the user interrupted the menu selection via ctrl+c.
  1252. */
  1253. static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
  1254. {
  1255. void *choice;
  1256. struct menu *m;
  1257. int err;
  1258. m = pxe_menu_to_menu(cfg);
  1259. if (!m)
  1260. return;
  1261. err = menu_get_choice(m, &choice);
  1262. menu_destroy(m);
  1263. /*
  1264. * err == 1 means we got a choice back from menu_get_choice.
  1265. *
  1266. * err == -ENOENT if the menu was setup to select the default but no
  1267. * default was set. in that case, we should continue trying to boot
  1268. * labels that haven't been attempted yet.
  1269. *
  1270. * otherwise, the user interrupted or there was some other error and
  1271. * we give up.
  1272. */
  1273. if (err == 1) {
  1274. err = label_boot(cmdtp, choice);
  1275. if (!err)
  1276. return;
  1277. } else if (err != -ENOENT) {
  1278. return;
  1279. }
  1280. boot_unattempted_labels(cmdtp, cfg);
  1281. }
  1282. #ifdef CONFIG_CMD_NET
  1283. /*
  1284. * Boots a system using a pxe file
  1285. *
  1286. * Returns 0 on success, 1 on error.
  1287. */
  1288. static int
  1289. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1290. {
  1291. unsigned long pxefile_addr_r;
  1292. struct pxe_menu *cfg;
  1293. char *pxefile_addr_str;
  1294. do_getfile = do_get_tftp;
  1295. if (argc == 1) {
  1296. pxefile_addr_str = from_env("pxefile_addr_r");
  1297. if (!pxefile_addr_str)
  1298. return 1;
  1299. } else if (argc == 2) {
  1300. pxefile_addr_str = argv[1];
  1301. } else {
  1302. return CMD_RET_USAGE;
  1303. }
  1304. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1305. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1306. return 1;
  1307. }
  1308. cfg = parse_pxefile(cmdtp, pxefile_addr_r);
  1309. if (cfg == NULL) {
  1310. printf("Error parsing config file\n");
  1311. return 1;
  1312. }
  1313. handle_pxe_menu(cmdtp, cfg);
  1314. destroy_pxe_menu(cfg);
  1315. copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
  1316. return 0;
  1317. }
  1318. static cmd_tbl_t cmd_pxe_sub[] = {
  1319. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  1320. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  1321. };
  1322. static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1323. {
  1324. cmd_tbl_t *cp;
  1325. if (argc < 2)
  1326. return CMD_RET_USAGE;
  1327. is_pxe = true;
  1328. /* drop initial "pxe" arg */
  1329. argc--;
  1330. argv++;
  1331. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  1332. if (cp)
  1333. return cp->cmd(cmdtp, flag, argc, argv);
  1334. return CMD_RET_USAGE;
  1335. }
  1336. U_BOOT_CMD(
  1337. pxe, 3, 1, do_pxe,
  1338. "commands to get and boot from pxe files",
  1339. "get - try to retrieve a pxe file using tftp\npxe "
  1340. "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  1341. );
  1342. #endif
  1343. /*
  1344. * Boots a system using a local disk syslinux/extlinux file
  1345. *
  1346. * Returns 0 on success, 1 on error.
  1347. */
  1348. static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1349. {
  1350. unsigned long pxefile_addr_r;
  1351. struct pxe_menu *cfg;
  1352. char *pxefile_addr_str;
  1353. char *filename;
  1354. int prompt = 0;
  1355. is_pxe = false;
  1356. if (argc > 1 && strstr(argv[1], "-p")) {
  1357. prompt = 1;
  1358. argc--;
  1359. argv++;
  1360. }
  1361. if (argc < 4)
  1362. return cmd_usage(cmdtp);
  1363. if (argc < 5) {
  1364. pxefile_addr_str = from_env("pxefile_addr_r");
  1365. if (!pxefile_addr_str)
  1366. return 1;
  1367. } else {
  1368. pxefile_addr_str = argv[4];
  1369. }
  1370. if (argc < 6)
  1371. filename = getenv("bootfile");
  1372. else {
  1373. filename = argv[5];
  1374. setenv("bootfile", filename);
  1375. }
  1376. if (strstr(argv[3], "ext2"))
  1377. do_getfile = do_get_ext2;
  1378. else if (strstr(argv[3], "fat"))
  1379. do_getfile = do_get_fat;
  1380. else if (strstr(argv[3], "any"))
  1381. do_getfile = do_get_any;
  1382. else {
  1383. printf("Invalid filesystem: %s\n", argv[3]);
  1384. return 1;
  1385. }
  1386. fs_argv[1] = argv[1];
  1387. fs_argv[2] = argv[2];
  1388. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1389. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1390. return 1;
  1391. }
  1392. if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
  1393. printf("Error reading config file\n");
  1394. return 1;
  1395. }
  1396. cfg = parse_pxefile(cmdtp, pxefile_addr_r);
  1397. if (cfg == NULL) {
  1398. printf("Error parsing config file\n");
  1399. return 1;
  1400. }
  1401. if (prompt)
  1402. cfg->prompt = 1;
  1403. handle_pxe_menu(cmdtp, cfg);
  1404. destroy_pxe_menu(cfg);
  1405. return 0;
  1406. }
  1407. U_BOOT_CMD(
  1408. sysboot, 7, 1, do_sysboot,
  1409. "command to get and boot from syslinux files",
  1410. "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
  1411. " - load and parse syslinux menu file 'filename' from ext2, fat\n"
  1412. " or any filesystem on 'dev' on 'interface' to address 'addr'"
  1413. );