summaryrefslogtreecommitdiff
path: root/usr/src/cmd/boot/installgrub/installgrub.c
blob: 09c03fd1f4cc0ff1ad4ef26a07062f604d6681df (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
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
/*
 * 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2012 Milan Jurik. All rights reserved.
 * Copyright 2016 Toomas Soome <tsoome@me.com>
 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <libintl.h>
#include <locale.h>
#include <errno.h>
#include <libfdisk.h>
#include <stdarg.h>
#include <assert.h>

#include <sys/mount.h>
#include <sys/mnttab.h>
#include <sys/dktp/fdisk.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/multiboot.h>
#include <sys/sysmacros.h>
#include <sys/efi_partition.h>

#include <libnvpair.h>
#include <libfstyp.h>

#include "message.h"
#include "installgrub.h"
#include "./../common/bblk_einfo.h"
#include "./../common/boot_utils.h"
#include "./../common/mboot_extra.h"
#include "getresponse.h"

#ifndef	TEXT_DOMAIN
#define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
#endif

/*
 * Variables to track installgrub desired mode of operation.
 * 'nowrite' and 'boot_debug' come from boot_common.h.
 */
static boolean_t write_mbr = B_FALSE;
static boolean_t force_mbr = B_FALSE;
static boolean_t force_update = B_FALSE;
static boolean_t do_getinfo = B_FALSE;
static boolean_t do_version = B_FALSE;
static boolean_t do_mirror_bblk = B_FALSE;
static boolean_t strip = B_FALSE;
static boolean_t verbose_dump = B_FALSE;

/* Installing the bootblock is the default operation. */
static boolean_t do_install = B_TRUE;

/* Versioning string, if present. */
static char *update_str;

/*
 * Temporary buffer to store the first 32K of data looking for a multiboot
 * signature.
 */
char	mboot_scan[MBOOT_SCAN_SIZE];

/* Function prototypes. */
static void check_options(char *);
static int handle_install(char *, char **);
static int handle_mirror(char *, char **);
static int handle_getinfo(char *, char **);
static int commit_to_disk(ig_data_t *, char *);
static int init_device(ig_device_t *, char *path);
static void cleanup_device(ig_device_t *);
static void cleanup_stage2(ig_stage2_t *);
static int get_start_sector(ig_device_t *);
static int get_disk_fd(ig_device_t *device);
static int get_raw_partition_fd(ig_device_t *);
static char *get_raw_partition_path(ig_device_t *);
static int propagate_bootblock(ig_data_t *, ig_data_t *, char *);
static int find_x86_bootpar(struct mboot *, int *, uint32_t *);
static int write_stage2(ig_data_t *);
static int write_stage1(ig_data_t *);
static void usage(char *);
static int read_stage1_from_file(char *, ig_data_t *);
static int read_stage2_from_file(char *, ig_data_t *);
static int read_stage1_from_disk(int, char *);
static int read_stage2_from_disk(int, ig_stage2_t *, int);
static int prepare_stage1(ig_data_t *);
static int prepare_stage2(ig_data_t *, char *);
static void prepare_fake_multiboot(ig_stage2_t *);
static void add_stage2_einfo(ig_stage2_t *, char *updt_str);
static boolean_t is_update_necessary(ig_data_t *, char *);

extern int read_stage2_blocklist(int, unsigned int *);

int
main(int argc, char *argv[])
{
	int	opt;
	int	params = 3;
	int	ret;
	char	**handle_args;
	char	*progname;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);
	if (init_yes() < 0) {
		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
		    strerror(errno));
		exit(BC_ERROR);
	}

	/*
	 * retro-compatibility: installing the bootblock is the default
	 * and there is no switch for it.
	 */
	do_install = B_TRUE;

	while ((opt = getopt(argc, argv, "dVMFfmneiu:")) != EOF) {
		switch (opt) {
		case 'm':
			write_mbr = B_TRUE;
			break;
		case 'n':
			nowrite = B_TRUE;
			break;
		case 'f':
			force_mbr = B_TRUE;
			break;
		case 'i':
			do_getinfo = B_TRUE;
			do_install = B_FALSE;
			params = 1;
			break;
		case 'V':
			verbose_dump = B_TRUE;
			break;
		case 'd':
			boot_debug = B_TRUE;
			break;
		case 'F':
			force_update = B_TRUE;
			break;
		case 'e':
			strip = B_TRUE;
			break;
		case 'M':
			do_mirror_bblk = B_TRUE;
			do_install = B_FALSE;
			params = 2;
			break;
		case 'u':
			do_version = B_TRUE;

			update_str = malloc(strlen(optarg) + 1);
			if (update_str == NULL) {
				(void) fprintf(stderr, gettext("Unable to "
				    "allocate memory\n"));
				exit(BC_ERROR);
			}
			(void) strlcpy(update_str, optarg, strlen(optarg) + 1);
			break;
		default:
			/* fall through to process non-optional args */
			break;
		}
	}

	/* check arguments */
	if (argc != optind + params) {
		usage(argv[0]);
		exit(BC_ERROR);
	}

	/*
	 * clean up options (and bail out if an unrecoverable combination is
	 * requested.
	 */
	progname = argv[0];
	check_options(progname);
	handle_args = argv + optind;

	if (nowrite)
		(void) fprintf(stdout, DRY_RUN);

	if (do_getinfo) {
		ret = handle_getinfo(progname, handle_args);
	} else if (do_mirror_bblk) {
		ret = handle_mirror(progname, handle_args);
	} else {
		ret = handle_install(progname, handle_args);
	}
	return (ret);
}

