summaryrefslogtreecommitdiff
path: root/usr/src/cmd/boot/bootadm/bootadm_hyper.c
blob: f88960383e385936985e4601d9ecddaa4993d287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2016 Toomas Soome <tsoome@me.com>
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <ctype.h>
#include <sys/types.h>

#include "bootadm.h"

#define	HYPER_KERNEL_DIR		"/platform/i86xpv/kernel"
#define	METAL_KERNEL_DIR		"/platform/i86pc/kernel"

#define	BOOTRC_FILE			"/boot/solaris/bootenv.rc"
#define	ZFS_BOOTSTR			"$ZFS-BOOTFS"

#define	BFLAG				"-B"
#define	DEFAULT_SERIAL			"9600,8,n,1"

#define	TTYXMODE_TO_COMNUM(ttyxmode)	((int)(*((ttyxmode) + 3) - '`'))
#define	COMNAME_TO_COMNUM(comname)	((int)(*((comname) + 3) - '0'))

#define	WHITESPC(x)			(x)

static char *serial_config[2] = { NULL, NULL };
static char *console_dev = NULL;

static char *bootenv_rc_serial[2] = { NULL, NULL };
static char *bootenv_rc_console = NULL;

static unsigned zfs_boot = 0;

/*
 * Append the string pointed to by "str" to the string pointed to by "orig"
 * adding the delimeter "delim" in between.
 *
 * Return a pointer to the new string or NULL, if we were passed a bad string.
 */
