|
root / base / usr / src / cmd / boot / bootadm / bootadm_upgrade.c
bootadm_upgrade.c C 1017 lines 26.7 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2017 Toomas Soome <tsoome@me.com>
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include <strings.h>

#include <sys/mman.h>
#include <sys/elf.h>
#include <sys/multiboot.h>

#include "bootadm.h"

direct_or_multi_t bam_direct = BAM_DIRECT_NOT_SET;
hv_t bam_is_hv = BAM_HV_UNKNOWN;
findroot_t bam_is_findroot = BAM_FINDROOT_UNKNOWN;

static void
get_findroot_cap(const char *osroot)
{
	FILE		*fp;
	char		path[PATH_MAX];
	char		buf[BAM_MAXLINE];
	struct stat	sb;
	int		dboot;
	int		error;
	int		ret;
	const char	*fcn = "get_findroot_cap()";

	(void) snprintf(path, sizeof (path), "%s/%s",
	    osroot, "boot/grub/capability");

	if (stat(path, &sb) == -1) {
		bam_is_findroot = BAM_FINDROOT_ABSENT;
		BAM_DPRINTF(("%s: findroot capability absent\n", fcn));
		return;
	}

	fp = fopen(path, "r");
	error = errno;
	INJECT_ERROR1("GET_CAP_FINDROOT_FOPEN", fp = NULL);
	if (fp == NULL) {
		bam_error(_("failed to open file: %s: %s\n"), path,
		    strerror(error));
		return;
	}

	dboot = 0;
	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
		if (strcmp(buf, "findroot") == 0) {
			BAM_DPRINTF(("%s: findroot capability present\n", fcn));
			bam_is_findroot = BAM_FINDROOT_PRESENT;
		}
		if (strcmp(buf, "dboot") == 0) {
			BAM_DPRINTF(("%s: dboot capability present\n", fcn));
			dboot = 1;
		}
	}

	assert(dboot);

	if (bam_is_findroot == BAM_FINDROOT_UNKNOWN) {
		bam_is_findroot = BAM_FINDROOT_ABSENT;
		BAM_DPRINTF(("%s: findroot capability absent\n", fcn));
	}

	ret = fclose(fp);
	error = errno;
	INJECT_ERROR1("GET_CAP_FINDROOT_FCLOSE", ret = 1);
	if (ret != 0) {
		bam_error(_("failed to close file: %s: %s\n"),
		    path, strerror(error));
	}
}

