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