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