-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpxe_utils.c
1633 lines (1375 loc) · 36 KB
/
pxe_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2010-2011 Calxeda, Inc.
* Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
*/
#include <common.h>
#include <command.h>
#include <dm.h>
#include <env.h>
#include <image.h>
#include <log.h>
#include <malloc.h>
#include <mapmem.h>
#include <net.h>
#include <fdt_support.h>
#include <video.h>
#include <linux/libfdt.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <errno.h>
#include <linux/list.h>
#ifdef CONFIG_DM_RNG
#include <rng.h>
#endif
#include <splash.h>
#include <asm/io.h>
#include "menu.h"
#include "cli.h"
#include "pxe_utils.h"
#define MAX_TFTP_PATH_LEN 512
int pxe_get_file_size(ulong *sizep)
{
const char *val;
val = from_env("filesize");
if (!val)
return -ENOENT;
if (strict_strtoul(val, 16, sizep) < 0)
return -EINVAL;
return 0;
}
/**
* format_mac_pxe() - obtain a MAC address in the PXE format
*
* This produces a MAC-address string in the format for the current ethernet
* device:
*
* 01-aa-bb-cc-dd-ee-ff
*
* where aa-ff is the MAC address in hex
*
* @outbuf: Buffer to write string to
* @outbuf_len: length of buffer
* Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
* current ethernet device
*/
int format_mac_pxe(char *outbuf, size_t outbuf_len)
{
uchar ethaddr[6];
if (outbuf_len < 21) {
printf("outbuf is too small (%zd < 21)\n", outbuf_len);
return -ENOSPC;
}
if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
return -ENOENT;
sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
ethaddr[0], ethaddr[1], ethaddr[2],
ethaddr[3], ethaddr[4], ethaddr[5]);
return 1;
}
/**
* get_relfile() - read a file relative to the PXE file
*
* As in pxelinux, paths to files referenced from files we retrieve are
* relative to the location of bootfile. get_relfile takes such a path and
* joins it with the bootfile path to get the full path to the target file. If
* the bootfile path is NULL, we use file_path as is.
*
* @ctx: PXE context
* @file_path: File path to read (relative to the PXE file)
* @file_addr: Address to load file to
* @filesizep: If not NULL, returns the file size in bytes
* Returns 1 for success, or < 0 on error
*/
static int get_relfile(struct pxe_context *ctx, const char *file_path,
unsigned long file_addr, ulong *filesizep)
{
size_t path_len;
char relfile[MAX_TFTP_PATH_LEN + 1];
char addr_buf[18];
ulong size;
int ret;
if (file_path[0] == '/' && ctx->allow_abs_path)
*relfile = '\0';
else
strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
path_len = strlen(file_path) + strlen(relfile);
if (path_len > MAX_TFTP_PATH_LEN) {
printf("Base path too long (%s%s)\n", relfile, file_path);
return -ENAMETOOLONG;
}
strcat(relfile, file_path);
printf("Retrieving file: %s\n", relfile);
sprintf(addr_buf, "%lx", file_addr);
ret = ctx->getfile(ctx, relfile, addr_buf, &size);
if (ret < 0)
return log_msg_ret("get", ret);
if (filesizep)
*filesizep = size;
return 1;
}
/**
* get_pxe_file() - read a file
*
* The file is read and nul-terminated
*
* @ctx: PXE context
* @file_path: File path to read (relative to the PXE file)
* @file_addr: Address to load file to
* Returns 1 for success, or < 0 on error
*/
int get_pxe_file(struct pxe_context *ctx, const char *file_path,
ulong file_addr)
{
ulong size;
int err;
char *buf;
err = get_relfile(ctx, file_path, file_addr, &size);
if (err < 0)
return err;
buf = map_sysmem(file_addr + size, 1);
*buf = '\0';
unmap_sysmem(buf);
return 1;
}
#define PXELINUX_DIR "pxelinux.cfg/"
/**
* get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
*
* @ctx: PXE context
* @file: Filename to process (relative to pxelinux.cfg/)
* Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
* or other value < 0 on other error
*/
int get_pxelinux_path(struct pxe_context *ctx, const char *file,
unsigned long pxefile_addr_r)
{
size_t base_len = strlen(PXELINUX_DIR);
char path[MAX_TFTP_PATH_LEN + 1];
if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
printf("path (%s%s) too long, skipping\n",
PXELINUX_DIR, file);
return -ENAMETOOLONG;
}
sprintf(path, PXELINUX_DIR "%s", file);
return get_pxe_file(ctx, path, pxefile_addr_r);
}
/**
* get_relfile_envaddr() - read a file to an address in an env var
*
* Wrapper to make it easier to store the file at file_path in the location
* specified by envaddr_name. file_path will be joined to the bootfile path,
* if any is specified.
*
* @ctx: PXE context
* @file_path: File path to read (relative to the PXE file)
* @envaddr_name: Name of environment variable which contains the address to
* load to
* @filesizep: Returns the file size in bytes
* Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
* environment variable, -EINVAL if its format is not valid hex, or other
* value < 0 on other error
*/
static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
const char *envaddr_name, ulong *filesizep)
{
unsigned long file_addr;
char *envaddr;
envaddr = from_env(envaddr_name);
if (!envaddr)
return -ENOENT;
if (strict_strtoul(envaddr, 16, &file_addr) < 0)
return -EINVAL;
return get_relfile(ctx, file_path, file_addr, filesizep);
}
/**
* label_create() - crate a new PXE label
*
* Allocates memory for and initializes a pxe_label. This uses malloc, so the
* result must be free()'d to reclaim the memory.
*
* Returns a pointer to the label, or NULL if out of memory
*/
static struct pxe_label *label_create(void)
{
struct pxe_label *label;
label = malloc(sizeof(struct pxe_label));
if (!label)
return NULL;
memset(label, 0, sizeof(struct pxe_label));
return label;
}
/**
* label_destroy() - free the memory used by a pxe_label
*
* This frees @label itself as well as memory used by its name,
* kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
* they're non-NULL.
*
* So - be sure to only use dynamically allocated memory for the members of
* the pxe_label struct, unless you want to clean it up first. These are
* currently only created by the pxe file parsing code.
*
* @label: Label to free
*/
static void label_destroy(struct pxe_label *label)
{
free(label->name);
free(label->kernel_label);
free(label->kernel);
free(label->config);
free(label->append);
free(label->initrd);
free(label->fdt);
free(label->fdtdir);
free(label->fdtoverlays);
free(label);
}
/**
* label_print() - Print a label and its string members if they're defined
*
* This is passed as a callback to the menu code for displaying each
* menu entry.
*
* @data: Label to print (is cast to struct pxe_label *)
*/
static void label_print(void *data)
{
struct pxe_label *label = data;
const char *c = label->menu ? label->menu : label->name;
printf("%s:\t%s\n", label->num, c);
}
/**
* label_localboot() - Boot a label that specified 'localboot'
*
* This requires that the 'localcmd' environment variable is defined. Its
* contents will be executed as U-Boot commands. If the label specified an
* 'append' line, its contents will be used to overwrite the contents of the
* 'bootargs' environment variable prior to running 'localcmd'.
*
* @label: Label to process
* Returns 1 on success or < 0 on error
*/
static int label_localboot(struct pxe_label *label)
{
char *localcmd;
localcmd = from_env("localcmd");
if (!localcmd)
return -ENOENT;
if (label->append) {
char bootargs[CONFIG_SYS_CBSIZE];
cli_simple_process_macros(label->append, bootargs,
sizeof(bootargs));
env_set("bootargs", bootargs);
}
debug("running: %s\n", localcmd);
return run_command_list(localcmd, strlen(localcmd), 0);
}
/*
* label_boot_kaslrseed generate kaslrseed from hw rng
*/
static void label_boot_kaslrseed(void)
{
#ifdef CONFIG_DM_RNG
ulong fdt_addr;
struct fdt_header *working_fdt;
size_t n = 0x8;
struct udevice *dev;
u64 *buf;
int nodeoffset;
int err;
/* Get the main fdt and map it */
fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
working_fdt = map_sysmem(fdt_addr, 0);
err = fdt_check_header(working_fdt);
if (err)
return;
/* add extra size for holding kaslr-seed */
/* err is new fdt size, 0 or negtive */
err = fdt_shrink_to_minimum(working_fdt, 512);
if (err <= 0)
return;
if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
printf("No RNG device\n");
return;
}
nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
if (nodeoffset < 0) {
printf("Reading chosen node failed\n");
return;
}
buf = malloc(n);
if (!buf) {
printf("Out of memory\n");
return;
}
if (dm_rng_read(dev, buf, n)) {
printf("Reading RNG failed\n");
goto err;
}
err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
if (err < 0) {
printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err));
goto err;
}
err:
free(buf);
#endif
return;
}
/**
* label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
* or 'devicetree-overlay'
*
* @ctx: PXE context
* @label: Label to process
*/
#ifdef CONFIG_OF_LIBFDT_OVERLAY
static void label_boot_fdtoverlay(struct pxe_context *ctx,
struct pxe_label *label)
{
char *fdtoverlay = label->fdtoverlays;
struct fdt_header *working_fdt;
char *fdtoverlay_addr_env;
ulong fdtoverlay_addr;
ulong fdt_addr;
int err;
/* Get the main fdt and map it */
fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
working_fdt = map_sysmem(fdt_addr, 0);
err = fdt_check_header(working_fdt);
if (err)
return;
/* Get the specific overlay loading address */
fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
if (!fdtoverlay_addr_env) {
printf("Invalid fdtoverlay_addr_r for loading overlays\n");
return;
}
fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
/* Cycle over the overlay files and apply them in order */
do {
struct fdt_header *blob;
char *overlayfile;
char *end;
int len;
/* Drop leading spaces */
while (*fdtoverlay == ' ')
++fdtoverlay;
/* Copy a single filename if multiple provided */
end = strstr(fdtoverlay, " ");
if (end) {
len = (int)(end - fdtoverlay);
overlayfile = malloc(len + 1);
strncpy(overlayfile, fdtoverlay, len);
overlayfile[len] = '\0';
} else
overlayfile = fdtoverlay;
if (!strlen(overlayfile))
goto skip_overlay;
/* Load overlay file */
err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
NULL);
if (err < 0) {
printf("Failed loading overlay %s\n", overlayfile);
goto skip_overlay;
}
/* Resize main fdt */
fdt_shrink_to_minimum(working_fdt, 8192);
blob = map_sysmem(fdtoverlay_addr, 0);
err = fdt_check_header(blob);
if (err) {
printf("Invalid overlay %s, skipping\n",
overlayfile);
goto skip_overlay;
}
err = fdt_overlay_apply_verbose(working_fdt, blob);
if (err) {
printf("Failed to apply overlay %s, skipping\n",
overlayfile);
goto skip_overlay;
}
skip_overlay:
if (end)
free(overlayfile);
} while ((fdtoverlay = strstr(fdtoverlay, " ")));
}
#endif
/**
* label_boot() - Boot according to the contents of a pxe_label
*
* If we can't boot for any reason, we return. A successful boot never
* returns.
*
* The kernel will be stored in the location given by the 'kernel_addr_r'
* environment variable.
*
* If the label specifies an initrd file, it will be stored in the location
* given by the 'ramdisk_addr_r' environment variable.
*
* If the label specifies an 'append' line, its contents will overwrite that
* of the 'bootargs' environment variable.
*
* @ctx: PXE context
* @label: Label to process
* Returns does not return on success, otherwise returns 0 if a localboot
* label was processed, or 1 on error
*/
static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
{
char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
char *kernel_addr = NULL;
char *initrd_addr_str = NULL;
char initrd_filesize[10];
char initrd_str[28];
char mac_str[29] = "";
char ip_str[68] = "";
char *fit_addr = NULL;
int bootm_argc = 2;
int zboot_argc = 3;
int len = 0;
ulong kernel_addr_r;
void *buf;
label_print(label);
label->attempted = 1;
if (label->localboot) {
if (label->localboot_val >= 0)
label_localboot(label);
return 0;
}
if (!label->kernel) {
printf("No kernel given, skipping %s\n",
label->name);
return 1;
}
if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
NULL) < 0) {
printf("Skipping %s for failure retrieving kernel\n",
label->name);
return 1;
}
kernel_addr = env_get("kernel_addr_r");
/* for FIT, append the configuration identifier */
if (label->config) {
int len = strlen(kernel_addr) + strlen(label->config) + 1;
fit_addr = malloc(len);
if (!fit_addr) {
printf("malloc fail (FIT address)\n");
return 1;
}
snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
kernel_addr = fit_addr;
}
/* For FIT, the label can be identical to kernel one */
if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
initrd_addr_str = kernel_addr;
} else if (label->initrd) {
ulong size;
if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
&size) < 0) {
printf("Skipping %s for failure retrieving initrd\n",
label->name);
goto cleanup;
}
initrd_addr_str = env_get("ramdisk_addr_r");
size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
initrd_addr_str, size);
if (size >= sizeof(initrd_str))
goto cleanup;
}
if (label->ipappend & 0x1) {
sprintf(ip_str, " ip=%s:%s:%s:%s",
env_get("ipaddr"), env_get("serverip"),
env_get("gatewayip"), env_get("netmask"));
}
if (IS_ENABLED(CONFIG_CMD_NET)) {
if (label->ipappend & 0x2) {
int err;
strcpy(mac_str, " BOOTIF=");
err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
if (err < 0)
mac_str[0] = '\0';
}
}
if ((label->ipappend & 0x3) || label->append) {
char bootargs[CONFIG_SYS_CBSIZE] = "";
char finalbootargs[CONFIG_SYS_CBSIZE];
if (strlen(label->append ?: "") +
strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
strlen(label->append ?: ""),
strlen(ip_str), strlen(mac_str),
sizeof(bootargs));
goto cleanup;
}
if (label->append)
strncpy(bootargs, label->append, sizeof(bootargs));
strcat(bootargs, ip_str);
strcat(bootargs, mac_str);
cli_simple_process_macros(bootargs, finalbootargs,
sizeof(finalbootargs));
env_set("bootargs", finalbootargs);
printf("append: %s\n", finalbootargs);
}
/*
* fdt usage is optional:
* It handles the following scenarios.
*
* Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
* defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
* bootm, and adjust argc appropriately.
*
* If retrieve fails and no exact fdt blob is specified in pxe file with
* "fdt" label, try Scenario 2.
*
* Scenario 2: If there is an fdt_addr specified, pass it along to
* bootm, and adjust argc appropriately.
*
* Scenario 3: If there is an fdtcontroladdr specified, pass it along to
* bootm, and adjust argc appropriately, unless the image type is fitImage.
*
* Scenario 4: fdt blob is not available.
*/
bootm_argv[3] = env_get("fdt_addr_r");
/* For FIT, the label can be identical to kernel one */
if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
bootm_argv[3] = kernel_addr;
/* if fdt label is defined then get fdt from server */
} else if (bootm_argv[3]) {
char *fdtfile = NULL;
char *fdtfilefree = NULL;
if (label->fdt) {
fdtfile = label->fdt;
} else if (label->fdtdir) {
char *f1, *f2, *f3, *f4, *slash;
f1 = env_get("fdtfile");
if (f1) {
f2 = "";
f3 = "";
f4 = "";
} else {
/*
* For complex cases where this code doesn't
* generate the correct filename, the board
* code should set $fdtfile during early boot,
* or the boot scripts should set $fdtfile
* before invoking "pxe" or "sysboot".
*/
f1 = env_get("soc");
f2 = "-";
f3 = env_get("board");
f4 = ".dtb";
if (!f1) {
f1 = "";
f2 = "";
}
if (!f3) {
f2 = "";
f3 = "";
}
}
len = strlen(label->fdtdir);
if (!len)
slash = "./";
else if (label->fdtdir[len - 1] != '/')
slash = "/";
else
slash = "";
len = strlen(label->fdtdir) + strlen(slash) +
strlen(f1) + strlen(f2) + strlen(f3) +
strlen(f4) + 1;
fdtfilefree = malloc(len);
if (!fdtfilefree) {
printf("malloc fail (FDT filename)\n");
goto cleanup;
}
snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
label->fdtdir, slash, f1, f2, f3, f4);
fdtfile = fdtfilefree;
}
if (fdtfile) {
int err = get_relfile_envaddr(ctx, fdtfile,
"fdt_addr_r", NULL);
free(fdtfilefree);
if (err < 0) {
bootm_argv[3] = NULL;
if (label->fdt) {
printf("Skipping %s for failure retrieving FDT\n",
label->name);
goto cleanup;
}
}
if (label->kaslrseed)
label_boot_kaslrseed();
#ifdef CONFIG_OF_LIBFDT_OVERLAY
if (label->fdtoverlays)
label_boot_fdtoverlay(ctx, label);
#endif
} else {
bootm_argv[3] = NULL;
}
}
bootm_argv[1] = kernel_addr;
zboot_argv[1] = kernel_addr;
if (initrd_addr_str) {
bootm_argv[2] = initrd_str;
bootm_argc = 3;
zboot_argv[3] = initrd_addr_str;
zboot_argv[4] = initrd_filesize;
zboot_argc = 5;
}
if (!bootm_argv[3])
bootm_argv[3] = env_get("fdt_addr");
kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
buf = map_sysmem(kernel_addr_r, 0);
if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT)
bootm_argv[3] = env_get("fdtcontroladdr");
if (bootm_argv[3]) {
if (!bootm_argv[2])
bootm_argv[2] = "-";
bootm_argc = 4;
}
/* Try bootm for legacy and FIT format image */
if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
IS_ENABLED(CONFIG_CMD_BOOTM))
do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
/* Try booting an AArch64 Linux kernel image */
else if (IS_ENABLED(CONFIG_CMD_BOOTI))
do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
/* Try booting a Image */
else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
/* Try booting an x86_64 Linux kernel image */
else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
unmap_sysmem(buf);
cleanup:
free(fit_addr);
return 1;
}
/** enum token_type - Tokens for the pxe file parser */
enum token_type {
T_EOL,
T_STRING,
T_EOF,
T_MENU,
T_TITLE,
T_TIMEOUT,
T_LABEL,
T_KERNEL,
T_LINUX,
T_APPEND,
T_INITRD,
T_LOCALBOOT,
T_DEFAULT,
T_PROMPT,
T_INCLUDE,
T_FDT,
T_FDTDIR,
T_FDTOVERLAYS,
T_ONTIMEOUT,
T_IPAPPEND,
T_BACKGROUND,
T_KASLRSEED,
T_INVALID
};
/** struct token - token - given by a value and a type */
struct token {
char *val;
enum token_type type;
};
/* Keywords recognized */
static const struct token keywords[] = {
{"menu", T_MENU},
{"title", T_TITLE},
{"timeout", T_TIMEOUT},
{"default", T_DEFAULT},
{"prompt", T_PROMPT},
{"label", T_LABEL},
{"kernel", T_KERNEL},
{"linux", T_LINUX},
{"localboot", T_LOCALBOOT},
{"append", T_APPEND},
{"initrd", T_INITRD},
{"include", T_INCLUDE},
{"devicetree", T_FDT},
{"fdt", T_FDT},
{"devicetreedir", T_FDTDIR},
{"fdtdir", T_FDTDIR},
{"fdtoverlays", T_FDTOVERLAYS},
{"devicetree-overlay", T_FDTOVERLAYS},
{"ontimeout", T_ONTIMEOUT,},
{"ipappend", T_IPAPPEND,},
{"background", T_BACKGROUND,},
{"kaslrseed", T_KASLRSEED,},
{NULL, T_INVALID}
};
/**
* enum lex_state - lexer state
*
* Since pxe(linux) files don't have a token to identify the start of a
* literal, we have to keep track of when we're in a state where a literal is
* expected vs when we're in a state a keyword is expected.
*/
enum lex_state {
L_NORMAL = 0,
L_KEYWORD,
L_SLITERAL
};
/**
* get_string() - retrieves a string from *p and stores it as a token in *t.
*
* This is used for scanning both string literals and keywords.
*
* Characters from *p are copied into t-val until a character equal to
* delim is found, or a NUL byte is reached. If delim has the special value of
* ' ', any whitespace character will be used as a delimiter.
*
* If lower is unequal to 0, uppercase characters will be converted to
* lowercase in the result. This is useful to make keywords case
* insensitive.
*
* The location of *p is updated to point to the first character after the end
* of the token - the ending delimiter.
*
* Memory for t->val is allocated using malloc and must be free()'d to reclaim
* it.
*
* @p: Points to a pointer to the current position in the input being processed.
* Updated to point at the first character after the current token
* @t: Pointers to a token to fill in
* @delim: Delimiter character to look for, either newline or space
* @lower: true to convert the string to lower case when storing
* Returns the new value of t->val, on success, NULL if out of memory
*/
static char *get_string(char **p, struct token *t, char delim, int lower)
{
char *b, *e;
size_t len, i;
/*
* b and e both start at the beginning of the input stream.
*
* e is incremented until we find the ending delimiter, or a NUL byte
* is reached. Then, we take e - b to find the length of the token.
*/
b = *p;
e = *p;
while (*e) {
if ((delim == ' ' && isspace(*e)) || delim == *e)
break;
e++;
}
len = e - b;
/*
* Allocate memory to hold the string, and copy it in, converting
* characters to lowercase if lower is != 0.
*/
t->val = malloc(len + 1);
if (!t->val)
return NULL;
for (i = 0; i < len; i++, b++) {
if (lower)
t->val[i] = tolower(*b);
else
t->val[i] = *b;
}
t->val[len] = '\0';
/* Update *p so the caller knows where to continue scanning */
*p = e;
t->type = T_STRING;
return t->val;
}
/**
* get_keyword() - Populate a keyword token with a type and value
*
* Updates the ->type field based on the keyword string in @val
* @t: Token to populate
*/
static void get_keyword(struct token *t)
{
int i;
for (i = 0; keywords[i].val; i++) {
if (!strcmp(t->val, keywords[i].val)) {
t->type = keywords[i].type;
break;
}
}
}
/**
* get_token() - Get the next token
*
* We have to keep track of which state we're in to know if we're looking to get
* a string literal or a keyword.
*
* @p: Points to a pointer to the current position in the input being processed.
* Updated to point at the first character after the current token
*/
static void get_token(char **p, struct token *t, enum lex_state state)
{
char *c = *p;
t->type = T_INVALID;
/* eat non EOL whitespace */
while (isblank(*c))
c++;
/*
* eat comments. note that string literals can't begin with #, but
* can contain a # after their first character.
*/
if (*c == '#') {
while (*c && *c != '\n')
c++;
}
if (*c == '\n') {
t->type = T_EOL;
c++;
} else if (*c == '\0') {
t->type = T_EOF;
c++;
} else if (state == L_SLITERAL) {
get_string(&c, t, '\n', 0);
} else if (state == L_KEYWORD) {
/*
* when we expect a keyword, we first get the next string
* token delimited by whitespace, and then check if it
* matches a keyword in our keyword list. if it does, it's
* converted to a keyword token of the appropriate type, and
* if not, it remains a string token.
*/
get_string(&c, t, ' ', 1);
get_keyword(t);
}
*p = c;
}
/**
* eol_or_eof() - Find end of line
*
* Increment *c until we get to the end of the current line, or EOF
*
* @c: Points to a pointer to the current position in the input being processed.
* Updated to point at the first character after the current token
*/
static void eol_or_eof(char **c)
{
while (**c && **c != '\n')
(*c)++;
}
/*
* All of these parse_* functions share some common behavior.
*
* They finish with *c pointing after the token they parse, and return 1 on
* success, or < 0 on error.
*/
/*