error_t
get_boot_cap(const char *osroot)
{
	char		fname[PATH_MAX];
	char		*image;
	uchar_t		*ident;
	uchar_t		class;
	int		fd;
	int		m;
	multiboot_header_t *mbh;
	struct stat	sb;
	int		error;
	const char	*fcn = "get_boot_cap()";

	if (is_sparc()) {
		/* there is no non dboot sparc new-boot */
		bam_direct = BAM_DIRECT_DBOOT;
		BAM_DPRINTF(("%s: is sparc - always DBOOT\n", fcn));
		return (BAM_SUCCESS);
	}

	/*
	 * The install media can support both 64 and 32 bit boot
	 * by using boot archive as ramdisk image. However, to save
	 * the memory, the ramdisk may only have either 32 or 64
	 * bit kernel files. To avoid error message about missing unix,
	 * we should try both variants here and only complain if neither
	 * is found. Since the 64-bit systems are more common, we start
	 * from amd64.
	 */
	class = ELFCLASS64;
	(void) snprintf(fname, PATH_MAX, "%s/%s", osroot,
	    "kernel/unix");
	fd = open(fname, O_RDONLY);
	error = errno;
	INJECT_ERROR1("GET_CAP_UNIX_OPEN", fd = -1);
	if (fd < 0) {
		bam_error(_("failed to open file: %s: %s\n"), fname,
		    strerror(error));
		return (BAM_ERROR);
	}

	/*
	 * Verify that this is a sane unix at least 8192 bytes in length
	 */
	if (fstat(fd, &sb) == -1 || sb.st_size < 8192) {
		(void) close(fd);
		bam_error(_("invalid or corrupted binary: %s\n"), fname);
		return (BAM_ERROR);
	}

	/*
	 * mmap the first 8K
	 */
	image = mmap(NULL, 8192, PROT_READ, MAP_SHARED, fd, 0);
	error = errno;
	INJECT_ERROR1("GET_CAP_MMAP", image = MAP_FAILED);
	if (image == MAP_FAILED) {
		bam_error(_("failed to mmap file: %s: %s\n"), fname,
		    strerror(error));
		return (BAM_ERROR);
	}

	ident = (uchar_t *)image;
	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
		bam_error(_("%s is not an ELF file.\n"), fname);
		return (BAM_ERROR);
	}
	if (ident[EI_CLASS] != class) {
		bam_error(_("%s is wrong ELF class 0x%x\n"), fname,
		    ident[EI_CLASS]);
		return (BAM_ERROR);
	}

	/*
	 * The GRUB multiboot header must be 32-bit aligned and completely
	 * contained in the 1st 8K of the file.  If the unix binary has
	 * a multiboot header, then it is a 'dboot' kernel.  Otherwise,
	 * this kernel must be booted via multiboot -- we call this a
	 * 'multiboot' kernel.
	 */
	bam_direct = BAM_DIRECT_MULTIBOOT;
	for (m = 0; m < 8192 - sizeof (multiboot_header_t); m += 4) {
		mbh = (void *)(image + m);
		if (mbh->magic == MB_HEADER_MAGIC) {
			BAM_DPRINTF(("%s: is DBOOT unix\n", fcn));
			bam_direct = BAM_DIRECT_DBOOT;
			break;
		}
	}
	(void) munmap(image, 8192);
	(void) close(fd);

	INJECT_ERROR1("GET_CAP_MULTIBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
	if (bam_direct == BAM_DIRECT_DBOOT) {
		if (bam_is_hv == BAM_HV_PRESENT) {
			BAM_DPRINTF(("%s: is xVM system\n", fcn));
		} else {
			BAM_DPRINTF(("%s: is *NOT* xVM system\n", fcn));
		}
	} else {
		BAM_DPRINTF(("%s: is MULTIBOOT unix\n", fcn));
	}

	/* Not a fatal error if this fails */
	get_findroot_cap(osroot);

	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
	return (BAM_SUCCESS);
}

#define	INST_RELEASE	"var/sadm/system/admin/INST_RELEASE"

/*
 * Return true if root has been bfu'ed.  bfu will blow away
 * var/sadm/system/admin/INST_RELEASE, so if it's still there, we can
 * assume the system has not been bfu'ed.
 */
static int
is_bfu_system(const char *root)
{
	static int		is_bfu = -1;
	char			path[PATH_MAX];
	struct stat		sb;
	const char		*fcn = "is_bfu_system()";

	if (is_bfu != -1) {
		BAM_DPRINTF(("%s: already done bfu test. bfu is %s present\n",
		    fcn, is_bfu ? "" : "NOT"));
		return (is_bfu);
	}

	(void) snprintf(path, sizeof (path), "%s/%s", root, INST_RELEASE);
	if (stat(path, &sb) != 0) {
		is_bfu = 1;
		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
	} else {
		is_bfu = 0;
		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
	}
	return (is_bfu);
}

#define	MENU_URL(root)	(is_bfu_system(root) ?		\
	"http://illumos.org/msg/SUNOS-8000-CF" :	\
	"http://illumos.org/msg/SUNOS-8000-AK")

/*
 * Simply allocate a new line and copy in cmd + sep + arg
 */
void
update_line(line_t *linep)
{
	size_t		size;
	const char	*fcn = "update_line()";

	BAM_DPRINTF(("%s: line before update: %s\n", fcn, linep->line));
	free(linep->line);
	size = strlen(linep->cmd) + strlen(linep->sep) + strlen(linep->arg) + 1;
	linep->line = s_calloc(1, size);
	(void) snprintf(linep->line, size, "%s%s%s", linep->cmd, linep->sep,
	    linep->arg);
	BAM_DPRINTF(("%s: line after update: %s\n", fcn, linep->line));
}

