Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
msc_disk.h
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __USB_DISK_H__
32 #define __USB_DISK_H__ 1
33 
34 /*
35 #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI>0)
36 #define CONTROLLER_ID kUSB_ControllerEhci0
37 #endif
38 #if defined(USB_DEVICE_CONFIG_KHCI) && (USB_DEVICE_CONFIG_KHCI>0)
39 #define CONTROLLER_ID kUSB_ControllerKhci0
40 #endif
41  */
42 #define USB_DEVICE_INTERRUPT_PRIORITY (4)
43 
45 // Definitions
47 
49 
50 #define U32_LE(n) ((n)&0xff), (((n) >> 8) & 0xff), (((n) >> 16) & 0xff), (((n) >> 24) & 0xff)
52 
54 #define U16_LE(n) ((n)&0xff), (((n) >> 8) & 0xff)
55 
56 
57 /* Length of Each Logical Address Block */
58 #define LENGTH_OF_EACH_LBA (512)
59 /* total number of logical blocks present */
60 #define TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL (48)
61 /* Net Disk Size , default disk is 48*512, that is 24kByte, however , the disk reconnised by that PC only has 4k Byte,
62  * This is caused by that the file system also need memory*/
63 //#define DISK_SIZE_NORMAL (TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL * LENGTH_OF_EACH_LBA)
64 
65 #define LOGICAL_UNIT_SUPPORTED (1)
66 
67 #define USB_DEVICE_MSC_WRITE_BUFF_NUM 2
68 #define USB_DEVICE_MSC_WRITE_BUFF_SIZE 512
69 #define USB_DEVICE_MSC_READ_BUFF_SIZE 512
70 
72 enum _disk_constants
73 {
82  kDiskSectorsPerFat = (kDiskDataClusterCount * kDiskFat16EntrySize + kDiskSectorSize - 1) /
84 };
85 
87 enum _sector_numbers
88 {
89  kPbsSector = 0,
100 
102 };
103 
105 #define CLUSTER_FROM_SECTOR(s) (((s)-kFirstClusterSector) / kDiskClusterSize)
106 
108 enum _root_dir_entry_numbers
109 {
110  kInfoFileDirEntry = 11,
111  kStatusFileDirEntry = 12,
112  kFirstUnusedDirEntry = 13
113 };
114 
119 typedef struct SectorInfo
120 {
121  uint32_t sector;
122  const uint8_t *data;
123  uint32_t length;
124 } sector_info_t;
125 
127 typedef enum _sb_transfer_status
128 {
129  kReadyStatus = 0,
130  kTransferringStatus,
131  kSuccessStatus,
132  kFailureStatus
133 } sb_transfer_status_t;
134 
136 typedef enum _sb_loader_status
137 {
138  kSbLoaderFree = 0,
139  kSbLoaderInited,
140  kSbLoaderLoading,
141  kSbLoaderFinal,
142 } sb_loader_status_t;
143 
145 typedef enum _sector_buffer_status
146 {
147  kBufferFree = 0,
148  kBufferPending,
149  kBufferFilled,
150  kBufferInUse
151 } sector_buffer_status_t;
152 
154 enum _usb_msd_constants
155 {
157  kNumberOfSectorBuffers = 2,
158 
160  kInvalidBufferReadIndex = ~0
161 };
162 
166 typedef struct _usb_msd_state
167 {
168  uint32_t start_app;
169  uint8_t buffer[kDiskSectorSize];
170  uint8_t sbBuffer[kDiskSectorSize];
171  uint8_t isDiskLocked;
172  uint8_t read_write_error;
173  uint8_t isActive;
174  uint8_t isTransferring;
175  uint8_t sbLoaderStatus; // //!< Status to indicate that the SB loader has been initialized.
176  uint8_t isProcessing;
178  uint32_t infoFileLength;
179  uint32_t nextSector;
182  sb_transfer_status_t transferStatus;
184 
185 typedef struct _usb_msc_struct
186 {
187  usb_device_handle device_handle;
188  class_handle_t msc_handle;
189  // uint8_t storage_disk[DISK_SIZE_NORMAL];
190  usb_msd_state_t msc_state;
191  uint8_t current_configuration;
192  uint8_t current_interface_alternate_setting[USB_MSC_INTERFACE_COUNT];
193  uint8_t speed;
194  uint8_t attach;
196 
197 #endif
First sector available for data.
Definition: msc_disk.h:99
Number of sectors per cluster.
Definition: msc_disk.h:80
Sector number of the first cluster.
Definition: msc_disk.h:101
Sector information.
Definition: msc_disk.h:119
Number of sectors occupied by each FAT, rounded up.
Definition: msc_disk.h:82
uint8_t isProcessing
Set to true while the SB file is being processed.
Definition: msc_disk.h:176
const uint8_t * data
Data for the sector.
Definition: msc_disk.h:122
Mac OS X &#39;.fseventsd&#39; directory.
Definition: msc_disk.h:94
Size in bytes of each sector of the disk.
Definition: msc_disk.h:74
FAT2 (unused)
Definition: msc_disk.h:91
uint8_t isActive
Set to true when the first SB file is transferred.
Definition: msc_disk.h:173
uint32_t length
Number of bytes of data.
Definition: msc_disk.h:123
Windows 8.1 &#39;System Volume Information&#39; directory.
Definition: msc_disk.h:95
Root directory 2 (16 x 32-byte entries)
Definition: msc_disk.h:93
PBS.
Definition: msc_disk.h:78
uint8_t isReceivePending
Whether a sector write is being held off until a buffer is free.
Definition: msc_disk.h:177
Definition: msc_disk.h:185
Size in bytes of the total disk.
Definition: msc_disk.h:76
Root directory 1 (16 x 32-byte entries)
Definition: msc_disk.h:92
uint32_t infoFileLength
Size in bytes of the info.txt file.
Definition: msc_disk.h:178
Windows 8.1 &#39;IndexerVolumeGuid&#39; file.
Definition: msc_disk.h:96
State information for the USB MSD peripheral.
Definition: msc_disk.h:166
Partition Boot Sector.
Definition: msc_disk.h:89
uint32_t remainingFileLength
Total length in bytes of the SB file being transferred.
Definition: msc_disk.h:180
Number of SCSI LUNs.
Definition: msc_disk.h:77
uint32_t dataBytesAvailable
Number of bytes available in sectorBuffer.
Definition: msc_disk.h:181
Info .txt file.
Definition: msc_disk.h:97
uint32_t nextSector
The next expected sector write for the SB file transfer.
Definition: msc_disk.h:179
Total number of sectors in the disk.
Definition: msc_disk.h:75
uint32_t sector
Sector number.
Definition: msc_disk.h:121
Status .txt file.
Definition: msc_disk.h:98
uint8_t isTransferring
Whether an SB file transfer has been started.
Definition: msc_disk.h:174
FAT1.
Definition: msc_disk.h:90
sb_transfer_status_t transferStatus
Status to present in the status.txt file.
Definition: msc_disk.h:182
Two bytes per FAT16 entry.
Definition: msc_disk.h:79