#define	MEANINGLESS_OPT	gettext("%s specified but meaningless, ignoring\n")
static void
check_options(char *progname)
{
	if (do_getinfo && do_mirror_bblk) {
		(void) fprintf(stderr, gettext("Only one of -M and -i can be "
		    "specified at the same time\n"));
		usage(progname);
		exit(BC_ERROR);
	}

	if (do_mirror_bblk) {
		/*
		 * -u and -F may actually reflect a user intent that is not
		 * correct with this command (mirror can be interpreted
		 * "similar" to install. Emit a message and continue.
		 * -e and -V have no meaning, be quiet here and only report the
		 * incongruence if a debug output is requested.
		 */
		if (do_version) {
			(void) fprintf(stderr, MEANINGLESS_OPT, "-u");
			do_version = B_FALSE;
		}
		if (force_update) {
			(void) fprintf(stderr, MEANINGLESS_OPT, "-F");
			force_update = B_FALSE;
		}
		if (strip || verbose_dump) {
			BOOT_DEBUG(MEANINGLESS_OPT, "-e|-V");
			strip = B_FALSE;
			verbose_dump = B_FALSE;
		}
	}

	if (do_getinfo) {
		if (write_mbr || force_mbr || do_version || force_update) {
			BOOT_DEBUG(MEANINGLESS_OPT, "-m|-f|-u|-F");
			write_mbr = force_mbr = do_version = B_FALSE;
			force_update = B_FALSE;
		}
	}
}

/*
 * Install a new stage1/stage2 pair on the specified device. handle_install()
 * expects argv to contain 3 parameters (the path to stage1, the path to stage2,
 * the target device).
 *
 * Returns:	BC_SUCCESS - if the installation is successful
 *		BC_ERROR   - if the installation failed
 *		BC_NOUPDT  - if no installation was performed because the GRUB
 *		             version currently installed is more recent than the
 *			     supplied one.
 *
 */
static int
handle_install(char *progname, char **argv)
{
	ig_data_t	install_data;
	char		*stage1_path = NULL;
	char		*stage2_path = NULL;
	char		*device_path = NULL;
	int		ret = BC_ERROR;

	stage1_path = strdup(argv[0]);
	stage2_path = strdup(argv[1]);
	device_path = strdup(argv[2]);

	bzero(&install_data, sizeof (ig_data_t));

	if (!stage1_path || !stage2_path || !device_path) {
		(void) fprintf(stderr, gettext("Missing parameter"));
		usage(progname);
		goto out;
	}

	BOOT_DEBUG("stage1 path: %s, stage2 path: %s, device: %s\n",
	    stage1_path, stage2_path, device_path);

	if (init_device(&install_data.device, device_path) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Unable to gather device "
		    "information for %s\n"), device_path);
		goto out;
	}

	/* read in stage1 and stage2. */
	if (read_stage1_from_file(stage1_path, &install_data) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error opening %s\n"),
		    stage1_path);
		goto out_dev;
	}

	if (read_stage2_from_file(stage2_path, &install_data) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error opening %s\n"),
		    stage2_path);
		goto out_dev;
	}

	/* We do not support versioning on PCFS. */
	if (is_bootpar(install_data.device.type) && do_version)
		do_version = B_FALSE;

	/*
	 * is_update_necessary() will take care of checking if versioning and/or
	 * forcing the update have been specified. It will also emit a warning
	 * if a non-versioned update is attempted over a versioned bootblock.
	 */
	if (!is_update_necessary(&install_data, update_str)) {
		(void) fprintf(stderr, gettext("GRUB version installed "
		    "on %s is more recent or identical\n"
		    "Use -F to override or install without the -u option\n"),
		    device_path);
		ret = BC_NOUPDT;
		goto out_dev;
	}
	/*
	 * We get here if:
	 * - the installed GRUB version is older than the one about to be
	 *   installed.
	 * - no versioning string has been passed through the command line.
	 * - a forced update is requested (-F).
	 */
	BOOT_DEBUG("Ready to commit to disk\n");
	ret = commit_to_disk(&install_data, update_str);

out_dev:
	cleanup_device(&install_data.device);
out:
	free(stage1_path);
	free(stage2_path);
	free(device_path);
	return (ret);
}

/*
 * Retrieves from a device the extended information (einfo) associated to the
 * installed stage2.
 * Expects one parameter, the device path, in the form: /dev/rdsk/c?[t?]d?s0.
 * Returns:
 *        - BC_SUCCESS (and prints out einfo contents depending on 'flags')
 *	  - BC_ERROR (on error)
 *        - BC_NOEINFO (no extended information available)
 */
static int
handle_getinfo(char *progname, char **argv)
{
	ig_data_t	data;
	ig_stage2_t	*stage2 = &data.stage2;
	ig_device_t	*device = &data.device;
	bblk_einfo_t	*einfo;
	uint8_t		flags = 0;
	uint32_t	size;
	char		*device_path;
	int		retval = BC_ERROR;
	int		ret;

	device_path = strdup(argv[0]);
	if (!device_path) {
		(void) fprintf(stderr, gettext("Missing parameter"));
		usage(progname);
		goto out;
	}

	bzero(&data, sizeof (ig_data_t));
	BOOT_DEBUG("device path: %s\n", device_path);

	if (init_device(device, device_path) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Unable to gather device "
		    "information for %s\n"), device_path);
		goto out_dev;
	}

	if (is_bootpar(device->type)) {
		(void) fprintf(stderr, gettext("Versioning not supported on "
		    "PCFS\n"));
		goto out_dev;
	}

	ret = read_stage2_from_disk(device->part_fd, stage2, device->type);
	if (ret == BC_ERROR) {
		(void) fprintf(stderr, gettext("Error reading stage2 from "
		    "%s\n"), device_path);
		goto out_dev;
	}

	if (ret == BC_NOEXTRA) {
		(void) fprintf(stdout, gettext("No multiboot header found on "
		    "%s, unable to locate extra information area\n"),
		    device_path);
		retval = BC_NOEINFO;
		goto out_dev;
	}

	einfo = find_einfo(stage2->extra, stage2->extra_size);
	if (einfo == NULL) {
		retval = BC_NOEINFO;
		(void) fprintf(stderr, gettext("No extended information "
		    "found\n"));
		goto out_dev;
	}

	/* Print the extended information. */
	if (strip)
		flags |= EINFO_EASY_PARSE;
	if (verbose_dump)
		flags |= EINFO_PRINT_HEADER;

	size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
	print_einfo(flags, einfo, size);
	retval = BC_SUCCESS;

