8 #include <linux/string.h>
9 #include "../drivers/mmc/card_sdio.h"
10 #include "thor_mode.h"
11 #include "tizen_misc.h"
12 #include "calibration_detect.h"
15 #define thor_debug(fmt, arg...) printf(fmt, ## arg)
17 #define thor_debug(fmt, arg...)
20 static unsigned char thor_tx_data_buf[sizeof(struct rsp_box)];
21 static unsigned char thor_rx_data_buf[sizeof(struct rqt_box)];
23 static struct rqt_box rqt_buf;
24 static struct rsp_box rsp_buf;
25 static struct data_rsp_box data_rsp_buf;
27 static unsigned int thor_file_size;
28 static unsigned long long total_file_size;
29 static unsigned long long downloaded_file_size;
31 static u32 download_addr = CONFIG_THOR_TRANSFER_BUFFER;
34 struct thor_part_info part_info;
35 static char f_name[F_NAME_BUF_SIZE];
37 extern void MMU_DisableIDCM(void);
38 extern void draw_progress(int per);
39 extern int32 USB_WriteEx(uint8 *pBuf,uint32 len);
40 extern int USB_ReadEx(unsigned char *pBuf, int len);
41 extern void thor_USB_Init(void);
42 extern int thor_usb_is_connected(void);
43 extern unsigned int thor_get_part_info(struct thor_part_info *part_info, const char *name);
44 extern int thor_save_env(char *str);
46 static void thor_clear_part_info(void)
55 static void send_rsp(const struct rsp_box *rsp)
57 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
58 USB_WriteEx(thor_tx_data_buf, sizeof(struct rsp_box));
60 thor_debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
63 static void send_data_rsp(signed int ack, signed int count)
65 struct data_rsp_box *rsp = &data_rsp_buf;
70 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
71 USB_WriteEx(thor_tx_data_buf, sizeof(struct data_rsp_box));
73 thor_debug("-DATA RSP: %d, %d\n", ack, count);
76 static int download_file_start(void)
79 unsigned int buffered = 0;
80 unsigned int remained = thor_file_size;
81 unsigned int progressed = 0;
82 unsigned int write_ofs = 0;
85 int download_done = 0;
89 thor_debug("Download file start\n");
91 while (!download_done) {
92 dn_addr = download_addr + buffered;
93 ret = USB_ReadEx((unsigned char *) dn_addr, THOR_PACKET_SIZE);
99 if (progressed >= thor_file_size)
103 downloaded_file_size += thor_file_size % ret;
105 downloaded_file_size += ret;
107 per = (int) lldiv(100 * downloaded_file_size, total_file_size);
110 /* MAX UNIT SIZE or done */
111 if ((buffered >= THOR_STORE_UNIT_SIZE) || download_done) {
112 unsigned int count, remain, mod;
114 remain = MIN(buffered, remained);
115 mod = remain % part_info.blksz;
116 count = remain / part_info.blksz;
118 /* write remain byte < block_size */
123 /* check board signature when download u-boot-mmc.bin */
124 ret = check_board_signature(f_name, download_addr, thor_file_size);
126 lcd_printf("Signature Check Failed\n");
130 ret = Emmc_Write(PARTITION_USER, part_info.offset + write_ofs, count, (uint8 *)download_addr);
135 remained -= buffered;
139 send_data_rsp(0, ++usb_pkt_cnt);
145 static long long int process_rqt_download(const struct rqt_box *rqt)
147 struct rsp_box *rsp = &rsp_buf;
148 int file_type, ret = 0;
150 memset(rsp, 0, sizeof(struct rsp_box));
152 rsp->rsp_data = rqt->rqt_data;
154 switch (rqt->rqt_data) {
156 thor_file_size = rqt->int_data[0];
157 total_file_size = thor_file_size;
159 downloaded_file_size = 0;
160 /* clear partition info */
161 thor_clear_part_info();
163 thor_debug("INIT: total %d bytes\n", rqt->int_data[0]);
165 case RQT_DL_FILE_INFO:
166 file_type = rqt->int_data[0];
167 if (file_type == FILE_TYPE_PIT) {
168 thor_debug("PIT table file - not supported\n");
169 rsp->ack = -ENOTSUPP;
174 thor_file_size = rqt->int_data[1];
175 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
177 thor_debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
178 f_name, 0, thor_file_size, file_type);
180 rsp->int_data[0] = THOR_PACKET_SIZE;
181 /* Get partition info by binary name */
182 ret = thor_get_part_info(&part_info, f_name);
184 thor_debug("Unsupported binary\n");
189 if ((uint64_t)thor_file_size >
190 (uint64_t)part_info.size * (uint64_t)part_info.blksz) {
191 thor_debug("Too large binary\n");
197 case RQT_DL_FILE_START:
199 return download_file_start();
200 case RQT_DL_FILE_END:
201 thor_debug("DL FILE_END\n");
204 thor_debug("DL EXIT\n");
207 thor_debug("Operation not supported: %d", rqt->rqt_data);
215 static int process_rqt_info(const struct rqt_box *rqt)
217 struct rsp_box *rsp = &rsp_buf;
218 memset(rsp, 0, sizeof(struct rsp_box));
221 rsp->rsp_data = rqt->rqt_data;
223 switch (rqt->rqt_data) {
224 case RQT_INFO_VER_PROTOCOL:
225 rsp->int_data[0] = VER_PROTOCOL_MAJOR;
226 rsp->int_data[1] = VER_PROTOCOL_MINOR;
228 case RQT_INIT_VER_HW:
230 case RQT_INIT_VER_BOOT:
231 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
233 case RQT_INIT_VER_KERNEL:
234 sprintf(rsp->str_data[0], "%s", "k unknown");
236 case RQT_INIT_VER_PLATFORM:
237 sprintf(rsp->str_data[0], "%s", "p unknown");
239 case RQT_INIT_VER_CSC:
240 sprintf(rsp->str_data[0], "%s", "c unknown");
250 static int process_rqt_cmd(const struct rqt_box *rqt)
252 struct rsp_box *rsp = &rsp_buf;
253 memset(rsp, 0, sizeof(struct rsp_box));
256 rsp->rsp_data = rqt->rqt_data;
258 switch (rqt->rqt_data) {
260 thor_debug("TARGET RESET\n");
264 thor_save_env("thor");
268 case RQT_CMD_POWEROFF:
269 case RQT_CMD_EFSCLEAR:
272 thor_debug("Command not supported -> cmd: %d\n", rqt->rqt_data);
279 static int process_data(void)
281 struct rqt_box *rqt = &rqt_buf;
284 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
286 thor_debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
290 ret = process_rqt_info(rqt);
293 ret = process_rqt_cmd(rqt);
296 ret = (int) process_rqt_download(rqt);
301 thor_debug("unknown request (%d)", rqt->rqt);
307 int thor_handle(void)
312 /* receive the data from Host PC */
314 if (thor_usb_is_connected()) {
315 ret = USB_ReadEx(thor_rx_data_buf, strlen("THOR"));
316 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR")))
318 USB_WriteEx((uint8 *)"ROHT", strlen("ROHT"));
321 ret = USB_ReadEx(thor_rx_data_buf + strlen("THOR"), sizeof(thor_rx_data_buf) - strlen("THOR"));
324 ret = process_data();
328 thor_debug("%s: No data received!\n", __func__);
337 extern int hw_revision;
340 lcd_set_cur_pos(1, 2);
341 lcd_printf("THOR MODE\n");
343 lcd_set_cur_pos(3, 2);
344 lcd_setfgcolor (CONSOLE_COLOR_WHITE);
345 lcd_printf("HW REV: %d\n", hw_revision);
353 int do_thor_down(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
355 puts("TIZEN \"THOR\" Downloader\n");
362 U_BOOT_CMD(thordown, 1, 1, do_thor_down,
363 "TIZEN \"THOR\" downloader",