static char *
skip_wspace(char *ptr)
{
	const char		*fcn = "skip_wspace()";

	INJECT_ERROR1("SKIP_WSPACE", ptr = NULL);
	if (ptr == NULL) {
		BAM_DPRINTF(("%s: NULL ptr\n", fcn));
		return (NULL);
	}

	BAM_DPRINTF(("%s: ptr on entry: %s\n", fcn, ptr));
	for (; *ptr != '\0'; ptr++) {
		if ((*ptr != ' ') && (*ptr != '\t') &&
		    (*ptr != '\n'))
			break;
	}

	ptr = (*ptr == '\0' ? NULL : ptr);

	BAM_DPRINTF(("%s: ptr on exit: %s\n", fcn, ptr ? ptr : "NULL"));

	return (ptr);
}

static char *
rskip_bspace(char *bound, char *ptr)
{
	const char		*fcn = "rskip_bspace()";
	assert(bound);
	assert(ptr);
	assert(bound <= ptr);
	assert(*bound != ' ' && *bound != '\t' && *bound != '\n');

	BAM_DPRINTF(("%s: ptr on entry: %s\n", fcn, ptr));
	for (; ptr > bound; ptr--) {
		if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
			break;
	}

	BAM_DPRINTF(("%s: ptr on exit: %s\n", fcn, ptr));
	return (ptr);
}

/*
 * The parse_kernel_line function examines a menu.lst kernel line.  For
 * multiboot, this is:
 *
 * kernel <multiboot path> <flags1> <kernel path> <flags2>
 *
 * <multiboot path> is either /platform/i86pc/multiboot or /boot/multiboot
 *
 * <kernel path> may be missing, or may be any full or relative path to unix.
 *	We check for it by looking for a word ending in "/unix".  If it ends
 *	in "kernel/unix", we upgrade it to a 32-bit entry.  If it ends in
 *	"kernel/amd64/unix", we upgrade it to the default entry.  Otherwise,
 *	it's a custom kernel, and we skip it.
 *
 * <flags*> are anything that doesn't fit either of the above - these will be
 *	copied over.
 *
 * For direct boot, the defaults are
 *
 * kernel$ <kernel path> <flags>
 *
 * <kernel path> is one of:
 *	/platform/i86pc/kernel/$ISADIR/unix
 *	/boot/platform/i86pc/kernel/$ISADIR/unix
 *	/platform/i86pc/kernel/unix
 *	/platform/i86pc/kernel/amd64/unix
 *	/boot/platform/i86pc/kernel/unix
 *	/boot/platform/i86pc/kernel/amd64/unix
 *
 * If <kernel path> is any of the last four, the command may also be "kernel".
 *
 * <flags> is anything that isn't <kernel path>.
 *
 * This function is only called to convert a multiboot entry to a dboot entry
 *
 * For safety, we do one more check: if the kernel path starts with /boot,
 * we verify that the new kernel exists before changing it.  This is mainly
 * done for bfu, as it may cause the failsafe archives to be a different
 * boot architecture from the newly bfu'ed system.
 */
