disklabel.c revision 1.6.30.1 1 1.6.30.1 skrll /* $NetBSD: disklabel.c,v 1.6.30.1 2016/03/19 11:29:57 skrll 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.4 dsl u_int dkcksum(struct disklabel *);
60 1.6.30.1 skrll uint32_t readdisklabel(char *, struct disklabel *);
61 1.1 leo
62 1.4 dsl static int bsd_label(int, off_t, struct disklabel *);
63 1.6.30.1 skrll static int ahdi_label(int, uint32_t *, struct disklabel *);
64 1.4 dsl static int ahdi_getparts(int, daddr_t, daddr_t, struct ahdilabel *);
65 1.1 leo
66 1.1 leo u_int
67 1.5 dsl dkcksum (struct disklabel *dl)
68 1.1 leo {
69 1.6.30.1 skrll uint16_t sum = 0,
70 1.6.30.1 skrll *st = (uint16_t *)dl,
71 1.6.30.1 skrll *end = (uint16_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.6.30.1 skrll return sum;
76 1.1 leo }
77 1.1 leo
78 1.6.30.1 skrll uint32_t
79 1.6.30.1 skrll readdisklabel(char *fn, struct disklabel *dl)
80 1.1 leo {
81 1.1 leo int fd, e;
82 1.6.30.1 skrll uint32_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.6.30.1 skrll if (e == 0)
93 1.6.30.1 skrll 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.6.30.1 skrll if (e == 0)
99 1.6.30.1 skrll return bbsec;
100 1.1 leo
101 1.1 leo warnx("%s: Unknown disk label format.", fn);
102 1.6.30.1 skrll return NO_BOOT_BLOCK;
103 1.1 leo }
104 1.1 leo
105 1.1 leo static int
106 1.6.30.1 skrll 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.6.30.1 skrll return -1;
113 1.1 leo if (read(fd, &bb, sizeof(bb)) != sizeof(bb))
114 1.6.30.1 skrll return -1;
115 1.1 leo
116 1.1 leo p = (struct disklabel *)bb.bb_label;
117 1.6.30.1 skrll if ((offs == 0 && bb.bb_magic != NBDAMAGIC) ||
118 1.6.30.1 skrll (offs != 0 && bb.bb_magic != AHDIMAGIC) ||
119 1.6.30.1 skrll p->d_npartitions > MAXPARTITIONS ||
120 1.6.30.1 skrll p->d_magic2 != DISKMAGIC ||
121 1.6.30.1 skrll p->d_magic != DISKMAGIC ||
122 1.6.30.1 skrll dkcksum(p) != 0) {
123 1.6.30.1 skrll return 1;
124 1.1 leo }
125 1.1 leo
126 1.1 leo *label = *p;
127 1.6.30.1 skrll return 0;
128 1.1 leo }
129 1.1 leo
130 1.1 leo static int
131 1.6.30.1 skrll ahdi_label(int fd, uint32_t *bbsec, struct disklabel *label)
132 1.1 leo {
133 1.1 leo struct ahdilabel al;
134 1.1 leo u_int i, j;
135 1.1 leo int e;
136 1.1 leo
137 1.1 leo memset(&al, 0, sizeof(al));
138 1.6.30.1 skrll if ((e = ahdi_getparts(fd, AHDI_BBLOCK, AHDI_BBLOCK, &al)) != 0)
139 1.6.30.1 skrll return e;
140 1.1 leo
141 1.1 leo /*
142 1.1 leo * Perform sanity checks.
143 1.1 leo */
144 1.1 leo if (al.bslst == 0 || al.bslend == 0)
145 1.6.30.1 skrll return 1;
146 1.1 leo if (al.nsecs == 0 || al.nparts == 0)
147 1.6.30.1 skrll return 1;
148 1.1 leo if (al.nparts > AHDI_MAXPARTS)
149 1.1 leo warnx("Too many AHDI partitions (%u).", al.nparts);
150 1.1 leo for (i = 0; i < al.nparts; ++i) {
151 1.1 leo struct ahdi_part *p1 = &al.parts[i];
152 1.1 leo for (j = 0; j < al.nroots; ++j) {
153 1.1 leo daddr_t aux = al.roots[j];
154 1.1 leo if (aux >= p1->ap_st && aux <= p1->ap_end)
155 1.6.30.1 skrll return 1;
156 1.1 leo }
157 1.1 leo for (j = i + 1; j < al.nparts; ++j) {
158 1.1 leo struct ahdi_part *p2 = &al.parts[j];
159 1.1 leo if (p1->ap_st >= p2->ap_st && p1->ap_st <= p2->ap_end)
160 1.6.30.1 skrll return 1;
161 1.1 leo if (p2->ap_st >= p1->ap_st && p2->ap_st <= p1->ap_end)
162 1.6.30.1 skrll return 1;
163 1.1 leo }
164 1.1 leo if (p1->ap_st >= al.bslst && p1->ap_st <= al.bslend)
165 1.6.30.1 skrll return 1;
166 1.1 leo if (al.bslst >= p1->ap_st && al.bslst <= p1->ap_end)
167 1.6.30.1 skrll return 1;
168 1.1 leo }
169 1.1 leo
170 1.1 leo /*
171 1.1 leo * Search for a NetBSD boot block
172 1.1 leo */
173 1.1 leo for (i = 0; i < al.nparts; ++i) {
174 1.1 leo struct ahdi_part *pd = &al.parts[i];
175 1.6 chs u_int id;
176 1.1 leo
177 1.6 chs memcpy(&id, &pd->ap_flg, sizeof (id));
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.6.30.1 skrll return e;
182 1.6.30.1 skrll if (e == 0) {
183 1.1 leo *bbsec = pd->ap_st; /* got it */
184 1.6.30.1 skrll 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.6.30.1 skrll return 0;
190 1.1 leo }
191 1.1 leo
192 1.1 leo static int
193 1.6.30.1 skrll ahdi_getparts(int fd, daddr_t rsec, daddr_t esec, struct ahdilabel *alab)
194 1.1 leo {
195 1.1 leo struct ahdi_part *part, *end;
196 1.1 leo struct ahdi_root root;
197 1.1 leo off_t ro;
198 1.1 leo
199 1.1 leo ro = rsec * AHDI_BSIZE;
200 1.1 leo if (lseek(fd, ro, SEEK_SET) != ro) {
201 1.1 leo off_t mend = lseek(fd, 0, SEEK_END);
202 1.1 leo if (mend == -1 || mend > ro)
203 1.6.30.1 skrll return -1;
204 1.6.30.1 skrll return 1;
205 1.1 leo }
206 1.1 leo if (read(fd, &root, sizeof(root)) != sizeof(root))
207 1.6.30.1 skrll return -1;
208 1.1 leo
209 1.1 leo if (rsec == AHDI_BBLOCK)
210 1.1 leo end = &root.ar_parts[AHDI_MAXRPD];
211 1.6.30.1 skrll else
212 1.6.30.1 skrll end = &root.ar_parts[AHDI_MAXARPD];
213 1.1 leo for (part = root.ar_parts; part < end; ++part) {
214 1.6 chs u_int id;
215 1.6 chs
216 1.6 chs memcpy(&id, &part->ap_flg, sizeof (id));
217 1.6.30.1 skrll if ((id & 0x01000000) == 0)
218 1.1 leo continue;
219 1.1 leo if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
220 1.1 leo int e;
221 1.1 leo daddr_t aux = part->ap_st + esec;
222 1.1 leo alab->roots = realloc(alab->roots,
223 1.6.30.1 skrll (alab->nroots + 1) * sizeof(*alab->roots));
224 1.1 leo alab->roots[alab->nroots++] = aux;
225 1.1 leo e = ahdi_getparts(fd, aux,
226 1.6.30.1 skrll esec == AHDI_BBLOCK ? aux : esec, alab);
227 1.6.30.1 skrll if (e != 0)
228 1.6.30.1 skrll return e;
229 1.1 leo } else {
230 1.1 leo struct ahdi_part *p;
231 1.1 leo alab->parts = realloc(alab->parts,
232 1.6.30.1 skrll (alab->nparts + 1) * sizeof(*alab->parts));
233 1.1 leo p = &alab->parts[alab->nparts++];
234 1.6 chs memcpy(&p->ap_flg, &id, sizeof (id));
235 1.1 leo p->ap_st = part->ap_st + rsec;
236 1.1 leo p->ap_end = p->ap_st + part->ap_size - 1;
237 1.1 leo }
238 1.1 leo }
239 1.1 leo alab->nsecs = root.ar_hdsize;
240 1.1 leo alab->bslst = root.ar_bslst;
241 1.1 leo alab->bslend = root.ar_bslst + root.ar_bslsize - 1;
242 1.6.30.1 skrll return 0;
243 1.1 leo }
244