aboutsummaryrefslogtreecommitdiffstats
path: root/os/various/ramdisk.c
blob: 23bf658021e9da2bbbf13acd87e6a2dc6094a920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
    ChibiOS/HAL - Copyright (C) 2016 Uladzimir Pylinsky aka barthess

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    ramdisk.c
 * @brief   Virtual block devise driver source.
 *
 * @addtogroup ramdisk
 * @{
 */

#include "hal.h"

#include "ramdisk.h"

#include <string.h>

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*
 * Interface implementation.
 */
static bool overflow(const RamDisk *rd, uint32_t startblk, uint32_t n) {
  return (startblk + n) > rd->blk_num;
}

static bool is_inserted(void *instance) {
  (void)instance;
  return true;
}

static bool is_protected(void *instance) {
  RamDisk *rd = instance;
  if (BLK_READY == rd->state) {
    return rd->readonly;
  }
  else {
    return true;
  }
}

static bool connect(void *instance) {
  RamDisk *rd = instance;
  if (BLK_STOP == rd->state) {
    rd->state = BLK_READY;
  }
  return HAL_SUCCESS;
}

static bool disconnect(void *instance) {
  RamDisk *rd = instance;
  if (BLK_STOP != rd->state) {
    rd->state = BLK_STOP;
  }
  return HAL_SUCCESS;
}

static bool read(void *instance, uint32_t startblk,
                 uint8_t *buffer, uint32_t n) {

  RamDisk *rd = instance;

  if (overflow(rd, startblk, n)) {
    return HAL_FAILED;
  }
  else {
    const uint32_t bs = rd->blk_size;
    memcpy(buffer, &rd->storage[startblk * bs], n * bs);
    return HAL_SUCCESS;
  }
}

static bool write(void *instance, uint32_t startblk,
                const uint8_t *buffer, uint32_t n) {

  RamDisk *rd = instance;
  if (overflow(rd, startblk, n)) {
    return HAL_FAILED;
  }
  else {
    const uint32_t bs = rd->blk_size;
    memcpy(&rd->storage[startblk * bs], buffer, n * bs);
    return HAL_SUCCESS;
  }
}

static bool sync(void *instance) {

  RamDisk *rd = instance;
  if (BLK_READY != rd->state) {
    return HAL_FAILED;
  }
  else {
    return HAL_SUCCESS;
  }
}

static bool get_info(void *instance, BlockDeviceInfo *bdip) {

  RamDisk *rd = instance;
  if (BLK_READY != rd->state) {
    return HAL_FAILED;
  }
  else {
    bdip->blk_num = rd->blk_num;
    bdip->blk_size = rd->blk_size;
    return HAL_SUCCESS;
  }
}

/**
 *
 */
static const struct BaseBlockDeviceVMT vmt = {
    is_inserted,
    is_protected,
    connect,
    disconnect,
    read,
    write,
    sync,
    get_info
};

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   RAM disk object initialization.
 *
 * @param[in] rdp   pointer to @p RamDisk object
 *
 * @init
 */
void ramdiskObjectInit(RamDisk *rdp) {

  rdp->vmt = &vmt;
  rdp->state = BLK_STOP;
}

/**
 * @brief   Starts RAM disk.
 *
 * @param[in] rdp       pointer to @p RamDisk object
 * @param[in] storage   pointer to array representing disk storage
 * @param[in] blksize   size of blocks in bytes
 * @param[in] blknum    total number of blocks in device
 * @param[in] readonly  read only flag
 *
 * @api
 */
void ramdiskStart(RamDisk *rdp, uint8_t *storage, uint32_t blksize,
                  uint32_t blknum, bool readonly) {

  osalDbgCheck(rdp != NULL);

  osalSysLock();
  osalDbgAssert((rdp->state == BLK_STOP) || (rdp->state == BLK_READY),
                "invalid state");
  rdp->blk_num  = blknum;
  rdp->blk_size = blksize;
  rdp->readonly = readonly;
  rdp->storage  = storage;
  rdp->state    = BLK_READY;
  osalSysUnlock();
}

/**
 * @brief   Stops RAM disk.
 *
 * @param[in] rdp       pointer to @p RamDisk object
 *
 * @api
 */
void ramdiskStop(RamDisk *rdp) {

  osalDbgCheck(rdp != NULL);

  osalSysLock();
  osalDbgAssert((rdp->state == BLK_STOP) || (rdp->state == BLK_READY),
                "invalid state");
  rdp->storage = NULL;
  rdp->state = BLK_STOP;
  osalSysUnlock();
}

/** @} */