Index: head/stand/common/bootstrap.h =================================================================== --- head/stand/common/bootstrap.h (revision 332557) +++ head/stand/common/bootstrap.h (revision 332558) @@ -1,337 +1,337 @@ /*- * Copyright (c) 1998 Michael Smith * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. * * $FreeBSD$ */ #ifndef _BOOTSTRAP_H_ #define _BOOTSTRAP_H_ #include #include #include /* Commands and return values; nonzero return sets command_errmsg != NULL */ typedef int (bootblk_cmd_t)(int argc, char *argv[]); #define COMMAND_ERRBUFSZ (256) -extern char *command_errmsg; +extern const char *command_errmsg; extern char command_errbuf[COMMAND_ERRBUFSZ]; #define CMD_OK 0 #define CMD_WARN 1 #define CMD_ERROR 2 #define CMD_CRIT 3 #define CMD_FATAL 4 /* interp.c */ void interact(void); void interp_emit_prompt(void); int interp_builtin_cmd(int argc, char *argv[]); /* Called by interp.c for interp_*.c embedded interpreters */ int interp_include(const char *filename); /* Execute commands from filename */ void interp_init(void); /* Initialize interpreater */ int interp_run(const char *line); /* Run a single command */ /* interp_backslash.c */ char *backslash(const char *str); /* interp_parse.c */ int parse(int *argc, char ***argv, const char *str); /* boot.c */ int autoboot(int timeout, char *prompt); void autoboot_maybe(void); int getrootmount(char *rootdev); /* misc.c */ char *unargv(int argc, char *argv[]); void hexdump(caddr_t region, size_t len); size_t strlenout(vm_offset_t str); char *strdupout(vm_offset_t str); void kern_bzero(vm_offset_t dest, size_t len); int kern_pread(int fd, vm_offset_t dest, size_t len, off_t off); void *alloc_pread(int fd, off_t off, size_t len); /* bcache.c */ void bcache_init(size_t nblks, size_t bsize); void bcache_add_dev(int); void *bcache_allocate(void); void bcache_free(void *); int bcache_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize); /* * Disk block cache */ struct bcache_devdata { int (*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize); void *dv_devdata; void *dv_cache; }; /* * Modular console support. */ struct console { const char *c_name; const char *c_desc; int c_flags; #define C_PRESENTIN (1<<0) /* console can provide input */ #define C_PRESENTOUT (1<<1) /* console can provide output */ #define C_ACTIVEIN (1<<2) /* user wants input from console */ #define C_ACTIVEOUT (1<<3) /* user wants output to console */ #define C_WIDEOUT (1<<4) /* c_out routine groks wide chars */ void (* c_probe)(struct console *cp); /* set c_flags to match hardware */ int (* c_init)(int arg); /* reinit XXX may need more args */ void (* c_out)(int c); /* emit c */ int (* c_in)(void); /* wait for and return input */ int (* c_ready)(void); /* return nonzer if input waiting */ }; extern struct console *consoles[]; void cons_probe(void); /* * Plug-and-play enumerator/configurator interface. */ struct pnphandler { const char *pp_name; /* handler/bus name */ void (* pp_enumerate)(void); /* enumerate PnP devices, add to chain */ }; struct pnpident { char *id_ident; /* ASCII identifier, actual format varies with bus/handler */ STAILQ_ENTRY(pnpident) id_link; }; struct pnpinfo { char *pi_desc; /* ASCII description, optional */ int pi_revision; /* optional revision (or -1) if not supported */ char *pi_module; /* module/args nominated to handle device */ int pi_argc; /* module arguments */ char **pi_argv; struct pnphandler *pi_handler; /* handler which detected this device */ STAILQ_HEAD(,pnpident) pi_ident; /* list of identifiers */ STAILQ_ENTRY(pnpinfo) pi_link; }; STAILQ_HEAD(pnpinfo_stql, pnpinfo); extern struct pnphandler *pnphandlers[]; /* provided by MD code */ void pnp_addident(struct pnpinfo *pi, char *ident); struct pnpinfo *pnp_allocinfo(void); void pnp_freeinfo(struct pnpinfo *pi); void pnp_addinfo(struct pnpinfo *pi); char *pnp_eisaformat(uint8_t *data); /* * < 0 - No ISA in system * == 0 - Maybe ISA, search for read data port * > 0 - ISA in system, value is read data port address */ extern int isapnp_readport; /* * Preloaded file metadata header. * * Metadata are allocated on our heap, and copied into kernel space * before executing the kernel. */ struct file_metadata { size_t md_size; uint16_t md_type; struct file_metadata *md_next; char md_data[1]; /* data are immediately appended */ }; struct preloaded_file; struct mod_depend; struct kernel_module { char *m_name; /* module name */ int m_version; /* module version */ /* char *m_args;*/ /* arguments for the module */ struct preloaded_file *m_fp; struct kernel_module *m_next; }; /* * Preloaded file information. Depending on type, file can contain * additional units called 'modules'. * * At least one file (the kernel) must be loaded in order to boot. * The kernel is always loaded first. * * String fields (m_name, m_type) should be dynamically allocated. */ struct preloaded_file { char *f_name; /* file name */ char *f_type; /* verbose file type, eg 'ELF kernel', 'pnptable', etc. */ char *f_args; /* arguments for the file */ struct file_metadata *f_metadata; /* metadata that will be placed in the module directory */ int f_loader; /* index of the loader that read the file */ vm_offset_t f_addr; /* load address */ size_t f_size; /* file size */ struct kernel_module *f_modules; /* list of modules if any */ struct preloaded_file *f_next; /* next file */ }; struct file_format { /* Load function must return EFTYPE if it can't handle the module supplied */ int (* l_load)(char *filename, uint64_t dest, struct preloaded_file **result); /* Only a loader that will load a kernel (first module) should have an exec handler */ int (* l_exec)(struct preloaded_file *mp); }; extern struct file_format *file_formats[]; /* supplied by consumer */ extern struct preloaded_file *preloaded_files; int mod_load(char *name, struct mod_depend *verinfo, int argc, char *argv[]); int mod_loadkld(const char *name, int argc, char *argv[]); void unload(void); struct preloaded_file *file_alloc(void); struct preloaded_file *file_findfile(const char *name, const char *type); struct file_metadata *file_findmetadata(struct preloaded_file *fp, int type); struct preloaded_file *file_loadraw(const char *name, char *type, int insert); void file_discard(struct preloaded_file *fp); void file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p); int file_addmodule(struct preloaded_file *fp, char *modname, int version, struct kernel_module **newmp); void file_removemetadata(struct preloaded_file *fp); /* MI module loaders */ #ifdef __elfN /* Relocation types. */ #define ELF_RELOC_REL 1 #define ELF_RELOC_RELA 2 /* Relocation offset for some architectures */ extern uint64_t __elfN(relocation_offset); struct elf_file; typedef Elf_Addr (symaddr_fn)(struct elf_file *ef, Elf_Size symidx); int __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result); int __elfN(obj_loadfile)(char *filename, uint64_t dest, struct preloaded_file **result); int __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata, int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len); int __elfN(loadfile_raw)(char *filename, uint64_t dest, struct preloaded_file **result, int multiboot); int __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest); #endif /* * Support for commands */ struct bootblk_command { const char *c_name; const char *c_desc; bootblk_cmd_t *c_fn; }; #define COMMAND_SET(tag, key, desc, func) \ static bootblk_cmd_t func; \ static struct bootblk_command _cmd_ ## tag = { key, desc, func }; \ DATA_SET(Xcommand_set, _cmd_ ## tag) SET_DECLARE(Xcommand_set, struct bootblk_command); /* * The intention of the architecture switch is to provide a convenient * encapsulation of the interface between the bootstrap MI and MD code. * MD code may selectively populate the switch at runtime based on the * actual configuration of the target system. */ struct arch_switch { /* Automatically load modules as required by detected hardware */ int (*arch_autoload)(void); /* Locate the device for (name), return pointer to tail in (*path) */ int (*arch_getdev)(void **dev, const char *name, const char **path); /* Copy from local address space to module address space, similar to bcopy() */ ssize_t (*arch_copyin)(const void *src, vm_offset_t dest, const size_t len); /* Copy to local address space from module address space, similar to bcopy() */ ssize_t (*arch_copyout)(const vm_offset_t src, void *dest, const size_t len); /* Read from file to module address space, same semantics as read() */ ssize_t (*arch_readin)(const int fd, vm_offset_t dest, const size_t len); /* Perform ISA byte port I/O (only for systems with ISA) */ int (*arch_isainb)(int port); void (*arch_isaoutb)(int port, int value); /* * Interface to adjust the load address according to the "object" * being loaded. */ uint64_t (*arch_loadaddr)(u_int type, void *data, uint64_t addr); #define LOAD_ELF 1 /* data points to the ELF header. */ #define LOAD_RAW 2 /* data points to the file name. */ /* * Interface to inform MD code about a loaded (ELF) segment. This * can be used to flush caches and/or set up translations. */ #ifdef __elfN void (*arch_loadseg)(Elf_Ehdr *eh, Elf_Phdr *ph, uint64_t delta); #else void (*arch_loadseg)(void *eh, void *ph, uint64_t delta); #endif /* Probe ZFS pool(s), if needed. */ void (*arch_zfs_probe)(void); /* For kexec-type loaders, get ksegment structure */ void (*arch_kexec_kseg_get)(int *nseg, void **kseg); }; extern struct arch_switch archsw; /* This must be provided by the MD code, but should it be in the archsw? */ void delay(int delay); void dev_cleanup(void); time_t time(time_t *tloc); #ifndef CTASSERT #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") #endif #endif /* !_BOOTSTRAP_H_ */ Index: head/stand/common/commands.c =================================================================== --- head/stand/common/commands.c (revision 332557) +++ head/stand/common/commands.c (revision 332558) @@ -1,512 +1,512 @@ /*- * Copyright (c) 1998 Michael Smith * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 "bootstrap.h" -char *command_errmsg; +const char *command_errmsg; /* XXX should have procedural interface for setting, size limit? */ char command_errbuf[COMMAND_ERRBUFSZ]; static int page_file(char *filename); /* * Help is read from a formatted text file. * * Entries in the file are formatted as # Ttopic [Ssubtopic] Ddescription help text here # * * Note that for code simplicity's sake, the above format must be followed * exactly. * * Subtopic entries must immediately follow the topic (this is used to * produce the listing of subtopics). * * If no argument(s) are supplied by the user, the help for 'help' is displayed. */ COMMAND_SET(help, "help", "detailed help", command_help); static int help_getnext(int fd, char **topic, char **subtopic, char **desc) { char line[81], *cp, *ep; /* Make sure we provide sane values. */ *topic = *subtopic = *desc = NULL; for (;;) { if (fgetstr(line, 80, fd) < 0) return(0); if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' ')) continue; *topic = *subtopic = *desc = NULL; cp = line + 2; while((cp != NULL) && (*cp != 0)) { ep = strchr(cp, ' '); if ((*cp == 'T') && (*topic == NULL)) { if (ep != NULL) *ep++ = 0; *topic = strdup(cp + 1); } else if ((*cp == 'S') && (*subtopic == NULL)) { if (ep != NULL) *ep++ = 0; *subtopic = strdup(cp + 1); } else if (*cp == 'D') { *desc = strdup(cp + 1); ep = NULL; } cp = ep; } if (*topic == NULL) { free(*subtopic); free(*desc); continue; } return(1); } } static int help_emitsummary(char *topic, char *subtopic, char *desc) { int i; pager_output(" "); pager_output(topic); i = strlen(topic); if (subtopic != NULL) { pager_output(" "); pager_output(subtopic); i += strlen(subtopic) + 1; } if (desc != NULL) { do { pager_output(" "); } while (i++ < 30); pager_output(desc); } return (pager_output("\n")); } static int command_help(int argc, char *argv[]) { char buf[81]; /* XXX buffer size? */ int hfd, matched, doindex; char *topic, *subtopic, *t, *s, *d; /* page the help text from our load path */ snprintf(buf, sizeof(buf), "%s/boot/loader.help", getenv("loaddev")); if ((hfd = open(buf, O_RDONLY)) < 0) { printf("Verbose help not available, use '?' to list commands\n"); return(CMD_OK); } /* pick up request from arguments */ topic = subtopic = NULL; switch(argc) { case 3: subtopic = strdup(argv[2]); case 2: topic = strdup(argv[1]); break; case 1: topic = strdup("help"); break; default: command_errmsg = "usage is 'help []"; close(hfd); return(CMD_ERROR); } /* magic "index" keyword */ doindex = !strcmp(topic, "index"); matched = doindex; /* Scan the helpfile looking for help matching the request */ pager_open(); while(help_getnext(hfd, &t, &s, &d)) { if (doindex) { /* dink around formatting */ if (help_emitsummary(t, s, d)) break; } else if (strcmp(topic, t)) { /* topic mismatch */ if (matched) /* nothing more on this topic, stop scanning */ break; } else { /* topic matched */ matched = 1; if (((subtopic == NULL) && (s == NULL)) || ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) { /* exact match, print text */ while ((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) { if (pager_output(buf)) break; if (pager_output("\n")) break; } } else if ((subtopic == NULL) && (s != NULL)) { /* topic match, list subtopics */ if (help_emitsummary(t, s, d)) break; } } free(t); free(s); free(d); t = s = d = NULL; } free(t); free(s); free(d); pager_close(); close(hfd); if (!matched) { snprintf(command_errbuf, sizeof(command_errbuf), "no help available for '%s'", topic); free(topic); free(subtopic); return(CMD_ERROR); } free(topic); free(subtopic); return(CMD_OK); } COMMAND_SET(commandlist, "?", "list commands", command_commandlist); /* * Please note: although we use the pager for the list of commands, * this routine is called from the ? FORTH function which then * unconditionally prints some commands. This will lead to anomalous * behavior. There's no 'pager_output' binding to FORTH to allow * things to work right, so I'm documenting the bug rather than * fixing it. */ static int command_commandlist(int argc, char *argv[]) { struct bootblk_command **cmdp; int res; char name[20]; res = 0; pager_open(); res = pager_output("Available commands:\n"); SET_FOREACH(cmdp, Xcommand_set) { if (res) break; if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) { snprintf(name, sizeof(name), " %-15s ", (*cmdp)->c_name); pager_output(name); pager_output((*cmdp)->c_desc); res = pager_output("\n"); } } pager_close(); return(CMD_OK); } /* * XXX set/show should become set/echo if we have variable * substitution happening. */ COMMAND_SET(show, "show", "show variable(s)", command_show); static int command_show(int argc, char *argv[]) { struct env_var *ev; char *cp; if (argc < 2) { /* * With no arguments, print everything. */ pager_open(); for (ev = environ; ev != NULL; ev = ev->ev_next) { pager_output(ev->ev_name); cp = getenv(ev->ev_name); if (cp != NULL) { pager_output("="); pager_output(cp); } if (pager_output("\n")) break; } pager_close(); } else { if ((cp = getenv(argv[1])) != NULL) { printf("%s\n", cp); } else { snprintf(command_errbuf, sizeof(command_errbuf), "variable '%s' not found", argv[1]); return(CMD_ERROR); } } return(CMD_OK); } COMMAND_SET(set, "set", "set a variable", command_set); static int command_set(int argc, char *argv[]) { int err; if (argc != 2) { command_errmsg = "wrong number of arguments"; return(CMD_ERROR); } else { if ((err = putenv(argv[1])) != 0) { command_errmsg = strerror(err); return(CMD_ERROR); } } return(CMD_OK); } COMMAND_SET(unset, "unset", "unset a variable", command_unset); static int command_unset(int argc, char *argv[]) { int err; if (argc != 2) { command_errmsg = "wrong number of arguments"; return(CMD_ERROR); } else { if ((err = unsetenv(argv[1])) != 0) { command_errmsg = strerror(err); return(CMD_ERROR); } } return(CMD_OK); } COMMAND_SET(echo, "echo", "echo arguments", command_echo); static int command_echo(int argc, char *argv[]) { char *s; int nl, ch; nl = 0; optind = 1; optreset = 1; while ((ch = getopt(argc, argv, "n")) != -1) { switch(ch) { case 'n': nl = 1; break; case '?': default: /* getopt has already reported an error */ return(CMD_OK); } } argv += (optind); argc -= (optind); s = unargv(argc, argv); if (s != NULL) { printf("%s", s); free(s); } if (!nl) printf("\n"); return(CMD_OK); } /* * A passable emulation of the sh(1) command of the same name. */ COMMAND_SET(read, "read", "read input from the terminal", command_read); static int command_read(int argc, char *argv[]) { char *prompt; int timeout; time_t when; char *cp; char *name; char buf[256]; /* XXX size? */ int c; timeout = -1; prompt = NULL; optind = 1; optreset = 1; while ((c = getopt(argc, argv, "p:t:")) != -1) { switch(c) { case 'p': prompt = optarg; break; case 't': timeout = strtol(optarg, &cp, 0); if (cp == optarg) { snprintf(command_errbuf, sizeof(command_errbuf), "bad timeout '%s'", optarg); return(CMD_ERROR); } break; default: return(CMD_OK); } } argv += (optind); argc -= (optind); name = (argc > 0) ? argv[0]: NULL; if (prompt != NULL) printf("%s", prompt); if (timeout >= 0) { when = time(NULL) + timeout; while (!ischar()) if (time(NULL) >= when) return(CMD_OK); /* is timeout an error? */ } ngets(buf, sizeof(buf)); if (name != NULL) setenv(name, buf, 1); return(CMD_OK); } /* * File pager */ COMMAND_SET(more, "more", "show contents of a file", command_more); static int command_more(int argc, char *argv[]) { int i; int res; char line[80]; res=0; pager_open(); for (i = 1; (i < argc) && (res == 0); i++) { snprintf(line, sizeof(line), "*** FILE %s BEGIN ***\n", argv[i]); if (pager_output(line)) break; res = page_file(argv[i]); if (!res) { snprintf(line, sizeof(line), "*** FILE %s END ***\n", argv[i]); res = pager_output(line); } } pager_close(); if (res == 0) return CMD_OK; else return CMD_ERROR; } static int page_file(char *filename) { int result; result = pager_file(filename); if (result == -1) { snprintf(command_errbuf, sizeof(command_errbuf), "error showing %s", filename); } return result; } /* * List all disk-like devices */ COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev); static int command_lsdev(int argc, char *argv[]) { int verbose, ch, i; char line[80]; verbose = 0; optind = 1; optreset = 1; while ((ch = getopt(argc, argv, "v")) != -1) { switch(ch) { case 'v': verbose = 1; break; case '?': default: /* getopt has already reported an error */ return(CMD_OK); } } argv += (optind); argc -= (optind); pager_open(); for (i = 0; devsw[i] != NULL; i++) { if (devsw[i]->dv_print != NULL){ if (devsw[i]->dv_print(verbose)) break; } else { snprintf(line, sizeof(line), "%s: (unknown)\n", devsw[i]->dv_name); if (pager_output(line)) break; } } pager_close(); return(CMD_OK); }