biosdisk.c revision 1.53 1 1.53 manu /* $NetBSD: biosdisk.c,v 1.53 2019/12/10 02:02:47 manu 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.50 manu #include <fs/unicode.h>
82 1.1 perry
83 1.1 perry #include <lib/libsa/saerrno.h>
84 1.29 jmcneill #include <machine/cpu.h>
85 1.1 perry
86 1.1 perry #include "libi386.h"
87 1.1 perry #include "biosdisk_ll.h"
88 1.5 drochner #include "biosdisk.h"
89 1.9 drochner #ifdef _STANDALONE
90 1.5 drochner #include "bootinfo.h"
91 1.9 drochner #endif
92 1.16 dsl
93 1.50 manu #ifndef NO_GPT
94 1.50 manu #define MAXDEVNAME 39 /* "NAME=" + 34 char part_name */
95 1.50 manu #else
96 1.50 manu #define MAXDEVNAME 16
97 1.50 manu #endif
98 1.50 manu
99 1.47 nonaka #ifndef BIOSDISK_BUFSIZE
100 1.47 nonaka #define BIOSDISK_BUFSIZE 2048 /* must be large enough for a CD sector */
101 1.47 nonaka #endif
102 1.1 perry
103 1.36 jakllsch #define BIOSDISKNPART 26
104 1.36 jakllsch
105 1.3 thorpej struct biosdisk {
106 1.3 thorpej struct biosdisk_ll ll;
107 1.32 jakllsch daddr_t boff;
108 1.47 nonaka char buf[BIOSDISK_BUFSIZE];
109 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
110 1.47 nonaka struct biosdisk_partition part[BIOSDISKNPART];
111 1.36 jakllsch #endif
112 1.1 perry };
113 1.1 perry
114 1.50 manu #include <dev/raidframe/raidframevar.h>
115 1.50 manu #define RF_COMPONENT_INFO_OFFSET 16384 /* from sys/dev/raidframe/rf_netbsdkintf.c */
116 1.50 manu #define RF_COMPONENT_LABEL_VERSION 2 /* from <dev/raidframe/rf_raid.h> */
117 1.50 manu
118 1.50 manu #define RAIDFRAME_NDEV 16 /* abitrary limit to 15 raidframe devices */
119 1.50 manu struct raidframe {
120 1.50 manu int last_unit;
121 1.50 manu int serial;
122 1.50 manu int biosdev;
123 1.50 manu int parent_part;
124 1.50 manu #ifndef NO_GPT
125 1.50 manu char parent_name[MAXDEVNAME + 1];
126 1.50 manu #endif
127 1.50 manu daddr_t offset;
128 1.50 manu daddr_t size;
129 1.50 manu };
130 1.50 manu
131 1.50 manu
132 1.36 jakllsch #ifndef NO_GPT
133 1.36 jakllsch const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
134 1.36 jakllsch const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
135 1.36 jakllsch const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
136 1.36 jakllsch const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
137 1.46 nonaka const struct uuid GET_nbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
138 1.46 nonaka const struct uuid GET_nbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
139 1.50 manu
140 1.46 nonaka const struct uuid GET_efi = GPT_ENT_TYPE_EFI;
141 1.46 nonaka const struct uuid GET_mbr = GPT_ENT_TYPE_MBR;
142 1.46 nonaka const struct uuid GET_fbsd = GPT_ENT_TYPE_FREEBSD;
143 1.46 nonaka const struct uuid GET_fbsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
144 1.46 nonaka const struct uuid GET_fbsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
145 1.46 nonaka const struct uuid GET_fbsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
146 1.46 nonaka const struct uuid GET_fbsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
147 1.46 nonaka const struct uuid GET_ms_rsvd = GPT_ENT_TYPE_MS_RESERVED;
148 1.46 nonaka const struct uuid GET_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
149 1.46 nonaka const struct uuid GET_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
150 1.46 nonaka const struct uuid GET_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
151 1.46 nonaka const struct uuid GET_linux_data = GPT_ENT_TYPE_LINUX_DATA;
152 1.46 nonaka const struct uuid GET_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
153 1.46 nonaka const struct uuid GET_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
154 1.46 nonaka const struct uuid GET_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
155 1.46 nonaka const struct uuid GET_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
156 1.46 nonaka const struct uuid GET_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
157 1.46 nonaka const struct uuid GET_bios = GPT_ENT_TYPE_BIOS;
158 1.46 nonaka
159 1.46 nonaka const struct gpt_part gpt_parts[] = {
160 1.46 nonaka { &GET_nbsd_raid, "NetBSD RAID" },
161 1.46 nonaka { &GET_nbsd_ffs, "NetBSD FFS" },
162 1.46 nonaka { &GET_nbsd_lfs, "NetBSD LFS" },
163 1.46 nonaka { &GET_nbsd_swap, "NetBSD Swap" },
164 1.46 nonaka { &GET_nbsd_ccd, "NetBSD ccd" },
165 1.46 nonaka { &GET_nbsd_cgd, "NetBSD cgd" },
166 1.46 nonaka { &GET_efi, "EFI System" },
167 1.46 nonaka { &GET_mbr, "MBR" },
168 1.46 nonaka { &GET_fbsd, "FreeBSD" },
169 1.46 nonaka { &GET_fbsd_swap, "FreeBSD Swap" },
170 1.46 nonaka { &GET_fbsd_ufs, "FreeBSD UFS" },
171 1.46 nonaka { &GET_fbsd_vinum, "FreeBSD Vinum" },
172 1.46 nonaka { &GET_fbsd_zfs, "FreeBSD ZFS" },
173 1.46 nonaka { &GET_ms_rsvd, "Microsoft Reserved" },
174 1.46 nonaka { &GET_ms_basic_data, "Microsoft Basic data" },
175 1.46 nonaka { &GET_ms_ldm_metadata, "Microsoft LDM metadata" },
176 1.46 nonaka { &GET_ms_ldm_data, "Microsoft LDM data" },
177 1.46 nonaka { &GET_linux_data, "Linux data" },
178 1.46 nonaka { &GET_linux_raid, "Linux RAID" },
179 1.46 nonaka { &GET_linux_swap, "Linux Swap" },
180 1.46 nonaka { &GET_linux_lvm, "Linux LVM" },
181 1.46 nonaka { &GET_apple_hfs, "Apple HFS" },
182 1.46 nonaka { &GET_apple_ufs, "Apple UFS" },
183 1.46 nonaka { &GET_bios, "BIOS Boot (GRUB)" },
184 1.46 nonaka };
185 1.36 jakllsch #endif /* NO_GPT */
186 1.36 jakllsch
187 1.52 manu struct btinfo_bootdisk bi_disk;
188 1.52 manu struct btinfo_bootwedge bi_wedge;
189 1.5 drochner
190 1.43 christos #define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
191 1.43 christos
192 1.47 nonaka #ifndef devb2cdb
193 1.47 nonaka #define devb2cdb(bno) (((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
194 1.47 nonaka #endif
195 1.47 nonaka
196 1.50 manu static void
197 1.50 manu dealloc_biosdisk(struct biosdisk *d)
198 1.50 manu {
199 1.50 manu #ifndef NO_GPT
200 1.50 manu int i;
201 1.50 manu
202 1.50 manu for (i = 0; i < __arraycount(d->part); i++) {
203 1.50 manu if (d->part[i].part_name != NULL)
204 1.50 manu dealloc(d->part[i].part_name, BIOSDISK_PART_NAME_LEN);
205 1.50 manu }
206 1.50 manu #endif
207 1.50 manu
208 1.50 manu dealloc(d, sizeof(*d));
209 1.50 manu
210 1.50 manu return;
211 1.50 manu }
212 1.50 manu
213 1.50 manu static struct biosdisk_partition *
214 1.50 manu copy_biosdisk_part(struct biosdisk *d)
215 1.50 manu {
216 1.50 manu struct biosdisk_partition *part;
217 1.50 manu
218 1.50 manu part = alloc(sizeof(d->part));
219 1.50 manu if (part == NULL)
220 1.50 manu goto out;
221 1.50 manu
222 1.50 manu memcpy(part, d->part, sizeof(d->part));
223 1.50 manu
224 1.50 manu #ifndef NO_GPT
225 1.50 manu int i;
226 1.50 manu
227 1.50 manu for (i = 0; i < __arraycount(d->part); i++) {
228 1.50 manu if (d->part[i].part_name != NULL) {
229 1.50 manu part[i].part_name = alloc(BIOSDISK_PART_NAME_LEN);
230 1.50 manu memcpy(part[i].part_name, d->part[i].part_name,
231 1.50 manu BIOSDISK_PART_NAME_LEN);
232 1.50 manu }
233 1.50 manu }
234 1.50 manu #endif
235 1.50 manu
236 1.50 manu out:
237 1.50 manu return part;
238 1.50 manu }
239 1.50 manu
240 1.22 junyoung int
241 1.24 junyoung biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
242 1.24 junyoung void *buf, size_t *rsize)
243 1.1 perry {
244 1.3 thorpej struct biosdisk *d;
245 1.22 junyoung int blks, frag;
246 1.1 perry
247 1.3 thorpej if (flag != F_READ)
248 1.22 junyoung return EROFS;
249 1.1 perry
250 1.3 thorpej d = (struct biosdisk *) devdata;
251 1.1 perry
252 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_CD)
253 1.47 nonaka dblk = devb2cdb(dblk);
254 1.24 junyoung
255 1.3 thorpej dblk += d->boff;
256 1.3 thorpej
257 1.24 junyoung blks = size / d->ll.secsize;
258 1.3 thorpej if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
259 1.3 thorpej if (rsize)
260 1.3 thorpej *rsize = 0;
261 1.22 junyoung return EIO;
262 1.3 thorpej }
263 1.22 junyoung
264 1.24 junyoung /* needed for CD */
265 1.24 junyoung frag = size % d->ll.secsize;
266 1.3 thorpej if (frag) {
267 1.3 thorpej if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
268 1.3 thorpej if (rsize)
269 1.24 junyoung *rsize = blks * d->ll.secsize;
270 1.22 junyoung return EIO;
271 1.3 thorpej }
272 1.24 junyoung memcpy(buf + blks * d->ll.secsize, d->buf, frag);
273 1.3 thorpej }
274 1.22 junyoung
275 1.3 thorpej if (rsize)
276 1.3 thorpej *rsize = size;
277 1.22 junyoung return 0;
278 1.1 perry }
279 1.1 perry
280 1.16 dsl static struct biosdisk *
281 1.23 junyoung alloc_biosdisk(int biosdev)
282 1.1 perry {
283 1.3 thorpej struct biosdisk *d;
284 1.1 perry
285 1.23 junyoung d = alloc(sizeof(*d));
286 1.22 junyoung if (d == NULL)
287 1.16 dsl return NULL;
288 1.23 junyoung memset(d, 0, sizeof(*d));
289 1.16 dsl
290 1.23 junyoung d->ll.dev = biosdev;
291 1.11 fvdl if (set_geometry(&d->ll, NULL)) {
292 1.1 perry #ifdef DISK_DEBUG
293 1.3 thorpej printf("no geometry information\n");
294 1.1 perry #endif
295 1.50 manu dealloc_biosdisk(d);
296 1.16 dsl return NULL;
297 1.3 thorpej }
298 1.16 dsl return d;
299 1.16 dsl }
300 1.16 dsl
301 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
302 1.36 jakllsch static void
303 1.36 jakllsch md5(void *hash, const void *data, size_t len)
304 1.36 jakllsch {
305 1.36 jakllsch MD5_CTX ctx;
306 1.36 jakllsch
307 1.36 jakllsch MD5Init(&ctx);
308 1.36 jakllsch MD5Update(&ctx, data, len);
309 1.36 jakllsch MD5Final(hash, &ctx);
310 1.36 jakllsch
311 1.36 jakllsch return;
312 1.36 jakllsch }
313 1.36 jakllsch #endif
314 1.36 jakllsch
315 1.36 jakllsch #ifndef NO_GPT
316 1.49 nonaka bool
317 1.36 jakllsch guid_is_nil(const struct uuid *u)
318 1.36 jakllsch {
319 1.36 jakllsch static const struct uuid nil = { .time_low = 0 };
320 1.36 jakllsch return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
321 1.36 jakllsch }
322 1.36 jakllsch
323 1.49 nonaka bool
324 1.36 jakllsch guid_is_equal(const struct uuid *a, const struct uuid *b)
325 1.36 jakllsch {
326 1.36 jakllsch return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
327 1.36 jakllsch }
328 1.36 jakllsch
329 1.50 manu #ifndef NO_GPT
330 1.50 manu static void
331 1.50 manu part_name_utf8(const uint16_t *utf16_src, size_t utf16_srclen,
332 1.50 manu char *utf8_dst, size_t utf8_dstlen)
333 1.50 manu {
334 1.50 manu char *c = utf8_dst;
335 1.50 manu size_t r = utf8_dstlen - 1;
336 1.50 manu size_t n;
337 1.50 manu int j;
338 1.50 manu
339 1.50 manu if (utf8_dst == NULL)
340 1.50 manu return;
341 1.50 manu
342 1.50 manu for (j = 0; j < utf16_srclen && utf16_src[j] != 0x0000; j++) {
343 1.50 manu n = wput_utf8(c, r, le16toh(utf16_src[j]));
344 1.50 manu if (n == 0)
345 1.50 manu break;
346 1.50 manu c += n; r -= n;
347 1.50 manu }
348 1.50 manu *c = '\0';
349 1.50 manu
350 1.50 manu return;
351 1.50 manu }
352 1.50 manu #endif
353 1.50 manu
354 1.36 jakllsch static int
355 1.50 manu check_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t sector)
356 1.36 jakllsch {
357 1.36 jakllsch struct gpt_hdr gpth;
358 1.36 jakllsch const struct gpt_ent *ep;
359 1.36 jakllsch const struct uuid *u;
360 1.36 jakllsch daddr_t entblk;
361 1.36 jakllsch size_t size;
362 1.36 jakllsch uint32_t crc;
363 1.36 jakllsch int sectors;
364 1.36 jakllsch int entries;
365 1.36 jakllsch int entry;
366 1.36 jakllsch int i, j;
367 1.36 jakllsch
368 1.36 jakllsch /* read in gpt_hdr sector */
369 1.36 jakllsch if (readsects(&d->ll, sector, 1, d->buf, 1)) {
370 1.36 jakllsch #ifdef DISK_DEBUG
371 1.36 jakllsch printf("Error reading GPT header at %"PRId64"\n", sector);
372 1.36 jakllsch #endif
373 1.36 jakllsch return EIO;
374 1.36 jakllsch }
375 1.36 jakllsch
376 1.43 christos memcpy(&gpth, d->buf, sizeof(gpth));
377 1.36 jakllsch
378 1.36 jakllsch if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
379 1.36 jakllsch return -1;
380 1.36 jakllsch
381 1.36 jakllsch crc = gpth.hdr_crc_self;
382 1.36 jakllsch gpth.hdr_crc_self = 0;
383 1.36 jakllsch gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
384 1.36 jakllsch if (gpth.hdr_crc_self != crc) {
385 1.36 jakllsch return -1;
386 1.36 jakllsch }
387 1.36 jakllsch
388 1.50 manu if (gpth.hdr_lba_self + rf_offset != sector)
389 1.36 jakllsch return -1;
390 1.36 jakllsch
391 1.36 jakllsch #ifdef _STANDALONE
392 1.36 jakllsch bi_wedge.matchblk = sector;
393 1.36 jakllsch bi_wedge.matchnblks = 1;
394 1.36 jakllsch
395 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
396 1.36 jakllsch #endif
397 1.36 jakllsch
398 1.36 jakllsch sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
399 1.36 jakllsch entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
400 1.50 manu entblk = gpth.hdr_lba_table + rf_offset;
401 1.36 jakllsch crc = crc32(0, NULL, 0);
402 1.36 jakllsch
403 1.36 jakllsch j = 0;
404 1.36 jakllsch ep = (const struct gpt_ent *)d->buf;
405 1.36 jakllsch
406 1.36 jakllsch for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
407 1.36 jakllsch size = MIN(sizeof(d->buf),
408 1.36 jakllsch (gpth.hdr_entries - entry) * gpth.hdr_entsz);
409 1.36 jakllsch entries = size / gpth.hdr_entsz;
410 1.36 jakllsch sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
411 1.36 jakllsch if (readsects(&d->ll, entblk, sectors, d->buf, 1))
412 1.36 jakllsch return -1;
413 1.36 jakllsch entblk += sectors;
414 1.36 jakllsch crc = crc32(crc, (const void *)d->buf, size);
415 1.36 jakllsch
416 1.46 nonaka for (i = 0; j < BIOSDISKNPART && i < entries; i++) {
417 1.36 jakllsch u = (const struct uuid *)ep[i].ent_type;
418 1.36 jakllsch if (!guid_is_nil(u)) {
419 1.36 jakllsch d->part[j].offset = ep[i].ent_lba_start;
420 1.36 jakllsch d->part[j].size = ep[i].ent_lba_end -
421 1.36 jakllsch ep[i].ent_lba_start + 1;
422 1.36 jakllsch if (guid_is_equal(u, &GET_nbsd_ffs))
423 1.36 jakllsch d->part[j].fstype = FS_BSDFFS;
424 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_lfs))
425 1.36 jakllsch d->part[j].fstype = FS_BSDLFS;
426 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_raid))
427 1.36 jakllsch d->part[j].fstype = FS_RAID;
428 1.36 jakllsch else if (guid_is_equal(u, &GET_nbsd_swap))
429 1.36 jakllsch d->part[j].fstype = FS_SWAP;
430 1.46 nonaka else if (guid_is_equal(u, &GET_nbsd_ccd))
431 1.46 nonaka d->part[j].fstype = FS_CCD;
432 1.46 nonaka else if (guid_is_equal(u, &GET_nbsd_cgd))
433 1.46 nonaka d->part[j].fstype = FS_CGD;
434 1.36 jakllsch else
435 1.36 jakllsch d->part[j].fstype = FS_OTHER;
436 1.50 manu #ifndef NO_GPT
437 1.46 nonaka for (int k = 0;
438 1.46 nonaka k < __arraycount(gpt_parts);
439 1.46 nonaka k++) {
440 1.46 nonaka if (guid_is_equal(u, gpt_parts[k].guid))
441 1.46 nonaka d->part[j].guid = &gpt_parts[k];
442 1.46 nonaka }
443 1.46 nonaka d->part[j].attr = ep[i].ent_attr;
444 1.50 manu
445 1.50 manu d->part[j].part_name =
446 1.50 manu alloc(BIOSDISK_PART_NAME_LEN);
447 1.50 manu part_name_utf8(ep[i].ent_name,
448 1.50 manu sizeof(ep[i].ent_name),
449 1.50 manu d->part[j].part_name,
450 1.50 manu BIOSDISK_PART_NAME_LEN);
451 1.46 nonaka #endif
452 1.46 nonaka j++;
453 1.36 jakllsch }
454 1.36 jakllsch }
455 1.36 jakllsch
456 1.36 jakllsch }
457 1.36 jakllsch
458 1.36 jakllsch if (crc != gpth.hdr_crc_table) {
459 1.36 jakllsch #ifdef DISK_DEBUG
460 1.36 jakllsch printf("GPT table CRC invalid\n");
461 1.36 jakllsch #endif
462 1.36 jakllsch return -1;
463 1.36 jakllsch }
464 1.36 jakllsch
465 1.36 jakllsch return 0;
466 1.36 jakllsch }
467 1.36 jakllsch
468 1.36 jakllsch static int
469 1.50 manu read_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t rf_size)
470 1.36 jakllsch {
471 1.36 jakllsch struct biosdisk_extinfo ed;
472 1.36 jakllsch daddr_t gptsector[2];
473 1.36 jakllsch int i, error;
474 1.36 jakllsch
475 1.37 jakllsch if (d->ll.type != BIOSDISK_TYPE_HD)
476 1.37 jakllsch /* No GPT on floppy and CD */
477 1.37 jakllsch return -1;
478 1.37 jakllsch
479 1.50 manu if (rf_offset && rf_size) {
480 1.50 manu gptsector[0] = rf_offset + GPT_HDR_BLKNO;
481 1.50 manu gptsector[1] = rf_offset + rf_size - 1;
482 1.36 jakllsch } else {
483 1.50 manu gptsector[0] = GPT_HDR_BLKNO;
484 1.50 manu if (set_geometry(&d->ll, &ed) == 0 &&
485 1.50 manu d->ll.flags & BIOSDISK_INT13EXT) {
486 1.50 manu gptsector[1] = ed.totsec - 1;
487 1.50 manu /* Sanity check values returned from BIOS */
488 1.50 manu if (ed.sbytes >= 512 &&
489 1.50 manu (ed.sbytes & (ed.sbytes - 1)) == 0)
490 1.50 manu d->ll.secsize = ed.sbytes;
491 1.50 manu } else {
492 1.36 jakllsch #ifdef DISK_DEBUG
493 1.50 manu printf("Unable to determine extended disk geometry - "
494 1.50 manu "using CHS\n");
495 1.36 jakllsch #endif
496 1.50 manu /* at least try some other reasonable values then */
497 1.50 manu gptsector[1] = d->ll.chs_sectors - 1;
498 1.50 manu }
499 1.36 jakllsch }
500 1.36 jakllsch
501 1.36 jakllsch for (i = 0; i < __arraycount(gptsector); i++) {
502 1.50 manu error = check_gpt(d, rf_offset, gptsector[i]);
503 1.36 jakllsch if (error == 0)
504 1.36 jakllsch break;
505 1.36 jakllsch }
506 1.36 jakllsch
507 1.36 jakllsch if (i >= __arraycount(gptsector)) {
508 1.36 jakllsch memset(d->part, 0, sizeof(d->part));
509 1.36 jakllsch return -1;
510 1.36 jakllsch }
511 1.36 jakllsch
512 1.44 jakllsch #ifndef USE_SECONDARY_GPT
513 1.44 jakllsch if (i > 0) {
514 1.44 jakllsch #ifdef DISK_DEBUG
515 1.44 jakllsch printf("ignoring valid secondary GPT\n");
516 1.44 jakllsch #endif
517 1.44 jakllsch return -1;
518 1.44 jakllsch }
519 1.44 jakllsch #endif
520 1.44 jakllsch
521 1.36 jakllsch #ifdef DISK_DEBUG
522 1.36 jakllsch printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
523 1.36 jakllsch #endif
524 1.36 jakllsch return 0;
525 1.36 jakllsch }
526 1.36 jakllsch #endif /* !NO_GPT */
527 1.36 jakllsch
528 1.16 dsl #ifndef NO_DISKLABEL
529 1.37 jakllsch static void
530 1.37 jakllsch ingest_label(struct biosdisk *d, struct disklabel *lp)
531 1.37 jakllsch {
532 1.37 jakllsch int part;
533 1.37 jakllsch
534 1.37 jakllsch memset(d->part, 0, sizeof(d->part));
535 1.37 jakllsch
536 1.37 jakllsch for (part = 0; part < lp->d_npartitions; part++) {
537 1.37 jakllsch if (lp->d_partitions[part].p_size == 0)
538 1.37 jakllsch continue;
539 1.37 jakllsch if (lp->d_partitions[part].p_fstype == FS_UNUSED)
540 1.37 jakllsch continue;
541 1.37 jakllsch d->part[part].fstype = lp->d_partitions[part].p_fstype;
542 1.37 jakllsch d->part[part].offset = lp->d_partitions[part].p_offset;
543 1.37 jakllsch d->part[part].size = lp->d_partitions[part].p_size;
544 1.37 jakllsch }
545 1.37 jakllsch }
546 1.37 jakllsch
547 1.16 dsl static int
548 1.32 jakllsch check_label(struct biosdisk *d, daddr_t sector)
549 1.17 dsl {
550 1.17 dsl struct disklabel *lp;
551 1.17 dsl
552 1.17 dsl /* find partition in NetBSD disklabel */
553 1.17 dsl if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
554 1.17 dsl #ifdef DISK_DEBUG
555 1.17 dsl printf("Error reading disklabel\n");
556 1.17 dsl #endif
557 1.17 dsl return EIO;
558 1.17 dsl }
559 1.17 dsl lp = (struct disklabel *) (d->buf + LABELOFFSET);
560 1.17 dsl if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
561 1.17 dsl #ifdef DISK_DEBUG
562 1.33 jakllsch printf("warning: no disklabel in sector %"PRId64"\n", sector);
563 1.17 dsl #endif
564 1.17 dsl return -1;
565 1.17 dsl }
566 1.17 dsl
567 1.37 jakllsch ingest_label(d, lp);
568 1.36 jakllsch
569 1.39 gsutre bi_disk.labelsector = sector + LABELSECTOR;
570 1.36 jakllsch bi_disk.label.type = lp->d_type;
571 1.36 jakllsch memcpy(bi_disk.label.packname, lp->d_packname, 16);
572 1.36 jakllsch bi_disk.label.checksum = lp->d_checksum;
573 1.36 jakllsch
574 1.39 gsutre bi_wedge.matchblk = sector + LABELSECTOR;
575 1.36 jakllsch bi_wedge.matchnblks = 1;
576 1.36 jakllsch
577 1.36 jakllsch md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
578 1.36 jakllsch
579 1.17 dsl return 0;
580 1.17 dsl }
581 1.17 dsl
582 1.17 dsl static int
583 1.40 christos read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
584 1.40 christos int this_ext, daddr_t sector)
585 1.40 christos {
586 1.40 christos struct mbr_partition mbr[MBR_PART_COUNT];
587 1.40 christos int i;
588 1.40 christos int typ;
589 1.40 christos struct partition *p;
590 1.40 christos
591 1.40 christos if (readsects(&d->ll, sector, 1, d->buf, 0)) {
592 1.40 christos #ifdef DISK_DEBUG
593 1.42 tsutsui printf("Error reading MFS sector %"PRId64"\n", sector);
594 1.40 christos #endif
595 1.40 christos return EIO;
596 1.40 christos }
597 1.40 christos if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
598 1.40 christos return -1;
599 1.40 christos }
600 1.43 christos memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
601 1.40 christos for (i = 0; i < MBR_PART_COUNT; i++) {
602 1.40 christos typ = mbr[i].mbrp_type;
603 1.40 christos if (typ == 0)
604 1.40 christos continue;
605 1.40 christos sector = this_ext + mbr[i].mbrp_start;
606 1.40 christos if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
607 1.40 christos continue;
608 1.40 christos p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
609 1.40 christos p->p_offset = sector;
610 1.40 christos p->p_size = mbr[i].mbrp_size;
611 1.40 christos p->p_fstype = xlat_mbr_fstype(typ);
612 1.40 christos }
613 1.40 christos return 0;
614 1.40 christos }
615 1.40 christos
616 1.47 nonaka #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
617 1.47 nonaka static int
618 1.47 nonaka check_cd9660(struct biosdisk *d)
619 1.47 nonaka {
620 1.47 nonaka struct biosdisk_extinfo ed;
621 1.47 nonaka struct iso_primary_descriptor *vd;
622 1.47 nonaka daddr_t bno;
623 1.47 nonaka
624 1.47 nonaka for (bno = 16;; bno++) {
625 1.47 nonaka if (readsects(&d->ll, bno, 1, d->buf, 0))
626 1.47 nonaka return -1;
627 1.47 nonaka vd = (struct iso_primary_descriptor *)d->buf;
628 1.47 nonaka if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
629 1.47 nonaka return -1;
630 1.47 nonaka if (isonum_711(vd->type) == ISO_VD_END)
631 1.47 nonaka return -1;
632 1.47 nonaka if (isonum_711(vd->type) == ISO_VD_PRIMARY)
633 1.47 nonaka break;
634 1.47 nonaka }
635 1.47 nonaka if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
636 1.47 nonaka return -1;
637 1.47 nonaka
638 1.47 nonaka if (set_geometry(&d->ll, &ed))
639 1.47 nonaka return -1;
640 1.47 nonaka
641 1.47 nonaka memset(d->part, 0, sizeof(d->part));
642 1.47 nonaka d->part[0].fstype = FS_ISO9660;
643 1.47 nonaka d->part[0].offset = 0;
644 1.47 nonaka d->part[0].size = ed.totsec;
645 1.47 nonaka return 0;
646 1.47 nonaka }
647 1.47 nonaka #endif
648 1.47 nonaka
649 1.40 christos static int
650 1.50 manu read_label(struct biosdisk *d, daddr_t offset)
651 1.16 dsl {
652 1.17 dsl struct disklabel dflt_lbl;
653 1.18 lukem struct mbr_partition mbr[MBR_PART_COUNT];
654 1.17 dsl struct partition *p;
655 1.42 tsutsui uint32_t sector;
656 1.42 tsutsui int i;
657 1.17 dsl int error;
658 1.17 dsl int typ;
659 1.42 tsutsui uint32_t ext_base, this_ext, next_ext;
660 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
661 1.17 dsl int sector_386bsd = -1;
662 1.17 dsl #endif
663 1.17 dsl
664 1.23 junyoung memset(&dflt_lbl, 0, sizeof(dflt_lbl));
665 1.17 dsl dflt_lbl.d_npartitions = 8;
666 1.5 drochner
667 1.7 drochner d->boff = 0;
668 1.7 drochner
669 1.24 junyoung if (d->ll.type != BIOSDISK_TYPE_HD)
670 1.24 junyoung /* No label on floppy and CD */
671 1.16 dsl return -1;
672 1.7 drochner
673 1.3 thorpej /*
674 1.7 drochner * find NetBSD Partition in DOS partition table
675 1.7 drochner * XXX check magic???
676 1.3 thorpej */
677 1.50 manu ext_base = offset;
678 1.50 manu next_ext = offset;
679 1.17 dsl for (;;) {
680 1.17 dsl this_ext = ext_base + next_ext;
681 1.50 manu next_ext = offset;
682 1.17 dsl if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
683 1.1 perry #ifdef DISK_DEBUG
684 1.42 tsutsui printf("error reading MBR sector %u\n", this_ext);
685 1.17 dsl #endif
686 1.17 dsl return EIO;
687 1.17 dsl }
688 1.43 christos memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
689 1.17 dsl /* Look for NetBSD partition ID */
690 1.18 lukem for (i = 0; i < MBR_PART_COUNT; i++) {
691 1.18 lukem typ = mbr[i].mbrp_type;
692 1.17 dsl if (typ == 0)
693 1.17 dsl continue;
694 1.17 dsl sector = this_ext + mbr[i].mbrp_start;
695 1.27 dsl #ifdef DISK_DEBUG
696 1.42 tsutsui printf("ptn type %d in sector %u\n", typ, sector);
697 1.27 dsl #endif
698 1.40 christos if (typ == MBR_PTYPE_MINIX_14B) {
699 1.40 christos if (!read_minix_subp(d, &dflt_lbl,
700 1.40 christos this_ext, sector)) {
701 1.40 christos /* Don't add "container" partition */
702 1.40 christos continue;
703 1.40 christos }
704 1.40 christos }
705 1.17 dsl if (typ == MBR_PTYPE_NETBSD) {
706 1.17 dsl error = check_label(d, sector);
707 1.17 dsl if (error >= 0)
708 1.17 dsl return error;
709 1.17 dsl }
710 1.17 dsl if (MBR_IS_EXTENDED(typ)) {
711 1.50 manu next_ext = mbr[i].mbrp_start + offset;
712 1.17 dsl continue;
713 1.17 dsl }
714 1.17 dsl #ifdef COMPAT_386BSD_MBRPART
715 1.50 manu if (this_ext == offset && typ == MBR_PTYPE_386BSD)
716 1.17 dsl sector_386bsd = sector;
717 1.1 perry #endif
718 1.50 manu if (this_ext != offset) {
719 1.17 dsl if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
720 1.17 dsl continue;
721 1.17 dsl p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
722 1.17 dsl } else
723 1.17 dsl p = &dflt_lbl.d_partitions[i];
724 1.17 dsl p->p_offset = sector;
725 1.17 dsl p->p_size = mbr[i].mbrp_size;
726 1.17 dsl p->p_fstype = xlat_mbr_fstype(typ);
727 1.17 dsl }
728 1.50 manu if (next_ext == offset)
729 1.3 thorpej break;
730 1.50 manu if (ext_base == offset) {
731 1.17 dsl ext_base = next_ext;
732 1.50 manu next_ext = offset;
733 1.3 thorpej }
734 1.17 dsl }
735 1.17 dsl
736 1.50 manu sector = offset;
737 1.8 drochner #ifdef COMPAT_386BSD_MBRPART
738 1.17 dsl if (sector_386bsd != -1) {
739 1.17 dsl printf("old BSD partition ID!\n");
740 1.17 dsl sector = sector_386bsd;
741 1.8 drochner }
742 1.8 drochner #endif
743 1.7 drochner
744 1.17 dsl /*
745 1.17 dsl * One of two things:
746 1.17 dsl * 1. no MBR
747 1.17 dsl * 2. no NetBSD partition in MBR
748 1.17 dsl *
749 1.17 dsl * We simply default to "start of disk" in this case and
750 1.17 dsl * press on.
751 1.17 dsl */
752 1.17 dsl error = check_label(d, sector);
753 1.17 dsl if (error >= 0)
754 1.17 dsl return error;
755 1.16 dsl
756 1.47 nonaka #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
757 1.47 nonaka /* Check CD/DVD */
758 1.47 nonaka error = check_cd9660(d);
759 1.47 nonaka if (error >= 0)
760 1.47 nonaka return error;
761 1.47 nonaka #endif
762 1.47 nonaka
763 1.17 dsl /*
764 1.17 dsl * Nothing at start of disk, return info from mbr partitions.
765 1.17 dsl */
766 1.17 dsl /* XXX fill it to make checksum match kernel one */
767 1.17 dsl dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
768 1.37 jakllsch ingest_label(d, &dflt_lbl);
769 1.27 dsl return 0;
770 1.16 dsl }
771 1.16 dsl #endif /* NO_DISKLABEL */
772 1.16 dsl
773 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
774 1.36 jakllsch static int
775 1.50 manu read_partitions(struct biosdisk *d, daddr_t offset, daddr_t size)
776 1.36 jakllsch {
777 1.36 jakllsch int error;
778 1.36 jakllsch
779 1.36 jakllsch error = -1;
780 1.36 jakllsch
781 1.36 jakllsch #ifndef NO_GPT
782 1.50 manu error = read_gpt(d, offset, size);
783 1.36 jakllsch if (error == 0)
784 1.36 jakllsch return 0;
785 1.36 jakllsch
786 1.36 jakllsch #endif
787 1.36 jakllsch #ifndef NO_DISKLABEL
788 1.50 manu error = read_label(d, offset);
789 1.36 jakllsch
790 1.36 jakllsch #endif
791 1.36 jakllsch return error;
792 1.36 jakllsch }
793 1.36 jakllsch #endif
794 1.36 jakllsch
795 1.50 manu #ifndef NO_RAIDFRAME
796 1.50 manu static void
797 1.50 manu raidframe_probe(struct raidframe *raidframe, int *raidframe_count,
798 1.50 manu struct biosdisk *d, int part)
799 1.50 manu {
800 1.50 manu int i = *raidframe_count;
801 1.50 manu struct RF_ComponentLabel_s label;
802 1.50 manu daddr_t offset;
803 1.50 manu
804 1.50 manu if (i + 1 > RAIDFRAME_NDEV)
805 1.50 manu return;
806 1.50 manu
807 1.50 manu offset = d->part[part].offset;
808 1.50 manu if ((biosdisk_read_raidframe(d->ll.dev, offset, &label)) != 0)
809 1.50 manu return;
810 1.50 manu
811 1.50 manu if (label.version != RF_COMPONENT_LABEL_VERSION)
812 1.50 manu printf("Unexpected raidframe label version\n");
813 1.50 manu
814 1.50 manu raidframe[i].last_unit = label.last_unit;
815 1.50 manu raidframe[i].serial = label.serial_number;
816 1.50 manu raidframe[i].biosdev = d->ll.dev;
817 1.50 manu raidframe[i].parent_part = part;
818 1.50 manu #ifndef NO_GPT
819 1.50 manu if (d->part[part].part_name)
820 1.50 manu strlcpy(raidframe[i].parent_name,
821 1.50 manu d->part[part].part_name, MAXDEVNAME);
822 1.50 manu else
823 1.50 manu raidframe[i].parent_name[0] = '\0';
824 1.50 manu #endif
825 1.50 manu raidframe[i].offset = offset;
826 1.50 manu raidframe[i].size = label.__numBlocks;
827 1.50 manu
828 1.50 manu (*raidframe_count)++;
829 1.50 manu
830 1.50 manu return;
831 1.50 manu }
832 1.50 manu #endif
833 1.50 manu
834 1.29 jmcneill void
835 1.29 jmcneill biosdisk_probe(void)
836 1.29 jmcneill {
837 1.50 manu struct biosdisk *d;
838 1.29 jmcneill struct biosdisk_extinfo ed;
839 1.50 manu #ifndef NO_RAIDFRAME
840 1.50 manu struct raidframe raidframe[RAIDFRAME_NDEV];
841 1.50 manu int raidframe_count = 0;
842 1.50 manu #endif
843 1.29 jmcneill uint64_t size;
844 1.36 jakllsch int first;
845 1.29 jmcneill int i;
846 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
847 1.36 jakllsch int part;
848 1.36 jakllsch #endif
849 1.29 jmcneill
850 1.29 jmcneill for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
851 1.29 jmcneill first = 1;
852 1.50 manu d = alloc(sizeof(*d));
853 1.50 manu if (d == NULL) {
854 1.50 manu printf("Out of memory\n");
855 1.50 manu return;
856 1.50 manu }
857 1.51 kamil memset(d, 0, sizeof(*d));
858 1.29 jmcneill memset(&ed, 0, sizeof(ed));
859 1.29 jmcneill if (i >= MAX_BIOSDISKS)
860 1.50 manu d->ll.dev = 0x00 + i - MAX_BIOSDISKS; /* fd */
861 1.29 jmcneill else
862 1.50 manu d->ll.dev = 0x80 + i; /* hd/cd */
863 1.50 manu if (set_geometry(&d->ll, &ed))
864 1.50 manu goto next_disk;
865 1.30 jmcneill printf("disk ");
866 1.50 manu switch (d->ll.type) {
867 1.29 jmcneill case BIOSDISK_TYPE_CD:
868 1.30 jmcneill printf("cd0\n cd0a\n");
869 1.29 jmcneill break;
870 1.29 jmcneill case BIOSDISK_TYPE_FD:
871 1.50 manu printf("fd%d\n", d->ll.dev & 0x7f);
872 1.50 manu printf(" fd%da\n", d->ll.dev & 0x7f);
873 1.29 jmcneill break;
874 1.29 jmcneill case BIOSDISK_TYPE_HD:
875 1.50 manu printf("hd%d", d->ll.dev & 0x7f);
876 1.50 manu if (d->ll.flags & BIOSDISK_INT13EXT) {
877 1.29 jmcneill printf(" size ");
878 1.29 jmcneill size = ed.totsec * ed.sbytes;
879 1.29 jmcneill if (size >= (10ULL * 1024 * 1024 * 1024))
880 1.33 jakllsch printf("%"PRIu64" GB",
881 1.29 jmcneill size / (1024 * 1024 * 1024));
882 1.29 jmcneill else
883 1.33 jakllsch printf("%"PRIu64" MB",
884 1.29 jmcneill size / (1024 * 1024));
885 1.29 jmcneill }
886 1.29 jmcneill printf("\n");
887 1.29 jmcneill break;
888 1.29 jmcneill }
889 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
890 1.50 manu if (d->ll.type != BIOSDISK_TYPE_HD)
891 1.50 manu goto next_disk;
892 1.36 jakllsch
893 1.50 manu if (read_partitions(d, 0, 0) != 0)
894 1.50 manu goto next_disk;
895 1.36 jakllsch
896 1.36 jakllsch for (part = 0; part < BIOSDISKNPART; part++) {
897 1.50 manu if (d->part[part].size == 0)
898 1.29 jmcneill continue;
899 1.50 manu if (d->part[part].fstype == FS_UNUSED)
900 1.29 jmcneill continue;
901 1.50 manu #ifndef NO_RAIDFRAME
902 1.50 manu if (d->part[part].fstype == FS_RAID)
903 1.50 manu raidframe_probe(raidframe,
904 1.50 manu &raidframe_count, d, part);
905 1.50 manu #endif
906 1.29 jmcneill if (first) {
907 1.29 jmcneill printf(" ");
908 1.29 jmcneill first = 0;
909 1.29 jmcneill }
910 1.50 manu #ifndef NO_GPT
911 1.50 manu if (d->part[part].part_name != NULL)
912 1.50 manu printf(" NAME=%s(", d->part[part].part_name);
913 1.50 manu else
914 1.50 manu #endif
915 1.50 manu printf(" hd%d%c(", d->ll.dev & 0x7f, part + 'a');
916 1.50 manu
917 1.50 manu #ifndef NO_GPT
918 1.50 manu if (d->part[part].guid != NULL)
919 1.50 manu printf("%s", d->part[part].guid->name);
920 1.46 nonaka else
921 1.46 nonaka #endif
922 1.50 manu
923 1.50 manu if (d->part[part].fstype < FSMAXTYPES)
924 1.29 jmcneill printf("%s",
925 1.50 manu fstypenames[d->part[part].fstype]);
926 1.29 jmcneill else
927 1.50 manu printf("%d", d->part[part].fstype);
928 1.29 jmcneill printf(")");
929 1.29 jmcneill }
930 1.36 jakllsch #endif
931 1.29 jmcneill if (first == 0)
932 1.29 jmcneill printf("\n");
933 1.50 manu
934 1.50 manu next_disk:
935 1.50 manu dealloc_biosdisk(d);
936 1.29 jmcneill }
937 1.50 manu
938 1.50 manu #ifndef NO_RAIDFRAME
939 1.50 manu for (i = 0; i < raidframe_count; i++) {
940 1.50 manu size_t secsize;
941 1.50 manu
942 1.50 manu if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
943 1.50 manu printf("Out of memory\n");
944 1.50 manu return;
945 1.50 manu }
946 1.50 manu
947 1.50 manu secsize = d->ll.secsize;
948 1.50 manu
949 1.50 manu printf("raidframe raid%d serial %d in ",
950 1.50 manu raidframe[i].last_unit, raidframe[i].serial);
951 1.50 manu #ifndef NO_GPT
952 1.50 manu if (raidframe[i].parent_name[0])
953 1.50 manu printf("NAME=%s size ", raidframe[i].parent_name);
954 1.50 manu else
955 1.50 manu #endif
956 1.50 manu printf("hd%d%c size ", d->ll.dev & 0x7f,
957 1.50 manu raidframe[i].parent_part + 'a');
958 1.50 manu if (raidframe[i].size >= (10ULL * 1024 * 1024 * 1024 / secsize))
959 1.50 manu printf("%"PRIu64" GB",
960 1.50 manu raidframe[i].size / (1024 * 1024 * 1024 / secsize));
961 1.50 manu else
962 1.50 manu printf("%"PRIu64" MB",
963 1.50 manu raidframe[i].size / (1024 * 1024 / secsize));
964 1.50 manu printf("\n");
965 1.50 manu
966 1.50 manu if (read_partitions(d,
967 1.50 manu raidframe[i].offset + RF_PROTECTED_SECTORS,
968 1.50 manu raidframe[i].size) != 0)
969 1.50 manu goto next_raidrame;
970 1.50 manu
971 1.50 manu first = 1;
972 1.50 manu for (part = 0; part < BIOSDISKNPART; part++) {
973 1.50 manu #ifndef NO_GPT
974 1.50 manu bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
975 1.50 manu #else
976 1.50 manu bool bootme = 0;
977 1.50 manu #endif
978 1.50 manu
979 1.50 manu if (d->part[part].size == 0)
980 1.50 manu continue;
981 1.50 manu if (d->part[part].fstype == FS_UNUSED)
982 1.50 manu continue;
983 1.50 manu if (d->part[part].fstype == FS_RAID)
984 1.50 manu continue;
985 1.50 manu if (first) {
986 1.50 manu printf(" ");
987 1.50 manu first = 0;
988 1.50 manu }
989 1.50 manu #ifndef NO_GPT
990 1.50 manu if (d->part[part].part_name != NULL)
991 1.50 manu printf(" NAME=%s(", d->part[part].part_name);
992 1.50 manu else
993 1.50 manu #endif
994 1.50 manu printf(" raid%d%c(", raidframe[i].last_unit,
995 1.50 manu part + 'a');
996 1.50 manu #ifndef NO_GPT
997 1.50 manu if (d->part[part].guid != NULL)
998 1.50 manu printf("%s", d->part[part].guid->name);
999 1.50 manu else
1000 1.50 manu #endif
1001 1.50 manu if (d->part[part].fstype < FSMAXTYPES)
1002 1.50 manu printf("%s",
1003 1.50 manu fstypenames[d->part[part].fstype]);
1004 1.50 manu else
1005 1.50 manu printf("%d", d->part[part].fstype);
1006 1.50 manu printf("%s)", bootme ? ", bootme" : "");
1007 1.50 manu }
1008 1.50 manu
1009 1.50 manu next_raidrame:
1010 1.50 manu if (first == 0)
1011 1.50 manu printf("\n");
1012 1.50 manu
1013 1.50 manu dealloc_biosdisk(d);
1014 1.50 manu }
1015 1.50 manu #endif
1016 1.29 jmcneill }
1017 1.29 jmcneill
1018 1.16 dsl /* Determine likely partition for possible sector number of dos
1019 1.17 dsl * partition.
1020 1.17 dsl */
1021 1.16 dsl
1022 1.24 junyoung int
1023 1.50 manu biosdisk_findpartition(int biosdev, daddr_t sector,
1024 1.50 manu int *partition, const char **part_name)
1025 1.16 dsl {
1026 1.36 jakllsch #if defined(NO_DISKLABEL) && defined(NO_GPT)
1027 1.50 manu *partition = 0;
1028 1.50 manu *part_name = NULL;
1029 1.16 dsl return 0;
1030 1.16 dsl #else
1031 1.50 manu int i;
1032 1.16 dsl struct biosdisk *d;
1033 1.50 manu int biosboot_sector_part = -1;
1034 1.50 manu int bootable_fs_part = -1;
1035 1.50 manu int boot_part = 0;
1036 1.50 manu #ifndef NO_GPT
1037 1.50 manu int gpt_bootme_part = -1;
1038 1.50 manu static char namebuf[MAXDEVNAME + 1];
1039 1.46 nonaka #endif
1040 1.46 nonaka
1041 1.27 dsl #ifdef DISK_DEBUG
1042 1.33 jakllsch printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
1043 1.27 dsl #endif
1044 1.16 dsl
1045 1.50 manu /* default ot first partition */
1046 1.50 manu *partition = 0;
1047 1.50 manu *part_name = NULL;
1048 1.50 manu
1049 1.16 dsl /* Look for netbsd partition that is the dos boot one */
1050 1.16 dsl d = alloc_biosdisk(biosdev);
1051 1.17 dsl if (d == NULL)
1052 1.50 manu return -1;
1053 1.17 dsl
1054 1.50 manu if (read_partitions(d, 0, 0) == 0) {
1055 1.50 manu for (i = 0; i < BIOSDISKNPART; i++) {
1056 1.50 manu if (d->part[i].fstype == FS_UNUSED)
1057 1.16 dsl continue;
1058 1.50 manu
1059 1.50 manu if (d->part[i].offset == sector &&
1060 1.50 manu biosboot_sector_part == -1)
1061 1.50 manu biosboot_sector_part = i;
1062 1.50 manu
1063 1.50 manu #ifndef NO_GPT
1064 1.50 manu if (d->part[i].attr & GPT_ENT_ATTR_BOOTME &&
1065 1.50 manu gpt_bootme_part == -1)
1066 1.50 manu gpt_bootme_part = i;
1067 1.50 manu #endif
1068 1.50 manu switch (d->part[i].fstype) {
1069 1.48 nonaka case FS_BSDFFS:
1070 1.48 nonaka case FS_BSDLFS:
1071 1.48 nonaka case FS_RAID:
1072 1.48 nonaka case FS_CCD:
1073 1.48 nonaka case FS_CGD:
1074 1.48 nonaka case FS_ISO9660:
1075 1.50 manu if (bootable_fs_part == -1)
1076 1.50 manu bootable_fs_part = i;
1077 1.48 nonaka break;
1078 1.46 nonaka
1079 1.48 nonaka default:
1080 1.46 nonaka break;
1081 1.46 nonaka }
1082 1.50 manu }
1083 1.50 manu
1084 1.50 manu #ifndef NO_GPT
1085 1.50 manu if (gpt_bootme_part != -1)
1086 1.50 manu boot_part = gpt_bootme_part;
1087 1.50 manu else
1088 1.46 nonaka #endif
1089 1.50 manu if (biosboot_sector_part != -1)
1090 1.50 manu boot_part = biosboot_sector_part;
1091 1.50 manu else if (bootable_fs_part != -1)
1092 1.50 manu boot_part = bootable_fs_part;
1093 1.50 manu else
1094 1.50 manu boot_part = 0;
1095 1.50 manu
1096 1.50 manu *partition = boot_part;
1097 1.50 manu #ifndef NO_GPT
1098 1.50 manu if (part_name && d->part[boot_part].part_name) {
1099 1.50 manu strlcpy(namebuf, d->part[boot_part].part_name,
1100 1.50 manu BIOSDISK_PART_NAME_LEN);
1101 1.50 manu *part_name = namebuf;
1102 1.16 dsl }
1103 1.46 nonaka #endif
1104 1.16 dsl }
1105 1.16 dsl
1106 1.50 manu dealloc_biosdisk(d);
1107 1.50 manu return 0;
1108 1.36 jakllsch #endif /* NO_DISKLABEL && NO_GPT */
1109 1.36 jakllsch }
1110 1.36 jakllsch
1111 1.47 nonaka int
1112 1.50 manu biosdisk_readpartition(int biosdev, daddr_t offset, daddr_t size,
1113 1.50 manu struct biosdisk_partition **partpp, int *rnum)
1114 1.47 nonaka {
1115 1.47 nonaka #if defined(NO_DISKLABEL) && defined(NO_GPT)
1116 1.47 nonaka return ENOTSUP;
1117 1.47 nonaka #else
1118 1.47 nonaka struct biosdisk *d;
1119 1.47 nonaka struct biosdisk_partition *part;
1120 1.47 nonaka int rv;
1121 1.47 nonaka
1122 1.47 nonaka /* Look for netbsd partition that is the dos boot one */
1123 1.47 nonaka d = alloc_biosdisk(biosdev);
1124 1.47 nonaka if (d == NULL)
1125 1.47 nonaka return ENOMEM;
1126 1.47 nonaka
1127 1.50 manu if (read_partitions(d, offset, size)) {
1128 1.47 nonaka rv = EINVAL;
1129 1.47 nonaka goto out;
1130 1.47 nonaka }
1131 1.47 nonaka
1132 1.50 manu part = copy_biosdisk_part(d);
1133 1.47 nonaka if (part == NULL) {
1134 1.47 nonaka rv = ENOMEM;
1135 1.47 nonaka goto out;
1136 1.47 nonaka }
1137 1.47 nonaka
1138 1.47 nonaka *partpp = part;
1139 1.47 nonaka *rnum = (int)__arraycount(d->part);
1140 1.47 nonaka rv = 0;
1141 1.47 nonaka out:
1142 1.50 manu dealloc_biosdisk(d);
1143 1.50 manu return rv;
1144 1.50 manu #endif /* NO_DISKLABEL && NO_GPT */
1145 1.50 manu }
1146 1.50 manu
1147 1.50 manu #ifndef NO_RAIDFRAME
1148 1.50 manu int
1149 1.50 manu biosdisk_read_raidframe(int biosdev, daddr_t offset,
1150 1.50 manu struct RF_ComponentLabel_s *label)
1151 1.50 manu {
1152 1.50 manu #if defined(NO_DISKLABEL) && defined(NO_GPT)
1153 1.50 manu return ENOTSUP;
1154 1.50 manu #else
1155 1.50 manu struct biosdisk *d;
1156 1.50 manu struct biosdisk_extinfo ed;
1157 1.50 manu daddr_t size;
1158 1.50 manu int rv = -1;
1159 1.50 manu
1160 1.50 manu /* Look for netbsd partition that is the dos boot one */
1161 1.50 manu d = alloc_biosdisk(biosdev);
1162 1.50 manu if (d == NULL)
1163 1.50 manu goto out;
1164 1.50 manu
1165 1.50 manu if (d->ll.type != BIOSDISK_TYPE_HD)
1166 1.50 manu /* No raidframe on floppy and CD */
1167 1.50 manu goto out;
1168 1.50 manu
1169 1.50 manu if (set_geometry(&d->ll, &ed) != 0)
1170 1.50 manu goto out;
1171 1.50 manu
1172 1.50 manu /* Sanity check values returned from BIOS */
1173 1.50 manu if (ed.sbytes >= 512 &&
1174 1.50 manu (ed.sbytes & (ed.sbytes - 1)) == 0)
1175 1.50 manu d->ll.secsize = ed.sbytes;
1176 1.50 manu
1177 1.50 manu offset += (RF_COMPONENT_INFO_OFFSET / d->ll.secsize);
1178 1.50 manu size = roundup(sizeof(*label), d->ll.secsize) / d->ll.secsize;
1179 1.50 manu if (readsects(&d->ll, offset, size, d->buf, 0))
1180 1.50 manu goto out;
1181 1.50 manu memcpy(label, d->buf, sizeof(*label));
1182 1.50 manu rv = 0;
1183 1.50 manu out:
1184 1.50 manu if (d != NULL)
1185 1.50 manu dealloc_biosdisk(d);
1186 1.47 nonaka return rv;
1187 1.47 nonaka #endif /* NO_DISKLABEL && NO_GPT */
1188 1.47 nonaka }
1189 1.50 manu #endif /* NO_RAIDFRAME */
1190 1.47 nonaka
1191 1.36 jakllsch #ifdef _STANDALONE
1192 1.36 jakllsch static void
1193 1.36 jakllsch add_biosdisk_bootinfo(void)
1194 1.36 jakllsch {
1195 1.36 jakllsch if (bootinfo == NULL) {
1196 1.36 jakllsch return;
1197 1.36 jakllsch }
1198 1.36 jakllsch BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
1199 1.36 jakllsch BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
1200 1.36 jakllsch return;
1201 1.16 dsl }
1202 1.36 jakllsch #endif
1203 1.36 jakllsch
1204 1.50 manu #ifndef NO_GPT
1205 1.50 manu static daddr_t
1206 1.50 manu raidframe_part_offset(struct biosdisk *d, int part)
1207 1.50 manu {
1208 1.50 manu struct biosdisk raidframe;
1209 1.50 manu daddr_t rf_offset;
1210 1.50 manu daddr_t rf_size;
1211 1.50 manu int i, candidate;
1212 1.50 manu
1213 1.50 manu memset(&raidframe, 0, sizeof(raidframe));
1214 1.50 manu raidframe.ll = d->ll;
1215 1.50 manu
1216 1.50 manu rf_offset = d->part[part].offset + RF_PROTECTED_SECTORS;
1217 1.50 manu rf_size = d->part[part].size;
1218 1.50 manu if (read_gpt(&raidframe, rf_offset, rf_size) != 0)
1219 1.50 manu return RF_PROTECTED_SECTORS;
1220 1.50 manu
1221 1.50 manu candidate = 0;
1222 1.50 manu for (i = 0; i < BIOSDISKNPART; i++) {
1223 1.50 manu if (raidframe.part[i].size == 0)
1224 1.50 manu continue;
1225 1.50 manu if (raidframe.part[i].fstype == FS_UNUSED)
1226 1.50 manu continue;
1227 1.50 manu #ifndef NO_GPT
1228 1.50 manu if (raidframe.part[i].attr & GPT_ENT_ATTR_BOOTME)
1229 1.50 manu candidate = i;
1230 1.50 manu #endif
1231 1.50 manu }
1232 1.50 manu
1233 1.50 manu return RF_PROTECTED_SECTORS + raidframe.part[candidate].offset;
1234 1.50 manu }
1235 1.50 manu #endif
1236 1.50 manu
1237 1.22 junyoung int
1238 1.24 junyoung biosdisk_open(struct open_file *f, ...)
1239 1.23 junyoung /* struct open_file *f, int biosdev, int partition */
1240 1.16 dsl {
1241 1.16 dsl va_list ap;
1242 1.16 dsl struct biosdisk *d;
1243 1.23 junyoung int biosdev;
1244 1.16 dsl int partition;
1245 1.16 dsl int error = 0;
1246 1.16 dsl
1247 1.16 dsl va_start(ap, f);
1248 1.23 junyoung biosdev = va_arg(ap, int);
1249 1.23 junyoung d = alloc_biosdisk(biosdev);
1250 1.23 junyoung if (d == NULL) {
1251 1.16 dsl error = ENXIO;
1252 1.16 dsl goto out;
1253 1.16 dsl }
1254 1.16 dsl
1255 1.16 dsl partition = va_arg(ap, int);
1256 1.16 dsl bi_disk.biosdev = d->ll.dev;
1257 1.16 dsl bi_disk.partition = partition;
1258 1.16 dsl bi_disk.labelsector = -1;
1259 1.21 thorpej
1260 1.21 thorpej bi_wedge.biosdev = d->ll.dev;
1261 1.21 thorpej bi_wedge.matchblk = -1;
1262 1.16 dsl
1263 1.36 jakllsch #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
1264 1.50 manu error = read_partitions(d, 0, 0);
1265 1.16 dsl if (error == -1) {
1266 1.16 dsl error = 0;
1267 1.16 dsl goto nolabel;
1268 1.16 dsl }
1269 1.16 dsl if (error)
1270 1.16 dsl goto out;
1271 1.16 dsl
1272 1.36 jakllsch if (partition >= BIOSDISKNPART ||
1273 1.36 jakllsch d->part[partition].fstype == FS_UNUSED) {
1274 1.1 perry #ifdef DISK_DEBUG
1275 1.3 thorpej printf("illegal partition\n");
1276 1.1 perry #endif
1277 1.3 thorpej error = EPART;
1278 1.3 thorpej goto out;
1279 1.16 dsl }
1280 1.21 thorpej
1281 1.36 jakllsch d->boff = d->part[partition].offset;
1282 1.36 jakllsch
1283 1.36 jakllsch if (d->part[partition].fstype == FS_RAID)
1284 1.50 manu #ifndef NO_GPT
1285 1.50 manu d->boff += raidframe_part_offset(d, partition);
1286 1.50 manu #else
1287 1.36 jakllsch d->boff += RF_PROTECTED_SECTORS;
1288 1.50 manu #endif
1289 1.21 thorpej
1290 1.36 jakllsch #ifdef _STANDALONE
1291 1.36 jakllsch bi_wedge.startblk = d->part[partition].offset;
1292 1.36 jakllsch bi_wedge.nblks = d->part[partition].size;
1293 1.16 dsl #endif
1294 1.36 jakllsch
1295 1.7 drochner nolabel:
1296 1.36 jakllsch #endif
1297 1.1 perry #ifdef DISK_DEBUG
1298 1.34 jakllsch printf("partition @%"PRId64"\n", d->boff);
1299 1.1 perry #endif
1300 1.1 perry
1301 1.9 drochner #ifdef _STANDALONE
1302 1.36 jakllsch add_biosdisk_bootinfo();
1303 1.9 drochner #endif
1304 1.5 drochner
1305 1.3 thorpej f->f_devdata = d;
1306 1.1 perry out:
1307 1.5 drochner va_end(ap);
1308 1.3 thorpej if (error)
1309 1.50 manu dealloc_biosdisk(d);
1310 1.50 manu return error;
1311 1.50 manu }
1312 1.50 manu
1313 1.50 manu #ifndef NO_GPT
1314 1.50 manu static int
1315 1.50 manu biosdisk_find_name(const char *fname, int *biosdev,
1316 1.50 manu daddr_t *offset, daddr_t *size)
1317 1.50 manu {
1318 1.50 manu struct biosdisk *d;
1319 1.50 manu char name[MAXDEVNAME + 1];
1320 1.50 manu char *sep;
1321 1.50 manu #ifndef NO_RAIDFRAME
1322 1.50 manu struct raidframe raidframe[RAIDFRAME_NDEV];
1323 1.50 manu int raidframe_count = 0;
1324 1.50 manu #endif
1325 1.50 manu int i;
1326 1.50 manu int part;
1327 1.50 manu int ret = -1;
1328 1.50 manu
1329 1.50 manu /* Strip leadinf NAME= and cut after the coloon included */
1330 1.50 manu strlcpy(name, fname + 5, MAXDEVNAME);
1331 1.50 manu sep = strchr(name, ':');
1332 1.50 manu if (sep)
1333 1.50 manu *sep = '\0';
1334 1.50 manu
1335 1.50 manu for (i = 0; i < MAX_BIOSDISKS; i++) {
1336 1.50 manu d = alloc(sizeof(*d));
1337 1.50 manu if (d == NULL) {
1338 1.50 manu printf("Out of memory\n");
1339 1.50 manu goto out;
1340 1.50 manu }
1341 1.50 manu
1342 1.50 manu memset(d, 0, sizeof(*d));
1343 1.50 manu d->ll.dev = 0x80 + i; /* hd/cd */
1344 1.50 manu if (set_geometry(&d->ll, NULL))
1345 1.50 manu goto next_disk;
1346 1.50 manu
1347 1.50 manu if (d->ll.type != BIOSDISK_TYPE_HD)
1348 1.50 manu goto next_disk;
1349 1.50 manu
1350 1.50 manu if (read_partitions(d, 0, 0) != 0)
1351 1.50 manu goto next_disk;
1352 1.50 manu
1353 1.50 manu for (part = 0; part < BIOSDISKNPART; part++) {
1354 1.50 manu if (d->part[part].size == 0)
1355 1.50 manu continue;
1356 1.50 manu if (d->part[part].fstype == FS_UNUSED)
1357 1.50 manu continue;
1358 1.50 manu #ifndef NO_RAIDFRAME
1359 1.50 manu if (d->part[part].fstype == FS_RAID) {
1360 1.50 manu raidframe_probe(raidframe,
1361 1.50 manu &raidframe_count, d, part);
1362 1.50 manu /*
1363 1.50 manu * Do not match RAID partition for a name,
1364 1.50 manu * we want to report an inner partition.
1365 1.50 manu */
1366 1.50 manu continue;
1367 1.50 manu }
1368 1.50 manu #endif
1369 1.50 manu if (d->part[part].part_name != NULL &&
1370 1.50 manu strcmp(d->part[part].part_name, name) == 0) {
1371 1.50 manu *biosdev = d->ll.dev;
1372 1.50 manu *offset = d->part[part].offset;
1373 1.50 manu *size = d->part[part].size;
1374 1.50 manu ret = 0;
1375 1.50 manu goto out;
1376 1.50 manu }
1377 1.50 manu
1378 1.50 manu }
1379 1.50 manu next_disk:
1380 1.50 manu dealloc_biosdisk(d);
1381 1.50 manu d = NULL;
1382 1.50 manu }
1383 1.50 manu
1384 1.50 manu #ifndef NO_RAIDFRAME
1385 1.50 manu for (i = 0; i < raidframe_count; i++) {
1386 1.50 manu int candidate = -1;
1387 1.50 manu
1388 1.50 manu if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1389 1.50 manu printf("Out of memory\n");
1390 1.50 manu goto out;
1391 1.50 manu }
1392 1.50 manu
1393 1.50 manu if (read_partitions(d,
1394 1.50 manu raidframe[i].offset + RF_PROTECTED_SECTORS,
1395 1.50 manu raidframe[i].size) != 0)
1396 1.50 manu goto next_raidframe;
1397 1.50 manu
1398 1.50 manu for (part = 0; part < BIOSDISKNPART; part++) {
1399 1.50 manu bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
1400 1.50 manu if (d->part[part].size == 0)
1401 1.50 manu continue;
1402 1.50 manu if (d->part[part].fstype == FS_UNUSED)
1403 1.50 manu continue;
1404 1.53 manu
1405 1.53 manu if (d->part[part].part_name != NULL &&
1406 1.53 manu strcmp(d->part[part].part_name, name) == 0) {
1407 1.50 manu *biosdev = raidframe[i].biosdev;
1408 1.50 manu *offset = raidframe[i].offset
1409 1.50 manu + RF_PROTECTED_SECTORS
1410 1.50 manu + d->part[part].offset;
1411 1.50 manu *size = d->part[part].size;
1412 1.50 manu ret = 0;
1413 1.50 manu goto out;
1414 1.50 manu }
1415 1.50 manu if (strcmp(raidframe[i].parent_name, name) == 0) {
1416 1.50 manu if (candidate == -1 || bootme)
1417 1.50 manu candidate = part;
1418 1.50 manu continue;
1419 1.50 manu }
1420 1.50 manu }
1421 1.50 manu
1422 1.50 manu if (candidate != -1) {
1423 1.50 manu *biosdev = raidframe[i].biosdev;
1424 1.50 manu *offset = raidframe[i].offset
1425 1.50 manu + RF_PROTECTED_SECTORS
1426 1.50 manu + d->part[candidate].offset;
1427 1.50 manu *size = d->part[candidate].size;
1428 1.50 manu ret = 0;
1429 1.50 manu goto out;
1430 1.50 manu }
1431 1.50 manu
1432 1.50 manu next_raidframe:
1433 1.50 manu dealloc_biosdisk(d);
1434 1.50 manu d = NULL;
1435 1.50 manu }
1436 1.50 manu #endif
1437 1.50 manu
1438 1.50 manu out:
1439 1.50 manu if (d != NULL)
1440 1.50 manu dealloc_biosdisk(d);
1441 1.50 manu
1442 1.50 manu return ret;
1443 1.50 manu }
1444 1.50 manu #endif
1445 1.50 manu
1446 1.50 manu #ifndef NO_RAIDFRAME
1447 1.50 manu static int
1448 1.50 manu biosdisk_find_raid(const char *name, int *biosdev,
1449 1.50 manu daddr_t *offset, daddr_t *size)
1450 1.50 manu {
1451 1.50 manu struct biosdisk *d = NULL;
1452 1.50 manu struct raidframe raidframe[RAIDFRAME_NDEV];
1453 1.50 manu int raidframe_count = 0;
1454 1.50 manu int i;
1455 1.50 manu int target_unit = 0;
1456 1.50 manu int target_part;
1457 1.50 manu int part;
1458 1.50 manu int ret = -1;
1459 1.50 manu
1460 1.50 manu if (strstr(name, "raid") != name)
1461 1.50 manu goto out;
1462 1.50 manu
1463 1.50 manu #define isnum(c) ((c) >= '0' && (c) <= '9')
1464 1.50 manu i = 4; /* skip leading "raid" */
1465 1.50 manu if (!isnum(name[i]))
1466 1.50 manu goto out;
1467 1.50 manu do {
1468 1.50 manu target_unit *= 10;
1469 1.50 manu target_unit += name[i++] - '0';
1470 1.50 manu } while (isnum(name[i]));
1471 1.50 manu
1472 1.50 manu #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
1473 1.50 manu
1474 1.50 manu if (!isvalidpart(name[i]))
1475 1.50 manu goto out;
1476 1.50 manu target_part = name[i] - 'a';
1477 1.50 manu
1478 1.50 manu for (i = 0; i < MAX_BIOSDISKS; i++) {
1479 1.50 manu d = alloc(sizeof(*d));
1480 1.50 manu if (d == NULL) {
1481 1.50 manu printf("Out of memory\n");
1482 1.50 manu goto out;
1483 1.50 manu }
1484 1.50 manu
1485 1.50 manu memset(d, 0, sizeof(*d));
1486 1.50 manu d->ll.dev = 0x80 + i; /* hd/cd */
1487 1.50 manu if (set_geometry(&d->ll, NULL))
1488 1.50 manu goto next_disk;
1489 1.50 manu
1490 1.50 manu if (d->ll.type != BIOSDISK_TYPE_HD)
1491 1.50 manu goto next_disk;
1492 1.50 manu
1493 1.50 manu if (read_partitions(d, 0, 0) != 0)
1494 1.50 manu goto next_disk;
1495 1.50 manu
1496 1.50 manu for (part = 0; part < BIOSDISKNPART; part++) {
1497 1.50 manu if (d->part[part].size == 0)
1498 1.50 manu continue;
1499 1.50 manu if (d->part[part].fstype != FS_RAID)
1500 1.50 manu continue;
1501 1.50 manu raidframe_probe(raidframe,
1502 1.50 manu &raidframe_count, d, part);
1503 1.50 manu
1504 1.50 manu }
1505 1.50 manu next_disk:
1506 1.50 manu dealloc_biosdisk(d);
1507 1.50 manu d = NULL;
1508 1.50 manu }
1509 1.50 manu
1510 1.50 manu for (i = 0; i < raidframe_count; i++) {
1511 1.50 manu if (raidframe[i].last_unit != target_unit)
1512 1.50 manu continue;
1513 1.50 manu
1514 1.50 manu if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1515 1.50 manu printf("Out of memory\n");
1516 1.50 manu goto out;
1517 1.50 manu }
1518 1.50 manu
1519 1.50 manu if (read_partitions(d,
1520 1.50 manu raidframe[i].offset + RF_PROTECTED_SECTORS,
1521 1.50 manu raidframe[i].size) != 0)
1522 1.50 manu goto next_raidframe;
1523 1.50 manu
1524 1.50 manu for (part = 0; part < BIOSDISKNPART; part++) {
1525 1.50 manu if (d->part[part].size == 0)
1526 1.50 manu continue;
1527 1.50 manu if (d->part[part].fstype == FS_UNUSED)
1528 1.50 manu continue;
1529 1.50 manu if (part == target_part) {
1530 1.50 manu *biosdev = raidframe[i].biosdev;
1531 1.50 manu *offset = raidframe[i].offset
1532 1.50 manu + RF_PROTECTED_SECTORS
1533 1.50 manu + d->part[part].offset;
1534 1.50 manu *size = d->part[part].size;
1535 1.50 manu ret = 0;
1536 1.50 manu goto out;
1537 1.50 manu }
1538 1.50 manu }
1539 1.50 manu next_raidframe:
1540 1.50 manu dealloc_biosdisk(d);
1541 1.50 manu d = NULL;
1542 1.50 manu }
1543 1.50 manu out:
1544 1.50 manu if (d != NULL)
1545 1.50 manu dealloc_biosdisk(d);
1546 1.50 manu
1547 1.50 manu return ret;
1548 1.50 manu }
1549 1.50 manu #endif
1550 1.50 manu
1551 1.50 manu int
1552 1.50 manu biosdisk_open_name(struct open_file *f, const char *name)
1553 1.50 manu {
1554 1.50 manu #if defined(NO_GPT) && defined(NO_RAIDFRAME)
1555 1.50 manu return ENXIO;
1556 1.50 manu #else
1557 1.50 manu struct biosdisk *d = NULL;
1558 1.50 manu int biosdev;
1559 1.50 manu daddr_t offset;
1560 1.50 manu daddr_t size;
1561 1.50 manu int error = -1;
1562 1.50 manu
1563 1.50 manu #ifndef NO_GPT
1564 1.50 manu if (strstr(name, "NAME=") == name)
1565 1.50 manu error = biosdisk_find_name(name, &biosdev, &offset, &size);
1566 1.50 manu #endif
1567 1.50 manu #ifndef NO_RAIDFRAME
1568 1.50 manu if (strstr(name, "raid") == name)
1569 1.50 manu error = biosdisk_find_raid(name, &biosdev, &offset, &size);
1570 1.50 manu #endif
1571 1.50 manu
1572 1.50 manu if (error != 0) {
1573 1.50 manu printf("%s not found\n", name);
1574 1.50 manu error = ENXIO;
1575 1.50 manu goto out;
1576 1.50 manu }
1577 1.50 manu
1578 1.50 manu d = alloc_biosdisk(biosdev);
1579 1.50 manu if (d == NULL) {
1580 1.50 manu error = ENXIO;
1581 1.50 manu goto out;
1582 1.50 manu }
1583 1.50 manu
1584 1.50 manu bi_disk.biosdev = d->ll.dev;
1585 1.50 manu bi_disk.partition = 0;
1586 1.50 manu bi_disk.labelsector = -1;
1587 1.50 manu
1588 1.50 manu bi_wedge.biosdev = d->ll.dev;
1589 1.50 manu
1590 1.50 manu /*
1591 1.50 manu * If we did not get wedge match info from check_gpt()
1592 1.50 manu * compute it now.
1593 1.50 manu */
1594 1.50 manu if (bi_wedge.matchblk == -1) {
1595 1.50 manu if (readsects(&d->ll, offset, 1, d->buf, 1)) {
1596 1.50 manu #ifdef DISK_DEBUG
1597 1.50 manu printf("Error reading sector at %"PRId64"\n", offset);
1598 1.50 manu #endif
1599 1.50 manu error = EIO;
1600 1.50 manu goto out;
1601 1.50 manu }
1602 1.50 manu
1603 1.50 manu bi_wedge.matchblk = offset;
1604 1.50 manu bi_wedge.matchnblks = 1;
1605 1.50 manu
1606 1.50 manu md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
1607 1.50 manu }
1608 1.50 manu
1609 1.50 manu d->boff = offset;
1610 1.50 manu
1611 1.50 manu bi_wedge.startblk = offset;
1612 1.50 manu bi_wedge.nblks = size;
1613 1.50 manu
1614 1.52 manu #ifdef _STANDALONE
1615 1.50 manu add_biosdisk_bootinfo();
1616 1.50 manu #endif
1617 1.50 manu
1618 1.50 manu f->f_devdata = d;
1619 1.50 manu out:
1620 1.50 manu if (error && d != NULL)
1621 1.50 manu dealloc_biosdisk(d);
1622 1.22 junyoung return error;
1623 1.50 manu #endif
1624 1.1 perry }
1625 1.1 perry
1626 1.50 manu
1627 1.50 manu
1628 1.12 drochner #ifndef LIBSA_NO_FS_CLOSE
1629 1.22 junyoung int
1630 1.24 junyoung biosdisk_close(struct open_file *f)
1631 1.1 perry {
1632 1.3 thorpej struct biosdisk *d = f->f_devdata;
1633 1.1 perry
1634 1.24 junyoung /* let the floppy drive go off */
1635 1.24 junyoung if (d->ll.type == BIOSDISK_TYPE_FD)
1636 1.31 tsutsui wait_sec(3); /* 2s is enough on all PCs I found */
1637 1.1 perry
1638 1.50 manu dealloc_biosdisk(d);
1639 1.3 thorpej f->f_devdata = NULL;
1640 1.22 junyoung return 0;
1641 1.1 perry }
1642 1.12 drochner #endif
1643 1.1 perry
1644 1.22 junyoung int
1645 1.24 junyoung biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
1646 1.1 perry {
1647 1.3 thorpej return EIO;
1648 1.1 perry }
1649