changeset 99:2a0aa3efc228

The shell script will now theroretically load the program into memory, will not run it. The shell script loads a program into memory and displays the address but as of now does not jump to it (though it does display the address it would jump to) Added a memset function Added a python script (credit for its writing to Jeff) that sets up a filesystem
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sat, 14 May 2011 19:40:18 -0400
parents 28c230f0700e
children d8f21e4a75e3
files Makefile include/string.h scripts/linkerProg.script src/shell.c src/string.c utility/fsGen.py
diffstat 6 files changed, 201 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat May 14 17:12:12 2011 -0400
+++ b/Makefile	Sat May 14 19:40:18 2011 -0400
@@ -16,7 +16,7 @@
 sarpn_OBJS=src/sarpn.o src/std.o src/string.o src/stack.o src/operations.o src/math.o arch/arch.a
 dynamic_OBJS=src/dynamic.o src/std.o src/string.o src/stack.o arch/arch.a
 testFS_OBJS=src/testFS.o src/std.o src/string.o src/fs.o arch/arch.a
-shell_OBJS=src/shell.o src/std.o src/fs.o src/string.o arch/arch.a
+shell_OBJS=src/shell.o src/std.o src/fs.o src/string.o src/stdio.o arch/arch.a
 
 ARCH_OBJS=arch/io.o arch/cons.o arch/ebcdic.o arch/fba.o arch/ioint.o \
 	  arch/svc.o arch/svcint.o
@@ -104,7 +104,8 @@
 src/operations.o: include/stack.h include/string.h include/math.h
 src/sarpn.o: include/std.h include/die.h include/string.h
 src/sarpn.o: include/operations.h include/stack.h
-src/shell.o: include/std.h include/die.h include/error.h include/fs.h
+src/shell.o: include/std.h include/die.h include/string.h include/stdio.h
+src/shell.o: include/error.h include/fs.h include/elf.h
 src/stack.o: include/stack.h include/std.h include/die.h
 src/std.o: include/std.h include/die.h include/string.h
 src/stdio.o: include/stdio.h include/std.h include/die.h include/string.h
--- a/include/string.h	Sat May 14 17:12:12 2011 -0400
+++ b/include/string.h	Sat May 14 19:40:18 2011 -0400
@@ -4,7 +4,8 @@
 void strcpy(char *dest, const char *src);
 int strcmp(const char *a, const char *b);
 char* strcat(char *dest, const char *src);
-void* memcpy(void *destination, const void *source, size_t num);
+void* memcpy(void *dest, const void *src, size_t num);
+void* memset(void *dest, u8 value, size_t num);
 size_t strlen(const char *str);
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linkerProg.script	Sat May 14 19:40:18 2011 -0400
@@ -0,0 +1,8 @@
+SECTIONS
+{
+  ENTRY(start)
+  . = 0x400000;
+  .text : { *(.text) }
+  .data : { *(.data) }
+  .bss : { *(.bss) }
+}
--- a/src/shell.c	Sat May 14 17:12:12 2011 -0400
+++ b/src/shell.c	Sat May 14 19:40:18 2011 -0400
@@ -1,24 +1,50 @@
 #include <std.h>
+#include <string.h>
+#include <stdio.h>
 #include <error.h>
 #include <fs.h>
 #include <die.h>
-
-#define LIST 0
-#define RUN 1
+#include <elf.h>
 
 void start(u64 __memsize)
 {
 	init_all(__memsize);
 	if(isError(init_fs(0x100, __memsize))) die();
 	char buffer[1024];
+	ErrCode err;
 	while(1) {
-		sPrint("> ");
+		sPrint("$ ");
 		sGet(buffer, 78);
-		u32 progID;
-		if(isError(lookupFile(buffer, &progID))) {
-			sPrint("ERROR: command not found\n");
+		u32 fid;
+		if(isError(err = lookupFile(buffer, &fid))) {
+			if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n");
+			continue;
 		} else {
-			sPrint("THE COMMAND WAS FOUND!!!  YAY!!!\n");
+			u32 fileSize;
+			if(isError(getFileSize(fid, &fileSize)));
+			void *data = malloc(fileSize);
+			if(!data) continue;
+			if(isError(getFileData(fid, data))) goto loopCont;
+			Elf64_Ehdr *eHdr = data;
+			if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 ||
+					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3)
+				goto loopCont;
+			if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont;
+			int i;
+			for(i = 0; i < eHdr->e_phnum; i++) {
+				Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i);
+				if(pHdr->p_type == PT_LOAD) {
+					void *fromLoc = data + pHdr->p_offset;
+					void *toLoc = (void*)pHdr->p_vaddr;
+					memcpy(toLoc, fromLoc, pHdr->p_filesz);
+					size_t diff = pHdr->p_memsz - pHdr->p_filesz;
+					if(diff) memset(toLoc + pHdr->p_filesz, 0, diff);
+				}
+			}
+			sprintf(buffer, "Going to memory address %i\n", eHdr->e_entry);
+			sPrint(buffer);
+		loopCont:
+			free(data);
 		}
 	}
 
