mbrlabel.c revision 1.15 1 /* $NetBSD: mbrlabel.c,v 1.15 2001/02/18 03:36:07 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.15 2001/02/18 03:36:07 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/disklabel_mbr.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 int nbsdtype(int);
61 u_int16_t getshort(void *);
62 u_int32_t getlong(void *);
63
64 struct disklabel label;
65 extern char *__progname;
66
67 void
68 getlabel(int sd)
69 {
70
71 if (ioctl(sd, DIOCGDINFO, &label) < 0) {
72 perror("get label");
73 exit(1);
74 }
75 /*
76 * Some ports seem to not set the number of partitions
77 * correctly, albeit they seem to set the raw partiton ok!
78 */
79 if (label.d_npartitions <= RAW_PART)
80 label.d_npartitions = RAW_PART + 1;
81 }
82
83 void
84 setlabel(int sd, int doraw)
85 {
86
87 label.d_checksum = 0;
88 label.d_checksum = dkcksum(&label);
89 if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
90 perror("set label");
91 exit(1);
92 }
93 }
94
95 static struct typetab {
96 int mbrtype;
97 int nbsdtype;
98 } typetable[] = {
99 { MBR_PTYPE_386BSD, FS_BSDFFS },
100 { MBR_PTYPE_FAT12, FS_MSDOS },
101 { MBR_PTYPE_FAT16B, FS_MSDOS },
102 { MBR_PTYPE_FAT16L, FS_MSDOS },
103 { MBR_PTYPE_FAT16S, FS_MSDOS },
104 { MBR_PTYPE_FAT32, FS_MSDOS },
105 { MBR_PTYPE_FAT32L, FS_MSDOS },
106 { MBR_PTYPE_LNXEXT2, FS_EX2FS },
107 { MBR_PTYPE_LNXSWAP, FS_SWAP },
108 { MBR_PTYPE_NETBSD, FS_BSDFFS },
109 { MBR_PTYPE_NTFS, FS_NTFS },
110 { 0, 0 }
111 };
112
113 int
114 nbsdtype(int type)
115 {
116 struct typetab *tt;
117
118 for (tt = typetable; tt->mbrtype; tt++)
119 if (tt->mbrtype == type)
120 return (tt->nbsdtype);
121 return (FS_OTHER);
122 }
123
124 u_int16_t
125 getshort(void *p)
126 {
127 unsigned char *cp = p;
128
129 return (cp[0] | (cp[1] << 8));
130 }
131
132 u_int32_t
133 getlong(void *p)
134 {
135 unsigned char *cp = p;
136
137 return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
138 }
139
140 int
141 getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
142 {
143 unsigned char buf[DEV_BSIZE];
144 struct mbr_partition parts[NMBRPART];
145 struct partition npe;
146 off_t loff;
147 int i, j, unused, changed;
148
149 changed = 0;
150 loff = (off_t)off * DEV_BSIZE;
151
152 if (lseek(sd, loff, SEEK_SET) != loff) {
153 perror("seek label");
154 exit(1);
155 }
156 if (read(sd, buf, DEV_BSIZE) != DEV_BSIZE) {
157 perror("read label");
158 exit(1);
159 }
160 if (getshort(buf + MBR_MAGICOFF) != MBR_MAGIC)
161 return (changed);
162 memcpy(parts, buf + MBR_PARTOFF, sizeof parts);
163
164 /* scan partition table */
165 for (i = 0; i < NMBRPART; i++) {
166 if (parts[i].mbrp_typ == 0 ||
167 /* extended partitions are handled below */
168 MBR_IS_EXTENDED(parts[i].mbrp_typ))
169 continue;
170
171 memset((void *)&npe, 0, sizeof(npe));
172 npe.p_size = getlong(&parts[i].mbrp_size);
173 npe.p_offset = getlong(&parts[i].mbrp_start) + off;
174 npe.p_fstype = nbsdtype(parts[i].mbrp_typ);
175
176 /* find existing entry, or first free slot */
177 unused = -1; /* flag as no free slot */
178 if (verbose)
179 printf(
180 "Found %s partition; size %u (%u MB), offset %u\n",
181 fstypenames[npe.p_fstype],
182 npe.p_size, npe.p_size / 2048, npe.p_offset);
183 for (j = 0; j < label.d_npartitions; j++) {
184 struct partition *lpe;
185
186 if (j == RAW_PART)
187 continue;
188 lpe = &label.d_partitions[j];
189 if (lpe->p_size == npe.p_size &&
190 lpe->p_offset == npe.p_offset
191 #ifdef notyet
192 && (lpe->p_fstype == npe.p_fstype ||
193 lpe->p_fstype == FS_UNUSED) */
194 #endif
195 ) {
196 if (verbose)
197 printf(
198 " skipping existing %s partition at slot %c.\n",
199 fstypenames[lpe->p_fstype],
200 j + 'a');
201 unused = -2; /* flag as existing */
202 break;
203 }
204 if (unused == -1 && lpe->p_size == 0 &&
205 lpe->p_fstype == FS_UNUSED)
206 unused = j;
207 }
208 if (unused == -2)
209 continue; /* entry exists, skip... */
210 if (unused == -1) {
211 if (label.d_npartitions < MAXPARTITIONS) {
212 unused = label.d_npartitions;
213 label.d_npartitions++;
214 } else {
215 printf(
216 " WARNING: no slots free for %s partition.\n",
217 fstypenames[npe.p_fstype]);
218 continue;
219 }
220 }
221
222 if (verbose)
223 printf(" adding %s partition to slot %c.\n",
224 fstypenames[npe.p_fstype], unused + 'a');
225 switch (npe.p_fstype) {
226 case FS_BSDFFS:
227 npe.p_size = 16384; /* XXX */
228 npe.p_fsize = 1024;
229 npe.p_frag = 8;
230 npe.p_cpg = 16;
231 break;
232 #ifdef __does_not_happen__
233 case FS_BSDLFS:
234 npe.p_size = 16384; /* XXX */
235 npe.p_fsize = 1024;
236 npe.p_frag = 8;
237 npe.p_sgs = XXX;
238 break;
239 #endif
240 }
241 changed++;
242 label.d_partitions[unused] = npe;
243 }
244
245 /* recursively scan extended partitions */
246 for (i = 0; i < NMBRPART; i++) {
247 u_int32_t poff;
248
249 if (MBR_IS_EXTENDED(parts[i].mbrp_typ)) {
250 poff = getlong(&parts[i].mbrp_start) + extoff;
251 changed += getparts(sd, poff,
252 extoff ? extoff : poff, verbose);
253 }
254 }
255 return (changed);
256 }
257
258 void
259 usage(void)
260 {
261 fprintf(stderr, "Usage: %s [-fqrw] rawdisk\n", __progname);
262 exit(1);
263 }
264
265
266 int
267 main(int argc, char **argv)
268 {
269 int sd, ch, changed;
270 char name[MAXPATHLEN];
271 int force; /* force label update */
272 int raw; /* update on-disk label as well */
273 int verbose; /* verbose output */
274 int write_it; /* update in-core label if changed */
275
276 force = 0;
277 raw = 0;
278 verbose = 1;
279 write_it = 0;
280 while ((ch = getopt(argc, argv, "fqrw")) != -1) {
281 switch (ch) {
282 case 'f':
283 force = 1;
284 break;
285 case 'q':
286 verbose = 0;
287 break;
288 case 'r':
289 raw = 1;
290 break;
291 case 'w':
292 write_it = 1;
293 break;
294 default:
295 usage();
296 }
297 }
298 argc -= optind;
299 argv += optind;
300 if (argc != 1)
301 usage();
302
303 if ((sd = opendisk(argv[0], O_RDWR, name, MAXPATHLEN, 0)) < 0) {
304 perror(argv[0]);
305 exit(1);
306 }
307 getlabel(sd);
308 changed = getparts(sd, MBR_BBSECTOR, 0, verbose);
309
310 if (verbose) {
311 putchar('\n');
312 showpartitions(stdout, &label, 0);
313 putchar('\n');
314 }
315 if (write_it) {
316 if (! changed && ! force)
317 printf("No change; not updating disk label.\n");
318 else {
319 if (verbose)
320 printf("Updating in-core %sdisk label.\n",
321 raw ? "and on-disk " : "");
322 setlabel(sd, raw);
323 }
324 } else {
325 printf("Not updating disk label.\n");
326 }
327 close(sd);
328 return (0);
329 }
330