static error_t
cvt_kernel_line(line_t *line, const char *osroot, entry_t *entry)
{
	char		path[PATH_MAX], path_64[PATH_MAX];
	char		linebuf[PATH_MAX];
	char		new_arg[PATH_MAX];
	struct stat	sb, sb_64;
	char		*old_ptr;
	char		*unix_ptr;
	char		*flags1_ptr;
	char		*flags2_ptr;
	const char	*fcn = "cvt_kernel_line()";

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

	/*
	 * We only convert multiboot to dboot and nothing else.
	 */
	if (!(entry->flags & BAM_ENTRY_MULTIBOOT)) {
		BAM_DPRINTF(("%s: not MULTIBOOT, not converting\n", fcn));
		return (BAM_SUCCESS);
	}

	if (entry->flags & BAM_ENTRY_FAILSAFE) {
		/*
		 * We're attempting to change failsafe to dboot.
		 * In the bfu case, we may not have a dboot failsafe
		 * kernel i.e. a "unix" under the "/boot" hierarchy.
		 * If so, just emit a message in verbose mode and
		 * return success.
		 */
		BAM_DPRINTF(("%s: trying to convert failsafe to DBOOT\n", fcn));
		(void) snprintf(path, PATH_MAX, "%s%s", osroot,
		    DIRECT_BOOT_FAILSAFE_32);
		(void) snprintf(path_64, PATH_MAX, "%s%s", osroot,
		    DIRECT_BOOT_FAILSAFE_64);
		if (stat(path, &sb) != 0 && stat(path_64, &sb_64) != 0) {
			if (bam_verbose) {
				bam_error(_("bootadm -m upgrade run, but the "
				    "failsafe archives have not been\nupdated. "
				    "Not updating line %d\n"), line->lineNum);
			}
			BAM_DPRINTF(("%s: no FAILSAFE unix, not converting\n",
			    fcn));
			return (BAM_SUCCESS);
		}
	}

	/*
	 * Make sure we have the correct cmd
	 */

	free(line->cmd);
	line->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
	BAM_DPRINTF(("%s: converted kernel cmd to %s\n", fcn, line->cmd));

	assert(sizeof (linebuf) > strlen(line->arg) + 32);
	(void) strlcpy(linebuf, line->arg, sizeof (linebuf));

	old_ptr = strpbrk(linebuf, " \t\n");
	old_ptr = skip_wspace(old_ptr);
	if (old_ptr == NULL) {
		/*
		 * only multiboot and nothing else
		 * i.e. flags1 = unix = flags2 = NULL
		 */
		flags1_ptr = unix_ptr = flags2_ptr = NULL;
		BAM_DPRINTF(("%s: NULL flags1, unix, flags2\n", fcn))
		goto create;
	}

	/*
	 *
	 * old_ptr is either at "flags1" or "unix"
	 */
	if ((unix_ptr = strstr(old_ptr, "/unix")) != NULL) {

		/*
		 * There is a  unix.
		 */
		BAM_DPRINTF(("%s: unix present\n", fcn));

		/* See if there's a flags2 past unix */
		flags2_ptr = unix_ptr + strlen("/unix");
		flags2_ptr = skip_wspace(flags2_ptr);
		if (flags2_ptr) {
			BAM_DPRINTF(("%s: flags2 present: %s\n", fcn,
			    flags2_ptr));
		} else {
			BAM_DPRINTF(("%s: flags2 absent\n", fcn));
		}

		/* see if there is a flags1 before unix */
		unix_ptr = rskip_bspace(old_ptr, unix_ptr);

		if (unix_ptr == old_ptr) {
			flags1_ptr = NULL;
			BAM_DPRINTF(("%s: flags1 absent\n", fcn));
		} else {
			flags1_ptr = old_ptr;
			*unix_ptr = '\0';
			unix_ptr++;
			BAM_DPRINTF(("%s: flags1 present: %s\n", fcn,
			    flags1_ptr));
		}

	} else  {
		/* There is no unix, there is only a bunch of flags */
		flags1_ptr = old_ptr;
		unix_ptr = flags2_ptr = NULL;
		BAM_DPRINTF(("%s: flags1 present: %s, unix, flags2 absent\n",
		    fcn, flags1_ptr));
	}

	/*
	 * With dboot, unix is fixed and is at the beginning. We need to
	 * migrate flags1 and flags2
	 */
create:
	if (entry->flags & BAM_ENTRY_FAILSAFE) {
		(void) snprintf(new_arg, sizeof (new_arg), "%s",
		    DIRECT_BOOT_FAILSAFE_KERNEL);
	} else {
		(void) snprintf(new_arg, sizeof (new_arg), "%s",
		    DIRECT_BOOT_KERNEL);
	}
	BAM_DPRINTF(("%s: converted unix: %s\n", fcn, new_arg));

	if (flags1_ptr != NULL) {
		(void) strlcat(new_arg, " ", sizeof (new_arg));
		(void) strlcat(new_arg, flags1_ptr, sizeof (new_arg));
	}

	if (flags2_ptr != NULL) {
		(void) strlcat(new_arg, " ", sizeof (new_arg));
		(void) strlcat(new_arg, flags2_ptr, sizeof (new_arg));
	}

	BAM_DPRINTF(("%s: converted unix with flags : %s\n", fcn, new_arg));

	free(line->arg);
	line->arg = s_strdup(new_arg);
	update_line(line);
	BAM_DPRINTF(("%s: converted line is: %s\n", fcn, line->line));
	return (BAM_SUCCESS);
}

