]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/Reliance-Edge/core/driver/blockio.c
Update version numbers in preparation for new release.
[freertos] / FreeRTOS-Plus / Source / Reliance-Edge / core / driver / blockio.c
1 /*             ----> DO NOT REMOVE THE FOLLOWING NOTICE <----\r
2 \r
3                    Copyright (c) 2014-2015 Datalight, Inc.\r
4                        All Rights Reserved Worldwide.\r
5 \r
6     This program is free software; you can redistribute it and/or modify\r
7     it under the terms of the GNU General Public License as published by\r
8     the Free Software Foundation; use version 2 of the License.\r
9 \r
10     This program is distributed in the hope that it will be useful,\r
11     but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty\r
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13     GNU General Public License for more details.\r
14 \r
15     You should have received a copy of the GNU General Public License along\r
16     with this program; if not, write to the Free Software Foundation, Inc.,\r
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 */\r
19 /*  Businesses and individuals that for commercial or other reasons cannot\r
20     comply with the terms of the GPLv2 license may obtain a commercial license\r
21     before incorporating Reliance Edge into proprietary software for\r
22     distribution in any form.  Visit http://www.datalight.com/reliance-edge for\r
23     more information.\r
24 */\r
25 /** @file\r
26     @brief Implements block device I/O using logical blocks as the units.\r
27 \r
28     The OS block device implementations operate on sectors.  The core does I/O\r
29     in terms of logical blocks: this module translates from logical blocks to\r
30     sectors.\r
31 */\r
32 #include <redfs.h>\r
33 #include <redcore.h>\r
34 \r
35 \r
36 /** @brief Read a range of logical blocks.\r
37 \r
38     @param bVolNum      The volume whose block device is being read from.\r
39     @param ulBlockStart The first block to read.\r
40     @param ulBlockCount The number of blocks to read.\r
41     @param pBuffer      The buffer to populate with the data read.\r
42 \r
43     @return A negated ::REDSTATUS code indicating the operation result.\r
44 \r
45     @retval 0           Operation was successful.\r
46     @retval -RED_EIO    A disk I/O error occurred.\r
47     @retval -RED_EINVAL Invalid parameters.\r
48 */\r
49 REDSTATUS RedIoRead(\r
50     uint8_t     bVolNum,\r
51     uint32_t    ulBlockStart,\r
52     uint32_t    ulBlockCount,\r
53     void       *pBuffer)\r
54 {\r
55     REDSTATUS   ret;\r
56 \r
57     if(    (bVolNum >= REDCONF_VOLUME_COUNT)\r
58         || (ulBlockStart >= gaRedVolume[bVolNum].ulBlockCount)\r
59         || ((gaRedVolume[bVolNum].ulBlockCount - ulBlockStart) < ulBlockCount)\r
60         || (ulBlockCount == 0U)\r
61         || (pBuffer == NULL))\r
62     {\r
63         REDERROR();\r
64         ret = -RED_EINVAL;\r
65     }\r
66     else\r
67     {\r
68         uint8_t  bSectorShift = gaRedVolume[bVolNum].bBlockSectorShift;\r
69         uint64_t ullSectorStart = (uint64_t)ulBlockStart << bSectorShift;\r
70         uint32_t ulSectorCount = ulBlockCount << bSectorShift;\r
71 \r
72         REDASSERT(bSectorShift < 32U);\r
73         REDASSERT((ulSectorCount >> bSectorShift) == ulBlockCount);\r
74 \r
75         ret = RedOsBDevRead(bVolNum, ullSectorStart, ulSectorCount, pBuffer);\r
76     }\r
77 \r
78     CRITICAL_ASSERT(ret == 0);\r
79 \r
80     return ret;\r
81 }\r
82 \r
83 \r
84 #if REDCONF_READ_ONLY == 0\r
85 /** @brief Write a range of logical blocks.\r
86 \r
87     @param bVolNum      The volume whose block device is being written to.\r
88     @param ulBlockStart The first block to write.\r
89     @param ulBlockCount The number of blocks to write.\r
90     @param pBuffer      The buffer containing the data to write.\r
91 \r
92     @return A negated ::REDSTATUS code indicating the operation result.\r
93 \r
94     @retval 0           Operation was successful.\r
95     @retval -RED_EIO    A disk I/O error occurred.\r
96     @retval -RED_EINVAL Invalid parameters.\r
97 */\r
98 REDSTATUS RedIoWrite(\r
99     uint8_t     bVolNum,\r
100     uint32_t    ulBlockStart,\r
101     uint32_t    ulBlockCount,\r
102     const void *pBuffer)\r
103 {\r
104     REDSTATUS   ret;\r
105 \r
106     if(    (bVolNum >= REDCONF_VOLUME_COUNT)\r
107         || (ulBlockStart >= gaRedVolume[bVolNum].ulBlockCount)\r
108         || ((gaRedVolume[bVolNum].ulBlockCount - ulBlockStart) < ulBlockCount)\r
109         || (ulBlockCount == 0U)\r
110         || (pBuffer == NULL))\r
111     {\r
112         REDERROR();\r
113         ret = -RED_EINVAL;\r
114     }\r
115     else\r
116     {\r
117         uint8_t  bSectorShift = gaRedVolume[bVolNum].bBlockSectorShift;\r
118         uint64_t ullSectorStart = (uint64_t)ulBlockStart << bSectorShift;\r
119         uint32_t ulSectorCount = ulBlockCount << bSectorShift;\r
120 \r
121         REDASSERT(bSectorShift < 32U);\r
122         REDASSERT((ulSectorCount >> bSectorShift) == ulBlockCount);\r
123 \r
124         ret = RedOsBDevWrite(bVolNum, ullSectorStart, ulSectorCount, pBuffer);\r
125     }\r
126 \r
127     CRITICAL_ASSERT(ret == 0);\r
128 \r
129     return ret;\r
130 }\r
131 \r
132 \r
133 /** @brief Flush any caches beneath the file system.\r
134 \r
135     @param bVolNum  The volume number of the volume whose block device is being\r
136                     flushed.\r
137 \r
138     @return A negated ::REDSTATUS code indicating the operation result.\r
139 \r
140     @retval 0           Operation was successful.\r
141     @retval -RED_EINVAL @p bVolNum is an invalid volume number.\r
142     @retval -RED_EIO    A disk I/O error occurred.\r
143 */\r
144 REDSTATUS RedIoFlush(\r
145     uint8_t     bVolNum)\r
146 {\r
147     REDSTATUS   ret;\r
148 \r
149     if(bVolNum >= REDCONF_VOLUME_COUNT)\r
150     {\r
151         REDERROR();\r
152         ret = -RED_EINVAL;\r
153     }\r
154     else\r
155     {\r
156         ret = RedOsBDevFlush(bVolNum);\r
157     }\r
158 \r
159     CRITICAL_ASSERT(ret == 0);\r
160 \r
161     return ret;\r
162 }\r
163 #endif /* REDCONF_READ_ONLY == 0 */\r
164 \r