Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / cmd / boot / bootadm / bootadm_hyper.c
blob08c3759d2b2e595852a7c19759496c682b7cafc7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2016 Toomas Soome <tsoome@me.com>
24 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
27 #include <stdio.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <alloca.h>
33 #include <ctype.h>
34 #include <sys/types.h>
36 #include "bootadm.h"
38 #define HYPER_KERNEL_DIR "/platform/i86xpv/kernel"
39 #define METAL_KERNEL_DIR "/platform/i86pc/kernel"
41 #define BOOTRC_FILE "/boot/solaris/bootenv.rc"
42 #define ZFS_BOOTSTR "$ZFS-BOOTFS"
44 #define BFLAG "-B"
45 #define DEFAULT_SERIAL "9600,8,n,1"
47 #define TTYXMODE_TO_COMNUM(ttyxmode) ((int)(*((ttyxmode) + 3) - '`'))
48 #define COMNAME_TO_COMNUM(comname) ((int)(*((comname) + 3) - '0'))
50 #define WHITESPC(x) (x)
52 static char *serial_config[2] = { NULL, NULL };
53 static char *console_dev = NULL;
55 static char *bootenv_rc_serial[2] = { NULL, NULL };
56 static char *bootenv_rc_console = NULL;
58 static unsigned zfs_boot = 0;
61 * Append the string pointed to by "str" to the string pointed to by "orig"
62 * adding the delimeter "delim" in between.
64 * Return a pointer to the new string or NULL, if we were passed a bad string.
66 static char *
67 append_str(char *orig, char *str, char *delim)
69 char *newstr;
70 int len;
72 if ((str == NULL) || (delim == NULL))
73 return (NULL);
75 if ((orig == NULL) || (*orig == NULL)) {
77 * Return a pointer to a copy of the path so a caller can
78 * always rely upon being able to free() a returned pointer.
80 return (s_strdup(str));
83 len = strlen(orig) + strlen(str) + strlen(delim) + 1;
84 if ((newstr = malloc(len)) == NULL) {
85 bam_error(_("could not allocate memory: size = %u\n"), len);
86 bam_exit(1);
89 (void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
90 return (newstr);
94 * Replace the substring "old_str" in a path with the substring "new_str"
96 * Return a pointer to the modified string.
98 static char *
99 modify_path(char *path, char *old_str, char *new_str)
101 char *newpath;
102 char *pc;
103 int len;
106 * Return a pointer to a copy of the path so a caller can always rely
107 * upon being able to free() a returned pointer.
109 if ((pc = strstr(path, old_str)) == NULL)
110 return (s_strdup(path));
113 * Allocate space for duplicate of path with name changes and
114 * NULL terminating byte
116 len = strlen(path) - strlen(old_str) + strlen(new_str) + 1;
118 if ((newpath = malloc(len)) == NULL) {
119 bam_error(_("could not allocate memory: size = %u\n"), len);
120 bam_exit(1);
123 (void) strlcpy(newpath, path, (pc - path) + 1);
124 pc += strlen(old_str);
126 (void) strcat(newpath, new_str);
127 (void) strcat(newpath, pc);
128 return (newpath);
132 * Set "token" to be the the string starting from the pointer "str" delimited
133 * by any character in the string "delim" or the end of the string, but IGNORE
134 * any characters between single or double quotes.
136 * Return a pointer to the next non-whitespace character after the delimiter
137 * or NULL if we hit the end of the string. Also return NULL upon failure to
138 * find any characters from the delimeter string or upon failure to allocate
139 * memory for the new token string.
141 static char *
142 get_token(char **token, char *str, char *delim)
144 char *dp;
145 char *start = str;
146 unsigned len;
148 *token = NULL;
150 if ((str == NULL) || (*str == NULL))
151 return (NULL);
153 do {
154 if ((*str == '\'') || (*str == '"')) {
155 char quote = *str++;
157 while ((*str != NULL) && (*str != quote))
158 str++;
160 /* no matching quote found in string */
161 if (*str++ == NULL)
162 return (NULL);
165 /* look for a character from the delimiter string */
166 for (dp = delim; ((*dp != NULL) && (*dp != *str)); dp++)
169 if (*dp != NULL) {
170 len = str - start + 1;
172 /* found a delimiter, so create a token string */
173 if ((*token = malloc(len)) == NULL) {
174 bam_error(_("could not allocate memory: "
175 "size = %u\n"), len);
176 bam_exit(1);
179 (void) strlcpy(*token, start, len);
181 while (isspace((int)*++str))
184 return (str);
186 } while (*str++ != NULL);
188 /* if we hit the end of the string, the token is the whole string */
189 *token = s_strdup(start);
190 return (NULL);
194 * Convert a metal "console" device name to an equivalent one suitable for
195 * use with the hypervisor.
197 * Default to "vga" if we can't parse the console device.
199 static void
200 console_metal_to_hyper(char *console)
202 if ((*console == '\'') || (*console == '"'))
203 console++;
205 if (strncmp(console, "ttya", 4) == 0)
206 console_dev = "console=com1";
207 else if (strncmp(console, "ttyb", 4) == 0)
208 console_dev = "console=com2";
209 else
210 console_dev = "console=vga";
213 static int
214 set_serial_rate(int com, char *rate)
216 char **rp = &serial_config[com - 1];
218 if ((com < 1) || (com > 2))
219 return (-1);
222 * If rate is a NULL pointer, erase any existing serial configuration
223 * for this serial port.
225 if (rate == NULL) {
226 if (*rp != NULL) {
227 free(*rp);
228 *rp = NULL;
230 return (0);
233 *rp = s_realloc(*rp, strlen(rate) + 1);
234 (void) strcpy(*rp, rate);
235 return (0);
239 * Convert "metal" serial port parameters to values compatible with the
240 * hypervisor.
242 * Return 0 on success, otherwise -1.
244 static int
245 serial_metal_to_hyper(char *metal_port, char *metal_serial)
247 #define COM_RATE_LEN 16 /* strlen("com1=115200,8n1") */
249 char com_rate[COM_RATE_LEN];
251 unsigned com, baud, bits, stop;
252 char parity, handshake;
254 if ((strcmp(metal_port, "ttya-mode") == 0) ||
255 (strcmp(metal_port, "ttyb-mode") == 0))
256 com = TTYXMODE_TO_COMNUM(metal_port);
257 else
258 return (-1);
260 if ((*metal_serial == '\'') || (*metal_serial == '"'))
261 metal_serial++;
264 * Check if it's specified as the default rate; if so it defaults to
265 * "auto" and we need not set it for they hypervisor.
267 if (strncmp(metal_serial, DEFAULT_SERIAL,
268 strlen(DEFAULT_SERIAL)) == 0) {
269 (void) set_serial_rate(com, NULL);
270 return (0);
273 /* read the serial port format as set forth in common/io/asy.c */
274 if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
275 &handshake) != 5)
276 return (-1);
278 /* validate serial port parameters */
279 if (((bits < 5) || (bits > 8)) || (stop > 1) ||
280 ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
281 ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
282 return (-1);
284 /* validate baud rate */
285 switch (baud) {
286 case 150:
287 case 300:
288 case 600:
289 case 1200:
290 case 2400:
291 case 4800:
292 case 9600:
293 case 19200:
294 case 38400:
295 case 57600:
296 case 115200:
297 break;
299 default:
300 return (-1);
304 * The hypervisor has no way to specify a handshake method, so it gets
305 * quietly dropped in the conversion.
307 (void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
308 bits, parity, stop);
309 (void) set_serial_rate(com, com_rate);
310 return (0);
314 * Convert "name=value" metal options to values suitable for use with the
315 * hypervisor.
317 * Our main concerns are the console device and serial port settings.
319 * Return values:
321 * -1: Unparseable line
322 * 0: Success
323 * (n > 0): A property unimportant to us
325 static int
326 cvt_metal_option(char *optstr)
328 char *value;
329 unsigned namlen;
331 if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
332 zfs_boot = 1;
333 return (0);
336 if ((value = strchr(optstr, '=')) == NULL)
337 return (-1);
339 namlen = value - optstr;
341 if (*++value == NULL)
342 return (1);
344 if (strncmp(optstr, "console", namlen) == 0) {
345 console_metal_to_hyper(value);
346 return (0);
349 if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
350 (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
351 char *port = strndupa(optstr, namlen);
353 return (serial_metal_to_hyper(port, value));
356 return (1);
360 * Convert "name=value" properties for use with a bare metal kernel
362 * Our main concerns are the console setting and serial port modes.
364 * Return values:
366 * -1: Unparseable line
367 * 0: Success
368 * (n > 0): A property unimportant to us
370 static int
371 cvt_hyper_option(char *optstr)
373 #define SER_LEN 15 /* strlen("115200,8,n,1,-") + 1 */
375 char ser[SER_LEN];
376 char *value;
378 unsigned namlen;
380 unsigned baud;
381 char bits, parity, stop;
383 if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
384 zfs_boot = 1;
385 return (0);
389 * If there's no "=" in the token, it's likely a standalone
390 * hypervisor token we don't care about (e.g. "noreboot" or
391 * "nosmp") so we ignore it.
393 if ((value = strchr(optstr, '=')) == NULL)
394 return (1);
396 namlen = value - optstr;
398 if (*++value == NULL)
399 return (1);
402 * Note that we use strncmp against the values because the
403 * hypervisor allows setting console parameters for both the
404 * console and debugger via the format:
406 * console=cons_dev,debug_dev
408 * and we only care about "cons_dev."
410 * This also allows us to extract "comN" from hypervisor constructs
411 * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
413 * Default the console device to "text" if it was "vga" or was
414 * unparseable.
416 if (strncmp(optstr, "console", namlen) == 0) {
417 /* ignore the "console=hypervisor" option */
418 if (strcmp(value, "hypervisor") == 0)
419 return (0);
421 if (strncmp(value, "com1", 4) == 0)
422 console_dev = "ttya";
423 else if (strncmp(value, "com2", 4) == 0)
424 console_dev = "ttyb";
425 else
426 console_dev = "text";
429 /* serial port parameter conversion */
431 if ((strncmp(optstr, "com1", namlen) == 0) ||
432 (strncmp(optstr, "com2", namlen) == 0)) {
433 unsigned com = COMNAME_TO_COMNUM(optstr);
436 * Check if it's "auto" - if so, use the default setting
437 * of "9600,8,n,1,-".
439 * We can't just assume the serial port will default to
440 * "9600,8,n,1" as there could be a directive in bootenv.rc
441 * that would set it to some other value and we want the serial
442 * parameters to be the same as that used by the hypervisor.
444 if (strcmp(value, "auto") == 0) {
445 (void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
446 } else {
448 * Extract the "B,PS" setting from the com line; ignore
449 * other settings like io_base or IRQ.
451 if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
452 &stop) != 4)
453 return (-1);
455 /* validate serial port parameters */
456 if (((stop != '0') && (stop != '1')) ||
457 ((bits < '5') && (bits > '8')) ||
458 ((parity != 'n') && (parity != 'e') &&
459 (parity != 'o')))
460 return (-1);
462 /* validate baud rate */
463 switch (baud) {
464 case 150:
465 case 300:
466 case 600:
467 case 1200:
468 case 2400:
469 case 4800:
470 case 19200:
471 case 38400:
472 case 57600:
473 case 115200:
474 break;
476 default:
477 return (-1);
481 * As the hypervisor has no way to denote handshaking
482 * in its serial port settings, emit a metal serial
483 * port configuration with none as well.
485 (void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
486 bits, parity, stop);
489 if (set_serial_rate(com, ser) != 0)
490 return (-1);
492 return (0);
495 return (1);
499 * Parse a hardware kernel's "kernel$" specifier into parameters we can then
500 * use to construct an appropriate "module$" line that can be used to specify
501 * how to boot the hypervisor's dom0.
503 * Return values:
505 * -1: error parsing kernel path
506 * 0: success
507 * 1: kernel already a hypervisor kernel
509 static int
510 cvt_metal_kernel(char *kernstr, char **path)
512 char *token, *parsestr;
514 parsestr = get_token(path, kernstr, " \t,");
515 if (*path == NULL)
516 return (-1);
519 * If the metal kernel specified contains the name of the hypervisor,
520 * we're probably trying to convert an entry already setup to run the
521 * hypervisor, so error out now.
523 if (strstr(*path, XEN_MENU) != NULL) {
524 bam_error(_("default entry already setup for use with the "
525 "hypervisor!\n"));
526 free(*path);
527 *path = NULL;
528 return (1);
531 /* if the path was the last item on the line, that's OK. */
532 if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
533 free(token);
534 return (0);
537 /* if the next token is "-B" process boot options */
538 if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
539 free(token);
540 return (0);
543 free(token);
545 while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
546 (void) cvt_metal_option(token);
547 free(token);
550 if (token != NULL) {
551 (void) cvt_metal_option(token);
552 free(token);
555 return (0);
559 * Parse a hypervisor's "kernel$" line into parameters that can be used to
560 * help build an appropriate "kernel$" line for booting a bare metal kernel.
562 * Return 0 on success, non-zero on failure.
564 static int
565 cvt_hyper_kernel(char *kernel)
567 char *token, *parsestr;
569 parsestr = get_token(&token, kernel, " \t,");
571 if (token == NULL)
572 return (-1);
575 * If the hypervisor kernel specified lives in the metal kernel
576 * directory, we're probably trying to convert an entry already setup
577 * to run on bare metal, so error out now.
579 if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
580 bam_error(_("default entry already setup for use with a metal "
581 "kernel!\n"));
582 free(token);
583 return (-1);
586 free(token);
588 /* check for kernel options */
589 while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
590 (void) cvt_hyper_option(token);
591 free(token);
594 if (token != NULL) {
595 (void) cvt_hyper_option(token);
596 free(token);
599 return (0);
603 * Parse a hypervisor's "module$" line into parameters that can be used to
604 * help build an appropriate "kernel$" line for booting a bare metal kernel.
606 static void
607 cvt_hyper_module(char *modstr, char **path)
609 char *token = NULL;
610 char *parsestr = modstr;
613 * If multiple pathnames exist on the module$ line, we just want
614 * the last one.
616 while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
617 if (*parsestr != '/')
618 break;
619 else
620 free(*path);
623 /* if the path was the last item on the line, that's OK. */
624 if ((parsestr == NULL) ||
625 ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
626 free(token);
627 return;
630 if (token == NULL)
631 return;
633 /* check for "-B" option */
634 if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
635 free(token);
636 return;
639 free(token);
641 /* check for kernel options */
642 while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
643 (void) cvt_hyper_option(token);
644 free(token);
647 if (token != NULL) {
648 (void) cvt_hyper_option(token);
649 free(token);
653 static void
654 parse_bootenvrc(char *osroot)
656 #define LINEBUF_SZ 1024
658 FILE *fp;
659 char *rcpath;
660 char line[LINEBUF_SZ]; /* make line buffer large but not ridiculous */
661 int len;
663 assert(osroot);
665 len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
666 rcpath = alloca(len);
668 (void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
670 /* if we couldn't open the bootenv.rc file, ignore the issue. */
671 if ((fp = fopen(rcpath, "r")) == NULL) {
672 BAM_DPRINTF(("could not open %s: %s\n", rcpath,
673 strerror(errno)));
674 return;
677 while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
678 char *parsestr, *token;
679 int port = 0;
681 /* we're only interested in parsing "setprop" directives. */
682 if (strncmp(line, "setprop", 7) != NULL)
683 continue;
685 /* eat initial "setprop" */
686 if ((parsestr = get_token(&token, line, " \t")) == NULL) {
687 free(token);
689 continue;
692 if (strcmp(token, "setprop") != 0) {
693 free(token);
694 continue;
697 free(token);
699 /* get property name */
700 if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
701 free(token);
703 continue;
706 if (strcmp(token, "console") == 0) {
707 free(token);
709 /* get console property value */
710 parsestr = get_token(&token, parsestr, " \t");
711 if (token == NULL)
712 continue;
714 free(bootenv_rc_console);
716 bootenv_rc_console = s_strdup(token);
717 continue;
720 /* check if it's a serial port setting */
721 if (strcmp(token, "ttya-mode") == 0) {
722 free(token);
723 port = 0;
724 } else if (strcmp(token, "ttyb-mode") == 0) {
725 free(token);
726 port = 1;
727 } else {
728 /* nope, so check the next line */
729 free(token);
730 continue;
733 /* get serial port setting */
734 parsestr = get_token(&token, parsestr, " \t");
736 if (token == NULL)
737 continue;
739 free(bootenv_rc_serial[port]);
741 bootenv_rc_serial[port] = s_strdup(token);
742 free(token);
745 (void) fclose(fp);
748 error_t
749 cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
751 const char *fcn = "cvt_to_hyper()";
753 line_t *lp;
754 entry_t *ent;
755 size_t len, zfslen;
757 char *newstr;
758 char *osdev;
760 char *title = NULL;
761 char *findroot = NULL;
762 char *bootfs = NULL;
763 char *kernel = NULL;
764 char *mod_kernel = NULL;
765 char *module = NULL;
767 char *kern_path = NULL;
768 char *kern_bargs = NULL;
770 int curdef, newdef;
771 int kp_allocated = 0;
772 int ret = BAM_ERROR;
774 assert(osroot);
776 BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, extra_args));
779 * First just check to verify osroot is a sane directory.
781 if ((osdev = get_special(osroot)) == NULL) {
782 bam_error(_("cant find special file for mount-point %s\n"),
783 osroot);
784 return (BAM_ERROR);
787 free(osdev);
790 * While the effect is purely cosmetic, if osroot is "/" don't
791 * bother prepending it to any paths as they are constructed to
792 * begin with "/" anyway.
794 if (strcmp(osroot, "/") == 0)
795 osroot = "";
798 * Found the GRUB signature on the target partitions, so now get the
799 * default GRUB boot entry number from the menu.lst file
801 curdef = atoi(mp->curdefault->arg);
803 /* look for the first line of the matching boot entry */
804 for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
805 ent = ent->next)
808 /* couldn't find it, so error out */
809 if (ent == NULL) {
810 bam_error(_("unable to find default boot entry (%d) in "
811 "menu.lst file.\n"), curdef);
812 goto abort;
816 * We found the proper menu entry, so first we need to process the
817 * bootenv.rc file to look for boot options the hypervisor might need
818 * passed as kernel start options such as the console device and serial
819 * port parameters.
821 * If there's no bootenv.rc, it's not an issue.
823 parse_bootenvrc(osroot);
825 if (bootenv_rc_console != NULL)
826 console_metal_to_hyper(bootenv_rc_console);
828 if (bootenv_rc_serial[0] != NULL)
829 (void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);
831 if (bootenv_rc_serial[1] != NULL)
832 (void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);
835 * Now process the entry itself.
837 for (lp = ent->start; lp != NULL; lp = lp->next) {
839 * Process important lines from menu.lst boot entry.
841 if (lp->flags == BAM_TITLE) {
842 title = strdupa(lp->arg);
843 } else if (lp->cmd != NULL) {
844 if (strcmp(lp->cmd, "findroot") == 0) {
845 findroot = strdupa(lp->arg);
846 } else if (strcmp(lp->cmd, "bootfs") == 0) {
847 bootfs = strdupa(lp->arg);
848 } else if (strcmp(lp->cmd,
849 menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
850 module = strdupa(lp->arg);
851 } else if ((strcmp(lp->cmd,
852 menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
853 (ret = cvt_metal_kernel(lp->arg,
854 &kern_path)) != 0) {
855 if (ret < 0) {
856 ret = BAM_ERROR;
857 bam_error(_("kernel$ in default boot "
858 "entry (%d) missing or not "
859 "parseable.\n"), curdef);
860 } else
861 ret = BAM_NOCHANGE;
863 goto abort;
867 if (lp == ent->end)
868 break;
872 * If findroot, module or kern_path are NULL, the boot entry is
873 * malformed.
875 if (findroot == NULL) {
876 bam_error(_("findroot in default boot entry (%d) missing.\n"),
877 curdef);
878 goto abort;
881 if (module == NULL) {
882 bam_error(_("module$ in default boot entry (%d) missing or "
883 "not parseable.\n"), curdef);
884 goto abort;
887 if (kern_path == NULL) {
888 bam_error(_("kernel$ in default boot entry (%d) missing.\n"),
889 curdef);
890 goto abort;
893 /* assemble new kernel and module arguments from parsed values */
894 if (console_dev != NULL) {
895 kern_bargs = s_strdup(console_dev);
897 if (serial_config[0] != NULL) {
898 newstr = append_str(kern_bargs, serial_config[0], " ");
899 free(kern_bargs);
900 kern_bargs = newstr;
903 if (serial_config[1] != NULL) {
904 newstr = append_str(kern_bargs, serial_config[1], " ");
905 free(kern_bargs);
906 kern_bargs = newstr;
910 if ((extra_args != NULL) && (*extra_args != NULL)) {
911 newstr = append_str(kern_bargs, extra_args, " ");
912 free(kern_bargs);
913 kern_bargs = newstr;
916 len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
917 WHITESPC(1) + 1;
919 kernel = alloca(len);
921 if (kern_bargs != NULL) {
922 if (*kern_bargs != NULL)
923 (void) snprintf(kernel, len, "%s%s %s", osroot,
924 XEN_MENU, kern_bargs);
926 free(kern_bargs);
927 } else {
928 (void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
932 * Change the kernel directory from the metal version to that needed for
933 * the hypervisor. Convert either "direct boot" path to the default
934 * path.
936 if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
937 (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
938 kern_path = HYPERVISOR_KERNEL;
939 } else {
940 newstr = modify_path(kern_path, METAL_KERNEL_DIR,
941 HYPER_KERNEL_DIR);
942 free(kern_path);
943 kern_path = newstr;
944 kp_allocated = 1;
948 * We need to allocate space for the kernel path (twice) plus an
949 * intervening space, possibly the ZFS boot string, and NULL,
950 * of course.
952 len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
953 zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
955 mod_kernel = alloca(len + zfslen);
956 (void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
958 if (kp_allocated)
959 free(kern_path);
961 if (zfs_boot) {
962 char *zfsstr = alloca(zfslen + 1);
964 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
965 (void) strcat(mod_kernel, zfsstr);
968 /* shut off warning messages from the entry line parser */
969 if (ent->flags & BAM_ENTRY_BOOTADM)
970 ent->flags &= ~BAM_ENTRY_BOOTADM;
972 BAM_DPRINTF(("%s: converted kernel cmd to %s\n", fcn, kernel));
973 BAM_DPRINTF(("%s: converted module cmd to %s\n", fcn, mod_kernel));
975 if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
976 module, bootfs)) == BAM_ERROR)
977 return (newdef);
980 * Now try to delete the current default entry from the menu and add
981 * the new hypervisor entry with the parameters we've setup.
983 if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
984 newdef--;
985 else
986 bam_print(_("unable to modify default entry; creating new "
987 "boot entry for %s\n"), title);
990 * If we successfully created the new entry, set the default boot
991 * entry to that entry and let the caller know the new menu should
992 * be written out.
994 return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
996 abort:
997 if (ret != BAM_NOCHANGE)
998 bam_error(_("error converting GRUB menu entry on %s for use "
999 "with the hypervisor.\nAborting.\n"),
1000 ((*osroot == NULL) ? "/" : osroot));
1002 return (ret);
1005 /*ARGSUSED*/
1006 error_t
1007 cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
1009 const char *fcn = "cvt_to_metal()";
1011 line_t *lp;
1012 entry_t *ent;
1013 size_t len, zfslen;
1015 char *delim = ",";
1016 char *newstr;
1017 char *osdev;
1019 char *title = NULL;
1020 char *findroot = NULL;
1021 char *bootfs = NULL;
1022 char *kernel = NULL;
1023 char *module = NULL;
1025 char *barchive_path = DIRECT_BOOT_ARCHIVE;
1026 char *kern_path = NULL;
1028 int curdef, newdef;
1029 int emit_bflag = 1;
1030 int ret = BAM_ERROR;
1032 assert(osroot);
1034 BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, ""));
1037 * First just check to verify osroot is a sane directory.
1039 if ((osdev = get_special(osroot)) == NULL) {
1040 bam_error(_("cant find special file for mount-point %s\n"),
1041 osroot);
1042 return (BAM_ERROR);
1045 free(osdev);
1048 * Found the GRUB signature on the target partitions, so now get the
1049 * default GRUB boot entry number from the menu.lst file
1051 curdef = atoi(mp->curdefault->arg);
1053 /* look for the first line of the matching boot entry */
1054 for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
1055 ent = ent->next)
1058 /* couldn't find it, so error out */
1059 if (ent == NULL) {
1060 bam_error(_("unable to find default boot entry (%d) in "
1061 "menu.lst file.\n"), curdef);
1062 goto abort;
1066 * Now process the entry itself.
1068 for (lp = ent->start; lp != NULL; lp = lp->next) {
1070 * Process important lines from menu.lst boot entry.
1072 if (lp->flags == BAM_TITLE) {
1073 title = strdupa(lp->arg);
1074 } else if (lp->cmd != NULL) {
1075 if (strcmp(lp->cmd, "findroot") == 0) {
1076 findroot = strdupa(lp->arg);
1077 } else if (strcmp(lp->cmd, "bootfs") == 0) {
1078 bootfs = strdupa(lp->arg);
1079 } else if (strcmp(lp->cmd,
1080 menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
1081 if (strstr(lp->arg, "boot_archive") == NULL) {
1082 module = strdupa(lp->arg);
1083 cvt_hyper_module(module, &kern_path);
1084 } else {
1085 barchive_path = strdupa(lp->arg);
1087 } else if ((strcmp(lp->cmd,
1088 menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
1089 (cvt_hyper_kernel(lp->arg) < 0)) {
1090 ret = BAM_NOCHANGE;
1091 goto abort;
1095 if (lp == ent->end)
1096 break;
1100 * If findroot, module or kern_path are NULL, the boot entry is
1101 * malformed.
1103 if (findroot == NULL) {
1104 bam_error(_("findroot in default boot entry (%d) missing.\n"),
1105 curdef);
1106 goto abort;
1109 if (module == NULL) {
1110 bam_error(_("module$ in default boot entry (%d) missing or "
1111 "not parseable.\n"), curdef);
1112 goto abort;
1115 if (kern_path == NULL) {
1116 bam_error(_("kernel$ in default boot entry (%d) missing.\n"),
1117 curdef);
1118 goto abort;
1122 * Assemble new kernel and module arguments from parsed values.
1124 * First, change the kernel directory from the hypervisor version to
1125 * that needed for a metal kernel.
1127 newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
1128 free(kern_path);
1129 kern_path = newstr;
1131 /* allocate initial space for the kernel path */
1132 len = strlen(kern_path) + 1;
1133 zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
1135 if ((kernel = malloc(len + zfslen)) == NULL) {
1136 free(kern_path);
1137 bam_error(_("could not allocate memory: size = %u\n"),
1138 len + zfslen);
1139 bam_exit(1);
1142 (void) snprintf(kernel, len, "%s", kern_path);
1143 free(kern_path);
1145 if (zfs_boot) {
1146 char *zfsstr = alloca(zfslen + 1);
1148 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
1149 (void) strcat(kernel, zfsstr);
1150 emit_bflag = 0;
1154 * Process the bootenv.rc file to look for boot options that would be
1155 * the same as what the hypervisor had manually set, as we need not set
1156 * those explicitly.
1158 * If there's no bootenv.rc, it's not an issue.
1160 parse_bootenvrc(osroot);
1163 * Don't emit a console setting if it's the same as what would be
1164 * set by bootenv.rc.
1166 if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
1167 (strcmp(console_dev, bootenv_rc_console) != 0))) {
1168 if (emit_bflag) {
1169 newstr = append_str(kernel, BFLAG, " ");
1170 free(kernel);
1171 kernel = append_str(newstr, "console=", " ");
1172 free(newstr);
1173 newstr = append_str(kernel, console_dev, "");
1174 free(kernel);
1175 kernel = newstr;
1176 emit_bflag = 0;
1177 } else {
1178 newstr = append_str(kernel, "console=", ",");
1179 free(kernel);
1180 kernel = append_str(newstr, console_dev, "");
1181 free(newstr);
1186 * We have to do some strange processing here because the hypervisor's
1187 * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
1188 * or to "auto" if nothing is specified.
1190 * This could result in a serial mode setting string being added when
1191 * it would otherwise not be needed, but it's better to play it safe.
1193 if (emit_bflag) {
1194 newstr = append_str(kernel, BFLAG, " ");
1195 free(kernel);
1196 kernel = newstr;
1197 delim = " ";
1198 emit_bflag = 0;
1201 if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
1202 (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
1203 newstr = append_str(kernel, "ttya-mode='", delim);
1204 free(kernel);
1207 * Pass the serial configuration as the delimiter to
1208 * append_str() as it will be inserted between the current
1209 * string and the string we're appending, in this case the
1210 * closing single quote.
1212 kernel = append_str(newstr, "'", serial_config[0]);
1213 free(newstr);
1214 delim = ",";
1217 if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
1218 (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
1219 newstr = append_str(kernel, "ttyb-mode='", delim);
1220 free(kernel);
1223 * Pass the serial configuration as the delimiter to
1224 * append_str() as it will be inserted between the current
1225 * string and the string we're appending, in this case the
1226 * closing single quote.
1228 kernel = append_str(newstr, "'", serial_config[1]);
1229 free(newstr);
1230 delim = ",";
1233 /* shut off warning messages from the entry line parser */
1234 if (ent->flags & BAM_ENTRY_BOOTADM)
1235 ent->flags &= ~BAM_ENTRY_BOOTADM;
1237 BAM_DPRINTF(("%s: converted kernel cmd to %s\n", fcn, kernel));
1238 BAM_DPRINTF(("%s: converted module cmd to %s\n", fcn, module));
1240 if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
1241 barchive_path, bootfs)) == BAM_ERROR) {
1242 free(kernel);
1243 return (newdef);
1247 * Now try to delete the current default entry from the menu and add
1248 * the new hypervisor entry with the parameters we've setup.
1250 if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
1251 newdef--;
1252 else
1253 bam_print(_("unable to modify default entry; creating new "
1254 "boot entry for %s\n"), title);
1256 free(kernel);
1259 * If we successfully created the new entry, set the default boot
1260 * entry to that entry and let the caller know the new menu should
1261 * be written out.
1263 return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
1265 abort:
1266 if (ret != BAM_NOCHANGE)
1267 bam_error(_("error converting GRUB menu entry on %s for use "
1268 "with a metal kernel.\nAborting.\n"), osroot);
1270 return (ret);