out_dev:
	cleanup_device(&data.device);
out:
	free(device_path);
	return (retval);
}

/*
 * Attempt to mirror (propagate) the current stage2 over the attaching disk.
 *
 * Returns:
 *	- BC_SUCCESS (a successful propagation happened)
 *	- BC_ERROR (an error occurred)
 *	- BC_NOEXTRA (it is not possible to dump the current bootblock since
 *			there is no multiboot information)
 */
static int
handle_mirror(char *progname, char **argv)
{
	ig_data_t	curr_data;
	ig_data_t	attach_data;
	ig_device_t	*curr_device = &curr_data.device;
	ig_device_t	*attach_device = &attach_data.device;
	ig_stage2_t	*stage2_curr = &curr_data.stage2;
	ig_stage2_t	*stage2_attach = &attach_data.stage2;
	bblk_einfo_t	*einfo_curr = NULL;
	char		*curr_device_path;
	char		*attach_device_path;
	char		*updt_str = NULL;
	int		retval = BC_ERROR;
	int		ret;

	curr_device_path = strdup(argv[0]);
	attach_device_path = strdup(argv[1]);

	if (!curr_device_path || !attach_device_path) {
		(void) fprintf(stderr, gettext("Missing parameter"));
		usage(progname);
		goto out;
	}
	BOOT_DEBUG("Current device path is: %s, attaching device path is: "
	    " %s\n", curr_device_path, attach_device_path);

	bzero(&curr_data, sizeof (ig_data_t));
	bzero(&attach_data, sizeof (ig_data_t));

	if (init_device(curr_device, curr_device_path) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Unable to gather device "
		    "information for %s (current device)\n"), curr_device_path);
		goto out_currdev;
	}

	if (init_device(attach_device, attach_device_path) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Unable to gather device "
		    "information for %s (attaching device)\n"),
		    attach_device_path);
		goto out_devs;
	}

	if (is_bootpar(curr_device->type) || is_bootpar(attach_device->type)) {
		(void) fprintf(stderr, gettext("boot block mirroring is not "
		    "supported on PCFS\n"));
		goto out_devs;
	}

	ret = read_stage2_from_disk(curr_device->part_fd, stage2_curr,
	    curr_device->type);
	if (ret == BC_ERROR) {
		BOOT_DEBUG("Error reading first stage2 blocks from %s\n",
		    curr_device->path);
		retval = BC_ERROR;
		goto out_devs;
	}

	if (ret == BC_NOEXTRA) {
		BOOT_DEBUG("No multiboot header found on %s, unable to grab "
		    "stage2\n", curr_device->path);
		retval = BC_NOEXTRA;
		goto out_devs;
	}

	einfo_curr = find_einfo(stage2_curr->extra, stage2_curr->extra_size);
	if (einfo_curr != NULL)
		updt_str = einfo_get_string(einfo_curr);

	write_mbr = B_TRUE;
	force_mbr = B_TRUE;
	retval = propagate_bootblock(&curr_data, &attach_data, updt_str);
	cleanup_stage2(stage2_curr);
	cleanup_stage2(stage2_attach);

out_devs:
	cleanup_device(attach_device);
out_currdev:
	cleanup_device(curr_device);
out:
	free(curr_device_path);
	free(attach_device_path);
	return (retval);
}

static int
commit_to_disk(ig_data_t *install, char *updt_str)
{
	assert(install != NULL);
	/*
	 * vanilla stage1 and stage2 need to be updated at runtime.
	 * Update stage2 before stage1 because stage1 needs to know the first
	 * sector stage2 will be written to.
	 */
	if (prepare_stage2(install, updt_str) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error building stage2\n"));
		return (BC_ERROR);
	}
	if (prepare_stage1(install) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error building stage1\n"));
		return (BC_ERROR);
	}

	/* Write stage2 out to disk. */
	if (write_stage2(install) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error writing stage2 to "
		    "disk\n"));
		return (BC_ERROR);
	}

	/* Write stage1 to disk and, if requested, to the MBR. */
	if (write_stage1(install) != BC_SUCCESS) {
		(void) fprintf(stderr, gettext("Error writing stage1 to "
		    "disk\n"));
		return (BC_ERROR);
	}

	return (BC_SUCCESS);
}

/*
 * Propagate the bootblock on the source disk to the destination disk and
 * version it with 'updt_str' in the process. Since we cannot trust any data
 * on the attaching disk, we do not perform any specific check on a potential
 * target extended information structure and we just blindly update.
 */
static int
propagate_bootblock(ig_data_t *source, ig_data_t *target, char *updt_str)
{
	ig_device_t	*src_device = &source->device;
	ig_device_t	*dest_device = &target->device;
	ig_stage2_t	*src_stage2 = &source->stage2;
	ig_stage2_t	*dest_stage2 = &target->stage2;
	uint32_t	buf_size;
	int		retval;

	assert(source != NULL);
	assert(target != NULL);

	/* read in stage1 from the source disk. */
	if (read_stage1_from_disk(src_device->part_fd, target->stage1_buf)
	    != BC_SUCCESS)
		return (BC_ERROR);

	/* Prepare target stage2 for commit_to_disk. */
	cleanup_stage2(dest_stage2);

	if (updt_str != NULL)
		do_version = B_TRUE;
	else
		do_version = B_FALSE;

	buf_size = src_stage2->file_size + SECTOR_SIZE;

	dest_stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);
	dest_stage2->buf = malloc(dest_stage2->buf_size);
	if (dest_stage2->buf == NULL) {
		perror(gettext("Memory allocation failed"));
		return (BC_ERROR);
	}
	dest_stage2->file = dest_stage2->buf;
	dest_stage2->file_size = src_stage2->file_size;
	memcpy(dest_stage2->file, src_stage2->file, dest_stage2->file_size);
	dest_stage2->extra = dest_stage2->buf +
	    P2ROUNDUP(dest_stage2->file_size, 8);

	/* If we get down here we do have a mboot structure. */
	assert(src_stage2->mboot);

	dest_stage2->mboot_off = src_stage2->mboot_off;
	dest_stage2->mboot = (multiboot_header_t *)(dest_stage2->buf +
	    dest_stage2->mboot_off);

	(void) fprintf(stdout, gettext("Propagating %s stage1/stage2 to %s\n"),
	    src_device->path, dest_device->path);
	retval = commit_to_disk(target, updt_str);

	return (retval);
}