static char *
append_str(char *orig, char *str, char *delim)
{
	char *newstr;
	int len;

	if ((str == NULL) || (delim == NULL))
		return (NULL);

	if ((orig == NULL) || (*orig == '\0')) {
		/*
		 * Return a pointer to a copy of the path so a caller can
		 * always rely upon being able to free() a returned pointer.
		 */
		return (s_strdup(str));
	}

	len = strlen(orig) + strlen(str) + strlen(delim) + 1;
	if ((newstr = malloc(len)) == NULL) {
		bam_error(_("could not allocate memory: size = %u\n"), len);
		bam_exit(1);
	}

	(void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
	return (newstr);
}

/*
 * Replace the substring "old_str" in a path with the substring "new_str"
 *
 * Return a pointer to the modified string.
 */
static char *
modify_path(char *path, char *old_str, char *new_str)
{
	char *newpath;
	char *pc;
	int len;

	/*
	 * Return a pointer to a copy of the path so a caller can always rely
	 * upon being able to free() a returned pointer.
	 */
	if ((pc = strstr(path, old_str)) == NULL)
		return (s_strdup(path));

	/*
	 * Allocate space for duplicate of path with name changes and
	 * NULL terminating byte
	 */
	len = strlen(path) - strlen(old_str) + strlen(new_str) + 1;

	if ((newpath = malloc(len)) == NULL) {
		bam_error(_("could not allocate memory: size = %u\n"), len);
		bam_exit(1);
	}

	(void) strlcpy(newpath, path, (pc - path) + 1);
	pc += strlen(old_str);

	(void) strcat(newpath, new_str);
	(void) strcat(newpath, pc);
	return (newpath);
}

/*
 * Set "token" to be the the string starting from the pointer "str" delimited
 * by any character in the string "delim" or the end of the string, but IGNORE
 * any characters between single or double quotes.
 *
 * Return a pointer to the next non-whitespace character after the delimiter
 * or NULL if we hit the end of the string. Also return NULL upon failure to
 * find any characters from the delimeter string or upon failure to allocate
 * memory for the new token string.
 */
static char *
get_token(char **token, char *str, char *delim)
{
	char *dp;
	char *start = str;
	unsigned len;

	*token = NULL;

	if ((str == NULL) || (*str == '\0'))
		return (NULL);

	do {
		if ((*str == '\'') || (*str == '"')) {
			char quote = *str++;

			while ((*str != '\0') && (*str != quote))
				str++;

			/* no matching quote found in string */
			if (*str++ == '\0')
				return (NULL);
		}

		/* look for a character from the delimiter string */
		for (dp = delim; ((*dp != '\0') && (*dp != *str)); dp++)
			;

		if (*dp != '\0') {
			len = str - start + 1;

			/* found a delimiter, so create a token string */
			if ((*token = malloc(len)) == NULL) {
				bam_error(_("could not allocate memory: "
				    "size = %u\n"), len);
				bam_exit(1);
			}

			(void) strlcpy(*token, start, len);

			while (isspace((int)*++str))
				;

			return (str);
		}
	} while (*str++ != '\0');

	/* if we hit the end of the string, the token is the whole string  */
	*token = s_strdup(start);
	return (NULL);
}

/*
 * Convert a metal "console" device name to an equivalent one suitable for
 * use with the hypervisor.
 *
 * Default to "vga" if we can't parse the console device.
 */
static void
console_metal_to_hyper(char *console)
{
	if ((*console == '\'') || (*console == '"'))
		console++;

	if (strncmp(console, "ttya", 4) == 0)
		console_dev = "console=com1";
	else if (strncmp(console, "ttyb", 4) == 0)
		console_dev = "console=com2";
	else
		console_dev = "console=vga";
}

static int
set_serial_rate(int com, char *rate)
{
	char **rp = &serial_config[com - 1];

	if ((com < 1) || (com > 2))
		return (-1);

	/*
	 * If rate is a NULL pointer, erase any existing serial configuration
	 * for this serial port.
	 */
	if (rate == NULL) {
		if (*rp != NULL) {
			free(*rp);
			*rp = NULL;
		}
		return (0);
	}

	*rp = s_realloc(*rp, strlen(rate) + 1);
	(void) strcpy(*rp, rate);
	return (0);
}

/*
 * Convert "metal" serial port parameters to values compatible with the
 * hypervisor.
 *
 * Return 0 on success, otherwise -1.
 */
static int
serial_metal_to_hyper(char *metal_port, char *metal_serial)
{
#define	COM_RATE_LEN	16	/* strlen("com1=115200,8n1") */

	char com_rate[COM_RATE_LEN];

	unsigned com, baud, bits, stop;
	char parity, handshake;

	if ((strcmp(metal_port, "ttya-mode") == 0) ||
	    (strcmp(metal_port, "ttyb-mode") == 0))
		com = TTYXMODE_TO_COMNUM(metal_port);
	else
		return (-1);

	if ((*metal_serial == '\'') || (*metal_serial == '"'))
		metal_serial++;

	/*
	 * Check if it's specified as the default rate; if so it defaults to
	 * "auto" and we need not set it for they hypervisor.
	 */
	if (strncmp(metal_serial, DEFAULT_SERIAL,
	    strlen(DEFAULT_SERIAL)) == 0) {
		(void) set_serial_rate(com, NULL);
		return (0);
	}

	/* read the serial port format as set forth in common/io/asy.c */
	if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
	    &handshake) != 5)
		return (-1);

	/* validate serial port parameters */
	if (((bits < 5) || (bits > 8)) || (stop > 1) ||
	    ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
	    ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
		return (-1);

	/* validate baud rate */
	switch (baud) {
		case 150:
		case 300:
		case 600:
		case 1200:
		case 2400:
		case 4800:
		case 9600:
		case 19200:
		case 38400:
		case 57600:
		case 115200:
			break;

		default:
			return (-1);
	}

	/*
	 * The hypervisor has no way to specify a handshake method, so it gets
	 * quietly dropped in the conversion.
	 */
	(void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
	    bits, parity, stop);
	(void) set_serial_rate(com, com_rate);
	return (0);
}

/*
 * Convert "name=value" metal options to values suitable for use with the
 * hypervisor.
 *
 * Our main concerns are the console device and serial port settings.
 *
 * Return values:
 *
 *    -1:	Unparseable line
 *    0:	Success
 *    (n > 0):	A property unimportant to us
 */
