From 288d581e559a0492fb49ed16c365b11afd84c584 Mon Sep 17 00:00:00 2001 From: jhb Date: Mon, 5 Mar 2012 19:53:17 +0000 Subject: Fix boot2 to handle boot config files that only contain a custom path to a loader or kernel. Specifically, kname cannot be pointed at cmd[] since it's value is change to be an empty string after the initial call to parse, and cmd[]'s value can be changed (thus losing a prior setting for kname) due to user input at the boot prompt. While here, ensure that that initial boot config file text is nul-terminated, that ops is initialized to zero, and that kname is always initialized to a valid string. Tested by: Domagoj Smolcic rank1seeker of gmail MFC after: 1 week --- sys/boot/i386/boot2/boot2.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'sys/boot') diff --git a/sys/boot/i386/boot2/boot2.c b/sys/boot/i386/boot2/boot2.c index 3603924..8291249 100644 --- a/sys/boot/i386/boot2/boot2.c +++ b/sys/boot/i386/boot2/boot2.c @@ -128,7 +128,7 @@ static struct dsk { unsigned start; int init; } dsk; -static char cmd[512], cmddup[512]; +static char cmd[512], cmddup[512], knamebuf[1024]; static const char *kname; static uint32_t opts; static int comspeed = SIOSPD; @@ -223,7 +223,9 @@ main(void) { uint8_t autoboot; ino_t ino; + size_t nbyte; + opts = 0; kname = NULL; dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; @@ -240,8 +242,10 @@ main(void) autoboot = 1; if ((ino = lookup(PATH_CONFIG)) || - (ino = lookup(PATH_DOTCONFIG))) - fsread(ino, cmd, sizeof(cmd)); + (ino = lookup(PATH_DOTCONFIG))) { + nbyte = fsread(ino, cmd, sizeof(cmd) - 1); + cmd[nbyte] = '\0'; + } if (*cmd) { memcpy(cmddup, cmd, sizeof(cmd)); @@ -258,9 +262,9 @@ main(void) * or in case of failure, try to load a kernel directly instead. */ - if (autoboot && !kname) { + if (!kname) { kname = PATH_BOOT3; - if (!keyhit(3*SECOND)) { + if (autoboot && !keyhit(3*SECOND)) { load(); kname = PATH_KERNEL; } @@ -457,7 +461,12 @@ parse() ? DRV_HARD : 0) + drv; dsk_meta = 0; } - kname = arg; + if ((i = ep - arg)) { + if ((size_t)i >= sizeof(knamebuf)) + return -1; + memcpy(knamebuf, arg, i + 1); + kname = knamebuf; + } } arg = p; } -- cgit v1.1