disklabel.c revision 1.1.1.1 1 1.1 leo /* $NetBSD: disklabel.c,v 1.1.1.1 1996/02/29 11:35:46 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Waldi Ravens
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Waldi Ravens.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <sys/types.h>
34 1.1 leo #include <sys/param.h>
35 1.1 leo #include <ufs/ffs/fs.h>
36 1.1 leo #include <sys/disklabel.h>
37 1.1 leo #include <machine/ahdilabel.h>
38 1.1 leo #include <unistd.h>
39 1.1 leo #include <string.h>
40 1.1 leo #include <stdlib.h>
41 1.1 leo #include <fcntl.h>
42 1.1 leo #include <err.h>
43 1.1 leo
44 1.1 leo #if (BBSIZE < MINBBSIZE)
45 1.1 leo #error BBSIZE is smaller than MINBBSIZE
46 1.1 leo #endif
47 1.1 leo
48 1.1 leo struct ahdilabel {
49 1.1 leo u_int nsecs;
50 1.1 leo daddr_t bslst;
51 1.1 leo daddr_t bslend;
52 1.1 leo u_int nroots;
53 1.1 leo daddr_t *roots;
54 1.1 leo u_int nparts;
55 1.1 leo struct ahdi_part *parts;
56 1.1 leo };
57 1.1 leo
58 1.1 leo u_int dkcksum __P((struct disklabel *));
59 1.1 leo daddr_t readdisklabel __P((char *, struct disklabel *));
60 1.1 leo
61 1.1 leo static int bsd_label __P((int, off_t, struct disklabel *));
62 1.1 leo static int ahdi_label __P((int, daddr_t *, struct disklabel *));
63 1.1 leo static int ahdi_getparts __P((int, daddr_t, daddr_t, struct ahdilabel *));
64 1.1 leo
65 1.1 leo u_int
66 1.1 leo dkcksum (dl)
67 1.1 leo struct disklabel *dl;
68 1.1 leo {
69 1.1 leo u_int16_t sum = 0,
70 1.1 leo *st = (u_int16_t *)dl,
71 1.1 leo *end = (u_int16_t *)&dl->d_partitions[dl->d_npartitions];
72 1.1 leo
73 1.1 leo while (st < end)
74 1.1 leo sum ^= *st++;
75 1.1 leo return(sum);
76 1.1 leo }
77 1.1 leo
78 1.1 leo daddr_t
79 1.1 leo readdisklabel (fn, dl)
80 1.1 leo char *fn;
81 1.1 leo struct disklabel *dl;
82 1.1 leo {
83 1.1 leo int fd, e;
84 1.1 leo daddr_t bbsec;
85 1.1 leo
86 1.1 leo memset(dl, 0, sizeof *dl);
87 1.1 leo
88 1.1 leo if ((fd = open(fn, O_RDONLY)) < 0)
89 1.1 leo err(EXIT_FAILURE, "%s", fn);
90 1.1 leo
91 1.1 leo /* Try NetBSD/Atari format first */
92 1.1 leo if ((e = bsd_label(fd, (off_t)0, dl)) < 0)
93 1.1 leo err(EXIT_FAILURE, "%s", fn);
94 1.1 leo if (!e)
95 1.1 leo return(0);
96 1.1 leo
97 1.1 leo /* Try unprotected AHDI format last */
98 1.1 leo if ((e = ahdi_label(fd, &bbsec, dl)) < 0)
99 1.1 leo err(EXIT_FAILURE, "%s", fn);
100 1.1 leo if (!e)
101 1.1 leo return(bbsec);
102 1.1 leo
103 1.1 leo warnx("%s: Unknown disk label format.", fn);
104 1.1 leo return(NO_BOOT_BLOCK);
105 1.1 leo }
106 1.1 leo
107 1.1 leo static int
108 1.1 leo bsd_label (fd, offs, label)
109 1.1 leo int fd;
110 1.1 leo off_t offs;
111 1.1 leo struct disklabel *label;
112 1.1 leo {
113 1.1 leo struct bootblock bb;
114 1.1 leo struct disklabel *p;
115 1.1 leo
116 1.1 leo if (lseek(fd, offs, SEEK_SET) != offs)
117 1.1 leo return(-1);
118 1.1 leo if (read(fd, &bb, sizeof(bb)) != sizeof(bb))
119 1.1 leo return(-1);
120 1.1 leo
121 1.1 leo p = (struct disklabel *)bb.bb_label;
122 1.1 leo if ( (offs == 0 && bb.bb_magic != NBDAMAGIC)
123 1.1 leo || (offs != 0 && bb.bb_magic != AHDIMAGIC)
124 1.1 leo || p->d_npartitions > MAXPARTITIONS
125 1.1 leo || p->d_magic2 != DISKMAGIC
126 1.1 leo || p->d_magic != DISKMAGIC
127 1.1 leo || dkcksum(p) != 0
128 1.1 leo ) {
129 1.1 leo return(1);
130 1.1 leo }
131 1.1 leo
132 1.1 leo *label = *p;
133 1.1 leo return(0);
134 1.1 leo }
135 1.1 leo
136 1.1 leo static int
137 1.1 leo ahdi_label (fd, bbsec, label)
138 1.1 leo int fd;
139 1.1 leo daddr_t *bbsec;
140 1.1 leo struct disklabel *label;
141 1.1 leo {
142 1.1 leo struct ahdilabel al;
143 1.1 leo u_int i, j;
144 1.1 leo int e;
145 1.1 leo
146 1.1 leo memset(&al, 0, sizeof(al));
147 1.1 leo if ((e = ahdi_getparts(fd, AHDI_BBLOCK, AHDI_BBLOCK, &al)))
148 1.1 leo return(e);
149 1.1 leo
150 1.1 leo /*
151 1.1 leo * Perform sanity checks.
152 1.1 leo */
153 1.1 leo if (al.bslst == 0 || al.bslend == 0)
154 1.1 leo return(1);
155 1.1 leo if (al.nsecs == 0 || al.nparts == 0)
156 1.1 leo return(1);
157 1.1 leo if (al.nparts > AHDI_MAXPARTS)
158 1.1 leo warnx("Too many AHDI partitions (%u).", al.nparts);
159 1.1 leo for (i = 0; i < al.nparts; ++i) {
160 1.1 leo struct ahdi_part *p1 = &al.parts[i];
161 1.1 leo for (j = 0; j < al.nroots; ++j) {
162 1.1 leo daddr_t aux = al.roots[j];
163 1.1 leo if (aux >= p1->ap_st && aux <= p1->ap_end)
164 1.1 leo return(1);
165 1.1 leo }
166 1.1 leo for (j = i + 1; j < al.nparts; ++j) {
167 1.1 leo struct ahdi_part *p2 = &al.parts[j];
168 1.1 leo if (p1->ap_st >= p2->ap_st && p1->ap_st <= p2->ap_end)
169 1.1 leo return(1);
170 1.1 leo if (p2->ap_st >= p1->ap_st && p2->ap_st <= p1->ap_end)
171 1.1 leo return(1);
172 1.1 leo }
173 1.1 leo if (p1->ap_st >= al.bslst && p1->ap_st <= al.bslend)
174 1.1 leo return(1);
175 1.1 leo if (al.bslst >= p1->ap_st && al.bslst <= p1->ap_end)
176 1.1 leo return(1);
177 1.1 leo }
178 1.1 leo
179 1.1 leo /*
180 1.1 leo * Search for a NetBSD boot block
181 1.1 leo */
182 1.1 leo for (i = 0; i < al.nparts; ++i) {
183 1.1 leo struct ahdi_part *pd = &al.parts[i];
184 1.1 leo u_int id = *((u_int32_t *)&pd->ap_flg);
185 1.1 leo
186 1.1 leo if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
187 1.1 leo off_t offs = pd->ap_st * AHDI_BSIZE;
188 1.1 leo if ((e = bsd_label(fd, offs, label)) < 0)
189 1.1 leo return(e);
190 1.1 leo if (!e) {
191 1.1 leo *bbsec = pd->ap_st; /* got it */
192 1.1 leo return(0);
193 1.1 leo }
194 1.1 leo }
195 1.1 leo }
196 1.1 leo *bbsec = NO_BOOT_BLOCK; /* AHDI label, no NetBSD boot block */
197 1.1 leo return(0);
198 1.1 leo }
199 1.1 leo
200 1.1 leo static int
201 1.1 leo ahdi_getparts(fd, rsec, esec, alab)
202 1.1 leo int fd;
203 1.1 leo daddr_t rsec,
204 1.1 leo esec;
205 1.1 leo struct ahdilabel *alab;
206 1.1 leo {
207 1.1 leo struct ahdi_part *part, *end;
208 1.1 leo struct ahdi_root root;
209 1.1 leo off_t ro;
210 1.1 leo
211 1.1 leo ro = rsec * AHDI_BSIZE;
212 1.1 leo if (lseek(fd, ro, SEEK_SET) != ro) {
213 1.1 leo off_t mend = lseek(fd, 0, SEEK_END);
214 1.1 leo if (mend == -1 || mend > ro)
215 1.1 leo return(-1);
216 1.1 leo return(1);
217 1.1 leo }
218 1.1 leo if (read(fd, &root, sizeof(root)) != sizeof(root))
219 1.1 leo return(-1);
220 1.1 leo
221 1.1 leo if (rsec == AHDI_BBLOCK)
222 1.1 leo end = &root.ar_parts[AHDI_MAXRPD];
223 1.1 leo else end = &root.ar_parts[AHDI_MAXARPD];
224 1.1 leo for (part = root.ar_parts; part < end; ++part) {
225 1.1 leo u_int id = *((u_int32_t *)&part->ap_flg);
226 1.1 leo if (!(id & 0x01000000))
227 1.1 leo continue;
228 1.1 leo if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
229 1.1 leo int e;
230 1.1 leo daddr_t aux = part->ap_st + esec;
231 1.1 leo alab->roots = realloc(alab->roots,
232 1.1 leo (alab->nroots + 1) * sizeof(*alab->roots));
233 1.1 leo alab->roots[alab->nroots++] = aux;
234 1.1 leo e = ahdi_getparts(fd, aux,
235 1.1 leo esec == AHDI_BBLOCK ? aux : esec, alab);
236 1.1 leo if (e)
237 1.1 leo return(e);
238 1.1 leo } else {
239 1.1 leo struct ahdi_part *p;
240 1.1 leo alab->parts = realloc(alab->parts,
241 1.1 leo (alab->nparts + 1) * sizeof(*alab->parts));
242 1.1 leo p = &alab->parts[alab->nparts++];
243 1.1 leo *((u_int32_t *)&p->ap_flg) = id;
244 1.1 leo p->ap_st = part->ap_st + rsec;
245 1.1 leo p->ap_end = p->ap_st + part->ap_size - 1;
246 1.1 leo }
247 1.1 leo }
248 1.1 leo alab->nsecs = root.ar_hdsize;
249 1.1 leo alab->bslst = root.ar_bslst;
250 1.1 leo alab->bslend = root.ar_bslst + root.ar_bslsize - 1;
251 1.1 leo return(0);
252 1.1 leo }
253