static int
cvt_metal_option(char *optstr)
{
	char *value;
	unsigned namlen;

	if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
		zfs_boot = 1;
		return (0);
	}

	if ((value = strchr(optstr, '=')) == NULL)
		return (-1);

	namlen = value - optstr;

	if (*++value == '\0')
		return (1);

	if (strncmp(optstr, "console", namlen) == 0) {
		console_metal_to_hyper(value);
		return (0);
	}

	if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
	    (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
		char *port = strndupa(optstr, namlen);

		return (serial_metal_to_hyper(port, value));
	}

	return (1);
}

/*
 * Convert "name=value" properties for use with a bare metal kernel
 *
 * Our main concerns are the console setting and serial port modes.
 *
 * Return values:
 *
 *    -1:	Unparseable line
 *    0:	Success
 *    (n > 0):	A property unimportant to us
 */
static int
cvt_hyper_option(char *optstr)
{
#define	SER_LEN		15	/* strlen("115200,8,n,1,-") + 1 */

	char ser[SER_LEN];
	char *value;

	unsigned namlen;

	unsigned baud;
	char bits, parity, stop;

	if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
		zfs_boot = 1;
		return (0);
	}

	/*
	 * If there's no "=" in the token, it's likely a standalone
	 * hypervisor token we don't care about (e.g. "noreboot" or
	 * "nosmp") so we ignore it.
	 */
	if ((value = strchr(optstr, '=')) == NULL)
		return (1);

	namlen = value - optstr;

	if (*++value == '\0')
		return (1);

	/*
	 * Note that we use strncmp against the values because the
	 * hypervisor allows setting console parameters for both the
	 * console and debugger via the format:
	 *
	 *   console=cons_dev,debug_dev
	 *
	 * and we only care about "cons_dev."
	 *
	 * This also allows us to extract "comN" from hypervisor constructs
	 * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
	 *
	 * Default the console device to "text" if it was "vga" or was
	 * unparseable.
	 */
	if (strncmp(optstr, "console", namlen) == 0) {
		/* ignore the "console=hypervisor" option */
		if (strcmp(value, "hypervisor") == 0)
			return (0);

		if (strncmp(value, "com1", 4) == 0)
			console_dev = "ttya";
		else if (strncmp(value, "com2", 4) == 0)
			console_dev = "ttyb";
		else
			console_dev = "text";
	}

	/* serial port parameter conversion */

	if ((strncmp(optstr, "com1", namlen) == 0) ||
	    (strncmp(optstr, "com2", namlen) == 0)) {
		unsigned com = COMNAME_TO_COMNUM(optstr);

		/*
		 * Check if it's "auto" - if so, use the default setting
		 * of "9600,8,n,1,-".
		 *
		 * We can't just assume the serial port will default to
		 * "9600,8,n,1" as there could be a directive in bootenv.rc
		 * that would set it to some other value and we want the serial
		 * parameters to be the same as that used by the hypervisor.
		 */
		if (strcmp(value, "auto") == 0) {
			(void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
		} else {
			/*
			 * Extract the "B,PS" setting from the com line; ignore
			 * other settings like io_base or IRQ.
			 */
			if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
			    &stop) != 4)
				return (-1);

			/* validate serial port parameters */
			if (((stop != '0') && (stop != '1')) ||
			    ((bits < '5') && (bits > '8')) ||
			    ((parity != 'n') && (parity != 'e') &&
			    (parity != 'o')))
				return (-1);

			/* validate baud rate */
			switch (baud) {
				case 150:
				case 300:
				case 600:
				case 1200:
				case 2400:
				case 4800:
				case 19200:
				case 38400:
				case 57600:
				case 115200:
					break;

				default:
					return (-1);
			}

			/*
			 * As the hypervisor has no way to denote handshaking
			 * in its serial port settings, emit a metal serial
			 * port configuration with none as well.
			 */
			(void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
			    bits, parity, stop);
		}

		if (set_serial_rate(com, ser) != 0)
			return (-1);

		return (0);
	}

	return (1);
}

