1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  *  that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  *   of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  *   list of conditions and the following disclaimer in the documentation and/or
17  *   other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  *   contributors may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "fsl_common.h"
37 #include "fsl_ram_disk.h"
38 #include "ffconf.h"
39 
40 /*******************************************************************************
41  * Definitions
42  ******************************************************************************/
43 /* clang-format off */
44 #define SECTOR_SIZE _MIN_SS /* usualy 512 B */
45 #define DISK_SIZE 65536     /* minmal disk size calculated as 128 * _MIN_SS (ff.c ln 4112) , 128*512=65536 */
46 /* clang-format on */
47 
48 /*******************************************************************************
49  * Globals
50  ******************************************************************************/
51 static uint8_t disk_space[DISK_SIZE];
52 
53 /*******************************************************************************
54  * Code
55  ******************************************************************************/
56 /*!
57  * @brief Get RAM disk status.
58  */
ram_disk_status(BYTE pdrv)59 DSTATUS ram_disk_status(BYTE pdrv)
60 {
61     if (pdrv != RAMDISK) {
62         return STA_NOINIT;
63     }
64 
65     return 0;
66 }
67 
68 /*!
69  * @brief Inidialize a RAM disk.
70  */
ram_disk_initialize(BYTE pdrv)71 DSTATUS ram_disk_initialize(BYTE pdrv)
72 {
73     if (pdrv != RAMDISK) {
74         return STA_NOINIT;
75     }
76 
77     return 0;
78 }
79 
80 /*!
81  * @brief Read Sector(s) from RAM disk.
82  */
ram_disk_read(BYTE pdrv,BYTE * buff,DWORD sector,UINT count)83 DRESULT ram_disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
84 {
85     if (pdrv != RAMDISK) {
86         return RES_PARERR;
87     }
88 
89     memcpy(buff, disk_space + sector * SECTOR_SIZE, SECTOR_SIZE * count);
90     return RES_OK;
91 }
92 
93 /*!
94  * @brief Write Sector(s) to RAM disk.
95  */
ram_disk_write(BYTE pdrv,const BYTE * buff,DWORD sector,UINT count)96 DRESULT ram_disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
97 {
98     if (pdrv != RAMDISK) {
99         return RES_PARERR;
100     }
101 
102     memcpy(disk_space + sector * SECTOR_SIZE, buff, SECTOR_SIZE * count);
103     return RES_OK;
104 }
105 
106 /*!
107  * @brief Miscellaneous RAM disk Functions.
108  */
ram_disk_ioctl(BYTE pdrv,BYTE cmd,void * buff)109 DRESULT ram_disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
110 {
111     if (pdrv != RAMDISK) {
112         return RES_PARERR;
113     }
114 
115     switch (cmd) {
116         case GET_SECTOR_COUNT:
117             *(uint32_t *)buff = DISK_SIZE / SECTOR_SIZE;
118             return RES_OK;
119             break;
120 
121         case GET_SECTOR_SIZE:
122             *(uint32_t *)buff = SECTOR_SIZE;
123             return RES_OK;
124             break;
125 
126         case CTRL_SYNC:
127             return RES_OK;
128             break;
129 
130         default:
131             break;
132     }
133 
134     return RES_PARERR;
135 }
136