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 /*
  23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2012 Daniil Lunev. All rights reserved.
  28  */
  29 
  30 #include <stdio.h>
  31 #include <errno.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 #include <unistd.h>
  35 #include <alloca.h>
  36 #include <ctype.h>
  37 #include <sys/types.h>
  38 
  39 #include "message.h"
  40 #include "bootadm.h"
  41 
  42 #define HYPER_KERNEL_DIR                "/platform/i86xpv/kernel"
  43 #define METAL_KERNEL_DIR                "/platform/i86pc/kernel"
  44 
  45 #define BOOTRC_FILE                     "/boot/solaris/bootenv.rc"
  46 #define ZFS_BOOTSTR                     "$ZFS_BOOTFS"
  47 
  48 #define BFLAG                           "-B"
  49 #define DEFAULT_SERIAL                  "9600,8,n,1"
  50 
  51 #define TTYXMODE_TO_COMNUM(ttyxmode)    ((int)(*((ttyxmode) + 3) - '`'))
  52 #define COMNAME_TO_COMNUM(comname)      ((int)(*((comname) + 3) - '0'))
  53 
  54 #define WHITESPC(x)                     (x)
  55 
  56 static char *serial_config[2] = { NULL, NULL };
  57 static char *console_dev = NULL;
  58 
  59 static char *bootenv_rc_serial[2] = { NULL, NULL };
  60 static char *bootenv_rc_console = NULL;
  61 
  62 static unsigned zfs_boot = 0;
  63 
  64 /*
  65  * Append the string pointed to by "str" to the string pointed to by "orig"
  66  * adding the delimeter "delim" in between.
  67  *
  68  * Return a pointer to the new string or NULL, if we were passed a bad string.
  69  */
  70 static char *
  71 append_str(char *orig, char *str, char *delim)
  72 {
  73         char *newstr;
  74         int len;
  75 
  76         if ((str == NULL) || (delim == NULL))
  77                 return (NULL);
  78 
  79         if ((orig == NULL) || (*orig == NULL)) {
  80                 /*
  81                  * Return a pointer to a copy of the path so a caller can
  82                  * always rely upon being able to free() a returned pointer.
  83                  */
  84                 return (s_strdup(str));
  85         }
  86 
  87         len = strlen(orig) + strlen(str) + strlen(delim) + 1;
  88         if ((newstr = malloc(len)) == NULL) {
  89                 bam_error(NO_MEM, len);
  90                 bam_exit(1);
  91         }
  92 
  93         (void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
  94         return (newstr);
  95 }
  96 
  97 /*
  98  * Replace the substring "old_str" in a path with the substring "new_str"
  99  *
 100  * Return a pointer to the modified string.
 101  */
 102 static char *
 103 modify_path(char *path, char *old_str, char *new_str)
 104 {
 105         char *newpath;
 106         char *pc;
 107         int len;
 108 
 109         /*
 110          * Return a pointer to a copy of the path so a caller can always rely
 111          * upon being able to free() a returned pointer.
 112          */
 113         if ((pc = strstr(path, old_str)) == NULL)
 114                 return (s_strdup(path));
 115 
 116         /*
 117          * Allocate space for duplicate of path with name changes and
 118          * NULL terminating byte
 119          */
 120         len = strlen(path) - strlen(old_str) + strlen(new_str) + 1;
 121 
 122         if ((newpath = malloc(len)) == NULL) {
 123                 bam_error(NO_MEM, len);
 124                 bam_exit(1);
 125         }
 126 
 127         (void) strlcpy(newpath, path, (pc - path) + 1);
 128         pc += strlen(old_str);
 129 
 130         (void) strcat(newpath, new_str);
 131         (void) strcat(newpath, pc);
 132         return (newpath);
 133 }
 134 
 135 /*
 136  * Set "token" to be the the string starting from the pointer "str" delimited
 137  * by any character in the string "delim" or the end of the string, but IGNORE
 138  * any characters between single or double quotes.
 139  *
 140  * Return a pointer to the next non-whitespace character after the delimiter
 141  * or NULL if we hit the end of the string. Also return NULL upon failure to
 142  * find any characters from the delimeter string or upon failure to allocate
 143  * memory for the new token string.
 144  */
 145 static char *
 146 get_token(char **token, char *str, char *delim)
 147 {
 148         char *dp;
 149         char *start = str;
 150         unsigned len;
 151 
 152         *token = NULL;
 153 
 154         if ((str == NULL) || (*str == NULL))
 155                 return (NULL);
 156 
 157         do {
 158                 if ((*str == '\'') || (*str == '"')) {
 159                         char quote = *str++;
 160 
 161                         while ((*str != NULL) && (*str != quote))
 162                                 str++;
 163 
 164                         /* no matching quote found in string */
 165                         if (*str++ == NULL)
 166                                 return (NULL);
 167                 }
 168 
 169                 /* look for a character from the delimiter string */
 170                 for (dp = delim; ((*dp != NULL) && (*dp != *str)); dp++)
 171                         ;
 172 
 173                 if (*dp != NULL) {
 174                         len = str - start + 1;
 175 
 176                         /* found a delimiter, so create a token string */
 177                         if ((*token = malloc(len)) == NULL) {
 178                                 bam_error(NO_MEM, len);
 179                                 bam_exit(1);
 180                         }
 181 
 182                         (void) strlcpy(*token, start, len);
 183 
 184                         while (isspace((int)*++str))
 185                                 ;
 186 
 187                         return (str);
 188                 }
 189         } while (*str++ != NULL);
 190 
 191         /* if we hit the end of the string, the token is the whole string  */
 192         *token = s_strdup(start);
 193         return (NULL);
 194 }
 195 
 196 /*
 197  * Convert a metal "console" device name to an equivalent one suitable for
 198  * use with the hypervisor.
 199  *
 200  * Default to "vga" if we can't parse the console device.
 201  */
 202 static void
 203 console_metal_to_hyper(char *console)
 204 {
 205         if ((*console == '\'') || (*console == '"'))
 206                 console++;
 207 
 208         if (strncmp(console, "ttya", 4) == 0)
 209                 console_dev = "console=com1";
 210         else if (strncmp(console, "ttyb", 4) == 0)
 211                 console_dev = "console=com2";
 212         else
 213                 console_dev = "console=vga";
 214 }
 215 
 216 static int
 217 set_serial_rate(int com, char *rate)
 218 {
 219         char **rp = &serial_config[com - 1];
 220 
 221         if ((com < 1) || (com > 2))
 222                 return (-1);
 223 
 224         /*
 225          * If rate is a NULL pointer, erase any existing serial configuration
 226          * for this serial port.
 227          */
 228         if (rate == NULL) {
 229                 if (*rp != NULL) {
 230                         free(*rp);
 231                         *rp = NULL;
 232                 }
 233                 return (0);
 234         }
 235 
 236         *rp = s_realloc(*rp, strlen(rate) + 1);
 237         (void) strcpy(*rp, rate);
 238         return (0);
 239 }
 240 
 241 /*
 242  * Convert "metal" serial port parameters to values compatible with the
 243  * hypervisor.
 244  *
 245  * Return 0 on success, otherwise -1.
 246  */
 247 static int
 248 serial_metal_to_hyper(char *metal_port, char *metal_serial)
 249 {
 250 #define COM_RATE_LEN    16      /* strlen("com1=115200,8n1") */
 251 
 252         char com_rate[COM_RATE_LEN];
 253 
 254         unsigned com, baud, bits, stop;
 255         char parity, handshake;
 256 
 257         if ((strcmp(metal_port, "ttya-mode") == 0) ||
 258             (strcmp(metal_port, "ttyb-mode") == 0))
 259                 com = TTYXMODE_TO_COMNUM(metal_port);
 260         else
 261                 return (-1);
 262 
 263         if ((*metal_serial == '\'') || (*metal_serial == '"'))
 264                 metal_serial++;
 265 
 266         /*
 267          * Check if it's specified as the default rate; if so it defaults to
 268          * "auto" and we need not set it for they hypervisor.
 269          */
 270         if (strncmp(metal_serial, DEFAULT_SERIAL,
 271             strlen(DEFAULT_SERIAL)) == 0) {
 272                 (void) set_serial_rate(com, NULL);
 273                 return (0);
 274         }
 275 
 276         /* read the serial port format as set forth in common/io/asy.c */
 277         if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
 278             &handshake) != 5)
 279                 return (-1);
 280 
 281         /* validate serial port parameters */
 282         if (((bits < 5) || (bits > 8)) || (stop > 1) ||
 283             ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
 284             ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
 285                 return (-1);
 286 
 287         /* validate baud rate */
 288         switch (baud) {
 289                 case 150:
 290                 case 300:
 291                 case 600:
 292                 case 1200:
 293                 case 2400:
 294                 case 4800:
 295                 case 9600:
 296                 case 19200:
 297                 case 38400:
 298                 case 57600:
 299                 case 115200:
 300                         break;
 301 
 302                 default:
 303                         return (-1);
 304         }
 305 
 306         /*
 307          * The hypervisor has no way to specify a handshake method, so it gets
 308          * quietly dropped in the conversion.
 309          */
 310         (void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
 311             bits, parity, stop);
 312         (void) set_serial_rate(com, com_rate);
 313         return (0);
 314 }
 315 
 316 /*
 317  * Convert "name=value" metal options to values suitable for use with the
 318  * hypervisor.
 319  *
 320  * Our main concerns are the console device and serial port settings.
 321  *
 322  * Return values:
 323  *
 324  *    -1:       Unparseable line
 325  *    0:        Success
 326  *    (n > 0):       A property unimportant to us
 327  */
 328 static int
 329 cvt_metal_option(char *optstr)
 330 {
 331         char *value;
 332         unsigned namlen;
 333 
 334         if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
 335                 zfs_boot = 1;
 336                 return (0);
 337         }
 338 
 339         if ((value = strchr(optstr, '=')) == NULL)
 340                 return (-1);
 341 
 342         namlen = value - optstr;
 343 
 344         if (*++value == NULL)
 345                 return (1);
 346 
 347         if (strncmp(optstr, "console", namlen) == 0) {
 348                 console_metal_to_hyper(value);
 349                 return (0);
 350         }
 351 
 352         if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
 353             (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
 354                 char *port = strndupa(optstr, namlen);
 355 
 356                 return (serial_metal_to_hyper(port, value));
 357         }
 358 
 359         return (1);
 360 }
 361 
 362 /*
 363  * Convert "name=value" properties for use with a bare metal kernel
 364  *
 365  * Our main concerns are the console setting and serial port modes.
 366  *
 367  * Return values:
 368  *
 369  *    -1:       Unparseable line
 370  *    0:        Success
 371  *    (n > 0):       A property unimportant to us
 372  */
 373 static int
 374 cvt_hyper_option(char *optstr)
 375 {
 376 #define SER_LEN         15      /* strlen("115200,8,n,1,-") + 1 */
 377 
 378         char ser[SER_LEN];
 379         char *value;
 380 
 381         unsigned namlen;
 382 
 383         unsigned baud;
 384         char bits, parity, stop;
 385 
 386         if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
 387                 zfs_boot = 1;
 388                 return (0);
 389         }
 390 
 391         /*
 392          * If there's no "=" in the token, it's likely a standalone
 393          * hypervisor token we don't care about (e.g. "noreboot" or
 394          * "nosmp") so we ignore it.
 395          */
 396         if ((value = strchr(optstr, '=')) == NULL)
 397                 return (1);
 398 
 399         namlen = value - optstr;
 400 
 401         if (*++value == NULL)
 402                 return (1);
 403 
 404         /*
 405          * Note that we use strncmp against the values because the
 406          * hypervisor allows setting console parameters for both the
 407          * console and debugger via the format:
 408          *
 409          *   console=cons_dev,debug_dev
 410          *
 411          * and we only care about "cons_dev."
 412          *
 413          * This also allows us to extract "comN" from hypervisor constructs
 414          * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
 415          *
 416          * Default the console device to "text" if it was "vga" or was
 417          * unparseable.
 418          */
 419         if (strncmp(optstr, "console", namlen) == 0) {
 420                 /* ignore the "console=hypervisor" option */
 421                 if (strcmp(value, "hypervisor") == 0)
 422                         return (0);
 423 
 424                 if (strncmp(value, "com1", 4) == 0)
 425                         console_dev = "ttya";
 426                 else if (strncmp(value, "com2", 4) == 0)
 427                         console_dev = "ttyb";
 428                 else
 429                         console_dev = "text";
 430         }
 431 
 432         /* serial port parameter conversion */
 433 
 434         if ((strncmp(optstr, "com1", namlen) == 0) ||
 435             (strncmp(optstr, "com2", namlen) == 0)) {
 436                 unsigned com = COMNAME_TO_COMNUM(optstr);
 437 
 438                 /*
 439                  * Check if it's "auto" - if so, use the default setting
 440                  * of "9600,8,n,1,-".
 441                  *
 442                  * We can't just assume the serial port will default to
 443                  * "9600,8,n,1" as there could be a directive in bootenv.rc
 444                  * that would set it to some other value and we want the serial
 445                  * parameters to be the same as that used by the hypervisor.
 446                  */
 447                 if (strcmp(value, "auto") == 0) {
 448                         (void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
 449                 } else {
 450                         /*
 451                          * Extract the "B,PS" setting from the com line; ignore
 452                          * other settings like io_base or IRQ.
 453                          */
 454                         if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
 455                             &stop) != 4)
 456                                 return (-1);
 457 
 458                         /* validate serial port parameters */
 459                         if (((stop != '0') && (stop != '1')) ||
 460                             ((bits < '5') && (bits > '8')) ||
 461                             ((parity != 'n') && (parity != 'e') &&
 462                             (parity != 'o')))
 463                                 return (-1);
 464 
 465                         /* validate baud rate */
 466                         switch (baud) {
 467                                 case 150:
 468                                 case 300:
 469                                 case 600:
 470                                 case 1200:
 471                                 case 2400:
 472                                 case 4800:
 473                                 case 19200:
 474                                 case 38400:
 475                                 case 57600:
 476                                 case 115200:
 477                                         break;
 478 
 479                                 default:
 480                                         return (-1);
 481                         }
 482 
 483                         /*
 484                          * As the hypervisor has no way to denote handshaking
 485                          * in its serial port settings, emit a metal serial
 486                          * port configuration with none as well.
 487                          */
 488                         (void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
 489                             bits, parity, stop);
 490                 }
 491 
 492                 if (set_serial_rate(com, ser) != 0)
 493                         return (-1);
 494 
 495                 return (0);
 496         }
 497 
 498         return (1);
 499 }
 500 
 501 /*
 502  * Parse a hardware kernel's "kernel$" specifier into parameters we can then
 503  * use to construct an appropriate "module$" line that can be used to specify
 504  * how to boot the hypervisor's dom0.
 505  *
 506  * Return values:
 507  *
 508  *      -1: error parsing kernel path
 509  *       0: success
 510  *       1: kernel already a hypervisor kernel
 511  */
 512 static int
 513 cvt_metal_kernel(char *kernstr, char **path)
 514 {
 515         char *token, *parsestr;
 516 
 517         parsestr = get_token(path, kernstr, " \t,");
 518         if (*path == NULL)
 519                 return (-1);
 520 
 521         /*
 522          * If the metal kernel specified contains the name of the hypervisor,
 523          * we're probably trying to convert an entry already setup to run the
 524          * hypervisor, so error out now.
 525          */
 526         if (strstr(*path, XEN_MENU) != NULL) {
 527                 bam_error(ALREADY_HYPER);
 528                 free(*path);
 529                 *path = NULL;
 530                 return (1);
 531         }
 532 
 533         /* if the path was the last item on the line, that's OK. */
 534         if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
 535                 if (token != NULL)
 536                         free(token);
 537                 return (0);
 538         }
 539 
 540         /* if the next token is "-B" process boot options */
 541         if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
 542                 free(token);
 543                 return (0);
 544         }
 545 
 546         free(token);
 547 
 548         while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
 549                 (void) cvt_metal_option(token);
 550                 free(token);
 551         }
 552 
 553         if (token != NULL) {
 554                 (void) cvt_metal_option(token);
 555                 free(token);
 556         }
 557 
 558         return (0);
 559 }
 560 
 561 /*
 562  * Parse a hypervisor's "kernel$" line into parameters that can be used to
 563  * help build an appropriate "kernel$" line for booting a bare metal kernel.
 564  *
 565  * Return 0 on success, non-zero on failure.
 566  */
 567 static int
 568 cvt_hyper_kernel(char *kernel)
 569 {
 570         char *token, *parsestr;
 571 
 572         parsestr = get_token(&token, kernel, " \t,");
 573 
 574         if (token == NULL)
 575                 return (-1);
 576 
 577         /*
 578          * If the hypervisor kernel specified lives in the metal kernel
 579          * directory, we're probably trying to convert an entry already setup
 580          * to run on bare metal, so error out now.
 581          */
 582         if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
 583                 bam_error(ALREADY_METAL);
 584                 free(token);
 585                 return (-1);
 586         }
 587 
 588         free(token);
 589 
 590         /* check for kernel options */
 591         while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
 592                 (void) cvt_hyper_option(token);
 593                 free(token);
 594         }
 595 
 596         if (token != NULL) {
 597                 (void) cvt_hyper_option(token);
 598                 free(token);
 599         }
 600 
 601         return (0);
 602 }
 603 
 604 /*
 605  * Parse a hypervisor's "module$" line into parameters that can be used to
 606  * help build an appropriate "kernel$" line for booting a bare metal kernel.
 607  */
 608 static void
 609 cvt_hyper_module(char *modstr, char **path)
 610 {
 611         char *token = NULL;
 612         char *parsestr = modstr;
 613 
 614         /*
 615          * If multiple pathnames exist on the module$ line, we just want
 616          * the last one.
 617          */
 618         while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
 619                 if (*parsestr != '/')
 620                         break;
 621                 else
 622                         free(*path);
 623         }
 624 
 625         /* if the path was the last item on the line, that's OK. */
 626         if ((parsestr == NULL) ||
 627             ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
 628                 if (token != NULL)
 629                         free(token);
 630                 return;
 631         }
 632 
 633         if (token == NULL)
 634                 return;
 635 
 636         /* check for "-B" option */
 637         if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
 638                 free(token);
 639                 return;
 640         }
 641 
 642         free(token);
 643 
 644         /* check for kernel options */
 645         while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
 646                 (void) cvt_hyper_option(token);
 647                 free(token);
 648         }
 649 
 650         if (token != NULL) {
 651                 (void) cvt_hyper_option(token);
 652                 free(token);
 653         }
 654 }
 655 
 656 static void
 657 parse_bootenvrc(char *osroot)
 658 {
 659 #define LINEBUF_SZ      1024
 660 
 661         FILE *fp;
 662         char *rcpath;
 663         char line[LINEBUF_SZ];  /* make line buffer large but not ridiculous */
 664         int len;
 665 
 666         assert(osroot);
 667 
 668         len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
 669         rcpath = alloca(len);
 670 
 671         (void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
 672 
 673         /* if we couldn't open the bootenv.rc file, ignore the issue. */
 674         if ((fp = fopen(rcpath, "r")) == NULL) {
 675                 BAM_DPRINTF((D_NO_BOOTENVRC, rcpath, strerror(errno)));
 676                 return;
 677         }
 678 
 679         while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
 680                 char *parsestr, *token;
 681                 int port = 0;
 682 
 683                 /* we're only interested in parsing "setprop" directives. */
 684                 if (strncmp(line, "setprop", 7) != NULL)
 685                         continue;
 686 
 687                 /* eat initial "setprop" */
 688                 if ((parsestr = get_token(&token, line, " \t")) == NULL) {
 689                         if (token != NULL)
 690                                 free(token);
 691 
 692                         continue;
 693                 }
 694 
 695                 if (strcmp(token, "setprop") != 0) {
 696                         free(token);
 697                         continue;
 698                 }
 699 
 700                 free(token);
 701 
 702                 /* get property name */
 703                 if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
 704                         if (token != NULL)
 705                                 free(token);
 706 
 707                         continue;
 708                 }
 709 
 710                 if (strcmp(token, "console") == 0) {
 711                         free(token);
 712 
 713                         /* get console property value */
 714                         parsestr = get_token(&token, parsestr, " \t");
 715                         if (token == NULL)
 716                                 continue;
 717 
 718                         if (bootenv_rc_console != NULL)
 719                                 free(bootenv_rc_console);
 720 
 721                         bootenv_rc_console = s_strdup(token);
 722                         continue;
 723                 }
 724 
 725                 /* check if it's a serial port setting */
 726                 if (strcmp(token, "ttya-mode") == 0) {
 727                         free(token);
 728                         port = 0;
 729                 } else if (strcmp(token, "ttyb-mode") == 0) {
 730                         free(token);
 731                         port = 1;
 732                 } else {
 733                         /* nope, so check the next line */
 734                         free(token);
 735                         continue;
 736                 }
 737 
 738                 /* get serial port setting */
 739                 parsestr = get_token(&token, parsestr, " \t");
 740 
 741                 if (token == NULL)
 742                         continue;
 743 
 744                 if (bootenv_rc_serial[port] != NULL)
 745                         free(bootenv_rc_serial[port]);
 746 
 747                 bootenv_rc_serial[port] = s_strdup(token);
 748                 free(token);
 749         }
 750 
 751         (void) fclose(fp);
 752 }
 753 
 754 error_t
 755 cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
 756 {
 757         const char *fcn = "cvt_to_hyper()";
 758 
 759         line_t *lp;
 760         entry_t *ent;
 761         size_t len, zfslen;
 762 
 763         char *newstr;
 764         char *osdev;
 765 
 766         char *title = NULL;
 767         char *findroot = NULL;
 768         char *bootfs = NULL;
 769         char *kernel = NULL;
 770         char *opts = NULL;
 771         char *mod_kernel = NULL;
 772         char *module = NULL;
 773         char *tmp = NULL;
 774 
 775         char *kern_path = NULL;
 776         char *kern_bargs = NULL;
 777 
 778         int curdef, newdef;
 779         int kp_allocated = 0;
 780         int ret = BAM_ERROR;
 781 
 782         assert(osroot);
 783 
 784         BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, extra_args));
 785 
 786         /*
 787          * First just check to verify osroot is a sane directory.
 788          */
 789         if ((osdev = get_special(osroot)) == NULL) {
 790                 bam_error(CANT_FIND_SPECIAL, osroot);
 791                 return (BAM_ERROR);
 792         }
 793 
 794         free(osdev);
 795 
 796         /*
 797          * While the effect is purely cosmetic, if osroot is "/" don't
 798          * bother prepending it to any paths as they are constructed to
 799          * begin with "/" anyway.
 800          */
 801         if (strcmp(osroot, "/") == 0)
 802                 osroot = "";
 803 
 804         /*
 805          * Found the GRUB signature on the target partitions, so now get the
 806          * default GRUB boot entry number from the menu.lst file
 807          */
 808         curdef = atoi(mp->curdefault->arg);
 809 
 810         /* look for the first line of the matching boot entry */
 811         for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
 812             ent = ent->next)
 813                 ;
 814 
 815         /* couldn't find it, so error out */
 816         if (ent == NULL) {
 817                 bam_error(CANT_FIND_DEFAULT, curdef);
 818                 goto abort;
 819         }
 820 
 821         /*
 822          * We found the proper menu entry, so first we need to process the
 823          * bootenv.rc file to look for boot options the hypervisor might need
 824          * passed as kernel start options such as the console device and serial
 825          * port parameters.
 826          *
 827          * If there's no bootenv.rc, it's not an issue.
 828          */
 829         parse_bootenvrc(osroot);
 830 
 831         if (bootenv_rc_console != NULL)
 832                 console_metal_to_hyper(bootenv_rc_console);
 833 
 834         if (bootenv_rc_serial[0] != NULL)
 835                 (void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);
 836 
 837         if (bootenv_rc_serial[1] != NULL)
 838                 (void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);
 839 
 840         /*
 841          * Now process the entry itself.
 842          */
 843         for (lp = ent->start; lp != NULL; lp = lp->next) {
 844                 /*
 845                  * Process important lines from menu.lst boot entry.
 846                  */
 847                 if (lp->flags == BAM_TITLE) {
 848                         title = strdupa(lp->arg);
 849                 } else if (lp->cmd != NULL) {
 850                         if (strcmp(lp->cmd, "pool_label") == 0) {
 851                                 findroot = strdupa(lp->arg);
 852                         } else if (strcmp(lp->cmd, "data_set") == 0) {
 853                                 bootfs = strdupa(lp->arg);
 854                         } else if (strcmp(lp->cmd, "kernel_options") == 0) {
 855                                 opts = strdupa(lp->arg);
 856                         } else if (strcmp(lp->cmd,
 857                             menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
 858                                 module = strdupa(lp->arg);
 859                         } else if ((strcmp(lp->cmd,
 860                             menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
 861                             (ret = cvt_metal_kernel(lp->arg,
 862                             &kern_path)) != 0) {
 863                                 if (ret < 0) {
 864                                         ret = BAM_ERROR;
 865                                         bam_error(KERNEL_NOT_PARSEABLE, curdef);
 866                                 } else
 867                                         ret = BAM_NOCHANGE;
 868 
 869                                 goto abort;
 870                         }
 871                 }
 872 
 873                 if (lp == ent->end)
 874                         break;
 875         }
 876 
 877         
 878         /*
 879          * If findroot, module or kern_path are NULL, the boot entry is
 880          * malformed.
 881          */
 882         if (findroot == NULL) {
 883                 bam_error(FINDROOT_NOT_FOUND, curdef);
 884                 goto abort;
 885         }
 886 
 887         if (module == NULL) {
 888                 bam_error(MODULE_NOT_PARSEABLE, curdef);
 889                 goto abort;
 890         }
 891 
 892         if (kern_path == NULL) {
 893                 bam_error(KERNEL_NOT_FOUND, curdef);
 894                 goto abort;
 895         }
 896 
 897         /* assemble new kernel and module arguments from parsed values */
 898         if (console_dev != NULL) {
 899                 kern_bargs = s_strdup(console_dev);
 900 
 901                 if (serial_config[0] != NULL) {
 902                         newstr = append_str(kern_bargs, serial_config[0], " ");
 903                         free(kern_bargs);
 904                         kern_bargs = newstr;
 905                 }
 906 
 907                 if (serial_config[1] != NULL) {
 908                         newstr = append_str(kern_bargs, serial_config[1], " ");
 909                         free(kern_bargs);
 910                         kern_bargs = newstr;
 911                 }
 912         }
 913 
 914         if ((extra_args != NULL) && (*extra_args != NULL)) {
 915                 newstr = append_str(kern_bargs, extra_args, " ");
 916                 free(kern_bargs);
 917                 kern_bargs = newstr;
 918         }
 919 
 920         len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
 921             WHITESPC(1) + 1;
 922 
 923         kernel = alloca(len);
 924 
 925         if (kern_bargs != NULL) {
 926                 if (*kern_bargs != NULL)
 927                         (void) snprintf(kernel, len, "%s%s %s", osroot,
 928                             XEN_MENU, kern_bargs);
 929 
 930                 free(kern_bargs);
 931         } else {
 932                 (void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
 933         }
 934 
 935         /*
 936          * Change the kernel directory from the metal version to that needed for
 937          * the hypervisor.  Convert either "direct boot" path to the default
 938          * path.
 939          */
 940         if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
 941             (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
 942                 kern_path = HYPERVISOR_KERNEL;
 943         } else {
 944                 newstr = modify_path(kern_path, METAL_KERNEL_DIR,
 945                     HYPER_KERNEL_DIR);
 946                 free(kern_path);
 947                 kern_path = newstr;
 948                 kp_allocated = 1;
 949         }
 950 
 951         /*
 952          * We need to allocate space for the kernel path (twice) plus an
 953          * intervening space, possibly the ZFS boot string, and NULL,
 954          * of course.
 955          */
 956         len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
 957         zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
 958 
 959         mod_kernel = alloca(len + zfslen);
 960         if (opts)
 961                 (void) snprintf(mod_kernel, len + strlen(opts) + 1, "%s %s %s", kern_path, kern_path, opts);
 962         else
 963                 (void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
 964 
 965         if (kp_allocated)
 966                 free(kern_path);
 967 
 968         if (zfs_boot) {
 969                 char *zfsstr = alloca(zfslen + 1);
 970 
 971                 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
 972                 (void) strcat(mod_kernel, zfsstr);
 973         }
 974 
 975         /* shut off warning messages from the entry line parser */
 976         if (ent->flags & BAM_ENTRY_BOOTADM)
 977                 ent->flags &= ~BAM_ENTRY_BOOTADM;
 978 
 979         BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
 980         BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, mod_kernel));
 981 
 982         if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
 983             module, bootfs)) == BAM_ERROR)
 984                 return (newdef);
 985 
 986         /*
 987          * Now try to delete the current default entry from the menu and add
 988          * the new hypervisor entry with the parameters we've setup.
 989          */
 990         if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
 991                 newdef--;
 992         else
 993                 bam_print(NEW_BOOT_ENTRY, title);
 994 
 995         /*
 996          * If we successfully created the new entry, set the default boot
 997          * entry to that entry and let the caller know the new menu should
 998          * be written out.
 999          */
