|
| 1 | +#include <loadcore.h> |
| 2 | +#include <iomanX.h> |
| 3 | +#include <errno.h> |
| 4 | +#include <string.h> |
| 5 | +#include <sysclib.h> |
| 6 | +#include <stdint.h> |
| 7 | +#include <bdm.h> |
| 8 | +#include <usbhdfsd-common.h> |
| 9 | +#include "mprintf.h" |
| 10 | + |
| 11 | +#define MODNAME "bdfs" |
| 12 | +IRX_ID(MODNAME, 1, 1); |
| 13 | + |
| 14 | +uint64_t file_offset = 0; |
| 15 | +uint64_t file_size = 0; |
| 16 | +struct block_device *bd = NULL; |
| 17 | + |
| 18 | +int bdfs_init(iomanX_iop_device_t *d) |
| 19 | +{ |
| 20 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 21 | + // Return succes |
| 22 | + return 0; |
| 23 | +} |
| 24 | +int bdfs_deinit(iomanX_iop_device_t *d) |
| 25 | +{ |
| 26 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 27 | + return 0; |
| 28 | +} |
| 29 | +int bdfs_format(iomanX_iop_file_t *f, const char *unk1, const char *unk2, void *unk3, int unk4) |
| 30 | +{ |
| 31 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 32 | + return -EIO; |
| 33 | +} |
| 34 | +int bdfs_open(iomanX_iop_file_t *f, const char *name, int mode, int unk) |
| 35 | +{ |
| 36 | + struct block_device *pbd[10]; |
| 37 | + char bdname[10]; |
| 38 | + int i; |
| 39 | + |
| 40 | + M_DEBUG("%s(%s, 0x%x)\n", __FUNCTION__, name, mode); |
| 41 | + |
| 42 | + while (name[0] == '/' || name[0] == '\\') { |
| 43 | + M_DEBUG("ignoring leading '/'\n"); |
| 44 | + name++; |
| 45 | + } |
| 46 | + |
| 47 | + file_offset = 0; |
| 48 | + file_size = 0; |
| 49 | + bd = NULL; |
| 50 | + |
| 51 | + bdm_get_bd(pbd, 10); |
| 52 | + for (i = 0; i < 10; i++) { |
| 53 | + sprintf(bdname, "%s%dp%d", pbd[i]->name, pbd[i]->devNr, pbd[i]->parNr); |
| 54 | + M_DEBUG("- BD[%d] = %s\n", i, bdname); |
| 55 | + if (strcmp(bdname, name) == 0) { |
| 56 | + bd = pbd[i]; |
| 57 | + file_size = pbd[i]->sectorCount * 512; |
| 58 | + return 1; // the 1 and only file descriptor, for now... |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return -EIO; |
| 63 | +} |
| 64 | +int bdfs_close(iomanX_iop_file_t *f) |
| 65 | +{ |
| 66 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +u8 sector_buffer[512]; |
| 71 | +int bdfs_read(iomanX_iop_file_t *f, void *buffer, int size) |
| 72 | +{ |
| 73 | + M_DEBUG("%s(0x%x, %d)\n", __FUNCTION__, buffer, size); |
| 74 | + |
| 75 | + int size_left = size; |
| 76 | + while (size_left > 0) { |
| 77 | + unsigned int file_sector = file_offset / 512; |
| 78 | + unsigned int file_sector_offset = file_offset & 511; |
| 79 | + int size_read = size_left; |
| 80 | + |
| 81 | + if (size_read > (512 - file_sector_offset)) |
| 82 | + size_read = 512 - file_sector_offset; |
| 83 | + |
| 84 | + M_DEBUG("reading %d bytes from lba %d, offset %d\n", size_read, file_sector, file_sector_offset); |
| 85 | + |
| 86 | + if (size_read == 512) { |
| 87 | + // Read whole sector |
| 88 | + if (bd->read(bd, file_sector, buffer, 1) != 1) |
| 89 | + return -EIO; |
| 90 | + } |
| 91 | + else { |
| 92 | + // Read part of sector via bounce buffer |
| 93 | + if (bd->read(bd, file_sector, sector_buffer, 1) != 1) |
| 94 | + return -EIO; |
| 95 | + memcpy(buffer, sector_buffer + file_sector_offset, size_read); |
| 96 | + } |
| 97 | + |
| 98 | + buffer += size_read; |
| 99 | + size_left -= size_read; |
| 100 | + file_offset += size_read; |
| 101 | + } |
| 102 | + return size; |
| 103 | +} |
| 104 | +int bdfs_write(iomanX_iop_file_t *f, void *buffer, int size) |
| 105 | +{ |
| 106 | + M_DEBUG("%s(0x%x, %d) - not supported\n", __FUNCTION__, buffer, size); |
| 107 | + return -EIO; |
| 108 | +} |
| 109 | +s64 bdfs_lseek64(iomanX_iop_file_t *, s64 offset, int whence); |
| 110 | +int bdfs_lseek(iomanX_iop_file_t *f, int offset, int whence) |
| 111 | +{ |
| 112 | + M_DEBUG("%s(%d, %d)\n", __FUNCTION__, offset, whence); |
| 113 | + return bdfs_lseek64(f, offset, whence); |
| 114 | +} |
| 115 | +int bdfs_ioctl(iomanX_iop_file_t *f, int, void *) |
| 116 | +{ |
| 117 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 118 | + return -EIO; |
| 119 | +} |
| 120 | +int bdfs_remove(iomanX_iop_file_t *f, const char *) |
| 121 | +{ |
| 122 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 123 | + return -EIO; |
| 124 | +} |
| 125 | +int bdfs_mkdir(iomanX_iop_file_t *f, const char *path, int unk) |
| 126 | +{ |
| 127 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 128 | + return -EIO; |
| 129 | +} |
| 130 | +int bdfs_rmdir(iomanX_iop_file_t *f, const char *path) |
| 131 | +{ |
| 132 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 133 | + return -EIO; |
| 134 | +} |
| 135 | +int bdfs_dopen(iomanX_iop_file_t *f, const char *path) |
| 136 | +{ |
| 137 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 138 | + return -EIO; |
| 139 | +} |
| 140 | +int bdfs_dclose(iomanX_iop_file_t *f) |
| 141 | +{ |
| 142 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 143 | + return -EIO; |
| 144 | +} |
| 145 | +int bdfs_dread(iomanX_iop_file_t *f, iox_dirent_t *dirent) |
| 146 | +{ |
| 147 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 148 | + return -EIO; |
| 149 | +} |
| 150 | +int bdfs_getstat(iomanX_iop_file_t *f, const char *name, iox_stat_t *stat) |
| 151 | +{ |
| 152 | + M_DEBUG("%s()\n", __FUNCTION__); |
| 153 | + return -EIO; |
| 154 | +} |
| 155 | +int bdfs_chstat(iomanX_iop_file_t *f, const char *name, iox_stat_t *stat, unsigned int) |
| 156 | +{ |
| 157 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 158 | + return -EIO; |
| 159 | +} |
| 160 | +/* Extended ops start here. */ |
| 161 | +int bdfs_rename(iomanX_iop_file_t *, const char *, const char *) |
| 162 | +{ |
| 163 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 164 | + return -EIO; |
| 165 | +} |
| 166 | +int bdfs_chdir(iomanX_iop_file_t *, const char *) |
| 167 | +{ |
| 168 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 169 | + return -EIO; |
| 170 | +} |
| 171 | +int bdfs_sync(iomanX_iop_file_t *, const char *, int) |
| 172 | +{ |
| 173 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 174 | + return -EIO; |
| 175 | +} |
| 176 | +int bdfs_mount(iomanX_iop_file_t *, const char *, const char *, int, void *, int) |
| 177 | +{ |
| 178 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 179 | + return -EIO; |
| 180 | +} |
| 181 | +int bdfs_umount(iomanX_iop_file_t *, const char *) |
| 182 | +{ |
| 183 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 184 | + return -EIO; |
| 185 | +} |
| 186 | +s64 bdfs_lseek64(iomanX_iop_file_t *, s64 offset, int whence) |
| 187 | +{ |
| 188 | + M_DEBUG("%s(%d, %d)\n", __FUNCTION__, offset, whence); |
| 189 | + |
| 190 | + switch (whence) { |
| 191 | + case SEEK_SET: file_offset = offset; break; |
| 192 | + case SEEK_CUR: file_offset += offset; break; |
| 193 | + case SEEK_END: file_offset = file_size + offset; break; |
| 194 | + default: |
| 195 | + return -1; |
| 196 | + } |
| 197 | + |
| 198 | + return file_offset; |
| 199 | +} |
| 200 | +int bdfs_devctl(iomanX_iop_file_t *, const char *, int, void *, unsigned int, void *, unsigned int) |
| 201 | +{ |
| 202 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 203 | + return -EIO; |
| 204 | +} |
| 205 | +int bdfs_symlink(iomanX_iop_file_t *, const char *, const char *) |
| 206 | +{ |
| 207 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 208 | + return -EIO; |
| 209 | +} |
| 210 | +int bdfs_readlink(iomanX_iop_file_t *, const char *, char *, unsigned int) |
| 211 | +{ |
| 212 | + M_DEBUG("%s() - not supported\n", __FUNCTION__); |
| 213 | + return -EIO; |
| 214 | +} |
| 215 | +int bdfs_ioctl2(iomanX_iop_file_t *, int cmd, void *data, unsigned int datalen, void *rdata, unsigned int rdatalen) |
| 216 | +{ |
| 217 | + int ret = -EINVAL; |
| 218 | + |
| 219 | + M_DEBUG("%s(%d)\n", __FUNCTION__, cmd); |
| 220 | + |
| 221 | + switch (cmd) { |
| 222 | + case USBMASS_IOCTL_GET_DRIVERNAME: |
| 223 | + ret = *(int *)bd->name; |
| 224 | + break; |
| 225 | + case USBMASS_IOCTL_GET_FRAGLIST: |
| 226 | + // Check for a return buffer and copy the device number. If no buffer is provided return an error. |
| 227 | + if (rdata == NULL || rdatalen < sizeof(bd_fragment_t)) |
| 228 | + { |
| 229 | + ret = -EINVAL; |
| 230 | + break; |
| 231 | + } |
| 232 | + // Block device always has 1 fragment |
| 233 | + { |
| 234 | + bd_fragment_t *f = (bd_fragment_t*)rdata; |
| 235 | + f[0].sector = bd->sectorOffset; |
| 236 | + f[0].count = bd->sectorCount; |
| 237 | + } |
| 238 | + ret = 1; |
| 239 | + break; |
| 240 | + case USBMASS_IOCTL_GET_DEVICE_NUMBER: |
| 241 | + { |
| 242 | + // Check for a return buffer and copy the device number. If no buffer is provided return an error. |
| 243 | + if (rdata == NULL || rdatalen < sizeof(u32)) |
| 244 | + { |
| 245 | + ret = -EINVAL; |
| 246 | + break; |
| 247 | + } |
| 248 | + *(u32*)rdata = bd->devNr; |
| 249 | + ret = 0; |
| 250 | + break; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return ret; |
| 255 | +} |
| 256 | + |
| 257 | +iomanX_iop_device_ops_t bdfs_device_ops = { |
| 258 | + bdfs_init, |
| 259 | + bdfs_deinit, |
| 260 | + bdfs_format, |
| 261 | + bdfs_open, |
| 262 | + bdfs_close, |
| 263 | + bdfs_read, |
| 264 | + bdfs_write, |
| 265 | + bdfs_lseek, |
| 266 | + bdfs_ioctl, |
| 267 | + bdfs_remove, |
| 268 | + bdfs_mkdir, |
| 269 | + bdfs_rmdir, |
| 270 | + bdfs_dopen, |
| 271 | + bdfs_dclose, |
| 272 | + bdfs_dread, |
| 273 | + bdfs_getstat, |
| 274 | + bdfs_chstat, |
| 275 | + bdfs_rename, |
| 276 | + bdfs_chdir, |
| 277 | + bdfs_sync, |
| 278 | + bdfs_mount, |
| 279 | + bdfs_umount, |
| 280 | + bdfs_lseek64, |
| 281 | + bdfs_devctl, |
| 282 | + bdfs_symlink, |
| 283 | + bdfs_readlink, |
| 284 | + bdfs_ioctl2 |
| 285 | +}; |
| 286 | + |
| 287 | +const char bdfs_fs_name[] = "bdfs"; |
| 288 | +iomanX_iop_device_t bdfs_device = { |
| 289 | + bdfs_fs_name, |
| 290 | + IOP_DT_FSEXT | IOP_DT_FS, |
| 291 | + 1, |
| 292 | + bdfs_fs_name, |
| 293 | + &bdfs_device_ops}; |
| 294 | + |
| 295 | +int _start(int argc, char *argv[]) |
| 296 | +{ |
| 297 | + M_DEBUG("Block Device read-only filesystem driver\n"); |
| 298 | + |
| 299 | + AddDrv(&bdfs_device); |
| 300 | + |
| 301 | + return MODULE_RESIDENT_END; |
| 302 | +} |
0 commit comments