📄 usb_download.c
字号:
#include <stdio.h>#include <string.h>#include "socket_base.h"#define SERVER_IP "192.168.1.100"#define CLIENT_IP "192.168.1.101"char *next_cmd = "reboot";char str[] = "blob>";char rbuf[4096], rbuf_save[256];HTRANSINTERFACE handle;char *p_buf;int read_comp_comm(int i){ int ret = 0; int readresult = 0; int compresult = 0; memset(rbuf, 0, 4096); char *pdest = NULL; int pos = 0; do { int k = 0, pos = 0; readresult = ReadPackage(rbuf, 4096, handle, 1, 3); if (readresult == 0) { printf("k:%d read %d bytes\n", k, readresult); goto kk; } printf("========Read Package is successfully==========\n"); p_buf = rbuf; printf("To compare the string to see wheterh fvscaler or not\n"); printf("p_buf= %s\n", p_buf); //compresult = strncmp(p_buf, "fCore:",6); pdest = strstr(p_buf, str); pos = pdest - p_buf + 1; if( pdest != NULL ) { printf( "%s found at position %d\n\n", str, pos); int j = 1; printf("get current fcore success\n"); pdest += 5; switch (i) { case 1: j = strncmp(pdest, "blob>", 5); break; case 2: j = strncmp(pdest, "Read flash from 0x60000, length 0x200000", 40); break; case 3: j = strncmp(pdest, "tftp_cmd: file 'zImage' loaded", 30); break; case 4: j = strncmp(pdest, "Done", 4); break; case 5: j = strncmp(pdest, "]#", 2); break; case 6: j = strncmp(pdest, "OK", 2); break; case 7: j = strncmp(pdest, "tftp_cmd: file 'rootfs.jffs2' loaded", 36); break; case 8: j = strncmp(pdest, "tftp_cmd: file 'boot.bin.usb' loaded", 36); break; case 9: j = strncmp(pdest, "Write flash from 0x0, length 0x60000", 36); break; case 10: j = strncmp(pdest, "Set id: 0x1286", 14); break; case 11: j = strncmp(pdest, "Set id: 0x9001", 14); break; case 12: j = strncmp(pdest, "Plug-in USB cable &", 18); break; default: printf("i is error\n"); } if (j == 0) { printf("The frist fvscaler info is succesful\n"); } else { printf("The frist fvscaler info is fail\n"); } compresult = 1; } else { printf( "%s not found\n", str); compresult = 0; };kk: k++; printf ("read package and get right comm content time: %d\n", k); if (k > 10) { printf("Read package via COMM, Fail to get right content or read\n"); ret = 1; break; } } while ( (readresult == 0)||(compresult == 0)); return ret;}int main(int argc, char *argv[]){ COMMANDPAR par; HTRANSINTERFACE handle; int result, last_result; int ret=0; int len=0; int i; char rbuf[4096], rbuf_save[256]; int *log_fd; char *p_buf; char *mac_buf; char *wbuf = malloc(256); char *macaddr = malloc(20); memset(macaddr, 0,20); mac_buf = macaddr; memset(rbuf, 0, 4096); memset(rbuf_save, 0, 256); par.opMode = COMM_OP; par.commAddr = malloc(30); par.commAddr = "/dev/ttyS0"; par.baudrate = malloc(15); par.baudrate = "B38400"; par.endPoint = SERVER; handle = OpenTransInterface(&par); if(handle == NULL){ printf("Open Transfer interface failed\n"); return -1; } sleep(5); printf("----Test platform can enter blob control interface----\n"); /* wait to enter blob command control interface*/ for (i=0; i<6; i++) { wbuf = "\n"; result = SendPackage(wbuf, 1, handle); if(result != 1){ printf("Send problem\n"); ret = -1; goto out; } sleep(1); } printf("echo anykey successed\n"); printf("=====Enter blob command prompt====="); result = read_comp_comm (1); if(result != 0){ printf("Enter blob command prompt is fail\n"); ret = -1; goto out; } printf("----Download boot.bin.usb into SRAM------\n"); wbuf = "tftp boot.bin.usb\n"; result = SendPackage(wbuf, 18, handle); if(result != 18){ printf("Send problem\n"); ret = -1; goto out; } //Tftp boot.bin.usb time sleep(2); result = read_comp_comm (8); if(result != 0){ printf("Download boot.bin.usb to SRAM is fail\n"); ret = -1; goto out; } printf("----Test write boot.bin.usb from SRAM to NAND FLASH------\n"); wbuf = "nandwrite -j 0x80800000 0x0 0x60000\n"; result = SendPackage(wbuf, 36, handle); if(result != 36){ printf("Send problem\n"); ret = -1; goto out; } //Write boot.bin.usb time sleep(2); result = read_comp_comm (9); if(result != 0){ printf("Write boot.bin.usb to NAND FLASH is fail\n"); ret = -1; goto out; } printf("Restart the platform to use this boot.bin.usb bootload\n"); //reboot the platform wbuf = "reboot\n"; result = SendPackage(wbuf, 7, handle); if(result != 7){ printf("Send problem\n"); ret = -1; goto out; } //Wait the bootload reboot time sleep(8); /* wait to enter blob command control interface*/ for (i=0; i<6; i++) { wbuf = "\n"; result = SendPackage(wbuf, 1, handle); if(result != 1){ printf("Send problem\n"); ret = -1; goto out; } sleep(1); } printf("echo anykey successed\n"); printf("=====Enter blob command prompt====="); result = read_comp_comm (1); if(result != 0){ printf("Enter blob command prompt is fail\n"); ret = -1; goto out; } printf("----Set USB vendor ID ----\n"); wbuf = "setusbid vid 0x1286\n"; result = SendPackage(wbuf, 20, handle); if(result != 20){ printf("Send problem\n"); ret = -1; goto out; } printf("=====Check USB vender ID set====="); result = read_comp_comm (10); if(result != 0){ printf("Setting USB vender ID is fail\n"); ret = -1; goto out; } printf("----Set USB product ID----\n"); wbuf = "setusbid pid 0x8001\n"; result = SendPackage(wbuf, 20, handle); if(result != 20){ printf("Send problem\n"); ret = -1; goto out; } printf("=====Check USB product ID set====="); result = read_comp_comm (11); if(result != 0){ printf("Setting USB product ID is fail\n"); ret = -1; goto out; } printf("----Set USB Server IP ----\n"); wbuf = "setip server 192.168.2.100\n"; result = SendPackage(wbuf, 27, handle); if(result != 27){ printf("Send problem\n"); ret = -1; goto out; } printf("----Set USB Client IP ----\n"); wbuf = "setip client 192.168.2.101\n"; result = SendPackage(wbuf, 27, handle); if(result != 27){ printf("Send problem\n"); ret = -1; goto out; } printf("===== Check USB HOT Plug/Remove information on the blob command prompt====="); wbuf = "tftp zImage\n"; result = SendPackage(wbuf, 12, handle); if(result != 12){ printf("Send problem\n"); ret = -1; goto out; } result = read_comp_comm (12); if(result != 0){ printf("Get the plug-in USB cable information is fail\n"); ret = -1; goto out; } printf("===== Inform Windows Client PC to simulate USB HOT Plug/Remove and Config USB Server IP address"); system("arm-sendmessage 192.168.1.103:10089"); sleep(20); printf("----download zImage------\n"); printf("----Test use tftp to download zImage to SRAM----\n"); // test download zImage to nandflash wbuf = "tftp zImage\n"; result = SendPackage(wbuf, 12, handle); if(result != 12){ printf("Send problem\n"); ret = -1; goto out; } //Tftp kernel zImage time sleep(4); result = read_comp_comm (3); if(result != 0){ printf("Download zImage to SRAM is fail\n"); ret = -1; goto out; } printf("----Test write zImage to nand flash------\n"); wbuf = "nandwrite -j 0x80800000 0x60000 0x200000\n"; result = SendPackage(wbuf, 41, handle); if(result != 41){ printf("Send problem\n"); ret = -1; goto out; } sleep(2); result = read_comp_comm (4); if(result != 0){ printf("Write zImage to NANDFLASH is fail\n"); ret = -1; goto out; } /* test download rootfs.jffs2 to nandflash*/ printf("----Test download rootfs.jffs2 to sram------\n"); wbuf = "tftp rootfs.jffs2\n"; result = SendPackage(wbuf, 18, handle); if(result != 18){ printf("Send problem: tftp rootfs.jffs2 is fail\n"); ret = -1; goto out; } //Download rootfs time sleep(50); result = read_comp_comm (7); if(result != 0){ printf("Downlaod rootfs.jffs2 to SRAM is fail\n"); ret = -1; goto out; } printf("---- nandwrite -j 0x80800000 0x260000 0x3000000 to console----\n"); wbuf = "nandwrite -j 0x80800000 0x260000 0x3000000\n"; result = SendPackage(wbuf, 43, handle); if(result != 43){ printf("Send problem\n"); ret = -1; goto out; } //nandwrite time sleep(15); result = read_comp_comm (4); if(result != 0){ printf("Downlaod rootfs.jffs2 to SRAM is fail\n"); ret = -1; goto out; } wbuf = "nkernel\n"; result = SendPackage(wbuf, 8, handle); if(result != 8){ printf("Send problem\n"); ret = -1; goto out; } result = read_comp_comm (4); if(result != 0){ printf("set_badblk 3 is fail\n"); ret = -1; goto out; } wbuf = "boot\n"; result = SendPackage(wbuf, 5, handle); if(result != 5){ printf("Send problem\n"); ret = -1; goto out; } sleep(30); printf("Send Enter key to console\n"); wbuf = "\n"; result = SendPackage(wbuf, 1, handle); if(result != 1){ printf("Send problem\n"); ret = -1; goto out; } sleep(10); result = read_comp_comm (5); if(result != 0){ printf("BOOT UP system is fail\n"); ret = -1; goto out; } printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); out: if(handle != NULL){ CloseTransInterface(handle); } system("ifconfig eth0 192.168.1.100"); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -