Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a0f8538864
Fetching contributors…

Cannot retrieve contributors at this time

file 286 lines (221 sloc) 6.206 kb
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
/*
* FFS plugin for Custom IOS.
*
* Copyright (C) 2009-2010 Waninkoko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>

#include "fat.h"
#include "ipc.h"
#include "isfs.h"
#include "syscalls.h"
#include "tools.h"
#include "types.h"

/* IOCTL commands */
#define IOCTL_FAT_FILESTATS 11

/* IOCTLV commands */
#define IOCTL_FAT_MKDIR 0x01
#define IOCTL_FAT_MKFILE 0x02
#define IOCTL_FAT_READDIR 0x03
#define IOCTL_FAT_READDIR_LFN 0x04
#define IOCTL_FAT_DELETE 0x05
#define IOCTL_FAT_DELETEDIR 0x06
#define IOCTL_FAT_RENAME 0x07
#define IOCTL_FAT_STATS 0x08
#define IOCTL_FAT_GETUSAGE 0x09
#define IOCTL_FAT_MOUNT_SD 0xF0
#define IOCTL_FAT_UMOUNT_SD 0xF1
#define IOCTL_FAT_MOUNT_USB 0xF2
#define IOCTL_FAT_UMOUNT_USB 0xF3

/* FAT structure */
typedef struct {
ioctlv vector[8];

union {
char filename[FAT_MAXPATH];

struct fstats fstats;

struct {
char filename[FAT_MAXPATH];
s32 mode;
} open;

struct {
char filename[FAT_MAXPATH];
s32 entries;
} dir;

struct {
char oldname[FAT_MAXPATH];
char newname[FAT_MAXPATH];
} rename;

struct {
char filename[FAT_MAXPATH];
struct stats stats;
} stats;

struct {
char filename[FAT_MAXPATH];
u64 size;
u8 padding[24];
u32 files;
} usage;
};
} ATTRIBUTE_PACKED fatBuf;


/* Variables */
static s32 fatFd = 0;

/* I/O buffer */
static fatBuf *iobuf = NULL;


s32 FAT_Init(void)
{
/* FAT module already open */
if (fatFd > 0)
return 0;

/* Allocate memory */
if (!iobuf) {
iobuf = os_heap_alloc_aligned(0, sizeof(*iobuf), 32);
if (!iobuf)
return IPC_ENOMEM;
}

/* Open FAT module */
fatFd = os_open("fat", 0);
if (fatFd < 0)
return fatFd;

return 0;
}

s32 FAT_CreateDir(const char *dirpath)
{
/* Copy path */
strcpy(iobuf->filename, dirpath);

/* Setup vector */
iobuf->vector[0].data = iobuf->filename;
iobuf->vector[0].len = FAT_MAXPATH;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Create directory */
return os_ioctlv(fatFd, IOCTL_FAT_MKDIR, 1, 0, iobuf->vector);
}

s32 FAT_CreateFile(const char *filepath)
{
/* Copy path */
strcpy(iobuf->filename, filepath);

/* Setup vector */
iobuf->vector[0].data = iobuf->filename;
iobuf->vector[0].len = FAT_MAXPATH;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Create file */
return os_ioctlv(fatFd, IOCTL_FAT_MKFILE, 1, 0, iobuf->vector);
}

s32 FAT_ReadDir(const char *dirpath, void *outbuf, u32 *entries)
{
u32 in = 1, io = 1;
s32 ret;

/* Copy path */
strcpy(iobuf->dir.filename, dirpath);

/* Setup vector */
iobuf->vector[0].data = iobuf->dir.filename;
iobuf->vector[0].len = FAT_MAXPATH;
iobuf->vector[1].data = &iobuf->dir.entries;
iobuf->vector[1].len = 4;

if (outbuf) {
u32 cnt = *entries;

/* Input/Output buffers */
in = io = 2;

/* Entries value */
iobuf->dir.entries = cnt;

/* Setup vector */
iobuf->vector[2].data = outbuf;
iobuf->vector[2].len = (FAT_MAXPATH * cnt);
iobuf->vector[3].data = &iobuf->dir.entries;
iobuf->vector[3].len = 4;
}

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Read directory */
ret = os_ioctlv(fatFd, IOCTL_FAT_READDIR, in, io, iobuf->vector);

/* Copy data */
if (ret >= 0)
*entries = iobuf->dir.entries;

return ret;
}

s32 FAT_Delete(const char *path)
{
/* Copy path */
strcpy(iobuf->filename, path);

/* Setup vector */
iobuf->vector[0].data = iobuf->filename;
iobuf->vector[0].len = FAT_MAXPATH;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Delete */
return os_ioctlv(fatFd, IOCTL_FAT_DELETE, 1, 0, iobuf->vector);
}

s32 FAT_DeleteDir(const char *dirpath)
{
/* Copy path */
strcpy(iobuf->filename, dirpath);

/* Setup vector */
iobuf->vector[0].data = iobuf->filename;
iobuf->vector[0].len = FAT_MAXPATH;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Delete directory */
return os_ioctlv(fatFd, IOCTL_FAT_DELETEDIR, 1, 0, iobuf->vector);
}

s32 FAT_Rename(const char *oldpath, const char *newpath)
{
/* Copy paths */
strcpy(iobuf->rename.oldname, oldpath);
strcpy(iobuf->rename.newname, newpath);

/* Setup vector */
iobuf->vector[0].data = iobuf->rename.oldname;
iobuf->vector[0].len = FAT_MAXPATH;
iobuf->vector[1].data = iobuf->rename.newname;
iobuf->vector[1].len = FAT_MAXPATH;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Rename */
return os_ioctlv(fatFd, IOCTL_FAT_RENAME, 2, 0, iobuf->vector);
}

s32 FAT_GetStats(const char *path, struct stats *stats)
{
s32 ret;

/* Copy path */
strcpy(iobuf->stats.filename, path);

/* Setup vector */
iobuf->vector[0].data = iobuf->stats.filename;
iobuf->vector[0].len = FAT_MAXPATH;
iobuf->vector[1].data = &iobuf->stats.stats;
iobuf->vector[1].len = sizeof(struct stats);

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Get stats */
ret = os_ioctlv(fatFd, IOCTL_FAT_STATS, 1, 1, iobuf->vector);

/* Copy data */
if (ret >= 0 && stats)
memcpy(stats, &iobuf->stats.stats, sizeof(struct stats));

return ret;
}

s32 FAT_GetUsage(const char *path, u32 *blocks, u32 *inodes)
{
s32 ret;

/* Copy path */
strcpy(iobuf->usage.filename, path);

/* Setup vector */
iobuf->vector[0].data = iobuf->usage.filename;
iobuf->vector[0].len = FAT_MAXPATH;
iobuf->vector[1].data = &iobuf->usage.size;
iobuf->vector[1].len = 8;
iobuf->vector[2].data = &iobuf->usage.files;
iobuf->vector[2].len = 4;

os_sync_after_write(iobuf, sizeof(*iobuf));

/* Get usage */
ret = os_ioctlv(fatFd, IOCTL_FAT_GETUSAGE, 1, 2, iobuf->vector);

/* Copy data */
if (ret >= 0) {
*blocks = (iobuf->usage.size / 0x4000);
*inodes = (iobuf->usage.files) ? iobuf->usage.files : 1;
}

return ret;
}
Something went wrong with that request. Please try again.