diff utility/fsGen.py @ 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
children d8f21e4a75e3
line wrap: on
line diff
--- /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])