Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lfs_emubd.c « emubd - github.com/littlefs-project/littlefs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c77945a76e4bec56d288f5ae90d159e8413ec49 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Block device emulated on standard files
 *
 * Copyright (c) 2017, Arm Limited. All rights reserved.
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include "emubd/lfs_emubd.h"

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>


// Emulated block device utils
static inline void lfs_emubd_tole32(lfs_emubd_t *emu) {
    emu->cfg.read_size     = lfs_tole32(emu->cfg.read_size);
    emu->cfg.prog_size     = lfs_tole32(emu->cfg.prog_size);
    emu->cfg.block_size    = lfs_tole32(emu->cfg.block_size);
    emu->cfg.block_count   = lfs_tole32(emu->cfg.block_count);

    emu->stats.read_count  = lfs_tole32(emu->stats.read_count);
    emu->stats.prog_count  = lfs_tole32(emu->stats.prog_count);
    emu->stats.erase_count = lfs_tole32(emu->stats.erase_count);

    for (unsigned i = 0; i < sizeof(emu->history.blocks) /
            sizeof(emu->history.blocks[0]); i++) {
        emu->history.blocks[i] = lfs_tole32(emu->history.blocks[i]);
    }
}

static inline void lfs_emubd_fromle32(lfs_emubd_t *emu) {
    emu->cfg.read_size     = lfs_fromle32(emu->cfg.read_size);
    emu->cfg.prog_size     = lfs_fromle32(emu->cfg.prog_size);
    emu->cfg.block_size    = lfs_fromle32(emu->cfg.block_size);
    emu->cfg.block_count   = lfs_fromle32(emu->cfg.block_count);

    emu->stats.read_count  = lfs_fromle32(emu->stats.read_count);
    emu->stats.prog_count  = lfs_fromle32(emu->stats.prog_count);
    emu->stats.erase_count = lfs_fromle32(emu->stats.erase_count);

    for (unsigned i = 0; i < sizeof(emu->history.blocks) /
            sizeof(emu->history.blocks[0]); i++) {
        emu->history.blocks[i] = lfs_fromle32(emu->history.blocks[i]);
    }
}


// Block device emulated on existing filesystem
int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
    lfs_emubd_t *emu = cfg->context;
    emu->cfg.read_size   = cfg->read_size;
    emu->cfg.prog_size   = cfg->prog_size;
    emu->cfg.block_size  = cfg->block_size;
    emu->cfg.block_count = cfg->block_count;

    // Allocate buffer for creating children files
    size_t pathlen = strlen(path);
    emu->path = malloc(pathlen + 1 + LFS_NAME_MAX + 1);
    if (!emu->path) {
        return -ENOMEM;
    }

    strcpy(emu->path, path);
    emu->path[pathlen] = '/';
    emu->child = &emu->path[pathlen+1];
    memset(emu->child, '\0', LFS_NAME_MAX+1);

    // Create directory if it doesn't exist
    int err = mkdir(path, 0777);
    if (err && errno != EEXIST) {
        return -errno;
    }

    // Load stats to continue incrementing
    snprintf(emu->child, LFS_NAME_MAX, ".stats");
    FILE *f = fopen(emu->path, "r");
    if (!f) {
        memset(&emu->stats, LFS_EMUBD_ERASE_VALUE, sizeof(emu->stats));
    } else {
        size_t res = fread(&emu->stats, sizeof(emu->stats), 1, f);
        lfs_emubd_fromle32(emu);
        if (res < 1) {
            return -errno;
        }

        err = fclose(f);
        if (err) {
            return -errno;
        }
    }

    // Load history
    snprintf(emu->child, LFS_NAME_MAX, ".history");
    f = fopen(emu->path, "r");
    if (!f) {
        memset(&emu->history, 0, sizeof(emu->history));
    } else {
        size_t res = fread(&emu->history, sizeof(emu->history), 1, f);
        lfs_emubd_fromle32(emu);
        if (res < 1) {
            return -errno;
        }

        err = fclose(f);
        if (err) {
            return -errno;
        }
    }

    return 0;
}

void lfs_emubd_destroy(const struct lfs_config *cfg) {
    lfs_emubd_sync(cfg);

    lfs_emubd_t *emu = cfg->context;
    free(emu->path);
}

