10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7563SPrasad.Singamsetty@Sun.COM * Common Development and Distribution License (the "License").
6*7563SPrasad.Singamsetty@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7563SPrasad.Singamsetty@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <libgen.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #define SECTOR_SIZE 512
370Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE];
380Sstevel@tonic-gate static char new_mboot[SECTOR_SIZE];
390Sstevel@tonic-gate
400Sstevel@tonic-gate static void
usage(char * progname)410Sstevel@tonic-gate usage(char *progname)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate fprintf(stderr, "Usage: %s [ -d | -n | -o | -r ] <device> [<mboot>]\n",
440Sstevel@tonic-gate basename(progname));
450Sstevel@tonic-gate fprintf(stderr, "\t-n Set new Solaris partition magic 0xbf\n");
460Sstevel@tonic-gate fprintf(stderr, "\t-o Set old Solaris partition magic 0x82\n");
470Sstevel@tonic-gate fprintf(stderr, "\t-r Replace master boot program "
480Sstevel@tonic-gate "(/usr/lib/fs/ufs/mboot)\n");
490Sstevel@tonic-gate exit(-1);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
main(int argc,char * argv[])530Sstevel@tonic-gate main(int argc, char *argv[])
540Sstevel@tonic-gate {
550Sstevel@tonic-gate int c, fd, i, sol_part = -1;
560Sstevel@tonic-gate int setold = 0, setnew = 0, write_mboot = 0, list_hd = 0;
570Sstevel@tonic-gate char *device;
580Sstevel@tonic-gate struct mboot *mboot;
590Sstevel@tonic-gate char *mboot_file = "/usr/lib/fs/ufs/mboot";
600Sstevel@tonic-gate
610Sstevel@tonic-gate while ((c = getopt(argc, argv, "dnor")) != EOF) {
620Sstevel@tonic-gate switch (c) {
630Sstevel@tonic-gate case 'd':
640Sstevel@tonic-gate list_hd = 1;
650Sstevel@tonic-gate continue;
660Sstevel@tonic-gate case 'n':
670Sstevel@tonic-gate setnew = 1;
680Sstevel@tonic-gate continue;
690Sstevel@tonic-gate case 'o':
700Sstevel@tonic-gate setold = 1;
710Sstevel@tonic-gate continue;
720Sstevel@tonic-gate case 'r':
730Sstevel@tonic-gate write_mboot = 1;
740Sstevel@tonic-gate continue;
750Sstevel@tonic-gate default:
760Sstevel@tonic-gate usage(argv[0]);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* check arguments */
810Sstevel@tonic-gate if ((setnew && setold) || argc < optind + 1) {
820Sstevel@tonic-gate usage(argv[0]);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate if (write_mboot && argc > optind + 1) {
860Sstevel@tonic-gate mboot_file = strdup(argv[optind + 1]);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate if (!mboot_file) {
890Sstevel@tonic-gate usage(argv[0]);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate fd = open(mboot_file, O_RDONLY);
920Sstevel@tonic-gate if (fd == -1 || read(fd, new_mboot, SECTOR_SIZE) != SECTOR_SIZE) {
930Sstevel@tonic-gate fprintf(stderr, "cannot read file %s\n", mboot_file);
940Sstevel@tonic-gate if (fd == -1)
950Sstevel@tonic-gate perror("open");
960Sstevel@tonic-gate else
970Sstevel@tonic-gate perror("read");
980Sstevel@tonic-gate exit(-1);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate close(fd);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate device = strdup(argv[optind]);
1030Sstevel@tonic-gate if (!device) {
1040Sstevel@tonic-gate usage(argv[0]);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate fd = open(device, O_RDWR);
1070Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
1080Sstevel@tonic-gate fprintf(stderr, "cannot read MBR on %s\n", device);
1090Sstevel@tonic-gate if (fd == -1)
1100Sstevel@tonic-gate perror("open");
1110Sstevel@tonic-gate else
1120Sstevel@tonic-gate perror("read");
1130Sstevel@tonic-gate exit(-1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate mboot = (struct mboot *)boot_sect;
1170Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) {
1180Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i;
1190Sstevel@tonic-gate if (!list_hd)
1200Sstevel@tonic-gate if (part->bootid == 128)
1210Sstevel@tonic-gate printf("active ");
1220Sstevel@tonic-gate else
1230Sstevel@tonic-gate printf(" ");
1240Sstevel@tonic-gate if (setnew && part->systid == 0x82) {
1250Sstevel@tonic-gate part->systid = 0xbf;
1260Sstevel@tonic-gate sol_part = i;
1270Sstevel@tonic-gate } else if (setold && part->systid == 0xbf) {
1280Sstevel@tonic-gate part->systid = 0x82;
1290Sstevel@tonic-gate sol_part = i;
1300Sstevel@tonic-gate } else if (list_hd &&
1310Sstevel@tonic-gate (part->systid == 0x82 || part->systid == 0xbf)) {
1320Sstevel@tonic-gate sol_part = i;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate if (!list_hd)
135*7563SPrasad.Singamsetty@Sun.COM printf("%d (0x%2x): start_sect %u, size_sect %u\n",
1360Sstevel@tonic-gate i + 1, part->systid, part->relsect, part->numsect);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (list_hd) {
1400Sstevel@tonic-gate printf("(hd0,%d,a)\n", sol_part);
1410Sstevel@tonic-gate (void) close(fd);
1420Sstevel@tonic-gate return (0);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* write new mboot */
1460Sstevel@tonic-gate if (write_mboot || sol_part != -1) {
1470Sstevel@tonic-gate if (write_mboot) {
1480Sstevel@tonic-gate /* copy over the new boot program */
1490Sstevel@tonic-gate bcopy((void *)new_mboot, (void *)boot_sect, BOOTSZ);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if ((lseek(fd, 0, SEEK_SET) < 0) ||
1530Sstevel@tonic-gate (write(fd, (void *)boot_sect, SECTOR_SIZE) < 0)) {
1540Sstevel@tonic-gate perror("failed to update MBR");
1550Sstevel@tonic-gate exit(-1);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate if (sol_part != -1) {
1580Sstevel@tonic-gate printf("Changed solaris partition %d", sol_part + 1);
1590Sstevel@tonic-gate if (setnew)
1600Sstevel@tonic-gate printf("from 0x82 to 0xbf\n");
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate printf("from 0xbf to 0x82\n");
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate if (write_mboot) {
1650Sstevel@tonic-gate printf("Replaced mboot program with %s\n", mboot_file);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate (void) close(fd);
1700Sstevel@tonic-gate return (0);
1710Sstevel@tonic-gate }
172