以前在tq2440上有linux下的dnw,不用装驱动一个ELF文件就可以执行下载.
把以前的代码拿过来修改了一下,发现还是不错的,呵呵,这下烧的时候不用插拔SD卡了! (附件中有编译好的dnw)
dusb.c
-
/* dnw2 linux main file. This depends on libusb.
-
*
-
* Author: Fox <hulifox008@163.com>
-
* License: GPL
-
* dusb.c
-
*/
-
-
#include <stdio.h>
-
#include <usb.h>
-
#include <errno.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
#if 1
-
#define QQ2440_SECBULK_IDVENDOR 0x04e8
-
#define QQ2440_SECBULK_IDPRODUCT 0x1234
-
#else
-
#define QQ2440_SECBULK_IDVENDOR 0x1234
-
#define QQ2440_SECBULK_IDPRODUCT 0x04e8
-
#endif
-
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) {
-
printf("idVendor:0x%xt,ipProduct:0x%xn",
-
dev->descriptor.idVendor, dev->descriptor.idProduct);
-
if (QQ2440_SECBULK_IDVENDOR == dev->descriptor.idVendor
-
&& QQ2440_SECBULK_IDPRODUCT == 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 nn");
-
}
-
-
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;
-
}
-
printf("Filename : %sn", filename);
-
printf("Filesize : 0x%x bytesn", (int)fs.st_size);
-
-
*((u_int32_t*) write_buf) = 0x30000000; //download address:这个值写啥无所谓
-
*((u_int32_t*) write_buf + 1) = fs.st_size + 10; //download size;
-
-
//USB format : addr(4)+size(4)+data(n)+cs(2)
-
// 主要是添加了下面这部分内容: 生成checksum值,并写到write_buf中
-
//如果重新编译的u-boot去掉了 #ifdef USB_CHECKSUM_EN,那么下面这个可以不加
-
int i;
-
unsigned short checkSum = 0;
-
-
unsigned char* cs_start = write_buf+8;
-
unsigned char* cs_end = write_buf+8+fs.st_size;
-
while(cs_start < cs_end) {
-
checkSum += *cs_start++;
-
}
-
printf("checkSum=0x%xn", checkSum);
-
-
*(unsigned short*)(write_buf+fs.st_size+8) = checkSum;
-
-
*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;
-
//端点与TQ2440不一样是个二
-
//if (towrite != usb_bulk_write(hdev, 0x03, write_buf + (len - remain), towrite, 3000)) {
-
if (towrite != usb_bulk_write(hdev, 0x02, write_buf + (len - remain), towrite, 3000)) {
-
perror("usb_bulk_write failed");
-
break;
-
}
-
remain -= towrite;
-
printf("r%dt %d bytes ", (len-remain)*100/len, len-remain);
-
fflush(stdout);
-
}
-
if (0 == remain)
-
printf("Done!n");
-
return 0;
-
}
Makefile:
-
dusb:dusb.c
-
gcc -g -o $@ $< -lusb
-
clean:
-
rm -rf ./*.o dusb
如果提示找不到头文件,
fatal error: usb.h: No such file or directory
这可能是libusb库没有安装
sudo apt-get install libusb-dev
附件是编译好的dnw与源码
dusb_ok6410.rar
阅读(1932) | 评论(0) | 转发(0) |