/*
 * Parse a hardware kernel's "kernel$" specifier into parameters we can then
 * use to construct an appropriate "module$" line that can be used to specify
 * how to boot the hypervisor's dom0.
 *
 * Return values:
 *
 *	-1: error parsing kernel path
 *	 0: success
 *	 1: kernel already a hypervisor kernel
 */
static int
cvt_metal_kernel(char *kernstr, char **path)
{
	char *token, *parsestr;

	parsestr = get_token(path, kernstr, " \t,");
	if (*path == NULL)
		return (-1);

	/*
	 * If the metal kernel specified contains the name of the hypervisor,
	 * we're probably trying to convert an entry already setup to run the
	 * hypervisor, so error out now.
	 */
	if (strstr(*path, XEN_MENU) != NULL) {
		bam_error(_("default entry already setup for use with the "
		    "hypervisor!\n"));
		free(*path);
		*path = NULL;
		return (1);
	}

	/* if the path was the last item on the line, that's OK. */
	if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
		if (token != NULL)
			free(token);
		return (0);
	}

	/* if the next token is "-B" process boot options */
	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
		free(token);
		return (0);
	}

	free(token);

	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
		(void) cvt_metal_option(token);
		free(token);
	}

	if (token != NULL) {
		(void) cvt_metal_option(token);
		free(token);
	}

	return (0);
}

/*
 * Parse a hypervisor's "kernel$" line into parameters that can be used to
 * help build an appropriate "kernel$" line for booting a bare metal kernel.
 *
 * Return 0 on success, non-zero on failure.
 */
static int
cvt_hyper_kernel(char *kernel)
{
	char *token, *parsestr;

	parsestr = get_token(&token, kernel, " \t,");

	if (token == NULL)
		return (-1);

	/*
	 * If the hypervisor kernel specified lives in the metal kernel
	 * directory, we're probably trying to convert an entry already setup
	 * to run on bare metal, so error out now.
	 */
	if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
		bam_error(_("default entry already setup for use with a metal "
		    "kernel!\n"));
		free(token);
		return (-1);
	}

	free(token);

	/* check for kernel options */
	while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
		(void) cvt_hyper_option(token);
		free(token);
	}

	if (token != NULL) {
		(void) cvt_hyper_option(token);
		free(token);
	}

	return (0);
}

/*
 * Parse a hypervisor's "module$" line into parameters that can be used to
 * help build an appropriate "kernel$" line for booting a bare metal kernel.
 */
static void
cvt_hyper_module(char *modstr, char **path)
{
	char *token = NULL;
	char *parsestr = modstr;

	/*
	 * If multiple pathnames exist on the module$ line, we just want
	 * the last one.
	 */
	while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
		if (*parsestr != '/')
			break;
		else
			free(*path);
	}

	/* if the path was the last item on the line, that's OK. */
	if ((parsestr == NULL) ||
	    ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
		if (token != NULL)
			free(token);
		return;
	}

	if (token == NULL)
		return;

	/* check for "-B" option */
	if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
		free(token);
		return;
	}

	free(token);

	/* check for kernel options */
	while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
		(void) cvt_hyper_option(token);
		free(token);
	}

	if (token != NULL) {
		(void) cvt_hyper_option(token);
		free(token);
	}
}

