1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * Kyle Harris, kharris@nexus-tech.net
5  */
6 
7 /*
8  * The "source" command allows to define "script images", i. e. files
9  * that contain command sequences that can be executed by the command
10  * interpreter. It returns the exit status of the last command
11  * executed from the script. This is very similar to running a shell
12  * script in a UNIX shell, hence the name for the command.
13  */
14 
15 /* #define DEBUG */
16 
17 #include <command.h>
18 #include <env.h>
19 #include <image.h>
20 #include <log.h>
21 #include <malloc.h>
22 #include <mapmem.h>
23 #include <asm/byteorder.h>
24 #include <asm/io.h>
25 
do_source(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])26 static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
27 		     char *const argv[])
28 {
29 	ulong addr;
30 	int rcode;
31 	const char *fit_uname = NULL, *confname = NULL;
32 
33 	/* Find script image */
34 	if (argc < 2) {
35 		addr = CONFIG_SYS_LOAD_ADDR;
36 		debug("*  source: default load address = 0x%08lx\n", addr);
37 #if defined(CONFIG_FIT)
38 	} else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
39 				      &fit_uname)) {
40 		debug("*  source: subimage '%s' from FIT image at 0x%08lx\n",
41 		      fit_uname, addr);
42 	} else if (fit_parse_conf(argv[1], image_load_addr, &addr, &confname)) {
43 		debug("*  source: config '%s' from FIT image at 0x%08lx\n",
44 		      confname, addr);
45 #endif
46 	} else {
47 		addr = hextoul(argv[1], NULL);
48 		debug("*  source: cmdline image address = 0x%08lx\n", addr);
49 	}
50 
51 	printf ("## Executing script at %08lx\n", addr);
52 	rcode = cmd_source_script(addr, fit_uname, confname);
53 	return rcode;
54 }
55 
56 U_BOOT_LONGHELP(source,
57 #if defined(CONFIG_FIT)
58 	"[<addr>][:[<image>]|#[<config>]]\n"
59 	"\t- Run script starting at addr\n"
60 	"\t- A FIT config name or subimage name may be specified with : or #\n"
61 	"\t  (like bootm). If the image or config name is omitted, the\n"
62 	"\t  default is used."
63 #else
64 	"[<addr>]\n"
65 	"\t- Run script starting at addr"
66 #endif
67 	);
68 
69 U_BOOT_CMD(
70 	source, 2, 0,	do_source,
71 	"run script from memory", source_help_text
72 );
73