17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d33344bbSsy  * Common Development and Distribution License (the "License").
6d33344bbSsy  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22af28f636SEnrico Perla - Sun Microsystems  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2333f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
24cc641e8dSToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
259890706eSHans Rosenfeld  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <libgen.h>
317c478bd9Sstevel@tonic-gate #include <malloc.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
36af28f636SEnrico Perla - Sun Microsystems #include <libintl.h>
37af28f636SEnrico Perla - Sun Microsystems #include <locale.h>
38af28f636SEnrico Perla - Sun Microsystems #include <errno.h>
39af28f636SEnrico Perla - Sun Microsystems #include <libfdisk.h>
40af28f636SEnrico Perla - Sun Microsystems #include <stdarg.h>
41af28f636SEnrico Perla - Sun Microsystems #include <assert.h>
42af28f636SEnrico Perla - Sun Microsystems 
437c478bd9Sstevel@tonic-gate #include <sys/mount.h>
447c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
457c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
46d33344bbSsy #include <sys/dkio.h>
47d33344bbSsy #include <sys/vtoc.h>
48af28f636SEnrico Perla - Sun Microsystems #include <sys/types.h>
49af28f636SEnrico Perla - Sun Microsystems #include <sys/stat.h>
50af28f636SEnrico Perla - Sun Microsystems #include <sys/multiboot.h>
51af28f636SEnrico Perla - Sun Microsystems #include <sys/sysmacros.h>
521a902ef8SHans Rosenfeld #include <sys/efi_partition.h>
531a902ef8SHans Rosenfeld 
541a902ef8SHans Rosenfeld #include <libnvpair.h>
551a902ef8SHans Rosenfeld #include <libfstyp.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "message.h"
58af28f636SEnrico Perla - Sun Microsystems #include "installgrub.h"
59af28f636SEnrico Perla - Sun Microsystems #include "./../common/bblk_einfo.h"
60af28f636SEnrico Perla - Sun Microsystems #include "./../common/boot_utils.h"
61af28f636SEnrico Perla - Sun Microsystems #include "./../common/mboot_extra.h"
6221ba817cSToomas Soome #include "getresponse.h"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
657c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
68af28f636SEnrico Perla - Sun Microsystems /*
69af28f636SEnrico Perla - Sun Microsystems  * Variables to track installgrub desired mode of operation.
70af28f636SEnrico Perla - Sun Microsystems  * 'nowrite' and 'boot_debug' come from boot_common.h.
71af28f636SEnrico Perla - Sun Microsystems  */
72af28f636SEnrico Perla - Sun Microsystems static boolean_t write_mbr = B_FALSE;
73af28f636SEnrico Perla - Sun Microsystems static boolean_t force_mbr = B_FALSE;
74af28f636SEnrico Perla - Sun Microsystems static boolean_t force_update = B_FALSE;
75af28f636SEnrico Perla - Sun Microsystems static boolean_t do_getinfo = B_FALSE;
76af28f636SEnrico Perla - Sun Microsystems static boolean_t do_version = B_FALSE;
77af28f636SEnrico Perla - Sun Microsystems static boolean_t do_mirror_bblk = B_FALSE;
78af28f636SEnrico Perla - Sun Microsystems static boolean_t strip = B_FALSE;
79af28f636SEnrico Perla - Sun Microsystems static boolean_t verbose_dump = B_FALSE;
80af28f636SEnrico Perla - Sun Microsystems 
81af28f636SEnrico Perla - Sun Microsystems /* Installing the bootblock is the default operation. */
82af28f636SEnrico Perla - Sun Microsystems static boolean_t do_install = B_TRUE;
83af28f636SEnrico Perla - Sun Microsystems 
84af28f636SEnrico Perla - Sun Microsystems /* Versioning string, if present. */
85af28f636SEnrico Perla - Sun Microsystems static char *update_str;
86af28f636SEnrico Perla - Sun Microsystems 
87af28f636SEnrico Perla - Sun Microsystems /*
88af28f636SEnrico Perla - Sun Microsystems  * Temporary buffer to store the first 32K of data looking for a multiboot
89af28f636SEnrico Perla - Sun Microsystems  * signature.
90af28f636SEnrico Perla - Sun Microsystems  */
91af28f636SEnrico Perla - Sun Microsystems char	mboot_scan[MBOOT_SCAN_SIZE];
92af28f636SEnrico Perla - Sun Microsystems 
93af28f636SEnrico Perla - Sun Microsystems /* Function prototypes. */
94af28f636SEnrico Perla - Sun Microsystems static void check_options(char *);
95af28f636SEnrico Perla - Sun Microsystems static int handle_install(char *, char **);
96af28f636SEnrico Perla - Sun Microsystems static int handle_mirror(char *, char **);
97af28f636SEnrico Perla - Sun Microsystems static int handle_getinfo(char *, char **);
98af28f636SEnrico Perla - Sun Microsystems static int commit_to_disk(ig_data_t *, char *);
99af28f636SEnrico Perla - Sun Microsystems static int init_device(ig_device_t *, char *path);
100af28f636SEnrico Perla - Sun Microsystems static void cleanup_device(ig_device_t *);
101af28f636SEnrico Perla - Sun Microsystems static void cleanup_stage2(ig_stage2_t *);
102af28f636SEnrico Perla - Sun Microsystems static int get_start_sector(ig_device_t *);
103af28f636SEnrico Perla - Sun Microsystems static int get_disk_fd(ig_device_t *device);
104af28f636SEnrico Perla - Sun Microsystems static int get_raw_partition_fd(ig_device_t *);
105af28f636SEnrico Perla - Sun Microsystems static char *get_raw_partition_path(ig_device_t *);
106af28f636SEnrico Perla - Sun Microsystems static int propagate_bootblock(ig_data_t *, ig_data_t *, char *);
107af28f636SEnrico Perla - Sun Microsystems static int find_x86_bootpar(struct mboot *, int *, uint32_t *);
108af28f636SEnrico Perla - Sun Microsystems static int write_stage2(ig_data_t *);
109af28f636SEnrico Perla - Sun Microsystems static int write_stage1(ig_data_t *);
1107c478bd9Sstevel@tonic-gate static void usage(char *);
111af28f636SEnrico Perla - Sun Microsystems static int read_stage1_from_file(char *, ig_data_t *);
112af28f636SEnrico Perla - Sun Microsystems static int read_stage2_from_file(char *, ig_data_t *);
113af28f636SEnrico Perla - Sun Microsystems static int read_stage1_from_disk(int, char *);
1141a902ef8SHans Rosenfeld static int read_stage2_from_disk(int, ig_stage2_t *, int);
115af28f636SEnrico Perla - Sun Microsystems static int prepare_stage1(ig_data_t *);
116af28f636SEnrico Perla - Sun Microsystems static int prepare_stage2(ig_data_t *, char *);
117af28f636SEnrico Perla - Sun Microsystems static void prepare_fake_multiboot(ig_stage2_t *);
118af28f636SEnrico Perla - Sun Microsystems static void add_stage2_einfo(ig_stage2_t *, char *updt_str);
119af28f636SEnrico Perla - Sun Microsystems static boolean_t is_update_necessary(ig_data_t *, char *);
1207c478bd9Sstevel@tonic-gate 
121342440ecSPrasad Singamsetty extern int read_stage2_blocklist(int, unsigned int *);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1247c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1257c478bd9Sstevel@tonic-gate {
126af28f636SEnrico Perla - Sun Microsystems 	int	opt;
127af28f636SEnrico Perla - Sun Microsystems 	int	params = 3;
128af28f636SEnrico Perla - Sun Microsystems 	int	ret;
129af28f636SEnrico Perla - Sun Microsystems 	char	**handle_args;
130af28f636SEnrico Perla - Sun Microsystems 	char	*progname;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1337c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
13421ba817cSToomas Soome 	if (init_yes() < 0) {
13521ba817cSToomas Soome 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
13621ba817cSToomas Soome 		    strerror(errno));
13721ba817cSToomas Soome 		exit(BC_ERROR);
13821ba817cSToomas Soome 	}
1397c478bd9Sstevel@tonic-gate 
140af28f636SEnrico Perla - Sun Microsystems 	/*
141af28f636SEnrico Perla - Sun Microsystems 	 * retro-compatibility: installing the bootblock is the default
142af28f636SEnrico Perla - Sun Microsystems 	 * and there is no switch for it.
143af28f636SEnrico Perla - Sun Microsystems 	 */
144af28f636SEnrico Perla - Sun Microsystems 	do_install = B_TRUE;
145af28f636SEnrico Perla - Sun Microsystems 
146af28f636SEnrico Perla - Sun Microsystems 	while ((opt = getopt(argc, argv, "dVMFfmneiu:")) != EOF) {
1477c478bd9Sstevel@tonic-gate 		switch (opt) {
1487c478bd9Sstevel@tonic-gate 		case 'm':
149af28f636SEnrico Perla - Sun Microsystems 			write_mbr = B_TRUE;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'n':
152af28f636SEnrico Perla - Sun Microsystems 			nowrite = B_TRUE;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'f':
155af28f636SEnrico Perla - Sun Microsystems 			force_mbr = B_TRUE;
1567c478bd9Sstevel@tonic-gate 			break;
1577ce76caaSEnrico Perla - Sun Microsystems 		case 'i':
158af28f636SEnrico Perla - Sun Microsystems 			do_getinfo = B_TRUE;
159af28f636SEnrico Perla - Sun Microsystems 			do_install = B_FALSE;
1607ce76caaSEnrico Perla - Sun Microsystems 			params = 1;
1617ce76caaSEnrico Perla - Sun Microsystems 			break;
162af28f636SEnrico Perla - Sun Microsystems 		case 'V':
163af28f636SEnrico Perla - Sun Microsystems 			verbose_dump = B_TRUE;
164af28f636SEnrico Perla - Sun Microsystems 			break;
165af28f636SEnrico Perla - Sun Microsystems 		case 'd':
166af28f636SEnrico Perla - Sun Microsystems 			boot_debug = B_TRUE;
167af28f636SEnrico Perla - Sun Microsystems 			break;
168af28f636SEnrico Perla - Sun Microsystems 		case 'F':
169af28f636SEnrico Perla - Sun Microsystems 			force_update = B_TRUE;
170af28f636SEnrico Perla - Sun Microsystems 			break;
1717ce76caaSEnrico Perla - Sun Microsystems 		case 'e':
172af28f636SEnrico Perla - Sun Microsystems 			strip = B_TRUE;
173af28f636SEnrico Perla - Sun Microsystems 			break;
174af28f636SEnrico Perla - Sun Microsystems 		case 'M':
175af28f636SEnrico Perla - Sun Microsystems 			do_mirror_bblk = B_TRUE;
176af28f636SEnrico Perla - Sun Microsystems 			do_install = B_FALSE;
177af28f636SEnrico Perla - Sun Microsystems 			params = 2;
1787ce76caaSEnrico Perla - Sun Microsystems 			break;
179af28f636SEnrico Perla - Sun Microsystems 		case 'u':
180af28f636SEnrico Perla - Sun Microsystems 			do_version = B_TRUE;
181af28f636SEnrico Perla - Sun Microsystems 
182af28f636SEnrico Perla - Sun Microsystems 			update_str = malloc(strlen(optarg) + 1);
183af28f636SEnrico Perla - Sun Microsystems 			if (update_str == NULL) {
184af28f636SEnrico Perla - Sun Microsystems 				(void) fprintf(stderr, gettext("Unable to "
185af28f636SEnrico Perla - Sun Microsystems 				    "allocate memory\n"));
186af28f636SEnrico Perla - Sun Microsystems 				exit(BC_ERROR);
187af28f636SEnrico Perla - Sun Microsystems 			}
188af28f636SEnrico Perla - Sun Microsystems 			(void) strlcpy(update_str, optarg, strlen(optarg) + 1);
1897ce76caaSEnrico Perla - Sun Microsystems 			break;
1907c478bd9Sstevel@tonic-gate 		default:
1917c478bd9Sstevel@tonic-gate 			/* fall through to process non-optional args */
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/* check arguments */
1977ce76caaSEnrico Perla - Sun Microsystems 	if (argc != optind + params) {
1987c478bd9Sstevel@tonic-gate 		usage(argv[0]);
199af28f636SEnrico Perla - Sun Microsystems 		exit(BC_ERROR);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
202af28f636SEnrico Perla - Sun Microsystems 	/*
203af28f636SEnrico Perla - Sun Microsystems 	 * clean up options (and bail out if an unrecoverable combination is
204af28f636SEnrico Perla - Sun Microsystems 	 * requested.
205af28f636SEnrico Perla - Sun Microsystems 	 */
206af28f636SEnrico Perla - Sun Microsystems 	progname = argv[0];
207af28f636SEnrico Perla - Sun Microsystems 	check_options(progname);
208af28f636SEnrico Perla - Sun Microsystems 	handle_args = argv + optind;
209af28f636SEnrico Perla - Sun Microsystems 
210af28f636SEnrico Perla - Sun Microsystems 	if (nowrite)
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, DRY_RUN);
212af28f636SEnrico Perla - Sun Microsystems 
213af28f636SEnrico Perla - Sun Microsystems 	if (do_getinfo) {
214af28f636SEnrico Perla - Sun Microsystems 		ret = handle_getinfo(progname, handle_args);
215af28f636SEnrico Perla - Sun Microsystems 	} else if (do_mirror_bblk) {
216af28f636SEnrico Perla - Sun Microsystems 		ret = handle_mirror(progname, handle_args);
217af28f636SEnrico Perla - Sun Microsystems 	} else {
218af28f636SEnrico Perla - Sun Microsystems 		ret = handle_install(progname, handle_args);
219af28f636SEnrico Perla - Sun Microsystems 	}
220af28f636SEnrico Perla - Sun Microsystems 	return (ret);
221af28f636SEnrico Perla - Sun Microsystems }
222af28f636SEnrico Perla - Sun Microsystems 
223af28f636SEnrico Perla - Sun Microsystems #define	MEANINGLESS_OPT	gettext("%s specified but meaningless, ignoring\n")
224af28f636SEnrico Perla - Sun Microsystems static void
check_options(char * progname)225af28f636SEnrico Perla - Sun Microsystems check_options(char *progname)
226af28f636SEnrico Perla - Sun Microsystems {
227af28f636SEnrico Perla - Sun Microsystems 	if (do_getinfo && do_mirror_bblk) {
228af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Only one of -M and -i can be "
229af28f636SEnrico Perla - Sun Microsystems 		    "specified at the same time\n"));
230af28f636SEnrico Perla - Sun Microsystems 		usage(progname);
231af28f636SEnrico Perla - Sun Microsystems 		exit(BC_ERROR);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
234af28f636SEnrico Perla - Sun Microsystems 	if (do_mirror_bblk) {
235af28f636SEnrico Perla - Sun Microsystems 		/*
236af28f636SEnrico Perla - Sun Microsystems 		 * -u and -F may actually reflect a user intent that is not
237af28f636SEnrico Perla - Sun Microsystems 		 * correct with this command (mirror can be interpreted
238af28f636SEnrico Perla - Sun Microsystems 		 * "similar" to install. Emit a message and continue.
239af28f636SEnrico Perla - Sun Microsystems 		 * -e and -V have no meaning, be quiet here and only report the
240af28f636SEnrico Perla - Sun Microsystems 		 * incongruence if a debug output is requested.
241af28f636SEnrico Perla - Sun Microsystems 		 */
242af28f636SEnrico Perla - Sun Microsystems 		if (do_version) {
243af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, MEANINGLESS_OPT, "-u");
244af28f636SEnrico Perla - Sun Microsystems 			do_version = B_FALSE;
245af28f636SEnrico Perla - Sun Microsystems 		}
246af28f636SEnrico Perla - Sun Microsystems 		if (force_update) {
247af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, MEANINGLESS_OPT, "-F");
248af28f636SEnrico Perla - Sun Microsystems 			force_update = B_FALSE;
2497ce76caaSEnrico Perla - Sun Microsystems 		}
250af28f636SEnrico Perla - Sun Microsystems 		if (strip || verbose_dump) {
251af28f636SEnrico Perla - Sun Microsystems 			BOOT_DEBUG(MEANINGLESS_OPT, "-e|-V");
252af28f636SEnrico Perla - Sun Microsystems 			strip = B_FALSE;
253af28f636SEnrico Perla - Sun Microsystems 			verbose_dump = B_FALSE;
254af28f636SEnrico Perla - Sun Microsystems 		}
255af28f636SEnrico Perla - Sun Microsystems 	}
2567c478bd9Sstevel@tonic-gate 
257af28f636SEnrico Perla - Sun Microsystems 	if (do_getinfo) {
258af28f636SEnrico Perla - Sun Microsystems 		if (write_mbr || force_mbr || do_version || force_update) {
259af28f636SEnrico Perla - Sun Microsystems 			BOOT_DEBUG(MEANINGLESS_OPT, "-m|-f|-u|-F");
260af28f636SEnrico Perla - Sun Microsystems 			write_mbr = force_mbr = do_version = B_FALSE;
261af28f636SEnrico Perla - Sun Microsystems 			force_update = B_FALSE;
2627ce76caaSEnrico Perla - Sun Microsystems 		}
2637c478bd9Sstevel@tonic-gate 	}
264af28f636SEnrico Perla - Sun Microsystems }
2657c478bd9Sstevel@tonic-gate 
266af28f636SEnrico Perla - Sun Microsystems /*
267af28f636SEnrico Perla - Sun Microsystems  * Install a new stage1/stage2 pair on the specified device. handle_install()
268af28f636SEnrico Perla - Sun Microsystems  * expects argv to contain 3 parameters (the path to stage1, the path to stage2,
269af28f636SEnrico Perla - Sun Microsystems  * the target device).
270af28f636SEnrico Perla - Sun Microsystems  *
271af28f636SEnrico Perla - Sun Microsystems  * Returns:	BC_SUCCESS - if the installation is successful
272af28f636SEnrico Perla - Sun Microsystems  *		BC_ERROR   - if the installation failed
273af28f636SEnrico Perla - Sun Microsystems  *		BC_NOUPDT  - if no installation was performed because the GRUB
274af28f636SEnrico Perla - Sun Microsystems  *		             version currently installed is more recent than the
275af28f636SEnrico Perla - Sun Microsystems  *			     supplied one.
276af28f636SEnrico Perla - Sun Microsystems  *
277af28f636SEnrico Perla - Sun Microsystems  */
278af28f636SEnrico Perla - Sun Microsystems static int
handle_install(char * progname,char ** argv)279af28f636SEnrico Perla - Sun Microsystems handle_install(char *progname, char **argv)
280af28f636SEnrico Perla - Sun Microsystems {
281af28f636SEnrico Perla - Sun Microsystems 	ig_data_t	install_data;
282af28f636SEnrico Perla - Sun Microsystems 	char		*stage1_path = NULL;
283af28f636SEnrico Perla - Sun Microsystems 	char		*stage2_path = NULL;
284af28f636SEnrico Perla - Sun Microsystems 	char		*device_path = NULL;
285af28f636SEnrico Perla - Sun Microsystems 	int		ret = BC_ERROR;
286af28f636SEnrico Perla - Sun Microsystems 
287af28f636SEnrico Perla - Sun Microsystems 	stage1_path = strdup(argv[0]);
288af28f636SEnrico Perla - Sun Microsystems 	stage2_path = strdup(argv[1]);
289af28f636SEnrico Perla - Sun Microsystems 	device_path = strdup(argv[2]);
290af28f636SEnrico Perla - Sun Microsystems 
291af28f636SEnrico Perla - Sun Microsystems 	bzero(&install_data, sizeof (ig_data_t));
292af28f636SEnrico Perla - Sun Microsystems 
293af28f636SEnrico Perla - Sun Microsystems 	if (!stage1_path || !stage2_path || !device_path) {
294af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Missing parameter"));
295af28f636SEnrico Perla - Sun Microsystems 		usage(progname);
296af28f636SEnrico Perla - Sun Microsystems 		goto out;
297af28f636SEnrico Perla - Sun Microsystems 	}
2987c478bd9Sstevel@tonic-gate 
299af28f636SEnrico Perla - Sun Microsystems 	BOOT_DEBUG("stage1 path: %s, stage2 path: %s, device: %s\n",
300af28f636SEnrico Perla - Sun Microsystems 	    stage1_path, stage2_path, device_path);
301af28f636SEnrico Perla - Sun Microsystems 
302af28f636SEnrico Perla - Sun Microsystems 	if (init_device(&install_data.device, device_path) != BC_SUCCESS) {
303af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Unable to gather device "
304af28f636SEnrico Perla - Sun Microsystems 		    "information for %s\n"), device_path);
305af28f636SEnrico Perla - Sun Microsystems 		goto out;
306af28f636SEnrico Perla - Sun Microsystems 	}
307af28f636SEnrico Perla - Sun Microsystems 
308af28f636SEnrico Perla - Sun Microsystems 	/* read in stage1 and stage2. */
309af28f636SEnrico Perla - Sun Microsystems 	if (read_stage1_from_file(stage1_path, &install_data) != BC_SUCCESS) {
310af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error opening %s\n"),
311af28f636SEnrico Perla - Sun Microsystems 		    stage1_path);
312af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
3137ce76caaSEnrico Perla - Sun Microsystems 	}
3147ce76caaSEnrico Perla - Sun Microsystems 
315af28f636SEnrico Perla - Sun Microsystems 	if (read_stage2_from_file(stage2_path, &install_data) != BC_SUCCESS) {
316af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error opening %s\n"),
317af28f636SEnrico Perla - Sun Microsystems 		    stage2_path);
318af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
319af28f636SEnrico Perla - Sun Microsystems 	}
3207c478bd9Sstevel@tonic-gate 
321af28f636SEnrico Perla - Sun Microsystems 	/* We do not support versioning on PCFS. */
322af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(install_data.device.type) && do_version)
323af28f636SEnrico Perla - Sun Microsystems 		do_version = B_FALSE;
3247ce76caaSEnrico Perla - Sun Microsystems 
325af28f636SEnrico Perla - Sun Microsystems 	/*
326af28f636SEnrico Perla - Sun Microsystems 	 * is_update_necessary() will take care of checking if versioning and/or
327af28f636SEnrico Perla - Sun Microsystems 	 * forcing the update have been specified. It will also emit a warning
328af28f636SEnrico Perla - Sun Microsystems 	 * if a non-versioned update is attempted over a versioned bootblock.
329af28f636SEnrico Perla - Sun Microsystems 	 */
330af28f636SEnrico Perla - Sun Microsystems 	if (!is_update_necessary(&install_data, update_str)) {
331af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("GRUB version installed "
332af28f636SEnrico Perla - Sun Microsystems 		    "on %s is more recent or identical\n"
333af28f636SEnrico Perla - Sun Microsystems 		    "Use -F to override or install without the -u option\n"),
334af28f636SEnrico Perla - Sun Microsystems 		    device_path);
335af28f636SEnrico Perla - Sun Microsystems 		ret = BC_NOUPDT;
336af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
3377c478bd9Sstevel@tonic-gate 	}
338af28f636SEnrico Perla - Sun Microsystems 	/*
339af28f636SEnrico Perla - Sun Microsystems 	 * We get here if:
340af28f636SEnrico Perla - Sun Microsystems 	 * - the installed GRUB version is older than the one about to be
341af28f636SEnrico Perla - Sun Microsystems 	 *   installed.
342af28f636SEnrico Perla - Sun Microsystems 	 * - no versioning string has been passed through the command line.
343af28f636SEnrico Perla - Sun Microsystems 	 * - a forced update is requested (-F).
344af28f636SEnrico Perla - Sun Microsystems 	 */
345af28f636SEnrico Perla - Sun Microsystems 	BOOT_DEBUG("Ready to commit to disk\n");
346af28f636SEnrico Perla - Sun Microsystems 	ret = commit_to_disk(&install_data, update_str);
347af28f636SEnrico Perla - Sun Microsystems 
348af28f636SEnrico Perla - Sun Microsystems out_dev:
349af28f636SEnrico Perla - Sun Microsystems 	cleanup_device(&install_data.device);
350af28f636SEnrico Perla - Sun Microsystems out:
351af28f636SEnrico Perla - Sun Microsystems 	free(stage1_path);
352af28f636SEnrico Perla - Sun Microsystems 	free(stage2_path);
353af28f636SEnrico Perla - Sun Microsystems 	free(device_path);
354af28f636SEnrico Perla - Sun Microsystems 	return (ret);
355af28f636SEnrico Perla - Sun Microsystems }
3567c478bd9Sstevel@tonic-gate 
357af28f636SEnrico Perla - Sun Microsystems /*
358af28f636SEnrico Perla - Sun Microsystems  * Retrieves from a device the extended information (einfo) associated to the
359af28f636SEnrico Perla - Sun Microsystems  * installed stage2.
360af28f636SEnrico Perla - Sun Microsystems  * Expects one parameter, the device path, in the form: /dev/rdsk/c?[t?]d?s0.
361af28f636SEnrico Perla - Sun Microsystems  * Returns:
362af28f636SEnrico Perla - Sun Microsystems  *        - BC_SUCCESS (and prints out einfo contents depending on 'flags')
363af28f636SEnrico Perla - Sun Microsystems  *	  - BC_ERROR (on error)
364af28f636SEnrico Perla - Sun Microsystems  *        - BC_NOEINFO (no extended information available)
365af28f636SEnrico Perla - Sun Microsystems  */
366af28f636SEnrico Perla - Sun Microsystems static int
handle_getinfo(char * progname,char ** argv)367af28f636SEnrico Perla - Sun Microsystems handle_getinfo(char *progname, char **argv)
368af28f636SEnrico Perla - Sun Microsystems {
369af28f636SEnrico Perla - Sun Microsystems 	ig_data_t	data;
370af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2 = &data.stage2;
371af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &data.device;
372af28f636SEnrico Perla - Sun Microsystems 	bblk_einfo_t	*einfo;
373af28f636SEnrico Perla - Sun Microsystems 	uint8_t		flags = 0;
374af28f636SEnrico Perla - Sun Microsystems 	uint32_t	size;
375af28f636SEnrico Perla - Sun Microsystems 	char		*device_path;
376af28f636SEnrico Perla - Sun Microsystems 	int		retval = BC_ERROR;
377af28f636SEnrico Perla - Sun Microsystems 	int		ret;
378af28f636SEnrico Perla - Sun Microsystems 
379af28f636SEnrico Perla - Sun Microsystems 	device_path = strdup(argv[0]);
380af28f636SEnrico Perla - Sun Microsystems 	if (!device_path) {
381af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Missing parameter"));
382af28f636SEnrico Perla - Sun Microsystems 		usage(progname);
383af28f636SEnrico Perla - Sun Microsystems 		goto out;
384af28f636SEnrico Perla - Sun Microsystems 	}
385af28f636SEnrico Perla - Sun Microsystems 
386af28f636SEnrico Perla - Sun Microsystems 	bzero(&data, sizeof (ig_data_t));
387af28f636SEnrico Perla - Sun Microsystems 	BOOT_DEBUG("device path: %s\n", device_path);
3887c478bd9Sstevel@tonic-gate 
389af28f636SEnrico Perla - Sun Microsystems 	if (init_device(device, device_path) != BC_SUCCESS) {
390af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Unable to gather device "
391af28f636SEnrico Perla - Sun Microsystems 		    "information for %s\n"), device_path);
392af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
393af28f636SEnrico Perla - Sun Microsystems 	}
3947c478bd9Sstevel@tonic-gate 
395af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
396af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Versioning not supported on "
397af28f636SEnrico Perla - Sun Microsystems 		    "PCFS\n"));
398af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
399af28f636SEnrico Perla - Sun Microsystems 	}
4007c478bd9Sstevel@tonic-gate 
4011a902ef8SHans Rosenfeld 	ret = read_stage2_from_disk(device->part_fd, stage2, device->type);
402af28f636SEnrico Perla - Sun Microsystems 	if (ret == BC_ERROR) {
403af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error reading stage2 from "
404af28f636SEnrico Perla - Sun Microsystems 		    "%s\n"), device_path);
405af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
406af28f636SEnrico Perla - Sun Microsystems 	}
4077ce76caaSEnrico Perla - Sun Microsystems 
408af28f636SEnrico Perla - Sun Microsystems 	if (ret == BC_NOEXTRA) {
409af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, gettext("No multiboot header found on "
410af28f636SEnrico Perla - Sun Microsystems 		    "%s, unable to locate extra information area\n"),
411af28f636SEnrico Perla - Sun Microsystems 		    device_path);
412af28f636SEnrico Perla - Sun Microsystems 		retval = BC_NOEINFO;
413af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
414af28f636SEnrico Perla - Sun Microsystems 	}
4157c478bd9Sstevel@tonic-gate 
41614d44f22SHans Rosenfeld 	einfo = find_einfo(stage2->extra, stage2->extra_size);
417af28f636SEnrico Perla - Sun Microsystems 	if (einfo == NULL) {
418af28f636SEnrico Perla - Sun Microsystems 		retval = BC_NOEINFO;
419af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("No extended information "
420af28f636SEnrico Perla - Sun Microsystems 		    "found\n"));
421af28f636SEnrico Perla - Sun Microsystems 		goto out_dev;
422af28f636SEnrico Perla - Sun Microsystems 	}
423af28f636SEnrico Perla - Sun Microsystems 
424af28f636SEnrico Perla - Sun Microsystems 	/* Print the extended information. */
425af28f636SEnrico Perla - Sun Microsystems 	if (strip)
426af28f636SEnrico Perla - Sun Microsystems 		flags |= EINFO_EASY_PARSE;
427af28f636SEnrico Perla - Sun Microsystems 	if (verbose_dump)
428af28f636SEnrico Perla - Sun Microsystems 		flags |= EINFO_PRINT_HEADER;
429af28f636SEnrico Perla - Sun Microsystems 
430af28f636SEnrico Perla - Sun Microsystems 	size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
431af28f636SEnrico Perla - Sun Microsystems 	print_einfo(flags, einfo, size);
432af28f636SEnrico Perla - Sun Microsystems 	retval = BC_SUCCESS;
433af28f636SEnrico Perla - Sun Microsystems 
434af28f636SEnrico Perla - Sun Microsystems out_dev:
435af28f636SEnrico Perla - Sun Microsystems 	cleanup_device(&data.device);
436af28f636SEnrico Perla - Sun Microsystems out:
437af28f636SEnrico Perla - Sun Microsystems 	free(device_path);
438af28f636SEnrico Perla - Sun Microsystems 	return (retval);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
441af28f636SEnrico Perla - Sun Microsystems /*
442af28f636SEnrico Perla - Sun Microsystems  * Attempt to mirror (propagate) the current stage2 over the attaching disk.
443af28f636SEnrico Perla - Sun Microsystems  *
444af28f636SEnrico Perla - Sun Microsystems  * Returns:
445af28f636SEnrico Perla - Sun Microsystems  *	- BC_SUCCESS (a successful propagation happened)
446af28f636SEnrico Perla - Sun Microsystems  *	- BC_ERROR (an error occurred)
447af28f636SEnrico Perla - Sun Microsystems  *	- BC_NOEXTRA (it is not possible to dump the current bootblock since
448af28f636SEnrico Perla - Sun Microsystems  *			there is no multiboot information)
449af28f636SEnrico Perla - Sun Microsystems  */
450af28f636SEnrico Perla - Sun Microsystems static int
handle_mirror(char * progname,char ** argv)451af28f636SEnrico Perla - Sun Microsystems handle_mirror(char *progname, char **argv)
4527c478bd9Sstevel@tonic-gate {
453af28f636SEnrico Perla - Sun Microsystems 	ig_data_t	curr_data;
454af28f636SEnrico Perla - Sun Microsystems 	ig_data_t	attach_data;
455af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*curr_device = &curr_data.device;
456af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*attach_device = &attach_data.device;
457af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2_curr = &curr_data.stage2;
458af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2_attach = &attach_data.stage2;
459af28f636SEnrico Perla - Sun Microsystems 	bblk_einfo_t	*einfo_curr = NULL;
460af28f636SEnrico Perla - Sun Microsystems 	char		*curr_device_path;
461af28f636SEnrico Perla - Sun Microsystems 	char		*attach_device_path;
462af28f636SEnrico Perla - Sun Microsystems 	char		*updt_str = NULL;
463af28f636SEnrico Perla - Sun Microsystems 	int		retval = BC_ERROR;
464af28f636SEnrico Perla - Sun Microsystems 	int		ret;
465af28f636SEnrico Perla - Sun Microsystems 
466af28f636SEnrico Perla - Sun Microsystems 	curr_device_path = strdup(argv[0]);
467af28f636SEnrico Perla - Sun Microsystems 	attach_device_path = strdup(argv[1]);
468af28f636SEnrico Perla - Sun Microsystems 
469af28f636SEnrico Perla - Sun Microsystems 	if (!curr_device_path || !attach_device_path) {
470af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Missing parameter"));
471af28f636SEnrico Perla - Sun Microsystems 		usage(progname);
472af28f636SEnrico Perla - Sun Microsystems 		goto out;
473af28f636SEnrico Perla - Sun Microsystems 	}
474af28f636SEnrico Perla - Sun Microsystems 	BOOT_DEBUG("Current device path is: %s, attaching device path is: "
475af28f636SEnrico Perla - Sun Microsystems 	    " %s\n", curr_device_path, attach_device_path);
476af28f636SEnrico Perla - Sun Microsystems 
477af28f636SEnrico Perla - Sun Microsystems 	bzero(&curr_data, sizeof (ig_data_t));
478af28f636SEnrico Perla - Sun Microsystems 	bzero(&attach_data, sizeof (ig_data_t));
479af28f636SEnrico Perla - Sun Microsystems 
480af28f636SEnrico Perla - Sun Microsystems 	if (init_device(curr_device, curr_device_path) != BC_SUCCESS) {
481af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Unable to gather device "
482af28f636SEnrico Perla - Sun Microsystems 		    "information for %s (current device)\n"), curr_device_path);
483af28f636SEnrico Perla - Sun Microsystems 		goto out_currdev;
484af28f636SEnrico Perla - Sun Microsystems 	}
485af28f636SEnrico Perla - Sun Microsystems 
486af28f636SEnrico Perla - Sun Microsystems 	if (init_device(attach_device, attach_device_path) != BC_SUCCESS) {
487af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Unable to gather device "
488af28f636SEnrico Perla - Sun Microsystems 		    "information for %s (attaching device)\n"),
489af28f636SEnrico Perla - Sun Microsystems 		    attach_device_path);
490af28f636SEnrico Perla - Sun Microsystems 		goto out_devs;
4917fc5d2a4SVikram Hegde 	}
4927fc5d2a4SVikram Hegde 
493af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(curr_device->type) || is_bootpar(attach_device->type)) {
494af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("boot block mirroring is not "
495af28f636SEnrico Perla - Sun Microsystems 		    "supported on PCFS\n"));
496af28f636SEnrico Perla - Sun Microsystems 		goto out_devs;
497af28f636SEnrico Perla - Sun Microsystems 	}
498af28f636SEnrico Perla - Sun Microsystems 
4991a902ef8SHans Rosenfeld 	ret = read_stage2_from_disk(curr_device->part_fd, stage2_curr,
5001a902ef8SHans Rosenfeld 	    curr_device->type);
501af28f636SEnrico Perla - Sun Microsystems 	if (ret == BC_ERROR) {
502af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Error reading first stage2 blocks from %s\n",
503af28f636SEnrico Perla - Sun Microsystems 		    curr_device->path);
504af28f636SEnrico Perla - Sun Microsystems 		retval = BC_ERROR;
505af28f636SEnrico Perla - Sun Microsystems 		goto out_devs;
506af28f636SEnrico Perla - Sun Microsystems 	}
507af28f636SEnrico Perla - Sun Microsystems 
508af28f636SEnrico Perla - Sun Microsystems 	if (ret == BC_NOEXTRA) {
509af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("No multiboot header found on %s, unable to grab "
510af28f636SEnrico Perla - Sun Microsystems 		    "stage2\n", curr_device->path);
511af28f636SEnrico Perla - Sun Microsystems 		retval = BC_NOEXTRA;
512af28f636SEnrico Perla - Sun Microsystems 		goto out_devs;
513af28f636SEnrico Perla - Sun Microsystems 	}
514af28f636SEnrico Perla - Sun Microsystems 
51514d44f22SHans Rosenfeld 	einfo_curr = find_einfo(stage2_curr->extra, stage2_curr->extra_size);
516af28f636SEnrico Perla - Sun Microsystems 	if (einfo_curr != NULL)
517af28f636SEnrico Perla - Sun Microsystems 		updt_str = einfo_get_string(einfo_curr);
518af28f636SEnrico Perla - Sun Microsystems 
519af28f636SEnrico Perla - Sun Microsystems 	write_mbr = B_TRUE;
520af28f636SEnrico Perla - Sun Microsystems 	force_mbr = B_TRUE;
521af28f636SEnrico Perla - Sun Microsystems 	retval = propagate_bootblock(&curr_data, &attach_data, updt_str);
522af28f636SEnrico Perla - Sun Microsystems 	cleanup_stage2(stage2_curr);
523af28f636SEnrico Perla - Sun Microsystems 	cleanup_stage2(stage2_attach);
524af28f636SEnrico Perla - Sun Microsystems 
525af28f636SEnrico Perla - Sun Microsystems out_devs:
526af28f636SEnrico Perla - Sun Microsystems 	cleanup_device(attach_device);
527af28f636SEnrico Perla - Sun Microsystems out_currdev:
528af28f636SEnrico Perla - Sun Microsystems 	cleanup_device(curr_device);
529af28f636SEnrico Perla - Sun Microsystems out:
530af28f636SEnrico Perla - Sun Microsystems 	free(curr_device_path);
531af28f636SEnrico Perla - Sun Microsystems 	free(attach_device_path);
532af28f636SEnrico Perla - Sun Microsystems 	return (retval);
533af28f636SEnrico Perla - Sun Microsystems }
534af28f636SEnrico Perla - Sun Microsystems 
535af28f636SEnrico Perla - Sun Microsystems static int
commit_to_disk(ig_data_t * install,char * updt_str)536af28f636SEnrico Perla - Sun Microsystems commit_to_disk(ig_data_t *install, char *updt_str)
537af28f636SEnrico Perla - Sun Microsystems {
538af28f636SEnrico Perla - Sun Microsystems 	assert(install != NULL);
5397fc5d2a4SVikram Hegde 	/*
540af28f636SEnrico Perla - Sun Microsystems 	 * vanilla stage1 and stage2 need to be updated at runtime.
541af28f636SEnrico Perla - Sun Microsystems 	 * Update stage2 before stage1 because stage1 needs to know the first
542af28f636SEnrico Perla - Sun Microsystems 	 * sector stage2 will be written to.
5437fc5d2a4SVikram Hegde 	 */
544af28f636SEnrico Perla - Sun Microsystems 	if (prepare_stage2(install, updt_str) != BC_SUCCESS) {
545af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error building stage2\n"));
546af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
547af28f636SEnrico Perla - Sun Microsystems 	}
548af28f636SEnrico Perla - Sun Microsystems 	if (prepare_stage1(install) != BC_SUCCESS) {
549af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error building stage1\n"));
550af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
551af28f636SEnrico Perla - Sun Microsystems 	}
552af28f636SEnrico Perla - Sun Microsystems 
553af28f636SEnrico Perla - Sun Microsystems 	/* Write stage2 out to disk. */
554af28f636SEnrico Perla - Sun Microsystems 	if (write_stage2(install) != BC_SUCCESS) {
555af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error writing stage2 to "
556af28f636SEnrico Perla - Sun Microsystems 		    "disk\n"));
557af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
558af28f636SEnrico Perla - Sun Microsystems 	}
559af28f636SEnrico Perla - Sun Microsystems 
560af28f636SEnrico Perla - Sun Microsystems 	/* Write stage1 to disk and, if requested, to the MBR. */
561af28f636SEnrico Perla - Sun Microsystems 	if (write_stage1(install) != BC_SUCCESS) {
562af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error writing stage1 to "
563af28f636SEnrico Perla - Sun Microsystems 		    "disk\n"));
564af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
565af28f636SEnrico Perla - Sun Microsystems 	}
566af28f636SEnrico Perla - Sun Microsystems 
567af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
568af28f636SEnrico Perla - Sun Microsystems }
569af28f636SEnrico Perla - Sun Microsystems 
570af28f636SEnrico Perla - Sun Microsystems /*
571af28f636SEnrico Perla - Sun Microsystems  * Propagate the bootblock on the source disk to the destination disk and
572af28f636SEnrico Perla - Sun Microsystems  * version it with 'updt_str' in the process. Since we cannot trust any data
573af28f636SEnrico Perla - Sun Microsystems  * on the attaching disk, we do not perform any specific check on a potential
574af28f636SEnrico Perla - Sun Microsystems  * target extended information structure and we just blindly update.
575af28f636SEnrico Perla - Sun Microsystems  */
576af28f636SEnrico Perla - Sun Microsystems static int
propagate_bootblock(ig_data_t * source,ig_data_t * target,char * updt_str)577af28f636SEnrico Perla - Sun Microsystems propagate_bootblock(ig_data_t *source, ig_data_t *target, char *updt_str)
578af28f636SEnrico Perla - Sun Microsystems {
579af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*src_device = &source->device;
580af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*dest_device = &target->device;
581af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*src_stage2 = &source->stage2;
582af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*dest_stage2 = &target->stage2;
583af28f636SEnrico Perla - Sun Microsystems 	uint32_t	buf_size;
584af28f636SEnrico Perla - Sun Microsystems 	int		retval;
585af28f636SEnrico Perla - Sun Microsystems 
586af28f636SEnrico Perla - Sun Microsystems 	assert(source != NULL);
587af28f636SEnrico Perla - Sun Microsystems 	assert(target != NULL);
588af28f636SEnrico Perla - Sun Microsystems 
589af28f636SEnrico Perla - Sun Microsystems 	/* read in stage1 from the source disk. */
590af28f636SEnrico Perla - Sun Microsystems 	if (read_stage1_from_disk(src_device->part_fd, target->stage1_buf)
591af28f636SEnrico Perla - Sun Microsystems 	    != BC_SUCCESS)
592af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
593af28f636SEnrico Perla - Sun Microsystems 
594af28f636SEnrico Perla - Sun Microsystems 	/* Prepare target stage2 for commit_to_disk. */
595af28f636SEnrico Perla - Sun Microsystems 	cleanup_stage2(dest_stage2);
596af28f636SEnrico Perla - Sun Microsystems 
597af28f636SEnrico Perla - Sun Microsystems 	if (updt_str != NULL)
598af28f636SEnrico Perla - Sun Microsystems 		do_version = B_TRUE;
599af28f636SEnrico Perla - Sun Microsystems 	else
600af28f636SEnrico Perla - Sun Microsystems 		do_version = B_FALSE;
601af28f636SEnrico Perla - Sun Microsystems 
602af28f636SEnrico Perla - Sun Microsystems 	buf_size = src_stage2->file_size + SECTOR_SIZE;
603af28f636SEnrico Perla - Sun Microsystems 
604af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);
605af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->buf = malloc(dest_stage2->buf_size);
606af28f636SEnrico Perla - Sun Microsystems 	if (dest_stage2->buf == NULL) {
607af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Memory allocation failed"));
608af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
609af28f636SEnrico Perla - Sun Microsystems 	}
610af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->file = dest_stage2->buf;
611af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->file_size = src_stage2->file_size;
612af28f636SEnrico Perla - Sun Microsystems 	memcpy(dest_stage2->file, src_stage2->file, dest_stage2->file_size);
613af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->extra = dest_stage2->buf +
614af28f636SEnrico Perla - Sun Microsystems 	    P2ROUNDUP(dest_stage2->file_size, 8);
615af28f636SEnrico Perla - Sun Microsystems 
616af28f636SEnrico Perla - Sun Microsystems 	/* If we get down here we do have a mboot structure. */
617af28f636SEnrico Perla - Sun Microsystems 	assert(src_stage2->mboot);
618af28f636SEnrico Perla - Sun Microsystems 
619af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->mboot_off = src_stage2->mboot_off;
620af28f636SEnrico Perla - Sun Microsystems 	dest_stage2->mboot = (multiboot_header_t *)(dest_stage2->buf +
621af28f636SEnrico Perla - Sun Microsystems 	    dest_stage2->mboot_off);
622af28f636SEnrico Perla - Sun Microsystems 
623af28f636SEnrico Perla - Sun Microsystems 	(void) fprintf(stdout, gettext("Propagating %s stage1/stage2 to %s\n"),
624af28f636SEnrico Perla - Sun Microsystems 	    src_device->path, dest_device->path);
625af28f636SEnrico Perla - Sun Microsystems 	retval = commit_to_disk(target, updt_str);
626af28f636SEnrico Perla - Sun Microsystems 
627af28f636SEnrico Perla - Sun Microsystems 	return (retval);
628af28f636SEnrico Perla - Sun Microsystems }
629af28f636SEnrico Perla - Sun Microsystems 
630af28f636SEnrico Perla - Sun Microsystems /*
631af28f636SEnrico Perla - Sun Microsystems  * open the device and fill the various members of ig_device_t.
632af28f636SEnrico Perla - Sun Microsystems  */
633af28f636SEnrico Perla - Sun Microsystems static int
init_device(ig_device_t * device,char * path)634af28f636SEnrico Perla - Sun Microsystems init_device(ig_device_t *device, char *path)
635af28f636SEnrico Perla - Sun Microsystems {
6361a902ef8SHans Rosenfeld 	struct dk_gpt *vtoc;
6371a902ef8SHans Rosenfeld 	fstyp_handle_t fhdl;
6381a902ef8SHans Rosenfeld 	const char *fident;
6391a902ef8SHans Rosenfeld 
640af28f636SEnrico Perla - Sun Microsystems 	bzero(device, sizeof (*device));
641af28f636SEnrico Perla - Sun Microsystems 	device->part_fd = -1;
642af28f636SEnrico Perla - Sun Microsystems 	device->disk_fd = -1;
643af28f636SEnrico Perla - Sun Microsystems 	device->path_p0 = NULL;
644af28f636SEnrico Perla - Sun Microsystems 
645af28f636SEnrico Perla - Sun Microsystems 	device->path = strdup(path);
646af28f636SEnrico Perla - Sun Microsystems 	if (device->path == NULL) {
647af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Memory allocation failed"));
648af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
649af28f636SEnrico Perla - Sun Microsystems 	}
650af28f636SEnrico Perla - Sun Microsystems 
651af28f636SEnrico Perla - Sun Microsystems 	if (strstr(device->path, "diskette")) {
652af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("installing GRUB to a floppy "
653af28f636SEnrico Perla - Sun Microsystems 		    "disk is no longer supported\n"));
654af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
655af28f636SEnrico Perla - Sun Microsystems 	}
656af28f636SEnrico Perla - Sun Microsystems 
657af28f636SEnrico Perla - Sun Microsystems 	/* Detect if the target device is a pcfs partition. */
658af28f636SEnrico Perla - Sun Microsystems 	if (strstr(device->path, "p0:boot"))
659af28f636SEnrico Perla - Sun Microsystems 		device->type = IG_DEV_X86BOOTPAR;
660af28f636SEnrico Perla - Sun Microsystems 
661af28f636SEnrico Perla - Sun Microsystems 	if (get_disk_fd(device) != BC_SUCCESS)
662af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
663af28f636SEnrico Perla - Sun Microsystems 
664af28f636SEnrico Perla - Sun Microsystems 	/* read in the device boot sector. */
665af28f636SEnrico Perla - Sun Microsystems 	if (read(device->disk_fd, device->boot_sector, SECTOR_SIZE)
666af28f636SEnrico Perla - Sun Microsystems 	    != SECTOR_SIZE) {
667af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("Error reading boot sector\n"));
668af28f636SEnrico Perla - Sun Microsystems 		perror("read");
669af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
670af28f636SEnrico Perla - Sun Microsystems 	}
671af28f636SEnrico Perla - Sun Microsystems 
672cc641e8dSToomas Soome 	if (efi_alloc_and_read(device->disk_fd, &vtoc) >= 0) {
6731a902ef8SHans Rosenfeld 		device->type = IG_DEV_EFI;
6741a902ef8SHans Rosenfeld 		efi_free(vtoc);
6751a902ef8SHans Rosenfeld 	}
6761a902ef8SHans Rosenfeld 
677af28f636SEnrico Perla - Sun Microsystems 	if (get_raw_partition_fd(device) != BC_SUCCESS)
678af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
679af28f636SEnrico Perla - Sun Microsystems 
6809348d232SHans Rosenfeld 	if (is_efi(device->type)) {
6819348d232SHans Rosenfeld 		if (fstyp_init(device->part_fd, 0, NULL, &fhdl) != 0)
6829348d232SHans Rosenfeld 			return (BC_ERROR);
6831a902ef8SHans Rosenfeld 
6849348d232SHans Rosenfeld 		if (fstyp_ident(fhdl, "zfs", &fident) != 0) {
6859348d232SHans Rosenfeld 			fstyp_fini(fhdl);
6869348d232SHans Rosenfeld 			(void) fprintf(stderr, gettext("Booting of EFI labeled "
6879348d232SHans Rosenfeld 			    "disks is only supported with ZFS\n"));
6889348d232SHans Rosenfeld 			return (BC_ERROR);
6899348d232SHans Rosenfeld 		}
6901a902ef8SHans Rosenfeld 		fstyp_fini(fhdl);
6911a902ef8SHans Rosenfeld 	}
6921a902ef8SHans Rosenfeld 
693af28f636SEnrico Perla - Sun Microsystems 	if (get_start_sector(device) != BC_SUCCESS)
694af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
695af28f636SEnrico Perla - Sun Microsystems 
696af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
697af28f636SEnrico Perla - Sun Microsystems }
698af28f636SEnrico Perla - Sun Microsystems 
699af28f636SEnrico Perla - Sun Microsystems static void
cleanup_device(ig_device_t * device)700af28f636SEnrico Perla - Sun Microsystems cleanup_device(ig_device_t *device)
701af28f636SEnrico Perla - Sun Microsystems {
702af28f636SEnrico Perla - Sun Microsystems 	if (device->path)
703af28f636SEnrico Perla - Sun Microsystems 		free(device->path);
704af28f636SEnrico Perla - Sun Microsystems 	if (device->path_p0)
705af28f636SEnrico Perla - Sun Microsystems 		free(device->path_p0);
706af28f636SEnrico Perla - Sun Microsystems 
707af28f636SEnrico Perla - Sun Microsystems 	if (device->part_fd != -1)
708af28f636SEnrico Perla - Sun Microsystems 		(void) close(device->part_fd);
709af28f636SEnrico Perla - Sun Microsystems 	if (device->disk_fd != -1)
710af28f636SEnrico Perla - Sun Microsystems 		(void) close(device->disk_fd);
711af28f636SEnrico Perla - Sun Microsystems 
712af28f636SEnrico Perla - Sun Microsystems 	bzero(device, sizeof (ig_device_t));
713af28f636SEnrico Perla - Sun Microsystems 	device->part_fd = -1;
714af28f636SEnrico Perla - Sun Microsystems 	device->disk_fd = -1;
715af28f636SEnrico Perla - Sun Microsystems }
716af28f636SEnrico Perla - Sun Microsystems 
717af28f636SEnrico Perla - Sun Microsystems static void
cleanup_stage2(ig_stage2_t * stage2)718af28f636SEnrico Perla - Sun Microsystems cleanup_stage2(ig_stage2_t *stage2)
719af28f636SEnrico Perla - Sun Microsystems {
720af28f636SEnrico Perla - Sun Microsystems 	if (stage2->buf)
721af28f636SEnrico Perla - Sun Microsystems 		free(stage2->buf);
722af28f636SEnrico Perla - Sun Microsystems 	bzero(stage2, sizeof (ig_stage2_t));
723af28f636SEnrico Perla - Sun Microsystems }
724af28f636SEnrico Perla - Sun Microsystems 
725af28f636SEnrico Perla - Sun Microsystems static int
get_start_sector(ig_device_t * device)726af28f636SEnrico Perla - Sun Microsystems get_start_sector(ig_device_t *device)
727af28f636SEnrico Perla - Sun Microsystems {
728af28f636SEnrico Perla - Sun Microsystems 	uint32_t		secnum = 0, numsec = 0;
729af28f636SEnrico Perla - Sun Microsystems 	int			i, pno, rval, log_part = 0;
730af28f636SEnrico Perla - Sun Microsystems 	struct mboot		*mboot;
7313bbf88b3SToomas Soome 	struct ipart		*part = NULL;
732af28f636SEnrico Perla - Sun Microsystems 	ext_part_t		*epp;
733af28f636SEnrico Perla - Sun Microsystems 	struct part_info	dkpi;
734af28f636SEnrico Perla - Sun Microsystems 	struct extpart_info	edkpi;
735af28f636SEnrico Perla - Sun Microsystems 
7361a902ef8SHans Rosenfeld 	if (is_efi(device->type)) {
7371a902ef8SHans Rosenfeld 		struct dk_gpt *vtoc;
7381a902ef8SHans Rosenfeld 
739cc641e8dSToomas Soome 		if (efi_alloc_and_read(device->disk_fd, &vtoc) < 0)
7401a902ef8SHans Rosenfeld 			return (BC_ERROR);
7411a902ef8SHans Rosenfeld 
7421a902ef8SHans Rosenfeld 		device->start_sector = vtoc->efi_parts[device->slice].p_start;
7431a902ef8SHans Rosenfeld 		/* GPT doesn't use traditional slice letters */
744*d23a2a08SToomas Soome 		device->partition = device->slice;
7451a902ef8SHans Rosenfeld 		device->slice = 0xff;
7461a902ef8SHans Rosenfeld 
7471a902ef8SHans Rosenfeld 		efi_free(vtoc);
7481a902ef8SHans Rosenfeld 		goto found_part;
7491a902ef8SHans Rosenfeld 	}
7501a902ef8SHans Rosenfeld 
751af28f636SEnrico Perla - Sun Microsystems 	mboot = (struct mboot *)device->boot_sector;
752af28f636SEnrico Perla - Sun Microsystems 
753af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
754af28f636SEnrico Perla - Sun Microsystems 		if (find_x86_bootpar(mboot, &pno, &secnum) != BC_SUCCESS) {
755af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, NOBOOTPAR);
756af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
757af28f636SEnrico Perla - Sun Microsystems 		} else {
758af28f636SEnrico Perla - Sun Microsystems 			device->start_sector = secnum;
759af28f636SEnrico Perla - Sun Microsystems 			device->partition = pno;
760af28f636SEnrico Perla - Sun Microsystems 			goto found_part;
761af28f636SEnrico Perla - Sun Microsystems 		}
7627fc5d2a4SVikram Hegde 	}
7637fc5d2a4SVikram Hegde 
7647fc5d2a4SVikram Hegde 	/*
765af28f636SEnrico Perla - Sun Microsystems 	 * Search for Solaris fdisk partition
7667fc5d2a4SVikram Hegde 	 * Get the solaris partition information from the device
7677fc5d2a4SVikram Hegde 	 * and compare the offset of S2 with offset of solaris partition
7687fc5d2a4SVikram Hegde 	 * from fdisk partition table.
7697fc5d2a4SVikram Hegde 	 */
770af28f636SEnrico Perla - Sun Microsystems 	if (ioctl(device->part_fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
771af28f636SEnrico Perla - Sun Microsystems 		if (ioctl(device->part_fd, DKIOCPARTINFO, &dkpi) < 0) {
7727fc5d2a4SVikram Hegde 			(void) fprintf(stderr, PART_FAIL);
773af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
7747fc5d2a4SVikram Hegde 		} else {
7757fc5d2a4SVikram Hegde 			edkpi.p_start = dkpi.p_start;
7767fc5d2a4SVikram Hegde 		}
7777fc5d2a4SVikram Hegde 	}
7787fc5d2a4SVikram Hegde 
7797fc5d2a4SVikram Hegde 	for (i = 0; i < FD_NUMPART; i++) {
7807fc5d2a4SVikram Hegde 		part = (struct ipart *)mboot->parts + i;
7817fc5d2a4SVikram Hegde 
7827fc5d2a4SVikram Hegde 		if (part->relsect == 0) {
7837fc5d2a4SVikram Hegde 			(void) fprintf(stderr, BAD_PART, i);
784af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
7857fc5d2a4SVikram Hegde 		}
7867fc5d2a4SVikram Hegde 
7877fc5d2a4SVikram Hegde 		if (edkpi.p_start >= part->relsect &&
7887fc5d2a4SVikram Hegde 		    edkpi.p_start < (part->relsect + part->numsect)) {
7897fc5d2a4SVikram Hegde 			/* Found the partition */
7907fc5d2a4SVikram Hegde 			break;
791d33344bbSsy 		}
792d33344bbSsy 	}
793d33344bbSsy 
7947fc5d2a4SVikram Hegde 	if (i == FD_NUMPART) {
7957fc5d2a4SVikram Hegde 		/* No solaris fdisk partitions (primary or logical) */
7967fc5d2a4SVikram Hegde 		(void) fprintf(stderr, NOSOLPAR);
797af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
7987fc5d2a4SVikram Hegde 	}
7997fc5d2a4SVikram Hegde 
8007fc5d2a4SVikram Hegde 	/*
8017fc5d2a4SVikram Hegde 	 * We have found a Solaris fdisk partition (primary or extended)
8027fc5d2a4SVikram Hegde 	 * Handle the simple case first: Solaris in a primary partition
8037fc5d2a4SVikram Hegde 	 */
8047fc5d2a4SVikram Hegde 	if (!fdisk_is_dos_extended(part->systid)) {
805af28f636SEnrico Perla - Sun Microsystems 		device->start_sector = part->relsect;
806af28f636SEnrico Perla - Sun Microsystems 		device->partition = i;
8077fc5d2a4SVikram Hegde 		goto found_part;
8087fc5d2a4SVikram Hegde 	}
8097fc5d2a4SVikram Hegde 
8107fc5d2a4SVikram Hegde 	/*
8117fc5d2a4SVikram Hegde 	 * Solaris in a logical partition. Find that partition in the
8127fc5d2a4SVikram Hegde 	 * extended part.
8137fc5d2a4SVikram Hegde 	 */
814af28f636SEnrico Perla - Sun Microsystems 	if ((rval = libfdisk_init(&epp, device->path_p0, NULL, FDISK_READ_DISK))
815aa1b14e7SSheshadri Vasudevan 	    != FDISK_SUCCESS) {
816aa1b14e7SSheshadri Vasudevan 		switch (rval) {
817aa1b14e7SSheshadri Vasudevan 			/*
8186cb5747bSSharath M Srinivasan 			 * The first 3 cases are not an error per-se, just that
8197fc5d2a4SVikram Hegde 			 * there is no Solaris logical partition
820aa1b14e7SSheshadri Vasudevan 			 */
821aa1b14e7SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
822aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
8236cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
8247fc5d2a4SVikram Hegde 				(void) fprintf(stderr, NOSOLPAR);
825af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
826aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOVGEOM:
827aa1b14e7SSheshadri Vasudevan 				(void) fprintf(stderr, NO_VIRT_GEOM);
828af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
829aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOPGEOM:
830aa1b14e7SSheshadri Vasudevan 				(void) fprintf(stderr, NO_PHYS_GEOM);
831af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
832aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOLGEOM:
833aa1b14e7SSheshadri Vasudevan 				(void) fprintf(stderr, NO_LABEL_GEOM);
834af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
835aa1b14e7SSheshadri Vasudevan 			default:
836aa1b14e7SSheshadri Vasudevan 				(void) fprintf(stderr, LIBFDISK_INIT_FAIL);
837af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
838aa1b14e7SSheshadri Vasudevan 		}
839aa1b14e7SSheshadri Vasudevan 	}
840aa1b14e7SSheshadri Vasudevan 
841aa1b14e7SSheshadri Vasudevan 	rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
8426cb5747bSSharath M Srinivasan 	libfdisk_fini(&epp);
8437fc5d2a4SVikram Hegde 	if (rval != FDISK_SUCCESS) {
8447fc5d2a4SVikram Hegde 		/* No solaris logical partition */
8457fc5d2a4SVikram Hegde 		(void) fprintf(stderr, NOSOLPAR);
846af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
847aa1b14e7SSheshadri Vasudevan 	}
848aa1b14e7SSheshadri Vasudevan 
849af28f636SEnrico Perla - Sun Microsystems 	device->start_sector = secnum;
850af28f636SEnrico Perla - Sun Microsystems 	device->partition = pno - 1;
8517fc5d2a4SVikram Hegde 	log_part = 1;
8527c478bd9Sstevel@tonic-gate 
8537fc5d2a4SVikram Hegde found_part:
8547c478bd9Sstevel@tonic-gate 	/* get confirmation for -m */
855af28f636SEnrico Perla - Sun Microsystems 	if (write_mbr && !force_mbr) {
8567c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, MBOOT_PROMPT);
85721ba817cSToomas Soome 		if (!yes()) {
858af28f636SEnrico Perla - Sun Microsystems 			write_mbr = 0;
8597c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
860af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647fc5d2a4SVikram Hegde 	/*
8657fc5d2a4SVikram Hegde 	 * Currently if Solaris is in an extended partition we need to
8667fc5d2a4SVikram Hegde 	 * write GRUB to the MBR. Check for this.
8677fc5d2a4SVikram Hegde 	 */
868af28f636SEnrico Perla - Sun Microsystems 	if (log_part && !write_mbr) {
869af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, gettext("Installing Solaris on an "
870af28f636SEnrico Perla - Sun Microsystems 		    "extended partition... forcing MBR update\n"));
871af28f636SEnrico Perla - Sun Microsystems 		write_mbr = 1;
872aa1b14e7SSheshadri Vasudevan 	}
873aa1b14e7SSheshadri Vasudevan 
8747fc5d2a4SVikram Hegde 	/*
8757fc5d2a4SVikram Hegde 	 * warn, if Solaris in primary partition and GRUB not in MBR and
8767fc5d2a4SVikram Hegde 	 * partition is not active
8777fc5d2a4SVikram Hegde 	 */
878305579dfSToomas Soome 	if (part != NULL) {
879305579dfSToomas Soome 		if (!log_part && part->bootid != 128 && !write_mbr) {
880305579dfSToomas Soome 			(void) fprintf(stdout, SOLPAR_INACTIVE,
881305579dfSToomas Soome 			    device->partition + 1);
882305579dfSToomas Soome 		}
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
885af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate static int
get_disk_fd(ig_device_t * device)889af28f636SEnrico Perla - Sun Microsystems get_disk_fd(ig_device_t *device)
8907c478bd9Sstevel@tonic-gate {
8913bbf88b3SToomas Soome 	int	i = 0;
8923bbf88b3SToomas Soome 	char	save[2] = { '\0', '\0' };
893af28f636SEnrico Perla - Sun Microsystems 	char	*end = NULL;
894af28f636SEnrico Perla - Sun Microsystems 
895af28f636SEnrico Perla - Sun Microsystems 	assert(device != NULL);
896af28f636SEnrico Perla - Sun Microsystems 	assert(device->path != NULL);
897af28f636SEnrico Perla - Sun Microsystems 
898af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
899af28f636SEnrico Perla - Sun Microsystems 		end = strstr(device->path, "p0:boot");
900af28f636SEnrico Perla - Sun Microsystems 		/* tested at the start of init_device() */
901af28f636SEnrico Perla - Sun Microsystems 		assert(end != NULL);
902af28f636SEnrico Perla - Sun Microsystems 		/* chop off :boot */
903af28f636SEnrico Perla - Sun Microsystems 		save[0] = end[2];
904af28f636SEnrico Perla - Sun Microsystems 		end[2] = '\0';
905af28f636SEnrico Perla - Sun Microsystems 	} else {
906af28f636SEnrico Perla - Sun Microsystems 		i = strlen(device->path);
907af28f636SEnrico Perla - Sun Microsystems 		save[0] = device->path[i - 2];
908af28f636SEnrico Perla - Sun Microsystems 		save[1] = device->path[i - 1];
909af28f636SEnrico Perla - Sun Microsystems 		device->path[i - 2] = 'p';
910af28f636SEnrico Perla - Sun Microsystems 		device->path[i - 1] = '0';
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (nowrite)
914af28f636SEnrico Perla - Sun Microsystems 		device->disk_fd = open(device->path, O_RDONLY);
9157c478bd9Sstevel@tonic-gate 	else
916af28f636SEnrico Perla - Sun Microsystems 		device->disk_fd = open(device->path, O_RDWR);
9177c478bd9Sstevel@tonic-gate 
918af28f636SEnrico Perla - Sun Microsystems 	device->path_p0 = strdup(device->path);
919af28f636SEnrico Perla - Sun Microsystems 	if (device->path_p0 == NULL) {
920af28f636SEnrico Perla - Sun Microsystems 		perror("strdup");
921af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
9227c478bd9Sstevel@tonic-gate 	}
923af28f636SEnrico Perla - Sun Microsystems 
924af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
925af28f636SEnrico Perla - Sun Microsystems 		end[2] = save[0];
926af28f636SEnrico Perla - Sun Microsystems 	} else {
927af28f636SEnrico Perla - Sun Microsystems 		device->path[i - 2] = save[0];
928af28f636SEnrico Perla - Sun Microsystems 		device->path[i - 1] = save[1];
929af28f636SEnrico Perla - Sun Microsystems 	}
930af28f636SEnrico Perla - Sun Microsystems 
931af28f636SEnrico Perla - Sun Microsystems 	if (device->disk_fd == -1) {
932af28f636SEnrico Perla - Sun Microsystems 		perror("open");
933af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
936af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate static void
prepare_fake_multiboot(ig_stage2_t * stage2)940af28f636SEnrico Perla - Sun Microsystems prepare_fake_multiboot(ig_stage2_t *stage2)
9417c478bd9Sstevel@tonic-gate {
942af28f636SEnrico Perla - Sun Microsystems 	multiboot_header_t	*mboot;
9437c478bd9Sstevel@tonic-gate 
944af28f636SEnrico Perla - Sun Microsystems 	assert(stage2 != NULL);
945af28f636SEnrico Perla - Sun Microsystems 	assert(stage2->mboot != NULL);
946af28f636SEnrico Perla - Sun Microsystems 	assert(stage2->buf != NULL);
9477c478bd9Sstevel@tonic-gate 
948af28f636SEnrico Perla - Sun Microsystems 	mboot = stage2->mboot;
949af28f636SEnrico Perla - Sun Microsystems 
950af28f636SEnrico Perla - Sun Microsystems 	/*
951af28f636SEnrico Perla - Sun Microsystems 	 * Currently we expect find_multiboot() to have located a multiboot
952af28f636SEnrico Perla - Sun Microsystems 	 * header with the AOUT kludge flag set.
953af28f636SEnrico Perla - Sun Microsystems 	 */
954af28f636SEnrico Perla - Sun Microsystems 	assert(mboot->flags & BB_MBOOT_AOUT_FLAG);
955af28f636SEnrico Perla - Sun Microsystems 
956af28f636SEnrico Perla - Sun Microsystems 	/* Insert the information necessary to locate stage2. */
957af28f636SEnrico Perla - Sun Microsystems 	mboot->header_addr = stage2->mboot_off;
958af28f636SEnrico Perla - Sun Microsystems 	mboot->load_addr = 0;
959af28f636SEnrico Perla - Sun Microsystems 	mboot->load_end_addr = stage2->file_size;
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate static void
add_stage2_einfo(ig_stage2_t * stage2,char * updt_str)963af28f636SEnrico Perla - Sun Microsystems add_stage2_einfo(ig_stage2_t *stage2, char *updt_str)
9647c478bd9Sstevel@tonic-gate {
965af28f636SEnrico Perla - Sun Microsystems 	bblk_hs_t	hs;
966af28f636SEnrico Perla - Sun Microsystems 	uint32_t	avail_space;
967af28f636SEnrico Perla - Sun Microsystems 
968af28f636SEnrico Perla - Sun Microsystems 	assert(stage2 != NULL);
969af28f636SEnrico Perla - Sun Microsystems 
970af28f636SEnrico Perla - Sun Microsystems 	/* Fill bootblock hashing source information. */
971af28f636SEnrico Perla - Sun Microsystems 	hs.src_buf = (unsigned char *)stage2->file;
972af28f636SEnrico Perla - Sun Microsystems 	hs.src_size = stage2->file_size;
973af28f636SEnrico Perla - Sun Microsystems 	/* How much space for the extended information structure? */
974af28f636SEnrico Perla - Sun Microsystems 	avail_space = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
975af28f636SEnrico Perla - Sun Microsystems 	add_einfo(stage2->extra, updt_str, &hs, avail_space);
976af28f636SEnrico Perla - Sun Microsystems }
977af28f636SEnrico Perla - Sun Microsystems 
978af28f636SEnrico Perla - Sun Microsystems 
979af28f636SEnrico Perla - Sun Microsystems static int
write_stage2(ig_data_t * install)980af28f636SEnrico Perla - Sun Microsystems write_stage2(ig_data_t *install)
981af28f636SEnrico Perla - Sun Microsystems {
982af28f636SEnrico Perla - Sun Microsystems 	ig_device_t		*device = &install->device;
983af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t		*stage2 = &install->stage2;
984af28f636SEnrico Perla - Sun Microsystems 	off_t			offset;
985af28f636SEnrico Perla - Sun Microsystems 
986af28f636SEnrico Perla - Sun Microsystems 	assert(install != NULL);
987af28f636SEnrico Perla - Sun Microsystems 
988af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
989af28f636SEnrico Perla - Sun Microsystems 		/*
990af28f636SEnrico Perla - Sun Microsystems 		 * stage2 is already on the filesystem, we only need to update
991af28f636SEnrico Perla - Sun Microsystems 		 * the first two blocks (that we have modified during
992af28f636SEnrico Perla - Sun Microsystems 		 * prepare_stage2())
993af28f636SEnrico Perla - Sun Microsystems 		 */
994af28f636SEnrico Perla - Sun Microsystems 		if (write_out(device->part_fd, stage2->file, SECTOR_SIZE,
995af28f636SEnrico Perla - Sun Microsystems 		    stage2->pcfs_first_sectors[0] * SECTOR_SIZE)
996af28f636SEnrico Perla - Sun Microsystems 		    != BC_SUCCESS ||
997af28f636SEnrico Perla - Sun Microsystems 		    write_out(device->part_fd, stage2->file + SECTOR_SIZE,
998af28f636SEnrico Perla - Sun Microsystems 		    SECTOR_SIZE, stage2->pcfs_first_sectors[1] * SECTOR_SIZE)
999af28f636SEnrico Perla - Sun Microsystems 		    != BC_SUCCESS) {
1000af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
1001af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
1002af28f636SEnrico Perla - Sun Microsystems 		}
1003af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
1004af28f636SEnrico Perla - Sun Microsystems 		return (BC_SUCCESS);
10057c478bd9Sstevel@tonic-gate 	}
1006af28f636SEnrico Perla - Sun Microsystems 
1007af28f636SEnrico Perla - Sun Microsystems 	/*
1008af28f636SEnrico Perla - Sun Microsystems 	 * For disk, write stage2 starting at STAGE2_BLKOFF sector.
1009af28f636SEnrico Perla - Sun Microsystems 	 * Note that we use stage2->buf rather than stage2->file, because we
1010af28f636SEnrico Perla - Sun Microsystems 	 * may have extended information after the latter.
10119348d232SHans Rosenfeld 	 *
10129348d232SHans Rosenfeld 	 * If we're writing to an EFI-labeled disk where stage2 lives in the
10139348d232SHans Rosenfeld 	 * 3.5MB boot loader gap following the ZFS vdev labels, make sure the
10149348d232SHans Rosenfeld 	 * size of the buffer doesn't exceed the size of the gap.
1015af28f636SEnrico Perla - Sun Microsystems 	 */
10169348d232SHans Rosenfeld 	if (is_efi(device->type) && stage2->buf_size > STAGE2_MAXSIZE) {
10179348d232SHans Rosenfeld 		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
10189348d232SHans Rosenfeld 		return (BC_ERROR);
10199348d232SHans Rosenfeld 	}
10209348d232SHans Rosenfeld 
10211a902ef8SHans Rosenfeld 	offset = STAGE2_BLKOFF(device->type) * SECTOR_SIZE;
10221a902ef8SHans Rosenfeld 
1023af28f636SEnrico Perla - Sun Microsystems 	if (write_out(device->part_fd, stage2->buf, stage2->buf_size,
1024af28f636SEnrico Perla - Sun Microsystems 	    offset) != BC_SUCCESS) {
1025af28f636SEnrico Perla - Sun Microsystems 		perror("write");
1026af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1027af28f636SEnrico Perla - Sun Microsystems 	}
1028af28f636SEnrico Perla - Sun Microsystems 
1029af28f636SEnrico Perla - Sun Microsystems 	/* Simulate the "old" installgrub output. */
1030af28f636SEnrico Perla - Sun Microsystems 	(void) fprintf(stdout, WRITE_STAGE2_DISK, device->partition,
10311a902ef8SHans Rosenfeld 	    (stage2->buf_size / SECTOR_SIZE) + 1, STAGE2_BLKOFF(device->type),
1032af28f636SEnrico Perla - Sun Microsystems 	    stage2->first_sector);
1033af28f636SEnrico Perla - Sun Microsystems 
1034af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
1037af28f636SEnrico Perla - Sun Microsystems static int
write_stage1(ig_data_t * install)1038af28f636SEnrico Perla - Sun Microsystems write_stage1(ig_data_t *install)
10397c478bd9Sstevel@tonic-gate {
1040af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &install->device;
1041af28f636SEnrico Perla - Sun Microsystems 
1042af28f636SEnrico Perla - Sun Microsystems 	assert(install != NULL);
1043af28f636SEnrico Perla - Sun Microsystems 
1044af28f636SEnrico Perla - Sun Microsystems 	if (write_out(device->part_fd, install->stage1_buf,
1045af28f636SEnrico Perla - Sun Microsystems 	    sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
1046af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, WRITE_FAIL_PBOOT);
1047af28f636SEnrico Perla - Sun Microsystems 		perror("write");
1048af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
10497c478bd9Sstevel@tonic-gate 	}
1050af28f636SEnrico Perla - Sun Microsystems 
1051af28f636SEnrico Perla - Sun Microsystems 	/* Simulate "old" installgrub output. */
1052af28f636SEnrico Perla - Sun Microsystems 	(void) fprintf(stdout, WRITE_PBOOT, device->partition,
1053af28f636SEnrico Perla - Sun Microsystems 	    device->start_sector);
1054af28f636SEnrico Perla - Sun Microsystems 
1055af28f636SEnrico Perla - Sun Microsystems 	if (write_mbr) {
1056af28f636SEnrico Perla - Sun Microsystems 		if (write_out(device->disk_fd, install->stage1_buf,
1057af28f636SEnrico Perla - Sun Microsystems 		    sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
1058af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stdout, WRITE_FAIL_BOOTSEC);
1059af28f636SEnrico Perla - Sun Microsystems 			perror("write");
1060af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
1061af28f636SEnrico Perla - Sun Microsystems 		}
1062af28f636SEnrico Perla - Sun Microsystems 		/* Simulate "old" installgrub output. */
1063af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, WRITE_MBOOT);
1064af28f636SEnrico Perla - Sun Microsystems 	}
1065af28f636SEnrico Perla - Sun Microsystems 
1066af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
1069af28f636SEnrico Perla - Sun Microsystems #define	USAGE_STRING	"%s [-m|-f|-n|-F|-u verstr] stage1 stage2 device\n"    \
1070af28f636SEnrico Perla - Sun Microsystems 			"%s -M [-n] device1 device2\n"			       \
1071af28f636SEnrico Perla - Sun Microsystems 			"%s [-V|-e] -i device\n"			       \
1072af28f636SEnrico Perla - Sun Microsystems 
1073af28f636SEnrico Perla - Sun Microsystems #define	CANON_USAGE_STR	gettext(USAGE_STRING)
1074af28f636SEnrico Perla - Sun Microsystems 
10757c478bd9Sstevel@tonic-gate static void
usage(char * progname)1076af28f636SEnrico Perla - Sun Microsystems usage(char *progname)
10777c478bd9Sstevel@tonic-gate {
1078af28f636SEnrico Perla - Sun Microsystems 	(void) fprintf(stdout, CANON_USAGE_STR, progname, progname, progname);
1079af28f636SEnrico Perla - Sun Microsystems }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 
1082af28f636SEnrico Perla - Sun Microsystems static int
read_stage1_from_file(char * path,ig_data_t * dest)1083af28f636SEnrico Perla - Sun Microsystems read_stage1_from_file(char *path, ig_data_t *dest)
1084af28f636SEnrico Perla - Sun Microsystems {
1085af28f636SEnrico Perla - Sun Microsystems 	int	fd;
1086af28f636SEnrico Perla - Sun Microsystems 
1087af28f636SEnrico Perla - Sun Microsystems 	assert(dest);
1088af28f636SEnrico Perla - Sun Microsystems 
1089af28f636SEnrico Perla - Sun Microsystems 	/* read the stage1 file from filesystem */
1090af28f636SEnrico Perla - Sun Microsystems 	fd = open(path, O_RDONLY);
1091af28f636SEnrico Perla - Sun Microsystems 	if (fd == -1 ||
1092af28f636SEnrico Perla - Sun Microsystems 	    read(fd, dest->stage1_buf, SECTOR_SIZE) != SECTOR_SIZE) {
1093af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, READ_FAIL_STAGE1, path);
1094af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	(void) close(fd);
1097af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
1100af28f636SEnrico Perla - Sun Microsystems static int
read_stage2_from_file(char * path,ig_data_t * dest)1101af28f636SEnrico Perla - Sun Microsystems read_stage2_from_file(char *path, ig_data_t *dest)
11027c478bd9Sstevel@tonic-gate {
1103af28f636SEnrico Perla - Sun Microsystems 	int		fd;
1104af28f636SEnrico Perla - Sun Microsystems 	struct stat	sb;
1105af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2 = &dest->stage2;
1106af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &dest->device;
1107af28f636SEnrico Perla - Sun Microsystems 	uint32_t	buf_size;
1108af28f636SEnrico Perla - Sun Microsystems 
1109af28f636SEnrico Perla - Sun Microsystems 	assert(dest);
1110af28f636SEnrico Perla - Sun Microsystems 	assert(stage2->buf == NULL);
1111af28f636SEnrico Perla - Sun Microsystems 
1112af28f636SEnrico Perla - Sun Microsystems 	fd = open(path, O_RDONLY);
1113af28f636SEnrico Perla - Sun Microsystems 	if (fstat(fd, &sb) == -1) {
1114af28f636SEnrico Perla - Sun Microsystems 		perror("fstat");
1115af28f636SEnrico Perla - Sun Microsystems 		goto out;
1116af28f636SEnrico Perla - Sun Microsystems 	}
1117af28f636SEnrico Perla - Sun Microsystems 
1118af28f636SEnrico Perla - Sun Microsystems 	stage2->file_size = sb.st_size;
1119af28f636SEnrico Perla - Sun Microsystems 
1120af28f636SEnrico Perla - Sun Microsystems 	if (!is_bootpar(device->type)) {
1121af28f636SEnrico Perla - Sun Microsystems 		/*
1122af28f636SEnrico Perla - Sun Microsystems 		 * buffer size needs to account for stage2 plus the extra
1123af28f636SEnrico Perla - Sun Microsystems 		 * versioning information at the end of it. We reserve one
1124af28f636SEnrico Perla - Sun Microsystems 		 * extra sector (plus we round up to the next sector boundary).
1125af28f636SEnrico Perla - Sun Microsystems 		 */
1126af28f636SEnrico Perla - Sun Microsystems 		buf_size = stage2->file_size + SECTOR_SIZE;
11277c478bd9Sstevel@tonic-gate 	} else {
1128af28f636SEnrico Perla - Sun Microsystems 		/* In the PCFS case we only need to read in stage2. */
1129af28f636SEnrico Perla - Sun Microsystems 		buf_size = stage2->file_size;
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
1132af28f636SEnrico Perla - Sun Microsystems 	stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);
1133af28f636SEnrico Perla - Sun Microsystems 
1134af28f636SEnrico Perla - Sun Microsystems 	BOOT_DEBUG("stage2 buffer size = %d (%d sectors)\n", stage2->buf_size,
1135af28f636SEnrico Perla - Sun Microsystems 	    stage2->buf_size / SECTOR_SIZE);
1136af28f636SEnrico Perla - Sun Microsystems 
1137af28f636SEnrico Perla - Sun Microsystems 	stage2->buf = malloc(stage2->buf_size);
1138af28f636SEnrico Perla - Sun Microsystems 	if (stage2->buf == NULL) {
1139af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Memory allocation failed"));
1140af28f636SEnrico Perla - Sun Microsystems 		goto out_fd;
1141af28f636SEnrico Perla - Sun Microsystems 	}
1142af28f636SEnrico Perla - Sun Microsystems 
1143af28f636SEnrico Perla - Sun Microsystems 	stage2->file = stage2->buf;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/*
1146af28f636SEnrico Perla - Sun Microsystems 	 * Extra information (e.g. the versioning structure) is placed at the
1147af28f636SEnrico Perla - Sun Microsystems 	 * end of stage2, aligned on a 8-byte boundary.
11487c478bd9Sstevel@tonic-gate 	 */
1149af28f636SEnrico Perla - Sun Microsystems 	if (!(is_bootpar(device->type)))
1150af28f636SEnrico Perla - Sun Microsystems 		stage2->extra = stage2->file + P2ROUNDUP(stage2->file_size, 8);
11517c478bd9Sstevel@tonic-gate 
1152af28f636SEnrico Perla - Sun Microsystems 	if (lseek(fd, 0, SEEK_SET) == -1) {
1153af28f636SEnrico Perla - Sun Microsystems 		perror("lseek");
1154af28f636SEnrico Perla - Sun Microsystems 		goto out_alloc;
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
1157af28f636SEnrico Perla - Sun Microsystems 	if (read(fd, stage2->file, stage2->file_size) < 0) {
1158af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("unable to read stage2"));
1159af28f636SEnrico Perla - Sun Microsystems 		goto out_alloc;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
1162af28f636SEnrico Perla - Sun Microsystems 	(void) close(fd);
1163af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
11647ce76caaSEnrico Perla - Sun Microsystems 
1165af28f636SEnrico Perla - Sun Microsystems out_alloc:
1166af28f636SEnrico Perla - Sun Microsystems 	free(stage2->buf);
1167af28f636SEnrico Perla - Sun Microsystems 	stage2->buf = NULL;
1168af28f636SEnrico Perla - Sun Microsystems out_fd:
1169af28f636SEnrico Perla - Sun Microsystems 	(void) close(fd);
1170af28f636SEnrico Perla - Sun Microsystems out:
1171af28f636SEnrico Perla - Sun Microsystems 	return (BC_ERROR);
11727ce76caaSEnrico Perla - Sun Microsystems }
11737ce76caaSEnrico Perla - Sun Microsystems 
1174af28f636SEnrico Perla - Sun Microsystems static int
prepare_stage1(ig_data_t * install)1175af28f636SEnrico Perla - Sun Microsystems prepare_stage1(ig_data_t *install)
11767ce76caaSEnrico Perla - Sun Microsystems {
1177af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &install->device;
11787ce76caaSEnrico Perla - Sun Microsystems 
1179af28f636SEnrico Perla - Sun Microsystems 	assert(install != NULL);
1180af28f636SEnrico Perla - Sun Microsystems 
1181af28f636SEnrico Perla - Sun Microsystems 	/* If PCFS add the BIOS Parameter Block. */
1182af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
1183af28f636SEnrico Perla - Sun Microsystems 		char	bpb_sect[SECTOR_SIZE];
1184af28f636SEnrico Perla - Sun Microsystems 
1185af28f636SEnrico Perla - Sun Microsystems 		if (pread(device->part_fd, bpb_sect, SECTOR_SIZE, 0)
1186af28f636SEnrico Perla - Sun Microsystems 		    != SECTOR_SIZE) {
1187af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, READ_FAIL_BPB);
1188af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
1189af28f636SEnrico Perla - Sun Microsystems 		}
1190af28f636SEnrico Perla - Sun Microsystems 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
1191af28f636SEnrico Perla - Sun Microsystems 		    install->stage1_buf + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
11927ce76caaSEnrico Perla - Sun Microsystems 	}
11937ce76caaSEnrico Perla - Sun Microsystems 
1194af28f636SEnrico Perla - Sun Microsystems 	/* copy MBR to stage1 in case of overwriting MBR sector. */
1195af28f636SEnrico Perla - Sun Microsystems 	bcopy(device->boot_sector + BOOTSZ, install->stage1_buf + BOOTSZ,
1196af28f636SEnrico Perla - Sun Microsystems 	    SECTOR_SIZE - BOOTSZ);
1197af28f636SEnrico Perla - Sun Microsystems 	/* modify default stage1 file generated by GRUB. */
1198af28f636SEnrico Perla - Sun Microsystems 	*((unsigned char *)(install->stage1_buf + STAGE1_FORCE_LBA)) = 1;
1199af28f636SEnrico Perla - Sun Microsystems 	*((ulong_t *)(install->stage1_buf + STAGE1_STAGE2_SECTOR))
1200af28f636SEnrico Perla - Sun Microsystems 	    = install->stage2.first_sector;
1201af28f636SEnrico Perla - Sun Microsystems 	*((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_ADDRESS))
1202af28f636SEnrico Perla - Sun Microsystems 	    = STAGE2_MEMADDR;
1203af28f636SEnrico Perla - Sun Microsystems 	*((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_SEGMENT))
1204af28f636SEnrico Perla - Sun Microsystems 	    = STAGE2_MEMADDR >> 4;
12057ce76caaSEnrico Perla - Sun Microsystems 
1206af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
12077ce76caaSEnrico Perla - Sun Microsystems }
12087ce76caaSEnrico Perla - Sun Microsystems 
1209af28f636SEnrico Perla - Sun Microsystems /*
1210af28f636SEnrico Perla - Sun Microsystems  * Grab stage1 from the specified device file descriptor.
1211af28f636SEnrico Perla - Sun Microsystems  */
12127ce76caaSEnrico Perla - Sun Microsystems static int
read_stage1_from_disk(int dev_fd,char * stage1_buf)1213af28f636SEnrico Perla - Sun Microsystems read_stage1_from_disk(int dev_fd, char *stage1_buf)
12147ce76caaSEnrico Perla - Sun Microsystems {
1215af28f636SEnrico Perla - Sun Microsystems 	assert(stage1_buf != NULL);
12167ce76caaSEnrico Perla - Sun Microsystems 
1217af28f636SEnrico Perla - Sun Microsystems 	if (read_in(dev_fd, stage1_buf, SECTOR_SIZE, 0) != BC_SUCCESS) {
1218af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Unable to read stage1 from disk"));
1219af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1220af28f636SEnrico Perla - Sun Microsystems 	}
1221af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
1222af28f636SEnrico Perla - Sun Microsystems }
12237ce76caaSEnrico Perla - Sun Microsystems 
1224af28f636SEnrico Perla - Sun Microsystems static int
read_stage2_from_disk(int dev_fd,ig_stage2_t * stage2,int type)12251a902ef8SHans Rosenfeld read_stage2_from_disk(int dev_fd, ig_stage2_t *stage2, int type)
1226af28f636SEnrico Perla - Sun Microsystems {
1227af28f636SEnrico Perla - Sun Microsystems 	uint32_t		size;
1228af28f636SEnrico Perla - Sun Microsystems 	uint32_t		buf_size;
1229af28f636SEnrico Perla - Sun Microsystems 	uint32_t		mboot_off;
1230af28f636SEnrico Perla - Sun Microsystems 	multiboot_header_t	*mboot;
1231af28f636SEnrico Perla - Sun Microsystems 
1232af28f636SEnrico Perla - Sun Microsystems 	assert(stage2 != NULL);
1233af28f636SEnrico Perla - Sun Microsystems 	assert(dev_fd != -1);
1234af28f636SEnrico Perla - Sun Microsystems 
1235af28f636SEnrico Perla - Sun Microsystems 	if (read_in(dev_fd, mboot_scan, sizeof (mboot_scan),
12361a902ef8SHans Rosenfeld 	    STAGE2_BLKOFF(type) * SECTOR_SIZE) != BC_SUCCESS) {
1237af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Error reading stage2 sectors"));
1238af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1239af28f636SEnrico Perla - Sun Microsystems 	}
12407ce76caaSEnrico Perla - Sun Microsystems 
1241af28f636SEnrico Perla - Sun Microsystems 	/* No multiboot means no chance of knowing stage2 size */
1242af28f636SEnrico Perla - Sun Microsystems 	if (find_multiboot(mboot_scan, sizeof (mboot_scan), &mboot_off)
1243af28f636SEnrico Perla - Sun Microsystems 	    != BC_SUCCESS) {
1244af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Unable to find multiboot header\n");
1245af28f636SEnrico Perla - Sun Microsystems 		return (BC_NOEXTRA);
1246af28f636SEnrico Perla - Sun Microsystems 	}
1247af28f636SEnrico Perla - Sun Microsystems 	mboot = (multiboot_header_t *)(mboot_scan + mboot_off);
12487ce76caaSEnrico Perla - Sun Microsystems 
1249af28f636SEnrico Perla - Sun Microsystems 	/*
1250af28f636SEnrico Perla - Sun Microsystems 	 * Unfilled mboot values mean an older version of installgrub installed
1251af28f636SEnrico Perla - Sun Microsystems 	 * the stage2. Again we have no chance of knowing stage2 size.
1252af28f636SEnrico Perla - Sun Microsystems 	 */
1253af28f636SEnrico Perla - Sun Microsystems 	if (mboot->load_end_addr == 0 ||
1254af28f636SEnrico Perla - Sun Microsystems 	    mboot->load_end_addr < mboot->load_addr)
1255af28f636SEnrico Perla - Sun Microsystems 		return (BC_NOEXTRA);
12567ce76caaSEnrico Perla - Sun Microsystems 
1257af28f636SEnrico Perla - Sun Microsystems 	/*
1258af28f636SEnrico Perla - Sun Microsystems 	 * Currently, the amount of space reserved for extra information
1259af28f636SEnrico Perla - Sun Microsystems 	 * is "fixed". We may have to scan for the terminating extra payload
1260af28f636SEnrico Perla - Sun Microsystems 	 * in the future.
1261af28f636SEnrico Perla - Sun Microsystems 	 */
1262af28f636SEnrico Perla - Sun Microsystems 	size = mboot->load_end_addr - mboot->load_addr;
1263af28f636SEnrico Perla - Sun Microsystems 	buf_size = P2ROUNDUP(size + SECTOR_SIZE, SECTOR_SIZE);
12647ce76caaSEnrico Perla - Sun Microsystems 
1265af28f636SEnrico Perla - Sun Microsystems 	stage2->buf = malloc(buf_size);
1266af28f636SEnrico Perla - Sun Microsystems 	if (stage2->buf == NULL) {
1267af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Memory allocation failed"));
1268af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1269af28f636SEnrico Perla - Sun Microsystems 	}
1270af28f636SEnrico Perla - Sun Microsystems 	stage2->buf_size = buf_size;
12717ce76caaSEnrico Perla - Sun Microsystems 
12721a902ef8SHans Rosenfeld 	if (read_in(dev_fd, stage2->buf, buf_size, STAGE2_BLKOFF(type) *
1273af28f636SEnrico Perla - Sun Microsystems 	    SECTOR_SIZE) != BC_SUCCESS) {
1274af28f636SEnrico Perla - Sun Microsystems 		perror("read");
1275af28f636SEnrico Perla - Sun Microsystems 		free(stage2->buf);
1276af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
12777ce76caaSEnrico Perla - Sun Microsystems 	}
12787ce76caaSEnrico Perla - Sun Microsystems 
1279af28f636SEnrico Perla - Sun Microsystems 	/* Update pointers. */
1280af28f636SEnrico Perla - Sun Microsystems 	stage2->file = stage2->buf;
1281af28f636SEnrico Perla - Sun Microsystems 	stage2->file_size = size;
1282af28f636SEnrico Perla - Sun Microsystems 	stage2->mboot_off = mboot_off;
1283af28f636SEnrico Perla - Sun Microsystems 	stage2->mboot = (multiboot_header_t *)(stage2->buf + stage2->mboot_off);
1284af28f636SEnrico Perla - Sun Microsystems 	stage2->extra = stage2->buf + P2ROUNDUP(stage2->file_size, 8);
128514d44f22SHans Rosenfeld 	stage2->extra_size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
12867ce76caaSEnrico Perla - Sun Microsystems 
1287af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
1288af28f636SEnrico Perla - Sun Microsystems }
12897ce76caaSEnrico Perla - Sun Microsystems 
1290af28f636SEnrico Perla - Sun Microsystems static boolean_t
is_update_necessary(ig_data_t * data,char * updt_str)1291af28f636SEnrico Perla - Sun Microsystems is_update_necessary(ig_data_t *data, char *updt_str)
12927ce76caaSEnrico Perla - Sun Microsystems {
1293af28f636SEnrico Perla - Sun Microsystems 	bblk_einfo_t	*einfo;
1294af28f636SEnrico Perla - Sun Microsystems 	bblk_hs_t	stage2_hs;
1295af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	stage2_disk;
1296af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2_file = &data->stage2;
1297af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &data->device;
1298af28f636SEnrico Perla - Sun Microsystems 	int		dev_fd = device->part_fd;
1299af28f636SEnrico Perla - Sun Microsystems 
1300af28f636SEnrico Perla - Sun Microsystems 	assert(data != NULL);
1301af28f636SEnrico Perla - Sun Microsystems 	assert(device->part_fd != -1);
1302af28f636SEnrico Perla - Sun Microsystems 
1303af28f636SEnrico Perla - Sun Microsystems 	bzero(&stage2_disk, sizeof (ig_stage2_t));
1304af28f636SEnrico Perla - Sun Microsystems 
1305af28f636SEnrico Perla - Sun Microsystems 	/* Gather stage2 (if present) from the target device. */
13061a902ef8SHans Rosenfeld 	if (read_stage2_from_disk(dev_fd, &stage2_disk, device->type)
13071a902ef8SHans Rosenfeld 	    != BC_SUCCESS) {
1308af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Unable to read stage2 from %s\n", device->path);
1309af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("No multiboot wrapped stage2 on %s\n", device->path);
1310af28f636SEnrico Perla - Sun Microsystems 		return (B_TRUE);
1311af28f636SEnrico Perla - Sun Microsystems 	}
13127ce76caaSEnrico Perla - Sun Microsystems 
1313af28f636SEnrico Perla - Sun Microsystems 	/*
1314af28f636SEnrico Perla - Sun Microsystems 	 * Look for the extended information structure in the extra payload
1315af28f636SEnrico Perla - Sun Microsystems 	 * area.
1316af28f636SEnrico Perla - Sun Microsystems 	 */
131714d44f22SHans Rosenfeld 	einfo = find_einfo(stage2_disk.extra, stage2_disk.extra_size);
1318af28f636SEnrico Perla - Sun Microsystems 	if (einfo == NULL) {
1319af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("No extended information available\n");
1320af28f636SEnrico Perla - Sun Microsystems 		return (B_TRUE);
1321af28f636SEnrico Perla - Sun Microsystems 	}
13227ce76caaSEnrico Perla - Sun Microsystems 
1323af28f636SEnrico Perla - Sun Microsystems 	if (!do_version || updt_str == NULL) {
1324af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stdout, "WARNING: target device %s has a "
1325af28f636SEnrico Perla - Sun Microsystems 		    "versioned stage2 that is going to be overwritten by a non "
1326af28f636SEnrico Perla - Sun Microsystems 		    "versioned one\n", device->path);
1327af28f636SEnrico Perla - Sun Microsystems 		return (B_TRUE);
1328af28f636SEnrico Perla - Sun Microsystems 	}
13297ce76caaSEnrico Perla - Sun Microsystems 
1330af28f636SEnrico Perla - Sun Microsystems 	if (force_update) {
1331af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Forcing update of %s bootblock\n", device->path);
1332af28f636SEnrico Perla - Sun Microsystems 		return (B_TRUE);
1333af28f636SEnrico Perla - Sun Microsystems 	}
13347ce76caaSEnrico Perla - Sun Microsystems 
1335af28f636SEnrico Perla - Sun Microsystems 	/* Compare the two extended information structures. */
1336af28f636SEnrico Perla - Sun Microsystems 	stage2_hs.src_buf = (unsigned char *)stage2_file->file;
1337af28f636SEnrico Perla - Sun Microsystems 	stage2_hs.src_size = stage2_file->file_size;
1338af28f636SEnrico Perla - Sun Microsystems 
1339af28f636SEnrico Perla - Sun Microsystems 	return (einfo_should_update(einfo, &stage2_hs, updt_str));
13407ce76caaSEnrico Perla - Sun Microsystems }
13417ce76caaSEnrico Perla - Sun Microsystems 
13427ce76caaSEnrico Perla - Sun Microsystems 
13437c478bd9Sstevel@tonic-gate #define	START_BLOCK(pos)	(*(ulong_t *)(pos))
13447c478bd9Sstevel@tonic-gate #define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
13457c478bd9Sstevel@tonic-gate #define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))
13467c478bd9Sstevel@tonic-gate 
1347af28f636SEnrico Perla - Sun Microsystems static int
prepare_stage2(ig_data_t * install,char * updt_str)1348af28f636SEnrico Perla - Sun Microsystems prepare_stage2(ig_data_t *install, char *updt_str)
13497c478bd9Sstevel@tonic-gate {
1350af28f636SEnrico Perla - Sun Microsystems 	ig_device_t	*device = &install->device;
1351af28f636SEnrico Perla - Sun Microsystems 	ig_stage2_t	*stage2 = &install->stage2;
1352af28f636SEnrico Perla - Sun Microsystems 	uint32_t	mboot_off = 0;
1353af28f636SEnrico Perla - Sun Microsystems 
1354af28f636SEnrico Perla - Sun Microsystems 	assert(install != NULL);
1355af28f636SEnrico Perla - Sun Microsystems 	assert(stage2->file != NULL);
1356af28f636SEnrico Perla - Sun Microsystems 
1357af28f636SEnrico Perla - Sun Microsystems 	/* New stage2 files come with an embedded stage2. */
1358af28f636SEnrico Perla - Sun Microsystems 	if (find_multiboot(stage2->file, stage2->file_size, &mboot_off)
1359af28f636SEnrico Perla - Sun Microsystems 	    != BC_SUCCESS) {
1360af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("WARNING: no multiboot structure found in stage2, "
1361af28f636SEnrico Perla - Sun Microsystems 		    "are you using an old GRUB stage2?\n");
1362af28f636SEnrico Perla - Sun Microsystems 		if (do_version == B_TRUE) {
1363af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, gettext("Versioning requested "
1364af28f636SEnrico Perla - Sun Microsystems 			    "but stage2 does not support it.. skipping.\n"));
1365af28f636SEnrico Perla - Sun Microsystems 			do_version = B_FALSE;
1366af28f636SEnrico Perla - Sun Microsystems 		}
1367af28f636SEnrico Perla - Sun Microsystems 	} else {
1368af28f636SEnrico Perla - Sun Microsystems 		/* Keep track of where the multiboot header is. */
1369af28f636SEnrico Perla - Sun Microsystems 		stage2->mboot_off = mboot_off;
1370af28f636SEnrico Perla - Sun Microsystems 		stage2->mboot = (multiboot_header_t *)(stage2->file +
1371af28f636SEnrico Perla - Sun Microsystems 		    mboot_off);
1372af28f636SEnrico Perla - Sun Microsystems 		if (do_version) {
1373af28f636SEnrico Perla - Sun Microsystems 			/*
1374af28f636SEnrico Perla - Sun Microsystems 			 * Adding stage2 information needs to happen before
1375af28f636SEnrico Perla - Sun Microsystems 			 * we modify the copy of stage2 we have in memory, so
1376af28f636SEnrico Perla - Sun Microsystems 			 * that the hashing reflects the one of the file.
1377af28f636SEnrico Perla - Sun Microsystems 			 * An error here is not fatal.
1378af28f636SEnrico Perla - Sun Microsystems 			 */
1379af28f636SEnrico Perla - Sun Microsystems 			add_stage2_einfo(stage2, updt_str);
1380af28f636SEnrico Perla - Sun Microsystems 		}
1381af28f636SEnrico Perla - Sun Microsystems 		/*
1382af28f636SEnrico Perla - Sun Microsystems 		 * Fill multiboot information. We add them even without
1383af28f636SEnrico Perla - Sun Microsystems 		 * versioning to support as much as possible mirroring.
1384af28f636SEnrico Perla - Sun Microsystems 		 */
1385af28f636SEnrico Perla - Sun Microsystems 		prepare_fake_multiboot(stage2);
13867ce76caaSEnrico Perla - Sun Microsystems 	}
13877c478bd9Sstevel@tonic-gate 
1388af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
1389af28f636SEnrico Perla - Sun Microsystems 		uint32_t	blocklist[SECTOR_SIZE / sizeof (uint32_t)];
1390af28f636SEnrico Perla - Sun Microsystems 		uint32_t	install_addr = STAGE2_MEMADDR + SECTOR_SIZE;
1391af28f636SEnrico Perla - Sun Microsystems 		int		i = 0;
1392af28f636SEnrico Perla - Sun Microsystems 		uchar_t		*pos;
1393af28f636SEnrico Perla - Sun Microsystems 
1394af28f636SEnrico Perla - Sun Microsystems 		bzero(blocklist, sizeof (blocklist));
1395af28f636SEnrico Perla - Sun Microsystems 		if (read_stage2_blocklist(device->part_fd, blocklist) != 0) {
1396af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, gettext("Error reading pcfs "
1397af28f636SEnrico Perla - Sun Microsystems 			    "stage2 blocklist\n"));
1398af28f636SEnrico Perla - Sun Microsystems 			return (BC_ERROR);
1399af28f636SEnrico Perla - Sun Microsystems 		}
1400af28f636SEnrico Perla - Sun Microsystems 
1401af28f636SEnrico Perla - Sun Microsystems 		pos = (uchar_t *)stage2->file + STAGE2_BLOCKLIST;
1402af28f636SEnrico Perla - Sun Microsystems 		stage2->first_sector = device->start_sector + blocklist[0];
1403af28f636SEnrico Perla - Sun Microsystems 		stage2->pcfs_first_sectors[0] = blocklist[0];
1404af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		if (blocklist[1] > 1) {
14087c478bd9Sstevel@tonic-gate 			blocklist[0]++;
14097c478bd9Sstevel@tonic-gate 			blocklist[1]--;
14107c478bd9Sstevel@tonic-gate 		} else {
14117c478bd9Sstevel@tonic-gate 			i += 2;
14127c478bd9Sstevel@tonic-gate 		}
14137c478bd9Sstevel@tonic-gate 
1414af28f636SEnrico Perla - Sun Microsystems 		stage2->pcfs_first_sectors[1] = blocklist[i];
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		while (blocklist[i]) {
14177c478bd9Sstevel@tonic-gate 			if (START_BLOCK(pos - 8) != 0 &&
14187c478bd9Sstevel@tonic-gate 			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
14197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, PCFS_FRAGMENTED);
1420af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
14217c478bd9Sstevel@tonic-gate 			}
1422af28f636SEnrico Perla - Sun Microsystems 			START_BLOCK(pos) = blocklist[i] + device->start_sector;
14237c478bd9Sstevel@tonic-gate 			START_SEG(pos) = (ushort_t)(install_addr >> 4);
14247c478bd9Sstevel@tonic-gate 			NUM_BLOCK(pos) = blocklist[i + 1];
14257c478bd9Sstevel@tonic-gate 			install_addr += blocklist[i + 1] * SECTOR_SIZE;
14267c478bd9Sstevel@tonic-gate 			pos -= 8;
14277c478bd9Sstevel@tonic-gate 			i += 2;
14287c478bd9Sstevel@tonic-gate 		}
14297c478bd9Sstevel@tonic-gate 	} else {
14309890706eSHans Rosenfeld 		/* Solaris VTOC & EFI */
14319890706eSHans Rosenfeld 		if (device->start_sector >
14329890706eSHans Rosenfeld 		    UINT32_MAX - STAGE2_BLKOFF(device->type)) {
14339890706eSHans Rosenfeld 			fprintf(stderr, gettext("Error: partition start sector "
14349890706eSHans Rosenfeld 			    "must be less than %lld\n"),
14359890706eSHans Rosenfeld 			    (uint64_t)UINT32_MAX - STAGE2_BLKOFF(device->type));
14369890706eSHans Rosenfeld 			return (BC_ERROR);
14379890706eSHans Rosenfeld 		}
14381a902ef8SHans Rosenfeld 		stage2->first_sector = device->start_sector +
14391a902ef8SHans Rosenfeld 		    STAGE2_BLKOFF(device->type);
1440af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);
14417c478bd9Sstevel@tonic-gate 		/*
14427c478bd9Sstevel@tonic-gate 		 * In a solaris partition, stage2 is written to contiguous
14437c478bd9Sstevel@tonic-gate 		 * blocks. So we update the starting block only.
14447c478bd9Sstevel@tonic-gate 		 */
1445af28f636SEnrico Perla - Sun Microsystems 		*((ulong_t *)(stage2->file + STAGE2_BLOCKLIST)) =
1446af28f636SEnrico Perla - Sun Microsystems 		    stage2->first_sector + 1;
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
1449af28f636SEnrico Perla - Sun Microsystems 	/* force lba and set disk partition */
1450af28f636SEnrico Perla - Sun Microsystems 	*((unsigned char *) (stage2->file + STAGE2_FORCE_LBA)) = 1;
1451af28f636SEnrico Perla - Sun Microsystems 	*((long *)(stage2->file + STAGE2_INSTALLPART))
1452af28f636SEnrico Perla - Sun Microsystems 	    = (device->partition << 16) | (device->slice << 8) | 0xff;
1453af28f636SEnrico Perla - Sun Microsystems 
1454af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
14557c478bd9Sstevel@tonic-gate }
14567c478bd9Sstevel@tonic-gate 
1457af28f636SEnrico Perla - Sun Microsystems static int
find_x86_bootpar(struct mboot * mboot,int * part_num,uint32_t * start_sect)1458af28f636SEnrico Perla - Sun Microsystems find_x86_bootpar(struct mboot *mboot, int *part_num, uint32_t *start_sect)
14597c478bd9Sstevel@tonic-gate {
1460af28f636SEnrico Perla - Sun Microsystems 	int	i;
14617c478bd9Sstevel@tonic-gate 
1462af28f636SEnrico Perla - Sun Microsystems 	for (i = 0; i < FD_NUMPART; i++) {
1463af28f636SEnrico Perla - Sun Microsystems 		struct ipart	*part;
14647c478bd9Sstevel@tonic-gate 
1465af28f636SEnrico Perla - Sun Microsystems 		part = (struct ipart *)mboot->parts + i;
1466af28f636SEnrico Perla - Sun Microsystems 		if (part->systid == 0xbe) {
1467af28f636SEnrico Perla - Sun Microsystems 			if (start_sect)
1468af28f636SEnrico Perla - Sun Microsystems 				*start_sect = part->relsect;
1469af28f636SEnrico Perla - Sun Microsystems 			if (part_num)
1470af28f636SEnrico Perla - Sun Microsystems 				*part_num = i;
1471af28f636SEnrico Perla - Sun Microsystems 			/* solaris boot part */
1472af28f636SEnrico Perla - Sun Microsystems 			return (BC_SUCCESS);
1473af28f636SEnrico Perla - Sun Microsystems 		}
1474af28f636SEnrico Perla - Sun Microsystems 	}
1475af28f636SEnrico Perla - Sun Microsystems 	return (BC_ERROR);
1476af28f636SEnrico Perla - Sun Microsystems }
14777c478bd9Sstevel@tonic-gate 
1478af28f636SEnrico Perla - Sun Microsystems static char *
get_raw_partition_path(ig_device_t * device)1479af28f636SEnrico Perla - Sun Microsystems get_raw_partition_path(ig_device_t *device)
1480af28f636SEnrico Perla - Sun Microsystems {
1481af28f636SEnrico Perla - Sun Microsystems 	char	*raw;
1482af28f636SEnrico Perla - Sun Microsystems 	int	len;
1483af28f636SEnrico Perla - Sun Microsystems 
1484af28f636SEnrico Perla - Sun Microsystems 	if (is_bootpar(device->type)) {
1485af28f636SEnrico Perla - Sun Microsystems 		int		part;
1486af28f636SEnrico Perla - Sun Microsystems 		struct mboot	*mboot;
1487af28f636SEnrico Perla - Sun Microsystems 
1488af28f636SEnrico Perla - Sun Microsystems 		mboot = (struct mboot *)device->boot_sector;
1489af28f636SEnrico Perla - Sun Microsystems 		if (find_x86_bootpar(mboot, &part, NULL) != BC_SUCCESS) {
1490af28f636SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, BOOTPAR_NOTFOUND,
1491af28f636SEnrico Perla - Sun Microsystems 			    device->path_p0);
1492af28f636SEnrico Perla - Sun Microsystems 			return (NULL);
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 
1495af28f636SEnrico Perla - Sun Microsystems 		raw = strdup(device->path_p0);
1496af28f636SEnrico Perla - Sun Microsystems 		if (raw == NULL) {
1497af28f636SEnrico Perla - Sun Microsystems 			perror(gettext("Memory allocation failed"));
1498af28f636SEnrico Perla - Sun Microsystems 			return (NULL);
14997c478bd9Sstevel@tonic-gate 		}
1500af28f636SEnrico Perla - Sun Microsystems 
1501af28f636SEnrico Perla - Sun Microsystems 		raw[strlen(raw) - 2] = '1' + part;
15027c478bd9Sstevel@tonic-gate 		return (raw);
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/* For disk, remember slice and return whole fdisk partition  */
1506af28f636SEnrico Perla - Sun Microsystems 	raw = strdup(device->path);
1507af28f636SEnrico Perla - Sun Microsystems 	if (raw == NULL) {
1508af28f636SEnrico Perla - Sun Microsystems 		perror(gettext("Memory allocation failed"));
1509af28f636SEnrico Perla - Sun Microsystems 		return (NULL);
1510af28f636SEnrico Perla - Sun Microsystems 	}
1511af28f636SEnrico Perla - Sun Microsystems 
15127c478bd9Sstevel@tonic-gate 	len = strlen(raw);
15131a902ef8SHans Rosenfeld 	if (!is_efi(device->type) &&
15141a902ef8SHans Rosenfeld 	    (raw[len - 2] != 's' || raw[len - 1] == '2')) {
15157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_ROOT_SLICE);
1516af28f636SEnrico Perla - Sun Microsystems 		free(raw);
1517af28f636SEnrico Perla - Sun Microsystems 		return (NULL);
15187c478bd9Sstevel@tonic-gate 	}
1519af28f636SEnrico Perla - Sun Microsystems 	device->slice = atoi(&raw[len - 1]);
15207c478bd9Sstevel@tonic-gate 
15211a902ef8SHans Rosenfeld 	if (!is_efi(device->type)) {
15221a902ef8SHans Rosenfeld 		raw[len - 2] = 's';
15231a902ef8SHans Rosenfeld 		raw[len - 1] = '2';
15241a902ef8SHans Rosenfeld 	}
1525af28f636SEnrico Perla - Sun Microsystems 
15267c478bd9Sstevel@tonic-gate 	return (raw);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
1529af28f636SEnrico Perla - Sun Microsystems static int
get_raw_partition_fd(ig_device_t * device)1530af28f636SEnrico Perla - Sun Microsystems get_raw_partition_fd(ig_device_t *device)
1531af28f636SEnrico Perla - Sun Microsystems {
1532af28f636SEnrico Perla - Sun Microsystems 	struct stat	stat = {0};
1533af28f636SEnrico Perla - Sun Microsystems 	char		*raw;
1534af28f636SEnrico Perla - Sun Microsystems 
1535af28f636SEnrico Perla - Sun Microsystems 	raw = get_raw_partition_path(device);
1536af28f636SEnrico Perla - Sun Microsystems 	if (raw == NULL)
1537af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1538af28f636SEnrico Perla - Sun Microsystems 
1539af28f636SEnrico Perla - Sun Microsystems 	if (nowrite)
1540af28f636SEnrico Perla - Sun Microsystems 		device->part_fd = open(raw, O_RDONLY);
1541af28f636SEnrico Perla - Sun Microsystems 	else
1542af28f636SEnrico Perla - Sun Microsystems 		device->part_fd = open(raw, O_RDWR);
1543af28f636SEnrico Perla - Sun Microsystems 
1544af28f636SEnrico Perla - Sun Microsystems 	if (device->part_fd < 0 || fstat(device->part_fd, &stat) != 0) {
1545af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, OPEN_FAIL, raw);
1546af28f636SEnrico Perla - Sun Microsystems 		free(raw);
1547af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1548af28f636SEnrico Perla - Sun Microsystems 	}
1549af28f636SEnrico Perla - Sun Microsystems 
1550af28f636SEnrico Perla - Sun Microsystems 	if (S_ISCHR(stat.st_mode) == 0) {
1551af28f636SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, NOT_RAW_DEVICE, raw);
1552af28f636SEnrico Perla - Sun Microsystems 		(void) close(device->part_fd);
1553af28f636SEnrico Perla - Sun Microsystems 		device->part_fd = -1;
1554af28f636SEnrico Perla - Sun Microsystems 		free(raw);
1555af28f636SEnrico Perla - Sun Microsystems 		return (BC_ERROR);
1556af28f636SEnrico Perla - Sun Microsystems 	}
1557af28f636SEnrico Perla - Sun Microsystems 
1558af28f636SEnrico Perla - Sun Microsystems 	free(raw);
1559af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
1560af28f636SEnrico Perla - Sun Microsystems }
1561