xref: /rk3399_rockchip-uboot/cmd/script_update.c (revision cbfcaedb2b42d90f95783eb836cee6cbd224719e)
1 /*
2  * (C) Copyright 2022 Rockchip Electronics Co., Ltd.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <malloc.h>
9 
10 /*
11  * Script file example:
12  **************************
13  * > # script file start
14  * > echo "hello world"
15  * > % script file end
16  **************************
17  */
18 #define SCRIPT_FILE_MAX_SIZE	(12 * 1024)
19 #define SCRIPT_FILE_COMMENT	'#'
20 #define SCRIPT_FILE_END		'%'
21 #define IS_COMMENT(x)		(SCRIPT_FILE_COMMENT == (x))
22 #define IS_FILE_END(x)		(SCRIPT_FILE_END == (x))
23 #define IS_LINE_END(x)		('\r' == (x) || '\n' == (x))
24 #define MAX_LINE_SIZE		8000
25 
26 static char *script_next_line(char **line_buf_ptr)
27 {
28 	char *line_buf = (*line_buf_ptr);
29 	char *next_line;
30 	int i = 0;
31 
32 	/* strip '\r', '\n' and comment */
33 	while (1) {
34 		/* strip '\r' & '\n' */
35 		if (IS_LINE_END(line_buf[0])) {
36 			line_buf++;
37 		/* strip comment */
38 		} else if (IS_COMMENT(line_buf[0])) {
39 			for (i = 0; !IS_LINE_END(line_buf[0]) && i <= MAX_LINE_SIZE; i++)
40 				line_buf++;
41 
42 			if (i > MAX_LINE_SIZE) {
43 				line_buf[0] = SCRIPT_FILE_END;
44 				printf("Error: max line length is %d!!!\n", MAX_LINE_SIZE);
45 				break;
46 			}
47 		} else {
48 			break;
49 		}
50 	}
51 
52 	/* get next line */
53 	if (IS_FILE_END(line_buf[0])) {
54 		next_line = NULL;
55 	} else {
56 		next_line = line_buf;
57 		for (i = 0; !IS_LINE_END(line_buf[0]) && i <= MAX_LINE_SIZE; i++)
58 			line_buf++;
59 
60 		if (i > MAX_LINE_SIZE) {
61 			next_line = NULL;
62 			printf("Error: max line length is %d!!!\n", MAX_LINE_SIZE);
63 		} else {
64 			line_buf[0] = '\0';
65 			*line_buf_ptr = line_buf + 1;
66 		}
67 	}
68 
69 	return next_line;
70 }
71 
72 static int do_script(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73 {
74 	char *next_line, *script, *buf;
75 	ulong addr;
76 	int ret = CMD_RET_SUCCESS;
77 
78 	if (argc != 2 || !argv[1])
79 		return CMD_RET_USAGE;
80 
81 	addr = simple_strtoul(argv[1], NULL, 16);
82 	if (!addr)
83 		return CMD_RET_USAGE;
84 
85 	buf = calloc(SCRIPT_FILE_MAX_SIZE, 1);
86 	if (!buf)
87 		return CMD_RET_FAILURE;
88 
89 	script = buf;
90 	memcpy(buf, (char *)addr, SCRIPT_FILE_MAX_SIZE);
91 	while ((next_line = script_next_line(&script)) != NULL) {
92 		printf("\n$ %s\n", next_line);
93 		ret = run_command(next_line, 0);
94 		if (ret)
95 			break;	/* fail */
96 	}
97 	free(buf);
98 
99 	return ret;
100 }
101 
102 static int do_sd_update(cmd_tbl_t *cmdtp, int flag,
103 			int argc, char * const argv[])
104 {
105 	char cmd[128];
106 	char *buf;
107 	int ret;
108 
109 	buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2);
110 	if (!buf)
111 		return CMD_RET_FAILURE;
112 
113 	snprintf(cmd, 128, "fatload mmc 1 0x%08lx sd_update.txt", (ulong)buf);
114 	ret = run_command(cmd, 0);
115 	if (!ret) {
116 		snprintf(cmd, 128, "script 0x%08lx", (ulong)buf);
117 		ret = run_command(cmd, 0);
118 	}
119 	free(buf);
120 
121 	return ret;
122 }
123 
124 static int do_usb_update(cmd_tbl_t *cmdtp, int flag,
125 			 int argc, char * const argv[])
126 {
127 	char cmd[128];
128 	char *buf;
129 	int ret;
130 
131 	buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2);
132 	if (!buf)
133 		return CMD_RET_FAILURE;
134 
135 	snprintf(cmd, 128, "usb reset");
136 	ret = run_command(cmd, 0);
137 	if (!ret) {
138 		snprintf(cmd, 128, "fatload usb 0 0x%08lx usb_update.txt", (ulong)buf);
139 		ret = run_command(cmd, 0);
140 	}
141 	if (!ret) {
142 		snprintf(cmd, 128, "script 0x%08lx", (ulong)buf);
143 		ret = run_command(cmd, 0);
144 	}
145 	free(buf);
146 
147 	return ret;
148 }
149 
150 static int do_tftp_update(cmd_tbl_t *cmdtp, int flag,
151 			  int argc, char * const argv[])
152 {
153 	char cmd[128];
154 	char *buf;
155 	int dhcp = 0;
156 	int ret;
157 
158 	if ((argc > 1) && !strcmp(argv[1], "-d"))
159 		dhcp = 1;
160 
161 	buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2);
162 	if (!buf)
163 		return CMD_RET_FAILURE;
164 
165 	if (dhcp)
166 		run_command("dhcp", 0);
167 
168 	snprintf(cmd, 128, "tftp 0x%08lx tftp_update.txt", (ulong)buf);
169 	ret = run_command(cmd, 0);
170 	if (!ret) {
171 		snprintf(cmd, 128, "script 0x%08lx", (ulong)buf);
172 		ret = run_command(cmd, 0);
173 	}
174 	free(buf);
175 
176 	return ret;
177 }
178 
179 U_BOOT_CMD(
180 	script, 2, 1, do_script,
181 	"Run a script", "[file addr]"
182 );
183 
184 U_BOOT_CMD(
185 	sd_update, 1, 1, do_sd_update,
186 	"sdcard auto upgrade", ""
187 );
188 
189 U_BOOT_CMD(
190 	usb_update, 1, 1, do_usb_update,
191 	"usb auto upgrade", ""
192 );
193 
194 U_BOOT_CMD(
195 	tftp_update, 2, 1, do_tftp_update,
196 	"tftp auto upgrade", "[-d]"
197 );
198 
199