/*
 * open the device and fill the various members of ig_device_t.
 */
static int
init_device(ig_device_t *device, char *path)
{
	struct dk_gpt *vtoc;
	fstyp_handle_t fhdl;
	const char *fident;

	bzero(device, sizeof (*device));
	device->part_fd = -1;
	device->disk_fd = -1;
	device->path_p0 = NULL;

	device->path = strdup(path);
	if (device->path == NULL) {
		perror(gettext("Memory allocation failed"));
		return (BC_ERROR);
	}

	if (strstr(device->path, "diskette")) {
		(void) fprintf(stderr, gettext("installing GRUB to a floppy "
		    "disk is no longer supported\n"));
		return (BC_ERROR);
	}

	/* Detect if the target device is a pcfs partition. */
	if (strstr(device->path, "p0:boot"))
		device->type = IG_DEV_X86BOOTPAR;

	if (get_disk_fd(device) != BC_SUCCESS)
		return (BC_ERROR);

	/* read in the device boot sector. */
	if (read(device->disk_fd, device->boot_sector, SECTOR_SIZE)
	    != SECTOR_SIZE) {
		(void) fprintf(stderr, gettext("Error reading boot sector\n"));
		perror("read");
		return (BC_ERROR);
	}

	if (efi_alloc_and_read(device->disk_fd, &vtoc) >= 0) {
		device->type = IG_DEV_EFI;
		efi_free(vtoc);
	}

	if (get_raw_partition_fd(device) != BC_SUCCESS)
		return (BC_ERROR);

	if (is_efi(device->type)) {
		if (fstyp_init(device->part_fd, 0, NULL, &fhdl) != 0)
			return (BC_ERROR);

		if (fstyp_ident(fhdl, "zfs", &fident) != 0) {
			fstyp_fini(fhdl);
			(void) fprintf(stderr, gettext("Booting of EFI labeled "
			    "disks is only supported with ZFS\n"));
			return (BC_ERROR);
		}
		fstyp_fini(fhdl);
	}

	if (get_start_sector(device) != BC_SUCCESS)
		return (BC_ERROR);

	return (BC_SUCCESS);
}

static void
cleanup_device(ig_device_t *device)
{
	if (device->path)
		free(device->path);
	if (device->path_p0)
		free(device->path_p0);

	if (device->part_fd != -1)
		(void) close(device->part_fd);
	if (device->disk_fd != -1)
		(void) close(device->disk_fd);

	bzero(device, sizeof (ig_device_t));
	device->part_fd = -1;
	device->disk_fd = -1;
}

static void
cleanup_stage2(ig_stage2_t *stage2)
{
	if (stage2->buf)
		free(stage2->buf);
	bzero(stage2, sizeof (ig_stage2_t));
}