/*
 * Similar to above, except this time we're looking at a module line,
 * which is quite a bit simpler.
 *
 * Under multiboot, the archive line is:
 *
 * module /platform/i86pc/boot_archive
 *
 * Under directboot, the archive line is:
 *
 * module$ /platform/i86pc/$ISADIR/boot_archive
 *
 * which may be specified exactly as either of:
 *
 * module /platform/i86pc/boot_archive
 * module /platform/i86pc/amd64/boot_archive
 *
 * Under multiboot, the failsafe is:
 *
 * module /boot/x86.miniroot-safe
 *
 * Under dboot, the failsafe is:
 *
 * module$ /boot/$ISADIR/x86.miniroot-safe
 *
 * which may be specified exactly as either of:
 *
 * module /boot/x86.miniroot-safe
 * module /boot/amd64/x86.miniroot-safe
 */
static error_t
cvt_module_line(line_t *line, entry_t *entry)
{
	const char		*fcn = "cvt_module_line()";

	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, line->line));

	/*
	 * We only convert multiboot to dboot and nothing else
	 */
	if (!(entry->flags & BAM_ENTRY_MULTIBOOT)) {
		BAM_DPRINTF(("%s: not MULTIBOOT, not converting\n", fcn));
		return (BAM_SUCCESS);
	}

	if (entry->flags & BAM_ENTRY_FAILSAFE) {
		if (strcmp(line->arg, FAILSAFE_ARCHIVE) == 0) {
			BAM_DPRINTF(("%s: failsafe module line needs no "
			    "conversion: %s\n", fcn, line->arg));
			BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
			return (BAM_SUCCESS);
		}
	} else if (strcmp(line->arg, MULTIBOOT_ARCHIVE) != 0) {
		bam_error(_("module command on line %d not recognized.\n"),
		    line->lineNum);
		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
		return (BAM_MSG);
	}

	free(line->cmd);
	free(line->arg);
	line->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);

	line->arg = s_strdup(entry->flags & BAM_ENTRY_FAILSAFE ?
	    FAILSAFE_ARCHIVE : DIRECT_BOOT_ARCHIVE);

	update_line(line);
	BAM_DPRINTF(("%s: converted module line is: %s\n", fcn, line->line));
	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
	return (BAM_SUCCESS);
}

static void
bam_warn_hand_entries(menu_t *mp, char *osroot)
{
	int		hand_num;
	int		hand_max;
	int		*hand_list;
	int		i;
	entry_t		*entry;
	const char	*fcn = "bam_warn_hand_entries()";

	if (bam_force) {
		/*
		 * No warning needed, we are automatically converting
		 * the "hand" entries
		 */
		BAM_DPRINTF(("%s: force specified, no warnings about hand "
		    "entries\n",  fcn));
		return;
	}

	hand_num = 0;
	hand_max = BAM_ENTRY_NUM;
	hand_list = s_calloc(1, hand_max);

	for (entry = mp->entries; entry; entry = entry->next) {
		if (entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))
			continue;
		BAM_DPRINTF(("%s: found hand entry #: %d\n", fcn,
		    entry->entryNum));
		if (++hand_num > hand_max) {
			hand_max *= 2;
			hand_list = s_realloc(hand_list,
			    hand_max * sizeof (int));
		}
		hand_list[hand_num - 1] = entry->entryNum;
	}

	bam_error(_("bootadm(8) will only upgrade GRUB menu entries added "
	    "by \nbootadm(8) or lu(8). The following entries on %s will "
	    "not be upgraded.\nFor details on manually updating entries, "
	    "see %s\n"), osroot, MENU_URL(osroot));
	bam_print_stderr("Entry Number%s: ", (hand_num > 1) ?
	    "s" : "");
	for (i = 0; i < hand_num; i++) {
		bam_print_stderr("%d ", hand_list[i]);
	}
	bam_print_stderr("\n");
}

