biosdisk.c revision 1.40 1 1.40 christos /* $NetBSD: biosdisk.c,v 1.40 2012/01/16 18:47:57 christos Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.8 drochner * Copyright (c) 1996, 1998
5 1.1 perry * Matthias Drochner. All rights reserved.
6 1.1 perry *
7 1.1 perry * Redistribution and use in source and binary forms, with or without
8 1.1 perry * modification, are permitted provided that the following conditions
9 1.1 perry * are met:
10 1.1 perry * 1. Redistributions of source code must retain the above copyright
11 1.1 perry * notice, this list of conditions and the following disclaimer.
12 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer in the
14 1.1 perry * documentation and/or other materials provided with the distribution.
15 1.1 perry *
16 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 perry *
27 1.1 perry */
28 1.1 perry
29 1.3 thorpej /*
30 1.4 drochner * raw BIOS disk device for libsa.
31 1.4 drochner * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
32 1.4 drochner * partly from netbsd:sys/arch/i386/boot/disk.c
33 1.4 drochner * no bad144 handling!
34 1.17 dsl *
35 1.17 dsl * A lot of this must match sys/kern/subr_disk_mbr.c
36 1.1 perry */
37 1.1 perry
38 1.1 perry /*
39 1.1 perry * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
40 1.1 perry *
41 1.1 perry * Mach Operating System
42 1.1 perry * Copyright (c) 1992, 1991 Carnegie Mellon University
43 1.1 perry * All Rights Reserved.
44 1.3 thorpej *
45 1.1 perry * Permission to use, copy, modify and distribute this software and its
46 1.1 perry * documentation is hereby granted, provided that both the copyright
47 1.1 perry * notice and this permission notice appear in all copies of the
48 1.1 perry * software, derivative works or modified versions, and any portions
49 1.1 perry * thereof, and that both notices appear in supporting documentation.
50 1.3 thorpej *
51 1.1 perry * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52 1.1 perry * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
53 1.1 perry * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 1.3 thorpej *
55 1.1 perry * Carnegie Mellon requests users of this software to return to
56 1.3 thorpej *
57 1.1 perry * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
58 1.1 perry * School of Computer Science
59 1.1 perry * Carnegie Mellon University
60 1.1 perry * Pittsburgh PA 15213-3890
61 1.3 thorpej *
62 1.1 perry * any improvements or extensions that they make and grant Carnegie Mellon
63 1.1 perry * the rights to redistribute these changes.
64 1.1 perry */
65 1.1 perry
66 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
67 1.29 jmcneill #define FSTYPENAMES
68 1.29 jmcneill #endif
69 1.29 jmcneill
70 1.36 jakllsch #include <lib/libkern/libkern.h>
71 1.36 jakllsch #include <lib/libsa/stand.h>
72 1.36 jakllsch
73 1.1 perry #include <sys/types.h>
74 1.21 thorpej #include <sys/md5.h>
75 1.24 junyoung #include <sys/param.h>
76 1.29 jmcneill #include <sys/disklabel.h>
77 1.36 jakllsch #include <sys/disklabel_gpt.h>
78 1.36 jakllsch #include <sys/uuid.h>
79 1.24 junyoung
80 1.24 junyoung #include <fs/cd9660/iso.h>
81 1.1 perry
82 1.1 perry #include <lib/libsa/saerrno.h>
83 1.29 jmcneill #include <machine/cpu.h>
84 1.1 perry
85 1.1 perry #include "libi386.h"
86 1.1 perry #include "biosdisk_ll.h"
87 1.5 drochner #include "biosdisk.h"
88 1.9 drochner #ifdef _STANDALONE
89 1.5 drochner #include "bootinfo.h"
90 1.9 drochner #endif
91 1.16 dsl
92 1.24 junyoung #define BUFSIZE 2048 /* must be large enough for a CD sector */
93 1.1 perry
94 1.36 jakllsch #define BIOSDISKNPART 26
95 1.36 jakllsch
96 1.3 thorpej struct biosdisk {
97 1.3 thorpej struct biosdisk_ll ll;
98 1.32 jakllsch daddr_t boff;
99 1.3 thorpej char buf[BUFSIZE];
100 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
101 1.36 jakllsch struct {
102 1.36 jakllsch daddr_t offset;
103 1.36 jakllsch daddr_t size;
104 1.36 jakllsch int fstype;
105 1.36 jakllsch } part[BIOSDISKNPART];
106 1.36 jakllsch #endif
107 1.1 perry };
108 1.1 perry
109 1.36 jakllsch #ifndef NO_GPT
110 1.36 jakllsch const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
111 1.36 jakllsch const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
112 1.36 jakllsch const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
113 1.36 jakllsch const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
114 1.36 jakllsch #endif /* NO_GPT */
115 1.36 jakllsch
116 1.9 drochner #ifdef _STANDALONE
117 1.5 drochner static struct btinfo_bootdisk bi_disk;
118 1.21 thorpej static struct btinfo_bootwedge bi_wedge;
119 1.9 drochner #endif
120 1.5 drochner
121 1.13 lukem #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
122 1.13 lukem
123 1.22 junyoung int
124 1.24 junyoung biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
125 1.24 junyoung void *buf, size_t *rsize)
126 1.1 perry {
127 1.3 thorpej struct biosdisk *d;
128 1.22 junyoung int blks, frag;
129 1.1 perry
130 1.3 thorpej if (flag != F_READ)
131 1.22 junyoung return EROFS;
132 1.1 perry
133 1.3 thorpej d = (struct biosdisk *) devdata;
134 1.1 perry
135 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_CD)
136 1.24 junyoung dblk = dblk * DEV_BSIZE / ISO_DEFAULT_BLOCK_SIZE;
137 1.24 junyoung
138 1.3 thorpej dblk += d->boff;
139 1.3 thorpej
140 1.24 junyoung blks = size / d->ll.secsize;
141 1.3 thorpej if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
142 1.3 thorpej if (rsize)
143 1.3 thorpej *rsize = 0;
144 1.22 junyoung return EIO;
145 1.3 thorpej }
146 1.22 junyoung
147 1.24 junyoung /* needed for CD */
148 1.24 junyoung frag = size % d->ll.secsize;
149 1.3 thorpej if (frag) {
150 1.3 thorpej if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
151 1.3 thorpej if (rsize)
152 1.24 junyoung *rsize = blks * d->ll.secsize;
153 1.22 junyoung return EIO;
154 1.3 thorpej }
155 1.24 junyoung memcpy(buf + blks * d->ll.secsize, d->buf, frag);
156 1.3 thorpej }
157 1.22 junyoung
158 1.3 thorpej if (rsize)
159 1.3 thorpej *rsize = size;
160 1.22 junyoung return 0;
161 1.1 perry }
162 1.1 perry
163 1.16 dsl static struct biosdisk *
164 1.23 junyoung alloc_biosdisk(int biosdev)
165 1.1 perry {
166 1.3 thorpej struct biosdisk *d;
167 1.1 perry
168 1.23 junyoung d = alloc(sizeof(*d));
169 1.22 junyoung if (d == NULL)
170 1.16 dsl return NULL;
171 1.23 junyoung memset(d, 0, sizeof(*d));
172 1.16 dsl
173 1.23 junyoung d->ll.dev = biosdev;
174 1.11 fvdl if (set_geometry(&d->ll, NULL)) {
175 1.1 perry #ifdef DISK_DEBUG
176 1.3 thorpej printf("no geometry information\n");
177 1.1 perry #endif
178 1.26 christos dealloc(d, sizeof(*d));
179 1.16 dsl return NULL;
180 1.3 thorpej }
181 1.16 dsl return d;
182 1.16 dsl }
183 1.16 dsl
184 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
185 1.36 jakllsch static void
186 1.36 jakllsch md5(void *hash, const void *data, size_t len)
187 1.36 jakllsch {
188 1.36 jakllsch MD5_CTX ctx;
189 1.36 jakllsch
190 1.36 jakllsch MD5Init(&ctx);
191 1.36 jakllsch MD5Update(&ctx, data, len);
192 1.36 jakllsch MD5Final(hash, &ctx);
193 1.36 jakllsch
194 1.36 jakllsch return;
195 1.36 jakllsch }
196 1.36 jakllsch #endif
197 1.36 jakllsch
198 1.36 jakllsch #ifndef NO_GPT
199 1.36 jakllsch static bool
200 1.36 jakllsch guid_is_nil(const struct uuid *u)
201 1.36 jakllsch {
202 1.36 jakllsch static const struct uuid nil = { .time_low = 0 };
203 1.36 jakllsch return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
204 1.36 jakllsch }
205 1.36 jakllsch
206 1.36 jakllsch static bool
207 1.36 jakllsch guid_is_equal(const struct uuid *a, const struct uuid *b)
208 1.36 jakllsch {
209 1.36 jakllsch return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
210 1.36 jakllsch }
211 1.36 jakllsch
212 1.36 jakllsch static int
213 1.36 jakllsch check_gpt(struct biosdisk *d, daddr_t sector)
214 1.36 jakllsch {
215 1.36 jakllsch struct gpt_hdr gpth;
216 1.36 jakllsch const struct gpt_ent *ep;
217 1.36 jakllsch const struct uuid *u;
218 1.36 jakllsch daddr_t entblk;
219 1.36 jakllsch size_t size;
220 1.36 jakllsch uint32_t crc;
221 1.36 jakllsch int sectors;
222 1.36 jakllsch int entries;
223 1.36 jakllsch int entry;
224 1.36 jakllsch int i, j;
225 1.36 jakllsch
226 1.36 jakllsch /* read in gpt_hdr sector */
227 1.36 jakllsch if (readsects(&d->ll, sector, 1, d->buf, 1)) {
228 1.36 jakllsch #ifdef DISK_DEBUG
229 1.36 jakllsch printf("Error reading GPT header at %"PRId64"\n", sector);
230 1.36 jakllsch #endif
231 1.36 jakllsch return EIO;
232 1.36 jakllsch }
233 1.36 jakllsch
234 1.36 jakllsch gpth = *(const struct gpt_hdr *)d->buf;
235 1.36 jakllsch
236 1.36 jakllsch if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
237 1.36 jakllsch return -1;
238 1.36 jakllsch
239 1.36 jakllsch crc = gpth.hdr_crc_self;
240 1.36 jakllsch gpth.hdr_crc_self = 0;
241 1.36 jakllsch gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
242 1.36 jakllsch if (gpth.hdr_crc_self != crc) {
243 1.36 jakllsch return -1;
244 1.36 jakllsch }
245 1.36 jakllsch
246 1.36 jakllsch if (gpth.hdr_lba_self != sector)
247 1.36 jakllsch return -1;
248 1.36 jakllsch
249 1.36 jakllsch #ifdef _STANDALONE
250 1.36 jakllsch bi_wedge.matchblk = sector;
251 1.36 jakllsch bi_wedge.matchnblks = 1;
252 1.36 jakllsch
253 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
254 1.36 jakllsch #endif
255 1.36 jakllsch
256 1.36 jakllsch sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
257 1.36 jakllsch entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
258 1.36 jakllsch entblk = gpth.hdr_lba_table;
259 1.36 jakllsch crc = crc32(0, NULL, 0);
260 1.36 jakllsch
261 1.36 jakllsch j = 0;
262 1.36 jakllsch ep = (const struct gpt_ent *)d->buf;
263 1.36 jakllsch
264 1.36 jakllsch for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
265 1.36 jakllsch size = MIN(sizeof(d->buf),
266 1.36 jakllsch (gpth.hdr_entries - entry) * gpth.hdr_entsz);
267 1.36 jakllsch entries = size / gpth.hdr_entsz;
268 1.36 jakllsch sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
269 1.36 jakllsch if (readsects(&d->ll, entblk, sectors, d->buf, 1))
270 1.36 jakllsch return -1;
271 1.36 jakllsch entblk += sectors;
272 1.36 jakllsch crc = crc32(crc, (const void *)d->buf, size);
273 1.36 jakllsch
274 1.36 jakllsch for (i = 0; j < BIOSDISKNPART && i < entries; i++, j++) {
275 1.36 jakllsch u = (const struct uuid *)ep[i].ent_type;
276 1.36 jakllsch if (!guid_is_nil(u)) {
277 1.36 jakllsch d->part[j].offset = ep[i].ent_lba_start;
278 1.36 jakllsch d->part[j].size = ep[i].ent_lba_end -
279 1.36 jakllsch ep[i].ent_lba_start + 1;
280 1.36 jakllsch if (guid_is_equal(u, &GET_nbsd_ffs))
281 1.36 jakllsch d->part[j].fstype = FS_BSDFFS;
282 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_lfs))
283 1.36 jakllsch d->part[j].fstype = FS_BSDLFS;
284 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_raid))
285 1.36 jakllsch d->part[j].fstype = FS_RAID;
286 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_swap))
287 1.36 jakllsch d->part[j].fstype = FS_SWAP;
288 1.36 jakllsch else
289 1.36 jakllsch d->part[j].fstype = FS_OTHER;
290 1.36 jakllsch }
291 1.36 jakllsch }
292 1.36 jakllsch
293 1.36 jakllsch }
294 1.36 jakllsch
295 1.36 jakllsch if (crc != gpth.hdr_crc_table) {
296 1.36 jakllsch #ifdef DISK_DEBUG
297 1.36 jakllsch printf("GPT table CRC invalid\n");
298 1.36 jakllsch #endif
299 1.36 jakllsch return -1;
300 1.36 jakllsch }
301 1.36 jakllsch
302 1.36 jakllsch return 0;
303 1.36 jakllsch }
304 1.36 jakllsch
305 1.36 jakllsch static int
306 1.36 jakllsch read_gpt(struct biosdisk *d)
307 1.36 jakllsch {
308 1.36 jakllsch struct biosdisk_extinfo ed;
309 1.36 jakllsch daddr_t gptsector[2];
310 1.36 jakllsch int i, error;
311 1.36 jakllsch
312 1.37 jakllsch if (d->ll.type != BIOSDISK_TYPE_HD)
313 1.37 jakllsch /* No GPT on floppy and CD */
314 1.37 jakllsch return -1;
315 1.37 jakllsch
316 1.36 jakllsch gptsector[0] = GPT_HDR_BLKNO;
317 1.36 jakllsch if (set_geometry(&d->ll, &ed) == 0 && d->ll.flags & BIOSDISK_INT13EXT) {
318 1.36 jakllsch gptsector[1] = ed.totsec - 1;
319 1.36 jakllsch d->ll.secsize = ed.sbytes;
320 1.36 jakllsch } else {
321 1.36 jakllsch #ifdef DISK_DEBUG
322 1.36 jakllsch printf("Unable to determine extended disk geometry - "
323 1.36 jakllsch "using CHS\n");
324 1.36 jakllsch #endif
325 1.36 jakllsch /* at least try some other reasonable values then */
326 1.36 jakllsch gptsector[1] = d->ll.chs_sectors - 1;
327 1.36 jakllsch }
328 1.36 jakllsch
329 1.36 jakllsch /*
330 1.36 jakllsch * Use any valid GPT available, do not require both GPTs to be valid
331 1.36 jakllsch */
332 1.36 jakllsch for (i = 0; i < __arraycount(gptsector); i++) {
333 1.36 jakllsch error = check_gpt(d, gptsector[i]);
334 1.36 jakllsch if (error == 0)
335 1.36 jakllsch break;
336 1.36 jakllsch }
337 1.36 jakllsch
338 1.36 jakllsch if (i >= __arraycount(gptsector)) {
339 1.36 jakllsch memset(d->part, 0, sizeof(d->part));
340 1.36 jakllsch return -1;
341 1.36 jakllsch }
342 1.36 jakllsch
343 1.36 jakllsch #ifdef DISK_DEBUG
344 1.36 jakllsch printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
345 1.36 jakllsch #endif
346 1.36 jakllsch return 0;
347 1.36 jakllsch }
348 1.36 jakllsch #endif /* !NO_GPT */
349 1.36 jakllsch
350 1.16 dsl #ifndef NO_DISKLABEL
351 1.37 jakllsch static void
352 1.37 jakllsch ingest_label(struct biosdisk *d, struct disklabel *lp)
353 1.37 jakllsch {
354 1.37 jakllsch int part;
355 1.37 jakllsch
356 1.37 jakllsch memset(d->part, 0, sizeof(d->part));
357 1.37 jakllsch
358 1.37 jakllsch for (part = 0; part < lp->d_npartitions; part++) {
359 1.37 jakllsch if (lp->d_partitions[part].p_size == 0)
360 1.37 jakllsch continue;
361 1.37 jakllsch if (lp->d_partitions[part].p_fstype == FS_UNUSED)
362 1.37 jakllsch continue;
363 1.37 jakllsch d->part[part].fstype = lp->d_partitions[part].p_fstype;
364 1.37 jakllsch d->part[part].offset = lp->d_partitions[part].p_offset;
365 1.37 jakllsch d->part[part].size = lp->d_partitions[part].p_size;
366 1.37 jakllsch }
367 1.37 jakllsch }
368 1.37 jakllsch
369 1.16 dsl static int
370 1.32 jakllsch check_label(struct biosdisk *d, daddr_t sector)
371 1.17 dsl {
372 1.17 dsl struct disklabel *lp;
373 1.17 dsl
374 1.17 dsl /* find partition in NetBSD disklabel */
375 1.17 dsl if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
376 1.17 dsl #ifdef DISK_DEBUG
377 1.17 dsl printf("Error reading disklabel\n");
378 1.17 dsl #endif
379 1.17 dsl return EIO;
380 1.17 dsl }
381 1.17 dsl lp = (struct disklabel *) (d->buf + LABELOFFSET);
382 1.17 dsl if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
383 1.17 dsl #ifdef DISK_DEBUG
384 1.33 jakllsch printf("warning: no disklabel in sector %"PRId64"\n", sector);
385 1.17 dsl #endif
386 1.17 dsl return -1;
387 1.17 dsl }
388 1.17 dsl
389 1.37 jakllsch ingest_label(d, lp);
390 1.36 jakllsch
391 1.36 jakllsch #ifdef _STANDALONE
392 1.39 gsutre bi_disk.labelsector = sector + LABELSECTOR;
393 1.36 jakllsch bi_disk.label.type = lp->d_type;
394 1.36 jakllsch memcpy(bi_disk.label.packname, lp->d_packname, 16);
395 1.36 jakllsch bi_disk.label.checksum = lp->d_checksum;
396 1.36 jakllsch
397 1.39 gsutre bi_wedge.matchblk = sector + LABELSECTOR;
398 1.36 jakllsch bi_wedge.matchnblks = 1;
399 1.36 jakllsch
400 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
401 1.36 jakllsch #endif
402 1.36 jakllsch
403 1.17 dsl return 0;
404 1.17 dsl }
405 1.17 dsl
406 1.17 dsl static int
407 1.40 christos read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
408 1.40 christos int this_ext, daddr_t sector)
409 1.40 christos {
410 1.40 christos struct mbr_partition mbr[MBR_PART_COUNT];
411 1.40 christos int i;
412 1.40 christos int typ;
413 1.40 christos struct partition *p;
414 1.40 christos
415 1.40 christos if (readsects(&d->ll, sector, 1, d->buf, 0)) {
416 1.40 christos #ifdef DISK_DEBUG
417 1.40 christos printf("Error reading MFS sector %d\n", sector);
418 1.40 christos #endif
419 1.40 christos return EIO;
420 1.40 christos }
421 1.40 christos if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
422 1.40 christos return -1;
423 1.40 christos }
424 1.40 christos memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts, sizeof(mbr));
425 1.40 christos for (i = 0; i < MBR_PART_COUNT; i++) {
426 1.40 christos typ = mbr[i].mbrp_type;
427 1.40 christos if (typ == 0)
428 1.40 christos continue;
429 1.40 christos sector = this_ext + mbr[i].mbrp_start;
430 1.40 christos if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
431 1.40 christos continue;
432 1.40 christos p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
433 1.40 christos p->p_offset = sector;
434 1.40 christos p->p_size = mbr[i].mbrp_size;
435 1.40 christos p->p_fstype = xlat_mbr_fstype(typ);
436 1.40 christos }
437 1.40 christos return 0;
438 1.40 christos }
439 1.40 christos
440 1.40 christos static int
441 1.16 dsl read_label(struct biosdisk *d)
442 1.16 dsl {
443 1.17 dsl struct disklabel dflt_lbl;
444 1.18 lukem struct mbr_partition mbr[MBR_PART_COUNT];
445 1.17 dsl struct partition *p;
446 1.16 dsl int sector, i;
447 1.17 dsl int error;
448 1.17 dsl int typ;
449 1.17 dsl int ext_base, this_ext, next_ext;
450 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
451 1.17 dsl int sector_386bsd = -1;
452 1.17 dsl #endif
453 1.17 dsl
454 1.23 junyoung memset(&dflt_lbl, 0, sizeof(dflt_lbl));
455 1.17 dsl dflt_lbl.d_npartitions = 8;
456 1.5 drochner
457 1.7 drochner d->boff = 0;
458 1.7 drochner
459 1.24 junyoung if (d->ll.type != BIOSDISK_TYPE_HD)
460 1.24 junyoung /* No label on floppy and CD */
461 1.16 dsl return -1;
462 1.7 drochner
463 1.3 thorpej /*
464 1.7 drochner * find NetBSD Partition in DOS partition table
465 1.7 drochner * XXX check magic???
466 1.3 thorpej */
467 1.17 dsl ext_base = 0;
468 1.17 dsl next_ext = 0;
469 1.17 dsl for (;;) {
470 1.17 dsl this_ext = ext_base + next_ext;
471 1.17 dsl next_ext = 0;
472 1.17 dsl if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
473 1.1 perry #ifdef DISK_DEBUG
474 1.17 dsl printf("error reading MBR sector %d\n", this_ext);
475 1.17 dsl #endif
476 1.17 dsl return EIO;
477 1.17 dsl }
478 1.23 junyoung memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts,
479 1.23 junyoung sizeof(mbr));
480 1.17 dsl /* Look for NetBSD partition ID */
481 1.18 lukem for (i = 0; i < MBR_PART_COUNT; i++) {
482 1.18 lukem typ = mbr[i].mbrp_type;
483 1.17 dsl if (typ == 0)
484 1.17 dsl continue;
485 1.17 dsl sector = this_ext + mbr[i].mbrp_start;
486 1.27 dsl #ifdef DISK_DEBUG
487 1.34 jakllsch printf("ptn type %d in sector %d\n", typ, sector);
488 1.27 dsl #endif
489 1.40 christos if (typ == MBR_PTYPE_MINIX_14B) {
490 1.40 christos if (!read_minix_subp(d, &dflt_lbl,
491 1.40 christos this_ext, sector)) {
492 1.40 christos /* Don't add "container" partition */
493 1.40 christos continue;
494 1.40 christos }
495 1.40 christos }
496 1.17 dsl if (typ == MBR_PTYPE_NETBSD) {
497 1.17 dsl error = check_label(d, sector);
498 1.17 dsl if (error >= 0)
499 1.17 dsl return error;
500 1.17 dsl }
501 1.17 dsl if (MBR_IS_EXTENDED(typ)) {
502 1.17 dsl next_ext = mbr[i].mbrp_start;
503 1.17 dsl continue;
504 1.17 dsl }
505 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
506 1.17 dsl if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
507 1.17 dsl sector_386bsd = sector;
508 1.1 perry #endif
509 1.17 dsl if (this_ext != 0) {
510 1.17 dsl if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
511 1.17 dsl continue;
512 1.17 dsl p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
513 1.17 dsl } else
514 1.17 dsl p = &dflt_lbl.d_partitions[i];
515 1.17 dsl p->p_offset = sector;
516 1.17 dsl p->p_size = mbr[i].mbrp_size;
517 1.17 dsl p->p_fstype = xlat_mbr_fstype(typ);
518 1.17 dsl }
519 1.17 dsl if (next_ext == 0)
520 1.3 thorpej break;
521 1.17 dsl if (ext_base == 0) {
522 1.17 dsl ext_base = next_ext;
523 1.17 dsl next_ext = 0;
524 1.3 thorpej }
525 1.17 dsl }
526 1.17 dsl
527 1.17 dsl sector = 0;
528 1.8 drochner #ifdef COMPAT_386BSD_MBRPART
529 1.17 dsl if (sector_386bsd != -1) {
530 1.17 dsl printf("old BSD partition ID!\n");
531 1.17 dsl sector = sector_386bsd;
532 1.8 drochner }
533 1.8 drochner #endif
534 1.7 drochner
535 1.17 dsl /*
536 1.17 dsl * One of two things:
537 1.17 dsl * 1. no MBR
538 1.17 dsl * 2. no NetBSD partition in MBR
539 1.17 dsl *
540 1.17 dsl * We simply default to "start of disk" in this case and
541 1.17 dsl * press on.
542 1.17 dsl */
543 1.17 dsl error = check_label(d, sector);
544 1.17 dsl if (error >= 0)
545 1.17 dsl return error;
546 1.16 dsl
547 1.17 dsl /*
548 1.17 dsl * Nothing at start of disk, return info from mbr partitions.
549 1.17 dsl */
550 1.17 dsl /* XXX fill it to make checksum match kernel one */
551 1.17 dsl dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
552 1.37 jakllsch ingest_label(d, &dflt_lbl);
553 1.27 dsl return 0;
554 1.16 dsl }
555 1.16 dsl #endif /* NO_DISKLABEL */
556 1.16 dsl
557 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
558 1.36 jakllsch static int
559 1.36 jakllsch read_partitions(struct biosdisk *d)
560 1.36 jakllsch {
561 1.36 jakllsch int error;
562 1.36 jakllsch
563 1.36 jakllsch error = -1;
564 1.36 jakllsch
565 1.36 jakllsch #ifndef NO_GPT
566 1.36 jakllsch error = read_gpt(d);
567 1.36 jakllsch if (error == 0)
568 1.36 jakllsch return 0;
569 1.36 jakllsch
570 1.36 jakllsch #endif
571 1.36 jakllsch #ifndef NO_DISKLABEL
572 1.36 jakllsch error = read_label(d);
573 1.36 jakllsch
574 1.36 jakllsch #endif
575 1.36 jakllsch return error;
576 1.36 jakllsch }
577 1.36 jakllsch #endif
578 1.36 jakllsch
579 1.29 jmcneill void
580 1.29 jmcneill biosdisk_probe(void)
581 1.29 jmcneill {
582 1.29 jmcneill struct biosdisk d;
583 1.29 jmcneill struct biosdisk_extinfo ed;
584 1.29 jmcneill uint64_t size;
585 1.36 jakllsch int first;
586 1.29 jmcneill int i;
587 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
588 1.36 jakllsch int part;
589 1.36 jakllsch #endif
590 1.29 jmcneill
591 1.29 jmcneill for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
592 1.29 jmcneill first = 1;
593 1.29 jmcneill memset(&d, 0, sizeof(d));
594 1.29 jmcneill memset(&ed, 0, sizeof(ed));
595 1.29 jmcneill if (i >= MAX_BIOSDISKS)
596 1.29 jmcneill d.ll.dev = 0x00 + i - MAX_BIOSDISKS; /* fd */
597 1.29 jmcneill else
598 1.29 jmcneill d.ll.dev = 0x80 + i; /* hd/cd */
599 1.29 jmcneill if (set_geometry(&d.ll, &ed))
600 1.29 jmcneill continue;
601 1.30 jmcneill printf("disk ");
602 1.29 jmcneill switch (d.ll.type) {
603 1.29 jmcneill case BIOSDISK_TYPE_CD:
604 1.30 jmcneill printf("cd0\n cd0a\n");
605 1.29 jmcneill break;
606 1.29 jmcneill case BIOSDISK_TYPE_FD:
607 1.30 jmcneill printf("fd%d\n", d.ll.dev & 0x7f);
608 1.30 jmcneill printf(" fd%da\n", d.ll.dev & 0x7f);
609 1.29 jmcneill break;
610 1.29 jmcneill case BIOSDISK_TYPE_HD:
611 1.30 jmcneill printf("hd%d", d.ll.dev & 0x7f);
612 1.29 jmcneill if (d.ll.flags & BIOSDISK_INT13EXT) {
613 1.29 jmcneill printf(" size ");
614 1.29 jmcneill size = ed.totsec * ed.sbytes;
615 1.29 jmcneill if (size >= (10ULL * 1024 * 1024 * 1024))
616 1.33 jakllsch printf("%"PRIu64" GB",
617 1.29 jmcneill size / (1024 * 1024 * 1024));
618 1.29 jmcneill else
619 1.33 jakllsch printf("%"PRIu64" MB",
620 1.29 jmcneill size / (1024 * 1024));
621 1.29 jmcneill }
622 1.29 jmcneill printf("\n");
623 1.29 jmcneill break;
624 1.29 jmcneill }
625 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
626 1.30 jmcneill if (d.ll.type != BIOSDISK_TYPE_HD)
627 1.30 jmcneill continue;
628 1.36 jakllsch
629 1.36 jakllsch if (read_partitions(&d) != 0)
630 1.36 jakllsch continue;
631 1.36 jakllsch
632 1.36 jakllsch for (part = 0; part < BIOSDISKNPART; part++) {
633 1.36 jakllsch if (d.part[part].size == 0)
634 1.29 jmcneill continue;
635 1.36 jakllsch if (d.part[part].fstype == FS_UNUSED)
636 1.29 jmcneill continue;
637 1.29 jmcneill if (first) {
638 1.29 jmcneill printf(" ");
639 1.29 jmcneill first = 0;
640 1.29 jmcneill }
641 1.29 jmcneill printf(" hd%d%c(", d.ll.dev & 0x7f, part + 'a');
642 1.36 jakllsch if (d.part[part].fstype < FSMAXTYPES)
643 1.29 jmcneill printf("%s",
644 1.36 jakllsch fstypenames[d.part[part].fstype]);
645 1.29 jmcneill else
646 1.36 jakllsch printf("%d", d.part[part].fstype);
647 1.29 jmcneill printf(")");
648 1.29 jmcneill }
649 1.36 jakllsch #endif
650 1.29 jmcneill if (first == 0)
651 1.29 jmcneill printf("\n");
652 1.29 jmcneill }
653 1.29 jmcneill }
654 1.29 jmcneill
655 1.16 dsl /* Determine likely partition for possible sector number of dos
656 1.17 dsl * partition.
657 1.17 dsl */
658 1.16 dsl
659 1.24 junyoung int
660 1.32 jakllsch biosdisk_findpartition(int biosdev, daddr_t sector)
661 1.16 dsl {
662 1.36 jakllsch #if defined(NO_DISKLABEL) && defined(NO_GPT)
663 1.16 dsl return 0;
664 1.16 dsl #else
665 1.16 dsl struct biosdisk *d;
666 1.24 junyoung int partition = 0;
667 1.27 dsl #ifdef DISK_DEBUG
668 1.33 jakllsch printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
669 1.27 dsl #endif
670 1.16 dsl
671 1.16 dsl /* Look for netbsd partition that is the dos boot one */
672 1.16 dsl d = alloc_biosdisk(biosdev);
673 1.17 dsl if (d == NULL)
674 1.17 dsl return 0;
675 1.17 dsl
676 1.36 jakllsch if (read_partitions(d) == 0) {
677 1.36 jakllsch for (partition = (BIOSDISKNPART-1); --partition;) {
678 1.36 jakllsch if (d->part[partition].fstype == FS_UNUSED)
679 1.16 dsl continue;
680 1.36 jakllsch if (d->part[partition].offset == sector)
681 1.16 dsl break;
682 1.16 dsl }
683 1.16 dsl }
684 1.16 dsl
685 1.26 christos dealloc(d, sizeof(*d));
686 1.16 dsl return partition;
687 1.36 jakllsch #endif /* NO_DISKLABEL && NO_GPT */
688 1.36 jakllsch }
689 1.36 jakllsch
690 1.36 jakllsch #ifdef _STANDALONE
691 1.36 jakllsch static void
692 1.36 jakllsch add_biosdisk_bootinfo(void)
693 1.36 jakllsch {
694 1.36 jakllsch static bool done;
695 1.36 jakllsch
696 1.36 jakllsch if (bootinfo == NULL) {
697 1.36 jakllsch done = false;
698 1.36 jakllsch return;
699 1.36 jakllsch }
700 1.36 jakllsch
701 1.36 jakllsch if (done)
702 1.36 jakllsch return;
703 1.36 jakllsch
704 1.36 jakllsch BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
705 1.36 jakllsch BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
706 1.36 jakllsch
707 1.36 jakllsch done = true;
708 1.36 jakllsch
709 1.36 jakllsch return;
710 1.16 dsl }
711 1.16 dsl
712 1.36 jakllsch #endif
713 1.36 jakllsch
714 1.22 junyoung int
715 1.24 junyoung biosdisk_open(struct open_file *f, ...)
716 1.23 junyoung /* struct open_file *f, int biosdev, int partition */
717 1.16 dsl {
718 1.16 dsl va_list ap;
719 1.16 dsl struct biosdisk *d;
720 1.23 junyoung int biosdev;
721 1.16 dsl int partition;
722 1.16 dsl int error = 0;
723 1.16 dsl
724 1.16 dsl va_start(ap, f);
725 1.23 junyoung biosdev = va_arg(ap, int);
726 1.23 junyoung d = alloc_biosdisk(biosdev);
727 1.23 junyoung if (d == NULL) {
728 1.16 dsl error = ENXIO;
729 1.16 dsl goto out;
730 1.16 dsl }
731 1.16 dsl
732 1.16 dsl partition = va_arg(ap, int);
733 1.16 dsl #ifdef _STANDALONE
734 1.16 dsl bi_disk.biosdev = d->ll.dev;
735 1.16 dsl bi_disk.partition = partition;
736 1.16 dsl bi_disk.labelsector = -1;
737 1.21 thorpej
738 1.21 thorpej bi_wedge.biosdev = d->ll.dev;
739 1.21 thorpej bi_wedge.matchblk = -1;
740 1.16 dsl #endif
741 1.16 dsl
742 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
743 1.36 jakllsch error = read_partitions(d);
744 1.16 dsl if (error == -1) {
745 1.16 dsl error = 0;
746 1.16 dsl goto nolabel;
747 1.16 dsl }
748 1.16 dsl if (error)
749 1.16 dsl goto out;
750 1.16 dsl
751 1.36 jakllsch if (partition >= BIOSDISKNPART ||
752 1.36 jakllsch d->part[partition].fstype == FS_UNUSED) {
753 1.1 perry #ifdef DISK_DEBUG
754 1.3 thorpej printf("illegal partition\n");
755 1.1 perry #endif
756 1.3 thorpej error = EPART;
757 1.3 thorpej goto out;
758 1.16 dsl }
759 1.21 thorpej
760 1.36 jakllsch d->boff = d->part[partition].offset;
761 1.36 jakllsch
762 1.36 jakllsch if (d->part[partition].fstype == FS_RAID)
763 1.36 jakllsch d->boff += RF_PROTECTED_SECTORS;
764 1.21 thorpej
765 1.36 jakllsch #ifdef _STANDALONE
766 1.36 jakllsch bi_wedge.startblk = d->part[partition].offset;
767 1.36 jakllsch bi_wedge.nblks = d->part[partition].size;
768 1.16 dsl #endif
769 1.36 jakllsch
770 1.7 drochner nolabel:
771 1.36 jakllsch #endif
772 1.1 perry #ifdef DISK_DEBUG
773 1.34 jakllsch printf("partition @%"PRId64"\n", d->boff);
774 1.1 perry #endif
775 1.1 perry
776 1.9 drochner #ifdef _STANDALONE
777 1.36 jakllsch add_biosdisk_bootinfo();
778 1.9 drochner #endif
779 1.5 drochner
780 1.3 thorpej f->f_devdata = d;
781 1.1 perry out:
782 1.5 drochner va_end(ap);
783 1.3 thorpej if (error)
784 1.35 jakllsch dealloc(d, sizeof(*d));
785 1.22 junyoung return error;
786 1.1 perry }
787 1.1 perry
788 1.12 drochner #ifndef LIBSA_NO_FS_CLOSE
789 1.22 junyoung int
790 1.24 junyoung biosdisk_close(struct open_file *f)
791 1.1 perry {
792 1.3 thorpej struct biosdisk *d = f->f_devdata;
793 1.1 perry
794 1.24 junyoung /* let the floppy drive go off */
795 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_FD)
796 1.31 tsutsui wait_sec(3); /* 2s is enough on all PCs I found */
797 1.1 perry
798 1.35 jakllsch dealloc(d, sizeof(*d));
799 1.3 thorpej f->f_devdata = NULL;
800 1.22 junyoung return 0;
801 1.1 perry }
802 1.12 drochner #endif
803 1.1 perry
804 1.22 junyoung int
805 1.24 junyoung biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
806 1.1 perry {
807 1.3 thorpej return EIO;
808 1.1 perry }
809