Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / cmd / boot / installgrub / installgrub.c
blob50359029082c325b31b7d6860c2c2c9ebcac58ca
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
24 * Copyright 2016 Toomas Soome <tsoome@me.com>
25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <libgen.h>
31 #include <malloc.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <strings.h>
36 #include <libintl.h>
37 #include <locale.h>
38 #include <errno.h>
39 #include <libfdisk.h>
40 #include <stdarg.h>
41 #include <assert.h>
43 #include <sys/mount.h>
44 #include <sys/mnttab.h>
45 #include <sys/dktp/fdisk.h>
46 #include <sys/dkio.h>
47 #include <sys/vtoc.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/multiboot.h>
51 #include <sys/sysmacros.h>
52 #include <sys/efi_partition.h>
54 #include <libnvpair.h>
55 #include <libfstyp.h>
57 #include "message.h"
58 #include "installgrub.h"
59 #include "./../common/bblk_einfo.h"
60 #include "./../common/boot_utils.h"
61 #include "./../common/mboot_extra.h"
62 #include "getresponse.h"
64 #ifndef TEXT_DOMAIN
65 #define TEXT_DOMAIN "SUNW_OST_OSCMD"
66 #endif
69 * Variables to track installgrub desired mode of operation.
70 * 'nowrite' and 'boot_debug' come from boot_common.h.
72 static boolean_t write_mbr = B_FALSE;
73 static boolean_t force_mbr = B_FALSE;
74 static boolean_t force_update = B_FALSE;
75 static boolean_t do_getinfo = B_FALSE;
76 static boolean_t do_version = B_FALSE;
77 static boolean_t do_mirror_bblk = B_FALSE;
78 static boolean_t strip = B_FALSE;
79 static boolean_t verbose_dump = B_FALSE;
81 /* Installing the bootblock is the default operation. */
82 static boolean_t do_install = B_TRUE;
84 /* Versioning string, if present. */
85 static char *update_str;
88 * Temporary buffer to store the first 32K of data looking for a multiboot
89 * signature.
91 char mboot_scan[MBOOT_SCAN_SIZE];
93 /* Function prototypes. */
94 static void check_options(char *);
95 static int handle_install(char *, char **);
96 static int handle_mirror(char *, char **);
97 static int handle_getinfo(char *, char **);
98 static int commit_to_disk(ig_data_t *, char *);
99 static int init_device(ig_device_t *, char *path);
100 static void cleanup_device(ig_device_t *);
101 static void cleanup_stage2(ig_stage2_t *);
102 static int get_start_sector(ig_device_t *);
103 static int get_disk_fd(ig_device_t *device);
104 static int get_raw_partition_fd(ig_device_t *);
105 static char *get_raw_partition_path(ig_device_t *);
106 static int propagate_bootblock(ig_data_t *, ig_data_t *, char *);
107 static int find_x86_bootpar(struct mboot *, int *, uint32_t *);
108 static int write_stage2(ig_data_t *);
109 static int write_stage1(ig_data_t *);
110 static void usage(char *);
111 static int read_stage1_from_file(char *, ig_data_t *);
112 static int read_stage2_from_file(char *, ig_data_t *);
113 static int read_stage1_from_disk(int, char *);
114 static int read_stage2_from_disk(int, ig_stage2_t *, int);
115 static int prepare_stage1(ig_data_t *);
116 static int prepare_stage2(ig_data_t *, char *);
117 static void prepare_fake_multiboot(ig_stage2_t *);
118 static void add_stage2_einfo(ig_stage2_t *, char *updt_str);
119 static boolean_t is_update_necessary(ig_data_t *, char *);
121 extern int read_stage2_blocklist(int, unsigned int *);
124 main(int argc, char *argv[])
126 int opt;
127 int params = 3;
128 int ret;
129 char **handle_args;
130 char *progname;
132 (void) setlocale(LC_ALL, "");
133 (void) textdomain(TEXT_DOMAIN);
134 if (init_yes() < 0) {
135 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
136 strerror(errno));
137 exit(BC_ERROR);
141 * retro-compatibility: installing the bootblock is the default
142 * and there is no switch for it.
144 do_install = B_TRUE;
146 while ((opt = getopt(argc, argv, "dVMFfmneiu:")) != EOF) {
147 switch (opt) {
148 case 'm':
149 write_mbr = B_TRUE;
150 break;
151 case 'n':
152 nowrite = B_TRUE;
153 break;
154 case 'f':
155 force_mbr = B_TRUE;
156 break;
157 case 'i':
158 do_getinfo = B_TRUE;
159 do_install = B_FALSE;
160 params = 1;
161 break;
162 case 'V':
163 verbose_dump = B_TRUE;
164 break;
165 case 'd':
166 boot_debug = B_TRUE;
167 break;
168 case 'F':
169 force_update = B_TRUE;
170 break;
171 case 'e':
172 strip = B_TRUE;
173 break;
174 case 'M':
175 do_mirror_bblk = B_TRUE;
176 do_install = B_FALSE;
177 params = 2;
178 break;
179 case 'u':
180 do_version = B_TRUE;
182 update_str = malloc(strlen(optarg) + 1);
183 if (update_str == NULL) {
184 (void) fprintf(stderr, gettext("Unable to "
185 "allocate memory\n"));
186 exit(BC_ERROR);
188 (void) strlcpy(update_str, optarg, strlen(optarg) + 1);
189 break;
190 default:
191 /* fall through to process non-optional args */
192 break;
196 /* check arguments */
197 if (argc != optind + params) {
198 usage(argv[0]);
199 exit(BC_ERROR);
203 * clean up options (and bail out if an unrecoverable combination is
204 * requested.
206 progname = argv[0];
207 check_options(progname);
208 handle_args = argv + optind;
210 if (nowrite)
211 (void) fprintf(stdout, DRY_RUN);
213 if (do_getinfo) {
214 ret = handle_getinfo(progname, handle_args);
215 } else if (do_mirror_bblk) {
216 ret = handle_mirror(progname, handle_args);
217 } else {
218 ret = handle_install(progname, handle_args);
220 return (ret);
223 #define MEANINGLESS_OPT gettext("%s specified but meaningless, ignoring\n")
224 static void
225 check_options(char *progname)
227 if (do_getinfo && do_mirror_bblk) {
228 (void) fprintf(stderr, gettext("Only one of -M and -i can be "
229 "specified at the same time\n"));
230 usage(progname);
231 exit(BC_ERROR);
234 if (do_mirror_bblk) {
236 * -u and -F may actually reflect a user intent that is not
237 * correct with this command (mirror can be interpreted
238 * "similar" to install. Emit a message and continue.
239 * -e and -V have no meaning, be quiet here and only report the
240 * incongruence if a debug output is requested.
242 if (do_version) {
243 (void) fprintf(stderr, MEANINGLESS_OPT, "-u");
244 do_version = B_FALSE;
246 if (force_update) {
247 (void) fprintf(stderr, MEANINGLESS_OPT, "-F");
248 force_update = B_FALSE;
250 if (strip || verbose_dump) {
251 BOOT_DEBUG(MEANINGLESS_OPT, "-e|-V");
252 strip = B_FALSE;
253 verbose_dump = B_FALSE;
257 if (do_getinfo) {
258 if (write_mbr || force_mbr || do_version || force_update) {
259 BOOT_DEBUG(MEANINGLESS_OPT, "-m|-f|-u|-F");
260 write_mbr = force_mbr = do_version = B_FALSE;
261 force_update = B_FALSE;
267 * Install a new stage1/stage2 pair on the specified device. handle_install()
268 * expects argv to contain 3 parameters (the path to stage1, the path to stage2,
269 * the target device).
271 * Returns: BC_SUCCESS - if the installation is successful
272 * BC_ERROR - if the installation failed
273 * BC_NOUPDT - if no installation was performed because the GRUB
274 * version currently installed is more recent than the
275 * supplied one.
278 static int
279 handle_install(char *progname, char **argv)
281 ig_data_t install_data;
282 char *stage1_path = NULL;
283 char *stage2_path = NULL;
284 char *device_path = NULL;
285 int ret = BC_ERROR;
287 stage1_path = strdup(argv[0]);
288 stage2_path = strdup(argv[1]);
289 device_path = strdup(argv[2]);
291 bzero(&install_data, sizeof (ig_data_t));
293 if (!stage1_path || !stage2_path || !device_path) {
294 (void) fprintf(stderr, gettext("Missing parameter"));
295 usage(progname);
296 goto out;
299 BOOT_DEBUG("stage1 path: %s, stage2 path: %s, device: %s\n",
300 stage1_path, stage2_path, device_path);
302 if (init_device(&install_data.device, device_path) != BC_SUCCESS) {
303 (void) fprintf(stderr, gettext("Unable to gather device "
304 "information for %s\n"), device_path);
305 goto out;
308 /* read in stage1 and stage2. */
309 if (read_stage1_from_file(stage1_path, &install_data) != BC_SUCCESS) {
310 (void) fprintf(stderr, gettext("Error opening %s\n"),
311 stage1_path);
312 goto out_dev;
315 if (read_stage2_from_file(stage2_path, &install_data) != BC_SUCCESS) {
316 (void) fprintf(stderr, gettext("Error opening %s\n"),
317 stage2_path);
318 goto out_dev;
321 /* We do not support versioning on PCFS. */
322 if (is_bootpar(install_data.device.type) && do_version)
323 do_version = B_FALSE;
326 * is_update_necessary() will take care of checking if versioning and/or
327 * forcing the update have been specified. It will also emit a warning
328 * if a non-versioned update is attempted over a versioned bootblock.
330 if (!is_update_necessary(&install_data, update_str)) {
331 (void) fprintf(stderr, gettext("GRUB version installed "
332 "on %s is more recent or identical\n"
333 "Use -F to override or install without the -u option\n"),
334 device_path);
335 ret = BC_NOUPDT;
336 goto out_dev;
339 * We get here if:
340 * - the installed GRUB version is older than the one about to be
341 * installed.
342 * - no versioning string has been passed through the command line.
343 * - a forced update is requested (-F).
345 BOOT_DEBUG("Ready to commit to disk\n");
346 ret = commit_to_disk(&install_data, update_str);
348 out_dev:
349 cleanup_device(&install_data.device);
350 out:
351 free(stage1_path);
352 free(stage2_path);
353 free(device_path);
354 return (ret);
358 * Retrieves from a device the extended information (einfo) associated to the
359 * installed stage2.
360 * Expects one parameter, the device path, in the form: /dev/rdsk/c?[t?]d?s0.
361 * Returns:
362 * - BC_SUCCESS (and prints out einfo contents depending on 'flags')
363 * - BC_ERROR (on error)
364 * - BC_NOEINFO (no extended information available)
366 static int
367 handle_getinfo(char *progname, char **argv)
369 ig_data_t data;
370 ig_stage2_t *stage2 = &data.stage2;
371 ig_device_t *device = &data.device;
372 bblk_einfo_t *einfo;
373 uint8_t flags = 0;
374 uint32_t size;
375 char *device_path;
376 int retval = BC_ERROR;
377 int ret;
379 device_path = strdup(argv[0]);
380 if (!device_path) {
381 (void) fprintf(stderr, gettext("Missing parameter"));
382 usage(progname);
383 goto out;
386 bzero(&data, sizeof (ig_data_t));
387 BOOT_DEBUG("device path: %s\n", device_path);
389 if (init_device(device, device_path) != BC_SUCCESS) {
390 (void) fprintf(stderr, gettext("Unable to gather device "
391 "information for %s\n"), device_path);
392 goto out_dev;
395 if (is_bootpar(device->type)) {
396 (void) fprintf(stderr, gettext("Versioning not supported on "
397 "PCFS\n"));
398 goto out_dev;
401 ret = read_stage2_from_disk(device->part_fd, stage2, device->type);
402 if (ret == BC_ERROR) {
403 (void) fprintf(stderr, gettext("Error reading stage2 from "
404 "%s\n"), device_path);
405 goto out_dev;
408 if (ret == BC_NOEXTRA) {
409 (void) fprintf(stdout, gettext("No multiboot header found on "
410 "%s, unable to locate extra information area\n"),
411 device_path);
412 retval = BC_NOEINFO;
413 goto out_dev;
416 einfo = find_einfo(stage2->extra, stage2->extra_size);
417 if (einfo == NULL) {
418 retval = BC_NOEINFO;
419 (void) fprintf(stderr, gettext("No extended information "
420 "found\n"));
421 goto out_dev;
424 /* Print the extended information. */
425 if (strip)
426 flags |= EINFO_EASY_PARSE;
427 if (verbose_dump)
428 flags |= EINFO_PRINT_HEADER;
430 size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
431 print_einfo(flags, einfo, size);
432 retval = BC_SUCCESS;
434 out_dev:
435 cleanup_device(&data.device);
436 out:
437 free(device_path);
438 return (retval);
442 * Attempt to mirror (propagate) the current stage2 over the attaching disk.
444 * Returns:
445 * - BC_SUCCESS (a successful propagation happened)
446 * - BC_ERROR (an error occurred)
447 * - BC_NOEXTRA (it is not possible to dump the current bootblock since
448 * there is no multiboot information)
450 static int
451 handle_mirror(char *progname, char **argv)
453 ig_data_t curr_data;
454 ig_data_t attach_data;
455 ig_device_t *curr_device = &curr_data.device;
456 ig_device_t *attach_device = &attach_data.device;
457 ig_stage2_t *stage2_curr = &curr_data.stage2;
458 ig_stage2_t *stage2_attach = &attach_data.stage2;
459 bblk_einfo_t *einfo_curr = NULL;
460 char *curr_device_path;
461 char *attach_device_path;
462 char *updt_str = NULL;
463 int retval = BC_ERROR;
464 int ret;
466 curr_device_path = strdup(argv[0]);
467 attach_device_path = strdup(argv[1]);
469 if (!curr_device_path || !attach_device_path) {
470 (void) fprintf(stderr, gettext("Missing parameter"));
471 usage(progname);
472 goto out;
474 BOOT_DEBUG("Current device path is: %s, attaching device path is: "
475 " %s\n", curr_device_path, attach_device_path);
477 bzero(&curr_data, sizeof (ig_data_t));
478 bzero(&attach_data, sizeof (ig_data_t));
480 if (init_device(curr_device, curr_device_path) != BC_SUCCESS) {
481 (void) fprintf(stderr, gettext("Unable to gather device "
482 "information for %s (current device)\n"), curr_device_path);
483 goto out_currdev;
486 if (init_device(attach_device, attach_device_path) != BC_SUCCESS) {
487 (void) fprintf(stderr, gettext("Unable to gather device "
488 "information for %s (attaching device)\n"),
489 attach_device_path);
490 goto out_devs;
493 if (is_bootpar(curr_device->type) || is_bootpar(attach_device->type)) {
494 (void) fprintf(stderr, gettext("boot block mirroring is not "
495 "supported on PCFS\n"));
496 goto out_devs;
499 ret = read_stage2_from_disk(curr_device->part_fd, stage2_curr,
500 curr_device->type);
501 if (ret == BC_ERROR) {
502 BOOT_DEBUG("Error reading first stage2 blocks from %s\n",
503 curr_device->path);
504 retval = BC_ERROR;
505 goto out_devs;
508 if (ret == BC_NOEXTRA) {
509 BOOT_DEBUG("No multiboot header found on %s, unable to grab "
510 "stage2\n", curr_device->path);
511 retval = BC_NOEXTRA;
512 goto out_devs;
515 einfo_curr = find_einfo(stage2_curr->extra, stage2_curr->extra_size);
516 if (einfo_curr != NULL)
517 updt_str = einfo_get_string(einfo_curr);
519 write_mbr = B_TRUE;
520 force_mbr = B_TRUE;
521 retval = propagate_bootblock(&curr_data, &attach_data, updt_str);
522 cleanup_stage2(stage2_curr);
523 cleanup_stage2(stage2_attach);
525 out_devs:
526 cleanup_device(attach_device);
527 out_currdev:
528 cleanup_device(curr_device);
529 out:
530 free(curr_device_path);
531 free(attach_device_path);
532 return (retval);
535 static int
536 commit_to_disk(ig_data_t *install, char *updt_str)
538 assert(install != NULL);
540 * vanilla stage1 and stage2 need to be updated at runtime.
541 * Update stage2 before stage1 because stage1 needs to know the first
542 * sector stage2 will be written to.
544 if (prepare_stage2(install, updt_str) != BC_SUCCESS) {
545 (void) fprintf(stderr, gettext("Error building stage2\n"));
546 return (BC_ERROR);
548 if (prepare_stage1(install) != BC_SUCCESS) {
549 (void) fprintf(stderr, gettext("Error building stage1\n"));
550 return (BC_ERROR);
553 /* Write stage2 out to disk. */
554 if (write_stage2(install) != BC_SUCCESS) {
555 (void) fprintf(stderr, gettext("Error writing stage2 to "
556 "disk\n"));
557 return (BC_ERROR);
560 /* Write stage1 to disk and, if requested, to the MBR. */
561 if (write_stage1(install) != BC_SUCCESS) {
562 (void) fprintf(stderr, gettext("Error writing stage1 to "
563 "disk\n"));
564 return (BC_ERROR);
567 return (BC_SUCCESS);
571 * Propagate the bootblock on the source disk to the destination disk and
572 * version it with 'updt_str' in the process. Since we cannot trust any data
573 * on the attaching disk, we do not perform any specific check on a potential
574 * target extended information structure and we just blindly update.
576 static int
577 propagate_bootblock(ig_data_t *source, ig_data_t *target, char *updt_str)
579 ig_device_t *src_device = &source->device;
580 ig_device_t *dest_device = &target->device;
581 ig_stage2_t *src_stage2 = &source->stage2;
582 ig_stage2_t *dest_stage2 = &target->stage2;
583 uint32_t buf_size;
584 int retval;
586 assert(source != NULL);
587 assert(target != NULL);
589 /* read in stage1 from the source disk. */
590 if (read_stage1_from_disk(src_device->part_fd, target->stage1_buf)
591 != BC_SUCCESS)
592 return (BC_ERROR);
594 /* Prepare target stage2 for commit_to_disk. */
595 cleanup_stage2(dest_stage2);
597 if (updt_str != NULL)
598 do_version = B_TRUE;
599 else
600 do_version = B_FALSE;
602 buf_size = src_stage2->file_size + SECTOR_SIZE;
604 dest_stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);
605 dest_stage2->buf = malloc(dest_stage2->buf_size);
606 if (dest_stage2->buf == NULL) {
607 perror(gettext("Memory allocation failed"));
608 return (BC_ERROR);
610 dest_stage2->file = dest_stage2->buf;
611 dest_stage2->file_size = src_stage2->file_size;
612 memcpy(dest_stage2->file, src_stage2->file, dest_stage2->file_size);
613 dest_stage2->extra = dest_stage2->buf +
614 P2ROUNDUP(dest_stage2->file_size, 8);
616 /* If we get down here we do have a mboot structure. */
617 assert(src_stage2->mboot);
619 dest_stage2->mboot_off = src_stage2->mboot_off;
620 dest_stage2->mboot = (multiboot_header_t *)(dest_stage2->buf +
621 dest_stage2->mboot_off);
623 (void) fprintf(stdout, gettext("Propagating %s stage1/stage2 to %s\n"),
624 src_device->path, dest_device->path);
625 retval = commit_to_disk(target, updt_str);
627 return (retval);
631 * open the device and fill the various members of ig_device_t.
633 static int
634 init_device(ig_device_t *device, char *path)
636 struct dk_gpt *vtoc;
637 fstyp_handle_t fhdl;
638 const char *fident;
640 bzero(device, sizeof (*device));
641 device->part_fd = -1;
642 device->disk_fd = -1;
643 device->path_p0 = NULL;
645 device->path = strdup(path);
646 if (device->path == NULL) {
647 perror(gettext("Memory allocation failed"));
648 return (BC_ERROR);
651 if (strstr(device->path, "diskette")) {
652 (void) fprintf(stderr, gettext("installing GRUB to a floppy "
653 "disk is no longer supported\n"));
654 return (BC_ERROR);
657 /* Detect if the target device is a pcfs partition. */
658 if (strstr(device->path, "p0:boot"))
659 device->type = IG_DEV_X86BOOTPAR;
661 if (get_disk_fd(device) != BC_SUCCESS)
662 return (BC_ERROR);
664 /* read in the device boot sector. */
665 if (read(device->disk_fd, device->boot_sector, SECTOR_SIZE)
666 != SECTOR_SIZE) {
667 (void) fprintf(stderr, gettext("Error reading boot sector\n"));
668 perror("read");
669 return (BC_ERROR);
672 if (efi_alloc_and_read(device->disk_fd, &vtoc) >= 0) {
673 device->type = IG_DEV_EFI;
674 efi_free(vtoc);
677 if (get_raw_partition_fd(device) != BC_SUCCESS)
678 return (BC_ERROR);
680 if (is_efi(device->type)) {
681 if (fstyp_init(device->part_fd, 0, NULL, &fhdl) != 0)
682 return (BC_ERROR);
684 if (fstyp_ident(fhdl, "zfs", &fident) != 0) {
685 fstyp_fini(fhdl);
686 (void) fprintf(stderr, gettext("Booting of EFI labeled "
687 "disks is only supported with ZFS\n"));
688 return (BC_ERROR);
690 fstyp_fini(fhdl);
693 if (get_start_sector(device) != BC_SUCCESS)
694 return (BC_ERROR);
696 return (BC_SUCCESS);
699 static void
700 cleanup_device(ig_device_t *device)
702 free(device->path);
703 free(device->path_p0);
705 if (device->part_fd != -1)
706 (void) close(device->part_fd);
707 if (device->disk_fd != -1)
708 (void) close(device->disk_fd);
710 bzero(device, sizeof (ig_device_t));
711 device->part_fd = -1;
712 device->disk_fd = -1;
715 static void
716 cleanup_stage2(ig_stage2_t *stage2)
718 free(stage2->buf);
719 bzero(stage2, sizeof (ig_stage2_t));
722 static int
723 get_start_sector(ig_device_t *device)
725 uint32_t secnum = 0, numsec = 0;
726 int i, pno, rval, log_part = 0;
727 struct mboot *mboot;
728 struct ipart *part = NULL;
729 ext_part_t *epp;
730 struct part_info dkpi;
731 struct extpart_info edkpi;
733 if (is_efi(device->type)) {
734 struct dk_gpt *vtoc;
736 if (efi_alloc_and_read(device->disk_fd, &vtoc) < 0)
737 return (BC_ERROR);
739 device->start_sector = vtoc->efi_parts[device->slice].p_start;
740 /* GPT doesn't use traditional slice letters */
741 device->slice = 0xff;
742 device->partition = 0;
744 efi_free(vtoc);
745 goto found_part;
748 mboot = (struct mboot *)device->boot_sector;
750 if (is_bootpar(device->type)) {
751 if (find_x86_bootpar(mboot, &pno, &secnum) != BC_SUCCESS) {
752 (void) fprintf(stderr, NOBOOTPAR);
753 return (BC_ERROR);
754 } else {
755 device->start_sector = secnum;
756 device->partition = pno;
757 goto found_part;
762 * Search for Solaris fdisk partition
763 * Get the solaris partition information from the device
764 * and compare the offset of S2 with offset of solaris partition
765 * from fdisk partition table.
767 if (ioctl(device->part_fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
768 if (ioctl(device->part_fd, DKIOCPARTINFO, &dkpi) < 0) {
769 (void) fprintf(stderr, PART_FAIL);
770 return (BC_ERROR);
771 } else {
772 edkpi.p_start = dkpi.p_start;
776 for (i = 0; i < FD_NUMPART; i++) {
777 part = (struct ipart *)mboot->parts + i;
779 if (part->relsect == 0) {
780 (void) fprintf(stderr, BAD_PART, i);
781 return (BC_ERROR);
784 if (edkpi.p_start >= part->relsect &&
785 edkpi.p_start < (part->relsect + part->numsect)) {
786 /* Found the partition */
787 break;
791 if (i == FD_NUMPART) {
792 /* No solaris fdisk partitions (primary or logical) */
793 (void) fprintf(stderr, NOSOLPAR);
794 return (BC_ERROR);
798 * We have found a Solaris fdisk partition (primary or extended)
799 * Handle the simple case first: Solaris in a primary partition
801 if (!fdisk_is_dos_extended(part->systid)) {
802 device->start_sector = part->relsect;
803 device->partition = i;
804 goto found_part;
808 * Solaris in a logical partition. Find that partition in the
809 * extended part.
811 if ((rval = libfdisk_init(&epp, device->path_p0, NULL, FDISK_READ_DISK))
812 != FDISK_SUCCESS) {
813 switch (rval) {
815 * The first 3 cases are not an error per-se, just that
816 * there is no Solaris logical partition
818 case FDISK_EBADLOGDRIVE:
819 case FDISK_ENOLOGDRIVE:
820 case FDISK_EBADMAGIC:
821 (void) fprintf(stderr, NOSOLPAR);
822 return (BC_ERROR);
823 case FDISK_ENOVGEOM:
824 (void) fprintf(stderr, NO_VIRT_GEOM);
825 return (BC_ERROR);
826 case FDISK_ENOPGEOM:
827 (void) fprintf(stderr, NO_PHYS_GEOM);
828 return (BC_ERROR);
829 case FDISK_ENOLGEOM:
830 (void) fprintf(stderr, NO_LABEL_GEOM);
831 return (BC_ERROR);
832 default:
833 (void) fprintf(stderr, LIBFDISK_INIT_FAIL);
834 return (BC_ERROR);
838 rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
839 libfdisk_fini(&epp);
840 if (rval != FDISK_SUCCESS) {
841 /* No solaris logical partition */
842 (void) fprintf(stderr, NOSOLPAR);
843 return (BC_ERROR);
846 device->start_sector = secnum;
847 device->partition = pno - 1;
848 log_part = 1;
850 found_part:
851 /* get confirmation for -m */
852 if (write_mbr && !force_mbr) {
853 (void) fprintf(stdout, MBOOT_PROMPT);
854 if (!yes()) {
855 write_mbr = 0;
856 (void) fprintf(stdout, MBOOT_NOT_UPDATED);
857 return (BC_ERROR);
862 * Currently if Solaris is in an extended partition we need to
863 * write GRUB to the MBR. Check for this.
865 if (log_part && !write_mbr) {
866 (void) fprintf(stdout, gettext("Installing Solaris on an "
867 "extended partition... forcing MBR update\n"));
868 write_mbr = 1;
872 * warn, if Solaris in primary partition and GRUB not in MBR and
873 * partition is not active
875 if (part != NULL) {
876 if (!log_part && part->bootid != 128 && !write_mbr) {
877 (void) fprintf(stdout, SOLPAR_INACTIVE,
878 device->partition + 1);
882 return (BC_SUCCESS);
885 static int
886 get_disk_fd(ig_device_t *device)
888 int i = 0;
889 char save[2] = { '\0', '\0' };
890 char *end = NULL;
892 assert(device != NULL);
893 assert(device->path != NULL);
895 if (is_bootpar(device->type)) {
896 end = strstr(device->path, "p0:boot");
897 /* tested at the start of init_device() */
898 assert(end != NULL);
899 /* chop off :boot */
900 save[0] = end[2];
901 end[2] = '\0';
902 } else {
903 i = strlen(device->path);
904 save[0] = device->path[i - 2];
905 save[1] = device->path[i - 1];
906 device->path[i - 2] = 'p';
907 device->path[i - 1] = '0';
910 if (nowrite)
911 device->disk_fd = open(device->path, O_RDONLY);
912 else
913 device->disk_fd = open(device->path, O_RDWR);
915 device->path_p0 = strdup(device->path);
916 if (device->path_p0 == NULL) {
917 perror("strdup");
918 return (BC_ERROR);
921 if (is_bootpar(device->type)) {
922 end[2] = save[0];
923 } else {
924 device->path[i - 2] = save[0];
925 device->path[i - 1] = save[1];
928 if (device->disk_fd == -1) {
929 perror("open");
930 return (BC_ERROR);
933 return (BC_SUCCESS);
936 static void
937 prepare_fake_multiboot(ig_stage2_t *stage2)
939 multiboot_header_t *mboot;
941 assert(stage2 != NULL);
942 assert(stage2->mboot != NULL);
943 assert(stage2->buf != NULL);
945 mboot = stage2->mboot;
948 * Currently we expect find_multiboot() to have located a multiboot
949 * header with the AOUT kludge flag set.
951 assert(mboot->flags & BB_MBOOT_AOUT_FLAG);
953 /* Insert the information necessary to locate stage2. */
954 mboot->header_addr = stage2->mboot_off;
955 mboot->load_addr = 0;
956 mboot->load_end_addr = stage2->file_size;
959 static void
960 add_stage2_einfo(ig_stage2_t *stage2, char *updt_str)
962 bblk_hs_t hs;
963 uint32_t avail_space;
965 assert(stage2 != NULL);
967 /* Fill bootblock hashing source information. */
968 hs.src_buf = (unsigned char *)stage2->file;
969 hs.src_size = stage2->file_size;
970 /* How much space for the extended information structure? */
971 avail_space = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
972 add_einfo(stage2->extra, updt_str, &hs, avail_space);
976 static int
977 write_stage2(ig_data_t *install)
979 ig_device_t *device = &install->device;
980 ig_stage2_t *stage2 = &install->stage2;
981 off_t offset;
983 assert(install != NULL);
985 if (is_bootpar(device->type)) {
987 * stage2 is already on the filesystem, we only need to update
988 * the first two blocks (that we have modified during
989 * prepare_stage2())
991 if (write_out(device->part_fd, stage2->file, SECTOR_SIZE,
992 stage2->pcfs_first_sectors[0] * SECTOR_SIZE)
993 != BC_SUCCESS ||
994 write_out(device->part_fd, stage2->file + SECTOR_SIZE,
995 SECTOR_SIZE, stage2->pcfs_first_sectors[1] * SECTOR_SIZE)
996 != BC_SUCCESS) {
997 (void) fprintf(stderr, WRITE_FAIL_STAGE2);
998 return (BC_ERROR);
1000 (void) fprintf(stdout, WRITE_STAGE2_PCFS);
1001 return (BC_SUCCESS);
1005 * For disk, write stage2 starting at STAGE2_BLKOFF sector.
1006 * Note that we use stage2->buf rather than stage2->file, because we
1007 * may have extended information after the latter.
1009 * If we're writing to an EFI-labeled disk where stage2 lives in the
1010 * 3.5MB boot loader gap following the ZFS vdev labels, make sure the
1011 * size of the buffer doesn't exceed the size of the gap.
1013 if (is_efi(device->type) && stage2->buf_size > STAGE2_MAXSIZE) {
1014 (void) fprintf(stderr, WRITE_FAIL_STAGE2);
1015 return (BC_ERROR);
1018 offset = STAGE2_BLKOFF(device->type) * SECTOR_SIZE;
1020 if (write_out(device->part_fd, stage2->buf, stage2->buf_size,
1021 offset) != BC_SUCCESS) {
1022 perror("write");
1023 return (BC_ERROR);
1026 /* Simulate the "old" installgrub output. */
1027 (void) fprintf(stdout, WRITE_STAGE2_DISK, device->partition,
1028 (stage2->buf_size / SECTOR_SIZE) + 1, STAGE2_BLKOFF(device->type),
1029 stage2->first_sector);
1031 return (BC_SUCCESS);
1034 static int
1035 write_stage1(ig_data_t *install)
1037 ig_device_t *device = &install->device;
1039 assert(install != NULL);
1041 if (write_out(device->part_fd, install->stage1_buf,
1042 sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
1043 (void) fprintf(stdout, WRITE_FAIL_PBOOT);
1044 perror("write");
1045 return (BC_ERROR);
1048 /* Simulate "old" installgrub output. */
1049 (void) fprintf(stdout, WRITE_PBOOT, device->partition,
1050 device->start_sector);
1052 if (write_mbr) {
1053 if (write_out(device->disk_fd, install->stage1_buf,
1054 sizeof (install->stage1_buf), 0) != BC_SUCCESS) {
1055 (void) fprintf(stdout, WRITE_FAIL_BOOTSEC);
1056 perror("write");
1057 return (BC_ERROR);
1059 /* Simulate "old" installgrub output. */
1060 (void) fprintf(stdout, WRITE_MBOOT);
1063 return (BC_SUCCESS);
1066 #define USAGE_STRING "%s [-m|-f|-n|-F|-u verstr] stage1 stage2 device\n" \
1067 "%s -M [-n] device1 device2\n" \
1068 "%s [-V|-e] -i device\n" \
1070 #define CANON_USAGE_STR gettext(USAGE_STRING)
1072 static void
1073 usage(char *progname)
1075 (void) fprintf(stdout, CANON_USAGE_STR, progname, progname, progname);
1079 static int
1080 read_stage1_from_file(char *path, ig_data_t *dest)
1082 int fd;
1084 assert(dest);
1086 /* read the stage1 file from filesystem */
1087 fd = open(path, O_RDONLY);
1088 if (fd == -1 ||
1089 read(fd, dest->stage1_buf, SECTOR_SIZE) != SECTOR_SIZE) {
1090 (void) fprintf(stderr, READ_FAIL_STAGE1, path);
1091 return (BC_ERROR);
1093 (void) close(fd);
1094 return (BC_SUCCESS);
1097 static int
1098 read_stage2_from_file(char *path, ig_data_t *dest)
1100 int fd;
1101 struct stat sb;
1102 ig_stage2_t *stage2 = &dest->stage2;
1103 ig_device_t *device = &dest->device;
1104 uint32_t buf_size;
1106 assert(dest);
1107 assert(stage2->buf == NULL);
1109 fd = open(path, O_RDONLY);
1110 if (fstat(fd, &sb) == -1) {
1111 perror("fstat");
1112 goto out;
1115 stage2->file_size = sb.st_size;
1117 if (!is_bootpar(device->type)) {
1119 * buffer size needs to account for stage2 plus the extra
1120 * versioning information at the end of it. We reserve one
1121 * extra sector (plus we round up to the next sector boundary).
1123 buf_size = stage2->file_size + SECTOR_SIZE;
1124 } else {
1125 /* In the PCFS case we only need to read in stage2. */
1126 buf_size = stage2->file_size;
1129 stage2->buf_size = P2ROUNDUP(buf_size, SECTOR_SIZE);
1131 BOOT_DEBUG("stage2 buffer size = %d (%d sectors)\n", stage2->buf_size,
1132 stage2->buf_size / SECTOR_SIZE);
1134 stage2->buf = malloc(stage2->buf_size);
1135 if (stage2->buf == NULL) {
1136 perror(gettext("Memory allocation failed"));
1137 goto out_fd;
1140 stage2->file = stage2->buf;
1143 * Extra information (e.g. the versioning structure) is placed at the
1144 * end of stage2, aligned on a 8-byte boundary.
1146 if (!(is_bootpar(device->type)))
1147 stage2->extra = stage2->file + P2ROUNDUP(stage2->file_size, 8);
1149 if (lseek(fd, 0, SEEK_SET) == -1) {
1150 perror("lseek");
1151 goto out_alloc;
1154 if (read(fd, stage2->file, stage2->file_size) < 0) {
1155 perror(gettext("unable to read stage2"));
1156 goto out_alloc;
1159 (void) close(fd);
1160 return (BC_SUCCESS);
1162 out_alloc:
1163 free(stage2->buf);
1164 stage2->buf = NULL;
1165 out_fd:
1166 (void) close(fd);
1167 out:
1168 return (BC_ERROR);
1171 static int
1172 prepare_stage1(ig_data_t *install)
1174 ig_device_t *device = &install->device;
1176 assert(install != NULL);
1178 /* If PCFS add the BIOS Parameter Block. */
1179 if (is_bootpar(device->type)) {
1180 char bpb_sect[SECTOR_SIZE];
1182 if (pread(device->part_fd, bpb_sect, SECTOR_SIZE, 0)
1183 != SECTOR_SIZE) {
1184 (void) fprintf(stderr, READ_FAIL_BPB);
1185 return (BC_ERROR);
1187 bcopy(bpb_sect + STAGE1_BPB_OFFSET,
1188 install->stage1_buf + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
1191 /* copy MBR to stage1 in case of overwriting MBR sector. */
1192 bcopy(device->boot_sector + BOOTSZ, install->stage1_buf + BOOTSZ,
1193 SECTOR_SIZE - BOOTSZ);
1194 /* modify default stage1 file generated by GRUB. */
1195 *((unsigned char *)(install->stage1_buf + STAGE1_FORCE_LBA)) = 1;
1196 *((ulong_t *)(install->stage1_buf + STAGE1_STAGE2_SECTOR))
1197 = install->stage2.first_sector;
1198 *((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_ADDRESS))
1199 = STAGE2_MEMADDR;
1200 *((ushort_t *)(install->stage1_buf + STAGE1_STAGE2_SEGMENT))
1201 = STAGE2_MEMADDR >> 4;
1203 return (BC_SUCCESS);
1207 * Grab stage1 from the specified device file descriptor.
1209 static int
1210 read_stage1_from_disk(int dev_fd, char *stage1_buf)
1212 assert(stage1_buf != NULL);
1214 if (read_in(dev_fd, stage1_buf, SECTOR_SIZE, 0) != BC_SUCCESS) {
1215 perror(gettext("Unable to read stage1 from disk"));
1216 return (BC_ERROR);
1218 return (BC_SUCCESS);
1221 static int
1222 read_stage2_from_disk(int dev_fd, ig_stage2_t *stage2, int type)
1224 uint32_t size;
1225 uint32_t buf_size;
1226 uint32_t mboot_off;
1227 multiboot_header_t *mboot;
1229 assert(stage2 != NULL);
1230 assert(dev_fd != -1);
1232 if (read_in(dev_fd, mboot_scan, sizeof (mboot_scan),
1233 STAGE2_BLKOFF(type) * SECTOR_SIZE) != BC_SUCCESS) {
1234 perror(gettext("Error reading stage2 sectors"));
1235 return (BC_ERROR);
1238 /* No multiboot means no chance of knowing stage2 size */
1239 if (find_multiboot(mboot_scan, sizeof (mboot_scan), &mboot_off)
1240 != BC_SUCCESS) {
1241 BOOT_DEBUG("Unable to find multiboot header\n");
1242 return (BC_NOEXTRA);
1244 mboot = (multiboot_header_t *)(mboot_scan + mboot_off);
1247 * Unfilled mboot values mean an older version of installgrub installed
1248 * the stage2. Again we have no chance of knowing stage2 size.
1250 if (mboot->load_end_addr == 0 ||
1251 mboot->load_end_addr < mboot->load_addr)
1252 return (BC_NOEXTRA);
1255 * Currently, the amount of space reserved for extra information
1256 * is "fixed". We may have to scan for the terminating extra payload
1257 * in the future.
1259 size = mboot->load_end_addr - mboot->load_addr;
1260 buf_size = P2ROUNDUP(size + SECTOR_SIZE, SECTOR_SIZE);
1262 stage2->buf = malloc(buf_size);
1263 if (stage2->buf == NULL) {
1264 perror(gettext("Memory allocation failed"));
1265 return (BC_ERROR);
1267 stage2->buf_size = buf_size;
1269 if (read_in(dev_fd, stage2->buf, buf_size, STAGE2_BLKOFF(type) *
1270 SECTOR_SIZE) != BC_SUCCESS) {
1271 perror("read");
1272 free(stage2->buf);
1273 return (BC_ERROR);
1276 /* Update pointers. */
1277 stage2->file = stage2->buf;
1278 stage2->file_size = size;
1279 stage2->mboot_off = mboot_off;
1280 stage2->mboot = (multiboot_header_t *)(stage2->buf + stage2->mboot_off);
1281 stage2->extra = stage2->buf + P2ROUNDUP(stage2->file_size, 8);
1282 stage2->extra_size = stage2->buf_size - P2ROUNDUP(stage2->file_size, 8);
1284 return (BC_SUCCESS);
1287 static boolean_t
1288 is_update_necessary(ig_data_t *data, char *updt_str)
1290 bblk_einfo_t *einfo;
1291 bblk_hs_t stage2_hs;
1292 ig_stage2_t stage2_disk;
1293 ig_stage2_t *stage2_file = &data->stage2;
1294 ig_device_t *device = &data->device;
1295 int dev_fd = device->part_fd;
1297 assert(data != NULL);
1298 assert(device->part_fd != -1);
1300 bzero(&stage2_disk, sizeof (ig_stage2_t));
1302 /* Gather stage2 (if present) from the target device. */
1303 if (read_stage2_from_disk(dev_fd, &stage2_disk, device->type)
1304 != BC_SUCCESS) {
1305 BOOT_DEBUG("Unable to read stage2 from %s\n", device->path);
1306 BOOT_DEBUG("No multiboot wrapped stage2 on %s\n", device->path);
1307 return (B_TRUE);
1311 * Look for the extended information structure in the extra payload
1312 * area.
1314 einfo = find_einfo(stage2_disk.extra, stage2_disk.extra_size);
1315 if (einfo == NULL) {
1316 BOOT_DEBUG("No extended information available\n");
1317 return (B_TRUE);
1320 if (!do_version || updt_str == NULL) {
1321 (void) fprintf(stdout, "WARNING: target device %s has a "
1322 "versioned stage2 that is going to be overwritten by a non "
1323 "versioned one\n", device->path);
1324 return (B_TRUE);
1327 if (force_update) {
1328 BOOT_DEBUG("Forcing update of %s bootblock\n", device->path);
1329 return (B_TRUE);
1332 /* Compare the two extended information structures. */
1333 stage2_hs.src_buf = (unsigned char *)stage2_file->file;
1334 stage2_hs.src_size = stage2_file->file_size;
1336 return (einfo_should_update(einfo, &stage2_hs, updt_str));
1340 #define START_BLOCK(pos) (*(ulong_t *)(pos))
1341 #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4))
1342 #define START_SEG(pos) (*(ushort_t *)((pos) + 6))
1344 static int
1345 prepare_stage2(ig_data_t *install, char *updt_str)
1347 ig_device_t *device = &install->device;
1348 ig_stage2_t *stage2 = &install->stage2;
1349 uint32_t mboot_off = 0;
1351 assert(install != NULL);
1352 assert(stage2->file != NULL);
1354 /* New stage2 files come with an embedded stage2. */
1355 if (find_multiboot(stage2->file, stage2->file_size, &mboot_off)
1356 != BC_SUCCESS) {
1357 BOOT_DEBUG("WARNING: no multiboot structure found in stage2, "
1358 "are you using an old GRUB stage2?\n");
1359 if (do_version == B_TRUE) {
1360 (void) fprintf(stderr, gettext("Versioning requested "
1361 "but stage2 does not support it.. skipping.\n"));
1362 do_version = B_FALSE;
1364 } else {
1365 /* Keep track of where the multiboot header is. */
1366 stage2->mboot_off = mboot_off;
1367 stage2->mboot = (multiboot_header_t *)(stage2->file +
1368 mboot_off);
1369 if (do_version) {
1371 * Adding stage2 information needs to happen before
1372 * we modify the copy of stage2 we have in memory, so
1373 * that the hashing reflects the one of the file.
1374 * An error here is not fatal.
1376 add_stage2_einfo(stage2, updt_str);
1379 * Fill multiboot information. We add them even without
1380 * versioning to support as much as possible mirroring.
1382 prepare_fake_multiboot(stage2);
1385 if (is_bootpar(device->type)) {
1386 uint32_t blocklist[SECTOR_SIZE / sizeof (uint32_t)];
1387 uint32_t install_addr = STAGE2_MEMADDR + SECTOR_SIZE;
1388 int i = 0;
1389 uchar_t *pos;
1391 bzero(blocklist, sizeof (blocklist));
1392 if (read_stage2_blocklist(device->part_fd, blocklist) != 0) {
1393 (void) fprintf(stderr, gettext("Error reading pcfs "
1394 "stage2 blocklist\n"));
1395 return (BC_ERROR);
1398 pos = (uchar_t *)stage2->file + STAGE2_BLOCKLIST;
1399 stage2->first_sector = device->start_sector + blocklist[0];
1400 stage2->pcfs_first_sectors[0] = blocklist[0];
1401 BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);
1404 if (blocklist[1] > 1) {
1405 blocklist[0]++;
1406 blocklist[1]--;
1407 } else {
1408 i += 2;
1411 stage2->pcfs_first_sectors[1] = blocklist[i];
1413 while (blocklist[i]) {
1414 if (START_BLOCK(pos - 8) != 0 &&
1415 START_BLOCK(pos - 8) != blocklist[i + 2]) {
1416 (void) fprintf(stderr, PCFS_FRAGMENTED);
1417 return (BC_ERROR);
1419 START_BLOCK(pos) = blocklist[i] + device->start_sector;
1420 START_SEG(pos) = (ushort_t)(install_addr >> 4);
1421 NUM_BLOCK(pos) = blocklist[i + 1];
1422 install_addr += blocklist[i + 1] * SECTOR_SIZE;
1423 pos -= 8;
1424 i += 2;
1426 } else {
1427 /* Solaris VTOC & EFI */
1428 if (device->start_sector >
1429 UINT32_MAX - STAGE2_BLKOFF(device->type)) {
1430 fprintf(stderr, gettext("Error: partition start sector "
1431 "must be less than %lld\n"),
1432 (uint64_t)UINT32_MAX - STAGE2_BLKOFF(device->type));
1433 return (BC_ERROR);
1435 stage2->first_sector = device->start_sector +
1436 STAGE2_BLKOFF(device->type);
1437 BOOT_DEBUG("stage2 first sector: %d\n", stage2->first_sector);
1439 * In a solaris partition, stage2 is written to contiguous
1440 * blocks. So we update the starting block only.
1442 *((ulong_t *)(stage2->file + STAGE2_BLOCKLIST)) =
1443 stage2->first_sector + 1;
1446 /* force lba and set disk partition */
1447 *((unsigned char *) (stage2->file + STAGE2_FORCE_LBA)) = 1;
1448 *((long *)(stage2->file + STAGE2_INSTALLPART))
1449 = (device->partition << 16) | (device->slice << 8) | 0xff;
1451 return (BC_SUCCESS);
1454 static int
1455 find_x86_bootpar(struct mboot *mboot, int *part_num, uint32_t *start_sect)
1457 int i;
1459 for (i = 0; i < FD_NUMPART; i++) {
1460 struct ipart *part;
1462 part = (struct ipart *)mboot->parts + i;
1463 if (part->systid == 0xbe) {
1464 if (start_sect)
1465 *start_sect = part->relsect;
1466 if (part_num)
1467 *part_num = i;
1468 /* solaris boot part */
1469 return (BC_SUCCESS);
1472 return (BC_ERROR);
1475 static char *
1476 get_raw_partition_path(ig_device_t *device)
1478 char *raw;
1479 int len;
1481 if (is_bootpar(device->type)) {
1482 int part;
1483 struct mboot *mboot;
1485 mboot = (struct mboot *)device->boot_sector;
1486 if (find_x86_bootpar(mboot, &part, NULL) != BC_SUCCESS) {
1487 (void) fprintf(stderr, BOOTPAR_NOTFOUND,
1488 device->path_p0);
1489 return (NULL);
1492 raw = strdup(device->path_p0);
1493 if (raw == NULL) {
1494 perror(gettext("Memory allocation failed"));
1495 return (NULL);
1498 raw[strlen(raw) - 2] = '1' + part;
1499 return (raw);
1502 /* For disk, remember slice and return whole fdisk partition */
1503 raw = strdup(device->path);
1504 if (raw == NULL) {
1505 perror(gettext("Memory allocation failed"));
1506 return (NULL);
1509 len = strlen(raw);
1510 if (!is_efi(device->type) &&
1511 (raw[len - 2] != 's' || raw[len - 1] == '2')) {
1512 (void) fprintf(stderr, NOT_ROOT_SLICE);
1513 free(raw);
1514 return (NULL);
1516 device->slice = atoi(&raw[len - 1]);
1518 if (!is_efi(device->type)) {
1519 raw[len - 2] = 's';
1520 raw[len - 1] = '2';
1523 return (raw);
1526 static int
1527 get_raw_partition_fd(ig_device_t *device)
1529 struct stat stat = {0};
1530 char *raw;
1532 raw = get_raw_partition_path(device);
1533 if (raw == NULL)
1534 return (BC_ERROR);
1536 if (nowrite)
1537 device->part_fd = open(raw, O_RDONLY);
1538 else
1539 device->part_fd = open(raw, O_RDWR);
1541 if (device->part_fd < 0 || fstat(device->part_fd, &stat) != 0) {
1542 (void) fprintf(stderr, OPEN_FAIL, raw);
1543 free(raw);
1544 return (BC_ERROR);
1547 if (S_ISCHR(stat.st_mode) == 0) {
1548 (void) fprintf(stderr, NOT_RAW_DEVICE, raw);
1549 (void) close(device->part_fd);
1550 device->part_fd = -1;
1551 free(raw);
1552 return (BC_ERROR);
1555 free(raw);
1556 return (BC_SUCCESS);