static entry_t *
find_matching_entry(
	entry_t *estart,
	char *grubsign,
	char *grubroot,
	int root_opt)
{
	entry_t		*entry;
	line_t		*line;
	char		opt[10];
	const char	*fcn = "find_matching_entry()";

	assert(grubsign);
	assert(root_opt == 0 || root_opt == 1);

	(void) snprintf(opt, sizeof (opt), "%d", root_opt);
	BAM_DPRINTF(("%s: entered. args: %s %s %s\n", fcn, grubsign,
	    grubroot, opt));

	for (entry = estart; entry; entry = entry->next) {

		if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) &&
		    !bam_force) {
			BAM_DPRINTF(("%s: skipping hand entry #: %d\n",
			    fcn, entry->entryNum));
			continue;
		}

		if (entry->flags & BAM_ENTRY_ROOT) {
			for (line = entry->start; line; line = line->next) {
				if (line->cmd == NULL || line->arg == NULL) {
					if (line == entry->end) {
						BAM_DPRINTF(("%s: entry has "
						    "ended\n", fcn));
						break;
					} else {
						BAM_DPRINTF(("%s: skipping "
						    "NULL line\n", fcn));
						continue;
					}
				}
				if (strcmp(line->cmd, menu_cmds[ROOT_CMD])
				    == 0 && strcmp(line->arg, grubroot) == 0) {
					BAM_DPRINTF(("%s: found matching root "
					    "line: %s,%s\n", fcn,
					    line->line, grubsign));
					return (entry);
				}
				if (line == entry->end) {
					BAM_DPRINTF(("%s: entry has ended\n",
					    fcn));
					break;
				}
			}
		} else if (entry->flags & BAM_ENTRY_FINDROOT) {
			for (line = entry->start; line; line = line->next) {
				if (line->cmd == NULL || line->arg == NULL) {
					if (line == entry->end) {
						BAM_DPRINTF(("%s: entry has "
						    "ended\n", fcn));
						break;
					} else {
						BAM_DPRINTF(("%s: skipping "
						    "NULL line\n", fcn));
						continue;
					}
				}
				if (strcmp(line->cmd, menu_cmds[FINDROOT_CMD])
				    == 0 && strcmp(line->arg, grubsign) == 0) {
					BAM_DPRINTF(("%s: found matching "
					    "findroot line: %s,%s\n", fcn,
					    line->line, grubsign));
					return (entry);
				}
				if (line == entry->end) {
					BAM_DPRINTF(("%s: entry has ended\n",
					    fcn));
					break;
				}
			}
		} else if (root_opt) {
			/* Neither root nor findroot */
			BAM_DPRINTF(("%s: no root or findroot and root is "
			    "opt: %d\n", fcn, entry->entryNum));
			return (entry);
		}
	}

	BAM_DPRINTF(("%s: no matching entry found\n", fcn));
	return (NULL);
}

/*
 * The following is a set of routines that attempt to convert the
 * menu entries for the supplied osroot into a format compatible
 * with the GRUB installation on osroot.
 *
 * Each of these conversion routines make no assumptions about
 * the current state of the menu entry, it does its best to
 * convert the menu entry to the new state. In the process
 * we may either upgrade or downgrade.
 *
 * We don't make any heroic efforts at conversion. It is better
 * to be conservative and bail out at the first sign of error. We will
 * in such cases, point the user at the knowledge-base article
 * so that they can upgrade manually.
 */
static error_t
bam_add_findroot(menu_t *mp, char *grubsign, char *grubroot, int root_opt)
{
	entry_t		*entry;
	line_t		*line;
	line_t		*newlp;
	int		update_num;
	char		linebuf[PATH_MAX];
	const char	*fcn = "bam_add_findroot()";

	update_num = 0;

	bam_print(_("converting entries to findroot...\n"));

	entry = find_matching_entry(mp->entries, grubsign, grubroot, root_opt);
	while (entry != NULL) {
		if (entry->flags & BAM_ENTRY_FINDROOT) {
			/* already converted */
			BAM_DPRINTF(("%s: entry %d already converted to "
			    "findroot\n", fcn, entry->entryNum));
			entry = find_matching_entry(entry->next, grubsign,
			    grubroot, root_opt);
			continue;
		}
		for (line = entry->start; line; line = line->next) {
			if (line->cmd == NULL || line->arg == NULL) {
				if (line == entry->end) {
					BAM_DPRINTF(("%s: entry has ended\n",
					    fcn));
					break;
				} else {
					BAM_DPRINTF(("%s: skipping NULL line\n",
					    fcn));
					continue;
				}
			}
			if (strcmp(line->cmd, menu_cmds[TITLE_CMD]) == 0) {
				newlp = s_calloc(1, sizeof (line_t));
				newlp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
				newlp->sep = s_strdup(" ");
				newlp->arg = s_strdup(grubsign);
				(void) snprintf(linebuf, sizeof (linebuf),
				    "%s%s%s", newlp->cmd, newlp->sep,
				    newlp->arg);
				newlp->line = s_strdup(linebuf);
				bam_add_line(mp, entry, line, newlp);
				update_num = 1;
				entry->flags &= ~BAM_ENTRY_ROOT;
				entry->flags |= BAM_ENTRY_FINDROOT;
				BAM_DPRINTF(("%s: added findroot line: %s\n",
				    fcn, newlp->line));
				line = newlp;
			}
			if (strcmp(line->cmd, menu_cmds[ROOT_CMD]) == 0) {
				BAM_DPRINTF(("%s: freeing root line: %s\n",
				    fcn, line->line));
				unlink_line(mp, line);
				line_free(line);
			}
			if (line == entry->end) {
				BAM_DPRINTF(("%s: entry has ended\n", fcn));
				break;
			}
		}
		entry = find_matching_entry(entry->next, grubsign, grubroot,
		    root_opt);
	}

	if (update_num) {
		BAM_DPRINTF(("%s: updated numbering\n", fcn));
		update_numbering(mp);
	}

	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
	return (BAM_SUCCESS);
}

