disklabel.c revision 1.2.154.1 1 1.2.154.1 yamt /* $NetBSD: disklabel.c,v 1.2.154.1 2009/05/04 08:10:49 yamt 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.2 leo #include <ufs/ufs/dinode.h>
36 1.1 leo #include <ufs/ffs/fs.h>
37 1.1 leo #include <sys/disklabel.h>
38 1.1 leo #include <machine/ahdilabel.h>
39 1.1 leo #include <unistd.h>
40 1.1 leo #include <string.h>
41 1.1 leo #include <stdlib.h>
42 1.1 leo #include <fcntl.h>
43 1.1 leo #include <err.h>
44 1.1 leo
45 1.1 leo #if (BBSIZE < MINBBSIZE)
46 1.1 leo #error BBSIZE is smaller than MINBBSIZE
47 1.1 leo #endif
48 1.1 leo
49 1.1 leo struct ahdilabel {
50 1.1 leo u_int nsecs;
51 1.1 leo daddr_t bslst;
52 1.1 leo daddr_t bslend;
53 1.1 leo u_int nroots;
54 1.1 leo daddr_t *roots;
55 1.1 leo u_int nparts;
56 1.1 leo struct ahdi_part *parts;
57 1.1 leo };
58 1.1 leo
59 1.2.154.1 yamt u_int dkcksum(struct disklabel *);
60 1.2.154.1 yamt u_int32_t readdisklabel(char *, struct disklabel *);
61 1.1 leo
62 1.2.154.1 yamt static int bsd_label(int, off_t, struct disklabel *);
63 1.2.154.1 yamt static int ahdi_label(int, u_int32_t *, struct disklabel *);
64 1.2.154.1 yamt static int ahdi_getparts(int, daddr_t, daddr_t, struct ahdilabel *);
65 1.1 leo
66 1.1 leo u_int
67 1.2.154.1 yamt dkcksum (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.2.154.1 yamt u_int32_t
79 1.2.154.1 yamt readdisklabel (char *fn, struct disklabel *dl)
80 1.1 leo {
81 1.1 leo int fd, e;
82 1.2.154.1 yamt u_int32_t bbsec;
83 1.1 leo
84 1.1 leo memset(dl, 0, sizeof *dl);
85 1.1 leo
86 1.1 leo if ((fd = open(fn, O_RDONLY)) < 0)
87 1.1 leo err(EXIT_FAILURE, "%s", fn);
88 1.1 leo
89 1.1 leo /* Try NetBSD/Atari format first */
90 1.1 leo if ((e = bsd_label(fd, (off_t)0, dl)) < 0)
91 1.1 leo err(EXIT_FAILURE, "%s", fn);
92 1.1 leo if (!e)
93 1.1 leo return(0);
94 1.1 leo
95 1.1 leo /* Try unprotected AHDI format last */
96 1.1 leo if ((e = ahdi_label(fd, &bbsec, dl)) < 0)
97 1.1 leo err(EXIT_FAILURE, "%s", fn);
98 1.1 leo if (!e)
99 1.1 leo return(bbsec);
100 1.1 leo
101 1.1 leo warnx("%s: Unknown disk label format.", fn);
102 1.1 leo return(NO_BOOT_BLOCK);
103 1.1 leo }
104 1.1 leo
105 1.1 leo static int
106 1.2.154.1 yamt bsd_label (int fd, off_t offs, struct disklabel *label)
107 1.1 leo {
108 1.1 leo struct bootblock bb;
109 1.1 leo struct disklabel *p;
110 1.1 leo
111 1.1 leo if (lseek(fd, offs, SEEK_SET) != offs)
112 1.1 leo return(-1);
113 1.1 leo if (read(fd, &bb, sizeof(bb)) != sizeof(bb))
114 1.1 leo return(-1);
115 1.1 leo
116 1.1 leo p = (struct disklabel *)bb.bb_label;
117 1.1 leo if ( (offs == 0 && bb.bb_magic != NBDAMAGIC)
118 1.1 leo || (offs != 0 && bb.bb_magic != AHDIMAGIC)
119 1.1 leo || p->d_npartitions > MAXPARTITIONS
120 1.1 leo || p->d_magic2 != DISKMAGIC
121 1.1 leo || p->d_magic != DISKMAGIC
122 1.1 leo || dkcksum(p) != 0
123 1.1 leo ) {
124 1.1 leo return(1);
125 1.1 leo }
126 1.1 leo
127 1.1 leo *label = *p;
128 1.1 leo return(0);
129 1.1 leo }
130 1.1 leo
131 1.1 leo static int
132 1.2.154.1 yamt ahdi_label (int fd, u_int32_t *bbsec, struct disklabel *label)
133 1.1 leo {
134 1.1 leo struct ahdilabel al;
135 1.1 leo u_int i, j;
136 1.1 leo int e;
137 1.1 leo
138 1.1 leo memset(&al, 0, sizeof(al));
139 1.1 leo if ((e = ahdi_getparts(fd, AHDI_BBLOCK, AHDI_BBLOCK, &al)))
140 1.1 leo return(e);
141 1.1 leo
142 1.1 leo /*
143 1.1 leo * Perform sanity checks.
144 1.1 leo */
145 1.1 leo if (al.bslst == 0 || al.bslend == 0)
146 1.1 leo return(1);
147 1.1 leo if (al.nsecs == 0 || al.nparts == 0)
148 1.1 leo return(1);
149 1.1 leo if (al.nparts > AHDI_MAXPARTS)
150 1.1 leo warnx("Too many AHDI partitions (%u).", al.nparts);
151 1.1 leo for (i = 0; i < al.nparts; ++i) {
152 1.1 leo struct ahdi_part *p1 = &al.parts[i];
153 1.1 leo for (j = 0; j < al.nroots; ++j) {
154 1.1 leo daddr_t aux = al.roots[j];
155 1.1 leo if (aux >= p1->ap_st && aux <= p1->ap_end)
156 1.1 leo return(1);
157 1.1 leo }
158 1.1 leo for (j = i + 1; j < al.nparts; ++j) {
159 1.1 leo struct ahdi_part *p2 = &al.parts[j];
160 1.1 leo if (p1->ap_st >= p2->ap_st && p1->ap_st <= p2->ap_end)
161 1.1 leo return(1);
162 1.1 leo if (p2->ap_st >= p1->ap_st && p2->ap_st <= p1->ap_end)
163 1.1 leo return(1);
164 1.1 leo }
165 1.1 leo if (p1->ap_st >= al.bslst && p1->ap_st <= al.bslend)
166 1.1 leo return(1);
167 1.1 leo if (al.bslst >= p1->ap_st && al.bslst <= p1->ap_end)
168 1.1 leo return(1);
169 1.1 leo }
170 1.1 leo
171 1.1 leo /*
172 1.1 leo * Search for a NetBSD boot block
173 1.1 leo */
174 1.1 leo for (i = 0; i < al.nparts; ++i) {
175 1.1 leo struct ahdi_part *pd = &al.parts[i];
176 1.1 leo u_int id = *((u_int32_t *)&pd->ap_flg);
177 1.1 leo
178 1.1 leo if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
179 1.1 leo off_t offs = pd->ap_st * AHDI_BSIZE;
180 1.1 leo if ((e = bsd_label(fd, offs, label)) < 0)
181 1.1 leo return(e);
182 1.1 leo if (!e) {
183 1.1 leo *bbsec = pd->ap_st; /* got it */
184 1.1 leo return(0);
185 1.1 leo }
186 1.1 leo }
187 1.1 leo }
188 1.1 leo *bbsec = NO_BOOT_BLOCK; /* AHDI label, no NetBSD boot block */
189 1.1 leo return(0);
190 1.1 leo }
191 1.1 leo
192 1.1 leo static int
193 1.1 leo ahdi_getparts(fd, rsec, esec, alab)
194 1.1 leo int fd;
195 1.1 leo daddr_t rsec,
196 1.1 leo esec;
197 1.1 leo struct ahdilabel *alab;
198 1.1 leo {
199 1.1 leo struct ahdi_part *part, *end;
200 1.1 leo struct ahdi_root root;
201 1.1 leo off_t ro;
202 1.1 leo
203 1.1 leo ro = rsec * AHDI_BSIZE;
204 1.1 leo if (lseek(fd, ro, SEEK_SET) != ro) {
205 1.1 leo off_t mend = lseek(fd, 0, SEEK_END);
206 1.1 leo if (mend == -1 || mend > ro)
207 1.1 leo return(-1);
208 1.1 leo return(1);
209 1.1 leo }
210 1.1 leo if (read(fd, &root, sizeof(root)) != sizeof(root))
211 1.1 leo return(-1);
212 1.1 leo
213 1.1 leo if (rsec == AHDI_BBLOCK)
214 1.1 leo end = &root.ar_parts[AHDI_MAXRPD];
215 1.1 leo else end = &root.ar_parts[AHDI_MAXARPD];
216 1.1 leo for (part = root.ar_parts; part < end; ++part) {
217 1.1 leo u_int id = *((u_int32_t *)&part->ap_flg);
218 1.1 leo if (!(id & 0x01000000))
219 1.1 leo continue;
220 1.1 leo if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
221 1.1 leo int e;
222 1.1 leo daddr_t aux = part->ap_st + esec;
223 1.1 leo alab->roots = realloc(alab->roots,
224 1.1 leo (alab->nroots + 1) * sizeof(*alab->roots));
225 1.1 leo alab->roots[alab->nroots++] = aux;
226 1.1 leo e = ahdi_getparts(fd, aux,
227 1.1 leo esec == AHDI_BBLOCK ? aux : esec, alab);
228 1.1 leo if (e)
229 1.1 leo return(e);
230 1.1 leo } else {
231 1.1 leo struct ahdi_part *p;
232 1.1 leo alab->parts = realloc(alab->parts,
233 1.1 leo (alab->nparts + 1) * sizeof(*alab->parts));
234 1.1 leo p = &alab->parts[alab->nparts++];
235 1.1 leo *((u_int32_t *)&p->ap_flg) = id;
236 1.1 leo p->ap_st = part->ap_st + rsec;
237 1.1 leo p->ap_end = p->ap_st + part->ap_size - 1;
238 1.1 leo }
239 1.1 leo }
240 1.1 leo alab->nsecs = root.ar_hdsize;
241 1.1 leo alab->bslst = root.ar_bslst;
242 1.1 leo alab->bslend = root.ar_bslst + root.ar_bslsize - 1;
243 1.1 leo return(0);
244 1.1 leo }
245