diff --git a/exp/Memory manager/.cproject b/exp/Memory manager/.cproject new file mode 100644 index 0000000..a03d121 --- /dev/null +++ b/exp/Memory manager/.cproject @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/exp/Memory manager/Debug/Memory manager.exe b/exp/Memory manager/Debug/Memory manager.exe new file mode 100644 index 0000000..3784a52 Binary files /dev/null and b/exp/Memory manager/Debug/Memory manager.exe differ diff --git a/exp/Memory manager/Debug/main.d b/exp/Memory manager/Debug/main.d new file mode 100644 index 0000000..9f7a18b --- /dev/null +++ b/exp/Memory manager/Debug/main.d @@ -0,0 +1,3 @@ +main.d main.o: ../main.c ../mem.h + +../mem.h: diff --git a/exp/Memory manager/Debug/main.o b/exp/Memory manager/Debug/main.o new file mode 100644 index 0000000..341a5b6 Binary files /dev/null and b/exp/Memory manager/Debug/main.o differ diff --git a/exp/Memory manager/Debug/makefile b/exp/Memory manager/Debug/makefile new file mode 100644 index 0000000..823a137 --- /dev/null +++ b/exp/Memory manager/Debug/makefile @@ -0,0 +1,43 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +endif + +-include ../makefile.defs + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: Memory\ manager.exe + +# Tool invocations +Memory\ manager.exe: $(OBJS) $(USER_OBJS) + @echo 'Building target: $@' + @echo 'Invoking: Cygwin C Linker' + gcc -o "Memory manager.exe" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) "Memory manager.exe" + -@echo ' ' + +.PHONY: all clean dependents +.SECONDARY: + +-include ../makefile.targets diff --git a/exp/Memory manager/Debug/memory.d b/exp/Memory manager/Debug/memory.d new file mode 100644 index 0000000..26e6bbd --- /dev/null +++ b/exp/Memory manager/Debug/memory.d @@ -0,0 +1,3 @@ +memory.d memory.o: ../memory.c ../mem.h + +../mem.h: diff --git a/exp/Memory manager/Debug/memory.o b/exp/Memory manager/Debug/memory.o new file mode 100644 index 0000000..c02b151 Binary files /dev/null and b/exp/Memory manager/Debug/memory.o differ diff --git a/exp/Memory manager/Debug/objects.mk b/exp/Memory manager/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/exp/Memory manager/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/exp/Memory manager/Debug/sources.mk b/exp/Memory manager/Debug/sources.mk new file mode 100644 index 0000000..df1db10 --- /dev/null +++ b/exp/Memory manager/Debug/sources.mk @@ -0,0 +1,17 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +O_SRCS := +C_SRCS := +S_UPPER_SRCS := +OBJ_SRCS := +ASM_SRCS := +OBJS := +C_DEPS := +EXECUTABLES := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/exp/Memory manager/Debug/subdir.mk b/exp/Memory manager/Debug/subdir.mk new file mode 100644 index 0000000..9f531a7 --- /dev/null +++ b/exp/Memory manager/Debug/subdir.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +C_SRCS += \ +../main.c \ +../memory.c + +OBJS += \ +./main.o \ +./memory.o + +C_DEPS += \ +./main.d \ +./memory.d + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.c + @echo 'Building file: $<' + @echo 'Invoking: Cygwin C Compiler' + gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/exp/Memory manager/Memory manager.exe.stackdump b/exp/Memory manager/Memory manager.exe.stackdump new file mode 100644 index 0000000..18ea39f --- /dev/null +++ b/exp/Memory manager/Memory manager.exe.stackdump @@ -0,0 +1,12 @@ +Exception: STATUS_ACCESS_VIOLATION at eip=004016B3 +eax=00A01CB0 ebx=009A0000 ecx=00403100 edx=FFF9E34C esi=00A01CAA edi=00000000 +ebp=0022CB18 esp=0022CAF0 program=C:\Users\Tiberiu\workspace\osdev\Memory manager\Debug\Memory manager.exe, pid 5948, thread main +cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023 +Stack trace: +Frame Function Args +0022CB18 004016B3 (00000050, 00403100, 0000000A, 6112C040) +0022CB38 00401376 (00000002, 0022CB60, 00000000, 00000000) +0022CCA8 00401259 (00403100, 00001000, 00000000, 00401415) +0022CCD8 0040143F (00000001, 009C8608, 009C8508, 00008000) +0022CD68 61007038 (00000000, 0022CDA4, 61006980, 7FFDC000) +End of stack trace diff --git a/exp/Memory manager/PROJEC~1 b/exp/Memory manager/PROJEC~1 new file mode 100644 index 0000000..76b7b43 --- /dev/null +++ b/exp/Memory manager/PROJEC~1 @@ -0,0 +1,82 @@ + + + Memory manager + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.buildLocation + ${workspace_loc:/Memory manager/Debug} + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/exp/Memory manager/main.c b/exp/Memory manager/main.c new file mode 100644 index 0000000..e1a5933 --- /dev/null +++ b/exp/Memory manager/main.c @@ -0,0 +1,33 @@ +/* + * main.c + * + * Created on: Aug 24, 2011 + * Author: Tiberiu + */ + + +#include +#include +#include "mem.h" + +void print_memmap() +{ + uint32 i; + printf("memory map:\n"); + for (i = 0; i <= TotalFrames>>5; i++) + printf("0x%x ", FrameMap[i]); + printf("\n"); +} + +int main(int argc, char **argv) { + + MemPhInitialize(1024); + MemPhReserveFrames(0, 0x20000); + MemPhReserveFrames(0xF0000, 0x10000); + + // Find a free frame + uint32 i; + for (i = 0; i < 10; i++) MemPhAllocFrame(0,0); + + return 0; +} diff --git a/exp/Memory manager/mem.h b/exp/Memory manager/mem.h new file mode 100644 index 0000000..7209e72 --- /dev/null +++ b/exp/Memory manager/mem.h @@ -0,0 +1,37 @@ +/* + * ord-arr.h + * + * Created on: Aug 24, 2011 + * Author: Tiberiu + */ + +#ifndef ORD_ARR_H_ +#define ORD_ARR_H_ +#include + +typedef unsigned int uint32; +typedef unsigned char uint8; + +#define Log(...) { printf(__VA_ARGS__); } +#define Error(...) { fprintf(stderr, __VA_ARGS__); } + +#define Panic(...) { printf("[PANIC] Kernel panic: "); printf(__VA_ARGS__); } + +#define LogMem(...) { Log("[MemLog] "); Log(__VA_ARGS__); } +#define ErrorMem(...) { Error("[MemErr] "); Error(__VA_ARGS__); } + +extern uint32* FrameMap; +extern uint32 TotalFrames; +extern uint32 TotalMemory; + +extern void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset); +extern uint32 ConvertFrameToIndex (uint32 address, uint32 offset); + +extern void MemPhSetFrame (uint32 frame, uint8 value); +extern uint32 MemPhGetFrame (uint32 frame); +extern uint32 MemPhFindFreeFrame(); +extern void MemPhAllocFrame(uint8 isKernel, uint8 isWriteable); +extern void MemPhInitialize(uint32 SystemMemoryKb); +extern void MemPhReserveFrames (uint32 address, uint32 length); + +#endif /* ORD_ARR_H_ */ diff --git a/exp/Memory manager/memory.c b/exp/Memory manager/memory.c new file mode 100644 index 0000000..554bf7f --- /dev/null +++ b/exp/Memory manager/memory.c @@ -0,0 +1,102 @@ +/* + * mem-phys.c + * + * Created on: Aug 27, 2011 + * Author: Tiberiu + */ + +#include +#include "mem.h" + +uint32* FrameMap; +uint32 TotalFrames; +uint32 TotalMemory; + +void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset) +{ + *address = (index >> 5); + *offset = index & 0x1f; +} + +uint32 ConvertFrameToIndex (uint32 address, uint32 offset) +{ + return (address<<5) | offset; +} + +void MemPhSetFrame (uint32 frame, uint8 value) +{ + uint32 addr, off; + ConvertIndexToFrame(frame, &addr, &off); + + if (value) FrameMap[addr] |= 1<> 5; addr++) + if (FrameMap[addr] != 0xffffffff) + { + for (pos = 0; (FrameMap[addr] & (1<Frame != 0) return; + + uint32 free = MemPhFindFreeFrame(); + if (!free) return; + + LogMem("Physical: allocated frame %u.\n", free); + + MemPhSetFrame(free, 1); + /*page->Present = 1; + page->User = (isKernel) ? 0 : 1; + page->Writeable = (isWriteable) ? 1 : 0; + page->Frame = free;*/ +} + +/*void MemPhFreeFrame()//Page* page) +{ + uint32 frame = page->Frame; + if (!frame) return; + + MemPhSetFrame(frame, 0); + page->Frame = 0; +}*/ + +void MemPhInitialize(uint32 SystemMemoryKb) +{ + LogMem("Starting physical memory manager...\n"); + TotalFrames = SystemMemoryKb >> 2; + TotalMemory = SystemMemoryKb; + + FrameMap = (uint32*) malloc(sizeof(uint32) * (1 + (TotalFrames>>5))); + memset(FrameMap, 0, sizeof(uint32) * (1 + (TotalFrames>>5))); + LogMem("Started ok!, found %ukb total_frames=%u\n", SystemMemoryKb, TotalFrames); +} + +void MemPhReserveFrames (uint32 address, uint32 length) +{ + address >>= 12; + length = (length>>12) + ((length & 0xfff) > 0); + uint32 end = address + length; + + for (; address < end && address < TotalFrames ; address++) + MemPhSetFrame(address, 1); +} diff --git a/exp/luxRomImageMaker/.cproject b/exp/luxRomImageMaker/.cproject new file mode 100644 index 0000000..9df1331 --- /dev/null +++ b/exp/luxRomImageMaker/.cproject @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/exp/luxRomImageMaker/Debug/commands.d b/exp/luxRomImageMaker/Debug/commands.d new file mode 100644 index 0000000..0492857 --- /dev/null +++ b/exp/luxRomImageMaker/Debug/commands.d @@ -0,0 +1,3 @@ +commands.d commands.o: ../commands.cpp ../app.h + +../app.h: diff --git a/exp/luxRomImageMaker/Debug/commands.o b/exp/luxRomImageMaker/Debug/commands.o new file mode 100644 index 0000000..7c25de2 Binary files /dev/null and b/exp/luxRomImageMaker/Debug/commands.o differ diff --git a/exp/luxRomImageMaker/Debug/luxRomImageMaker.exe b/exp/luxRomImageMaker/Debug/luxRomImageMaker.exe new file mode 100644 index 0000000..44f5064 Binary files /dev/null and b/exp/luxRomImageMaker/Debug/luxRomImageMaker.exe differ diff --git a/exp/luxRomImageMaker/Debug/main.d b/exp/luxRomImageMaker/Debug/main.d new file mode 100644 index 0000000..a80ee2e --- /dev/null +++ b/exp/luxRomImageMaker/Debug/main.d @@ -0,0 +1,3 @@ +main.d main.o: ../main.cpp ../app.h + +../app.h: diff --git a/exp/luxRomImageMaker/Debug/main.o b/exp/luxRomImageMaker/Debug/main.o new file mode 100644 index 0000000..c1301d5 Binary files /dev/null and b/exp/luxRomImageMaker/Debug/main.o differ diff --git a/exp/luxRomImageMaker/Debug/makefile b/exp/luxRomImageMaker/Debug/makefile new file mode 100644 index 0000000..3099943 --- /dev/null +++ b/exp/luxRomImageMaker/Debug/makefile @@ -0,0 +1,58 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: luxRomImageMaker.exe + +# Tool invocations +luxRomImageMaker.exe: $(OBJS) $(USER_OBJS) + @echo 'Building target: $@' + @echo 'Invoking: Cygwin C++ Linker' + g++ -o "luxRomImageMaker.exe" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) luxRomImageMaker.exe + -@echo ' ' + +.PHONY: all clean dependents +.SECONDARY: + +-include ../makefile.targets diff --git a/exp/luxRomImageMaker/Debug/objects.mk b/exp/luxRomImageMaker/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/exp/luxRomImageMaker/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/exp/luxRomImageMaker/Debug/romdisk.d b/exp/luxRomImageMaker/Debug/romdisk.d new file mode 100644 index 0000000..e5a51f6 --- /dev/null +++ b/exp/luxRomImageMaker/Debug/romdisk.d @@ -0,0 +1,3 @@ +romdisk.d romdisk.o: ../romdisk.cpp ../app.h + +../app.h: diff --git a/exp/luxRomImageMaker/Debug/romdisk.o b/exp/luxRomImageMaker/Debug/romdisk.o new file mode 100644 index 0000000..daf17af Binary files /dev/null and b/exp/luxRomImageMaker/Debug/romdisk.o differ diff --git a/exp/luxRomImageMaker/Debug/sources.mk b/exp/luxRomImageMaker/Debug/sources.mk new file mode 100644 index 0000000..c1514fd --- /dev/null +++ b/exp/luxRomImageMaker/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +O_SRCS := +CPP_SRCS := +C_UPPER_SRCS := +C_SRCS := +S_UPPER_SRCS := +OBJ_SRCS := +ASM_SRCS := +CXX_SRCS := +C++_SRCS := +CC_SRCS := +C++_DEPS := +OBJS := +C_DEPS := +CC_DEPS := +CPP_DEPS := +EXECUTABLES := +CXX_DEPS := +C_UPPER_DEPS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/exp/luxRomImageMaker/Debug/subdir.mk b/exp/luxRomImageMaker/Debug/subdir.mk new file mode 100644 index 0000000..272c01d --- /dev/null +++ b/exp/luxRomImageMaker/Debug/subdir.mk @@ -0,0 +1,30 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../commands.cpp \ +../main.cpp \ +../romdisk.cpp + +OBJS += \ +./commands.o \ +./main.o \ +./romdisk.o + +CPP_DEPS += \ +./commands.d \ +./main.d \ +./romdisk.d + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp + @echo 'Building file: $<' + @echo 'Invoking: Cygwin C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/exp/luxRomImageMaker/PROJEC~1 b/exp/luxRomImageMaker/PROJEC~1 new file mode 100644 index 0000000..751985f --- /dev/null +++ b/exp/luxRomImageMaker/PROJEC~1 @@ -0,0 +1,83 @@ + + + luxRomImageMaker + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.buildLocation + ${workspace_loc:/luxRomImageMaker/Debug} + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/exp/luxRomImageMaker/app.h b/exp/luxRomImageMaker/app.h new file mode 100644 index 0000000..2bbe2b2 --- /dev/null +++ b/exp/luxRomImageMaker/app.h @@ -0,0 +1,71 @@ +/* + * app.h + * + * Created on: Aug 31, 2011 + * Author: Tiberiu + */ + +#ifndef APP_H_ +#define APP_H_ +#include +using namespace std; + +extern int cmd_create (char** argv, int argc); +extern int cmd_close (char** argv, int argc); +extern int cmd_mkdir (char** argv, int argc); +extern int cmd_cd (char** argv, int argc); +extern int cmd_add (char** argv, int argc); +extern int cmd_setflags (char** argv, int argc); + + +struct Node { + char Name[256]; + unsigned Flags; + + vector Children; + Node* Parent; +}; + +struct DirectoryEntry { + char Name[256]; + unsigned Flags, OwnerId, GroupId, Size; + struct { unsigned Date; unsigned Time; } TimeCreated, TimeModified, TimeAccessed; + unsigned Offset; +}; + + +class RomDisk +{ +private: + char OutputFile[256]; + unsigned FlagsDefault; + + Node Root; + Node* Current; + + int IndexOf (char* s); + +public: + RomDisk(); + void SetOutFile (char* s); + void SetFlags (unsigned n); + int ChangeCurrentDirectory (char* n); + void AddFile (char* name); + void AddDirectory (char* name); + +// WRITE TO FILE +private: + void *buffer; + FILE* file; + + void prepareBuffer(); + void writeToBuffer(void*, unsigned); + void writeNode (Node* n); + long fileSize (char* file); + long nodeSize (Node* n); + +public: + void Close(); +}; + +#endif /* APP_H_ */ diff --git a/exp/luxRomImageMaker/commands.cpp b/exp/luxRomImageMaker/commands.cpp new file mode 100644 index 0000000..5833ef8 --- /dev/null +++ b/exp/luxRomImageMaker/commands.cpp @@ -0,0 +1,97 @@ +/* + * commands.cpp + * + * Created on: Aug 31, 2011 + * Author: Tiberiu + */ + +#include +#include "app.h" + +RomDisk* disk = NULL; + +char* remove_quotes (char* str) +{ + if (*str == '"') str = str + 1; + + int temp = strlen (str); + if (str[temp-1] == '"') str[temp-1] = 0; + + return str; +} + +int cmd_create (char** argv, int argc) +{ + // Check number of parameters + if (argc < 2) return 0; + + // Create a new disk + disk = new RomDisk(); + + // Set path + argv[1] = remove_quotes(argv[1]); + disk->SetOutFile(argv[1]); + + printf("Created ramdisk '%s'.", argv[1]); + return 1; +} + +int cmd_close (char** argv, int argc) +{ + if (!disk) fprintf(stderr, "Error: no romdisk opened!"); + + disk->Close(); + delete disk; + + printf("Closed ramdisk '%s'.", argv[1]); + return 1; +} + +int cmd_mkdir (char** argv, int argc) +{ + if (!disk) fprintf(stderr, "Error: no romdisk opened!"); + if (argc < 2) return 0; + + argv[1] = remove_quotes(argv[1]); + disk->AddDirectory(argv[1]); + + printf("Created directory '%s'.", argv[1]); + return 1; +} + +int cmd_cd (char** argv, int argc) +{ + if (!disk) fprintf(stderr, "Error: no romdisk opened!"); + if (argc < 2) return 0; + + argv[1] = remove_quotes(argv[1]); + if (disk->ChangeCurrentDirectory(argv[1]) == -1) fprintf(stderr, "Error: invalid path."); + + printf("Changed directory to '%s'.", argv[1]); + return 1; +} + +int cmd_add (char** argv, int argc) +{ + if (!disk) fprintf(stderr, "Error: no romdisk opened!"); + if (argc < 2) return 0; + + argv[1] = remove_quotes(argv[1]); + disk->AddFile(argv[1]); + + printf("Added file '%s'.", argv[1]); + return 1; +} + +int cmd_setflags (char** argv, int argc) +{ + if (!disk) fprintf(stderr, "Error: no romdisk opened!"); + if (argc < 2) return 0; + + argv[1] = remove_quotes(argv[1]); + unsigned flags = strtoul(argv[1], 0, 16); + disk->SetFlags(flags); + + printf("Changed flags to 0x%x.", flags); + return 1; +} diff --git a/exp/luxRomImageMaker/luxRomImageMaker.exe.stackdump b/exp/luxRomImageMaker/luxRomImageMaker.exe.stackdump new file mode 100644 index 0000000..342fd2a --- /dev/null +++ b/exp/luxRomImageMaker/luxRomImageMaker.exe.stackdump @@ -0,0 +1,13 @@ +Exception: STATUS_ACCESS_VIOLATION at eip=61113783 +eax=00001000 ebx=00000000 ecx=FFFFFFFF edx=00001000 esi=00000000 edi=00001000 +ebp=0022B4D8 esp=0022B4D4 program=C:\Users\Tiberiu\workspace\osdev\luxRomImageMaker\Debug\luxRomImageMaker.exe, pid 6932, thread main +cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023 +Stack trace: +Frame Function Args +0022B4D8 61113783 (00001000, 0022BC0A, 00402020, 00000001) +0022BC28 6111B2F4 (0022D000, 611663A0, 00402020, 0022BC58) +0022BC48 6112CF00 (00402020, 00001000, 0000000D, 00401800) +0022BC98 610C3545 (0022BCD0, 00001000, 61166330, 00401212) +0022CCD8 00401247 (00000001, 00A08608, 00A08508, 00008000) +0022CD68 61007038 (00000000, 0022CDA4, 61006980, 7FFD9000) +End of stack trace diff --git a/exp/luxRomImageMaker/main.cpp b/exp/luxRomImageMaker/main.cpp new file mode 100644 index 0000000..a7d4152 --- /dev/null +++ b/exp/luxRomImageMaker/main.cpp @@ -0,0 +1,97 @@ +/* + * main.cpp + * + * Created on: Aug 31, 2011 + * Author: Tiberiu + */ + + +#include +#include +#include + +#include "app.h" +using namespace std; + +void GetParams (char* buffer, char* params[4], int& count) +{ + bool inside_quotes = false; + int len = strlen(buffer); + count = 0; + + if (!isspace(buffer[0])) params[count++] = buffer; + else buffer[0] = 0; + + for (int i = 1; i < len && count < 4; i++) + { + if (buffer[i] == '"') { + inside_quotes = (inside_quotes) ? false : true; + if (inside_quotes && buffer[i-1] == 0) params[count++] = &buffer[i]; + ++i; + } + if (inside_quotes) continue; + + // Comment + if (buffer[i] == '#') { + buffer[i] = 0; + break; + } + + if (!isspace(buffer[i]) && (buffer[i-1]==0)) + params[count++] = &buffer[i]; + + else if (isspace(buffer[i])) buffer[i] = 0; + } +} + +void ProcessLine (char* buffer, int lineno) +{ + char* argv[4]; int argc, res; + GetParams(buffer, argv, argc); + + printf("%i: ", lineno); + + if (strcmp(argv[0], "CREATE") == 0) res = cmd_create(argv, argc); + else if (strcmp(argv[0], "CLOSE") == 0) res = cmd_close(argv, argc); + else if (strcmp(argv[0], "MKDIR") == 0) res = cmd_mkdir(argv, argc); + else if (strcmp(argv[0], "CD") == 0) res = cmd_cd(argv, argc); + else if (strcmp(argv[0], "ADD") == 0) res = cmd_add(argv, argc); + else if (strcmp(argv[0], "SETFLAGS") == 0) res = cmd_setflags(argv, argc); + else if (strcmp(argv[0], "" )) return; + + else printf("Invalid command %s, ignored.", argv[0]); + + if (!res) { + printf("Syntax error!"); + exit(1); + } + + printf("\n"); +} + + +int main(int argc, char **argv) { + + char buffer[0x1000]; + int lineno = 0; + + // Make sure we have an argument + if (argc < 2) { + fprintf(stderr, "Missing argument - script file."); + return 0; + } + + // Try to open file + FILE* input = fopen(argv[1], "r"); + if (!input) { perror(argv[1]); return 0; } + + // Read file line by line + printf("Reading script...\n"); + while ( fgets(buffer, 0x1000, input) ) + ProcessLine(buffer, ++lineno); + + // Cleanup + fclose(input); + return 0; +} + diff --git a/exp/luxRomImageMaker/romdisk.cpp b/exp/luxRomImageMaker/romdisk.cpp new file mode 100644 index 0000000..57e2155 --- /dev/null +++ b/exp/luxRomImageMaker/romdisk.cpp @@ -0,0 +1,165 @@ +/* + * romdisk.cpp + * + * Created on: Aug 31, 2011 + * Author: Tiberiu + */ + +#include +#include + +#include "app.h" +using namespace std; + +RomDisk::RomDisk() +{ + FlagsDefault = 0xB68; + Current = &Root; + + Root.Flags = FlagsDefault; + Root.Parent = &Root; +} + +int RomDisk::IndexOf(char* s) +{ + for (int i = 0; i < (int)Current->Children.size(); i++) + if (strcmp(s, Current->Children[i].Name) == 0) return i; + + return -1; +} + +void RomDisk::SetOutFile (char* s) +{ + strcpy(OutputFile, s); +} + +void RomDisk::SetFlags (unsigned n) +{ + FlagsDefault = n; +} + +int RomDisk::ChangeCurrentDirectory (char* n) +{ + int len = strlen(n); + if (!len) return -1; + + // See if we need to go to root + if (n[0] == '\\') + { + Current = &Root; + n = n+1; + } + + // Process every folder in path + while (char* next = strchr(n, '\\')) + { + // mark next entry to parse + *next = 0; next = next+1; + + // previous dir? + if (strcmp(n, "..") == 0) Current = Current->Parent; + + else { + // Find next node + int index = IndexOf(n); + if (index == -1 && strcmp(n, ".") != 0) return -1; // Invalid path + + // Set as current + Current = &Current->Children[index]; + } + + n = next; + } + + return 0; +} + +void RomDisk::AddFile (char* name) +{ + Node f; + strcpy(f.Name, name); + f.Flags = (FlagsDefault & 0x497) | 1; // File + f.Parent = Current; + + Current->Children.push_back(f); +} + +void RomDisk::AddDirectory (char* name) +{ + Node dir; + strcpy(dir.Name, name); + dir.Flags = (FlagsDefault & 0x497) | 2; // Directory + dir.Parent = Current; + + Current->Children.push_back(dir); +} + +void RomDisk::prepareBuffer() +{ + buffer = malloc(0x10000); + + file = fopen(OutputFile, "w"); + if (!file) { + perror(OutputFile); + return; + } +} + + +#define MIN(a,b) ((a > b) ? (b) : (a)) + +void RomDisk::writeToBuffer(void* data, unsigned size) +{ + while (size) + { + unsigned qty = MIN (size, 0x10000); + memcpy(buffer, data, qty); + size -= qty; + + fwrite(buffer, sizeof(unsigned char), qty, file); + } +} + +long RomDisk::fileSize(char* fn) +{ + FILE* f = fopen(fn, "r"); + if (!f) { + fprintf(stderr, "Invalid file: %s.\n", fn); + exit (1); + } + + fseek(f, 0, SEEK_END); + long size = ftell(f); + fclose(f); + + return size; +} + +long RomDisk::nodeSize(Node* n) +{ + if (n->Flags & 1) return fileSize(n->Name); + + return (n->Children.size() * (sizeof(DirectoryEntry))); +} + +void RomDisk::writeNode(Node* n) +{ + +} + +void RomDisk::Close() +{ + // Init + prepareBuffer(); + + // Write ramdisk header (magic number) + unsigned MAGIC = 0x51042A0D; + writeToBuffer(&MAGIC, sizeof(unsigned)); + + // Write root directory entry + writeNode(&Root); + + // Cleanup + free(buffer); + fclose(file); +}