static void
parse_bootenvrc(char *osroot)
{
#define	LINEBUF_SZ	1024

	FILE *fp;
	char *rcpath;
	char line[LINEBUF_SZ];	/* make line buffer large but not ridiculous */
	int len;

	assert(osroot);

	len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
	rcpath = alloca(len);

	(void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);

	/* if we couldn't open the bootenv.rc file, ignore the issue. */
	if ((fp = fopen(rcpath, "r")) == NULL) {
		BAM_DPRINTF(("could not open %s: %s\n", rcpath,
		    strerror(errno)));
		return;
	}

	while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
		char *parsestr, *token;
		int port = 0;

		/* we're only interested in parsing "setprop" directives. */
		if (strncmp(line, "setprop", 7) != 0)
			continue;

		/* eat initial "setprop" */
		if ((parsestr = get_token(&token, line, " \t")) == NULL) {
			if (token != NULL)
				free(token);

			continue;
		}

		if (strcmp(token, "setprop") != 0) {
			free(token);
			continue;
		}

		free(token);

		/* get property name */
		if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
			if (token != NULL)
				free(token);

			continue;
		}

		if (strcmp(token, "console") == 0) {
			free(token);

			/* get console property value */
			parsestr = get_token(&token, parsestr, " \t");
			if (token == NULL)
				continue;

			if (bootenv_rc_console != NULL)
				free(bootenv_rc_console);

			bootenv_rc_console = s_strdup(token);
			continue;
		}

		/* check if it's a serial port setting */
		if (strcmp(token, "ttya-mode") == 0) {
			free(token);
			port = 0;
		} else if (strcmp(token, "ttyb-mode") == 0) {
			free(token);
			port = 1;
		} else {
			/* nope, so check the next line */
			free(token);
			continue;
		}

		/* get serial port setting */
		parsestr = get_token(&token, parsestr, " \t");

		if (token == NULL)
			continue;

		if (bootenv_rc_serial[port] != NULL)
			free(bootenv_rc_serial[port]);

		bootenv_rc_serial[port] = s_strdup(token);
		free(token);
	}

	(void) fclose(fp);
}

