/* dnw2 linux main file. This depends on libusb.
* *
* * Author: Fox <hulifox008@163.com>
* * License: GPL
* *
* */
#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> #define TINY4412_SECBULK_IDVENDOR 0x04e8
#define TINY4412_SECBULK_IDPRODUCT 0x1234 #define TINY4412_RAM_BASE 0xc0000000 // TINY4412
#define RAM_BASE TINY4412_RAM_BASE
#define VENDOR_ID TINY4412_SECBULK_IDVENDOR
#define PRODUCT_ID TINY4412_SECBULK_IDPRODUCT struct usb_dev_handle * open_port()
{
struct usb_bus *busses, *bus; usb_init();
usb_find_busses();
usb_find_devices(); busses = usb_get_busses();
for(bus=busses;bus;bus=bus->next)
{
struct usb_device *dev;
for(dev=bus->devices;dev;dev=dev->next)
{
if( VENDOR_ID==dev->descriptor.idVendor
&& PRODUCT_ID==dev->descriptor.idProduct)
{
printf("Target usb device found!\n");
struct usb_dev_handle *hdev = usb_open(dev);
if(!hdev)
{
perror("Cannot open device");
}
else
{
if(0!=usb_claim_interface(hdev, 0))
{
perror("Cannot claim interface");
usb_close(hdev);
hdev = NULL;
}
}
return hdev;
}
}
} printf("Target usb device not found!\n"); return NULL;
} void usage()
{
printf("Usage: dnw2 <file>\n\n");
} unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs; int fd = open(filename, O_RDONLY);
if(-1==fd)
{
perror("Cannot open file");
return NULL;
}
if(-1==fstat(fd, &fs))
{
perror("Cannot get file size");
goto error;
}
write_buf = (unsigned char*)malloc(fs.st_size+10);
if(NULL==write_buf)
{
perror("malloc failed");
goto error;
} if(fs.st_size != read(fd, write_buf+8, fs.st_size))
{
perror("Reading file failed");
goto error;
}
unsigned short sum = 0;
int i;
for(i=8; i<fs.st_size+8; i++)
{
sum += write_buf[i];
}
printf("Filename : %s\n", filename);
printf("Filesize : %d bytes\n", (int)fs.st_size);
printf ("Sum is %x\n",sum); *((u_int32_t*)write_buf) = RAM_BASE; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;
write_buf [fs.st_size + 8] = sum & 0xff;
write_buf [fs.st_size + 9] = sum >> 8;
*len = fs.st_size + 10;
return write_buf; error:
if(fd!=-1) close(fd);
if(NULL!=write_buf) free(write_buf);
fs.st_size = 0;
return NULL; } int main(int argc, char *argv[])
{
if(2!=argc)
{
usage();
return 1;
} struct usb_dev_handle *hdev = open_port();
if(!hdev)
{
return 1;
} unsigned int len = 0;
unsigned char* write_buf = prepare_write_buf(argv[1], &len);
if(NULL==write_buf) return 1; unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...\n");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("\r %d \t %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!\n");
return 0;
}

makefile

#******************************************************************************
#*
#* Copyright (C) 2009-2012 Broadcom Corporation
#*
#* Licensed under the Apache License, Version 2.0 (the "License");
#* you may not use this file except in compliance with the License.
#* You may obtain a copy of the License at
#*
#* http://www.apache.org/licenses/LICENSE-2.0
#*
#* Unless required by applicable law or agreed to in writing, software
#* distributed under the License is distributed on an "AS IS" BASIS,
#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#* See the License for the specific language governing permissions and
#* limitations under the License.
#*
#****************************************************************************** LDLIBS = -lusb
CFLAGS=-g dnw: dnw.o

最新文章

  1. 编译QtAV工程库
  2. json入门(二)
  3. ArcGIS Server API for JavaScript调用错误:已阻止跨源请求:同源策略禁止读取位于......
  4. Qt之事件系统
  5. HDU5418.Victor and World(状压DP)
  6. 自定义cell相关注意事项
  7. Android小功能之垂直滚动条
  8. java数据库编程之嵌套子查询及exists的使用
  9. H5缓存
  10. Android NFC开发(一)——初探NFC,了解当前前沿技术
  11. Python基础综合运用——搭建名片管理系统
  12. Centos解除端口占用
  13. C++ 中 const、volatile、mutable的用法
  14. BZOJ4963 : String
  15. CodeChef题目选讲
  16. MFC单文档
  17. Azure SQL 数据库仓库Data Warehouse (3) DWU
  18. 分布式计算课程补充笔记 part 1
  19. 第四章,java面向对象特性
  20. 【linux】——FTP出现500 OOPS: cannot change directory的解决方法

热门文章

  1. CentOS7.X 挂载磁盘 与Mysql 自动备份
  2. Java 虚拟机垃圾收集机制详解
  3. Flink实战(102):配置(一)管理配置
  4. 【Docker】 CentOS7 安装 Docker 及其使用方法 ( 一 )
  5. Oracle10g安装
  6. john快速破解各种散列hash
  7. 理解 tf.reduce_sum(),以及tensorflow的维axis
  8. 自己整理了一个 Dapper的Helper助手类
  9. 原生js之事件解绑
  10. sqli-labs less32-37(宽字节注入)