cmd_pxe.c 36 KB

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