![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Student project by David Berlin and Boris Dogadov made for the Embedded Systems Workshop course given in Tel-Aviv University on 2010 by Sivan Toledo. Visit the project website for more details: http://davidberlin.co.il/sadna/ .
Dependencies: EthernetNetIf NTPClient_NetServices mbed HTTPServer HTTPClient CyaSSL
MassStorage/MassStorage.cpp@1:b05231650f32, 2011-04-25 (annotated)
- Committer:
- sivan_toledo
- Date:
- Mon Apr 25 12:31:46 2011 +0000
- Revision:
- 1:b05231650f32
- Parent:
- 0:3e7d6f496a67
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sivan_toledo | 0:3e7d6f496a67 | 1 | |
sivan_toledo | 0:3e7d6f496a67 | 2 | /* |
sivan_toledo | 0:3e7d6f496a67 | 3 | Copyright (c) 2010 Peter Barrett |
sivan_toledo | 0:3e7d6f496a67 | 4 | |
sivan_toledo | 0:3e7d6f496a67 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
sivan_toledo | 0:3e7d6f496a67 | 6 | of this software and associated documentation files (the "Software"), to deal |
sivan_toledo | 0:3e7d6f496a67 | 7 | in the Software without restriction, including without limitation the rights |
sivan_toledo | 0:3e7d6f496a67 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
sivan_toledo | 0:3e7d6f496a67 | 9 | copies of the Software, and to permit persons to whom the Software is |
sivan_toledo | 0:3e7d6f496a67 | 10 | furnished to do so, subject to the following conditions: |
sivan_toledo | 0:3e7d6f496a67 | 11 | |
sivan_toledo | 0:3e7d6f496a67 | 12 | The above copyright notice and this permission notice shall be included in |
sivan_toledo | 0:3e7d6f496a67 | 13 | all copies or substantial portions of the Software. |
sivan_toledo | 0:3e7d6f496a67 | 14 | |
sivan_toledo | 0:3e7d6f496a67 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
sivan_toledo | 0:3e7d6f496a67 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
sivan_toledo | 0:3e7d6f496a67 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
sivan_toledo | 0:3e7d6f496a67 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
sivan_toledo | 0:3e7d6f496a67 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
sivan_toledo | 0:3e7d6f496a67 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
sivan_toledo | 0:3e7d6f496a67 | 21 | THE SOFTWARE. |
sivan_toledo | 0:3e7d6f496a67 | 22 | */ |
sivan_toledo | 0:3e7d6f496a67 | 23 | |
sivan_toledo | 0:3e7d6f496a67 | 24 | #include "stdlib.h" |
sivan_toledo | 0:3e7d6f496a67 | 25 | #include "stdio.h" |
sivan_toledo | 0:3e7d6f496a67 | 26 | #include "string.h" |
sivan_toledo | 0:3e7d6f496a67 | 27 | |
sivan_toledo | 0:3e7d6f496a67 | 28 | #include "Utils.h" |
sivan_toledo | 0:3e7d6f496a67 | 29 | #include "USBHost.h" |
sivan_toledo | 0:3e7d6f496a67 | 30 | |
sivan_toledo | 0:3e7d6f496a67 | 31 | |
sivan_toledo | 0:3e7d6f496a67 | 32 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); |
sivan_toledo | 0:3e7d6f496a67 | 33 | int MassStorage_ReadBlock(int device, u32 block, u8* dst); |
sivan_toledo | 0:3e7d6f496a67 | 34 | int MassStorage_WriteBlock(int device, u32 block, const u8* dst); |
sivan_toledo | 0:3e7d6f496a67 | 35 | |
sivan_toledo | 0:3e7d6f496a67 | 36 | |
sivan_toledo | 0:3e7d6f496a67 | 37 | #define ERR_BAD_CSW_SIGNATURE -200 |
sivan_toledo | 0:3e7d6f496a67 | 38 | |
sivan_toledo | 0:3e7d6f496a67 | 39 | #define CBW_SIGNATURE 0x43425355 |
sivan_toledo | 0:3e7d6f496a67 | 40 | #define CSW_SIGNATURE 0x53425355 |
sivan_toledo | 0:3e7d6f496a67 | 41 | |
sivan_toledo | 0:3e7d6f496a67 | 42 | // Command Block |
sivan_toledo | 0:3e7d6f496a67 | 43 | typedef struct |
sivan_toledo | 0:3e7d6f496a67 | 44 | { |
sivan_toledo | 0:3e7d6f496a67 | 45 | u32 Signature; |
sivan_toledo | 0:3e7d6f496a67 | 46 | u32 Tag; |
sivan_toledo | 0:3e7d6f496a67 | 47 | u32 TransferLength; |
sivan_toledo | 0:3e7d6f496a67 | 48 | u8 Flags; |
sivan_toledo | 0:3e7d6f496a67 | 49 | u8 LUN; |
sivan_toledo | 0:3e7d6f496a67 | 50 | u8 CBLength; |
sivan_toledo | 0:3e7d6f496a67 | 51 | u8 CB[16]; // only 6 really |
sivan_toledo | 0:3e7d6f496a67 | 52 | } CBW; |
sivan_toledo | 0:3e7d6f496a67 | 53 | |
sivan_toledo | 0:3e7d6f496a67 | 54 | // Status block |
sivan_toledo | 0:3e7d6f496a67 | 55 | typedef struct |
sivan_toledo | 0:3e7d6f496a67 | 56 | { |
sivan_toledo | 0:3e7d6f496a67 | 57 | u32 Signature; |
sivan_toledo | 0:3e7d6f496a67 | 58 | u32 Tag; |
sivan_toledo | 0:3e7d6f496a67 | 59 | u32 DataResidue; |
sivan_toledo | 0:3e7d6f496a67 | 60 | u8 Status; |
sivan_toledo | 0:3e7d6f496a67 | 61 | } CSW; |
sivan_toledo | 0:3e7d6f496a67 | 62 | |
sivan_toledo | 0:3e7d6f496a67 | 63 | int SCSIRequestSense(int device); |
sivan_toledo | 0:3e7d6f496a67 | 64 | |
sivan_toledo | 0:3e7d6f496a67 | 65 | int DoSCSI(int device, const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) |
sivan_toledo | 0:3e7d6f496a67 | 66 | { |
sivan_toledo | 0:3e7d6f496a67 | 67 | CBW cbw; |
sivan_toledo | 0:3e7d6f496a67 | 68 | cbw.Signature = CBW_SIGNATURE; |
sivan_toledo | 0:3e7d6f496a67 | 69 | cbw.Tag = 0; |
sivan_toledo | 0:3e7d6f496a67 | 70 | cbw.TransferLength = transferLen; |
sivan_toledo | 0:3e7d6f496a67 | 71 | cbw.Flags = flags; |
sivan_toledo | 0:3e7d6f496a67 | 72 | cbw.LUN = 0; |
sivan_toledo | 0:3e7d6f496a67 | 73 | cbw.CBLength = cmdLen; |
sivan_toledo | 0:3e7d6f496a67 | 74 | memset(cbw.CB,0,sizeof(cbw.CB)); |
sivan_toledo | 0:3e7d6f496a67 | 75 | memcpy(cbw.CB,cmd,cmdLen); |
sivan_toledo | 0:3e7d6f496a67 | 76 | |
sivan_toledo | 0:3e7d6f496a67 | 77 | int r; |
sivan_toledo | 0:3e7d6f496a67 | 78 | r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command |
sivan_toledo | 0:3e7d6f496a67 | 79 | if (r < 0) |
sivan_toledo | 0:3e7d6f496a67 | 80 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 81 | |
sivan_toledo | 0:3e7d6f496a67 | 82 | if (data) |
sivan_toledo | 0:3e7d6f496a67 | 83 | { |
sivan_toledo | 0:3e7d6f496a67 | 84 | r = USBBulkTransfer(device,flags | 1,data,transferLen); |
sivan_toledo | 0:3e7d6f496a67 | 85 | if (r < 0) |
sivan_toledo | 0:3e7d6f496a67 | 86 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 87 | } |
sivan_toledo | 0:3e7d6f496a67 | 88 | |
sivan_toledo | 0:3e7d6f496a67 | 89 | CSW csw; |
sivan_toledo | 0:3e7d6f496a67 | 90 | csw.Signature = 0; |
sivan_toledo | 0:3e7d6f496a67 | 91 | r = USBBulkTransfer(device,0x81,(u8*)&csw,13); |
sivan_toledo | 0:3e7d6f496a67 | 92 | if (r < 0) |
sivan_toledo | 0:3e7d6f496a67 | 93 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 94 | |
sivan_toledo | 0:3e7d6f496a67 | 95 | if (csw.Signature != CSW_SIGNATURE) |
sivan_toledo | 0:3e7d6f496a67 | 96 | return ERR_BAD_CSW_SIGNATURE; |
sivan_toledo | 0:3e7d6f496a67 | 97 | |
sivan_toledo | 0:3e7d6f496a67 | 98 | // ModeSense? |
sivan_toledo | 0:3e7d6f496a67 | 99 | if (csw.Status == 1 && cmd[0] != 3) |
sivan_toledo | 0:3e7d6f496a67 | 100 | return SCSIRequestSense(device); |
sivan_toledo | 0:3e7d6f496a67 | 101 | |
sivan_toledo | 0:3e7d6f496a67 | 102 | return csw.Status; |
sivan_toledo | 0:3e7d6f496a67 | 103 | } |
sivan_toledo | 0:3e7d6f496a67 | 104 | |
sivan_toledo | 0:3e7d6f496a67 | 105 | int SCSITestUnitReady(int device) |
sivan_toledo | 0:3e7d6f496a67 | 106 | { |
sivan_toledo | 0:3e7d6f496a67 | 107 | u8 cmd[6]; |
sivan_toledo | 0:3e7d6f496a67 | 108 | memset(cmd,0,6); |
sivan_toledo | 0:3e7d6f496a67 | 109 | return DoSCSI(device,cmd,6,DEVICE_TO_HOST,0,0); |
sivan_toledo | 0:3e7d6f496a67 | 110 | } |
sivan_toledo | 0:3e7d6f496a67 | 111 | |
sivan_toledo | 0:3e7d6f496a67 | 112 | int SCSIRequestSense(int device) |
sivan_toledo | 0:3e7d6f496a67 | 113 | { |
sivan_toledo | 0:3e7d6f496a67 | 114 | u8 cmd[6] = {0x03,0,0,0,18,0}; |
sivan_toledo | 0:3e7d6f496a67 | 115 | u8 result[18]; |
sivan_toledo | 0:3e7d6f496a67 | 116 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,18); |
sivan_toledo | 0:3e7d6f496a67 | 117 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 118 | } |
sivan_toledo | 0:3e7d6f496a67 | 119 | |
sivan_toledo | 0:3e7d6f496a67 | 120 | int SCSIInquiry(int device) |
sivan_toledo | 0:3e7d6f496a67 | 121 | { |
sivan_toledo | 0:3e7d6f496a67 | 122 | u8 cmd[6] = {0x12,0,0,0,36,0}; |
sivan_toledo | 0:3e7d6f496a67 | 123 | u8 result[36+2]; |
sivan_toledo | 0:3e7d6f496a67 | 124 | result[36] = '\n'; |
sivan_toledo | 0:3e7d6f496a67 | 125 | result[37] = 0; |
sivan_toledo | 0:3e7d6f496a67 | 126 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,36); |
sivan_toledo | 0:3e7d6f496a67 | 127 | if (r == 0) |
sivan_toledo | 0:3e7d6f496a67 | 128 | printf((const char*)result + 8); |
sivan_toledo | 0:3e7d6f496a67 | 129 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 130 | } |
sivan_toledo | 0:3e7d6f496a67 | 131 | |
sivan_toledo | 0:3e7d6f496a67 | 132 | int SCSIReadCapacity(int device, u32* blockCount, u32* blockSize) |
sivan_toledo | 0:3e7d6f496a67 | 133 | { |
sivan_toledo | 0:3e7d6f496a67 | 134 | u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0}; |
sivan_toledo | 0:3e7d6f496a67 | 135 | u8 result[8]; |
sivan_toledo | 0:3e7d6f496a67 | 136 | *blockSize = 0; |
sivan_toledo | 0:3e7d6f496a67 | 137 | *blockCount = 0; |
sivan_toledo | 0:3e7d6f496a67 | 138 | int r = DoSCSI(device,cmd,10,DEVICE_TO_HOST,result,8); |
sivan_toledo | 0:3e7d6f496a67 | 139 | if (r == 0) |
sivan_toledo | 0:3e7d6f496a67 | 140 | { |
sivan_toledo | 0:3e7d6f496a67 | 141 | *blockCount = BE32(result); |
sivan_toledo | 0:3e7d6f496a67 | 142 | *blockSize = BE32(result+4); |
sivan_toledo | 0:3e7d6f496a67 | 143 | } |
sivan_toledo | 0:3e7d6f496a67 | 144 | return r; |
sivan_toledo | 0:3e7d6f496a67 | 145 | } |
sivan_toledo | 0:3e7d6f496a67 | 146 | |
sivan_toledo | 0:3e7d6f496a67 | 147 | int SCSITransfer(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) |
sivan_toledo | 0:3e7d6f496a67 | 148 | { |
sivan_toledo | 0:3e7d6f496a67 | 149 | // USB hardware will only do 4k per transfer |
sivan_toledo | 0:3e7d6f496a67 | 150 | while (blockCount*blockSize > 4096) |
sivan_toledo | 0:3e7d6f496a67 | 151 | { |
sivan_toledo | 0:3e7d6f496a67 | 152 | int count = 4096/blockSize; |
sivan_toledo | 0:3e7d6f496a67 | 153 | int r = SCSITransfer(device,blockAddr,count,dst,blockSize,direction); |
sivan_toledo | 0:3e7d6f496a67 | 154 | dst += count*blockSize; |
sivan_toledo | 0:3e7d6f496a67 | 155 | blockAddr += count; |
sivan_toledo | 0:3e7d6f496a67 | 156 | blockCount -= count; |
sivan_toledo | 0:3e7d6f496a67 | 157 | } |
sivan_toledo | 0:3e7d6f496a67 | 158 | |
sivan_toledo | 0:3e7d6f496a67 | 159 | u8 cmd[10]; |
sivan_toledo | 0:3e7d6f496a67 | 160 | memset(cmd,0,10); |
sivan_toledo | 0:3e7d6f496a67 | 161 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
sivan_toledo | 0:3e7d6f496a67 | 162 | BE32(blockAddr,cmd+2); |
sivan_toledo | 0:3e7d6f496a67 | 163 | BE16(blockCount,cmd+7); |
sivan_toledo | 0:3e7d6f496a67 | 164 | return DoSCSI(device,cmd,10,direction,dst,blockSize*blockCount); |
sivan_toledo | 0:3e7d6f496a67 | 165 | } |
sivan_toledo | 0:3e7d6f496a67 | 166 | |
sivan_toledo | 0:3e7d6f496a67 | 167 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize) |
sivan_toledo | 0:3e7d6f496a67 | 168 | { |
sivan_toledo | 0:3e7d6f496a67 | 169 | return SCSIReadCapacity(device,blockCount,blockSize); |
sivan_toledo | 0:3e7d6f496a67 | 170 | } |
sivan_toledo | 0:3e7d6f496a67 | 171 | |
sivan_toledo | 0:3e7d6f496a67 | 172 | int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
sivan_toledo | 0:3e7d6f496a67 | 173 | { |
sivan_toledo | 0:3e7d6f496a67 | 174 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,DEVICE_TO_HOST); |
sivan_toledo | 0:3e7d6f496a67 | 175 | } |
sivan_toledo | 0:3e7d6f496a67 | 176 | |
sivan_toledo | 0:3e7d6f496a67 | 177 | int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
sivan_toledo | 0:3e7d6f496a67 | 178 | { |
sivan_toledo | 0:3e7d6f496a67 | 179 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,HOST_TO_DEVICE); |
sivan_toledo | 0:3e7d6f496a67 | 180 | } |