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