error_t
cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
{
	const char *fcn = "cvt_to_hyper()";

	line_t *lp;
	entry_t *ent;
	size_t len, zfslen;

	char *newstr;
	char *osdev;

	char *title = NULL;
	char *findroot = NULL;
	char *bootfs = NULL;
	char *kernel = NULL;
	char *mod_kernel = NULL;
	char *module = NULL;

	char *kern_path = NULL;
	char *kern_bargs = NULL;

	int curdef, newdef;
	int kp_allocated = 0;
	int ret = BAM_ERROR;

	assert(osroot);

	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, extra_args));

	/*
	 * First just check to verify osroot is a sane directory.
	 */
	if ((osdev = get_special(osroot)) == NULL) {
		bam_error(_("cant find special file for mount-point %s\n"),
		    osroot);
		return (BAM_ERROR);
	}

	free(osdev);

	/*
	 * While the effect is purely cosmetic, if osroot is "/" don't
	 * bother prepending it to any paths as they are constructed to
	 * begin with "/" anyway.
	 */
	if (strcmp(osroot, "/") == 0)
		osroot = "";

	/*
	 * Found the GRUB signature on the target partitions, so now get the
	 * default GRUB boot entry number from the menu.lst file
	 */
	curdef = atoi(mp->curdefault->arg);

	/* look for the first line of the matching boot entry */
	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
	    ent = ent->next)
		;

	/* couldn't find it, so error out */
	if (ent == NULL) {
		bam_error(_("unable to find default boot entry (%d) in "
		    "menu.lst file.\n"), curdef);
		goto abort;
	}

	/*
	 * We found the proper menu entry, so first we need to process the
	 * bootenv.rc file to look for boot options the hypervisor might need
	 * passed as kernel start options such as the console device and serial
	 * port parameters.
	 *
	 * If there's no bootenv.rc, it's not an issue.
	 */
	parse_bootenvrc(osroot);

	if (bootenv_rc_console != NULL)
		console_metal_to_hyper(bootenv_rc_console);

	if (bootenv_rc_serial[0] != NULL)
		(void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);

	if (bootenv_rc_serial[1] != NULL)
		(void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);

	/*
	 * Now process the entry itself.
	 */
	for (lp = ent->start; lp != NULL; lp = lp->next) {
		/*
		 * Process important lines from menu.lst boot entry.
		 */
		if (lp->flags == BAM_TITLE) {
			title = strdupa(lp->arg);
		} else if (lp->cmd != NULL) {
			if (strcmp(lp->cmd, "findroot") == 0) {
				findroot = strdupa(lp->arg);
			} else if (strcmp(lp->cmd, "bootfs") == 0) {
				bootfs = strdupa(lp->arg);
			} else if (strcmp(lp->cmd,
			    menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
				module = strdupa(lp->arg);
			} else if ((strcmp(lp->cmd,
			    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
			    (ret = cvt_metal_kernel(lp->arg,
			    &kern_path)) != 0) {
				if (ret < 0) {
					ret = BAM_ERROR;
					bam_error(_("kernel$ in default boot "
					    "entry (%d) missing or not "
					    "parseable.\n"), curdef);
				} else
					ret = BAM_NOCHANGE;

				goto abort;
			}
		}

		if (lp == ent->end)
			break;
	}

	/*
	 * If findroot, module or kern_path are NULL, the boot entry is
	 * malformed.
	 */
	if (findroot == NULL) {
		bam_error(_("findroot in default boot entry (%d) missing.\n"),
		    curdef);
		goto abort;
	}

	if (module == NULL) {
		bam_error(_("module$ in default boot entry (%d) missing or "
		    "not parseable.\n"), curdef);
		goto abort;
	}

	if (kern_path == NULL) {
		bam_error(_("kernel$ in default boot entry (%d) missing.\n"),
		    curdef);
		goto abort;
	}

	/* assemble new kernel and module arguments from parsed values */
	if (console_dev != NULL) {
		kern_bargs = s_strdup(console_dev);

		if (serial_config[0] != NULL) {
			newstr = append_str(kern_bargs, serial_config[0], " ");
			free(kern_bargs);
			kern_bargs = newstr;
		}

		if (serial_config[1] != NULL) {
			newstr = append_str(kern_bargs, serial_config[1], " ");
			free(kern_bargs);
			kern_bargs = newstr;
		}
	}

	if ((extra_args != NULL) && (*extra_args != '\0')) {
		newstr = append_str(kern_bargs, extra_args, " ");
		free(kern_bargs);
		kern_bargs = newstr;
	}

	len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
	    WHITESPC(1) + 1;

	kernel = alloca(len);

	if (kern_bargs != NULL) {
		if (*kern_bargs != '\0')
			(void) snprintf(kernel, len, "%s%s %s", osroot,
			    XEN_MENU, kern_bargs);

		free(kern_bargs);
	} else {
		(void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
	}

	/*
	 * Change the kernel directory from the metal version to that needed for
	 * the hypervisor.  Convert either "direct boot" path to the default
	 * path.
	 */
	if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
	    (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
		kern_path = HYPERVISOR_KERNEL;
	} else {
		newstr = modify_path(kern_path, METAL_KERNEL_DIR,
		    HYPER_KERNEL_DIR);
		free(kern_path);
		kern_path = newstr;
		kp_allocated = 1;
	}

	/*
	 * We need to allocate space for the kernel path (twice) plus an
	 * intervening space, possibly the ZFS boot string, and NULL,
	 * of course.
	 */
	len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);

	mod_kernel = alloca(len + zfslen);
	(void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);

	if (kp_allocated)
		free(kern_path);

	if (zfs_boot) {
		char *zfsstr = alloca(zfslen + 1);

		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
		(void) strcat(mod_kernel, zfsstr);
	}

	/* shut off warning messages from the entry line parser */
	if (ent->flags & BAM_ENTRY_BOOTADM)
		ent->flags &= ~BAM_ENTRY_BOOTADM;

	BAM_DPRINTF(("%s: converted kernel cmd to %s\n", fcn, kernel));
	BAM_DPRINTF(("%s: converted module cmd to %s\n", fcn, mod_kernel));

	if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
	    module, bootfs)) == BAM_ERROR)
		return (newdef);

	/*
	 * Now try to delete the current default entry from the menu and add
	 * the new hypervisor entry with the parameters we've setup.
	 */
	if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
		newdef--;
	else
		bam_print(_("unable to modify default entry; creating new "
		    "boot entry for %s\n"), title);

	/*
	 * If we successfully created the new entry, set the default boot
	 * entry to that entry and let the caller know the new menu should
	 * be written out.
	 */
	return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));

