1
2
3
4
5
6
7#include <common.h>
8#include <command.h>
9#include <dm.h>
10#include <env.h>
11#include <image.h>
12#include <log.h>
13#include <malloc.h>
14#include <mapmem.h>
15#include <net.h>
16#include <fdt_support.h>
17#include <video.h>
18#include <linux/libfdt.h>
19#include <linux/string.h>
20#include <linux/ctype.h>
21#include <errno.h>
22#include <linux/list.h>
23
24#ifdef CONFIG_DM_RNG
25#include <rng.h>
26#endif
27
28#include <splash.h>
29#include <asm/io.h>
30
31#include "menu.h"
32#include "cli.h"
33
34#include "pxe_utils.h"
35
36#define MAX_TFTP_PATH_LEN 512
37
38int pxe_get_file_size(ulong *sizep)
39{
40 const char *val;
41
42 val = from_env("filesize");
43 if (!val)
44 return -ENOENT;
45
46 if (strict_strtoul(val, 16, sizep) < 0)
47 return -EINVAL;
48
49 return 0;
50}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67int format_mac_pxe(char *outbuf, size_t outbuf_len)
68{
69 uchar ethaddr[6];
70
71 if (outbuf_len < 21) {
72 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
73 return -ENOSPC;
74 }
75
76 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
77 return -ENOENT;
78
79 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
80 ethaddr[0], ethaddr[1], ethaddr[2],
81 ethaddr[3], ethaddr[4], ethaddr[5]);
82
83 return 1;
84}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100static int get_relfile(struct pxe_context *ctx, const char *file_path,
101 unsigned long file_addr, ulong *filesizep)
102{
103 size_t path_len;
104 char relfile[MAX_TFTP_PATH_LEN + 1];
105 char addr_buf[18];
106 ulong size;
107 int ret;
108
109 if (file_path[0] == '/' && ctx->allow_abs_path)
110 *relfile = '\0';
111 else
112 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
113
114 path_len = strlen(file_path) + strlen(relfile);
115
116 if (path_len > MAX_TFTP_PATH_LEN) {
117 printf("Base path too long (%s%s)\n", relfile, file_path);
118
119 return -ENAMETOOLONG;
120 }
121
122 strcat(relfile, file_path);
123
124 printf("Retrieving file: %s\n", relfile);
125
126 sprintf(addr_buf, "%lx", file_addr);
127
128 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
129 if (ret < 0)
130 return log_msg_ret("get", ret);
131 if (filesizep)
132 *filesizep = size;
133
134 return 1;
135}
136
137
138
139
140
141
142
143
144
145
146
147int get_pxe_file(struct pxe_context *ctx, const char *file_path,
148 ulong file_addr)
149{
150 ulong size;
151 int err;
152 char *buf;
153
154 err = get_relfile(ctx, file_path, file_addr, &size);
155 if (err < 0)
156 return err;
157
158 buf = map_sysmem(file_addr + size, 1);
159 *buf = '\0';
160 unmap_sysmem(buf);
161
162 return 1;
163}
164
165#define PXELINUX_DIR "pxelinux.cfg/"
166
167
168
169
170
171
172
173
174
175int get_pxelinux_path(struct pxe_context *ctx, const char *file,
176 unsigned long pxefile_addr_r)
177{
178 size_t base_len = strlen(PXELINUX_DIR);
179 char path[MAX_TFTP_PATH_LEN + 1];
180
181 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
182 printf("path (%s%s) too long, skipping\n",
183 PXELINUX_DIR, file);
184 return -ENAMETOOLONG;
185 }
186
187 sprintf(path, PXELINUX_DIR "%s", file);
188
189 return get_pxe_file(ctx, path, pxefile_addr_r);
190}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
209 const char *envaddr_name, ulong *filesizep)
210{
211 unsigned long file_addr;
212 char *envaddr;
213
214 envaddr = from_env(envaddr_name);
215 if (!envaddr)
216 return -ENOENT;
217
218 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
219 return -EINVAL;
220
221 return get_relfile(ctx, file_path, file_addr, filesizep);
222}
223
224
225
226
227
228
229
230
231
232static struct pxe_label *label_create(void)
233{
234 struct pxe_label *label;
235
236 label = malloc(sizeof(struct pxe_label));
237 if (!label)
238 return NULL;
239
240 memset(label, 0, sizeof(struct pxe_label));
241
242 return label;
243}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258static void label_destroy(struct pxe_label *label)
259{
260 free(label->name);
261 free(label->kernel_label);
262 free(label->kernel);
263 free(label->config);
264 free(label->append);
265 free(label->initrd);
266 free(label->fdt);
267 free(label->fdtdir);
268 free(label->fdtoverlays);
269 free(label);
270}
271
272
273
274
275
276
277
278
279
280static void label_print(void *data)
281{
282 struct pxe_label *label = data;
283 const char *c = label->menu ? label->menu : label->name;
284
285 printf("%s:\t%s\n", label->num, c);
286}
287
288
289
290
291
292
293
294
295
296
297
298
299static int label_localboot(struct pxe_label *label)
300{
301 char *localcmd;
302
303 localcmd = from_env("localcmd");
304 if (!localcmd)
305 return -ENOENT;
306
307 if (label->append) {
308 char bootargs[CONFIG_SYS_CBSIZE];
309
310 cli_simple_process_macros(label->append, bootargs,
311 sizeof(bootargs));
312 env_set("bootargs", bootargs);
313 }
314
315 debug("running: %s\n", localcmd);
316
317 return run_command_list(localcmd, strlen(localcmd), 0);
318}
319
320
321
322
323
324static void label_boot_kaslrseed(void)
325{
326#ifdef CONFIG_DM_RNG
327 ulong fdt_addr;
328 struct fdt_header *working_fdt;
329 size_t n = 0x8;
330 struct udevice *dev;
331 u64 *buf;
332 int nodeoffset;
333 int err;
334
335
336 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
337 working_fdt = map_sysmem(fdt_addr, 0);
338 err = fdt_check_header(working_fdt);
339 if (err)
340 return;
341
342
343
344 err = fdt_shrink_to_minimum(working_fdt, 512);
345 if (err <= 0)
346 return;
347
348 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
349 printf("No RNG device\n");
350 return;
351 }
352
353 nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
354 if (nodeoffset < 0) {
355 printf("Reading chosen node failed\n");
356 return;
357 }
358
359 buf = malloc(n);
360 if (!buf) {
361 printf("Out of memory\n");
362 return;
363 }
364
365 if (dm_rng_read(dev, buf, n)) {
366 printf("Reading RNG failed\n");
367 goto err;
368 }
369
370 err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
371 if (err < 0) {
372 printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err));
373 goto err;
374 }
375err:
376 free(buf);
377#endif
378 return;
379}
380
381
382
383
384
385
386
387
388#ifdef CONFIG_OF_LIBFDT_OVERLAY
389static void label_boot_fdtoverlay(struct pxe_context *ctx,
390 struct pxe_label *label)
391{
392 char *fdtoverlay = label->fdtoverlays;
393 struct fdt_header *working_fdt;
394 char *fdtoverlay_addr_env;
395 ulong fdtoverlay_addr;
396 ulong fdt_addr;
397 int err;
398
399
400 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
401 working_fdt = map_sysmem(fdt_addr, 0);
402 err = fdt_check_header(working_fdt);
403 if (err)
404 return;
405
406
407 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
408 if (!fdtoverlay_addr_env) {
409 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
410 return;
411 }
412
413 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
414
415
416 do {
417 struct fdt_header *blob;
418 char *overlayfile;
419 char *end;
420 int len;
421
422
423 while (*fdtoverlay == ' ')
424 ++fdtoverlay;
425
426
427 end = strstr(fdtoverlay, " ");
428 if (end) {
429 len = (int)(end - fdtoverlay);
430 overlayfile = malloc(len + 1);
431 strncpy(overlayfile, fdtoverlay, len);
432 overlayfile[len] = '\0';
433 } else
434 overlayfile = fdtoverlay;
435
436 if (!strlen(overlayfile))
437 goto skip_overlay;
438
439
440 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
441 NULL);
442 if (err < 0) {
443 printf("Failed loading overlay %s\n", overlayfile);
444 goto skip_overlay;
445 }
446
447
448 fdt_shrink_to_minimum(working_fdt, 8192);
449
450 blob = map_sysmem(fdtoverlay_addr, 0);
451 err = fdt_check_header(blob);
452 if (err) {
453 printf("Invalid overlay %s, skipping\n",
454 overlayfile);
455 goto skip_overlay;
456 }
457
458 err = fdt_overlay_apply_verbose(working_fdt, blob);
459 if (err) {
460 printf("Failed to apply overlay %s, skipping\n",
461 overlayfile);
462 goto skip_overlay;
463 }
464
465skip_overlay:
466 if (end)
467 free(overlayfile);
468 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
469}
470#endif
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
493{
494 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
495 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
496 char *kernel_addr = NULL;
497 char *initrd_addr_str = NULL;
498 char initrd_filesize[10];
499 char initrd_str[28];
500 char mac_str[29] = "";
501 char ip_str[68] = "";
502 char *fit_addr = NULL;
503 int bootm_argc = 2;
504 int zboot_argc = 3;
505 int len = 0;
506 ulong kernel_addr_r;
507 void *buf;
508
509 label_print(label);
510
511 label->attempted = 1;
512
513 if (label->localboot) {
514 if (label->localboot_val >= 0)
515 label_localboot(label);
516 return 0;
517 }
518
519 if (!label->kernel) {
520 printf("No kernel given, skipping %s\n",
521 label->name);
522 return 1;
523 }
524
525 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
526 NULL) < 0) {
527 printf("Skipping %s for failure retrieving kernel\n",
528 label->name);
529 return 1;
530 }
531
532 kernel_addr = env_get("kernel_addr_r");
533
534 if (label->config) {
535 int len = strlen(kernel_addr) + strlen(label->config) + 1;
536
537 fit_addr = malloc(len);
538 if (!fit_addr) {
539 printf("malloc fail (FIT address)\n");
540 return 1;
541 }
542 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
543 kernel_addr = fit_addr;
544 }
545
546
547 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
548 initrd_addr_str = kernel_addr;
549 } else if (label->initrd) {
550 ulong size;
551 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
552 &size) < 0) {
553 printf("Skipping %s for failure retrieving initrd\n",
554 label->name);
555 goto cleanup;
556 }
557
558 initrd_addr_str = env_get("ramdisk_addr_r");
559 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
560 initrd_addr_str, size);
561 if (size >= sizeof(initrd_str))
562 goto cleanup;
563 }
564
565 if (label->ipappend & 0x1) {
566 sprintf(ip_str, " ip=%s:%s:%s:%s",
567 env_get("ipaddr"), env_get("serverip"),
568 env_get("gatewayip"), env_get("netmask"));
569 }
570
571 if (IS_ENABLED(CONFIG_CMD_NET)) {
572 if (label->ipappend & 0x2) {
573 int err;
574
575 strcpy(mac_str, " BOOTIF=");
576 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
577 if (err < 0)
578 mac_str[0] = '\0';
579 }
580 }
581
582 if ((label->ipappend & 0x3) || label->append) {
583 char bootargs[CONFIG_SYS_CBSIZE] = "";
584 char finalbootargs[CONFIG_SYS_CBSIZE];
585
586 if (strlen(label->append ?: "") +
587 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
588 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
589 strlen(label->append ?: ""),
590 strlen(ip_str), strlen(mac_str),
591 sizeof(bootargs));
592 goto cleanup;
593 }
594
595 if (label->append)
596 strncpy(bootargs, label->append, sizeof(bootargs));
597
598 strcat(bootargs, ip_str);
599 strcat(bootargs, mac_str);
600
601 cli_simple_process_macros(bootargs, finalbootargs,
602 sizeof(finalbootargs));
603 env_set("bootargs", finalbootargs);
604 printf("append: %s\n", finalbootargs);
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 bootm_argv[3] = env_get("fdt_addr_r");
627
628
629 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
630 bootm_argv[3] = kernel_addr;
631
632 } else if (bootm_argv[3]) {
633 char *fdtfile = NULL;
634 char *fdtfilefree = NULL;
635
636 if (label->fdt) {
637 fdtfile = label->fdt;
638 } else if (label->fdtdir) {
639 char *f1, *f2, *f3, *f4, *slash;
640
641 f1 = env_get("fdtfile");
642 if (f1) {
643 f2 = "";
644 f3 = "";
645 f4 = "";
646 } else {
647
648
649
650
651
652
653
654 f1 = env_get("soc");
655 f2 = "-";
656 f3 = env_get("board");
657 f4 = ".dtb";
658 if (!f1) {
659 f1 = "";
660 f2 = "";
661 }
662 if (!f3) {
663 f2 = "";
664 f3 = "";
665 }
666 }
667
668 len = strlen(label->fdtdir);
669 if (!len)
670 slash = "./";
671 else if (label->fdtdir[len - 1] != '/')
672 slash = "/";
673 else
674 slash = "";
675
676 len = strlen(label->fdtdir) + strlen(slash) +
677 strlen(f1) + strlen(f2) + strlen(f3) +
678 strlen(f4) + 1;
679 fdtfilefree = malloc(len);
680 if (!fdtfilefree) {
681 printf("malloc fail (FDT filename)\n");
682 goto cleanup;
683 }
684
685 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
686 label->fdtdir, slash, f1, f2, f3, f4);
687 fdtfile = fdtfilefree;
688 }
689
690 if (fdtfile) {
691 int err = get_relfile_envaddr(ctx, fdtfile,
692 "fdt_addr_r", NULL);
693
694 free(fdtfilefree);
695 if (err < 0) {
696 bootm_argv[3] = NULL;
697
698 if (label->fdt) {
699 printf("Skipping %s for failure retrieving FDT\n",
700 label->name);
701 goto cleanup;
702 }
703 }
704
705 if (label->kaslrseed)
706 label_boot_kaslrseed();
707
708#ifdef CONFIG_OF_LIBFDT_OVERLAY
709 if (label->fdtoverlays)
710 label_boot_fdtoverlay(ctx, label);
711#endif
712 } else {
713 bootm_argv[3] = NULL;
714 }
715 }
716
717 bootm_argv[1] = kernel_addr;
718 zboot_argv[1] = kernel_addr;
719
720 if (initrd_addr_str) {
721 bootm_argv[2] = initrd_str;
722 bootm_argc = 3;
723
724 zboot_argv[3] = initrd_addr_str;
725 zboot_argv[4] = initrd_filesize;
726 zboot_argc = 5;
727 }
728
729 if (!bootm_argv[3])
730 bootm_argv[3] = env_get("fdt_addr");
731
732 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
733 buf = map_sysmem(kernel_addr_r, 0);
734
735 if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT)
736 bootm_argv[3] = env_get("fdtcontroladdr");
737
738 if (bootm_argv[3]) {
739 if (!bootm_argv[2])
740 bootm_argv[2] = "-";
741 bootm_argc = 4;
742 }
743
744
745 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
746 IS_ENABLED(CONFIG_CMD_BOOTM))
747 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
748
749 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
750 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
751
752 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
753 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
754
755 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
756 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
757
758 unmap_sysmem(buf);
759
760cleanup:
761 free(fit_addr);
762
763 return 1;
764}
765
766
767enum token_type {
768 T_EOL,
769 T_STRING,
770 T_EOF,
771 T_MENU,
772 T_TITLE,
773 T_TIMEOUT,
774 T_LABEL,
775 T_KERNEL,
776 T_LINUX,
777 T_APPEND,
778 T_INITRD,
779 T_LOCALBOOT,
780 T_DEFAULT,
781 T_PROMPT,
782 T_INCLUDE,
783 T_FDT,
784 T_FDTDIR,
785 T_FDTOVERLAYS,
786 T_ONTIMEOUT,
787 T_IPAPPEND,
788 T_BACKGROUND,
789 T_KASLRSEED,
790 T_INVALID
791};
792
793
794struct token {
795 char *val;
796 enum token_type type;
797};
798
799
800static const struct token keywords[] = {
801 {"menu", T_MENU},
802 {"title", T_TITLE},
803 {"timeout", T_TIMEOUT},
804 {"default", T_DEFAULT},
805 {"prompt", T_PROMPT},
806 {"label", T_LABEL},
807 {"kernel", T_KERNEL},
808 {"linux", T_LINUX},
809 {"localboot", T_LOCALBOOT},
810 {"append", T_APPEND},
811 {"initrd", T_INITRD},
812 {"include", T_INCLUDE},
813 {"devicetree", T_FDT},
814 {"fdt", T_FDT},
815 {"devicetreedir", T_FDTDIR},
816 {"fdtdir", T_FDTDIR},
817 {"fdtoverlays", T_FDTOVERLAYS},
818 {"devicetree-overlay", T_FDTOVERLAYS},
819 {"ontimeout", T_ONTIMEOUT,},
820 {"ipappend", T_IPAPPEND,},
821 {"background", T_BACKGROUND,},
822 {"kaslrseed", T_KASLRSEED,},
823 {NULL, T_INVALID}
824};
825
826
827
828
829
830
831
832
833enum lex_state {
834 L_NORMAL = 0,
835 L_KEYWORD,
836 L_SLITERAL
837};
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865static char *get_string(char **p, struct token *t, char delim, int lower)
866{
867 char *b, *e;
868 size_t len, i;
869
870
871
872
873
874
875
876 b = *p;
877 e = *p;
878 while (*e) {
879 if ((delim == ' ' && isspace(*e)) || delim == *e)
880 break;
881 e++;
882 }
883
884 len = e - b;
885
886
887
888
889
890 t->val = malloc(len + 1);
891 if (!t->val)
892 return NULL;
893
894 for (i = 0; i < len; i++, b++) {
895 if (lower)
896 t->val[i] = tolower(*b);
897 else
898 t->val[i] = *b;
899 }
900
901 t->val[len] = '\0';
902
903
904 *p = e;
905 t->type = T_STRING;
906
907 return t->val;
908}
909
910
911
912
913
914
915
916static void get_keyword(struct token *t)
917{
918 int i;
919
920 for (i = 0; keywords[i].val; i++) {
921 if (!strcmp(t->val, keywords[i].val)) {
922 t->type = keywords[i].type;
923 break;
924 }
925 }
926}
927
928
929
930
931
932
933
934
935
936
937static void get_token(char **p, struct token *t, enum lex_state state)
938{
939 char *c = *p;
940
941 t->type = T_INVALID;
942
943
944 while (isblank(*c))
945 c++;
946
947
948
949
950
951 if (*c == '#') {
952 while (*c && *c != '\n')
953 c++;
954 }
955
956 if (*c == '\n') {
957 t->type = T_EOL;
958 c++;
959 } else if (*c == '\0') {
960 t->type = T_EOF;
961 c++;
962 } else if (state == L_SLITERAL) {
963 get_string(&c, t, '\n', 0);
964 } else if (state == L_KEYWORD) {
965
966
967
968
969
970
971
972 get_string(&c, t, ' ', 1);
973 get_keyword(t);
974 }
975
976 *p = c;
977}
978
979
980
981
982
983
984
985
986
987static void eol_or_eof(char **c)
988{
989 while (**c && **c != '\n')
990 (*c)++;
991}
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004static int parse_sliteral(char **c, char **dst)
1005{
1006 struct token t;
1007 char *s = *c;
1008
1009 get_token(c, &t, L_SLITERAL);
1010
1011 if (t.type != T_STRING) {
1012 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1013 return -EINVAL;
1014 }
1015
1016 *dst = t.val;
1017
1018 return 1;
1019}
1020
1021
1022
1023
1024static int parse_integer(char **c, int *dst)
1025{
1026 struct token t;
1027 char *s = *c;
1028
1029 get_token(c, &t, L_SLITERAL);
1030 if (t.type != T_STRING) {
1031 printf("Expected string: %.*s\n", (int)(*c - s), s);
1032 return -EINVAL;
1033 }
1034
1035 *dst = simple_strtol(t.val, NULL, 10);
1036
1037 free(t.val);
1038
1039 return 1;
1040}
1041
1042static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
1043 struct pxe_menu *cfg, int nest_level);
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
1054 struct pxe_menu *cfg, int nest_level)
1055{
1056 char *include_path;
1057 char *s = *c;
1058 int err;
1059 char *buf;
1060 int ret;
1061
1062 err = parse_sliteral(c, &include_path);
1063 if (err < 0) {
1064 printf("Expected include path: %.*s\n", (int)(*c - s), s);
1065 return err;
1066 }
1067
1068 err = get_pxe_file(ctx, include_path, base);
1069 if (err < 0) {
1070 printf("Couldn't retrieve %s\n", include_path);
1071 return err;
1072 }
1073
1074 buf = map_sysmem(base, 0);
1075 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
1076 unmap_sysmem(buf);
1077
1078 return ret;
1079}
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
1092 unsigned long base, int nest_level)
1093{
1094 struct token t;
1095 char *s = *c;
1096 int err = 0;
1097
1098 get_token(c, &t, L_KEYWORD);
1099
1100 switch (t.type) {
1101 case T_TITLE:
1102 err = parse_sliteral(c, &cfg->title);
1103
1104 break;
1105
1106 case T_INCLUDE:
1107 err = handle_include(ctx, c, base, cfg, nest_level + 1);
1108 break;
1109
1110 case T_BACKGROUND:
1111 err = parse_sliteral(c, &cfg->bmp);
1112 break;
1113
1114 default:
1115 printf("Ignoring malformed menu command: %.*s\n",
1116 (int)(*c - s), s);
1117 }
1118 if (err < 0)
1119 return err;
1120
1121 eol_or_eof(c);
1122
1123 return 1;
1124}
1125
1126
1127
1128
1129static int parse_label_menu(char **c, struct pxe_menu *cfg,
1130 struct pxe_label *label)
1131{
1132 struct token t;
1133 char *s;
1134
1135 s = *c;
1136
1137 get_token(c, &t, L_KEYWORD);
1138
1139 switch (t.type) {
1140 case T_DEFAULT:
1141 if (!cfg->default_label)
1142 cfg->default_label = strdup(label->name);
1143
1144 if (!cfg->default_label)
1145 return -ENOMEM;
1146
1147 break;
1148 case T_LABEL:
1149 parse_sliteral(c, &label->menu);
1150 break;
1151 default:
1152 printf("Ignoring malformed menu command: %.*s\n",
1153 (int)(*c - s), s);
1154 }
1155
1156 eol_or_eof(c);
1157
1158 return 0;
1159}
1160
1161
1162
1163
1164
1165static int parse_label_kernel(char **c, struct pxe_label *label)
1166{
1167 char *s;
1168 int err;
1169
1170 err = parse_sliteral(c, &label->kernel);
1171 if (err < 0)
1172 return err;
1173
1174
1175 label->kernel_label = strdup(label->kernel);
1176 if (!label->kernel_label)
1177 return -ENOMEM;
1178
1179 s = strstr(label->kernel, "#");
1180 if (!s)
1181 return 1;
1182
1183 label->config = strdup(s);
1184 if (!label->config)
1185 return -ENOMEM;
1186
1187 *s = 0;
1188
1189 return 1;
1190}
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200static int parse_label(char **c, struct pxe_menu *cfg)
1201{
1202 struct token t;
1203 int len;
1204 char *s = *c;
1205 struct pxe_label *label;
1206 int err;
1207
1208 label = label_create();
1209 if (!label)
1210 return -ENOMEM;
1211
1212 err = parse_sliteral(c, &label->name);
1213 if (err < 0) {
1214 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1215 label_destroy(label);
1216 return -EINVAL;
1217 }
1218
1219 list_add_tail(&label->list, &cfg->labels);
1220
1221 while (1) {
1222 s = *c;
1223 get_token(c, &t, L_KEYWORD);
1224
1225 err = 0;
1226 switch (t.type) {
1227 case T_MENU:
1228 err = parse_label_menu(c, cfg, label);
1229 break;
1230
1231 case T_KERNEL:
1232 case T_LINUX:
1233 err = parse_label_kernel(c, label);
1234 break;
1235
1236 case T_APPEND:
1237 err = parse_sliteral(c, &label->append);
1238 if (label->initrd)
1239 break;
1240 s = strstr(label->append, "initrd=");
1241 if (!s)
1242 break;
1243 s += 7;
1244 len = (int)(strchr(s, ' ') - s);
1245 label->initrd = malloc(len + 1);
1246 strncpy(label->initrd, s, len);
1247 label->initrd[len] = '\0';
1248
1249 break;
1250
1251 case T_INITRD:
1252 if (!label->initrd)
1253 err = parse_sliteral(c, &label->initrd);
1254 break;
1255
1256 case T_FDT:
1257 if (!label->fdt)
1258 err = parse_sliteral(c, &label->fdt);
1259 break;
1260
1261 case T_FDTDIR:
1262 if (!label->fdtdir)
1263 err = parse_sliteral(c, &label->fdtdir);
1264 break;
1265
1266 case T_FDTOVERLAYS:
1267 if (!label->fdtoverlays)
1268 err = parse_sliteral(c, &label->fdtoverlays);
1269 break;
1270
1271 case T_LOCALBOOT:
1272 label->localboot = 1;
1273 err = parse_integer(c, &label->localboot_val);
1274 break;
1275
1276 case T_IPAPPEND:
1277 err = parse_integer(c, &label->ipappend);
1278 break;
1279
1280 case T_KASLRSEED:
1281 label->kaslrseed = 1;
1282 break;
1283
1284 case T_EOL:
1285 break;
1286
1287 default:
1288
1289
1290
1291
1292
1293 *c = s;
1294 return 1;
1295 }
1296
1297 if (err < 0)
1298 return err;
1299 }
1300}
1301
1302
1303
1304
1305
1306
1307
1308#define MAX_NEST_LEVEL 16
1309
1310
1311
1312
1313
1314
1315
1316static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
1317 struct pxe_menu *cfg, int nest_level)
1318{
1319 struct token t;
1320 char *s, *b, *label_name;
1321 int err;
1322
1323 b = p;
1324
1325 if (nest_level > MAX_NEST_LEVEL) {
1326 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1327 return -EMLINK;
1328 }
1329
1330 while (1) {
1331 s = p;
1332
1333 get_token(&p, &t, L_KEYWORD);
1334
1335 err = 0;
1336 switch (t.type) {
1337 case T_MENU:
1338 cfg->prompt = 1;
1339 err = parse_menu(ctx, &p, cfg,
1340 base + ALIGN(strlen(b) + 1, 4),
1341 nest_level);
1342 break;
1343
1344 case T_TIMEOUT:
1345 err = parse_integer(&p, &cfg->timeout);
1346 break;
1347
1348 case T_LABEL:
1349 err = parse_label(&p, cfg);
1350 break;
1351
1352 case T_DEFAULT:
1353 case T_ONTIMEOUT:
1354 err = parse_sliteral(&p, &label_name);
1355
1356 if (label_name) {
1357 if (cfg->default_label)
1358 free(cfg->default_label);
1359
1360 cfg->default_label = label_name;
1361 }
1362
1363 break;
1364
1365 case T_INCLUDE:
1366 err = handle_include(ctx, &p,
1367 base + ALIGN(strlen(b), 4), cfg,
1368 nest_level + 1);
1369 break;
1370
1371 case T_PROMPT:
1372 err = parse_integer(&p, &cfg->prompt);
1373
1374 if (err < 0)
1375 eol_or_eof(&p);
1376 break;
1377
1378 case T_EOL:
1379 break;
1380
1381 case T_EOF:
1382 return 1;
1383
1384 default:
1385 printf("Ignoring unknown command: %.*s\n",
1386 (int)(p - s), s);
1387 eol_or_eof(&p);
1388 }
1389
1390 if (err < 0)
1391 return err;
1392 }
1393}
1394
1395
1396
1397void destroy_pxe_menu(struct pxe_menu *cfg)
1398{
1399 struct list_head *pos, *n;
1400 struct pxe_label *label;
1401
1402 free(cfg->title);
1403 free(cfg->default_label);
1404
1405 list_for_each_safe(pos, n, &cfg->labels) {
1406 label = list_entry(pos, struct pxe_label, list);
1407
1408 label_destroy(label);
1409 }
1410
1411 free(cfg);
1412}
1413
1414struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
1415{
1416 struct pxe_menu *cfg;
1417 char *buf;
1418 int r;
1419
1420 cfg = malloc(sizeof(struct pxe_menu));
1421 if (!cfg)
1422 return NULL;
1423
1424 memset(cfg, 0, sizeof(struct pxe_menu));
1425
1426 INIT_LIST_HEAD(&cfg->labels);
1427
1428 buf = map_sysmem(menucfg, 0);
1429 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
1430 unmap_sysmem(buf);
1431 if (r < 0) {
1432 destroy_pxe_menu(cfg);
1433 return NULL;
1434 }
1435
1436 return cfg;
1437}
1438
1439
1440
1441
1442
1443static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1444{
1445 struct pxe_label *label;
1446 struct list_head *pos;
1447 struct menu *m;
1448 char *label_override;
1449 int err;
1450 int i = 1;
1451 char *default_num = NULL;
1452 char *override_num = NULL;
1453
1454
1455
1456
1457 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1458 cfg->prompt, NULL, label_print, NULL, NULL);
1459 if (!m)
1460 return NULL;
1461
1462 label_override = env_get("pxe_label_override");
1463
1464 list_for_each(pos, &cfg->labels) {
1465 label = list_entry(pos, struct pxe_label, list);
1466
1467 sprintf(label->num, "%d", i++);
1468 if (menu_item_add(m, label->num, label) != 1) {
1469 menu_destroy(m);
1470 return NULL;
1471 }
1472 if (cfg->default_label &&
1473 (strcmp(label->name, cfg->default_label) == 0))
1474 default_num = label->num;
1475 if (label_override && !strcmp(label->name, label_override))
1476 override_num = label->num;
1477 }
1478
1479
1480 if (label_override) {
1481 if (override_num)
1482 default_num = override_num;
1483 else
1484 printf("Missing override pxe label: %s\n",
1485 label_override);
1486 }
1487
1488
1489
1490
1491
1492 if (default_num) {
1493 err = menu_default_set(m, default_num);
1494 if (err != 1) {
1495 if (err != -ENOENT) {
1496 menu_destroy(m);
1497 return NULL;
1498 }
1499
1500 printf("Missing default: %s\n", cfg->default_label);
1501 }
1502 }
1503
1504 return m;
1505}
1506
1507
1508
1509
1510static void boot_unattempted_labels(struct pxe_context *ctx,
1511 struct pxe_menu *cfg)
1512{
1513 struct list_head *pos;
1514 struct pxe_label *label;
1515
1516 list_for_each(pos, &cfg->labels) {
1517 label = list_entry(pos, struct pxe_label, list);
1518
1519 if (!label->attempted)
1520 label_boot(ctx, label);
1521 }
1522}
1523
1524void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
1525{
1526 void *choice;
1527 struct menu *m;
1528 int err;
1529
1530 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1531
1532 if (cfg->bmp) {
1533 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
1534#if defined(CONFIG_VIDEO)
1535 struct udevice *dev;
1536
1537 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1538 if (!err)
1539 video_clear(dev);
1540#endif
1541 bmp_display(image_load_addr,
1542 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1543 } else {
1544 printf("Skipping background bmp %s for failure\n",
1545 cfg->bmp);
1546 }
1547 }
1548 }
1549
1550 m = pxe_menu_to_menu(cfg);
1551 if (!m)
1552 return;
1553
1554 err = menu_get_choice(m, &choice);
1555 menu_destroy(m);
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 if (err == 1) {
1569 err = label_boot(ctx, choice);
1570 if (!err)
1571 return;
1572 } else if (err != -ENOENT) {
1573 return;
1574 }
1575
1576 boot_unattempted_labels(ctx, cfg);
1577}
1578
1579int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1580 pxe_getfile_func getfile, void *userdata,
1581 bool allow_abs_path, const char *bootfile, bool use_ipv6)
1582{
1583 const char *last_slash;
1584 size_t path_len = 0;
1585
1586 memset(ctx, '\0', sizeof(*ctx));
1587 ctx->cmdtp = cmdtp;
1588 ctx->getfile = getfile;
1589 ctx->userdata = userdata;
1590 ctx->allow_abs_path = allow_abs_path;
1591 ctx->use_ipv6 = use_ipv6;
1592
1593
1594 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1595 return -ENOSPC;
1596 ctx->bootdir = strdup(bootfile ? bootfile : "");
1597 if (!ctx->bootdir)
1598 return -ENOMEM;
1599
1600 if (bootfile) {
1601 last_slash = strrchr(bootfile, '/');
1602 if (last_slash)
1603 path_len = (last_slash - bootfile) + 1;
1604 }
1605 ctx->bootdir[path_len] = '\0';
1606
1607 return 0;
1608}
1609
1610void pxe_destroy_ctx(struct pxe_context *ctx)
1611{
1612 free(ctx->bootdir);
1613}
1614
1615int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1616{
1617 struct pxe_menu *cfg;
1618
1619 cfg = parse_pxefile(ctx, pxefile_addr_r);
1620 if (!cfg) {
1621 printf("Error parsing config file\n");
1622 return 1;
1623 }
1624
1625 if (prompt)
1626 cfg->prompt = 1;
1627
1628 handle_pxe_menu(ctx, cfg);
1629
1630 destroy_pxe_menu(cfg);
1631
1632 return 0;
1633}
1634