Linux内核---21.Linux下的dnw

以前在tq2440上有linux下的dnw,不用装驱动一个ELF文件就可以执行下载.
把以前的代码拿过来修改了一下,发现还是不错的,呵呵,这下烧的时候不用插拔SD卡了! (附件中有编译好的dnw)
dusb.c
  1. /* dnw2 linux main file. This depends on libusb.
  2.  *
  3.  * Author: Fox <hulifox008@163.com>
  4.  * License: GPL
  5.  * dusb.c
  6.  */

  7. #include <stdio.h>
  8. #include <usb.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #if 1
  14. #define QQ2440_SECBULK_IDVENDOR        0x04e8
  15. #define QQ2440_SECBULK_IDPRODUCT    0x1234
  16. #else
  17. #define QQ2440_SECBULK_IDVENDOR        0x1234
  18. #define QQ2440_SECBULK_IDPRODUCT    0x04e8
  19. #endif
  20. struct usb_dev_handle * open_port() {
  21.     struct usb_bus *busses, *bus;

  22.     usb_init();
  23.     usb_find_busses();
  24.     usb_find_devices();

  25.     busses = usb_get_busses();
  26.     for (bus = busses; bus; bus = bus->next) {
  27.         struct usb_device *dev;
  28.         for (dev = bus->devices; dev; dev = dev->next) {
  29.             printf("idVendor:0x%xt,ipProduct:0x%xn",
  30.                     dev->descriptor.idVendor, dev->descriptor.idProduct);
  31.             if (QQ2440_SECBULK_IDVENDOR == dev->descriptor.idVendor
  32.                     && QQ2440_SECBULK_IDPRODUCT == dev->descriptor.idProduct) {
  33.                 printf("Target usb device found!n");
  34.                 struct usb_dev_handle *hdev = usb_open(dev);
  35.                 if (!hdev) {
  36.                     perror("Cannot open device");
  37.                 } else {
  38.                     if (!= usb_claim_interface(hdev, 0)) {
  39.                         perror("Cannot claim interface");
  40.                         usb_close(hdev);
  41.                         hdev = NULL;
  42.                     }
  43.                 }
  44.                 return hdev;
  45.             }
  46.         }
  47.     }

  48.     printf("Target usb device not found!n");

  49.     return NULL;
  50. }

  51. void usage() {
  52.     printf("Usage: dnw2 nn");
  53. }

  54. unsigned char* prepare_write_buf(char *filename, unsigned int *len) {
  55.     unsigned char *write_buf = NULL;
  56.     struct stat fs;

  57.     int fd = open(filename, O_RDONLY);
  58.     if (-== fd) {
  59.         perror("Cannot open file");
  60.         return NULL;
  61.     }
  62.     if (-== fstat(fd, &fs)) {
  63.         perror("Cannot get file size");
  64.         goto error;
  65.     }
  66.     write_buf = (unsigned char*) malloc(fs.st_size + 10);
  67.     if (NULL == write_buf) {
  68.         perror("malloc failed");
  69.         goto error;
  70.     }
  71.     if (fs.st_size != read(fd, write_buf+8, fs.st_size)) {
  72.         perror("Reading file failed");
  73.         goto error;
  74.     }
  75.     printf("Filename : %sn", filename);
  76.     printf("Filesize : 0x%x bytesn", (int)fs.st_size);

  77.     *((u_int32_t*) write_buf) = 0x30000000;              //download address:这个值写啥无所谓
  78.     *((u_int32_t*) write_buf + 1) = fs.st_size + 10;     //download size;

  79.     //USB format : addr(4)+size(4)+data(n)+cs(2)
  80.      // 主要是添加了下面这部分内容: 生成checksum值,并写到write_buf中
  81.     //如果重新编译的u-boot去掉了 #ifdef USB_CHECKSUM_EN,那么下面这个可以不加
  82.     int i;
  83.     unsigned short checkSum = 0;

  84.     unsigned char* cs_start = write_buf+8;
  85.     unsigned char* cs_end = write_buf+8+fs.st_size;
  86.     while(cs_start < cs_end) {
  87.         checkSum += *cs_start++;
  88.     }
  89.     printf("checkSum=0x%xn", checkSum);

  90.     *(unsigned short*)(write_buf+fs.st_size+8) = checkSum;

  91.     *len = fs.st_size + 10;
  92.     return write_buf;

  93.     error: if (fd != -1)
  94.         close(fd);
  95.     if (NULL != write_buf)
  96.         free(write_buf);
  97.     fs.st_size = 0;
  98.     return NULL;

  99. }

  100. int main(int argc, char *argv[]) {
  101.     if (!= argc) {
  102.         usage();
  103.         return 1;
  104.     }

  105.     struct usb_dev_handle *hdev = open_port();
  106.     if (!hdev) {
  107.         return 1;
  108.     }

  109.     unsigned int len = 0;
  110.     unsigned char* write_buf = prepare_write_buf(argv[1], &len);
  111.     if (NULL == write_buf)
  112.         return 1;

  113.     unsigned int remain = len;
  114.     unsigned int towrite;
  115.     printf("Writing data ...n");
  116.     while (remain) {
  117.         towrite = remain > 512 ? 512 : remain;
  118.         //端点与TQ2440不一样是个二
  119.         //if (towrite != usb_bulk_write(hdev, 0x03, write_buf + (len - remain), towrite, 3000)) {
  120.         if (towrite != usb_bulk_write(hdev, 0x02, write_buf + (len - remain), towrite, 3000)) {
  121.             perror("usb_bulk_write failed");
  122.             break;
  123.         }
  124.         remain -= towrite;
  125.         printf("r%dt %d bytes ", (len-remain)*100/len, len-remain);
  126.         fflush(stdout);
  127.     }
  128.     if (== remain)
  129.         printf("Done!n");
  130.     return 0;
  131. }

Makefile:
  1. dusb:dusb.c
  2.     gcc --o $@ $< -lusb
  3. clean:
  4.     rm -rf ./*.o dusb
如果提示找不到头文件,
fatal error: usb.h: No such file or directory
这可能是libusb库没有安装

sudo apt-get install libusb-dev

附件是编译好的dnw与源码
dusb_ok6410.rar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值