mbrlabel.c revision 1.8 1 1.8 matt /* $NetBSD: mbrlabel.c,v 1.8 2000/07/03 03:37:59 matt Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1998 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1998 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws
34 1.1 ws #include <sys/cdefs.h>
35 1.1 ws #ifndef lint
36 1.8 matt __RCSID("$NetBSD: mbrlabel.c,v 1.8 2000/07/03 03:37:59 matt Exp $");
37 1.1 ws #endif /* not lint */
38 1.1 ws
39 1.1 ws #include <stdio.h>
40 1.1 ws #include <fcntl.h>
41 1.8 matt #include <stdlib.h>
42 1.1 ws #include <string.h>
43 1.1 ws #include <unistd.h>
44 1.1 ws #include <util.h>
45 1.1 ws
46 1.1 ws #include <sys/param.h>
47 1.1 ws #include <sys/disklabel.h>
48 1.3 thorpej #include <sys/disklabel_mbr.h>
49 1.1 ws #include <sys/ioctl.h>
50 1.1 ws
51 1.1 ws #include "dkcksum.h"
52 1.1 ws
53 1.1 ws #define FIRSTPART 0
54 1.1 ws
55 1.1 ws int main __P((int, char **));
56 1.1 ws void usage __P((void));
57 1.1 ws void getlabel __P((int));
58 1.1 ws void setlabel __P((int));
59 1.6 ws int getparts __P((int, int, u_int32_t, u_int32_t));
60 1.1 ws int nbsdtype __P((int));
61 1.1 ws u_int32_t getlong __P((void *p));
62 1.1 ws
63 1.1 ws struct disklabel label;
64 1.1 ws
65 1.1 ws void
66 1.1 ws getlabel(sd)
67 1.1 ws int sd;
68 1.1 ws {
69 1.1 ws struct partition save;
70 1.1 ws
71 1.1 ws if (ioctl(sd, DIOCGDINFO, &label) < 0) {
72 1.1 ws perror("get label");
73 1.1 ws exit(1);
74 1.1 ws }
75 1.1 ws save = label.d_partitions[RAW_PART];
76 1.1 ws memset(label.d_partitions, 0, sizeof label.d_partitions);
77 1.1 ws label.d_partitions[RAW_PART] = save;
78 1.1 ws /*
79 1.1 ws * Some ports seem to not set the number of partitions
80 1.1 ws * correctly, albeit they seem to set the raw partiton ok!
81 1.1 ws */
82 1.1 ws if (label.d_npartitions <= RAW_PART)
83 1.1 ws label.d_npartitions = RAW_PART + 1;
84 1.1 ws }
85 1.1 ws
86 1.1 ws void
87 1.1 ws setlabel(sd)
88 1.1 ws int sd;
89 1.1 ws {
90 1.1 ws label.d_checksum = 0;
91 1.1 ws label.d_checksum = dkcksum(&label);
92 1.1 ws if (ioctl(sd, DIOCSDINFO, &label) < 0) {
93 1.1 ws perror("set label");
94 1.1 ws exit(1);
95 1.1 ws }
96 1.1 ws }
97 1.1 ws
98 1.1 ws static struct typetab {
99 1.1 ws int mbrtype;
100 1.1 ws int nbsdtype;
101 1.1 ws } typetable[] = {
102 1.3 thorpej { MBR_PTYPE_NETBSD, FS_BSDFFS },
103 1.3 thorpej { MBR_PTYPE_386BSD, FS_BSDFFS },
104 1.3 thorpej { MBR_PTYPE_FAT12, FS_MSDOS },
105 1.3 thorpej { MBR_PTYPE_FAT16S, FS_MSDOS },
106 1.3 thorpej { MBR_PTYPE_FAT16B, FS_MSDOS },
107 1.3 thorpej { MBR_PTYPE_FAT32, FS_MSDOS },
108 1.3 thorpej { MBR_PTYPE_FAT32L, FS_MSDOS },
109 1.3 thorpej { MBR_PTYPE_FAT16L, FS_MSDOS },
110 1.3 thorpej { MBR_PTYPE_LNXEXT2, FS_EX2FS },
111 1.1 ws { 0, 0 }
112 1.1 ws };
113 1.1 ws
114 1.1 ws int
115 1.1 ws nbsdtype(type)
116 1.1 ws int type;
117 1.1 ws {
118 1.1 ws struct typetab *tt;
119 1.1 ws
120 1.1 ws for (tt = typetable; tt->mbrtype; tt++)
121 1.1 ws if (tt->mbrtype == type)
122 1.1 ws return tt->nbsdtype;
123 1.1 ws return FS_OTHER;
124 1.1 ws }
125 1.1 ws
126 1.1 ws u_int32_t
127 1.1 ws getlong(p)
128 1.1 ws void *p;
129 1.1 ws {
130 1.1 ws unsigned char *cp = p;
131 1.1 ws
132 1.1 ws return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
133 1.1 ws }
134 1.1 ws
135 1.1 ws int
136 1.6 ws getparts(sd, np, off, eoff)
137 1.1 ws int sd;
138 1.1 ws int np;
139 1.1 ws u_int32_t off;
140 1.6 ws u_int32_t eoff;
141 1.1 ws {
142 1.1 ws unsigned char buf[DEV_BSIZE];
143 1.5 cgd struct mbr_partition parts[NMBRPART];
144 1.2 fair off_t loff = 0; /* XXX this nonsense shuts up GCC 2.7.2.2 */
145 1.5 cgd int i;
146 1.2 fair
147 1.2 fair loff = (off_t)off * DEV_BSIZE;
148 1.1 ws
149 1.1 ws if (lseek(sd, loff, SEEK_SET) != loff) {
150 1.1 ws perror("seek label");
151 1.1 ws exit(1);
152 1.1 ws }
153 1.1 ws if (read(sd, buf, DEV_BSIZE) != DEV_BSIZE) {
154 1.1 ws perror("read label");
155 1.1 ws exit(1);
156 1.1 ws }
157 1.1 ws if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa)
158 1.1 ws return np;
159 1.5 cgd memcpy(parts, buf + MBR_PARTOFF, sizeof parts);
160 1.5 cgd for (i = 0; i < NMBRPART; i++) {
161 1.5 cgd switch (parts[i].mbrp_typ) {
162 1.1 ws case 0:
163 1.1 ws /* Nothing to do */
164 1.1 ws break;
165 1.3 thorpej case MBR_PTYPE_EXT:
166 1.3 thorpej case MBR_PTYPE_EXT_LBA:
167 1.6 ws case MBR_PTYPE_EXT_LNX:
168 1.1 ws /* Will be handled below */
169 1.1 ws break;
170 1.1 ws default:
171 1.5 cgd label.d_partitions[np].p_size = getlong(&parts[i].mbrp_size);
172 1.5 cgd label.d_partitions[np].p_offset = getlong(&parts[i].mbrp_start) + off;
173 1.5 cgd label.d_partitions[np].p_fstype = nbsdtype(parts[i].mbrp_typ);
174 1.1 ws switch (label.d_partitions[np].p_fstype) {
175 1.1 ws case FS_BSDFFS:
176 1.1 ws label.d_partitions[np].p_size = 16384;
177 1.1 ws label.d_partitions[np].p_fsize = 1024;
178 1.1 ws label.d_partitions[np].p_frag = 8;
179 1.1 ws label.d_partitions[np].p_cpg = 16;
180 1.1 ws break;
181 1.1 ws #ifdef __does_not_happen__
182 1.1 ws case FS_BSDLFS:
183 1.1 ws label.d_partitions[np].p_size = 16384;
184 1.1 ws label.d_partitions[np].p_fsize = 1024;
185 1.1 ws label.d_partitions[np].p_frag = 8;
186 1.1 ws label.d_partitions[np].p_sgs = XXX;
187 1.1 ws break;
188 1.1 ws #endif
189 1.1 ws }
190 1.1 ws np++;
191 1.1 ws break;
192 1.1 ws }
193 1.1 ws if (np >MAXPARTITIONS)
194 1.1 ws return np;
195 1.1 ws if (np == RAW_PART)
196 1.1 ws np++;
197 1.1 ws }
198 1.5 cgd for (i = 0; i < NMBRPART; i++) {
199 1.6 ws u_int32_t poff;
200 1.6 ws
201 1.5 cgd switch (parts[i].mbrp_typ) {
202 1.3 thorpej case MBR_PTYPE_EXT:
203 1.3 thorpej case MBR_PTYPE_EXT_LBA:
204 1.6 ws case MBR_PTYPE_EXT_LNX:
205 1.6 ws poff = getlong(&parts[i].mbrp_start) + eoff;
206 1.6 ws np = getparts(sd, np, poff, eoff ? eoff : poff);
207 1.1 ws break;
208 1.1 ws default:
209 1.1 ws break;
210 1.1 ws }
211 1.1 ws if (np >MAXPARTITIONS)
212 1.1 ws return np;
213 1.1 ws }
214 1.1 ws return np;
215 1.1 ws }
216 1.1 ws
217 1.1 ws void
218 1.1 ws usage()
219 1.1 ws {
220 1.4 cgd fprintf(stderr, "usage: mbrlabel rawdisk\n");
221 1.1 ws exit(1);
222 1.1 ws }
223 1.1 ws
224 1.1 ws int
225 1.1 ws main(argc, argv)
226 1.1 ws int argc;
227 1.1 ws char **argv;
228 1.1 ws {
229 1.1 ws int sd;
230 1.1 ws int np;
231 1.1 ws char name[MAXPATHLEN];
232 1.1 ws
233 1.1 ws if (argc != 2)
234 1.1 ws usage();
235 1.1 ws
236 1.1 ws if ((sd = opendisk(*++argv, O_RDWR, name, MAXPATHLEN, 0)) < 0) {
237 1.1 ws perror(*argv);
238 1.1 ws exit(1);
239 1.1 ws }
240 1.1 ws getlabel(sd);
241 1.6 ws np = getparts(sd, FIRSTPART, MBR_BBSECTOR, 0);
242 1.1 ws if (np > label.d_npartitions)
243 1.1 ws label.d_npartitions = np;
244 1.1 ws setlabel(sd);
245 1.1 ws close(sd);
246 1.1 ws return 0;
247 1.1 ws }
248