Skip to content

Commit

Permalink
Implement some missing fs functions (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkosto authored and fincs committed Jul 28, 2018
1 parent 27b8868 commit d726c7c
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
17 changes: 17 additions & 0 deletions nx/include/switch/services/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ Result fsMount_SaveData(FsFileSystem* out, u64 titleID, u128 userID);
/// WARNING: You can brick when writing to SystemSaveData, if the data is corrupted etc.
Result fsMount_SystemSaveData(FsFileSystem* out, u64 saveID);

typedef enum
{
FsFileSystemType_Logo = 2,
FsFileSystemType_ContentControl = 3,
FsFileSystemType_ContentManual = 4,
FsFileSystemType_ContentMeta = 5,
FsFileSystemType_ContentData = 6,
FsFileSystemType_ApplicationPackage = 7
} FsFileSystemType;

/// Mount requested filesystem type from content file
Result fsOpenFileSystem(FsFileSystem* out, u64 titleId, FsFileSystemType fsType); /// only on 1.0.0, only works with registered content
Result fsOpenFileSystemWithId(FsFileSystem* out, u64 titleId, FsFileSystemType fsType, const char* contentPath); /// 2.0.0+, contentPath must be resolved manually

// IFileSystem
Result fsFsCreateFile(FsFileSystem* fs, const char* path, size_t size, int flags);
Result fsFsDeleteFile(FsFileSystem* fs, const char* path);
Expand Down Expand Up @@ -200,6 +214,9 @@ void fsDirClose(FsDir* d);

// IStorage
Result fsStorageRead(FsStorage* s, u64 off, void* buf, size_t len);
Result fsStorageWrite(FsStorage* s, u64 off, const void* buf, size_t len);
Result fsStorageFlush(FsStorage* s);
Result fsStorageSetSize(FsStorage* s, u64 sz);
Result fsStorageGetSize(FsStorage* s, u64* out);
void fsStorageClose(FsStorage* s);

Expand Down
182 changes: 182 additions & 0 deletions nx/source/services/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,88 @@ Result fsMount_SystemSaveData(FsFileSystem* out, u64 saveID) {
return fsMountSystemSaveData(out, FsSaveDataSpaceId_NandSystem, &save);
}

Result fsOpenFileSystem(FsFileSystem* out, u64 titleId, FsFileSystemType fsType) {
IpcCommand c;
ipcInitialize(&c);

struct {
u64 magic;
u64 cmd_id;
u32 fsType;
u64 titleId;
} *raw;

raw = ipcPrepareHeader(&c, sizeof(*raw));

raw->magic = SFCI_MAGIC;
raw->cmd_id = 0;
raw->fsType = fsType;
raw->titleId = titleId;

Result rc = serviceIpcDispatch(&g_fsSrv);

if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);

struct {
u64 magic;
u64 result;
} *resp = r.Raw;

rc = resp->result;

if (R_SUCCEEDED(rc)) {
serviceCreate(&out->s, r.Handles[0]);
}
}

return rc;
}

Result fsOpenFileSystemWithId(FsFileSystem* out, u64 titleId, FsFileSystemType fsType, const char* contentPath) {
char sendStr[FS_MAX_PATH] = {0};
strncpy(sendStr, contentPath, sizeof(sendStr)-1);

IpcCommand c;
ipcInitialize(&c);
ipcAddSendStatic(&c, sendStr, sizeof(sendStr), 0);

struct {
u64 magic;
u64 cmd_id;
u32 fsType;
u64 titleId;
} *raw;

raw = ipcPrepareHeader(&c, sizeof(*raw));

raw->magic = SFCI_MAGIC;
raw->cmd_id = 8;
raw->fsType = fsType;
raw->titleId = titleId;

Result rc = serviceIpcDispatch(&g_fsSrv);

if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);

struct {
u64 magic;
u64 result;
} *resp = r.Raw;

rc = resp->result;

if (R_SUCCEEDED(rc)) {
serviceCreate(&out->s, r.Handles[0]);
}
}

return rc;
}

// IFileSystem impl
Result fsFsCreateFile(FsFileSystem* fs, const char* path, size_t size, int flags) {
IpcCommand c;
Expand Down Expand Up @@ -1216,6 +1298,106 @@ Result fsStorageRead(FsStorage* s, u64 off, void* buf, size_t len) {
return rc;
}

Result fsStorageWrite(FsStorage* s, u64 off, const void* buf, size_t len) {
IpcCommand c;
ipcInitialize(&c);
ipcAddSendBuffer(&c, buf, len, 1);

struct {
u64 magic;
u64 cmd_id;
u64 offset;
u64 write_size;
} *raw;

raw = ipcPrepareHeader(&c, sizeof(*raw));

raw->magic = SFCI_MAGIC;
raw->cmd_id = 1;
raw->offset = off;
raw->write_size = len;

Result rc = serviceIpcDispatch(&s->s);

if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);

struct {
u64 magic;
u64 result;
} *resp = r.Raw;

rc = resp->result;
}

return rc;
}

Result fsStorageFlush(FsStorage* s) {
IpcCommand c;
ipcInitialize(&c);

struct {
u64 magic;
u64 cmd_id;
} *raw;

raw = ipcPrepareHeader(&c, sizeof(*raw));

raw->magic = SFCI_MAGIC;
raw->cmd_id = 2;

Result rc = serviceIpcDispatch(&s->s);

if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);

struct {
u64 magic;
u64 result;
} *resp = r.Raw;

rc = resp->result;
}

return rc;
}

Result fsStorageSetSize(FsStorage* s, u64 sz) {
IpcCommand c;
ipcInitialize(&c);

struct {
u64 magic;
u64 cmd_id;
u64 size;
} *raw;

raw = ipcPrepareHeader(&c, sizeof(*raw));

raw->magic = SFCI_MAGIC;
raw->cmd_id = 3;
raw->size = sz;

Result rc = serviceIpcDispatch(&s->s);

if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);

struct {
u64 magic;
u64 result;
} *resp = r.Raw;

rc = resp->result;
}

return rc;
}

Result fsStorageGetSize(FsStorage* s, u64* out) {
IpcCommand c;
ipcInitialize(&c);
Expand Down

0 comments on commit d726c7c

Please sign in to comment.