1000         return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
1001 
1002 abort:
1003         if (ret != BAM_NOCHANGE)
1004                 bam_error(HYPER_ABORT, ((*osroot == NULL) ? "/" : osroot));
1005 
1006         return (ret);
1007 }
1008 
1009 /*ARGSUSED*/
1010 error_t
1011 cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
1012 {
1013         const char *fcn = "cvt_to_metal()";
1014 
1015         line_t *lp;
1016         entry_t *ent;
1017         size_t len, zfslen;
1018 
1019         char *delim = ",";
1020         char *newstr;
1021         char *osdev;
1022 
1023         char *title = NULL;
1024         char *findroot = NULL;
1025         char *bootfs = NULL;
1026         char *kernel = NULL;
1027         char *module = NULL;
1028 
1029         char *barchive_path = DIRECT_BOOT_ARCHIVE;
1030         char *kern_path = NULL;
1031 
1032         int curdef, newdef;
1033         int emit_bflag = 1;
1034         int ret = BAM_ERROR;
1035 
1036         assert(osroot);
1037 
1038         BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, ""));
1039 
1040         /*
1041          * First just check to verify osroot is a sane directory.
1042          */
1043         if ((osdev = get_special(osroot)) == NULL) {
1044                 bam_error(CANT_FIND_SPECIAL, osroot);
1045                 return (BAM_ERROR);
1046         }
1047 
1048         free(osdev);
1049 
1050         /*
1051          * Found the GRUB signature on the target partitions, so now get the
1052          * default GRUB boot entry number from the menu.lst file
1053          */
1054         curdef = atoi(mp->curdefault->arg);
1055 
1056         /* look for the first line of the matching boot entry */
1057         for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
1058             ent = ent->next)
1059                 ;
1060 
1061         /* couldn't find it, so error out */
1062         if (ent == NULL) {
1063                 bam_error(CANT_FIND_DEFAULT, curdef);
1064                 goto abort;
1065         }
1066 
1067         /*
1068          * Now process the entry itself.
1069          */
1070         for (lp = ent->start; lp != NULL; lp = lp->next) {
1071                 /*
1072                  * Process important lines from menu.lst boot entry.
1073                  */
1074                 if (lp->flags == BAM_TITLE) {
1075                         title = strdupa(lp->arg);
1076                 } else if (lp->cmd != NULL) {
1077                         if (strcmp(lp->cmd, "pool_label") == 0) {
1078                                 findroot = strdupa(lp->arg);
1079                         } else if (strcmp(lp->cmd, "data_set") == 0) {
1080                                 bootfs = strdupa(lp->arg);
1081                         } else if (strcmp(lp->cmd,
1082                             menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
1083                                 if (strstr(lp->arg, "boot_archive") == NULL) {
1084                                         module = strdupa(lp->arg);
1085                                         cvt_hyper_module(module, &kern_path);
1086                                 } else {
1087                                         barchive_path = strdupa(lp->arg);
1088                                 }
1089                         } else if ((strcmp(lp->cmd,
1090                             menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
1091                             (cvt_hyper_kernel(lp->arg) < 0)) {
1092                                 ret = BAM_NOCHANGE;
1093                                 goto abort;
1094                         }
1095                 }
1096 
1097                 if (lp == ent->end)
1098                         break;
1099         }
1100 
1101         /*
1102          * If findroot, module or kern_path are NULL, the boot entry is
1103          * malformed.
1104          */
1105         if (findroot == NULL) {
1106                 bam_error(FINDROOT_NOT_FOUND, curdef);
1107                 goto abort;
1108         }
1109 
1110         if (module == NULL) {
1111                 bam_error(MODULE_NOT_PARSEABLE, curdef);
1112                 goto abort;
1113         }
1114 
1115         if (kern_path == NULL) {
1116                 bam_error(KERNEL_NOT_FOUND, curdef);
1117                 goto abort;
1118         }
1119 
1120         /*
1121          * Assemble new kernel and module arguments from parsed values.
1122          *
1123          * First, change the kernel directory from the hypervisor version to
1124          * that needed for a metal kernel.
1125          */
1126         newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
1127         free(kern_path);
1128         kern_path = newstr;
1129 
1130         /* allocate initial space for the kernel path */
1131         len = strlen(kern_path) + 1;
1132         zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
1133 
1134         if ((kernel = malloc(len + zfslen)) == NULL) {
1135                 free(kern_path);
1136                 bam_error(NO_MEM, len + zfslen);
1137                 bam_exit(1);
1138         }
1139 
1140         (void) snprintf(kernel, len, "%s", kern_path);
1141         free(kern_path);
1142 
1143         if (zfs_boot) {
1144                 char *zfsstr = alloca(zfslen + 1);
1145 
1146                 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
1147                 (void) strcat(kernel, zfsstr);
1148                 emit_bflag = 0;
1149         }
1150 
1151         /*
1152          * Process the bootenv.rc file to look for boot options that would be
1153          * the same as what the hypervisor had manually set, as we need not set
1154          * those explicitly.
1155          *
1156          * If there's no bootenv.rc, it's not an issue.
1157          */
1158         parse_bootenvrc(osroot);
1159 
1160         /*
1161          * Don't emit a console setting if it's the same as what would be
1162          * set by bootenv.rc.
1163          */
1164         if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
1165             (strcmp(console_dev, bootenv_rc_console) != 0))) {
1166                 if (emit_bflag) {
1167                         newstr = append_str(kernel, BFLAG, " ");
1168                         free(kernel);
1169                         kernel = append_str(newstr, "console=", " ");
1170                         free(newstr);
1171                         newstr = append_str(kernel, console_dev, "");
1172                         free(kernel);
1173                         kernel = newstr;
1174                         emit_bflag = 0;
1175                 } else {
1176                         newstr = append_str(kernel, "console=", ",");
1177                         free(kernel);
1178                         kernel = append_str(newstr, console_dev, "");
1179                         free(newstr);
1180                 }
1181         }
1182 
1183         /*
1184          * We have to do some strange processing here because the hypervisor's
1185          * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
1186          * or to "auto" if nothing is specified.
1187          *
1188          * This could result in a serial mode setting string being added when
1189          * it would otherwise not be needed, but it's better to play it safe.
1190          */
1191         if (emit_bflag) {
1192                 newstr = append_str(kernel, BFLAG, " ");
1193                 free(kernel);
1194                 kernel = newstr;
1195                 delim = " ";
1196                 emit_bflag = 0;
1197         }
1198 
1199         if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
1200             (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
1201                 newstr = append_str(kernel, "ttya-mode='", delim);
1202                 free(kernel);
1203 
1204                 /*
1205                  * Pass the serial configuration as the delimiter to
1206                  * append_str() as it will be inserted between the current
1207                  * string and the string we're appending, in this case the
1208                  * closing single quote.
1209                  */
1210                 kernel = append_str(newstr, "'", serial_config[0]);
1211                 free(newstr);
1212                 delim = ",";
1213         }
1214 
1215         if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
1216             (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
1217                 newstr = append_str(kernel, "ttyb-mode='", delim);
1218                 free(kernel);
1219 
1220                 /*
1221                  * Pass the serial configuration as the delimiter to
1222                  * append_str() as it will be inserted between the current
1223                  * string and the string we're appending, in this case the
1224                  * closing single quote.
1225                  */
1226                 kernel = append_str(newstr, "'", serial_config[1]);
1227                 free(newstr);
1228                 delim = ",";
1229         }
1230 
1231         /* shut off warning messages from the entry line parser */
1232         if (ent->flags & BAM_ENTRY_BOOTADM)
1233                 ent->flags &= ~BAM_ENTRY_BOOTADM;
1234 
1235         BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
1236         BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, module));
1237 
1238         if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
1239             barchive_path, bootfs)) == BAM_ERROR) {
1240                 free(kernel);
1241                 return (newdef);
1242         }
1243 
1244         /*
1245          * Now try to delete the current default entry from the menu and add
1246          * the new hypervisor entry with the parameters we've setup.
1247          */
1248         if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
1249                 newdef--;
1250         else
1251                 bam_print(NEW_BOOT_ENTRY, title);
1252 
1253         free(kernel);
1254 
1255         /*
1256          * If we successfully created the new entry, set the default boot
1257          * entry to that entry and let the caller know the new menu should
1258          * be written out.
1259          */
1260         return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
1261 
1262 abort:
1263         if (ret != BAM_NOCHANGE)
1264                 bam_error(METAL_ABORT, osroot);
1265 
1266         return (ret);
1267 }