Contiki-Inga 3.x
diskio.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Institute of Operating Systems and Computer Networks (TU Braunschweig).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Author: Christoph Peltz <peltz@ibr.cs.tu-bs.de>
30  */
31 
32 /**
33  * \addtogroup cfs
34  * @{
35  */
36 
37 /** \defgroup diskio_layer DiskIO Abstraction Layer
38  *
39  * \note This interface was inspired by the diskio-interface of the FatFS-Module.
40  *
41  * <p> It's normally not important for Filesystem-Drivers to know, what type the underlying
42  * storage device is. It's only important that the Driver can write and read on this device.
43  * This abstraction layer enabled the filesystem drivers to do exactly that.</p>
44  *
45  * @{
46  */
47 
48 /**
49  * \file
50  * DiskIO Layer definitions
51  * \author
52  * Original Source Code:
53  * Christoph Peltz <peltz@ibr.cs.tu-bs.de>
54  */
55 #ifndef _DISKIO_H_
56 #define _DISKIO_H_
57 
58 /** Allows raw access to disks, used by the MBR-Subsystem */
59 #define DISKIO_DEVICE_TYPE_NOT_RECOGNIZED 0
60 #define DISKIO_DEVICE_TYPE_SD_CARD 1
61 #define DISKIO_DEVICE_TYPE_GENERIC_FLASH 2
62 #define DISKIO_DEVICE_TYPE_PARTITION 128
63 
64 /** Bigger sectors then this are not supported. May be reduced down to 512 to use less memory. */
65 #define DISKIO_MAX_SECTOR_SIZE 512
66 
67 /** Mask used to ignore modifiers like the PARTITION flag */
68 #define DISKIO_DEVICE_TYPE_MASK 0x7f
69 
70 #define DISKIO_SUCCESS 0
71 #define DISKIO_ERROR_NO_DEVICE_SELECTED 1
72 #define DISKIO_ERROR_INTERNAL_ERROR 2
73 #define DISKIO_ERROR_DEVICE_TYPE_NOT_RECOGNIZED 3
74 #define DISKIO_ERROR_OPERATION_NOT_SUPPORTED 4
75 #define DISKIO_ERROR_TO_BE_IMPLEMENTED 5
76 #define DISKIO_FAILURE 6
77 #define DISKIO_ERROR_TRY_AGAIN 7
78 #define DISKIO_ERROR_FATAL 8
79 
80 #ifndef DISKIO_MAX_DEVICES
81 #define DISKIO_MAX_DEVICES 5
82 #endif
83 
84 #define DISKIO_OP_WRITE_BLOCK 1
85 #define DISKIO_OP_READ_BLOCK 2
86 #define DISKIO_OP_WRITE_BLOCKS_START 10
87 #define DISKIO_OP_WRITE_BLOCKS_NEXT 11
88 #define DISKIO_OP_WRITE_BLOCKS_DONE 12
89 #define DISKIO_OP_READ_BLOCKS 4
90 
91 
92 #include <stdint.h>
93 
94 /**
95  * Stores the necessary information to identify a device using the diskio-Library.
96  */
98  /** Specifies the recognized type of the memory */
99  uint8_t type;
100  /** Number to identify device */
101  uint8_t number;
102  /** Will be ignored if PARTITION flag is not set in type,
103  * otherwise tells which partition of this device is meant */
104  uint8_t partition;
105  /** Number of sectors on this device */
106  uint32_t num_sectors;
107  /** How big is one sector in bytes */
108  uint16_t sector_size;
109  /** If this is a Partition, this indicates which is the
110  * first_sector belonging to this partition on this device */
111  uint32_t first_sector;
112 };
113 
114 /**
115  * Prints information about the specified device.
116  *
117  * <b>Output Format:</b> \n
118  * DiskIO Device Info\n
119  * type = text \n
120  * number = X\n
121  * partition = X\n
122  * num_sectors = X\n
123  * sector_size = X\n
124  * first_sector = X
125 
126  * \param *dev the pointer to the device info struct
127  */
129 
130 /**
131  * Reads one block from the specified device and stores it in buffer.
132  *
133  * \param *dev the pointer to the device info struct
134  * \param block_address Which block should be read
135  * \param *buffer buffer in which the data is written
136  * \return DISKIO_SUCCESS on success, otherwise not 0 for an error
137  */
138 int diskio_read_block(struct diskio_device_info *dev, uint32_t block_address, uint8_t *buffer);
139 
140 /**
141  * Reads multiple blocks from the specified device.
142  *
143  * This may or may not be supported for every device. DISKIO_ERROR_OPERATION_NOT_SUPPORTED will be
144  * returned in that case.
145  * \param *dev the pointer to the device info
146  * \param block_start_address the address of the first block to be read
147  * \param num_blocks the number of blocks to be read
148  * \param *buffer buffer in which the data is written
149  * \return DISKIO_SUCCESS on success, otherwise not 0 for an error
150  */
151 int diskio_read_blocks(struct diskio_device_info *dev, uint32_t block_start_address, uint32_t num_blocks, uint8_t *buffer);
152 
153 /**
154  * Writes a single block to the specified device
155  *
156  * \param *dev the pointer to the device info
157  * \param block_address address where the block should be written
158  * \param *buffer buffer in which the data is stored
159  * \return DISKIO_SUCCESS on success, !0 on error
160  */
161 int diskio_write_block(struct diskio_device_info *dev, uint32_t block_address, uint8_t *buffer);
162 
163 /**
164  * Start writing multiple blocks to the specified device
165  *
166  * \param *dev the pointer to the device info
167  * \param block_start_address the address of the first block to be written
168  * \param num_blocks number of blocks to be written
169  * \return DISKIO_SUCCESS on success, !0 on error
170  */
171 int diskio_write_blocks_start(struct diskio_device_info *dev, uint32_t block_start_address, uint32_t num_blocks);
172 
173 /**
174  * Write next of multiple blocks to the specified device
175  *
176  * \param *dev the pointer to the device info
177  * \param *buffer buffer in which the data is stored
178  * \return DISKIO_SUCCESS on success, !0 on error
179  */
180 int diskio_write_blocks_next(struct diskio_device_info *dev, uint8_t *buffer);
181 
182 /**
183  * Start writing multiple blocks to the specified device
184  *
185  * \param *dev the pointer to the device info
186  * \return DISKIO_SUCCESS on success, !0 on error
187  */
189 
190 /**
191  * Returns the device-Database.
192  *
193  * Number of devices in the database is limited by the DISKIO_MAX_DEVICES define.
194  * \return pointer to the Array of device infos. No available device entries have the type set to DISKIO_DEVICE_TYPE_NOT_RECOGNIZED.
195  */
197 
198 /**
199  * Creates the internal database of available devices.
200  *
201  * Adds virtual devices for multiple Partitions on devices using the MBR-Library.
202  * Number of devices in the database is limited by the DISKIO_MAX_DEVICES define.
203  * Warning the device numbers may change when calling this function. It should be called
204  * once on start but may also be called, if the microSD-Card was ejected to update the device
205  * database.
206  */
208 
209 /**
210  * Sets the default operation device.
211  *
212  * This allows to call the diskio_* functions with the dev parameter set to NULL.
213  * The functions will then use the default device to operate.
214  * \param *dev the pointer to the device info, which will be the new default device
215  */
217 
218 #endif
219 
220 /** @} */
221 /** @} */