summaryrefslogtreecommitdiff
path: root/src/sys/wii/isfs.c
blob: 6168864ba533168393e5eb1e866605ed8a9df642 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*

libisfs -- a NAND filesystem devoptab library for the Wii

Copyright (C) 2008 Joseph Jordan <joe.ftpii@psychlaw.com.au>

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1.The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.

2.Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3.This notice may not be removed or altered from any source distribution.


[In compliance with the above: I patched this code up somewhat so that it
builds with all warnings. -- Storlek]
*/
#include <errno.h>
#include <ogc/isfs.h>
#include <ogc/lwp_watchdog.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dir.h>
#include <sys/iosupport.h>

#include "isfs.h"

#define DEVICE_NAME "isfs"

#define FLAG_DIR 1
#define DIR_SEPARATOR '/'
#define SECTOR_SIZE 0x800
#define BUFFER_SIZE 0x8000

#define UNUSED __attribute__((unused))

typedef struct DIR_ENTRY_STRUCT {
    char *name;
    char *abspath;
    u32 size;
    u8 flags;
    u32 fileCount;
    struct DIR_ENTRY_STRUCT *children;
} DIR_ENTRY;

typedef struct {
    DIR_ENTRY *entry;
    s32 isfs_fd;
    bool inUse;
} FILE_STRUCT;

typedef struct {
    DIR_ENTRY *entry;
    u32 index;
    bool inUse;
} DIR_STATE_STRUCT;

static char read_buffer[BUFFER_SIZE] __attribute__((aligned(32)));

static DIR_ENTRY *root = NULL;
static DIR_ENTRY *current = NULL;
static s32 dotab_device = -1;

static bool is_dir(DIR_ENTRY *entry) {
    return entry->flags & FLAG_DIR;
}

static bool invalid_drive_specifier(const char *path) {
    if (strchr(path, ':') == NULL) return false;
    int namelen = strlen(DEVICE_NAME);
    if (!strncmp(DEVICE_NAME, path, namelen) && path[namelen] == ':') return false;
    return true;
}

static DIR_ENTRY *entry_from_path(const char *path) {
    if (invalid_drive_specifier(path)) return NULL;
    if (strchr(path, ':') != NULL) path = strchr(path, ':') + 1;
    DIR_ENTRY *entry;
    bool found = false;
    bool notFound = false;
    const char *pathPosition = path;
    const char *pathEnd = strchr(path, '\0');
    if (pathPosition[0] == DIR_SEPARATOR) {
	entry = root;
	while (pathPosition[0] == DIR_SEPARATOR) pathPosition++;
	if (pathPosition >= pathEnd) found = true;
    } else {
	entry = current;
    }
    if (entry == root && !strcmp(".", pathPosition)) found = true;
    DIR_ENTRY *dir = entry;
    while (!found && !notFound) {
	const char *nextPathPosition = strchr(pathPosition, DIR_SEPARATOR);
	size_t dirnameLength;
	if (nextPathPosition != NULL) dirnameLength = nextPathPosition - pathPosition;
	else dirnameLength = strlen(pathPosition);
	if (dirnameLength >= ISFS_MAXPATHLEN) return NULL;

	u32 fileIndex = 0;
	while (fileIndex < dir->fileCount && !found && !notFound) {
	    entry = &dir->children[fileIndex];
	    if (dirnameLength == strnlen(entry->name, ISFS_MAXPATHLEN - 1)
		&& !strncasecmp(pathPosition, entry->name, dirnameLength)) found = true;
	    if (found && !is_dir(entry) && nextPathPosition) found = false;
	    if (!found) fileIndex++;
	}

	if (fileIndex >= dir->fileCount) {
	    notFound = true;
	    found = false;
	} else if (!nextPathPosition || nextPathPosition >= pathEnd) {
	    found = true;
	} else if (is_dir(entry)) {
	    dir = entry;
	    pathPosition = nextPathPosition;
	    while (pathPosition[0] == DIR_SEPARATOR) pathPosition++;
	    if (pathPosition >= pathEnd) found = true;
	    else found = false;
	}
    }

    if (found && !notFound) return entry;
    return NULL;
}

static int _ISFS_open_r(struct _reent *r, void *fileStruct, const char *path,
			UNUSED int flags, UNUSED int mode) {
    FILE_STRUCT *file = (FILE_STRUCT *)fileStruct;
    DIR_ENTRY *entry = entry_from_path(path);
    if (!entry) {
	r->_errno = ENOENT;
	return -1;
    } else if (is_dir(entry)) {
	r->_errno = EISDIR;
	return -1;
    }

    file->entry = entry;
    file->inUse = true;
    file->isfs_fd = ISFS_Open(entry->abspath, ISFS_OPEN_READ);
    if (file->isfs_fd < 0) {
	if (file->isfs_fd == ISFS_EINVAL) r->_errno = EACCES;
	else r->_errno = -file->isfs_fd;
	return -1;
    }

    return (int)file;
}

