cmd_pxe.c 36 KB

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