mbrlabel.c revision 1.23 1 /* $NetBSD: mbrlabel.c,v 1.23 2003/10/08 04:25:44 lukem Exp $ */
2
3 /*
4 * Copyright (C) 1998 Wolfgang Solfrank.
5 * Copyright (C) 1998 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: mbrlabel.c,v 1.23 2003/10/08 04:25:44 lukem Exp $");
37 #endif /* not lint */
38
39 #include <stdio.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <util.h>
45
46 #include <sys/param.h>
47 #define FSTYPENAMES
48 #include <sys/disklabel.h>
49 #include <sys/bootblock.h>
50 #include <sys/ioctl.h>
51
52 #include "dkcksum.h"
53 #include "extern.h"
54
55 int main(int, char **);
56 void usage(void);
57 void getlabel(int);
58 void setlabel(int, int);
59 int getparts(int, u_int32_t, u_int32_t, int);
60 u_int16_t getshort(void *);
61 u_int32_t getlong(void *);
62
63 struct disklabel label;
64
65 void
66 getlabel(int sd)
67 {
68
69 if (ioctl(sd, DIOCGDINFO, &label) < 0) {
70 perror("get label");
71 exit(1);
72 }
73 /*
74 * Some ports seem to not set the number of partitions
75 * correctly, albeit they seem to set the raw partition ok!
76 */
77 if (label.d_npartitions <= getrawpartition())
78 label.d_npartitions = getrawpartition() + 1;
79 }
80
81 void
82 setlabel(int sd, int doraw)
83 {
84 int one = 1;
85
86 label.d_checksum = 0;
87 label.d_checksum = dkcksum(&label);
88 if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
89 perror("set label");
90 exit(1);
91 }
92 if (!doraw)
93 /* If we haven't written to the disk, don't discard on close */
94 ioctl(sd, DIOCKLABEL, &one);
95
96 }
97
98 u_int16_t
99 getshort(void *p)
100 {
101 unsigned char *cp = p;
102
103 return (cp[0] | (cp[1] << 8));
104 }
105
106 u_int32_t
107 getlong(void *p)
108 {
109 unsigned char *cp = p;
110
111 return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
112 }
113
114 int
115 getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
116 {
117 unsigned char buf[DEV_BSIZE];
118 struct mbr_partition parts[MBR_PART_COUNT];
119 struct partition npe;
120 off_t loff;
121 int i, j, unused, changed;
122
123 changed = 0;
124 loff = (off_t)off * DEV_BSIZE;
125
126 if (lseek(sd, loff, SEEK_SET) != loff) {
127 perror("seek label");
128 exit(1);
129 }
130 if (read(sd, buf, sizeof buf) != DEV_BSIZE) {
131 perror("read label");
132 exit(1);
133 }
134 if (getshort(buf + MBR_MAGIC_OFFSET) != MBR_MAGIC)
135 return (changed);
136 memcpy(parts, buf + MBR_PART_OFFSET, sizeof parts);
137
138 /* scan partition table */
139 for (i = 0; i < MBR_PART_COUNT; i++) {
140 if (parts[i].mbrp_type == 0 ||
141 /* extended partitions are handled below */
142 MBR_IS_EXTENDED(parts[i].mbrp_type))
143 continue;
144
145 memset((void *)&npe, 0, sizeof(npe));
146 npe.p_size = getlong(&parts[i].mbrp_size);
147 npe.p_offset = getlong(&parts[i].mbrp_start) + off;
148 npe.p_fstype = xlat_mbr_fstype(parts[i].mbrp_type);
149
150 /* find existing entry, or first free slot */
151 unused = -1; /* flag as no free slot */
152 if (verbose)
153 printf(
154 "Found %s partition; size %u (%u MB), offset %u\n",
155 fstypenames[npe.p_fstype],
156 npe.p_size, npe.p_size / 2048, npe.p_offset);
157 for (j = 0; j < label.d_npartitions; j++) {
158 struct partition *lpe;
159
160 if (j == RAW_PART)
161 continue;
162 lpe = &label.d_partitions[j];
163 if (lpe->p_size == npe.p_size &&
164 lpe->p_offset == npe.p_offset
165 #ifdef notyet
166 && (lpe->p_fstype == npe.p_fstype ||
167 lpe->p_fstype == FS_UNUSED) */
168 #endif
169 ) {
170 if (verbose)
171 printf(
172 " skipping existing %s partition at slot %c.\n",
173 fstypenames[lpe->p_fstype],
174 j + 'a');
175 unused = -2; /* flag as existing */
176 break;
177 }
178 if (unused == -1 && lpe->p_size == 0 &&
179 lpe->p_fstype == FS_UNUSED)
180 unused = j;
181 }
182 if (unused == -2)
183 continue; /* entry exists, skip... */
184 if (unused == -1) {
185 if (label.d_npartitions < MAXPARTITIONS) {
186 unused = label.d_npartitions;
187 label.d_npartitions++;
188 } else {
189 printf(
190 " WARNING: no slots free for %s partition.\n",
191 fstypenames[npe.p_fstype]);
192 continue;
193 }
194 }
195
196 if (verbose)
197 printf(" adding %s partition to slot %c.\n",
198 fstypenames[npe.p_fstype], unused + 'a');
199 switch (npe.p_fstype) {
200 case FS_BSDFFS:
201 case FS_APPLEUFS:
202 npe.p_size = 16384; /* XXX */
203 npe.p_fsize = 1024;
204 npe.p_frag = 8;
205 npe.p_cpg = 16;
206 break;
207 #ifdef __does_not_happen__
208 case FS_BSDLFS:
209 npe.p_size = 16384; /* XXX */
210 npe.p_fsize = 1024;
211 npe.p_frag = 8;
212 npe.p_sgs = XXX;
213 break;
214 #endif
215 }
216 changed++;
217 label.d_partitions[unused] = npe;
218 }
219
220 /* recursively scan extended partitions */
221 for (i = 0; i < MBR_PART_COUNT; i++) {
222 u_int32_t poff;
223
224 if (MBR_IS_EXTENDED(parts[i].mbrp_type)) {
225 poff = getlong(&parts[i].mbrp_start) + extoff;
226 changed += getparts(sd, poff,
227 extoff ? extoff : poff, verbose);
228 }
229 }
230 return (changed);
231 }
232
233 void
234 usage(void)
235 {
236 fprintf(stderr, "Usage: %s [-fqrw] rawdisk\n", getprogname());
237 exit(1);
238 }
239
240
241 int
242 main(int argc, char **argv)
243 {
244 int sd, ch, changed;
245 char name[MAXPATHLEN];
246 int force; /* force label update */
247 int raw; /* update on-disk label as well */
248 int verbose; /* verbose output */
249 int write_it; /* update in-core label if changed */
250
251 force = 0;
252 raw = 0;
253 verbose = 1;
254 write_it = 0;
255 while ((ch = getopt(argc, argv, "fqrw")) != -1) {
256 switch (ch) {
257 case 'f':
258 force = 1;
259 break;
260 case 'q':
261 verbose = 0;
262 break;
263 case 'r':
264 raw = 1;
265 break;
266 case 'w':
267 write_it = 1;
268 break;
269 default:
270 usage();
271 }
272 }
273 argc -= optind;
274 argv += optind;
275 if (argc != 1)
276 usage();
277
278 if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
279 (size_t)MAXPATHLEN, 0)) < 0) {
280 perror(argv[0]);
281 exit(1);
282 }
283 getlabel(sd);
284 changed = getparts(sd, MBR_BBSECTOR, 0, verbose);
285
286 if (verbose) {
287 putchar('\n');
288 showpartitions(stdout, &label, 0);
289 putchar('\n');
290 }
291 if (write_it) {
292 if (! changed && ! force)
293 printf("No change; not updating disk label.\n");
294 else {
295 if (verbose)
296 printf("Updating in-core %sdisk label.\n",
297 raw ? "and on-disk " : "");
298 setlabel(sd, raw);
299 }
300 } else {
301 printf("Not updating disk label.\n");
302 }
303 close(sd);
304 return (0);
305 }
306