msc_disk.h
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __USB_DISK_H__
#define __USB_DISK_H__ 1
/*
#if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI>0)
#define CONTROLLER_ID kUSB_ControllerEhci0
#endif
#if defined(USB_DEVICE_CONFIG_KHCI) && (USB_DEVICE_CONFIG_KHCI>0)
#define CONTROLLER_ID kUSB_ControllerKhci0
#endif
*/
#define USB_DEVICE_INTERRUPT_PRIORITY (4)
// Definitions
#define U32_LE(n) ((n)&0xff), (((n) >> 8) & 0xff), (((n) >> 16) & 0xff), (((n) >> 24) & 0xff)
#define U16_LE(n) ((n)&0xff), (((n) >> 8) & 0xff)
/* Length of Each Logical Address Block */
#define LENGTH_OF_EACH_LBA (512)
/* total number of logical blocks present */
#define TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL (48)
/* Net Disk Size , default disk is 48*512, that is 24kByte, however , the disk reconnised by that PC only has 4k Byte,
* This is caused by that the file system also need memory*/
//#define DISK_SIZE_NORMAL (TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL * LENGTH_OF_EACH_LBA)
#define LOGICAL_UNIT_SUPPORTED (1)
#define USB_DEVICE_MSC_WRITE_BUFF_NUM 2
#define USB_DEVICE_MSC_WRITE_BUFF_SIZE 512
#define USB_DEVICE_MSC_READ_BUFF_SIZE 512
enum _disk_constants
{
kDiskSectorSize = 512,
kDiskTotalLogicalBlocks = 2048 * 16,
kDiskSizeInBytes = kDiskTotalLogicalBlocks * kDiskSectorSize,
kDiskLogicalUnits = 1,
kDiskHeaderSectorCount = 1,
kDiskFat16EntrySize = 2,
kDiskClusterSize = 1,
kDiskDataClusterCount = (kDiskTotalLogicalBlocks - kDiskHeaderSectorCount) / kDiskClusterSize,
kDiskSectorsPerFat = (kDiskDataClusterCount * kDiskFat16EntrySize + kDiskSectorSize - 1) /
kDiskSectorSize,
};
enum _sector_numbers
{
kPbsSector = 0,
kFat1Sector = kPbsSector + 1,
kFat2Sector = kFat1Sector + kDiskSectorsPerFat,
kRootDir1Sector = kFat2Sector + kDiskSectorsPerFat,
kRootDir2Sector,
kMacFseventsdDirSector,
kWindowsSysVolInfoDirSector,
kWindowsIndexerVolumeGuidSector,
kInfoFileSector,
kStatusFileSector,
kFirstUnusedSector,
kFirstClusterSector = kRootDir1Sector,
};
#define CLUSTER_FROM_SECTOR(s) (((s)-kFirstClusterSector) / kDiskClusterSize)
enum _root_dir_entry_numbers
{
kInfoFileDirEntry = 11,
kStatusFileDirEntry = 12,
kFirstUnusedDirEntry = 13
};
typedef struct SectorInfo
{
uint32_t sector;
const uint8_t *data;
uint32_t length;
} sector_info_t;
typedef enum _sb_transfer_status
{
kReadyStatus = 0,
kTransferringStatus,
kSuccessStatus,
kFailureStatus
} sb_transfer_status_t;
typedef enum _sb_loader_status
{
kSbLoaderFree = 0,
kSbLoaderInited,
kSbLoaderLoading,
kSbLoaderFinal,
} sb_loader_status_t;
typedef enum _sector_buffer_status
{
kBufferFree = 0,
kBufferPending,
kBufferFilled,
kBufferInUse
} sector_buffer_status_t;
enum _usb_msd_constants
{
kNumberOfSectorBuffers = 2,
kInvalidBufferReadIndex = ~0
};
typedef struct _usb_msd_state
{
uint32_t start_app;
uint8_t buffer[kDiskSectorSize];
uint8_t sbBuffer[kDiskSectorSize];
uint8_t isDiskLocked;
uint8_t read_write_error;
uint8_t isActive;
uint8_t isTransferring;
uint8_t sbLoaderStatus; // //!< Status to indicate that the SB loader has been initialized.
uint8_t isProcessing;
uint8_t isReceivePending;
uint32_t infoFileLength;
uint32_t nextSector;
uint32_t remainingFileLength;
uint32_t dataBytesAvailable;
sb_transfer_status_t transferStatus;
} usb_msd_state_t;
typedef struct _usb_msc_struct
{
usb_device_handle device_handle;
class_handle_t msc_handle;
// uint8_t storage_disk[DISK_SIZE_NORMAL];
usb_msd_state_t msc_state;
uint8_t current_configuration;
uint8_t current_interface_alternate_setting[USB_MSC_INTERFACE_COUNT];
uint8_t speed;
uint8_t attach;
} usb_msc_struct_t;
#endif
kFirstUnusedSector
First sector available for data.
Definition: msc_disk.h:99
kDiskClusterSize
Number of sectors per cluster.
Definition: msc_disk.h:80
kFirstClusterSector
Sector number of the first cluster.
Definition: msc_disk.h:101
sector_info_t
Sector information.
Definition: msc_disk.h:119
kDiskSectorsPerFat
Number of sectors occupied by each FAT, rounded up.
Definition: msc_disk.h:82
usb_msd_state_t::isProcessing
uint8_t isProcessing
Set to true while the SB file is being processed.
Definition: msc_disk.h:176
sector_info_t::data
const uint8_t * data
Data for the sector.
Definition: msc_disk.h:122
kMacFseventsdDirSector
Mac OS X &#39;.fseventsd&#39; directory.
Definition: msc_disk.h:94
kDiskSectorSize
Size in bytes of each sector of the disk.
Definition: msc_disk.h:74
kFat2Sector
FAT2 (unused)
Definition: msc_disk.h:91
usb_msd_state_t::isActive
uint8_t isActive
Set to true when the first SB file is transferred.
Definition: msc_disk.h:173
sector_info_t::length
uint32_t length
Number of bytes of data.
Definition: msc_disk.h:123
kWindowsSysVolInfoDirSector
Windows 8.1 &#39;System Volume Information&#39; directory.
Definition: msc_disk.h:95
kRootDir2Sector
Root directory 2 (16 x 32-byte entries)
Definition: msc_disk.h:93
kDiskHeaderSectorCount
PBS.
Definition: msc_disk.h:78
usb_msd_state_t::isReceivePending
uint8_t isReceivePending
Whether a sector write is being held off until a buffer is free.
Definition: msc_disk.h:177
usb_msc_struct_t
Definition: msc_disk.h:185
kDiskSizeInBytes
Size in bytes of the total disk.
Definition: msc_disk.h:76
kRootDir1Sector
Root directory 1 (16 x 32-byte entries)
Definition: msc_disk.h:92
usb_msd_state_t::infoFileLength
uint32_t infoFileLength
Size in bytes of the info.txt file.
Definition: msc_disk.h:178
kWindowsIndexerVolumeGuidSector
Windows 8.1 &#39;IndexerVolumeGuid&#39; file.
Definition: msc_disk.h:96
usb_msd_state_t
State information for the USB MSD peripheral.
Definition: msc_disk.h:166
kPbsSector
Partition Boot Sector.
Definition: msc_disk.h:89
usb_msd_state_t::remainingFileLength
uint32_t remainingFileLength
Total length in bytes of the SB file being transferred.
Definition: msc_disk.h:180
kDiskLogicalUnits
Number of SCSI LUNs.
Definition: msc_disk.h:77
usb_msd_state_t::dataBytesAvailable
uint32_t dataBytesAvailable
Number of bytes available in sectorBuffer.
Definition: msc_disk.h:181
kInfoFileSector
Info .txt file.
Definition: msc_disk.h:97
usb_msd_state_t::nextSector
uint32_t nextSector
The next expected sector write for the SB file transfer.
Definition: msc_disk.h:179
kDiskTotalLogicalBlocks
Total number of sectors in the disk.
Definition: msc_disk.h:75
sector_info_t::sector
uint32_t sector
Sector number.
Definition: msc_disk.h:121
kStatusFileSector
Status .txt file.
Definition: msc_disk.h:98
usb_msd_state_t::isTransferring
uint8_t isTransferring
Whether an SB file transfer has been started.
Definition: msc_disk.h:174
kFat1Sector
FAT1.
Definition: msc_disk.h:90
usb_msd_state_t::transferStatus
sb_transfer_status_t transferStatus
Status to present in the status.txt file.
Definition: msc_disk.h:182
kDiskFat16EntrySize
Two bytes per FAT16 entry.
Definition: msc_disk.h:79