cmd_pxe.c 37 KB

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