xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm_hyper.c (revision a4faba164aa153855d621c694fc5aa75dd183b81)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <alloca.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 
35 #include "message.h"
36 #include "bootadm.h"
37 
38 #define	HYPER_KERNEL_DIR 		"/platform/i86xpv/kernel"
39 #define	METAL_KERNEL_DIR 		"/platform/i86pc/kernel"
40 
41 #define	BOOTRC_FILE			"/boot/solaris/bootenv.rc"
42 #define	ZFS_BOOTSTR			"$ZFS-BOOTFS"
43 
44 #define	BFLAG				"-B"
45 #define	DEFAULT_SERIAL			"9600,8,n,1"
46 
47 #define	TTYXMODE_TO_COMNUM(ttyxmode)	((int)(*((ttyxmode) + 3) - '`'))
48 #define	COMNAME_TO_COMNUM(comname)	((int)(*((comname) + 3) - '0'))
49 
50 #define	WHITESPC(x)			(x)
51 
52 static char *serial_config[2] = { NULL, NULL };
53 
54 static char *console_dev = NULL;
55 
56 static unsigned zfs_boot = 0;
57 
58 /*
59  * Append the string pointed to by "str" to the string pointed to by "orig"
60  * adding the delimeter "delim" in between.
61  *
62  * Return a pointer to the new string or NULL, if we were passed a bad string
63  * or we couldn't allocate memory for the new one.
64  */
65 static char *
66 append_str(char *orig, char *str, char *delim)
67 {
68 	char *newstr;
69 	int len;
70 
71 	if ((orig == NULL) || (str == NULL) || (delim == NULL))
72 		return (NULL);
73 
74 	if (*orig == NULL)
75 		return (s_strdup(str));
76 
77 	len = strlen(orig) + strlen(str) + strlen(delim) + 1;
78 	newstr = s_realloc(orig, len);
79 
80 	if (newstr != NULL)
81 		(void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
82 
83 	return (newstr);
84 }
85 
86 /*
87  * Replace the substring "old_str" in a path with the substring "new_str"
88  *
89  * Return a pointer to the  modified string or NULL, if we couldn't allocate
90  * memory for the new string.
91  */
92 static char *
93 modify_path(char *path, char *old_str, char *new_str)
94 {
95 	char *newpath;
96 	char *oldpath;
97 	char *pc;
98 
99 	oldpath = s_strdup(path);
100 
101 	if ((pc = strstr(oldpath, old_str)) == NULL) {
102 		free(oldpath);
103 		return (path);
104 	}
105 
106 	/*
107 	 * Allocate space for duplicate of path with name changes and
108 	 * NULL terminating byte
109 	 */
110 	if ((newpath = s_realloc(path,
111 	    strlen(oldpath) - strlen(old_str) + strlen(new_str) + 1)) == NULL) {
112 		free(oldpath);
113 		return (NULL);
114 	}
115 
116 	(void) strlcpy(newpath, oldpath, (pc - oldpath) + 1);
117 	free(oldpath);
118 
119 	(void) strcat(newpath, new_str);
120 	pc += strlen(old_str);
121 	return (strcat(newpath, pc));
122 }
123 
124 /*
125  * Set "token" to be the the string starting from the pointer "str" delimited
126  * by any character in the string "delim" or the end of the string, but IGNORE
127  * any characters between single or double quotes.
128  *
129  * Return a pointer to the next non-whitespace character after the delimiter
130  * or NULL if we hit the end of the string. Also return NULL upon failure to
131  * find any characters from the delimeter string or upon failure to allocate
132  * memory for the new token string.
133  */
134 static char *
135 get_token(char **token, char *str, char *delim)
136 {
137 	char *dp;
138 	char *start = str;
139 	unsigned len;
140 
141 	*token = NULL;
142 
143 	if ((str == NULL) || (*str == NULL))
144 		return (NULL);
145 
146 	do {
147 		if ((*str == '\'') || (*str == '"')) {
148 			char quote = *str++;
149 
150 			while ((*str != NULL) && (*str != quote))
151 				str++;
152 
153 			/* no matching quote found in string */
154 			if (*str++ == NULL)
155 				return (NULL);
156 		}
157 
158 		/* look for a character from the delimiter string */
159 		for (dp = delim; ((*dp != NULL) && (*dp != *str)); dp++)
160 			;
161 
162 		if (*dp != NULL) {
163 			len = str - start + 1;
164 
165 			/* found a delimiter, so create a token string */
166 			if ((*token = malloc(len)) == NULL) {
167 				bam_error(NO_MEM, len);
168 				return (NULL);
169 			}
170 
171 			(void) strlcpy(*token, start, len);
172 
173 			while (isspace((int)*++str))
174 				;
175 
176 			return (str);
177 		}
178 	} while (*str++ != NULL);
179 
180 	/* if we hit the end of the string, the token is the whole string  */
181 	*token = s_strdup(start);
182 	return (NULL);
183 }
184 
185 /*
186  * Convert a metal "console" device name to an equivalent one suitable for
187  * use with the hypervisor.
188  *
189  * Default to "vga" if we can't parse the console device.
190  */
191 static void
192 console_metal_to_hyper(char *console)
193 {
194 	if ((*console == '\'') || (*console == '"'))
195 		console++;
196 
197 	if (strncmp(console, "ttya", 4) == 0)
198 		console_dev = "console=com1";
199 	else if (strncmp(console, "ttyb", 4) == 0)
200 		console_dev = "console=com2";
201 	else
202 		console_dev = "console=vga";
203 }
204 
205 static int
206 set_serial_rate(int com, char *rate)
207 {
208 	char **rp = &serial_config[com - 1];
209 
210 	/*
211 	 * If rate is a NULL pointer, erase any existing serial configuration
212 	 * for this serial port.
213 	 */
214 	if (rate == NULL) {
215 		if (*rp != NULL) {
216 			free(*rp);
217 			*rp = NULL;
218 		}
219 		return (0);
220 	}
221 
222 	*rp = s_realloc(*rp, strlen(rate));
223 	(void) strcpy(*rp, rate);
224 	return (0);
225 }
226 
227 /*
228  * Convert "metal" serial port parameters to values compatible with the
229  * hypervisor.
230  *
231  * Return 0 on success, otherwise -1.
232  */
233 static int
234 serial_metal_to_hyper(char *metal_port, char *metal_serial)
235 {
236 #define	COM_RATE_LEN	16	/* strlen("com1=115200,8n1") */
237 
238 	char com_rate[COM_RATE_LEN];
239 
240 	unsigned com, baud, bits, stop;
241 	char parity, handshake;
242 
243 	if ((strcmp(metal_port, "ttya-mode") == 0) ||
244 	    (strcmp(metal_port, "ttyb-mode") == 0))
245 		com = TTYXMODE_TO_COMNUM(metal_port);
246 	else
247 		return (-1);
248 
249 	if ((*metal_serial == '\'') || (*metal_serial == '"'))
250 		metal_serial++;
251 
252 	/*
253 	 * Check if it's specified as the default rate; if so it defaults to
254 	 * "auto" and we need not set it for they hypervisor.
255 	 */
256 	if (strncmp(metal_serial, DEFAULT_SERIAL,
257 	    strlen(DEFAULT_SERIAL)) == 0) {
258 		(void) set_serial_rate(com, NULL);
259 		return (0);
260 	}
261 
262 	/* read the serial port format as set forth in common/io/asy.c */
263 	if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
264 	    &handshake) != 5)
265 		return (-1);
266 
267 	/* validate serial port parameters */
268 	if (((bits < 5) || (bits > 8)) || (stop > 1) ||
269 	    ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
270 	    ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
271 		return (-1);
272 
273 	/* validate baud rate */
274 	switch (baud) {
275 		case 150:
276 		case 300:
277 		case 600:
278 		case 1200:
279 		case 2400:
280 		case 4800:
281 		case 9600:
282 		case 19200:
283 		case 38400:
284 		case 57600:
285 		case 115200:
286 			break;
287 
288 		default:
289 			return (-1);
290 	}
291 
292 	/*
293 	 * The hypervisor has no way to specify a handshake method, so it gets
294 	 * quietly dropped in the conversion.
295 	 */
296 	(void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
297 	    bits, parity, stop);
298 	(void) set_serial_rate(com, com_rate);
299 	return (0);
300 }
301 
302 /*
303  * Convert bootenv.rc boot property strings of the form:
304  *
305  *     setprop property value
306  *
307  * into boot option lines suitable for use with the hypervisor.
308  *
309  * Our main concerns are the console device and serial port settings.
310  *
311  * Return values:
312  *
313  *    -1:	Unparseable line
314  *    0:	Success
315  *    (n > 0):	A property unimportant to us
316  */
317 static int
318 cvt_bootprop(char *propstr)
319 {
320 	char *parsestr, *port, *token;
321 
322 	int retval = 0;
323 
324 	/* get initial "setprop" */
325 	if ((parsestr = get_token(&token, propstr, " \t")) == NULL) {
326 		if (token != NULL)
327 			free(token);
328 
329 		return (-1);
330 	}
331 
332 	if (strcmp(token, "setprop")) {
333 		free(token);
334 		return (1);
335 	}
336 
337 	free(token);
338 
339 	/* get property name */
340 	if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
341 		if (token != NULL)
342 			free(token);
343 
344 		return (-2);
345 	}
346 
347 	if (strcmp(token, "console") == 0) {
348 		free(token);
349 
350 		/* get console property value */
351 		parsestr = get_token(&token, parsestr, " \t");
352 		if (token == NULL)
353 			return (-3);
354 
355 		console_metal_to_hyper(token);
356 		free(token);
357 		return (0);
358 	}
359 
360 	/* check if it's a serial port setting */
361 	if ((strcmp(token, "ttya-mode") == 0) ||
362 	    (strcmp(token, "ttyb-mode") == 0)) {
363 		port = token;
364 	} else {
365 		free(token);
366 		return (3);
367 	}
368 
369 	/* get serial port setting */
370 	parsestr = get_token(&token, parsestr, " \t");
371 
372 	if (token == NULL) {
373 		free(port);
374 		return (-1);
375 	}
376 
377 	retval = serial_metal_to_hyper(port, token);
378 
379 	free(port);
380 	free(token);
381 	return (retval);
382 }
383 
384 /*
385  * Convert "name=value" metal options to values suitable for use with the
386  * hypervisor.
387  *
388  * Our main concerns are the console device and serial port settings.
389  *
390  * Return values:
391  *
392  *    -1:	Unparseable line
393  *    0:	Success
394  *    (n > 0):	A property unimportant to us
395  */
396 static int
397 cvt_metal_option(char *optstr)
398 {
399 	char *value;
400 	unsigned namlen;
401 
402 	if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
403 		zfs_boot = 1;
404 		return (0);
405 	}
406 
407 	if ((value = strchr(optstr, '=')) == NULL)
408 		return (-1);
409 
410 	namlen = value - optstr;
411 
412 	if (*++value == NULL)
413 		return (1);
414 
415 	if (strncmp(optstr, "console", namlen) == 0) {
416 		console_metal_to_hyper(value);
417 		return (0);
418 	}
419 
420 	if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
421 	    (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
422 		char *port = alloca(namlen + 1);
423 
424 		(void) strlcpy(port, optstr, namlen);
425 		return (serial_metal_to_hyper(port, value));
426 	}
427 
428 	return (1);
429 }
430 
431 /*
432  * Convert "name=value" properties for use with a bare metal kernel
433  *
434  * Our main concerns are the console setting and serial port modes.
435  *
436  * Return values:
437  *
438  *    -1:	Unparseable line
439  *    0:	Success
440  *    (n > 0):	A property unimportant to us
441  */
442 static int
443 cvt_hyper_option(char *optstr)
444 {
445 #define	SER_LEN		27	/* strlen("ttyb-mode='115200,8,n,1,-'") */
446 
447 	char ser[SER_LEN];
448 	char *value;
449 
450 	unsigned namlen;
451 
452 	unsigned baud;
453 	char bits, parity, stop;
454 
455 	if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
456 		zfs_boot = 1;
457 		return (0);
458 	}
459 
460 	/*
461 	 * If there's no "=" in the token, it's likely a standalone
462 	 * hypervisor token we don't care about (e.g. "noreboot" or
463 	 * "nosmp") so we ignore it.
464 	 */
465 	if ((value = strchr(optstr, '=')) == NULL)
466 		return (1);
467 
468 	namlen = value - optstr;
469 
470 	if (*++value == NULL)
471 		return (1);
472 
473 	/*
474 	 * Note that we use strncmp against the values because the
475 	 * hypervisor allows setting console parameters for both the
476 	 * console and debugger via the format:
477 	 *
478 	 *   console=cons_dev,debug_dev
479 	 *
480 	 * and we only care about "cons_dev."
481 	 *
482 	 * This also allows us to extract "comN" from hypervisor constructs
483 	 * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
484 	 *
485 	 * Default the console device to "text" if it was "vga" or was
486 	 * unparseable.
487 	 */
488 	if (strncmp(optstr, "console", namlen) == 0) {
489 		/* ignore the "console=hypervisor" option */
490 		if (strcmp(value, "hypervisor") == 0)
491 			return (0);
492 
493 		if (strncmp(value, "com1", 4) == 0)
494 			console_dev = "console=ttya";
495 		else if (strncmp(value, "com2", 4) == 0)
496 			console_dev = "console=ttyb";
497 		else
498 			console_dev = "console=text";
499 	}
500 
501 	/* serial port parameter conversion */
502 
503 	if ((strncmp(optstr, "com1", namlen) == 0) ||
504 	    (strncmp(optstr, "com2", namlen) == 0)) {
505 		unsigned com = COMNAME_TO_COMNUM(optstr);
506 
507 		/*
508 		 * Check if it's "auto" - if so, use the default setting
509 		 * of "9600,8,n,1,-".
510 		 *
511 		 * We can't just assume the serial port will default to
512 		 * "9600,8,n,1" as there could be a directive in bootenv.rc
513 		 * that would set it to some other value and we want the serial
514 		 * parameters to be the same as that used by the hypervisor.
515 		 */
516 		if (strcmp(value, "auto") == 0) {
517 			(void) snprintf(ser, SER_LEN,
518 			    "tty%c-mode='9600,8,n,1,-'", '`' + com);
519 
520 			if (set_serial_rate(com, ser) != 0)
521 				return (-1);
522 
523 			return (0);
524 		}
525 
526 		/*
527 		 * Extract the "B,PS" setting from the com line; ignore other
528 		 * settings like io_base or IRQ.
529 		 */
530 		if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
531 		    &stop) != 4)
532 			return (-1);
533 
534 		/* validate serial port parameters */
535 		if (((stop != '0') && (stop != '1')) ||
536 		    ((bits < '5') && (bits > '8')) ||
537 		    ((parity != 'n') && (parity != 'e') && (parity != 'o')))
538 			return (-1);
539 
540 		/* validate baud rate */
541 		switch (baud) {
542 			case 150:
543 			case 300:
544 			case 600:
545 			case 1200:
546 			case 2400:
547 			case 4800:
548 			case 19200:
549 			case 38400:
550 			case 57600:
551 			case 115200:
552 				break;
553 
554 			default:
555 				return (-1);
556 		}
557 
558 		/*
559 		 * As the hypervisor has no way to denote handshaking in its
560 		 * serial port settings, emit a metal serial port configuration
561 		 * with none as well.
562 		 */
563 		(void) snprintf(ser, SER_LEN, "tty%c-mode='%u,%c,%c,%c,-'",
564 		    '`' + com, baud, bits, parity, stop);
565 
566 		if (set_serial_rate(com, ser) != 0)
567 			return (-1);
568 
569 		return (0);
570 	}
571 
572 	return (1);
573 }
574 
575 /*
576  * Parse a hardware kernel's "kernel$" specifier into parameters we can then
577  * use to construct an appropriate "module$" line that can be used to specify
578  * how to boot the hypervisor's dom0.
579  *
580  * Return 0 on success, non-zero on failure.
581  */
582 static int
583 cvt_metal_kernel(char *kernstr, char **path)
584 {
585 	char *token, *parsestr;
586 
587 	if ((parsestr = get_token(path, kernstr, " \t,")) == NULL)
588 		return (*path == NULL);
589 
590 	/*
591 	 * If the metal kernel specified contains the name of the hypervisor,
592 	 * we're probably trying to convert an entry already setup to run the
593 	 * hypervisor, so error out now.
594 	 */
595 	if (strstr(*path, XEN_MENU) != NULL) {
596 		bam_error(ALREADY_HYPER);
597 		return (-1);
598 	}
599 
600 	/* if the path was the last item on the line, that's OK. */
601 	if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
602 		if (token != NULL)
603 			free(token);
604 		return (0);
605 	}
606 
607 	/* if the next token is "-B" process boot options */
608 	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
609 		free(token);
610 		return (0);
611 	}
612 
613 	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
614 		(void) cvt_metal_option(token);
615 		free(token);
616 	}
617 
618 	if (token != NULL) {
619 		(void) cvt_metal_option(token);
620 		free(token);
621 	}
622 
623 	return (0);
624 }
625 
626 /*
627  * Parse a hypervisor's "kernel$" line into parameters that can be used to
628  * help build an appropriate "kernel$" line for booting a bare metal kernel.
629  *
630  * Return 0 on success, non-zero on failure.
631  */
632 static int
633 cvt_hyper_kernel(char *kernel)
634 {
635 	char *token, *parsestr;
636 
637 	/* eat the kernel path, which should be the first token */
638 	if ((parsestr = get_token(&token, kernel, " \t,")) == NULL)
639 		return (0);
640 
641 	/*
642 	 * If the hypervisor kernel specified lives in the metal kernel
643 	 * directory, we're probably trying to convert an entry already setup
644 	 * to run on bare metal, so error out now.
645 	 */
646 	if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
647 		bam_error(ALREADY_METAL);
648 		(void) free(token);
649 		return (-1);
650 	}
651 
652 	/* if the path was the last item on the line, that's OK. */
653 	free(token);
654 
655 	/* check for kernel options */
656 	while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
657 		(void) cvt_hyper_option(token);
658 		free(token);
659 	}
660 
661 	if (token != NULL) {
662 		(void) cvt_hyper_option(token);
663 		free(token);
664 	}
665 
666 	return (0);
667 }
668 
669 /*
670  * Parse a hypervisor's "module$" line into parameters that can be used to
671  * help build an appropriate "kernel$" line for booting a bare metal kernel.
672  */
673 static void
674 cvt_hyper_module(char *modstr, char **path)
675 {
676 	char *token;
677 	char *parsestr = modstr;
678 
679 	/*
680 	 * If multiple pathnames exist on the module$ line, we just want
681 	 * the last one.
682 	 */
683 	while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
684 		if (*parsestr != '/')
685 			break;
686 
687 		free(*path);
688 	}
689 
690 	/* if the path was the last item on the line, that's OK. */
691 	if ((parsestr == NULL) ||
692 	    ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
693 		if (token != NULL)
694 			free(token);
695 		return;
696 	}
697 
698 	/* check for "-B" option */
699 	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
700 		free(token);
701 		return;
702 	}
703 
704 	/* check for kernel options */
705 	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
706 		(void) cvt_hyper_option(token);
707 		free(token);
708 	}
709 
710 	if (token != NULL) {
711 		(void) cvt_hyper_option(token);
712 		free(token);
713 	}
714 }
715 
716 static void
717 parse_bootenvrc(char *osroot)
718 {
719 #define	LINEBUF_SZ	1024
720 
721 	FILE *fp;
722 	char *rcpath;
723 	char line[LINEBUF_SZ];	/* make line buffer large but not ridiculous */
724 	int len;
725 
726 	assert(osroot);
727 
728 	len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
729 	rcpath = alloca(len);
730 	(void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
731 
732 	/* if we couldn't open the bootenv.rc file, ignore the issue. */
733 	if ((fp = fopen(rcpath, "r")) == NULL) {
734 		BAM_DPRINTF((D_NO_BOOTENVRC, rcpath, strerror(errno)));
735 		return;
736 	}
737 
738 	while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
739 		/* we're only interested in parsing "setprop" directives. */
740 		if (strncmp(line, "setprop", 7) != NULL)
741 			continue;
742 
743 		(void) cvt_bootprop(line);
744 	}
745 
746 	(void) fclose(fp);
747 }
748 
749 error_t
750 cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
751 {
752 	const char *fcn = "cvt_to_hyper()";
753 
754 	line_t *lp;
755 	entry_t *ent;
756 	size_t len, zfslen;
757 
758 	char *osdev;
759 
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;
766 
767 	char *kern_path = NULL;
768 	char *kern_bargs = NULL;
769 
770 	int curdef;
771 	int kp_allocated = 1;
772 	int ret = BAM_ERROR;
773 
774 	assert(osroot);
775 
776 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, extra_args));
777 
778 	/*
779 	 * First just check to verify osroot is a sane directory.
780 	 */
781 	if ((osdev = get_special(osroot)) == NULL) {
782 		bam_error(CANT_FIND_SPECIAL, osroot);
783 		return (BAM_ERROR);
784 	}
785 
786 	free(osdev);
787 
788 	/*
789 	 * While the effect is purely cosmetic, if osroot is "/" don't
790 	 * bother prepending it to any paths as they are constructed to
791 	 * begin with "/" anyway.
792 	 */
793 	if (strcmp(osroot, "/") == 0)
794 		osroot = "";
795 
796 	/*
797 	 * Found the GRUB signature on the target partitions, so now get the
798 	 * default GRUB boot entry number from the menu.lst file
799 	 */
800 	curdef = atoi(mp->curdefault->arg);
801 
802 	/* look for the first line of the matching boot entry */
803 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
804 	    ent = ent->next)
805 		;
806 
807 	/* couldn't find it, so error out */
808 	if (ent == NULL) {
809 		bam_error(CANT_FIND_DEFAULT, curdef);
810 		goto abort;
811 	}
812 
813 	/*
814 	 * We found the proper menu entry, so first we need to process the
815 	 * bootenv.rc file to look for boot options the hypervisor might need
816 	 * passed as kernel start options such as the console device and serial
817 	 * port parameters.
818 	 *
819 	 * If there's no bootenv.rc, it's not an issue.
820 	 */
821 	parse_bootenvrc(osroot);
822 
823 	/*
824 	 * Now process the entry itself.
825 	 */
826 	for (lp = ent->start; lp != NULL; lp = lp->next) {
827 		/*
828 		 * Process important lines from menu.lst boot entry.
829 		 */
830 		if (lp->flags == BAM_TITLE) {
831 			title = alloca(strlen(lp->arg) + 1);
832 			(void) strcpy(title, lp->arg);
833 		} else if (strcmp(lp->cmd, "findroot") == 0) {
834 			findroot = alloca(strlen(lp->arg) + 1);
835 			(void) strcpy(findroot, lp->arg);
836 		} else if (strcmp(lp->cmd, "bootfs") == 0) {
837 			bootfs = alloca(strlen(lp->arg) + 1);
838 			(void) strcpy(bootfs, lp->arg);
839 		} else if (strcmp(lp->cmd, menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
840 			module = alloca(strlen(lp->arg) + 1);
841 			(void) strcpy(module, lp->arg);
842 		} else if ((strcmp(lp->cmd,
843 		    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
844 		    (cvt_metal_kernel(lp->arg, &kern_path) < 0)) {
845 			ret = BAM_NOCHANGE;
846 			goto abort;
847 		}
848 
849 		if (lp == ent->end)
850 			break;
851 	}
852 
853 	/*
854 	 * If findroot, module or kern_path are NULL, boot entry was malformed
855 	 */
856 	if (findroot == NULL) {
857 		bam_error(FINDROOT_NOT_FOUND, curdef);
858 		goto abort;
859 	}
860 
861 	if (module == NULL) {
862 		bam_error(MODULE_NOT_PARSEABLE, curdef);
863 		goto abort;
864 	}
865 
866 	if (kern_path == NULL) {
867 		bam_error(KERNEL_NOT_FOUND, curdef);
868 		goto abort;
869 	}
870 
871 	/* assemble new kernel and module arguments from parsed values */
872 	if (console_dev != NULL) {
873 		kern_bargs = s_strdup(console_dev);
874 
875 		if (serial_config[0] != NULL)
876 			kern_bargs = append_str(kern_bargs, serial_config[0],
877 			    " ");
878 
879 		if (serial_config[1] != NULL)
880 			kern_bargs = append_str(kern_bargs, serial_config[1],
881 			    " ");
882 	}
883 
884 	if ((extra_args != NULL) && (*extra_args != NULL))
885 		kern_bargs = append_str(kern_bargs, extra_args, " ");
886 
887 	len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
888 	    WHITESPC(1) + 1;
889 
890 	kernel = alloca(len);
891 
892 	if ((kern_bargs != NULL) && (*kern_bargs != NULL)) {
893 		(void) snprintf(kernel, len, "%s%s %s", osroot, XEN_MENU,
894 		    kern_bargs);
895 		free(kern_bargs);
896 	} else {
897 		(void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
898 	}
899 
900 	/*
901 	 * Change the kernel directory from the metal version to that needed for
902 	 * the hypervisor.  Convert either "direct boot" path to the default
903 	 * path.
904 	 */
905 	if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
906 	    (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
907 		kern_path = HYPERVISOR_KERNEL;
908 		kp_allocated = 0;
909 	} else {
910 		kern_path = modify_path(kern_path, METAL_KERNEL_DIR,
911 		    HYPER_KERNEL_DIR);
912 	}
913 
914 	/*
915 	 * We need to allocate space for the kernel path (twice) plus an
916 	 * intervening space, possibly the ZFS boot string, and NULL,
917 	 * of course.
918 	 */
919 	len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
920 	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
921 
922 	mod_kernel = alloca(len + zfslen);
923 
924 	(void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
925 
926 	if (kp_allocated)
927 		free(kern_path);
928 
929 	if (zfs_boot) {
930 		char *zfsstr = alloca(zfslen + 1);
931 
932 		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
933 		(void) strcat(mod_kernel, zfsstr);
934 	}
935 
936 	/* shut off warning messages from the entry line parser */
937 	if (ent->flags & BAM_ENTRY_BOOTADM)
938 		ent->flags &= ~BAM_ENTRY_BOOTADM;
939 
940 	BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
941 	BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, mod_kernel));
942 
943 	/*
944 	 * Now try to delete the current default entry from the menu and add
945 	 * the new hypervisor entry with the parameters we've setup.
946 	 */
947 	if (delete_boot_entry(mp, curdef, DBE_QUIET) != BAM_SUCCESS)
948 		bam_print(NEW_BOOT_ENTRY, title);
949 
950 	curdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
951 	    module, bootfs);
952 
953 	/*
954 	 * If we successfully created the new entry, set the default boot
955 	 * entry to that entry and let the caller know the new menu should
956 	 * be written out.
957 	 */
958 	if (curdef != BAM_ERROR)
959 		return (set_global(mp, menu_cmds[DEFAULT_CMD], curdef));
960 
961 	return (BAM_ERROR);
962 
963 abort:
964 	if ((kp_allocated) && (kern_path != NULL))
965 		free(kern_path);
966 
967 	if (ret != BAM_NOCHANGE)
968 		bam_error(HYPER_ABORT, ((*osroot == NULL) ? "/" : osroot));
969 
970 	return (ret);
971 }
972 
973 /*ARGSUSED*/
974 error_t
975 cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
976 {
977 	const char *fcn = "cvt_to_metal()";
978 
979 	line_t *lp;
980 	entry_t *ent;
981 	size_t len, zfslen;
982 
983 	char *delim = ", ";
984 	char *osdev;
985 
986 	char *title = NULL;
987 	char *findroot = NULL;
988 	char *bootfs = NULL;
989 	char *kernel = NULL;
990 	char *module = NULL;
991 
992 	char *barchive_path = DIRECT_BOOT_ARCHIVE;
993 	char *kern_path = NULL;
994 
995 	int curdef;
996 	int emit_bflag = 1;
997 	int ret = BAM_ERROR;
998 
999 	assert(osroot);
1000 
1001 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, ""));
1002 
1003 	/*
1004 	 * First just check to verify osroot is a sane directory.
1005 	 */
1006 	if ((osdev = get_special(osroot)) == NULL) {
1007 		bam_error(CANT_FIND_SPECIAL, osroot);
1008 		return (BAM_ERROR);
1009 	}
1010 
1011 	free(osdev);
1012 
1013 	/*
1014 	 * Found the GRUB signature on the target partitions, so now get the
1015 	 * default GRUB boot entry number from the menu.lst file
1016 	 */
1017 	curdef = atoi(mp->curdefault->arg);
1018 
1019 	/* look for the first line of the matching boot entry */
1020 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
1021 	    ent = ent->next)
1022 		;
1023 
1024 	/* couldn't find it, so error out */
1025 	if (ent == NULL) {
1026 		bam_error(CANT_FIND_DEFAULT, curdef);
1027 		goto abort;
1028 	}
1029 
1030 	/*
1031 	 * Now process the entry itself.
1032 	 */
1033 	for (lp = ent->start; lp != NULL; lp = lp->next) {
1034 		/*
1035 		 * Process important lines from menu.lst boot entry.
1036 		 */
1037 		if (lp->flags == BAM_TITLE) {
1038 			title = alloca(strlen(lp->arg) + 1);
1039 			(void) strcpy(title, lp->arg);
1040 		} else if (strcmp(lp->cmd, "findroot") == 0) {
1041 			findroot = alloca(strlen(lp->arg) + 1);
1042 			(void) strcpy(findroot, lp->arg);
1043 		} else if (strcmp(lp->cmd, "bootfs") == 0) {
1044 			bootfs = alloca(strlen(lp->arg) + 1);
1045 			(void) strcpy(bootfs, lp->arg);
1046 		} else if (strcmp(lp->cmd, menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
1047 			if (strstr(lp->arg, "boot_archive") == NULL) {
1048 				module = alloca(strlen(lp->arg) + 1);
1049 				(void) strcpy(module, lp->arg);
1050 				cvt_hyper_module(module, &kern_path);
1051 			} else {
1052 				barchive_path = alloca(strlen(lp->arg) + 1);
1053 				(void) strcpy(barchive_path, lp->arg);
1054 			}
1055 		} else if ((strcmp(lp->cmd,
1056 		    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
1057 		    (cvt_hyper_kernel(lp->arg) < 0)) {
1058 			ret = BAM_NOCHANGE;
1059 			goto abort;
1060 		}
1061 
1062 		if (lp == ent->end)
1063 			break;
1064 	}
1065 
1066 	/*
1067 	 * If findroot, module or kern_path are NULL, boot entry was malformed
1068 	 */
1069 	if (findroot == NULL) {
1070 		bam_error(FINDROOT_NOT_FOUND, curdef);
1071 		goto abort;
1072 	}
1073 
1074 	if (module == NULL) {
1075 		bam_error(MODULE_NOT_PARSEABLE, curdef);
1076 		goto abort;
1077 	}
1078 
1079 	if (kern_path == NULL) {
1080 		bam_error(KERNEL_NOT_FOUND, curdef);
1081 		goto abort;
1082 	}
1083 
1084 	/*
1085 	 * Assemble new kernel and module arguments from parsed values.
1086 	 *
1087 	 * First, change the kernel directory from the hypervisor version to
1088 	 * that needed for a metal kernel.
1089 	 */
1090 	kern_path = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
1091 
1092 	/* allocate initial space for the kernel path */
1093 	len = strlen(kern_path) + 1;
1094 	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
1095 
1096 	if ((kernel = malloc(len + zfslen)) == NULL) {
1097 		free(kern_path);
1098 		bam_error(NO_MEM, len);
1099 		goto abort;
1100 	}
1101 
1102 	(void) snprintf(kernel, len, "%s", kern_path);
1103 	free(kern_path);
1104 
1105 	if (zfs_boot) {
1106 		char *zfsstr = alloca(zfslen + 1);
1107 
1108 		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
1109 		(void) strcat(kernel, zfsstr);
1110 		emit_bflag = 0;
1111 	}
1112 
1113 	if (console_dev != NULL) {
1114 		if (emit_bflag) {
1115 			kernel = append_str(kernel, BFLAG, " ");
1116 			kernel = append_str(kernel, console_dev, " ");
1117 			emit_bflag = 0;
1118 		} else {
1119 			kernel = append_str(kernel, console_dev, ", ");
1120 		}
1121 	}
1122 
1123 	/*
1124 	 * We have to do some strange processing here because the
1125 	 * hypervisor's serial ports default to "9600,8,n,1,-" if
1126 	 * "comX=auto" is specified, or to "auto" if nothing is
1127 	 * specified.
1128 	 *
1129 	 * Since there could be entries in the bootenv.rc file that
1130 	 * set the serial port to some other setting, when converting
1131 	 * a hypervisor entry to a metal entry we must force the
1132 	 * serial ports to their defaults.
1133 	 */
1134 
1135 	if (emit_bflag) {
1136 		kernel = append_str(kernel, BFLAG, " ");
1137 		delim = " ";
1138 		emit_bflag = 0;
1139 	}
1140 
1141 	if (serial_config[0] != NULL)
1142 		kernel = append_str(kernel, serial_config[0], delim);
1143 	else
1144 		kernel = append_str(kernel, "ttya-mode='9600,8,n,1,-'", delim);
1145 
1146 	if (serial_config[1] != NULL)
1147 		kernel = append_str(kernel, serial_config[1], ", ");
1148 	else
1149 		kernel = append_str(kernel, "ttyb-mode='9600,8,n,1,-'", ", ");
1150 
1151 	/* shut off warning messages from the entry line parser */
1152 	if (ent->flags & BAM_ENTRY_BOOTADM)
1153 		ent->flags &= ~BAM_ENTRY_BOOTADM;
1154 
1155 	BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
1156 	BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, module));
1157 
1158 	/*
1159 	 * Now try to delete the current default entry from the menu and add
1160 	 * the new hypervisor entry with the parameters we've setup.
1161 	 */
1162 	if (delete_boot_entry(mp, curdef, DBE_QUIET) != BAM_SUCCESS)
1163 		bam_print(NEW_BOOT_ENTRY, title);
1164 
1165 	curdef = add_boot_entry(mp, title, findroot, kernel, NULL,
1166 	    barchive_path, bootfs);
1167 
1168 	/*
1169 	 * If we successfully created the new entry, set the default boot
1170 	 * entry to that entry and let the caller know the new menu should
1171 	 * be written out.
1172 	 */
1173 	if (curdef != BAM_ERROR)
1174 		return (set_global(mp, menu_cmds[DEFAULT_CMD], curdef));
1175 
1176 	return (BAM_ERROR);
1177 
1178 abort:
1179 	if (ret != BAM_NOCHANGE)
1180 		bam_error(METAL_ABORT, osroot);
1181 
1182 	return (ret);
1183 }
1184