bootadm_hyper.c revision 772d6a58e8b646d197debb720cdea7723d5892d8
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <alloca.h>
2N/A#include <ctype.h>
2N/A#include <sys/types.h>
2N/A
2N/A#include "message.h"
2N/A#include "bootadm.h"
2N/A
2N/A#define HYPER_KERNEL_DIR "/platform/i86xpv/kernel"
2N/A#define METAL_KERNEL_DIR "/platform/i86pc/kernel"
2N/A
2N/A#define BOOTRC_FILE "/boot/solaris/bootenv.rc"
2N/A#define ZFS_BOOTSTR "$ZFS-BOOTFS"
2N/A
2N/A#define BFLAG "-B"
2N/A#define DEFAULT_SERIAL "9600,8,n,1"
2N/A
2N/A#define TTYXMODE_TO_COMNUM(ttyxmode) ((int)(*((ttyxmode) + 3) - '`'))
2N/A#define COMNAME_TO_COMNUM(comname) ((int)(*((comname) + 3) - '0'))
2N/A
2N/A#define WHITESPC(x) (x)
2N/A
2N/Astatic char *serial_config[2] = { NULL, NULL };
2N/Astatic char *console_dev = NULL;
2N/A
2N/Astatic char *bootenv_rc_serial[2] = { NULL, NULL };
2N/Astatic char *bootenv_rc_console = NULL;
2N/A
2N/Astatic unsigned zfs_boot = 0;
2N/A
2N/A/*
2N/A * Append the string pointed to by "str" to the string pointed to by "orig"
2N/A * adding the delimeter "delim" in between.
2N/A *
2N/A * Return a pointer to the new string or NULL, if we were passed a bad string.
2N/A */
2N/Astatic char *
2N/Aappend_str(char *orig, char *str, char *delim)
2N/A{
2N/A char *newstr;
2N/A int len;
2N/A
2N/A if ((str == NULL) || (delim == NULL))
2N/A return (NULL);
2N/A
2N/A if ((orig == NULL) || (*orig == NULL)) {
2N/A /*
2N/A * Return a pointer to a copy of the path so a caller can
2N/A * always rely upon being able to free() a returned pointer.
2N/A */
2N/A return (s_strdup(str));
2N/A }
2N/A
2N/A len = strlen(orig) + strlen(str) + strlen(delim) + 1;
2N/A if ((newstr = malloc(len)) == NULL) {
2N/A bam_error(NO_MEM, len);
2N/A bam_exit(1);
2N/A }
2N/A
2N/A (void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
2N/A return (newstr);
2N/A}
2N/A
2N/A/*
2N/A * Replace the substring "old_str" in a path with the substring "new_str"
2N/A *
2N/A * Return a pointer to the modified string.
2N/A */
2N/Astatic char *
2N/Amodify_path(char *path, char *old_str, char *new_str)
2N/A{
2N/A char *newpath;
2N/A char *pc;
2N/A int len;
2N/A
2N/A /*
2N/A * Return a pointer to a copy of the path so a caller can always rely
2N/A * upon being able to free() a returned pointer.
2N/A */
2N/A if ((pc = strstr(path, old_str)) == NULL)
2N/A return (s_strdup(path));
2N/A
2N/A /*
2N/A * Allocate space for duplicate of path with name changes and
2N/A * NULL terminating byte
2N/A */
2N/A len = strlen(path) - strlen(old_str) + strlen(new_str) + 1;
2N/A
2N/A if ((newpath = malloc(len)) == NULL) {
2N/A bam_error(NO_MEM, len);
2N/A bam_exit(1);
2N/A }
2N/A
2N/A (void) strlcpy(newpath, path, (pc - path) + 1);
2N/A pc += strlen(old_str);
2N/A
2N/A (void) strcat(newpath, new_str);
2N/A (void) strcat(newpath, pc);
2N/A return (newpath);
2N/A}
2N/A
2N/A/*
2N/A * Set "token" to be the the string starting from the pointer "str" delimited
2N/A * by any character in the string "delim" or the end of the string, but IGNORE
2N/A * any characters between single or double quotes.
2N/A *
2N/A * Return a pointer to the next non-whitespace character after the delimiter
2N/A * or NULL if we hit the end of the string. Also return NULL upon failure to
2N/A * find any characters from the delimeter string or upon failure to allocate
2N/A * memory for the new token string.
2N/A */
2N/Astatic char *
2N/Aget_token(char **token, char *str, char *delim)
2N/A{
2N/A char *dp;
2N/A char *start = str;
2N/A unsigned len;
2N/A
2N/A *token = NULL;
2N/A
2N/A if ((str == NULL) || (*str == NULL))
2N/A return (NULL);
2N/A
2N/A do {
2N/A if ((*str == '\'') || (*str == '"')) {
2N/A char quote = *str++;
2N/A
2N/A while ((*str != NULL) && (*str != quote))
2N/A str++;
2N/A
2N/A /* no matching quote found in string */
2N/A if (*str++ == NULL)
2N/A return (NULL);
2N/A }
2N/A
2N/A /* look for a character from the delimiter string */
2N/A for (dp = delim; ((*dp != NULL) && (*dp != *str)); dp++)
2N/A ;
2N/A
2N/A if (*dp != NULL) {
2N/A len = str - start + 1;
2N/A
2N/A /* found a delimiter, so create a token string */
2N/A if ((*token = malloc(len)) == NULL) {
2N/A bam_error(NO_MEM, len);
2N/A bam_exit(1);
2N/A }
2N/A
2N/A (void) strlcpy(*token, start, len);
2N/A
2N/A while (isspace((int)*++str))
2N/A ;
2N/A
2N/A return (str);
2N/A }
2N/A } while (*str++ != NULL);
2N/A
2N/A /* if we hit the end of the string, the token is the whole string */
2N/A *token = s_strdup(start);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Convert a metal "console" device name to an equivalent one suitable for
2N/A * use with the hypervisor.
2N/A *
2N/A * Default to "vga" if we can't parse the console device.
2N/A */
2N/Astatic void
2N/Aconsole_metal_to_hyper(char *console)
2N/A{
2N/A if ((*console == '\'') || (*console == '"'))
2N/A console++;
2N/A
2N/A if (strncmp(console, "ttya", 4) == 0)
2N/A console_dev = "console=com1";
2N/A else if (strncmp(console, "ttyb", 4) == 0)
2N/A console_dev = "console=com2";
2N/A else
2N/A console_dev = "console=vga";
2N/A}
2N/A
2N/Astatic int
2N/Aset_serial_rate(int com, char *rate)
2N/A{
2N/A char **rp = &serial_config[com - 1];
2N/A
2N/A if ((com < 1) || (com > 2))
2N/A return (-1);
2N/A
2N/A /*
2N/A * If rate is a NULL pointer, erase any existing serial configuration
2N/A * for this serial port.
2N/A */
2N/A if (rate == NULL) {
2N/A if (*rp != NULL) {
2N/A free(*rp);
2N/A *rp = NULL;
2N/A }
2N/A return (0);
2N/A }
2N/A
2N/A *rp = s_realloc(*rp, strlen(rate) + 1);
2N/A (void) strcpy(*rp, rate);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Convert "metal" serial port parameters to values compatible with the
2N/A * hypervisor.
2N/A *
2N/A * Return 0 on success, otherwise -1.
2N/A */
2N/Astatic int
2N/Aserial_metal_to_hyper(char *metal_port, char *metal_serial)
2N/A{
2N/A#define COM_RATE_LEN 16 /* strlen("com1=115200,8n1") */
2N/A
2N/A char com_rate[COM_RATE_LEN];
2N/A
2N/A unsigned com, baud, bits, stop;
2N/A char parity, handshake;
2N/A
2N/A if ((strcmp(metal_port, "ttya-mode") == 0) ||
2N/A (strcmp(metal_port, "ttyb-mode") == 0))
2N/A com = TTYXMODE_TO_COMNUM(metal_port);
2N/A else
2N/A return (-1);
2N/A
2N/A if ((*metal_serial == '\'') || (*metal_serial == '"'))
2N/A metal_serial++;
2N/A
2N/A /*
2N/A * Check if it's specified as the default rate; if so it defaults to
2N/A * "auto" and we need not set it for they hypervisor.
2N/A */
2N/A if (strncmp(metal_serial, DEFAULT_SERIAL,
2N/A strlen(DEFAULT_SERIAL)) == 0) {
2N/A (void) set_serial_rate(com, NULL);
2N/A return (0);
2N/A }
2N/A
2N/A /* read the serial port format as set forth in common/io/asy.c */
2N/A if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
2N/A &handshake) != 5)
2N/A return (-1);
2N/A
2N/A /* validate serial port parameters */
2N/A if (((bits < 5) || (bits > 8)) || (stop > 1) ||
2N/A ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
2N/A ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
2N/A return (-1);
2N/A
2N/A /* validate baud rate */
2N/A switch (baud) {
2N/A case 150:
2N/A case 300:
2N/A case 600:
2N/A case 1200:
2N/A case 2400:
2N/A case 4800:
2N/A case 9600:
2N/A case 19200:
2N/A case 38400:
2N/A case 57600:
2N/A case 115200:
2N/A break;
2N/A
2N/A default:
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * The hypervisor has no way to specify a handshake method, so it gets
2N/A * quietly dropped in the conversion.
2N/A */
2N/A (void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
2N/A bits, parity, stop);
2N/A (void) set_serial_rate(com, com_rate);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Convert "name=value" metal options to values suitable for use with the
2N/A * hypervisor.
2N/A *
2N/A * Our main concerns are the console device and serial port settings.
2N/A *
2N/A * Return values:
2N/A *
2N/A * -1: Unparseable line
2N/A * 0: Success
2N/A * (n > 0): A property unimportant to us
2N/A */
2N/Astatic int
2N/Acvt_metal_option(char *optstr)
2N/A{
2N/A char *value;
2N/A unsigned namlen;
2N/A
2N/A if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
2N/A zfs_boot = 1;
2N/A return (0);
2N/A }
2N/A
2N/A if ((value = strchr(optstr, '=')) == NULL)
2N/A return (-1);
2N/A
2N/A namlen = value - optstr;
2N/A
2N/A if (*++value == NULL)
2N/A return (1);
2N/A
2N/A if (strncmp(optstr, "console", namlen) == 0) {
2N/A console_metal_to_hyper(value);
2N/A return (0);
2N/A }
2N/A
2N/A if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
2N/A (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
2N/A char *port = alloca(namlen + 1);
2N/A
2N/A (void) strlcpy(port, optstr, namlen + 1);
2N/A return (serial_metal_to_hyper(port, value));
2N/A }
2N/A
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Convert "name=value" properties for use with a bare metal kernel
2N/A *
2N/A * Our main concerns are the console setting and serial port modes.
2N/A *
2N/A * Return values:
2N/A *
2N/A * -1: Unparseable line
2N/A * 0: Success
2N/A * (n > 0): A property unimportant to us
2N/A */
2N/Astatic int
2N/Acvt_hyper_option(char *optstr)
2N/A{
2N/A#define SER_LEN 15 /* strlen("115200,8,n,1,-") + 1 */
2N/A
2N/A char ser[SER_LEN];
2N/A char *value;
2N/A
2N/A unsigned namlen;
2N/A
2N/A unsigned baud;
2N/A char bits, parity, stop;
2N/A
2N/A if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
2N/A zfs_boot = 1;
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * If there's no "=" in the token, it's likely a standalone
2N/A * hypervisor token we don't care about (e.g. "noreboot" or
2N/A * "nosmp") so we ignore it.
2N/A */
2N/A if ((value = strchr(optstr, '=')) == NULL)
2N/A return (1);
2N/A
2N/A namlen = value - optstr;
2N/A
2N/A if (*++value == NULL)
2N/A return (1);
2N/A
2N/A /*
2N/A * Note that we use strncmp against the values because the
2N/A * hypervisor allows setting console parameters for both the
2N/A * console and debugger via the format:
2N/A *
2N/A * console=cons_dev,debug_dev
2N/A *
2N/A * and we only care about "cons_dev."
2N/A *
2N/A * This also allows us to extract "comN" from hypervisor constructs
2N/A * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
2N/A *
2N/A * Default the console device to "text" if it was "vga" or was
2N/A * unparseable.
2N/A */
2N/A if (strncmp(optstr, "console", namlen) == 0) {
2N/A /* ignore the "console=hypervisor" option */
2N/A if (strcmp(value, "hypervisor") == 0)
2N/A return (0);
2N/A
2N/A if (strncmp(value, "com1", 4) == 0)
2N/A console_dev = "ttya";
2N/A else if (strncmp(value, "com2", 4) == 0)
2N/A console_dev = "ttyb";
2N/A else
2N/A console_dev = "text";
2N/A }
2N/A
2N/A /* serial port parameter conversion */
2N/A
2N/A if ((strncmp(optstr, "com1", namlen) == 0) ||
2N/A (strncmp(optstr, "com2", namlen) == 0)) {
2N/A unsigned com = COMNAME_TO_COMNUM(optstr);
2N/A
2N/A /*
2N/A * Check if it's "auto" - if so, use the default setting
2N/A * of "9600,8,n,1,-".
2N/A *
2N/A * We can't just assume the serial port will default to
2N/A * "9600,8,n,1" as there could be a directive in bootenv.rc
2N/A * that would set it to some other value and we want the serial
2N/A * parameters to be the same as that used by the hypervisor.
2N/A */
2N/A if (strcmp(value, "auto") == 0) {
2N/A (void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
2N/A } else {
2N/A /*
2N/A * Extract the "B,PS" setting from the com line; ignore
2N/A * other settings like io_base or IRQ.
2N/A */
2N/A if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
2N/A &stop) != 4)
2N/A return (-1);
2N/A
2N/A /* validate serial port parameters */
2N/A if (((stop != '0') && (stop != '1')) ||
2N/A ((bits < '5') && (bits > '8')) ||
2N/A ((parity != 'n') && (parity != 'e') &&
2N/A (parity != 'o')))
2N/A return (-1);
2N/A
2N/A /* validate baud rate */
2N/A switch (baud) {
2N/A case 150:
2N/A case 300:
2N/A case 600:
2N/A case 1200:
2N/A case 2400:
2N/A case 4800:
2N/A case 19200:
2N/A case 38400:
2N/A case 57600:
2N/A case 115200:
2N/A break;
2N/A
2N/A default:
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * As the hypervisor has no way to denote handshaking
2N/A * in its serial port settings, emit a metal serial
2N/A * port configuration with none as well.
2N/A */
2N/A (void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
2N/A bits, parity, stop);
2N/A }
2N/A
2N/A if (set_serial_rate(com, ser) != 0)
2N/A return (-1);
2N/A
2N/A return (0);
2N/A }
2N/A
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Parse a hardware kernel's "kernel$" specifier into parameters we can then
2N/A * use to construct an appropriate "module$" line that can be used to specify
2N/A * how to boot the hypervisor's dom0.
2N/A *
2N/A * Return values:
2N/A *
2N/A * -1: error parsing kernel path
2N/A * 0: success
2N/A * 1: kernel already a hypervisor kernel
2N/A */
2N/Astatic int
2N/Acvt_metal_kernel(char *kernstr, char **path)
2N/A{
2N/A char *token, *parsestr;
2N/A
2N/A parsestr = get_token(path, kernstr, " \t,");
2N/A if (*path == NULL)
2N/A return (-1);
2N/A
2N/A /*
2N/A * If the metal kernel specified contains the name of the hypervisor,
2N/A * we're probably trying to convert an entry already setup to run the
2N/A * hypervisor, so error out now.
2N/A */
2N/A if (strstr(*path, XEN_MENU) != NULL) {
2N/A bam_error(ALREADY_HYPER);
2N/A free(*path);
2N/A *path = NULL;
2N/A return (1);
2N/A }
2N/A
2N/A /* if the path was the last item on the line, that's OK. */
2N/A if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
2N/A if (token != NULL)
2N/A free(token);
2N/A return (0);
2N/A }
2N/A
2N/A /* if the next token is "-B" process boot options */
2N/A if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
2N/A free(token);
2N/A return (0);
2N/A }
2N/A
2N/A free(token);
2N/A
2N/A while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
2N/A (void) cvt_metal_option(token);
2N/A free(token);
2N/A }
2N/A
2N/A if (token != NULL) {
2N/A (void) cvt_metal_option(token);
2N/A free(token);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Parse a hypervisor's "kernel$" line into parameters that can be used to
2N/A * help build an appropriate "kernel$" line for booting a bare metal kernel.
2N/A *
2N/A * Return 0 on success, non-zero on failure.
2N/A */
2N/Astatic int
2N/Acvt_hyper_kernel(char *kernel)
2N/A{
2N/A char *token, *parsestr;
2N/A
2N/A parsestr = get_token(&token, kernel, " \t,");
2N/A
2N/A if (token == NULL)
2N/A return (-1);
2N/A
2N/A /*
2N/A * If the hypervisor kernel specified lives in the metal kernel
2N/A * directory, we're probably trying to convert an entry already setup
2N/A * to run on bare metal, so error out now.
2N/A */
2N/A if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
2N/A bam_error(ALREADY_METAL);
2N/A free(token);
2N/A return (-1);
2N/A }
2N/A
2N/A free(token);
2N/A
2N/A /* check for kernel options */
2N/A while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
2N/A (void) cvt_hyper_option(token);
2N/A free(token);
2N/A }
2N/A
2N/A if (token != NULL) {
2N/A (void) cvt_hyper_option(token);
2N/A free(token);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Parse a hypervisor's "module$" line into parameters that can be used to
2N/A * help build an appropriate "kernel$" line for booting a bare metal kernel.
2N/A */
2N/Astatic void
2N/Acvt_hyper_module(char *modstr, char **path)
2N/A{
2N/A char *token = NULL;
2N/A char *parsestr = modstr;
2N/A
2N/A /*
2N/A * If multiple pathnames exist on the module$ line, we just want
2N/A * the last one.
2N/A */
2N/A while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
2N/A if (*parsestr != '/')
2N/A break;
2N/A else
2N/A free(*path);
2N/A }
2N/A
2N/A /* if the path was the last item on the line, that's OK. */
2N/A if ((parsestr == NULL) ||
2N/A ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
2N/A if (token != NULL)
2N/A free(token);
2N/A return;
2N/A }
2N/A
2N/A if (token == NULL)
2N/A return;
2N/A
2N/A /* check for "-B" option */
2N/A if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
2N/A free(token);
2N/A return;
2N/A }
2N/A
2N/A free(token);
2N/A
2N/A /* check for kernel options */
2N/A while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
2N/A (void) cvt_hyper_option(token);
2N/A free(token);
2N/A }
2N/A
2N/A if (token != NULL) {
2N/A (void) cvt_hyper_option(token);
2N/A free(token);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Aparse_bootenvrc(char *osroot)
2N/A{
2N/A#define LINEBUF_SZ 1024
2N/A
2N/A FILE *fp;
2N/A char *rcpath;
2N/A char line[LINEBUF_SZ]; /* make line buffer large but not ridiculous */
2N/A int len;
2N/A
2N/A assert(osroot);
2N/A
2N/A len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
2N/A rcpath = alloca(len);
2N/A
2N/A (void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
2N/A
2N/A /* if we couldn't open the bootenv.rc file, ignore the issue. */
2N/A if ((fp = fopen(rcpath, "r")) == NULL) {
2N/A BAM_DPRINTF((D_NO_BOOTENVRC, rcpath, strerror(errno)));
2N/A return;
2N/A }
2N/A
2N/A while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
2N/A char *parsestr, *token;
2N/A int port = 0;
2N/A
2N/A /* we're only interested in parsing "setprop" directives. */
2N/A if (strncmp(line, "setprop", 7) != NULL)
2N/A continue;
2N/A
2N/A /* eat initial "setprop" */
2N/A if ((parsestr = get_token(&token, line, " \t")) == NULL) {
2N/A if (token != NULL)
2N/A free(token);
2N/A
2N/A continue;
2N/A }
2N/A
2N/A if (strcmp(token, "setprop") != 0) {
2N/A free(token);
2N/A continue;
2N/A }
2N/A
2N/A free(token);
2N/A
2N/A /* get property name */
2N/A if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
2N/A if (token != NULL)
2N/A free(token);
2N/A
2N/A continue;
2N/A }
2N/A
2N/A if (strcmp(token, "console") == 0) {
2N/A free(token);
2N/A
2N/A /* get console property value */
2N/A parsestr = get_token(&token, parsestr, " \t");
2N/A if (token == NULL)
2N/A continue;
2N/A
2N/A if (bootenv_rc_console != NULL)
2N/A free(bootenv_rc_console);
2N/A
2N/A bootenv_rc_console = s_strdup(token);
2N/A continue;
2N/A }
2N/A
2N/A /* check if it's a serial port setting */
2N/A if (strcmp(token, "ttya-mode") == 0) {
2N/A free(token);
2N/A port = 0;
2N/A } else if (strcmp(token, "ttyb-mode") == 0) {
2N/A free(token);
2N/A port = 1;
2N/A } else {
2N/A /* nope, so check the next line */
2N/A free(token);
2N/A continue;
2N/A }
2N/A
2N/A /* get serial port setting */
2N/A parsestr = get_token(&token, parsestr, " \t");
2N/A
2N/A if (token == NULL)
2N/A continue;
2N/A
2N/A if (bootenv_rc_serial[port] != NULL)
2N/A free(bootenv_rc_serial[port]);
2N/A
2N/A bootenv_rc_serial[port] = s_strdup(token);
2N/A free(token);
2N/A }
2N/A
2N/A (void) fclose(fp);
2N/A}
2N/A
2N/Aerror_t
2N/Acvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
2N/A{
2N/A const char *fcn = "cvt_to_hyper()";
2N/A
2N/A line_t *lp;
2N/A entry_t *ent;
2N/A size_t len, zfslen;
2N/A
2N/A char *newstr;
2N/A char *osdev;
2N/A
2N/A char *title = NULL;
2N/A char *findroot = NULL;
2N/A char *bootfs = NULL;
2N/A char *kernel = NULL;
2N/A char *mod_kernel = NULL;
2N/A char *module = NULL;
2N/A
2N/A char *kern_path = NULL;
2N/A char *kern_bargs = NULL;
2N/A
2N/A int curdef, newdef;
2N/A int kp_allocated = 0;
2N/A int ret = BAM_ERROR;
2N/A
2N/A assert(osroot);
2N/A
2N/A BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, extra_args));
2N/A
2N/A /*
2N/A * First just check to verify osroot is a sane directory.
2N/A */
2N/A if ((osdev = get_special(osroot)) == NULL) {
2N/A bam_error(CANT_FIND_SPECIAL, osroot);
2N/A return (BAM_ERROR);
2N/A }
2N/A
2N/A free(osdev);
2N/A
2N/A /*
2N/A * While the effect is purely cosmetic, if osroot is "/" don't
2N/A * bother prepending it to any paths as they are constructed to
2N/A * begin with "/" anyway.
2N/A */
2N/A if (strcmp(osroot, "/") == 0)
2N/A osroot = "";
2N/A
2N/A /*
2N/A * Found the GRUB signature on the target partitions, so now get the
2N/A * default GRUB boot entry number from the menu.lst file
2N/A */
2N/A curdef = atoi(mp->curdefault->arg);
2N/A
2N/A /* look for the first line of the matching boot entry */
2N/A for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
2N/A ent = ent->next)
2N/A ;
2N/A
2N/A /* couldn't find it, so error out */
2N/A if (ent == NULL) {
2N/A bam_error(CANT_FIND_DEFAULT, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A /*
2N/A * We found the proper menu entry, so first we need to process the
2N/A * bootenv.rc file to look for boot options the hypervisor might need
2N/A * passed as kernel start options such as the console device and serial
2N/A * port parameters.
2N/A *
2N/A * If there's no bootenv.rc, it's not an issue.
2N/A */
2N/A parse_bootenvrc(osroot);
2N/A
2N/A if (bootenv_rc_console != NULL)
2N/A console_metal_to_hyper(bootenv_rc_console);
2N/A
2N/A if (bootenv_rc_serial[0] != NULL)
2N/A (void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);
2N/A
2N/A if (bootenv_rc_serial[1] != NULL)
2N/A (void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);
2N/A
2N/A /*
2N/A * Now process the entry itself.
2N/A */
2N/A for (lp = ent->start; lp != NULL; lp = lp->next) {
2N/A /*
2N/A * Process important lines from menu.lst boot entry.
2N/A */
2N/A if (lp->flags == BAM_TITLE) {
2N/A title = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(title, lp->arg);
2N/A } else if (strcmp(lp->cmd, "findroot") == 0) {
2N/A findroot = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(findroot, lp->arg);
2N/A } else if (strcmp(lp->cmd, "bootfs") == 0) {
2N/A bootfs = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(bootfs, lp->arg);
2N/A } else if (strcmp(lp->cmd, menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
2N/A module = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(module, lp->arg);
2N/A } else if ((strcmp(lp->cmd,
2N/A menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
2N/A (ret = cvt_metal_kernel(lp->arg, &kern_path)) != 0) {
2N/A if (ret < 0) {
2N/A ret = BAM_ERROR;
2N/A bam_error(KERNEL_NOT_PARSEABLE, curdef);
2N/A } else
2N/A ret = BAM_NOCHANGE;
2N/A
2N/A goto abort;
2N/A }
2N/A
2N/A if (lp == ent->end)
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * If findroot, module or kern_path are NULL, the boot entry is
2N/A * malformed.
2N/A */
2N/A if (findroot == NULL) {
2N/A bam_error(FINDROOT_NOT_FOUND, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A if (module == NULL) {
2N/A bam_error(MODULE_NOT_PARSEABLE, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A if (kern_path == NULL) {
2N/A bam_error(KERNEL_NOT_FOUND, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A /* assemble new kernel and module arguments from parsed values */
2N/A if (console_dev != NULL) {
2N/A kern_bargs = s_strdup(console_dev);
2N/A
2N/A if (serial_config[0] != NULL) {
2N/A newstr = append_str(kern_bargs, serial_config[0], " ");
2N/A free(kern_bargs);
2N/A kern_bargs = newstr;
2N/A }
2N/A
2N/A if (serial_config[1] != NULL) {
2N/A newstr = append_str(kern_bargs, serial_config[1], " ");
2N/A free(kern_bargs);
2N/A kern_bargs = newstr;
2N/A }
2N/A }
2N/A
2N/A if ((extra_args != NULL) && (*extra_args != NULL)) {
2N/A newstr = append_str(kern_bargs, extra_args, " ");
2N/A free(kern_bargs);
2N/A kern_bargs = newstr;
2N/A }
2N/A
2N/A len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
2N/A WHITESPC(1) + 1;
2N/A
2N/A kernel = alloca(len);
2N/A
2N/A if (kern_bargs != NULL) {
2N/A if (*kern_bargs != NULL)
2N/A (void) snprintf(kernel, len, "%s%s %s", osroot,
2N/A XEN_MENU, kern_bargs);
2N/A
2N/A free(kern_bargs);
2N/A } else {
2N/A (void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
2N/A }
2N/A
2N/A /*
2N/A * Change the kernel directory from the metal version to that needed for
2N/A * the hypervisor. Convert either "direct boot" path to the default
2N/A * path.
2N/A */
2N/A if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
2N/A (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
2N/A kern_path = HYPERVISOR_KERNEL;
2N/A } else {
2N/A newstr = modify_path(kern_path, METAL_KERNEL_DIR,
2N/A HYPER_KERNEL_DIR);
2N/A free(kern_path);
2N/A kern_path = newstr;
2N/A kp_allocated = 1;
2N/A }
2N/A
2N/A /*
2N/A * We need to allocate space for the kernel path (twice) plus an
2N/A * intervening space, possibly the ZFS boot string, and NULL,
2N/A * of course.
2N/A */
2N/A len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
2N/A zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
2N/A
2N/A mod_kernel = alloca(len + zfslen);
2N/A (void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
2N/A
2N/A if (kp_allocated)
2N/A free(kern_path);
2N/A
2N/A if (zfs_boot) {
2N/A char *zfsstr = alloca(zfslen + 1);
2N/A
2N/A (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
2N/A (void) strcat(mod_kernel, zfsstr);
2N/A }
2N/A
2N/A /* shut off warning messages from the entry line parser */
2N/A if (ent->flags & BAM_ENTRY_BOOTADM)
2N/A ent->flags &= ~BAM_ENTRY_BOOTADM;
2N/A
2N/A BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
2N/A BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, mod_kernel));
2N/A
2N/A if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
2N/A module, bootfs)) == BAM_ERROR)
2N/A return (newdef);
2N/A
2N/A /*
2N/A * Now try to delete the current default entry from the menu and add
2N/A * the new hypervisor entry with the parameters we've setup.
2N/A */
2N/A if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
2N/A newdef--;
2N/A else
2N/A bam_print(NEW_BOOT_ENTRY, title);
2N/A
2N/A /*
2N/A * If we successfully created the new entry, set the default boot
2N/A * entry to that entry and let the caller know the new menu should
2N/A * be written out.
2N/A */
2N/A return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
2N/A
2N/Aabort:
2N/A if (ret != BAM_NOCHANGE)
2N/A bam_error(HYPER_ABORT, ((*osroot == NULL) ? "/" : osroot));
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Aerror_t
2N/Acvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
2N/A{
2N/A const char *fcn = "cvt_to_metal()";
2N/A
2N/A line_t *lp;
2N/A entry_t *ent;
2N/A size_t len, zfslen;
2N/A
2N/A char *delim = ",";
2N/A char *newstr;
2N/A char *osdev;
2N/A
2N/A char *title = NULL;
2N/A char *findroot = NULL;
2N/A char *bootfs = NULL;
2N/A char *kernel = NULL;
2N/A char *module = NULL;
2N/A
2N/A char *barchive_path = DIRECT_BOOT_ARCHIVE;
2N/A char *kern_path = NULL;
2N/A
2N/A int curdef, newdef;
2N/A int emit_bflag = 1;
2N/A int ret = BAM_ERROR;
2N/A
2N/A assert(osroot);
2N/A
2N/A BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, ""));
2N/A
2N/A /*
2N/A * First just check to verify osroot is a sane directory.
2N/A */
2N/A if ((osdev = get_special(osroot)) == NULL) {
2N/A bam_error(CANT_FIND_SPECIAL, osroot);
2N/A return (BAM_ERROR);
2N/A }
2N/A
2N/A free(osdev);
2N/A
2N/A /*
2N/A * Found the GRUB signature on the target partitions, so now get the
2N/A * default GRUB boot entry number from the menu.lst file
2N/A */
2N/A curdef = atoi(mp->curdefault->arg);
2N/A
2N/A /* look for the first line of the matching boot entry */
2N/A for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
2N/A ent = ent->next)
2N/A ;
2N/A
2N/A /* couldn't find it, so error out */
2N/A if (ent == NULL) {
2N/A bam_error(CANT_FIND_DEFAULT, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A /*
2N/A * Now process the entry itself.
2N/A */
2N/A for (lp = ent->start; lp != NULL; lp = lp->next) {
2N/A /*
2N/A * Process important lines from menu.lst boot entry.
2N/A */
2N/A if (lp->flags == BAM_TITLE) {
2N/A title = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(title, lp->arg);
2N/A } else if (strcmp(lp->cmd, "findroot") == 0) {
2N/A findroot = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(findroot, lp->arg);
2N/A } else if (strcmp(lp->cmd, "bootfs") == 0) {
2N/A bootfs = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(bootfs, lp->arg);
2N/A } else if (strcmp(lp->cmd, menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
2N/A if (strstr(lp->arg, "boot_archive") == NULL) {
2N/A module = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(module, lp->arg);
2N/A cvt_hyper_module(module, &kern_path);
2N/A } else {
2N/A barchive_path = alloca(strlen(lp->arg) + 1);
2N/A (void) strcpy(barchive_path, lp->arg);
2N/A }
2N/A } else if ((strcmp(lp->cmd,
2N/A menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
2N/A (cvt_hyper_kernel(lp->arg) < 0)) {
2N/A ret = BAM_NOCHANGE;
2N/A goto abort;
2N/A }
2N/A
2N/A if (lp == ent->end)
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * If findroot, module or kern_path are NULL, the boot entry is
2N/A * malformed.
2N/A */
2N/A if (findroot == NULL) {
2N/A bam_error(FINDROOT_NOT_FOUND, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A if (module == NULL) {
2N/A bam_error(MODULE_NOT_PARSEABLE, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A if (kern_path == NULL) {
2N/A bam_error(KERNEL_NOT_FOUND, curdef);
2N/A goto abort;
2N/A }
2N/A
2N/A /*
2N/A * Assemble new kernel and module arguments from parsed values.
2N/A *
2N/A * First, change the kernel directory from the hypervisor version to
2N/A * that needed for a metal kernel.
2N/A */
2N/A newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
2N/A free(kern_path);
2N/A kern_path = newstr;
2N/A
2N/A /* allocate initial space for the kernel path */
2N/A len = strlen(kern_path) + 1;
2N/A zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
2N/A
2N/A if ((kernel = malloc(len + zfslen)) == NULL) {
2N/A free(kern_path);
2N/A bam_error(NO_MEM, len + zfslen);
2N/A bam_exit(1);
2N/A }
2N/A
2N/A (void) snprintf(kernel, len, "%s", kern_path);
2N/A free(kern_path);
2N/A
2N/A if (zfs_boot) {
2N/A char *zfsstr = alloca(zfslen + 1);
2N/A
2N/A (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
2N/A (void) strcat(kernel, zfsstr);
2N/A emit_bflag = 0;
2N/A }
2N/A
2N/A /*
2N/A * Process the bootenv.rc file to look for boot options that would be
2N/A * the same as what the hypervisor had manually set, as we need not set
2N/A * those explicitly.
2N/A *
2N/A * If there's no bootenv.rc, it's not an issue.
2N/A */
2N/A parse_bootenvrc(osroot);
2N/A
2N/A /*
2N/A * Don't emit a console setting if it's the same as what would be
2N/A * set by bootenv.rc.
2N/A */
2N/A if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
2N/A (strcmp(console_dev, bootenv_rc_console) != 0))) {
2N/A if (emit_bflag) {
2N/A newstr = append_str(kernel, BFLAG, " ");
2N/A free(kernel);
2N/A kernel = append_str(newstr, "console=", " ");
2N/A free(newstr);
2N/A newstr = append_str(kernel, console_dev, "");
2N/A free(kernel);
2N/A kernel = newstr;
2N/A emit_bflag = 0;
2N/A } else {
2N/A newstr = append_str(kernel, "console=", ",");
2N/A free(kernel);
2N/A kernel = append_str(newstr, console_dev, "");
2N/A free(newstr);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * We have to do some strange processing here because the hypervisor's
2N/A * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
2N/A * or to "auto" if nothing is specified.
2N/A *
2N/A * This could result in a serial mode setting string being added when
2N/A * it would otherwise not be needed, but it's better to play it safe.
2N/A */
2N/A if (emit_bflag) {
2N/A newstr = append_str(kernel, BFLAG, " ");
2N/A free(kernel);
2N/A kernel = newstr;
2N/A delim = " ";
2N/A emit_bflag = 0;
2N/A }
2N/A
2N/A if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
2N/A (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
2N/A newstr = append_str(kernel, "ttya-mode='", delim);
2N/A free(kernel);
2N/A
2N/A /*
2N/A * Pass the serial configuration as the delimiter to
2N/A * append_str() as it will be inserted between the current
2N/A * string and the string we're appending, in this case the
2N/A * closing single quote.
2N/A */
2N/A kernel = append_str(newstr, "'", serial_config[0]);
2N/A free(newstr);
2N/A delim = ",";
2N/A }
2N/A
2N/A if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
2N/A (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
2N/A newstr = append_str(kernel, "ttyb-mode='", delim);
2N/A free(kernel);
2N/A
2N/A /*
2N/A * Pass the serial configuration as the delimiter to
2N/A * append_str() as it will be inserted between the current
2N/A * string and the string we're appending, in this case the
2N/A * closing single quote.
2N/A */
2N/A kernel = append_str(newstr, "'", serial_config[1]);
2N/A free(newstr);
2N/A delim = ",";
2N/A }
2N/A
2N/A /* shut off warning messages from the entry line parser */
2N/A if (ent->flags & BAM_ENTRY_BOOTADM)
2N/A ent->flags &= ~BAM_ENTRY_BOOTADM;
2N/A
2N/A BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
2N/A BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, module));
2N/A
2N/A if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
2N/A barchive_path, bootfs)) == BAM_ERROR) {
2N/A free(kernel);
2N/A return (newdef);
2N/A }
2N/A
2N/A /*
2N/A * Now try to delete the current default entry from the menu and add
2N/A * the new hypervisor entry with the parameters we've setup.
2N/A */
2N/A if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
2N/A newdef--;
2N/A else
2N/A bam_print(NEW_BOOT_ENTRY, title);
2N/A
2N/A free(kernel);
2N/A
2N/A /*
2N/A * If we successfully created the new entry, set the default boot
2N/A * entry to that entry and let the caller know the new menu should
2N/A * be written out.
2N/A */
2N/A return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
2N/A
2N/Aabort:
2N/A if (ret != BAM_NOCHANGE)
2N/A bam_error(METAL_ABORT, osroot);
2N/A
2N/A return (ret);
2N/A}
2N/A