]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/boot/pc/sub.c
9boot: replace strrchr() call with a loop and strchr() for ignoring bang path prefixes
[plan9front.git] / sys / src / boot / pc / sub.c
index 0836d82c3d8dd5a8b1d186df2bc0fd54a5d5f190..470d31c39ed7e1a3f65db813e53c1573f0caa521 100644 (file)
@@ -4,9 +4,10 @@
 #include "mem.h"
 
 void
-memset(void *p, int v, int n)
+memset(void *dst, int v, int n)
 {
-       uchar *d = p;
+       uchar *d = dst;
+
        while(n > 0){
                *d++ = v;
                n--;
@@ -20,17 +21,13 @@ memmove(void *dst, void *src, int n)
        uchar *s = src;
 
        if(d < s){
-               while(n > 0){
+               while(n-- > 0)
                        *d++ = *s++;
-                       n--;
-               }
-       } if(d > s){
+       } else if(d > s){
                s += n;
                d += n;
-               while(n > 0){
+               while(n-- > 0)
                        *--d = *--s;
-                       n--;
-               }
        }
 }
 
@@ -40,8 +37,11 @@ memcmp(void *src, void *dst, int n)
        uchar *d = dst;
        uchar *s = src;
        int r = 0;
-       while((n > 0) && (r = (*d++ - *s++)) == 0)
-               n--;
+
+       while(n-- > 0)
+               if(r = (*d++ - *s++))
+                       break;
+
        return r;
 }
 
@@ -49,8 +49,10 @@ int
 strlen(char *s)
 {
        char *p = s;
+
        while(*p)
                p++;
+
        return p - s;
 }
 
@@ -60,17 +62,8 @@ strchr(char *s, int c)
        for(; *s; s++)
                if(*s == c)
                        return s;
-       return 0;
-}
 
-char*
-strrchr(char *s, int c)
-{
-       char *r;
-       r = 0;
-       while(s = strchr(s, c))
-               r = s++;
-       return r;
+       return 0;
 }
 
 void
@@ -98,6 +91,7 @@ readn(void *f, void *data, int len)
                p += len;
        }
        putc('\b');
+
        return p - (uchar*)data;
 }
 
@@ -132,6 +126,7 @@ readline(void *f, char buf[64])
                        p--;
        }while(p == buf);
        *p = 0;
+
        return p - buf;
 }
 
@@ -157,53 +152,100 @@ char *confend;
 static void apmconf(int);
 static void e820conf(void);
 
+static int
+delconf(char *s)
+{
+       char *p, *e;
+
+       for(p = BOOTARGS; p < confend; p = e){
+               for(e = p+1; e < confend; e++){
+                       if(*e == '\n'){
+                               e++;
+                               break;
+                       }
+               }
+               if(!memcmp(p, s, strlen(s))){
+                       memmove(p, e, confend - e);
+                       confend -= e - p;
+                       *confend = 0;
+                       return 1;
+               }
+       }
+       return 0;
+}
+
 char*
 configure(void *f, char *path)
 {
-       char line[64], *kern, *p;
-       int inblock, n;
+       char line[64], *kern, *s, *p;
+       int inblock, nowait, n;
+
 Clear:
        kern = 0;
+       nowait = 1;
        inblock = 0;
 
        memset(BOOTLINE, 0, BOOTLINELEN);
 
        confend = BOOTARGS;
        memset(confend, 0, BOOTARGSLEN);
+
+       e820conf();
 Loop:
-       while((n = readline(f, line)) > 0){
+       while(readline(f, line) > 0){
                if(*line == 0 || strchr("#;=", *line))
                        continue;
                if(*line == '['){
                        inblock = memcmp("[common]", line, 8);
                        continue;
                }
-               if(!memcmp("clear", line, 6)){
-                       print("ok\r\n");
-                       goto Clear;
-               }
-               if(!memcmp("boot", line, 5))
+               if(!memcmp("boot", line, 5)){
+                       nowait=1;
                        break;
-               if(inblock || !strrchr(line, '='))
+               }
+               if(!memcmp("wait", line, 5)){
+                       nowait=0;
+                       continue;
+               }
+               if(!memcmp("clear", line, 5)){
+                       if(line[5] == 0){
+                               print("ok");
+                               print(crnl);
+                               goto Clear;
+                       } else if(line[5] == ' ' && delconf(line+6)){
+                               print("ok");
+                               print(crnl);
+                       }
                        continue;
-               if(!memcmp("bootfile=", line, 9))
-                       memmove(kern = path, line+9, 1 + n-9);
-               if(!memcmp("apm", line, 3) && line[4]=='='){
+               }
+               if(inblock || (p = strchr(line, '=')) == nil)
+                       continue;
+               *p++ = 0;
+               delconf(line);
+               if(!memcmp("apm", line, 3)){
                        apmconf('0' - line[3]);
                        continue;
                }
-               memmove(confend, line, n); confend += n;
+               if(!memcmp("bootfile", line, 8))
+                       memmove(kern = path, p, strlen(p)+1);
+
+               s = confend;
+               memmove(confend, line, n = strlen(line)); confend += n;
+               *confend++ = '=';
+               memmove(confend, p, n = strlen(p)); confend += n;
+               *confend = 0;
+
+               print(s); print(crnl);
+
                *confend++ = '\n';
-               print(line); print(crnl);
+               *confend = 0;
        }
-       e820conf();
-       *confend = 0;
 
        if(f){
                close(f);
                f = 0;
 
-               if(kern && timeout(500))
+               if(kern && (nowait==0 || timeout(1000)))
                        goto Loop;
        }
 
@@ -211,31 +253,13 @@ Loop:
                print("no bootfile\r\n");
                goto Loop;
        }
-       if(p = strrchr(kern, '!'))
+       while(p = strchr(kern, '!'))
                kern = p+1;
 
        return kern;
 }
 
 
-static ushort
-beswab(ushort s)
-{
-       uchar *p;
-
-       p = (uchar*)&s;
-       return (p[0]<<8) | p[1];
-}
-
-static ulong
-beswal(ulong l)
-{
-       uchar *p;
-
-       p = (uchar*)&l;
-       return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
-}
-
 static void
 hexfmt(char *s, int i, ulong a)
 {
@@ -287,6 +311,7 @@ apmconf(int id)
        print(s); print(crnl);
 
        *confend++ = '\n';
+       *confend = 0;
 }
 
 ulong e820(ulong bx, void *p);
@@ -304,32 +329,55 @@ e820conf(void)
        ulong bx;
        char *s;
 
-       memset(&e, 0, sizeof(e));
-       if((bx = e820(0, &e)) == 0)
-               return;
-
+       bx=0;
        s = confend;
-       memmove(confend, "e820=", 5);
-       confend += 5;
 
        do{
-               if(e.typ == 1 && (e.ext & 1) == 0 && e.len){
+               bx = e820(bx, &e);
+               if(e.typ == 1 && e.len != 0 && (e.ext & 3) == 1){
+                       if(confend == s){
+                               /* single entry <= 1MB is useless */
+                               if(bx == 0 && e.len <= 0x100000)
+                                       break;
+                               memmove(confend, "e820=", 5);
+                               confend += 5;
+                       }
                        v = e.base;
                        addconfx("", 8, v>>32);
                        addconfx("", 8, v&0xffffffff);
-                       v = e.base + e.len;
+                       v += e.len;
                        addconfx(" ", 8, v>>32);
                        addconfx("", 8, v&0xffffffff);
                        *confend++ = ' ';
                }
+       } while(bx);
 
-               memset(&e, 0, sizeof(e));
-       } while(bx = e820(bx, &e));
+       if(confend == s)
+               return;
 
        *confend = 0;
        print(s); print(crnl);
 
        *confend++ = '\n';
+       *confend = 0;
+}
+
+static ushort
+beswab(ushort s)
+{
+       uchar *p;
+
+       p = (uchar*)&s;
+       return (p[0]<<8) | p[1];
+}
+
+static ulong
+beswal(ulong l)
+{
+       uchar *p;
+
+       p = (uchar*)&l;
+       return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 }
 
 void a20(void);
@@ -341,9 +389,6 @@ bootkern(void *f)
        ulong n;
        Exec ex;
 
-       print("boot");
-       print(crnl);
-
        a20();
 
        if(readn(f, &ex, sizeof(ex)) != sizeof(ex))
@@ -365,7 +410,7 @@ bootkern(void *f)
        close(f);
        unload();
 
-       print("go!");
+       print("boot");
        print(crnl);
 
        jump(e);