static error_t
bam_add_hv(menu_t *mp, char *grubsign, char *grubroot, int root_opt)
{
	entry_t		*entry;
	const char	*fcn = "bam_add_hv()";

	bam_print(_("adding xVM entries...\n"));

	entry = find_matching_entry(mp->entries, grubsign, grubroot, root_opt);
	while (entry != NULL) {
		if (entry->flags & BAM_ENTRY_HV) {
			BAM_DPRINTF(("%s: entry %d already converted to "
			    "xvm HV\n", fcn, entry->entryNum));
			return (BAM_SUCCESS);
		}
		entry = find_matching_entry(entry->next, grubsign, grubroot,
		    root_opt);
	}

	(void) add_boot_entry(mp, NEW_HV_ENTRY, grubsign, XEN_MENU,
	    XEN_KERNEL_MODULE_LINE, DIRECT_BOOT_ARCHIVE, NULL);

	BAM_DPRINTF(("%s: added xVM HV entry via add_boot_entry()\n", fcn));

	update_numbering(mp);

	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));

	return (BAM_SUCCESS);
}

static error_t
bam_add_dboot(
	menu_t *mp,
	char *osroot,
	char *grubsign,
	char *grubroot,
	int root_opt)
{
	int		msg = 0;
	entry_t		*entry;
	line_t		*line;
	error_t		ret;
	const char 	*fcn = "bam_add_dboot()";

	bam_print(_("converting entries to dboot...\n"));

	entry = find_matching_entry(mp->entries, grubsign, grubroot, root_opt);
	while (entry != NULL) {
		for (line = entry->start; line; line = line->next) {
			if (line->cmd == NULL || line->arg == NULL) {
				if (line == entry->end) {
					BAM_DPRINTF(("%s: entry has ended\n",
					    fcn));
					break;
				} else {
					BAM_DPRINTF(("%s: skipping NULL line\n",
					    fcn));
					continue;
				}
			}

			/*
			 * If we have a kernel$ command, assume it
			 * is dboot already.  If it is not a dboot
			 * entry, something funny is going on and
			 * we will leave it alone
			 */
			if (strcmp(line->cmd, menu_cmds[KERNEL_CMD]) == 0) {
				ret = cvt_kernel_line(line, osroot, entry);
				INJECT_ERROR1("ADD_DBOOT_KERN_ERR",
				    ret = BAM_ERROR);
				INJECT_ERROR1("ADD_DBOOT_KERN_MSG",
				    ret = BAM_MSG);
				if (ret == BAM_ERROR) {
					BAM_DPRINTF(("%s: cvt_kernel_line() "
					    "failed\n", fcn));
					return (ret);
				} else if (ret == BAM_MSG) {
					msg = 1;
					BAM_DPRINTF(("%s: BAM_MSG returned "
					    "from cvt_kernel_line()\n", fcn));
				}
			}
			if (strcmp(line->cmd, menu_cmds[MODULE_CMD]) == 0) {
				ret = cvt_module_line(line, entry);
				INJECT_ERROR1("ADD_DBOOT_MOD_ERR",
				    ret = BAM_ERROR);
				INJECT_ERROR1("ADD_DBOOT_MOD_MSG",
				    ret = BAM_MSG);
				if (ret == BAM_ERROR) {
					BAM_DPRINTF(("%s: cvt_module_line() "
					    "failed\n", fcn));
					return (ret);
				} else if (ret == BAM_MSG) {
					BAM_DPRINTF(("%s: BAM_MSG returned "
					    "from cvt_module_line()\n", fcn));
					msg = 1;
				}
			}

			if (line == entry->end) {
				BAM_DPRINTF(("%s: entry has ended\n", fcn));
				break;
			}
		}
		entry = find_matching_entry(entry->next, grubsign, grubroot,
		    root_opt);
	}

	ret = msg ? BAM_MSG : BAM_SUCCESS;
	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, ret));
	return (ret);
}

