changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: bcm: move CONFADDR parsing into bootargs.c, simplify initcode start() args handling

changeset 7159: 66132ebbe687
parent 7158: 46cc885b933b
child 7160: fbb7d37047fc
author: cinap_lenrek@felloff.net
date: Thu, 11 Apr 2019 19:10:47 +0200
files: sys/src/9/bcm/bootargs.c sys/src/9/bcm/fns.h sys/src/9/bcm/main.c sys/src/9/bcm/mkfile
description: bcm: move CONFADDR parsing into bootargs.c, simplify initcode start() args handling
     1.1new file mode 100644
     1.2--- /dev/null
     1.3+++ b/sys/src/9/bcm/bootargs.c
     1.4@@ -0,0 +1,169 @@
     1.5+#include	"u.h"
     1.6+#include	"../port/lib.h"
     1.7+#include	"mem.h"
     1.8+#include	"dat.h"
     1.9+#include	"fns.h"
    1.10+
    1.11+#define BOOTARGS	((char*)CONFADDR)
    1.12+#define BOOTARGSLEN	((KZERO+REBOOTADDR)-CONFADDR)
    1.13+
    1.14+#define	MAXCONF 64
    1.15+static char *confname[MAXCONF];
    1.16+static char *confval[MAXCONF];
    1.17+static int nconf;
    1.18+
    1.19+typedef struct Atag Atag;
    1.20+struct Atag {
    1.21+	u32int	size;	/* size of atag in words, including this header */
    1.22+	u32int	tag;	/* atag type */
    1.23+	union {
    1.24+		u32int	data[1];	/* actually [size-2] */
    1.25+		/* AtagMem */
    1.26+		struct {
    1.27+			u32int	size;
    1.28+			u32int	base;
    1.29+		} mem;
    1.30+		/* AtagCmdLine */
    1.31+		char	cmdline[1];	/* actually [4*(size-2)] */
    1.32+	};
    1.33+};
    1.34+
    1.35+enum {
    1.36+	AtagNone	= 0x00000000,
    1.37+	AtagCore	= 0x54410001,
    1.38+	AtagMem		= 0x54410002,
    1.39+	AtagCmdline	= 0x54410009,
    1.40+};
    1.41+
    1.42+static int
    1.43+findconf(char *k)
    1.44+{
    1.45+	int i;
    1.46+
    1.47+	for(i = 0; i < nconf; i++)
    1.48+		if(cistrcmp(confname[i], k) == 0)
    1.49+			return i;
    1.50+	return -1;
    1.51+}
    1.52+
    1.53+static void
    1.54+addconf(char *k, char *v)
    1.55+{
    1.56+	int i;
    1.57+
    1.58+	i = findconf(k);
    1.59+	if(i < 0){
    1.60+		if(nconf >= MAXCONF)
    1.61+			return;
    1.62+		i = nconf++;
    1.63+		confname[i] = k;
    1.64+	}
    1.65+	confval[i] = v;
    1.66+}
    1.67+
    1.68+static void
    1.69+plan9iniinit(char *s, int cmdline)
    1.70+{
    1.71+	char *toks[MAXCONF];
    1.72+	int i, c, n;
    1.73+	char *v;
    1.74+
    1.75+	if((c = *s) < ' ' || c >= 0x80)
    1.76+		return;
    1.77+	if(cmdline)
    1.78+		n = tokenize(s, toks, MAXCONF);
    1.79+	else
    1.80+		n = getfields(s, toks, MAXCONF, 1, "\n");
    1.81+	for(i = 0; i < n; i++){
    1.82+		if(toks[i][0] == '#')
    1.83+			continue;
    1.84+		v = strchr(toks[i], '=');
    1.85+		if(v == nil)
    1.86+			continue;
    1.87+		*v++ = '\0';
    1.88+		addconf(toks[i], v);
    1.89+	}
    1.90+}
    1.91+
    1.92+void
    1.93+bootargsinit(void)
    1.94+{
    1.95+	Atag *a;
    1.96+	int n;
    1.97+
    1.98+	a = (Atag*)BOOTARGS;
    1.99+	if(a->tag != AtagCore){
   1.100+		plan9iniinit((char*)a, 0);
   1.101+		return;
   1.102+	}
   1.103+	while(a->tag != AtagNone && a->size != 0){
   1.104+		switch(a->tag){
   1.105+		case AtagMem:
   1.106+			/* use only first bank */
   1.107+			if(conf.mem[0].limit == 0 && a->mem.size != 0){
   1.108+				memsize = a->mem.size;
   1.109+				conf.mem[0].base = a->mem.base;
   1.110+				conf.mem[0].limit = a->mem.base + memsize;
   1.111+			}
   1.112+			break;
   1.113+		case AtagCmdline:
   1.114+			n = (a->size * sizeof(u32int)) - offsetof(Atag, cmdline[0]);
   1.115+			if(a->cmdline + n < BOOTARGS + BOOTARGSLEN)
   1.116+				a->cmdline[n] = 0;
   1.117+			else
   1.118+				BOOTARGS[BOOTARGSLEN-1] = 0;
   1.119+			plan9iniinit(a->cmdline, 1);
   1.120+			break;
   1.121+		}
   1.122+		a = (Atag*)((u32int*)a + a->size);
   1.123+	}
   1.124+}
   1.125+
   1.126+char*
   1.127+getconf(char *name)
   1.128+{
   1.129+	int i;
   1.130+
   1.131+	if((i = findconf(name)) < 0)
   1.132+		return nil;
   1.133+	return confval[i];
   1.134+}
   1.135+
   1.136+void
   1.137+setconfenv(void)
   1.138+{
   1.139+	int i;
   1.140+
   1.141+	for(i = 0; i < nconf; i++){
   1.142+		if(confname[i][0] != '*')
   1.143+			ksetenv(confname[i], confval[i], 0);
   1.144+		ksetenv(confname[i], confval[i], 1);
   1.145+	}
   1.146+}
   1.147+
   1.148+void
   1.149+writeconf(void)
   1.150+{
   1.151+	char *p, *q;
   1.152+	int n;
   1.153+
   1.154+	p = getconfenv();
   1.155+	if(waserror()) {
   1.156+		free(p);
   1.157+		nexterror();
   1.158+	}
   1.159+
   1.160+	/* convert to name=value\n format */
   1.161+	for(q=p; *q; q++) {
   1.162+		q += strlen(q);
   1.163+		*q = '=';
   1.164+		q += strlen(q);
   1.165+		*q = '\n';
   1.166+	}
   1.167+	n = q - p + 1;
   1.168+	if(n >= BOOTARGSLEN)
   1.169+		error("kernel configuration too large");
   1.170+	memmove(BOOTARGS, p, n);
   1.171+	poperror();
   1.172+	free(p);
   1.173+}
     2.1--- a/sys/src/9/bcm/fns.h
     2.2+++ b/sys/src/9/bcm/fns.h
     2.3@@ -5,6 +5,7 @@ Dirtab*	addarchfile(char*, int, long(*)(
     2.4 extern void archreboot(void);
     2.5 extern void archreset(void);
     2.6 extern void armtimerset(int);
     2.7+extern void bootargsinit(void);
     2.8 extern void cachedwbinv(void);
     2.9 extern void cachedwbse(void*, int);
    2.10 extern void cachedwbinvse(void*, int);
    2.11@@ -77,6 +78,7 @@ extern void procfork(Proc*);
    2.12 extern void procsetup(Proc*);
    2.13 extern void screeninit(void);
    2.14 extern void setclkrate(int, ulong);
    2.15+extern void setconfenv(void);
    2.16 extern void setpower(int, int);
    2.17 extern void setr13(int, u32int*);
    2.18 extern int startcpus(uint);
    2.19@@ -93,6 +95,7 @@ extern void vgpset(uint, int);
    2.20 extern void vtable(void);
    2.21 extern void wdogoff(void);
    2.22 extern void wdogfeed(void);
    2.23+extern void writeconf(void);
    2.24 extern void vtable(void);
    2.25 extern int l2ap(int);
    2.26 extern uint getcputemp(void);
     3.1--- a/sys/src/9/bcm/main.c
     3.2+++ b/sys/src/9/bcm/main.c
     3.3@@ -15,180 +15,11 @@
     3.4 #define	Minfirmrev	326770
     3.5 #define	Minfirmdate	"22 Jul 2012"
     3.6 
     3.7-/*
     3.8- * Where configuration info is left for the loaded programme.
     3.9- */
    3.10-#define BOOTARGS	((char*)CONFADDR)
    3.11-#define	BOOTARGSLEN	(REBOOTADDR-PADDR(CONFADDR))
    3.12-#define	MAXCONF		64
    3.13-#define MAXCONFLINE	160
    3.14-
    3.15 uintptr kseg0 = KZERO;
    3.16 Mach*	machaddr[MAXMACH];
    3.17 Conf	conf;
    3.18 ulong	memsize = 128*1024*1024;
    3.19 
    3.20-/*
    3.21- * Option arguments from the command line.
    3.22- * oargv[0] is the boot file.
    3.23- */
    3.24-static int oargc;
    3.25-static char* oargv[20];
    3.26-static char oargb[128];
    3.27-static int oargblen;
    3.28-
    3.29-static uintptr sp;		/* XXX - must go - user stack of init proc */
    3.30-
    3.31-/* store plan9.ini contents here at least until we stash them in #ec */
    3.32-static char confname[MAXCONF][KNAMELEN];
    3.33-static char confval[MAXCONF][MAXCONFLINE];
    3.34-static int nconf;
    3.35-
    3.36-typedef struct Atag Atag;
    3.37-struct Atag {
    3.38-	u32int	size;	/* size of atag in words, including this header */
    3.39-	u32int	tag;	/* atag type */
    3.40-	union {
    3.41-		u32int	data[1];	/* actually [size-2] */
    3.42-		/* AtagMem */
    3.43-		struct {
    3.44-			u32int	size;
    3.45-			u32int	base;
    3.46-		} mem;
    3.47-		/* AtagCmdLine */
    3.48-		char	cmdline[1];	/* actually [4*(size-2)] */
    3.49-	};
    3.50-};
    3.51-
    3.52-enum {
    3.53-	AtagNone	= 0x00000000,
    3.54-	AtagCore	= 0x54410001,
    3.55-	AtagMem		= 0x54410002,
    3.56-	AtagCmdline	= 0x54410009,
    3.57-};
    3.58-
    3.59-static int
    3.60-findconf(char *name)
    3.61-{
    3.62-	int i;
    3.63-
    3.64-	for(i = 0; i < nconf; i++)
    3.65-		if(cistrcmp(confname[i], name) == 0)
    3.66-			return i;
    3.67-	return -1;
    3.68-}
    3.69-
    3.70-char*
    3.71-getconf(char *name)
    3.72-{
    3.73-	int i;
    3.74-
    3.75-	i = findconf(name);
    3.76-	if(i >= 0)
    3.77-		return confval[i];
    3.78-	return nil;
    3.79-}
    3.80-
    3.81-void
    3.82-addconf(char *name, char *val)
    3.83-{
    3.84-	int i;
    3.85-
    3.86-	i = findconf(name);
    3.87-	if(i < 0){
    3.88-		if(val == nil || nconf >= MAXCONF)
    3.89-			return;
    3.90-		i = nconf++;
    3.91-		strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
    3.92-	}
    3.93-	strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
    3.94-}
    3.95-
    3.96-static void
    3.97-writeconf(void)
    3.98-{
    3.99-	char *p, *q;
   3.100-	int n;
   3.101-
   3.102-	p = getconfenv();
   3.103-
   3.104-	if(waserror()) {
   3.105-		free(p);
   3.106-		nexterror();
   3.107-	}
   3.108-
   3.109-	/* convert to name=value\n format */
   3.110-	for(q=p; *q; q++) {
   3.111-		q += strlen(q);
   3.112-		*q = '=';
   3.113-		q += strlen(q);
   3.114-		*q = '\n';
   3.115-	}
   3.116-	n = q - p + 1;
   3.117-	if(n >= BOOTARGSLEN)
   3.118-		error("kernel configuration too large");
   3.119-	memmove(BOOTARGS, p, n);
   3.120-	memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
   3.121-	poperror();
   3.122-	free(p);
   3.123-}
   3.124-
   3.125-static void
   3.126-plan9iniinit(char *s, int cmdline)
   3.127-{
   3.128-	char *toks[MAXCONF];
   3.129-	int i, c, n;
   3.130-	char *v;
   3.131-
   3.132-	if((c = *s) < ' ' || c >= 0x80)
   3.133-		return;
   3.134-	if(cmdline)
   3.135-		n = tokenize(s, toks, MAXCONF);
   3.136-	else
   3.137-		n = getfields(s, toks, MAXCONF, 1, "\n");
   3.138-	for(i = 0; i < n; i++){
   3.139-		if(toks[i][0] == '#')
   3.140-			continue;
   3.141-		v = strchr(toks[i], '=');
   3.142-		if(v == nil)
   3.143-			continue;
   3.144-		*v++ = '\0';
   3.145-		addconf(toks[i], v);
   3.146-	}
   3.147-}
   3.148-
   3.149-static void
   3.150-ataginit(Atag *a)
   3.151-{
   3.152-	int n;
   3.153-
   3.154-	if(a->tag != AtagCore){
   3.155-		plan9iniinit((char*)a, 0);
   3.156-		return;
   3.157-	}
   3.158-	while(a->tag != AtagNone){
   3.159-		switch(a->tag){
   3.160-		case AtagMem:
   3.161-			/* use only first bank */
   3.162-			if(conf.mem[0].limit == 0 && a->mem.size != 0){
   3.163-				memsize = a->mem.size;
   3.164-				conf.mem[0].base = a->mem.base;
   3.165-				conf.mem[0].limit = a->mem.base + memsize;
   3.166-			}
   3.167-			break;
   3.168-		case AtagCmdline:
   3.169-			n = (a->size * sizeof(u32int)) - offsetof(Atag, cmdline[0]);
   3.170-			if(a->cmdline + n < BOOTARGS + BOOTARGSLEN)
   3.171-				a->cmdline[n] = 0;
   3.172-			else
   3.173-				BOOTARGS[BOOTARGSLEN-1] = 0;
   3.174-			plan9iniinit(a->cmdline, 1);
   3.175-			break;
   3.176-		}
   3.177-		a = (Atag*)((u32int*)a + a->size);
   3.178-	}
   3.179-}
   3.180-
   3.181 void
   3.182 machinit(void)
   3.183 {
   3.184@@ -245,16 +76,6 @@ launchinit(void)
   3.185 		print("only %d cpu%s started\n", mach, mach == 1? "" : "s");
   3.186 }
   3.187 
   3.188-static void
   3.189-optionsinit(char* s)
   3.190-{
   3.191-	strecpy(oargb, oargb+sizeof(oargb), s);
   3.192-
   3.193-	oargblen = strlen(oargb);
   3.194-	oargc = tokenize(oargb, oargv, nelem(oargv)-1);
   3.195-	oargv[oargc] = nil;
   3.196-}
   3.197-
   3.198 void
   3.199 main(void)
   3.200 {
   3.201@@ -264,11 +85,8 @@ main(void)
   3.202 	m = (Mach*)MACHADDR;
   3.203 	memset(edata, 0, end - edata);	/* clear bss */
   3.204 	mach0init();
   3.205-
   3.206-	optionsinit("/boot/boot boot");
   3.207 	quotefmtinstall();
   3.208-	
   3.209-	ataginit((Atag*)BOOTARGS);
   3.210+	bootargsinit();
   3.211 	confinit();		/* figures out amount of memory */
   3.212 	xinit();
   3.213 	uartconsinit();
   3.214@@ -312,8 +130,7 @@ main(void)
   3.215 void
   3.216 init0(void)
   3.217 {
   3.218-	int i;
   3.219-	char buf[2*KNAMELEN];
   3.220+	char buf[2*KNAMELEN], **sp;
   3.221 
   3.222 	up->nerrlab = 0;
   3.223 	coherence();
   3.224@@ -342,48 +159,17 @@ init0(void)
   3.225 		ksetenv("etherargs", buf, 0);
   3.226 
   3.227 		/* convert plan9.ini variables to #e and #ec */
   3.228-		for(i = 0; i < nconf; i++) {
   3.229-			ksetenv(confname[i], confval[i], 0);
   3.230-			ksetenv(confname[i], confval[i], 1);
   3.231-		}
   3.232+		setconfenv();
   3.233 		poperror();
   3.234 	}
   3.235 	kproc("alarm", alarmkproc, 0);
   3.236-	touser(sp);
   3.237-	assert(0);			/* shouldn't have returned */
   3.238-}
   3.239-
   3.240-static void
   3.241-bootargs(uintptr base)
   3.242-{
   3.243-	int i;
   3.244-	ulong ssize;
   3.245-	char **av, *p;
   3.246 
   3.247-	/*
   3.248-	 * Push the boot args onto the stack.
   3.249-	 * The initial value of the user stack must be such
   3.250-	 * that the total used is larger than the maximum size
   3.251-	 * of the argument list checked in syscall.
   3.252-	 */
   3.253-	i = oargblen+1;
   3.254-	p = (void*)STACKALIGN(base + BY2PG - sizeof(Tos) - i);
   3.255-	memmove(p, oargb, i);
   3.256+	sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
   3.257+	sp[3] = sp[2] = sp[1] = nil;
   3.258+	strcpy(sp[0] = (char*)&sp[4], "boot");
   3.259 
   3.260-	/*
   3.261-	 * Now push the argv pointers.
   3.262-	 * The code jumped to by touser in lproc.s expects arguments
   3.263-	 *	main(char* argv0, ...)
   3.264-	 * and calls
   3.265-	 * 	startboot("/boot/boot", &argv0)
   3.266-	 * not the usual (int argc, char* argv[])
   3.267-	 */
   3.268-	av = (char**)(p - (oargc+1)*sizeof(char*));
   3.269-	ssize = base + BY2PG - (uintptr)av;
   3.270-	for(i = 0; i < oargc; i++)
   3.271-		*av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
   3.272-	*av = nil;
   3.273-	sp = USTKTOP - ssize;
   3.274+	touser((uintptr)sp);
   3.275+	assert(0);			/* shouldn't have returned */
   3.276 }
   3.277 
   3.278 /*
   3.279@@ -433,7 +219,7 @@ userinit(void)
   3.280 	pg = newpage(1, 0, USTKTOP-BY2PG);
   3.281 	segpage(s, pg);
   3.282 	k = kmap(pg);
   3.283-	bootargs(VA(k));
   3.284+	memset((void*)VA(k), 0, BY2PG);
   3.285 	kunmap(k);
   3.286 
   3.287 	/*
     4.1--- a/sys/src/9/bcm/mkfile
     4.2+++ b/sys/src/9/bcm/mkfile
     4.3@@ -45,6 +45,7 @@ OBJ=\
     4.4 	lexception.$O\
     4.5 	lproc.$O\
     4.6 	arch.$O\
     4.7+	bootargs.$O\
     4.8 	clock.$O\
     4.9 	fpi.$O\
    4.10 	fpiarm.$O\