blob: 2d21427b16171b8419aa4948be2cae080b4a2694 [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <common.h>
7#include <command.h>
8#include <env.h>
9#include <malloc.h>
10#include <asm/byteorder.h>
11#include <config.h>
12#include <asm/amlogic/arch/io.h>
13#include <amlogic/storage.h>
14#include <stdlib.h>
15
16#ifdef CONFIG_BOOTLOADER_CONTROL_BLOCK
17
18#define COMMANDBUF_SIZE 32
19#define STATUSBUF_SIZE 32
20#define RECOVERYBUF_SIZE 768
21
22#define BOOTINFO_OFFSET 864
23#define SLOTBUF_SIZE 32
24#define MISCBUF_SIZE 1088
25
26#define CMD_WIPE_DATA "wipe_data"
27#define CMD_SYSTEM_CRASH "system_crash"
28#define CMD_RUN_RECOVERY "boot-recovery"
29#define CMD_RESIZE_DATA "resize2fs_data"
30#define CMD_FOR_RECOVERY "recovery_"
31#define CMD_FASTBOOTD "fastbootd"
32
33static const char * const temp_for_compile[] = {"__test1", "__test2", "__test3", NULL};
34extern const char * const _env_list_execute_[0] __attribute__((weak, alias("temp_for_compile")));
35
36struct bootloader_message {
37 char command[32];
38 char status[32];
39 char recovery[768];
40
41 // The 'recovery' field used to be 1024 bytes. It has only ever
42 // been used to store the recovery command line, so 768 bytes
43 // should be plenty. We carve off the last 256 bytes to store the
44 // stage string (for multistage packages) and possible future
45 // expansion.
46 char stage[32];
47 char slot_suffix[32];
48 char reserved[192];
49};
50
Xindong Xuc0b93662023-12-08 10:35:55 +080051#if (IS_ENABLED(CONFIG_CMD_BCB))
Bo Lv72d0e902023-01-02 14:27:34 +000052static bool env_command_check(const char *cmd)
53{
54 int index = 0;
55 int envListNum = 0;
56 char *s = NULL;
57 char *v = NULL;
58 char *strcopy = NULL;
59 const char **envListArr = (const char **)_env_list_execute_;
60
61 const char **pArr = (const char **)envListArr;
62
63 while (*pArr++) {
64 ++envListNum;
65 }
66
67 printf("envListNum = %d\n", envListNum);
68
69 if (envListNum == 0) {
70 return false;
71 }
72
73 strcopy = strdup(cmd);
74 if (!strcopy) {
75 return false;
76 }
77
78 s = strcopy;
79 while ((v = strsep(&s, ";")) != NULL) {
80 char *p = v;
81
82 strsep(&p, " ");
83 for (index = 0; index < envListNum; index++) {
84 if (!strcmp(envListArr[index], v)) {
85 break;
86 }
87 }
88
89 if (index == envListNum) {
90 printf("%s not in the white list.\n", v);
91 free(strcopy);
92 strcopy = NULL;
93 return false;
94 }
95 }
96
97 free(strcopy);
98 strcopy = NULL;
99 return true;
100}
Xindong Xuc0b93662023-12-08 10:35:55 +0800101#endif
Bo Lv72d0e902023-01-02 14:27:34 +0000102
103static int clear_misc_partition(char *clearbuf, int size)
104{
105 char *partition = "misc";
106
107 memset(clearbuf, 0, size);
108 if (store_write((const char *)partition,
109 0, size, (unsigned char *)clearbuf) < 0) {
110 printf("failed to clear %s.\n", partition);
111 return -1;
112 }
113
114 return 0;
115}
116
117static int my_atoi(const char *str)
118{
119 int result = 0;
120 int signal = 1;
121
122 if ((*str >= '0' && *str <= '9') || *str == '-' || *str == '+') {
123 if (*str == '-' || *str == '+') {
124 if (*str == '-')
125 signal = -1;
126 str++;
127 }
128 } else {
129 return 0;
130 }
131
132 while (*str >= '0' && *str <= '9')
133 result = result * 10 + (*str++ - '0');
134
135 return signal * result;
136}
137
138static int do_RunBcbCommand(
139 cmd_tbl_t * cmdtp,
140 int flag,
141 int argc,
142 char * const argv[])
143{
144 int i = 0;
145 char command[COMMANDBUF_SIZE] = {0};
146 char status[STATUSBUF_SIZE] = {0};
147 char recovery[RECOVERYBUF_SIZE] = {0};
148 char miscbuf[MISCBUF_SIZE] = {0};
149 char clearbuf[COMMANDBUF_SIZE+STATUSBUF_SIZE+RECOVERYBUF_SIZE] = {0};
150 char* RebootMode;
151 int remain_time = 0;
152
153 if (argc != 2) {
154 return cmd_usage(cmdtp);
155 }
156
157 printf("Command: ");
158 for (i = 0; i < argc; i++) {
159 printf("%s ", argv[i]);
160 }
161 printf("\n");
162
163 char *partition = "misc";
164 char *command_mark = (char *)argv[1];
165
166 if (strlen(command_mark) > sizeof(command)) {
167 //printf("Bcb command mark range out of length(%d > %d).\n",
168 //strlen(command_mark), sizeof(command));
169 goto ERR;
170 }
171
172 if (!memcmp(command_mark, CMD_WIPE_DATA, strlen(command_mark))) {
173 printf("Start to write --wipe_data to %s\n", partition);
174 memcpy(miscbuf, CMD_RUN_RECOVERY, sizeof(CMD_RUN_RECOVERY));
175 memcpy(miscbuf+sizeof(command)+sizeof(status), "recovery\n--wipe_data", sizeof("recovery\n--wipe_data"));
176 store_write((const char *)partition, 0, sizeof(miscbuf), (unsigned char *)miscbuf);
177 } else if (!memcmp(command_mark, CMD_SYSTEM_CRASH, strlen(command_mark))) {
178 printf("Start to write --system_crash to %s\n", partition);
179 memcpy(miscbuf, CMD_RUN_RECOVERY, sizeof(CMD_RUN_RECOVERY));
180 memcpy(miscbuf+sizeof(command)+sizeof(status), "recovery\n--system_crash", sizeof("recovery\n--system_crash"));
181 store_write((const char *)partition, 0, sizeof(miscbuf), (unsigned char *)miscbuf);
182 } else if (!memcmp(command_mark, CMD_RESIZE_DATA, strlen(command_mark))) {
183 printf("Start to write --resize2fs_data to %s\n", partition);
184 memcpy(miscbuf, CMD_RUN_RECOVERY, sizeof(CMD_RUN_RECOVERY));
185 memcpy(miscbuf+sizeof(command)+sizeof(status), "recovery\n--resize2fs_data", sizeof("recovery\n--resize2fs_data"));
186 store_write((const char *)partition, 0, sizeof(miscbuf), (unsigned char *)miscbuf);
187 } else if (!memcmp(command_mark, CMD_FOR_RECOVERY, strlen(CMD_FOR_RECOVERY))) {
188 memcpy(miscbuf, CMD_RUN_RECOVERY, sizeof(CMD_RUN_RECOVERY));
189 sprintf(recovery, "%s%s", "recovery\n--", command_mark);
190 memcpy(miscbuf+sizeof(command)+sizeof(status), recovery, strlen(recovery));
191 store_write((const char *)partition, 0, sizeof(miscbuf), (unsigned char *)miscbuf);
192 return 0;
193 } else if (!memcmp(command_mark, CMD_FASTBOOTD, strlen(command_mark))) {
194 printf("write cmd to enter fastbootd \n");
195 memcpy(miscbuf, CMD_RUN_RECOVERY, sizeof(CMD_RUN_RECOVERY));
196 memcpy(miscbuf+sizeof(command)+sizeof(status), "recovery\n--fastboot", sizeof("recovery\n--fastboot"));
197 store_write((const char *)partition, 0, sizeof(miscbuf), (unsigned char *)miscbuf);
198 return 0;
199 }
200
201 printf("Start read %s partition datas!\n", partition);
202 if (store_read((const char *)partition,
203 0, sizeof(miscbuf), (unsigned char *)miscbuf) < 0) {
204 printf("failed to store read %s.\n", partition);
205 goto ERR;
206 }
207
208 // judge misc partition whether has datas
209 char tmpbuf[MISCBUF_SIZE];
210 memset(tmpbuf, 0, sizeof(tmpbuf));
211 if (!memcmp(tmpbuf, miscbuf, strlen(miscbuf))) {
212 env_set("retry_recovery_times", "7");
213#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
214 run_command("update_env_part -p retry_recovery_times;", 0);
215#else
216 run_command("saveenv;", 0);
217#endif
218 printf("BCB hasn't any datas,exit!\n");
219 return 0;
220 }
221
222 memcpy(command, miscbuf, sizeof(command));
223 memcpy(status, miscbuf+sizeof(command), sizeof(status));
224 memcpy(recovery, miscbuf+sizeof(command)+sizeof(status), sizeof(recovery));
225 memcpy(clearbuf, miscbuf, sizeof(clearbuf));
226
227 printf("get bootloader message from misc partition:\n");
228 printf("[command:%s]\n[status:%s]\n[recovery:%s]\n",
229 command, status, recovery);
230
231 run_command("get_rebootmode", 0);
232 RebootMode = env_get("reboot_mode");
233 if (strstr(RebootMode, "quiescent") != NULL) {
234 printf("quiescent mode.\n");
235 run_command("run storeargs", 0);
236 run_command("setenv bootconfig ${bootconfig} androidboot.quiescent=1;", 0);
237 }
238
239 char *retry_times;
240
241 retry_times = env_get("retry_recovery_times");
242 if (retry_times) {
243 printf("retry_time: %s\n", retry_times);
244 remain_time = my_atoi(retry_times);
245 printf("retry_time remain_time = %d\n", remain_time);
246 }
247
248 if (remain_time == 0) {
249 printf("clear recovery cmds in misc\n");
250 if (clear_misc_partition(clearbuf, sizeof(clearbuf)) < 0) {
251 printf("clear misc partition failed.\n");
252 goto ERR;
253 }
254 env_set("retry_recovery_times", "7");
255#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
256 run_command("update_env_part -p retry_recovery_times;", 0);
257#else
258 run_command("saveenv;", 0);
259#endif
260 run_command("run enter_fastboot", 0);
261 }
262
263 if (!memcmp(command, CMD_RUN_RECOVERY, strlen(CMD_RUN_RECOVERY))) {
264 if (retry_times && remain_time > 0) {
265 sprintf(retry_times, "%d", remain_time - 1);
266 printf("retry_time: %s\n", retry_times);
267 env_set("retry_recovery_times", retry_times);
268#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
269 run_command("update_env_part -p retry_recovery_times;", 0);
270#else
271 run_command("saveenv;", 0);
272#endif
273 }
274
275 if (run_command("run recovery_from_flash", 0) < 0) {
276 printf("run_command for cmd:run recovery_from_flash failed.\n");
277 return -1;
278 }
279 printf("run command:run recovery_from_flash successful.\n");
280 return 0;
281 }
282
Xindong Xuc0b93662023-12-08 10:35:55 +0800283#if (IS_ENABLED(CONFIG_CMD_BCB))
Bo Lv72d0e902023-01-02 14:27:34 +0000284 //uboot-command only valid once, not matter success or not
285 if (clear_misc_partition(clearbuf, sizeof(clearbuf)) < 0) {
286 printf("clear misc partition failed.\n");
287 goto ERR;
288 }
289
290 if (!memcmp(command_mark, command, strlen(command_mark))) {
291 printf("%s\n", recovery);
292 if (run_command((char *)recovery, 0) < 0) {
293 printf("run_command for cmd:%s failed.\n", recovery);
294 goto ERR;
295 }
296 printf("run command successful.\n");
297 } else if (!strncmp(command_mark, "uboot-command", strlen("uboot-command"))) {
298 printf("uboot-command: %s\n", command);
299
300 if (!env_command_check(command)) {
301 printf("not all uboot-command in white-list\n");
302 goto ERR;
303 }
304 if (run_command((char *)command, 0) < 0) {
305 printf("run_command for cmd:%s failed.\n", command);
306 goto ERR;
307 }
308 printf("run uboot-command successful.\n");
309
310 } else {
311 printf("command mark(%s) not match %s,don't execute.\n",
312 command_mark, command);
313 }
Xindong Xuc0b93662023-12-08 10:35:55 +0800314#endif
Bo Lv72d0e902023-01-02 14:27:34 +0000315
316 return 0;
317
318 ERR:
319 return -1;
320}
321#else
322static int do_RunBcbCommand(
323 cmd_tbl_t * cmdtp,
324 int flag,
325 int argc,
326 char * const argv[])
327{
328 if (argc != 2) {
329 return cmd_usage(cmdtp);
330 }
331
332 // Do-Nothing!
333 return 0;
334}
335#endif /* CONFIG_BOOTLOADER_CONTROL_BLOCK */
336
337
338// BCB: Bootloader Control Block
339U_BOOT_CMD(
340 bcb, 2, 0, do_RunBcbCommand,
341 "bcb",
342 "\nThis command will run some commands which saved in misc\n"
343 "partition by mark to decide whether execute command!\n"
344 "Command format:\n"
345 " bcb bcb_mark\n"
346 "Example:\n"
347 " /dev/block/misc partition is saved some contents:\n"
348 " uboot-command\n" // command mark
349 " N/A\n"
350 " setenv aa 11;setenv bb 22;setenv cc 33;saveenv;\n" // command
351 "So you can execute command: bcb uboot-command"
352);