USB Development Notes

Source: Internet
Author: User

The USB stick should belong to the mass storage class. 
USB mass storage devices, including Universal mass Storage sub-class, Cdrom,tape and so on, the U disk is actually a mass storage class in the general mass storage subclass . Universal mass storage devices are implemented on devices that are based on block/sector storage .
The USB organization defines the specifications for the mass storage device class, which includes 4 separate subclass specifications . Mainly refers to the transmission method on the USB bus and the operation command of the storage medium.
a mass storage device supports only one interface, the data interface, which has three endpoints Bulk input, Bulk output, and interrupts the endpoint. The interface of this device uses SCSI-2 's direct Access device protocol , and the media on the USB device is addressed using the same logical block as the SCSI-2.
second, bulk-only transmission protocol
when a USB flash drive is inserted into the host, the host will ask the USB device to return their descriptors, and when the hosts get these descriptors, the configuration of the device is completed. Identifies the USB device as a mass storage device that supports the BULK-ONLY transport protocol. The bulk-only transmission mode should be available at this time. In this way, the data transfer between USB and the device is achieved through bulk-in and bulk-out.
in this mode of transmission, there are three types of data transmitted on USB and devices , which are command block packages (CBW), Command execution status packs (CSW), and normal packets . CBW is the command sent to the device by the host. The format is as follows:

where the value of Dcbwsignature is 43425355h, which indicates that a CBW is currently being sent.
Dcbwtag: a command block ID sent by the host, the device needs to be sent as Dcswtag (part of CSW) and sent to host, mainly for associating CSW to the corresponding CBW.
dcbwdatatransferlength: Indicates the length of the data to be transmitted CBW this time.
Bmcbwflags: Indicates whether this CBW is read data or write number
bcwcblength: Indicates the length of the command.
CBWCB: Indicates the contents of this command. This is also the SCSI command.
when the device receives the CBW block from the host, it separates the SCSI command from the CBW and executes as required, and the result is sent to the host in CSW form.

the CSW format is as follows:

where the value of Dcswsignature is 53425355h, which indicates that a CSW is currently being sent.
Dcswtag: Must be the same as Dcbwtag in CBW.
Dcswdataresidue: The data to be transferred.
bcswstatue: The command execution state, when the command executes correctly, is 0.

third, SCSI command set

In the bulk-only command block Package (CBW), there is a CBECB content, which is the SCSI command block descriptor. The contents are as follows:
Operation code: It is the SCSI command operation codes.
Logical Block Address: Logical block addresses, which should be sectors for USB drives. As already mentioned: the Universal mass storage device is a block/sector-based storage device, so it is obvious that this parameter is provided in SCSI.
Transfer Length: Number of sectors to be transferred

There are many transfer commands for storage media of the direct access type in SCSI, such as:
INQUIRY: Its operation code is 12H
Test Unit ready: Its opcode is 00H
Format Unit: The opcode is 04H
.......


develop a USB flash drive that can run programs automatically
First, why to develop such a U disk
can automatically run the program's USB flash drive, inserted into the computer after the program will automatically run, such a U disk can be used as a software vendor storage program carrier, can also be used as a U disk manufacturer to extend the function of the U disk. For example: a USB flash drive with encryption can be stored in the encryption software to the USB flash drive, after inserting a USB stick to automatically run the encryption management software, greatly facilitate the user, the manufacturers do not have to provide additional software; I even think of the temperature chip into the U disk circuit, the program automatically run on the USB interface, read the temperature , show to the user:) as long as you think, such a U disk can also do a lot of things.

Second, the development of ideas
We know that the CD-ROM can be run automatically, so we canto simulate a region of a USB flash drive into a discThe form. This way we can use Autorun.inf to let the program run automatically.

Third, the development process
1. Make USB drive A zone appear as a disc
This should be very simple for the person who developed the USB stick, that is, when the operating system sends the SCSI device's INQUIRY instruction,The returned inquiry indicates the device type, which we set as the CD device。 Specific instructions can refer to the relevant information (spc3r23.pdf). When I developed it, inquiry returned the first few bytes of data as follows: 0X05,0X80,0X02,0X02 is for reference only.
2. Respond to operating system instructions for this disc area
into a disc,the operating system may send some SCSI instructions to this disc area. Should respond to certain instructions, some do not need to respond。 In the back I will be an appendix A USB flash drive Bushound monitoring file, you can refer to. It is best to buy a USB flash drive and then use Bushound to monitor the communication process.
3. Writing files to the disc area
It should be okturn the disc area into a CDRW ., so you can write directly, but you need to know a lot about protocols. Because of the time, I used a simple method,make an ISO image file of the file that needs to be written to the CD with the ISO tool, and write a small program to write the ISO image file to this disc area。 The program part code is attached to the document and is for informational purposes only. The written program eventually calls the DeviceIoControl function. After the file is written, reseat the USB drive.
Iv. description
Here is just a simple explanation of the principle, if it is just contact with this, there are a lot of things to figure out, I was in someone else a common U disk Development Board based on the addition of this function, the last good grasp, it is not easy, and did not get hardware firmware program. Hope to be helpful to beginners. If you have any questions, you can contact us by email.
Appendix:
1. Write ISO Program part code:
UINT cwriteisofiledlg::threadwrite (lpvoid pparam) {Cwriteisofiledlg* p = (cwriteisofiledlg*) Pparam; Csdsk SD; CString str; HANDLE Fh,hfind; Win32_find_data Findfiledata; ULONGLONG Fsize=0; CFileFind FF; DWORD startaddr=0; DWORD Nbytesread=0; Pbyte buf=Newbyte[20480]; memset (BUF,0,20480); Try {     if(!ff. FindFile (p->filename)) {str. Format ("The file%s could not be found",p->filename); Throw 1; } ff.     FindNextFile (); FH=CreateFile (ff.     GetFilePath (), generic_read,file_share_read,null,open_existing,file_attribute_normal,null); if(fh==Invalid_handle_value) {str. Format ("failed to open file%s",p->filename); Throw 1; }     if(!SD. Open ())Throw 0; intindex = SD. Getdevnum ()-1; if(!SD. Readcapacity (Index,0, Fsize)) Throw 0; if(ff. GetLength () >fsize) {STR="There is not enough disk space to write to. "; Throw 1; } fsize=ff.     GetLength (); if(!SD. Getflashparam (Index,0,1, BUF)) Throw 0; buf[0]&=0xBF; if(!SD. Setflashparam (Index,0,1, BUF,false))     Throw 0;  while(1)     {         if(ReadFile (Fh,buf,20480,&nbytesread,null)) {             if(nbytesread!=0)             {                 if(!SD. Write (Index,0, STARTADDR, +, BUF)) {                     Throw 0; } startaddr+= +; memset (BUF,0,20480); }             Else              Break; }         Else{str="Read File Error! "; Throw 1; }     }     //P->messagebox ("Write image file succeeded! ", null,mb_ok| Mb_iconinformation); Sd. Plugdisk (Index,0,true);     delete [] buf; Exit (0); } Catch(inte) {if(e==0) {p->messagebox (SD.ERR.GETERRMSG (), null,mb_ok|mb_iconerror); }     ElseP->messagebox (str,null,mb_ok|mb_iconerror);     } delete [] buf; Exit (-1); return 1; }












USB Development Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.