biosdisk.c revision 1.49 1 1.49 nonaka /* $NetBSD: biosdisk.c,v 1.49 2018/04/02 09:44:18 nonaka 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.47 nonaka #ifndef BIOSDISK_BUFSIZE
93 1.47 nonaka #define BIOSDISK_BUFSIZE 2048 /* must be large enough for a CD sector */
94 1.47 nonaka #endif
95 1.1 perry
96 1.36 jakllsch #define BIOSDISKNPART 26
97 1.36 jakllsch
98 1.3 thorpej struct biosdisk {
99 1.3 thorpej struct biosdisk_ll ll;
100 1.32 jakllsch daddr_t boff;
101 1.47 nonaka char buf[BIOSDISK_BUFSIZE];
102 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
103 1.47 nonaka struct biosdisk_partition part[BIOSDISKNPART];
104 1.36 jakllsch #endif
105 1.1 perry };
106 1.1 perry
107 1.36 jakllsch #ifndef NO_GPT
108 1.36 jakllsch const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
109 1.36 jakllsch const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
110 1.36 jakllsch const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
111 1.36 jakllsch const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
112 1.46 nonaka const struct uuid GET_nbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
113 1.46 nonaka const struct uuid GET_nbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
114 1.46 nonaka #ifdef EFIBOOT
115 1.46 nonaka const struct uuid GET_efi = GPT_ENT_TYPE_EFI;
116 1.46 nonaka const struct uuid GET_mbr = GPT_ENT_TYPE_MBR;
117 1.46 nonaka const struct uuid GET_fbsd = GPT_ENT_TYPE_FREEBSD;
118 1.46 nonaka const struct uuid GET_fbsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
119 1.46 nonaka const struct uuid GET_fbsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
120 1.46 nonaka const struct uuid GET_fbsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
121 1.46 nonaka const struct uuid GET_fbsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
122 1.46 nonaka const struct uuid GET_ms_rsvd = GPT_ENT_TYPE_MS_RESERVED;
123 1.46 nonaka const struct uuid GET_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
124 1.46 nonaka const struct uuid GET_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
125 1.46 nonaka const struct uuid GET_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
126 1.46 nonaka const struct uuid GET_linux_data = GPT_ENT_TYPE_LINUX_DATA;
127 1.46 nonaka const struct uuid GET_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
128 1.46 nonaka const struct uuid GET_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
129 1.46 nonaka const struct uuid GET_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
130 1.46 nonaka const struct uuid GET_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
131 1.46 nonaka const struct uuid GET_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
132 1.46 nonaka const struct uuid GET_bios = GPT_ENT_TYPE_BIOS;
133 1.46 nonaka
134 1.46 nonaka const struct gpt_part gpt_parts[] = {
135 1.46 nonaka { &GET_nbsd_raid, "NetBSD RAID" },
136 1.46 nonaka { &GET_nbsd_ffs, "NetBSD FFS" },
137 1.46 nonaka { &GET_nbsd_lfs, "NetBSD LFS" },
138 1.46 nonaka { &GET_nbsd_swap, "NetBSD Swap" },
139 1.46 nonaka { &GET_nbsd_ccd, "NetBSD ccd" },
140 1.46 nonaka { &GET_nbsd_cgd, "NetBSD cgd" },
141 1.46 nonaka { &GET_efi, "EFI System" },
142 1.46 nonaka { &GET_mbr, "MBR" },
143 1.46 nonaka { &GET_fbsd, "FreeBSD" },
144 1.46 nonaka { &GET_fbsd_swap, "FreeBSD Swap" },
145 1.46 nonaka { &GET_fbsd_ufs, "FreeBSD UFS" },
146 1.46 nonaka { &GET_fbsd_vinum, "FreeBSD Vinum" },
147 1.46 nonaka { &GET_fbsd_zfs, "FreeBSD ZFS" },
148 1.46 nonaka { &GET_ms_rsvd, "Microsoft Reserved" },
149 1.46 nonaka { &GET_ms_basic_data, "Microsoft Basic data" },
150 1.46 nonaka { &GET_ms_ldm_metadata, "Microsoft LDM metadata" },
151 1.46 nonaka { &GET_ms_ldm_data, "Microsoft LDM data" },
152 1.46 nonaka { &GET_linux_data, "Linux data" },
153 1.46 nonaka { &GET_linux_raid, "Linux RAID" },
154 1.46 nonaka { &GET_linux_swap, "Linux Swap" },
155 1.46 nonaka { &GET_linux_lvm, "Linux LVM" },
156 1.46 nonaka { &GET_apple_hfs, "Apple HFS" },
157 1.46 nonaka { &GET_apple_ufs, "Apple UFS" },
158 1.46 nonaka { &GET_bios, "BIOS Boot (GRUB)" },
159 1.46 nonaka };
160 1.46 nonaka #endif
161 1.36 jakllsch #endif /* NO_GPT */
162 1.36 jakllsch
163 1.9 drochner #ifdef _STANDALONE
164 1.5 drochner static struct btinfo_bootdisk bi_disk;
165 1.21 thorpej static struct btinfo_bootwedge bi_wedge;
166 1.9 drochner #endif
167 1.5 drochner
168 1.43 christos #define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
169 1.43 christos
170 1.13 lukem #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
171 1.13 lukem
172 1.47 nonaka #ifndef devb2cdb
173 1.47 nonaka #define devb2cdb(bno) (((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
174 1.47 nonaka #endif
175 1.47 nonaka
176 1.22 junyoung int
177 1.24 junyoung biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
178 1.24 junyoung void *buf, size_t *rsize)
179 1.1 perry {
180 1.3 thorpej struct biosdisk *d;
181 1.22 junyoung int blks, frag;
182 1.1 perry
183 1.3 thorpej if (flag != F_READ)
184 1.22 junyoung return EROFS;
185 1.1 perry
186 1.3 thorpej d = (struct biosdisk *) devdata;
187 1.1 perry
188 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_CD)
189 1.47 nonaka dblk = devb2cdb(dblk);
190 1.24 junyoung
191 1.3 thorpej dblk += d->boff;
192 1.3 thorpej
193 1.24 junyoung blks = size / d->ll.secsize;
194 1.3 thorpej if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
195 1.3 thorpej if (rsize)
196 1.3 thorpej *rsize = 0;
197 1.22 junyoung return EIO;
198 1.3 thorpej }
199 1.22 junyoung
200 1.24 junyoung /* needed for CD */
201 1.24 junyoung frag = size % d->ll.secsize;
202 1.3 thorpej if (frag) {
203 1.3 thorpej if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
204 1.3 thorpej if (rsize)
205 1.24 junyoung *rsize = blks * d->ll.secsize;
206 1.22 junyoung return EIO;
207 1.3 thorpej }
208 1.24 junyoung memcpy(buf + blks * d->ll.secsize, d->buf, frag);
209 1.3 thorpej }
210 1.22 junyoung
211 1.3 thorpej if (rsize)
212 1.3 thorpej *rsize = size;
213 1.22 junyoung return 0;
214 1.1 perry }
215 1.1 perry
216 1.16 dsl static struct biosdisk *
217 1.23 junyoung alloc_biosdisk(int biosdev)
218 1.1 perry {
219 1.3 thorpej struct biosdisk *d;
220 1.1 perry
221 1.23 junyoung d = alloc(sizeof(*d));
222 1.22 junyoung if (d == NULL)
223 1.16 dsl return NULL;
224 1.23 junyoung memset(d, 0, sizeof(*d));
225 1.16 dsl
226 1.23 junyoung d->ll.dev = biosdev;
227 1.11 fvdl if (set_geometry(&d->ll, NULL)) {
228 1.1 perry #ifdef DISK_DEBUG
229 1.3 thorpej printf("no geometry information\n");
230 1.1 perry #endif
231 1.26 christos dealloc(d, sizeof(*d));
232 1.16 dsl return NULL;
233 1.3 thorpej }
234 1.16 dsl return d;
235 1.16 dsl }
236 1.16 dsl
237 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
238 1.36 jakllsch static void
239 1.36 jakllsch md5(void *hash, const void *data, size_t len)
240 1.36 jakllsch {
241 1.36 jakllsch MD5_CTX ctx;
242 1.36 jakllsch
243 1.36 jakllsch MD5Init(&ctx);
244 1.36 jakllsch MD5Update(&ctx, data, len);
245 1.36 jakllsch MD5Final(hash, &ctx);
246 1.36 jakllsch
247 1.36 jakllsch return;
248 1.36 jakllsch }
249 1.36 jakllsch #endif
250 1.36 jakllsch
251 1.36 jakllsch #ifndef NO_GPT
252 1.49 nonaka bool
253 1.36 jakllsch guid_is_nil(const struct uuid *u)
254 1.36 jakllsch {
255 1.36 jakllsch static const struct uuid nil = { .time_low = 0 };
256 1.36 jakllsch return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
257 1.36 jakllsch }
258 1.36 jakllsch
259 1.49 nonaka bool
260 1.36 jakllsch guid_is_equal(const struct uuid *a, const struct uuid *b)
261 1.36 jakllsch {
262 1.36 jakllsch return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
263 1.36 jakllsch }
264 1.36 jakllsch
265 1.36 jakllsch static int
266 1.36 jakllsch check_gpt(struct biosdisk *d, daddr_t sector)
267 1.36 jakllsch {
268 1.36 jakllsch struct gpt_hdr gpth;
269 1.36 jakllsch const struct gpt_ent *ep;
270 1.36 jakllsch const struct uuid *u;
271 1.36 jakllsch daddr_t entblk;
272 1.36 jakllsch size_t size;
273 1.36 jakllsch uint32_t crc;
274 1.36 jakllsch int sectors;
275 1.36 jakllsch int entries;
276 1.36 jakllsch int entry;
277 1.36 jakllsch int i, j;
278 1.36 jakllsch
279 1.36 jakllsch /* read in gpt_hdr sector */
280 1.36 jakllsch if (readsects(&d->ll, sector, 1, d->buf, 1)) {
281 1.36 jakllsch #ifdef DISK_DEBUG
282 1.36 jakllsch printf("Error reading GPT header at %"PRId64"\n", sector);
283 1.36 jakllsch #endif
284 1.36 jakllsch return EIO;
285 1.36 jakllsch }
286 1.36 jakllsch
287 1.43 christos memcpy(&gpth, d->buf, sizeof(gpth));
288 1.36 jakllsch
289 1.36 jakllsch if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
290 1.36 jakllsch return -1;
291 1.36 jakllsch
292 1.36 jakllsch crc = gpth.hdr_crc_self;
293 1.36 jakllsch gpth.hdr_crc_self = 0;
294 1.36 jakllsch gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
295 1.36 jakllsch if (gpth.hdr_crc_self != crc) {
296 1.36 jakllsch return -1;
297 1.36 jakllsch }
298 1.36 jakllsch
299 1.36 jakllsch if (gpth.hdr_lba_self != sector)
300 1.36 jakllsch return -1;
301 1.36 jakllsch
302 1.36 jakllsch #ifdef _STANDALONE
303 1.36 jakllsch bi_wedge.matchblk = sector;
304 1.36 jakllsch bi_wedge.matchnblks = 1;
305 1.36 jakllsch
306 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
307 1.36 jakllsch #endif
308 1.36 jakllsch
309 1.36 jakllsch sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
310 1.36 jakllsch entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
311 1.36 jakllsch entblk = gpth.hdr_lba_table;
312 1.36 jakllsch crc = crc32(0, NULL, 0);
313 1.36 jakllsch
314 1.36 jakllsch j = 0;
315 1.36 jakllsch ep = (const struct gpt_ent *)d->buf;
316 1.36 jakllsch
317 1.36 jakllsch for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
318 1.36 jakllsch size = MIN(sizeof(d->buf),
319 1.36 jakllsch (gpth.hdr_entries - entry) * gpth.hdr_entsz);
320 1.36 jakllsch entries = size / gpth.hdr_entsz;
321 1.36 jakllsch sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
322 1.36 jakllsch if (readsects(&d->ll, entblk, sectors, d->buf, 1))
323 1.36 jakllsch return -1;
324 1.36 jakllsch entblk += sectors;
325 1.36 jakllsch crc = crc32(crc, (const void *)d->buf, size);
326 1.36 jakllsch
327 1.46 nonaka for (i = 0; j < BIOSDISKNPART && i < entries; i++) {
328 1.36 jakllsch u = (const struct uuid *)ep[i].ent_type;
329 1.36 jakllsch if (!guid_is_nil(u)) {
330 1.36 jakllsch d->part[j].offset = ep[i].ent_lba_start;
331 1.36 jakllsch d->part[j].size = ep[i].ent_lba_end -
332 1.36 jakllsch ep[i].ent_lba_start + 1;
333 1.36 jakllsch if (guid_is_equal(u, &GET_nbsd_ffs))
334 1.36 jakllsch d->part[j].fstype = FS_BSDFFS;
335 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_lfs))
336 1.36 jakllsch d->part[j].fstype = FS_BSDLFS;
337 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_raid))
338 1.36 jakllsch d->part[j].fstype = FS_RAID;
339 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_swap))
340 1.36 jakllsch d->part[j].fstype = FS_SWAP;
341 1.46 nonaka else if (guid_is_equal(u, &GET_nbsd_ccd))
342 1.46 nonaka d->part[j].fstype = FS_CCD;
343 1.46 nonaka else if (guid_is_equal(u, &GET_nbsd_cgd))
344 1.46 nonaka d->part[j].fstype = FS_CGD;
345 1.36 jakllsch else
346 1.36 jakllsch d->part[j].fstype = FS_OTHER;
347 1.46 nonaka #ifdef EFIBOOT
348 1.46 nonaka for (int k = 0;
349 1.46 nonaka k < __arraycount(gpt_parts);
350 1.46 nonaka k++) {
351 1.46 nonaka if (guid_is_equal(u, gpt_parts[k].guid))
352 1.46 nonaka d->part[j].guid = &gpt_parts[k];
353 1.46 nonaka }
354 1.46 nonaka d->part[j].attr = ep[i].ent_attr;
355 1.46 nonaka #endif
356 1.46 nonaka j++;
357 1.36 jakllsch }
358 1.36 jakllsch }
359 1.36 jakllsch
360 1.36 jakllsch }
361 1.36 jakllsch
362 1.36 jakllsch if (crc != gpth.hdr_crc_table) {
363 1.36 jakllsch #ifdef DISK_DEBUG
364 1.36 jakllsch printf("GPT table CRC invalid\n");
365 1.36 jakllsch #endif
366 1.36 jakllsch return -1;
367 1.36 jakllsch }
368 1.36 jakllsch
369 1.36 jakllsch return 0;
370 1.36 jakllsch }
371 1.36 jakllsch
372 1.36 jakllsch static int
373 1.36 jakllsch read_gpt(struct biosdisk *d)
374 1.36 jakllsch {
375 1.36 jakllsch struct biosdisk_extinfo ed;
376 1.36 jakllsch daddr_t gptsector[2];
377 1.36 jakllsch int i, error;
378 1.36 jakllsch
379 1.37 jakllsch if (d->ll.type != BIOSDISK_TYPE_HD)
380 1.37 jakllsch /* No GPT on floppy and CD */
381 1.37 jakllsch return -1;
382 1.37 jakllsch
383 1.36 jakllsch gptsector[0] = GPT_HDR_BLKNO;
384 1.36 jakllsch if (set_geometry(&d->ll, &ed) == 0 && d->ll.flags & BIOSDISK_INT13EXT) {
385 1.36 jakllsch gptsector[1] = ed.totsec - 1;
386 1.41 perseant /* Sanity check values returned from BIOS */
387 1.41 perseant if (ed.sbytes >= 512 && (ed.sbytes & (ed.sbytes - 1)) == 0)
388 1.41 perseant d->ll.secsize = ed.sbytes;
389 1.36 jakllsch } else {
390 1.36 jakllsch #ifdef DISK_DEBUG
391 1.36 jakllsch printf("Unable to determine extended disk geometry - "
392 1.36 jakllsch "using CHS\n");
393 1.36 jakllsch #endif
394 1.36 jakllsch /* at least try some other reasonable values then */
395 1.36 jakllsch gptsector[1] = d->ll.chs_sectors - 1;
396 1.36 jakllsch }
397 1.36 jakllsch
398 1.36 jakllsch for (i = 0; i < __arraycount(gptsector); i++) {
399 1.36 jakllsch error = check_gpt(d, gptsector[i]);
400 1.36 jakllsch if (error == 0)
401 1.36 jakllsch break;
402 1.36 jakllsch }
403 1.36 jakllsch
404 1.36 jakllsch if (i >= __arraycount(gptsector)) {
405 1.36 jakllsch memset(d->part, 0, sizeof(d->part));
406 1.36 jakllsch return -1;
407 1.36 jakllsch }
408 1.36 jakllsch
409 1.44 jakllsch #ifndef USE_SECONDARY_GPT
410 1.44 jakllsch if (i > 0) {
411 1.44 jakllsch #ifdef DISK_DEBUG
412 1.44 jakllsch printf("ignoring valid secondary GPT\n");
413 1.44 jakllsch #endif
414 1.44 jakllsch return -1;
415 1.44 jakllsch }
416 1.44 jakllsch #endif
417 1.44 jakllsch
418 1.36 jakllsch #ifdef DISK_DEBUG
419 1.36 jakllsch printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
420 1.36 jakllsch #endif
421 1.36 jakllsch return 0;
422 1.36 jakllsch }
423 1.36 jakllsch #endif /* !NO_GPT */
424 1.36 jakllsch
425 1.16 dsl #ifndef NO_DISKLABEL
426 1.37 jakllsch static void
427 1.37 jakllsch ingest_label(struct biosdisk *d, struct disklabel *lp)
428 1.37 jakllsch {
429 1.37 jakllsch int part;
430 1.37 jakllsch
431 1.37 jakllsch memset(d->part, 0, sizeof(d->part));
432 1.37 jakllsch
433 1.37 jakllsch for (part = 0; part < lp->d_npartitions; part++) {
434 1.37 jakllsch if (lp->d_partitions[part].p_size == 0)
435 1.37 jakllsch continue;
436 1.37 jakllsch if (lp->d_partitions[part].p_fstype == FS_UNUSED)
437 1.37 jakllsch continue;
438 1.37 jakllsch d->part[part].fstype = lp->d_partitions[part].p_fstype;
439 1.37 jakllsch d->part[part].offset = lp->d_partitions[part].p_offset;
440 1.37 jakllsch d->part[part].size = lp->d_partitions[part].p_size;
441 1.37 jakllsch }
442 1.37 jakllsch }
443 1.37 jakllsch
444 1.16 dsl static int
445 1.32 jakllsch check_label(struct biosdisk *d, daddr_t sector)
446 1.17 dsl {
447 1.17 dsl struct disklabel *lp;
448 1.17 dsl
449 1.17 dsl /* find partition in NetBSD disklabel */
450 1.17 dsl if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
451 1.17 dsl #ifdef DISK_DEBUG
452 1.17 dsl printf("Error reading disklabel\n");
453 1.17 dsl #endif
454 1.17 dsl return EIO;
455 1.17 dsl }
456 1.17 dsl lp = (struct disklabel *) (d->buf + LABELOFFSET);
457 1.17 dsl if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
458 1.17 dsl #ifdef DISK_DEBUG
459 1.33 jakllsch printf("warning: no disklabel in sector %"PRId64"\n", sector);
460 1.17 dsl #endif
461 1.17 dsl return -1;
462 1.17 dsl }
463 1.17 dsl
464 1.37 jakllsch ingest_label(d, lp);
465 1.36 jakllsch
466 1.36 jakllsch #ifdef _STANDALONE
467 1.39 gsutre bi_disk.labelsector = sector + LABELSECTOR;
468 1.36 jakllsch bi_disk.label.type = lp->d_type;
469 1.36 jakllsch memcpy(bi_disk.label.packname, lp->d_packname, 16);
470 1.36 jakllsch bi_disk.label.checksum = lp->d_checksum;
471 1.36 jakllsch
472 1.39 gsutre bi_wedge.matchblk = sector + LABELSECTOR;
473 1.36 jakllsch bi_wedge.matchnblks = 1;
474 1.36 jakllsch
475 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
476 1.36 jakllsch #endif
477 1.36 jakllsch
478 1.17 dsl return 0;
479 1.17 dsl }
480 1.17 dsl
481 1.17 dsl static int
482 1.40 christos read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
483 1.40 christos int this_ext, daddr_t sector)
484 1.40 christos {
485 1.40 christos struct mbr_partition mbr[MBR_PART_COUNT];
486 1.40 christos int i;
487 1.40 christos int typ;
488 1.40 christos struct partition *p;
489 1.40 christos
490 1.40 christos if (readsects(&d->ll, sector, 1, d->buf, 0)) {
491 1.40 christos #ifdef DISK_DEBUG
492 1.42 tsutsui printf("Error reading MFS sector %"PRId64"\n", sector);
493 1.40 christos #endif
494 1.40 christos return EIO;
495 1.40 christos }
496 1.40 christos if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
497 1.40 christos return -1;
498 1.40 christos }
499 1.43 christos memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
500 1.40 christos for (i = 0; i < MBR_PART_COUNT; i++) {
501 1.40 christos typ = mbr[i].mbrp_type;
502 1.40 christos if (typ == 0)
503 1.40 christos continue;
504 1.40 christos sector = this_ext + mbr[i].mbrp_start;
505 1.40 christos if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
506 1.40 christos continue;
507 1.40 christos p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
508 1.40 christos p->p_offset = sector;
509 1.40 christos p->p_size = mbr[i].mbrp_size;
510 1.40 christos p->p_fstype = xlat_mbr_fstype(typ);
511 1.40 christos }
512 1.40 christos return 0;
513 1.40 christos }
514 1.40 christos
515 1.47 nonaka #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
516 1.47 nonaka static int
517 1.47 nonaka check_cd9660(struct biosdisk *d)
518 1.47 nonaka {
519 1.47 nonaka struct biosdisk_extinfo ed;
520 1.47 nonaka struct iso_primary_descriptor *vd;
521 1.47 nonaka daddr_t bno;
522 1.47 nonaka
523 1.47 nonaka for (bno = 16;; bno++) {
524 1.47 nonaka if (readsects(&d->ll, bno, 1, d->buf, 0))
525 1.47 nonaka return -1;
526 1.47 nonaka vd = (struct iso_primary_descriptor *)d->buf;
527 1.47 nonaka if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
528 1.47 nonaka return -1;
529 1.47 nonaka if (isonum_711(vd->type) == ISO_VD_END)
530 1.47 nonaka return -1;
531 1.47 nonaka if (isonum_711(vd->type) == ISO_VD_PRIMARY)
532 1.47 nonaka break;
533 1.47 nonaka }
534 1.47 nonaka if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
535 1.47 nonaka return -1;
536 1.47 nonaka
537 1.47 nonaka if (set_geometry(&d->ll, &ed))
538 1.47 nonaka return -1;
539 1.47 nonaka
540 1.47 nonaka memset(d->part, 0, sizeof(d->part));
541 1.47 nonaka d->part[0].fstype = FS_ISO9660;
542 1.47 nonaka d->part[0].offset = 0;
543 1.47 nonaka d->part[0].size = ed.totsec;
544 1.47 nonaka return 0;
545 1.47 nonaka }
546 1.47 nonaka #endif
547 1.47 nonaka
548 1.40 christos static int
549 1.16 dsl read_label(struct biosdisk *d)
550 1.16 dsl {
551 1.17 dsl struct disklabel dflt_lbl;
552 1.18 lukem struct mbr_partition mbr[MBR_PART_COUNT];
553 1.17 dsl struct partition *p;
554 1.42 tsutsui uint32_t sector;
555 1.42 tsutsui int i;
556 1.17 dsl int error;
557 1.17 dsl int typ;
558 1.42 tsutsui uint32_t ext_base, this_ext, next_ext;
559 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
560 1.17 dsl int sector_386bsd = -1;
561 1.17 dsl #endif
562 1.17 dsl
563 1.23 junyoung memset(&dflt_lbl, 0, sizeof(dflt_lbl));
564 1.17 dsl dflt_lbl.d_npartitions = 8;
565 1.5 drochner
566 1.7 drochner d->boff = 0;
567 1.7 drochner
568 1.24 junyoung if (d->ll.type != BIOSDISK_TYPE_HD)
569 1.24 junyoung /* No label on floppy and CD */
570 1.16 dsl return -1;
571 1.7 drochner
572 1.3 thorpej /*
573 1.7 drochner * find NetBSD Partition in DOS partition table
574 1.7 drochner * XXX check magic???
575 1.3 thorpej */
576 1.17 dsl ext_base = 0;
577 1.17 dsl next_ext = 0;
578 1.17 dsl for (;;) {
579 1.17 dsl this_ext = ext_base + next_ext;
580 1.17 dsl next_ext = 0;
581 1.17 dsl if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
582 1.1 perry #ifdef DISK_DEBUG
583 1.42 tsutsui printf("error reading MBR sector %u\n", this_ext);
584 1.17 dsl #endif
585 1.17 dsl return EIO;
586 1.17 dsl }
587 1.43 christos memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
588 1.17 dsl /* Look for NetBSD partition ID */
589 1.18 lukem for (i = 0; i < MBR_PART_COUNT; i++) {
590 1.18 lukem typ = mbr[i].mbrp_type;
591 1.17 dsl if (typ == 0)
592 1.17 dsl continue;
593 1.17 dsl sector = this_ext + mbr[i].mbrp_start;
594 1.27 dsl #ifdef DISK_DEBUG
595 1.42 tsutsui printf("ptn type %d in sector %u\n", typ, sector);
596 1.27 dsl #endif
597 1.40 christos if (typ == MBR_PTYPE_MINIX_14B) {
598 1.40 christos if (!read_minix_subp(d, &dflt_lbl,
599 1.40 christos this_ext, sector)) {
600 1.40 christos /* Don't add "container" partition */
601 1.40 christos continue;
602 1.40 christos }
603 1.40 christos }
604 1.17 dsl if (typ == MBR_PTYPE_NETBSD) {
605 1.17 dsl error = check_label(d, sector);
606 1.17 dsl if (error >= 0)
607 1.17 dsl return error;
608 1.17 dsl }
609 1.17 dsl if (MBR_IS_EXTENDED(typ)) {
610 1.17 dsl next_ext = mbr[i].mbrp_start;
611 1.17 dsl continue;
612 1.17 dsl }
613 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
614 1.17 dsl if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
615 1.17 dsl sector_386bsd = sector;
616 1.1 perry #endif
617 1.17 dsl if (this_ext != 0) {
618 1.17 dsl if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
619 1.17 dsl continue;
620 1.17 dsl p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
621 1.17 dsl } else
622 1.17 dsl p = &dflt_lbl.d_partitions[i];
623 1.17 dsl p->p_offset = sector;
624 1.17 dsl p->p_size = mbr[i].mbrp_size;
625 1.17 dsl p->p_fstype = xlat_mbr_fstype(typ);
626 1.17 dsl }
627 1.17 dsl if (next_ext == 0)
628 1.3 thorpej break;
629 1.17 dsl if (ext_base == 0) {
630 1.17 dsl ext_base = next_ext;
631 1.17 dsl next_ext = 0;
632 1.3 thorpej }
633 1.17 dsl }
634 1.17 dsl
635 1.17 dsl sector = 0;
636 1.8 drochner #ifdef COMPAT_386BSD_MBRPART
637 1.17 dsl if (sector_386bsd != -1) {
638 1.17 dsl printf("old BSD partition ID!\n");
639 1.17 dsl sector = sector_386bsd;
640 1.8 drochner }
641 1.8 drochner #endif
642 1.7 drochner
643 1.17 dsl /*
644 1.17 dsl * One of two things:
645 1.17 dsl * 1. no MBR
646 1.17 dsl * 2. no NetBSD partition in MBR
647 1.17 dsl *
648 1.17 dsl * We simply default to "start of disk" in this case and
649 1.17 dsl * press on.
650 1.17 dsl */
651 1.17 dsl error = check_label(d, sector);
652 1.17 dsl if (error >= 0)
653 1.17 dsl return error;
654 1.16 dsl
655 1.47 nonaka #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
656 1.47 nonaka /* Check CD/DVD */
657 1.47 nonaka error = check_cd9660(d);
658 1.47 nonaka if (error >= 0)
659 1.47 nonaka return error;
660 1.47 nonaka #endif
661 1.47 nonaka
662 1.17 dsl /*
663 1.17 dsl * Nothing at start of disk, return info from mbr partitions.
664 1.17 dsl */
665 1.17 dsl /* XXX fill it to make checksum match kernel one */
666 1.17 dsl dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
667 1.37 jakllsch ingest_label(d, &dflt_lbl);
668 1.27 dsl return 0;
669 1.16 dsl }
670 1.16 dsl #endif /* NO_DISKLABEL */
671 1.16 dsl
672 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
673 1.36 jakllsch static int
674 1.36 jakllsch read_partitions(struct biosdisk *d)
675 1.36 jakllsch {
676 1.36 jakllsch int error;
677 1.36 jakllsch
678 1.36 jakllsch error = -1;
679 1.36 jakllsch
680 1.36 jakllsch #ifndef NO_GPT
681 1.36 jakllsch error = read_gpt(d);
682 1.36 jakllsch if (error == 0)
683 1.36 jakllsch return 0;
684 1.36 jakllsch
685 1.36 jakllsch #endif
686 1.36 jakllsch #ifndef NO_DISKLABEL
687 1.36 jakllsch error = read_label(d);
688 1.36 jakllsch
689 1.36 jakllsch #endif
690 1.36 jakllsch return error;
691 1.36 jakllsch }
692 1.36 jakllsch #endif
693 1.36 jakllsch
694 1.29 jmcneill void
695 1.29 jmcneill biosdisk_probe(void)
696 1.29 jmcneill {
697 1.29 jmcneill struct biosdisk d;
698 1.29 jmcneill struct biosdisk_extinfo ed;
699 1.29 jmcneill uint64_t size;
700 1.36 jakllsch int first;
701 1.29 jmcneill int i;
702 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
703 1.36 jakllsch int part;
704 1.36 jakllsch #endif
705 1.29 jmcneill
706 1.29 jmcneill for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
707 1.29 jmcneill first = 1;
708 1.29 jmcneill memset(&d, 0, sizeof(d));
709 1.29 jmcneill memset(&ed, 0, sizeof(ed));
710 1.29 jmcneill if (i >= MAX_BIOSDISKS)
711 1.29 jmcneill d.ll.dev = 0x00 + i - MAX_BIOSDISKS; /* fd */
712 1.29 jmcneill else
713 1.29 jmcneill d.ll.dev = 0x80 + i; /* hd/cd */
714 1.29 jmcneill if (set_geometry(&d.ll, &ed))
715 1.29 jmcneill continue;
716 1.30 jmcneill printf("disk ");
717 1.29 jmcneill switch (d.ll.type) {
718 1.29 jmcneill case BIOSDISK_TYPE_CD:
719 1.30 jmcneill printf("cd0\n cd0a\n");
720 1.29 jmcneill break;
721 1.29 jmcneill case BIOSDISK_TYPE_FD:
722 1.30 jmcneill printf("fd%d\n", d.ll.dev & 0x7f);
723 1.30 jmcneill printf(" fd%da\n", d.ll.dev & 0x7f);
724 1.29 jmcneill break;
725 1.29 jmcneill case BIOSDISK_TYPE_HD:
726 1.30 jmcneill printf("hd%d", d.ll.dev & 0x7f);
727 1.29 jmcneill if (d.ll.flags & BIOSDISK_INT13EXT) {
728 1.29 jmcneill printf(" size ");
729 1.29 jmcneill size = ed.totsec * ed.sbytes;
730 1.29 jmcneill if (size >= (10ULL * 1024 * 1024 * 1024))
731 1.33 jakllsch printf("%"PRIu64" GB",
732 1.29 jmcneill size / (1024 * 1024 * 1024));
733 1.29 jmcneill else
734 1.33 jakllsch printf("%"PRIu64" MB",
735 1.29 jmcneill size / (1024 * 1024));
736 1.29 jmcneill }
737 1.29 jmcneill printf("\n");
738 1.29 jmcneill break;
739 1.29 jmcneill }
740 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
741 1.30 jmcneill if (d.ll.type != BIOSDISK_TYPE_HD)
742 1.30 jmcneill continue;
743 1.36 jakllsch
744 1.36 jakllsch if (read_partitions(&d) != 0)
745 1.36 jakllsch continue;
746 1.36 jakllsch
747 1.36 jakllsch for (part = 0; part < BIOSDISKNPART; part++) {
748 1.36 jakllsch if (d.part[part].size == 0)
749 1.29 jmcneill continue;
750 1.36 jakllsch if (d.part[part].fstype == FS_UNUSED)
751 1.29 jmcneill continue;
752 1.29 jmcneill if (first) {
753 1.29 jmcneill printf(" ");
754 1.29 jmcneill first = 0;
755 1.29 jmcneill }
756 1.29 jmcneill printf(" hd%d%c(", d.ll.dev & 0x7f, part + 'a');
757 1.46 nonaka #ifdef EFIBOOT
758 1.46 nonaka if (d.part[part].guid != NULL)
759 1.46 nonaka printf("%s", d.part[part].guid->name);
760 1.46 nonaka else
761 1.46 nonaka #endif
762 1.36 jakllsch if (d.part[part].fstype < FSMAXTYPES)
763 1.29 jmcneill printf("%s",
764 1.36 jakllsch fstypenames[d.part[part].fstype]);
765 1.29 jmcneill else
766 1.36 jakllsch printf("%d", d.part[part].fstype);
767 1.29 jmcneill printf(")");
768 1.29 jmcneill }
769 1.36 jakllsch #endif
770 1.29 jmcneill if (first == 0)
771 1.29 jmcneill printf("\n");
772 1.29 jmcneill }
773 1.29 jmcneill }
774 1.29 jmcneill
775 1.16 dsl /* Determine likely partition for possible sector number of dos
776 1.17 dsl * partition.
777 1.17 dsl */
778 1.16 dsl
779 1.24 junyoung int
780 1.32 jakllsch biosdisk_findpartition(int biosdev, daddr_t sector)
781 1.16 dsl {
782 1.36 jakllsch #if defined(NO_DISKLABEL) && defined(NO_GPT)
783 1.16 dsl return 0;
784 1.16 dsl #else
785 1.16 dsl struct biosdisk *d;
786 1.24 junyoung int partition = 0;
787 1.46 nonaka #ifdef EFIBOOT
788 1.46 nonaka int candidate = 0;
789 1.46 nonaka #endif
790 1.46 nonaka
791 1.27 dsl #ifdef DISK_DEBUG
792 1.33 jakllsch printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
793 1.27 dsl #endif
794 1.16 dsl
795 1.16 dsl /* Look for netbsd partition that is the dos boot one */
796 1.16 dsl d = alloc_biosdisk(biosdev);
797 1.17 dsl if (d == NULL)
798 1.17 dsl return 0;
799 1.17 dsl
800 1.36 jakllsch if (read_partitions(d) == 0) {
801 1.36 jakllsch for (partition = (BIOSDISKNPART-1); --partition;) {
802 1.36 jakllsch if (d->part[partition].fstype == FS_UNUSED)
803 1.16 dsl continue;
804 1.46 nonaka #ifdef EFIBOOT
805 1.48 nonaka switch (d->part[partition].fstype) {
806 1.48 nonaka case FS_BSDFFS:
807 1.48 nonaka case FS_BSDLFS:
808 1.48 nonaka case FS_RAID:
809 1.48 nonaka case FS_CCD:
810 1.48 nonaka case FS_CGD:
811 1.48 nonaka case FS_ISO9660:
812 1.48 nonaka if (d->part[partition].attr & GPT_ENT_ATTR_BOOTME)
813 1.48 nonaka goto found;
814 1.48 nonaka candidate = partition;
815 1.48 nonaka break;
816 1.46 nonaka
817 1.48 nonaka default:
818 1.48 nonaka if (d->part[partition].attr & GPT_ENT_ATTR_BOOTME)
819 1.46 nonaka candidate = partition;
820 1.46 nonaka break;
821 1.46 nonaka }
822 1.46 nonaka #else
823 1.36 jakllsch if (d->part[partition].offset == sector)
824 1.16 dsl break;
825 1.46 nonaka #endif
826 1.16 dsl }
827 1.46 nonaka #ifdef EFIBOOT
828 1.48 nonaka found:
829 1.46 nonaka if (partition == 0 && candidate != 0)
830 1.46 nonaka partition = candidate;
831 1.46 nonaka #endif
832 1.16 dsl }
833 1.16 dsl
834 1.26 christos dealloc(d, sizeof(*d));
835 1.16 dsl return partition;
836 1.36 jakllsch #endif /* NO_DISKLABEL && NO_GPT */
837 1.36 jakllsch }
838 1.36 jakllsch
839 1.47 nonaka int
840 1.47 nonaka biosdisk_readpartition(int biosdev, struct biosdisk_partition **partpp,
841 1.47 nonaka int *rnum)
842 1.47 nonaka {
843 1.47 nonaka #if defined(NO_DISKLABEL) && defined(NO_GPT)
844 1.47 nonaka return ENOTSUP;
845 1.47 nonaka #else
846 1.47 nonaka struct biosdisk *d;
847 1.47 nonaka struct biosdisk_partition *part;
848 1.47 nonaka int rv;
849 1.47 nonaka
850 1.47 nonaka /* Look for netbsd partition that is the dos boot one */
851 1.47 nonaka d = alloc_biosdisk(biosdev);
852 1.47 nonaka if (d == NULL)
853 1.47 nonaka return ENOMEM;
854 1.47 nonaka
855 1.47 nonaka if (read_partitions(d)) {
856 1.47 nonaka rv = EINVAL;
857 1.47 nonaka goto out;
858 1.47 nonaka }
859 1.47 nonaka
860 1.47 nonaka part = alloc(sizeof(d->part));
861 1.47 nonaka if (part == NULL) {
862 1.47 nonaka rv = ENOMEM;
863 1.47 nonaka goto out;
864 1.47 nonaka }
865 1.47 nonaka
866 1.47 nonaka memcpy(part, d->part, sizeof(d->part));
867 1.47 nonaka *partpp = part;
868 1.47 nonaka *rnum = (int)__arraycount(d->part);
869 1.47 nonaka rv = 0;
870 1.47 nonaka out:
871 1.47 nonaka dealloc(d, sizeof(*d));
872 1.47 nonaka return rv;
873 1.47 nonaka #endif /* NO_DISKLABEL && NO_GPT */
874 1.47 nonaka }
875 1.47 nonaka
876 1.36 jakllsch #ifdef _STANDALONE
877 1.36 jakllsch static void
878 1.36 jakllsch add_biosdisk_bootinfo(void)
879 1.36 jakllsch {
880 1.36 jakllsch if (bootinfo == NULL) {
881 1.36 jakllsch return;
882 1.36 jakllsch }
883 1.36 jakllsch BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
884 1.36 jakllsch BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
885 1.36 jakllsch return;
886 1.16 dsl }
887 1.36 jakllsch #endif
888 1.36 jakllsch
889 1.22 junyoung int
890 1.24 junyoung biosdisk_open(struct open_file *f, ...)
891 1.23 junyoung /* struct open_file *f, int biosdev, int partition */
892 1.16 dsl {
893 1.16 dsl va_list ap;
894 1.16 dsl struct biosdisk *d;
895 1.23 junyoung int biosdev;
896 1.16 dsl int partition;
897 1.16 dsl int error = 0;
898 1.16 dsl
899 1.16 dsl va_start(ap, f);
900 1.23 junyoung biosdev = va_arg(ap, int);
901 1.23 junyoung d = alloc_biosdisk(biosdev);
902 1.23 junyoung if (d == NULL) {
903 1.16 dsl error = ENXIO;
904 1.16 dsl goto out;
905 1.16 dsl }
906 1.16 dsl
907 1.16 dsl partition = va_arg(ap, int);
908 1.16 dsl #ifdef _STANDALONE
909 1.16 dsl bi_disk.biosdev = d->ll.dev;
910 1.16 dsl bi_disk.partition = partition;
911 1.16 dsl bi_disk.labelsector = -1;
912 1.21 thorpej
913 1.21 thorpej bi_wedge.biosdev = d->ll.dev;
914 1.21 thorpej bi_wedge.matchblk = -1;
915 1.16 dsl #endif
916 1.16 dsl
917 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
918 1.36 jakllsch error = read_partitions(d);
919 1.16 dsl if (error == -1) {
920 1.16 dsl error = 0;
921 1.16 dsl goto nolabel;
922 1.16 dsl }
923 1.16 dsl if (error)
924 1.16 dsl goto out;
925 1.16 dsl
926 1.36 jakllsch if (partition >= BIOSDISKNPART ||
927 1.36 jakllsch d->part[partition].fstype == FS_UNUSED) {
928 1.1 perry #ifdef DISK_DEBUG
929 1.3 thorpej printf("illegal partition\n");
930 1.1 perry #endif
931 1.3 thorpej error = EPART;
932 1.3 thorpej goto out;
933 1.16 dsl }
934 1.21 thorpej
935 1.36 jakllsch d->boff = d->part[partition].offset;
936 1.36 jakllsch
937 1.36 jakllsch if (d->part[partition].fstype == FS_RAID)
938 1.36 jakllsch d->boff += RF_PROTECTED_SECTORS;
939 1.21 thorpej
940 1.36 jakllsch #ifdef _STANDALONE
941 1.36 jakllsch bi_wedge.startblk = d->part[partition].offset;
942 1.36 jakllsch bi_wedge.nblks = d->part[partition].size;
943 1.16 dsl #endif
944 1.36 jakllsch
945 1.7 drochner nolabel:
946 1.36 jakllsch #endif
947 1.1 perry #ifdef DISK_DEBUG
948 1.34 jakllsch printf("partition @%"PRId64"\n", d->boff);
949 1.1 perry #endif
950 1.1 perry
951 1.9 drochner #ifdef _STANDALONE
952 1.36 jakllsch add_biosdisk_bootinfo();
953 1.9 drochner #endif
954 1.5 drochner
955 1.3 thorpej f->f_devdata = d;
956 1.1 perry out:
957 1.5 drochner va_end(ap);
958 1.3 thorpej if (error)
959 1.35 jakllsch dealloc(d, sizeof(*d));
960 1.22 junyoung return error;
961 1.1 perry }
962 1.1 perry
963 1.12 drochner #ifndef LIBSA_NO_FS_CLOSE
964 1.22 junyoung int
965 1.24 junyoung biosdisk_close(struct open_file *f)
966 1.1 perry {
967 1.3 thorpej struct biosdisk *d = f->f_devdata;
968 1.1 perry
969 1.24 junyoung /* let the floppy drive go off */
970 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_FD)
971 1.31 tsutsui wait_sec(3); /* 2s is enough on all PCs I found */
972 1.1 perry
973 1.35 jakllsch dealloc(d, sizeof(*d));
974 1.3 thorpej f->f_devdata = NULL;
975 1.22 junyoung return 0;
976 1.1 perry }
977 1.12 drochner #endif
978 1.1 perry
979 1.22 junyoung int
980 1.24 junyoung biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
981 1.1 perry {
982 1.3 thorpej return EIO;
983 1.1 perry }
984