static int
get_start_sector(ig_device_t *device)
{
	uint32_t		secnum = 0, numsec = 0;
	int			i, pno, rval, log_part = 0;
	struct mboot		*mboot;
	struct ipart		*part = NULL;
	ext_part_t		*epp;
	struct part_info	dkpi;
	struct extpart_info	edkpi;

	if (is_efi(device->type)) {
		struct dk_gpt *vtoc;

		if (efi_alloc_and_read(device->disk_fd, &vtoc) < 0)
			return (BC_ERROR);

		device->start_sector = vtoc->efi_parts[device->slice].p_start;
		/* GPT doesn't use traditional slice letters */
		device->partition = device->slice;
		device->slice = 0xff;

		efi_free(vtoc);
		goto found_part;
	}

	mboot = (struct mboot *)device->boot_sector;

	if (is_bootpar(device->type)) {
		if (find_x86_bootpar(mboot, &pno, &secnum) != BC_SUCCESS) {
			(void) fprintf(stderr, NOBOOTPAR);
			return (BC_ERROR);
		} else {
			device->start_sector = secnum;
			device->partition = pno;
			goto found_part;
		}
	}

	/*
	 * Search for Solaris fdisk partition
	 * Get the solaris partition information from the device
	 * and compare the offset of S2 with offset of solaris partition
	 * from fdisk partition table.
	 */
	if (ioctl(device->part_fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
		if (ioctl(device->part_fd, DKIOCPARTINFO, &dkpi) < 0) {
			(void) fprintf(stderr, PART_FAIL);
			return (BC_ERROR);
		} else {
			edkpi.p_start = dkpi.p_start;
		}
	}

	for (i = 0; i < FD_NUMPART; i++) {
		part = (struct ipart *)mboot->parts + i;

		if (part->relsect == 0) {
			(void) fprintf(stderr, BAD_PART, i);
			return (BC_ERROR);
		}

		if (edkpi.p_start >= part->relsect &&
		    edkpi.p_start < (part->relsect + part->numsect)) {
			/* Found the partition */
			break;
		}
	}

	if (i == FD_NUMPART) {
		/* No solaris fdisk partitions (primary or logical) */
		(void) fprintf(stderr, NOSOLPAR);
		return (BC_ERROR);
	}

	/*
	 * We have found a Solaris fdisk partition (primary or extended)
	 * Handle the simple case first: Solaris in a primary partition
	 */
	if (!fdisk_is_dos_extended(part->systid)) {
		device->start_sector = part->relsect;
		device->partition = i;
		goto found_part;
	}

	/*
	 * Solaris in a logical partition. Find that partition in the
	 * extended part.
	 */
	if ((rval = libfdisk_init(&epp, device->path_p0, NULL, FDISK_READ_DISK))
	    != FDISK_SUCCESS) {
		switch (rval) {
			/*
			 * The first 3 cases are not an error per-se, just that
			 * there is no Solaris logical partition
			 */
			case FDISK_EBADLOGDRIVE:
			case FDISK_ENOLOGDRIVE:
			case FDISK_EBADMAGIC:
				(void) fprintf(stderr, NOSOLPAR);
				return (BC_ERROR);
			case FDISK_ENOVGEOM:
				(void) fprintf(stderr, NO_VIRT_GEOM);
				return (BC_ERROR);
			case FDISK_ENOPGEOM:
				(void) fprintf(stderr, NO_PHYS_GEOM);
				return (BC_ERROR);
			case FDISK_ENOLGEOM:
				(void) fprintf(stderr, NO_LABEL_GEOM);
				return (BC_ERROR);
			default:
				(void) fprintf(stderr, LIBFDISK_INIT_FAIL);
				return (BC_ERROR);
		}
	}

	rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
	libfdisk_fini(&epp);
	if (rval != FDISK_SUCCESS) {
		/* No solaris logical partition */
		(void) fprintf(stderr, NOSOLPAR);
		return (BC_ERROR);
	}

	device->start_sector = secnum;
	device->partition = pno - 1;
	log_part = 1;

found_part:
	/* get confirmation for -m */
	if (write_mbr && !force_mbr) {
		(void) fprintf(stdout, MBOOT_PROMPT);
		if (!yes()) {
			write_mbr = 0;
			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
			return (BC_ERROR);
		}
	}

	/*
	 * Currently if Solaris is in an extended partition we need to
	 * write GRUB to the MBR. Check for this.
	 */
	if (log_part && !write_mbr) {
		(void) fprintf(stdout, gettext("Installing Solaris on an "
		    "extended partition... forcing MBR update\n"));
		write_mbr = 1;
	}

	/*
	 * warn, if Solaris in primary partition and GRUB not in MBR and
	 * partition is not active
	 */
	if (part != NULL) {
		if (!log_part && part->bootid != 128 && !write_mbr) {
			(void) fprintf(stdout, SOLPAR_INACTIVE,
			    device->partition + 1);
		}
	}

	return (BC_SUCCESS);
}

static int
get_disk_fd(ig_device_t *device)
{
	int	i = 0;
	char	save[2] = { '\0', '\0' };
	char	*end = NULL;

	assert(device != NULL);
	assert(device->path != NULL);

	if (is_bootpar(device->type)) {
		end = strstr(device->path, "p0:boot");
		/* tested at the start of init_device() */
		assert(end != NULL);
		/* chop off :boot */
		save[0] = end[2];
		end[2] = '\0';
	} else {
		i = strlen(device->path);
		save[0] = device->path[i - 2];
		save[1] = device->path[i - 1];
		device->path[i - 2] = 'p';
		device->path[i - 1] = '0';
	}

	if (nowrite)
		device->disk_fd = open(device->path, O_RDONLY);
	else
		device->disk_fd = open(device->path, O_RDWR);

	device->path_p0 = strdup(device->path);
	if (device->path_p0 == NULL) {
		perror("strdup");
		return (BC_ERROR);
	}

	if (is_bootpar(device->type)) {
		end[2] = save[0];
	} else {
		device->path[i - 2] = save[0];
		device->path[i - 1] = save[1];
	}

	if (device->disk_fd == -1) {
		perror("open");
		return (BC_ERROR);
	}

	return (BC_SUCCESS);
}

static void
prepare_fake_multiboot(ig_stage2_t *stage2)
{
	multiboot_header_t	*mboot;

	assert(stage2 != NULL);
	assert(stage2->mboot != NULL);
	assert(stage2->buf != NULL);

	mboot = stage2->mboot;

	/*
	 * Currently we expect find_multiboot() to have located a multiboot
	 * header with the AOUT kludge flag set.
	 */
	assert(mboot->flags & BB_MBOOT_AOUT_FLAG);

	/* Insert the information necessary to locate stage2. */
	mboot->header_addr = stage2->mboot_off;
	mboot->load_addr = 0;
	mboot->load_end_addr = stage2->file_size;
}

static void
add_stage2_einfo(ig_stage2_t *stage2, char *updt_str)
{
	bblk_hs_t	hs;
	uint32_t	avail_space;

	assert(stage2 != NULL);

	/* Fill bootblock hashing source information. */
	hs.src_buf = (unsigned char *)stage2->file;
	hs.src_size = stage2->file_size;
	/* How much space for the extended information structure? */
	avail_space = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
	add_einfo(stage2->extra, updt_str, &hs, avail_space);
}


static int
write_stage2(ig_data_t *install)
{
	ig_device_t		*device = &install->device;
	ig_stage2_t		*stage2 = &install->stage2;
	off_t			offset;

	assert(install != NULL);

	if (is_bootpar(device->type)) {
		/*
		 * stage2 is already on the filesystem, we only need to update
		 * the first two blocks (that we have modified during
		 * prepare_stage2())
		 */
		if (write_out(device->part_fd, stage2->file, SECTOR_SIZE,
		    stage2->pcfs_first_sectors[0] * SECTOR_SIZE)
		    != BC_SUCCESS ||
		    write_out(device->part_fd, stage2->file + SECTOR_SIZE,
		    SECTOR_SIZE, stage2->pcfs_first_sectors[1] * SECTOR_SIZE)
		    != BC_SUCCESS) {
			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
			return (BC_ERROR);
		}
		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
		return (BC_SUCCESS);
	}

	/*
	 * For disk, write stage2 starting at STAGE2_BLKOFF sector.
	 * Note that we use stage2->buf rather than stage2->file, because we
	 * may have extended information after the latter.
	 *
	 * If we're writing to an EFI-labeled disk where stage2 lives in the
	 * 3.5MB boot loader gap following the ZFS vdev labels, make sure the
	 * size of the buffer doesn't exceed the size of the gap.
	 */
	if (is_efi(device->type) && stage2->buf_size > STAGE2_MAXSIZE) {
		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
		return (BC_ERROR);
	}

	offset = STAGE2_BLKOFF(device->type) * SECTOR_SIZE;

	if (write_out(device->part_fd, stage2->buf, stage2->buf_size,
	    offset) != BC_SUCCESS) {
		perror("write");
		return (BC_ERROR);
	}

	/* Simulate the "old" installgrub output. */
	(void) fprintf(stdout, WRITE_STAGE2_DISK, device->partition,
	    (stage2->buf_size / SECTOR_SIZE) + 1, STAGE2_BLKOFF(device->type),
	    stage2->first_sector);

	return (BC_SUCCESS);
}

static int
write_stage1(ig_data_t *install)
{
	ig_device_t	*device = &install->device;

	assert(install != NULL);

	if (write_out(device->part_fd, install->stage1_buf,
	    sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
		(void) fprintf(stdout, WRITE_FAIL_PBOOT);
		perror("write");
		return (BC_ERROR);
	}

	/* Simulate "old" installgrub output. */
	(void) fprintf(stdout, WRITE_PBOOT, device->partition,
	    device->start_sector);

	if (write_mbr) {
		if (write_out(device->disk_fd, install->stage1_buf,
		    sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
			(void) fprintf(stdout, WRITE_FAIL_BOOTSEC);
			perror("write");
			return (BC_ERROR);
		}
		/* Simulate "old" installgrub output. */
		(void) fprintf(stdout, WRITE_MBOOT);
	}

	return (BC_SUCCESS);
}

#define	USAGE_STRING	"%s [-m|-f|-n|-F|-u verstr] stage1 stage2 device\n"    \
			"%s -M [-n] device1 device2\n"			       \
			"%s [-V|-e] -i device\n"			       \

#define	CANON_USAGE_STR	gettext(USAGE_STRING)

static void
usage(char *progname)
{
	(void) fprintf(stdout, CANON_USAGE_STR, progname, progname, progname);
}


static int
read_stage1_from_file(char *path, ig_data_t *dest)
{
	int	fd;

	assert(dest);

	/* read the stage1 file from filesystem */
	fd = open(path, O_RDONLY);
	if (fd == -1 ||
	    read(fd, dest->stage1_buf, SECTOR_SIZE) != SECTOR_SIZE) {
		(void) fprintf(stderr, READ_FAIL_STAGE1, path);
		return (BC_ERROR);
	}
	(void) close(fd);
	return (BC_SUCCESS);
}

static int
read_stage2_from_file(char *path, ig_data_t *dest)
{
	int		fd;
	struct stat	sb;
	ig_stage2_t	*stage2 = &dest->stage2;
	ig_device_t	*device = &dest->device;
	uint32_t	buf_size;

	assert(dest);
	assert(stage2->buf == NULL);

	fd = open(path, O_RDONLY);
	if (fstat(fd, &sb) == -1) {
		perror("fstat");
		goto out;
	}

	stage2->file_size = sb.st_size;

	if (!is_bootpar(device->type)) {
		/*
		 * buffer size needs to account for stage2 plus the extra
		 * versioning information at the end of it. We reserve one
		 * extra sector (plus we round up to the next sector boundary).
		 */
		buf_size = stage2->file_size + SECTOR_SIZE;
	} else {
		/* In the PCFS case we only need to read in stage2. */
		buf_size = stage2->file_size;
	}

	stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);

	BOOT_DEBUG("stage2 buffer size = %d (%d sectors)\n", stage2->buf_size,
	    stage2->buf_size / SECTOR_SIZE);

	stage2->buf = malloc(stage2->buf_size);
	if (stage2->buf == NULL) {
		perror(gettext("Memory allocation failed"));
		goto out_fd;
	}

	stage2->file = stage2->buf;

	/*
	 * Extra information (e.g. the versioning structure) is placed at the
	 * end of stage2, aligned on a 8-byte boundary.
	 */
	if (!(is_bootpar(device->type)))
		stage2->extra = stage2->file + P2ROUNDUP(stage2->file_size, 8);

	if (lseek(fd, 0, SEEK_SET) == -1) {
		perror("lseek");
		goto out_alloc;
	}

	if (read(fd, stage2->file, stage2->file_size) < 0) {
		perror(gettext("unable to read stage2"));
		goto out_alloc;
	}

	(void) close(fd);
	return (BC_SUCCESS);

out_alloc:
	free(stage2->buf);
	stage2->buf = NULL;
out_fd:
	(void) close(fd);
out:
	return (BC_ERROR);
}

static int
prepare_stage1(ig_data_t *install)
{
	ig_device_t	*device = &install->device;

	assert(install != NULL);

	/* If PCFS add the BIOS Parameter Block. */
	if (is_bootpar(device->type)) {
		char	bpb_sect[SECTOR_SIZE];

		if (pread(device->part_fd, bpb_sect, SECTOR_SIZE, 0)
		    != SECTOR_SIZE) {
			(void) fprintf(stderr, READ_FAIL_BPB);
			return (BC_ERROR);
		}
		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
		    install->stage1_buf + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
	}

	/* copy MBR to stage1 in case of overwriting MBR sector. */
	bcopy(device->boot_sector + BOOTSZ, install->stage1_buf + BOOTSZ,
	    SECTOR_SIZE - BOOTSZ);
	/* modify default stage1 file generated by GRUB. */
	*((unsigned char *)(install->stage1_buf + STAGE1_FORCE_LBA)) = 1;
	*((ulong_t *)(install->stage1_buf + STAGE1_STAGE2_SECTOR))
	    = install->stage2.first_sector;
	*((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_ADDRESS))
	    = STAGE2_MEMADDR;
	*((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_SEGMENT))
	    = STAGE2_MEMADDR >> 4;

	return (BC_SUCCESS);
}

/*
 * Grab stage1 from the specified device file descriptor.
 */
static int
read_stage1_from_disk(int dev_fd, char *stage1_buf)
{
	assert(stage1_buf != NULL);

	if (read_in(dev_fd, stage1_buf, SECTOR_SIZE, 0) != BC_SUCCESS) {
		perror(gettext("Unable to read stage1 from disk"));
		return (BC_ERROR);
	}
	return (BC_SUCCESS);
}

static int
read_stage2_from_disk(int dev_fd, ig_stage2_t *stage2, int type)
{
	uint32_t		size;
	uint32_t		buf_size;
	uint32_t		mboot_off;
	multiboot_header_t	*mboot;

	assert(stage2 != NULL);
	assert(dev_fd != -1);

	if (read_in(dev_fd, mboot_scan, sizeof (mboot_scan),
	    STAGE2_BLKOFF(type) * SECTOR_SIZE) != BC_SUCCESS) {
		perror(gettext("Error reading stage2 sectors"));
		return (BC_ERROR);
	}

	/* No multiboot means no chance of knowing stage2 size */
	if (find_multiboot(mboot_scan, sizeof (mboot_scan), &mboot_off)
	    != BC_SUCCESS) {
		BOOT_DEBUG("Unable to find multiboot header\n");
		return (BC_NOEXTRA);
	}
	mboot = (multiboot_header_t *)(mboot_scan + mboot_off);

	/*
	 * Unfilled mboot values mean an older version of installgrub installed
	 * the stage2. Again we have no chance of knowing stage2 size.
	 */
	if (mboot->load_end_addr == 0 ||
	    mboot->load_end_addr < mboot->load_addr)
		return (BC_NOEXTRA);

	/*
	 * Currently, the amount of space reserved for extra information
	 * is "fixed". We may have to scan for the terminating extra payload
	 * in the future.
	 */
	size = mboot->load_end_addr - mboot->load_addr;
	buf_size = P2ROUNDUP(size + SECTOR_SIZE, SECTOR_SIZE);

	stage2->buf = malloc(buf_size);
	if (stage2->buf == NULL) {
		perror(gettext("Memory allocation failed"));
		return (BC_ERROR);
	}
	stage2->buf_size = buf_size;

	if (read_in(dev_fd, stage2->buf, buf_size, STAGE2_BLKOFF(type) *
	    SECTOR_SIZE) != BC_SUCCESS) {
		perror("read");
		free(stage2->buf);
		return (BC_ERROR);
	}

	/* Update pointers. */
	stage2->file = stage2->buf;
	stage2->file_size = size;
	stage2->mboot_off = mboot_off;
	stage2->mboot = (multiboot_header_t *)(stage2->buf + stage2->mboot_off);
	stage2->extra = stage2->buf + P2ROUNDUP(stage2->file_size, 8);
	stage2->extra_size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);

	return (BC_SUCCESS);
}

static boolean_t
is_update_necessary(ig_data_t *data, char *updt_str)
{
	bblk_einfo_t	*einfo;
	bblk_hs_t	stage2_hs;
	ig_stage2_t	stage2_disk;
	ig_stage2_t	*stage2_file = &data->stage2;
	ig_device_t	*device = &data->device;
	int		dev_fd = device->part_fd;

	assert(data != NULL);
	assert(device->part_fd != -1);

	bzero(&stage2_disk, sizeof (ig_stage2_t));

	/* Gather stage2 (if present) from the target device. */
	if (read_stage2_from_disk(dev_fd, &stage2_disk, device->type)
	    != BC_SUCCESS) {
		BOOT_DEBUG("Unable to read stage2 from %s\n", device->path);
		BOOT_DEBUG("No multiboot wrapped stage2 on %s\n", device->path);
		return (B_TRUE);
	}

	/*
	 * Look for the extended information structure in the extra payload
	 * area.
	 */
	einfo = find_einfo(stage2_disk.extra, stage2_disk.extra_size);
	if (einfo == NULL) {
		BOOT_DEBUG("No extended information available\n");
		return (B_TRUE);
	}

	if (!do_version || updt_str == NULL) {
		(void) fprintf(stdout, "WARNING: target device %s has a "
		    "versioned stage2 that is going to be overwritten by a non "
		    "versioned one\n", device->path);
		return (B_TRUE);
	}

	if (force_update) {
		BOOT_DEBUG("Forcing update of %s bootblock\n", device->path);
		return (B_TRUE);
	}

	/* Compare the two extended information structures. */
	stage2_hs.src_buf = (unsigned char *)stage2_file->file;
	stage2_hs.src_size = stage2_file->file_size;

	return (einfo_should_update(einfo, &stage2_hs, updt_str));
}


#define	START_BLOCK(pos)	(*(ulong_t *)(pos))
#define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
#define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))

static int
prepare_stage2(ig_data_t *install, char *updt_str)
{
	ig_device_t	*device = &install->device;
	ig_stage2_t	*stage2 = &install->stage2;
	uint32_t	mboot_off = 0;

	assert(install != NULL);
	assert(stage2->file != NULL);

	/* New stage2 files come with an embedded stage2. */
	if (find_multiboot(stage2->file, stage2->file_size, &mboot_off)
	    != BC_SUCCESS) {
		BOOT_DEBUG("WARNING: no multiboot structure found in stage2, "
		    "are you using an old GRUB stage2?\n");
		if (do_version == B_TRUE) {
			(void) fprintf(stderr, gettext("Versioning requested "
			    "but stage2 does not support it.. skipping.\n"));
			do_version = B_FALSE;
		}
	} else {
		/* Keep track of where the multiboot header is. */
		stage2->mboot_off = mboot_off;
		stage2->mboot = (multiboot_header_t *)(stage2->file +
		    mboot_off);
		if (do_version) {
			/*
			 * Adding stage2 information needs to happen before
			 * we modify the copy of stage2 we have in memory, so
			 * that the hashing reflects the one of the file.
			 * An error here is not fatal.
			 */
			add_stage2_einfo(stage2, updt_str);
		}
		/*
		 * Fill multiboot information. We add them even without
		 * versioning to support as much as possible mirroring.
		 */
		prepare_fake_multiboot(stage2);
	}

	if (is_bootpar(device->type)) {
		uint32_t	blocklist[SECTOR_SIZE / sizeof (uint32_t)];
		uint32_t	install_addr = STAGE2_MEMADDR + SECTOR_SIZE;
		int		i = 0;
		uchar_t		*pos;

		bzero(blocklist, sizeof (blocklist));
		if (read_stage2_blocklist(device->part_fd, blocklist) != 0) {
			(void) fprintf(stderr, gettext("Error reading pcfs "
			    "stage2 blocklist\n"));
			return (BC_ERROR);
		}

		pos = (uchar_t *)stage2->file + STAGE2_BLOCKLIST;
		stage2->first_sector = device->start_sector + blocklist[0];
		stage2->pcfs_first_sectors[0] = blocklist[0];
		BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);


		if (blocklist[1] > 1) {
			blocklist[0]++;
			blocklist[1]--;
		} else {
			i += 2;
		}

		stage2->pcfs_first_sectors[1] = blocklist[i];

		while (blocklist[i]) {
			if (START_BLOCK(pos - 8) != 0 &&
			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
				(void) fprintf(stderr, PCFS_FRAGMENTED);
				return (BC_ERROR);
			}
			START_BLOCK(pos) = blocklist[i] + device->start_sector;
			START_SEG(pos) = (ushort_t)(install_addr >> 4);
			NUM_BLOCK(pos) = blocklist[i + 1];
			install_addr += blocklist[i + 1] * SECTOR_SIZE;
			pos -= 8;
			i += 2;
		}
	} else {
		/* Solaris VTOC & EFI */
		if (device->start_sector >
		    UINT32_MAX - STAGE2_BLKOFF(device->type)) {
			fprintf(stderr, gettext("Error: partition start sector "
			    "must be less than %lld\n"),
			    (uint64_t)UINT32_MAX - STAGE2_BLKOFF(device->type));
			return (BC_ERROR);
		}
		stage2->first_sector = device->start_sector +
		    STAGE2_BLKOFF(device->type);
		BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);
		/*
		 * In a solaris partition, stage2 is written to contiguous
		 * blocks. So we update the starting block only.
		 */
		*((ulong_t *)(stage2->file + STAGE2_BLOCKLIST)) =
		    stage2->first_sector + 1;
	}

	/* force lba and set disk partition */
	*((unsigned char *) (stage2->file + STAGE2_FORCE_LBA)) = 1;
	*((long *)(stage2->file + STAGE2_INSTALLPART))
	    = (device->partition << 16) | (device->slice << 8) | 0xff;

	return (BC_SUCCESS);
}

