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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/types.h>
34 #include <sys/dktp/fdisk.h>
36 #define SECTOR_SIZE 512
37 static char boot_sect
[SECTOR_SIZE
];
38 static char new_mboot
[SECTOR_SIZE
];
43 fprintf(stderr
, "Usage: %s [ -d | -n | -o | -r ] <device> [<mboot>]\n",
45 fprintf(stderr
, "\t-n Set new Solaris partition magic 0xbf\n");
46 fprintf(stderr
, "\t-o Set old Solaris partition magic 0x82\n");
47 fprintf(stderr
, "\t-r Replace master boot program "
48 "(/usr/lib/fs/ufs/mboot)\n");
53 main(int argc
, char *argv
[])
55 int c
, fd
, i
, sol_part
= -1;
56 int setold
= 0, setnew
= 0, write_mboot
= 0, list_hd
= 0;
59 char *mboot_file
= "/usr/lib/fs/ufs/mboot";
61 while ((c
= getopt(argc
, argv
, "dnor")) != EOF
) {
81 if ((setnew
&& setold
) || argc
< optind
+ 1) {
85 if (write_mboot
&& argc
> optind
+ 1) {
86 mboot_file
= strdup(argv
[optind
+ 1]);
91 fd
= open(mboot_file
, O_RDONLY
);
92 if (fd
== -1 || read(fd
, new_mboot
, SECTOR_SIZE
) != SECTOR_SIZE
) {
93 fprintf(stderr
, "cannot read file %s\n", mboot_file
);
102 device
= strdup(argv
[optind
]);
106 fd
= open(device
, O_RDWR
);
107 if (fd
== -1 || read(fd
, boot_sect
, SECTOR_SIZE
) != SECTOR_SIZE
) {
108 fprintf(stderr
, "cannot read MBR on %s\n", device
);
116 mboot
= (struct mboot
*)boot_sect
;
117 for (i
= 0; i
< FD_NUMPART
; i
++) {
118 struct ipart
*part
= (struct ipart
*)mboot
->parts
+ i
;
120 if (part
->bootid
== 128)
125 if (setnew
&& part
->systid
== 0x82) {
128 } else if (setold
&& part
->systid
== 0xbf) {
131 } else if (list_hd
&&
132 (part
->systid
== 0x82 || part
->systid
== 0xbf)) {
136 printf("%d (0x%2x): start_sect %u, size_sect %u\n",
137 i
+ 1, part
->systid
, part
->relsect
, part
->numsect
);
141 printf("(hd0,%d,a)\n", sol_part
);
146 /* write new mboot */
147 if (write_mboot
|| sol_part
!= -1) {
149 /* copy over the new boot program */
150 bcopy((void *)new_mboot
, (void *)boot_sect
, BOOTSZ
);
153 if ((lseek(fd
, 0, SEEK_SET
) < 0) ||
154 (write(fd
, (void *)boot_sect
, SECTOR_SIZE
) < 0)) {
155 perror("failed to update MBR");
158 if (sol_part
!= -1) {
159 printf("Changed solaris partition %d", sol_part
+ 1);
161 printf("from 0x82 to 0xbf\n");
163 printf("from 0xbf to 0x82\n");
166 printf("Replaced mboot program with %s\n", mboot_file
);