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 77 if (argc != 2 || !argv[1]) 78 return CMD_RET_USAGE; 79 80 addr = simple_strtoul(argv[1], NULL, 16); 81 if (!addr) 82 return CMD_RET_USAGE; 83 84 buf = calloc(SCRIPT_FILE_MAX_SIZE, 1); 85 if (!buf) 86 return CMD_RET_FAILURE; 87 88 script = buf; 89 memcpy(buf, (char *)addr, SCRIPT_FILE_MAX_SIZE); 90 while ((next_line = script_next_line(&script)) != NULL) { 91 printf("\n$ %s\n", next_line); 92 run_command(next_line, 0); 93 } 94 free(buf); 95 96 return CMD_RET_SUCCESS; 97 } 98 99 static int do_sd_update(cmd_tbl_t *cmdtp, int flag, 100 int argc, char * const argv[]) 101 { 102 char cmd[128]; 103 char *buf; 104 int ret; 105 106 buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2); 107 if (!buf) 108 return CMD_RET_FAILURE; 109 110 snprintf(cmd, 128, "fatload mmc 1 0x%08lx sd_update.txt", (ulong)buf); 111 ret = run_command(cmd, 0); 112 if (!ret) { 113 snprintf(cmd, 128, "script 0x%08lx", (ulong)buf); 114 ret = run_command(cmd, 0); 115 } 116 free(buf); 117 118 return ret; 119 } 120 121 static int do_usb_update(cmd_tbl_t *cmdtp, int flag, 122 int argc, char * const argv[]) 123 { 124 char cmd[128]; 125 char *buf; 126 int ret; 127 128 buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2); 129 if (!buf) 130 return CMD_RET_FAILURE; 131 132 snprintf(cmd, 128, "usb reset"); 133 ret = run_command(cmd, 0); 134 if (!ret) { 135 snprintf(cmd, 128, "fatload usb 0 0x%08lx usb_update.txt", (ulong)buf); 136 ret = run_command(cmd, 0); 137 } 138 if (!ret) { 139 snprintf(cmd, 128, "script 0x%08lx", (ulong)buf); 140 ret = run_command(cmd, 0); 141 } 142 free(buf); 143 144 return ret; 145 } 146 147 static int do_tftp_update(cmd_tbl_t *cmdtp, int flag, 148 int argc, char * const argv[]) 149 { 150 char cmd[128]; 151 char *buf; 152 int dhcp = 0; 153 int ret; 154 155 if ((argc > 1) && !strcmp(argv[1], "-d")) 156 dhcp = 1; 157 158 buf = memalign(ARCH_DMA_MINALIGN, SCRIPT_FILE_MAX_SIZE * 2); 159 if (!buf) 160 return CMD_RET_FAILURE; 161 162 if (dhcp) 163 run_command("dhcp", 0); 164 165 snprintf(cmd, 128, "tftp 0x%08lx tftp_update.txt", (ulong)buf); 166 ret = run_command(cmd, 0); 167 if (!ret) { 168 snprintf(cmd, 128, "script 0x%08lx", (ulong)buf); 169 ret = run_command(cmd, 0); 170 } 171 free(buf); 172 173 return ret; 174 } 175 176 U_BOOT_CMD( 177 script, 2, 1, do_script, 178 "Run a script", "[file addr]" 179 ); 180 181 U_BOOT_CMD( 182 sd_update, 1, 1, do_sd_update, 183 "sdcard auto upgrade", "" 184 ); 185 186 U_BOOT_CMD( 187 usb_update, 1, 1, do_usb_update, 188 "usb auto upgrade", "" 189 ); 190 191 U_BOOT_CMD( 192 tftp_update, 2, 1, do_tftp_update, 193 "tftp auto upgrade", "[-d]" 194 ); 195 196