--- a/src/string.c	Sat May 14 17:12:12 2011 -0400
+++ b/src/string.c	Sat May 14 19:40:18 2011 -0400
@@ -22,19 +22,29 @@
 	return ret;
 }
 
-void* memcpy(void *destination, const void *source, size_t num)
+void* memcpy(void *dest, const void *src, size_t num)
 {
-	int i;
-	const u8 *from = source;
-	u8 *to = destination;
+	size_t i;
+	const u8 *from = src;
+	u8 *to = dest;
 	for(i = 0; i < num; i++)
 		to[i] = from[i];
-	return destination;
+	return dest;
+}
+
+void* memset(void *dest, u8 value, size_t num)
+{
+	size_t i;
+	u8 *ptr = dest;
+	for(i = 0; i < num; i++, ptr++) {
+		*ptr = value;
+	}
+	return dest;
 }
 
 size_t strlen(const char *str)
 {
-	char *ptr = str;
+	const char *ptr = str;
 	while(*ptr && *++ptr);
 	return ptr - str;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utility/fsGen.py	Sat May 14 19:40:18 2011 -0400
@@ -0,0 +1,138 @@
+#!/usr/bin/env python
+
+import sys
+import math
+import random
+
+DEV_SIZE   = 1024
+BLOCK_SIZE = 1024
+datafiles = sys.argv[1:]
+blocks = {}
+
+SB_MAGIC = 0x42420374
+
+def __word(i, l, s):
+	assert (i < (1<<s))
+	ret = ""
+	for x in l:
+		ret = chr(i % 256) + ret
+		i /= 256
+	return ret
+
+def hword(i):
+	return __word(i, (0,1), 16)
+
+def word(i):
+	return __word(i, (0,1,2,3), 32)
+
+def dword(i):
+	return __word(i, (0,1,2,3,4,5,6,7), 64)
+
+def tod():
+	return 0
+
+def pad_block(s=""):
+	assert (len(s) <= BLOCK_SIZE)
+	return s + ("\0" * (BLOCK_SIZE-len(s)))
+
+def use_block(x):
+	# update alloc map
+	blk = 2+(x/(BLOCK_SIZE*8))
+	idx = (x/8)%BLOCK_SIZE
+	bit = x%8
+
+	d = blocks[blk]
+	a = d[idx]
+	b = chr(ord(a) | (0x80 >> bit))
+		
+	blocks[blk] = d[0:idx] + b + d[(idx+1):BLOCK_SIZE]
+
+	assert (a != b)
+
+lastb = 16
+def rand_block():
+	while True:
+		# random
+		x = random.randint(0, DEV_SIZE-1)
+
+		# or sequential
+		#global lastb
+		#x = lastb
+		#lastb += 1
+
+		if x in blocks.keys():
+			continue
+
+		blocks[x] = pad_block()
+
+		use_block(x)
+
+		return x
+
+def make_ipl_head():
+	# allocation map
+	for x in range(2,2+int(math.ceil(DEV_SIZE/(BLOCK_SIZE*8.0)))):
+		blocks[x] = pad_block()
+		use_block(x)
+
+	blocks[0] = pad_block() # ipl record
+	blocks[1] = pad_block() # sb record
+
+	use_block(0)
+	use_block(1)
+
+def make_sb(rootinode):
+	blocks[1] = pad_block(word(SB_MAGIC) + word(rootinode) + word(DEV_SIZE))
+
+def make_inode(l, blist):
+	i = word(l) + word(0) + dword(tod()) + dword(tod()) + hword(len(blist)) + hword(0) + word(0)
+
+	for b in blist:
+		i += word(b)
+	
+	t = rand_block()
+	blocks[t] = pad_block(i)
+	return t
+
+make_ipl_head()
+d = ""
+for fn in datafiles:
+	x = file(fn).read()
+	fb = []
+	l = len(x)
+
+	while len(x) > 0:
+		if len(x) < BLOCK_SIZE:
+			y = pad_block(x)
+			x = ""
+		elif len(x) == BLOCK_SIZE:
+			y = x
+			x = ""
+		else:
+			y = x[0:BLOCK_SIZE]
+			x = x[BLOCK_SIZE:]
+
+		t = rand_block()
+		fb.append(t)
+		blocks[t] = y
+
+	i = make_inode(l, fb)
+	d += (fn + (" " * (28 - len(fn)))) + word(i)
+
+db = rand_block()
+blocks[db] = pad_block(d)
+ri = make_inode(len(d), [db,])
+make_sb(ri)
+
+#for k in range(0, DEV_SIZE):
+#	if k not in blocks:
+#		print k, "<empty>"
+#	else:
+#		assert (len(blocks[k]) == BLOCK_SIZE)
+#		print k, repr(blocks[k])
+for k in range(0, DEV_SIZE):
+	if k not in blocks:
+		sys.stdout.write(pad_block())
+	else:
+		assert (len(blocks[k]) == BLOCK_SIZE)
+		sys.stdout.write(blocks[k])