int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
        lfs_off_t off, void *buffer, lfs_size_t size) {
    lfs_emubd_t *emu = cfg->context;
    uint8_t *data = buffer;

    // Check if read is valid
    assert(off  % cfg->read_size == 0);
    assert(size % cfg->read_size == 0);
    assert(block < cfg->block_count);

    // Zero out buffer for debugging
    memset(data, 0, size);

    // Read data
    snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);

    FILE *f = fopen(emu->path, "rb");
    if (!f && errno != ENOENT) {
        return -errno;
    }

    if (f) {
        int err = fseek(f, off, SEEK_SET);
        if (err) {
            return -errno;
        }

        size_t res = fread(data, 1, size, f);
        if (res < size && !feof(f)) {
            return -errno;
        }

        err = fclose(f);
        if (err) {
            return -errno;
        }
    }

    emu->stats.read_count += 1;
    return 0;
}

int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
        lfs_off_t off, const void *buffer, lfs_size_t size) {
    lfs_emubd_t *emu = cfg->context;
    const uint8_t *data = buffer;

    // Check if write is valid
    assert(off  % cfg->prog_size == 0);
    assert(size % cfg->prog_size == 0);
    assert(block < cfg->block_count);

    // Program data
    snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);

    FILE *f = fopen(emu->path, "r+b");
    if (!f) {
        return (errno == EACCES) ? 0 : -errno;
    }

    // Check that file was erased
    assert(f);

    int err = fseek(f, off, SEEK_SET);
    if (err) {
        return -errno;
    }

    size_t res = fwrite(data, 1, size, f);
    if (res < size) {
        return -errno;
    }

    err = fseek(f, off, SEEK_SET);
    if (err) {
        return -errno;
    }

    uint8_t dat;
    res = fread(&dat, 1, 1, f);
    if (res < 1) {
        return -errno;
    }

    err = fclose(f);
    if (err) {
        return -errno;
    }

    // update history and stats
    if (block != emu->history.blocks[0]) {
        memcpy(&emu->history.blocks[1], &emu->history.blocks[0],
                sizeof(emu->history) - sizeof(emu->history.blocks[0]));
        emu->history.blocks[0] = block;
    }

    emu->stats.prog_count += 1;
    return 0;
}

int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
    lfs_emubd_t *emu = cfg->context;

    // Check if erase is valid
    assert(block < cfg->block_count);

    // Erase the block
    snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
    struct stat st;
    int err = stat(emu->path, &st);
    if (err && errno != ENOENT) {
        return -errno;
    }

    if (!err && S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode)) {
        err = unlink(emu->path);
        if (err) {
            return -errno;
        }
    }

    if (err || (S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode))) {
        FILE *f = fopen(emu->path, "w");
        if (!f) {
            return -errno;
        }

        err = fclose(f);
        if (err) {
            return -errno;
        }
    }

    emu->stats.erase_count += 1;
    return 0;
}

int lfs_emubd_sync(const struct lfs_config *cfg) {
    lfs_emubd_t *emu = cfg->context;

    // Just write out info/stats for later lookup
    snprintf(emu->child, LFS_NAME_MAX, ".config");
    FILE *f = fopen(emu->path, "w");
    if (!f) {
        return -errno;
    }

    lfs_emubd_tole32(emu);
    size_t res = fwrite(&emu->cfg, sizeof(emu->cfg), 1, f);
    lfs_emubd_fromle32(emu);
    if (res < 1) {
        return -errno;
    }

    int err = fclose(f);
    if (err) {
        return -errno;
    }

    snprintf(emu->child, LFS_NAME_MAX, ".stats");
    f = fopen(emu->path, "w");
    if (!f) {
        return -errno;
    }

    lfs_emubd_tole32(emu);
    res = fwrite(&emu->stats, sizeof(emu->stats), 1, f);
    lfs_emubd_fromle32(emu);
    if (res < 1) {
        return -errno;
    }

    err = fclose(f);
    if (err) {
        return -errno;
    }

    snprintf(emu->child, LFS_NAME_MAX, ".history");
    f = fopen(emu->path, "w");
    if (!f) {
        return -errno;
    }

    lfs_emubd_tole32(emu);
    res = fwrite(&emu->history, sizeof(emu->history), 1, f);
    lfs_emubd_fromle32(emu);
    if (res < 1) {
        return -errno;
    }

    err = fclose(f);
    if (err) {
        return -errno;
    }

    return 0;
}