ref: 8b24f795e2bb2a5d169641ed0fda0e99b69e4466
dir: /main.c/
#include "u.h"
#include "lib.h"
#include "kern/dat.h"
#include "kern/fns.h"
#include "user.h"
#include "drawcpu.h"
#include "args.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);
close(fd);
// We need to set our actual nvlen
if((fd = open(nvram, OREAD)) < 0)
panic("open %d: %r", 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);
}
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(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);
}