static int
find_x86_bootpar(struct mboot *mboot, int *part_num, uint32_t *start_sect)
{
	int	i;

	for (i = 0; i < FD_NUMPART; i++) {
		struct ipart	*part;

		part = (struct ipart *)mboot->parts + i;
		if (part->systid == 0xbe) {
			if (start_sect)
				*start_sect = part->relsect;
			if (part_num)
				*part_num = i;
			/* solaris boot part */
			return (BC_SUCCESS);
		}
	}
	return (BC_ERROR);
}

static char *
get_raw_partition_path(ig_device_t *device)
{
	char	*raw;
	int	len;

	if (is_bootpar(device->type)) {
		int		part;
		struct mboot	*mboot;

		mboot = (struct mboot *)device->boot_sector;
		if (find_x86_bootpar(mboot, &part, NULL) != BC_SUCCESS) {
			(void) fprintf(stderr, BOOTPAR_NOTFOUND,
			    device->path_p0);
			return (NULL);
		}

		raw = strdup(device->path_p0);
		if (raw == NULL) {
			perror(gettext("Memory allocation failed"));
			return (NULL);
		}

		raw[strlen(raw) - 2] = '1' + part;
		return (raw);
	}

	/* For disk, remember slice and return whole fdisk partition  */
	raw = strdup(device->path);
	if (raw == NULL) {
		perror(gettext("Memory allocation failed"));
		return (NULL);
	}

	len = strlen(raw);
	if (!is_efi(device->type) &&
	    (raw[len - 2] != 's' || raw[len - 1] == '2')) {
		(void) fprintf(stderr, NOT_ROOT_SLICE);
		free(raw);
		return (NULL);
	}
	device->slice = atoi(&raw[len - 1]);

	if (!is_efi(device->type)) {
		raw[len - 2] = 's';
		raw[len - 1] = '2';
	}

	return (raw);
}

static int
get_raw_partition_fd(ig_device_t *device)
{
	struct stat	stat = {0};
	char		*raw;

	raw = get_raw_partition_path(device);
	if (raw == NULL)
		return (BC_ERROR);

	if (nowrite)
		device->part_fd = open(raw, O_RDONLY);
	else
		device->part_fd = open(raw, O_RDWR);

	if (device->part_fd < 0 || fstat(device->part_fd, &stat) != 0) {
		(void) fprintf(stderr, OPEN_FAIL, raw);
		free(raw);
		return (BC_ERROR);
	}

	if (S_ISCHR(stat.st_mode) == 0) {
		(void) fprintf(stderr, NOT_RAW_DEVICE, raw);
		(void) close(device->part_fd);
		device->part_fd = -1;
		free(raw);
		return (BC_ERROR);
	}

	free(raw);
	return (BC_SUCCESS);
}