Files
Decrypt9/source/fs.c
T
Shadowtrance 6ad9fd39a3 Improved Ttitle Decryptor
o Implemented Seed crypto
o Fixed directory parsing
o Improved the on screen output
2015-07-21 00:56:02 +10:00

201 lines
4.0 KiB
C

#include "fs.h"
#include "draw.h"
#include <stdio.h>
#include <string.h>
#include "fatfs/ff.h"
static FATFS fs;
static FIL file;
static DIR dir;
bool InitFS()
{
#ifndef EXEC_GATEWAY
// TODO: Magic?
*(u32*)0x10000020 = 0;
*(u32*)0x10000020 = 0x340;
#endif
bool ret = (f_mount(&fs, "0:", 0) == FR_OK);
#ifdef WORKDIR
f_mkdir(WORKDIR);
f_chdir(WORKDIR);
#endif
return ret;
}
void DeinitFS()
{
f_mount(NULL, "0:", 0);
}
bool FileOpen(const char* path)
{
unsigned flags = FA_READ | FA_WRITE | FA_OPEN_EXISTING;
bool ret = (f_open(&file, path, flags) == FR_OK);
f_lseek(&file, 0);
f_sync(&file);
return ret;
}
bool DebugFileOpen(const char* path) {
Debug("Opening %s ...", path);
if (!FileOpen(path)) {
Debug("Could not open %s!", path);
return false;
}
return true;
}
bool FileCreate(const char* path, bool truncate)
{
unsigned flags = FA_READ | FA_WRITE;
flags |= truncate ? FA_CREATE_ALWAYS : FA_OPEN_ALWAYS;
bool ret = (f_open(&file, path, flags) == FR_OK);
f_lseek(&file, 0);
f_sync(&file);
return ret;
}
bool DebugFileCreate(const char* path, bool truncate) {
Debug("Creating %s ...", path);
if (!FileCreate(path, truncate)) {
Debug("Could not create %s!", path);
return false;
}
return true;
}
size_t FileRead(void* buf, size_t size, size_t foffset)
{
UINT bytes_read = 0;
f_lseek(&file, foffset);
f_read(&file, buf, size, &bytes_read);
return bytes_read;
}
bool DebugFileRead(void* buf, size_t size, size_t foffset) {
size_t bytesRead = FileRead(buf, size, foffset);
if(bytesRead != size) {
Debug("ERROR, file is too small!");
return false;
}
return true;
}
size_t FileWrite(void* buf, size_t size, size_t foffset)
{
UINT bytes_written = 0;
f_lseek(&file, foffset);
f_write(&file, buf, size, &bytes_written);
f_sync(&file);
return bytes_written;
}
bool DebugFileWrite(void* buf, size_t size, size_t foffset) {
size_t bytesWritten = FileWrite(buf, size, foffset);
if(bytesWritten != size) {
Debug("ERROR, SD card may be full!");
return false;
}
return true;
}
size_t FileGetSize()
{
return f_size(&file);
}
void FileClose()
{
f_close(&file);
}
bool DirMake(const char* path)
{
FRESULT res = f_mkdir(path);
bool ret = (res == FR_OK) || (res == FR_EXIST);
return ret;
}
bool DirOpen(const char* path)
{
bool ret = (f_opendir(&dir, path) == FR_OK);
return ret;
}
bool DebugDirOpen(const char* path) {
Debug("Opening %s ...", path);
if (!DirOpen(path)) {
Debug("Could not open %s!", path);
return false;
}
return true;
}
bool DirRead(char* fname, int fsize)
{
FILINFO fno;
fno.lfname = fname;
fno.lfsize = fsize;
bool ret = false;
while (f_readdir(&dir, &fno) == FR_OK) {
if (fno.fname[0] == 0) break;
if ((fno.fname[0] != '.') && !(fno.fattrib & AM_DIR)) {
if (fname[0] == 0)
strcpy(fname, fno.fname);
ret = true;
break;
}
}
return ret;
}
void DirClose()
{
f_closedir(&dir);
}
static uint64_t ClustersToBytes(FATFS* fs, DWORD clusters)
{
uint64_t sectors = clusters * fs->csize;
#if _MAX_SS != _MIN_SS
return sectors * fs->ssize;
#else
return sectors * _MAX_SS;
#endif
}
uint64_t RemainingStorageSpace()
{
DWORD free_clusters;
FATFS *fs2;
FRESULT res = f_getfree("0:", &free_clusters, &fs2);
if (res)
return -1;
return ClustersToBytes(&fs, free_clusters);
}
bool FileOpenSplash(const char* path)
{
unsigned flags = FA_READ | FA_WRITE | FA_OPEN_EXISTING;
bool ret = (f_open(&file, path, flags) == FR_OK);
f_lseek(&file, 0);
f_sync(&file);
return ret;
}
size_t FileReadSplash(void* buf, size_t size, size_t foffset)
{
UINT bytes_read = 0;
f_lseek(&file, foffset);
f_read(&file, buf, size, &bytes_read);
return bytes_read;
}