Under the guidance of a friend in place, my Ubuntu can finally download something through USB! Let's talk about the steps here to make it easier for you and hope it will be good for you! 1. Download the usb driver: apt-getinstalllibusb-dev 2. Compile DNW2, gcc-odnw2-lusb, and DNW2 will be generated under the current directory! File Download:./dnw2filecode: # include & lt; stdio. h & g
Under the guidance of a friend in place, my Ubuntu can finally download something through USB! Let's talk about the steps here to make it easier for you and hope it will be good for you!
1. Download the usb driver: apt-get install libusb-dev
2. Compile DNW2 and gcc-o dnw2-lusb
Haha, DNW2 will be generated in the current directory!
File Download:
./Dnw2 file
Code:
# Include
# Include
# Include
# Include
# Include
# Include
# Define qq2440_secbulk_idvender 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-> next)
{
Struct usb_device * dev;
For (dev = bus-> devices; 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 ");
}
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;
}
This code is downloaded from the Internet. Do not be malicious!