abort:
	if (ret != BAM_NOCHANGE)
		bam_error(_("error converting GRUB menu entry on %s for use "
		    "with the hypervisor.\nAborting.\n"),
		    ((*osroot == '\0') ? "/" : osroot));

	return (ret);
}

/*ARGSUSED*/
error_t
cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
{
	const char *fcn = "cvt_to_metal()";

	line_t *lp;
	entry_t *ent;
	size_t len, zfslen;

	char *delim = ",";
	char *newstr;
	char *osdev;

	char *title = NULL;
	char *findroot = NULL;
	char *bootfs = NULL;
	char *kernel = NULL;
	char *module = NULL;

	char *barchive_path = DIRECT_BOOT_ARCHIVE;
	char *kern_path = NULL;

	int curdef, newdef;
	int emit_bflag = 1;
	int ret = BAM_ERROR;

	assert(osroot);

	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, ""));

	/*
	 * First just check to verify osroot is a sane directory.
	 */
	if ((osdev = get_special(osroot)) == NULL) {
		bam_error(_("cant find special file for mount-point %s\n"),
		    osroot);
		return (BAM_ERROR);
	}

	free(osdev);

	/*
	 * Found the GRUB signature on the target partitions, so now get the
	 * default GRUB boot entry number from the menu.lst file
	 */
	curdef = atoi(mp->curdefault->arg);

	/* look for the first line of the matching boot entry */
	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
	    ent = ent->next)
		;

	/* couldn't find it, so error out */
	if (ent == NULL) {
		bam_error(_("unable to find default boot entry (%d) in "
		    "menu.lst file.\n"), curdef);
		goto abort;
	}

	/*
	 * Now process the entry itself.
	 */
	for (lp = ent->start; lp != NULL; lp = lp->next) {
		/*
		 * Process important lines from menu.lst boot entry.
		 */
		if (lp->flags == BAM_TITLE) {
			title = strdupa(lp->arg);
		} else if (lp->cmd != NULL) {
			if (strcmp(lp->cmd, "findroot") == 0) {
				findroot = strdupa(lp->arg);
			} else if (strcmp(lp->cmd, "bootfs") == 0) {
				bootfs = strdupa(lp->arg);
			} else if (strcmp(lp->cmd,
			    menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
				if (strstr(lp->arg, "boot_archive") == NULL) {
					module = strdupa(lp->arg);
					cvt_hyper_module(module, &kern_path);
				} else {
					barchive_path = strdupa(lp->arg);
				}
			} else if ((strcmp(lp->cmd,
			    menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
			    (cvt_hyper_kernel(lp->arg) < 0)) {
				ret = BAM_NOCHANGE;
				goto abort;
			}
		}

		if (lp == ent->end)
			break;
	}

	/*
	 * If findroot, module or kern_path are NULL, the boot entry is
	 * malformed.
	 */
	if (findroot == NULL) {
		bam_error(_("findroot in default boot entry (%d) missing.\n"),
		    curdef);
		goto abort;
	}

	if (module == NULL) {
		bam_error(_("module$ in default boot entry (%d) missing or "
		    "not parseable.\n"), curdef);
		goto abort;
	}

	if (kern_path == NULL) {
		bam_error(_("kernel$ in default boot entry (%d) missing.\n"),
		    curdef);
		goto abort;
	}

	/*
	 * Assemble new kernel and module arguments from parsed values.
	 *
	 * First, change the kernel directory from the hypervisor version to
	 * that needed for a metal kernel.
	 */
	newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
	free(kern_path);
	kern_path = newstr;

	/* allocate initial space for the kernel path */
	len = strlen(kern_path) + 1;
	zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);

	if ((kernel = malloc(len + zfslen)) == NULL) {
		free(kern_path);
		bam_error(_("could not allocate memory: size = %u\n"),
		    len + zfslen);
		bam_exit(1);
	}

	(void) snprintf(kernel, len, "%s", kern_path);
	free(kern_path);

	if (zfs_boot) {
		char *zfsstr = alloca(zfslen + 1);

		(void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
		(void) strcat(kernel, zfsstr);
		emit_bflag = 0;
	}

	/*
	 * Process the bootenv.rc file to look for boot options that would be
	 * the same as what the hypervisor had manually set, as we need not set
	 * those explicitly.
	 *
	 * If there's no bootenv.rc, it's not an issue.
	 */
	parse_bootenvrc(osroot);

	/*
	 * Don't emit a console setting if it's the same as what would be
	 * set by bootenv.rc.
	 */
	if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
	    (strcmp(console_dev, bootenv_rc_console) != 0))) {
		if (emit_bflag) {
			newstr = append_str(kernel, BFLAG, " ");
			free(kernel);
			kernel = append_str(newstr, "console=", " ");
			free(newstr);
			newstr = append_str(kernel, console_dev, "");
			free(kernel);
			kernel = newstr;
			emit_bflag = 0;
		} else {
			newstr = append_str(kernel, "console=", ",");
			free(kernel);
			kernel = append_str(newstr, console_dev, "");
			free(newstr);
		}
	}

	/*
	 * We have to do some strange processing here because the hypervisor's
	 * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
	 * or to "auto" if nothing is specified.
	 *
	 * This could result in a serial mode setting string being added when
	 * it would otherwise not be needed, but it's better to play it safe.
	 */
	if (emit_bflag) {
		newstr = append_str(kernel, BFLAG, " ");
		free(kernel);
		kernel = newstr;
		delim = " ";
		emit_bflag = 0;
	}

	if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
	    (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
		newstr = append_str(kernel, "ttya-mode='", delim);
		free(kernel);

		/*
		 * Pass the serial configuration as the delimiter to
		 * append_str() as it will be inserted between the current
		 * string and the string we're appending, in this case the
		 * closing single quote.
		 */
		kernel = append_str(newstr, "'", serial_config[0]);
		free(newstr);
		delim = ",";
	}

	if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
	    (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
		newstr = append_str(kernel, "ttyb-mode='", delim);
		free(kernel);

		/*
		 * Pass the serial configuration as the delimiter to
		 * append_str() as it will be inserted between the current
		 * string and the string we're appending, in this case the
		 * closing single quote.
		 */
		kernel = append_str(newstr, "'", serial_config[1]);
		free(newstr);
		delim = ",";
	}

	/* shut off warning messages from the entry line parser */
	if (ent->flags & BAM_ENTRY_BOOTADM)
		ent->flags &= ~BAM_ENTRY_BOOTADM;

	BAM_DPRINTF(("%s: converted kernel cmd to %s\n", fcn, kernel));
	BAM_DPRINTF(("%s: converted module cmd to %s\n", fcn, module));

	if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
	    barchive_path, bootfs)) == BAM_ERROR) {
		free(kernel);
		return (newdef);
	}

	/*
	 * Now try to delete the current default entry from the menu and add
	 * the new hypervisor entry with the parameters we've setup.
	 */
	if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
		newdef--;
	else
		bam_print(_("unable to modify default entry; creating new "
		    "boot entry for %s\n"), title);

	free(kernel);

	/*
	 * If we successfully created the new entry, set the default boot
	 * entry to that entry and let the caller know the new menu should
	 * be written out.
	 */
	return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));

abort:
	if (ret != BAM_NOCHANGE)
		bam_error(_("error converting GRUB menu entry on %s for use "
		    "with a metal kernel.\nAborting.\n"), osroot);

	return (ret);
}