view utility/fsGen.py @ 100:d8f21e4a75e3

Added a simple program that is just an infinite loop The heap now starts at 8MB I now try to set the context to the loaded program when it is called in the shell script
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 19 May 2011 09:40:10 -0400
parents 2a0aa3efc228
children
line wrap: on
line source

#!/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)
	foo, bar, fn = fn.rpartition('/')
	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])

# vim: set noexpandtab: