xref: /illumos-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision adc586debf12d2592024c0b8b9e44ffa104f858c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * bootadm(1M) is a new utility for managing bootability of
28  * Solaris *Newboot* environments. It has two primary tasks:
29  * 	- Allow end users to manage bootability of Newboot Solaris instances
30  *	- Provide services to other subsystems in Solaris (primarily Install)
31  */
32 
33 /* Headers */
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdarg.h>
42 #include <limits.h>
43 #include <signal.h>
44 #include <sys/wait.h>
45 #include <sys/mnttab.h>
46 #include <sys/mntent.h>
47 #include <sys/statvfs.h>
48 #include <libnvpair.h>
49 #include <ftw.h>
50 #include <fcntl.h>
51 #include <strings.h>
52 #include <utime.h>
53 #include <sys/systeminfo.h>
54 #include <sys/dktp/fdisk.h>
55 #include <sys/param.h>
56 #include <dirent.h>
57 #include <ctype.h>
58 #include <libgen.h>
59 #include <sys/sysmacros.h>
60 #include <libscf.h>
61 
62 #if !defined(_OPB)
63 #include <sys/ucode.h>
64 #endif
65 
66 #include <pwd.h>
67 #include <grp.h>
68 #include <device_info.h>
69 #include <sys/vtoc.h>
70 #include <sys/efi_partition.h>
71 
72 #include <locale.h>
73 
74 #include "message.h"
75 #include "bootadm.h"
76 
77 #ifndef TEXT_DOMAIN
78 #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
79 #endif	/* TEXT_DOMAIN */
80 
81 /* Type definitions */
82 
83 /* Primary subcmds */
84 typedef enum {
85 	BAM_MENU = 3,
86 	BAM_ARCHIVE
87 } subcmd_t;
88 
89 typedef enum {
90     OPT_ABSENT = 0,	/* No option */
91     OPT_REQ,		/* option required */
92     OPT_OPTIONAL	/* option may or may not be present */
93 } option_t;
94 
95 typedef struct {
96 	char	*subcmd;
97 	option_t option;
98 	error_t (*handler)();
99 	int	unpriv;			/* is this an unprivileged command */
100 } subcmd_defn_t;
101 
102 #define	LINE_INIT	0	/* lineNum initial value */
103 #define	ENTRY_INIT	-1	/* entryNum initial value */
104 #define	ALL_ENTRIES	-2	/* selects all boot entries */
105 
106 #define	GRUB_DIR		"/boot/grub"
107 #define	GRUB_STAGE2		GRUB_DIR "/stage2"
108 #define	GRUB_MENU		"/boot/grub/menu.lst"
109 #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
110 #define	GRUB_BACKUP_MENU	"/etc/lu/GRUB_backup_menu"
111 #define	RAMDISK_SPECIAL		"/ramdisk"
112 #define	STUBBOOT		"/stubboot"
113 #define	MULTIBOOT		"/platform/i86pc/multiboot"
114 #define	GRUBSIGN_DIR		"/boot/grub/bootsign"
115 #define	GRUBSIGN_BACKUP		"/etc/bootsign"
116 #define	GRUBSIGN_UFS_PREFIX	"rootfs"
117 #define	GRUBSIGN_ZFS_PREFIX	"pool_"
118 #define	GRUBSIGN_LU_PREFIX	"BE_"
119 #define	UFS_SIGNATURE_LIST	"/var/run/grub_ufs_signatures"
120 #define	ZFS_LEGACY_MNTPT	"/tmp/bootadm_mnt_zfs_legacy"
121 
122 #define	BOOTADM_RDONLY_TEST	"BOOTADM_RDONLY_TEST"
123 
124 /* lock related */
125 #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
126 #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
127 
128 #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
129 #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
130 #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
131 #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
132 
133 #define	GRUB_slice		"/etc/lu/GRUB_slice"
134 #define	GRUB_root		"/etc/lu/GRUB_root"
135 #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
136 #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
137 #define	FINDROOT_INSTALLGRUB	"/etc/lu/installgrub.findroot"
138 #define	LULIB			"/usr/lib/lu/lulib"
139 #define	LULIB_PROPAGATE_FILE	"lulib_propagate_file"
140 #define	CKSUM			"/usr/bin/cksum"
141 #define	LU_MENU_CKSUM		"/etc/lu/menu.cksum"
142 #define	BOOTADM			"/sbin/bootadm"
143 
144 #define	INSTALLGRUB		"/sbin/installgrub"
145 #define	STAGE1			"/boot/grub/stage1"
146 #define	STAGE2			"/boot/grub/stage2"
147 
148 typedef enum zfs_mnted {
149 	ZFS_MNT_ERROR = -1,
150 	LEGACY_MOUNTED = 1,
151 	LEGACY_ALREADY,
152 	ZFS_MOUNTED,
153 	ZFS_ALREADY
154 } zfs_mnted_t;
155 
156 
157 
158 
159 /*
160  * The following two defines are used to detect and create the correct
161  * boot archive  when safemode patching is underway.  LOFS_PATCH_FILE is a
162  * contracted private interface between bootadm and the install
163  * consolidation.  It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE
164  * is applied.
165  */
166 
167 #define	LOFS_PATCH_FILE		"/var/run/.patch_loopback_mode"
168 #define	LOFS_PATCH_MNT		"/var/run/.patch_root_loopbackmnt"
169 
170 /*
171  * Default file attributes
172  */
173 #define	DEFAULT_DEV_MODE	0644	/* default permissions */
174 #define	DEFAULT_DEV_UID		0	/* user root */
175 #define	DEFAULT_DEV_GID		3	/* group sys */
176 
177 /*
178  * Menu related
179  * menu_cmd_t and menu_cmds must be kept in sync
180  */
181 char *menu_cmds[] = {
182 	"default",	/* DEFAULT_CMD */
183 	"timeout",	/* TIMEOUT_CMD */
184 	"title",	/* TITLE_CMD */
185 	"root",		/* ROOT_CMD */
186 	"kernel",	/* KERNEL_CMD */
187 	"kernel$",	/* KERNEL_DOLLAR_CMD */
188 	"module",	/* MODULE_CMD */
189 	"module$",	/* MODULE_DOLLAR_CMD */
190 	" ",		/* SEP_CMD */
191 	"#",		/* COMMENT_CMD */
192 	"chainloader",	/* CHAINLOADER_CMD */
193 	"args",		/* ARGS_CMD */
194 	"findroot",	/* FINDROOT_CMD */
195 	NULL
196 };
197 
198 #define	OPT_ENTRY_NUM	"entry"
199 
200 /*
201  * exec_cmd related
202  */
203 typedef struct {
204 	line_t *head;
205 	line_t *tail;
206 } filelist_t;
207 
208 #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
209 #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
210 
211 #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
212 #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
213 #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
214 #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
215 
216 /* Globals */
217 int bam_verbose;
218 int bam_force;
219 int bam_debug;
220 static char *prog;
221 static subcmd_t bam_cmd;
222 static char *bam_root;
223 static int bam_rootlen;
224 static int bam_root_readonly;
225 static int bam_alt_root;
226 static char *bam_subcmd;
227 static char *bam_opt;
228 static char **bam_argv;
229 static int bam_argc;
230 static int bam_check;
231 static int bam_smf_check;
232 static int bam_lock_fd = -1;
233 static int bam_zfs;
234 static char rootbuf[PATH_MAX] = "/";
235 static int bam_update_all;
236 static int bam_alt_platform;
237 static char *bam_platform;
238 
239 /* function prototypes */
240 static void parse_args_internal(int, char *[]);
241 static void parse_args(int, char *argv[]);
242 static error_t bam_menu(char *, char *, int, char *[]);
243 static error_t bam_archive(char *, char *);
244 
245 static void bam_exit(int);
246 static void bam_lock(void);
247 static void bam_unlock(void);
248 
249 static int exec_cmd(char *, filelist_t *);
250 static error_t read_globals(menu_t *, char *, char *, int);
251 static int menu_on_bootdisk(char *os_root, char *menu_root);
252 static menu_t *menu_read(char *);
253 static error_t menu_write(char *, menu_t *);
254 static void linelist_free(line_t *);
255 static void menu_free(menu_t *);
256 static void filelist_free(filelist_t *);
257 static error_t list2file(char *, char *, char *, line_t *);
258 static error_t list_entry(menu_t *, char *, char *);
259 static error_t delete_all_entries(menu_t *, char *, char *);
260 static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
261 static error_t update_temp(menu_t *mp, char *dummy, char *opt);
262 
263 static error_t update_archive(char *, char *);
264 static error_t list_archive(char *, char *);
265 static error_t update_all(char *, char *);
266 static error_t read_list(char *, filelist_t *);
267 static error_t set_global(menu_t *, char *, int);
268 static error_t set_option(menu_t *, char *, char *);
269 static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
270 static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
271 static char *expand_path(const char *);
272 
273 static long s_strtol(char *);
274 static int s_fputs(char *, FILE *);
275 
276 static int is_zfs(char *root);
277 static int is_ufs(char *root);
278 static int is_pcfs(char *root);
279 static int is_amd64(void);
280 static char *get_machine(void);
281 static void append_to_flist(filelist_t *, char *);
282 static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted);
283 static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt);
284 static int ufs_add_to_sign_list(char *sign);
285 static error_t synchronize_BE_menu(void);
286 
287 #if !defined(_OPB)
288 static void ucode_install();
289 #endif
290 
291 /* Menu related sub commands */
292 static subcmd_defn_t menu_subcmds[] = {
293 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
294 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
295 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
296 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
297 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
298 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
299 	NULL,			0,		NULL, 0	/* must be last */
300 };
301 
302 /* Archive related sub commands */
303 static subcmd_defn_t arch_subcmds[] = {
304 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
305 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
306 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
307 	NULL,			0,		NULL, 0	/* must be last */
308 };
309 
310 static struct {
311 	nvlist_t *new_nvlp;
312 	nvlist_t *old_nvlp;
313 	int need_update;
314 } walk_arg;
315 
316 
317 struct safefile {
318 	char *name;
319 	struct safefile *next;
320 };
321 
322 static struct safefile *safefiles = NULL;
323 #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
324 
325 static void
326 usage(void)
327 {
328 	(void) fprintf(stderr, "USAGE:\n");
329 
330 
331 	/* archive usage */
332 	(void) fprintf(stderr,
333 	    "\t%s update-archive [-vn] [-R altroot [-p platform>]]\n", prog);
334 	(void) fprintf(stderr,
335 	    "\t%s list-archive [-R altroot [-p platform>]]\n", prog);
336 #if !defined(_OPB)
337 	/* x86 only */
338 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
339 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
340 #endif
341 }
342 
343 int
344 main(int argc, char *argv[])
345 {
346 	error_t ret;
347 
348 	(void) setlocale(LC_ALL, "");
349 	(void) textdomain(TEXT_DOMAIN);
350 
351 	if ((prog = strrchr(argv[0], '/')) == NULL) {
352 		prog = argv[0];
353 	} else {
354 		prog++;
355 	}
356 
357 	INJECT_ERROR1("ASSERT_ON", assert(0))
358 
359 	/*
360 	 * Don't depend on caller's umask
361 	 */
362 	(void) umask(0022);
363 
364 	parse_args(argc, argv);
365 
366 	switch (bam_cmd) {
367 		case BAM_MENU:
368 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
369 			break;
370 		case BAM_ARCHIVE:
371 			ret = bam_archive(bam_subcmd, bam_opt);
372 			break;
373 		default:
374 			usage();
375 			bam_exit(1);
376 	}
377 
378 	if (ret != BAM_SUCCESS)
379 		bam_exit(1);
380 
381 	bam_unlock();
382 	return (0);
383 }
384 
385 /*
386  * Equivalence of public and internal commands:
387  *	update-archive  -- -a update
388  *	list-archive	-- -a list
389  *	set-menu	-- -m set_option
390  *	list-menu	-- -m list_entry
391  *	update-menu	-- -m update_entry
392  */
393 static struct cmd_map {
394 	char *bam_cmdname;
395 	int bam_cmd;
396 	char *bam_subcmd;
397 } cmd_map[] = {
398 	{ "update-archive",	BAM_ARCHIVE,	"update"},
399 	{ "list-archive",	BAM_ARCHIVE,	"list"},
400 	{ "set-menu",		BAM_MENU,	"set_option"},
401 	{ "list-menu",		BAM_MENU,	"list_entry"},
402 	{ "update-menu",	BAM_MENU,	"update_entry"},
403 	{ NULL,			0,		NULL}
404 };
405 
406 /*
407  * Commands syntax published in bootadm(1M) are parsed here
408  */
409 static void
410 parse_args(int argc, char *argv[])
411 {
412 	struct cmd_map *cmp = cmd_map;
413 
414 	/* command conforming to the final spec */
415 	if (argc > 1 && argv[1][0] != '-') {
416 		/*
417 		 * Map commands to internal table.
418 		 */
419 		while (cmp->bam_cmdname) {
420 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
421 				bam_cmd = cmp->bam_cmd;
422 				bam_subcmd = cmp->bam_subcmd;
423 				break;
424 			}
425 			cmp++;
426 		}
427 		if (cmp->bam_cmdname == NULL) {
428 			usage();
429 			bam_exit(1);
430 		}
431 		argc--;
432 		argv++;
433 	}
434 
435 	parse_args_internal(argc, argv);
436 }
437 
438 /*
439  * A combination of public and private commands are parsed here.
440  * The internal syntax and the corresponding functionality are:
441  *	-a update	-- update-archive
442  *	-a list		-- list-archive
443  *	-a update-all	-- (reboot to sync all mounted OS archive)
444  *	-m update_entry	-- update-menu
445  *	-m list_entry	-- list-menu
446  *	-m update_temp	-- (reboot -- [boot-args])
447  *	-m delete_all_entries -- (called from install)
448  */
449 static void
450 parse_args_internal(int argc, char *argv[])
451 {
452 	int c, error;
453 	extern char *optarg;
454 	extern int optind, opterr;
455 
456 	/* Suppress error message from getopt */
457 	opterr = 0;
458 
459 	error = 0;
460 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:p:Z")) != -1) {
461 		switch (c) {
462 		case 'a':
463 			if (bam_cmd) {
464 				error = 1;
465 				bam_error(MULT_CMDS, c);
466 			}
467 			bam_cmd = BAM_ARCHIVE;
468 			bam_subcmd = optarg;
469 			break;
470 		case 'd':
471 			if (bam_debug) {
472 				error = 1;
473 				bam_error(DUP_OPT, c);
474 			}
475 			bam_debug = s_strtol(optarg);
476 			break;
477 		case 'f':
478 			if (bam_force) {
479 				error = 1;
480 				bam_error(DUP_OPT, c);
481 			}
482 			bam_force = 1;
483 			break;
484 		case 'm':
485 			if (bam_cmd) {
486 				error = 1;
487 				bam_error(MULT_CMDS, c);
488 			}
489 			bam_cmd = BAM_MENU;
490 			bam_subcmd = optarg;
491 			break;
492 		case 'n':
493 			if (bam_check) {
494 				error = 1;
495 				bam_error(DUP_OPT, c);
496 			}
497 			bam_check = 1;
498 			break;
499 		case 'o':
500 			if (bam_opt) {
501 				error = 1;
502 				bam_error(DUP_OPT, c);
503 			}
504 			bam_opt = optarg;
505 			break;
506 		case 'v':
507 			if (bam_verbose) {
508 				error = 1;
509 				bam_error(DUP_OPT, c);
510 			}
511 			bam_verbose = 1;
512 			break;
513 		case 'C':
514 			bam_smf_check = 1;
515 			break;
516 		case 'R':
517 			if (bam_root) {
518 				error = 1;
519 				bam_error(DUP_OPT, c);
520 				break;
521 			} else if (realpath(optarg, rootbuf) == NULL) {
522 				error = 1;
523 				bam_error(CANT_RESOLVE, optarg,
524 				    strerror(errno));
525 				break;
526 			}
527 			bam_alt_root = 1;
528 			bam_root = rootbuf;
529 			bam_rootlen = strlen(rootbuf);
530 			break;
531 		case 'p':
532 			bam_alt_platform = 1;
533 			bam_platform = optarg;
534 			if ((strcmp(bam_platform, "i86pc") != 0) &&
535 			    (strcmp(bam_platform, "sun4u") != 0) &&
536 			    (strcmp(bam_platform, "sun4v") != 0)) {
537 				error = 1;
538 				bam_error(INVALID_PLAT, bam_platform);
539 			}
540 			break;
541 		case 'Z':
542 			bam_zfs = 1;
543 			break;
544 		case '?':
545 			error = 1;
546 			bam_error(BAD_OPT, optopt);
547 			break;
548 		default :
549 			error = 1;
550 			bam_error(BAD_OPT, c);
551 			break;
552 		}
553 	}
554 
555 	/*
556 	 * An alternate platform requires an alternate root
557 	 */
558 	if (bam_alt_platform && bam_alt_root == 0) {
559 		usage();
560 		bam_exit(0);
561 	}
562 
563 	/*
564 	 * A command option must be specfied
565 	 */
566 	if (!bam_cmd) {
567 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
568 			usage();
569 			bam_exit(0);
570 		}
571 		bam_error(NEED_CMD);
572 		error = 1;
573 	}
574 
575 	if (error) {
576 		usage();
577 		bam_exit(1);
578 	}
579 
580 	if (optind > argc) {
581 		bam_error(INT_ERROR, "parse_args");
582 		bam_exit(1);
583 	} else if (optind < argc) {
584 		bam_argv = &argv[optind];
585 		bam_argc = argc - optind;
586 	}
587 
588 	/*
589 	 * -n implies verbose mode
590 	 */
591 	if (bam_check)
592 		bam_verbose = 1;
593 }
594 
595 static error_t
596 check_subcmd_and_options(
597 	char *subcmd,
598 	char *opt,
599 	subcmd_defn_t *table,
600 	error_t (**fp)())
601 {
602 	int i;
603 
604 	if (subcmd == NULL) {
605 		bam_error(NEED_SUBCMD);
606 		return (BAM_ERROR);
607 	}
608 
609 	if (strcmp(subcmd, "set_option") == 0) {
610 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
611 			bam_error(MISSING_ARG);
612 			usage();
613 			return (BAM_ERROR);
614 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
615 			bam_error(TRAILING_ARGS);
616 			usage();
617 			return (BAM_ERROR);
618 		}
619 	} else if (bam_argc || bam_argv) {
620 		bam_error(TRAILING_ARGS);
621 		usage();
622 		return (BAM_ERROR);
623 	}
624 
625 	if (bam_root == NULL) {
626 		bam_root = rootbuf;
627 		bam_rootlen = 1;
628 	}
629 
630 	/* verify that subcmd is valid */
631 	for (i = 0; table[i].subcmd != NULL; i++) {
632 		if (strcmp(table[i].subcmd, subcmd) == 0)
633 			break;
634 	}
635 
636 	if (table[i].subcmd == NULL) {
637 		bam_error(INVALID_SUBCMD, subcmd);
638 		return (BAM_ERROR);
639 	}
640 
641 	if (table[i].unpriv == 0 && geteuid() != 0) {
642 		bam_error(MUST_BE_ROOT);
643 		return (BAM_ERROR);
644 	}
645 
646 	/*
647 	 * Currently only privileged commands need a lock
648 	 */
649 	if (table[i].unpriv == 0)
650 		bam_lock();
651 
652 	/* subcmd verifies that opt is appropriate */
653 	if (table[i].option != OPT_OPTIONAL) {
654 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
655 			if (opt)
656 				bam_error(NO_OPT_REQ, subcmd);
657 			else
658 				bam_error(MISS_OPT, subcmd);
659 			return (BAM_ERROR);
660 		}
661 	}
662 
663 	*fp = table[i].handler;
664 
665 	return (BAM_SUCCESS);
666 }
667 
668 /*
669  * NOTE: A single "/" is also considered a trailing slash and will
670  * be deleted.
671  */
672 static void
673 elide_trailing_slash(const char *src, char *dst, size_t dstsize)
674 {
675 	size_t dstlen;
676 
677 	assert(src);
678 	assert(dst);
679 
680 	(void) strlcpy(dst, src, dstsize);
681 
682 	dstlen = strlen(dst);
683 	if (dst[dstlen - 1] == '/') {
684 		dst[dstlen - 1] = '\0';
685 	}
686 }
687 
688 static error_t
689 bam_menu(char *subcmd, char *opt, int largc, char *largv[])
690 {
691 	error_t			ret;
692 	char			menu_path[PATH_MAX];
693 	char			clean_menu_root[PATH_MAX];
694 	char			path[PATH_MAX];
695 	menu_t			*menu;
696 	char			menu_root[PATH_MAX];
697 	struct stat		sb;
698 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
699 	char			*special;
700 	char			*pool = NULL;
701 	zfs_mnted_t		zmnted;
702 	char			*zmntpt;
703 	char			*osdev;
704 	char			*osroot;
705 	const char		*fcn = "bam_menu()";
706 
707 	/*
708 	 * Menu sub-command only applies to GRUB (i.e. x86)
709 	 */
710 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
711 		bam_error(NOT_GRUB_BOOT);
712 		return (BAM_ERROR);
713 	}
714 
715 	/*
716 	 * Check arguments
717 	 */
718 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
719 	if (ret == BAM_ERROR) {
720 		return (BAM_ERROR);
721 	}
722 
723 	assert(bam_root);
724 
725 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
726 	osdev = osroot = NULL;
727 
728 	if (strcmp(subcmd, "update_entry") == 0) {
729 		assert(opt);
730 
731 		osdev = strtok(opt, ",");
732 		assert(osdev);
733 		osroot = strtok(NULL, ",");
734 		if (osroot) {
735 			/* fixup bam_root so that it points at osroot */
736 			if (realpath(osroot, rootbuf) == NULL) {
737 				bam_error(CANT_RESOLVE, osroot,
738 				    strerror(errno));
739 				return (BAM_ERROR);
740 			}
741 			bam_alt_root = 1;
742 			bam_root  = rootbuf;
743 			bam_rootlen = strlen(rootbuf);
744 		}
745 	}
746 
747 	/*
748 	 * We support menu on PCFS (under certain conditions), but
749 	 * not the OS root
750 	 */
751 	if (is_pcfs(bam_root)) {
752 		bam_error(PCFS_ROOT_NOTSUP, bam_root);
753 		return (BAM_ERROR);
754 	}
755 
756 	if (stat(menu_root, &sb) == -1) {
757 		bam_error(CANNOT_LOCATE_GRUB_MENU);
758 		return (BAM_ERROR);
759 	}
760 
761 	BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root));
762 
763 	/*
764 	 * We no longer use the GRUB slice file. If it exists, then
765 	 * the user is doing something that is unsupported (such as
766 	 * standard upgrading an old Live Upgrade BE). If that
767 	 * happens, mimic existing behavior i.e. pretend that it is
768 	 * not a BE. Emit a warning though.
769 	 */
770 	if (bam_alt_root) {
771 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
772 		    GRUB_slice);
773 	} else {
774 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
775 	}
776 
777 	if (stat(path, &sb) == 0)
778 		bam_error(GRUB_SLICE_FILE_EXISTS, path);
779 
780 	if (is_zfs(menu_root)) {
781 		assert(strcmp(menu_root, bam_root) == 0);
782 		special = get_special(menu_root);
783 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
784 		if (special == NULL) {
785 			bam_error(CANT_FIND_SPECIAL, menu_root);
786 			return (BAM_ERROR);
787 		}
788 		pool = strtok(special, "/");
789 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
790 		if (pool == NULL) {
791 			free(special);
792 			bam_error(CANT_FIND_POOL, menu_root);
793 			return (BAM_ERROR);
794 		}
795 		BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool));
796 
797 		zmntpt = mount_top_dataset(pool, &zmnted);
798 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
799 		if (zmntpt == NULL) {
800 			bam_error(CANT_MOUNT_POOL_DATASET, pool);
801 			free(special);
802 			return (BAM_ERROR);
803 		}
804 		BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt));
805 
806 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
807 		BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root));
808 	}
809 
810 	elide_trailing_slash(menu_root, clean_menu_root,
811 	    sizeof (clean_menu_root));
812 
813 	BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root));
814 
815 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
816 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
817 
818 	BAM_DPRINTF((D_MENU_PATH, fcn, menu_path));
819 
820 	/*
821 	 * If listing the menu, display the menu location
822 	 */
823 	if (strcmp(subcmd, "list_entry") == 0) {
824 		bam_print(GRUB_MENU_PATH, menu_path);
825 	}
826 
827 
828 	menu = menu_read(menu_path);
829 	assert(menu);
830 
831 	/*
832 	 * We already checked the following case in
833 	 * check_subcmd_and_suboptions() above. Complete the
834 	 * final step now.
835 	 */
836 	if (strcmp(subcmd, "set_option") == 0) {
837 		assert(largc == 1 && largv[0] && largv[1] == NULL);
838 		opt = largv[0];
839 	} else {
840 		assert(largc == 0 && largv == NULL);
841 	}
842 
843 	ret = get_boot_cap(bam_root);
844 	if (ret != BAM_SUCCESS) {
845 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
846 		goto out;
847 	}
848 
849 	/*
850 	 * Once the sub-cmd handler has run
851 	 * only the line field is guaranteed to have valid values
852 	 */
853 	if (strcmp(subcmd, "update_entry") == 0)
854 		ret = f(menu, menu_root, osdev);
855 	else if (strcmp(subcmd, "upgrade") == 0)
856 		ret = f(menu, bam_root, menu_root);
857 	else if (strcmp(subcmd, "list_entry") == 0)
858 		ret = f(menu, menu_path, opt);
859 	else
860 		ret = f(menu, NULL, opt);
861 
862 	if (ret == BAM_WRITE) {
863 		BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root));
864 		ret = menu_write(clean_menu_root, menu);
865 	}
866 
867 out:
868 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
869 	assert((is_zfs(menu_root)) ^ (pool == NULL));
870 	if (pool) {
871 		(void) umount_top_dataset(pool, zmnted, zmntpt);
872 		free(special);
873 	}
874 	menu_free(menu);
875 	return (ret);
876 }
877 
878 
879 static error_t
880 bam_archive(
881 	char *subcmd,
882 	char *opt)
883 {
884 	error_t			ret;
885 	error_t			(*f)(char *root, char *opt);
886 	const char		*fcn = "bam_archive()";
887 
888 	/*
889 	 * Add trailing / for archive subcommands
890 	 */
891 	if (rootbuf[strlen(rootbuf) - 1] != '/')
892 		(void) strcat(rootbuf, "/");
893 	bam_rootlen = strlen(rootbuf);
894 
895 	/*
896 	 * Check arguments
897 	 */
898 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
899 	if (ret != BAM_SUCCESS) {
900 		return (BAM_ERROR);
901 	}
902 
903 	ret = get_boot_cap(rootbuf);
904 	if (ret != BAM_SUCCESS) {
905 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
906 		return (ret);
907 	}
908 
909 	/*
910 	 * Check archive not supported with update_all
911 	 * since it is awkward to display out-of-sync
912 	 * information for each BE.
913 	 */
914 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
915 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
916 		return (BAM_ERROR);
917 	}
918 
919 	if (strcmp(subcmd, "update_all") == 0)
920 		bam_update_all = 1;
921 
922 #if !defined(_OPB)
923 	ucode_install(bam_root);
924 #endif
925 
926 	ret = f(bam_root, opt);
927 
928 	bam_update_all = 0;
929 
930 	return (ret);
931 }
932 
933 /*PRINTFLIKE1*/
934 void
935 bam_error(char *format, ...)
936 {
937 	va_list ap;
938 
939 	va_start(ap, format);
940 	(void) fprintf(stderr, "%s: ", prog);
941 	(void) vfprintf(stderr, format, ap);
942 	va_end(ap);
943 }
944 
945 /*PRINTFLIKE1*/
946 void
947 bam_derror(char *format, ...)
948 {
949 	va_list ap;
950 
951 	assert(bam_debug);
952 
953 	va_start(ap, format);
954 	(void) fprintf(stderr, "DEBUG: ");
955 	(void) vfprintf(stderr, format, ap);
956 	va_end(ap);
957 }
958 
959 /*PRINTFLIKE1*/
960 void
961 bam_print(char *format, ...)
962 {
963 	va_list ap;
964 
965 	va_start(ap, format);
966 	(void) vfprintf(stdout, format, ap);
967 	va_end(ap);
968 }
969 
970 /*PRINTFLIKE1*/
971 void
972 bam_print_stderr(char *format, ...)
973 {
974 	va_list ap;
975 
976 	va_start(ap, format);
977 	(void) vfprintf(stderr, format, ap);
978 	va_end(ap);
979 }
980 
981 static void
982 bam_exit(int excode)
983 {
984 	bam_unlock();
985 	exit(excode);
986 }
987 
988 static void
989 bam_lock(void)
990 {
991 	struct flock lock;
992 	pid_t pid;
993 
994 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
995 	if (bam_lock_fd < 0) {
996 		/*
997 		 * We may be invoked early in boot for archive verification.
998 		 * In this case, root is readonly and /var/run may not exist.
999 		 * Proceed without the lock
1000 		 */
1001 		if (errno == EROFS || errno == ENOENT) {
1002 			bam_root_readonly = 1;
1003 			return;
1004 		}
1005 
1006 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
1007 		bam_exit(1);
1008 	}
1009 
1010 	lock.l_type = F_WRLCK;
1011 	lock.l_whence = SEEK_SET;
1012 	lock.l_start = 0;
1013 	lock.l_len = 0;
1014 
1015 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
1016 		if (errno != EACCES && errno != EAGAIN) {
1017 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
1018 			(void) close(bam_lock_fd);
1019 			bam_lock_fd = -1;
1020 			bam_exit(1);
1021 		}
1022 		pid = 0;
1023 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
1024 		bam_print(FILE_LOCKED, pid);
1025 
1026 		lock.l_type = F_WRLCK;
1027 		lock.l_whence = SEEK_SET;
1028 		lock.l_start = 0;
1029 		lock.l_len = 0;
1030 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
1031 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
1032 			(void) close(bam_lock_fd);
1033 			bam_lock_fd = -1;
1034 			bam_exit(1);
1035 		}
1036 	}
1037 
1038 	/* We own the lock now */
1039 	pid = getpid();
1040 	(void) write(bam_lock_fd, &pid, sizeof (pid));
1041 }
1042 
1043 static void
1044 bam_unlock(void)
1045 {
1046 	struct flock unlock;
1047 
1048 	/*
1049 	 * NOP if we don't hold the lock
1050 	 */
1051 	if (bam_lock_fd < 0) {
1052 		return;
1053 	}
1054 
1055 	unlock.l_type = F_UNLCK;
1056 	unlock.l_whence = SEEK_SET;
1057 	unlock.l_start = 0;
1058 	unlock.l_len = 0;
1059 
1060 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
1061 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
1062 	}
1063 
1064 	if (close(bam_lock_fd) == -1) {
1065 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
1066 	}
1067 	bam_lock_fd = -1;
1068 }
1069 
1070 static error_t
1071 list_archive(char *root, char *opt)
1072 {
1073 	filelist_t flist;
1074 	filelist_t *flistp = &flist;
1075 	line_t *lp;
1076 
1077 	assert(root);
1078 	assert(opt == NULL);
1079 
1080 	flistp->head = flistp->tail = NULL;
1081 	if (read_list(root, flistp) != BAM_SUCCESS) {
1082 		return (BAM_ERROR);
1083 	}
1084 	assert(flistp->head && flistp->tail);
1085 
1086 	for (lp = flistp->head; lp; lp = lp->next) {
1087 		bam_print(PRINT, lp->line);
1088 	}
1089 
1090 	filelist_free(flistp);
1091 
1092 	return (BAM_SUCCESS);
1093 }
1094 
1095 /*
1096  * This routine writes a list of lines to a file.
1097  * The list is *not* freed
1098  */
1099 static error_t
1100 list2file(char *root, char *tmp, char *final, line_t *start)
1101 {
1102 	char		tmpfile[PATH_MAX];
1103 	char		path[PATH_MAX];
1104 	FILE		*fp;
1105 	int		ret;
1106 	struct stat	sb;
1107 	mode_t		mode;
1108 	uid_t		root_uid;
1109 	gid_t		sys_gid;
1110 	struct passwd	*pw;
1111 	struct group	*gp;
1112 	const char	*fcn = "list2file()";
1113 
1114 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
1115 
1116 	if (start == NULL) {
1117 		/* Empty GRUB menu */
1118 		if (stat(path, &sb) != -1) {
1119 			bam_print(UNLINK_EMPTY, path);
1120 			if (unlink(path) != 0) {
1121 				bam_error(UNLINK_FAIL, path, strerror(errno));
1122 				return (BAM_ERROR);
1123 			} else {
1124 				return (BAM_SUCCESS);
1125 			}
1126 		}
1127 		return (BAM_SUCCESS);
1128 	}
1129 
1130 	/*
1131 	 * Preserve attributes of existing file if possible,
1132 	 * otherwise ask the system for uid/gid of root/sys.
1133 	 * If all fails, fall back on hard-coded defaults.
1134 	 */
1135 	if (stat(path, &sb) != -1) {
1136 		mode = sb.st_mode;
1137 		root_uid = sb.st_uid;
1138 		sys_gid = sb.st_gid;
1139 	} else {
1140 		mode = DEFAULT_DEV_MODE;
1141 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
1142 			root_uid = pw->pw_uid;
1143 		} else {
1144 			bam_error(CANT_FIND_USER,
1145 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
1146 			root_uid = (uid_t)DEFAULT_DEV_UID;
1147 		}
1148 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
1149 			sys_gid = gp->gr_gid;
1150 		} else {
1151 			bam_error(CANT_FIND_GROUP,
1152 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
1153 			sys_gid = (gid_t)DEFAULT_DEV_GID;
1154 		}
1155 	}
1156 
1157 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
1158 
1159 	/* Truncate tmpfile first */
1160 	fp = fopen(tmpfile, "w");
1161 	if (fp == NULL) {
1162 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
1163 		return (BAM_ERROR);
1164 	}
1165 	ret = fclose(fp);
1166 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1167 	if (ret == EOF) {
1168 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
1169 		return (BAM_ERROR);
1170 	}
1171 
1172 	/* Now open it in append mode */
1173 	fp = fopen(tmpfile, "a");
1174 	if (fp == NULL) {
1175 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
1176 		return (BAM_ERROR);
1177 	}
1178 
1179 	for (; start; start = start->next) {
1180 		ret = s_fputs(start->line, fp);
1181 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1182 		if (ret == EOF) {
1183 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
1184 			(void) fclose(fp);
1185 			return (BAM_ERROR);
1186 		}
1187 	}
1188 
1189 	ret = fclose(fp);
1190 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1191 	if (ret == EOF) {
1192 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
1193 		return (BAM_ERROR);
1194 	}
1195 
1196 	/*
1197 	 * Set up desired attributes.  Ignore failures on filesystems
1198 	 * not supporting these operations - pcfs reports unsupported
1199 	 * operations as EINVAL.
1200 	 */
1201 	ret = chmod(tmpfile, mode);
1202 	if (ret == -1 &&
1203 	    errno != EINVAL && errno != ENOTSUP) {
1204 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
1205 		return (BAM_ERROR);
1206 	}
1207 
1208 	ret = chown(tmpfile, root_uid, sys_gid);
1209 	if (ret == -1 &&
1210 	    errno != EINVAL && errno != ENOTSUP) {
1211 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
1212 		return (BAM_ERROR);
1213 	}
1214 
1215 
1216 	/*
1217 	 * Do an atomic rename
1218 	 */
1219 	ret = rename(tmpfile, path);
1220 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
1221 	if (ret != 0) {
1222 		bam_error(RENAME_FAIL, path, strerror(errno));
1223 		return (BAM_ERROR);
1224 	}
1225 
1226 	BAM_DPRINTF((D_WROTE_FILE, fcn, path));
1227 	return (BAM_SUCCESS);
1228 }
1229 
1230 /*
1231  * This function should always return 0 - since we want
1232  * to create stat data for *all* files in the list.
1233  */
1234 /*ARGSUSED*/
1235 static int
1236 cmpstat(
1237 	const char *file,
1238 	const struct stat *stat,
1239 	int flags,
1240 	struct FTW *ftw)
1241 {
1242 	uint_t sz;
1243 	uint64_t *value;
1244 	uint64_t filestat[2];
1245 	int error;
1246 
1247 	struct safefile *safefilep;
1248 	FILE *fp;
1249 
1250 	/*
1251 	 * We only want regular files
1252 	 */
1253 	if (!S_ISREG(stat->st_mode))
1254 		return (0);
1255 
1256 	/*
1257 	 * new_nvlp may be NULL if there were errors earlier
1258 	 * but this is not fatal to update determination.
1259 	 */
1260 	if (walk_arg.new_nvlp) {
1261 		filestat[0] = stat->st_size;
1262 		filestat[1] = stat->st_mtime;
1263 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
1264 		    file + bam_rootlen, filestat, 2);
1265 		if (error)
1266 			bam_error(NVADD_FAIL, file, strerror(error));
1267 	}
1268 
1269 	/*
1270 	 * The remaining steps are only required if we haven't made a
1271 	 * decision about update or if we are checking (-n)
1272 	 */
1273 	if (walk_arg.need_update && !bam_check)
1274 		return (0);
1275 
1276 	/*
1277 	 * If we are invoked as part of system/filesystem/boot-archive, then
1278 	 * there are a number of things we should not worry about
1279 	 */
1280 	if (bam_smf_check) {
1281 		/* ignore amd64 modules unless we are booted amd64. */
1282 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
1283 			return (0);
1284 
1285 		/* read in list of safe files */
1286 		if (safefiles == NULL)
1287 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
1288 				safefiles = s_calloc(1,
1289 				    sizeof (struct safefile));
1290 				safefilep = safefiles;
1291 				safefilep->name = s_calloc(1, MAXPATHLEN +
1292 				    MAXNAMELEN);
1293 				safefilep->next = NULL;
1294 				while (s_fgets(safefilep->name, MAXPATHLEN +
1295 				    MAXNAMELEN, fp) != NULL) {
1296 					safefilep->next = s_calloc(1,
1297 					    sizeof (struct safefile));
1298 					safefilep = safefilep->next;
1299 					safefilep->name = s_calloc(1,
1300 					    MAXPATHLEN + MAXNAMELEN);
1301 					safefilep->next = NULL;
1302 				}
1303 				(void) fclose(fp);
1304 			}
1305 	}
1306 
1307 	/*
1308 	 * We need an update if file doesn't exist in old archive
1309 	 */
1310 	if (walk_arg.old_nvlp == NULL ||
1311 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
1312 	    file + bam_rootlen, &value, &sz) != 0) {
1313 		if (bam_smf_check)	/* ignore new during smf check */
1314 			return (0);
1315 		walk_arg.need_update = 1;
1316 		if (bam_verbose)
1317 			bam_print(PARSEABLE_NEW_FILE, file);
1318 		return (0);
1319 	}
1320 
1321 	/*
1322 	 * File exists in old archive. Check if file has changed
1323 	 */
1324 	assert(sz == 2);
1325 	bcopy(value, filestat, sizeof (filestat));
1326 
1327 	if (filestat[0] != stat->st_size ||
1328 	    filestat[1] != stat->st_mtime) {
1329 		if (bam_smf_check) {
1330 			safefilep = safefiles;
1331 			while (safefilep != NULL) {
1332 				if (strcmp(file + bam_rootlen,
1333 				    safefilep->name) == 0) {
1334 					(void) creat(NEED_UPDATE_FILE, 0644);
1335 					return (0);
1336 				}
1337 				safefilep = safefilep->next;
1338 			}
1339 		}
1340 		walk_arg.need_update = 1;
1341 		if (bam_verbose)
1342 			if (bam_smf_check)
1343 				bam_print("    %s\n", file);
1344 			else
1345 				bam_print(PARSEABLE_OUT_DATE, file);
1346 	}
1347 
1348 	return (0);
1349 }
1350 
1351 /*
1352  * Check flags and presence of required files.
1353  * The force flag and/or absence of files should
1354  * trigger an update.
1355  * Suppress stdout output if check (-n) option is set
1356  * (as -n should only produce parseable output.)
1357  */
1358 static void
1359 check_flags_and_files(char *root)
1360 {
1361 	char path[PATH_MAX];
1362 	struct stat sb;
1363 
1364 	/*
1365 	 * if force, create archive unconditionally
1366 	 */
1367 	if (bam_force) {
1368 		walk_arg.need_update = 1;
1369 		if (bam_verbose && !bam_check)
1370 			bam_print(UPDATE_FORCE);
1371 		return;
1372 	}
1373 
1374 	/*
1375 	 * If archive is missing, create archive
1376 	 */
1377 	if (is_sparc()) {
1378 		(void) snprintf(path, sizeof (path), "%s%s%s%s", root,
1379 		    ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
1380 	} else {
1381 		if (bam_direct == BAM_DIRECT_DBOOT) {
1382 			(void) snprintf(path, sizeof (path), "%s%s", root,
1383 			    DIRECT_BOOT_ARCHIVE_64);
1384 			if (stat(path, &sb) != 0) {
1385 				if (bam_verbose && !bam_check)
1386 					bam_print(UPDATE_ARCH_MISS, path);
1387 				walk_arg.need_update = 1;
1388 				return;
1389 			}
1390 		}
1391 		(void) snprintf(path, sizeof (path), "%s%s", root,
1392 		    DIRECT_BOOT_ARCHIVE_32);
1393 	}
1394 
1395 	if (stat(path, &sb) != 0) {
1396 		if (bam_verbose && !bam_check)
1397 			bam_print(UPDATE_ARCH_MISS, path);
1398 		walk_arg.need_update = 1;
1399 		return;
1400 	}
1401 }
1402 
1403 static error_t
1404 read_one_list(char *root, filelist_t  *flistp, char *filelist)
1405 {
1406 	char path[PATH_MAX];
1407 	FILE *fp;
1408 	char buf[BAM_MAXLINE];
1409 	const char *fcn = "read_one_list()";
1410 
1411 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
1412 
1413 	fp = fopen(path, "r");
1414 	if (fp == NULL) {
1415 		BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
1416 		return (BAM_ERROR);
1417 	}
1418 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1419 		/* skip blank lines */
1420 		if (strspn(buf, " \t") == strlen(buf))
1421 			continue;
1422 		append_to_flist(flistp, buf);
1423 	}
1424 	if (fclose(fp) != 0) {
1425 		bam_error(CLOSE_FAIL, path, strerror(errno));
1426 		return (BAM_ERROR);
1427 	}
1428 	return (BAM_SUCCESS);
1429 }
1430 
1431 static error_t
1432 read_list(char *root, filelist_t  *flistp)
1433 {
1434 	char path[PATH_MAX];
1435 	char cmd[PATH_MAX];
1436 	struct stat sb;
1437 	int n, rval;
1438 	const char *fcn = "read_list()";
1439 
1440 	flistp->head = flistp->tail = NULL;
1441 
1442 	/*
1443 	 * build and check path to extract_boot_filelist.ksh
1444 	 */
1445 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
1446 	if (n >= sizeof (path)) {
1447 		bam_error(NO_FLIST);
1448 		return (BAM_ERROR);
1449 	}
1450 
1451 	/*
1452 	 * If extract_boot_filelist is present, exec it, otherwise read
1453 	 * the filelists directly, for compatibility with older images.
1454 	 */
1455 	if (stat(path, &sb) == 0) {
1456 		/*
1457 		 * build arguments to exec extract_boot_filelist.ksh
1458 		 */
1459 		char *rootarg, *platarg;
1460 		int platarglen = 1, rootarglen = 1;
1461 		if (strlen(root) > 1)
1462 			rootarglen += strlen(root) + strlen("-R ");
1463 		if (bam_alt_platform)
1464 			platarglen += strlen(bam_platform) + strlen("-p ");
1465 		platarg = s_calloc(1, platarglen);
1466 		rootarg = s_calloc(1, rootarglen);
1467 		*platarg = 0;
1468 		*rootarg = 0;
1469 
1470 		if (strlen(root) > 1) {
1471 			(void) snprintf(rootarg, rootarglen,
1472 			    "-R %s", root);
1473 		}
1474 		if (bam_alt_platform) {
1475 			(void) snprintf(platarg, platarglen,
1476 			    "-p %s", bam_platform);
1477 		}
1478 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
1479 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
1480 		free(platarg);
1481 		free(rootarg);
1482 		if (n >= sizeof (cmd)) {
1483 			bam_error(NO_FLIST);
1484 			return (BAM_ERROR);
1485 		}
1486 		if (exec_cmd(cmd, flistp) != 0) {
1487 			BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
1488 			return (BAM_ERROR);
1489 		}
1490 	} else {
1491 		/*
1492 		 * Read current lists of files - only the first is mandatory
1493 		 */
1494 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
1495 		if (rval != BAM_SUCCESS)
1496 			return (rval);
1497 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
1498 	}
1499 
1500 	if (flistp->head == NULL) {
1501 		bam_error(NO_FLIST);
1502 		return (BAM_ERROR);
1503 	}
1504 
1505 	return (BAM_SUCCESS);
1506 }
1507 
1508 static void
1509 getoldstat(char *root)
1510 {
1511 	char path[PATH_MAX];
1512 	int fd, error;
1513 	struct stat sb;
1514 	char *ostat;
1515 
1516 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
1517 	fd = open(path, O_RDONLY);
1518 	if (fd == -1) {
1519 		if (bam_verbose)
1520 			bam_print(OPEN_FAIL, path, strerror(errno));
1521 		walk_arg.need_update = 1;
1522 		return;
1523 	}
1524 
1525 	if (fstat(fd, &sb) != 0) {
1526 		bam_error(STAT_FAIL, path, strerror(errno));
1527 		(void) close(fd);
1528 		walk_arg.need_update = 1;
1529 		return;
1530 	}
1531 
1532 	ostat = s_calloc(1, sb.st_size);
1533 
1534 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
1535 		bam_error(READ_FAIL, path, strerror(errno));
1536 		(void) close(fd);
1537 		free(ostat);
1538 		walk_arg.need_update = 1;
1539 		return;
1540 	}
1541 
1542 	(void) close(fd);
1543 
1544 	walk_arg.old_nvlp = NULL;
1545 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
1546 
1547 	free(ostat);
1548 
1549 	if (error) {
1550 		bam_error(UNPACK_FAIL, path, strerror(error));
1551 		walk_arg.old_nvlp = NULL;
1552 		walk_arg.need_update = 1;
1553 		return;
1554 	}
1555 }
1556 
1557 /*
1558  * Checks if a file in the current (old) archive has
1559  * been deleted from the root filesystem. This is needed for
1560  * software like Trusted Extensions (TX) that switch early
1561  * in boot based on presence/absence of a kernel module.
1562  */
1563 static void
1564 check4stale(char *root)
1565 {
1566 	nvpair_t	*nvp;
1567 	nvlist_t	*nvlp;
1568 	char 		*file;
1569 	char		path[PATH_MAX];
1570 	struct stat	sb;
1571 
1572 	/*
1573 	 * Skip stale file check during smf check
1574 	 */
1575 	if (bam_smf_check)
1576 		return;
1577 
1578 	/* Nothing to do if no old stats */
1579 	if ((nvlp = walk_arg.old_nvlp) == NULL)
1580 		return;
1581 
1582 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
1583 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
1584 		file = nvpair_name(nvp);
1585 		if (file == NULL)
1586 			continue;
1587 		(void) snprintf(path, sizeof (path), "%s/%s",
1588 		    root, file);
1589 		if (stat(path, &sb) == -1) {
1590 			walk_arg.need_update = 1;
1591 			if (bam_verbose)
1592 				bam_print(PARSEABLE_STALE_FILE, path);
1593 		}
1594 	}
1595 }
1596 
1597 static void
1598 create_newstat(void)
1599 {
1600 	int error;
1601 
1602 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
1603 	if (error) {
1604 		/*
1605 		 * Not fatal - we can still create archive
1606 		 */
1607 		walk_arg.new_nvlp = NULL;
1608 		bam_error(NVALLOC_FAIL, strerror(error));
1609 	}
1610 }
1611 
1612 static void
1613 walk_list(char *root, filelist_t *flistp)
1614 {
1615 	char path[PATH_MAX];
1616 	line_t *lp;
1617 
1618 	for (lp = flistp->head; lp; lp = lp->next) {
1619 		/*
1620 		 * Don't follow symlinks.  A symlink must refer to
1621 		 * a file that would appear in the archive through
1622 		 * a direct reference.  This matches the archive
1623 		 * construction behavior.
1624 		 */
1625 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
1626 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
1627 			/*
1628 			 * Some files may not exist.
1629 			 * For example: etc/rtc_config on a x86 diskless system
1630 			 * Emit verbose message only
1631 			 */
1632 			if (bam_verbose)
1633 				bam_print(NFTW_FAIL, path, strerror(errno));
1634 		}
1635 	}
1636 }
1637 
1638 static void
1639 savenew(char *root)
1640 {
1641 	char path[PATH_MAX];
1642 	char path2[PATH_MAX];
1643 	size_t sz;
1644 	char *nstat;
1645 	int fd, wrote, error;
1646 
1647 	nstat = NULL;
1648 	sz = 0;
1649 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
1650 	    NV_ENCODE_XDR, 0);
1651 	if (error) {
1652 		bam_error(PACK_FAIL, strerror(error));
1653 		return;
1654 	}
1655 
1656 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
1657 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
1658 	if (fd == -1) {
1659 		bam_error(OPEN_FAIL, path, strerror(errno));
1660 		free(nstat);
1661 		return;
1662 	}
1663 	wrote = write(fd, nstat, sz);
1664 	if (wrote != sz) {
1665 		bam_error(WRITE_FAIL, path, strerror(errno));
1666 		(void) close(fd);
1667 		free(nstat);
1668 		return;
1669 	}
1670 	(void) close(fd);
1671 	free(nstat);
1672 
1673 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
1674 	if (rename(path, path2) != 0) {
1675 		bam_error(RENAME_FAIL, path2, strerror(errno));
1676 	}
1677 }
1678 
1679 static void
1680 clear_walk_args(void)
1681 {
1682 	if (walk_arg.old_nvlp)
1683 		nvlist_free(walk_arg.old_nvlp);
1684 	if (walk_arg.new_nvlp)
1685 		nvlist_free(walk_arg.new_nvlp);
1686 	walk_arg.need_update = 0;
1687 	walk_arg.old_nvlp = NULL;
1688 	walk_arg.new_nvlp = NULL;
1689 }
1690 
1691 /*
1692  * Returns:
1693  *	0 - no update necessary
1694  *	1 - update required.
1695  *	BAM_ERROR (-1) - An error occurred
1696  *
1697  * Special handling for check (-n):
1698  * ================================
1699  * The check (-n) option produces parseable output.
1700  * To do this, we suppress all stdout messages unrelated
1701  * to out of sync files.
1702  * All stderr messages are still printed though.
1703  *
1704  */
1705 static int
1706 update_required(char *root)
1707 {
1708 	struct stat sb;
1709 	char path[PATH_MAX];
1710 	filelist_t flist;
1711 	filelist_t *flistp = &flist;
1712 	int need_update;
1713 
1714 	flistp->head = flistp->tail = NULL;
1715 
1716 	walk_arg.need_update = 0;
1717 
1718 	/*
1719 	 * Without consulting stat data, check if we need update
1720 	 */
1721 	check_flags_and_files(root);
1722 
1723 	/*
1724 	 * In certain deployment scenarios, filestat may not
1725 	 * exist. Ignore it during boot-archive SMF check.
1726 	 */
1727 	if (bam_smf_check) {
1728 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
1729 		if (stat(path, &sb) != 0)
1730 			return (0);
1731 	}
1732 
1733 	/*
1734 	 * consult stat data only if we haven't made a decision
1735 	 * about update. If checking (-n) however, we always
1736 	 * need stat data (since we want to compare old and new)
1737 	 */
1738 	if (!walk_arg.need_update || bam_check)
1739 		getoldstat(root);
1740 
1741 	/*
1742 	 * Check if the archive contains files that are no longer
1743 	 * present on the root filesystem.
1744 	 */
1745 	if (!walk_arg.need_update || bam_check)
1746 		check4stale(root);
1747 
1748 	/*
1749 	 * read list of files
1750 	 */
1751 	if (read_list(root, flistp) != BAM_SUCCESS) {
1752 		clear_walk_args();
1753 		return (BAM_ERROR);
1754 	}
1755 
1756 	assert(flistp->head && flistp->tail);
1757 
1758 	/*
1759 	 * At this point either the update is required
1760 	 * or the decision is pending. In either case
1761 	 * we need to create new stat nvlist
1762 	 */
1763 	create_newstat();
1764 
1765 	/*
1766 	 * This walk does 2 things:
1767 	 *  	- gets new stat data for every file
1768 	 *	- (optional) compare old and new stat data
1769 	 */
1770 	walk_list(root, &flist);
1771 
1772 	/* done with the file list */
1773 	filelist_free(flistp);
1774 
1775 	/*
1776 	 * if we didn't succeed in  creating new stat data above
1777 	 * just return result of update check so that archive is built.
1778 	 */
1779 	if (walk_arg.new_nvlp == NULL) {
1780 		bam_error(NO_NEW_STAT);
1781 		need_update = walk_arg.need_update;
1782 		clear_walk_args();
1783 		return (need_update ? 1 : 0);
1784 	}
1785 
1786 
1787 	/*
1788 	 * If no update required, discard newstat
1789 	 */
1790 	if (!walk_arg.need_update) {
1791 		clear_walk_args();
1792 		return (0);
1793 	}
1794 
1795 	return (1);
1796 }
1797 
1798 static error_t
1799 create_ramdisk(char *root)
1800 {
1801 	char *cmdline, path[PATH_MAX];
1802 	size_t len;
1803 	struct stat sb;
1804 
1805 	/*
1806 	 * Setup command args for create_ramdisk.ksh
1807 	 */
1808 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
1809 	if (stat(path, &sb) != 0) {
1810 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
1811 		return (BAM_ERROR);
1812 	}
1813 
1814 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
1815 	if (bam_alt_platform)
1816 		len += strlen(bam_platform) + strlen("-p ");
1817 	cmdline = s_calloc(1, len);
1818 
1819 	if (bam_alt_platform) {
1820 		assert(strlen(root) > 1);
1821 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
1822 		    path, bam_platform, root);
1823 		/* chop off / at the end */
1824 		cmdline[strlen(cmdline) - 1] = '\0';
1825 	} else if (strlen(root) > 1) {
1826 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
1827 		/* chop off / at the end */
1828 		cmdline[strlen(cmdline) - 1] = '\0';
1829 	} else
1830 		(void) snprintf(cmdline, len, "%s", path);
1831 
1832 	if (exec_cmd(cmdline, NULL) != 0) {
1833 		bam_error(ARCHIVE_FAIL, cmdline);
1834 		free(cmdline);
1835 		return (BAM_ERROR);
1836 	}
1837 	free(cmdline);
1838 
1839 	/*
1840 	 * The existence of the expected archives used to be
1841 	 * verified here. This check is done in create_ramdisk as
1842 	 * it needs to be in sync with the altroot operated upon.
1843 	 */
1844 
1845 	return (BAM_SUCCESS);
1846 }
1847 
1848 /*
1849  * Checks if target filesystem is on a ramdisk
1850  * 1 - is miniroot
1851  * 0 - is not
1852  * When in doubt assume it is not a ramdisk.
1853  */
1854 static int
1855 is_ramdisk(char *root)
1856 {
1857 	struct extmnttab mnt;
1858 	FILE *fp;
1859 	int found;
1860 	char mntpt[PATH_MAX];
1861 	char *cp;
1862 
1863 	/*
1864 	 * There are 3 situations where creating archive is
1865 	 * of dubious value:
1866 	 *	- create boot_archive on a lofi-mounted boot_archive
1867 	 *	- create it on a ramdisk which is the root filesystem
1868 	 *	- create it on a ramdisk mounted somewhere else
1869 	 * The first is not easy to detect and checking for it is not
1870 	 * worth it.
1871 	 * The other two conditions are handled here
1872 	 */
1873 
1874 	fp = fopen(MNTTAB, "r");
1875 	if (fp == NULL) {
1876 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
1877 		return (0);
1878 	}
1879 
1880 	resetmnttab(fp);
1881 
1882 	/*
1883 	 * Remove any trailing / from the mount point
1884 	 */
1885 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1886 	if (strcmp(root, "/") != 0) {
1887 		cp = mntpt + strlen(mntpt) - 1;
1888 		if (*cp == '/')
1889 			*cp = '\0';
1890 	}
1891 	found = 0;
1892 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1893 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
1894 			found = 1;
1895 			break;
1896 		}
1897 	}
1898 
1899 	if (!found) {
1900 		if (bam_verbose)
1901 			bam_error(NOT_IN_MNTTAB, mntpt);
1902 		(void) fclose(fp);
1903 		return (0);
1904 	}
1905 
1906 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
1907 		if (bam_verbose)
1908 			bam_error(IS_RAMDISK, bam_root);
1909 		(void) fclose(fp);
1910 		return (1);
1911 	}
1912 
1913 	(void) fclose(fp);
1914 
1915 	return (0);
1916 }
1917 
1918 static int
1919 is_boot_archive(char *root)
1920 {
1921 	char		path[PATH_MAX];
1922 	struct stat	sb;
1923 	int		error;
1924 	const char	*fcn = "is_boot_archive()";
1925 
1926 	/*
1927 	 * We can't create an archive without the create_ramdisk script
1928 	 */
1929 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
1930 	error = stat(path, &sb);
1931 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
1932 	if (error == -1) {
1933 		if (bam_verbose)
1934 			bam_print(FILE_MISS, path);
1935 		BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root));
1936 		return (0);
1937 	}
1938 
1939 	BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root));
1940 	return (1);
1941 }
1942 
1943 /*
1944  * Need to call this for anything that operates on the GRUB menu
1945  * In the x86 live upgrade case the directory /boot/grub may be present
1946  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
1947  * is to check for the presence of the stage2 binary which is present
1948  * only on GRUB targets (even on x86 boot partitions). Checking for the
1949  * presence of the multiboot binary is not correct as it is not present
1950  * on x86 boot partitions.
1951  */
1952 int
1953 is_grub(const char *root)
1954 {
1955 	char path[PATH_MAX];
1956 	struct stat sb;
1957 	const char *fcn = "is_grub()";
1958 
1959 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
1960 	if (stat(path, &sb) == -1) {
1961 		BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path));
1962 		return (0);
1963 	}
1964 
1965 	return (1);
1966 }
1967 
1968 static int
1969 is_zfs(char *root)
1970 {
1971 	struct statvfs		vfs;
1972 	int			ret;
1973 	const char		*fcn = "is_zfs()";
1974 
1975 	ret = statvfs(root, &vfs);
1976 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
1977 	if (ret != 0) {
1978 		bam_error(STATVFS_FAIL, root, strerror(errno));
1979 		return (0);
1980 	}
1981 
1982 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
1983 		BAM_DPRINTF((D_IS_ZFS, fcn, root));
1984 		return (1);
1985 	} else {
1986 		BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root));
1987 		return (0);
1988 	}
1989 }
1990 
1991 static int
1992 is_ufs(char *root)
1993 {
1994 	struct statvfs		vfs;
1995 	int			ret;
1996 	const char		*fcn = "is_ufs()";
1997 
1998 	ret = statvfs(root, &vfs);
1999 	INJECT_ERROR1("STATVFS_UFS", ret = 1);
2000 	if (ret != 0) {
2001 		bam_error(STATVFS_FAIL, root, strerror(errno));
2002 		return (0);
2003 	}
2004 
2005 	if (strncmp(vfs.f_basetype, "ufs", strlen("ufs")) == 0) {
2006 		BAM_DPRINTF((D_IS_UFS, fcn, root));
2007 		return (1);
2008 	} else {
2009 		BAM_DPRINTF((D_IS_NOT_UFS, fcn, root));
2010 		return (0);
2011 	}
2012 }
2013 
2014 static int
2015 is_pcfs(char *root)
2016 {
2017 	struct statvfs		vfs;
2018 	int			ret;
2019 	const char		*fcn = "is_pcfs()";
2020 
2021 	ret = statvfs(root, &vfs);
2022 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
2023 	if (ret != 0) {
2024 		bam_error(STATVFS_FAIL, root, strerror(errno));
2025 		return (0);
2026 	}
2027 
2028 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
2029 		BAM_DPRINTF((D_IS_PCFS, fcn, root));
2030 		return (1);
2031 	} else {
2032 		BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root));
2033 		return (0);
2034 	}
2035 }
2036 
2037 static int
2038 is_readonly(char *root)
2039 {
2040 	int		fd;
2041 	int		error;
2042 	char		testfile[PATH_MAX];
2043 	const char	*fcn = "is_readonly()";
2044 
2045 	/*
2046 	 * Using statvfs() to check for a read-only filesystem is not
2047 	 * reliable. The only way to reliably test is to attempt to
2048 	 * create a file
2049 	 */
2050 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
2051 	    root, BOOTADM_RDONLY_TEST, getpid());
2052 
2053 	(void) unlink(testfile);
2054 
2055 	errno = 0;
2056 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
2057 	error = errno;
2058 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
2059 	if (fd == -1 && error == EROFS) {
2060 		BAM_DPRINTF((D_RDONLY_FS, fcn, root));
2061 		return (1);
2062 	} else if (fd == -1) {
2063 		bam_error(RDONLY_TEST_ERROR, root, strerror(error));
2064 	}
2065 
2066 	(void) close(fd);
2067 	(void) unlink(testfile);
2068 
2069 	BAM_DPRINTF((D_RDWR_FS, fcn, root));
2070 	return (0);
2071 }
2072 
2073 static error_t
2074 update_archive(char *root, char *opt)
2075 {
2076 	error_t ret;
2077 
2078 	assert(root);
2079 	assert(opt == NULL);
2080 
2081 	/*
2082 	 * root must belong to a boot archive based OS,
2083 	 */
2084 	if (!is_boot_archive(root)) {
2085 		/*
2086 		 * Emit message only if not in context of update_all.
2087 		 * If in update_all, emit only if verbose flag is set.
2088 		 */
2089 		if (!bam_update_all || bam_verbose)
2090 			bam_print(NOT_ARCHIVE_BOOT, root);
2091 		return (BAM_SUCCESS);
2092 	}
2093 
2094 	/*
2095 	 * If smf check is requested when / is writable (can happen
2096 	 * on first reboot following an upgrade because service
2097 	 * dependency is messed up), skip the check.
2098 	 */
2099 	if (bam_smf_check && !bam_root_readonly)
2100 		return (BAM_SUCCESS);
2101 
2102 	/*
2103 	 * root must be writable. This check applies to alternate
2104 	 * root (-R option); bam_root_readonly applies to '/' only.
2105 	 */
2106 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
2107 		if (bam_verbose)
2108 			bam_print(RDONLY_FS, root);
2109 		return (BAM_SUCCESS);
2110 	}
2111 
2112 	/*
2113 	 * Don't generate archive on ramdisk
2114 	 */
2115 	if (is_ramdisk(root)) {
2116 		if (bam_verbose)
2117 			bam_print(SKIP_RAMDISK);
2118 		return (BAM_SUCCESS);
2119 	}
2120 
2121 	/*
2122 	 * Now check if updated is really needed
2123 	 */
2124 	ret = update_required(root);
2125 
2126 	/*
2127 	 * The check command (-n) is *not* a dry run
2128 	 * It only checks if the archive is in sync.
2129 	 */
2130 	if (bam_check) {
2131 		bam_exit((ret != 0) ? 1 : 0);
2132 	}
2133 
2134 	if (ret == 1) {
2135 		/* create the ramdisk */
2136 		ret = create_ramdisk(root);
2137 	}
2138 
2139 	/* if the archive is updated, save the new stat data */
2140 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
2141 		savenew(root);
2142 	}
2143 
2144 	clear_walk_args();
2145 
2146 	return (ret);
2147 }
2148 
2149 static error_t
2150 synchronize_BE_menu(void)
2151 {
2152 	struct stat	sb;
2153 	char		cmdline[PATH_MAX];
2154 	char		cksum_line[PATH_MAX];
2155 	filelist_t	flist = {0};
2156 	char		*old_cksum_str;
2157 	char		*old_size_str;
2158 	char		*old_file;
2159 	char		*curr_cksum_str;
2160 	char		*curr_size_str;
2161 	char		*curr_file;
2162 	FILE		*cfp;
2163 	int		found;
2164 	int		ret;
2165 	const char	*fcn = "synchronize_BE_menu()";
2166 
2167 	BAM_DPRINTF((D_FUNC_ENTRY0, fcn));
2168 
2169 	/* Check if findroot enabled LU BE */
2170 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
2171 		BAM_DPRINTF((D_NOT_LU_BE, fcn));
2172 		return (BAM_SUCCESS);
2173 	}
2174 
2175 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
2176 		BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM));
2177 		goto menu_sync;
2178 	}
2179 
2180 	cfp = fopen(LU_MENU_CKSUM, "r");
2181 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
2182 	if (cfp == NULL) {
2183 		bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM);
2184 		goto menu_sync;
2185 	}
2186 	BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM));
2187 
2188 	found = 0;
2189 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
2190 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
2191 		if (found) {
2192 			bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM);
2193 			(void) fclose(cfp);
2194 			goto menu_sync;
2195 		}
2196 		found = 1;
2197 	}
2198 	BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM));
2199 
2200 
2201 	old_cksum_str = strtok(cksum_line, " \t");
2202 	old_size_str = strtok(NULL, " \t");
2203 	old_file = strtok(NULL, " \t");
2204 
2205 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
2206 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
2207 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
2208 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
2209 		bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM);
2210 		goto menu_sync;
2211 	}
2212 	BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM));
2213 
2214 	/* Get checksum of current menu */
2215 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
2216 	    CKSUM, GRUB_MENU);
2217 	ret = exec_cmd(cmdline, &flist);
2218 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
2219 	if (ret != 0) {
2220 		bam_error(MENU_CKSUM_FAIL);
2221 		return (BAM_ERROR);
2222 	}
2223 	BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn));
2224 
2225 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
2226 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
2227 		bam_error(BAD_CKSUM);
2228 		filelist_free(&flist);
2229 		return (BAM_ERROR);
2230 	}
2231 	BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn));
2232 
2233 	curr_cksum_str = strtok(flist.head->line, " \t");
2234 	curr_size_str = strtok(NULL, " \t");
2235 	curr_file = strtok(NULL, " \t");
2236 
2237 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
2238 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
2239 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
2240 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
2241 	    curr_file == NULL) {
2242 		bam_error(BAD_CKSUM_PARSE);
2243 		filelist_free(&flist);
2244 		return (BAM_ERROR);
2245 	}
2246 	BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn));
2247 
2248 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
2249 	    strcmp(old_size_str, curr_size_str) == 0 &&
2250 	    strcmp(old_file, curr_file) == 0) {
2251 		filelist_free(&flist);
2252 		BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn));
2253 		return (BAM_SUCCESS);
2254 	}
2255 
2256 	filelist_free(&flist);
2257 
2258 	/* cksum doesn't match - the menu has changed */
2259 	BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn));
2260 
2261 menu_sync:
2262 	bam_print(PROP_GRUB_MENU);
2263 
2264 	(void) snprintf(cmdline, sizeof (cmdline),
2265 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
2266 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
2267 	ret = exec_cmd(cmdline, NULL);
2268 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
2269 	if (ret != 0) {
2270 		bam_error(MENU_PROP_FAIL);
2271 		return (BAM_ERROR);
2272 	}
2273 	BAM_DPRINTF((D_PROPAGATED_MENU, fcn));
2274 
2275 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
2276 	    GRUB_MENU, GRUB_BACKUP_MENU);
2277 	ret = exec_cmd(cmdline, NULL);
2278 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
2279 	if (ret != 0) {
2280 		bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU);
2281 		return (BAM_ERROR);
2282 	}
2283 	BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU));
2284 
2285 	(void) snprintf(cmdline, sizeof (cmdline),
2286 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
2287 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
2288 	ret = exec_cmd(cmdline, NULL);
2289 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
2290 	if (ret != 0) {
2291 		bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU);
2292 		return (BAM_ERROR);
2293 	}
2294 	BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU));
2295 
2296 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
2297 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
2298 	ret = exec_cmd(cmdline, NULL);
2299 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
2300 	if (ret != 0) {
2301 		bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM);
2302 		return (BAM_ERROR);
2303 	}
2304 	BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
2305 
2306 	(void) snprintf(cmdline, sizeof (cmdline),
2307 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
2308 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
2309 	ret = exec_cmd(cmdline, NULL);
2310 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
2311 	if (ret != 0) {
2312 		bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM);
2313 		return (BAM_ERROR);
2314 	}
2315 	BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
2316 
2317 	(void) snprintf(cmdline, sizeof (cmdline),
2318 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
2319 	    LULIB, LULIB_PROPAGATE_FILE, BOOTADM);
2320 	ret = exec_cmd(cmdline, NULL);
2321 	INJECT_ERROR1("PROPAGATE_BOOTADM_FILE", ret = 1);
2322 	if (ret != 0) {
2323 		bam_error(BOOTADM_PROP_FAIL, BOOTADM);
2324 		return (BAM_ERROR);
2325 	}
2326 	BAM_DPRINTF((D_PROPAGATED_BOOTADM, fcn, BOOTADM));
2327 
2328 	return (BAM_SUCCESS);
2329 }
2330 
2331 static error_t
2332 update_all(char *root, char *opt)
2333 {
2334 	struct extmnttab mnt;
2335 	struct stat sb;
2336 	FILE *fp;
2337 	char multibt[PATH_MAX];
2338 	char creatram[PATH_MAX];
2339 	error_t ret = BAM_SUCCESS;
2340 
2341 	assert(root);
2342 	assert(opt == NULL);
2343 
2344 	if (bam_rootlen != 1 || *root != '/') {
2345 		elide_trailing_slash(root, multibt, sizeof (multibt));
2346 		bam_error(ALT_ROOT_INVALID, multibt);
2347 		return (BAM_ERROR);
2348 	}
2349 
2350 	/*
2351 	 * Check to see if we are in the midst of safemode patching
2352 	 * If so skip building the archive for /. Instead build it
2353 	 * against the latest bits obtained by creating a fresh lofs
2354 	 * mount of root.
2355 	 */
2356 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
2357 		if (mkdir(LOFS_PATCH_MNT, 0755) == -1 &&
2358 		    errno != EEXIST) {
2359 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
2360 			    strerror(errno));
2361 			ret = BAM_ERROR;
2362 			goto out;
2363 		}
2364 		(void) snprintf(multibt, sizeof (multibt),
2365 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
2366 		if (exec_cmd(multibt, NULL) != 0) {
2367 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
2368 			ret = BAM_ERROR;
2369 		}
2370 		if (ret != BAM_ERROR) {
2371 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
2372 			    LOFS_PATCH_MNT);
2373 			bam_rootlen = strlen(rootbuf);
2374 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
2375 				ret = BAM_ERROR;
2376 			/*
2377 			 * unmount the lofs mount since there could be
2378 			 * multiple invocations of bootadm -a update_all
2379 			 */
2380 			(void) snprintf(multibt, sizeof (multibt),
2381 			    "/sbin/umount %s", LOFS_PATCH_MNT);
2382 			if (exec_cmd(multibt, NULL) != 0) {
2383 				bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT);
2384 				ret = BAM_ERROR;
2385 			}
2386 		}
2387 	} else {
2388 		/*
2389 		 * First update archive for current root
2390 		 */
2391 		if (update_archive(root, opt) != BAM_SUCCESS)
2392 			ret = BAM_ERROR;
2393 	}
2394 
2395 	if (ret == BAM_ERROR)
2396 		goto out;
2397 
2398 	/*
2399 	 * Now walk the mount table, performing archive update
2400 	 * for all mounted Newboot root filesystems
2401 	 */
2402 	fp = fopen(MNTTAB, "r");
2403 	if (fp == NULL) {
2404 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2405 		ret = BAM_ERROR;
2406 		goto out;
2407 	}
2408 
2409 	resetmnttab(fp);
2410 
2411 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
2412 		if (mnt.mnt_special == NULL)
2413 			continue;
2414 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
2415 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
2416 			continue;
2417 		if (strcmp(mnt.mnt_mountp, "/") == 0)
2418 			continue;
2419 
2420 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
2421 		    mnt.mnt_mountp, CREATE_RAMDISK);
2422 
2423 		if (stat(creatram, &sb) == -1)
2424 			continue;
2425 
2426 		/*
2427 		 * We put a trailing slash to be consistent with root = "/"
2428 		 * case, such that we don't have to print // in some cases.
2429 		 */
2430 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
2431 		    mnt.mnt_mountp);
2432 		bam_rootlen = strlen(rootbuf);
2433 
2434 		/*
2435 		 * It's possible that other mounts may be an alternate boot
2436 		 * architecture, so check it again.
2437 		 */
2438 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
2439 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
2440 			ret = BAM_ERROR;
2441 	}
2442 
2443 	(void) fclose(fp);
2444 
2445 out:
2446 	/*
2447 	 * We no longer use biosdev for Live Upgrade. Hence
2448 	 * there is no need to defer (to shutdown time) any fdisk
2449 	 * updates
2450 	 */
2451 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
2452 		bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target);
2453 	}
2454 
2455 	/*
2456 	 * If user has updated menu in current BE, propagate the
2457 	 * updates to all BEs.
2458 	 */
2459 	if (synchronize_BE_menu() != BAM_SUCCESS)
2460 		ret = BAM_ERROR;
2461 
2462 	return (ret);
2463 }
2464 
2465 static void
2466 append_line(menu_t *mp, line_t *lp)
2467 {
2468 	if (mp->start == NULL) {
2469 		mp->start = lp;
2470 	} else {
2471 		mp->end->next = lp;
2472 		lp->prev = mp->end;
2473 	}
2474 	mp->end = lp;
2475 }
2476 
2477 void
2478 unlink_line(menu_t *mp, line_t *lp)
2479 {
2480 	/* unlink from list */
2481 	if (lp->prev)
2482 		lp->prev->next = lp->next;
2483 	else
2484 		mp->start = lp->next;
2485 	if (lp->next)
2486 		lp->next->prev = lp->prev;
2487 	else
2488 		mp->end = lp->prev;
2489 }
2490 
2491 static entry_t *
2492 boot_entry_new(menu_t *mp, line_t *start, line_t *end)
2493 {
2494 	entry_t *ent, *prev;
2495 	const char *fcn = "boot_entry_new()";
2496 
2497 	assert(mp);
2498 	assert(start);
2499 	assert(end);
2500 
2501 	ent = s_calloc(1, sizeof (entry_t));
2502 	BAM_DPRINTF((D_ENTRY_NEW, fcn));
2503 	ent->start = start;
2504 	ent->end = end;
2505 
2506 	if (mp->entries == NULL) {
2507 		mp->entries = ent;
2508 		BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn));
2509 		return (ent);
2510 	}
2511 
2512 	prev = mp->entries;
2513 	while (prev->next)
2514 		prev = prev->next;
2515 	prev->next = ent;
2516 	ent->prev = prev;
2517 	BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn));
2518 	return (ent);
2519 }
2520 
2521 static void
2522 boot_entry_addline(entry_t *ent, line_t *lp)
2523 {
2524 	if (ent)
2525 		ent->end = lp;
2526 }
2527 
2528 /*
2529  * Check whether cmd matches the one indexed by which, and whether arg matches
2530  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
2531  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
2532  * strstr(), so it can be a partial match.
2533  */
2534 static int
2535 check_cmd(const char *cmd, const int which, const char *arg, const char *str)
2536 {
2537 	int			ret;
2538 	const char		*fcn = "check_cmd()";
2539 
2540 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str));
2541 
2542 	if ((strcmp(cmd, menu_cmds[which]) != 0) &&
2543 	    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
2544 		BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH,
2545 		    fcn, cmd, menu_cmds[which]));
2546 		return (0);
2547 	}
2548 	ret = (strstr(arg, str) != NULL);
2549 
2550 	if (ret) {
2551 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
2552 	} else {
2553 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
2554 	}
2555 
2556 	return (ret);
2557 }
2558 
2559 static error_t
2560 kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
2561 {
2562 	const char		*fcn  = "kernel_parser()";
2563 
2564 	assert(entry);
2565 	assert(cmd);
2566 	assert(arg);
2567 
2568 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
2569 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
2570 		BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd));
2571 		return (BAM_ERROR);
2572 	}
2573 
2574 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
2575 		BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg));
2576 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
2577 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
2578 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
2579 		BAM_DPRINTF((D_SET_DBOOT, fcn, arg));
2580 		entry->flags |= BAM_ENTRY_DBOOT;
2581 	} else if (strncmp(arg, DIRECT_BOOT_64,
2582 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
2583 		BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg));
2584 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
2585 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
2586 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
2587 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg));
2588 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
2589 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
2590 		BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg));
2591 		entry->flags |= BAM_ENTRY_MULTIBOOT;
2592 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
2593 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
2594 		BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg));
2595 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
2596 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
2597 		BAM_DPRINTF((D_SET_HV, fcn, arg));
2598 		entry->flags |= BAM_ENTRY_HV;
2599 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
2600 		BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg));
2601 		return (BAM_ERROR);
2602 	} else {
2603 		BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg));
2604 		bam_error(UNKNOWN_KERNEL_LINE, linenum);
2605 		return (BAM_ERROR);
2606 	}
2607 
2608 	return (BAM_SUCCESS);
2609 }
2610 
2611 static error_t
2612 module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
2613 {
2614 	const char		*fcn = "module_parser()";
2615 
2616 	assert(entry);
2617 	assert(cmd);
2618 	assert(arg);
2619 
2620 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
2621 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
2622 		BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd));
2623 		return (BAM_ERROR);
2624 	}
2625 
2626 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
2627 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
2628 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
2629 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
2630 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
2631 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
2632 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
2633 		BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg));
2634 		return (BAM_SUCCESS);
2635 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
2636 	    !(entry->flags & BAM_ENTRY_LU)) {
2637 		/* don't emit warning for hand entries */
2638 		BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg));
2639 		return (BAM_ERROR);
2640 	} else {
2641 		BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg));
2642 		bam_error(UNKNOWN_MODULE_LINE, linenum);
2643 		return (BAM_ERROR);
2644 	}
2645 }
2646 
2647 /*
2648  * A line in menu.lst looks like
2649  * [ ]*<cmd>[ \t=]*<arg>*
2650  */
2651 static void
2652 line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
2653 {
2654 	/*
2655 	 * save state across calls. This is so that
2656 	 * header gets the right entry# after title has
2657 	 * been processed
2658 	 */
2659 	static line_t *prev = NULL;
2660 	static entry_t *curr_ent = NULL;
2661 	static int in_liveupgrade = 0;
2662 
2663 	line_t	*lp;
2664 	char *cmd, *sep, *arg;
2665 	char save, *cp, *line;
2666 	menu_flag_t flag = BAM_INVALID;
2667 	const char *fcn = "line_parser()";
2668 
2669 	if (str == NULL) {
2670 		return;
2671 	}
2672 
2673 	/*
2674 	 * First save a copy of the entire line.
2675 	 * We use this later to set the line field.
2676 	 */
2677 	line = s_strdup(str);
2678 
2679 	/* Eat up leading whitespace */
2680 	while (*str == ' ' || *str == '\t')
2681 		str++;
2682 
2683 	if (*str == '#') {		/* comment */
2684 		cmd = s_strdup("#");
2685 		sep = NULL;
2686 		arg = s_strdup(str + 1);
2687 		flag = BAM_COMMENT;
2688 		if (strstr(arg, BAM_LU_HDR) != NULL) {
2689 			in_liveupgrade = 1;
2690 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
2691 			in_liveupgrade = 0;
2692 		}
2693 	} else if (*str == '\0') {	/* blank line */
2694 		cmd = sep = arg = NULL;
2695 		flag = BAM_EMPTY;
2696 	} else {
2697 		/*
2698 		 * '=' is not a documented separator in grub syntax.
2699 		 * However various development bits use '=' as a
2700 		 * separator. In addition, external users also
2701 		 * use = as a separator. So we will allow that usage.
2702 		 */
2703 		cp = str;
2704 		while (*str != ' ' && *str != '\t' && *str != '=') {
2705 			if (*str == '\0') {
2706 				cmd = s_strdup(cp);
2707 				sep = arg = NULL;
2708 				break;
2709 			}
2710 			str++;
2711 		}
2712 
2713 		if (*str != '\0') {
2714 			save = *str;
2715 			*str = '\0';
2716 			cmd = s_strdup(cp);
2717 			*str = save;
2718 
2719 			str++;
2720 			save = *str;
2721 			*str = '\0';
2722 			sep = s_strdup(str - 1);
2723 			*str = save;
2724 
2725 			while (*str == ' ' || *str == '\t')
2726 				str++;
2727 			if (*str == '\0')
2728 				arg = NULL;
2729 			else
2730 				arg = s_strdup(str);
2731 		}
2732 	}
2733 
2734 	lp = s_calloc(1, sizeof (line_t));
2735 
2736 	lp->cmd = cmd;
2737 	lp->sep = sep;
2738 	lp->arg = arg;
2739 	lp->line = line;
2740 	lp->lineNum = ++(*lineNum);
2741 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
2742 		lp->entryNum = ++(*entryNum);
2743 		lp->flags = BAM_TITLE;
2744 		if (prev && prev->flags == BAM_COMMENT &&
2745 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
2746 			prev->entryNum = lp->entryNum;
2747 			curr_ent = boot_entry_new(mp, prev, lp);
2748 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
2749 			BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg));
2750 		} else {
2751 			curr_ent = boot_entry_new(mp, lp, lp);
2752 			if (in_liveupgrade) {
2753 				curr_ent->flags |= BAM_ENTRY_LU;
2754 				BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg));
2755 			}
2756 		}
2757 		curr_ent->entryNum = *entryNum;
2758 	} else if (flag != BAM_INVALID) {
2759 		/*
2760 		 * For header comments, the entry# is "fixed up"
2761 		 * by the subsequent title
2762 		 */
2763 		lp->entryNum = *entryNum;
2764 		lp->flags = flag;
2765 	} else {
2766 		lp->entryNum = *entryNum;
2767 
2768 		if (*entryNum == ENTRY_INIT) {
2769 			lp->flags = BAM_GLOBAL;
2770 		} else {
2771 			lp->flags = BAM_ENTRY;
2772 
2773 			if (cmd && arg) {
2774 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
2775 					BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg));
2776 					curr_ent->flags |= BAM_ENTRY_ROOT;
2777 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
2778 				    == 0) {
2779 					BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn,
2780 					    arg));
2781 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
2782 				} else if (strcmp(cmd,
2783 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
2784 					BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn,
2785 					    arg));
2786 					curr_ent->flags |=
2787 					    BAM_ENTRY_CHAINLOADER;
2788 				} else if (kernel_parser(curr_ent, cmd, arg,
2789 				    lp->lineNum) != BAM_SUCCESS) {
2790 					(void) module_parser(curr_ent, cmd,
2791 					    arg, lp->lineNum);
2792 				}
2793 			}
2794 		}
2795 	}
2796 
2797 	/* record default, old default, and entry line ranges */
2798 	if (lp->flags == BAM_GLOBAL &&
2799 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
2800 		mp->curdefault = lp;
2801 	} else if (lp->flags == BAM_COMMENT &&
2802 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
2803 		mp->olddefault = lp;
2804 	} else if (lp->flags == BAM_COMMENT &&
2805 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
2806 		mp->old_rc_default = lp;
2807 	} else if (lp->flags == BAM_ENTRY ||
2808 	    (lp->flags == BAM_COMMENT &&
2809 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
2810 		boot_entry_addline(curr_ent, lp);
2811 	}
2812 	append_line(mp, lp);
2813 
2814 	prev = lp;
2815 }
2816 
2817 void
2818 update_numbering(menu_t *mp)
2819 {
2820 	int lineNum;
2821 	int entryNum;
2822 	int old_default_value;
2823 	line_t *lp, *prev, *default_lp, *default_entry;
2824 	char buf[PATH_MAX];
2825 
2826 	if (mp->start == NULL) {
2827 		return;
2828 	}
2829 
2830 	lineNum = LINE_INIT;
2831 	entryNum = ENTRY_INIT;
2832 	old_default_value = ENTRY_INIT;
2833 	lp = default_lp = default_entry = NULL;
2834 
2835 	prev = NULL;
2836 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
2837 		lp->lineNum = ++lineNum;
2838 
2839 		/*
2840 		 * Get the value of the default command
2841 		 */
2842 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
2843 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
2844 		    lp->arg) {
2845 			old_default_value = atoi(lp->arg);
2846 			default_lp = lp;
2847 		}
2848 
2849 		/*
2850 		 * If not a booting entry, nothing else to fix for this
2851 		 * entry
2852 		 */
2853 		if (lp->entryNum == ENTRY_INIT)
2854 			continue;
2855 
2856 		/*
2857 		 * Record the position of the default entry.
2858 		 * The following works because global
2859 		 * commands like default and timeout should precede
2860 		 * actual boot entries, so old_default_value
2861 		 * is already known (or default cmd is missing).
2862 		 */
2863 		if (default_entry == NULL &&
2864 		    old_default_value != ENTRY_INIT &&
2865 		    lp->entryNum == old_default_value) {
2866 			default_entry = lp;
2867 		}
2868 
2869 		/*
2870 		 * Now fixup the entry number
2871 		 */
2872 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
2873 			lp->entryNum = ++entryNum;
2874 			/* fixup the bootadm header */
2875 			if (prev && prev->flags == BAM_COMMENT &&
2876 			    prev->arg &&
2877 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
2878 				prev->entryNum = lp->entryNum;
2879 			}
2880 		} else {
2881 			lp->entryNum = entryNum;
2882 		}
2883 	}
2884 
2885 	/*
2886 	 * No default command in menu, simply return
2887 	 */
2888 	if (default_lp == NULL) {
2889 		return;
2890 	}
2891 
2892 	free(default_lp->arg);
2893 	free(default_lp->line);
2894 
2895 	if (default_entry == NULL) {
2896 		default_lp->arg = s_strdup("0");
2897 	} else {
2898 		(void) snprintf(buf, sizeof (buf), "%d",
2899 		    default_entry->entryNum);
2900 		default_lp->arg = s_strdup(buf);
2901 	}
2902 
2903 	/*
2904 	 * The following is required since only the line field gets
2905 	 * written back to menu.lst
2906 	 */
2907 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
2908 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
2909 	default_lp->line = s_strdup(buf);
2910 }
2911 
2912 
2913 static menu_t *
2914 menu_read(char *menu_path)
2915 {
2916 	FILE *fp;
2917 	char buf[BAM_MAXLINE], *cp;
2918 	menu_t *mp;
2919 	int line, entry, len, n;
2920 
2921 	mp = s_calloc(1, sizeof (menu_t));
2922 
2923 	fp = fopen(menu_path, "r");
2924 	if (fp == NULL) { /* Let the caller handle this error */
2925 		return (mp);
2926 	}
2927 
2928 
2929 	/* Note: GRUB boot entry number starts with 0 */
2930 	line = LINE_INIT;
2931 	entry = ENTRY_INIT;
2932 	cp = buf;
2933 	len = sizeof (buf);
2934 	while (s_fgets(cp, len, fp) != NULL) {
2935 		n = strlen(cp);
2936 		if (cp[n - 1] == '\\') {
2937 			len -= n - 1;
2938 			assert(len >= 2);
2939 			cp += n - 1;
2940 			continue;
2941 		}
2942 		line_parser(mp, buf, &line, &entry);
2943 		cp = buf;
2944 		len = sizeof (buf);
2945 	}
2946 
2947 	if (fclose(fp) == EOF) {
2948 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
2949 	}
2950 
2951 	return (mp);
2952 }
2953 
2954 static error_t
2955 selector(menu_t *mp, char *opt, int *entry, char **title)
2956 {
2957 	char *eq;
2958 	char *opt_dup;
2959 	int entryNum;
2960 
2961 	assert(mp);
2962 	assert(mp->start);
2963 	assert(opt);
2964 
2965 	opt_dup = s_strdup(opt);
2966 
2967 	if (entry)
2968 		*entry = ENTRY_INIT;
2969 	if (title)
2970 		*title = NULL;
2971 
2972 	eq = strchr(opt_dup, '=');
2973 	if (eq == NULL) {
2974 		bam_error(INVALID_OPT, opt);
2975 		free(opt_dup);
2976 		return (BAM_ERROR);
2977 	}
2978 
2979 	*eq = '\0';
2980 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
2981 		assert(mp->end);
2982 		entryNum = s_strtol(eq + 1);
2983 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
2984 			bam_error(INVALID_ENTRY, eq + 1);
2985 			free(opt_dup);
2986 			return (BAM_ERROR);
2987 		}
2988 		*entry = entryNum;
2989 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
2990 		*title = opt + (eq - opt_dup) + 1;
2991 	} else {
2992 		bam_error(INVALID_OPT, opt);
2993 		free(opt_dup);
2994 		return (BAM_ERROR);
2995 	}
2996 
2997 	free(opt_dup);
2998 	return (BAM_SUCCESS);
2999 }
3000 
3001 /*
3002  * If invoked with no titles/entries (opt == NULL)
3003  * only title lines in file are printed.
3004  *
3005  * If invoked with a title or entry #, all
3006  * lines in *every* matching entry are listed
3007  */
3008 static error_t
3009 list_entry(menu_t *mp, char *menu_path, char *opt)
3010 {
3011 	line_t *lp;
3012 	int entry = ENTRY_INIT;
3013 	int found;
3014 	char *title = NULL;
3015 
3016 	assert(mp);
3017 	assert(menu_path);
3018 
3019 	/* opt is optional */
3020 	BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path,
3021 	    opt ? opt : "<NULL>"));
3022 
3023 	if (mp->start == NULL) {
3024 		bam_error(NO_MENU, menu_path);
3025 		return (BAM_ERROR);
3026 	}
3027 
3028 	if (opt != NULL) {
3029 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
3030 			return (BAM_ERROR);
3031 		}
3032 		assert((entry != ENTRY_INIT) ^ (title != NULL));
3033 	} else {
3034 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
3035 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
3036 	}
3037 
3038 	found = 0;
3039 	for (lp = mp->start; lp; lp = lp->next) {
3040 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
3041 			continue;
3042 		if (opt == NULL && lp->flags == BAM_TITLE) {
3043 			bam_print(PRINT_TITLE, lp->entryNum,
3044 			    lp->arg);
3045 			found = 1;
3046 			continue;
3047 		}
3048 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
3049 			bam_print(PRINT, lp->line);
3050 			found = 1;
3051 			continue;
3052 		}
3053 
3054 		/*
3055 		 * We set the entry value here so that all lines
3056 		 * in entry get printed. If we subsequently match
3057 		 * title in other entries, all lines in those
3058 		 * entries get printed as well.
3059 		 */
3060 		if (title && lp->flags == BAM_TITLE && lp->arg &&
3061 		    strncmp(title, lp->arg, strlen(title)) == 0) {
3062 			bam_print(PRINT, lp->line);
3063 			entry = lp->entryNum;
3064 			found = 1;
3065 			continue;
3066 		}
3067 	}
3068 
3069 	if (!found) {
3070 		bam_error(NO_MATCH_ENTRY);
3071 		return (BAM_ERROR);
3072 	}
3073 
3074 	return (BAM_SUCCESS);
3075 }
3076 
3077 int
3078 add_boot_entry(menu_t *mp,
3079 	char *title,
3080 	char *findroot,
3081 	char *kernel,
3082 	char *mod_kernel,
3083 	char *module)
3084 {
3085 	int		lineNum;
3086 	int		entryNum;
3087 	char		linebuf[BAM_MAXLINE];
3088 	menu_cmd_t	k_cmd;
3089 	menu_cmd_t	m_cmd;
3090 	const char	*fcn = "add_boot_entry()";
3091 
3092 	assert(mp);
3093 
3094 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
3095 	if (findroot == NULL) {
3096 		bam_error(NULL_FINDROOT);
3097 		return (BAM_ERROR);
3098 	}
3099 
3100 	if (title == NULL) {
3101 		title = "Solaris";	/* default to Solaris */
3102 	}
3103 	if (kernel == NULL) {
3104 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
3105 		return (BAM_ERROR);
3106 	}
3107 	if (module == NULL) {
3108 		if (bam_direct != BAM_DIRECT_DBOOT) {
3109 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
3110 			return (BAM_ERROR);
3111 		}
3112 
3113 		/* Figure the commands out from the kernel line */
3114 		if (strstr(kernel, "$ISADIR") != NULL) {
3115 			module = DIRECT_BOOT_ARCHIVE;
3116 			k_cmd = KERNEL_DOLLAR_CMD;
3117 			m_cmd = MODULE_DOLLAR_CMD;
3118 		} else if (strstr(kernel, "amd64") != NULL) {
3119 			module = DIRECT_BOOT_ARCHIVE_64;
3120 			k_cmd = KERNEL_CMD;
3121 			m_cmd = MODULE_CMD;
3122 		} else {
3123 			module = DIRECT_BOOT_ARCHIVE_32;
3124 			k_cmd = KERNEL_CMD;
3125 			m_cmd = MODULE_CMD;
3126 		}
3127 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
3128 	    (strstr(kernel, "$ISADIR") != NULL)) {
3129 		/*
3130 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
3131 		 * command.  Otherwise, use "kernel".
3132 		 */
3133 		k_cmd = KERNEL_DOLLAR_CMD;
3134 		m_cmd = MODULE_DOLLAR_CMD;
3135 	} else {
3136 		k_cmd = KERNEL_CMD;
3137 		m_cmd = MODULE_CMD;
3138 	}
3139 
3140 	if (mp->start) {
3141 		lineNum = mp->end->lineNum;
3142 		entryNum = mp->end->entryNum;
3143 	} else {
3144 		lineNum = LINE_INIT;
3145 		entryNum = ENTRY_INIT;
3146 	}
3147 
3148 	/*
3149 	 * No separator for comment (HDR/FTR) commands
3150 	 * The syntax for comments is #<comment>
3151 	 */
3152 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
3153 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
3154 	line_parser(mp, linebuf, &lineNum, &entryNum);
3155 
3156 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3157 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
3158 	line_parser(mp, linebuf, &lineNum, &entryNum);
3159 
3160 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3161 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
3162 	line_parser(mp, linebuf, &lineNum, &entryNum);
3163 	BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum));
3164 
3165 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3166 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
3167 	line_parser(mp, linebuf, &lineNum, &entryNum);
3168 
3169 	if (mod_kernel != NULL) {
3170 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3171 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
3172 		line_parser(mp, linebuf, &lineNum, &entryNum);
3173 	}
3174 
3175 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3176 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
3177 	line_parser(mp, linebuf, &lineNum, &entryNum);
3178 
3179 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
3180 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
3181 	line_parser(mp, linebuf, &lineNum, &entryNum);
3182 
3183 	return (entryNum);
3184 }
3185 
3186 static error_t
3187 do_delete(menu_t *mp, int entryNum)
3188 {
3189 	line_t		*lp;
3190 	line_t		*freed;
3191 	entry_t		*ent;
3192 	entry_t		*tmp;
3193 	int		deleted;
3194 	const char	*fcn = "do_delete()";
3195 
3196 	assert(entryNum != ENTRY_INIT);
3197 
3198 	tmp = NULL;
3199 
3200 	ent = mp->entries;
3201 	while (ent) {
3202 		lp = ent->start;
3203 		/* check entry number and make sure it's a bootadm entry */
3204 		if (lp->flags != BAM_COMMENT ||
3205 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
3206 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
3207 			ent = ent->next;
3208 			continue;
3209 		}
3210 
3211 		/* free the entry content */
3212 		do {
3213 			freed = lp;
3214 			lp = lp->next;	/* prev stays the same */
3215 			BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum));
3216 			unlink_line(mp, freed);
3217 			line_free(freed);
3218 		} while (freed != ent->end);
3219 
3220 		/* free the entry_t structure */
3221 		assert(tmp == NULL);
3222 		tmp = ent;
3223 		ent = ent->next;
3224 		if (tmp->prev)
3225 			tmp->prev->next = ent;
3226 		else
3227 			mp->entries = ent;
3228 		if (ent)
3229 			ent->prev = tmp->prev;
3230 		BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum));
3231 		free(tmp);
3232 		tmp = NULL;
3233 		deleted = 1;
3234 	}
3235 
3236 	assert(tmp == NULL);
3237 
3238 	if (!deleted && entryNum != ALL_ENTRIES) {
3239 		bam_error(NO_BOOTADM_MATCH);
3240 		return (BAM_ERROR);
3241 	}
3242 
3243 	/*
3244 	 * Now that we have deleted an entry, update
3245 	 * the entry numbering and the default cmd.
3246 	 */
3247 	update_numbering(mp);
3248 
3249 	return (BAM_SUCCESS);
3250 }
3251 
3252 static error_t
3253 delete_all_entries(menu_t *mp, char *dummy, char *opt)
3254 {
3255 	assert(mp);
3256 	assert(dummy == NULL);
3257 	assert(opt == NULL);
3258 
3259 	BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries"));
3260 
3261 	if (mp->start == NULL) {
3262 		bam_print(EMPTY_MENU);
3263 		return (BAM_SUCCESS);
3264 	}
3265 
3266 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
3267 		return (BAM_ERROR);
3268 	}
3269 
3270 	return (BAM_WRITE);
3271 }
3272 
3273 static FILE *
3274 create_diskmap(char *osroot)
3275 {
3276 	FILE *fp;
3277 	char cmd[PATH_MAX];
3278 	const char *fcn = "create_diskmap()";
3279 
3280 	/* make sure we have a map file */
3281 	fp = fopen(GRUBDISK_MAP, "r");
3282 	if (fp == NULL) {
3283 		(void) snprintf(cmd, sizeof (cmd),
3284 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
3285 		if (exec_cmd(cmd, NULL) != 0)
3286 			return (NULL);
3287 		fp = fopen(GRUBDISK_MAP, "r");
3288 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
3289 		if (fp) {
3290 			BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP));
3291 		} else {
3292 			BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP));
3293 		}
3294 	}
3295 	return (fp);
3296 }
3297 
3298 #define	SECTOR_SIZE	512
3299 
3300 static int
3301 get_partition(char *device)
3302 {
3303 	int i, fd, is_pcfs, partno = -1;
3304 	struct mboot *mboot;
3305 	char boot_sect[SECTOR_SIZE];
3306 	char *wholedisk, *slice;
3307 
3308 	/* form whole disk (p0) */
3309 	slice = device + strlen(device) - 2;
3310 	is_pcfs = (*slice != 's');
3311 	if (!is_pcfs)
3312 		*slice = '\0';
3313 	wholedisk = s_calloc(1, strlen(device) + 3);
3314 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
3315 	if (!is_pcfs)
3316 		*slice = 's';
3317 
3318 	/* read boot sector */
3319 	fd = open(wholedisk, O_RDONLY);
3320 	free(wholedisk);
3321 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
3322 		return (partno);
3323 	}
3324 	(void) close(fd);
3325 
3326 	/* parse fdisk table */
3327 	mboot = (struct mboot *)((void *)boot_sect);
3328 	for (i = 0; i < FD_NUMPART; i++) {
3329 		struct ipart *part =
3330 		    (struct ipart *)(uintptr_t)mboot->parts + i;
3331 		if (is_pcfs) {	/* looking for solaris boot part */
3332 			if (part->systid == 0xbe) {
3333 				partno = i;
3334 				break;
3335 			}
3336 		} else {	/* look for solaris partition, old and new */
3337 			if (part->systid == SUNIXOS ||
3338 			    part->systid == SUNIXOS2) {
3339 				partno = i;
3340 				break;
3341 			}
3342 		}
3343 	}
3344 	return (partno);
3345 }
3346 
3347 char *
3348 get_grubroot(char *osroot, char *osdev, char *menu_root)
3349 {
3350 	char		*grubroot;	/* (hd#,#,#) */
3351 	char		*slice;
3352 	char		*grubhd;
3353 	int		fdiskpart;
3354 	int		found = 0;
3355 	char		*devname;
3356 	char		*ctdname = strstr(osdev, "dsk/");
3357 	char		linebuf[PATH_MAX];
3358 	FILE		*fp;
3359 	const char	*fcn = "get_grubroot()";
3360 
3361 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
3362 	if (ctdname == NULL) {
3363 		bam_error(INVALID_DEV_DSK, osdev);
3364 		return (NULL);
3365 	}
3366 
3367 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
3368 		/* menu bears no resemblance to our reality */
3369 		bam_error(CANNOT_GRUBROOT_BOOTDISK, fcn, osdev);
3370 		return (NULL);
3371 	}
3372 
3373 	ctdname += strlen("dsk/");
3374 	slice = strrchr(ctdname, 's');
3375 	if (slice)
3376 		*slice = '\0';
3377 
3378 	fp = create_diskmap(osroot);
3379 	if (fp == NULL) {
3380 		bam_error(DISKMAP_FAIL, osroot);
3381 		return (NULL);
3382 	}
3383 
3384 	rewind(fp);
3385 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
3386 		grubhd = strtok(linebuf, " \t\n");
3387 		if (grubhd)
3388 			devname = strtok(NULL, " \t\n");
3389 		else
3390 			devname = NULL;
3391 		if (devname && strcmp(devname, ctdname) == 0) {
3392 			found = 1;
3393 			break;
3394 		}
3395 	}
3396 
3397 	if (slice)
3398 		*slice = 's';
3399 
3400 	(void) fclose(fp);
3401 	fp = NULL;
3402 
3403 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
3404 	if (found == 0) {
3405 		bam_error(BIOSDEV_FAIL, osdev);
3406 		return (NULL);
3407 	}
3408 
3409 	fdiskpart = get_partition(osdev);
3410 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = -1);
3411 	if (fdiskpart == -1) {
3412 		bam_error(FDISKPART_FAIL, osdev);
3413 		return (NULL);
3414 	}
3415 
3416 	grubroot = s_calloc(1, 10);
3417 	if (slice) {
3418 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
3419 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
3420 	} else
3421 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
3422 		    grubhd, fdiskpart);
3423 
3424 	assert(fp == NULL);
3425 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
3426 	return (grubroot);
3427 }
3428 
3429 static char *
3430 find_primary_common(char *mntpt, char *fstype)
3431 {
3432 	char		signdir[PATH_MAX];
3433 	char		tmpsign[MAXNAMELEN + 1];
3434 	char		*lu;
3435 	char		*ufs;
3436 	char		*zfs;
3437 	DIR		*dirp = NULL;
3438 	struct dirent	*entp;
3439 	struct stat	sb;
3440 	const char	*fcn = "find_primary_common()";
3441 
3442 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
3443 	    mntpt, GRUBSIGN_DIR);
3444 
3445 	if (stat(signdir, &sb) == -1) {
3446 		BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir));
3447 		return (NULL);
3448 	}
3449 
3450 	dirp = opendir(signdir);
3451 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
3452 	if (dirp == NULL) {
3453 		bam_error(OPENDIR_FAILED, signdir, strerror(errno));
3454 		return (NULL);
3455 	}
3456 
3457 	ufs = zfs = lu = NULL;
3458 
3459 	while (entp = readdir(dirp)) {
3460 		if (strcmp(entp->d_name, ".") == 0 ||
3461 		    strcmp(entp->d_name, "..") == 0)
3462 			continue;
3463 
3464 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
3465 
3466 		if (lu == NULL &&
3467 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
3468 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
3469 			lu = s_strdup(tmpsign);
3470 		}
3471 
3472 		if (ufs == NULL &&
3473 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
3474 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
3475 			ufs = s_strdup(tmpsign);
3476 		}
3477 
3478 		if (zfs == NULL &&
3479 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
3480 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
3481 			zfs = s_strdup(tmpsign);
3482 		}
3483 	}
3484 
3485 	BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn,
3486 	    zfs ? zfs : "NULL",
3487 	    ufs ? ufs : "NULL",
3488 	    lu ? lu : "NULL"));
3489 
3490 	if (dirp) {
3491 		(void) closedir(dirp);
3492 		dirp = NULL;
3493 	}
3494 
3495 	if (strcmp(fstype, "ufs") == 0 && zfs) {
3496 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
3497 		free(zfs);
3498 		zfs = NULL;
3499 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
3500 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
3501 		free(ufs);
3502 		ufs = NULL;
3503 	}
3504 
3505 	assert(dirp == NULL);
3506 
3507 	/* For now, we let Live Upgrade take care of its signature itself */
3508 	if (lu) {
3509 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
3510 		free(lu);
3511 		lu = NULL;
3512 	}
3513 
3514 	return (zfs ? zfs : ufs);
3515 }
3516 
3517 static char *
3518 find_backup_common(char *mntpt, char *fstype)
3519 {
3520 	FILE		*bfp = NULL;
3521 	char		tmpsign[MAXNAMELEN + 1];
3522 	char		backup[PATH_MAX];
3523 	char		*ufs;
3524 	char		*zfs;
3525 	char		*lu;
3526 	int		error;
3527 	const char	*fcn = "find_backup_common()";
3528 
3529 	/*
3530 	 * We didn't find it in the primary directory.
3531 	 * Look at the backup
3532 	 */
3533 	(void) snprintf(backup, sizeof (backup), "%s%s",
3534 	    mntpt, GRUBSIGN_BACKUP);
3535 
3536 	bfp = fopen(backup, "r");
3537 	if (bfp == NULL) {
3538 		error = errno;
3539 		if (bam_verbose) {
3540 			bam_error(OPEN_FAIL, backup, strerror(error));
3541 		}
3542 		BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error)));
3543 		return (NULL);
3544 	}
3545 
3546 	ufs = zfs = lu = NULL;
3547 
3548 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
3549 
3550 		if (lu == NULL &&
3551 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
3552 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
3553 			lu = s_strdup(tmpsign);
3554 		}
3555 
3556 		if (ufs == NULL &&
3557 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
3558 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
3559 			ufs = s_strdup(tmpsign);
3560 		}
3561 
3562 		if (zfs == NULL &&
3563 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
3564 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
3565 			zfs = s_strdup(tmpsign);
3566 		}
3567 	}
3568 
3569 	BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn,
3570 	    zfs ? zfs : "NULL",
3571 	    ufs ? ufs : "NULL",
3572 	    lu ? lu : "NULL"));
3573 
3574 	if (bfp) {
3575 		(void) fclose(bfp);
3576 		bfp = NULL;
3577 	}
3578 
3579 	if (strcmp(fstype, "ufs") == 0 && zfs) {
3580 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
3581 		free(zfs);
3582 		zfs = NULL;
3583 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
3584 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
3585 		free(ufs);
3586 		ufs = NULL;
3587 	}
3588 
3589 	assert(bfp == NULL);
3590 
3591 	/* For now, we let Live Upgrade take care of its signature itself */
3592 	if (lu) {
3593 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
3594 		free(lu);
3595 		lu = NULL;
3596 	}
3597 
3598 	return (zfs ? zfs : ufs);
3599 }
3600 
3601 static char *
3602 find_ufs_existing(char *osroot)
3603 {
3604 	char		*sign;
3605 	const char	*fcn = "find_ufs_existing()";
3606 
3607 	sign = find_primary_common(osroot, "ufs");
3608 	if (sign == NULL) {
3609 		sign = find_backup_common(osroot, "ufs");
3610 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
3611 	} else {
3612 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
3613 	}
3614 
3615 	return (sign);
3616 }
3617 
3618 char *
3619 get_mountpoint(char *special, char *fstype)
3620 {
3621 	FILE		*mntfp;
3622 	struct mnttab	mp = {0};
3623 	struct mnttab	mpref = {0};
3624 	int		error;
3625 	int		ret;
3626 	const char	*fcn = "get_mountpoint()";
3627 
3628 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype));
3629 
3630 	mntfp = fopen(MNTTAB, "r");
3631 	error = errno;
3632 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
3633 	if (mntfp == NULL) {
3634 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
3635 		return (NULL);
3636 	}
3637 
3638 	mpref.mnt_special = special;
3639 	mpref.mnt_fstype = fstype;
3640 
3641 	ret = getmntany(mntfp, &mp, &mpref);
3642 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
3643 	if (ret != 0) {
3644 		(void) fclose(mntfp);
3645 		BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype));
3646 		return (NULL);
3647 	}
3648 	(void) fclose(mntfp);
3649 
3650 	assert(mp.mnt_mountp);
3651 
3652 	BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp));
3653 
3654 	return (s_strdup(mp.mnt_mountp));
3655 }
3656 
3657 /*
3658  * Mounts a "legacy" top dataset (if needed)
3659  * Returns:	The mountpoint of the legacy top dataset or NULL on error
3660  * 		mnted returns one of the above values defined for zfs_mnted_t
3661  */
3662 static char *
3663 mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
3664 {
3665 	char		cmd[PATH_MAX];
3666 	char		tmpmnt[PATH_MAX];
3667 	filelist_t	flist = {0};
3668 	char		*is_mounted;
3669 	struct stat	sb;
3670 	int		ret;
3671 	const char	*fcn = "mount_legacy_dataset()";
3672 
3673 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
3674 
3675 	*mnted = ZFS_MNT_ERROR;
3676 
3677 	(void) snprintf(cmd, sizeof (cmd),
3678 	    "/sbin/zfs get -Ho value mounted %s",
3679 	    pool);
3680 
3681 	ret = exec_cmd(cmd, &flist);
3682 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
3683 	if (ret != 0) {
3684 		bam_error(ZFS_MNTED_FAILED, pool);
3685 		return (NULL);
3686 	}
3687 
3688 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
3689 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3690 		bam_error(BAD_ZFS_MNTED, pool);
3691 		filelist_free(&flist);
3692 		return (NULL);
3693 	}
3694 
3695 	is_mounted = strtok(flist.head->line, " \t\n");
3696 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
3697 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
3698 	if (strcmp(is_mounted, "no") != 0) {
3699 		filelist_free(&flist);
3700 		*mnted = LEGACY_ALREADY;
3701 		/* get_mountpoint returns a strdup'ed string */
3702 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool));
3703 		return (get_mountpoint(pool, "zfs"));
3704 	}
3705 
3706 	filelist_free(&flist);
3707 
3708 	/*
3709 	 * legacy top dataset is not mounted. Mount it now
3710 	 * First create a mountpoint.
3711 	 */
3712 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
3713 	    ZFS_LEGACY_MNTPT, getpid());
3714 
3715 	ret = stat(tmpmnt, &sb);
3716 	if (ret == -1) {
3717 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt));
3718 		ret = mkdirp(tmpmnt, 0755);
3719 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
3720 		if (ret == -1) {
3721 			bam_error(MKDIR_FAILED, tmpmnt, strerror(errno));
3722 			return (NULL);
3723 		}
3724 	} else {
3725 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt));
3726 	}
3727 
3728 	(void) snprintf(cmd, sizeof (cmd),
3729 	    "/sbin/mount -F zfs %s %s",
3730 	    pool, tmpmnt);
3731 
3732 	ret = exec_cmd(cmd, NULL);
3733 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
3734 	if (ret != 0) {
3735 		bam_error(ZFS_MOUNT_FAILED, pool);
3736 		(void) rmdir(tmpmnt);
3737 		return (NULL);
3738 	}
3739 
3740 	*mnted = LEGACY_MOUNTED;
3741 	BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt));
3742 	return (s_strdup(tmpmnt));
3743 }
3744 
3745 /*
3746  * Mounts the top dataset (if needed)
3747  * Returns:	The mountpoint of the top dataset or NULL on error
3748  * 		mnted returns one of the above values defined for zfs_mnted_t
3749  */
3750 static char *
3751 mount_top_dataset(char *pool, zfs_mnted_t *mnted)
3752 {
3753 	char		cmd[PATH_MAX];
3754 	filelist_t	flist = {0};
3755 	char		*is_mounted;
3756 	char		*mntpt;
3757 	char		*zmntpt;
3758 	int		ret;
3759 	const char	*fcn = "mount_top_dataset()";
3760 
3761 	*mnted = ZFS_MNT_ERROR;
3762 
3763 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
3764 
3765 	/*
3766 	 * First check if the top dataset is a "legacy" dataset
3767 	 */
3768 	(void) snprintf(cmd, sizeof (cmd),
3769 	    "/sbin/zfs get -Ho value mountpoint %s",
3770 	    pool);
3771 	ret = exec_cmd(cmd, &flist);
3772 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
3773 	if (ret != 0) {
3774 		bam_error(ZFS_MNTPT_FAILED, pool);
3775 		return (NULL);
3776 	}
3777 
3778 	if (flist.head && (flist.head == flist.tail)) {
3779 		char *legacy = strtok(flist.head->line, " \t\n");
3780 		if (legacy && strcmp(legacy, "legacy") == 0) {
3781 			filelist_free(&flist);
3782 			BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool));
3783 			return (mount_legacy_dataset(pool, mnted));
3784 		}
3785 	}
3786 
3787 	filelist_free(&flist);
3788 
3789 	BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool));
3790 
3791 	(void) snprintf(cmd, sizeof (cmd),
3792 	    "/sbin/zfs get -Ho value mounted %s",
3793 	    pool);
3794 
3795 	ret = exec_cmd(cmd, &flist);
3796 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
3797 	if (ret != 0) {
3798 		bam_error(ZFS_MNTED_FAILED, pool);
3799 		return (NULL);
3800 	}
3801 
3802 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
3803 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3804 		bam_error(BAD_ZFS_MNTED, pool);
3805 		filelist_free(&flist);
3806 		return (NULL);
3807 	}
3808 
3809 	is_mounted = strtok(flist.head->line, " \t\n");
3810 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
3811 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
3812 	if (strcmp(is_mounted, "no") != 0) {
3813 		filelist_free(&flist);
3814 		*mnted = ZFS_ALREADY;
3815 		BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool));
3816 		goto mounted;
3817 	}
3818 
3819 	filelist_free(&flist);
3820 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool));
3821 
3822 	/* top dataset is not mounted. Mount it now */
3823 	(void) snprintf(cmd, sizeof (cmd),
3824 	    "/sbin/zfs mount %s", pool);
3825 	ret = exec_cmd(cmd, NULL);
3826 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
3827 	if (ret != 0) {
3828 		bam_error(ZFS_MOUNT_FAILED, pool);
3829 		return (NULL);
3830 	}
3831 	*mnted = ZFS_MOUNTED;
3832 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool));
3833 	/*FALLTHRU*/
3834 mounted:
3835 	/*
3836 	 * Now get the mountpoint
3837 	 */
3838 	(void) snprintf(cmd, sizeof (cmd),
3839 	    "/sbin/zfs get -Ho value mountpoint %s",
3840 	    pool);
3841 
3842 	ret = exec_cmd(cmd, &flist);
3843 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
3844 	if (ret != 0) {
3845 		bam_error(ZFS_MNTPT_FAILED, pool);
3846 		goto error;
3847 	}
3848 
3849 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
3850 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3851 		bam_error(NULL_ZFS_MNTPT, pool);
3852 		goto error;
3853 	}
3854 
3855 	mntpt = strtok(flist.head->line, " \t\n");
3856 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
3857 	if (*mntpt != '/') {
3858 		bam_error(BAD_ZFS_MNTPT, pool, mntpt);
3859 		goto error;
3860 	}
3861 	zmntpt = s_strdup(mntpt);
3862 
3863 	filelist_free(&flist);
3864 
3865 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt));
3866 
3867 	return (zmntpt);
3868 
3869 error:
3870 	filelist_free(&flist);
3871 	(void) umount_top_dataset(pool, *mnted, NULL);
3872 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
3873 	return (NULL);
3874 }
3875 
3876 static int
3877 umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
3878 {
3879 	char		cmd[PATH_MAX];
3880 	int		ret;
3881 	const char	*fcn = "umount_top_dataset()";
3882 
3883 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
3884 	switch (mnted) {
3885 	case LEGACY_ALREADY:
3886 	case ZFS_ALREADY:
3887 		/* nothing to do */
3888 		BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool,
3889 		    mntpt ? mntpt : "NULL"));
3890 		free(mntpt);
3891 		return (BAM_SUCCESS);
3892 	case LEGACY_MOUNTED:
3893 		(void) snprintf(cmd, sizeof (cmd),
3894 		    "/sbin/umount %s", pool);
3895 		ret = exec_cmd(cmd, NULL);
3896 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
3897 		if (ret != 0) {
3898 			bam_error(UMOUNT_FAILED, pool);
3899 			free(mntpt);
3900 			return (BAM_ERROR);
3901 		}
3902 		if (mntpt)
3903 			(void) rmdir(mntpt);
3904 		free(mntpt);
3905 		BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool));
3906 		return (BAM_SUCCESS);
3907 	case ZFS_MOUNTED:
3908 		free(mntpt);
3909 		(void) snprintf(cmd, sizeof (cmd),
3910 		    "/sbin/zfs unmount %s", pool);
3911 		ret = exec_cmd(cmd, NULL);
3912 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
3913 		if (ret != 0) {
3914 			bam_error(UMOUNT_FAILED, pool);
3915 			return (BAM_ERROR);
3916 		}
3917 		BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool));
3918 		return (BAM_SUCCESS);
3919 	default:
3920 		bam_error(INT_BAD_MNTSTATE, pool);
3921 		return (BAM_ERROR);
3922 	}
3923 	/*NOTREACHED*/
3924 }
3925 
3926 /*
3927  * For ZFS, osdev can be one of two forms
3928  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
3929  * It can be a /dev/[r]dsk special file. We handle both instances
3930  */
3931 static char *
3932 get_pool(char *osdev)
3933 {
3934 	char		cmd[PATH_MAX];
3935 	char		buf[PATH_MAX];
3936 	filelist_t	flist = {0};
3937 	char		*pool;
3938 	char		*cp;
3939 	char		*slash;
3940 	int		ret;
3941 	const char	*fcn = "get_pool()";
3942 
3943 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
3944 	if (osdev == NULL) {
3945 		bam_error(GET_POOL_OSDEV_NULL);
3946 		return (NULL);
3947 	}
3948 
3949 	BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev));
3950 
3951 	if (osdev[0] != '/') {
3952 		(void) strlcpy(buf, osdev, sizeof (buf));
3953 		slash = strchr(buf, '/');
3954 		if (slash)
3955 			*slash = '\0';
3956 		pool = s_strdup(buf);
3957 		BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
3958 		return (pool);
3959 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
3960 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
3961 		bam_error(GET_POOL_BAD_OSDEV, osdev);
3962 		return (NULL);
3963 	}
3964 
3965 	(void) snprintf(cmd, sizeof (cmd),
3966 	    "/usr/sbin/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
3967 	    osdev);
3968 
3969 	ret = exec_cmd(cmd, &flist);
3970 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
3971 	if (ret != 0) {
3972 		bam_error(FSTYP_A_FAILED, osdev);
3973 		return (NULL);
3974 	}
3975 
3976 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
3977 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3978 		bam_error(NULL_FSTYP_A, osdev);
3979 		filelist_free(&flist);
3980 		return (NULL);
3981 	}
3982 
3983 	(void) strtok(flist.head->line, "'");
3984 	cp = strtok(NULL, "'");
3985 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
3986 	if (cp == NULL) {
3987 		bam_error(BAD_FSTYP_A, osdev);
3988 		filelist_free(&flist);
3989 		return (NULL);
3990 	}
3991 
3992 	pool = s_strdup(cp);
3993 
3994 	filelist_free(&flist);
3995 
3996 	BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
3997 
3998 	return (pool);
3999 }
4000 
4001 static char *
4002 find_zfs_existing(char *osdev)
4003 {
4004 	char		*pool;
4005 	zfs_mnted_t	mnted;
4006 	char		*mntpt;
4007 	char		*sign;
4008 	const char	*fcn = "find_zfs_existing()";
4009 
4010 	pool = get_pool(osdev);
4011 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
4012 	if (pool == NULL) {
4013 		bam_error(ZFS_GET_POOL_FAILED, osdev);
4014 		return (NULL);
4015 	}
4016 
4017 	mntpt = mount_top_dataset(pool, &mnted);
4018 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
4019 	if (mntpt == NULL) {
4020 		bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool);
4021 		free(pool);
4022 		return (NULL);
4023 	}
4024 
4025 	sign = find_primary_common(mntpt, "zfs");
4026 	if (sign == NULL) {
4027 		sign = find_backup_common(mntpt, "zfs");
4028 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
4029 	} else {
4030 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
4031 	}
4032 
4033 	(void) umount_top_dataset(pool, mnted, mntpt);
4034 
4035 	free(pool);
4036 
4037 	return (sign);
4038 }
4039 
4040 static char *
4041 find_existing_sign(char *osroot, char *osdev, char *fstype)
4042 {
4043 	const char		*fcn = "find_existing_sign()";
4044 
4045 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
4046 	if (strcmp(fstype, "ufs") == 0) {
4047 		BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn));
4048 		return (find_ufs_existing(osroot));
4049 	} else if (strcmp(fstype, "zfs") == 0) {
4050 		BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn));
4051 		return (find_zfs_existing(osdev));
4052 	} else {
4053 		bam_error(GRUBSIGN_NOTSUP, fstype);
4054 		return (NULL);
4055 	}
4056 }
4057 
4058 #define	MH_HASH_SZ	16
4059 
4060 typedef enum {
4061 	MH_ERROR = -1,
4062 	MH_NOMATCH,
4063 	MH_MATCH
4064 } mh_search_t;
4065 
4066 typedef struct mcache {
4067 	char	*mc_special;
4068 	char	*mc_mntpt;
4069 	char	*mc_fstype;
4070 	struct mcache *mc_next;
4071 } mcache_t;
4072 
4073 typedef struct mhash {
4074 	mcache_t *mh_hash[MH_HASH_SZ];
4075 } mhash_t;
4076 
4077 static int
4078 mhash_fcn(char *key)
4079 {
4080 	int		i;
4081 	uint64_t	sum = 0;
4082 
4083 	for (i = 0; key[i] != '\0'; i++) {
4084 		sum += (uchar_t)key[i];
4085 	}
4086 
4087 	sum %= MH_HASH_SZ;
4088 
4089 	assert(sum < MH_HASH_SZ);
4090 
4091 	return (sum);
4092 }
4093 
4094 static mhash_t *
4095 cache_mnttab(void)
4096 {
4097 	FILE		*mfp;
4098 	struct extmnttab mnt;
4099 	mcache_t	*mcp;
4100 	mhash_t		*mhp;
4101 	char		*ctds;
4102 	int		idx;
4103 	int		error;
4104 	char		*special_dup;
4105 	const char	*fcn = "cache_mnttab()";
4106 
4107 	mfp = fopen(MNTTAB, "r");
4108 	error = errno;
4109 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
4110 	if (mfp == NULL) {
4111 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
4112 		return (NULL);
4113 	}
4114 
4115 	mhp = s_calloc(1, sizeof (mhash_t));
4116 
4117 	resetmnttab(mfp);
4118 
4119 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
4120 		/* only cache ufs */
4121 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
4122 			continue;
4123 
4124 		/* basename() modifies its arg, so dup it */
4125 		special_dup = s_strdup(mnt.mnt_special);
4126 		ctds = basename(special_dup);
4127 
4128 		mcp = s_calloc(1, sizeof (mcache_t));
4129 		mcp->mc_special = s_strdup(ctds);
4130 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
4131 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
4132 		BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds,
4133 		    mnt.mnt_mountp, mnt.mnt_fstype));
4134 		idx = mhash_fcn(ctds);
4135 		mcp->mc_next = mhp->mh_hash[idx];
4136 		mhp->mh_hash[idx] = mcp;
4137 		free(special_dup);
4138 	}
4139 
4140 	(void) fclose(mfp);
4141 
4142 	return (mhp);
4143 }
4144 
4145 static void
4146 free_mnttab(mhash_t *mhp)
4147 {
4148 	mcache_t	*mcp;
4149 	int		i;
4150 
4151 	for (i = 0; i < MH_HASH_SZ; i++) {
4152 		/*LINTED*/
4153 		while (mcp = mhp->mh_hash[i]) {
4154 			mhp->mh_hash[i] = mcp->mc_next;
4155 			free(mcp->mc_special);
4156 			free(mcp->mc_mntpt);
4157 			free(mcp->mc_fstype);
4158 			free(mcp);
4159 		}
4160 	}
4161 
4162 	for (i = 0; i < MH_HASH_SZ; i++) {
4163 		assert(mhp->mh_hash[i] == NULL);
4164 	}
4165 	free(mhp);
4166 }
4167 
4168 static mh_search_t
4169 search_hash(mhash_t *mhp, char *special, char **mntpt)
4170 {
4171 	int		idx;
4172 	mcache_t	*mcp;
4173 	const char 	*fcn = "search_hash()";
4174 
4175 	assert(mntpt);
4176 
4177 	*mntpt = NULL;
4178 
4179 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
4180 	if (strchr(special, '/')) {
4181 		bam_error(INVALID_MHASH_KEY, special);
4182 		return (MH_ERROR);
4183 	}
4184 
4185 	idx = mhash_fcn(special);
4186 
4187 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
4188 		if (strcmp(mcp->mc_special, special) == 0)
4189 			break;
4190 	}
4191 
4192 	if (mcp == NULL) {
4193 		BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special));
4194 		return (MH_NOMATCH);
4195 	}
4196 
4197 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
4198 	*mntpt = mcp->mc_mntpt;
4199 	BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special));
4200 	return (MH_MATCH);
4201 }
4202 
4203 static int
4204 check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
4205 {
4206 	char		*sign;
4207 	char		*signline;
4208 	char		signbuf[MAXNAMELEN];
4209 	int		len;
4210 	int		error;
4211 	const char	*fcn = "check_add_ufs_sign_to_list()";
4212 
4213 	/* safe to specify NULL as "osdev" arg for UFS */
4214 	sign = find_existing_sign(mntpt, NULL, "ufs");
4215 	if (sign == NULL) {
4216 		/* No existing signature, nothing to add to list */
4217 		BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt));
4218 		return (0);
4219 	}
4220 
4221 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
4222 	signline = signbuf;
4223 
4224 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
4225 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
4226 	    strlen(GRUBSIGN_UFS_PREFIX))) {
4227 		bam_error(INVALID_UFS_SIGNATURE, sign);
4228 		free(sign);
4229 		/* ignore invalid signatures */
4230 		return (0);
4231 	}
4232 
4233 	len = fputs(signline, tfp);
4234 	error = errno;
4235 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
4236 	if (len != strlen(signline)) {
4237 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
4238 		free(sign);
4239 		return (-1);
4240 	}
4241 
4242 	free(sign);
4243 
4244 	BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt));
4245 	return (0);
4246 }
4247 
4248 /*
4249  * slice is a basename not a full pathname
4250  */
4251 static int
4252 process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
4253 {
4254 	int		ret;
4255 	char		cmd[PATH_MAX];
4256 	char		path[PATH_MAX];
4257 	struct stat	sbuf;
4258 	char		*mntpt;
4259 	filelist_t	flist = {0};
4260 	char		*fstype;
4261 	char		blkslice[PATH_MAX];
4262 	const char	*fcn = "process_slice_common()";
4263 
4264 
4265 	ret = search_hash(mhp, slice, &mntpt);
4266 	switch (ret) {
4267 		case MH_MATCH:
4268 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
4269 				return (-1);
4270 			else
4271 				return (0);
4272 		case MH_NOMATCH:
4273 			break;
4274 		case MH_ERROR:
4275 		default:
4276 			return (-1);
4277 	}
4278 
4279 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
4280 	if (stat(path, &sbuf) == -1) {
4281 		BAM_DPRINTF((D_SLICE_ENOENT, fcn, path));
4282 		return (0);
4283 	}
4284 
4285 	/* Check if ufs */
4286 	(void) snprintf(cmd, sizeof (cmd),
4287 	    "/usr/sbin/fstyp /dev/rdsk/%s 2>/dev/null",
4288 	    slice);
4289 
4290 	if (exec_cmd(cmd, &flist) != 0) {
4291 		if (bam_verbose)
4292 			bam_print(FSTYP_FAILED, slice);
4293 		return (0);
4294 	}
4295 
4296 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
4297 		if (bam_verbose)
4298 			bam_print(FSTYP_BAD, slice);
4299 		filelist_free(&flist);
4300 		return (0);
4301 	}
4302 
4303 	fstype = strtok(flist.head->line, " \t\n");
4304 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
4305 		if (bam_verbose)
4306 			bam_print(NOT_UFS_SLICE, slice, fstype);
4307 		filelist_free(&flist);
4308 		return (0);
4309 	}
4310 
4311 	filelist_free(&flist);
4312 
4313 	/*
4314 	 * Since we are mounting the filesystem read-only, the
4315 	 * the last mount field of the superblock is unchanged
4316 	 * and does not need to be fixed up post-mount;
4317 	 */
4318 
4319 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
4320 	    slice);
4321 
4322 	(void) snprintf(cmd, sizeof (cmd),
4323 	    "/usr/sbin/mount -F ufs -o ro %s %s "
4324 	    "> /dev/null 2>&1", blkslice, tmpmnt);
4325 
4326 	if (exec_cmd(cmd, NULL) != 0) {
4327 		if (bam_verbose)
4328 			bam_print(MOUNT_FAILED, blkslice, "ufs");
4329 		return (0);
4330 	}
4331 
4332 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
4333 
4334 	(void) snprintf(cmd, sizeof (cmd),
4335 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
4336 	    tmpmnt);
4337 
4338 	if (exec_cmd(cmd, NULL) != 0) {
4339 		bam_print(UMOUNT_FAILED, slice);
4340 		return (0);
4341 	}
4342 
4343 	return (ret);
4344 }
4345 
4346 static int
4347 process_vtoc_slices(
4348 	char *s0,
4349 	struct vtoc *vtoc,
4350 	FILE *tfp,
4351 	mhash_t *mhp,
4352 	char *tmpmnt)
4353 {
4354 	int		idx;
4355 	char		slice[PATH_MAX];
4356 	size_t		len;
4357 	char		*cp;
4358 	const char	*fcn = "process_vtoc_slices()";
4359 
4360 	len = strlen(s0);
4361 
4362 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
4363 
4364 	s0[len - 1] = '\0';
4365 
4366 	(void) strlcpy(slice, s0, sizeof (slice));
4367 
4368 	s0[len - 1] = '0';
4369 
4370 	cp = slice + len - 1;
4371 
4372 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
4373 
4374 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
4375 
4376 		if (vtoc->v_part[idx].p_size == 0) {
4377 			BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice));
4378 			continue;
4379 		}
4380 
4381 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
4382 		switch (vtoc->v_part[idx].p_tag) {
4383 		case V_SWAP:
4384 		case V_USR:
4385 		case V_BACKUP:
4386 		case V_VAR:
4387 		case V_HOME:
4388 		case V_ALTSCTR:
4389 			BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice));
4390 			continue;
4391 		default:
4392 			BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice));
4393 			break;
4394 		}
4395 
4396 		/* skip unmountable and readonly slices */
4397 		switch (vtoc->v_part[idx].p_flag) {
4398 		case V_UNMNT:
4399 		case V_RONLY:
4400 			BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice));
4401 			continue;
4402 		default:
4403 			BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice));
4404 			break;
4405 		}
4406 
4407 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
4408 			return (-1);
4409 		}
4410 	}
4411 
4412 	return (0);
4413 }
4414 
4415 static int
4416 process_efi_slices(
4417 	char *s0,
4418 	struct dk_gpt *efi,
4419 	FILE *tfp,
4420 	mhash_t *mhp,
4421 	char *tmpmnt)
4422 {
4423 	int		idx;
4424 	char		slice[PATH_MAX];
4425 	size_t		len;
4426 	char		*cp;
4427 	const char	*fcn = "process_efi_slices()";
4428 
4429 	len = strlen(s0);
4430 
4431 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
4432 
4433 	s0[len - 1] = '\0';
4434 
4435 	(void) strlcpy(slice, s0, sizeof (slice));
4436 
4437 	s0[len - 1] = '0';
4438 
4439 	cp = slice + len - 1;
4440 
4441 	for (idx = 0; idx < efi->efi_nparts; idx++) {
4442 
4443 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
4444 
4445 		if (efi->efi_parts[idx].p_size == 0) {
4446 			BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice));
4447 			continue;
4448 		}
4449 
4450 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
4451 		switch (efi->efi_parts[idx].p_tag) {
4452 		case V_SWAP:
4453 		case V_USR:
4454 		case V_BACKUP:
4455 		case V_VAR:
4456 		case V_HOME:
4457 		case V_ALTSCTR:
4458 			BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice));
4459 			continue;
4460 		default:
4461 			BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice));
4462 			break;
4463 		}
4464 
4465 		/* skip unmountable and readonly slices */
4466 		switch (efi->efi_parts[idx].p_flag) {
4467 		case V_UNMNT:
4468 		case V_RONLY:
4469 			BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice));
4470 			continue;
4471 		default:
4472 			BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice));
4473 			break;
4474 		}
4475 
4476 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
4477 			return (-1);
4478 		}
4479 	}
4480 
4481 	return (0);
4482 }
4483 
4484 /*
4485  * s0 is a basename not a full path
4486  */
4487 static int
4488 process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
4489 {
4490 	struct vtoc		vtoc;
4491 	struct dk_gpt		*efi;
4492 	char			s0path[PATH_MAX];
4493 	struct stat		sbuf;
4494 	int			e_flag;
4495 	int			v_flag;
4496 	int			retval;
4497 	int			err;
4498 	int			fd;
4499 	const char		*fcn = "process_slice0()";
4500 
4501 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
4502 
4503 	if (stat(s0path, &sbuf) == -1) {
4504 		BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path));
4505 		return (0);
4506 	}
4507 
4508 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
4509 	if (fd == -1) {
4510 		bam_error(OPEN_FAIL, s0path, strerror(errno));
4511 		return (0);
4512 	}
4513 
4514 	e_flag = v_flag = 0;
4515 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
4516 	switch (retval) {
4517 		case VT_EIO:
4518 			BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path));
4519 			break;
4520 		case VT_EINVAL:
4521 			BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path));
4522 			break;
4523 		case VT_ERROR:
4524 			BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path));
4525 			break;
4526 		case VT_ENOTSUP:
4527 			e_flag = 1;
4528 			BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path));
4529 			break;
4530 		case 0:
4531 			v_flag = 1;
4532 			BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path));
4533 			break;
4534 		default:
4535 			BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path));
4536 			break;
4537 	}
4538 
4539 
4540 	if (e_flag) {
4541 		e_flag = 0;
4542 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
4543 		switch (retval) {
4544 		case VT_EIO:
4545 			BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path));
4546 			break;
4547 		case VT_EINVAL:
4548 			BAM_DPRINTF((D_EFI_INVALID, fcn, s0path));
4549 			break;
4550 		case VT_ERROR:
4551 			BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path));
4552 			break;
4553 		case VT_ENOTSUP:
4554 			BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path));
4555 			break;
4556 		case 0:
4557 			e_flag = 1;
4558 			BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path));
4559 			break;
4560 		default:
4561 			BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path));
4562 			break;
4563 		}
4564 	}
4565 
4566 	(void) close(fd);
4567 
4568 	if (v_flag) {
4569 		retval = process_vtoc_slices(s0,
4570 		    &vtoc, tfp, mhp, tmpmnt);
4571 	} else if (e_flag) {
4572 		retval = process_efi_slices(s0,
4573 		    efi, tfp, mhp, tmpmnt);
4574 	} else {
4575 		BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path));
4576 		return (0);
4577 	}
4578 
4579 	return (retval);
4580 }
4581 
4582 /*
4583  * Find and create a list of all existing UFS boot signatures
4584  */
4585 static int
4586 FindAllUfsSignatures(void)
4587 {
4588 	mhash_t		*mnttab_hash;
4589 	DIR		*dirp = NULL;
4590 	struct dirent	*dp;
4591 	char		tmpmnt[PATH_MAX];
4592 	char		cmd[PATH_MAX];
4593 	struct stat	sb;
4594 	int		fd;
4595 	FILE		*tfp;
4596 	size_t		len;
4597 	int		ret;
4598 	int		error;
4599 	const char	*fcn = "FindAllUfsSignatures()";
4600 
4601 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
4602 		bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST);
4603 		return (0);
4604 	}
4605 
4606 	fd = open(UFS_SIGNATURE_LIST".tmp",
4607 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
4608 	error = errno;
4609 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
4610 	if (fd == -1) {
4611 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
4612 		return (-1);
4613 	}
4614 
4615 	ret = close(fd);
4616 	error = errno;
4617 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
4618 	if (ret == -1) {
4619 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
4620 		    strerror(error));
4621 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4622 		return (-1);
4623 	}
4624 
4625 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
4626 	error = errno;
4627 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
4628 	if (tfp == NULL) {
4629 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
4630 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4631 		return (-1);
4632 	}
4633 
4634 	mnttab_hash = cache_mnttab();
4635 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
4636 	if (mnttab_hash == NULL) {
4637 		(void) fclose(tfp);
4638 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4639 		bam_error(CACHE_MNTTAB_FAIL, fcn);
4640 		return (-1);
4641 	}
4642 
4643 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
4644 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
4645 	(void) unlink(tmpmnt);
4646 
4647 	ret = mkdirp(tmpmnt, 0755);
4648 	error = errno;
4649 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
4650 	if (ret == -1) {
4651 		bam_error(MKDIR_FAILED, tmpmnt, strerror(error));
4652 		free_mnttab(mnttab_hash);
4653 		(void) fclose(tfp);
4654 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4655 		return (-1);
4656 	}
4657 
4658 	dirp = opendir("/dev/rdsk");
4659 	error = errno;
4660 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
4661 	if (dirp == NULL) {
4662 		bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error));
4663 		goto fail;
4664 	}
4665 
4666 	while (dp = readdir(dirp)) {
4667 		if (strcmp(dp->d_name, ".") == 0 ||
4668 		    strcmp(dp->d_name, "..") == 0)
4669 			continue;
4670 
4671 		/*
4672 		 * we only look for the s0 slice. This is guranteed to
4673 		 * have 's' at len - 2.
4674 		 */
4675 		len = strlen(dp->d_name);
4676 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
4677 			BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name));
4678 			continue;
4679 		}
4680 
4681 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
4682 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
4683 		if (ret == -1)
4684 			goto fail;
4685 	}
4686 
4687 	(void) closedir(dirp);
4688 	free_mnttab(mnttab_hash);
4689 	(void) rmdir(tmpmnt);
4690 
4691 	ret = fclose(tfp);
4692 	error = errno;
4693 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
4694 	if (ret == EOF) {
4695 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
4696 		    strerror(error));
4697 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4698 		return (-1);
4699 	}
4700 
4701 	/* We have a list of existing GRUB signatures. Sort it first */
4702 	(void) snprintf(cmd, sizeof (cmd),
4703 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
4704 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
4705 
4706 	ret = exec_cmd(cmd, NULL);
4707 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
4708 	if (ret != 0) {
4709 		bam_error(GRUBSIGN_SORT_FAILED);
4710 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
4711 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
4712 		return (-1);
4713 	}
4714 
4715 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
4716 
4717 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
4718 	error = errno;
4719 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
4720 	if (ret == -1) {
4721 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
4722 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
4723 		return (-1);
4724 	}
4725 
4726 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
4727 		BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST));
4728 	}
4729 
4730 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4731 	return (0);
4732 
4733 fail:
4734 	if (dirp)
4735 		(void) closedir(dirp);
4736 	free_mnttab(mnttab_hash);
4737 	(void) rmdir(tmpmnt);
4738 	(void) fclose(tfp);
4739 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
4740 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
4741 	return (-1);
4742 }
4743 
4744 static char *
4745 create_ufs_sign(void)
4746 {
4747 	struct stat	sb;
4748 	int		signnum = -1;
4749 	char		tmpsign[MAXNAMELEN + 1];
4750 	char		*numstr;
4751 	int		i;
4752 	FILE		*tfp;
4753 	int		ret;
4754 	int		error;
4755 	const char	*fcn = "create_ufs_sign()";
4756 
4757 	bam_print(SEARCHING_UFS_SIGN);
4758 
4759 	ret = FindAllUfsSignatures();
4760 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
4761 	if (ret == -1) {
4762 		bam_error(ERR_FIND_UFS_SIGN);
4763 		return (NULL);
4764 	}
4765 
4766 	/* Make sure the list exists and is owned by root */
4767 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
4768 	    (void) unlink(UFS_SIGNATURE_LIST));
4769 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
4770 		(void) unlink(UFS_SIGNATURE_LIST);
4771 		bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST);
4772 		return (NULL);
4773 	}
4774 
4775 	if (sb.st_size == 0) {
4776 		bam_print(GRUBSIGN_UFS_NONE);
4777 		i = 0;
4778 		goto found;
4779 	}
4780 
4781 	/* The signature list was sorted when it was created */
4782 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
4783 	error = errno;
4784 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
4785 	if (tfp == NULL) {
4786 		bam_error(UFS_SIGNATURE_LIST_OPENERR,
4787 		    UFS_SIGNATURE_LIST, strerror(error));
4788 		(void) unlink(UFS_SIGNATURE_LIST);
4789 		return (NULL);
4790 	}
4791 
4792 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
4793 
4794 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
4795 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
4796 			(void) fclose(tfp);
4797 			(void) unlink(UFS_SIGNATURE_LIST);
4798 			bam_error(UFS_BADSIGN, tmpsign);
4799 			return (NULL);
4800 		}
4801 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
4802 
4803 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
4804 			(void) fclose(tfp);
4805 			(void) unlink(UFS_SIGNATURE_LIST);
4806 			bam_error(UFS_BADSIGN, tmpsign);
4807 			return (NULL);
4808 		}
4809 
4810 		signnum = atoi(numstr);
4811 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
4812 		if (signnum < 0) {
4813 			(void) fclose(tfp);
4814 			(void) unlink(UFS_SIGNATURE_LIST);
4815 			bam_error(UFS_BADSIGN, tmpsign);
4816 			return (NULL);
4817 		}
4818 
4819 		if (i != signnum) {
4820 			BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i));
4821 			break;
4822 		}
4823 	}
4824 
4825 	(void) fclose(tfp);
4826 
4827 found:
4828 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
4829 
4830 	/* add the ufs signature to the /var/run list of signatures */
4831 	ret = ufs_add_to_sign_list(tmpsign);
4832 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
4833 	if (ret == -1) {
4834 		(void) unlink(UFS_SIGNATURE_LIST);
4835 		bam_error(FAILED_ADD_SIGNLIST, tmpsign);
4836 		return (NULL);
4837 	}
4838 
4839 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4840 
4841 	return (s_strdup(tmpsign));
4842 }
4843 
4844 static char *
4845 get_fstype(char *osroot)
4846 {
4847 	FILE		*mntfp;
4848 	struct mnttab	mp = {0};
4849 	struct mnttab	mpref = {0};
4850 	int		error;
4851 	int		ret;
4852 	const char	*fcn = "get_fstype()";
4853 
4854 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
4855 	if (osroot == NULL) {
4856 		bam_error(GET_FSTYPE_ARGS);
4857 		return (NULL);
4858 	}
4859 
4860 	mntfp = fopen(MNTTAB, "r");
4861 	error = errno;
4862 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
4863 	if (mntfp == NULL) {
4864 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
4865 		return (NULL);
4866 	}
4867 
4868 	if (*osroot == '\0')
4869 		mpref.mnt_mountp = "/";
4870 	else
4871 		mpref.mnt_mountp = osroot;
4872 
4873 	ret = getmntany(mntfp, &mp, &mpref);
4874 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
4875 	if (ret != 0) {
4876 		bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB);
4877 		(void) fclose(mntfp);
4878 		return (NULL);
4879 	}
4880 	(void) fclose(mntfp);
4881 
4882 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
4883 	if (mp.mnt_fstype == NULL) {
4884 		bam_error(MNTTAB_FSTYPE_NULL, osroot);
4885 		return (NULL);
4886 	}
4887 
4888 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4889 
4890 	return (s_strdup(mp.mnt_fstype));
4891 }
4892 
4893 static char *
4894 create_zfs_sign(char *osdev)
4895 {
4896 	char		tmpsign[PATH_MAX];
4897 	char		*pool;
4898 	const char	*fcn = "create_zfs_sign()";
4899 
4900 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev));
4901 
4902 	/*
4903 	 * First find the pool name
4904 	 */
4905 	pool = get_pool(osdev);
4906 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
4907 	if (pool == NULL) {
4908 		bam_error(GET_POOL_FAILED, osdev);
4909 		return (NULL);
4910 	}
4911 
4912 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
4913 
4914 	BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign));
4915 
4916 	free(pool);
4917 
4918 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4919 
4920 	return (s_strdup(tmpsign));
4921 }
4922 
4923 static char *
4924 create_new_sign(char *osdev, char *fstype)
4925 {
4926 	char		*sign;
4927 	const char	*fcn = "create_new_sign()";
4928 
4929 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
4930 
4931 	if (strcmp(fstype, "zfs") == 0) {
4932 		BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn));
4933 		sign = create_zfs_sign(osdev);
4934 	} else if (strcmp(fstype, "ufs") == 0) {
4935 		BAM_DPRINTF((D_CREATE_NEW_UFS, fcn));
4936 		sign = create_ufs_sign();
4937 	} else {
4938 		bam_error(GRUBSIGN_NOTSUP, fstype);
4939 		sign = NULL;
4940 	}
4941 
4942 	BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>"));
4943 	return (sign);
4944 }
4945 
4946 static int
4947 set_backup_common(char *mntpt, char *sign)
4948 {
4949 	FILE		*bfp;
4950 	char		backup[PATH_MAX];
4951 	char		tmpsign[PATH_MAX];
4952 	int		error;
4953 	char		*bdir;
4954 	char		*backup_dup;
4955 	struct stat	sb;
4956 	int		ret;
4957 	const char	*fcn = "set_backup_common()";
4958 
4959 	(void) snprintf(backup, sizeof (backup), "%s%s",
4960 	    mntpt, GRUBSIGN_BACKUP);
4961 
4962 	/* First read the backup */
4963 	bfp = fopen(backup, "r");
4964 	if (bfp != NULL) {
4965 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
4966 			if (strcmp(tmpsign, sign) == 0) {
4967 				BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign));
4968 				(void) fclose(bfp);
4969 				return (0);
4970 			}
4971 		}
4972 		(void) fclose(bfp);
4973 		BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign));
4974 	} else {
4975 		BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup));
4976 	}
4977 
4978 	/*
4979 	 * Didn't find the correct signature. First create
4980 	 * the directory if necessary.
4981 	 */
4982 
4983 	/* dirname() modifies its argument so dup it */
4984 	backup_dup = s_strdup(backup);
4985 	bdir = dirname(backup_dup);
4986 	assert(bdir);
4987 
4988 	ret = stat(bdir, &sb);
4989 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
4990 	if (ret == -1) {
4991 		BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir));
4992 		ret = mkdirp(bdir, 0755);
4993 		error = errno;
4994 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
4995 		if (ret == -1) {
4996 			bam_error(GRUBSIGN_BACKUP_MKDIRERR,
4997 			    GRUBSIGN_BACKUP, strerror(error));
4998 			free(backup_dup);
4999 			return (-1);
5000 		}
5001 	}
5002 	free(backup_dup);
5003 
5004 	/*
5005 	 * Open the backup in append mode to add the correct
5006 	 * signature;
5007 	 */
5008 	bfp = fopen(backup, "a");
5009 	error = errno;
5010 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
5011 	if (bfp == NULL) {
5012 		bam_error(GRUBSIGN_BACKUP_OPENERR,
5013 		    GRUBSIGN_BACKUP, strerror(error));
5014 		return (-1);
5015 	}
5016 
5017 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
5018 
5019 	ret = fputs(tmpsign, bfp);
5020 	error = errno;
5021 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
5022 	if (ret != strlen(tmpsign)) {
5023 		bam_error(GRUBSIGN_BACKUP_WRITEERR,
5024 		    GRUBSIGN_BACKUP, strerror(error));
5025 		(void) fclose(bfp);
5026 		return (-1);
5027 	}
5028 
5029 	(void) fclose(bfp);
5030 
5031 	if (bam_verbose)
5032 		bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP);
5033 
5034 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5035 
5036 	return (0);
5037 }
5038 
5039 static int
5040 set_backup_ufs(char *osroot, char *sign)
5041 {
5042 	const char	*fcn = "set_backup_ufs()";
5043 
5044 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
5045 	return (set_backup_common(osroot, sign));
5046 }
5047 
5048 static int
5049 set_backup_zfs(char *osdev, char *sign)
5050 {
5051 	char		*pool;
5052 	char		*mntpt;
5053 	zfs_mnted_t	mnted;
5054 	int		ret;
5055 	const char	*fcn = "set_backup_zfs()";
5056 
5057 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
5058 
5059 	pool = get_pool(osdev);
5060 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
5061 	if (pool == NULL) {
5062 		bam_error(GET_POOL_FAILED, osdev);
5063 		return (-1);
5064 	}
5065 
5066 	mntpt = mount_top_dataset(pool, &mnted);
5067 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
5068 	if (mntpt == NULL) {
5069 		bam_error(FAIL_MNT_TOP_DATASET, pool);
5070 		free(pool);
5071 		return (-1);
5072 	}
5073 
5074 	ret = set_backup_common(mntpt, sign);
5075 
5076 	(void) umount_top_dataset(pool, mnted, mntpt);
5077 
5078 	free(pool);
5079 
5080 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
5081 	if (ret == 0) {
5082 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5083 	} else {
5084 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5085 	}
5086 
5087 	return (ret);
5088 }
5089 
5090 static int
5091 set_backup(char *osroot, char *osdev, char *sign, char *fstype)
5092 {
5093 	const char	*fcn = "set_backup()";
5094 	int		ret;
5095 
5096 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
5097 
5098 	if (strcmp(fstype, "ufs") == 0) {
5099 		BAM_DPRINTF((D_SET_BACKUP_UFS, fcn));
5100 		ret = set_backup_ufs(osroot, sign);
5101 	} else if (strcmp(fstype, "zfs") == 0) {
5102 		BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn));
5103 		ret = set_backup_zfs(osdev, sign);
5104 	} else {
5105 		bam_error(GRUBSIGN_NOTSUP, fstype);
5106 		ret = -1;
5107 	}
5108 
5109 	if (ret == 0) {
5110 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5111 	} else {
5112 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5113 	}
5114 
5115 	return (ret);
5116 }
5117 
5118 static int
5119 set_primary_common(char *mntpt, char *sign)
5120 {
5121 	char		signfile[PATH_MAX];
5122 	char		signdir[PATH_MAX];
5123 	struct stat	sb;
5124 	int		fd;
5125 	int		error;
5126 	int		ret;
5127 	const char	*fcn = "set_primary_common()";
5128 
5129 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
5130 	    mntpt, GRUBSIGN_DIR, sign);
5131 
5132 	if (stat(signfile, &sb) != -1) {
5133 		if (bam_verbose)
5134 			bam_print(PRIMARY_SIGN_EXISTS, sign);
5135 		return (0);
5136 	} else {
5137 		BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile));
5138 	}
5139 
5140 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
5141 	    mntpt, GRUBSIGN_DIR);
5142 
5143 	if (stat(signdir, &sb) == -1) {
5144 		BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir));
5145 		ret = mkdirp(signdir, 0755);
5146 		error = errno;
5147 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
5148 		if (ret == -1) {
5149 			bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno));
5150 			return (-1);
5151 		}
5152 	}
5153 
5154 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
5155 	error = errno;
5156 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
5157 	if (fd == -1) {
5158 		bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error));
5159 		return (-1);
5160 	}
5161 
5162 	ret = fsync(fd);
5163 	error = errno;
5164 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
5165 	if (ret != 0) {
5166 		bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error));
5167 	}
5168 
5169 	(void) close(fd);
5170 
5171 	if (bam_verbose)
5172 		bam_print(GRUBSIGN_CREATED_PRIMARY, signfile);
5173 
5174 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5175 
5176 	return (0);
5177 }
5178 
5179 static int
5180 set_primary_ufs(char *osroot, char *sign)
5181 {
5182 	const char	*fcn = "set_primary_ufs()";
5183 
5184 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
5185 	return (set_primary_common(osroot, sign));
5186 }
5187 
5188 static int
5189 set_primary_zfs(char *osdev, char *sign)
5190 {
5191 	char		*pool;
5192 	char		*mntpt;
5193 	zfs_mnted_t	mnted;
5194 	int		ret;
5195 	const char	*fcn = "set_primary_zfs()";
5196 
5197 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
5198 
5199 	pool = get_pool(osdev);
5200 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
5201 	if (pool == NULL) {
5202 		bam_error(GET_POOL_FAILED, osdev);
5203 		return (-1);
5204 	}
5205 
5206 	/* Pool name must exist in the sign */
5207 	ret = (strstr(sign, pool) != NULL);
5208 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
5209 	if (ret == 0) {
5210 		bam_error(POOL_SIGN_INCOMPAT, pool, sign);
5211 		free(pool);
5212 		return (-1);
5213 	}
5214 
5215 	mntpt = mount_top_dataset(pool, &mnted);
5216 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
5217 	if (mntpt == NULL) {
5218 		bam_error(FAIL_MNT_TOP_DATASET, pool);
5219 		free(pool);
5220 		return (-1);
5221 	}
5222 
5223 	ret = set_primary_common(mntpt, sign);
5224 
5225 	(void) umount_top_dataset(pool, mnted, mntpt);
5226 
5227 	free(pool);
5228 
5229 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
5230 	if (ret == 0) {
5231 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5232 	} else {
5233 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5234 	}
5235 
5236 	return (ret);
5237 }
5238 
5239 static int
5240 set_primary(char *osroot, char *osdev, char *sign, char *fstype)
5241 {
5242 	const char	*fcn = "set_primary()";
5243 	int		ret;
5244 
5245 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
5246 	if (strcmp(fstype, "ufs") == 0) {
5247 		BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn));
5248 		ret = set_primary_ufs(osroot, sign);
5249 	} else if (strcmp(fstype, "zfs") == 0) {
5250 		BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn));
5251 		ret = set_primary_zfs(osdev, sign);
5252 	} else {
5253 		bam_error(GRUBSIGN_NOTSUP, fstype);
5254 		ret = -1;
5255 	}
5256 
5257 	if (ret == 0) {
5258 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5259 	} else {
5260 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5261 	}
5262 
5263 	return (ret);
5264 }
5265 
5266 static int
5267 ufs_add_to_sign_list(char *sign)
5268 {
5269 	FILE		*tfp;
5270 	char		signline[MAXNAMELEN];
5271 	char		cmd[PATH_MAX];
5272 	int		ret;
5273 	int		error;
5274 	const char	*fcn = "ufs_add_to_sign_list()";
5275 
5276 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
5277 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
5278 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
5279 		bam_error(INVALID_UFS_SIGN, sign);
5280 		(void) unlink(UFS_SIGNATURE_LIST);
5281 		return (-1);
5282 	}
5283 
5284 	/*
5285 	 * most failures in this routine are not a fatal error
5286 	 * We simply unlink the /var/run file and continue
5287 	 */
5288 
5289 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
5290 	error = errno;
5291 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
5292 	if (ret == -1) {
5293 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp",
5294 		    strerror(error));
5295 		(void) unlink(UFS_SIGNATURE_LIST);
5296 		return (0);
5297 	}
5298 
5299 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
5300 	error = errno;
5301 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
5302 	if (tfp == NULL) {
5303 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
5304 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5305 		return (0);
5306 	}
5307 
5308 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
5309 
5310 	ret = fputs(signline, tfp);
5311 	error = errno;
5312 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
5313 	if (ret != strlen(signline)) {
5314 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
5315 		(void) fclose(tfp);
5316 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5317 		return (0);
5318 	}
5319 
5320 	ret = fclose(tfp);
5321 	error = errno;
5322 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
5323 	if (ret == EOF) {
5324 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
5325 		    strerror(error));
5326 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5327 		return (0);
5328 	}
5329 
5330 	/* Sort the list again */
5331 	(void) snprintf(cmd, sizeof (cmd),
5332 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
5333 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
5334 
5335 	ret = exec_cmd(cmd, NULL);
5336 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
5337 	if (ret != 0) {
5338 		bam_error(GRUBSIGN_SORT_FAILED);
5339 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
5340 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5341 		return (0);
5342 	}
5343 
5344 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
5345 
5346 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
5347 	error = errno;
5348 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
5349 	if (ret == -1) {
5350 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
5351 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
5352 		return (0);
5353 	}
5354 
5355 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5356 
5357 	return (0);
5358 }
5359 
5360 static int
5361 set_signature(char *osroot, char *osdev, char *sign, char *fstype)
5362 {
5363 	int		ret;
5364 	const char	*fcn = "set_signature()";
5365 
5366 	BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype));
5367 
5368 	ret = set_backup(osroot, osdev, sign, fstype);
5369 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
5370 	if (ret == -1) {
5371 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5372 		bam_error(SET_BACKUP_FAILED, sign, osroot, osdev);
5373 		return (-1);
5374 	}
5375 
5376 	ret = set_primary(osroot, osdev, sign, fstype);
5377 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
5378 
5379 	if (ret == 0) {
5380 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5381 	} else {
5382 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5383 		bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev);
5384 
5385 	}
5386 	return (ret);
5387 }
5388 
5389 char *
5390 get_grubsign(char *osroot, char *osdev)
5391 {
5392 	char		*grubsign;	/* (<sign>,#,#) */
5393 	char		*slice;
5394 	int		fdiskpart;
5395 	char		*sign;
5396 	char		*fstype;
5397 	int		ret;
5398 	const char	*fcn = "get_grubsign()";
5399 
5400 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev));
5401 
5402 	fstype = get_fstype(osroot);
5403 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
5404 	if (fstype == NULL) {
5405 		bam_error(GET_FSTYPE_FAILED, osroot);
5406 		return (NULL);
5407 	}
5408 
5409 	sign = find_existing_sign(osroot, osdev, fstype);
5410 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
5411 	if (sign == NULL) {
5412 		BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev));
5413 		sign = create_new_sign(osdev, fstype);
5414 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
5415 		if (sign == NULL) {
5416 			bam_error(GRUBSIGN_CREATE_FAIL, osdev);
5417 			free(fstype);
5418 			return (NULL);
5419 		}
5420 	}
5421 
5422 	ret = set_signature(osroot, osdev, sign, fstype);
5423 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
5424 	if (ret == -1) {
5425 		bam_error(GRUBSIGN_WRITE_FAIL, osdev);
5426 		free(sign);
5427 		free(fstype);
5428 		(void) unlink(UFS_SIGNATURE_LIST);
5429 		return (NULL);
5430 	}
5431 
5432 	free(fstype);
5433 
5434 	if (bam_verbose)
5435 		bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev);
5436 
5437 	fdiskpart = get_partition(osdev);
5438 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = -1);
5439 	if (fdiskpart == -1) {
5440 		bam_error(FDISKPART_FAIL, osdev);
5441 		free(sign);
5442 		return (NULL);
5443 	}
5444 
5445 	slice = strrchr(osdev, 's');
5446 
5447 	grubsign = s_calloc(1, MAXNAMELEN + 10);
5448 	if (slice) {
5449 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
5450 		    sign, fdiskpart, slice[1] + 'a' - '0');
5451 	} else
5452 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
5453 		    sign, fdiskpart);
5454 
5455 	free(sign);
5456 
5457 	BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign));
5458 
5459 	return (grubsign);
5460 }
5461 
5462 static char *
5463 get_title(char *rootdir)
5464 {
5465 	static char	title[80];
5466 	char		*cp = NULL;
5467 	char		release[PATH_MAX];
5468 	FILE		*fp;
5469 	const char	*fcn = "get_title()";
5470 
5471 	/* open the /etc/release file */
5472 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
5473 
5474 	fp = fopen(release, "r");
5475 	if (fp == NULL) {
5476 		bam_error(OPEN_FAIL, release, strerror(errno));
5477 		cp = NULL;
5478 		goto out;
5479 	}
5480 
5481 	while (s_fgets(title, sizeof (title), fp) != NULL) {
5482 		cp = strstr(title, "Solaris");
5483 		if (cp)
5484 			break;
5485 	}
5486 	(void) fclose(fp);
5487 
5488 out:
5489 	cp = cp ? cp : "Solaris";
5490 
5491 	BAM_DPRINTF((D_GET_TITLE, fcn, cp));
5492 
5493 	return (cp);
5494 }
5495 
5496 char *
5497 get_special(char *mountp)
5498 {
5499 	FILE		*mntfp;
5500 	struct mnttab	mp = {0};
5501 	struct mnttab	mpref = {0};
5502 	int		error;
5503 	int		ret;
5504 	const char 	*fcn = "get_special()";
5505 
5506 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
5507 	if (mountp == NULL) {
5508 		bam_error(GET_SPECIAL_NULL_MNTPT);
5509 		return (NULL);
5510 	}
5511 
5512 	mntfp = fopen(MNTTAB, "r");
5513 	error = errno;
5514 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
5515 	if (mntfp == NULL) {
5516 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5517 		return (NULL);
5518 	}
5519 
5520 	if (*mountp == '\0')
5521 		mpref.mnt_mountp = "/";
5522 	else
5523 		mpref.mnt_mountp = mountp;
5524 
5525 	ret = getmntany(mntfp, &mp, &mpref);
5526 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
5527 	if (ret != 0) {
5528 		(void) fclose(mntfp);
5529 		BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp));
5530 		return (NULL);
5531 	}
5532 	(void) fclose(mntfp);
5533 
5534 	BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special));
5535 
5536 	return (s_strdup(mp.mnt_special));
5537 }
5538 
5539 static void
5540 free_physarray(char **physarray, int n)
5541 {
5542 	int			i;
5543 	const char		*fcn = "free_physarray()";
5544 
5545 	assert(physarray);
5546 	assert(n);
5547 
5548 	BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n));
5549 
5550 	for (i = 0; i < n; i++) {
5551 		free(physarray[i]);
5552 	}
5553 	free(physarray);
5554 
5555 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5556 }
5557 
5558 static int
5559 zfs_get_physical(char *special, char ***physarray, int *n)
5560 {
5561 	char			sdup[PATH_MAX];
5562 	char			cmd[PATH_MAX];
5563 	char			dsk[PATH_MAX];
5564 	char			*pool;
5565 	filelist_t		flist = {0};
5566 	line_t			*lp;
5567 	line_t			*startlp;
5568 	char			*comp1;
5569 	int			i;
5570 	int			ret;
5571 	const char		*fcn = "zfs_get_physical()";
5572 
5573 	assert(special);
5574 
5575 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
5576 
5577 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
5578 	if (special[0] == '/') {
5579 		bam_error(INVALID_ZFS_SPECIAL, special);
5580 		return (-1);
5581 	}
5582 
5583 	(void) strlcpy(sdup, special, sizeof (sdup));
5584 
5585 	pool = strtok(sdup, "/");
5586 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
5587 	if (pool == NULL) {
5588 		bam_error(CANT_FIND_POOL_FROM_SPECIAL, special);
5589 		return (-1);
5590 	}
5591 
5592 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
5593 
5594 	ret = exec_cmd(cmd, &flist);
5595 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
5596 	if (ret != 0) {
5597 		bam_error(ZFS_GET_POOL_STATUS, pool);
5598 		return (-1);
5599 	}
5600 
5601 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
5602 	if (flist.head == NULL) {
5603 		bam_error(BAD_ZPOOL_STATUS, pool);
5604 		filelist_free(&flist);
5605 		return (-1);
5606 	}
5607 
5608 	for (lp = flist.head; lp; lp = lp->next) {
5609 		BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line));
5610 		comp1 = strtok(lp->line, " \t");
5611 		if (comp1 == NULL) {
5612 			free(lp->line);
5613 			lp->line = NULL;
5614 		} else {
5615 			comp1 = s_strdup(comp1);
5616 			free(lp->line);
5617 			lp->line = comp1;
5618 		}
5619 	}
5620 
5621 	for (lp = flist.head; lp; lp = lp->next) {
5622 		if (lp->line == NULL)
5623 			continue;
5624 		if (strcmp(lp->line, pool) == 0) {
5625 			BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool));
5626 			break;
5627 		}
5628 	}
5629 
5630 	if (lp == NULL) {
5631 		bam_error(NO_POOL_IN_ZPOOL_STATUS, pool);
5632 		filelist_free(&flist);
5633 		return (-1);
5634 	}
5635 
5636 	startlp = lp->next;
5637 	for (i = 0, lp = startlp; lp; lp = lp->next) {
5638 		if (lp->line == NULL)
5639 			continue;
5640 		if (strcmp(lp->line, "mirror") == 0)
5641 			continue;
5642 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
5643 			break;
5644 		i++;
5645 		BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i));
5646 	}
5647 
5648 	if (i == 0) {
5649 		bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool);
5650 		filelist_free(&flist);
5651 		return (-1);
5652 	}
5653 
5654 	*n = i;
5655 	*physarray = s_calloc(*n, sizeof (char *));
5656 	for (i = 0, lp = startlp; lp; lp = lp->next) {
5657 		if (lp->line == NULL)
5658 			continue;
5659 		if (strcmp(lp->line, "mirror") == 0)
5660 			continue;
5661 		if (strcmp(lp->line, "errors:") == 0)
5662 			break;
5663 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5664 		    strncmp(lp->line, "/dev/rdsk/",
5665 		    strlen("/dev/rdsk/")) != 0)  {
5666 			(void) snprintf(dsk, sizeof (dsk), "/dev/dsk/%s",
5667 			    lp->line);
5668 		} else {
5669 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
5670 		}
5671 		BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool));
5672 		(*physarray)[i++] = s_strdup(dsk);
5673 	}
5674 
5675 	assert(i == *n);
5676 
5677 	filelist_free(&flist);
5678 
5679 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5680 	return (0);
5681 }
5682 
5683 /*
5684  * Certain services needed to run metastat successfully may not
5685  * be enabled. Enable them now.
5686  */
5687 /*
5688  * Checks if the specified service is online
5689  * Returns: 	1 if the service is online
5690  *		0 if the service is not online
5691  *		-1 on error
5692  */
5693 static int
5694 is_svc_online(char *svc)
5695 {
5696 	char			*state;
5697 	const char		*fcn = "is_svc_online()";
5698 
5699 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, svc));
5700 
5701 	state = smf_get_state(svc);
5702 	INJECT_ERROR2("GET_SVC_STATE", free(state), state = NULL);
5703 	if (state == NULL) {
5704 		bam_error(GET_SVC_STATE_ERR, svc);
5705 		return (-1);
5706 	}
5707 	BAM_DPRINTF((D_GOT_SVC_STATUS, fcn, svc));
5708 
5709 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
5710 		BAM_DPRINTF((D_SVC_ONLINE, fcn, svc));
5711 		free(state);
5712 		return (1);
5713 	}
5714 
5715 	BAM_DPRINTF((D_SVC_NOT_ONLINE, fcn, state, svc));
5716 
5717 	free(state);
5718 
5719 	return (0);
5720 }
5721 
5722 static int
5723 enable_svc(char *svc)
5724 {
5725 	int			ret;
5726 	int			sleeptime;
5727 	const char		*fcn = "enable_svc()";
5728 
5729 	ret = is_svc_online(svc);
5730 	if (ret == -1) {
5731 		bam_error(SVC_IS_ONLINE_FAILED, svc);
5732 		return (-1);
5733 	} else if (ret == 1) {
5734 		BAM_DPRINTF((D_SVC_ALREADY_ONLINE, fcn, svc));
5735 		return (0);
5736 	}
5737 
5738 	/* Service is not enabled. Enable it now. */
5739 	ret = smf_enable_instance(svc, 0);
5740 	INJECT_ERROR1("ENABLE_SVC_FAILED", ret = -1);
5741 	if (ret != 0) {
5742 		bam_error(ENABLE_SVC_FAILED, svc);
5743 		return (-1);
5744 	}
5745 
5746 	BAM_DPRINTF((D_SVC_ONLINE_INITIATED, fcn, svc));
5747 
5748 	sleeptime = 0;
5749 	do {
5750 		ret = is_svc_online(svc);
5751 		INJECT_ERROR1("SVC_ONLINE_SUCCESS", ret = 1);
5752 		INJECT_ERROR1("SVC_ONLINE_FAILURE", ret = -1);
5753 		INJECT_ERROR1("SVC_ONLINE_NOTYET", ret = 0);
5754 		if (ret == -1) {
5755 			bam_error(ERR_SVC_GET_ONLINE, svc);
5756 			return (-1);
5757 		} else if (ret == 1) {
5758 			BAM_DPRINTF((D_SVC_NOW_ONLINE, fcn, svc));
5759 			return (1);
5760 		}
5761 		(void) sleep(1);
5762 	} while (sleeptime < 60);
5763 
5764 	bam_error(TIMEOUT_ENABLE_SVC, svc);
5765 
5766 	return (-1);
5767 }
5768 
5769 static int
5770 ufs_get_physical(char *special, char ***physarray, int *n)
5771 {
5772 	char			cmd[PATH_MAX];
5773 	char			*shortname;
5774 	filelist_t		flist = {0};
5775 	char			*meta;
5776 	char			*type;
5777 	char			*comp1;
5778 	char			*comp2;
5779 	char			*comp3;
5780 	char			*comp4;
5781 	int			i;
5782 	line_t			*lp;
5783 	int			ret;
5784 	char			*svc;
5785 	const char		*fcn = "ufs_get_physical()";
5786 
5787 	assert(special);
5788 
5789 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
5790 
5791 	if (strncmp(special, "/dev/md/", strlen("/dev/md/")) != 0) {
5792 		bam_error(UFS_GET_PHYS_NOT_SVM, special);
5793 		return (-1);
5794 	}
5795 
5796 	if (strncmp(special, "/dev/md/dsk/", strlen("/dev/md/dsk/")) == 0) {
5797 		shortname = special + strlen("/dev/md/dsk/");
5798 	} else if (strncmp(special, "/dev/md/rdsk/",
5799 	    strlen("/dev/md/rdsk/")) == 0) {
5800 		shortname = special + strlen("/dev/md/rdsk");
5801 	} else {
5802 		bam_error(UFS_GET_PHYS_INVALID_SVM, special);
5803 		return (-1);
5804 	}
5805 
5806 	BAM_DPRINTF((D_UFS_SVM_SHORT, fcn, special, shortname));
5807 
5808 	svc = "network/rpc/meta:default";
5809 	if (enable_svc(svc) == -1) {
5810 		bam_error(UFS_SVM_METASTAT_SVC_ERR, svc);
5811 	}
5812 
5813 	(void) snprintf(cmd, sizeof (cmd), "/sbin/metastat -p %s", shortname);
5814 
5815 	ret = exec_cmd(cmd, &flist);
5816 	INJECT_ERROR1("UFS_SVM_METASTAT", ret = 1);
5817 	if (ret != 0) {
5818 		bam_error(UFS_SVM_METASTAT_ERR, shortname);
5819 		return (-1);
5820 	}
5821 
5822 	INJECT_ERROR1("UFS_SVM_METASTAT_OUT", flist.head = NULL);
5823 	if (flist.head == NULL) {
5824 		bam_error(BAD_UFS_SVM_METASTAT, shortname);
5825 		filelist_free(&flist);
5826 		return (-1);
5827 	}
5828 
5829 	/*
5830 	 * Check if not a mirror. We only parse a single metadevice
5831 	 * if not a mirror
5832 	 */
5833 	meta = strtok(flist.head->line, " \t");
5834 	type = strtok(NULL, " \t");
5835 	if (meta == NULL || type == NULL) {
5836 		bam_error(ERROR_PARSE_UFS_SVM_METASTAT, shortname);
5837 		filelist_free(&flist);
5838 		return (-1);
5839 	}
5840 	if (strcmp(type, "-m") != 0) {
5841 		comp1 = strtok(NULL, " \t");
5842 		comp2 = strtok(NULL, " \t");
5843 		if (comp1 == NULL || comp2 != NULL) {
5844 			bam_error(INVALID_UFS_SVM_METASTAT, shortname);
5845 			filelist_free(&flist);
5846 			return (-1);
5847 		}
5848 		BAM_DPRINTF((D_UFS_SVM_ONE_COMP, fcn, comp1, shortname));
5849 		*physarray = s_calloc(1, sizeof (char *));
5850 		(*physarray)[0] = s_strdup(comp1);
5851 		*n = 1;
5852 		filelist_free(&flist);
5853 		return (0);
5854 	}
5855 
5856 	/*
5857 	 * Okay we have a mirror. Everything after the first line
5858 	 * is a submirror
5859 	 */
5860 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
5861 		if (strstr(lp->line, "/dev/dsk/") == NULL &&
5862 		    strstr(lp->line, "/dev/rdsk/") == NULL) {
5863 			bam_error(CANNOT_PARSE_UFS_SVM_METASTAT, shortname);
5864 			filelist_free(&flist);
5865 			return (-1);
5866 		}
5867 		i++;
5868 	}
5869 
5870 	*physarray = s_calloc(i, sizeof (char *));
5871 	*n = i;
5872 
5873 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
5874 		comp1 = strtok(lp->line, " \t");
5875 		comp2 = strtok(NULL, " \t");
5876 		comp3 = strtok(NULL, " \t");
5877 		comp4 = strtok(NULL, " \t");
5878 
5879 		if (comp3 == NULL || comp4 == NULL ||
5880 		    (strncmp(comp4, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5881 		    strncmp(comp4, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0)) {
5882 			bam_error(CANNOT_PARSE_UFS_SVM_SUBMIRROR, shortname);
5883 			filelist_free(&flist);
5884 			free_physarray(*physarray, *n);
5885 			return (-1);
5886 		}
5887 
5888 		(*physarray)[i++] = s_strdup(comp4);
5889 	}
5890 
5891 	assert(i == *n);
5892 
5893 	filelist_free(&flist);
5894 
5895 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5896 	return (0);
5897 }
5898 
5899 static int
5900 get_physical(char *menu_root, char ***physarray, int *n)
5901 {
5902 	char			*special;
5903 	int			ret;
5904 	const char		*fcn = "get_physical()";
5905 
5906 	assert(menu_root);
5907 	assert(physarray);
5908 	assert(n);
5909 
5910 	*physarray = NULL;
5911 	*n = 0;
5912 
5913 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root));
5914 
5915 	/* First get the device special file from /etc/mnttab */
5916 	special = get_special(menu_root);
5917 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
5918 	if (special == NULL) {
5919 		bam_error(GET_SPECIAL_NULL, menu_root);
5920 		return (-1);
5921 	}
5922 
5923 	/* If already a physical device nothing to do */
5924 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
5925 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
5926 		BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special));
5927 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
5928 		*physarray = s_calloc(1, sizeof (char *));
5929 		(*physarray)[0] = special;
5930 		*n = 1;
5931 		return (0);
5932 	}
5933 
5934 	if (is_zfs(menu_root)) {
5935 		ret = zfs_get_physical(special, physarray, n);
5936 	} else if (is_ufs(menu_root)) {
5937 		ret = ufs_get_physical(special, physarray, n);
5938 	} else {
5939 		bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special);
5940 		ret = -1;
5941 	}
5942 
5943 	free(special);
5944 
5945 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
5946 	if (ret == -1) {
5947 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5948 	} else {
5949 		int	i;
5950 		assert (*n > 0);
5951 		for (i = 0; i < *n; i++) {
5952 			BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i]));
5953 		}
5954 	}
5955 
5956 	return (ret);
5957 }
5958 
5959 static int
5960 is_bootdisk(char *osroot, char *physical)
5961 {
5962 	int			ret;
5963 	char			*grubroot;
5964 	char			*bootp;
5965 	const char		*fcn = "is_bootdisk()";
5966 
5967 	assert(osroot);
5968 	assert(physical);
5969 
5970 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical));
5971 
5972 	bootp = strstr(physical, "p0:boot");
5973 	if (bootp)
5974 		*bootp = '\0';
5975 	/*
5976 	 * We just want the BIOS mapping for menu disk.
5977 	 * Don't pass menu_root to get_grubroot() as the
5978 	 * check that it is used for is not relevant here.
5979 	 * The osroot is immaterial as well - it is only used to
5980 	 * to find create_diskmap script. Everything hinges on
5981 	 * "physical"
5982 	 */
5983 	grubroot = get_grubroot(osroot, physical, NULL);
5984 
5985 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
5986 	if (grubroot == NULL) {
5987 		bam_error(NO_GRUBROOT_FOR_DISK, fcn, physical);
5988 		return (0);
5989 	}
5990 	ret = grubroot[3] == '0';
5991 	free(grubroot);
5992 
5993 	BAM_DPRINTF((D_RETURN_RET, fcn, ret));
5994 
5995 	return (ret);
5996 }
5997 
5998 /*
5999  * Check if menu is on the boot device
6000  * Return 0 (false) on error
6001  */
6002 static int
6003 menu_on_bootdisk(char *osroot, char *menu_root)
6004 {
6005 	char		**physarray;
6006 	int		ret;
6007 	int		n;
6008 	int		i;
6009 	int		on_bootdisk;
6010 	const char	*fcn = "menu_on_bootdisk()";
6011 
6012 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
6013 
6014 	ret = get_physical(menu_root, &physarray, &n);
6015 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
6016 	if (ret != 0) {
6017 		bam_error(GET_PHYSICAL_MENU_NULL, menu_root);
6018 		return (0);
6019 	}
6020 
6021 	assert(physarray);
6022 	assert(n > 0);
6023 
6024 	on_bootdisk = 0;
6025 	for (i = 0; i < n; i++) {
6026 		assert(strncmp(physarray[i], "/dev/dsk/",
6027 		    strlen("/dev/dsk/")) == 0 ||
6028 		    strncmp(physarray[i], "/dev/rdsk/",
6029 		    strlen("/dev/rdsk/")) == 0);
6030 
6031 		BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i]));
6032 		if (is_bootdisk(osroot, physarray[i])) {
6033 			on_bootdisk = 1;
6034 			BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i]));
6035 		}
6036 	}
6037 
6038 	free_physarray(physarray, n);
6039 
6040 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
6041 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
6042 	if (on_bootdisk) {
6043 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6044 	} else {
6045 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6046 	}
6047 
6048 	return (on_bootdisk);
6049 }
6050 
6051 void
6052 bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
6053 {
6054 	const char	*fcn = "bam_add_line()";
6055 
6056 	assert(mp);
6057 	assert(entry);
6058 	assert(prev);
6059 	assert(lp);
6060 
6061 	lp->next = prev->next;
6062 	if (prev->next) {
6063 		BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn));
6064 		prev->next->prev = lp;
6065 	} else {
6066 		BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn));
6067 	}
6068 	prev->next = lp;
6069 	lp->prev = prev;
6070 
6071 	if (entry->end == prev) {
6072 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn));
6073 		entry->end = lp;
6074 	}
6075 	if (mp->end == prev) {
6076 		assert(lp->next == NULL);
6077 		mp->end = lp;
6078 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn));
6079 	}
6080 }
6081 
6082 /*
6083  * look for matching bootadm entry with specified parameters
6084  * Here are the rules (based on existing usage):
6085  * - If title is specified, match on title only
6086  * - Else, match on root/findroot, kernel, and module.
6087  *   Note that, if root_opt is non-zero, the absence of
6088  *   root line is considered a match.
6089  */
6090 static entry_t *
6091 find_boot_entry(
6092 	menu_t *mp,
6093 	char *title,
6094 	char *kernel,
6095 	char *findroot,
6096 	char *root,
6097 	char *module,
6098 	int root_opt,
6099 	int *entry_num)
6100 {
6101 	int		i;
6102 	line_t		*lp;
6103 	entry_t		*ent;
6104 	const char	*fcn = "find_boot_entry()";
6105 
6106 	if (entry_num)
6107 		*entry_num = BAM_ERROR;
6108 
6109 	/* find matching entry */
6110 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
6111 		lp = ent->start;
6112 
6113 		/* first line of entry must be bootadm comment */
6114 		lp = ent->start;
6115 		if (lp->flags != BAM_COMMENT ||
6116 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
6117 			continue;
6118 		}
6119 
6120 		/* advance to title line */
6121 		lp = lp->next;
6122 		if (title) {
6123 			if (lp->flags == BAM_TITLE && lp->arg &&
6124 			    strcmp(lp->arg, title) == 0) {
6125 				BAM_DPRINTF((D_MATCHED_TITLE, fcn, title));
6126 				break;
6127 			}
6128 			BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg));
6129 			continue;	/* check title only */
6130 		}
6131 
6132 		lp = lp->next;	/* advance to root line */
6133 		if (lp == NULL) {
6134 			continue;
6135 		} else if (strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
6136 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
6137 			    findroot = NULL);
6138 			if (findroot == NULL) {
6139 				BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL,
6140 				    fcn, lp->arg));
6141 				continue;
6142 			}
6143 			/* findroot command found, try match  */
6144 			if (strcmp(lp->arg, findroot) != 0) {
6145 				BAM_DPRINTF((D_NOMATCH_FINDROOT,
6146 				    fcn, findroot, lp->arg));
6147 				continue;
6148 			}
6149 			BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot));
6150 			lp = lp->next;	/* advance to kernel line */
6151 		} else if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
6152 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
6153 			if (root == NULL) {
6154 				BAM_DPRINTF((D_NOMATCH_ROOT_NULL,
6155 				    fcn, lp->arg));
6156 				continue;
6157 			}
6158 			/* root cmd found, try match */
6159 			if (strcmp(lp->arg, root) != 0) {
6160 				BAM_DPRINTF((D_NOMATCH_ROOT,
6161 				    fcn, root, lp->arg));
6162 				continue;
6163 			}
6164 			BAM_DPRINTF((D_MATCHED_ROOT, fcn, root));
6165 			lp = lp->next;	/* advance to kernel line */
6166 		} else {
6167 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
6168 			    root_opt = 0);
6169 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
6170 			    root_opt = 1);
6171 			/* no root command, see if root is optional */
6172 			if (root_opt == 0) {
6173 				BAM_DPRINTF((D_NO_ROOT_OPT, fcn));
6174 				continue;
6175 			}
6176 			BAM_DPRINTF((D_ROOT_OPT, fcn));
6177 		}
6178 
6179 		if (lp == NULL || lp->next == NULL) {
6180 			continue;
6181 		}
6182 
6183 		if (kernel &&
6184 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
6185 			continue;
6186 		}
6187 		BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg));
6188 
6189 		/*
6190 		 * Check for matching module entry (failsafe or normal).
6191 		 * If it fails to match, we go around the loop again.
6192 		 * For xpv entries, there are two module lines, so we
6193 		 * do the check twice.
6194 		 */
6195 		lp = lp->next;	/* advance to module line */
6196 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
6197 		    (((lp = lp->next) != NULL) &&
6198 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
6199 			/* match found */
6200 			BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg));
6201 			break;
6202 		}
6203 	}
6204 
6205 	if (ent && entry_num) {
6206 		*entry_num = i;
6207 	}
6208 
6209 	if (ent) {
6210 		BAM_DPRINTF((D_RETURN_RET, fcn, i));
6211 	} else {
6212 		BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR));
6213 	}
6214 	return (ent);
6215 }
6216 
6217 static int
6218 update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
6219     char *kernel, char *mod_kernel, char *module, int root_opt)
6220 {
6221 	int		i;
6222 	int		change_kernel = 0;
6223 	entry_t		*ent;
6224 	line_t		*lp;
6225 	line_t		*tlp;
6226 	char		linebuf[BAM_MAXLINE];
6227 	const char	*fcn = "update_boot_entry()";
6228 
6229 	/* note: don't match on title, it's updated on upgrade */
6230 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
6231 	    root_opt, &i);
6232 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
6233 		/*
6234 		 * We may be upgrading a kernel from multiboot to
6235 		 * directboot.  Look for a multiboot entry. A multiboot
6236 		 * entry will not have a findroot line.
6237 		 */
6238 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
6239 		    MULTIBOOT_ARCHIVE, root_opt, &i);
6240 		if (ent != NULL) {
6241 			BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root));
6242 			change_kernel = 1;
6243 		}
6244 	} else if (ent) {
6245 		BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot));
6246 	}
6247 
6248 	if (ent == NULL) {
6249 		BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot));
6250 		return (add_boot_entry(mp, title, findroot,
6251 		    kernel, mod_kernel, module));
6252 	}
6253 
6254 	/* replace title of existing entry and update findroot line */
6255 	lp = ent->start;
6256 	lp = lp->next;	/* title line */
6257 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
6258 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
6259 	free(lp->arg);
6260 	free(lp->line);
6261 	lp->arg = s_strdup(title);
6262 	lp->line = s_strdup(linebuf);
6263 	BAM_DPRINTF((D_CHANGING_TITLE, fcn, title));
6264 
6265 	tlp = lp;	/* title line */
6266 	lp = lp->next;	/* root line */
6267 
6268 	/* if no root or findroot command, create a new line_t */
6269 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
6270 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0) {
6271 		lp = s_calloc(1, sizeof (line_t));
6272 		bam_add_line(mp, ent, tlp, lp);
6273 	} else {
6274 		free(lp->cmd);
6275 		free(lp->sep);
6276 		free(lp->arg);
6277 		free(lp->line);
6278 	}
6279 
6280 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
6281 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
6282 	lp->arg = s_strdup(findroot);
6283 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
6284 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
6285 	lp->line = s_strdup(linebuf);
6286 	BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot));
6287 
6288 	/* kernel line */
6289 	lp = lp->next;
6290 
6291 	if (change_kernel) {
6292 		/*
6293 		 * We're upgrading from multiboot to directboot.
6294 		 */
6295 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
6296 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
6297 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
6298 			    kernel);
6299 			free(lp->cmd);
6300 			free(lp->arg);
6301 			free(lp->line);
6302 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
6303 			lp->arg = s_strdup(kernel);
6304 			lp->line = s_strdup(linebuf);
6305 			lp = lp->next;
6306 			BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel));
6307 		}
6308 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
6309 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
6310 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
6311 			    module);
6312 			free(lp->cmd);
6313 			free(lp->arg);
6314 			free(lp->line);
6315 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
6316 			lp->arg = s_strdup(module);
6317 			lp->line = s_strdup(linebuf);
6318 			lp = lp->next;
6319 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
6320 		}
6321 	}
6322 	BAM_DPRINTF((D_RETURN_RET, fcn, i));
6323 	return (i);
6324 }
6325 
6326 int
6327 root_optional(char *osroot, char *menu_root)
6328 {
6329 	char			*ospecial;
6330 	char			*mspecial;
6331 	char			*slash;
6332 	int			root_opt;
6333 	int			ret1;
6334 	int			ret2;
6335 	const char		*fcn = "root_optional()";
6336 
6337 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
6338 
6339 	/*
6340 	 * For all filesystems except ZFS, a straight compare of osroot
6341 	 * and menu_root will tell us if root is optional.
6342 	 * For ZFS, the situation is complicated by the fact that
6343 	 * menu_root and osroot are always different
6344 	 */
6345 	ret1 = is_zfs(osroot);
6346 	ret2 = is_zfs(menu_root);
6347 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
6348 	if (!ret1 || !ret2) {
6349 		BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root));
6350 		root_opt = (strcmp(osroot, menu_root) == 0);
6351 		goto out;
6352 	}
6353 
6354 	ospecial = get_special(osroot);
6355 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
6356 	if (ospecial == NULL) {
6357 		bam_error(GET_OSROOT_SPECIAL_ERR, osroot);
6358 		return (0);
6359 	}
6360 	BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot));
6361 
6362 	mspecial = get_special(menu_root);
6363 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
6364 	if (mspecial == NULL) {
6365 		bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root);
6366 		free(ospecial);
6367 		return (0);
6368 	}
6369 	BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root));
6370 
6371 	slash = strchr(ospecial, '/');
6372 	if (slash)
6373 		*slash = '\0';
6374 	BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot));
6375 
6376 	root_opt = (strcmp(ospecial, mspecial) == 0);
6377 
6378 	free(ospecial);
6379 	free(mspecial);
6380 
6381 out:
6382 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
6383 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
6384 	if (root_opt) {
6385 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6386 	} else {
6387 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6388 	}
6389 
6390 	return (root_opt);
6391 }
6392 
6393 /*ARGSUSED*/
6394 static error_t
6395 update_entry(menu_t *mp, char *menu_root, char *osdev)
6396 {
6397 	int		entry;
6398 	char		*grubsign;
6399 	char		*grubroot;
6400 	char		*title;
6401 	char		osroot[PATH_MAX];
6402 	char		*failsafe_kernel = NULL;
6403 	struct stat	sbuf;
6404 	char		failsafe[256];
6405 	int		ret;
6406 	const char	*fcn = "update_entry()";
6407 
6408 	assert(mp);
6409 	assert(menu_root);
6410 	assert(osdev);
6411 	assert(bam_root);
6412 
6413 	BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root));
6414 
6415 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
6416 
6417 	title = get_title(osroot);
6418 	assert(title);
6419 
6420 	grubsign = get_grubsign(osroot, osdev);
6421 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
6422 	if (grubsign == NULL) {
6423 		bam_error(GET_GRUBSIGN_ERROR, osroot, osdev);
6424 		return (BAM_ERROR);
6425 	}
6426 
6427 	/*
6428 	 * It is not a fatal error if get_grubroot() fails
6429 	 * We no longer rely on biosdev to populate the
6430 	 * menu
6431 	 */
6432 	grubroot = get_grubroot(osroot, osdev, menu_root);
6433 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
6434 	if (grubroot) {
6435 		BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS,
6436 		    fcn, osroot, osdev, menu_root));
6437 	} else {
6438 		BAM_DPRINTF((D_GET_GRUBROOT_FAILURE,
6439 		    fcn, osroot, osdev, menu_root));
6440 	}
6441 
6442 	/* add the entry for normal Solaris */
6443 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
6444 	    bam_direct = BAM_DIRECT_MULTIBOOT);
6445 	if (bam_direct == BAM_DIRECT_DBOOT) {
6446 		entry = update_boot_entry(mp, title, grubsign, grubroot,
6447 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
6448 		    NULL, DIRECT_BOOT_ARCHIVE,
6449 		    root_optional(osroot, menu_root));
6450 		BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign));
6451 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
6452 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
6453 			    grubroot, XEN_MENU, bam_zfs ?
6454 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
6455 			    DIRECT_BOOT_ARCHIVE,
6456 			    root_optional(osroot, menu_root));
6457 			BAM_DPRINTF((D_UPDATED_HV_ENTRY,
6458 			    fcn, bam_zfs, grubsign));
6459 		}
6460 	} else {
6461 		entry = update_boot_entry(mp, title, grubsign, grubroot,
6462 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
6463 		    root_optional(osroot, menu_root));
6464 
6465 		BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign));
6466 	}
6467 
6468 	/*
6469 	 * Add the entry for failsafe archive.  On a bfu'd system, the
6470 	 * failsafe may be different than the installed kernel.
6471 	 */
6472 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
6473 	    osroot, FAILSAFE_ARCHIVE);
6474 	if (stat(failsafe, &sbuf) == 0) {
6475 
6476 		/* Figure out where the kernel line should point */
6477 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
6478 		    DIRECT_BOOT_FAILSAFE_KERNEL);
6479 		if (stat(failsafe, &sbuf) == 0) {
6480 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
6481 		} else {
6482 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
6483 			    osroot, MULTI_BOOT_FAILSAFE);
6484 			if (stat(failsafe, &sbuf) == 0) {
6485 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
6486 			}
6487 		}
6488 		if (failsafe_kernel != NULL) {
6489 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
6490 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
6491 			    root_optional(osroot, menu_root));
6492 			BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn,
6493 			    failsafe_kernel));
6494 		}
6495 	}
6496 	free(grubroot);
6497 
6498 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
6499 	if (entry == BAM_ERROR) {
6500 		bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign);
6501 		free(grubsign);
6502 		return (BAM_ERROR);
6503 	}
6504 	free(grubsign);
6505 
6506 	update_numbering(mp);
6507 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
6508 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
6509 	if (ret == BAM_ERROR) {
6510 		bam_error(SET_DEFAULT_FAILED, entry);
6511 	}
6512 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6513 	return (BAM_WRITE);
6514 }
6515 
6516 static void
6517 save_default_entry(menu_t *mp, const char *which)
6518 {
6519 	int		lineNum;
6520 	int		entryNum;
6521 	int		entry = 0;	/* default is 0 */
6522 	char		linebuf[BAM_MAXLINE];
6523 	line_t		*lp = mp->curdefault;
6524 	const char	*fcn = "save_default_entry()";
6525 
6526 	if (mp->start) {
6527 		lineNum = mp->end->lineNum;
6528 		entryNum = mp->end->entryNum;
6529 	} else {
6530 		lineNum = LINE_INIT;
6531 		entryNum = ENTRY_INIT;
6532 	}
6533 
6534 	if (lp)
6535 		entry = s_strtol(lp->arg);
6536 
6537 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
6538 	BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf));
6539 	line_parser(mp, linebuf, &lineNum, &entryNum);
6540 	BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum));
6541 }
6542 
6543 static void
6544 restore_default_entry(menu_t *mp, const char *which, line_t *lp)
6545 {
6546 	int		entry;
6547 	char		*str;
6548 	const char	*fcn = "restore_default_entry()";
6549 
6550 	if (lp == NULL) {
6551 		BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn));
6552 		return;		/* nothing to restore */
6553 	}
6554 
6555 	BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which));
6556 
6557 	str = lp->arg + strlen(which);
6558 	entry = s_strtol(str);
6559 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
6560 
6561 	BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry));
6562 
6563 	/* delete saved old default line */
6564 	unlink_line(mp, lp);
6565 	line_free(lp);
6566 }
6567 
6568 /*
6569  * This function is for supporting reboot with args.
6570  * The opt value can be:
6571  * NULL		delete temp entry, if present
6572  * entry=<n>	switches default entry to <n>
6573  * else		treated as boot-args and setup a temperary menu entry
6574  *		and make it the default
6575  * Note that we are always rebooting the current OS instance
6576  * so osroot == / always.
6577  */
6578 #define	REBOOT_TITLE	"Solaris_reboot_transient"
6579 
6580 /*ARGSUSED*/
6581 static error_t
6582 update_temp(menu_t *mp, char *dummy, char *opt)
6583 {
6584 	int		entry;
6585 	char		*osdev;
6586 	char		*fstype;
6587 	char		*sign;
6588 	char		*opt_ptr;
6589 	char		*path;
6590 	char		kernbuf[BUFSIZ];
6591 	char		args_buf[BUFSIZ];
6592 	char		signbuf[PATH_MAX];
6593 	int		ret;
6594 	const char	*fcn = "update_temp()";
6595 
6596 	assert(mp);
6597 	assert(dummy == NULL);
6598 
6599 	/* opt can be NULL */
6600 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>"));
6601 	BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root));
6602 
6603 	if (bam_alt_root || bam_rootlen != 1 ||
6604 	    strcmp(bam_root, "/") != 0 ||
6605 	    strcmp(rootbuf, "/") != 0) {
6606 		bam_error(ALT_ROOT_INVALID, bam_root);
6607 		return (BAM_ERROR);
6608 	}
6609 
6610 	/* If no option, delete exiting reboot menu entry */
6611 	if (opt == NULL) {
6612 		entry_t		*ent;
6613 		BAM_DPRINTF((D_OPT_NULL, fcn));
6614 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
6615 		    NULL, NULL, 0, &entry);
6616 		if (ent == NULL) {	/* not found is ok */
6617 			BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn));
6618 			return (BAM_SUCCESS);
6619 		}
6620 		(void) do_delete(mp, entry);
6621 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
6622 		mp->olddefault = NULL;
6623 		BAM_DPRINTF((D_RESTORED_DEFAULT, fcn));
6624 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6625 		return (BAM_WRITE);
6626 	}
6627 
6628 	/* if entry= is specified, set the default entry */
6629 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
6630 		int entryNum = s_strtol(opt + strlen("entry="));
6631 		BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt));
6632 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
6633 			/* this is entry=# option */
6634 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
6635 			BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret));
6636 			return (ret);
6637 		} else {
6638 			bam_error(SET_DEFAULT_FAILED, entryNum);
6639 			return (BAM_ERROR);
6640 		}
6641 	}
6642 
6643 	/*
6644 	 * add a new menu entry based on opt and make it the default
6645 	 */
6646 
6647 	fstype = get_fstype("/");
6648 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
6649 	if (fstype == NULL) {
6650 		bam_error(REBOOT_FSTYPE_FAILED);
6651 		return (BAM_ERROR);
6652 	}
6653 
6654 	osdev = get_special("/");
6655 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
6656 	if (osdev == NULL) {
6657 		free(fstype);
6658 		bam_error(REBOOT_SPECIAL_FAILED);
6659 		return (BAM_ERROR);
6660 	}
6661 
6662 	sign = find_existing_sign("/", osdev, fstype);
6663 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
6664 	if (sign == NULL) {
6665 		free(fstype);
6666 		free(osdev);
6667 		bam_error(REBOOT_SIGN_FAILED);
6668 		return (BAM_ERROR);
6669 	}
6670 
6671 	free(fstype);
6672 	free(osdev);
6673 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
6674 	free(sign);
6675 
6676 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
6677 	    strchr(signbuf, ')') == NULL);
6678 
6679 	/*
6680 	 * There is no alternate root while doing reboot with args
6681 	 * This version of bootadm is only delivered with a DBOOT
6682 	 * version of Solaris.
6683 	 */
6684 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
6685 	if (bam_direct != BAM_DIRECT_DBOOT) {
6686 		bam_error(REBOOT_DIRECT_FAILED);
6687 		return (BAM_ERROR);
6688 	}
6689 
6690 	/* add an entry for Solaris reboot */
6691 	if (opt[0] == '-') {
6692 		/* It's an option - first see if boot-file is set */
6693 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
6694 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
6695 		if (ret != BAM_SUCCESS) {
6696 			bam_error(REBOOT_GET_KERNEL_FAILED);
6697 			return (BAM_ERROR);
6698 		}
6699 		if (kernbuf[0] == '\0')
6700 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
6701 			    sizeof (kernbuf));
6702 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
6703 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
6704 		BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf));
6705 	} else if (opt[0] == '/') {
6706 		/* It's a full path, so write it out. */
6707 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
6708 
6709 		/*
6710 		 * If someone runs:
6711 		 *
6712 		 *	# eeprom boot-args='-kd'
6713 		 *	# reboot /platform/i86pc/kernel/unix
6714 		 *
6715 		 * we want to use the boot-args as part of the boot
6716 		 * line.  On the other hand, if someone runs:
6717 		 *
6718 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
6719 		 *
6720 		 * we don't need to mess with boot-args.  If there's
6721 		 * no space in the options string, assume we're in the
6722 		 * first case.
6723 		 */
6724 		if (strchr(opt, ' ') == NULL) {
6725 			ret = get_kernel(mp, ARGS_CMD, args_buf,
6726 			    sizeof (args_buf));
6727 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
6728 			if (ret != BAM_SUCCESS) {
6729 				bam_error(REBOOT_GET_ARGS_FAILED);
6730 				return (BAM_ERROR);
6731 			}
6732 
6733 			if (args_buf[0] != '\0') {
6734 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
6735 				(void) strlcat(kernbuf, args_buf,
6736 				    sizeof (kernbuf));
6737 			}
6738 		}
6739 		BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf));
6740 	} else {
6741 		/*
6742 		 * It may be a partial path, or it may be a partial
6743 		 * path followed by options.  Assume that only options
6744 		 * follow a space.  If someone sends us a kernel path
6745 		 * that includes a space, they deserve to be broken.
6746 		 */
6747 		opt_ptr = strchr(opt, ' ');
6748 		if (opt_ptr != NULL) {
6749 			*opt_ptr = '\0';
6750 		}
6751 
6752 		path = expand_path(opt);
6753 		if (path != NULL) {
6754 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
6755 			free(path);
6756 
6757 			/*
6758 			 * If there were options given, use those.
6759 			 * Otherwise, copy over the default options.
6760 			 */
6761 			if (opt_ptr != NULL) {
6762 				/* Restore the space in opt string */
6763 				*opt_ptr = ' ';
6764 				(void) strlcat(kernbuf, opt_ptr,
6765 				    sizeof (kernbuf));
6766 			} else {
6767 				ret = get_kernel(mp, ARGS_CMD, args_buf,
6768 				    sizeof (args_buf));
6769 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
6770 				    ret = BAM_ERROR);
6771 				if (ret != BAM_SUCCESS) {
6772 					bam_error(REBOOT_GET_ARGS_FAILED);
6773 					return (BAM_ERROR);
6774 				}
6775 
6776 				if (args_buf[0] != '\0') {
6777 					(void) strlcat(kernbuf, " ",
6778 					    sizeof (kernbuf));
6779 					(void) strlcat(kernbuf,
6780 					    args_buf, sizeof (kernbuf));
6781 				}
6782 			}
6783 			BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf));
6784 		} else {
6785 			bam_error(UNKNOWN_KERNEL, opt);
6786 			bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
6787 			return (BAM_ERROR);
6788 		}
6789 	}
6790 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
6791 	    NULL, NULL);
6792 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
6793 	if (entry == BAM_ERROR) {
6794 		bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED);
6795 		return (BAM_ERROR);
6796 	}
6797 
6798 	save_default_entry(mp, BAM_OLDDEF);
6799 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
6800 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
6801 	if (ret == BAM_ERROR) {
6802 		bam_error(REBOOT_SET_DEFAULT_FAILED, entry);
6803 	}
6804 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6805 	return (BAM_WRITE);
6806 }
6807 
6808 static error_t
6809 set_global(menu_t *mp, char *globalcmd, int val)
6810 {
6811 	line_t		*lp;
6812 	line_t		*found;
6813 	line_t		*last;
6814 	char		*cp;
6815 	char		*str;
6816 	char		prefix[BAM_MAXLINE];
6817 	size_t		len;
6818 	const char	*fcn = "set_global()";
6819 
6820 	assert(mp);
6821 	assert(globalcmd);
6822 
6823 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
6824 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
6825 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
6826 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
6827 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
6828 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
6829 			bam_error(INVALID_ENTRY, prefix);
6830 			return (BAM_ERROR);
6831 		}
6832 	}
6833 
6834 	found = last = NULL;
6835 	for (lp = mp->start; lp; lp = lp->next) {
6836 		if (lp->flags != BAM_GLOBAL)
6837 			continue;
6838 
6839 		last = lp; /* track the last global found */
6840 
6841 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
6842 		if (lp->cmd == NULL) {
6843 			bam_error(NO_CMD, lp->lineNum);
6844 			continue;
6845 		}
6846 		if (strcmp(globalcmd, lp->cmd) != 0)
6847 			continue;
6848 
6849 		BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd));
6850 
6851 		if (found) {
6852 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
6853 		}
6854 		found = lp;
6855 	}
6856 
6857 	if (found == NULL) {
6858 		lp = s_calloc(1, sizeof (line_t));
6859 		if (last == NULL) {
6860 			lp->next = mp->start;
6861 			mp->start = lp;
6862 			mp->end = (mp->end) ? mp->end : lp;
6863 		} else {
6864 			lp->next = last->next;
6865 			last->next = lp;
6866 			if (lp->next == NULL)
6867 				mp->end = lp;
6868 		}
6869 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
6870 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
6871 		len += 10;	/* val < 10 digits */
6872 		lp->line = s_calloc(1, len);
6873 		(void) snprintf(lp->line, len, "%s%s%d",
6874 		    globalcmd, menu_cmds[SEP_CMD], val);
6875 		BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line));
6876 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6877 		return (BAM_WRITE);
6878 	}
6879 
6880 	/*
6881 	 * We are changing an existing entry. Retain any prefix whitespace,
6882 	 * but overwrite everything else. This preserves tabs added for
6883 	 * readability.
6884 	 */
6885 	str = found->line;
6886 	cp = prefix;
6887 	while (*str == ' ' || *str == '\t')
6888 		*(cp++) = *(str++);
6889 	*cp = '\0'; /* Terminate prefix */
6890 	len = strlen(prefix) + strlen(globalcmd);
6891 	len += strlen(menu_cmds[SEP_CMD]) + 10;
6892 
6893 	free(found->line);
6894 	found->line = s_calloc(1, len);
6895 	(void) snprintf(found->line, len,
6896 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
6897 
6898 	BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line));
6899 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6900 	return (BAM_WRITE); /* need a write to menu */
6901 }
6902 
6903 /*
6904  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
6905  * expand it to a full unix path.  The calling function is expected to
6906  * output a message if an error occurs and NULL is returned.
6907  */
6908 static char *
6909 expand_path(const char *partial_path)
6910 {
6911 	int		new_path_len;
6912 	char		*new_path;
6913 	char		new_path2[PATH_MAX];
6914 	struct stat	sb;
6915 	const char	*fcn = "expand_path()";
6916 
6917 	new_path_len = strlen(partial_path) + 64;
6918 	new_path = s_calloc(1, new_path_len);
6919 
6920 	/* First, try the simplest case - something like "kernel/unix" */
6921 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
6922 	    partial_path);
6923 	if (stat(new_path, &sb) == 0) {
6924 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
6925 		return (new_path);
6926 	}
6927 
6928 	if (strcmp(partial_path, "kmdb") == 0) {
6929 		(void) snprintf(new_path, new_path_len, "%s -k",
6930 		    DIRECT_BOOT_KERNEL);
6931 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
6932 		return (new_path);
6933 	}
6934 
6935 	/*
6936 	 * We've quickly reached unsupported usage.  Try once more to
6937 	 * see if we were just given a glom name.
6938 	 */
6939 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
6940 	    partial_path);
6941 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
6942 	    partial_path);
6943 	if (stat(new_path, &sb) == 0) {
6944 		if (stat(new_path2, &sb) == 0) {
6945 			/*
6946 			 * We matched both, so we actually
6947 			 * want to write the $ISADIR version.
6948 			 */
6949 			(void) snprintf(new_path, new_path_len,
6950 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
6951 			    partial_path);
6952 		}
6953 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
6954 		return (new_path);
6955 	}
6956 
6957 	free(new_path);
6958 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6959 	return (NULL);
6960 }
6961 
6962 /*
6963  * The kernel cmd and arg have been changed, so
6964  * check whether the archive line needs to change.
6965  */
6966 static void
6967 set_archive_line(entry_t *entryp, line_t *kernelp)
6968 {
6969 	line_t		*lp = entryp->start;
6970 	char		*new_archive;
6971 	menu_cmd_t	m_cmd;
6972 	const char	*fcn = "set_archive_line()";
6973 
6974 	for (; lp != NULL; lp = lp->next) {
6975 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
6976 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
6977 			break;
6978 		}
6979 
6980 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
6981 		if (lp == entryp->end) {
6982 			BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn,
6983 			    entryp->entryNum));
6984 			return;
6985 		}
6986 	}
6987 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
6988 	if (lp == NULL) {
6989 		BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum));
6990 		return;
6991 	}
6992 
6993 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
6994 		new_archive = DIRECT_BOOT_ARCHIVE;
6995 		m_cmd = MODULE_DOLLAR_CMD;
6996 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
6997 		new_archive = DIRECT_BOOT_ARCHIVE_64;
6998 		m_cmd = MODULE_CMD;
6999 	} else {
7000 		new_archive = DIRECT_BOOT_ARCHIVE_32;
7001 		m_cmd = MODULE_CMD;
7002 	}
7003 
7004 	if (strcmp(lp->arg, new_archive) == 0) {
7005 		BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg));
7006 		return;
7007 	}
7008 
7009 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
7010 		free(lp->cmd);
7011 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
7012 	}
7013 
7014 	free(lp->arg);
7015 	lp->arg = s_strdup(new_archive);
7016 	update_line(lp);
7017 	BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line));
7018 }
7019 
7020 /*
7021  * Title for an entry to set properties that once went in bootenv.rc.
7022  */
7023 #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
7024 
7025 /*
7026  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
7027  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
7028  * string, reset the value to the default.  If path is a non-zero-length
7029  * string, set the kernel or arguments.
7030  */
7031 static error_t
7032 get_set_kernel(
7033 	menu_t *mp,
7034 	menu_cmd_t optnum,
7035 	char *path,
7036 	char *buf,
7037 	size_t bufsize)
7038 {
7039 	int		entryNum;
7040 	int		rv = BAM_SUCCESS;
7041 	int		free_new_path = 0;
7042 	entry_t		*entryp;
7043 	line_t		*ptr;
7044 	line_t		*kernelp;
7045 	char		*new_arg;
7046 	char		*old_args;
7047 	char		*space;
7048 	char		*new_path;
7049 	char		old_space;
7050 	size_t		old_kernel_len;
7051 	size_t		new_str_len;
7052 	char		*fstype;
7053 	char		*osdev;
7054 	char		*sign;
7055 	char		signbuf[PATH_MAX];
7056 	int		ret;
7057 	const char	*fcn = "get_set_kernel()";
7058 
7059 	assert(bufsize > 0);
7060 
7061 	ptr = kernelp = NULL;
7062 	new_arg = old_args = space = NULL;
7063 	new_path = NULL;
7064 	buf[0] = '\0';
7065 
7066 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
7067 	    bam_direct = BAM_DIRECT_MULTIBOOT);
7068 	if (bam_direct != BAM_DIRECT_DBOOT) {
7069 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
7070 		return (BAM_ERROR);
7071 	}
7072 
7073 	/*
7074 	 * If a user changed the default entry to a non-bootadm controlled
7075 	 * one, we don't want to mess with it.  Just print an error and
7076 	 * return.
7077 	 */
7078 	if (mp->curdefault) {
7079 		entryNum = s_strtol(mp->curdefault->arg);
7080 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
7081 			if (entryp->entryNum == entryNum)
7082 				break;
7083 		}
7084 		if ((entryp != NULL) &&
7085 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
7086 			bam_error(DEFAULT_NOT_BAM);
7087 			return (BAM_ERROR);
7088 		}
7089 	}
7090 
7091 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
7092 	    0, &entryNum);
7093 
7094 	if (entryp != NULL) {
7095 		for (ptr = entryp->start; ptr && ptr != entryp->end;
7096 		    ptr = ptr->next) {
7097 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
7098 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
7099 				kernelp = ptr;
7100 				break;
7101 			}
7102 		}
7103 		if (kernelp == NULL) {
7104 			bam_error(NO_KERNEL, entryNum);
7105 			return (BAM_ERROR);
7106 		}
7107 
7108 		old_kernel_len = strcspn(kernelp->arg, " \t");
7109 		space = old_args = kernelp->arg + old_kernel_len;
7110 		while ((*old_args == ' ') || (*old_args == '\t'))
7111 			old_args++;
7112 	}
7113 
7114 	if (path == NULL) {
7115 		if (entryp == NULL) {
7116 			BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn));
7117 			BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7118 			return (BAM_SUCCESS);
7119 		}
7120 		assert(kernelp);
7121 		if (optnum == ARGS_CMD) {
7122 			if (old_args[0] != '\0') {
7123 				(void) strlcpy(buf, old_args, bufsize);
7124 				BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf));
7125 			}
7126 		} else {
7127 			/*
7128 			 * We need to print the kernel, so we just turn the
7129 			 * first space into a '\0' and print the beginning.
7130 			 * We don't print anything if it's the default kernel.
7131 			 */
7132 			old_space = *space;
7133 			*space = '\0';
7134 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
7135 				(void) strlcpy(buf, kernelp->arg, bufsize);
7136 				BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf));
7137 			}
7138 			*space = old_space;
7139 		}
7140 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7141 		return (BAM_SUCCESS);
7142 	}
7143 
7144 	/*
7145 	 * First, check if we're resetting an entry to the default.
7146 	 */
7147 	if ((path[0] == '\0') ||
7148 	    ((optnum == KERNEL_CMD) &&
7149 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
7150 		if ((entryp == NULL) || (kernelp == NULL)) {
7151 			/* No previous entry, it's already the default */
7152 			BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn));
7153 			return (BAM_SUCCESS);
7154 		}
7155 
7156 		/*
7157 		 * Check if we can delete the entry.  If we're resetting the
7158 		 * kernel command, and the args is already empty, or if we're
7159 		 * resetting the args command, and the kernel is already the
7160 		 * default, we can restore the old default and delete the entry.
7161 		 */
7162 		if (((optnum == KERNEL_CMD) &&
7163 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
7164 		    ((optnum == ARGS_CMD) &&
7165 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
7166 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
7167 			kernelp = NULL;
7168 			(void) do_delete(mp, entryNum);
7169 			restore_default_entry(mp, BAM_OLD_RC_DEF,
7170 			    mp->old_rc_default);
7171 			mp->old_rc_default = NULL;
7172 			rv = BAM_WRITE;
7173 			BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn));
7174 			goto done;
7175 		}
7176 
7177 		if (optnum == KERNEL_CMD) {
7178 			/*
7179 			 * At this point, we've already checked that old_args
7180 			 * and entryp are valid pointers.  The "+ 2" is for
7181 			 * a space a the string termination character.
7182 			 */
7183 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
7184 			    strlen(old_args) + 2;
7185 			new_arg = s_calloc(1, new_str_len);
7186 			(void) snprintf(new_arg, new_str_len, "%s %s",
7187 			    DIRECT_BOOT_KERNEL, old_args);
7188 			free(kernelp->arg);
7189 			kernelp->arg = new_arg;
7190 
7191 			/*
7192 			 * We have changed the kernel line, so we may need
7193 			 * to update the archive line as well.
7194 			 */
7195 			set_archive_line(entryp, kernelp);
7196 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG,
7197 			    fcn, kernelp->arg));
7198 		} else {
7199 			/*
7200 			 * We're resetting the boot args to nothing, so
7201 			 * we only need to copy the kernel.  We've already
7202 			 * checked that the kernel is not the default.
7203 			 */
7204 			new_arg = s_calloc(1, old_kernel_len + 1);
7205 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
7206 			    kernelp->arg);
7207 			free(kernelp->arg);
7208 			kernelp->arg = new_arg;
7209 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL,
7210 			    fcn, kernelp->arg));
7211 		}
7212 		rv = BAM_WRITE;
7213 		goto done;
7214 	}
7215 
7216 	/*
7217 	 * Expand the kernel file to a full path, if necessary
7218 	 */
7219 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
7220 		new_path = expand_path(path);
7221 		if (new_path == NULL) {
7222 			bam_error(UNKNOWN_KERNEL, path);
7223 			BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7224 			return (BAM_ERROR);
7225 		}
7226 		free_new_path = 1;
7227 	} else {
7228 		new_path = path;
7229 		free_new_path = 0;
7230 	}
7231 
7232 	/*
7233 	 * At this point, we know we're setting a new value.  First, take care
7234 	 * of the case where there was no previous entry.
7235 	 */
7236 	if (entryp == NULL) {
7237 
7238 		/* Similar to code in update_temp */
7239 		fstype = get_fstype("/");
7240 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
7241 		if (fstype == NULL) {
7242 			bam_error(BOOTENV_FSTYPE_FAILED);
7243 			rv = BAM_ERROR;
7244 			goto done;
7245 		}
7246 
7247 		osdev = get_special("/");
7248 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
7249 		if (osdev == NULL) {
7250 			free(fstype);
7251 			bam_error(BOOTENV_SPECIAL_FAILED);
7252 			rv = BAM_ERROR;
7253 			goto done;
7254 		}
7255 
7256 		sign = find_existing_sign("/", osdev, fstype);
7257 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
7258 		if (sign == NULL) {
7259 			free(fstype);
7260 			free(osdev);
7261 			bam_error(BOOTENV_SIGN_FAILED);
7262 			rv = BAM_ERROR;
7263 			goto done;
7264 		}
7265 
7266 		free(fstype);
7267 		free(osdev);
7268 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
7269 		free(sign);
7270 		assert(strchr(signbuf, '(') == NULL &&
7271 		    strchr(signbuf, ',') == NULL &&
7272 		    strchr(signbuf, ')') == NULL);
7273 
7274 		if (optnum == KERNEL_CMD) {
7275 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn, new_path));
7276 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
7277 			    signbuf, new_path, NULL, NULL);
7278 		} else {
7279 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
7280 			    strlen(path) + 8;
7281 			new_arg = s_calloc(1, new_str_len);
7282 
7283 			(void) snprintf(new_arg, new_str_len, "%s %s",
7284 			    DIRECT_BOOT_KERNEL, path);
7285 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg));
7286 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
7287 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE);
7288 			free(new_arg);
7289 		}
7290 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
7291 		    entryNum = BAM_ERROR);
7292 		if (entryNum == BAM_ERROR) {
7293 			bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY,
7294 			    BOOTENV_RC_TITLE);
7295 			rv = BAM_ERROR;
7296 			goto done;
7297 		}
7298 		save_default_entry(mp, BAM_OLD_RC_DEF);
7299 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
7300 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
7301 		if (ret == BAM_ERROR) {
7302 			bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum);
7303 		}
7304 		rv = BAM_WRITE;
7305 		goto done;
7306 	}
7307 
7308 	/*
7309 	 * There was already an bootenv entry which we need to edit.
7310 	 */
7311 	if (optnum == KERNEL_CMD) {
7312 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
7313 		new_arg = s_calloc(1, new_str_len);
7314 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
7315 		    old_args);
7316 		free(kernelp->arg);
7317 		kernelp->arg = new_arg;
7318 
7319 		/*
7320 		 * If we have changed the kernel line, we may need to update
7321 		 * the archive line as well.
7322 		 */
7323 		set_archive_line(entryp, kernelp);
7324 		BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn,
7325 		    kernelp->arg));
7326 	} else {
7327 		new_str_len = old_kernel_len + strlen(path) + 8;
7328 		new_arg = s_calloc(1, new_str_len);
7329 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
7330 		(void) strlcat(new_arg, " ", new_str_len);
7331 		(void) strlcat(new_arg, path, new_str_len);
7332 		free(kernelp->arg);
7333 		kernelp->arg = new_arg;
7334 		BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn,
7335 		    kernelp->arg));
7336 	}
7337 	rv = BAM_WRITE;
7338 
7339 done:
7340 	if ((rv == BAM_WRITE) && kernelp)
7341 		update_line(kernelp);
7342 	if (free_new_path)
7343 		free(new_path);
7344 	if (rv == BAM_WRITE) {
7345 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7346 	} else {
7347 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7348 	}
7349 	return (rv);
7350 }
7351 
7352 static error_t
7353 get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
7354 {
7355 	const char	*fcn = "get_kernel()";
7356 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum]));
7357 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
7358 }
7359 
7360 static error_t
7361 set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
7362 {
7363 	const char	*fcn = "set_kernel()";
7364 	assert(path != NULL);
7365 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path));
7366 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
7367 }
7368 
7369 /*ARGSUSED*/
7370 static error_t
7371 set_option(menu_t *mp, char *dummy, char *opt)
7372 {
7373 	int		optnum;
7374 	int		optval;
7375 	char		*val;
7376 	char		buf[BUFSIZ] = "";
7377 	error_t		rv;
7378 	const char	*fcn = "set_option()";
7379 
7380 	assert(mp);
7381 	assert(opt);
7382 	assert(dummy == NULL);
7383 
7384 	/* opt is set from bam_argv[0] and is always non-NULL */
7385 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt));
7386 
7387 	val = strchr(opt, '=');
7388 	if (val != NULL) {
7389 		*val = '\0';
7390 	}
7391 
7392 	if (strcmp(opt, "default") == 0) {
7393 		optnum = DEFAULT_CMD;
7394 	} else if (strcmp(opt, "timeout") == 0) {
7395 		optnum = TIMEOUT_CMD;
7396 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
7397 		optnum = KERNEL_CMD;
7398 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
7399 		optnum = ARGS_CMD;
7400 	} else {
7401 		bam_error(INVALID_OPTION, opt);
7402 		return (BAM_ERROR);
7403 	}
7404 
7405 	/*
7406 	 * kernel and args are allowed without "=new_value" strings.  All
7407 	 * others cause errors
7408 	 */
7409 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
7410 		bam_error(NO_OPTION_ARG, opt);
7411 		return (BAM_ERROR);
7412 	} else if (val != NULL) {
7413 		*val = '=';
7414 	}
7415 
7416 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
7417 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum],
7418 		    val ? val + 1 : "NULL"));
7419 
7420 		if (val)
7421 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
7422 		else
7423 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
7424 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
7425 			(void) printf("%s\n", buf);
7426 	} else {
7427 		optval = s_strtol(val + 1);
7428 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1));
7429 		rv = set_global(mp, menu_cmds[optnum], optval);
7430 	}
7431 
7432 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
7433 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7434 	} else {
7435 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7436 	}
7437 
7438 	return (rv);
7439 }
7440 
7441 /*
7442  * The quiet argument suppresses messages. This is used
7443  * when invoked in the context of other commands (e.g. list_entry)
7444  */
7445 static error_t
7446 read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
7447 {
7448 	line_t *lp;
7449 	char *arg;
7450 	int done, ret = BAM_SUCCESS;
7451 
7452 	assert(mp);
7453 	assert(menu_path);
7454 	assert(globalcmd);
7455 
7456 	if (mp->start == NULL) {
7457 		if (!quiet)
7458 			bam_error(NO_MENU, menu_path);
7459 		return (BAM_ERROR);
7460 	}
7461 
7462 	done = 0;
7463 	for (lp = mp->start; lp; lp = lp->next) {
7464 		if (lp->flags != BAM_GLOBAL)
7465 			continue;
7466 
7467 		if (lp->cmd == NULL) {
7468 			if (!quiet)
7469 				bam_error(NO_CMD, lp->lineNum);
7470 			continue;
7471 		}
7472 
7473 		if (strcmp(globalcmd, lp->cmd) != 0)
7474 			continue;
7475 
7476 		/* Found global. Check for duplicates */
7477 		if (done && !quiet) {
7478 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
7479 			ret = BAM_ERROR;
7480 		}
7481 
7482 		arg = lp->arg ? lp->arg : "";
7483 		bam_print(GLOBAL_CMD, globalcmd, arg);
7484 		done = 1;
7485 	}
7486 
7487 	if (!done && bam_verbose)
7488 		bam_print(NO_ENTRY, globalcmd);
7489 
7490 	return (ret);
7491 }
7492 
7493 static error_t
7494 menu_write(char *root, menu_t *mp)
7495 {
7496 	const char *fcn = "menu_write()";
7497 
7498 	BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root));
7499 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
7500 }
7501 
7502 void
7503 line_free(line_t *lp)
7504 {
7505 	if (lp == NULL)
7506 		return;
7507 
7508 	if (lp->cmd)
7509 		free(lp->cmd);
7510 	if (lp->sep)
7511 		free(lp->sep);
7512 	if (lp->arg)
7513 		free(lp->arg);
7514 	if (lp->line)
7515 		free(lp->line);
7516 	free(lp);
7517 }
7518 
7519 static void
7520 linelist_free(line_t *start)
7521 {
7522 	line_t *lp;
7523 
7524 	while (start) {
7525 		lp = start;
7526 		start = start->next;
7527 		line_free(lp);
7528 	}
7529 }
7530 
7531 static void
7532 filelist_free(filelist_t *flistp)
7533 {
7534 	linelist_free(flistp->head);
7535 	flistp->head = NULL;
7536 	flistp->tail = NULL;
7537 }
7538 
7539 static void
7540 menu_free(menu_t *mp)
7541 {
7542 	entry_t *ent, *tmp;
7543 	assert(mp);
7544 
7545 	if (mp->start)
7546 		linelist_free(mp->start);
7547 	ent = mp->entries;
7548 	while (ent) {
7549 		tmp = ent;
7550 		ent = tmp->next;
7551 		free(tmp);
7552 	}
7553 
7554 	free(mp);
7555 }
7556 
7557 /*
7558  * Utility routines
7559  */
7560 
7561 
7562 /*
7563  * Returns 0 on success
7564  * Any other value indicates an error
7565  */
7566 static int
7567 exec_cmd(char *cmdline, filelist_t *flistp)
7568 {
7569 	char buf[BUFSIZ];
7570 	int ret;
7571 	FILE *ptr;
7572 	sigset_t set;
7573 	void (*disp)(int);
7574 
7575 	/*
7576 	 * For security
7577 	 * - only absolute paths are allowed
7578 	 * - set IFS to space and tab
7579 	 */
7580 	if (*cmdline != '/') {
7581 		bam_error(ABS_PATH_REQ, cmdline);
7582 		return (-1);
7583 	}
7584 	(void) putenv("IFS= \t");
7585 
7586 	/*
7587 	 * We may have been exec'ed with SIGCHLD blocked
7588 	 * unblock it here
7589 	 */
7590 	(void) sigemptyset(&set);
7591 	(void) sigaddset(&set, SIGCHLD);
7592 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
7593 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
7594 		return (-1);
7595 	}
7596 
7597 	/*
7598 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
7599 	 */
7600 	disp = sigset(SIGCHLD, SIG_DFL);
7601 	if (disp == SIG_ERR) {
7602 		bam_error(FAILED_SIG, strerror(errno));
7603 		return (-1);
7604 	}
7605 	if (disp == SIG_HOLD) {
7606 		bam_error(BLOCKED_SIG, cmdline);
7607 		return (-1);
7608 	}
7609 
7610 	ptr = popen(cmdline, "r");
7611 	if (ptr == NULL) {
7612 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
7613 		return (-1);
7614 	}
7615 
7616 	/*
7617 	 * If we simply do a pclose() following a popen(), pclose()
7618 	 * will close the reader end of the pipe immediately even
7619 	 * if the child process has not started/exited. pclose()
7620 	 * does wait for cmd to terminate before returning though.
7621 	 * When the executed command writes its output to the pipe
7622 	 * there is no reader process and the command dies with
7623 	 * SIGPIPE. To avoid this we read repeatedly until read
7624 	 * terminates with EOF. This indicates that the command
7625 	 * (writer) has closed the pipe and we can safely do a
7626 	 * pclose().
7627 	 *
7628 	 * Since pclose() does wait for the command to exit,
7629 	 * we can safely reap the exit status of the command
7630 	 * from the value returned by pclose()
7631 	 */
7632 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
7633 		if (flistp == NULL) {
7634 			/* s_fgets strips newlines, so insert them at the end */
7635 			bam_print(PRINT, buf);
7636 		} else {
7637 			append_to_flist(flistp, buf);
7638 		}
7639 	}
7640 
7641 	ret = pclose(ptr);
7642 	if (ret == -1) {
7643 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
7644 		return (-1);
7645 	}
7646 
7647 	if (WIFEXITED(ret)) {
7648 		return (WEXITSTATUS(ret));
7649 	} else {
7650 		bam_error(EXEC_FAIL, cmdline, ret);
7651 		return (-1);
7652 	}
7653 }
7654 
7655 /*
7656  * Since this function returns -1 on error
7657  * it cannot be used to convert -1. However,
7658  * that is sufficient for what we need.
7659  */
7660 static long
7661 s_strtol(char *str)
7662 {
7663 	long l;
7664 	char *res = NULL;
7665 
7666 	if (str == NULL) {
7667 		return (-1);
7668 	}
7669 
7670 	errno = 0;
7671 	l = strtol(str, &res, 10);
7672 	if (errno || *res != '\0') {
7673 		return (-1);
7674 	}
7675 
7676 	return (l);
7677 }
7678 
7679 /*
7680  * Wrapper around fputs, that adds a newline (since fputs doesn't)
7681  */
7682 static int
7683 s_fputs(char *str, FILE *fp)
7684 {
7685 	char linebuf[BAM_MAXLINE];
7686 
7687 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
7688 	return (fputs(linebuf, fp));
7689 }
7690 
7691 /*
7692  * Wrapper around fgets, that strips newlines returned by fgets
7693  */
7694 char *
7695 s_fgets(char *buf, int buflen, FILE *fp)
7696 {
7697 	int n;
7698 
7699 	buf = fgets(buf, buflen, fp);
7700 	if (buf) {
7701 		n = strlen(buf);
7702 		if (n == buflen - 1 && buf[n-1] != '\n')
7703 			bam_error(TOO_LONG, buflen - 1, buf);
7704 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
7705 	}
7706 
7707 	return (buf);
7708 }
7709 
7710 void *
7711 s_calloc(size_t nelem, size_t sz)
7712 {
7713 	void *ptr;
7714 
7715 	ptr = calloc(nelem, sz);
7716 	if (ptr == NULL) {
7717 		bam_error(NO_MEM, nelem*sz);
7718 		bam_exit(1);
7719 	}
7720 	return (ptr);
7721 }
7722 
7723 void *
7724 s_realloc(void *ptr, size_t sz)
7725 {
7726 	ptr = realloc(ptr, sz);
7727 	if (ptr == NULL) {
7728 		bam_error(NO_MEM, sz);
7729 		bam_exit(1);
7730 	}
7731 	return (ptr);
7732 }
7733 
7734 char *
7735 s_strdup(char *str)
7736 {
7737 	char *ptr;
7738 
7739 	if (str == NULL)
7740 		return (NULL);
7741 
7742 	ptr = strdup(str);
7743 	if (ptr == NULL) {
7744 		bam_error(NO_MEM, strlen(str) + 1);
7745 		bam_exit(1);
7746 	}
7747 	return (ptr);
7748 }
7749 
7750 /*
7751  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
7752  * Returns 0 otherwise
7753  */
7754 static int
7755 is_amd64(void)
7756 {
7757 	static int amd64 = -1;
7758 	char isabuf[257];	/* from sysinfo(2) manpage */
7759 
7760 	if (amd64 != -1)
7761 		return (amd64);
7762 
7763 	if (bam_alt_platform) {
7764 		if (strcmp(bam_platform, "i86pc") == 0) {
7765 			amd64 = 1;		/* diskless server */
7766 		}
7767 	} else {
7768 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
7769 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
7770 			amd64 = 1;
7771 		} else if (strstr(isabuf, "i386") == NULL) {
7772 			amd64 = 1;		/* diskless server */
7773 		}
7774 	}
7775 	if (amd64 == -1)
7776 		amd64 = 0;
7777 
7778 	return (amd64);
7779 }
7780 
7781 static char *
7782 get_machine(void)
7783 {
7784 	static int cached = -1;
7785 	static char mbuf[257];	/* from sysinfo(2) manpage */
7786 
7787 	if (cached == 0)
7788 		return (mbuf);
7789 
7790 	if (bam_alt_platform) {
7791 		return (bam_platform);
7792 	} else {
7793 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
7794 			cached = 1;
7795 		}
7796 	}
7797 	if (cached == -1) {
7798 		mbuf[0] = '\0';
7799 		cached = 0;
7800 	}
7801 
7802 	return (mbuf);
7803 }
7804 
7805 int
7806 is_sparc(void)
7807 {
7808 	static int issparc = -1;
7809 	char mbuf[257];	/* from sysinfo(2) manpage */
7810 
7811 	if (issparc != -1)
7812 		return (issparc);
7813 
7814 	if (bam_alt_platform) {
7815 		if (strncmp(bam_platform, "sun4", 4) == 0) {
7816 			issparc = 1;
7817 		}
7818 	} else {
7819 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
7820 		    strcmp(mbuf, "sparc") == 0) {
7821 			issparc = 1;
7822 		}
7823 	}
7824 	if (issparc == -1)
7825 		issparc = 0;
7826 
7827 	return (issparc);
7828 }
7829 
7830 static void
7831 append_to_flist(filelist_t *flistp, char *s)
7832 {
7833 	line_t *lp;
7834 
7835 	lp = s_calloc(1, sizeof (line_t));
7836 	lp->line = s_strdup(s);
7837 	if (flistp->head == NULL)
7838 		flistp->head = lp;
7839 	else
7840 		flistp->tail->next = lp;
7841 	flistp->tail = lp;
7842 }
7843 
7844 #if !defined(_OPB)
7845 
7846 UCODE_VENDORS;
7847 
7848 /*ARGSUSED*/
7849 static void
7850 ucode_install(char *root)
7851 {
7852 	int i;
7853 
7854 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
7855 		int cmd_len = PATH_MAX + 256;
7856 		char cmd[PATH_MAX + 256];
7857 		char file[PATH_MAX];
7858 		char timestamp[PATH_MAX];
7859 		struct stat fstatus, tstatus;
7860 		struct utimbuf u_times;
7861 
7862 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
7863 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
7864 		    ucode_vendors[i].extstr);
7865 
7866 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
7867 			continue;
7868 
7869 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
7870 
7871 		if (stat(timestamp, &tstatus) == 0 &&
7872 		    fstatus.st_mtime <= tstatus.st_mtime)
7873 			continue;
7874 
7875 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
7876 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
7877 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
7878 		if (system(cmd) != 0)
7879 			return;
7880 
7881 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
7882 			return;
7883 
7884 		u_times.actime = fstatus.st_atime;
7885 		u_times.modtime = fstatus.st_mtime;
7886 		(void) utime(timestamp, &u_times);
7887 	}
7888 }
7889 #endif
7890