1 /*
2 * Copyright (c) 2023-2024 Embedded Solutions GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/disk.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/fs/fs_interface.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/sys/util.h>
12
13 #include <zephyr/drivers/loopback_disk.h>
14
15 LOG_MODULE_REGISTER(loopback_disk_access, CONFIG_LOOPBACK_DISK_LOG_LEVEL);
16
17 #define LOOPBACK_SECTOR_SIZE CONFIG_LOOPBACK_DISK_SECTOR_SIZE
18
get_ctx(struct disk_info * info)19 static inline struct loopback_disk_access *get_ctx(struct disk_info *info)
20 {
21 return CONTAINER_OF(info, struct loopback_disk_access, info);
22 }
23
loopback_disk_access_status(struct disk_info * disk)24 static int loopback_disk_access_status(struct disk_info *disk)
25 {
26 return DISK_STATUS_OK;
27 }
loopback_disk_access_read(struct disk_info * disk,uint8_t * data_buf,uint32_t start_sector,uint32_t num_sector)28 static int loopback_disk_access_read(struct disk_info *disk, uint8_t *data_buf,
29 uint32_t start_sector, uint32_t num_sector)
30 {
31 struct loopback_disk_access *ctx = get_ctx(disk);
32
33 int ret = fs_seek(&ctx->file, start_sector * LOOPBACK_SECTOR_SIZE, FS_SEEK_SET);
34
35 if (ret != 0) {
36 LOG_ERR("Failed to seek backing file: %d", ret);
37 return ret;
38 }
39
40 const size_t total_len = num_sector * LOOPBACK_SECTOR_SIZE;
41 size_t len_left = total_len;
42
43 while (len_left > 0) {
44 ret = fs_read(&ctx->file, data_buf, len_left);
45 if (ret < 0) {
46 LOG_ERR("Failed to read from backing file: %d", ret);
47 return ret;
48 }
49 if (ret == 0) {
50 LOG_WRN("Tried to read past end of backing file");
51 return -EIO;
52 }
53 __ASSERT(ret <= len_left,
54 "fs_read returned more than we asked for: %d instead of %ld", ret,
55 len_left);
56 len_left -= ret;
57 }
58
59 return 0;
60 }
loopback_disk_access_write(struct disk_info * disk,const uint8_t * data_buf,uint32_t start_sector,uint32_t num_sector)61 static int loopback_disk_access_write(struct disk_info *disk, const uint8_t *data_buf,
62 uint32_t start_sector, uint32_t num_sector)
63 {
64 struct loopback_disk_access *ctx = get_ctx(disk);
65
66 if (start_sector + num_sector > ctx->num_sectors) {
67 LOG_WRN("Tried to write past end of backing file");
68 return -EIO;
69 }
70
71 int ret = fs_seek(&ctx->file, start_sector * LOOPBACK_SECTOR_SIZE, FS_SEEK_SET);
72
73 if (ret != 0) {
74 LOG_ERR("Failed to seek backing file: %d", ret);
75 return ret;
76 }
77
78 const size_t total_len = num_sector * LOOPBACK_SECTOR_SIZE;
79 size_t buf_offset = 0;
80
81 while (buf_offset < total_len) {
82 ret = fs_write(&ctx->file, &data_buf[buf_offset], total_len - buf_offset);
83 if (ret < 0) {
84 LOG_ERR("Failed to write to backing file: %d", ret);
85 return ret;
86 }
87 if (ret == 0) {
88 LOG_ERR("0-byte write to backing file");
89 return -EIO;
90 }
91 buf_offset += ret;
92 }
93
94 return 0;
95 }
loopback_disk_access_erase(struct disk_info * disk,uint32_t start_sector,uint32_t num_sector)96 static int loopback_disk_access_erase(struct disk_info *disk, uint32_t start_sector,
97 uint32_t num_sector)
98 {
99 const uint8_t erase_bytes[CONFIG_LOOPBACK_DISK_SECTOR_SIZE] = { 0x00 };
100 struct loopback_disk_access *ctx = get_ctx(disk);
101
102 if (start_sector + num_sector > ctx->num_sectors) {
103 LOG_WRN("Tried to erase past end of backing file");
104 return -EINVAL;
105 }
106
107 int ret = fs_seek(&ctx->file, start_sector * LOOPBACK_SECTOR_SIZE, FS_SEEK_SET);
108
109 if (ret != 0) {
110 LOG_ERR("Failed to seek backing file: %d", ret);
111 return ret;
112 }
113
114 for (int i = 0; i < num_sector; i++) {
115 ret = fs_write(&ctx->file, erase_bytes, LOOPBACK_SECTOR_SIZE);
116 if (ret < 0) {
117 LOG_ERR("Failed to erase backing file: %d", ret);
118 return ret;
119 }
120 }
121
122 return 0;
123 }
loopback_disk_access_ioctl(struct disk_info * disk,uint8_t cmd,void * buff)124 static int loopback_disk_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
125 {
126 struct loopback_disk_access *ctx = get_ctx(disk);
127
128 switch (cmd) {
129 case DISK_IOCTL_GET_SECTOR_COUNT: {
130 *(uint32_t *)buff = ctx->num_sectors;
131 return 0;
132 }
133 case DISK_IOCTL_GET_SECTOR_SIZE: {
134 *(uint32_t *)buff = LOOPBACK_SECTOR_SIZE;
135 return 0;
136 }
137 case DISK_IOCTL_CTRL_DEINIT:
138 case DISK_IOCTL_CTRL_SYNC:
139 return fs_sync(&ctx->file);
140 case DISK_IOCTL_CTRL_INIT:
141 return 0;
142 default:
143 return -ENOTSUP;
144 }
145 }
loopback_disk_access_init(struct disk_info * disk)146 static int loopback_disk_access_init(struct disk_info *disk)
147 {
148 return loopback_disk_access_ioctl(disk, DISK_IOCTL_CTRL_INIT, NULL);
149 }
150
151 static const struct disk_operations loopback_disk_operations = {
152 .init = loopback_disk_access_init,
153 .status = loopback_disk_access_status,
154 .read = loopback_disk_access_read,
155 .write = loopback_disk_access_write,
156 .erase = loopback_disk_access_erase,
157 .ioctl = loopback_disk_access_ioctl,
158 };
159
loopback_disk_access_register(struct loopback_disk_access * ctx,const char * file_path,const char * disk_access_name)160 int loopback_disk_access_register(struct loopback_disk_access *ctx, const char *file_path,
161 const char *disk_access_name)
162 {
163 ctx->file_path = file_path;
164
165 ctx->info.name = disk_access_name;
166 ctx->info.ops = &loopback_disk_operations;
167
168 struct fs_dirent entry;
169 int ret = fs_stat(ctx->file_path, &entry);
170
171 if (ret != 0) {
172 LOG_ERR("Failed to stat backing file: %d", ret);
173 return ret;
174 }
175 if (entry.size % LOOPBACK_SECTOR_SIZE != 0) {
176 LOG_WRN("Backing file is not a multiple of sector size (%d bytes), "
177 "rounding down: %ld bytes",
178 LOOPBACK_SECTOR_SIZE, entry.size);
179 }
180 ctx->num_sectors = entry.size / LOOPBACK_SECTOR_SIZE;
181
182 fs_file_t_init(&ctx->file);
183 ret = fs_open(&ctx->file, ctx->file_path, FS_O_READ | FS_O_WRITE);
184 if (ret != 0) {
185 LOG_ERR("Failed to open backing file: %d", ret);
186 return ret;
187 }
188
189 ret = disk_access_register(&ctx->info);
190 if (ret != 0) {
191 LOG_ERR("Failed to register disk access: %d", ret);
192 return ret;
193 }
194
195 return 0;
196 }
197
loopback_disk_access_unregister(struct loopback_disk_access * ctx)198 int loopback_disk_access_unregister(struct loopback_disk_access *ctx)
199 {
200 int ret;
201
202 ret = disk_access_unregister(&ctx->info);
203 if (ret != 0) {
204 LOG_ERR("Failed to unregister disk access: %d", ret);
205 return ret;
206 }
207 ctx->info.name = NULL;
208 ctx->info.ops = NULL;
209
210 ret = fs_close(&ctx->file);
211 if (ret != 0) {
212 LOG_ERR("Failed to close backing file: %d", ret);
213 return ret;
214 }
215
216 return 0;
217 }
218