⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mx1-load-usb.c

📁 mx1ads 的bootloader下载程序
💻 C
字号:
/*******************************************************************  MX1-Loader - DragonBall M9328/MX1 loader  mx1-load-usb.c  - USB communication with target  (C) Copyright 2004 by Pavel Pisa - project originator        http://cmp.felk.cvut.cz/~pisa  (C) Copyright 2004 PiKRON Ltd.        http://www.pikron.com  (C) Copyright 2004 Roman Bartosinski        (bartosr@centrum.cz)  The MX1-Loader project can be used and distributed  in compliance with any of next licenses   - GPL - GNU Public License     See file COPYING for details.   - LGPL - Lesser GNU Public License   - MPL - Mozilla Public License   - and other licenses added by project originator  Code can be modified and re-distributed under any combination  of the above listed licenses. If contributor does not agree with  some of the licenses, he/she can delete appropriate line.  WARNING: if you delete all lines, you are not allowed to  distribute code or sources in any form. *******************************************************************/#define _GNU_SOURCE#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <termios.h>#include <sys/time.h>#include <sys/types.h>#include <errno.h>#include <unistd.h>#include <fcntl.h>#include "universal-load.h"#include "usb_loader.h"#define USB_TIMEOUT 500int verbose=0;/*****************************************************************************//*****************************************************************************//*****************************************************************************//* USB functions */void print_devices(void){  struct usb_bus *bus;  struct usb_device *dev;  int i = 0;  usb_init(); // NO for more devices  usb_find_busses();  usb_find_devices();  printf("All connected usb devices\n");  printf("  bus/device    idVendor/idProduct\n");  for (bus = usb_busses; bus; bus = bus->next) {    for (dev = bus->devices; dev; dev = dev->next) {      i++;      printf("    %s/%s     0x%04X/0x%04X\n", bus->dirname, dev->filename, dev->descriptor.idVendor, dev->descriptor.idProduct);    }  }  if ( !i)    printf(" -- no device.\n");}struct usb_device *find_usb_device(int vendor, int product){  struct usb_bus *bus;  struct usb_device *dev;  for (bus = usb_busses; bus; bus = bus->next) {    for (dev = bus->devices; dev; dev = dev->next) {      if((dev->descriptor.idVendor==vendor)&&         (dev->descriptor.idProduct)==product)         return dev;    }  }  return NULL;}usb_dev_handle *usb_open_device( int uvid, int upid) {  struct usb_device *dev;  usb_dev_handle *hdev;  usb_init(); // NO for more devices  usb_find_busses();  usb_find_devices();  dev = find_usb_device( uvid, upid);  if( !dev) {    if( verbose) printf( "!!! Cannot find device 0x%04X:0x%04X\n", uvid, upid);    return NULL;  }  if (( hdev = usb_open( dev)) == NULL) {    if ( verbose) printf( "!!! USB device wasn't opened !!!\n");    return NULL;  }  usb_claim_interface( hdev, 0);  if ( verbose) printf(" USB Device 0x%04X:0x%04X '%s' is open.\n", uvid, upid, dev->filename);  return hdev;}int usb_close_device( usb_dev_handle *hdev) {  int bRes=1;  usb_release_interface( hdev, 0);  bRes = usb_close( hdev);  if ( bRes && verbose)    printf( "!!! USB Device wasn't closed !!!\n");  return bRes;}/*****************************************************************************//*****************************************************************************//*****************************************************************************/int uniusb_setup(uniload_alg_info_t *algi){  /*usb_dev_handle *hdev=algi->usb_hdev;*/  return 0;}int uniusb_mem_write(uniload_alg_info_t *algi, int mem_type, unsigned long start, unsigned long len, const char *buff){  usb_dev_handle *hdev=algi->usb_hdev;  int res;  int blen=1024;  int i;  int cnt;    while(len){    cnt=(len>blen)?blen:len;    i = 3;    do {      res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USB_VENDOR_GET_SET_MEMORY | mem_type /*USB_VENDOR_TARGET_XDATA*/,                            (unsigned long)(start) & 0xffff, (((unsigned long)start) >> 16) & 0xffff, (void *)buff, len, 150+len); //USB_TIMEOUT);//      res = usb_bulk_write( hdev, USB_ENDPOINT_IN | 0x02, buf, len, 1000);      i--;    } while ( res<0 && i);    if(res<0){      fprintf(stderr,"USB write data failed %d\n",res);      return res;    }        start+=cnt;    len-=cnt;    buff+=cnt;  }        return 0;}int uniusb_mem_read(uniload_alg_info_t *algi, int mem_type, unsigned long start, unsigned long len, char *buff){  usb_dev_handle *hdev=algi->usb_hdev;  int res;  int blen=1024;  int i;  int cnt;    while(len){    cnt=(len>blen)?blen:len;    i = 3;    do {      res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_VENDOR_GET_SET_MEMORY | mem_type /*USB_VENDOR_TARGET_XDATA*/,                            (unsigned long)(start) & 0xffff, (((unsigned long)start) >> 16) & 0xffff, (void *)buff, len, 100+len); //USB_TIMEOUT);//      res = usb_bulk_write( hdev, USB_ENDPOINT_IN | 0x02, buf, len, 1000);      i--;    } while ( res<0 && i);    if(res<0){      fprintf(stderr,"USB read data failed %d\n",res);      return res;    }        start+=cnt;    len-=cnt;    buff+=cnt;  }        return 0;}int uniusb_go_addr(uniload_alg_info_t *algi, unsigned long addr){  usb_dev_handle *hdev=algi->usb_hdev;  int res;  res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,           USB_VENDOR_GOTO, (unsigned long)addr & 0xffff, ((unsigned long)addr >> 16) & 0xffff, NULL, 0, USB_TIMEOUT);  if(res<0) fprintf(stderr,"Goto to %4lX ERROR %d\n",addr,res);       else fprintf(stderr,"Goto to %4lX OK\n",addr);  return 0;}int uniusb_masserase(uniload_alg_info_t *algi, unsigned long mode){  usb_dev_handle *hdev=algi->usb_hdev;  int res;  res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,          USB_VENDOR_MASS_ERASE, mode & 0xffff, (mode >> 16) & 0xffff, NULL, 0, USB_TIMEOUT*20);  if(res<0) {    printf("Mass Erase to %4lX ERROR %d\n",mode,res);    return -1;  }  return 0;}int uniusb_regerase(uniload_alg_info_t *algi, unsigned long addr, unsigned long len){   usb_dev_handle *hdev=algi->usb_hdev;  int res;  if(addr+len<0x10000) {    res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,            USB_VENDOR_ERASE_MEMORY, addr, len, NULL, 0, USB_TIMEOUT*5);  } else {    len += addr & 0x3ff;    res = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,            USB_VENDOR_ERASE_1KB_MEMORY, addr>>10, len>>10, NULL, 0, USB_TIMEOUT*10);  }  return 0;}int uniusb_release(uniload_alg_info_t *algi){  usb_dev_handle *hdev=algi->usb_hdev;  int res;  res = usb_close_device(hdev);    return res;}uniload_alg_ops_t uniusb_alg_ops={  .setup=uniusb_setup,  .mem_write=uniusb_mem_write,  .mem_read=uniusb_mem_read,  .go_addr=uniusb_go_addr,  .release=uniusb_release,  .masserase=uniusb_masserase,  .regerase=uniusb_regerase,};int mx1_usb_open(uniload_alg_info_t *algi, char *dev_name){  usb_dev_handle *hdev=algi->usb_hdev;  int uvid=0;  int upid=0;  char *p, *r;    p=dev_name;  if(*p!=':') {    if((p[0]=='0')&&(p[1]=='x'))      p+=2;    uvid=strtoul(p,&r,16);    if(p==r)      return -1;    p=r;    if(*p!=':')      return -1;  }  p++;  if(*p) {      if((p[0]=='0')&&(p[1]=='x'))    p+=2;      upid=strtoul(p,&r,16);    if(p==r)      return -1;  }  hdev=usb_open_device(uvid,upid);  if(!hdev)    return -1;  algi->usb_hdev=hdev;  algi->dev_name=dev_name;  algi->alg_ops=&uniusb_alg_ops;    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -