os/kernelhwsrv/kerneltest/f32test/smassstorage/src/scsicmdbuilder.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Provides utilities to build SCSI commands
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
/**
sl@0
    19
 @file
sl@0
    20
 @internalTechnology
sl@0
    21
*/
sl@0
    22
sl@0
    23
#include <e32std.h>
sl@0
    24
#include <e32std_private.h>
sl@0
    25
#include <testusbc.h>
sl@0
    26
#include "scsicmdbuilder.h"
sl@0
    27
sl@0
    28
#define SCSI_READ_WRITE_LEN       10
sl@0
    29
#define SCSI_UNIT_READY_LEN        6
sl@0
    30
#define SCSI_MED_RMVL_LEN          6
sl@0
    31
#define SCSI_START_STOP_LEN        6
sl@0
    32
sl@0
    33
GLDEF_D TBuf8<KScsiCmdMaxLen> scsiCmdBuf;
sl@0
    34
GLREF_D RDevTestUsbcClient usbcClient;
sl@0
    35
sl@0
    36
void BuildReadWrite(TInt aFlag, TInt aLogicalBlkAddr, TInt aTotalBlks)
sl@0
    37
    // 
sl@0
    38
    // Builds SCSI read(10) / write(10) command
sl@0
    39
    //
sl@0
    40
    {
sl@0
    41
    // Zero out the whole buffer
sl@0
    42
    scsiCmdBuf.FillZ(SCSI_READ_WRITE_LEN);
sl@0
    43
    scsiCmdBuf[0] = TUint8(aFlag);     // opcode for read (0x28)/write (0x2A)
sl@0
    44
sl@0
    45
    // Fill in logical block address. Big endian
sl@0
    46
    scsiCmdBuf[2] = TUint8((aLogicalBlkAddr >> 24) & 0xFF);
sl@0
    47
    scsiCmdBuf[3] = TUint8((aLogicalBlkAddr >> 16) & 0xFF);
sl@0
    48
    scsiCmdBuf[4] = TUint8((aLogicalBlkAddr >> 8) & 0xFF);
sl@0
    49
    scsiCmdBuf[5] = TUint8(aLogicalBlkAddr & 0xFF);
sl@0
    50
    
sl@0
    51
    // Data transfer length (# of sectors). Big endian
sl@0
    52
    scsiCmdBuf[7] = TUint8((aTotalBlks >> 8) & 0xFF);
sl@0
    53
    scsiCmdBuf[8] = TUint8((aTotalBlks & 0xFF));
sl@0
    54
sl@0
    55
    scsiCmdBuf.SetLength(SCSI_READ_WRITE_LEN);
sl@0
    56
    }
sl@0
    57
sl@0
    58
void BuildMediumRemoval(TInt aFlag)
sl@0
    59
    //
sl@0
    60
    // Builds prevent/allow medium removal command
sl@0
    61
    // 
sl@0
    62
    {
sl@0
    63
    // Zero out the buf
sl@0
    64
    scsiCmdBuf.SetLength(SCSI_MED_RMVL_LEN);
sl@0
    65
    scsiCmdBuf.FillZ(SCSI_MED_RMVL_LEN);
sl@0
    66
    scsiCmdBuf[0] = 0x1E;             // opcode for medium removal
sl@0
    67
    scsiCmdBuf[4] = TUint8(aFlag);    // prevent(1)/allow(0) medium removal 
sl@0
    68
    }
sl@0
    69
sl@0
    70
void BuildTestUnitReady()
sl@0
    71
    //
sl@0
    72
    // Builds test unit ready command
sl@0
    73
    // 
sl@0
    74
    {
sl@0
    75
    // Zero out the buf
sl@0
    76
    scsiCmdBuf.FillZ(SCSI_UNIT_READY_LEN);
sl@0
    77
    scsiCmdBuf[0] = 0;                // opcode for test unit ready
sl@0
    78
sl@0
    79
    scsiCmdBuf.SetLength(SCSI_UNIT_READY_LEN);
sl@0
    80
    }
sl@0
    81
sl@0
    82
void BuildStartStopUnit(TInt aFlag)
sl@0
    83
    //
sl@0
    84
    // Builds start/stop unit command
sl@0
    85
    // 
sl@0
    86
    {
sl@0
    87
    const TUint8 KStartMask = 0x01;
sl@0
    88
    const TUint8 KLoejMask = 0x02;
sl@0
    89
    // Zero out the buf
sl@0
    90
    scsiCmdBuf.FillZ(SCSI_START_STOP_LEN);
sl@0
    91
    scsiCmdBuf[0] = 0x1B;             // opcode for test unit ready
sl@0
    92
    scsiCmdBuf[1] = 0x01;			  // set immed bit to true
sl@0
    93
    scsiCmdBuf[4] = TUint8((aFlag == 1) ? KLoejMask & KStartMask : KLoejMask);  // LOEJ + START or STOP
sl@0
    94
sl@0
    95
    scsiCmdBuf.SetLength(SCSI_START_STOP_LEN);
sl@0
    96
    }
sl@0
    97
sl@0
    98
void fillInt(TUint8* dest, TInt source)
sl@0
    99
    // 
sl@0
   100
    // Copy an int. Little endian
sl@0
   101
    //
sl@0
   102
    {
sl@0
   103
    for (TInt i = 0; i < 4; i++)
sl@0
   104
        {
sl@0
   105
        *dest++ = TUint8((source >> i*8) & 0xFF);
sl@0
   106
        }
sl@0
   107
    }
sl@0
   108
sl@0
   109
TInt extractInt(const TUint8* aBuf)
sl@0
   110
    //
sl@0
   111
    // Extract an integer from a buffer. Assume little endian
sl@0
   112
    //
sl@0
   113
    {
sl@0
   114
    return aBuf[0] + (aBuf[1] << 8) + (aBuf[2] << 16) + (aBuf[3] << 24);
sl@0
   115
    }
sl@0
   116
sl@0
   117
void createCBW(TDes8& aCbw, TInt aDCBWTag, TInt aDataTransferLen, TUint8 aInOutFlag, TDes8& aCBWCB, TUint8 aTestLun)
sl@0
   118
    // 
sl@0
   119
    // aCbw:            stores CBW
sl@0
   120
    // aDCBWTag:        a command block tag sent by the host. Used to associates a CSW
sl@0
   121
    //                  with corresponding CBW
sl@0
   122
    // aDataTranferLen: the number of bytes the host expects to transfer
sl@0
   123
    // aInOutFlag:      value for bmCBWFlags field, indicating the direction of transfer
sl@0
   124
    // aCBWCB			the actual command to be wrapped
sl@0
   125
    // aTestLun			local unit number
sl@0
   126
    {
sl@0
   127
    // Zero out aCbw
sl@0
   128
    aCbw.SetLength(KCbwLength);
sl@0
   129
    aCbw.FillZ();
sl@0
   130
sl@0
   131
    // dCBWSignature field, the value comes from spec
sl@0
   132
    TInt dCBWSignature = 0x43425355;
sl@0
   133
    fillInt(&aCbw[0], dCBWSignature);
sl@0
   134
    // dCBWTag field
sl@0
   135
    fillInt(&aCbw[4], aDCBWTag);
sl@0
   136
    // dCBWDataTransferLength field
sl@0
   137
    fillInt(&aCbw[8], aDataTransferLen);
sl@0
   138
    // bmCBWFlags field
sl@0
   139
    aCbw[12] = aInOutFlag;
sl@0
   140
	aCbw[13] = aTestLun;
sl@0
   141
    // bCBWCBLength field
sl@0
   142
    aCbw[14] = TUint8(aCBWCB.Length());          
sl@0
   143
	
sl@0
   144
    // CBWCB field
sl@0
   145
    for (TInt i = 0; i < aCbw[14]; ++i)
sl@0
   146
    	{
sl@0
   147
    	aCbw[15 + i] = aCBWCB[i];
sl@0
   148
    	}
sl@0
   149
    }
sl@0
   150