From: John Hodge (sonata) Date: Fri, 25 Jan 2013 14:46:55 +0000 (+0800) Subject: Tools/DiskTool - Splitting parts out to be shared with NetTest X-Git-Tag: rel0.15~597^2~9^2~8 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c3b821ccc009ec819f2580a53c0ed423aad43bf9;p=tpg%2Facess2.git Tools/DiskTool - Splitting parts out to be shared with NetTest --- diff --git a/Tools/DiskTool/Makefile b/Tools/DiskTool/Makefile new file mode 100644 index 00000000..9d68da35 --- /dev/null +++ b/Tools/DiskTool/Makefile @@ -0,0 +1,103 @@ + +TARGET := $(shell gcc -v 2>&1 | grep Targ | awk '{print $$2}') + +include ../../Makefile.Version.cfg +-include Makefile.BuildNum +ifeq ($(BUILD_NUM),) +BUILD_NUM = 1 +endif + + +KERNEL_SRC = ../../KernelLand/Kernel/ +MODULE_SRC = ../../KernelLand/Modules/ + +BIN = ../disktool +# Kernel Sources (compiled with -ffreestanding) +K_OBJ := lib.o +K_OBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o +K_OBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o +K_OBJ += vfs/fs/root.o vfs/fs/devfs.o +K_OBJ += drvutil_disk.o drv/proc.o +# Modules +MODULES := Storage/LVM Filesystems/FAT Filesystems/Ext2 Filesystems/NTFS +# Local kernel soruces (same as above, but located in same directory as Makefile) +L_OBJ = vfs_handles.o nativefs.o actions.o +# Native Sources (compiled as usual) +N_OBJ = main.o script.o helpers.o + +# Compilation Options +CFLAGS := -Wall -std=gnu99 -g -Werror +CPPFLAGS := -I include/ -I ../nativelib +K_CPPFLAGS := -I $(KERNEL_SRC)include -I $(MODULE_SRC) +LDFLAGS += -Wl,--defsym,__buildnum=$(BUILD_NUM) -g -L ../ -lnativelib + +BUILDINFO_OBJ := obj/$(TARGET)/buildinfo.o +BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c) + +# ==================== +# == Start of Magic == +# ==================== +# -- Load modules --- +$(foreach module,$(MODULES), $(eval include $(MODULE_SRC)$(module)/Makefile) $(eval M_OBJ += $(addprefix $(module)/,$(OBJ))) ) + +# -- Apply Prefixes to object paths +OBJ_PREFIX = obj/$(TARGET)/ +K_OBJ_PREFIX = $(OBJ_PREFIX)_Kernel/ +M_OBJ_PREFIX = $(OBJ_PREFIX)_Module/ +K_OBJ := $(addprefix $(K_OBJ_PREFIX),$(K_OBJ)) +M_OBJ := $(addprefix $(M_OBJ_PREFIX),$(M_OBJ)) +L_OBJ := $(addprefix $(OBJ_PREFIX),$(L_OBJ)) +N_OBJ := $(addprefix $(OBJ_PREFIX),$(N_OBJ)) + +OBJ := $(N_OBJ) $(L_OBJ) $(K_OBJ) $(M_OBJ) $(BUILDINFO_OBJ) + +DEPFILES = $(filter %.o,$(OBJ)) +DEPFILES := $(DEPFILES:%=%.dep) + + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) -f $(OBJ) $(DEPFILES) $(BIN) + +$(BIN): $(OBJ) + @echo [CC Link] -o $(BIN) + @$(CC) -o $(BIN) $(OBJ) $(LDFLAGS) + @echo BUILD_NUM = $$(( $(BUILD_NUM) + 1 )) > Makefile.BuildNum + +$(M_OBJ): $(M_OBJ_PREFIX)%.o: $(MODULE_SRC)%.c + @mkdir -p $(dir $@) + @echo [CC Module] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(K_OBJ): $(K_OBJ_PREFIX)%.o: $(KERNEL_SRC)%.c + @mkdir -p $(dir $@) + @echo [CC Kernel] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(L_OBJ): $(OBJ_PREFIX)%.o: %.c + @mkdir -p $(dir $@) + @echo [CC Local] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(N_OBJ): $(OBJ_PREFIX)%.o: %.c + @mkdir -p $(dir $@) + @echo [CC Native] -o $@ + @$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) -MMD -MP -MF $@.dep + +# Hacky buildinfo.c file +$(BUILDINFO_SRC): $(filter-out $(BUILDINFO_OBJ), $(OBJ)) Makefile + @echo "" > $@ + @echo "const char gsKernelVersion[] = \"$(ACESS_VERSION)\";" >> $@ + @echo "const char gsGitHash[] = \""`git log -n 1 | head -n 1 | awk '{print $$2}'`"\";" >> $@ + @echo "const int giBuildNumber = $(BUILD_NUM);" >> $@ + @echo "const char gsBuildInfo[] = \"Acess2 DiskTool v$(ACESS_VERSION)\";" >> $@ +$(BUILDINFO_OBJ): $(BUILDINFO_SRC) + @echo [CC] -o $@ + @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) + +$(OBJ): Makefile + +-include $(DEPFILES) diff --git a/Tools/DiskTool/actions.c b/Tools/DiskTool/actions.c new file mode 100644 index 00000000..5cda5885 --- /dev/null +++ b/Tools/DiskTool/actions.c @@ -0,0 +1,206 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * actions.c + * - High level actions that call the VFS + * # Kernel-space compiled + */ +#include +#include +#include + +// === IMPORTS === +extern int NativeFS_Install(char **Arguments); +extern int LVM_Cleanup(void); + +// === PROTOTYPES === +void DiskTool_Initialise(void) __attribute__((constructor(101))); +void DiskTool_Cleanup(void); + int DiskTool_int_TranslateOpen(const char *File, int Mode); + int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest); + int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest); +void DiskTool_LVM_Cleanup(void *Handle); + +// === GLOBALS === +tLVM_VolType gDiskTool_VolumeType = { + .Name = "DiskTool", + .Read = DiskTool_LVM_Read, + .Write = DiskTool_LVM_Write, + .Cleanup = DiskTool_LVM_Cleanup +}; + +// === CODE === +void DiskTool_Initialise(void) +{ + VFS_Init(); + NativeFS_Install(NULL); + VFS_MkDir("/Native"); + VFS_Mount("/", "/Native", "nativefs", ""); +} + +void DiskTool_Cleanup(void) +{ + int vfs_rv, lvm_rv; + int nNochangeLoop = 0; + // Unmount all + do { + lvm_rv = LVM_Cleanup(); + vfs_rv = VFS_UnmountAll(); + Log_Debug("DiskTool", "Unmounted %i volumes", vfs_rv); + if( vfs_rv == 0 && lvm_rv == 0 ) { + nNochangeLoop ++; + if(nNochangeLoop == 2) { + Log_Error("DiskTool", "Possible handle leak"); + break; + } + } + else { + nNochangeLoop = 0; + } + } + while( vfs_rv >= 0 || lvm_rv != 0 ); +} + +int DiskTool_RegisterLVM(const char *Identifier, const char *Path) +{ + int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); + if(fd == -1) { + Log_Notice("DiskTool", "Can't open '%s' for LVM %s", Path, Identifier); + return -1; + } + VFS_Seek(fd, 0, SEEK_END); + LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512); + Log_Debug("DiskTool", "Registered '%s' for LVM %s", Path, Identifier); + return 0; +} + +int DiskTool_MountImage(const char *Identifier, const char *Path) +{ + // Validate Identifier and make mountpoint string + char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1]; + strcpy(mountpoint, "/Mount/"); + strcat(mountpoint, Identifier); + + // Translate path + size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path); + char tpath[tpath_len-1]; + DiskTool_int_TranslatePath(tpath, Path); + + // Call mount + VFS_MkDir(mountpoint); + // TODO: Detect filesystem? + return VFS_Mount(tpath, mountpoint, "", ""); +} + +int DiskTool_MkDir(const char *Directory) +{ + return -1; +} + +int DiskTool_Copy(const char *Source, const char *Destination) +{ + int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ); + if( src == -1 ) { + Log_Error("DiskTool", "Unable to open %s for reading", Source); + return -1; + } + int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE); + if( dst == -1 ) { + Log_Error("DiskTool", "Unable to open %s for writing", Destination); + VFS_Close(src); + return -1; + } + + char buf[1024]; + size_t len, total = 0; + while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) { + VFS_Write(dst, len, buf); + total += len; + } + VFS_Write(dst, len, buf), total += len; + + Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination); + + VFS_Close(dst); + VFS_Close(src); + + return 0; +} + +int DiskTool_ListDirectory(const char *Directory) +{ + int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY); + if(fd == -1) { +// fprintf(stderr, "Can't open '%s'\n", Directory); + return -1; + } + + Log("Directory listing of '%s'", Directory); + + char name[256]; + while( VFS_ReadDir(fd, name) ) + { + tFInfo fi; + int child = VFS_OpenChild(fd, name, 0); + if( child != -1 ) + { + VFS_FInfo(child, &fi, 0); + VFS_Close(child); + } + Log("- %02x %6lli %s", fi.flags, fi.size, name); + } + + VFS_Close(fd); + + return 0; +} + +int DiskTool_Cat(const char *File) +{ + int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ); + if( src == -1 ) { + Log_Error("DiskTool", "Unable to open %s for reading", File); + return -1; + } + + char buf[1024]; + size_t len, total = 0; + while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) { + _fwrite_stdout(len, buf); + total += len; + } + _fwrite_stdout(len, buf); + total += len; + + Log_Notice("DiskTool", "%i bytes from %s", total, File); + + VFS_Close(src); + return 0; +} + +int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest) +{ + return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512; +} +int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest) +{ + return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512; +} +void DiskTool_LVM_Cleanup(void *Handle) +{ + VFS_Close( (int)(tVAddr)Handle ); +} + +// --- Internal helpers --- +int DiskTool_int_TranslateOpen(const char *File, int Flags) +{ + size_t tpath_len = DiskTool_int_TranslatePath(NULL, File); + if(tpath_len == 0) + return -1; + char tpath[tpath_len-1]; + DiskTool_int_TranslatePath(tpath, File); + + return VFS_Open(tpath, Flags); +} + diff --git a/Tools/DiskTool/helpers.c b/Tools/DiskTool/helpers.c new file mode 100644 index 00000000..f9e3bc4a --- /dev/null +++ b/Tools/DiskTool/helpers.c @@ -0,0 +1,74 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * helpers.c + */ +#include +#include +#include +#include +#include + +// === GLOBALS === +char gsWorkingDirectory[1024]; + + +// === CODE === +size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path) +{ + int len; + const char *colon = strchr(Path, ':'); + + if( Path[0] == '#' ) + { + len = strlen(Path+1); + if(Buffer) { + strcpy(Buffer, Path+1); + } + } + else if( Path[0] == ':' ) + { + len = strlen("/Devices/LVM/"); + len += strlen(Path+1); + if(Buffer) { + strcpy(Buffer, "/Devices/LVM/"); + strcat(Buffer, Path+1); + } + } + else if( colon ) + { + const char *pos; + for(pos = Path; pos < colon; pos ++) + { + if( !isalpha(*pos) ) + goto native_path; + } + + len = strlen("/Mount/"); + len += strlen(Path); + if( Buffer ) { + strcpy(Buffer, "/Mount/"); + strncat(Buffer+strlen("/Mount/"), Path, colon - Path); + strcat(Buffer, colon + 1); + } + } + else + { + native_path: + if( !gsWorkingDirectory[0] ) { + getcwd(gsWorkingDirectory, 1024); + } + + len = strlen("/Native"); + len += strlen( gsWorkingDirectory ) + 1; + len += strlen(Path); + if( Buffer ) { + strcpy(Buffer, "/Native"); + strcat(Buffer, gsWorkingDirectory); + strcat(Buffer, "/"); + strcat(Buffer, Path); + } + } + return len; +} diff --git a/Tools/DiskTool/include/acess.h b/Tools/DiskTool/include/acess.h new file mode 100644 index 00000000..7eff8c91 --- /dev/null +++ b/Tools/DiskTool/include/acess.h @@ -0,0 +1,138 @@ +/* + * Acess2 DiskTool utility + * - By John Hodge (thePowersGang) + * + * include/acess.h + * - Mock kernel core header + */ +#ifndef _DISKTOOL__ACESS_H_ +#define _DISKTOOL__ACESS_H_ + +#define CONCAT(x,y) x ## y +#define EXPAND_CONCAT(x,y) CONCAT(x,y) +#define STR(x) #x +#define EXPAND_STR(x) STR(x) + +#define ASSERT(x) do{}while(0) + +extern char __buildnum[]; +#define BUILD_NUM ((int)(Uint)&__buildnum) +extern const char gsGitHash[]; +extern const char gsBuildInfo[]; + +#define BITS 32 +#define NULL ((void*)0) +#include + +typedef uintptr_t Uint; +//typedef unsigned int size_t; +#include +typedef uint64_t off_t; +typedef char BOOL; + + +typedef uint8_t Uint8; +typedef uint16_t Uint16; +typedef uint32_t Uint32; +typedef uint64_t Uint64; + +typedef int8_t Sint8; +typedef int16_t Sint16; +typedef int32_t Sint32; +typedef int64_t Sint64; + +typedef uintptr_t tVAddr; +typedef uint32_t tPAddr; + +typedef uint32_t tUID; +typedef uint32_t tGID; +typedef uint32_t tTID; + +// NOTE: Since this is single-threaded (for now) mutexes can be implimented as simple locks +typedef char tShortSpinlock; + +typedef int64_t tTime; +extern tTime now(void); +extern int64_t timestamp(int sec, int min, int hr, int day, int month, int year); +extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms); + +#define PACKED __attribute__((packed)) +#define DEPRECATED +#define EXPORT(s) +#define EXPORTV(s) + +#include + +// These are actually library functions, but they can't be included, so they're defined manually +extern void *malloc(size_t bytes); +extern void *calloc(size_t nmemb, size_t size); +extern void *realloc(void *oldptr, size_t bytes); +extern void free(void *buffer); + +#include +#include + +// Threads +extern int *Threads_GetErrno(void); +//extern tPGID Threads_GetPGID(void); +//extern tPID Threads_GetPID(void); +extern tTID Threads_GetTID(void); +extern tUID Threads_GetUID(void); +extern tGID Threads_GetGID(void); + +// Kinda hacky way of not colliding with native errno +#define errno (*(Threads_GetErrno())) + +/** + * \name Endianness Swapping + * \{ + */ +#ifdef __BIG_ENDIAN__ +#define LittleEndian16(_val) SwapEndian16(_val) +#define LittleEndian32(_val) SwapEndian32(_val) +#define LittleEndian64(_val) SwapEndian32(_val) +#define BigEndian16(_val) (_val) +#define BigEndian32(_val) (_val) +#define BigEndian64(_val) (_val) +#else +#define LittleEndian16(_val) (_val) +#define LittleEndian32(_val) (_val) +#define LittleEndian64(_val) (_val) +#define BigEndian16(_val) SwapEndian16(_val) +#define BigEndian32(_val) SwapEndian32(_val) +#define BigEndian64(_val) SwapEndian64(_val) +#endif +extern Uint16 SwapEndian16(Uint16 Val); +extern Uint32 SwapEndian32(Uint32 Val); +extern Uint64 SwapEndian64(Uint64 Val); +/** + * \} + */ + + +#include +extern int strucmp(const char *s1, const char *s2); +extern int strpos(const char *Str, char Ch); +extern void itoa(char *buf, uint64_t num, int base, int minLength, char pad); +extern int snprintf(char *buf, size_t len, const char *fmt, ...); +extern int sprintf(char *buf, const char *fmt, ...); +extern int ReadUTF8(const Uint8 *str, Uint32 *Val); +extern int WriteUTF8(Uint8 *str, Uint32 Val); +#define CheckString(str) (1) +#define CheckMem(mem,sz) (1) +#include + +// TODO: Move out? +extern int DivUp(int value, int divisor); +extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); + +static inline void SHORTLOCK(tShortSpinlock *Lock) { + if(*Lock) Log_KernelPanic("---", "Double short lock"); + *Lock = 1; +} +static inline void SHORTREL(tShortSpinlock *m) { *m = 0; } + +static inline intptr_t MM_GetPhysAddr(void *Ptr) { return 1; } + +#endif + diff --git a/Tools/DiskTool/include/acess_logging.h b/Tools/DiskTool/include/acess_logging.h new file mode 100644 index 00000000..f8577dd5 --- /dev/null +++ b/Tools/DiskTool/include/acess_logging.h @@ -0,0 +1,34 @@ + +#ifndef _DISKTOOL__ACESS_LOGGING_H_ +#define _DISKTOOL__ACESS_LOGGING_H_ + +#if DEBUG +# define ENTER(str, v...) Debug_TraceEnter(__func__, str, ##v) +# define LOG(fmt, v...) Debug_TraceLog(__func__, fmt, ##v) +# define LEAVE(t, v...) Debug_TraceLeave(__func__, t, ##v) +# define LEAVE_RET(t,v) do{LEAVE('-');return v;}while(0) +#else +# define ENTER(...) do{}while(0) +# define LOG(...) do{}while(0) +# define LEAVE(...) do{}while(0) +# define LEAVE_RET(t,v) return v; +#endif + +extern void Log_KernelPanic(const char *Ident, const char *Message, ...) __attribute__((noreturn)); +extern void Log_Panic(const char *Ident, const char *Message, ...); +extern void Log_Error(const char *Ident, const char *Message, ...); +extern void Log_Warning(const char *Ident, const char *Message, ...); +extern void Log_Notice(const char *Ident, const char *Message, ...); +extern void Log_Log(const char *Ident, const char *Message, ...); +extern void Log_Debug(const char *Ident, const char *Message, ...); + +extern void Warning(const char *Message, ...); +extern void Log(const char *Message, ...); +extern void Debug_HexDump(const char *Prefix, const void *Data, size_t Length); + +extern void Debug_TraceEnter(const char *Function, const char *Format, ...); +extern void Debug_TraceLog(const char *Function, const char *Format, ...); +extern void Debug_TraceLeave(const char *Function, char Type, ...); + +#endif + diff --git a/Tools/DiskTool/include/arch.h b/Tools/DiskTool/include/arch.h new file mode 100644 index 00000000..e69de29b diff --git a/Tools/DiskTool/include/disktool_common.h b/Tools/DiskTool/include/disktool_common.h new file mode 100644 index 00000000..38703c21 --- /dev/null +++ b/Tools/DiskTool/include/disktool_common.h @@ -0,0 +1,24 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * include/disktool_common.h + * - DiskTool internal API between native and kernel code + */ +#ifndef _INCLUDE__DISKTOOL_COMMON_H_ +#define _INCLUDE__DISKTOOL_COMMON_H_ + +extern void DiskTool_Cleanup(void); + +extern int DiskTool_RegisterLVM(const char *Identifier, const char *Path); +extern int DiskTool_MountImage(const char *Identifier, const char *Path); +extern int DiskTool_Copy(const char *Source, const char *Destination); +extern int DiskTool_ListDirectory(const char *Directory); +extern int DiskTool_Cat(const char *File); + +extern size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path); + +extern size_t _fwrite_stdout(size_t bytes, const void *data); + +#endif + diff --git a/Tools/DiskTool/include/modules.h b/Tools/DiskTool/include/modules.h new file mode 100644 index 00000000..c940c10b --- /dev/null +++ b/Tools/DiskTool/include/modules.h @@ -0,0 +1,20 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * include/modules.h + * - Reimplimentation of kernel module interface for POSIX userland + */ +#ifndef _INCLUDE__MODULES_H_ +#define _INCLUDE__MODULES_H_ + +enum +{ + MODULE_ERR_OK, +}; + +#define MODULE_DEFINE(flags, version, name, init, deinit, deps...) \ +void __init_##init(void) __attribute__((constructor(200))); void __init_##init(void){init(NULL);} + +#endif + diff --git a/Tools/DiskTool/include/mutex.h b/Tools/DiskTool/include/mutex.h new file mode 100644 index 00000000..9fe1c4f8 --- /dev/null +++ b/Tools/DiskTool/include/mutex.h @@ -0,0 +1,19 @@ + +#ifndef _MUTEX_H_ +#define _MUTEX_H_ + +typedef struct { + void *LockerReturnAddr; +} tMutex; + +static inline int Mutex_Acquire(tMutex *m) { + if(m->LockerReturnAddr) + Log_KernelPanic("---", "Double mutex lock of %p by %p (was locked by %p)", + m, __builtin_return_address(0), m->LockerReturnAddr); + m->LockerReturnAddr = __builtin_return_address(0);; + return 0; +} +static inline void Mutex_Release(tMutex *m) { m->LockerReturnAddr = 0; } + +#endif + diff --git a/Tools/DiskTool/include/rwlock.h b/Tools/DiskTool/include/rwlock.h new file mode 100644 index 00000000..b9ff514a --- /dev/null +++ b/Tools/DiskTool/include/rwlock.h @@ -0,0 +1,22 @@ +/* + * Acess2 Disk Tool + */ +#ifndef _RWLOCK_H +#define _RWLOCK_H + +typedef char tRWLock; + +static inline int RWLock_AcquireRead(tRWLock *m) { + if(*m) Log_KernelPanic("---", "Double mutex lock"); + *m = 1; + return 0; +} +static inline int RWLock_AcquireWrite(tRWLock *m) { + if(*m) Log_KernelPanic("---", "Double mutex lock"); + *m = 1; + return 0; +} +static inline void RWLock_Release(tRWLock *m) { *m = 0; } + +#endif + diff --git a/Tools/DiskTool/main.c b/Tools/DiskTool/main.c new file mode 100644 index 00000000..81a087c1 --- /dev/null +++ b/Tools/DiskTool/main.c @@ -0,0 +1,102 @@ +/* + */ +#include +#include +#include +#include +#include +#include + +// === CODE === +int main(int argc, char *argv[]) +{ + // Parse arguments + for( int i = 1; i < argc; i ++ ) + { + if( strcmp("mount", argv[i]) == 0 || strcmp("-i", argv[i]) == 0 ) { + // Mount an image + if( argc - i < 3 ) { + fprintf(stderr, "mount takes 2 arguments (image and mountpoint)\n"); + exit(-1); + } + + if( DiskTool_MountImage(argv[i+2], argv[i+1]) ) { + fprintf(stderr, "Unable to mount '%s' as '%s'\n", argv[i+1], argv[i+2]); + break; + } + + i += 2; + continue ; + } + + if( strcmp("mountlvm", argv[i]) == 0 || strcmp("lvm", argv[i]) == 0 ) { + + if( argc - i < 3 ) { + fprintf(stderr, "lvm takes 2 arguments (iamge and ident)\n"); + exit(-1); + } + + if( DiskTool_RegisterLVM(argv[i+2], argv[i+1]) ) { + fprintf(stderr, "Unable to register '%s' as LVM '%s'\n", argv[i+1], argv[i+2]); + break; + } + + i += 2; + continue ; + } + + if( strcmp("ls", argv[i]) == 0 ) { + if( argc - i < 2 ) { + fprintf(stderr, "ls takes 1 argument (path)\n"); + break; + } + + DiskTool_ListDirectory(argv[i+1]); + i += 1; + continue ; + } + + if( strcmp("cp", argv[i]) == 0 ) { + + if( argc - i < 3 ) { + fprintf(stderr, "cp takes 2 arguments (source and destination)\n"); + break; + } + + DiskTool_Copy(argv[i+1], argv[i+2]); + + i += 2; + continue ; + } + + if( strcmp("cat", argv[i]) == 0 ) { + + if( argc - 1 < 2 ) { + fprintf(stderr, "cat takes 1 argument (path)\n"); + break; + } + + DiskTool_Cat(argv[i+1]); + + i += 1; + continue; + } + + fprintf(stderr, "Unknown command '%s'\n", argv[i]); + } + + DiskTool_Cleanup(); + + return 0; +} + +// NOTE: This is in a native compiled file because it needs access to the real errno macro +int *Threads_GetErrno(void) +{ + return &errno; +} + +size_t _fwrite_stdout(size_t bytes, const void *data) +{ + return fwrite(data, bytes, 1, stdout); +} diff --git a/Tools/DiskTool/nativefs.c b/Tools/DiskTool/nativefs.c new file mode 120000 index 00000000..cdb86d61 --- /dev/null +++ b/Tools/DiskTool/nativefs.c @@ -0,0 +1 @@ +../../AcessNative/acesskernel_src/nativefs.c \ No newline at end of file diff --git a/Tools/DiskTool/script.c b/Tools/DiskTool/script.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/DiskTool/src/Makefile b/Tools/DiskTool/src/Makefile deleted file mode 100644 index e91bc7f8..00000000 --- a/Tools/DiskTool/src/Makefile +++ /dev/null @@ -1,103 +0,0 @@ - -TARGET := $(shell gcc -v 2>&1 | grep Targ | awk '{print $$2}') - -include ../../../Makefile.Version.cfg --include Makefile.BuildNum -ifeq ($(BUILD_NUM),) -BUILD_NUM = 1 -endif - - -KERNEL_SRC = ../../../KernelLand/Kernel/ -MODULE_SRC = ../../../KernelLand/Modules/ - -BIN = ../DiskTool -# Kernel Sources (compiled with -ffreestanding) -K_OBJ := lib.o -K_OBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o -K_OBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o -K_OBJ += vfs/fs/root.o vfs/fs/devfs.o -K_OBJ += drvutil_disk.o drv/proc.o -# Modules -MODULES := Storage/LVM Filesystems/FAT Filesystems/Ext2 Filesystems/NTFS -# Local kernel soruces (same as above, but located in same directory as Makefile) -L_OBJ = vfs_handles.o threads.o nativefs.o time.o actions.o -# Native Sources (compiled as usual) -N_OBJ = main.o script.o logging.o helpers.o - -# Compilation Options -CFLAGS := -Wall -std=gnu99 -g -Werror -CPPFLAGS := -I include/ -K_CPPFLAGS := -I $(KERNEL_SRC)include -I $(MODULE_SRC) -LDFLAGS += -Wl,--defsym,__buildnum=$(BUILD_NUM) -g - -BUILDINFO_OBJ := obj/$(TARGET)/buildinfo.o -BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c) - -# ==================== -# == Start of Magic == -# ==================== -# -- Load modules --- -$(foreach module,$(MODULES), $(eval include $(MODULE_SRC)$(module)/Makefile) $(eval M_OBJ += $(addprefix $(module)/,$(OBJ))) ) - -# -- Apply Prefixes to object paths -OBJ_PREFIX = obj/$(TARGET)/ -K_OBJ_PREFIX = $(OBJ_PREFIX)_Kernel/ -M_OBJ_PREFIX = $(OBJ_PREFIX)_Module/ -K_OBJ := $(addprefix $(K_OBJ_PREFIX),$(K_OBJ)) -M_OBJ := $(addprefix $(M_OBJ_PREFIX),$(M_OBJ)) -L_OBJ := $(addprefix $(OBJ_PREFIX),$(L_OBJ)) -N_OBJ := $(addprefix $(OBJ_PREFIX),$(N_OBJ)) - -OBJ := $(N_OBJ) $(L_OBJ) $(K_OBJ) $(M_OBJ) $(BUILDINFO_OBJ) - -DEPFILES = $(filter %.o,$(OBJ)) -DEPFILES := $(DEPFILES:%=%.dep) - - -.PHONY: all clean - -all: $(BIN) - -clean: - $(RM) -f $(OBJ) $(DEPFILES) $(BIN) - -$(BIN): $(OBJ) - @echo [CC Link] -o $(BIN) - @$(CC) -o $(BIN) $(OBJ) $(LDFLAGS) - @echo BUILD_NUM = $$(( $(BUILD_NUM) + 1 )) > Makefile.BuildNum - -$(M_OBJ): $(M_OBJ_PREFIX)%.o: $(MODULE_SRC)%.c - @mkdir -p $(dir $@) - @echo [CC Module] -o $@ - @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep - -$(K_OBJ): $(K_OBJ_PREFIX)%.o: $(KERNEL_SRC)%.c - @mkdir -p $(dir $@) - @echo [CC Kernel] -o $@ - @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep - -$(L_OBJ): $(OBJ_PREFIX)%.o: %.c - @mkdir -p $(dir $@) - @echo [CC Local] -o $@ - @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep - -$(N_OBJ): $(OBJ_PREFIX)%.o: %.c - @mkdir -p $(dir $@) - @echo [CC Native] -o $@ - @$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) -MMD -MP -MF $@.dep - -# Hacky buildinfo.c file -$(BUILDINFO_SRC): $(filter-out $(BUILDINFO_OBJ), $(OBJ)) Makefile - @echo "" > $@ - @echo "const char gsKernelVersion[] = \"$(ACESS_VERSION)\";" >> $@ - @echo "const char gsGitHash[] = \""`git log -n 1 | head -n 1 | awk '{print $$2}'`"\";" >> $@ - @echo "const int giBuildNumber = $(BUILD_NUM);" >> $@ - @echo "const char gsBuildInfo[] = \"Acess2 DiskTool v$(ACESS_VERSION)\";" >> $@ -$(BUILDINFO_OBJ): $(BUILDINFO_SRC) - @echo [CC] -o $@ - @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) - -$(OBJ): Makefile - --include $(DEPFILES) diff --git a/Tools/DiskTool/src/actions.c b/Tools/DiskTool/src/actions.c deleted file mode 100644 index 5cda5885..00000000 --- a/Tools/DiskTool/src/actions.c +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Acess2 DiskTool - * - By John Hodge (thePowersGang) - * - * actions.c - * - High level actions that call the VFS - * # Kernel-space compiled - */ -#include -#include -#include - -// === IMPORTS === -extern int NativeFS_Install(char **Arguments); -extern int LVM_Cleanup(void); - -// === PROTOTYPES === -void DiskTool_Initialise(void) __attribute__((constructor(101))); -void DiskTool_Cleanup(void); - int DiskTool_int_TranslateOpen(const char *File, int Mode); - int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest); - int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest); -void DiskTool_LVM_Cleanup(void *Handle); - -// === GLOBALS === -tLVM_VolType gDiskTool_VolumeType = { - .Name = "DiskTool", - .Read = DiskTool_LVM_Read, - .Write = DiskTool_LVM_Write, - .Cleanup = DiskTool_LVM_Cleanup -}; - -// === CODE === -void DiskTool_Initialise(void) -{ - VFS_Init(); - NativeFS_Install(NULL); - VFS_MkDir("/Native"); - VFS_Mount("/", "/Native", "nativefs", ""); -} - -void DiskTool_Cleanup(void) -{ - int vfs_rv, lvm_rv; - int nNochangeLoop = 0; - // Unmount all - do { - lvm_rv = LVM_Cleanup(); - vfs_rv = VFS_UnmountAll(); - Log_Debug("DiskTool", "Unmounted %i volumes", vfs_rv); - if( vfs_rv == 0 && lvm_rv == 0 ) { - nNochangeLoop ++; - if(nNochangeLoop == 2) { - Log_Error("DiskTool", "Possible handle leak"); - break; - } - } - else { - nNochangeLoop = 0; - } - } - while( vfs_rv >= 0 || lvm_rv != 0 ); -} - -int DiskTool_RegisterLVM(const char *Identifier, const char *Path) -{ - int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); - if(fd == -1) { - Log_Notice("DiskTool", "Can't open '%s' for LVM %s", Path, Identifier); - return -1; - } - VFS_Seek(fd, 0, SEEK_END); - LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512); - Log_Debug("DiskTool", "Registered '%s' for LVM %s", Path, Identifier); - return 0; -} - -int DiskTool_MountImage(const char *Identifier, const char *Path) -{ - // Validate Identifier and make mountpoint string - char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1]; - strcpy(mountpoint, "/Mount/"); - strcat(mountpoint, Identifier); - - // Translate path - size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path); - char tpath[tpath_len-1]; - DiskTool_int_TranslatePath(tpath, Path); - - // Call mount - VFS_MkDir(mountpoint); - // TODO: Detect filesystem? - return VFS_Mount(tpath, mountpoint, "", ""); -} - -int DiskTool_MkDir(const char *Directory) -{ - return -1; -} - -int DiskTool_Copy(const char *Source, const char *Destination) -{ - int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ); - if( src == -1 ) { - Log_Error("DiskTool", "Unable to open %s for reading", Source); - return -1; - } - int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE); - if( dst == -1 ) { - Log_Error("DiskTool", "Unable to open %s for writing", Destination); - VFS_Close(src); - return -1; - } - - char buf[1024]; - size_t len, total = 0; - while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) { - VFS_Write(dst, len, buf); - total += len; - } - VFS_Write(dst, len, buf), total += len; - - Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination); - - VFS_Close(dst); - VFS_Close(src); - - return 0; -} - -int DiskTool_ListDirectory(const char *Directory) -{ - int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY); - if(fd == -1) { -// fprintf(stderr, "Can't open '%s'\n", Directory); - return -1; - } - - Log("Directory listing of '%s'", Directory); - - char name[256]; - while( VFS_ReadDir(fd, name) ) - { - tFInfo fi; - int child = VFS_OpenChild(fd, name, 0); - if( child != -1 ) - { - VFS_FInfo(child, &fi, 0); - VFS_Close(child); - } - Log("- %02x %6lli %s", fi.flags, fi.size, name); - } - - VFS_Close(fd); - - return 0; -} - -int DiskTool_Cat(const char *File) -{ - int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ); - if( src == -1 ) { - Log_Error("DiskTool", "Unable to open %s for reading", File); - return -1; - } - - char buf[1024]; - size_t len, total = 0; - while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) { - _fwrite_stdout(len, buf); - total += len; - } - _fwrite_stdout(len, buf); - total += len; - - Log_Notice("DiskTool", "%i bytes from %s", total, File); - - VFS_Close(src); - return 0; -} - -int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest) -{ - return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512; -} -int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest) -{ - return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512; -} -void DiskTool_LVM_Cleanup(void *Handle) -{ - VFS_Close( (int)(tVAddr)Handle ); -} - -// --- Internal helpers --- -int DiskTool_int_TranslateOpen(const char *File, int Flags) -{ - size_t tpath_len = DiskTool_int_TranslatePath(NULL, File); - if(tpath_len == 0) - return -1; - char tpath[tpath_len-1]; - DiskTool_int_TranslatePath(tpath, File); - - return VFS_Open(tpath, Flags); -} - diff --git a/Tools/DiskTool/src/helpers.c b/Tools/DiskTool/src/helpers.c deleted file mode 100644 index f9e3bc4a..00000000 --- a/Tools/DiskTool/src/helpers.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Acess2 DiskTool - * - By John Hodge (thePowersGang) - * - * helpers.c - */ -#include -#include -#include -#include -#include - -// === GLOBALS === -char gsWorkingDirectory[1024]; - - -// === CODE === -size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path) -{ - int len; - const char *colon = strchr(Path, ':'); - - if( Path[0] == '#' ) - { - len = strlen(Path+1); - if(Buffer) { - strcpy(Buffer, Path+1); - } - } - else if( Path[0] == ':' ) - { - len = strlen("/Devices/LVM/"); - len += strlen(Path+1); - if(Buffer) { - strcpy(Buffer, "/Devices/LVM/"); - strcat(Buffer, Path+1); - } - } - else if( colon ) - { - const char *pos; - for(pos = Path; pos < colon; pos ++) - { - if( !isalpha(*pos) ) - goto native_path; - } - - len = strlen("/Mount/"); - len += strlen(Path); - if( Buffer ) { - strcpy(Buffer, "/Mount/"); - strncat(Buffer+strlen("/Mount/"), Path, colon - Path); - strcat(Buffer, colon + 1); - } - } - else - { - native_path: - if( !gsWorkingDirectory[0] ) { - getcwd(gsWorkingDirectory, 1024); - } - - len = strlen("/Native"); - len += strlen( gsWorkingDirectory ) + 1; - len += strlen(Path); - if( Buffer ) { - strcpy(Buffer, "/Native"); - strcat(Buffer, gsWorkingDirectory); - strcat(Buffer, "/"); - strcat(Buffer, Path); - } - } - return len; -} diff --git a/Tools/DiskTool/src/include/acess.h b/Tools/DiskTool/src/include/acess.h deleted file mode 100644 index 7eff8c91..00000000 --- a/Tools/DiskTool/src/include/acess.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Acess2 DiskTool utility - * - By John Hodge (thePowersGang) - * - * include/acess.h - * - Mock kernel core header - */ -#ifndef _DISKTOOL__ACESS_H_ -#define _DISKTOOL__ACESS_H_ - -#define CONCAT(x,y) x ## y -#define EXPAND_CONCAT(x,y) CONCAT(x,y) -#define STR(x) #x -#define EXPAND_STR(x) STR(x) - -#define ASSERT(x) do{}while(0) - -extern char __buildnum[]; -#define BUILD_NUM ((int)(Uint)&__buildnum) -extern const char gsGitHash[]; -extern const char gsBuildInfo[]; - -#define BITS 32 -#define NULL ((void*)0) -#include - -typedef uintptr_t Uint; -//typedef unsigned int size_t; -#include -typedef uint64_t off_t; -typedef char BOOL; - - -typedef uint8_t Uint8; -typedef uint16_t Uint16; -typedef uint32_t Uint32; -typedef uint64_t Uint64; - -typedef int8_t Sint8; -typedef int16_t Sint16; -typedef int32_t Sint32; -typedef int64_t Sint64; - -typedef uintptr_t tVAddr; -typedef uint32_t tPAddr; - -typedef uint32_t tUID; -typedef uint32_t tGID; -typedef uint32_t tTID; - -// NOTE: Since this is single-threaded (for now) mutexes can be implimented as simple locks -typedef char tShortSpinlock; - -typedef int64_t tTime; -extern tTime now(void); -extern int64_t timestamp(int sec, int min, int hr, int day, int month, int year); -extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms); - -#define PACKED __attribute__((packed)) -#define DEPRECATED -#define EXPORT(s) -#define EXPORTV(s) - -#include - -// These are actually library functions, but they can't be included, so they're defined manually -extern void *malloc(size_t bytes); -extern void *calloc(size_t nmemb, size_t size); -extern void *realloc(void *oldptr, size_t bytes); -extern void free(void *buffer); - -#include -#include - -// Threads -extern int *Threads_GetErrno(void); -//extern tPGID Threads_GetPGID(void); -//extern tPID Threads_GetPID(void); -extern tTID Threads_GetTID(void); -extern tUID Threads_GetUID(void); -extern tGID Threads_GetGID(void); - -// Kinda hacky way of not colliding with native errno -#define errno (*(Threads_GetErrno())) - -/** - * \name Endianness Swapping - * \{ - */ -#ifdef __BIG_ENDIAN__ -#define LittleEndian16(_val) SwapEndian16(_val) -#define LittleEndian32(_val) SwapEndian32(_val) -#define LittleEndian64(_val) SwapEndian32(_val) -#define BigEndian16(_val) (_val) -#define BigEndian32(_val) (_val) -#define BigEndian64(_val) (_val) -#else -#define LittleEndian16(_val) (_val) -#define LittleEndian32(_val) (_val) -#define LittleEndian64(_val) (_val) -#define BigEndian16(_val) SwapEndian16(_val) -#define BigEndian32(_val) SwapEndian32(_val) -#define BigEndian64(_val) SwapEndian64(_val) -#endif -extern Uint16 SwapEndian16(Uint16 Val); -extern Uint32 SwapEndian32(Uint32 Val); -extern Uint64 SwapEndian64(Uint64 Val); -/** - * \} - */ - - -#include -extern int strucmp(const char *s1, const char *s2); -extern int strpos(const char *Str, char Ch); -extern void itoa(char *buf, uint64_t num, int base, int minLength, char pad); -extern int snprintf(char *buf, size_t len, const char *fmt, ...); -extern int sprintf(char *buf, const char *fmt, ...); -extern int ReadUTF8(const Uint8 *str, Uint32 *Val); -extern int WriteUTF8(Uint8 *str, Uint32 Val); -#define CheckString(str) (1) -#define CheckMem(mem,sz) (1) -#include - -// TODO: Move out? -extern int DivUp(int value, int divisor); -extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); - -static inline void SHORTLOCK(tShortSpinlock *Lock) { - if(*Lock) Log_KernelPanic("---", "Double short lock"); - *Lock = 1; -} -static inline void SHORTREL(tShortSpinlock *m) { *m = 0; } - -static inline intptr_t MM_GetPhysAddr(void *Ptr) { return 1; } - -#endif - diff --git a/Tools/DiskTool/src/include/acess_logging.h b/Tools/DiskTool/src/include/acess_logging.h deleted file mode 100644 index f8577dd5..00000000 --- a/Tools/DiskTool/src/include/acess_logging.h +++ /dev/null @@ -1,34 +0,0 @@ - -#ifndef _DISKTOOL__ACESS_LOGGING_H_ -#define _DISKTOOL__ACESS_LOGGING_H_ - -#if DEBUG -# define ENTER(str, v...) Debug_TraceEnter(__func__, str, ##v) -# define LOG(fmt, v...) Debug_TraceLog(__func__, fmt, ##v) -# define LEAVE(t, v...) Debug_TraceLeave(__func__, t, ##v) -# define LEAVE_RET(t,v) do{LEAVE('-');return v;}while(0) -#else -# define ENTER(...) do{}while(0) -# define LOG(...) do{}while(0) -# define LEAVE(...) do{}while(0) -# define LEAVE_RET(t,v) return v; -#endif - -extern void Log_KernelPanic(const char *Ident, const char *Message, ...) __attribute__((noreturn)); -extern void Log_Panic(const char *Ident, const char *Message, ...); -extern void Log_Error(const char *Ident, const char *Message, ...); -extern void Log_Warning(const char *Ident, const char *Message, ...); -extern void Log_Notice(const char *Ident, const char *Message, ...); -extern void Log_Log(const char *Ident, const char *Message, ...); -extern void Log_Debug(const char *Ident, const char *Message, ...); - -extern void Warning(const char *Message, ...); -extern void Log(const char *Message, ...); -extern void Debug_HexDump(const char *Prefix, const void *Data, size_t Length); - -extern void Debug_TraceEnter(const char *Function, const char *Format, ...); -extern void Debug_TraceLog(const char *Function, const char *Format, ...); -extern void Debug_TraceLeave(const char *Function, char Type, ...); - -#endif - diff --git a/Tools/DiskTool/src/include/arch.h b/Tools/DiskTool/src/include/arch.h deleted file mode 100644 index e69de29b..00000000 diff --git a/Tools/DiskTool/src/include/disktool_common.h b/Tools/DiskTool/src/include/disktool_common.h deleted file mode 100644 index 38703c21..00000000 --- a/Tools/DiskTool/src/include/disktool_common.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Acess2 DiskTool - * - By John Hodge (thePowersGang) - * - * include/disktool_common.h - * - DiskTool internal API between native and kernel code - */ -#ifndef _INCLUDE__DISKTOOL_COMMON_H_ -#define _INCLUDE__DISKTOOL_COMMON_H_ - -extern void DiskTool_Cleanup(void); - -extern int DiskTool_RegisterLVM(const char *Identifier, const char *Path); -extern int DiskTool_MountImage(const char *Identifier, const char *Path); -extern int DiskTool_Copy(const char *Source, const char *Destination); -extern int DiskTool_ListDirectory(const char *Directory); -extern int DiskTool_Cat(const char *File); - -extern size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path); - -extern size_t _fwrite_stdout(size_t bytes, const void *data); - -#endif - diff --git a/Tools/DiskTool/src/include/modules.h b/Tools/DiskTool/src/include/modules.h deleted file mode 100644 index c940c10b..00000000 --- a/Tools/DiskTool/src/include/modules.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Acess2 DiskTool - * - By John Hodge (thePowersGang) - * - * include/modules.h - * - Reimplimentation of kernel module interface for POSIX userland - */ -#ifndef _INCLUDE__MODULES_H_ -#define _INCLUDE__MODULES_H_ - -enum -{ - MODULE_ERR_OK, -}; - -#define MODULE_DEFINE(flags, version, name, init, deinit, deps...) \ -void __init_##init(void) __attribute__((constructor(200))); void __init_##init(void){init(NULL);} - -#endif - diff --git a/Tools/DiskTool/src/include/mutex.h b/Tools/DiskTool/src/include/mutex.h deleted file mode 100644 index 9fe1c4f8..00000000 --- a/Tools/DiskTool/src/include/mutex.h +++ /dev/null @@ -1,19 +0,0 @@ - -#ifndef _MUTEX_H_ -#define _MUTEX_H_ - -typedef struct { - void *LockerReturnAddr; -} tMutex; - -static inline int Mutex_Acquire(tMutex *m) { - if(m->LockerReturnAddr) - Log_KernelPanic("---", "Double mutex lock of %p by %p (was locked by %p)", - m, __builtin_return_address(0), m->LockerReturnAddr); - m->LockerReturnAddr = __builtin_return_address(0);; - return 0; -} -static inline void Mutex_Release(tMutex *m) { m->LockerReturnAddr = 0; } - -#endif - diff --git a/Tools/DiskTool/src/include/rwlock.h b/Tools/DiskTool/src/include/rwlock.h deleted file mode 100644 index b9ff514a..00000000 --- a/Tools/DiskTool/src/include/rwlock.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Acess2 Disk Tool - */ -#ifndef _RWLOCK_H -#define _RWLOCK_H - -typedef char tRWLock; - -static inline int RWLock_AcquireRead(tRWLock *m) { - if(*m) Log_KernelPanic("---", "Double mutex lock"); - *m = 1; - return 0; -} -static inline int RWLock_AcquireWrite(tRWLock *m) { - if(*m) Log_KernelPanic("---", "Double mutex lock"); - *m = 1; - return 0; -} -static inline void RWLock_Release(tRWLock *m) { *m = 0; } - -#endif - diff --git a/Tools/DiskTool/src/logging.c b/Tools/DiskTool/src/logging.c deleted file mode 100644 index 0cb3ed32..00000000 --- a/Tools/DiskTool/src/logging.c +++ /dev/null @@ -1,204 +0,0 @@ -/* - * - */ -#include -#include -#include -#include -#include -#include -#include - -#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) -#define LOGTAIL() fprintf(stderr, "\e[0m\n") - -#define PUTERR(col,type) {\ - LOGHDR(col,type);\ - va_list args; va_start(args, Message);\ - vfprintf(stderr, Message, args);\ - va_end(args);\ - LOGTAIL();\ -} - -// === CODE === -void Log_KernelPanic(const char *Ident, const char *Message, ...) { - PUTERR("35", "k") - abort(); -} -void Log_Panic(const char *Ident, const char *Message, ...) - PUTERR("34", "p") -void Log_Error(const char *Ident, const char *Message, ...) - PUTERR("31", "e") -void Log_Warning(const char *Ident, const char *Message, ...) - PUTERR("33", "w") -void Log_Notice(const char *Ident, const char *Message, ...) - PUTERR("32", "n") -void Log_Log(const char *Ident, const char *Message, ...) - PUTERR("37", "l") -void Log_Debug(const char *Ident, const char *Message, ...) - PUTERR("37", "d") - -void Warning(const char *Message, ...) { - const char *Ident = ""; - PUTERR("33", "W") -} -void Log(const char *Message, ...) { - const char *Ident = ""; - PUTERR("37", "L") -} - -void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) -{ - const uint8_t *data = Data; - size_t ofs; - fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length); - for( ofs = 0; ofs + 16 <= Length; ofs += 16 ) - { - fprintf(stderr, "[HexDump ]d %s:", Prefix); - fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", - data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); - data += 8; - fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", - data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); - data += 8; - fprintf(stderr, "\n"); - } - - fprintf(stderr, "[HexDump ]d %s:", Prefix); - for( ; ofs < Length; ofs ++ ) - { - if( ofs % 8 == 0 ) fprintf(stderr, " "); - fprintf(stderr, " %02x", data[ofs%16]); - } - fprintf(stderr, "\n"); -} - - int giDebug_TraceLevel = 0; - -void Debug_TraceEnter(const char *Function, const char *Format, ...) -{ - const char *Ident = "Trace"; - LOGHDR("37","T"); - for( int i = 0; i < giDebug_TraceLevel; i ++ ) - fprintf(stderr, " "); - fprintf(stderr, "%s: (", Function); - - va_list args; - va_start(args, Format); - - int hasBeenPrev = 0; - while(*Format) - { - while( *Format && isblank(*Format) ) - Format ++; - if( !*Format ) break; - - char type = *Format++; - const char *start = Format; - while( *Format && !isblank(*Format) ) - Format ++; - - if(hasBeenPrev) - fprintf(stderr, ","); - hasBeenPrev = 1; - - fprintf(stderr, "%.*s=", (int)(Format-start), start); - switch(type) - { - case 'p': - fprintf(stderr, "%p", va_arg(args,const void *)); - break; - case 's': - fprintf(stderr, "\"%s\"", va_arg(args,const char *)); - break; - case 'i': - fprintf(stderr, "%i", va_arg(args,int)); - break; - case 'x': - fprintf(stderr, "0x%x", va_arg(args,unsigned int)); - break; - case 'X': - fprintf(stderr, "0x%"PRIx64, va_arg(args,uint64_t)); - break; - default: - va_arg(args,uintptr_t); - fprintf(stderr, "?"); - break; - } - } - - va_end(args); - - fprintf(stderr, ")"); - LOGTAIL(); - giDebug_TraceLevel ++; -} - -void Debug_TraceLog(const char *Function, const char *Format, ...) -{ - const char *Ident = "Trace"; - LOGHDR("37","T"); - - for( int i = 0; i < giDebug_TraceLevel; i ++ ) - fprintf(stderr, " "); - fprintf(stderr, "%s: ", Function); - - va_list args; - va_start(args, Format); - - vfprintf(stderr, Format, args); - - va_end(args); - LOGTAIL(); -} - -void Debug_TraceLeave(const char *Function, char Type, ...) -{ - if( giDebug_TraceLevel == 0 ) { - Log_Error("Debug", "Function %s called LEAVE without ENTER", Function); - } - - const char *Ident = "Trace"; - LOGHDR("37","T"); - - va_list args; - va_start(args, Type); - - if( giDebug_TraceLevel > 0 ) - { - giDebug_TraceLevel --; - for( int i = 0; i < giDebug_TraceLevel; i ++ ) - fprintf(stderr, " "); - } - fprintf(stderr, "%s: RETURN", Function); - switch(Type) - { - case '-': - break; - case 'i': - fprintf(stderr, " %i", va_arg(args, int)); - break; - case 'x': - fprintf(stderr, " 0x%x", va_arg(args, unsigned int)); - break; - case 'X': - fprintf(stderr, " 0x%"PRIx64, va_arg(args,uint64_t)); - break; - case 's': - fprintf(stderr, " \"%s\"", va_arg(args, const char *)); - break; - case 'p': - fprintf(stderr, " %p", va_arg(args, const void *)); - break; - case 'n': - fprintf(stderr, " NULL"); - break; - default: - fprintf(stderr, " ?"); - break; - } - - va_end(args); - LOGTAIL(); -} - diff --git a/Tools/DiskTool/src/main.c b/Tools/DiskTool/src/main.c deleted file mode 100644 index 2de26a6d..00000000 --- a/Tools/DiskTool/src/main.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - */ -#include -#include -#include -#include -#include -#include - -// === CODE === -int main(int argc, char *argv[]) -{ - // Parse arguments - for( int i = 1; i < argc; i ++ ) - { - if( strcmp("mount", argv[i]) == 0 || strcmp("-i", argv[i]) == 0 ) { - // Mount an image - if( argc - i < 3 ) { - fprintf(stderr, "mount takes 2 arguments (image and mountpoint)\n"); - exit(-1); - } - - if( DiskTool_MountImage(argv[i+2], argv[i+1]) ) { - fprintf(stderr, "Unable to mount '%s' as '%s'\n", argv[i+1], argv[i+2]); - break; - } - - i += 2; - continue ; - } - - if( strcmp("mountlvm", argv[i]) == 0 || strcmp("lvm", argv[i]) == 0 ) { - - if( argc - i < 3 ) { - fprintf(stderr, "lvm takes 2 arguments (iamge and ident)\n"); - exit(-1); - } - - if( DiskTool_RegisterLVM(argv[i+2], argv[i+1]) ) { - fprintf(stderr, "Unable to register '%s' as LVM '%s'\n", argv[i+1], argv[i+2]); - break; - } - - i += 2; - continue ; - } - - if( strcmp("ls", argv[i]) == 0 ) { - if( argc - i < 2 ) { - fprintf(stderr, "ls takes 1 argument (path)\n"); - break; - } - - DiskTool_ListDirectory(argv[i+1]); - i += 1; - continue ; - } - - if( strcmp("cp", argv[i]) == 0 ) { - - if( argc - i < 3 ) { - fprintf(stderr, "cp takes 2 arguments (source and destination)\n"); - break; - } - - DiskTool_Copy(argv[i+1], argv[i+2]); - - i += 2; - continue ; - } - - if( strcmp("cat", argv[i]) == 0 ) { - - if( argc - 1 < 2 ) { - fprintf(stderr, "cat takes 1 argument (path)\n"); - break; - } - - DiskTool_Cat(argv[i+1]); - - i += 1; - continue; - } - - fprintf(stderr, "Unknown command '%s'\n", argv[i]); - } - - DiskTool_Cleanup(); - - return 0; -} - -// NOTE: This is in a native compiled file because it needs access to the real errno macro -int *Threads_GetErrno(void) -{ - return &errno; -} - -// TODO: Move into a helper lib? -void itoa(char *buf, uint64_t num, int base, int minLength, char pad) -{ - char fmt[] = "%0ll*x"; - switch(base) - { - case 8: fmt[5] = 'o'; break; - case 10: fmt[5] = 'd'; break; - case 16: fmt[5] = 'x'; break; - } - if(pad != '0') { - fmt[1] = '%'; - sprintf(buf, fmt+1, minLength, num); - } - else { - sprintf(buf, fmt, minLength, num); - } -} - -int strpos(const char *Str, char Ch) -{ - const char *r = strchr(Str, Ch); - if(!r) return -1; - return r - Str; -} - -int strucmp(const char *s1, const char *s2) -{ - return strcasecmp(s1, s2); -} - -uint64_t DivMod64U(uint64_t value, uint64_t divisor, uint64_t *remainder) -{ - if(remainder) - *remainder = value % divisor; - return value / divisor; -} - -size_t _fwrite_stdout(size_t bytes, const void *data) -{ - return fwrite(data, bytes, 1, stdout); -} diff --git a/Tools/DiskTool/src/nativefs.c b/Tools/DiskTool/src/nativefs.c deleted file mode 120000 index 164ccfc9..00000000 --- a/Tools/DiskTool/src/nativefs.c +++ /dev/null @@ -1 +0,0 @@ -../../../AcessNative/acesskernel_src/nativefs.c \ No newline at end of file diff --git a/Tools/DiskTool/src/script.c b/Tools/DiskTool/src/script.c deleted file mode 100644 index e69de29b..00000000 diff --git a/Tools/DiskTool/src/threads.c b/Tools/DiskTool/src/threads.c deleted file mode 100644 index 79ec4c65..00000000 --- a/Tools/DiskTool/src/threads.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * - */ -#include -#include - -// === CODE === -tThread *Proc_GetCurThread(void) -{ - return NULL; -} - -void Threads_PostEvent(tThread *Thread, Uint32 Events) -{ - -} - -Uint32 Threads_WaitEvents(Uint32 Events) -{ - Log_KernelPanic("Threads", "Can't use _WaitEvents in DiskTool"); - return 0; -} - -void Threads_ClearEvent(Uint32 Mask) -{ - -} - -tUID Threads_GetUID(void) { return 0; } -tGID Threads_GetGID(void) { return 0; } - -int *Threads_GetMaxFD(void) { static int max_fd=32; return &max_fd; } -char **Threads_GetCWD(void) { static char *cwd; return &cwd; } -char **Threads_GetChroot(void) { static char *chroot; return &chroot; } - diff --git a/Tools/DiskTool/src/time.c b/Tools/DiskTool/src/time.c deleted file mode 100644 index 445b04b1..00000000 --- a/Tools/DiskTool/src/time.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Acess2 DiskTool - * - By John Hodge (thePowersGang) - * - * time.c - * - Timing functions (emulated) - */ -#include -#include - -// === CODE === -tTimer *Time_AllocateTimer(tTimerCallback *Callback, void *Argument) -{ - return NULL; -} - -void Time_ScheduleTimer(tTimer *Timer, int Delta) -{ - -} - -void Time_FreeTimer(tTimer *Timer) -{ - -} - -Sint64 now(void) -{ - // TODO: Translate UNIX time into Acess time - return 0; -} - diff --git a/Tools/DiskTool/src/vfs_handles.c b/Tools/DiskTool/src/vfs_handles.c deleted file mode 100644 index 4f30151a..00000000 --- a/Tools/DiskTool/src/vfs_handles.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * - */ -#include -#include -#include - -#define MAX_KERNEL_FILES 32 - -// === GLOBALS === -tVFS_Handle gaKernelHandles[MAX_KERNEL_FILES]; - -// === CODE === -int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode) -{ - for( int i = 0; i < MAX_KERNEL_FILES; i ++ ) - { - if(gaKernelHandles[i].Node) continue; - gaKernelHandles[i].Node = Node; - gaKernelHandles[i].Position = 0; - gaKernelHandles[i].Mode = Mode; - return i; - } - - return -1; -} - -tVFS_Handle *VFS_GetHandle(int ID) -{ - if( ID < 0 || ID >= MAX_KERNEL_FILES ) - return NULL; - return &gaKernelHandles[ID]; -} diff --git a/Tools/DiskTool/vfs_handles.c b/Tools/DiskTool/vfs_handles.c new file mode 100644 index 00000000..4f30151a --- /dev/null +++ b/Tools/DiskTool/vfs_handles.c @@ -0,0 +1,33 @@ +/* + * + */ +#include +#include +#include + +#define MAX_KERNEL_FILES 32 + +// === GLOBALS === +tVFS_Handle gaKernelHandles[MAX_KERNEL_FILES]; + +// === CODE === +int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode) +{ + for( int i = 0; i < MAX_KERNEL_FILES; i ++ ) + { + if(gaKernelHandles[i].Node) continue; + gaKernelHandles[i].Node = Node; + gaKernelHandles[i].Position = 0; + gaKernelHandles[i].Mode = Mode; + return i; + } + + return -1; +} + +tVFS_Handle *VFS_GetHandle(int ID) +{ + if( ID < 0 || ID >= MAX_KERNEL_FILES ) + return NULL; + return &gaKernelHandles[ID]; +} diff --git a/Tools/NetTest/Makefile b/Tools/NetTest/Makefile new file mode 100644 index 00000000..1679a283 --- /dev/null +++ b/Tools/NetTest/Makefile @@ -0,0 +1,103 @@ + +TARGET := $(shell gcc -v 2>&1 | grep Targ | awk '{print $$2}') + +include ../../Makefile.Version.cfg +-include Makefile.BuildNum +ifeq ($(BUILD_NUM),) +BUILD_NUM = 1 +endif + + +KERNEL_SRC = ../../KernelLand/Kernel/ +MODULE_SRC = ../../KernelLand/Modules/ + +BIN = ../nettest +# Kernel Sources (compiled with -ffreestanding) +K_OBJ := lib.o adt.o +#K_OBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o +#K_OBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o +#K_OBJ += vfs/fs/root.o vfs/fs/devfs.o +#K_OBJ += drvutil_disk.o drv/proc.o +# Modules +MODULES := IPStack +# Local kernel soruces (same as above, but located in same directory as Makefile) +L_OBJ = vfs_shim.o nic.o tcpclient.o tcpserver.o +# Native Sources (compiled as usual) +N_OBJ = main.o tap.o + +# Compilation Options +CFLAGS := -Wall -std=gnu99 -g -Werror +CPPFLAGS := -I include/ -I ../nativelib/include +K_CPPFLAGS := -I $(KERNEL_SRC)include -I $(MODULE_SRC) +LDFLAGS += -Wl,--defsym,__buildnum=$(BUILD_NUM) -g -L .. -lnativelib + +BUILDINFO_OBJ := obj/$(TARGET)/buildinfo.o +BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c) + +# ==================== +# == Start of Magic == +# ==================== +# -- Load modules --- +$(foreach module,$(MODULES), $(eval include $(MODULE_SRC)$(module)/Makefile) $(eval M_OBJ += $(addprefix $(module)/,$(OBJ))) ) + +# -- Apply Prefixes to object paths +OBJ_PREFIX = obj/$(TARGET)/ +K_OBJ_PREFIX = $(OBJ_PREFIX)_Kernel/ +M_OBJ_PREFIX = $(OBJ_PREFIX)_Module/ +K_OBJ := $(addprefix $(K_OBJ_PREFIX),$(K_OBJ)) +M_OBJ := $(addprefix $(M_OBJ_PREFIX),$(M_OBJ)) +L_OBJ := $(addprefix $(OBJ_PREFIX),$(L_OBJ)) +N_OBJ := $(addprefix $(OBJ_PREFIX),$(N_OBJ)) + +OBJ := $(N_OBJ) $(L_OBJ) $(K_OBJ) $(M_OBJ) $(BUILDINFO_OBJ) + +DEPFILES = $(filter %.o,$(OBJ)) +DEPFILES := $(DEPFILES:%=%.dep) + + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) -f $(OBJ) $(DEPFILES) $(BIN) + +$(BIN): $(OBJ) + @echo [CC Link] -o $(BIN) + @$(CC) -o $(BIN) $(OBJ) $(LDFLAGS) + @echo BUILD_NUM = $$(( $(BUILD_NUM) + 1 )) > Makefile.BuildNum + +$(M_OBJ): $(M_OBJ_PREFIX)%.o: $(MODULE_SRC)%.c + @mkdir -p $(dir $@) + @echo [CC Module] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(K_OBJ): $(K_OBJ_PREFIX)%.o: $(KERNEL_SRC)%.c + @mkdir -p $(dir $@) + @echo [CC Kernel] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(L_OBJ): $(OBJ_PREFIX)%.o: %.c + @mkdir -p $(dir $@) + @echo [CC Local] -o $@ + @$(CC) -c $< -o $@ -ffreestanding $(CFLAGS) $(CPPFLAGS) $(K_CPPFLAGS) -MMD -MP -MF $@.dep + +$(N_OBJ): $(OBJ_PREFIX)%.o: %.c + @mkdir -p $(dir $@) + @echo [CC Native] -o $@ + @$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) -MMD -MP -MF $@.dep + +# Hacky buildinfo.c file +$(BUILDINFO_SRC): $(filter-out $(BUILDINFO_OBJ), $(OBJ)) Makefile + @echo "" > $@ + @echo "const char gsKernelVersion[] = \"$(ACESS_VERSION)\";" >> $@ + @echo "const char gsGitHash[] = \""`git log -n 1 | head -n 1 | awk '{print $$2}'`"\";" >> $@ + @echo "const int giBuildNumber = $(BUILD_NUM);" >> $@ + @echo "const char gsBuildInfo[] = \"Acess2 DiskTool v$(ACESS_VERSION)\";" >> $@ +$(BUILDINFO_OBJ): $(BUILDINFO_SRC) + @echo [CC] -o $@ + @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) + +$(OBJ): Makefile + +-include $(DEPFILES) diff --git a/Tools/NetTest/logging.c b/Tools/NetTest/logging.c new file mode 100644 index 00000000..0cb3ed32 --- /dev/null +++ b/Tools/NetTest/logging.c @@ -0,0 +1,204 @@ +/* + * + */ +#include +#include +#include +#include +#include +#include +#include + +#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) +#define LOGTAIL() fprintf(stderr, "\e[0m\n") + +#define PUTERR(col,type) {\ + LOGHDR(col,type);\ + va_list args; va_start(args, Message);\ + vfprintf(stderr, Message, args);\ + va_end(args);\ + LOGTAIL();\ +} + +// === CODE === +void Log_KernelPanic(const char *Ident, const char *Message, ...) { + PUTERR("35", "k") + abort(); +} +void Log_Panic(const char *Ident, const char *Message, ...) + PUTERR("34", "p") +void Log_Error(const char *Ident, const char *Message, ...) + PUTERR("31", "e") +void Log_Warning(const char *Ident, const char *Message, ...) + PUTERR("33", "w") +void Log_Notice(const char *Ident, const char *Message, ...) + PUTERR("32", "n") +void Log_Log(const char *Ident, const char *Message, ...) + PUTERR("37", "l") +void Log_Debug(const char *Ident, const char *Message, ...) + PUTERR("37", "d") + +void Warning(const char *Message, ...) { + const char *Ident = ""; + PUTERR("33", "W") +} +void Log(const char *Message, ...) { + const char *Ident = ""; + PUTERR("37", "L") +} + +void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) +{ + const uint8_t *data = Data; + size_t ofs; + fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length); + for( ofs = 0; ofs + 16 <= Length; ofs += 16 ) + { + fprintf(stderr, "[HexDump ]d %s:", Prefix); + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, "\n"); + } + + fprintf(stderr, "[HexDump ]d %s:", Prefix); + for( ; ofs < Length; ofs ++ ) + { + if( ofs % 8 == 0 ) fprintf(stderr, " "); + fprintf(stderr, " %02x", data[ofs%16]); + } + fprintf(stderr, "\n"); +} + + int giDebug_TraceLevel = 0; + +void Debug_TraceEnter(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: (", Function); + + va_list args; + va_start(args, Format); + + int hasBeenPrev = 0; + while(*Format) + { + while( *Format && isblank(*Format) ) + Format ++; + if( !*Format ) break; + + char type = *Format++; + const char *start = Format; + while( *Format && !isblank(*Format) ) + Format ++; + + if(hasBeenPrev) + fprintf(stderr, ","); + hasBeenPrev = 1; + + fprintf(stderr, "%.*s=", (int)(Format-start), start); + switch(type) + { + case 'p': + fprintf(stderr, "%p", va_arg(args,const void *)); + break; + case 's': + fprintf(stderr, "\"%s\"", va_arg(args,const char *)); + break; + case 'i': + fprintf(stderr, "%i", va_arg(args,int)); + break; + case 'x': + fprintf(stderr, "0x%x", va_arg(args,unsigned int)); + break; + case 'X': + fprintf(stderr, "0x%"PRIx64, va_arg(args,uint64_t)); + break; + default: + va_arg(args,uintptr_t); + fprintf(stderr, "?"); + break; + } + } + + va_end(args); + + fprintf(stderr, ")"); + LOGTAIL(); + giDebug_TraceLevel ++; +} + +void Debug_TraceLog(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: ", Function); + + va_list args; + va_start(args, Format); + + vfprintf(stderr, Format, args); + + va_end(args); + LOGTAIL(); +} + +void Debug_TraceLeave(const char *Function, char Type, ...) +{ + if( giDebug_TraceLevel == 0 ) { + Log_Error("Debug", "Function %s called LEAVE without ENTER", Function); + } + + const char *Ident = "Trace"; + LOGHDR("37","T"); + + va_list args; + va_start(args, Type); + + if( giDebug_TraceLevel > 0 ) + { + giDebug_TraceLevel --; + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + } + fprintf(stderr, "%s: RETURN", Function); + switch(Type) + { + case '-': + break; + case 'i': + fprintf(stderr, " %i", va_arg(args, int)); + break; + case 'x': + fprintf(stderr, " 0x%x", va_arg(args, unsigned int)); + break; + case 'X': + fprintf(stderr, " 0x%"PRIx64, va_arg(args,uint64_t)); + break; + case 's': + fprintf(stderr, " \"%s\"", va_arg(args, const char *)); + break; + case 'p': + fprintf(stderr, " %p", va_arg(args, const void *)); + break; + case 'n': + fprintf(stderr, " NULL"); + break; + default: + fprintf(stderr, " ?"); + break; + } + + va_end(args); + LOGTAIL(); +} + diff --git a/Tools/NetTest/main.c b/Tools/NetTest/main.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/NetTest/nic.c b/Tools/NetTest/nic.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/NetTest/tap.c b/Tools/NetTest/tap.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/NetTest/tcpclient.c b/Tools/NetTest/tcpclient.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/NetTest/tcpserver.c b/Tools/NetTest/tcpserver.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/NetTest/vfs_shim.c b/Tools/NetTest/vfs_shim.c new file mode 100644 index 00000000..e69de29b diff --git a/Tools/nativelib/Makefile b/Tools/nativelib/Makefile new file mode 100644 index 00000000..2a6eb6df --- /dev/null +++ b/Tools/nativelib/Makefile @@ -0,0 +1,27 @@ + +KERNEL_DIR := ../../KernelLand/Kernel + +NOBJ := logging.o misc.o +KOBJ := threads.o time.o mutex.o +OBJ := $(NOBJ) $(KOBJ) +BIN := ../libnativelib.a + +CFLAGS := -Wall -std=c99 -Werror +CPPFLAGS := -I include + + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) $(BIN) $(OBJ) + +$(BIN): $(OBJ) + ar cru $(BIN) $(OBJ) + +$(NOBJ): %.o: %.c + $(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) +$(KOBJ): %.o: %.c + $(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include + diff --git a/Tools/nativelib/include/acess.h b/Tools/nativelib/include/acess.h new file mode 100644 index 00000000..c7f77751 --- /dev/null +++ b/Tools/nativelib/include/acess.h @@ -0,0 +1,162 @@ +/* + * Acess2 DiskTool utility + * - By John Hodge (thePowersGang) + * + * include/acess.h + * - Mock kernel core header + */ +#ifndef _DISKTOOL__ACESS_H_ +#define _DISKTOOL__ACESS_H_ + +#define CONCAT(x,y) x ## y +#define EXPAND_CONCAT(x,y) CONCAT(x,y) +#define STR(x) #x +#define EXPAND_STR(x) STR(x) + +#define ASSERT(x) do{}while(0) + +extern char __buildnum[]; +#define BUILD_NUM ((int)(Uint)&__buildnum) +extern const char gsGitHash[]; +extern const char gsBuildInfo[]; +#define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF)) + + +#define BITS 32 +#define NULL ((void*)0) +#include + +typedef uintptr_t Uint; +//typedef unsigned int size_t; +#include +typedef uint64_t off_t; +typedef char BOOL; + + +typedef uint8_t Uint8; +typedef uint16_t Uint16; +typedef uint32_t Uint32; +typedef uint64_t Uint64; + +typedef int8_t Sint8; +typedef int16_t Sint16; +typedef int32_t Sint32; +typedef int64_t Sint64; + +typedef uintptr_t tVAddr; +typedef uint32_t tPAddr; + +typedef uint32_t tUID; +typedef uint32_t tGID; +typedef uint32_t tTID; + +// NOTE: Since this is single-threaded (for now) mutexes can be implimented as simple locks +typedef char tShortSpinlock; + +typedef int64_t tTime; +extern tTime now(void); +extern int64_t timestamp(int sec, int min, int hr, int day, int month, int year); +extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms); + +#define PACKED __attribute__((packed)) +#define DEPRECATED +#define EXPORT(s) +#define EXPORTV(s) + +#include + +// These are actually library functions, but they can't be included, so they're defined manually +extern void *malloc(size_t bytes); +extern void *calloc(size_t nmemb, size_t size); +extern void *realloc(void *oldptr, size_t bytes); +extern void free(void *buffer); + +#include +#include + +// Threads +extern int *Threads_GetErrno(void); +//extern tPGID Threads_GetPGID(void); +//extern tPID Threads_GetPID(void); +extern tTID Threads_GetTID(void); +extern tUID Threads_GetUID(void); +extern tGID Threads_GetGID(void); + +extern struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data); +extern void Threads_Sleep(void); +extern void Threads_Yield(void); +extern int Threads_SetName(const char *NewName); + +// Kinda hacky way of not colliding with native errno +#define errno (*(Threads_GetErrno())) + +/** + * \name Endianness Swapping + * \{ + */ +#ifdef __BIG_ENDIAN__ +#define LittleEndian16(_val) SwapEndian16(_val) +#define LittleEndian32(_val) SwapEndian32(_val) +#define LittleEndian64(_val) SwapEndian32(_val) +#define BigEndian16(_val) (_val) +#define BigEndian32(_val) (_val) +#define BigEndian64(_val) (_val) +#else +#define LittleEndian16(_val) (_val) +#define LittleEndian32(_val) (_val) +#define LittleEndian64(_val) (_val) +#define BigEndian16(_val) SwapEndian16(_val) +#define BigEndian32(_val) SwapEndian32(_val) +#define BigEndian64(_val) SwapEndian64(_val) +#endif +extern Uint16 SwapEndian16(Uint16 Val); +extern Uint32 SwapEndian32(Uint32 Val); +extern Uint64 SwapEndian64(Uint64 Val); +/** + * \} + */ + + +#include +#include +#if 0 +extern int strucmp(const char *s1, const char *s2); +extern int strpos(const char *Str, char Ch); +extern void itoa(char *buf, uint64_t num, int base, int minLength, char pad); +extern int snprintf(char *buf, size_t len, const char *fmt, ...); +extern int sprintf(char *buf, const char *fmt, ...); +extern int ReadUTF8(const Uint8 *str, Uint32 *Val); +extern int WriteUTF8(Uint8 *str, Uint32 Val); +extern int ParseInt(const char *string, int *Val); +#include +extern int ModUtil_SetIdent(char *Dest, const char *Value); +extern int ModUtil_LookupString(const char **Array, const char *Needle); +extern Uint8 ByteSum(const void *Ptr, int Size); +extern int Hex(char *Dest, size_t Size, const Uint8 *SourceData); +extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString); +#endif +#define CheckString(str) (1) +#define CheckMem(mem,sz) (1) +extern int rand(void); + +// TODO: Move out? +extern int DivUp(int value, int divisor); +extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); +static inline int MIN(int a, int b) { return a < b ? a : b; } +static inline int MAX(int a, int b) { return a > b ? a : b; } + +#if USE_MULTITHREADING +#error "TODO: Impliment multithreaded SHORTLOCK" +#else +static inline void SHORTLOCK(tShortSpinlock *Lock) { + if(*Lock) Log_KernelPanic("---", "Double short lock"); + *Lock = 1; +} +static inline void SHORTREL(tShortSpinlock *m) { *m = 0; } +static inline int CPU_HAS_LOCK(tShortSpinlock *m) { return *m; } +#endif + +static inline intptr_t MM_GetPhysAddr(void *Ptr) { return 1; } + +#endif + diff --git a/Tools/nativelib/include/acess_logging.h b/Tools/nativelib/include/acess_logging.h new file mode 100644 index 00000000..5f007f9b --- /dev/null +++ b/Tools/nativelib/include/acess_logging.h @@ -0,0 +1,34 @@ + +#ifndef _DISKTOOL__ACESS_LOGGING_H_ +#define _DISKTOOL__ACESS_LOGGING_H_ + +#if DEBUG +# define ENTER(str, v...) Debug_TraceEnter(__func__, str, ##v) +# define LOG(fmt, v...) Debug_TraceLog(__func__, fmt, ##v) +# define LEAVE(t, v...) Debug_TraceLeave(__func__, t, ##v) +# define LEAVE_RET(t,v) do{LEAVE('-');return v;}while(0) +#else +# define ENTER(...) do{}while(0) +# define LOG(...) do{}while(0) +# define LEAVE(...) do{}while(0) +# define LEAVE_RET(t,v) do{return v;}while(0) +#endif + +extern void Log_KernelPanic(const char *Ident, const char *Message, ...) __attribute__((noreturn)); +extern void Log_Panic(const char *Ident, const char *Message, ...); +extern void Log_Error(const char *Ident, const char *Message, ...); +extern void Log_Warning(const char *Ident, const char *Message, ...); +extern void Log_Notice(const char *Ident, const char *Message, ...); +extern void Log_Log(const char *Ident, const char *Message, ...); +extern void Log_Debug(const char *Ident, const char *Message, ...); + +extern void Warning(const char *Message, ...); +extern void Log(const char *Message, ...); +extern void Debug_HexDump(const char *Prefix, const void *Data, size_t Length); + +extern void Debug_TraceEnter(const char *Function, const char *Format, ...); +extern void Debug_TraceLog(const char *Function, const char *Format, ...); +extern void Debug_TraceLeave(const char *Function, char Type, ...); + +#endif + diff --git a/Tools/nativelib/include/arch.h b/Tools/nativelib/include/arch.h new file mode 100644 index 00000000..e69de29b diff --git a/Tools/nativelib/logging.c b/Tools/nativelib/logging.c new file mode 100644 index 00000000..0cb3ed32 --- /dev/null +++ b/Tools/nativelib/logging.c @@ -0,0 +1,204 @@ +/* + * + */ +#include +#include +#include +#include +#include +#include +#include + +#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) +#define LOGTAIL() fprintf(stderr, "\e[0m\n") + +#define PUTERR(col,type) {\ + LOGHDR(col,type);\ + va_list args; va_start(args, Message);\ + vfprintf(stderr, Message, args);\ + va_end(args);\ + LOGTAIL();\ +} + +// === CODE === +void Log_KernelPanic(const char *Ident, const char *Message, ...) { + PUTERR("35", "k") + abort(); +} +void Log_Panic(const char *Ident, const char *Message, ...) + PUTERR("34", "p") +void Log_Error(const char *Ident, const char *Message, ...) + PUTERR("31", "e") +void Log_Warning(const char *Ident, const char *Message, ...) + PUTERR("33", "w") +void Log_Notice(const char *Ident, const char *Message, ...) + PUTERR("32", "n") +void Log_Log(const char *Ident, const char *Message, ...) + PUTERR("37", "l") +void Log_Debug(const char *Ident, const char *Message, ...) + PUTERR("37", "d") + +void Warning(const char *Message, ...) { + const char *Ident = ""; + PUTERR("33", "W") +} +void Log(const char *Message, ...) { + const char *Ident = ""; + PUTERR("37", "L") +} + +void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) +{ + const uint8_t *data = Data; + size_t ofs; + fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length); + for( ofs = 0; ofs + 16 <= Length; ofs += 16 ) + { + fprintf(stderr, "[HexDump ]d %s:", Prefix); + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, "\n"); + } + + fprintf(stderr, "[HexDump ]d %s:", Prefix); + for( ; ofs < Length; ofs ++ ) + { + if( ofs % 8 == 0 ) fprintf(stderr, " "); + fprintf(stderr, " %02x", data[ofs%16]); + } + fprintf(stderr, "\n"); +} + + int giDebug_TraceLevel = 0; + +void Debug_TraceEnter(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: (", Function); + + va_list args; + va_start(args, Format); + + int hasBeenPrev = 0; + while(*Format) + { + while( *Format && isblank(*Format) ) + Format ++; + if( !*Format ) break; + + char type = *Format++; + const char *start = Format; + while( *Format && !isblank(*Format) ) + Format ++; + + if(hasBeenPrev) + fprintf(stderr, ","); + hasBeenPrev = 1; + + fprintf(stderr, "%.*s=", (int)(Format-start), start); + switch(type) + { + case 'p': + fprintf(stderr, "%p", va_arg(args,const void *)); + break; + case 's': + fprintf(stderr, "\"%s\"", va_arg(args,const char *)); + break; + case 'i': + fprintf(stderr, "%i", va_arg(args,int)); + break; + case 'x': + fprintf(stderr, "0x%x", va_arg(args,unsigned int)); + break; + case 'X': + fprintf(stderr, "0x%"PRIx64, va_arg(args,uint64_t)); + break; + default: + va_arg(args,uintptr_t); + fprintf(stderr, "?"); + break; + } + } + + va_end(args); + + fprintf(stderr, ")"); + LOGTAIL(); + giDebug_TraceLevel ++; +} + +void Debug_TraceLog(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: ", Function); + + va_list args; + va_start(args, Format); + + vfprintf(stderr, Format, args); + + va_end(args); + LOGTAIL(); +} + +void Debug_TraceLeave(const char *Function, char Type, ...) +{ + if( giDebug_TraceLevel == 0 ) { + Log_Error("Debug", "Function %s called LEAVE without ENTER", Function); + } + + const char *Ident = "Trace"; + LOGHDR("37","T"); + + va_list args; + va_start(args, Type); + + if( giDebug_TraceLevel > 0 ) + { + giDebug_TraceLevel --; + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + } + fprintf(stderr, "%s: RETURN", Function); + switch(Type) + { + case '-': + break; + case 'i': + fprintf(stderr, " %i", va_arg(args, int)); + break; + case 'x': + fprintf(stderr, " 0x%x", va_arg(args, unsigned int)); + break; + case 'X': + fprintf(stderr, " 0x%"PRIx64, va_arg(args,uint64_t)); + break; + case 's': + fprintf(stderr, " \"%s\"", va_arg(args, const char *)); + break; + case 'p': + fprintf(stderr, " %p", va_arg(args, const void *)); + break; + case 'n': + fprintf(stderr, " NULL"); + break; + default: + fprintf(stderr, " ?"); + break; + } + + va_end(args); + LOGTAIL(); +} + diff --git a/Tools/nativelib/misc.c b/Tools/nativelib/misc.c new file mode 100644 index 00000000..20a7442d --- /dev/null +++ b/Tools/nativelib/misc.c @@ -0,0 +1,57 @@ +/* + * Acess2 nativelib + * - By John Hodge (thePowersGang) + * + * misc.c + * - Random functions + */ +#include +#include +#include +#include +#include // strcasecmp + +// TODO: Move into a helper lib? +void itoa(char *buf, uint64_t num, int base, int minLength, char pad) +{ + char fmt[] = "%0ll*x"; + switch(base) + { + case 8: fmt[5] = 'o'; break; + case 10: fmt[5] = 'd'; break; + case 16: fmt[5] = 'x'; break; + } + if(pad != '0') { + fmt[1] = '%'; + sprintf(buf, fmt+1, minLength, num); + } + else { + sprintf(buf, fmt, minLength, num); + } +} + +int ParseInt(const char *string, int *value) +{ + char *next; + *value = strtol(string, &next, 0); + return next - string; +} + +int strpos(const char *Str, char Ch) +{ + const char *r = strchr(Str, Ch); + if(!r) return -1; + return r - Str; +} + +int strucmp(const char *s1, const char *s2) +{ + return strcasecmp(s1, s2); +} + +uint64_t DivMod64U(uint64_t value, uint64_t divisor, uint64_t *remainder) +{ + if(remainder) + *remainder = value % divisor; + return value / divisor; +} diff --git a/Tools/nativelib/mutex.c b/Tools/nativelib/mutex.c new file mode 100644 index 00000000..54940f71 --- /dev/null +++ b/Tools/nativelib/mutex.c @@ -0,0 +1,22 @@ +/* + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * mutex.c + * - Mutex emulation + */ +#include + + +// === CODE === +int Mutex_Acquire(tMutex *Mutex) +{ + // TODO: Assert + return 0; +} + +void Mutex_Release(tMutex *Mutex) +{ + return; +} + diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c new file mode 100644 index 00000000..5055dc3e --- /dev/null +++ b/Tools/nativelib/threads.c @@ -0,0 +1,65 @@ +/* + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * threads.c + * - Threads handling + */ +#include +#include + +// === CODE === +tThread *Proc_GetCurThread(void) +{ + return NULL; +} + +void Threads_PostEvent(tThread *Thread, Uint32 Events) +{ + +} + +Uint32 Threads_WaitEvents(Uint32 Events) +{ + Log_KernelPanic("Threads", "Can't use _WaitEvents in DiskTool"); + return 0; +} + +void Threads_ClearEvent(Uint32 Mask) +{ + +} + +tUID Threads_GetUID(void) { return 0; } +tGID Threads_GetGID(void) { return 0; } + +tTID Threads_GetTID(void) { return 0; } + +int *Threads_GetMaxFD(void) { static int max_fd=32; return &max_fd; } +char **Threads_GetCWD(void) { static char *cwd; return &cwd; } +char **Threads_GetChroot(void) { static char *chroot; return &chroot; } + +void Threads_Yield(void) +{ + Log_Warning("Threads", "Threads_Yield DEFINITELY shouldn't be used"); +} + +void Threads_Sleep(void) +{ + Log_Warning("Threads", "Threads_Sleep shouldn't be used"); +} + +int Threads_SetName(const char *Name) +{ + Log_Notice("Threads", "TODO: Threads_SetName('%s')", Name); + return 0; +} + +int *Threads_GetErrno(void) __attribute__ ((weak)); + +int *Threads_GetErrno(void)// __attribute__ ((weak)) +{ + static int a_errno; + return &a_errno; +} + diff --git a/Tools/nativelib/time.c b/Tools/nativelib/time.c new file mode 100644 index 00000000..a091c0bf --- /dev/null +++ b/Tools/nativelib/time.c @@ -0,0 +1,37 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * time.c + * - Timing functions (emulated) + */ +#include +#include + +// === CODE === +tTimer *Time_AllocateTimer(tTimerCallback *Callback, void *Argument) +{ + return NULL; +} + +void Time_ScheduleTimer(tTimer *Timer, int Delta) +{ + +} + +void Time_RemoveTimer(tTimer *Timer) +{ + +} + +void Time_FreeTimer(tTimer *Timer) +{ + +} + +Sint64 now(void) +{ + // TODO: Translate UNIX time into Acess time + return 0; +} +