/*ARGSUSED*/
error_t
upgrade_menu(menu_t *mp, char *osroot, char *menu_root)
{
	char		*osdev;
	char		*grubsign;
	char		*grubroot;
	int		ret1;
	int		ret2;
	int		ret3;
	const char	*fcn = "upgrade_menu()";

	assert(osroot);
	assert(menu_root);

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

	/*
	 * We only support upgrades. Xen may not be present
	 * on smaller metaclusters so we don't check for that.
	 */
	if (bam_is_findroot != BAM_FINDROOT_PRESENT ||
	    bam_direct != BAM_DIRECT_DBOOT) {
		bam_error(_("automated downgrade of GRUB menu to older "
		    "version not supported.\n"));
		return (BAM_ERROR);
	}

	/*
	 * First get the GRUB signature
	 */
	osdev = get_special(osroot);
	INJECT_ERROR1("UPGRADE_OSDEV", osdev = NULL);
	if (osdev == NULL) {
		bam_error(_("cant find special file for mount-point %s\n"),
		    osroot);
		return (BAM_ERROR);
	}

	grubsign = get_grubsign(osroot, osdev);
	INJECT_ERROR1("UPGRADE_GRUBSIGN", grubsign = NULL);
	if (grubsign == NULL) {
		free(osdev);
		bam_error(_("cannot find GRUB signature for %s\n"), osroot);
		return (BAM_ERROR);
	}

	/* not fatal if we can't get grubroot */
	grubroot = get_grubroot(osroot, osdev, menu_root);
	INJECT_ERROR1("UPGRADE_GRUBROOT", grubroot = NULL);

	free(osdev);

	ret1 = bam_add_findroot(mp, grubsign,
	    grubroot, root_optional(osroot, menu_root));
	INJECT_ERROR1("UPGRADE_ADD_FINDROOT", ret1 = BAM_ERROR);
	if (ret1 == BAM_ERROR)
		goto abort;

	if (bam_is_hv == BAM_HV_PRESENT) {
		ret2 = bam_add_hv(mp, grubsign, grubroot,
		    root_optional(osroot, menu_root));
		INJECT_ERROR1("UPGRADE_ADD_HV", ret2 = BAM_ERROR);
		if (ret2 == BAM_ERROR)
			goto abort;
	} else
		ret2 = BAM_SUCCESS;

	ret3 = bam_add_dboot(mp, osroot, grubsign,
	    grubroot, root_optional(osroot, menu_root));
	INJECT_ERROR1("UPGRADE_ADD_DBOOT", ret3 = BAM_ERROR);
	if (ret3 == BAM_ERROR)
		goto abort;

	if (ret1 == BAM_MSG || ret2 == BAM_MSG || ret3 == BAM_MSG) {
		bam_error(_("one or more GRUB menu entries were not "
		    "automatically upgraded\nFor details on manually "
		    "updating entries, see %s\n"), MENU_URL(osroot));
	} else {
		bam_warn_hand_entries(mp, osroot);
	}

	free(grubsign);

	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, BAM_WRITE));
	return (BAM_WRITE);

abort:
	free(grubsign);
	bam_error(_("error upgrading GRUB menu entries on %s. Aborting.\n"
	    "For details on manually updating entries, see %s\n"), osroot,
	    MENU_URL(osroot));
	return (BAM_ERROR);
}