请选择 进入手机版 | 继续访问电脑版

雨林木风

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 52|回复: 0

QQ2440开发板在Ubuntu下下载内核

[复制链接]

6401

主题

6409

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
19783
发表于 2019-12-13 09:07:32 | 显示全部楼层 |阅读模式
  在位的好友的指导下,我的Ubuntu终于能通过USB下载东西了!在这里说一下步骤,方便自己也希望对大家有好处!
  一、下载usb驱动: apt-get install libusb-dev
  二、编译DNW2,gcc -o dnw2 -lusb
  呵呵,DNW2就会在当前目录下生成!
  文件下载:
  ./dnw2 file
  code:
  #include
  #include
  #include
  #include
  #include
  #include
  #define QQ2440_SECBULK_IDVENDOR 0x5345
  #define QQ2440_SECBULK_IDPRODUCT 0x1234
  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( 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 \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;
  }
  printf("Filename : %s\n", filename);
  printf("Filesize : %d bytes\n", fs.st_size);
  *((u_int32_t*)write_buf) = 0x30000000; //download address
  *((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;
  *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, 0x03, 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;
  }
  本代码从网上下载,别恶意!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

|appname
快速回复 返回顶部 返回列表