shithub: drawcpu

ref: 746e0c9217ab9ee1884b721a955b712d59b13549
dir: /main.c/

View raw version
#include "u.h"
#include "lib.h"
#include "kern/dat.h"
#include "kern/fns.h"
#include "user.h"
#include "drawcpu.h"
#include "arg.h"
#include "proc.h"

char *argv0;
int debug;
char *ninepath;

void
sizebug(void)
{
	/*
	 * Needed by various parts of the code.
	 * This is a huge bug.
	 */
	assert(sizeof(char)==1);
	assert(sizeof(short)==2);
	assert(sizeof(ushort)==2);
	assert(sizeof(int)==4);
	assert(sizeof(uint)==4);
	assert(sizeof(long)==4);
	assert(sizeof(ulong)==4);
	assert(sizeof(vlong)==8);
	assert(sizeof(uvlong)==8);
}

// TODO: remove libgui, or at least revamp as cpubody goes away
void cpubody(void) {}

char*
estrdup(char *s)
{
	s = strdup(s);
	if(s == nil)
		sysfatal("out of memory");
    return s;
}


static void
usage(void)
{
	fprintf(stderr, "usage: drawcpu [-p <path> -d]\n");
	exit(1);
}

void
notehandler(void *d, char *note)
{
	USED(d);
	if(strncmp(note, "sys:", 4) == 0)
		return;
	
	if(strncmp(note, "emu:", 4) == 0)
		exits(note);

	addnote(note);
	return;
}

static void
envinit(char *nvram)
{
	int fd, nvrlen;

	/* We want to set up our env based on some details here 
	 * nvram, nvroff, nvrlen, cputype 
	 */
	if((fd = create("/env/nvram", ORDWR, 0666)) < 0)
		panic("open env/nvram: %r");
	fprint(fd, "%s", nvram);
	nvrlen = seek(fd, 0, SEEK_END);
	close(fd);

	if((fd = create("/env/nvroff", ORDWR, 0666)) < 0)
		panic("open env/nvroff: %r");
	fprint(fd, "0");
	close(fd);

	if((fd = create("/env/nvrlen", ORDWR, 0666)) < 0)
		panic("open env/nvrlen: %r");
	fprint(fd, "%d", nvrlen);
	close(fd);


	if((fd = create("/env/cputype", ORDWR, 0666)) < 0)
		panic("open env/cputype: %r");
	fprint(fd, "arm");
	close(fd);
}

void
dump(void)
{
	print("R00 %.8ux\tR01 %.8ux\tR02 %.8ux\tR03 %.8ux\tpid: %d\nR04 %.8ux\tR05 %.8ux\tR06 %.8ux\tR07 %.8ux\nR08 %.8ux\tR09 %.8ux\tR10 %.8ux\tR11 %.8ux\nR12 %.8ux\tR13 %.8ux\tR14 %.8ux\tR15 %.8ux\n", 
		up->R[0], up->R[1], up->R[2], up->R[3], up->pid,
		up->R[4], up->R[5], up->R[6], up->R[7], 
		up->R[8], up->R[9], up->R[10], up->R[11], 
		up->R[12], up->R[13], up->R[14], up->R[15]
	);
}

int
main(int argc, char **argv)
{
	extern ulong kerndate;
	char *file, *nvram;

	debug = 0;
	kerndate = seconds();
	eve = getuser();
	nvram = "/tmp/nvram";

	ARGBEGIN {
	case 'p':
		ninepath = EARGF(usage());
		break;
	case 'd':
		debug++;
		break;
	case 'n':
		nvram = EARGF(usage());
		break;
	case 'u':
		eve = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND;


	if(!ninepath)
		ninepath = getwd(ninepath, 256);

	sizebug();
	osinit();
	procinit0();
	printinit();

	chandevreset();
	chandevinit();
	quotefmtinstall();

	if(bind("#c", "/dev", MBEFORE) < 0)
		panic("bind #c: %r");
	if(bind("#e", "/env", MREPL|MCREATE) < 0)
		panic("bind #e: %r");
	if(bind("#L", "/fd", MREPL|MCREATE) < 0)
		panic("bind #L");
	if(bind("#I", "/net", MBEFORE) < 0)
		panic("bind #I: %r");
	if(bind("#U", "/root", MREPL|MCREATE) < 0)
		panic("bind #U: %r");
	if(bind("#p", "/proc", MBEFORE) < 0)
		panic("bind #p: %r");

	if(bind(smprint("/root/%s/arm", ninepath), "/arm", MREPL|MCREATE) < 0)
		panic("bind arm: %r");
	if(bind(smprint("/root/%s/rc", ninepath), "/rc", MREPL|MCREATE) < 0)
		panic("bind rc: %r");

	bind("#A", "/dev", MAFTER);
	bind("#N", "/dev", MAFTER);
	bind("#¤", "/dev", MAFTER);
	bind("#C", "/", MAFTER);

	if(bind("/root", "/", MAFTER) < 0)
		panic("bind /root: %r");

	/* This should work, but it doesn't */
	if(bind("/arm/bin", "/bin", MCREATE|MREPL) < 0 || bind("/rc/bin", "/bin", MAFTER) < 0)
		panic("bind bin: %r");

	if(**argv == '/' || **argv == '.' || **argv == '#') {
		if(loadtext(*argv, argc, argv) < 0)
			panic("loadtext: %r");
	} else {
		file = smprint("/bin/%s", *argv);
		if(loadtext(file, argc, argv) < 0)
			panic("loadtext: %r");
		free(file);
	}

	envinit(nvram);
	//if(authsrv){
	//	if((fd = create("")))
	//}

	if(open("/dev/cons", OREAD) != 0)
			panic("open0: %r");
	if(open("/dev/cons", OWRITE) != 1)
			panic("open1: %r");
	if(open("/dev/cons", OWRITE) != 2)
			panic("open2: %r");

	notify(notehandler);
	procrun(0);

	exits(0);
}