Index: sys/boot/common/Makefile.inc =================================================================== --- sys/boot/common/Makefile.inc +++ sys/boot/common/Makefile.inc @@ -2,7 +2,7 @@ SRCS+= boot.c commands.c console.c devopen.c interp.c SRCS+= interp_backslash.c interp_parse.c ls.c misc.c -SRCS+= module.c panic.c +SRCS+= module.c panic.c lskernel.c .if ${MACHINE} == "i386" || ${MACHINE_CPUARCH} == "amd64" SRCS+= load_elf32.c load_elf32_obj.c reloc_elf32.c Index: sys/boot/common/lskernel.c =================================================================== --- /dev/null +++ sys/boot/common/lskernel.c @@ -0,0 +1,187 @@ +/* + * $FreeBSD$ + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * Copyright (c) 1996 + * Matthias Drochner. All rights reserved. + * Copyright (c) 2017 Allan Jude . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include + +#include "bootstrap.h" + +static char typestr[] = "?fc?d?b? ?l?s?w"; + +static int ls_getdir(char **pathp); + +COMMAND_SET(lskernel, "lskernel", "list installed kernels", command_lskernel); + +static int +command_lskernel(int argc, char *argv[]) +{ + int fd; + struct stat sb; + struct dirent *d; + char *path; + char lbuf[1024], kbuf[1024] = { 0 }; + int result, ch; + int verbose, setkenv; + + result = CMD_OK; + fd = -1; + verbose = 0; + setkenv = 0; + optind = 1; + optreset = 1; + while ((ch = getopt(argc, argv, "ls")) != -1) { + switch(ch) { + case 'l': + verbose = 1; + break; + case 's': + setkenv = 1; + break; + case '?': + default: + /* getopt has already reported an error */ + return(CMD_OK); + } + } + argv += (optind - 1); + argc -= (optind - 1); + + if (argc < 2) { + path = "/boot"; + } else { + path = argv[1]; + } + + fd = ls_getdir(&path); + if (fd == -1) { + result = CMD_ERROR; + goto out; + } + + while ((d = readdirfd(fd)) != NULL) { + if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { + snprintf(lbuf, sizeof(lbuf), "%s/%s/kernel", path, d->d_name); + /* stat the file, if possible */ + sb.st_size = 0; + if (stat(lbuf, &sb) < 0) { + continue; + } + if (!S_ISREG(sb.st_mode)) { + continue; + } + if (setkenv) { + strlcat(kbuf, d->d_name, sizeof(kbuf)); + strlcat(kbuf, " ", sizeof(kbuf)); + } else if (verbose) { + printf(" %c %8d %s\n", typestr[d->d_type], + (int)sb.st_size, d->d_name); + } else { + printf("%s\n", d->d_name); + } + } + } + out: + if (fd != -1) + close(fd); + if (path != NULL) + free(path); + + if (kbuf[0] != '\0') { + setenv("kernels", (const char *)&kbuf, 1); + } + return(result); +} + +/* + * Given (path) containing a vaguely reasonable path specification, return an fd + * on the directory, and an allocated copy of the path to the directory. + */ +static int +ls_getdir(char **pathp) +{ + struct stat sb; + int fd; + const char *cp; + char *path, *tail; + + tail = NULL; + fd = -1; + + /* one extra byte for a possible trailing slash required */ + path = malloc(strlen(*pathp) + 2); + strcpy(path, *pathp); + + /* Make sure the path is respectable to begin with */ + if (archsw.arch_getdev(NULL, path, &cp)) { + sprintf(command_errbuf, "bad path '%s'", path); + goto out; + } + + /* If there's no path on the device, assume '/' */ + if (*cp == 0) + strcat(path, "/"); + + fd = open(path, O_RDONLY); + if (fd < 0) { + sprintf(command_errbuf, "open '%s' failed: %s", path, strerror(errno)); + goto out; + } + if (fstat(fd, &sb) < 0) { + sprintf(command_errbuf, "stat failed: %s", strerror(errno)); + goto out; + } + if (!S_ISDIR(sb.st_mode)) { + sprintf(command_errbuf, "%s: %s", path, strerror(ENOTDIR)); + goto out; + } + + *pathp = path; + return(fd); + + out: + free(path); + *pathp = NULL; + if (fd != -1) + close(fd); + return(-1); +} Index: sys/boot/forth/menu-commands.4th =================================================================== --- sys/boot/forth/menu-commands.4th +++ sys/boot/forth/menu-commands.4th @@ -388,6 +388,9 @@ s" /boot/defaults/loader.conf" read-conf s" /boot/loader.conf" read-conf s" /boot/loader.conf.local" read-conf + s" kernel_autolist" getenv? if + s" lskernel -s" evaluate + then init_bootenv be_draw_screen menu-redraw Index: sys/boot/forth/menu.rc =================================================================== --- sys/boot/forth/menu.rc +++ sys/boot/forth/menu.rc @@ -196,6 +196,14 @@ drop ( xt=0 ) [then] +s" kernels" getenv dup -1 = [if] + drop s" set kernel_autolist=YES" evaluate +[then] + +s" kernel_autolist" getenv? [if] + s" lskernel -s" evaluate +[then] + \ Display the main menu (see `menu.4th') set menuset_initial=1 menuset-loadinitial