17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d33344bbSsy25831 * Common Development and Distribution License (the "License"). 6d33344bbSsy25831 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22af28f636SEnrico Perla - Sun Microsystems * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2333f5ff17SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved. 241a902ef8SHans Rosenfeld * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <stdio.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <libgen.h> 307c478bd9Sstevel@tonic-gate #include <malloc.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <fcntl.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 35af28f636SEnrico Perla - Sun Microsystems #include <libintl.h> 36af28f636SEnrico Perla - Sun Microsystems #include <locale.h> 37af28f636SEnrico Perla - Sun Microsystems #include <errno.h> 38af28f636SEnrico Perla - Sun Microsystems #include <libfdisk.h> 39af28f636SEnrico Perla - Sun Microsystems #include <stdarg.h> 40af28f636SEnrico Perla - Sun Microsystems #include <assert.h> 41af28f636SEnrico Perla - Sun Microsystems 427c478bd9Sstevel@tonic-gate #include <sys/mount.h> 437c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 447c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 45d33344bbSsy25831 #include <sys/dkio.h> 46d33344bbSsy25831 #include <sys/vtoc.h> 47af28f636SEnrico Perla - Sun Microsystems #include <sys/types.h> 48af28f636SEnrico Perla - Sun Microsystems #include <sys/stat.h> 49af28f636SEnrico Perla - Sun Microsystems #include <sys/multiboot.h> 50af28f636SEnrico Perla - Sun Microsystems #include <sys/sysmacros.h> 511a902ef8SHans Rosenfeld #include <sys/efi_partition.h> 521a902ef8SHans Rosenfeld 531a902ef8SHans Rosenfeld #include <libnvpair.h> 541a902ef8SHans Rosenfeld #include <libfstyp.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #include "message.h" 57af28f636SEnrico Perla - Sun Microsystems #include "installgrub.h" 58af28f636SEnrico Perla - Sun Microsystems #include "./../common/bblk_einfo.h" 59af28f636SEnrico Perla - Sun Microsystems #include "./../common/boot_utils.h" 60af28f636SEnrico Perla - Sun Microsystems #include "./../common/mboot_extra.h" 61*21ba817cSToomas Soome #include "getresponse.h" 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 647c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 657c478bd9Sstevel@tonic-gate #endif 667c478bd9Sstevel@tonic-gate 67af28f636SEnrico Perla - Sun Microsystems /* 68af28f636SEnrico Perla - Sun Microsystems * Variables to track installgrub desired mode of operation. 69af28f636SEnrico Perla - Sun Microsystems * 'nowrite' and 'boot_debug' come from boot_common.h. 70af28f636SEnrico Perla - Sun Microsystems */ 71af28f636SEnrico Perla - Sun Microsystems static boolean_t write_mbr = B_FALSE; 72af28f636SEnrico Perla - Sun Microsystems static boolean_t force_mbr = B_FALSE; 73af28f636SEnrico Perla - Sun Microsystems static boolean_t force_update = B_FALSE; 74af28f636SEnrico Perla - Sun Microsystems static boolean_t do_getinfo = B_FALSE; 75af28f636SEnrico Perla - Sun Microsystems static boolean_t do_version = B_FALSE; 76af28f636SEnrico Perla - Sun Microsystems static boolean_t do_mirror_bblk = B_FALSE; 77af28f636SEnrico Perla - Sun Microsystems static boolean_t strip = B_FALSE; 78af28f636SEnrico Perla - Sun Microsystems static boolean_t verbose_dump = B_FALSE; 797c478bd9Sstevel@tonic-gate 80af28f636SEnrico Perla - Sun Microsystems /* Installing the bootblock is the default operation. */ 81af28f636SEnrico Perla - Sun Microsystems static boolean_t do_install = B_TRUE; 827c478bd9Sstevel@tonic-gate 83af28f636SEnrico Perla - Sun Microsystems /* Versioning string, if present. */ 84af28f636SEnrico Perla - Sun Microsystems static char *update_str; 857c478bd9Sstevel@tonic-gate 86af28f636SEnrico Perla - Sun Microsystems /* 87af28f636SEnrico Perla - Sun Microsystems * Temporary buffer to store the first 32K of data looking for a multiboot 88af28f636SEnrico Perla - Sun Microsystems * signature. 89af28f636SEnrico Perla - Sun Microsystems */ 90af28f636SEnrico Perla - Sun Microsystems char mboot_scan[MBOOT_SCAN_SIZE]; 917ce76caaSEnrico Perla - Sun Microsystems 92af28f636SEnrico Perla - Sun Microsystems /* Function prototypes. */ 93af28f636SEnrico Perla - Sun Microsystems static void check_options(char *); 94af28f636SEnrico Perla - Sun Microsystems static int handle_install(char *, char **); 95af28f636SEnrico Perla - Sun Microsystems static int handle_mirror(char *, char **); 96af28f636SEnrico Perla - Sun Microsystems static int handle_getinfo(char *, char **); 97af28f636SEnrico Perla - Sun Microsystems static int commit_to_disk(ig_data_t *, char *); 98af28f636SEnrico Perla - Sun Microsystems static int init_device(ig_device_t *, char *path); 99af28f636SEnrico Perla - Sun Microsystems static void cleanup_device(ig_device_t *); 100af28f636SEnrico Perla - Sun Microsystems static void cleanup_stage2(ig_stage2_t *); 101af28f636SEnrico Perla - Sun Microsystems static int get_start_sector(ig_device_t *); 102af28f636SEnrico Perla - Sun Microsystems static int get_disk_fd(ig_device_t *device); 103af28f636SEnrico Perla - Sun Microsystems static int get_raw_partition_fd(ig_device_t *); 104af28f636SEnrico Perla - Sun Microsystems static char *get_raw_partition_path(ig_device_t *); 105af28f636SEnrico Perla - Sun Microsystems static boolean_t gather_stage2_from_dev(ig_data_t *); 106af28f636SEnrico Perla - Sun Microsystems static int propagate_bootblock(ig_data_t *, ig_data_t *, char *); 107af28f636SEnrico Perla - Sun Microsystems static int find_x86_bootpar(struct mboot *, int *, uint32_t *); 108af28f636SEnrico Perla - Sun Microsystems static int copy_stage2_to_pcfs(ig_data_t *); 109af28f636SEnrico Perla - Sun Microsystems static int write_stage2(ig_data_t *); 110af28f636SEnrico Perla - Sun Microsystems static int write_stage1(ig_data_t *); 1117c478bd9Sstevel@tonic-gate static void usage(char *); 112af28f636SEnrico Perla - Sun Microsystems static int read_stage1_from_file(char *, ig_data_t *); 113af28f636SEnrico Perla - Sun Microsystems static int read_stage2_from_file(char *, ig_data_t *); 114af28f636SEnrico Perla - Sun Microsystems static int read_stage1_from_disk(int, char *); 1151a902ef8SHans Rosenfeld static int read_stage2_from_disk(int, ig_stage2_t *, int); 116af28f636SEnrico Perla - Sun Microsystems static int prepare_stage1(ig_data_t *); 117af28f636SEnrico Perla - Sun Microsystems static int prepare_stage2(ig_data_t *, char *); 118af28f636SEnrico Perla - Sun Microsystems static void prepare_fake_multiboot(ig_stage2_t *); 119af28f636SEnrico Perla - Sun Microsystems static void add_stage2_einfo(ig_stage2_t *, char *updt_str); 120af28f636SEnrico Perla - Sun Microsystems static boolean_t is_update_necessary(ig_data_t *, char *); 1217c478bd9Sstevel@tonic-gate 122342440ecSPrasad Singamsetty extern int read_stage2_blocklist(int, unsigned int *); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate int 1257c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1267c478bd9Sstevel@tonic-gate { 127af28f636SEnrico Perla - Sun Microsystems int opt; 128af28f636SEnrico Perla - Sun Microsystems int params = 3; 129af28f636SEnrico Perla - Sun Microsystems int ret; 130af28f636SEnrico Perla - Sun Microsystems char **handle_args; 131af28f636SEnrico Perla - Sun Microsystems char *progname; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1347c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 135*21ba817cSToomas Soome if (init_yes() < 0) { 136*21ba817cSToomas Soome (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES), 137*21ba817cSToomas Soome strerror(errno)); 138*21ba817cSToomas Soome exit(BC_ERROR); 139*21ba817cSToomas Soome } 1407c478bd9Sstevel@tonic-gate 141af28f636SEnrico Perla - Sun Microsystems /* 142af28f636SEnrico Perla - Sun Microsystems * retro-compatibility: installing the bootblock is the default 143af28f636SEnrico Perla - Sun Microsystems * and there is no switch for it. 144af28f636SEnrico Perla - Sun Microsystems */ 145af28f636SEnrico Perla - Sun Microsystems do_install = B_TRUE; 146af28f636SEnrico Perla - Sun Microsystems 147af28f636SEnrico Perla - Sun Microsystems while ((opt = getopt(argc, argv, "dVMFfmneiu:")) != EOF) { 1487c478bd9Sstevel@tonic-gate switch (opt) { 1497c478bd9Sstevel@tonic-gate case 'm': 150af28f636SEnrico Perla - Sun Microsystems write_mbr = B_TRUE; 1517c478bd9Sstevel@tonic-gate break; 1527c478bd9Sstevel@tonic-gate case 'n': 153af28f636SEnrico Perla - Sun Microsystems nowrite = B_TRUE; 1547c478bd9Sstevel@tonic-gate break; 1557c478bd9Sstevel@tonic-gate case 'f': 156af28f636SEnrico Perla - Sun Microsystems force_mbr = B_TRUE; 1577c478bd9Sstevel@tonic-gate break; 1587ce76caaSEnrico Perla - Sun Microsystems case 'i': 159af28f636SEnrico Perla - Sun Microsystems do_getinfo = B_TRUE; 160af28f636SEnrico Perla - Sun Microsystems do_install = B_FALSE; 1617ce76caaSEnrico Perla - Sun Microsystems params = 1; 1627ce76caaSEnrico Perla - Sun Microsystems break; 163af28f636SEnrico Perla - Sun Microsystems case 'V': 164af28f636SEnrico Perla - Sun Microsystems verbose_dump = B_TRUE; 1657ce76caaSEnrico Perla - Sun Microsystems break; 166af28f636SEnrico Perla - Sun Microsystems case 'd': 167af28f636SEnrico Perla - Sun Microsystems boot_debug = B_TRUE; 168af28f636SEnrico Perla - Sun Microsystems break; 169af28f636SEnrico Perla - Sun Microsystems case 'F': 170af28f636SEnrico Perla - Sun Microsystems force_update = B_TRUE; 171af28f636SEnrico Perla - Sun Microsystems break; 172af28f636SEnrico Perla - Sun Microsystems case 'e': 173af28f636SEnrico Perla - Sun Microsystems strip = B_TRUE; 174af28f636SEnrico Perla - Sun Microsystems break; 175af28f636SEnrico Perla - Sun Microsystems case 'M': 176af28f636SEnrico Perla - Sun Microsystems do_mirror_bblk = B_TRUE; 177af28f636SEnrico Perla - Sun Microsystems do_install = B_FALSE; 178af28f636SEnrico Perla - Sun Microsystems params = 2; 179af28f636SEnrico Perla - Sun Microsystems break; 180af28f636SEnrico Perla - Sun Microsystems case 'u': 181af28f636SEnrico Perla - Sun Microsystems do_version = B_TRUE; 182af28f636SEnrico Perla - Sun Microsystems 183af28f636SEnrico Perla - Sun Microsystems update_str = malloc(strlen(optarg) + 1); 184af28f636SEnrico Perla - Sun Microsystems if (update_str == NULL) { 185af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Unable to " 186af28f636SEnrico Perla - Sun Microsystems "allocate memory\n")); 187af28f636SEnrico Perla - Sun Microsystems exit(BC_ERROR); 188af28f636SEnrico Perla - Sun Microsystems } 189af28f636SEnrico Perla - Sun Microsystems (void) strlcpy(update_str, optarg, strlen(optarg) + 1); 1907ce76caaSEnrico Perla - Sun Microsystems break; 1917c478bd9Sstevel@tonic-gate default: 1927c478bd9Sstevel@tonic-gate /* fall through to process non-optional args */ 1937c478bd9Sstevel@tonic-gate break; 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* check arguments */ 1987ce76caaSEnrico Perla - Sun Microsystems if (argc != optind + params) { 1997c478bd9Sstevel@tonic-gate usage(argv[0]); 200af28f636SEnrico Perla - Sun Microsystems exit(BC_ERROR); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 203af28f636SEnrico Perla - Sun Microsystems /* 204af28f636SEnrico Perla - Sun Microsystems * clean up options (and bail out if an unrecoverable combination is 205af28f636SEnrico Perla - Sun Microsystems * requested. 206af28f636SEnrico Perla - Sun Microsystems */ 207af28f636SEnrico Perla - Sun Microsystems progname = argv[0]; 208af28f636SEnrico Perla - Sun Microsystems check_options(progname); 209af28f636SEnrico Perla - Sun Microsystems handle_args = argv + optind; 210af28f636SEnrico Perla - Sun Microsystems 211af28f636SEnrico Perla - Sun Microsystems if (nowrite) 2127c478bd9Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 213af28f636SEnrico Perla - Sun Microsystems 214af28f636SEnrico Perla - Sun Microsystems if (do_getinfo) { 215af28f636SEnrico Perla - Sun Microsystems ret = handle_getinfo(progname, handle_args); 216af28f636SEnrico Perla - Sun Microsystems } else if (do_mirror_bblk) { 217af28f636SEnrico Perla - Sun Microsystems ret = handle_mirror(progname, handle_args); 218af28f636SEnrico Perla - Sun Microsystems } else { 219af28f636SEnrico Perla - Sun Microsystems ret = handle_install(progname, handle_args); 220af28f636SEnrico Perla - Sun Microsystems } 221af28f636SEnrico Perla - Sun Microsystems return (ret); 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate 224af28f636SEnrico Perla - Sun Microsystems #define MEANINGLESS_OPT gettext("%s specified but meaningless, ignoring\n") 225af28f636SEnrico Perla - Sun Microsystems static void 226af28f636SEnrico Perla - Sun Microsystems check_options(char *progname) 2277c478bd9Sstevel@tonic-gate { 228af28f636SEnrico Perla - Sun Microsystems if (do_getinfo && do_mirror_bblk) { 229af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Only one of -M and -i can be " 230af28f636SEnrico Perla - Sun Microsystems "specified at the same time\n")); 231af28f636SEnrico Perla - Sun Microsystems usage(progname); 232af28f636SEnrico Perla - Sun Microsystems exit(BC_ERROR); 233af28f636SEnrico Perla - Sun Microsystems } 234af28f636SEnrico Perla - Sun Microsystems 235af28f636SEnrico Perla - Sun Microsystems if (do_mirror_bblk) { 236af28f636SEnrico Perla - Sun Microsystems /* 237af28f636SEnrico Perla - Sun Microsystems * -u and -F may actually reflect a user intent that is not 238af28f636SEnrico Perla - Sun Microsystems * correct with this command (mirror can be interpreted 239af28f636SEnrico Perla - Sun Microsystems * "similar" to install. Emit a message and continue. 240af28f636SEnrico Perla - Sun Microsystems * -e and -V have no meaning, be quiet here and only report the 241af28f636SEnrico Perla - Sun Microsystems * incongruence if a debug output is requested. 242af28f636SEnrico Perla - Sun Microsystems */ 243af28f636SEnrico Perla - Sun Microsystems if (do_version) { 244af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, MEANINGLESS_OPT, "-u"); 245af28f636SEnrico Perla - Sun Microsystems do_version = B_FALSE; 246af28f636SEnrico Perla - Sun Microsystems } 247af28f636SEnrico Perla - Sun Microsystems if (force_update) { 248af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, MEANINGLESS_OPT, "-F"); 249af28f636SEnrico Perla - Sun Microsystems force_update = B_FALSE; 250af28f636SEnrico Perla - Sun Microsystems } 251af28f636SEnrico Perla - Sun Microsystems if (strip || verbose_dump) { 252af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG(MEANINGLESS_OPT, "-e|-V"); 253af28f636SEnrico Perla - Sun Microsystems strip = B_FALSE; 254af28f636SEnrico Perla - Sun Microsystems verbose_dump = B_FALSE; 255af28f636SEnrico Perla - Sun Microsystems } 256af28f636SEnrico Perla - Sun Microsystems } 257af28f636SEnrico Perla - Sun Microsystems 258af28f636SEnrico Perla - Sun Microsystems if (do_getinfo) { 259af28f636SEnrico Perla - Sun Microsystems if (write_mbr || force_mbr || do_version || force_update) { 260af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG(MEANINGLESS_OPT, "-m|-f|-u|-F"); 261af28f636SEnrico Perla - Sun Microsystems write_mbr = force_mbr = do_version = B_FALSE; 262af28f636SEnrico Perla - Sun Microsystems force_update = B_FALSE; 263af28f636SEnrico Perla - Sun Microsystems } 264af28f636SEnrico Perla - Sun Microsystems } 265af28f636SEnrico Perla - Sun Microsystems } 266af28f636SEnrico Perla - Sun Microsystems 267af28f636SEnrico Perla - Sun Microsystems /* 268af28f636SEnrico Perla - Sun Microsystems * Install a new stage1/stage2 pair on the specified device. handle_install() 269af28f636SEnrico Perla - Sun Microsystems * expects argv to contain 3 parameters (the path to stage1, the path to stage2, 270af28f636SEnrico Perla - Sun Microsystems * the target device). 271af28f636SEnrico Perla - Sun Microsystems * 272af28f636SEnrico Perla - Sun Microsystems * Returns: BC_SUCCESS - if the installation is successful 273af28f636SEnrico Perla - Sun Microsystems * BC_ERROR - if the installation failed 274af28f636SEnrico Perla - Sun Microsystems * BC_NOUPDT - if no installation was performed because the GRUB 275af28f636SEnrico Perla - Sun Microsystems * version currently installed is more recent than the 276af28f636SEnrico Perla - Sun Microsystems * supplied one. 277af28f636SEnrico Perla - Sun Microsystems * 278af28f636SEnrico Perla - Sun Microsystems */ 279af28f636SEnrico Perla - Sun Microsystems static int 280af28f636SEnrico Perla - Sun Microsystems handle_install(char *progname, char **argv) 281af28f636SEnrico Perla - Sun Microsystems { 282af28f636SEnrico Perla - Sun Microsystems ig_data_t install_data; 283af28f636SEnrico Perla - Sun Microsystems char *stage1_path = NULL; 284af28f636SEnrico Perla - Sun Microsystems char *stage2_path = NULL; 285af28f636SEnrico Perla - Sun Microsystems char *device_path = NULL; 286af28f636SEnrico Perla - Sun Microsystems int ret = BC_ERROR; 287af28f636SEnrico Perla - Sun Microsystems 288af28f636SEnrico Perla - Sun Microsystems stage1_path = strdup(argv[0]); 289af28f636SEnrico Perla - Sun Microsystems stage2_path = strdup(argv[1]); 290af28f636SEnrico Perla - Sun Microsystems device_path = strdup(argv[2]); 291af28f636SEnrico Perla - Sun Microsystems 292af28f636SEnrico Perla - Sun Microsystems bzero(&install_data, sizeof (ig_data_t)); 293af28f636SEnrico Perla - Sun Microsystems 294af28f636SEnrico Perla - Sun Microsystems if (!stage1_path || !stage2_path || !device_path) { 295af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Missing parameter")); 296af28f636SEnrico Perla - Sun Microsystems usage(progname); 297af28f636SEnrico Perla - Sun Microsystems goto out; 298af28f636SEnrico Perla - Sun Microsystems } 299af28f636SEnrico Perla - Sun Microsystems 300af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("stage1 path: %s, stage2 path: %s, device: %s\n", 301af28f636SEnrico Perla - Sun Microsystems stage1_path, stage2_path, device_path); 302af28f636SEnrico Perla - Sun Microsystems 303af28f636SEnrico Perla - Sun Microsystems if (init_device(&install_data.device, device_path) != BC_SUCCESS) { 304af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Unable to gather device " 305af28f636SEnrico Perla - Sun Microsystems "information for %s\n"), device_path); 306af28f636SEnrico Perla - Sun Microsystems goto out; 307af28f636SEnrico Perla - Sun Microsystems } 308af28f636SEnrico Perla - Sun Microsystems 309af28f636SEnrico Perla - Sun Microsystems /* read in stage1 and stage2. */ 310af28f636SEnrico Perla - Sun Microsystems if (read_stage1_from_file(stage1_path, &install_data) != BC_SUCCESS) { 311af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error opening %s\n"), 312af28f636SEnrico Perla - Sun Microsystems stage1_path); 313af28f636SEnrico Perla - Sun Microsystems goto out_dev; 314af28f636SEnrico Perla - Sun Microsystems } 315af28f636SEnrico Perla - Sun Microsystems 316af28f636SEnrico Perla - Sun Microsystems if (read_stage2_from_file(stage2_path, &install_data) != BC_SUCCESS) { 317af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error opening %s\n"), 318af28f636SEnrico Perla - Sun Microsystems stage2_path); 319af28f636SEnrico Perla - Sun Microsystems goto out_dev; 320af28f636SEnrico Perla - Sun Microsystems } 321af28f636SEnrico Perla - Sun Microsystems 322af28f636SEnrico Perla - Sun Microsystems /* We do not support versioning on PCFS. */ 323af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(install_data.device.type) && do_version) 324af28f636SEnrico Perla - Sun Microsystems do_version = B_FALSE; 325af28f636SEnrico Perla - Sun Microsystems 326af28f636SEnrico Perla - Sun Microsystems /* 327af28f636SEnrico Perla - Sun Microsystems * is_update_necessary() will take care of checking if versioning and/or 328af28f636SEnrico Perla - Sun Microsystems * forcing the update have been specified. It will also emit a warning 329af28f636SEnrico Perla - Sun Microsystems * if a non-versioned update is attempted over a versioned bootblock. 330af28f636SEnrico Perla - Sun Microsystems */ 331af28f636SEnrico Perla - Sun Microsystems if (!is_update_necessary(&install_data, update_str)) { 332af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("GRUB version installed " 333af28f636SEnrico Perla - Sun Microsystems "on %s is more recent or identical\n" 334af28f636SEnrico Perla - Sun Microsystems "Use -F to override or install without the -u option\n"), 335af28f636SEnrico Perla - Sun Microsystems device_path); 336af28f636SEnrico Perla - Sun Microsystems ret = BC_NOUPDT; 337af28f636SEnrico Perla - Sun Microsystems goto out_dev; 338af28f636SEnrico Perla - Sun Microsystems } 339af28f636SEnrico Perla - Sun Microsystems /* 340af28f636SEnrico Perla - Sun Microsystems * We get here if: 341af28f636SEnrico Perla - Sun Microsystems * - the installed GRUB version is older than the one about to be 342af28f636SEnrico Perla - Sun Microsystems * installed. 343af28f636SEnrico Perla - Sun Microsystems * - no versioning string has been passed through the command line. 344af28f636SEnrico Perla - Sun Microsystems * - a forced update is requested (-F). 345af28f636SEnrico Perla - Sun Microsystems */ 346af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Ready to commit to disk\n"); 347af28f636SEnrico Perla - Sun Microsystems ret = commit_to_disk(&install_data, update_str); 348af28f636SEnrico Perla - Sun Microsystems 349af28f636SEnrico Perla - Sun Microsystems out_dev: 350af28f636SEnrico Perla - Sun Microsystems cleanup_device(&install_data.device); 351af28f636SEnrico Perla - Sun Microsystems out: 352af28f636SEnrico Perla - Sun Microsystems free(stage1_path); 353af28f636SEnrico Perla - Sun Microsystems free(stage2_path); 354af28f636SEnrico Perla - Sun Microsystems free(device_path); 355af28f636SEnrico Perla - Sun Microsystems return (ret); 356af28f636SEnrico Perla - Sun Microsystems } 357af28f636SEnrico Perla - Sun Microsystems 358af28f636SEnrico Perla - Sun Microsystems /* 359af28f636SEnrico Perla - Sun Microsystems * Retrieves from a device the extended information (einfo) associated to the 360af28f636SEnrico Perla - Sun Microsystems * installed stage2. 361af28f636SEnrico Perla - Sun Microsystems * Expects one parameter, the device path, in the form: /dev/rdsk/c?[t?]d?s0. 362af28f636SEnrico Perla - Sun Microsystems * Returns: 363af28f636SEnrico Perla - Sun Microsystems * - BC_SUCCESS (and prints out einfo contents depending on 'flags') 364af28f636SEnrico Perla - Sun Microsystems * - BC_ERROR (on error) 365af28f636SEnrico Perla - Sun Microsystems * - BC_NOEINFO (no extended information available) 366af28f636SEnrico Perla - Sun Microsystems */ 367af28f636SEnrico Perla - Sun Microsystems static int 368af28f636SEnrico Perla - Sun Microsystems handle_getinfo(char *progname, char **argv) 369af28f636SEnrico Perla - Sun Microsystems { 370af28f636SEnrico Perla - Sun Microsystems ig_data_t data; 371af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2 = &data.stage2; 372af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &data.device; 373af28f636SEnrico Perla - Sun Microsystems bblk_einfo_t *einfo; 374af28f636SEnrico Perla - Sun Microsystems uint8_t flags = 0; 375af28f636SEnrico Perla - Sun Microsystems uint32_t size; 376af28f636SEnrico Perla - Sun Microsystems char *device_path; 377af28f636SEnrico Perla - Sun Microsystems int retval = BC_ERROR; 378af28f636SEnrico Perla - Sun Microsystems int ret; 379af28f636SEnrico Perla - Sun Microsystems 380af28f636SEnrico Perla - Sun Microsystems device_path = strdup(argv[0]); 381af28f636SEnrico Perla - Sun Microsystems if (!device_path) { 382af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Missing parameter")); 383af28f636SEnrico Perla - Sun Microsystems usage(progname); 384af28f636SEnrico Perla - Sun Microsystems goto out; 385af28f636SEnrico Perla - Sun Microsystems } 386af28f636SEnrico Perla - Sun Microsystems 387af28f636SEnrico Perla - Sun Microsystems bzero(&data, sizeof (ig_data_t)); 388af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("device path: %s\n", device_path); 389af28f636SEnrico Perla - Sun Microsystems 390af28f636SEnrico Perla - Sun Microsystems if (init_device(device, device_path) != BC_SUCCESS) { 391af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Unable to gather device " 392af28f636SEnrico Perla - Sun Microsystems "information for %s\n"), device_path); 393af28f636SEnrico Perla - Sun Microsystems goto out_dev; 394af28f636SEnrico Perla - Sun Microsystems } 395af28f636SEnrico Perla - Sun Microsystems 396af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 397af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Versioning not supported on " 398af28f636SEnrico Perla - Sun Microsystems "PCFS\n")); 399af28f636SEnrico Perla - Sun Microsystems goto out_dev; 400af28f636SEnrico Perla - Sun Microsystems } 401af28f636SEnrico Perla - Sun Microsystems 4021a902ef8SHans Rosenfeld ret = read_stage2_from_disk(device->part_fd, stage2, device->type); 403af28f636SEnrico Perla - Sun Microsystems if (ret == BC_ERROR) { 404af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error reading stage2 from " 405af28f636SEnrico Perla - Sun Microsystems "%s\n"), device_path); 406af28f636SEnrico Perla - Sun Microsystems goto out_dev; 407af28f636SEnrico Perla - Sun Microsystems } 408af28f636SEnrico Perla - Sun Microsystems 409af28f636SEnrico Perla - Sun Microsystems if (ret == BC_NOEXTRA) { 410af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, gettext("No multiboot header found on " 411af28f636SEnrico Perla - Sun Microsystems "%s, unable to locate extra information area\n"), 412af28f636SEnrico Perla - Sun Microsystems device_path); 413af28f636SEnrico Perla - Sun Microsystems retval = BC_NOEINFO; 414af28f636SEnrico Perla - Sun Microsystems goto out_dev; 415af28f636SEnrico Perla - Sun Microsystems } 416af28f636SEnrico Perla - Sun Microsystems 41714d44f22SHans Rosenfeld einfo = find_einfo(stage2->extra, stage2->extra_size); 418af28f636SEnrico Perla - Sun Microsystems if (einfo == NULL) { 419af28f636SEnrico Perla - Sun Microsystems retval = BC_NOEINFO; 420af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("No extended information " 421af28f636SEnrico Perla - Sun Microsystems "found\n")); 422af28f636SEnrico Perla - Sun Microsystems goto out_dev; 423af28f636SEnrico Perla - Sun Microsystems } 424af28f636SEnrico Perla - Sun Microsystems 425af28f636SEnrico Perla - Sun Microsystems /* Print the extended information. */ 426af28f636SEnrico Perla - Sun Microsystems if (strip) 427af28f636SEnrico Perla - Sun Microsystems flags |= EINFO_EASY_PARSE; 428af28f636SEnrico Perla - Sun Microsystems if (verbose_dump) 429af28f636SEnrico Perla - Sun Microsystems flags |= EINFO_PRINT_HEADER; 430af28f636SEnrico Perla - Sun Microsystems 431af28f636SEnrico Perla - Sun Microsystems size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8); 432af28f636SEnrico Perla - Sun Microsystems print_einfo(flags, einfo, size); 433af28f636SEnrico Perla - Sun Microsystems retval = BC_SUCCESS; 434af28f636SEnrico Perla - Sun Microsystems 435af28f636SEnrico Perla - Sun Microsystems out_dev: 436af28f636SEnrico Perla - Sun Microsystems cleanup_device(&data.device); 437af28f636SEnrico Perla - Sun Microsystems out: 438af28f636SEnrico Perla - Sun Microsystems free(device_path); 439af28f636SEnrico Perla - Sun Microsystems return (retval); 440af28f636SEnrico Perla - Sun Microsystems } 441af28f636SEnrico Perla - Sun Microsystems 442af28f636SEnrico Perla - Sun Microsystems /* 443af28f636SEnrico Perla - Sun Microsystems * Attempt to mirror (propagate) the current stage2 over the attaching disk. 444af28f636SEnrico Perla - Sun Microsystems * 445af28f636SEnrico Perla - Sun Microsystems * Returns: 446af28f636SEnrico Perla - Sun Microsystems * - BC_SUCCESS (a successful propagation happened) 447af28f636SEnrico Perla - Sun Microsystems * - BC_ERROR (an error occurred) 448af28f636SEnrico Perla - Sun Microsystems * - BC_NOEXTRA (it is not possible to dump the current bootblock since 449af28f636SEnrico Perla - Sun Microsystems * there is no multiboot information) 450af28f636SEnrico Perla - Sun Microsystems */ 451af28f636SEnrico Perla - Sun Microsystems static int 452af28f636SEnrico Perla - Sun Microsystems handle_mirror(char *progname, char **argv) 453af28f636SEnrico Perla - Sun Microsystems { 454af28f636SEnrico Perla - Sun Microsystems ig_data_t curr_data; 455af28f636SEnrico Perla - Sun Microsystems ig_data_t attach_data; 456af28f636SEnrico Perla - Sun Microsystems ig_device_t *curr_device = &curr_data.device; 457af28f636SEnrico Perla - Sun Microsystems ig_device_t *attach_device = &attach_data.device; 458af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2_curr = &curr_data.stage2; 459af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2_attach = &attach_data.stage2; 460af28f636SEnrico Perla - Sun Microsystems bblk_einfo_t *einfo_curr = NULL; 461af28f636SEnrico Perla - Sun Microsystems char *curr_device_path; 462af28f636SEnrico Perla - Sun Microsystems char *attach_device_path; 463af28f636SEnrico Perla - Sun Microsystems char *updt_str = NULL; 464af28f636SEnrico Perla - Sun Microsystems int retval = BC_ERROR; 465af28f636SEnrico Perla - Sun Microsystems int ret; 466af28f636SEnrico Perla - Sun Microsystems 467af28f636SEnrico Perla - Sun Microsystems curr_device_path = strdup(argv[0]); 468af28f636SEnrico Perla - Sun Microsystems attach_device_path = strdup(argv[1]); 469af28f636SEnrico Perla - Sun Microsystems 470af28f636SEnrico Perla - Sun Microsystems if (!curr_device_path || !attach_device_path) { 471af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Missing parameter")); 472af28f636SEnrico Perla - Sun Microsystems usage(progname); 473af28f636SEnrico Perla - Sun Microsystems goto out; 474af28f636SEnrico Perla - Sun Microsystems } 475af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Current device path is: %s, attaching device path is: " 476af28f636SEnrico Perla - Sun Microsystems " %s\n", curr_device_path, attach_device_path); 477af28f636SEnrico Perla - Sun Microsystems 478af28f636SEnrico Perla - Sun Microsystems bzero(&curr_data, sizeof (ig_data_t)); 479af28f636SEnrico Perla - Sun Microsystems bzero(&attach_data, sizeof (ig_data_t)); 480af28f636SEnrico Perla - Sun Microsystems 481af28f636SEnrico Perla - Sun Microsystems if (init_device(curr_device, curr_device_path) != BC_SUCCESS) { 482af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Unable to gather device " 483af28f636SEnrico Perla - Sun Microsystems "information for %s (current device)\n"), curr_device_path); 484af28f636SEnrico Perla - Sun Microsystems goto out_currdev; 485af28f636SEnrico Perla - Sun Microsystems } 486af28f636SEnrico Perla - Sun Microsystems 487af28f636SEnrico Perla - Sun Microsystems if (init_device(attach_device, attach_device_path) != BC_SUCCESS) { 488af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Unable to gather device " 489af28f636SEnrico Perla - Sun Microsystems "information for %s (attaching device)\n"), 490af28f636SEnrico Perla - Sun Microsystems attach_device_path); 491af28f636SEnrico Perla - Sun Microsystems goto out_devs; 492af28f636SEnrico Perla - Sun Microsystems } 493af28f636SEnrico Perla - Sun Microsystems 494af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(curr_device->type) || is_bootpar(attach_device->type)) { 495af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("boot block mirroring is not " 496af28f636SEnrico Perla - Sun Microsystems "supported on PCFS\n")); 497af28f636SEnrico Perla - Sun Microsystems goto out_devs; 498af28f636SEnrico Perla - Sun Microsystems } 499af28f636SEnrico Perla - Sun Microsystems 5001a902ef8SHans Rosenfeld ret = read_stage2_from_disk(curr_device->part_fd, stage2_curr, 5011a902ef8SHans Rosenfeld curr_device->type); 502af28f636SEnrico Perla - Sun Microsystems if (ret == BC_ERROR) { 503af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Error reading first stage2 blocks from %s\n", 504af28f636SEnrico Perla - Sun Microsystems curr_device->path); 505af28f636SEnrico Perla - Sun Microsystems retval = BC_ERROR; 506af28f636SEnrico Perla - Sun Microsystems goto out_devs; 507af28f636SEnrico Perla - Sun Microsystems } 508af28f636SEnrico Perla - Sun Microsystems 509af28f636SEnrico Perla - Sun Microsystems if (ret == BC_NOEXTRA) { 510af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("No multiboot header found on %s, unable to grab " 511af28f636SEnrico Perla - Sun Microsystems "stage2\n", curr_device->path); 512af28f636SEnrico Perla - Sun Microsystems retval = BC_NOEXTRA; 513af28f636SEnrico Perla - Sun Microsystems goto out_devs; 514af28f636SEnrico Perla - Sun Microsystems } 515af28f636SEnrico Perla - Sun Microsystems 51614d44f22SHans Rosenfeld einfo_curr = find_einfo(stage2_curr->extra, stage2_curr->extra_size); 517af28f636SEnrico Perla - Sun Microsystems if (einfo_curr != NULL) 518af28f636SEnrico Perla - Sun Microsystems updt_str = einfo_get_string(einfo_curr); 519af28f636SEnrico Perla - Sun Microsystems 520af28f636SEnrico Perla - Sun Microsystems write_mbr = B_TRUE; 521af28f636SEnrico Perla - Sun Microsystems force_mbr = B_TRUE; 522af28f636SEnrico Perla - Sun Microsystems retval = propagate_bootblock(&curr_data, &attach_data, updt_str); 523af28f636SEnrico Perla - Sun Microsystems cleanup_stage2(stage2_curr); 524af28f636SEnrico Perla - Sun Microsystems cleanup_stage2(stage2_attach); 525af28f636SEnrico Perla - Sun Microsystems 526af28f636SEnrico Perla - Sun Microsystems out_devs: 527af28f636SEnrico Perla - Sun Microsystems cleanup_device(attach_device); 528af28f636SEnrico Perla - Sun Microsystems out_currdev: 529af28f636SEnrico Perla - Sun Microsystems cleanup_device(curr_device); 530af28f636SEnrico Perla - Sun Microsystems out: 531af28f636SEnrico Perla - Sun Microsystems free(curr_device_path); 532af28f636SEnrico Perla - Sun Microsystems free(attach_device_path); 533af28f636SEnrico Perla - Sun Microsystems return (retval); 534af28f636SEnrico Perla - Sun Microsystems } 535af28f636SEnrico Perla - Sun Microsystems 536af28f636SEnrico Perla - Sun Microsystems static int 537af28f636SEnrico Perla - Sun Microsystems commit_to_disk(ig_data_t *install, char *updt_str) 538af28f636SEnrico Perla - Sun Microsystems { 539af28f636SEnrico Perla - Sun Microsystems assert(install != NULL); 540af28f636SEnrico Perla - Sun Microsystems /* 541af28f636SEnrico Perla - Sun Microsystems * vanilla stage1 and stage2 need to be updated at runtime. 542af28f636SEnrico Perla - Sun Microsystems * Update stage2 before stage1 because stage1 needs to know the first 543af28f636SEnrico Perla - Sun Microsystems * sector stage2 will be written to. 544af28f636SEnrico Perla - Sun Microsystems */ 545af28f636SEnrico Perla - Sun Microsystems if (prepare_stage2(install, updt_str) != BC_SUCCESS) { 546af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error building stage2\n")); 547af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 548af28f636SEnrico Perla - Sun Microsystems } 549af28f636SEnrico Perla - Sun Microsystems if (prepare_stage1(install) != BC_SUCCESS) { 550af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error building stage1\n")); 551af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 552af28f636SEnrico Perla - Sun Microsystems } 553af28f636SEnrico Perla - Sun Microsystems 554af28f636SEnrico Perla - Sun Microsystems /* Write stage2 out to disk. */ 555af28f636SEnrico Perla - Sun Microsystems if (write_stage2(install) != BC_SUCCESS) { 556af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error writing stage2 to " 557af28f636SEnrico Perla - Sun Microsystems "disk\n")); 558af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 559af28f636SEnrico Perla - Sun Microsystems } 560af28f636SEnrico Perla - Sun Microsystems 561af28f636SEnrico Perla - Sun Microsystems /* Write stage1 to disk and, if requested, to the MBR. */ 562af28f636SEnrico Perla - Sun Microsystems if (write_stage1(install) != BC_SUCCESS) { 563af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error writing stage1 to " 564af28f636SEnrico Perla - Sun Microsystems "disk\n")); 565af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 566af28f636SEnrico Perla - Sun Microsystems } 567af28f636SEnrico Perla - Sun Microsystems 568af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 569af28f636SEnrico Perla - Sun Microsystems } 570af28f636SEnrico Perla - Sun Microsystems 571af28f636SEnrico Perla - Sun Microsystems /* 572af28f636SEnrico Perla - Sun Microsystems * Propagate the bootblock on the source disk to the destination disk and 573af28f636SEnrico Perla - Sun Microsystems * version it with 'updt_str' in the process. Since we cannot trust any data 574af28f636SEnrico Perla - Sun Microsystems * on the attaching disk, we do not perform any specific check on a potential 575af28f636SEnrico Perla - Sun Microsystems * target extended information structure and we just blindly update. 576af28f636SEnrico Perla - Sun Microsystems */ 577af28f636SEnrico Perla - Sun Microsystems static int 578af28f636SEnrico Perla - Sun Microsystems propagate_bootblock(ig_data_t *source, ig_data_t *target, char *updt_str) 579af28f636SEnrico Perla - Sun Microsystems { 580af28f636SEnrico Perla - Sun Microsystems ig_device_t *src_device = &source->device; 581af28f636SEnrico Perla - Sun Microsystems ig_device_t *dest_device = &target->device; 582af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *src_stage2 = &source->stage2; 583af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *dest_stage2 = &target->stage2; 584af28f636SEnrico Perla - Sun Microsystems uint32_t buf_size; 585af28f636SEnrico Perla - Sun Microsystems int retval; 586af28f636SEnrico Perla - Sun Microsystems 587af28f636SEnrico Perla - Sun Microsystems assert(source != NULL); 588af28f636SEnrico Perla - Sun Microsystems assert(target != NULL); 589af28f636SEnrico Perla - Sun Microsystems 590af28f636SEnrico Perla - Sun Microsystems /* read in stage1 from the source disk. */ 591af28f636SEnrico Perla - Sun Microsystems if (read_stage1_from_disk(src_device->part_fd, target->stage1_buf) 592af28f636SEnrico Perla - Sun Microsystems != BC_SUCCESS) 593af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 594af28f636SEnrico Perla - Sun Microsystems 595af28f636SEnrico Perla - Sun Microsystems /* Prepare target stage2 for commit_to_disk. */ 596af28f636SEnrico Perla - Sun Microsystems cleanup_stage2(dest_stage2); 597af28f636SEnrico Perla - Sun Microsystems 598af28f636SEnrico Perla - Sun Microsystems if (updt_str != NULL) 599af28f636SEnrico Perla - Sun Microsystems do_version = B_TRUE; 600af28f636SEnrico Perla - Sun Microsystems else 601af28f636SEnrico Perla - Sun Microsystems do_version = B_FALSE; 602af28f636SEnrico Perla - Sun Microsystems 603af28f636SEnrico Perla - Sun Microsystems buf_size = src_stage2->file_size + SECTOR_SIZE; 604af28f636SEnrico Perla - Sun Microsystems 605af28f636SEnrico Perla - Sun Microsystems dest_stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE); 606af28f636SEnrico Perla - Sun Microsystems dest_stage2->buf = malloc(dest_stage2->buf_size); 607af28f636SEnrico Perla - Sun Microsystems if (dest_stage2->buf == NULL) { 608af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 609af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 610af28f636SEnrico Perla - Sun Microsystems } 611af28f636SEnrico Perla - Sun Microsystems dest_stage2->file = dest_stage2->buf; 612af28f636SEnrico Perla - Sun Microsystems dest_stage2->file_size = src_stage2->file_size; 613af28f636SEnrico Perla - Sun Microsystems memcpy(dest_stage2->file, src_stage2->file, dest_stage2->file_size); 614af28f636SEnrico Perla - Sun Microsystems dest_stage2->extra = dest_stage2->buf + 615af28f636SEnrico Perla - Sun Microsystems P2ROUNDUP(dest_stage2->file_size, 8); 616af28f636SEnrico Perla - Sun Microsystems 617af28f636SEnrico Perla - Sun Microsystems /* If we get down here we do have a mboot structure. */ 618af28f636SEnrico Perla - Sun Microsystems assert(src_stage2->mboot); 619af28f636SEnrico Perla - Sun Microsystems 620af28f636SEnrico Perla - Sun Microsystems dest_stage2->mboot_off = src_stage2->mboot_off; 621af28f636SEnrico Perla - Sun Microsystems dest_stage2->mboot = (multiboot_header_t *)(dest_stage2->buf + 622af28f636SEnrico Perla - Sun Microsystems dest_stage2->mboot_off); 623af28f636SEnrico Perla - Sun Microsystems 624af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, gettext("Propagating %s stage1/stage2 to %s\n"), 625af28f636SEnrico Perla - Sun Microsystems src_device->path, dest_device->path); 626af28f636SEnrico Perla - Sun Microsystems retval = commit_to_disk(target, updt_str); 627af28f636SEnrico Perla - Sun Microsystems 628af28f636SEnrico Perla - Sun Microsystems return (retval); 629af28f636SEnrico Perla - Sun Microsystems } 630af28f636SEnrico Perla - Sun Microsystems 631af28f636SEnrico Perla - Sun Microsystems /* 632af28f636SEnrico Perla - Sun Microsystems * open the device and fill the various members of ig_device_t. 633af28f636SEnrico Perla - Sun Microsystems */ 634af28f636SEnrico Perla - Sun Microsystems static int 635af28f636SEnrico Perla - Sun Microsystems init_device(ig_device_t *device, char *path) 636af28f636SEnrico Perla - Sun Microsystems { 6371a902ef8SHans Rosenfeld struct dk_gpt *vtoc; 6381a902ef8SHans Rosenfeld fstyp_handle_t fhdl; 6391a902ef8SHans Rosenfeld const char *fident; 6401a902ef8SHans Rosenfeld 641af28f636SEnrico Perla - Sun Microsystems bzero(device, sizeof (*device)); 642af28f636SEnrico Perla - Sun Microsystems device->part_fd = -1; 643af28f636SEnrico Perla - Sun Microsystems device->disk_fd = -1; 644af28f636SEnrico Perla - Sun Microsystems device->path_p0 = NULL; 645af28f636SEnrico Perla - Sun Microsystems 646af28f636SEnrico Perla - Sun Microsystems device->path = strdup(path); 647af28f636SEnrico Perla - Sun Microsystems if (device->path == NULL) { 648af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 649af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 650af28f636SEnrico Perla - Sun Microsystems } 651af28f636SEnrico Perla - Sun Microsystems 652af28f636SEnrico Perla - Sun Microsystems if (strstr(device->path, "diskette")) { 653af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("installing GRUB to a floppy " 654af28f636SEnrico Perla - Sun Microsystems "disk is no longer supported\n")); 655af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 656af28f636SEnrico Perla - Sun Microsystems } 657af28f636SEnrico Perla - Sun Microsystems 658af28f636SEnrico Perla - Sun Microsystems /* Detect if the target device is a pcfs partition. */ 659af28f636SEnrico Perla - Sun Microsystems if (strstr(device->path, "p0:boot")) 660af28f636SEnrico Perla - Sun Microsystems device->type = IG_DEV_X86BOOTPAR; 661af28f636SEnrico Perla - Sun Microsystems 662af28f636SEnrico Perla - Sun Microsystems if (get_disk_fd(device) != BC_SUCCESS) 663af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 664af28f636SEnrico Perla - Sun Microsystems 665af28f636SEnrico Perla - Sun Microsystems /* read in the device boot sector. */ 666af28f636SEnrico Perla - Sun Microsystems if (read(device->disk_fd, device->boot_sector, SECTOR_SIZE) 667af28f636SEnrico Perla - Sun Microsystems != SECTOR_SIZE) { 668af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error reading boot sector\n")); 669af28f636SEnrico Perla - Sun Microsystems perror("read"); 670af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 671af28f636SEnrico Perla - Sun Microsystems } 672af28f636SEnrico Perla - Sun Microsystems 6731a902ef8SHans Rosenfeld if (efi_alloc_and_read(device->disk_fd, &vtoc) > 0) { 6741a902ef8SHans Rosenfeld device->type = IG_DEV_EFI; 6751a902ef8SHans Rosenfeld efi_free(vtoc); 6761a902ef8SHans Rosenfeld } 6771a902ef8SHans Rosenfeld 678af28f636SEnrico Perla - Sun Microsystems if (get_raw_partition_fd(device) != BC_SUCCESS) 679af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 680af28f636SEnrico Perla - Sun Microsystems 6819348d232SHans Rosenfeld if (is_efi(device->type)) { 6821a902ef8SHans Rosenfeld if (fstyp_init(device->part_fd, 0, NULL, &fhdl) != 0) 6831a902ef8SHans Rosenfeld return (BC_ERROR); 6841a902ef8SHans Rosenfeld 6851a902ef8SHans Rosenfeld if (fstyp_ident(fhdl, "zfs", &fident) != 0) { 6861a902ef8SHans Rosenfeld fstyp_fini(fhdl); 6879348d232SHans Rosenfeld (void) fprintf(stderr, gettext("Booting of EFI labeled " 6889348d232SHans Rosenfeld "disks is only supported with ZFS\n")); 6891a902ef8SHans Rosenfeld return (BC_ERROR); 6901a902ef8SHans Rosenfeld } 6911a902ef8SHans Rosenfeld fstyp_fini(fhdl); 6929348d232SHans Rosenfeld } 6931a902ef8SHans Rosenfeld 694af28f636SEnrico Perla - Sun Microsystems if (get_start_sector(device) != BC_SUCCESS) 695af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 696af28f636SEnrico Perla - Sun Microsystems 697af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 698af28f636SEnrico Perla - Sun Microsystems } 699af28f636SEnrico Perla - Sun Microsystems 700af28f636SEnrico Perla - Sun Microsystems static void 701af28f636SEnrico Perla - Sun Microsystems cleanup_device(ig_device_t *device) 702af28f636SEnrico Perla - Sun Microsystems { 703af28f636SEnrico Perla - Sun Microsystems if (device->path) 704af28f636SEnrico Perla - Sun Microsystems free(device->path); 705af28f636SEnrico Perla - Sun Microsystems if (device->path_p0) 706af28f636SEnrico Perla - Sun Microsystems free(device->path_p0); 707af28f636SEnrico Perla - Sun Microsystems 708af28f636SEnrico Perla - Sun Microsystems if (device->part_fd != -1) 709af28f636SEnrico Perla - Sun Microsystems (void) close(device->part_fd); 710af28f636SEnrico Perla - Sun Microsystems if (device->disk_fd != -1) 711af28f636SEnrico Perla - Sun Microsystems (void) close(device->disk_fd); 712af28f636SEnrico Perla - Sun Microsystems 713af28f636SEnrico Perla - Sun Microsystems bzero(device, sizeof (ig_device_t)); 714af28f636SEnrico Perla - Sun Microsystems device->part_fd = -1; 715af28f636SEnrico Perla - Sun Microsystems device->disk_fd = -1; 716af28f636SEnrico Perla - Sun Microsystems } 717af28f636SEnrico Perla - Sun Microsystems 718af28f636SEnrico Perla - Sun Microsystems static void 719af28f636SEnrico Perla - Sun Microsystems cleanup_stage2(ig_stage2_t *stage2) 720af28f636SEnrico Perla - Sun Microsystems { 721af28f636SEnrico Perla - Sun Microsystems if (stage2->buf) 722af28f636SEnrico Perla - Sun Microsystems free(stage2->buf); 723af28f636SEnrico Perla - Sun Microsystems bzero(stage2, sizeof (ig_stage2_t)); 724af28f636SEnrico Perla - Sun Microsystems } 725af28f636SEnrico Perla - Sun Microsystems 726af28f636SEnrico Perla - Sun Microsystems static int 727af28f636SEnrico Perla - Sun Microsystems get_start_sector(ig_device_t *device) 728af28f636SEnrico Perla - Sun Microsystems { 7297fc5d2a4SVikram Hegde uint32_t secnum = 0, numsec = 0; 7307fc5d2a4SVikram Hegde int i, pno, rval, log_part = 0; 7317c478bd9Sstevel@tonic-gate struct mboot *mboot; 7327c478bd9Sstevel@tonic-gate struct ipart *part; 733aa1b14e7SSheshadri Vasudevan ext_part_t *epp; 7347fc5d2a4SVikram Hegde struct part_info dkpi; 7357fc5d2a4SVikram Hegde struct extpart_info edkpi; 7367c478bd9Sstevel@tonic-gate 7371a902ef8SHans Rosenfeld if (is_efi(device->type)) { 7381a902ef8SHans Rosenfeld struct dk_gpt *vtoc; 7391a902ef8SHans Rosenfeld 7401a902ef8SHans Rosenfeld if (efi_alloc_and_read(device->disk_fd, &vtoc) <= 0) 7411a902ef8SHans Rosenfeld return (BC_ERROR); 7421a902ef8SHans Rosenfeld 7431a902ef8SHans Rosenfeld device->start_sector = vtoc->efi_parts[device->slice].p_start; 7441a902ef8SHans Rosenfeld /* GPT doesn't use traditional slice letters */ 7451a902ef8SHans Rosenfeld device->slice = 0xff; 7461a902ef8SHans Rosenfeld device->partition = 0; 7471a902ef8SHans Rosenfeld 7481a902ef8SHans Rosenfeld efi_free(vtoc); 7491a902ef8SHans Rosenfeld goto found_part; 7501a902ef8SHans Rosenfeld } 7511a902ef8SHans Rosenfeld 752af28f636SEnrico Perla - Sun Microsystems mboot = (struct mboot *)device->boot_sector; 7537c478bd9Sstevel@tonic-gate 754af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 755af28f636SEnrico Perla - Sun Microsystems if (find_x86_bootpar(mboot, &pno, &secnum) != BC_SUCCESS) { 756af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, NOBOOTPAR); 757af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 758af28f636SEnrico Perla - Sun Microsystems } else { 759af28f636SEnrico Perla - Sun Microsystems device->start_sector = secnum; 760af28f636SEnrico Perla - Sun Microsystems device->partition = pno; 7617fc5d2a4SVikram Hegde goto found_part; 7627fc5d2a4SVikram Hegde } 763d33344bbSsy25831 } 764d33344bbSsy25831 765aa1b14e7SSheshadri Vasudevan /* 766af28f636SEnrico Perla - Sun Microsystems * Search for Solaris fdisk partition 767d33344bbSsy25831 * Get the solaris partition information from the device 768d33344bbSsy25831 * and compare the offset of S2 with offset of solaris partition 769d33344bbSsy25831 * from fdisk partition table. 770d33344bbSsy25831 */ 771af28f636SEnrico Perla - Sun Microsystems if (ioctl(device->part_fd, DKIOCEXTPARTINFO, &edkpi) < 0) { 772af28f636SEnrico Perla - Sun Microsystems if (ioctl(device->part_fd, DKIOCPARTINFO, &dkpi) < 0) { 773d33344bbSsy25831 (void) fprintf(stderr, PART_FAIL); 774af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 775342440ecSPrasad Singamsetty } else { 776342440ecSPrasad Singamsetty edkpi.p_start = dkpi.p_start; 777342440ecSPrasad Singamsetty } 778d33344bbSsy25831 } 779d33344bbSsy25831 780d33344bbSsy25831 for (i = 0; i < FD_NUMPART; i++) { 781d33344bbSsy25831 part = (struct ipart *)mboot->parts + i; 782d33344bbSsy25831 783d33344bbSsy25831 if (part->relsect == 0) { 784d33344bbSsy25831 (void) fprintf(stderr, BAD_PART, i); 785af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 786d33344bbSsy25831 } 787aa1b14e7SSheshadri Vasudevan 788342440ecSPrasad Singamsetty if (edkpi.p_start >= part->relsect && 789342440ecSPrasad Singamsetty edkpi.p_start < (part->relsect + part->numsect)) { 790d33344bbSsy25831 /* Found the partition */ 7917c478bd9Sstevel@tonic-gate break; 792d33344bbSsy25831 } 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate 7957fc5d2a4SVikram Hegde if (i == FD_NUMPART) { 7967fc5d2a4SVikram Hegde /* No solaris fdisk partitions (primary or logical) */ 7977fc5d2a4SVikram Hegde (void) fprintf(stderr, NOSOLPAR); 798af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 7997c478bd9Sstevel@tonic-gate } 8007c478bd9Sstevel@tonic-gate 8017fc5d2a4SVikram Hegde /* 8027fc5d2a4SVikram Hegde * We have found a Solaris fdisk partition (primary or extended) 8037fc5d2a4SVikram Hegde * Handle the simple case first: Solaris in a primary partition 8047fc5d2a4SVikram Hegde */ 8057fc5d2a4SVikram Hegde if (!fdisk_is_dos_extended(part->systid)) { 806af28f636SEnrico Perla - Sun Microsystems device->start_sector = part->relsect; 807af28f636SEnrico Perla - Sun Microsystems device->partition = i; 8087fc5d2a4SVikram Hegde goto found_part; 8097fc5d2a4SVikram Hegde } 8107fc5d2a4SVikram Hegde 8117fc5d2a4SVikram Hegde /* 8127fc5d2a4SVikram Hegde * Solaris in a logical partition. Find that partition in the 8137fc5d2a4SVikram Hegde * extended part. 8147fc5d2a4SVikram Hegde */ 815af28f636SEnrico Perla - Sun Microsystems if ((rval = libfdisk_init(&epp, device->path_p0, NULL, FDISK_READ_DISK)) 8167fc5d2a4SVikram Hegde != FDISK_SUCCESS) { 8177fc5d2a4SVikram Hegde switch (rval) { 8187fc5d2a4SVikram Hegde /* 8196cb5747bSSharath M Srinivasan * The first 3 cases are not an error per-se, just that 8207fc5d2a4SVikram Hegde * there is no Solaris logical partition 8217fc5d2a4SVikram Hegde */ 8227fc5d2a4SVikram Hegde case FDISK_EBADLOGDRIVE: 8237fc5d2a4SVikram Hegde case FDISK_ENOLOGDRIVE: 8246cb5747bSSharath M Srinivasan case FDISK_EBADMAGIC: 8257fc5d2a4SVikram Hegde (void) fprintf(stderr, NOSOLPAR); 826af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8277fc5d2a4SVikram Hegde case FDISK_ENOVGEOM: 8287fc5d2a4SVikram Hegde (void) fprintf(stderr, NO_VIRT_GEOM); 829af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8307fc5d2a4SVikram Hegde case FDISK_ENOPGEOM: 8317fc5d2a4SVikram Hegde (void) fprintf(stderr, NO_PHYS_GEOM); 832af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8337fc5d2a4SVikram Hegde case FDISK_ENOLGEOM: 8347fc5d2a4SVikram Hegde (void) fprintf(stderr, NO_LABEL_GEOM); 835af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8367fc5d2a4SVikram Hegde default: 8377fc5d2a4SVikram Hegde (void) fprintf(stderr, LIBFDISK_INIT_FAIL); 838af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8397fc5d2a4SVikram Hegde } 8407fc5d2a4SVikram Hegde } 8417fc5d2a4SVikram Hegde 8427fc5d2a4SVikram Hegde rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec); 8436cb5747bSSharath M Srinivasan libfdisk_fini(&epp); 8447fc5d2a4SVikram Hegde if (rval != FDISK_SUCCESS) { 8457fc5d2a4SVikram Hegde /* No solaris logical partition */ 8467fc5d2a4SVikram Hegde (void) fprintf(stderr, NOSOLPAR); 847af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8487fc5d2a4SVikram Hegde } 8497fc5d2a4SVikram Hegde 850af28f636SEnrico Perla - Sun Microsystems device->start_sector = secnum; 851af28f636SEnrico Perla - Sun Microsystems device->partition = pno - 1; 8527fc5d2a4SVikram Hegde log_part = 1; 8537fc5d2a4SVikram Hegde 8547fc5d2a4SVikram Hegde found_part: 8557c478bd9Sstevel@tonic-gate /* get confirmation for -m */ 856af28f636SEnrico Perla - Sun Microsystems if (write_mbr && !force_mbr) { 8577c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 858*21ba817cSToomas Soome if (!yes()) { 859af28f636SEnrico Perla - Sun Microsystems write_mbr = 0; 8607c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 861af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate 8657fc5d2a4SVikram Hegde /* 8667fc5d2a4SVikram Hegde * Currently if Solaris is in an extended partition we need to 8677fc5d2a4SVikram Hegde * write GRUB to the MBR. Check for this. 8687fc5d2a4SVikram Hegde */ 869af28f636SEnrico Perla - Sun Microsystems if (log_part && !write_mbr) { 870af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, gettext("Installing Solaris on an " 871af28f636SEnrico Perla - Sun Microsystems "extended partition... forcing MBR update\n")); 872af28f636SEnrico Perla - Sun Microsystems write_mbr = 1; 873aa1b14e7SSheshadri Vasudevan } 874aa1b14e7SSheshadri Vasudevan 8757fc5d2a4SVikram Hegde /* 8767fc5d2a4SVikram Hegde * warn, if Solaris in primary partition and GRUB not in MBR and 8777fc5d2a4SVikram Hegde * partition is not active 8787fc5d2a4SVikram Hegde */ 879af28f636SEnrico Perla - Sun Microsystems if (!log_part && part->bootid != 128 && !write_mbr) { 880af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, SOLPAR_INACTIVE, device->partition + 1); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate 883af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate 886af28f636SEnrico Perla - Sun Microsystems static int 887af28f636SEnrico Perla - Sun Microsystems get_disk_fd(ig_device_t *device) 888af28f636SEnrico Perla - Sun Microsystems { 889af28f636SEnrico Perla - Sun Microsystems int i; 890af28f636SEnrico Perla - Sun Microsystems char save[2]; 891af28f636SEnrico Perla - Sun Microsystems char *end = NULL; 892af28f636SEnrico Perla - Sun Microsystems 893af28f636SEnrico Perla - Sun Microsystems assert(device != NULL); 894af28f636SEnrico Perla - Sun Microsystems assert(device->path != NULL); 895af28f636SEnrico Perla - Sun Microsystems 896af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 897af28f636SEnrico Perla - Sun Microsystems end = strstr(device->path, "p0:boot"); 898af28f636SEnrico Perla - Sun Microsystems /* tested at the start of init_device() */ 899af28f636SEnrico Perla - Sun Microsystems assert(end != NULL); 900af28f636SEnrico Perla - Sun Microsystems /* chop off :boot */ 901af28f636SEnrico Perla - Sun Microsystems save[0] = end[2]; 902af28f636SEnrico Perla - Sun Microsystems end[2] = '\0'; 903af28f636SEnrico Perla - Sun Microsystems } else { 904af28f636SEnrico Perla - Sun Microsystems i = strlen(device->path); 905af28f636SEnrico Perla - Sun Microsystems save[0] = device->path[i - 2]; 906af28f636SEnrico Perla - Sun Microsystems save[1] = device->path[i - 1]; 907af28f636SEnrico Perla - Sun Microsystems device->path[i - 2] = 'p'; 908af28f636SEnrico Perla - Sun Microsystems device->path[i - 1] = '0'; 909af28f636SEnrico Perla - Sun Microsystems } 910af28f636SEnrico Perla - Sun Microsystems 911af28f636SEnrico Perla - Sun Microsystems if (nowrite) 912af28f636SEnrico Perla - Sun Microsystems device->disk_fd = open(device->path, O_RDONLY); 913af28f636SEnrico Perla - Sun Microsystems else 914af28f636SEnrico Perla - Sun Microsystems device->disk_fd = open(device->path, O_RDWR); 915af28f636SEnrico Perla - Sun Microsystems 916af28f636SEnrico Perla - Sun Microsystems device->path_p0 = strdup(device->path); 917af28f636SEnrico Perla - Sun Microsystems if (device->path_p0 == NULL) { 918af28f636SEnrico Perla - Sun Microsystems perror("strdup"); 919af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 920af28f636SEnrico Perla - Sun Microsystems } 921af28f636SEnrico Perla - Sun Microsystems 922af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 923af28f636SEnrico Perla - Sun Microsystems end[2] = save[0]; 924af28f636SEnrico Perla - Sun Microsystems } else { 925af28f636SEnrico Perla - Sun Microsystems device->path[i - 2] = save[0]; 926af28f636SEnrico Perla - Sun Microsystems device->path[i - 1] = save[1]; 927af28f636SEnrico Perla - Sun Microsystems } 928af28f636SEnrico Perla - Sun Microsystems 929af28f636SEnrico Perla - Sun Microsystems if (device->disk_fd == -1) { 930af28f636SEnrico Perla - Sun Microsystems perror("open"); 931af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 932af28f636SEnrico Perla - Sun Microsystems } 933af28f636SEnrico Perla - Sun Microsystems 934af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 935af28f636SEnrico Perla - Sun Microsystems } 936af28f636SEnrico Perla - Sun Microsystems 937af28f636SEnrico Perla - Sun Microsystems static void 938af28f636SEnrico Perla - Sun Microsystems prepare_fake_multiboot(ig_stage2_t *stage2) 939af28f636SEnrico Perla - Sun Microsystems { 940af28f636SEnrico Perla - Sun Microsystems multiboot_header_t *mboot; 941af28f636SEnrico Perla - Sun Microsystems 942af28f636SEnrico Perla - Sun Microsystems assert(stage2 != NULL); 943af28f636SEnrico Perla - Sun Microsystems assert(stage2->mboot != NULL); 944af28f636SEnrico Perla - Sun Microsystems assert(stage2->buf != NULL); 945af28f636SEnrico Perla - Sun Microsystems 946af28f636SEnrico Perla - Sun Microsystems mboot = stage2->mboot; 947af28f636SEnrico Perla - Sun Microsystems 948af28f636SEnrico Perla - Sun Microsystems /* 949af28f636SEnrico Perla - Sun Microsystems * Currently we expect find_multiboot() to have located a multiboot 950af28f636SEnrico Perla - Sun Microsystems * header with the AOUT kludge flag set. 951af28f636SEnrico Perla - Sun Microsystems */ 952af28f636SEnrico Perla - Sun Microsystems assert(mboot->flags & BB_MBOOT_AOUT_FLAG); 953af28f636SEnrico Perla - Sun Microsystems 954af28f636SEnrico Perla - Sun Microsystems /* Insert the information necessary to locate stage2. */ 955af28f636SEnrico Perla - Sun Microsystems mboot->header_addr = stage2->mboot_off; 956af28f636SEnrico Perla - Sun Microsystems mboot->load_addr = 0; 957af28f636SEnrico Perla - Sun Microsystems mboot->load_end_addr = stage2->file_size; 958af28f636SEnrico Perla - Sun Microsystems } 959af28f636SEnrico Perla - Sun Microsystems 960af28f636SEnrico Perla - Sun Microsystems static void 961af28f636SEnrico Perla - Sun Microsystems add_stage2_einfo(ig_stage2_t *stage2, char *updt_str) 962af28f636SEnrico Perla - Sun Microsystems { 963af28f636SEnrico Perla - Sun Microsystems bblk_hs_t hs; 964af28f636SEnrico Perla - Sun Microsystems uint32_t avail_space; 965af28f636SEnrico Perla - Sun Microsystems 966af28f636SEnrico Perla - Sun Microsystems assert(stage2 != NULL); 967af28f636SEnrico Perla - Sun Microsystems 968af28f636SEnrico Perla - Sun Microsystems /* Fill bootblock hashing source information. */ 969af28f636SEnrico Perla - Sun Microsystems hs.src_buf = (unsigned char *)stage2->file; 970af28f636SEnrico Perla - Sun Microsystems hs.src_size = stage2->file_size; 971af28f636SEnrico Perla - Sun Microsystems /* How much space for the extended information structure? */ 972af28f636SEnrico Perla - Sun Microsystems avail_space = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8); 973af28f636SEnrico Perla - Sun Microsystems add_einfo(stage2->extra, updt_str, &hs, avail_space); 974af28f636SEnrico Perla - Sun Microsystems } 975af28f636SEnrico Perla - Sun Microsystems 976af28f636SEnrico Perla - Sun Microsystems 977af28f636SEnrico Perla - Sun Microsystems static int 978af28f636SEnrico Perla - Sun Microsystems write_stage2(ig_data_t *install) 979af28f636SEnrico Perla - Sun Microsystems { 980af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &install->device; 981af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2 = &install->stage2; 982af28f636SEnrico Perla - Sun Microsystems off_t offset; 983af28f636SEnrico Perla - Sun Microsystems 984af28f636SEnrico Perla - Sun Microsystems assert(install != NULL); 985af28f636SEnrico Perla - Sun Microsystems 986af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 987af28f636SEnrico Perla - Sun Microsystems /* 988af28f636SEnrico Perla - Sun Microsystems * stage2 is already on the filesystem, we only need to update 989af28f636SEnrico Perla - Sun Microsystems * the first two blocks (that we have modified during 990af28f636SEnrico Perla - Sun Microsystems * prepare_stage2()) 991af28f636SEnrico Perla - Sun Microsystems */ 992af28f636SEnrico Perla - Sun Microsystems if (write_out(device->part_fd, stage2->file, SECTOR_SIZE, 993af28f636SEnrico Perla - Sun Microsystems stage2->pcfs_first_sectors[0] * SECTOR_SIZE) 994af28f636SEnrico Perla - Sun Microsystems != BC_SUCCESS || 995af28f636SEnrico Perla - Sun Microsystems write_out(device->part_fd, stage2->file + SECTOR_SIZE, 996af28f636SEnrico Perla - Sun Microsystems SECTOR_SIZE, stage2->pcfs_first_sectors[1] * SECTOR_SIZE) 997af28f636SEnrico Perla - Sun Microsystems != BC_SUCCESS) { 998af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, WRITE_FAIL_STAGE2); 999af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1000af28f636SEnrico Perla - Sun Microsystems } 1001af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_STAGE2_PCFS); 1002af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 1003af28f636SEnrico Perla - Sun Microsystems } 1004af28f636SEnrico Perla - Sun Microsystems 1005af28f636SEnrico Perla - Sun Microsystems /* 1006af28f636SEnrico Perla - Sun Microsystems * For disk, write stage2 starting at STAGE2_BLKOFF sector. 1007af28f636SEnrico Perla - Sun Microsystems * Note that we use stage2->buf rather than stage2->file, because we 1008af28f636SEnrico Perla - Sun Microsystems * may have extended information after the latter. 10099348d232SHans Rosenfeld * 10109348d232SHans Rosenfeld * If we're writing to an EFI-labeled disk where stage2 lives in the 10119348d232SHans Rosenfeld * 3.5MB boot loader gap following the ZFS vdev labels, make sure the 10129348d232SHans Rosenfeld * size of the buffer doesn't exceed the size of the gap. 1013af28f636SEnrico Perla - Sun Microsystems */ 10149348d232SHans Rosenfeld if (is_efi(device->type) && stage2->buf_size > STAGE2_MAXSIZE) { 10159348d232SHans Rosenfeld (void) fprintf(stderr, WRITE_FAIL_STAGE2); 10169348d232SHans Rosenfeld return (BC_ERROR); 10179348d232SHans Rosenfeld } 10189348d232SHans Rosenfeld 10191a902ef8SHans Rosenfeld offset = STAGE2_BLKOFF(device->type) * SECTOR_SIZE; 10201a902ef8SHans Rosenfeld 1021af28f636SEnrico Perla - Sun Microsystems if (write_out(device->part_fd, stage2->buf, stage2->buf_size, 1022af28f636SEnrico Perla - Sun Microsystems offset) != BC_SUCCESS) { 1023af28f636SEnrico Perla - Sun Microsystems perror("write"); 1024af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1025af28f636SEnrico Perla - Sun Microsystems } 1026af28f636SEnrico Perla - Sun Microsystems 1027af28f636SEnrico Perla - Sun Microsystems /* Simulate the "old" installgrub output. */ 1028af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_STAGE2_DISK, device->partition, 10291a902ef8SHans Rosenfeld (stage2->buf_size / SECTOR_SIZE) + 1, STAGE2_BLKOFF(device->type), 1030af28f636SEnrico Perla - Sun Microsystems stage2->first_sector); 1031af28f636SEnrico Perla - Sun Microsystems 1032af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 1033af28f636SEnrico Perla - Sun Microsystems } 1034af28f636SEnrico Perla - Sun Microsystems 1035af28f636SEnrico Perla - Sun Microsystems static int 1036af28f636SEnrico Perla - Sun Microsystems write_stage1(ig_data_t *install) 1037af28f636SEnrico Perla - Sun Microsystems { 1038af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &install->device; 1039af28f636SEnrico Perla - Sun Microsystems 1040af28f636SEnrico Perla - Sun Microsystems assert(install != NULL); 1041af28f636SEnrico Perla - Sun Microsystems 1042af28f636SEnrico Perla - Sun Microsystems if (write_out(device->part_fd, install->stage1_buf, 1043af28f636SEnrico Perla - Sun Microsystems sizeof (install->stage1_buf), 0) != BC_SUCCESS) { 1044af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_FAIL_PBOOT); 1045af28f636SEnrico Perla - Sun Microsystems perror("write"); 1046af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1047af28f636SEnrico Perla - Sun Microsystems } 1048af28f636SEnrico Perla - Sun Microsystems 1049af28f636SEnrico Perla - Sun Microsystems /* Simulate "old" installgrub output. */ 1050af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_PBOOT, device->partition, 1051af28f636SEnrico Perla - Sun Microsystems device->start_sector); 1052af28f636SEnrico Perla - Sun Microsystems 1053af28f636SEnrico Perla - Sun Microsystems if (write_mbr) { 1054af28f636SEnrico Perla - Sun Microsystems if (write_out(device->disk_fd, install->stage1_buf, 1055af28f636SEnrico Perla - Sun Microsystems sizeof (install->stage1_buf), 0) != BC_SUCCESS) { 1056af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_FAIL_BOOTSEC); 1057af28f636SEnrico Perla - Sun Microsystems perror("write"); 1058af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1059af28f636SEnrico Perla - Sun Microsystems } 1060af28f636SEnrico Perla - Sun Microsystems /* Simulate "old" installgrub output. */ 1061af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, WRITE_MBOOT); 1062af28f636SEnrico Perla - Sun Microsystems } 1063af28f636SEnrico Perla - Sun Microsystems 1064af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 1065af28f636SEnrico Perla - Sun Microsystems } 1066af28f636SEnrico Perla - Sun Microsystems 1067af28f636SEnrico Perla - Sun Microsystems #define USAGE_STRING "%s [-m|-f|-n|-F|-u verstr] stage1 stage2 device\n" \ 1068af28f636SEnrico Perla - Sun Microsystems "%s -M [-n] device1 device2\n" \ 1069af28f636SEnrico Perla - Sun Microsystems "%s [-V|-e] -i device\n" \ 1070af28f636SEnrico Perla - Sun Microsystems 1071af28f636SEnrico Perla - Sun Microsystems #define CANON_USAGE_STR gettext(USAGE_STRING) 1072af28f636SEnrico Perla - Sun Microsystems 10737c478bd9Sstevel@tonic-gate static void 10747c478bd9Sstevel@tonic-gate usage(char *progname) 10757c478bd9Sstevel@tonic-gate { 1076af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, CANON_USAGE_STR, progname, progname, progname); 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate 1079af28f636SEnrico Perla - Sun Microsystems 10807c478bd9Sstevel@tonic-gate static int 1081af28f636SEnrico Perla - Sun Microsystems read_stage1_from_file(char *path, ig_data_t *dest) 10827c478bd9Sstevel@tonic-gate { 10837c478bd9Sstevel@tonic-gate int fd; 10847c478bd9Sstevel@tonic-gate 1085af28f636SEnrico Perla - Sun Microsystems assert(dest); 1086af28f636SEnrico Perla - Sun Microsystems 10877c478bd9Sstevel@tonic-gate /* read the stage1 file from filesystem */ 1088af28f636SEnrico Perla - Sun Microsystems fd = open(path, O_RDONLY); 1089af28f636SEnrico Perla - Sun Microsystems if (fd == -1 || 1090af28f636SEnrico Perla - Sun Microsystems read(fd, dest->stage1_buf, SECTOR_SIZE) != SECTOR_SIZE) { 1091af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, READ_FAIL_STAGE1, path); 1092af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate (void) close(fd); 1095af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate 1098af28f636SEnrico Perla - Sun Microsystems static int 1099af28f636SEnrico Perla - Sun Microsystems read_stage2_from_file(char *path, ig_data_t *dest) 11007c478bd9Sstevel@tonic-gate { 1101af28f636SEnrico Perla - Sun Microsystems int fd; 1102af28f636SEnrico Perla - Sun Microsystems struct stat sb; 1103af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2 = &dest->stage2; 1104af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &dest->device; 1105af28f636SEnrico Perla - Sun Microsystems uint32_t buf_size; 1106af28f636SEnrico Perla - Sun Microsystems 1107af28f636SEnrico Perla - Sun Microsystems assert(dest); 1108af28f636SEnrico Perla - Sun Microsystems assert(stage2->buf == NULL); 1109af28f636SEnrico Perla - Sun Microsystems 1110af28f636SEnrico Perla - Sun Microsystems fd = open(path, O_RDONLY); 1111af28f636SEnrico Perla - Sun Microsystems if (fstat(fd, &sb) == -1) { 1112af28f636SEnrico Perla - Sun Microsystems perror("fstat"); 1113af28f636SEnrico Perla - Sun Microsystems goto out; 11147c478bd9Sstevel@tonic-gate } 11157c478bd9Sstevel@tonic-gate 1116af28f636SEnrico Perla - Sun Microsystems stage2->file_size = sb.st_size; 11177c478bd9Sstevel@tonic-gate 1118af28f636SEnrico Perla - Sun Microsystems if (!is_bootpar(device->type)) { 1119af28f636SEnrico Perla - Sun Microsystems /* 1120af28f636SEnrico Perla - Sun Microsystems * buffer size needs to account for stage2 plus the extra 1121af28f636SEnrico Perla - Sun Microsystems * versioning information at the end of it. We reserve one 1122af28f636SEnrico Perla - Sun Microsystems * extra sector (plus we round up to the next sector boundary). 1123af28f636SEnrico Perla - Sun Microsystems */ 1124af28f636SEnrico Perla - Sun Microsystems buf_size = stage2->file_size + SECTOR_SIZE; 11257c478bd9Sstevel@tonic-gate } else { 1126af28f636SEnrico Perla - Sun Microsystems /* In the PCFS case we only need to read in stage2. */ 1127af28f636SEnrico Perla - Sun Microsystems buf_size = stage2->file_size; 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate 1130af28f636SEnrico Perla - Sun Microsystems stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE); 1131af28f636SEnrico Perla - Sun Microsystems 1132af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("stage2 buffer size = %d (%d sectors)\n", stage2->buf_size, 1133af28f636SEnrico Perla - Sun Microsystems stage2->buf_size / SECTOR_SIZE); 1134af28f636SEnrico Perla - Sun Microsystems 1135af28f636SEnrico Perla - Sun Microsystems stage2->buf = malloc(stage2->buf_size); 1136af28f636SEnrico Perla - Sun Microsystems if (stage2->buf == NULL) { 1137af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 1138af28f636SEnrico Perla - Sun Microsystems goto out_fd; 1139af28f636SEnrico Perla - Sun Microsystems } 1140af28f636SEnrico Perla - Sun Microsystems 1141af28f636SEnrico Perla - Sun Microsystems stage2->file = stage2->buf; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate /* 1144af28f636SEnrico Perla - Sun Microsystems * Extra information (e.g. the versioning structure) is placed at the 1145af28f636SEnrico Perla - Sun Microsystems * end of stage2, aligned on a 8-byte boundary. 11467c478bd9Sstevel@tonic-gate */ 1147af28f636SEnrico Perla - Sun Microsystems if (!(is_bootpar(device->type))) 1148af28f636SEnrico Perla - Sun Microsystems stage2->extra = stage2->file + P2ROUNDUP(stage2->file_size, 8); 11497c478bd9Sstevel@tonic-gate 1150af28f636SEnrico Perla - Sun Microsystems if (lseek(fd, 0, SEEK_SET) == -1) { 1151af28f636SEnrico Perla - Sun Microsystems perror("lseek"); 1152af28f636SEnrico Perla - Sun Microsystems goto out_alloc; 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate 1155af28f636SEnrico Perla - Sun Microsystems if (read(fd, stage2->file, stage2->file_size) < 0) { 1156af28f636SEnrico Perla - Sun Microsystems perror(gettext("unable to read stage2")); 1157af28f636SEnrico Perla - Sun Microsystems goto out_alloc; 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate 1160af28f636SEnrico Perla - Sun Microsystems (void) close(fd); 1161af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 11627ce76caaSEnrico Perla - Sun Microsystems 1163af28f636SEnrico Perla - Sun Microsystems out_alloc: 1164af28f636SEnrico Perla - Sun Microsystems free(stage2->buf); 1165af28f636SEnrico Perla - Sun Microsystems stage2->buf = NULL; 1166af28f636SEnrico Perla - Sun Microsystems out_fd: 1167af28f636SEnrico Perla - Sun Microsystems (void) close(fd); 1168af28f636SEnrico Perla - Sun Microsystems out: 1169af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 11707ce76caaSEnrico Perla - Sun Microsystems } 11717ce76caaSEnrico Perla - Sun Microsystems 11727ce76caaSEnrico Perla - Sun Microsystems static int 1173af28f636SEnrico Perla - Sun Microsystems prepare_stage1(ig_data_t *install) 11747ce76caaSEnrico Perla - Sun Microsystems { 1175af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &install->device; 11767ce76caaSEnrico Perla - Sun Microsystems 1177af28f636SEnrico Perla - Sun Microsystems assert(install != NULL); 11787ce76caaSEnrico Perla - Sun Microsystems 1179af28f636SEnrico Perla - Sun Microsystems /* If PCFS add the BIOS Parameter Block. */ 1180af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 1181af28f636SEnrico Perla - Sun Microsystems char bpb_sect[SECTOR_SIZE]; 1182af28f636SEnrico Perla - Sun Microsystems 1183af28f636SEnrico Perla - Sun Microsystems if (pread(device->part_fd, bpb_sect, SECTOR_SIZE, 0) 1184af28f636SEnrico Perla - Sun Microsystems != SECTOR_SIZE) { 1185af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, READ_FAIL_BPB); 1186af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1187af28f636SEnrico Perla - Sun Microsystems } 1188af28f636SEnrico Perla - Sun Microsystems bcopy(bpb_sect + STAGE1_BPB_OFFSET, 1189af28f636SEnrico Perla - Sun Microsystems install->stage1_buf + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 11907ce76caaSEnrico Perla - Sun Microsystems } 11917ce76caaSEnrico Perla - Sun Microsystems 1192af28f636SEnrico Perla - Sun Microsystems /* copy MBR to stage1 in case of overwriting MBR sector. */ 1193af28f636SEnrico Perla - Sun Microsystems bcopy(device->boot_sector + BOOTSZ, install->stage1_buf + BOOTSZ, 1194af28f636SEnrico Perla - Sun Microsystems SECTOR_SIZE - BOOTSZ); 1195af28f636SEnrico Perla - Sun Microsystems /* modify default stage1 file generated by GRUB. */ 1196af28f636SEnrico Perla - Sun Microsystems *((unsigned char *)(install->stage1_buf + STAGE1_FORCE_LBA)) = 1; 1197af28f636SEnrico Perla - Sun Microsystems *((ulong_t *)(install->stage1_buf + STAGE1_STAGE2_SECTOR)) 1198af28f636SEnrico Perla - Sun Microsystems = install->stage2.first_sector; 1199af28f636SEnrico Perla - Sun Microsystems *((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_ADDRESS)) 1200af28f636SEnrico Perla - Sun Microsystems = STAGE2_MEMADDR; 1201af28f636SEnrico Perla - Sun Microsystems *((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_SEGMENT)) 1202af28f636SEnrico Perla - Sun Microsystems = STAGE2_MEMADDR >> 4; 12037ce76caaSEnrico Perla - Sun Microsystems 1204af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 12057ce76caaSEnrico Perla - Sun Microsystems } 12067ce76caaSEnrico Perla - Sun Microsystems 1207af28f636SEnrico Perla - Sun Microsystems /* 1208af28f636SEnrico Perla - Sun Microsystems * Grab stage1 from the specified device file descriptor. 1209af28f636SEnrico Perla - Sun Microsystems */ 1210af28f636SEnrico Perla - Sun Microsystems static int 1211af28f636SEnrico Perla - Sun Microsystems read_stage1_from_disk(int dev_fd, char *stage1_buf) 1212af28f636SEnrico Perla - Sun Microsystems { 1213af28f636SEnrico Perla - Sun Microsystems assert(stage1_buf != NULL); 12147ce76caaSEnrico Perla - Sun Microsystems 1215af28f636SEnrico Perla - Sun Microsystems if (read_in(dev_fd, stage1_buf, SECTOR_SIZE, 0) != BC_SUCCESS) { 1216af28f636SEnrico Perla - Sun Microsystems perror(gettext("Unable to read stage1 from disk")); 1217af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 12187ce76caaSEnrico Perla - Sun Microsystems } 1219af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 12207ce76caaSEnrico Perla - Sun Microsystems } 12217ce76caaSEnrico Perla - Sun Microsystems 12227ce76caaSEnrico Perla - Sun Microsystems static int 12231a902ef8SHans Rosenfeld read_stage2_from_disk(int dev_fd, ig_stage2_t *stage2, int type) 12247ce76caaSEnrico Perla - Sun Microsystems { 1225af28f636SEnrico Perla - Sun Microsystems uint32_t size; 1226af28f636SEnrico Perla - Sun Microsystems uint32_t buf_size; 1227af28f636SEnrico Perla - Sun Microsystems uint32_t mboot_off; 1228af28f636SEnrico Perla - Sun Microsystems multiboot_header_t *mboot; 12297ce76caaSEnrico Perla - Sun Microsystems 1230af28f636SEnrico Perla - Sun Microsystems assert(stage2 != NULL); 1231af28f636SEnrico Perla - Sun Microsystems assert(dev_fd != -1); 12327ce76caaSEnrico Perla - Sun Microsystems 1233af28f636SEnrico Perla - Sun Microsystems if (read_in(dev_fd, mboot_scan, sizeof (mboot_scan), 12341a902ef8SHans Rosenfeld STAGE2_BLKOFF(type) * SECTOR_SIZE) != BC_SUCCESS) { 1235af28f636SEnrico Perla - Sun Microsystems perror(gettext("Error reading stage2 sectors")); 1236af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1237af28f636SEnrico Perla - Sun Microsystems } 12387ce76caaSEnrico Perla - Sun Microsystems 1239af28f636SEnrico Perla - Sun Microsystems /* No multiboot means no chance of knowing stage2 size */ 1240af28f636SEnrico Perla - Sun Microsystems if (find_multiboot(mboot_scan, sizeof (mboot_scan), &mboot_off) 1241af28f636SEnrico Perla - Sun Microsystems != BC_SUCCESS) { 1242af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Unable to find multiboot header\n"); 1243af28f636SEnrico Perla - Sun Microsystems return (BC_NOEXTRA); 1244af28f636SEnrico Perla - Sun Microsystems } 1245af28f636SEnrico Perla - Sun Microsystems mboot = (multiboot_header_t *)(mboot_scan + mboot_off); 12467ce76caaSEnrico Perla - Sun Microsystems 1247af28f636SEnrico Perla - Sun Microsystems /* 1248af28f636SEnrico Perla - Sun Microsystems * Unfilled mboot values mean an older version of installgrub installed 1249af28f636SEnrico Perla - Sun Microsystems * the stage2. Again we have no chance of knowing stage2 size. 1250af28f636SEnrico Perla - Sun Microsystems */ 1251af28f636SEnrico Perla - Sun Microsystems if (mboot->load_end_addr == 0 || 1252af28f636SEnrico Perla - Sun Microsystems mboot->load_end_addr < mboot->load_addr) 1253af28f636SEnrico Perla - Sun Microsystems return (BC_NOEXTRA); 1254af28f636SEnrico Perla - Sun Microsystems 1255af28f636SEnrico Perla - Sun Microsystems /* 1256af28f636SEnrico Perla - Sun Microsystems * Currently, the amount of space reserved for extra information 1257af28f636SEnrico Perla - Sun Microsystems * is "fixed". We may have to scan for the terminating extra payload 1258af28f636SEnrico Perla - Sun Microsystems * in the future. 1259af28f636SEnrico Perla - Sun Microsystems */ 1260af28f636SEnrico Perla - Sun Microsystems size = mboot->load_end_addr - mboot->load_addr; 1261af28f636SEnrico Perla - Sun Microsystems buf_size = P2ROUNDUP(size + SECTOR_SIZE, SECTOR_SIZE); 1262af28f636SEnrico Perla - Sun Microsystems 1263af28f636SEnrico Perla - Sun Microsystems stage2->buf = malloc(buf_size); 1264af28f636SEnrico Perla - Sun Microsystems if (stage2->buf == NULL) { 1265af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 1266af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1267af28f636SEnrico Perla - Sun Microsystems } 1268af28f636SEnrico Perla - Sun Microsystems stage2->buf_size = buf_size; 1269af28f636SEnrico Perla - Sun Microsystems 12701a902ef8SHans Rosenfeld if (read_in(dev_fd, stage2->buf, buf_size, STAGE2_BLKOFF(type) * 1271af28f636SEnrico Perla - Sun Microsystems SECTOR_SIZE) != BC_SUCCESS) { 1272af28f636SEnrico Perla - Sun Microsystems perror("read"); 1273af28f636SEnrico Perla - Sun Microsystems free(stage2->buf); 1274af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1275af28f636SEnrico Perla - Sun Microsystems } 1276af28f636SEnrico Perla - Sun Microsystems 1277af28f636SEnrico Perla - Sun Microsystems /* Update pointers. */ 1278af28f636SEnrico Perla - Sun Microsystems stage2->file = stage2->buf; 1279af28f636SEnrico Perla - Sun Microsystems stage2->file_size = size; 1280af28f636SEnrico Perla - Sun Microsystems stage2->mboot_off = mboot_off; 1281af28f636SEnrico Perla - Sun Microsystems stage2->mboot = (multiboot_header_t *)(stage2->buf + stage2->mboot_off); 1282af28f636SEnrico Perla - Sun Microsystems stage2->extra = stage2->buf + P2ROUNDUP(stage2->file_size, 8); 128314d44f22SHans Rosenfeld stage2->extra_size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8); 1284af28f636SEnrico Perla - Sun Microsystems 1285af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 1286af28f636SEnrico Perla - Sun Microsystems } 1287af28f636SEnrico Perla - Sun Microsystems 1288af28f636SEnrico Perla - Sun Microsystems static boolean_t 1289af28f636SEnrico Perla - Sun Microsystems is_update_necessary(ig_data_t *data, char *updt_str) 1290af28f636SEnrico Perla - Sun Microsystems { 1291af28f636SEnrico Perla - Sun Microsystems bblk_einfo_t *einfo; 1292af28f636SEnrico Perla - Sun Microsystems bblk_hs_t stage2_hs; 1293af28f636SEnrico Perla - Sun Microsystems ig_stage2_t stage2_disk; 1294af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2_file = &data->stage2; 1295af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &data->device; 1296af28f636SEnrico Perla - Sun Microsystems int dev_fd = device->part_fd; 1297af28f636SEnrico Perla - Sun Microsystems 1298af28f636SEnrico Perla - Sun Microsystems assert(data != NULL); 1299af28f636SEnrico Perla - Sun Microsystems assert(device->part_fd != -1); 1300af28f636SEnrico Perla - Sun Microsystems 1301af28f636SEnrico Perla - Sun Microsystems bzero(&stage2_disk, sizeof (ig_stage2_t)); 1302af28f636SEnrico Perla - Sun Microsystems 1303af28f636SEnrico Perla - Sun Microsystems /* Gather stage2 (if present) from the target device. */ 13041a902ef8SHans Rosenfeld if (read_stage2_from_disk(dev_fd, &stage2_disk, device->type) 13051a902ef8SHans Rosenfeld != BC_SUCCESS) { 1306af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Unable to read stage2 from %s\n", device->path); 1307af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("No multiboot wrapped stage2 on %s\n", device->path); 1308af28f636SEnrico Perla - Sun Microsystems return (B_TRUE); 1309af28f636SEnrico Perla - Sun Microsystems } 1310af28f636SEnrico Perla - Sun Microsystems 1311af28f636SEnrico Perla - Sun Microsystems /* 1312af28f636SEnrico Perla - Sun Microsystems * Look for the extended information structure in the extra payload 1313af28f636SEnrico Perla - Sun Microsystems * area. 1314af28f636SEnrico Perla - Sun Microsystems */ 131514d44f22SHans Rosenfeld einfo = find_einfo(stage2_disk.extra, stage2_disk.extra_size); 1316af28f636SEnrico Perla - Sun Microsystems if (einfo == NULL) { 1317af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("No extended information available\n"); 1318af28f636SEnrico Perla - Sun Microsystems return (B_TRUE); 1319af28f636SEnrico Perla - Sun Microsystems } 1320af28f636SEnrico Perla - Sun Microsystems 1321af28f636SEnrico Perla - Sun Microsystems if (!do_version || updt_str == NULL) { 1322af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stdout, "WARNING: target device %s has a " 1323af28f636SEnrico Perla - Sun Microsystems "versioned stage2 that is going to be overwritten by a non " 1324af28f636SEnrico Perla - Sun Microsystems "versioned one\n", device->path); 1325af28f636SEnrico Perla - Sun Microsystems return (B_TRUE); 1326af28f636SEnrico Perla - Sun Microsystems } 1327af28f636SEnrico Perla - Sun Microsystems 1328af28f636SEnrico Perla - Sun Microsystems if (force_update) { 1329af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("Forcing update of %s bootblock\n", device->path); 1330af28f636SEnrico Perla - Sun Microsystems return (B_TRUE); 1331af28f636SEnrico Perla - Sun Microsystems } 1332af28f636SEnrico Perla - Sun Microsystems 1333af28f636SEnrico Perla - Sun Microsystems /* Compare the two extended information structures. */ 1334af28f636SEnrico Perla - Sun Microsystems stage2_hs.src_buf = (unsigned char *)stage2_file->file; 1335af28f636SEnrico Perla - Sun Microsystems stage2_hs.src_size = stage2_file->file_size; 1336af28f636SEnrico Perla - Sun Microsystems 1337af28f636SEnrico Perla - Sun Microsystems return (einfo_should_update(einfo, &stage2_hs, updt_str)); 13387ce76caaSEnrico Perla - Sun Microsystems } 13397ce76caaSEnrico Perla - Sun Microsystems 13407ce76caaSEnrico Perla - Sun Microsystems 13417c478bd9Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 13427c478bd9Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 13437c478bd9Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 13447c478bd9Sstevel@tonic-gate 1345af28f636SEnrico Perla - Sun Microsystems static int 1346af28f636SEnrico Perla - Sun Microsystems prepare_stage2(ig_data_t *install, char *updt_str) 13477c478bd9Sstevel@tonic-gate { 1348af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &install->device; 1349af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2 = &install->stage2; 1350af28f636SEnrico Perla - Sun Microsystems uint32_t mboot_off = 0; 13517ce76caaSEnrico Perla - Sun Microsystems 1352af28f636SEnrico Perla - Sun Microsystems assert(install != NULL); 1353af28f636SEnrico Perla - Sun Microsystems assert(stage2->file != NULL); 1354af28f636SEnrico Perla - Sun Microsystems 1355af28f636SEnrico Perla - Sun Microsystems /* New stage2 files come with an embedded stage2. */ 1356af28f636SEnrico Perla - Sun Microsystems if (find_multiboot(stage2->file, stage2->file_size, &mboot_off) 1357af28f636SEnrico Perla - Sun Microsystems != BC_SUCCESS) { 1358af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("WARNING: no multiboot structure found in stage2, " 1359af28f636SEnrico Perla - Sun Microsystems "are you using an old GRUB stage2?\n"); 1360af28f636SEnrico Perla - Sun Microsystems if (do_version == B_TRUE) { 1361af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Versioning requested " 1362af28f636SEnrico Perla - Sun Microsystems "but stage2 does not support it.. skipping.\n")); 1363af28f636SEnrico Perla - Sun Microsystems do_version = B_FALSE; 1364af28f636SEnrico Perla - Sun Microsystems } 1365af28f636SEnrico Perla - Sun Microsystems } else { 1366af28f636SEnrico Perla - Sun Microsystems /* Keep track of where the multiboot header is. */ 1367af28f636SEnrico Perla - Sun Microsystems stage2->mboot_off = mboot_off; 1368af28f636SEnrico Perla - Sun Microsystems stage2->mboot = (multiboot_header_t *)(stage2->file + 1369af28f636SEnrico Perla - Sun Microsystems mboot_off); 13707ce76caaSEnrico Perla - Sun Microsystems if (do_version) { 1371af28f636SEnrico Perla - Sun Microsystems /* 1372af28f636SEnrico Perla - Sun Microsystems * Adding stage2 information needs to happen before 1373af28f636SEnrico Perla - Sun Microsystems * we modify the copy of stage2 we have in memory, so 1374af28f636SEnrico Perla - Sun Microsystems * that the hashing reflects the one of the file. 1375af28f636SEnrico Perla - Sun Microsystems * An error here is not fatal. 1376af28f636SEnrico Perla - Sun Microsystems */ 1377af28f636SEnrico Perla - Sun Microsystems add_stage2_einfo(stage2, updt_str); 1378af28f636SEnrico Perla - Sun Microsystems } 1379af28f636SEnrico Perla - Sun Microsystems /* 1380af28f636SEnrico Perla - Sun Microsystems * Fill multiboot information. We add them even without 1381af28f636SEnrico Perla - Sun Microsystems * versioning to support as much as possible mirroring. 1382af28f636SEnrico Perla - Sun Microsystems */ 1383af28f636SEnrico Perla - Sun Microsystems prepare_fake_multiboot(stage2); 13847ce76caaSEnrico Perla - Sun Microsystems } 13857c478bd9Sstevel@tonic-gate 1386af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 1387af28f636SEnrico Perla - Sun Microsystems uint32_t blocklist[SECTOR_SIZE / sizeof (uint32_t)]; 1388af28f636SEnrico Perla - Sun Microsystems uint32_t install_addr = STAGE2_MEMADDR + SECTOR_SIZE; 13897c478bd9Sstevel@tonic-gate int i = 0; 1390af28f636SEnrico Perla - Sun Microsystems uchar_t *pos; 13917c478bd9Sstevel@tonic-gate 1392af28f636SEnrico Perla - Sun Microsystems bzero(blocklist, sizeof (blocklist)); 1393af28f636SEnrico Perla - Sun Microsystems if (read_stage2_blocklist(device->part_fd, blocklist) != 0) { 1394af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("Error reading pcfs " 1395af28f636SEnrico Perla - Sun Microsystems "stage2 blocklist\n")); 1396af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1397af28f636SEnrico Perla - Sun Microsystems } 13987c478bd9Sstevel@tonic-gate 1399af28f636SEnrico Perla - Sun Microsystems pos = (uchar_t *)stage2->file + STAGE2_BLOCKLIST; 1400af28f636SEnrico Perla - Sun Microsystems stage2->first_sector = device->start_sector + blocklist[0]; 1401af28f636SEnrico Perla - Sun Microsystems stage2->pcfs_first_sectors[0] = blocklist[0]; 1402af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector); 1403af28f636SEnrico Perla - Sun Microsystems 1404af28f636SEnrico Perla - Sun Microsystems 14057c478bd9Sstevel@tonic-gate if (blocklist[1] > 1) { 14067c478bd9Sstevel@tonic-gate blocklist[0]++; 14077c478bd9Sstevel@tonic-gate blocklist[1]--; 14087c478bd9Sstevel@tonic-gate } else { 14097c478bd9Sstevel@tonic-gate i += 2; 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 1412af28f636SEnrico Perla - Sun Microsystems stage2->pcfs_first_sectors[1] = blocklist[i]; 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate while (blocklist[i]) { 14157c478bd9Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 14167c478bd9Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 14177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 1418af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 14197c478bd9Sstevel@tonic-gate } 1420af28f636SEnrico Perla - Sun Microsystems START_BLOCK(pos) = blocklist[i] + device->start_sector; 14217c478bd9Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 14227c478bd9Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 14237c478bd9Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 14247c478bd9Sstevel@tonic-gate pos -= 8; 14257c478bd9Sstevel@tonic-gate i += 2; 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate } else { 1428af28f636SEnrico Perla - Sun Microsystems /* Solaris VTOC */ 14291a902ef8SHans Rosenfeld stage2->first_sector = device->start_sector + 14301a902ef8SHans Rosenfeld STAGE2_BLKOFF(device->type); 1431af28f636SEnrico Perla - Sun Microsystems BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector); 14327c478bd9Sstevel@tonic-gate /* 14337c478bd9Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 14347c478bd9Sstevel@tonic-gate * blocks. So we update the starting block only. 14357c478bd9Sstevel@tonic-gate */ 1436af28f636SEnrico Perla - Sun Microsystems *((ulong_t *)(stage2->file + STAGE2_BLOCKLIST)) = 1437af28f636SEnrico Perla - Sun Microsystems stage2->first_sector + 1; 14387c478bd9Sstevel@tonic-gate } 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate /* force lba and set disk partition */ 1441af28f636SEnrico Perla - Sun Microsystems *((unsigned char *) (stage2->file + STAGE2_FORCE_LBA)) = 1; 1442af28f636SEnrico Perla - Sun Microsystems *((long *)(stage2->file + STAGE2_INSTALLPART)) 1443af28f636SEnrico Perla - Sun Microsystems = (device->partition << 16) | (device->slice << 8) | 0xff; 1444af28f636SEnrico Perla - Sun Microsystems 1445af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 14467c478bd9Sstevel@tonic-gate } 14477c478bd9Sstevel@tonic-gate 1448af28f636SEnrico Perla - Sun Microsystems static int 1449af28f636SEnrico Perla - Sun Microsystems find_x86_bootpar(struct mboot *mboot, int *part_num, uint32_t *start_sect) 1450af28f636SEnrico Perla - Sun Microsystems { 1451af28f636SEnrico Perla - Sun Microsystems int i; 14527c478bd9Sstevel@tonic-gate 1453af28f636SEnrico Perla - Sun Microsystems for (i = 0; i < FD_NUMPART; i++) { 1454af28f636SEnrico Perla - Sun Microsystems struct ipart *part; 14557c478bd9Sstevel@tonic-gate 1456af28f636SEnrico Perla - Sun Microsystems part = (struct ipart *)mboot->parts + i; 1457af28f636SEnrico Perla - Sun Microsystems if (part->systid == 0xbe) { 1458af28f636SEnrico Perla - Sun Microsystems if (start_sect) 1459af28f636SEnrico Perla - Sun Microsystems *start_sect = part->relsect; 1460af28f636SEnrico Perla - Sun Microsystems if (part_num) 1461af28f636SEnrico Perla - Sun Microsystems *part_num = i; 1462af28f636SEnrico Perla - Sun Microsystems /* solaris boot part */ 1463af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate } 1466af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 14677c478bd9Sstevel@tonic-gate } 14687c478bd9Sstevel@tonic-gate 14697c478bd9Sstevel@tonic-gate static char * 1470af28f636SEnrico Perla - Sun Microsystems get_raw_partition_path(ig_device_t *device) 14717c478bd9Sstevel@tonic-gate { 1472af28f636SEnrico Perla - Sun Microsystems char *raw; 14737c478bd9Sstevel@tonic-gate int len; 1474af28f636SEnrico Perla - Sun Microsystems 1475af28f636SEnrico Perla - Sun Microsystems if (is_bootpar(device->type)) { 1476af28f636SEnrico Perla - Sun Microsystems int part; 14777c478bd9Sstevel@tonic-gate struct mboot *mboot; 14787c478bd9Sstevel@tonic-gate 1479af28f636SEnrico Perla - Sun Microsystems mboot = (struct mboot *)device->boot_sector; 1480af28f636SEnrico Perla - Sun Microsystems if (find_x86_bootpar(mboot, &part, NULL) != BC_SUCCESS) { 1481af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, BOOTPAR_NOTFOUND, 1482af28f636SEnrico Perla - Sun Microsystems device->path_p0); 1483af28f636SEnrico Perla - Sun Microsystems return (NULL); 14847c478bd9Sstevel@tonic-gate } 14857c478bd9Sstevel@tonic-gate 1486af28f636SEnrico Perla - Sun Microsystems raw = strdup(device->path_p0); 1487af28f636SEnrico Perla - Sun Microsystems if (raw == NULL) { 1488af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 1489af28f636SEnrico Perla - Sun Microsystems return (NULL); 14907c478bd9Sstevel@tonic-gate } 1491af28f636SEnrico Perla - Sun Microsystems 1492af28f636SEnrico Perla - Sun Microsystems raw[strlen(raw) - 2] = '1' + part; 14937c478bd9Sstevel@tonic-gate return (raw); 14947c478bd9Sstevel@tonic-gate } 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 1497af28f636SEnrico Perla - Sun Microsystems raw = strdup(device->path); 1498af28f636SEnrico Perla - Sun Microsystems if (raw == NULL) { 1499af28f636SEnrico Perla - Sun Microsystems perror(gettext("Memory allocation failed")); 1500af28f636SEnrico Perla - Sun Microsystems return (NULL); 1501af28f636SEnrico Perla - Sun Microsystems } 1502af28f636SEnrico Perla - Sun Microsystems 15037c478bd9Sstevel@tonic-gate len = strlen(raw); 15041a902ef8SHans Rosenfeld if (!is_efi(device->type) && 15051a902ef8SHans Rosenfeld (raw[len - 2] != 's' || raw[len - 1] == '2')) { 15067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 1507af28f636SEnrico Perla - Sun Microsystems free(raw); 1508af28f636SEnrico Perla - Sun Microsystems return (NULL); 15097c478bd9Sstevel@tonic-gate } 1510af28f636SEnrico Perla - Sun Microsystems device->slice = atoi(&raw[len - 1]); 15117c478bd9Sstevel@tonic-gate 15121a902ef8SHans Rosenfeld if (!is_efi(device->type)) { 15137c478bd9Sstevel@tonic-gate raw[len - 2] = 's'; 15147c478bd9Sstevel@tonic-gate raw[len - 1] = '2'; 15151a902ef8SHans Rosenfeld } 1516af28f636SEnrico Perla - Sun Microsystems 15177c478bd9Sstevel@tonic-gate return (raw); 15187c478bd9Sstevel@tonic-gate } 15197c478bd9Sstevel@tonic-gate 1520af28f636SEnrico Perla - Sun Microsystems static int 1521af28f636SEnrico Perla - Sun Microsystems get_raw_partition_fd(ig_device_t *device) 1522af28f636SEnrico Perla - Sun Microsystems { 1523af28f636SEnrico Perla - Sun Microsystems struct stat stat = {0}; 1524af28f636SEnrico Perla - Sun Microsystems char *raw; 1525af28f636SEnrico Perla - Sun Microsystems 1526af28f636SEnrico Perla - Sun Microsystems raw = get_raw_partition_path(device); 1527af28f636SEnrico Perla - Sun Microsystems if (raw == NULL) 1528af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1529af28f636SEnrico Perla - Sun Microsystems 1530af28f636SEnrico Perla - Sun Microsystems if (nowrite) 1531af28f636SEnrico Perla - Sun Microsystems device->part_fd = open(raw, O_RDONLY); 1532af28f636SEnrico Perla - Sun Microsystems else 1533af28f636SEnrico Perla - Sun Microsystems device->part_fd = open(raw, O_RDWR); 1534af28f636SEnrico Perla - Sun Microsystems 1535af28f636SEnrico Perla - Sun Microsystems if (device->part_fd < 0 || fstat(device->part_fd, &stat) != 0) { 1536af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, OPEN_FAIL, raw); 1537af28f636SEnrico Perla - Sun Microsystems free(raw); 1538af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1539af28f636SEnrico Perla - Sun Microsystems } 1540af28f636SEnrico Perla - Sun Microsystems 1541af28f636SEnrico Perla - Sun Microsystems if (S_ISCHR(stat.st_mode) == 0) { 1542af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, NOT_RAW_DEVICE, raw); 1543af28f636SEnrico Perla - Sun Microsystems (void) close(device->part_fd); 1544af28f636SEnrico Perla - Sun Microsystems device->part_fd = -1; 1545af28f636SEnrico Perla - Sun Microsystems free(raw); 1546af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 1547af28f636SEnrico Perla - Sun Microsystems } 1548af28f636SEnrico Perla - Sun Microsystems 1549af28f636SEnrico Perla - Sun Microsystems free(raw); 1550af28f636SEnrico Perla - Sun Microsystems return (BC_SUCCESS); 1551af28f636SEnrico Perla - Sun Microsystems } 1552af28f636SEnrico Perla - Sun Microsystems 15537c478bd9Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 1554af28f636SEnrico Perla - Sun Microsystems static int 1555af28f636SEnrico Perla - Sun Microsystems copy_stage2_to_pcfs(ig_data_t *install) 15567c478bd9Sstevel@tonic-gate { 15577c478bd9Sstevel@tonic-gate FILE *mntfp; 1558af28f636SEnrico Perla - Sun Microsystems int pcfs_fp; 1559af28f636SEnrico Perla - Sun Microsystems int status = BC_ERROR; 15607c478bd9Sstevel@tonic-gate char buf[SECTOR_SIZE]; 15617c478bd9Sstevel@tonic-gate char *cp; 15627c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 1563af28f636SEnrico Perla - Sun Microsystems ig_device_t *device = &install->device; 1564af28f636SEnrico Perla - Sun Microsystems ig_stage2_t *stage2 = &install->stage2; 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 1567af28f636SEnrico Perla - Sun Microsystems (void) strncpy(buf, device->path, sizeof (buf)); 15687c478bd9Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 15697c478bd9Sstevel@tonic-gate cp = strchr(buf, 'r'); 15707c478bd9Sstevel@tonic-gate if (cp == NULL) { 1571af28f636SEnrico Perla - Sun Microsystems (void) fprintf(stderr, CONVERT_FAIL, device->path); 1572af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate do { 15757c478bd9Sstevel@tonic-gate *cp = *(cp + 1); 15767c478bd9Sstevel@tonic-gate } while (*(++cp)); 15777c478bd9Sstevel@tonic-gate 15787c478bd9Sstevel@tonic-gate /* get the mount point, if any */ 15797c478bd9Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 15807c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 15817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 1582af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 15837c478bd9Sstevel@tonic-gate } 15847c478bd9Sstevel@tonic-gate 15857c478bd9Sstevel@tonic-gate mpref.mnt_special = buf; 15867c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 15877c478bd9Sstevel@tonic-gate char cmd[128]; 15887c478bd9Sstevel@tonic-gate 15897c478bd9Sstevel@tonic-gate /* not mounted, try remount */ 15907c478bd9Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 15917c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 15927c478bd9Sstevel@tonic-gate buf, TMP_MNTPT); 15937c478bd9Sstevel@tonic-gate (void) system(cmd); 15947c478bd9Sstevel@tonic-gate rewind(mntfp); 15957c478bd9Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 15967c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 15977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 1598af28f636SEnrico Perla - Sun Microsystems return (BC_ERROR); 15997c478bd9Sstevel@tonic-gate } 16007c478bd9Sstevel@tonic-gate } 16017c478bd9Sstevel@tonic-gate 16027c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 16037c478bd9Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 16047c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 16057c478bd9Sstevel@tonic-gate (void) strcat(buf, "/grub"); 16067c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 16077c478bd9Sstevel@tonic-gate 16087c478bd9Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 16097c478bd9Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 16107c478bd9Sstevel@tonic-gate if (pcfs_fp == -1) { 16117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 16127c478bd9Sstevel@tonic-gate perror("open:"); 1613af28f636SEnrico Perla - Sun Microsystems goto out; 16147c478bd9Sstevel@tonic-gate } 16157c478bd9Sstevel@tonic-gate 1616af28f636SEnrico Perla - Sun Microsystems /* write stage2 to the pcfs mounted filesystem. */ 1617af28f636SEnrico Perla - Sun Microsystems if (write(pcfs_fp, stage2->file, stage2->file_size) 1618af28f636SEnrico Perla - Sun Microsystems != stage2->file_size) { 1619af28f636SEnrico Perla - Sun Microsystems perror(gettext("Error writing stage2")); 1620af28f636SEnrico Perla - Sun Microsystems goto out; 16217c478bd9Sstevel@tonic-gate } 1622af28f636SEnrico Perla - Sun Microsystems 1623af28f636SEnrico Perla - Sun Microsystems status = BC_SUCCESS; 1624af28f636SEnrico Perla - Sun Microsystems out_fd: 16257c478bd9Sstevel@tonic-gate (void) close(pcfs_fp); 1626af28f636SEnrico Perla - Sun Microsystems out: 16277c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 1628af28f636SEnrico Perla - Sun Microsystems (void) rmdir(TMP_MNTPT); 1629af28f636SEnrico Perla - Sun Microsystems return (status); 16307c478bd9Sstevel@tonic-gate } 1631