]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_source.c
arc: introduce U-Boot port for ARCv2 ISA
[people/ms/u-boot.git] / common / cmd_source.c
CommitLineData
d0dd1077
WD
1/*
2 * (C) Copyright 2001
3 * Kyle Harris, kharris@nexus-tech.net
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
d0dd1077
WD
6 */
7
8/*
74de7aef
WD
9 * The "source" command allows to define "script images", i. e. files
10 * that contain command sequences that can be executed by the command
11 * interpreter. It returns the exit status of the last command
12 * executed from the script. This is very similar to running a shell
13 * script in a UNIX shell, hence the name for the command.
d0dd1077
WD
14 */
15
16/* #define DEBUG */
17
18#include <common.h>
19#include <command.h>
20#include <image.h>
21#include <malloc.h>
22#include <asm/byteorder.h>
4ca30d60 23#include <asm/io.h>
d0dd1077
WD
24#if defined(CONFIG_8xx)
25#include <mpc8xx.h>
26#endif
d0dd1077 27
d0dd1077 28int
74de7aef 29source (ulong addr, const char *fit_uname)
d0dd1077 30{
53677ef1 31 ulong len;
21d29f7f 32#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
4ca30d60 33 const image_header_t *hdr;
21d29f7f 34#endif
424c4abd 35 ulong *data;
424c4abd 36 int verify;
4ca30d60 37 void *buf;
424c4abd
MB
38#if defined(CONFIG_FIT)
39 const void* fit_hdr;
40 int noffset;
41 const void *fit_data;
42 size_t fit_len;
43#endif
d0dd1077 44
edbed247 45 verify = getenv_yesno ("verify");
d0dd1077 46
4ca30d60
SG
47 buf = map_sysmem(addr, 0);
48 switch (genimg_get_format(buf)) {
21d29f7f 49#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7 50 case IMAGE_FORMAT_LEGACY:
4ca30d60 51 hdr = buf;
d0dd1077 52
d5934ad7
MB
53 if (!image_check_magic (hdr)) {
54 puts ("Bad magic number\n");
55 return 1;
56 }
d0dd1077 57
d5934ad7
MB
58 if (!image_check_hcrc (hdr)) {
59 puts ("Bad header crc\n");
d0dd1077
WD
60 return 1;
61 }
d0dd1077 62
d5934ad7
MB
63 if (verify) {
64 if (!image_check_dcrc (hdr)) {
65 puts ("Bad data crc\n");
66 return 1;
67 }
68 }
69
70 if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
71 puts ("Bad image type\n");
72 return 1;
73 }
d0dd1077 74
d5934ad7
MB
75 /* get length of script */
76 data = (ulong *)image_get_data (hdr);
d0dd1077 77
9a4daad0 78 if ((len = uimage_to_cpu (*data)) == 0) {
d5934ad7
MB
79 puts ("Empty Script\n");
80 return 1;
81 }
36cc8cbb
BS
82
83 /*
84 * scripts are just multi-image files with one component, seek
85 * past the zero-terminated sequence of image lengths to get
86 * to the actual image data
87 */
88 while (*data++);
d5934ad7 89 break;
21d29f7f 90#endif
d5934ad7
MB
91#if defined(CONFIG_FIT)
92 case IMAGE_FORMAT_FIT:
424c4abd
MB
93 if (fit_uname == NULL) {
94 puts ("No FIT subimage unit name\n");
95 return 1;
96 }
97
4ca30d60 98 fit_hdr = buf;
424c4abd
MB
99 if (!fit_check_format (fit_hdr)) {
100 puts ("Bad FIT image format\n");
101 return 1;
102 }
103
104 /* get script component image node offset */
105 noffset = fit_image_get_node (fit_hdr, fit_uname);
106 if (noffset < 0) {
107 printf ("Can't find '%s' FIT subimage\n", fit_uname);
108 return 1;
109 }
110
111 if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
112 puts ("Not a image image\n");
113 return 1;
114 }
115
116 /* verify integrity */
117 if (verify) {
b8da8366 118 if (!fit_image_verify(fit_hdr, noffset)) {
424c4abd
MB
119 puts ("Bad Data Hash\n");
120 return 1;
121 }
122 }
123
124 /* get script subimage data address and length */
125 if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
126 puts ("Could not find script subimage data\n");
127 return 1;
128 }
129
130 data = (ulong *)fit_data;
131 len = (ulong)fit_len;
132 break;
d5934ad7
MB
133#endif
134 default:
74de7aef 135 puts ("Wrong image format for \"source\" command\n");
d0dd1077
WD
136 return 1;
137 }
138
699b13a6 139 debug ("** Script length: %ld\n", len);
d51004a8 140 return run_command_list((char *)data, len, 0);
d0dd1077
WD
141}
142
8bde7f77 143/**************************************************/
74de7aef 144#if defined(CONFIG_CMD_SOURCE)
0e350f81 145static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
d0dd1077
WD
146{
147 ulong addr;
148 int rcode;
424c4abd 149 const char *fit_uname = NULL;
d0dd1077 150
424c4abd 151 /* Find script image */
d0dd1077 152 if (argc < 2) {
6d0f6bcf 153 addr = CONFIG_SYS_LOAD_ADDR;
74de7aef 154 debug ("* source: default load address = 0x%08lx\n", addr);
424c4abd
MB
155#if defined(CONFIG_FIT)
156 } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
74de7aef 157 debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
424c4abd
MB
158 fit_uname, addr);
159#endif
d0dd1077 160 } else {
424c4abd 161 addr = simple_strtoul(argv[1], NULL, 16);
74de7aef 162 debug ("* source: cmdline image address = 0x%08lx\n", addr);
d0dd1077
WD
163 }
164
424c4abd 165 printf ("## Executing script at %08lx\n", addr);
74de7aef 166 rcode = source (addr, fit_uname);
d0dd1077
WD
167 return rcode;
168}
8bde7f77 169
088f1b19
KP
170#ifdef CONFIG_SYS_LONGHELP
171static char source_help_text[] =
74de7aef
WD
172 "[addr]\n"
173 "\t- run script starting at addr\n"
a89c33db 174 "\t- A valid image header must be present"
424c4abd 175#if defined(CONFIG_FIT)
a89c33db 176 "\n"
424c4abd 177 "For FIT format uImage addr must include subimage\n"
a89c33db 178 "unit name in the form of addr:<subimg_uname>"
90253178 179#endif
088f1b19
KP
180 "";
181#endif
182
183U_BOOT_CMD(
184 source, 2, 0, do_source,
185 "run script from memory", source_help_text
424c4abd 186);
90253178 187#endif