xref: /titanic_52/usr/src/cmd/boot/mbr/mbr.c (revision 734b6a94890be549309b21156f8ed6d4561cac51)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <libgen.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <sys/dktp/fdisk.h>
38 
39 #define	SECTOR_SIZE	512
40 static char boot_sect[SECTOR_SIZE];
41 static char new_mboot[SECTOR_SIZE];
42 
43 static void
44 usage(char *progname)
45 {
46 	fprintf(stderr, "Usage: %s [ -d | -n | -o | -r ] <device> [<mboot>]\n",
47 	    basename(progname));
48 	fprintf(stderr, "\t-n Set new Solaris partition magic 0xbf\n");
49 	fprintf(stderr, "\t-o Set old Solaris partition magic 0x82\n");
50 	fprintf(stderr, "\t-r Replace master boot program "
51 	    "(/usr/lib/fs/ufs/mboot)\n");
52 	exit(-1);
53 }
54 
55 int
56 main(int argc, char *argv[])
57 {
58 	int c, fd, i, sol_part = -1;
59 	int setold = 0, setnew = 0, write_mboot = 0, list_hd = 0;
60 	char *device;
61 	struct mboot *mboot;
62 	char *mboot_file = "/usr/lib/fs/ufs/mboot";
63 
64 	while ((c = getopt(argc, argv, "dnor")) != EOF) {
65 		switch (c) {
66 		case 'd':
67 			list_hd = 1;
68 			continue;
69 		case 'n':
70 			setnew = 1;
71 			continue;
72 		case 'o':
73 			setold = 1;
74 			continue;
75 		case 'r':
76 			write_mboot = 1;
77 			continue;
78 		default:
79 			usage(argv[0]);
80 		}
81 	}
82 
83 	/* check arguments */
84 	if ((setnew && setold) || argc < optind + 1) {
85 		usage(argv[0]);
86 	}
87 
88 	if (write_mboot && argc > optind + 1) {
89 		mboot_file = strdup(argv[optind + 1]);
90 	}
91 	if (!mboot_file) {
92 		usage(argv[0]);
93 	}
94 	fd = open(mboot_file, O_RDONLY);
95 	if (fd == -1 || read(fd, new_mboot, SECTOR_SIZE) != SECTOR_SIZE) {
96 		fprintf(stderr, "cannot read file %s\n", mboot_file);
97 		if (fd == -1)
98 			perror("open");
99 		else
100 			perror("read");
101 		exit(-1);
102 	}
103 	close(fd);
104 
105 	device = strdup(argv[optind]);
106 	if (!device) {
107 		usage(argv[0]);
108 	}
109 	fd = open(device, O_RDWR);
110 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
111 		fprintf(stderr, "cannot read MBR on %s\n", device);
112 		if (fd == -1)
113 			perror("open");
114 		else
115 			perror("read");
116 		exit(-1);
117 	}
118 
119 	mboot = (struct mboot *)boot_sect;
120 	for (i = 0; i < FD_NUMPART; i++) {
121 		struct ipart *part = (struct ipart *)mboot->parts + i;
122 		if (!list_hd)
123 			if (part->bootid == 128)
124 				printf("active ");
125 			else
126 				printf("       ");
127 		if (setnew && part->systid == 0x82) {
128 			part->systid = 0xbf;
129 			sol_part = i;
130 		} else if (setold && part->systid == 0xbf) {
131 			part->systid = 0x82;
132 			sol_part = i;
133 		} else if (list_hd &&
134 		    (part->systid == 0x82 || part->systid == 0xbf)) {
135 			sol_part = i;
136 		}
137 		if (!list_hd)
138 			printf("%d (0x%2x): start_sect %d, size_sect %d\n",
139 			    i + 1, part->systid, part->relsect, part->numsect);
140 	}
141 
142 	if (list_hd) {
143 		printf("(hd0,%d,a)\n", sol_part);
144 		(void) close(fd);
145 		return (0);
146 	}
147 
148 	/* write new mboot */
149 	if (write_mboot || sol_part != -1) {
150 		if (write_mboot) {
151 			/* copy over the new boot program */
152 			bcopy((void *)new_mboot, (void *)boot_sect, BOOTSZ);
153 		}
154 
155 		if ((lseek(fd, 0, SEEK_SET) < 0) ||
156 		    (write(fd, (void *)boot_sect, SECTOR_SIZE) < 0)) {
157 			perror("failed to update MBR");
158 			exit(-1);
159 		}
160 		if (sol_part != -1) {
161 			printf("Changed solaris partition %d", sol_part + 1);
162 			if (setnew)
163 				printf("from 0x82 to 0xbf\n");
164 			else
165 				printf("from 0xbf to 0x82\n");
166 		}
167 		if (write_mboot) {
168 			printf("Replaced mboot program with %s\n", mboot_file);
169 		}
170 	}
171 
172 	(void) close(fd);
173 	return (0);
174 }
175