static int _ISFS_close_r(struct _reent *r, int fd) {
    FILE_STRUCT *file = (FILE_STRUCT *)fd;
    if (!file->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    file->inUse = false;

    s32 ret = ISFS_Close(file->isfs_fd);
    if (ret < 0) {
	r->_errno = -ret;
	return -1;
    }

    return 0;
}

static int _ISFS_read_r(struct _reent *r, int fd, char *ptr, size_t len) {
    FILE_STRUCT *file = (FILE_STRUCT *)fd;
    if (!file->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    if (len <= 0) {
	return 0;
    }

    s32 ret = ISFS_Read(file->isfs_fd, read_buffer, len);
    if (ret < 0) {
	r->_errno = -ret;
	return -1;
    } else if ((size_t) ret < len) {
	r->_errno = EOVERFLOW;
    }
    
    memcpy(ptr, read_buffer, ret);
    return ret;
}

static off_t _ISFS_seek_r(struct _reent *r, int fd, off_t pos, int dir) {
    FILE_STRUCT *file = (FILE_STRUCT *)fd;
    if (!file->inUse) {
	r->_errno = EBADF;
	return -1;
    }

    s32 ret = ISFS_Seek(file->isfs_fd, pos, dir);
    if (ret < 0) {
	r->_errno = -ret;
	return -1;
    }
    return ret;
}

static void stat_entry(DIR_ENTRY *entry, struct stat *st) {
    st->st_dev = 0x4957;
    st->st_ino = 0;
    st->st_mode = ((is_dir(entry)) ? S_IFDIR : S_IFREG) | (S_IRUSR | S_IRGRP | S_IROTH);
    st->st_nlink = 1;
    st->st_uid = 1;
    st->st_gid = 2;
    st->st_rdev = st->st_dev;
    st->st_size = entry->size;
    st->st_atime = 0;
    st->st_spare1 = 0;
    st->st_mtime = 0;
    st->st_spare2 = 0;
    st->st_ctime = 0;
    st->st_spare3 = 0;
    st->st_blksize = SECTOR_SIZE;
    st->st_blocks = (entry->size + SECTOR_SIZE - 1) / SECTOR_SIZE;
    st->st_spare4[0] = 0;
    st->st_spare4[1] = 0;
}

static int _ISFS_fstat_r(struct _reent *r, int fd, struct stat *st) {
    FILE_STRUCT *file = (FILE_STRUCT *)fd;
    if (!file->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    stat_entry(file->entry, st);
    return 0;
}

static int _ISFS_stat_r(struct _reent *r, const char *path, struct stat *st) {
    DIR_ENTRY *entry = entry_from_path(path);
    if (!entry) {
	r->_errno = ENOENT;
	return -1;
    }
    stat_entry(entry, st);
    return 0;
}

static int _ISFS_chdir_r(struct _reent *r, const char *path) {
    DIR_ENTRY *entry = entry_from_path(path);
    if (!entry) {
	r->_errno = ENOENT;
	return -1;
    } else if (!is_dir(entry)) {
	r->_errno = ENOTDIR;
	return -1;
    }
    return 0;
}

static DIR_ITER *_ISFS_diropen_r(struct _reent *r, DIR_ITER *dirState, const char *path) {
    DIR_STATE_STRUCT *state = (DIR_STATE_STRUCT *)(dirState->dirStruct);
    state->entry = entry_from_path(path);
    if (!state->entry) {
	r->_errno = ENOENT;
	return NULL;
    } else if (!is_dir(state->entry)) {
	r->_errno = ENOTDIR;
	return NULL;
    }
    state->index = 0;
    state->inUse = true;
    return dirState;
}

static int _ISFS_dirreset_r(struct _reent *r, DIR_ITER *dirState) {
    DIR_STATE_STRUCT *state = (DIR_STATE_STRUCT *)(dirState->dirStruct);
    if (!state->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    state->index = 0;
    return 0;
}

static int _ISFS_dirnext_r(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st) {
    DIR_STATE_STRUCT *state = (DIR_STATE_STRUCT *)(dirState->dirStruct);
    if (!state->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    if (state->index >= state->entry->fileCount) {
	r->_errno = ENOENT;
	return -1;
    }
    DIR_ENTRY *entry = &state->entry->children[state->index++];
    strncpy(filename, entry->name, ISFS_MAXPATHLEN - 1);
    stat_entry(entry, st);
    return 0;
}

static int _ISFS_dirclose_r(struct _reent *r, DIR_ITER *dirState) {
    DIR_STATE_STRUCT *state = (DIR_STATE_STRUCT *)(dirState->dirStruct);
    if (!state->inUse) {
	r->_errno = EBADF;
	return -1;
    }
    state->inUse = false;
    return 0;
}

static const devoptab_t dotab_isfs = {
    DEVICE_NAME,
    sizeof(FILE_STRUCT),
    _ISFS_open_r,
    _ISFS_close_r,
    NULL,
    _ISFS_read_r,
    _ISFS_seek_r,
    _ISFS_fstat_r,
    _ISFS_stat_r,
    NULL,
    NULL,
    _ISFS_chdir_r,
    NULL,
    NULL,
    sizeof(DIR_STATE_STRUCT),
    _ISFS_diropen_r,
    _ISFS_dirreset_r,
    _ISFS_dirnext_r,
    _ISFS_dirclose_r,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};

static DIR_ENTRY *add_child_entry(DIR_ENTRY *dir, const char *name) {
    DIR_ENTRY *newChildren = realloc(dir->children, (dir->fileCount + 1) * sizeof(DIR_ENTRY));
    if (!newChildren) return NULL;
    bzero(newChildren + dir->fileCount, sizeof(DIR_ENTRY));
    dir->children = newChildren;
    DIR_ENTRY *child = &dir->children[dir->fileCount++];
    child->name = strdup(name);
    if (!child->name) return NULL;
    child->abspath = malloc(strlen(dir->abspath) + (dir != root) + strlen(name) + 1);
    if (!child->abspath) return NULL;
    sprintf(child->abspath, "%s/%s", dir == root ? "" : dir->abspath, name);
    return child;
}

static bool read_recursive(DIR_ENTRY *parent) {
    u32 fileCount;
    s32 ret = ISFS_ReadDir(parent->abspath, NULL, &fileCount);
    if (ret != ISFS_OK) {
	s32 fd = ISFS_Open(parent->abspath, ISFS_OPEN_READ);
	if (fd >= 0) {
	    static fstats st __attribute__((aligned(32)));
	    if (ISFS_GetFileStats(fd, &st) == ISFS_OK) parent->size = st.file_length;
	    ISFS_Close(fd);
	}
	return true;
    }
    parent->flags = FLAG_DIR;
    if (fileCount > 0) {
	if ((ISFS_MAXPATHLEN * fileCount) > BUFFER_SIZE) return false;
	ret = ISFS_ReadDir(parent->abspath, read_buffer, &fileCount);
	if (ret != ISFS_OK) return false;
	u32 fileNum;
	char *name = read_buffer;
	for (fileNum = 0; fileNum < fileCount; fileNum++) {
	    DIR_ENTRY *child = add_child_entry(parent, name);
	    if (!child) return false;
	    name += strlen(name) + 1;
	}
	for (fileNum = 0; fileNum < fileCount; fileNum++)
	    if (!read_recursive(parent->children + fileNum))
		return false;
    }
    return true;
}

static bool read_isfs() {
    root = malloc(sizeof(DIR_ENTRY));
    if (!root) return false;
    bzero(root, sizeof(DIR_ENTRY));
    current = root;
    root->name = strdup("/");
    if (!root->name) return false;
    root->abspath = strdup("/");
    if (!root->abspath) return false;
    return read_recursive(root);
}

static void cleanup_recursive(DIR_ENTRY *entry) {
    u32 i;
    for (i = 0; i < entry->fileCount; i++) cleanup_recursive(&entry->children[i]);
    if (entry->children) free(entry->children);
    if (entry->name) free(entry->name);
    if (entry->abspath) free(entry->abspath);
}

bool ISFS_Mount() {
    ISFS_Unmount();
    bool success = read_isfs() && (dotab_device = AddDevice(&dotab_isfs)) >= 0;
    if (!success) ISFS_Unmount();
    return success;
}

bool ISFS_Unmount() {
    if (root) {
	cleanup_recursive(root);
	free(root);
	root = NULL;
    }
    current = root;
    if (dotab_device >= 0) {
	dotab_device = -1;
	return !RemoveDevice(DEVICE_NAME ":");
    }
    return true;
}

#include "certs_bin.h"
#include "su_tik_bin.h"
#include "su_tmd_bin.h"

s32 ISFS_SU() {
    u32 key = 0;
    return ES_Identify((signed_blob *) certs_bin, sizeof(certs_bin),
		       (signed_blob *) su_tmd_bin, sizeof(su_tmd_bin),
		       (signed_blob *) su_tik_bin, sizeof(su_tik_bin),
		       &key);
}
© All Rights Reserved