[ftpii] r289 committed - Initial commit of libseeprom

13 views
Skip to first unread message

codesite...@google.com

unread,
Feb 12, 2010, 7:22:54 PM2/12/10
to ftpii-...@googlegroups.com
Revision: 289
Author: joe.g...@psychlaw.com.au
Date: Fri Feb 12 16:22:04 2010
Log: Initial commit of libseeprom

http://code.google.com/p/ftpii/source/detail?r=289

Added:
/seeprom
/seeprom/trunk
/seeprom/trunk/LICENCE.TXT
/seeprom/trunk/Makefile
/seeprom/trunk/README.TXT
/seeprom/trunk/seeprom_example
/seeprom/trunk/seeprom_example/Makefile
/seeprom/trunk/seeprom_example/seeprom_example.c
/seeprom/trunk/source
/seeprom/trunk/source/mini_seeprom.c
/seeprom/trunk/source/mini_seeprom.h
/seeprom/trunk/source/seeprom.c
/seeprom/trunk/source/seeprom.h

=======================================
--- /dev/null
+++ /seeprom/trunk/LICENCE.TXT Fri Feb 12 16:22:04 2010
@@ -0,0 +1,19 @@
+Copyright (C) 2008 Joseph Jordan <joe....@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.
=======================================
--- /dev/null
+++ /seeprom/trunk/Makefile Fri Feb 12 16:22:04 2010
@@ -0,0 +1,153 @@
+#---------------------------------------------------------------------------------
+# Clear the implicit built in rules
+#---------------------------------------------------------------------------------
+.SUFFIXES:
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(DEVKITPPC)),)
+$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path
to>devkitPPC")
+endif
+
+include $(DEVKITPPC)/wii_rules
+
+#---------------------------------------------------------------------------------
+# TARGET is the name of the output
+# BUILD is the directory where object files & intermediate files will be
placed
+# SOURCES is a list of directories containing source code
+# INCLUDES is a list of directories containing extra header files
+#---------------------------------------------------------------------------------
+TARGET := seeprom
+BUILD := build
+SOURCES := source
+DATA := data
+INCLUDES := $(DEVKITPRO)/libogc
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+
+CFLAGS = -save-temps -g -O2 -Wall $(MACHDEP) $(INCLUDE)
+CXXFLAGS = $(CFLAGS)
+ASFLAGS = $(INCLUDE) -D_LANGUAGE_ASSEMBLY
+
+LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
+ARFLAGS = rcs
+
+#---------------------------------------------------------------------------------
+# any extra libraries we wish to link with the project
+#---------------------------------------------------------------------------------
+LIBS :=
+
+#---------------------------------------------------------------------------------
+# list of directories containing libraries, this must be the top level
containing
+# include and lib
+#---------------------------------------------------------------------------------
+LIBDIRS := $(DEVKITPRO)/libogc
+
+#---------------------------------------------------------------------------------
+# no real need to edit anything past this point unless you need to add
additional
+# rules for different file extensions
+#---------------------------------------------------------------------------------
+ifneq ($(BUILD),$(notdir $(CURDIR)))
+#---------------------------------------------------------------------------------
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+
+export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
+ $(foreach dir,$(DATA),$(CURDIR)/$(dir))
+
+export DEPSDIR := $(CURDIR)/$(BUILD)
+
+#---------------------------------------------------------------------------------
+# automatically build a list of object files for our project
+#---------------------------------------------------------------------------------
+CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
+CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
+sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
+SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
+BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
+
+#---------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+ export LD := $(CC)
+else
+ export LD := $(CXX)
+endif
+
+export OFILES := $(addsuffix .o,$(BINFILES)) \
+ $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
+ $(sFILES:.s=.o) $(SFILES:.S=.o)
+
+#---------------------------------------------------------------------------------
+# build a list of include paths
+#---------------------------------------------------------------------------------
+export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I$(CURDIR)/$(BUILD) \
+ -I$(LIBOGC_INC)
+
+#---------------------------------------------------------------------------------
+# build a list of library paths
+#---------------------------------------------------------------------------------
+export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
+ -L$(LIBOGC_LIB)
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+.PHONY: $(BUILD) clean
+
+#---------------------------------------------------------------------------------
+$(BUILD):
+ @[ -d $@ ] || mkdir -p $@
+ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr $(BUILD)
+ @rm -f lib$(TARGET).a
+#---------------------------------------------------------------------------------
+run:
+ wiiload $(OUTPUT).dol
+
+#---------------------------------------------------------------------------------
+
+install:
+ @echo Installing ...
+ @mkdir -p $(LIBOGC_INC)/$(TARGET)
+ @install -v -m 644 lib$(TARGET).a $(LIBOGC_LIB)
+ @install -v -m 644 source/$(TARGET).h $(LIBOGC_INC)/$(TARGET)
+#---------------------------------------------------------------------------------
+else
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+$(OUTPUT).a: $(OFILES)
+
+
+#---------------------------------------------------------------------------------
+# This rule links in binary data with the .bin extension
+#---------------------------------------------------------------------------------
+%.bin.o : %.bin
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ $(bin2o)
+
+
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------
+# This rule will create a library with all the object files
+#---------------------------------------------------------------------------------
+%.a:
+ @echo Archiving ... lib$(notdir $@)
+ @$(AR) $(ARFLAGS) ../lib$(notdir $@) $^
+ @echo $(TARGET)
+
+#---------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------
=======================================
--- /dev/null
+++ /seeprom/trunk/README.TXT Fri Feb 12 16:22:04 2010
@@ -0,0 +1,28 @@
+libseeprom -- a library that provides filesystem access to the Wii's
SEEPROM
+
+
+*** INSTALLATION ***
+
+make # build libseeprom.a
+make install # install libseeprom.a and seeprom/seeprom.h
+
+
+*** THANKS ***
+
+Thanks to those in EFNet #wiidev for all the help, and to all those
+who help make devkitPPC, libogc, libfat and The Homebrew Channel
+the great homebrew/development environment that it is:
+ bushing, marcan, chishm, shagkur, WinterMute and many others.
+
+
+*** CONTACT ***
+
+http://code.google.com/p/ftpii/
+
+libseeprom is written and maintained by Joe Jordan
<joe....@psychlaw.com.au>
+joedj @ EFNet #wiidev
+
+
+*** HISTORY ***
+
+For subversion changes, see http://code.google.com/p/ftpii/source/list
=======================================
--- /dev/null
+++ /seeprom/trunk/seeprom_example/Makefile Fri Feb 12 16:22:04 2010
@@ -0,0 +1,134 @@
+#---------------------------------------------------------------------------------
+# Clear the implicit built in rules
+#---------------------------------------------------------------------------------
+.SUFFIXES:
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(DEVKITPPC)),)
+$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path
to>devkitPPC)
+endif
+
+include $(DEVKITPPC)/wii_rules
+
+#---------------------------------------------------------------------------------
+# TARGET is the name of the output
+# BUILD is the directory where object files & intermediate files will be
placed
+# SOURCES is a list of directories containing source code
+# INCLUDES is a list of directories containing extra header files
+#---------------------------------------------------------------------------------
+TARGET := $(notdir $(CURDIR))
+BUILD := build
+SOURCES := ./
+DATA := data
+INCLUDES :=
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+
+CFLAGS = -g -O2 -mrvl -Wall $(MACHDEP) $(INCLUDE)
+CXXFLAGS = $(CFLAGS)
+
+LDFLAGS = -g $(MACHDEP) -mrvl -Wl,-Map,$(notdir $@).map
+
+#---------------------------------------------------------------------------------
+# any extra libraries we wish to link with the project
+#---------------------------------------------------------------------------------
+LIBS := -ldi -lseeprom -logc
+
+#---------------------------------------------------------------------------------
+# list of directories containing libraries, this must be the top level
containing
+# include and lib
+#---------------------------------------------------------------------------------
+LIBDIRS :=
+
+#---------------------------------------------------------------------------------
+# no real need to edit anything past this point unless you need to add
additional
+# rules for different file extensions
+#---------------------------------------------------------------------------------
+ifneq ($(BUILD),$(notdir $(CURDIR)))
+#---------------------------------------------------------------------------------
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+
+export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
+ $(foreach dir,$(DATA),$(CURDIR)/$(dir))
+
+export DEPSDIR := $(CURDIR)/$(BUILD)
+
+#---------------------------------------------------------------------------------
+# automatically build a list of object files for our project
+#---------------------------------------------------------------------------------
+CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
+CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
+sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
+SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
+BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
+
+#---------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+ export LD := $(CC)
+else
+ export LD := $(CXX)
+endif
+
+export OFILES := $(addsuffix .o,$(BINFILES)) \
+ $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
+ $(sFILES:.s=.o) $(SFILES:.S=.o)
+
+#---------------------------------------------------------------------------------
+# build a list of include paths
+#---------------------------------------------------------------------------------
+export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I$(CURDIR)/$(BUILD) \
+ -I$(LIBOGC_INC)
+
+#---------------------------------------------------------------------------------
+# build a list of library paths
+#---------------------------------------------------------------------------------
+export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
+ -L$(LIBOGC_LIB)
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+.PHONY: $(BUILD) clean
+
+#---------------------------------------------------------------------------------
+$(BUILD):
+ @[ -d $@ ] || mkdir -p $@
+ @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
+
+#---------------------------------------------------------------------------------
+run:
+ wiiload $(TARGET).dol
+
+#---------------------------------------------------------------------------------
+else
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+$(OUTPUT).dol: $(OUTPUT).elf
+$(OUTPUT).elf: $(OFILES)
+
+#---------------------------------------------------------------------------------
+# This rule links in binary data with the .jpg extension
+#---------------------------------------------------------------------------------
+%.jpg.o : %.jpg
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ $(bin2o)
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------
=======================================
--- /dev/null
+++ /seeprom/trunk/seeprom_example/seeprom_example.c Fri Feb 12 16:22:04
2010
@@ -0,0 +1,88 @@
+/*
+
+libseeprom-- a library that provides filesystem access to the Wii's SEEPROM
+
+Copyright (C) 2008 Joseph Jordan <joe....@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.
+
+*/
+#include <di/di.h>
+#include <errno.h>
+#include <ogcsys.h>
+#include <stdio.h>
+#include <sys/dir.h>
+#include <unistd.h>
+#include <seeprom/seeprom.h>
+
+static void initialise_video() {
+ VIDEO_Init();
+ GXRModeObj *rmode = VIDEO_GetPreferredMode(NULL);
+ VIDEO_Configure(rmode);
+ void *xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
+ VIDEO_SetNextFramebuffer(xfb);
+ VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
+ VIDEO_Flush();
+ VIDEO_WaitVSync();
+ if (rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();
+ CON_InitEx(rmode, 20, 30, rmode->fbWidth - 40, rmode->xfbHeight - 60);
+ CON_EnableGecko(1, 0);
+ VIDEO_SetBlack(FALSE);
+ VIDEO_Flush();
+ VIDEO_WaitVSync();
+ if (rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();
+}
+
+static bool initialise() {
+ printf("Initialising...\n");
+ return SEEPROM_Mount();
+}
+
+int main(int argc, char **argv) {
+ DI_Init();
+ initialise_video();
+ PAD_Init();
+
+ if (!initialise()) {
+ printf("Unable to initialise.\n");
+ return 1;
+ }
+
+ printf("Listing SEEPROM:/\n");
+ u32 fileCount = 0;
+ DIR_ITER *dir = diropen("seeprom:/");
+ if (!dir) {
+ printf("Unable to diropen(\"seeprom:/\"): %i\n", errno);
+ return 1;
+ }
+ char filename[SEEPROM_MAXPATHLEN];
+ struct stat st;
+ while (dirnext(dir, filename, &st) == 0) {
+ printf("%9llu %s\n", st.st_size, filename);
+ fileCount++;
+ }
+ printf("%u entries.\n", fileCount);
+
+ SEEPROM_Unmount();
+
+ printf("Exiting in 5 seconds...\n");
+ sleep(5);
+
+ return 0;
+}
=======================================
--- /dev/null
+++ /seeprom/trunk/source/mini_seeprom.c Fri Feb 12 16:22:04 2010
@@ -0,0 +1,79 @@
+/*
+ mini - a Free Software replacement for the Nintendo/BroadOn IOS.
+ SEEPROM support
+
+Copyright (C) 2008, 2009 Sven Peter <sven...@gmail.com>
+Copyright (C) 2008, 2009 Haxx Enterprises <bus...@gmail.com>
+Copyright (C) 2008, 2009 Hector Martin "marcan" <mar...@marcansoft.com>
+Copyright (C) 2008, 2009 John Kelley <wii...@kelley.ca>
+
+# This code is licensed to you under the terms of the GNU GPL, version 2;
+# see http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
+*/
+#include <ogc/machine/processor.h>
+#include <ogcsys.h>
+#include <unistd.h>
+
+#define HW_REG_BASE 0xd800000
+#define HW_GPIO1OUT (HW_REG_BASE + 0x0e0)
+#define HW_GPIO1IN (HW_REG_BASE + 0x0e8)
+
+enum {
+ GP_EEP_CS = 0x000400,
+ GP_EEP_CLK = 0x000800,
+ GP_EEP_MOSI = 0x001000,
+ GP_EEP_MISO = 0x002000
+};
+
+#define eeprom_delay() usleep(5)
+
+static void send_bits(int b, int bits) {
+ while (bits--) {
+ if (b & (1 << bits))
+ mask32(HW_GPIO1OUT, 0, GP_EEP_MOSI);
+ else
+ mask32(HW_GPIO1OUT, GP_EEP_MOSI, 0);
+ eeprom_delay();
+ mask32(HW_GPIO1OUT, 0, GP_EEP_CLK);
+ eeprom_delay();
+ mask32(HW_GPIO1OUT, GP_EEP_CLK, 0);
+ eeprom_delay();
+ }
+}
+
+static int recv_bits(int bits) {
+ int res = 0;
+ while (bits--) {
+ res <<= 1;
+ mask32(HW_GPIO1OUT, 0, GP_EEP_CLK);
+ eeprom_delay();
+ mask32(HW_GPIO1OUT, GP_EEP_CLK, 0);
+ eeprom_delay();
+ res |= !!(read32(HW_GPIO1IN) & GP_EEP_MISO);
+ }
+ return res;
+}
+
+int seeprom_read(void *dst, int offset, int size) {
+ int i;
+ u16 *ptr = (u16 *)dst;
+ u16 recv;
+
+ if (size & 1)
+ return -1;
+
+ mask32(HW_GPIO1OUT, GP_EEP_CLK, 0);
+ mask32(HW_GPIO1OUT, GP_EEP_CS, 0);
+ eeprom_delay();
+
+ for (i = 0; i < size; i++) {
+ mask32(HW_GPIO1OUT, 0, GP_EEP_CS);
+ send_bits((0x600 | (offset + i)), 11);
+ recv = recv_bits(16);
+ *ptr++ = recv;
+ mask32(HW_GPIO1OUT, GP_EEP_CS, 0);
+ eeprom_delay();
+ }
+
+ return size;
+}
=======================================
--- /dev/null
+++ /seeprom/trunk/source/mini_seeprom.h Fri Feb 12 16:22:04 2010
@@ -0,0 +1,15 @@
+/*
+ mini - a Free Software replacement for the Nintendo/BroadOn IOS.
+ SEEPROM support
+
+Copyright (C) 2008, 2009 Sven Peter <sven...@gmail.com>
+
+# This code is licensed to you under the terms of the GNU GPL, version 2;
+# see http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
+*/
+#ifndef _MINI_SEEPROM_H
+#define _MINI_SEEPROM_H
+
+int seeprom_read(void *dst, int offset, int size);
+
+#endif /* _MINI_SEEPROM_H */
=======================================
--- /dev/null
+++ /seeprom/trunk/source/seeprom.c Fri Feb 12 16:22:04 2010
@@ -0,0 +1,341 @@
+/*
+
+libseeprom-- a library that provides filesystem access to the Wii's SEEPROM
+
+Copyright (C) 2008 Joseph Jordan <joe....@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.
+
+*/
+#include <errno.h>
+#include <ogcsys.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/iosupport.h>
+
+#include "seeprom.h"
+#include "mini_seeprom.h"
+
+#define DEVICE_NAME "seeprom"
+
+#define SEEPROM_SIZE 256
+#define BLOCK_SIZE 2
+#define DIR_SEPARATOR '/'
+
+typedef struct {
+ char name[SEEPROM_MAXPATHLEN];
+ u32 size;
+ u8 addr;
+} DIR_ENTRY;
+
+typedef struct {
+ DIR_ENTRY *entry;
+ u32 offset;
+ bool inUse;
+} FILE_STRUCT;
+
+typedef struct {
+ DIR_ENTRY *entry;
+ u32 index;
+ bool inUse;
+} DIR_STATE_STRUCT;
+
+static DIR_ENTRY entries[] = {
+ { "", 0, 0 },
+ { "seeprom.bin", 256, 0 },
+ { "ms_id", 4, 0 },
+ { "ca_id", 4, 4 },
+ { "ng_id", 4, 8 },
+ { "ng_sig", 60, 12 },
+ { "counters1.1", 2, 72 },
+ { "counters1.2", 2, 74 },
+ { "counters1.3", 2, 76 },
+ { "counters1.4", 2, 78 },
+ { "counters1.5", 2, 80 },
+ { "counters2.1", 2, 92 },
+ { "counters2.2", 2, 94 },
+ { "counters2.3", 2, 96 },
+ { "korean_key", 16, 98 }
+};
+static const u32 FILE_COUNT = sizeof(entries) / sizeof(DIR_ENTRY);
+
+static u8 seeprom[SEEPROM_SIZE];
+static s32 dotab_device = -1;
+
+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;
+ const char *pathPosition = path;
+ const char *pathEnd = strchr(path, '\0');
+ if (pathPosition[0] == DIR_SEPARATOR) {
+ while (pathPosition[0] == DIR_SEPARATOR) pathPosition++;
+ if (pathPosition >= pathEnd) return &entries[0];
+ }
+ if (!strcmp(".", pathPosition)) return &entries[0];
+ u32 i;
+ for (i = 1; i < FILE_COUNT; i++)
+ if (!strcasecmp(pathPosition, entries[i].name))
+ return &entries[i];
+ return NULL;
+}
+
+static int _SEEPROM_open_r(struct _reent *r, void *fileStruct, const char
*path, int flags, int mode) {
+ FILE_STRUCT *file = (FILE_STRUCT *)fileStruct;
+ DIR_ENTRY *entry = entry_from_path(path);
+ if (!entry) {
+ r->_errno = ENOENT;
+ return -1;
+ } else if (entry == &entries[0]) {
+ r->_errno = EISDIR;
+ return -1;
+ }
+
+ file->entry = entry;
+ file->offset = 0;
+ file->inUse = true;
+
+ return (int)file;
+}
+
+static int _SEEPROM_close_r(struct _reent *r, int fd) {
+ FILE_STRUCT *file = (FILE_STRUCT *)fd;
+ if (!file->inUse) {
+ r->_errno = EBADF;
+ return -1;
+ }
+ file->inUse = false;
+ return 0;
+}
+
+static int _SEEPROM_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 (file->offset >= file->entry->size) {
+ r->_errno = EOVERFLOW;
+ return 0;
+ }
+ if (len + file->offset > file->entry->size) {
+ r->_errno = EOVERFLOW;
+ len = file->entry->size - file->offset;
+ }
+ if (len <= 0) {
+ return 0;
+ }
+
+ memcpy(ptr, seeprom + file->entry->addr + file->offset, len);
+ file->offset += len;
+ return len;
+}
+
+static off_t _SEEPROM_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;
+ }
+
+ s64 position;
+
+ switch (dir) {
+ case SEEK_SET:
+ position = pos;
+ break;
+ case SEEK_CUR:
+ position = file->offset + pos;
+ break;
+ case SEEK_END:
+ position = file->entry->size + pos;
+ break;
+ default:
+ r->_errno = EINVAL;
+ return -1;
+ }
+
+ if (pos > 0 && position < 0) {
+ r->_errno = EOVERFLOW;
+ return -1;
+ }
+
+ if (position < 0 || position > file->entry->size) {
+ r->_errno = EINVAL;
+ return -1;
+ }
+
+ file->offset = position;
+
+ return position;
+}
+
+static void stat_entry(DIR_ENTRY *entry, struct stat *st) {
+ st->st_dev = 0x4f54;
+ st->st_ino = 0;
+ st->st_mode = ((entry == &entries[0]) ? 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 = BLOCK_SIZE;
+ st->st_blocks = (entry->size + BLOCK_SIZE - 1) / BLOCK_SIZE;
+ st->st_spare4[0] = 0;
+ st->st_spare4[1] = 0;
+}
+
+static int _SEEPROM_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 _SEEPROM_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 _SEEPROM_chdir_r(struct _reent *r, const char *path) {
+ DIR_ENTRY *entry = entry_from_path(path);
+ if (!entry) {
+ r->_errno = ENOENT;
+ return -1;
+ } else if (entry != &entries[0]) {
+ r->_errno = ENOTDIR;
+ return -1;
+ }
+ return 0;
+}
+
+static DIR_ITER *_SEEPROM_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 (state->entry != &entries[0]) {
+ r->_errno = ENOTDIR;
+ return NULL;
+ }
+ state->index = 1;
+ state->inUse = true;
+ return dirState;
+}
+
+static int _SEEPROM_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 = 1;
+ return 0;
+}
+
+static int _SEEPROM_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;
+ }
+ DIR_ENTRY *entry = NULL;
+ if (state->index >= FILE_COUNT) {
+ r->_errno = ENOENT;
+ return -1;
+ }
+ entry = &entries[state->index++];
+ strcpy(filename, entry->name);
+ stat_entry(entry, st);
+ return 0;
+}
+
+static int _SEEPROM_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_seeprom = {
+ DEVICE_NAME,
+ sizeof(FILE_STRUCT),
+ _SEEPROM_open_r,
+ _SEEPROM_close_r,
+ NULL,
+ _SEEPROM_read_r,
+ _SEEPROM_seek_r,
+ _SEEPROM_fstat_r,
+ _SEEPROM_stat_r,
+ NULL,
+ NULL,
+ _SEEPROM_chdir_r,
+ NULL,
+ NULL,
+ sizeof(DIR_STATE_STRUCT),
+ _SEEPROM_diropen_r,
+ _SEEPROM_dirreset_r,
+ _SEEPROM_dirnext_r,
+ _SEEPROM_dirclose_r,
+ NULL
+};
+
+static bool read_seeprom() {
+ return seeprom_read(seeprom, 0, SEEPROM_SIZE / 2) >= 0 && *(((u32
*)seeprom) + 2) != 0;
+}
+
+bool SEEPROM_Mount() {
+ SEEPROM_Unmount();
+ bool success = read_seeprom() && (dotab_device =
AddDevice(&dotab_seeprom)) >= 0;
+ if (!success) SEEPROM_Unmount();
+ return success;
+}
+
+bool SEEPROM_Unmount() {
+ memset(seeprom, 0, SEEPROM_SIZE);
+ if (dotab_device >= 0) {
+ dotab_device = -1;
+ return !RemoveDevice(DEVICE_NAME ":");
+ }
+ return true;
+}
=======================================
--- /dev/null
+++ /seeprom/trunk/source/seeprom.h Fri Feb 12 16:22:04 2010
@@ -0,0 +1,34 @@
+/*
+
+libseeprom-- a library that provides filesystem access to the Wii's SEEPROM
+
+Copyright (C) 2008 Joseph Jordan <joe....@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.
+
+*/
+#ifndef _SEEPROM_H
+#define _SEEPROM_H
+
+#define SEEPROM_MAXPATHLEN 32
+
+bool SEEPROM_Mount();
+bool SEEPROM_Unmount();
+
+#endif /* _SEEPROM_H */

Reply all
Reply to author
Forward
0 new messages