In the "USB series three", we implemented a series of SCSI commands, in this series, we want to implement the command to write sector to the U disk, so this article is relatively easy, more is to give a realization of the source program.
In the third USB series, the SCSI commands we implement are: INQUIRY, read capacity (10), TEST UNIT ready, REQUEST sense, read (10), all the commands that are read out, so it doesn't break the contents of the USB stick, on page 29th of document SBC-2, there is a table of SCSI commands, in which all commands are listed, and the type "M" is the command that SCSI devices must implement, which are:
Num |
Command Name |
Operation Code |
Type |
Reference |
1 |
FORMAT UNIT |
04h |
M |
SBC-2 |
2 |
INQUIRY |
12h |
M |
SPC-3 |
3 |
READ (6) |
08h |
M |
SBC-2 |
4 |
READ (10) |
28h |
M |
SBC-2 |
5 |
READ (16) |
88h |
M |
SBC-2 |
6 |
READ Capacity (10) |
25h |
M |
SBC-2 |
7 |
READ Capacity (16) |
9eh/10h |
M |
SBC-2 |
8 |
REQUEST Sense |
03h |
M |
SPC-3 |
9 |
SEND DIAGNOSTIC |
1Dh |
M |
SPC-3 |
10 |
TEST UNIT Ready |
00h |
M |
SPC-3 |
11 |
WRITE (10) |
2Ah |
O |
SBC-2 |
The last command in this is not SBC-2 in the mandatory implementation, but optional, but if we do not implement, the operation of the U disk will be a lot more pale; we are not going to implement commands 1, 3, 5, 7, and 9, read (6), read (16), and read (10) are very similar, Only the length of the LBA is different, if need to implement, refer to read (10) on it, format and send diagnostic two commands on the use of the chip of the USB stick is meaningless, of course, the hard disk is meaningful, so in this article, We just have to implement a very important wrte (10), write data to the USB stick, we need to prepare a USB stick without useful data, because we want to change the content.
WRITE (10) Source code: Http://blog.hengch.com/source/usb-write.zip
program, we to the "USB series three" as in the program, reset, and then get the largest LUN, this step is not necessary, and then we send a write (10) command to the device, note that this is an out transaction, so, cbw_flags= 0x00 instead of 0x80 as before, after issuing the write (10) command, we also send the data to the device to be written, 64 bytes each, a sector 512 bytes need to start 8 out transactions, this work and function putdata complete, Each time the 64 bytes sent we write to the 0--63, in the program, we write this data into the lba=100 sector, after writing, we use the "USB series three" described in the Read (10) command to read the same sector, we will see the results we want, Since we have completely cleared the buffer to 0 before reading it, we are confident that the data we read is true.
Here, we have the main command to control the U disk has been introduced, using DOSUSB, we have the possibility to write a simple driver for the U disk, but maybe we do not know how to write a DOS driver, starting from the next article, we will temporarily put down the USB series of articles, Introduce the DOS driver's wording.
USB Series Four: Write data to the USB drive