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