xref: /titanic_44/usr/src/cmd/boot/bootadm/bootadm_hyper.c (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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 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 = alloca(namlen + 1);
351 
352 		(void) strlcpy(port, optstr, namlen + 1);
353 		return (serial_metal_to_hyper(port, value));
354 	}
355 
356 	return (1);
357 }
358 
359 /*
360  * Convert "name=value" properties for use with a bare metal kernel
361  *
362  * Our main concerns are the console setting and serial port modes.
363  *
364  * Return values:
365  *
366  *    -1:	Unparseable line
367  *    0:	Success
368  *    (n > 0):	A property unimportant to us
369  */
370 static int
371 cvt_hyper_option(char *optstr)
372 {
373 #define	SER_LEN		15	/* strlen("115200,8,n,1,-") + 1 */
374 
375 	char ser[SER_LEN];
376 	char *value;
377 
378 	unsigned namlen;
379 
380 	unsigned baud;
381 	char bits, parity, stop;
382 
383 	if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
384 		zfs_boot = 1;
385 		return (0);
386 	}
387 
388 	/*
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.
392 	 */
393 	if ((value = strchr(optstr, '=')) == NULL)
394 		return (1);
395 
396 	namlen = value - optstr;
397 
398 	if (*++value == NULL)
399 		return (1);
400 
401 	/*
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:
405 	 *
406 	 *   console=cons_dev,debug_dev
407 	 *
408 	 * and we only care about "cons_dev."
409 	 *
410 	 * This also allows us to extract "comN" from hypervisor constructs
411 	 * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
412 	 *
413 	 * Default the console device to "text" if it was "vga" or was
414 	 * unparseable.
415 	 */
416 	if (strncmp(optstr, "console", namlen) == 0) {
417 		/* ignore the "console=hypervisor" option */
418 		if (strcmp(value, "hypervisor") == 0)
419 			return (0);
420 
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";
427 	}
428 
429 	/* serial port parameter conversion */
430 
431 	if ((strncmp(optstr, "com1", namlen) == 0) ||
432 	    (strncmp(optstr, "com2", namlen) == 0)) {
433 		unsigned com = COMNAME_TO_COMNUM(optstr);
434 
435 		/*
436 		 * Check if it's "auto" - if so, use the default setting
437 		 * of "9600,8,n,1,-".
438 		 *
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.
443 		 */
444 		if (strcmp(value, "auto") == 0) {
445 			(void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
446 		} else {
447 			/*
448 			 * Extract the "B,PS" setting from the com line; ignore
449 			 * other settings like io_base or IRQ.
450 			 */
451 			if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
452 			    &stop) != 4)
453 				return (-1);
454 
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);
461 
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;
475 
476 				default:
477 					return (-1);
478 			}
479 
480 			/*
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.
484 			 */
485 			(void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
486 			    bits, parity, stop);
487 		}
488 
489 		if (set_serial_rate(com, ser) != 0)
490 			return (-1);
491 
492 		return (0);
493 	}
494 
495 	return (1);
496 }
497 
498 /*
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.
502  *
503  * Return values:
504  *
505  *	-1: error parsing kernel path
506  *	 0: success
507  *	 1: kernel already a hypervisor kernel
508  */
509 static int
510 cvt_metal_kernel(char *kernstr, char **path)
511 {
512 	char *token, *parsestr;
513 
514 	parsestr = get_token(path, kernstr, " \t,");
515 	if (*path == NULL)
516 		return (-1);
517 
518 	/*
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.
522 	 */
523 	if (strstr(*path, XEN_MENU) != NULL) {
524 		bam_error(ALREADY_HYPER);
525 		free(*path);
526 		*path = NULL;
527 		return (1);
528 	}
529 
530 	/* if the path was the last item on the line, that's OK. */
531 	if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
532 		if (token != NULL)
533 			free(token);
534 		return (0);
535 	}
536 
537 	/* if the next token is "-B" process boot options */
538 	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
539 		free(token);
540 		return (0);
541 	}
542 
543 	free(token);
544 
545 	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
546 		(void) cvt_metal_option(token);
547 		free(token);
548 	}
549 
550 	if (token != NULL) {
551 		(void) cvt_metal_option(token);
552 		free(token);
553 	}
554 
555 	return (0);
556 }
557 
558 /*
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.
561  *
562  * Return 0 on success, non-zero on failure.
563  */
564 static int
565 cvt_hyper_kernel(char *kernel)
566 {
567 	char *token, *parsestr;
568 
569 	parsestr = get_token(&token, kernel, " \t,");
570 
571 	if (token == NULL)
572 		return (-1);
573 
574 	/*
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.
578 	 */
579 	if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
580 		bam_error(ALREADY_METAL);
581 		free(token);
582 		return (-1);
583 	}
584 
585 	free(token);
586 
587 	/* check for kernel options */
588 	while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
589 		(void) cvt_hyper_option(token);
590 		free(token);
591 	}
592 
593 	if (token != NULL) {
594 		(void) cvt_hyper_option(token);
595 		free(token);
596 	}
597 
598 	return (0);
599 }
600 
601 /*
602  * Parse a hypervisor's "module$" line into parameters that can be used to
603  * help build an appropriate "kernel$" line for booting a bare metal kernel.
604  */
605 static void
606 cvt_hyper_module(char *modstr, char **path)
607 {
608 	char *token = NULL;
609 	char *parsestr = modstr;
610 
611 	/*
612 	 * If multiple pathnames exist on the module$ line, we just want
613 	 * the last one.
614 	 */
615 	while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
616 		if (*parsestr != '/')
617 			break;
618 		else
619 			free(*path);
620 	}
621 
622 	/* if the path was the last item on the line, that's OK. */
623 	if ((parsestr == NULL) ||
624 	    ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
625 		if (token != NULL)
626 			free(token);
627 		return;
628 	}
629 
630 	if (token == NULL)
631 		return;
632 
633 	/* check for "-B" option */
634 	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
635 		free(token);
636 		return;
637 	}
638 
639 	free(token);
640 
641 	/* check for kernel options */
642 	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
643 		(void) cvt_hyper_option(token);
644 		free(token);
645 	}
646 
647 	if (token != NULL) {
648 		(void) cvt_hyper_option(token);
649 		free(token);
650 	}
651 }
652 
653 static void
654 parse_bootenvrc(char *osroot)
655 {
656 #define	LINEBUF_SZ	1024
657 
658 	FILE *fp;
659 	char *rcpath;
660 	char line[LINEBUF_SZ];	/* make line buffer large but not ridiculous */
661 	int len;
662 
663 	assert(osroot);
664 
665 	len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
666 	rcpath = alloca(len);
667 
668 	(void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
669 
670 	/* if we couldn't open the bootenv.rc file, ignore the issue. */
671 	if ((fp = fopen(rcpath, "r")) == NULL) {
672 		BAM_DPRINTF((D_NO_BOOTENVRC, rcpath, strerror(errno)));
673 		return;
674 	}
675 
676 	while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
677 		char *parsestr, *token;
678 		int port = 0;
679 
680 		/* we're only interested in parsing "setprop" directives. */
681 		if (strncmp(line, "setprop", 7) != NULL)
682 			continue;
683 
684 		/* eat initial "setprop" */
685 		if ((parsestr = get_token(&token, line, " \t")) == NULL) {
686 			if (token != NULL)
687 				free(token);
688 
689 			continue;
690 		}
691 
692 		if (strcmp(token, "setprop") != 0) {
693 			free(token);
694 			continue;
695 		}
696 
697 		free(token);
698 
699 		/* get property name */
700 		if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
701 			if (token != NULL)
702 				free(token);
703 
704 			continue;
705 		}
706 
707 		if (strcmp(token, "console") == 0) {
708 			free(token);
709 
710 			/* get console property value */
711 			parsestr = get_token(&token, parsestr, " \t");
712 			if (token == NULL)
713 				continue;
714 
715 			if (bootenv_rc_console != NULL)
716 				free(bootenv_rc_console);
717 
718 			bootenv_rc_console = s_strdup(token);
719 			continue;
720 		}
721 
722 		/* check if it's a serial port setting */
723 		if (strcmp(token, "ttya-mode") == 0) {
724 			free(token);
725 			port = 0;
726 		} else if (strcmp(token, "ttyb-mode") == 0) {
727 			free(token);
728 			port = 1;
729 		} else {
730 			/* nope, so check the next line */
731 			free(token);
732 			continue;
733 		}
734 
735 		/* get serial port setting */
736 		parsestr = get_token(&token, parsestr, " \t");
737 
738 		if (token == NULL)
739 			continue;
740 
741 		if (bootenv_rc_serial[port] != NULL)
742 			free(bootenv_rc_serial[port]);
743 
744 		bootenv_rc_serial[port] = s_strdup(token);
745 		free(token);
746 	}
747 
748 	(void) fclose(fp);
749 }
750 
751 error_t
752 cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
753 {
754 	const char *fcn = "cvt_to_hyper()";
755 
756 	line_t *lp;
757 	entry_t *ent;
758 	size_t len, zfslen;
759 
760 	char *newstr;
761 	char *osdev;
762 
763 	char *title = NULL;
764 	char *findroot = NULL;
765 	char *bootfs = NULL;
766 	char *kernel = NULL;
767 	char *mod_kernel = NULL;
768 	char *module = NULL;
769 
770 	char *kern_path = NULL;
771 	char *kern_bargs = NULL;
772 
773 	int curdef, newdef;
774 	int kp_allocated = 0;
775 	int ret = BAM_ERROR;
776 
777 	assert(osroot);
778 
779 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, extra_args));
780 
781 	/*
782 	 * First just check to verify osroot is a sane directory.
783 	 */
784 	if ((osdev = get_special(osroot)) == NULL) {
785 		bam_error(CANT_FIND_SPECIAL, osroot);
786 		return (BAM_ERROR);
787 	}
788 
789 	free(osdev);
790 
791 	/*
792 	 * While the effect is purely cosmetic, if osroot is "/" don't
793 	 * bother prepending it to any paths as they are constructed to
794 	 * begin with "/" anyway.
795 	 */
796 	if (strcmp(osroot, "/") == 0)
797 		osroot = "";
798 
799 	/*
800 	 * Found the GRUB signature on the target partitions, so now get the
801 	 * default GRUB boot entry number from the menu.lst file
802 	 */
803 	curdef = atoi(mp->curdefault->arg);
804 
805 	/* look for the first line of the matching boot entry */
806 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
807 	    ent = ent->next)
808 		;
809 
810 	/* couldn't find it, so error out */
811 	if (ent == NULL) {
812 		bam_error(CANT_FIND_DEFAULT, curdef);
813 		goto abort;
814 	}
815 
816 	/*
817 	 * We found the proper menu entry, so first we need to process the
818 	 * bootenv.rc file to look for boot options the hypervisor might need
819 	 * passed as kernel start options such as the console device and serial
820 	 * port parameters.
821 	 *
822 	 * If there's no bootenv.rc, it's not an issue.
823 	 */
824 	parse_bootenvrc(osroot);
825 
826 	if (bootenv_rc_console != NULL)
827 		console_metal_to_hyper(bootenv_rc_console);
828 
829 	if (bootenv_rc_serial[0] != NULL)
830 		(void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);
831 
832 	if (bootenv_rc_serial[1] != NULL)
833 		(void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);
834 
835 	/*
836 	 * Now process the entry itself.
837 	 */
838 	for (lp = ent->start; lp != NULL; lp = lp->next) {
839 		/*
840 		 * Process important lines from menu.lst boot entry.
841 		 */
842 		if (lp->flags == BAM_TITLE) {
843 			title = alloca(strlen(lp->arg) + 1);
844 			(void) strcpy(title, lp->arg);
845 		} else if (lp->cmd != NULL) {
846 			if (strcmp(lp->cmd, "findroot") == 0) {
847 				findroot = alloca(strlen(lp->arg) + 1);
848 				(void) strcpy(findroot, lp->arg);
849 			} else if (strcmp(lp->cmd, "bootfs") == 0) {
850 				bootfs = alloca(strlen(lp->arg) + 1);
851 				(void) strcpy(bootfs, lp->arg);
852 			} else if (strcmp(lp->cmd,
853 			    menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
854 				module = alloca(strlen(lp->arg) + 1);
855 				(void) strcpy(module, lp->arg);
856 			} else if ((strcmp(lp->cmd,
857 			    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
858 			    (ret = cvt_metal_kernel(lp->arg,
859 			    &kern_path)) != 0) {
860 				if (ret < 0) {
861 					ret = BAM_ERROR;
862 					bam_error(KERNEL_NOT_PARSEABLE, curdef);
863 				} else
864 					ret = BAM_NOCHANGE;
865 
866 				goto abort;
867 			}
868 		}
869 
870 		if (lp == ent->end)
871 			break;
872 	}
873 
874 	/*
875 	 * If findroot, module or kern_path are NULL, the boot entry is
876 	 * malformed.
877 	 */
878 	if (findroot == NULL) {
879 		bam_error(FINDROOT_NOT_FOUND, curdef);
880 		goto abort;
881 	}
882 
883 	if (module == NULL) {
884 		bam_error(MODULE_NOT_PARSEABLE, curdef);
885 		goto abort;
886 	}
887 
888 	if (kern_path == NULL) {
889 		bam_error(KERNEL_NOT_FOUND, curdef);
890 		goto abort;
891 	}
892 
893 	/* assemble new kernel and module arguments from parsed values */
894 	if (console_dev != NULL) {
895 		kern_bargs = s_strdup(console_dev);
896 
897 		if (serial_config[0] != NULL) {
898 			newstr = append_str(kern_bargs, serial_config[0], " ");
899 			free(kern_bargs);
900 			kern_bargs = newstr;
901 		}
902 
903 		if (serial_config[1] != NULL) {
904 			newstr = append_str(kern_bargs, serial_config[1], " ");
905 			free(kern_bargs);
906 			kern_bargs = newstr;
907 		}
908 	}
909 
910 	if ((extra_args != NULL) && (*extra_args != NULL)) {
911 		newstr = append_str(kern_bargs, extra_args, " ");
912 		free(kern_bargs);
913 		kern_bargs = newstr;
914 	}
915 
916 	len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
917 	    WHITESPC(1) + 1;
918 
919 	kernel = alloca(len);
920 
921 	if (kern_bargs != NULL) {
922 		if (*kern_bargs != NULL)
923 			(void) snprintf(kernel, len, "%s%s %s", osroot,
924 			    XEN_MENU, kern_bargs);
925 
926 		free(kern_bargs);
927 	} else {
928 		(void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
929 	}
930 
931 	/*
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.
935 	 */
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;
945 	}
946 
947 	/*
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.
951 	 */
952 	len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
953 	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
954 
955 	mod_kernel = alloca(len + zfslen);
956 	(void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
957 
958 	if (kp_allocated)
959 		free(kern_path);
960 
961 	if (zfs_boot) {
962 		char *zfsstr = alloca(zfslen + 1);
963 
964 		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
965 		(void) strcat(mod_kernel, zfsstr);
966 	}
967 
968 	/* shut off warning messages from the entry line parser */
969 	if (ent->flags & BAM_ENTRY_BOOTADM)
970 		ent->flags &= ~BAM_ENTRY_BOOTADM;
971 
972 	BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
973 	BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, mod_kernel));
974 
975 	if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
976 	    module, bootfs)) == BAM_ERROR)
977 		return (newdef);
978 
979 	/*
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.
982 	 */
983 	if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
984 		newdef--;
985 	else
986 		bam_print(NEW_BOOT_ENTRY, title);
987 
988 	/*
989 	 * If we successfully created the new entry, set the default boot
990 	 * entry to that entry and let the caller know the new menu should
991 	 * be written out.
992 	 */
993 	return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
994 
995 abort:
996 	if (ret != BAM_NOCHANGE)
997 		bam_error(HYPER_ABORT, ((*osroot == NULL) ? "/" : osroot));
998 
999 	return (ret);
1000 }
1001 
1002 /*ARGSUSED*/
1003 error_t
1004 cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
1005 {
1006 	const char *fcn = "cvt_to_metal()";
1007 
1008 	line_t *lp;
1009 	entry_t *ent;
1010 	size_t len, zfslen;
1011 
1012 	char *delim = ",";
1013 	char *newstr;
1014 	char *osdev;
1015 
1016 	char *title = NULL;
1017 	char *findroot = NULL;
1018 	char *bootfs = NULL;
1019 	char *kernel = NULL;
1020 	char *module = NULL;
1021 
1022 	char *barchive_path = DIRECT_BOOT_ARCHIVE;
1023 	char *kern_path = NULL;
1024 
1025 	int curdef, newdef;
1026 	int emit_bflag = 1;
1027 	int ret = BAM_ERROR;
1028 
1029 	assert(osroot);
1030 
1031 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, ""));
1032 
1033 	/*
1034 	 * First just check to verify osroot is a sane directory.
1035 	 */
1036 	if ((osdev = get_special(osroot)) == NULL) {
1037 		bam_error(CANT_FIND_SPECIAL, osroot);
1038 		return (BAM_ERROR);
1039 	}
1040 
1041 	free(osdev);
1042 
1043 	/*
1044 	 * Found the GRUB signature on the target partitions, so now get the
1045 	 * default GRUB boot entry number from the menu.lst file
1046 	 */
1047 	curdef = atoi(mp->curdefault->arg);
1048 
1049 	/* look for the first line of the matching boot entry */
1050 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
1051 	    ent = ent->next)
1052 		;
1053 
1054 	/* couldn't find it, so error out */
1055 	if (ent == NULL) {
1056 		bam_error(CANT_FIND_DEFAULT, curdef);
1057 		goto abort;
1058 	}
1059 
1060 	/*
1061 	 * Now process the entry itself.
1062 	 */
1063 	for (lp = ent->start; lp != NULL; lp = lp->next) {
1064 		/*
1065 		 * Process important lines from menu.lst boot entry.
1066 		 */
1067 		if (lp->flags == BAM_TITLE) {
1068 			title = alloca(strlen(lp->arg) + 1);
1069 			(void) strcpy(title, lp->arg);
1070 		} else if (lp->cmd != NULL) {
1071 			if (strcmp(lp->cmd, "findroot") == 0) {
1072 				findroot = alloca(strlen(lp->arg) + 1);
1073 				(void) strcpy(findroot, lp->arg);
1074 			} else if (strcmp(lp->cmd, "bootfs") == 0) {
1075 				bootfs = alloca(strlen(lp->arg) + 1);
1076 				(void) strcpy(bootfs, lp->arg);
1077 			} else if (strcmp(lp->cmd,
1078 			    menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
1079 				if (strstr(lp->arg, "boot_archive") == NULL) {
1080 					module = alloca(strlen(lp->arg) + 1);
1081 					(void) strcpy(module, lp->arg);
1082 					cvt_hyper_module(module, &kern_path);
1083 				} else {
1084 					barchive_path =
1085 					    alloca(strlen(lp->arg) + 1);
1086 					(void) strcpy(barchive_path, lp->arg);
1087 				}
1088 			} else if ((strcmp(lp->cmd,
1089 			    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
1090 			    (cvt_hyper_kernel(lp->arg) < 0)) {
1091 				ret = BAM_NOCHANGE;
1092 				goto abort;
1093 			}
1094 		}
1095 
1096 		if (lp == ent->end)
1097 			break;
1098 	}
1099 
1100 	/*
1101 	 * If findroot, module or kern_path are NULL, the boot entry is
1102 	 * malformed.
1103 	 */
1104 	if (findroot == NULL) {
1105 		bam_error(FINDROOT_NOT_FOUND, curdef);
1106 		goto abort;
1107 	}
1108 
1109 	if (module == NULL) {
1110 		bam_error(MODULE_NOT_PARSEABLE, curdef);
1111 		goto abort;
1112 	}
1113 
1114 	if (kern_path == NULL) {
1115 		bam_error(KERNEL_NOT_FOUND, curdef);
1116 		goto abort;
1117 	}
1118 
1119 	/*
1120 	 * Assemble new kernel and module arguments from parsed values.
1121 	 *
1122 	 * First, change the kernel directory from the hypervisor version to
1123 	 * that needed for a metal kernel.
1124 	 */
1125 	newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
1126 	free(kern_path);
1127 	kern_path = newstr;
1128 
1129 	/* allocate initial space for the kernel path */
1130 	len = strlen(kern_path) + 1;
1131 	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
1132 
1133 	if ((kernel = malloc(len + zfslen)) == NULL) {
1134 		free(kern_path);
1135 		bam_error(NO_MEM, len + zfslen);
1136 		bam_exit(1);
1137 	}
1138 
1139 	(void) snprintf(kernel, len, "%s", kern_path);
1140 	free(kern_path);
1141 
1142 	if (zfs_boot) {
1143 		char *zfsstr = alloca(zfslen + 1);
1144 
1145 		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
1146 		(void) strcat(kernel, zfsstr);
1147 		emit_bflag = 0;
1148 	}
1149 
1150 	/*
1151 	 * Process the bootenv.rc file to look for boot options that would be
1152 	 * the same as what the hypervisor had manually set, as we need not set
1153 	 * those explicitly.
1154 	 *
1155 	 * If there's no bootenv.rc, it's not an issue.
1156 	 */
1157 	parse_bootenvrc(osroot);
1158 
1159 	/*
1160 	 * Don't emit a console setting if it's the same as what would be
1161 	 * set by bootenv.rc.
1162 	 */
1163 	if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
1164 	    (strcmp(console_dev, bootenv_rc_console) != 0))) {
1165 		if (emit_bflag) {
1166 			newstr = append_str(kernel, BFLAG, " ");
1167 			free(kernel);
1168 			kernel = append_str(newstr, "console=", " ");
1169 			free(newstr);
1170 			newstr = append_str(kernel, console_dev, "");
1171 			free(kernel);
1172 			kernel = newstr;
1173 			emit_bflag = 0;
1174 		} else {
1175 			newstr = append_str(kernel, "console=", ",");
1176 			free(kernel);
1177 			kernel = append_str(newstr, console_dev, "");
1178 			free(newstr);
1179 		}
1180 	}
1181 
1182 	/*
1183 	 * We have to do some strange processing here because the hypervisor's
1184 	 * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
1185 	 * or to "auto" if nothing is specified.
1186 	 *
1187 	 * This could result in a serial mode setting string being added when
1188 	 * it would otherwise not be needed, but it's better to play it safe.
1189 	 */
1190 	if (emit_bflag) {
1191 		newstr = append_str(kernel, BFLAG, " ");
1192 		free(kernel);
1193 		kernel = newstr;
1194 		delim = " ";
1195 		emit_bflag = 0;
1196 	}
1197 
1198 	if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
1199 	    (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
1200 		newstr = append_str(kernel, "ttya-mode='", delim);
1201 		free(kernel);
1202 
1203 		/*
1204 		 * Pass the serial configuration as the delimiter to
1205 		 * append_str() as it will be inserted between the current
1206 		 * string and the string we're appending, in this case the
1207 		 * closing single quote.
1208 		 */
1209 		kernel = append_str(newstr, "'", serial_config[0]);
1210 		free(newstr);
1211 		delim = ",";
1212 	}
1213 
1214 	if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
1215 	    (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
1216 		newstr = append_str(kernel, "ttyb-mode='", delim);
1217 		free(kernel);
1218 
1219 		/*
1220 		 * Pass the serial configuration as the delimiter to
1221 		 * append_str() as it will be inserted between the current
1222 		 * string and the string we're appending, in this case the
1223 		 * closing single quote.
1224 		 */
1225 		kernel = append_str(newstr, "'", serial_config[1]);
1226 		free(newstr);
1227 		delim = ",";
1228 	}
1229 
1230 	/* shut off warning messages from the entry line parser */
1231 	if (ent->flags & BAM_ENTRY_BOOTADM)
1232 		ent->flags &= ~BAM_ENTRY_BOOTADM;
1233 
1234 	BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
1235 	BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, module));
1236 
1237 	if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
1238 	    barchive_path, bootfs)) == BAM_ERROR) {
1239 		free(kernel);
1240 		return (newdef);
1241 	}
1242 
1243 	/*
1244 	 * Now try to delete the current default entry from the menu and add
1245 	 * the new hypervisor entry with the parameters we've setup.
1246 	 */
1247 	if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
1248 		newdef--;
1249 	else
1250 		bam_print(NEW_BOOT_ENTRY, title);
1251 
1252 	free(kernel);
1253 
1254 	/*
1255 	 * If we successfully created the new entry, set the default boot
1256 	 * entry to that entry and let the caller know the new menu should
1257 	 * be written out.
1258 	 */
1259 	return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
1260 
1261 abort:
1262 	if (ret != BAM_NOCHANGE)
1263 		bam_error(METAL_ABORT, osroot);
1264 
1265 	return (ret);
1266 }
1267