cmd_pxe.c 36 KB

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