cmd_pxe.c 33 KB

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