-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfs_init_loopback.c
58 lines (54 loc) · 1.76 KB
/
fs_init_loopback.c
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
/*
* Copyright 2024, Hiroyuki OYAMA. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <hardware/flash.h>
#include "blockdevice/flash.h"
#include "blockdevice/loopback.h"
#include "filesystem/fat.h"
#include "filesystem/littlefs.h"
#include "filesystem/vfs.h"
#define FLASH_LENGTH_ALL 0
#define LOOPBACK_FILE_SIZE (640 * 1024)
#define LOOPBACK_BLOCK_SIZE 512
bool fs_init(void) {
// mount onboard flash
blockdevice_t *device = blockdevice_flash_create(PICO_FLASH_SIZE_BYTES - PICO_FS_DEFAULT_SIZE, FLASH_LENGTH_ALL);
filesystem_t *fs = filesystem_littlefs_create(500, 16);
int err = fs_mount("/flash", fs, device);
if (err == -1) {
printf("format /flash\n");
err = fs_format(fs, device);
if (err == -1) {
printf("fs_format error: %s", strerror(errno));
return false;
}
err = fs_mount("/flash", fs, device);
if (err == -1) {
printf("fs_mount error: %s", strerror(errno));
return false;
}
}
// mount loopback device
printf("fs_init FAT on loopback on littlefs\n");
blockdevice_t *loopback = blockdevice_loopback_create("/flash/disk-image.dmg", LOOPBACK_FILE_SIZE, LOOPBACK_BLOCK_SIZE);
filesystem_t *fat = filesystem_fat_create();
err = fs_mount("/", fat, loopback);
if (err == -1) {
printf("format / with FAT\n");
err = fs_format(fat, loopback);
if (err == -1) {
printf("fs_format error: %s", strerror(errno));
return false;
}
err = fs_mount("/", fat, loopback);
if (err == -1) {
printf("fs_mount error: %s", strerror(errno));
return false;
}
}
return true;
}