1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | *␊ |
24 | *␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | *␊ |
31 | *␊ |
32 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
33 | *␊ |
34 | * This software is supplied under the terms of a license agreement or ␊ |
35 | * nondisclosure agreement with Intel Corporation and may not be copied ␊ |
36 | * nor disclosed except in accordance with the terms of that agreement.␊ |
37 | *␊ |
38 | * Copyright 1988, 1989 Intel Corporation␊ |
39 | *␊ |
40 | *␊ |
41 | * Copyright 1993 NeXT Computer, Inc.␊ |
42 | * All rights reserved.␊ |
43 | *␊ |
44 | *␊ |
45 | * Copyright 2007 VMware Inc.␊ |
46 | * "Preboot" ramdisk support added by David Elliott␊ |
47 | * GPT support added by David Elliott. Based on IOGUIDPartitionScheme.cpp.␊ |
48 | */␊ |
49 | ␊ |
50 | #if DEBUG␊ |
51 | #define DBG(x...)␉printf(x)␊ |
52 | #else␊ |
53 | #define DBG(x...)␉msglog(x)␊ |
54 | #endif␊ |
55 | ␊ |
56 | //Azi: style the rest later...␊ |
57 | ␊ |
58 | // Allow UFS_SUPPORT to be overridden with preprocessor option.␊ |
59 | #ifndef UFS_SUPPORT␊ |
60 | // zef: Disabled UFS support␊ |
61 | #define UFS_SUPPORT 0␊ |
62 | #endif␊ |
63 | ␊ |
64 | #if UFS_SUPPORT␊ |
65 | #include "ufs.h"␊ |
66 | #endif␊ |
67 | #include <limits.h>␊ |
68 | #include <IOKit/storage/IOApplePartitionScheme.h>␊ |
69 | #include <IOKit/storage/IOGUIDPartitionScheme.h>␊ |
70 | #include "libsaio.h"␊ |
71 | #include "sl.h"␊ |
72 | #include "boot.h"␊ |
73 | #include "bootstruct.h"␊ |
74 | #include "memory.h"␊ |
75 | #include "fdisk.h"␊ |
76 | #include "hfs.h"␊ |
77 | #include "ntfs.h"␊ |
78 | #include "exfat.h"␊ |
79 | #include "msdos.h"␊ |
80 | #include "ext2fs.h"␊ |
81 | #include "befs.h"␊ |
82 | #include "freebsd.h"␊ |
83 | #include "openbsd.h"␊ |
84 | #include "xml.h"␊ |
85 | #include "disk.h"␊ |
86 | // For EFI_GUID␊ |
87 | #include "efi.h"␊ |
88 | #include "efi_tables.h"␊ |
89 | ␊ |
90 | typedef struct gpt_hdr gpt_hdr;␊ |
91 | typedef struct gpt_ent gpt_ent;␊ |
92 | ␊ |
93 | #define PROBEFS_SIZE BPS * 4 /* buffer size for filesystem probe */␊ |
94 | #define CD_BPS 2048 /* CD-ROM block size */␊ |
95 | #define N_CACHE_SECS (BIOS_LEN / BPS) /* Must be a multiple of 4 for CD-ROMs */␊ |
96 | #define UFS_FRONT_PORCH 0␊ |
97 | #define kAPMSector 2 /* Sector number of Apple partition map */␊ |
98 | #define kAPMCDSector 8 /* Translated sector of Apple partition map on a CD */␊ |
99 | #define ECC_CORRECTED_ERR 0x11␊ |
100 | ␊ |
101 | /*␊ |
102 | * IORound and IOTrunc convenience functions, in the spirit␊ |
103 | * of vm's round_page() and trunc_page().␊ |
104 | */␊ |
105 | #define IORound(value,multiple) \␊ |
106 | ((((value) + (multiple) - 1) / (multiple)) * (multiple))␊ |
107 | ␊ |
108 | #define IOTrunc(value,multiple) \␊ |
109 | (((value) / (multiple)) * (multiple));␊ |
110 | ␊ |
111 | //==========================================================================␊ |
112 | // Maps (E)BIOS return codes to message strings.␊ |
113 | ␊ |
114 | struct NamedValue {␊ |
115 | ␉unsigned char value;␊ |
116 | ␉const char *name;␊ |
117 | };␊ |
118 | ␊ |
119 | /*␊ |
120 | * Map a disk drive to bootable volumes contained within.␊ |
121 | */␊ |
122 | struct DiskBVMap {␊ |
123 | ␉int␉␉␉biosdev;␉// BIOS device number (unique)␊ |
124 | ␉BVRef␉␉␉bvr;␉␉// chain of boot volumes on the disk␊ |
125 | ␉int␉␉␉bvrcnt;␉␉// number of boot volumes␊ |
126 | ␉struct DiskBVMap␉*next;␉␉// linkage to next mapping␊ |
127 | };␊ |
128 | ␊ |
129 | /*␊ |
130 | * trackbuf points to the start of the track cache. Biosread()␊ |
131 | * will store the sectors read from disk to this memory area.␊ |
132 | *␊ |
133 | * biosbuf points to a sector within the track cache, and is␊ |
134 | * updated by Biosread().␊ |
135 | */␊ |
136 | static char * const trackbuf = (char *) ptov(BIOS_ADDR);␊ |
137 | static char * biosbuf;␊ |
138 | ␊ |
139 | static struct DiskBVMap *gDiskBVMap = NULL;␊ |
140 | static struct disk_blk0 *gBootSector = NULL;␊ |
141 | ␊ |
142 | // Function pointers to be filled in if ramdisks are available:␊ |
143 | int (*p_ramdiskReadBytes)( int biosdev, unsigned int blkno,␊ |
144 | unsigned int byteoff,␊ |
145 | unsigned int byteCount, void * buffer ) = NULL;␊ |
146 | int (*p_get_ramdisk_info)(int biosdev, struct driveInfo *dip) = NULL;␊ |
147 | ␊ |
148 | static bool getOSVersion(BVRef bvr, char *str);␊ |
149 | static bool cache_valid = false;␊ |
150 | ␊ |
151 | static const struct NamedValue bios_errors[] =␊ |
152 | {␊ |
153 | ␉{ 0x10, "Media error" },␊ |
154 | ␉{ 0x11, "Corrected ECC error" },␊ |
155 | ␉{ 0x20, "Controller or device error" },␊ |
156 | ␉{ 0x40, "Seek failed" },␊ |
157 | ␉{ 0x80, "Device timeout" },␊ |
158 | ␉{ 0xAA, "Drive not ready" },␊ |
159 | ␉{ 0x00, NULL }␊ |
160 | };␊ |
161 | ␊ |
162 | static const struct NamedValue fdiskTypes[] =␊ |
163 | {␊ |
164 | ␉{ FDISK_DOS12, "DOS_FAT_12" }, // 0x01␊ |
165 | ␉{ FDISK_DOS16S, "DOS_FAT_16_S" }, // 0x04␊ |
166 | ␉{ FDISK_DOS16B, "DOS_FAT_16" }, // 0x06␊ |
167 | ␉{ FDISK_NTFS, "Windows_NTFS" }, // 0x07␊ |
168 | ␉{ FDISK_DOS32, "DOS_FAT_32" }, // 0x0B␊ |
169 | ␉{ FDISK_FAT32, "Windows_FAT_32" }, // 0x0C␊ |
170 | ␉{ FDISK_FAT16, "Windows_FAT_16" }, // 0x0E␊ |
171 | ␉{ FDISK_WIN_LDM, "Windows_LDM" }, // 0x42␊ |
172 | ␉{ FDISK_LINUX_SWAP, "Linux_Swap" }, // 0x82␊ |
173 | ␉{ FDISK_LINUX, "Linux" }, // 0x83␊ |
174 | ␉{ FDISK_LINUX_LVM, "Linux_LVM" }, // 0x8E␊ |
175 | ␉{ FDISK_FREEBSD, "FreeBSD" }, // 0xA5␊ |
176 | ␉{ FDISK_OPENBSD, "OpenBSD" }, // 0xA6␊ |
177 | ␉{ FDISK_NEXTNAME, "Apple_Rhapsody_UFS" }, // 0xA7␊ |
178 | ␉{ FDISK_UFS, "Apple_UFS" }, // 0xA8␊ |
179 | ␉{ FDISK_NETBSD, "NetBSD" }, // 0xA9␊ |
180 | ␉{ FDISK_BOOTER, "Apple_Boot" }, // 0xAB␊ |
181 | ␉{ FDISK_ENCRYPTED, "Apple_Encrypted" }, // 0xAE␊ |
182 | ␉{ FDISK_HFS, "Apple_HFS" }, // 0xAF␊ |
183 | ␉{ 0xCD, "CD-ROM" }, // 0xCD␊ |
184 | ␉{ 0xE7, "Windows_exFAT" }, // 0xE7␊ |
185 | ␉{ FDISK_BEFS, "Haiku" }, // 0xEB␊ |
186 | ␉{ FDISK_LINUX_RAID, "Linux_RAID" }, // 0xFD␊ |
187 | ␉{ 0x00, NULL } /* must be last */␊ |
188 | };␊ |
189 | ␊ |
190 | //==============================================================================␊ |
191 | ␊ |
192 | extern void spinActivityIndicator(int sectors);␊ |
193 | ␊ |
194 | //==========================================================================␊ |
195 | ␊ |
196 | static int getDriveInfo( int biosdev, struct driveInfo *dip )␊ |
197 | {␊ |
198 | ␉static struct driveInfo cached_di;␊ |
199 | ␉int cc;␊ |
200 | ␊ |
201 | ␉// Real BIOS devices are 8-bit, so anything above that is for internal use.␊ |
202 | ␉// Don't cache ramdisk drive info since it doesn't require several BIOS␊ |
203 | ␉// calls and is thus not worth it.␊ |
204 | ␉if (biosdev >= 0x100)␊ |
205 | ␉{␊ |
206 | ␉␉if (p_get_ramdisk_info != NULL)␊ |
207 | ␉␉{␊ |
208 | ␉␉␉cc = (*p_get_ramdisk_info)(biosdev, dip);␊ |
209 | ␉␉}␊ |
210 | ␉␉else␊ |
211 | ␉␉{␊ |
212 | ␉␉␉cc = -1;␊ |
213 | ␉␉}␊ |
214 | ␉␉if (cc < 0)␊ |
215 | ␉␉{␊ |
216 | ␉␉␉dip->valid = 0;␊ |
217 | ␉␉␉return -1;␊ |
218 | ␉␉}␊ |
219 | ␉␉else␊ |
220 | ␉␉{␊ |
221 | ␉␉␉return 0;␊ |
222 | ␉␉}␊ |
223 | ␉}␊ |
224 | ␊ |
225 | ␉if (!cached_di.valid || biosdev != cached_di.biosdev)␊ |
226 | ␉{␊ |
227 | ␉␉cc = get_drive_info(biosdev, &cached_di);␊ |
228 | ␊ |
229 | ␉␉if (cc < 0)␊ |
230 | ␉␉{␊ |
231 | ␉␉␉cached_di.valid = 0;␊ |
232 | ␉␉␉DEBUG_DISK(("get_drive_info returned error\n"));␊ |
233 | ␉␉␉return (-1); // BIOS call error␊ |
234 | ␉␉}␊ |
235 | ␉}␊ |
236 | ␊ |
237 | ␉bcopy(&cached_di, dip, sizeof(cached_di));␊ |
238 | ␊ |
239 | ␉return 0;␊ |
240 | }␊ |
241 | ␊ |
242 | //==========================================================================␊ |
243 | ␊ |
244 | static const char * getNameForValue( const struct NamedValue * nameTable,␊ |
245 | unsigned char value )␊ |
246 | {␊ |
247 | ␉const struct NamedValue *np;␊ |
248 | ␊ |
249 | ␉for ( np = nameTable; np->value; np++)␊ |
250 | ␉{␊ |
251 | ␉␉if (np->value == value)␊ |
252 | ␉␉{␊ |
253 | ␉␉␉return np->name;␊ |
254 | ␉␉}␊ |
255 | ␉}␊ |
256 | ␊ |
257 | ␉return NULL;␊ |
258 | }␊ |
259 | ␊ |
260 | //==============================================================================␊ |
261 | ␊ |
262 | static const char * bios_error(int errnum)␊ |
263 | {␊ |
264 | ␉static char errorstr[] = "Error 0x00";␊ |
265 | ␉const char * errname;␊ |
266 | ␊ |
267 | ␉errname = getNameForValue(bios_errors, errnum);␊ |
268 | ␊ |
269 | ␉if (errname)␊ |
270 | ␉{␊ |
271 | ␉␉return errname;␊ |
272 | ␉}␊ |
273 | ␊ |
274 | ␉sprintf(errorstr, "Error 0x%02x", errnum);␊ |
275 | ␉return errorstr; // No string, print error code only␊ |
276 | }␊ |
277 | ␊ |
278 | //==========================================================================␊ |
279 | // Use BIOS INT13 calls to read the sector specified. This function will␊ |
280 | // also perform read-ahead to cache a few subsequent sector to the sector␊ |
281 | // cache.␊ |
282 | // ␊ |
283 | // Return:␊ |
284 | // 0 on success, or an error code from INT13/F2 or INT13/F42 BIOS call.␊ |
285 | ␊ |
286 | static int Biosread( int biosdev, unsigned long long secno )␊ |
287 | {␊ |
288 | ␉static int xbiosdev, xcyl, xhead;␊ |
289 | ␉static unsigned int xsec, xnsecs;␊ |
290 | ␉struct driveInfo di;␊ |
291 | ␊ |
292 | ␉int rc = -1;␊ |
293 | ␉int cyl, head, sec;␊ |
294 | ␉int tries = 0;␊ |
295 | ␉int bps, divisor;␊ |
296 | ␊ |
297 | ␉if (getDriveInfo(biosdev, &di) < 0)␊ |
298 | ␉{␊ |
299 | ␉␉return -1;␊ |
300 | ␉}␊ |
301 | ␊ |
302 | ␉if (di.no_emulation)␊ |
303 | ␉{␊ |
304 | ␉␉bps = 2048; /* Always assume 2K block size since the BIOS may lie about the geometry */␊ |
305 | ␉}␊ |
306 | ␉else␊ |
307 | ␉{␊ |
308 | ␉␉bps = di.di.params.phys_nbps;␊ |
309 | ␊ |
310 | ␉␉if (bps == 0)␊ |
311 | ␉␉{␊ |
312 | ␉␉␉return -1;␊ |
313 | ␉␉}␊ |
314 | ␉}␊ |
315 | ␊ |
316 | ␉divisor = bps / BPS;␊ |
317 | ␊ |
318 | ␉DEBUG_DISK(("Biosread dev %x sec %d bps %d\n", biosdev, secno, bps));␊ |
319 | ␊ |
320 | ␉// To read the disk sectors, use EBIOS if we can. Otherwise,␊ |
321 | ␉// revert to the standard BIOS calls.␊ |
322 | ␊ |
323 | ␉if ((biosdev >= kBIOSDevTypeHardDrive) && (di.uses_ebios & EBIOS_FIXED_DISK_ACCESS))␊ |
324 | ␉{␊ |
325 | ␉␉if (cache_valid && (biosdev == xbiosdev) && (secno >= xsec) && ((unsigned int)secno < (xsec + xnsecs)))␊ |
326 | ␉␉{␊ |
327 | ␉␉␉biosbuf = trackbuf + (BPS * (secno - xsec));␊ |
328 | ␉␉␉return 0;␊ |
329 | ␉␉}␊ |
330 | ␊ |
331 | ␉␉xnsecs = N_CACHE_SECS;␊ |
332 | ␉␉xsec = (secno / divisor) * divisor;␊ |
333 | ␉␉cache_valid = false;␊ |
334 | ␊ |
335 | ␉␉while ((rc = ebiosread(biosdev, secno / divisor, xnsecs / divisor)) && (++tries < 5))␊ |
336 | ␉␉{␊ |
337 | ␉␉␉if (rc == ECC_CORRECTED_ERR)␊ |
338 | ␉␉␉{␊ |
339 | ␉␉␉␉rc = 0; /* Ignore corrected ECC errors */␊ |
340 | ␉␉␉␉break;␊ |
341 | ␉␉␉}␊ |
342 | ␊ |
343 | ␉␉␉error(" EBIOS read error: %s\n", bios_error(rc), rc);␊ |
344 | ␉␉␉error(" Block 0x%x Sectors %d\n", secno, xnsecs);␊ |
345 | ␉␉␉sleep(1);␊ |
346 | ␉␉}␊ |
347 | ␉}␊ |
348 | ␊ |
349 | ␉else␊ |
350 | ␉{␊ |
351 | ␉␉/* spc = spt * heads */␊ |
352 | ␉␉int spc = (di.di.params.phys_spt * di.di.params.phys_heads);␊ |
353 | ␉␉cyl = secno / spc;␊ |
354 | ␉␉head = (secno % spc) / di.di.params.phys_spt;␊ |
355 | ␉␉sec = secno % di.di.params.phys_spt;␊ |
356 | ␊ |
357 | ␉␉if (cache_valid && (biosdev == xbiosdev) && (cyl == xcyl) &&␊ |
358 | ␉␉␉(head == xhead) && ((unsigned int)sec >= xsec) && ((unsigned int)sec < (xsec + xnsecs)))␊ |
359 | ␊ |
360 | ␉␉{␊ |
361 | ␉␉␉// this sector is in trackbuf cache␊ |
362 | ␉␉␉biosbuf = trackbuf + (BPS * (sec - xsec));␊ |
363 | ␉␉␉return 0;␊ |
364 | ␉␉}␊ |
365 | ␊ |
366 | ␉␉// Cache up to a track worth of sectors, but do not cross a track boundary.␊ |
367 | ␊ |
368 | ␉␉xcyl = cyl;␊ |
369 | ␉␉xhead = head;␊ |
370 | ␉␉xsec = sec;␊ |
371 | ␉␉xnsecs = ((unsigned int)(sec + N_CACHE_SECS) > di.di.params.phys_spt) ? (di.di.params.phys_spt - sec) : N_CACHE_SECS;␊ |
372 | ␊ |
373 | ␉␉cache_valid = false;␊ |
374 | ␊ |
375 | ␉␉while ((rc = biosread(biosdev, cyl, head, sec, xnsecs)) && (++tries < 5))␊ |
376 | ␉␉{␊ |
377 | ␉␉␉if (rc == ECC_CORRECTED_ERR)␊ |
378 | ␉␉␉{␊ |
379 | ␉␉␉␉rc = 0; /* Ignore corrected ECC errors */␊ |
380 | ␉␉␉␉break;␊ |
381 | ␉␉␉}␊ |
382 | ␉␉␉error(" BIOS read error: %s\n", bios_error(rc), rc);␊ |
383 | ␉␉␉error(" Block %d, Cyl %d Head %d Sector %d\n", secno, cyl, head, sec);␊ |
384 | ␉␉␉sleep(1);␊ |
385 | ␉␉}␊ |
386 | ␉}␊ |
387 | ␊ |
388 | ␉// If the BIOS reported success, mark the sector cache as valid.␊ |
389 | ␊ |
390 | ␉if (rc == 0)␊ |
391 | ␉{␊ |
392 | ␉␉cache_valid = true;␊ |
393 | ␉}␊ |
394 | ␊ |
395 | ␉biosbuf = trackbuf + (secno % divisor) * BPS;␊ |
396 | ␉xbiosdev = biosdev;␊ |
397 | ␊ |
398 | ␉spinActivityIndicator(xnsecs);␊ |
399 | ␊ |
400 | ␉return rc;␊ |
401 | }␊ |
402 | ␊ |
403 | //==============================================================================␊ |
404 | ␊ |
405 | int testBiosread(int biosdev, unsigned long long secno)␊ |
406 | {␊ |
407 | ␉return Biosread(biosdev, secno);␊ |
408 | }␊ |
409 | ␊ |
410 | //==============================================================================␊ |
411 | ␊ |
412 | static int readBytes(int biosdev, unsigned long long blkno, unsigned int byteoff, unsigned int byteCount, void * buffer)␊ |
413 | {␊ |
414 | ␉// ramdisks require completely different code for reading.␊ |
415 | ␉if(p_ramdiskReadBytes != NULL && biosdev >= 0x100)␊ |
416 | ␉{␊ |
417 | ␉␉return (*p_ramdiskReadBytes)(biosdev, blkno, byteoff, byteCount, buffer);␊ |
418 | ␉}␊ |
419 | ␊ |
420 | ␉char * cbuf = (char *) buffer;␊ |
421 | ␉int error;␊ |
422 | ␉int copy_len;␊ |
423 | ␊ |
424 | ␉DEBUG_DISK(("%s: dev %X block %X [%d] -> 0x%X...", __FUNCTION__, biosdev, blkno, byteCount, (unsigned)cbuf));␊ |
425 | ␊ |
426 | ␉for (; byteCount; cbuf += copy_len, blkno++)␊ |
427 | ␉{␊ |
428 | ␉␉error = Biosread(biosdev, blkno);␊ |
429 | ␊ |
430 | ␉␉if (error)␊ |
431 | ␉␉{␊ |
432 | ␉␉␉DEBUG_DISK(("error\n"));␊ |
433 | ␊ |
434 | ␉␉␉return (-1);␊ |
435 | ␉␉}␊ |
436 | ␊ |
437 | ␉␉copy_len = ((byteCount + byteoff) > BPS) ? (BPS - byteoff) : byteCount;␊ |
438 | ␉␉bcopy( biosbuf + byteoff, cbuf, copy_len );␊ |
439 | ␉␉byteCount -= copy_len;␊ |
440 | ␉␉byteoff = 0;␊ |
441 | ␉}␊ |
442 | ␊ |
443 | ␉DEBUG_DISK(("done\n"));␊ |
444 | ␊ |
445 | ␉return 0; ␊ |
446 | }␊ |
447 | ␊ |
448 | //==============================================================================␊ |
449 | ␊ |
450 | static int isExtendedFDiskPartition( const struct fdisk_part *part )␊ |
451 | {␊ |
452 | ␉static unsigned char extParts[] =␊ |
453 | ␉{␊ |
454 | ␉␉0x05, /* Extended */␊ |
455 | ␉␉0x0f, /* Win95 extended */␊ |
456 | ␉␉0x85, /* Linux extended */␊ |
457 | ␉};␊ |
458 | ␊ |
459 | ␉unsigned int i;␊ |
460 | ␊ |
461 | ␉for (i = 0; i < sizeof(extParts)/sizeof(extParts[0]); i++)␊ |
462 | ␉{␊ |
463 | ␉␉if (extParts[i] == part->systid)␊ |
464 | ␉␉{␊ |
465 | ␉␉␉return 1;␊ |
466 | ␉␉}␊ |
467 | ␉}␊ |
468 | ␉return 0;␊ |
469 | }␊ |
470 | ␊ |
471 | //==============================================================================␊ |
472 | ␊ |
473 | static int getNextFDiskPartition( int biosdev, int *partno, const struct fdisk_part **outPart )␊ |
474 | {␊ |
475 | ␉static int sBiosdev = -1;␊ |
476 | ␉static int sNextPartNo;␊ |
477 | ␉static unsigned int sFirstBase;␊ |
478 | ␉static unsigned int sExtBase;␊ |
479 | ␉static unsigned int sExtDepth;␊ |
480 | ␉static struct fdisk_part *sExtPart;␊ |
481 | ␉struct fdisk_part *part;␊ |
482 | ␊ |
483 | ␉if ( sBiosdev != biosdev || *partno < 0 )␊ |
484 | ␉{␊ |
485 | ␉␉// Fetch MBR.␊ |
486 | ␉␉if ( readBootSector( biosdev, DISK_BLK0, 0 ) )␊ |
487 | ␉␉{␊ |
488 | ␉␉␉return 0;␊ |
489 | ␉␉}␊ |
490 | ␊ |
491 | ␉␉sBiosdev = biosdev;␊ |
492 | ␉␉sNextPartNo = 0;␊ |
493 | ␉␉sFirstBase = 0;␊ |
494 | ␉␉sExtBase = 0;␊ |
495 | ␉␉sExtDepth = 0;␊ |
496 | ␉␉sExtPart = NULL;␊ |
497 | ␉}␊ |
498 | ␊ |
499 | ␉while (1)␊ |
500 | ␉{␊ |
501 | ␉␉part = NULL;␊ |
502 | ␊ |
503 | ␉␉if ( sNextPartNo < FDISK_NPART )␊ |
504 | ␉␉{␊ |
505 | ␉␉␉part = (struct fdisk_part *) gBootSector->parts[sNextPartNo];␊ |
506 | ␉␉}␊ |
507 | ␉␉else if ( sExtPart )␊ |
508 | ␉␉{␊ |
509 | ␉␉␉unsigned int blkno = sExtPart->relsect + sFirstBase;␊ |
510 | ␊ |
511 | ␉␉␉// Save the block offset of the first extended partition.␊ |
512 | ␊ |
513 | ␉␉␉if (sExtDepth == 0)␊ |
514 | ␉␉␉{␊ |
515 | ␉␉␉␉sFirstBase = blkno;␊ |
516 | ␉␉␉}␊ |
517 | ␉␉␉sExtBase = blkno;␊ |
518 | ␊ |
519 | ␉␉␉// Load extended partition table.␊ |
520 | ␊ |
521 | ␉␉␉if ( readBootSector( biosdev, blkno, 0 ) == 0 )␊ |
522 | ␉␉␉{␊ |
523 | ␉␉␉␉sNextPartNo = 0;␊ |
524 | ␉␉␉␉sExtDepth++;␊ |
525 | ␉␉␉␉sExtPart = NULL;␊ |
526 | ␉␉␉␉continue;␊ |
527 | ␉␉␉}␊ |
528 | ␉␉␉// Fall through to part == NULL␊ |
529 | ␉␉}␊ |
530 | ␊ |
531 | ␉␉if ( part == NULL ) break; // Reached end of partition chain.␊ |
532 | ␊ |
533 | ␉␉// Advance to next partition number.␊ |
534 | ␊ |
535 | ␉␉sNextPartNo++;␊ |
536 | ␊ |
537 | ␉␉if ( isExtendedFDiskPartition(part) )␊ |
538 | ␉␉{␊ |
539 | ␉␉␉sExtPart = part;␊ |
540 | ␉␉␉continue;␊ |
541 | ␉␉}␊ |
542 | ␊ |
543 | ␉␉// Skip empty slots.␊ |
544 | ␊ |
545 | ␉␉if ( part->systid == 0x00 )␊ |
546 | ␉␉{␊ |
547 | ␉␉␉continue;␊ |
548 | ␉␉}␊ |
549 | ␊ |
550 | ␉␉// Change relative offset to an absolute offset.␊ |
551 | ␉␉part->relsect += sExtBase;␊ |
552 | ␊ |
553 | ␉␉*outPart = part;␊ |
554 | ␉␉*partno = sExtDepth ? (int)(sExtDepth + FDISK_NPART) : sNextPartNo;␊ |
555 | ␊ |
556 | ␉␉break;␊ |
557 | ␉}␊ |
558 | ␊ |
559 | ␉return (part != NULL);␊ |
560 | }␊ |
561 | ␊ |
562 | //==============================================================================␊ |
563 | ␊ |
564 | /*␊ |
565 | * Trying to figure out the filsystem type of a given partition.␊ |
566 | * X = fdisk partition type␊ |
567 | * 0 = Unknown/Unused␊ |
568 | * -1 = error␊ |
569 | */␊ |
570 | static int probeFileSystem(int biosdev, unsigned int blkoff)␊ |
571 | {␊ |
572 | ␉// detected filesystem type;␊ |
573 | ␉int result = -1;␊ |
574 | ␉int fatbits = 0;␊ |
575 | ␊ |
576 | ␉// Allocating buffer for 4 sectors.␊ |
577 | ␉const void *probeBuffer = malloc(PROBEFS_SIZE);␊ |
578 | ␉if (probeBuffer == NULL)␊ |
579 | ␉{␊ |
580 | ␉␉verbose("[probeFileSystem] Error: can't alloc memory for probe buffer.\n");␊ |
581 | ␉␉goto exit;␊ |
582 | ␉}␊ |
583 | ␊ |
584 | ␉// Reading first 4 sectors of current partition␊ |
585 | ␉int error = readBytes(biosdev, blkoff, 0, PROBEFS_SIZE, (void *)probeBuffer);␊ |
586 | ␊ |
587 | ␉if (error)␊ |
588 | ␉{␊ |
589 | ␉␉verbose("[probeFileSystem] Error: can't read from device=%02Xh.\n", biosdev);␊ |
590 | ␉␉goto exit;␊ |
591 | ␉}␊ |
592 | ␊ |
593 | ␉if (HFSProbe(probeBuffer))␊ |
594 | ␉{␊ |
595 | ␉␉result = FDISK_HFS;␊ |
596 | ␉}␊ |
597 | ␉else if (EX2Probe(probeBuffer))␊ |
598 | ␉{␊ |
599 | ␉␉result = FDISK_LINUX;␊ |
600 | ␉}␊ |
601 | ␉else if (FreeBSDProbe(probeBuffer))␊ |
602 | ␉{␊ |
603 | ␉␉result = FDISK_FREEBSD;␊ |
604 | ␉}␊ |
605 | ␉else if (OpenBSDProbe(probeBuffer))␊ |
606 | ␉{␊ |
607 | ␉␉result = FDISK_OPENBSD;␊ |
608 | ␉}␊ |
609 | ␉else if (BeFSProbe(probeBuffer))␊ |
610 | ␉{␊ |
611 | ␉␉result = FDISK_BEFS;␊ |
612 | ␉}␊ |
613 | ␉else if (NTFSProbe(probeBuffer))␊ |
614 | ␉{␊ |
615 | ␉␉result = FDISK_NTFS;␊ |
616 | ␉}␊ |
617 | ␉else if (EXFATProbe(probeBuffer))␊ |
618 | ␉{␊ |
619 | ␉␉result = 0xE7;␊ |
620 | ␉}␊ |
621 | ␉else if ((fatbits = MSDOSProbe(probeBuffer)))␊ |
622 | ␉{␊ |
623 | ␉␉switch (fatbits)␊ |
624 | ␉␉{␊ |
625 | ␉␉␉case 12:␊ |
626 | ␉␉␉␉result = FDISK_DOS12;␊ |
627 | ␉␉␉␉break;␊ |
628 | ␊ |
629 | ␉␉␉case 16:␊ |
630 | ␉␉␉␉result = FDISK_DOS16B;␊ |
631 | ␉␉␉␉break;␊ |
632 | ␊ |
633 | ␉␉␉case 32:␊ |
634 | ␉␉␉default:␊ |
635 | ␉␉␉␉result = FDISK_FAT32;␊ |
636 | ␉␉␉␉break;␊ |
637 | ␉␉}␊ |
638 | ␉}␊ |
639 | ␉else␊ |
640 | ␉{␊ |
641 | ␉␉// Couldn't detect filesystem type␊ |
642 | ␉␉result = 0;␊ |
643 | ␉}␊ |
644 | ␊ |
645 | exit:␊ |
646 | ␉if (probeBuffer != NULL)␊ |
647 | ␉{␊ |
648 | ␉␉free((void *)probeBuffer);␊ |
649 | ␉}␊ |
650 | ␊ |
651 | ␉return result;␊ |
652 | }␊ |
653 | ␊ |
654 | //==============================================================================␊ |
655 | ␊ |
656 | static BVRef newFDiskBVRef( int biosdev,␊ |
657 | int partno,␊ |
658 | unsigned int blkoff,␊ |
659 | const struct fdisk_part *part,␊ |
660 | FSInit initFunc,␊ |
661 | FSLoadFile loadFunc,␊ |
662 | FSReadFile readFunc,␊ |
663 | FSGetDirEntry getdirFunc,␊ |
664 | FSGetFileBlock getBlockFunc,␊ |
665 | FSGetUUID getUUIDFunc,␊ |
666 | BVGetDescription getDescriptionFunc,␊ |
667 | BVFree bvFreeFunc,␊ |
668 | int probe, int type, unsigned int bvrFlags )␊ |
669 | {␊ |
670 | ␉BVRef bvr = (BVRef)malloc(sizeof(*bvr));␊ |
671 | ␉if ( bvr )␊ |
672 | ␉{␊ |
673 | ␉␉bzero(bvr, sizeof(*bvr));␊ |
674 | ␊ |
675 | ␉␉bvr->biosdev = biosdev;␊ |
676 | ␉␉bvr->part_no = partno;␊ |
677 | ␉␉bvr->part_boff = blkoff;␊ |
678 | ␉␉bvr->part_type = part->systid;␊ |
679 | ␉␉bvr->fs_loadfile = loadFunc;␊ |
680 | ␉␉bvr->fs_readfile = readFunc;␊ |
681 | ␉␉bvr->fs_getdirentry = getdirFunc;␊ |
682 | ␉␉bvr->fs_getfileblock= getBlockFunc;␊ |
683 | ␉␉bvr->fs_getuuid = getUUIDFunc;␊ |
684 | ␉␉bvr->description = getDescriptionFunc;␊ |
685 | ␉␉bvr->type = type;␊ |
686 | ␉␉bvr->bv_free = bvFreeFunc;␊ |
687 | ␊ |
688 | ␉␉if ((part->bootid & FDISK_ACTIVE) && (part->systid == FDISK_HFS))␊ |
689 | ␉␉{␊ |
690 | ␉␉␉bvr->flags |= kBVFlagPrimary;␊ |
691 | ␉␉}␊ |
692 | ␊ |
693 | ␉␉// Probe the filesystem.␊ |
694 | ␊ |
695 | ␉␉if ( initFunc )␊ |
696 | ␉␉{␊ |
697 | ␉␉␉bvr->flags |= kBVFlagNativeBoot;␊ |
698 | ␊ |
699 | ␉␉␉if ( probe && initFunc( bvr ) != 0 )␊ |
700 | ␉␉␉{␊ |
701 | ␉␉␉␉// filesystem probe failed.␊ |
702 | ␊ |
703 | ␉␉␉␉DEBUG_DISK(("%s: failed probe on dev %X part %d\n", __FUNCTION__, biosdev, partno));␊ |
704 | ␊ |
705 | ␉␉␉␉(*bvr->bv_free)(bvr);␊ |
706 | ␉␉␉␉bvr = NULL;␊ |
707 | ␉␉␉}␊ |
708 | ␉␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
709 | ␉␉␉{␊ |
710 | ␉␉␉␉bvr->flags |= kBVFlagBootable;␊ |
711 | ␉␉␉}␊ |
712 | ␉␉}␊ |
713 | ␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
714 | ␉␉{␊ |
715 | ␉␉␉/*␊ |
716 | ␉␉␉ * This is an ugly place to check for exfat/FDisk, but it reads␊ |
717 | ␉␉␉ * the partition boot sector only once.␊ |
718 | ␉␉␉ *␊ |
719 | ␉␉␉if (bvr->part_type == FDISK_NTFS && EXFATProbe((void const *)0x7e00))␊ |
720 | ␉␉␉{␊ |
721 | ␉␉␉␉bvr->flags |= kBVFlagNativeBoot | kBVFlagBootable;␊ |
722 | ␉␉␉␉bvr->fs_loadfile = EXFATLoadFile;␊ |
723 | ␉␉␉␉bvr->fs_readfile = EXFATReadFile;␊ |
724 | ␉␉␉␉bvr->fs_getdirentry = EXFATGetDirEntry;␊ |
725 | ␉␉␉␉bvr->fs_getfileblock= EXFATGetFileBlock;␊ |
726 | ␉␉␉␉bvr->fs_getuuid = EXFATGetUUID;␊ |
727 | ␉␉␉␉bvr->description = EXFATGetDescription;␊ |
728 | ␉␉␉␉bvr->bv_free = EXFATFree;␊ |
729 | ␉␉␉}␊ |
730 | ␉␉␉else␊ |
731 | ␉␉␉{*/␊ |
732 | ␉␉␉␉bvr->flags |= kBVFlagForeignBoot;␊ |
733 | ␉␉␉//}␊ |
734 | ␉␉}␊ |
735 | ␉␉else␊ |
736 | ␉␉{␊ |
737 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
738 | ␉␉␉bvr = NULL;␊ |
739 | ␉␉}␊ |
740 | ␉}␊ |
741 | ␊ |
742 | ␉if ( bvr )␊ |
743 | ␉{␊ |
744 | ␉␉bvr->flags |= bvrFlags;␊ |
745 | ␉}␊ |
746 | ␊ |
747 | ␉return bvr;␊ |
748 | }␊ |
749 | ␊ |
750 | //==============================================================================␊ |
751 | ␊ |
752 | BVRef newAPMBVRef( int biosdev, int partno, unsigned int blkoff,␊ |
753 | const DPME * part,␊ |
754 | FSInit initFunc, FSLoadFile loadFunc,␊ |
755 | FSReadFile readFunc,␊ |
756 | FSGetDirEntry getdirFunc,␊ |
757 | FSGetFileBlock getBlockFunc,␊ |
758 | FSGetUUID getUUIDFunc,␊ |
759 | BVGetDescription getDescriptionFunc,␊ |
760 | BVFree bvFreeFunc,␊ |
761 | int probe, int type, unsigned int bvrFlags )␊ |
762 | {␊ |
763 | ␉BVRef bvr = (BVRef)malloc(sizeof(*bvr));␊ |
764 | ␉if ( bvr )␊ |
765 | ␉{␊ |
766 | ␉␉bzero(bvr, sizeof(*bvr));␊ |
767 | ␊ |
768 | ␉␉bvr->biosdev = biosdev;␊ |
769 | ␉␉bvr->part_no = partno;␊ |
770 | ␉␉bvr->part_boff = blkoff;␊ |
771 | ␉␉bvr->fs_loadfile = loadFunc;␊ |
772 | ␉␉bvr->fs_readfile = readFunc;␊ |
773 | ␉␉bvr->fs_getdirentry = getdirFunc;␊ |
774 | ␉␉bvr->fs_getfileblock= getBlockFunc;␊ |
775 | ␉␉bvr->fs_getuuid = getUUIDFunc;␊ |
776 | ␉␉bvr->description = getDescriptionFunc;␊ |
777 | ␉␉bvr->type = type;␊ |
778 | ␉␉bvr->bv_free = bvFreeFunc;␊ |
779 | ␉␉strlcpy(bvr->name, part->dpme_name, DPISTRLEN);␊ |
780 | ␉␉strlcpy(bvr->type_name, part->dpme_type, DPISTRLEN);␊ |
781 | ␊ |
782 | ␉␉/*␊ |
783 | ␉␉if ( part->bootid & FDISK_ACTIVE )␊ |
784 | ␉␉{␊ |
785 | ␉␉␉bvr->flags |= kBVFlagPrimary;␊ |
786 | ␉␉}␊ |
787 | ␉␉*/␊ |
788 | ␊ |
789 | ␉␉// Probe the filesystem.␊ |
790 | ␊ |
791 | ␉␉if ( initFunc )␊ |
792 | ␉␉{␊ |
793 | ␉␉␉bvr->flags |= kBVFlagNativeBoot | kBVFlagBootable | kBVFlagSystemVolume;␊ |
794 | ␊ |
795 | ␉␉␉if ( probe && initFunc( bvr ) != 0 )␊ |
796 | ␉␉␉{␊ |
797 | ␉␉␉␉// filesystem probe failed.␊ |
798 | ␊ |
799 | ␉␉␉␉DEBUG_DISK(("%s: failed probe on dev %X part %d\n", __FUNCTION__, biosdev, partno));␊ |
800 | ␊ |
801 | ␉␉␉␉(*bvr->bv_free)(bvr);␊ |
802 | ␉␉␉␉bvr = NULL;␊ |
803 | ␉␉␉}␊ |
804 | ␉␉}␊ |
805 | ␉␉/*␊ |
806 | ␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
807 | ␉␉{␊ |
808 | ␉␉␉bvr->flags |= kBVFlagForeignBoot;␊ |
809 | ␉␉}␊ |
810 | ␉␉*/␊ |
811 | ␉␉else␊ |
812 | ␉␉{␊ |
813 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
814 | ␉␉␉bvr = NULL;␊ |
815 | ␉␉}␊ |
816 | ␉}␊ |
817 | ␊ |
818 | ␉if ( bvr )␊ |
819 | ␉{␊ |
820 | ␉␉bvr->flags |= bvrFlags;␊ |
821 | ␉}␊ |
822 | ␊ |
823 | ␉return bvr;␊ |
824 | }␊ |
825 | ␊ |
826 | //==============================================================================␊ |
827 | ␊ |
828 | // GUID's in LE form:␊ |
829 | // HFS+ partition - 48465300-0000-11AA-AA11-00306543ECAC␊ |
830 | EFI_GUID const GPT_HFS_GUID␉␉= { 0x48465300, 0x0000, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF00 "Apple HFS/HFS+"␊ |
831 | ␊ |
832 | // turbo - Apple Boot Partition - 426F6F74-0000-11AA-AA11-00306543ECAC␊ |
833 | EFI_GUID const GPT_BOOT_GUID␉␉= { 0x426F6F74, 0x0000, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAB00 "Apple boot"␊ |
834 | ␊ |
835 | // turbo - or an EFI System Partition - C12A7328-F81F-11D2-BA4B-00A0C93EC93B␊ |
836 | EFI_GUID const GPT_EFISYS_GUID␉␉= { 0xC12A7328, 0xF81F, 0x11D2, { 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B } }; // 0xEF00 "EFI System"␊ |
837 | ␊ |
838 | // zef - Basic Data Partition - EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 for foreign OS support␊ |
839 | EFI_GUID const GPT_BASICDATA_GUID␉= { 0xEBD0A0A2, 0xB9E5, 0x4433, { 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7 } }; // 0x0100 "Microsoft basic data"␊ |
840 | ␊ |
841 | // Microsoft Reserved Partition - E3C9E316-0B5C-4DB8-817DF92DF00215AE␊ |
842 | EFI_GUID const GPT_BASICDATA2_GUID␉= { 0xE3C9E316, 0x0B5C, 0x4DB8, { 0x81, 0x7D, 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE } }; // 0x0C01 "Microsoft reserved"␊ |
843 | ␊ |
844 | // Apple OSX␊ |
845 | //EFI_GUID const GPT_UFS_GUID␉␉= { 0x55465300, 0x0000, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xA800 "Apple UFS"␊ |
846 | //EFI_GUID const GPT_RAID_GUID␉␉= { 0x52414944, 0x0000, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF01 "Apple RAID"␊ |
847 | //EFI_GUID const GPT_RAID_OFFLINE_GUID␉= { 0x52414944, 0x5f4f, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF02 "Apple RAID offline"␊ |
848 | //EFI_GUID const GPT_LABEL_GUID␉␉= { 0x4C616265, 0x6C00, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF03 "Apple label"␊ |
849 | //EFI_GUID const GPT_APPLETV_GUID␉= { 0x5265636F, 0x7665, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF04 "Apple TV recovery"␊ |
850 | //EFI_GUID const GPT_CORESTORAGE_GUID␉= { 0x53746F72, 0x6167, 0x11AA, { 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC } }; // 0xAF05 "Apple Core storage"␊ |
851 | // same as Apple ZFS␊ |
852 | //EFI_GUID const GPT_ZFS_GUID␉␉= { 0x6A898CC3, 0x1DD2, 0x11B2, { 0x99, 0xA6, 0x08, 0x00, 0x20, 0x73, 0x66, 0x31 } }; // 0xBF01 "Solaris /usr & Apple ZFS␊ |
853 | ␊ |
854 | BVRef newGPTBVRef( int biosdev,␊ |
855 | int partno,␊ |
856 | unsigned int blkoff,␊ |
857 | const gpt_ent *part,␊ |
858 | FSInit initFunc,␊ |
859 | FSLoadFile loadFunc,␊ |
860 | FSReadFile readFunc,␊ |
861 | FSGetDirEntry getdirFunc,␊ |
862 | FSGetFileBlock getBlockFunc,␊ |
863 | FSGetUUID getUUIDFunc,␊ |
864 | BVGetDescription getDescriptionFunc,␊ |
865 | BVFree bvFreeFunc,␊ |
866 | int probe,␊ |
867 | int type,␊ |
868 | unsigned int bvrFlags )␊ |
869 | {␊ |
870 | ␉BVRef bvr = (BVRef)malloc(sizeof(*bvr));␊ |
871 | ␉if ( bvr )␊ |
872 | ␉{␊ |
873 | ␉␉bzero(bvr, sizeof(*bvr));␊ |
874 | ␊ |
875 | ␉␉bvr->biosdev = biosdev;␊ |
876 | ␉␉bvr->part_no = partno;␊ |
877 | ␉␉bvr->part_boff = blkoff;␊ |
878 | ␉␉bvr->fs_loadfile = loadFunc;␊ |
879 | ␉␉bvr->fs_readfile = readFunc;␊ |
880 | ␉␉bvr->fs_getdirentry = getdirFunc;␊ |
881 | ␉␉bvr->fs_getfileblock = getBlockFunc;␊ |
882 | ␉␉bvr->fs_getuuid = getUUIDFunc;␊ |
883 | ␉␉bvr->description = getDescriptionFunc;␊ |
884 | ␉␉bvr->type = type;␊ |
885 | ␉␉bvr->bv_free = bvFreeFunc;␊ |
886 | ␉␉// FIXME: UCS-2 -> UTF-8 the name␊ |
887 | // Bungo:␊ |
888 | ␉␉//strlcpy(bvr->name, "----", DPISTRLEN);␊ |
889 | ␉␉uint8_t i;␊ |
890 | ␉␉for (i = 0; i < BVSTRLEN; i++ )␊ |
891 | ␉␉{␊ |
892 | ␉␉␉bvr->name[i] = (char)part->ent_name[i];␊ |
893 | ␉␉}␊ |
894 | ␊ |
895 | /*␊ |
896 | ␉␉if ( (efi_guid_compare(&GPT_BOOT_GUID, (EFI_GUID const *)part->ent_type) == 0) ||␊ |
897 | ␉␉␉(efi_guid_compare(&GPT_HFS_GUID, (EFI_GUID const*)part->ent_type) == 0) )␊ |
898 | ␉␉{␊ |
899 | ␉␉␉strlcpy(bvr->type_name, "GPT HFS+", DPISTRLEN);␊ |
900 | ␉␉}␊ |
901 | ␉␉else␊ |
902 | ␉␉{␊ |
903 | ␉␉␉strlcpy(bvr->type_name, "GPT Unknown", DPISTRLEN);␊ |
904 | ␉␉}␊ |
905 | */␊ |
906 | ␊ |
907 | ␉␉/*␊ |
908 | ␉␉if ( part->bootid & FDISK_ACTIVE )␊ |
909 | ␉␉{␊ |
910 | ␉␉␉bvr->flags |= kBVFlagPrimary;␊ |
911 | ␉␉}␊ |
912 | ␉␉*/␊ |
913 | ␊ |
914 | ␉␉// Probe the filesystem.␊ |
915 | ␊ |
916 | ␉␉if ( initFunc )␊ |
917 | ␉␉{␊ |
918 | ␉␉␉bvr->flags |= kBVFlagNativeBoot;␊ |
919 | ␊ |
920 | ␉␉␉if ( probe && initFunc( bvr ) != 0 )␊ |
921 | ␉␉␉{␊ |
922 | ␉␉␉␉// filesystem probe failed.␊ |
923 | ␊ |
924 | ␉␉␉␉DEBUG_DISK(("%s: failed probe on dev %x part %d\n", __FUNCTION__, biosdev, partno));␊ |
925 | ␊ |
926 | ␉␉␉␉(*bvr->bv_free)(bvr);␊ |
927 | ␉␉␉␉bvr = NULL;␊ |
928 | ␉␉␉}␊ |
929 | ␉␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
930 | ␉␉␉{␊ |
931 | ␉␉␉␉bvr->flags |= kBVFlagBootable;␊ |
932 | ␉␉␉}␊ |
933 | ␉␉}␊ |
934 | ␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
935 | ␉␉{␊ |
936 | ␉␉␉bvr->flags |= kBVFlagForeignBoot;␊ |
937 | ␉␉}␊ |
938 | ␉␉else␊ |
939 | ␉␉{␊ |
940 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
941 | ␉␉␉bvr = NULL;␊ |
942 | ␉␉}␊ |
943 | ␉}␊ |
944 | ␊ |
945 | ␉if ( bvr )␊ |
946 | ␉{␊ |
947 | ␉␉bvr->flags |= bvrFlags;␊ |
948 | ␉}␊ |
949 | ␊ |
950 | ␉return bvr;␊ |
951 | }␊ |
952 | ␊ |
953 | //==============================================================================␊ |
954 | ␊ |
955 | /* A note on partition numbers:␊ |
956 | * IOKit makes the primary partitions numbers 1-4, and then␊ |
957 | * extended partitions are numbered consecutively 5 and up.␊ |
958 | * So, for example, if you have two primary partitions and␊ |
959 | * one extended partition they will be numbered 1, 2, 5.␊ |
960 | */␊ |
961 | ␊ |
962 | static BVRef diskScanFDiskBootVolumes( int biosdev, int *countPtr )␊ |
963 | {␊ |
964 | ␉const struct fdisk_part␉*part;␊ |
965 | ␉struct DiskBVMap␉*map;␊ |
966 | ␉int␉␉␉partno = -1;␊ |
967 | ␉BVRef␉␉␉bvr;␊ |
968 | #if UFS_SUPPORT␊ |
969 | ␉BVRef␉␉␉booterUFS = NULL;␊ |
970 | #endif␊ |
971 | ␉int␉␉␉spc;␊ |
972 | ␉struct driveInfo␉di;␊ |
973 | ␉boot_drive_info_t␉*dp;␊ |
974 | ␊ |
975 | ␉verbose("Attempting to scan FDISK boot volumes [biosdev=%02Xh]:\n", biosdev);␊ |
976 | ␊ |
977 | ␉/* Initialize disk info */␊ |
978 | ␊ |
979 | ␉if (getDriveInfo(biosdev, &di) != 0)␊ |
980 | ␉{␊ |
981 | ␉␉return NULL;␊ |
982 | ␉}␊ |
983 | ␊ |
984 | ␉dp = &di.di;␊ |
985 | ␉spc = (dp->params.phys_spt * dp->params.phys_heads);␊ |
986 | ␊ |
987 | ␉if (spc == 0)␊ |
988 | ␉{␊ |
989 | ␉␉/* This is probably a CD-ROM; punt on the geometry. */␊ |
990 | ␉␉spc = 1;␊ |
991 | ␉}␊ |
992 | ␊ |
993 | ␉do␊ |
994 | ␉{␊ |
995 | ␉// Create a new mapping.␊ |
996 | ␊ |
997 | ␉map = (struct DiskBVMap *)malloc(sizeof(*map));␊ |
998 | ␉if ( map )␊ |
999 | {␊ |
1000 | ␉␉map->biosdev = biosdev;␊ |
1001 | ␉␉map->bvr = NULL;␊ |
1002 | ␉␉map->bvrcnt = 0;␊ |
1003 | ␉␉map->next = gDiskBVMap;␊ |
1004 | ␉␉gDiskBVMap = map;␊ |
1005 | ␊ |
1006 | ␉␉// Create a record for each partition found on the disk.␊ |
1007 | ␊ |
1008 | ␉␉while ( getNextFDiskPartition( biosdev, &partno, &part ) )␊ |
1009 | ␉␉{␊ |
1010 | ␉␉␉DEBUG_DISK(("%s: part %d [%X]\n", __FUNCTION__, partno, part->systid));␊ |
1011 | ␉␉␉bvr = 0;␊ |
1012 | ␊ |
1013 | ␉␉␉switch ( part->systid )␊ |
1014 | ␉␉␉{␊ |
1015 | #if UFS_SUPPORT␊ |
1016 | ␉␉␉␉case FDISK_UFS:␊ |
1017 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1018 | ␉␉␉␉␉␉biosdev, partno,␊ |
1019 | ␉␉␉␉␉␉part->relsect + UFS_FRONT_PORCH/BPS,␊ |
1020 | ␉␉␉␉␉␉part,␊ |
1021 | ␉␉␉␉␉␉UFSInitPartition,␊ |
1022 | ␉␉␉␉␉␉UFSLoadFile,␊ |
1023 | ␉␉␉␉␉␉UFSReadFile,␊ |
1024 | ␉␉␉␉␉␉UFSGetDirEntry,␊ |
1025 | ␉␉␉␉␉␉UFSGetFileBlock,␊ |
1026 | ␉␉␉␉␉␉UFSGetUUID,␊ |
1027 | ␉␉␉␉␉␉UFSGetDescription,␊ |
1028 | ␉␉␉␉␉␉UFSFree,␊ |
1029 | ␉␉␉␉␉␉0,␊ |
1030 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1031 | ␉␉␉␉␉␉0);␊ |
1032 | ␉␉␉␉␉break;␊ |
1033 | #endif␊ |
1034 | ␊ |
1035 | ␉␉␉␉case FDISK_HFS:␊ |
1036 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1037 | ␉␉␉␉␉␉biosdev, partno,␊ |
1038 | ␉␉␉␉␉␉part->relsect,␊ |
1039 | ␉␉␉␉␉␉part,␊ |
1040 | ␉␉␉␉␉␉HFSInitPartition,␊ |
1041 | ␉␉␉␉␉␉HFSLoadFile,␊ |
1042 | ␉␉␉␉␉␉HFSReadFile,␊ |
1043 | ␉␉␉␉␉␉HFSGetDirEntry,␊ |
1044 | ␉␉␉␉␉␉HFSGetFileBlock,␊ |
1045 | ␉␉␉␉␉␉HFSGetUUID,␊ |
1046 | ␉␉␉␉␉␉HFSGetDescription,␊ |
1047 | ␉␉␉␉␉␉HFSFree,␊ |
1048 | ␉␉␉␉␉␉0,␊ |
1049 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1050 | ␉␉␉␉␉␉0);␊ |
1051 | ␉␉␉␉␉break;␊ |
1052 | ␊ |
1053 | ␉␉␉␉// turbo - we want the booter type scanned also␊ |
1054 | ␉␉␉␉case FDISK_BOOTER:␊ |
1055 | ␉␉␉␉␉if (part->bootid & FDISK_ACTIVE)␊ |
1056 | ␉␉␉␉␉{␊ |
1057 | ␉␉␉␉␉␉gBIOSBootVolume = newFDiskBVRef(␊ |
1058 | ␉␉␉␉␉␉␉biosdev, partno,␊ |
1059 | ␉␉␉␉␉␉␉part->relsect,␊ |
1060 | ␉␉␉␉␉␉␉part,␊ |
1061 | ␉␉␉␉␉␉␉HFSInitPartition,␊ |
1062 | ␉␉␉␉␉␉␉HFSLoadFile,␊ |
1063 | ␉␉␉␉␉␉␉HFSReadFile,␊ |
1064 | ␉␉␉␉␉␉␉HFSGetDirEntry,␊ |
1065 | ␉␉␉␉␉␉␉HFSGetFileBlock,␊ |
1066 | ␉␉␉␉␉␉␉HFSGetUUID,␊ |
1067 | ␉␉␉␉␉␉␉HFSGetDescription,␊ |
1068 | ␉␉␉␉␉␉␉HFSFree,␊ |
1069 | ␉␉␉␉␉␉␉0,␊ |
1070 | ␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1071 | ␉␉␉␉␉␉␉0);␊ |
1072 | ␉␉␉␉␉}␊ |
1073 | ␊ |
1074 | #if UFS_SUPPORT␊ |
1075 | ␉␉␉␉␉booterUFS = newFDiskBVRef(␊ |
1076 | ␉␉␉␉␉␉biosdev, partno,␊ |
1077 | ␉␉␉␉␉␉((part->relsect + spc - 1) / spc) * spc,␊ |
1078 | ␉␉␉␉␉␉part,␊ |
1079 | ␉␉␉␉␉␉UFSInitPartition,␊ |
1080 | ␉␉␉␉␉␉UFSLoadFile,␊ |
1081 | ␉␉␉␉␉␉UFSReadFile,␊ |
1082 | ␉␉␉␉␉␉UFSGetDirEntry,␊ |
1083 | ␉␉␉␉␉␉UFSGetFileBlock,␊ |
1084 | ␉␉␉␉␉␉UFSGetUUID,␊ |
1085 | ␉␉␉␉␉␉UFSGetDescription,␊ |
1086 | ␉␉␉␉␉␉UFSFree,␊ |
1087 | ␉␉␉␉␉␉0,␊ |
1088 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1089 | ␉␉␉␉␉␉0);␊ |
1090 | #endif␊ |
1091 | ␉␉␉␉␉break;␊ |
1092 | ␊ |
1093 | ␉␉␉␉case FDISK_FAT32:␊ |
1094 | ␉␉␉␉case FDISK_DOS12:␊ |
1095 | ␉␉␉␉case FDISK_DOS16S:␊ |
1096 | ␉␉␉␉case FDISK_DOS16B:␊ |
1097 | ␉␉␉␉case FDISK_DOS32:␊ |
1098 | ␉␉␉␉case FDISK_FAT16:␊ |
1099 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1100 | ␉␉␉␉␉␉biosdev, partno,␊ |
1101 | ␉␉␉␉␉␉part->relsect,␊ |
1102 | ␉␉␉␉␉␉part,␊ |
1103 | ␉␉␉␉␉␉MSDOSInitPartition,␊ |
1104 | ␉␉␉␉␉␉MSDOSLoadFile,␊ |
1105 | ␉␉␉␉␉␉MSDOSReadFile,␊ |
1106 | ␉␉␉␉␉␉MSDOSGetDirEntry,␊ |
1107 | ␉␉␉␉␉␉MSDOSGetFileBlock,␊ |
1108 | ␉␉␉␉␉␉MSDOSGetUUID,␊ |
1109 | ␉␉␉␉␉␉MSDOSGetDescription,␊ |
1110 | ␉␉␉␉␉␉MSDOSFree,␊ |
1111 | ␉␉␉␉␉␉0,␊ |
1112 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1113 | ␉␉␉␉␉␉0);␊ |
1114 | ␉␉␉␉␉break;␊ |
1115 | ␊ |
1116 | ␉␉␉␉case FDISK_NTFS:␊ |
1117 | ␉␉␉␉␉if (probeFileSystem(biosdev, part->relsect) == 0xE7) // 0xE7 means 'FDISK_EXFAT'␊ |
1118 | ␉␉␉␉␉{␉bvr = newFDiskBVRef(␊ |
1119 | ␉␉␉␉␉␉␉biosdev,␊ |
1120 | ␉␉␉␉␉␉␉partno,␊ |
1121 | ␉␉␉␉␉␉␉part->relsect,␊ |
1122 | ␉␉␉␉␉␉␉part,␊ |
1123 | ␉␉␉␉␉␉␉EXFATInitPartition,␊ |
1124 | ␉␉␉␉␉␉␉EXFATLoadFile,␊ |
1125 | ␉␉␉␉␉␉␉EXFATReadFile,␊ |
1126 | ␉␉␉␉␉␉␉EXFATGetDirEntry,␊ |
1127 | ␉␉␉␉␉␉␉EXFATGetFileBlock,␊ |
1128 | ␉␉␉␉␉␉␉EXFATGetUUID,␊ |
1129 | ␉␉␉␉␉␉␉EXFATGetDescription,␊ |
1130 | ␉␉␉␉␉␉␉EXFATFree,␊ |
1131 | ␉ 0,␊ |
1132 | ␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1133 | ␉␉␉␉␉␉␉0);␊ |
1134 | ␉␉␉␉␉}␊ |
1135 | ␉␉␉␉␉else␊ |
1136 | ␉␉␉␉␉{␊ |
1137 | ␉␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1138 | ␉␉␉␉␉␉␉biosdev, partno,␊ |
1139 | ␉␉␉␉␉␉␉part->relsect,␊ |
1140 | ␉␉␉␉␉␉␉part,␊ |
1141 | ␉␉␉␉␉␉␉0, 0, 0, 0, 0,␊ |
1142 | ␉␉␉␉␉␉␉NTFSGetUUID,␊ |
1143 | ␉␉␉␉␉␉␉NTFSGetDescription,␊ |
1144 | ␉␉␉␉␉␉␉(BVFree)free,␊ |
1145 | ␉␉␉␉␉␉␉0,␊ |
1146 | ␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1147 | ␉␉␉␉␉␉␉0);␊ |
1148 | ␉␉␉␉␉}␊ |
1149 | ␉␉␉␉␉break;␊ |
1150 | ␊ |
1151 | ␉␉␉␉case FDISK_LINUX:␊ |
1152 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1153 | ␉␉␉␉␉␉biosdev, partno,␊ |
1154 | ␉␉␉␉␉␉part->relsect,␊ |
1155 | ␉␉␉␉␉␉part,␊ |
1156 | ␉␉␉␉␉␉0, 0, 0, 0, 0,␊ |
1157 | ␉␉␉␉␉␉EX2GetUUID,␊ |
1158 | ␉␉␉␉␉␉EX2GetDescription,␊ |
1159 | ␉␉␉␉␉␉(BVFree)free,␊ |
1160 | ␉␉␉␉␉␉0,␊ |
1161 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1162 | ␉␉␉␉␉␉0);␊ |
1163 | ␉␉␉␉␉break;␊ |
1164 | ␊ |
1165 | ␉␉␉␉case FDISK_BEFS:␊ |
1166 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1167 | ␉␉␉␉␉␉biosdev, partno,␊ |
1168 | ␉␉␉␉␉␉part->relsect,␊ |
1169 | ␉␉␉␉␉␉part,␊ |
1170 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0,␊ |
1171 | ␉␉␉␉␉␉BeFSGetDescription,␊ |
1172 | ␉␉␉␉␉␉(BVFree)free,␊ |
1173 | ␉␉␉␉␉␉0,␊ |
1174 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1175 | ␉␉␉␉␉␉0);␊ |
1176 | ␉␉␉␉␉break;␊ |
1177 | ␊ |
1178 | ␉␉␉␉case FDISK_FREEBSD:␊ |
1179 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1180 | ␉␉␉␉␉␉biosdev, partno,␊ |
1181 | ␉␉␉␉␉␉part->relsect,␊ |
1182 | ␉␉␉␉␉␉part,␊ |
1183 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0,␊ |
1184 | ␉␉␉␉␉␉FreeBSDGetDescription,␊ |
1185 | ␉␉␉␉␉␉(BVFree)free,␊ |
1186 | ␉␉␉␉␉␉0,␊ |
1187 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1188 | ␉␉␉␉␉␉0);␊ |
1189 | ␉␉␉␉␉break;␊ |
1190 | ␊ |
1191 | ␉␉␉␉case FDISK_OPENBSD:␊ |
1192 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1193 | ␉␉␉␉␉␉biosdev, partno,␊ |
1194 | ␉␉␉␉␉␉part->relsect,␊ |
1195 | ␉␉␉␉␉␉part,␊ |
1196 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0,␊ |
1197 | ␉␉␉␉␉␉OpenBSDGetDescription,␊ |
1198 | ␉␉␉␉␉␉(BVFree)free,␊ |
1199 | ␉␉␉␉␉␉0,␊ |
1200 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1201 | ␉␉␉␉␉␉0);␊ |
1202 | ␉␉␉␉␉break;␊ |
1203 | ␊ |
1204 | ␉␉␉␉default:␊ |
1205 | ␉␉␉␉␉bvr = newFDiskBVRef(␊ |
1206 | ␉␉␉␉␉␉biosdev, partno,␊ |
1207 | ␉␉␉␉␉␉part->relsect,␊ |
1208 | ␉␉␉␉␉␉part,␊ |
1209 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0, 0,␊ |
1210 | ␉␉␉␉␉␉(BVFree)free,␊ |
1211 | ␉␉␉␉␉␉0,␊ |
1212 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1213 | ␉␉␉␉␉␉0);␊ |
1214 | ␉␉␉␉␉break;␊ |
1215 | ␉␉␉␉}␊ |
1216 | ␊ |
1217 | ␉␉␉␉if ( bvr )␊ |
1218 | ␉␉␉␉{␊ |
1219 | ␉␉␉␉␉bvr->next = map->bvr;␊ |
1220 | ␉␉␉␉␉map->bvr = bvr;␊ |
1221 | ␉␉␉␉␉map->bvrcnt++;␊ |
1222 | ␉␉␉␉}␊ |
1223 | ␉␉␉}␊ |
1224 | ␊ |
1225 | #if UFS_SUPPORT␊ |
1226 | ␉␉␉// Booting from a CD with an UFS filesystem embedded␊ |
1227 | ␉␉␉// in a booter partition.␊ |
1228 | ␊ |
1229 | ␉␉␉if ( booterUFS )␊ |
1230 | ␉␉␉{␊ |
1231 | ␉␉␉␉if ( map->bvrcnt == 0 )␊ |
1232 | ␉␉␉␉{␊ |
1233 | ␉␉␉␉␉map->bvr = booterUFS;␊ |
1234 | ␉␉␉␉␉map->bvrcnt++;␊ |
1235 | ␉␉␉␉}␊ |
1236 | ␉␉␉␉else␊ |
1237 | ␉␉␉␉{␊ |
1238 | ␉␉␉␉␉free( booterUFS );␊ |
1239 | ␉␉␉␉}␊ |
1240 | ␉␉␉}␊ |
1241 | #endif␊ |
1242 | ␉␉}␊ |
1243 | ␉} while (0);␊ |
1244 | ␊ |
1245 | #if UNUSED␊ |
1246 | ␉/*␊ |
1247 | ␉ * If no FDisk partition, then we will check for␊ |
1248 | ␉ * an Apple partition map elsewhere.␊ |
1249 | ␉ */␊ |
1250 | ␉if (map && map->bvrcnt == 0)␊ |
1251 | ␉{␊ |
1252 | ␉␉static struct fdisk_part cdpart;␊ |
1253 | ␉␉cdpart.systid = 0xCD;␊ |
1254 | ␊ |
1255 | ␉␉/* Let's try assuming we are on a hybrid HFS/ISO9660 CD. */␊ |
1256 | ␉␉bvr = newFDiskBVRef(␊ |
1257 | ␉␉␉biosdev, 0,␊ |
1258 | ␉␉␉0,␊ |
1259 | ␉␉␉&cdpart,␊ |
1260 | ␉␉␉HFSInitPartition,␊ |
1261 | ␉␉␉HFSLoadFile,␊ |
1262 | ␉␉␉HFSReadFile,␊ |
1263 | ␉␉␉HFSGetDirEntry,␊ |
1264 | ␉␉␉HFSGetFileBlock,␊ |
1265 | ␉␉␉HFSGetUUID,␊ |
1266 | ␉␉␉HFSGetDescription,␊ |
1267 | ␉␉␉HFSFree,␊ |
1268 | ␉␉␉0,␊ |
1269 | ␉␉␉kBIOSDevTypeHardDrive, 0);␊ |
1270 | ␉␉bvr->next = map->bvr;␊ |
1271 | ␉␉map->bvr = bvr;␊ |
1272 | ␉␉map->bvrcnt++;␊ |
1273 | ␉}␊ |
1274 | #endif␊ |
1275 | ␉// Actually this should always be true given the above code␊ |
1276 | ␉// (unless malloc failed above)␊ |
1277 | ␉if (map && (map == gDiskBVMap))␊ |
1278 | ␉{␊ |
1279 | ␉␉// Don't leave a null map in the chain␊ |
1280 | ␉␉if ((map->bvrcnt == 0) && (map->bvr == NULL))␊ |
1281 | ␉␉{␊ |
1282 | ␉␉␉gDiskBVMap = map->next;␊ |
1283 | ␉␉␉free(map);␊ |
1284 | ␉␉␉map = NULL;␊ |
1285 | ␉␉}␊ |
1286 | ␉}␊ |
1287 | ␊ |
1288 | ␉if (countPtr)␊ |
1289 | ␉{␊ |
1290 | ␉␉*countPtr = map ? map->bvrcnt : 0;␊ |
1291 | ␉}␊ |
1292 | ␊ |
1293 | ␉return map ? map->bvr : NULL;␊ |
1294 | }␊ |
1295 | ␊ |
1296 | //==============================================================================␊ |
1297 | ␊ |
1298 | static BVRef diskScanAPMBootVolumes( int biosdev, int * countPtr )␊ |
1299 | {␊ |
1300 | ␉struct DiskBVMap␉*map;␊ |
1301 | ␉struct Block0␉␉*block0_p;␊ |
1302 | ␉unsigned int␉␉blksize;␊ |
1303 | ␉unsigned int␉␉factor;␊ |
1304 | ␊ |
1305 | ␉verbose("Attempting to scan APM boot volumes [biosdev=%02Xh]:\n", biosdev);␊ |
1306 | ␊ |
1307 | ␉void␉*buffer = malloc(BPS);␊ |
1308 | ␊ |
1309 | ␉if (!buffer)␊ |
1310 | ␉{␊ |
1311 | ␉␉return NULL;␊ |
1312 | ␉}␊ |
1313 | ␊ |
1314 | ␉bzero(buffer,BPS);␊ |
1315 | ␊ |
1316 | ␉/* Check for alternate block size */␊ |
1317 | ␉if (readBytes( biosdev, 0, 0, BPS, buffer ) != 0)␊ |
1318 | ␉{␊ |
1319 | ␉␉return NULL;␊ |
1320 | ␉}␊ |
1321 | ␊ |
1322 | ␉block0_p = buffer;␊ |
1323 | ␉if (OSSwapBigToHostInt16(block0_p->sbSig) == BLOCK0_SIGNATURE)␊ |
1324 | ␉{␊ |
1325 | ␉␉blksize = OSSwapBigToHostInt16(block0_p->sbBlkSize);␊ |
1326 | ␉␉if (blksize != BPS)␊ |
1327 | ␉␉{␊ |
1328 | ␉␉␉free(buffer);␊ |
1329 | ␉␉␉buffer = malloc(blksize);␊ |
1330 | ␉␉␉if (!buffer)␊ |
1331 | ␉␉␉{␊ |
1332 | ␉␉␉␉return NULL;␊ |
1333 | ␉␉␉}␊ |
1334 | ␉␉␉bzero(buffer,BPS);␊ |
1335 | ␉␉}␊ |
1336 | ␉␉factor = blksize / BPS;␊ |
1337 | ␉}␊ |
1338 | ␉else␊ |
1339 | ␉{␊ |
1340 | ␉␉blksize = BPS;␊ |
1341 | ␉␉factor = 1;␊ |
1342 | ␉}␊ |
1343 | ␊ |
1344 | ␉do␊ |
1345 | ␉{␊ |
1346 | ␉␉// Create a new mapping.␊ |
1347 | ␊ |
1348 | ␉␉map = (struct DiskBVMap *) malloc( sizeof(*map) );␊ |
1349 | ␉␉if ( map )␊ |
1350 | ␉␉{␊ |
1351 | ␉␉␉int error;␊ |
1352 | ␉␉␉DPME *dpme_p = (DPME *)buffer;␊ |
1353 | ␉␉␉UInt32 i, npart = UINT_MAX;␊ |
1354 | ␉␉␉BVRef bvr;␊ |
1355 | ␊ |
1356 | ␉␉␉map->biosdev = biosdev;␊ |
1357 | ␉␉␉map->bvr = NULL;␊ |
1358 | ␉␉␉map->bvrcnt = 0;␊ |
1359 | ␉␉␉map->next = gDiskBVMap;␊ |
1360 | ␉␉␉gDiskBVMap = map;␊ |
1361 | ␊ |
1362 | ␉␉␉for (i = 0; i < npart; i++)␊ |
1363 | ␉␉␉{␊ |
1364 | ␉␉␉␉error = readBytes( biosdev, (kAPMSector + i) * factor, 0, blksize, buffer );␊ |
1365 | ␊ |
1366 | ␉␉␉␉if (error || OSSwapBigToHostInt16(dpme_p->dpme_signature) != DPME_SIGNATURE)␊ |
1367 | ␉␉␉␉{␊ |
1368 | ␉␉␉␉␉break;␊ |
1369 | ␉␉␉␉}␊ |
1370 | ␊ |
1371 | ␉␉␉␉if (i == 0)␊ |
1372 | ␉␉␉␉{␊ |
1373 | ␉␉␉␉␉npart = OSSwapBigToHostInt32(dpme_p->dpme_map_entries);␊ |
1374 | ␉␉␉␉}␊ |
1375 | ␉␉␉␉/*␊ |
1376 | ␉␉␉␉printf("name = %s, %s%s %d -> %d [%d -> %d] {%d}\n",␊ |
1377 | ␉␉␉␉dpme.dpme_name, dpme.dpme_type, (dpme.dpme_flags & DPME_FLAGS_BOOTABLE) ? "(bootable)" : "",␊ |
1378 | ␉␉␉␉dpme.dpme_pblock_start, dpme.dpme_pblocks,␊ |
1379 | ␉␉␉␉dpme.dpme_lblock_start, dpme.dpme_lblocks,␊ |
1380 | ␉␉␉␉dpme.dpme_boot_block);␊ |
1381 | ␉␉␉␉*/␊ |
1382 | ␊ |
1383 | ␉␉␉␉if (strcmp(dpme_p->dpme_type, "Apple_HFS") == 0)␊ |
1384 | ␉␉␉␉{␊ |
1385 | ␉␉␉␉␉bvr = newAPMBVRef(biosdev,␊ |
1386 | ␉␉␉␉␉i,␊ |
1387 | ␉␉␉␉␉OSSwapBigToHostInt32(dpme_p->dpme_pblock_start) * factor,␊ |
1388 | ␉␉␉␉␉dpme_p,␊ |
1389 | ␉␉␉␉␉HFSInitPartition,␊ |
1390 | ␉␉␉␉␉HFSLoadFile,␊ |
1391 | ␉␉␉␉␉HFSReadFile,␊ |
1392 | ␉␉␉␉␉HFSGetDirEntry,␊ |
1393 | ␉␉␉␉␉HFSGetFileBlock,␊ |
1394 | ␉␉␉␉␉HFSGetUUID,␊ |
1395 | ␉␉␉␉␉HFSGetDescription,␊ |
1396 | ␉␉␉␉␉HFSFree,␊ |
1397 | ␉␉␉␉␉0,␊ |
1398 | ␉␉␉␉␉kBIOSDevTypeHardDrive, 0);␊ |
1399 | ␉␉␉␉␉bvr->next = map->bvr;␊ |
1400 | ␉␉␉␉␉map->bvr = bvr;␊ |
1401 | ␉␉␉␉␉map->bvrcnt++;␊ |
1402 | ␉␉␉␉}␊ |
1403 | ␉␉␉}␊ |
1404 | ␉␉}␊ |
1405 | ␉} while (0);␊ |
1406 | ␊ |
1407 | ␉if (buffer)␊ |
1408 | ␉{␊ |
1409 | ␉␉free(buffer);␊ |
1410 | ␉}␊ |
1411 | ␊ |
1412 | ␉if (countPtr)␊ |
1413 | ␉{␊ |
1414 | ␉␉*countPtr = map ? map->bvrcnt : 0;␊ |
1415 | ␉}␊ |
1416 | ␊ |
1417 | ␉return map ? map->bvr : NULL;␊ |
1418 | }␊ |
1419 | ␊ |
1420 | //==============================================================================␊ |
1421 | ␊ |
1422 | static bool isPartitionUsed(gpt_ent * partition)␊ |
1423 | {␊ |
1424 | ␊ |
1425 | ␉// Ask whether the given partition is used.␊ |
1426 | ␊ |
1427 | ␉return efi_guid_is_null((EFI_GUID const*)partition->ent_type) ? false : true;␊ |
1428 | }␊ |
1429 | ␊ |
1430 | //==============================================================================␊ |
1431 | ␊ |
1432 | static BVRef diskScanGPTBootVolumes(int biosdev, int * countPtr)␊ |
1433 | {␊ |
1434 | ␉verbose("Attempting to scan GPT boot volumes [biosdev=%02Xh]:\n", biosdev);␊ |
1435 | ␊ |
1436 | ␉struct DiskBVMap␉*map␉= NULL;␊ |
1437 | ␉void␉␉␉*buffer = malloc(BPS);␊ |
1438 | ␉int error;␊ |
1439 | ␊ |
1440 | ␉if ( !buffer )␊ |
1441 | ␉{␊ |
1442 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Can't allocate memory for buffer.\n");␊ |
1443 | ␉␉goto scanErr;␊ |
1444 | ␉}␊ |
1445 | ␊ |
1446 | ␉if ((error = readBytes( biosdev, /*secno*/0, 0, BPS, buffer )) != 0)␊ |
1447 | ␉{␊ |
1448 | ␉␉verbose("[diskScanGPTBootVolumes] Error(%d)! Failed to read boot sector.\n", error);␊ |
1449 | ␉␉goto scanErr;␊ |
1450 | ␉}␊ |
1451 | ␊ |
1452 | ␉struct REAL_disk_blk0 *fdiskMap = buffer;␊ |
1453 | ␉if ( OSSwapLittleToHostInt16(fdiskMap->signature) != DISK_SIGNATURE )␊ |
1454 | ␉{␊ |
1455 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Failed to find boot signature.\n");␊ |
1456 | ␉␉goto scanErr;␊ |
1457 | ␉}␊ |
1458 | ␊ |
1459 | ␉int fdiskID = 0;␊ |
1460 | ␉unsigned index;␊ |
1461 | ␉for ( index = 0; index < FDISK_NPART; index++ )␊ |
1462 | ␉{␊ |
1463 | ␉␉if ( fdiskMap->parts[index].systid ) // if used...␊ |
1464 | ␉␉{␊ |
1465 | ␉␉␉if ( fdiskMap->parts[index].systid == 0xEE ) // if GPT protective MBR...␊ |
1466 | ␉␉␉{␊ |
1467 | ␉␉␉␉// Fail if two 0xEE partitions are present which␊ |
1468 | ␉␉␉␉// means the FDISK code will wind up parsing it.␊ |
1469 | ␉␉␉␉if ( fdiskID )␊ |
1470 | ␉␉␉␉{␊ |
1471 | ␉␉␉␉␉verbose("[diskScanGPTBootVolumes] Error! Two GPT protective MBR (fdisk=0xEE) partitions found on same device, skipping.\n");␊ |
1472 | ␉␉␉␉␉goto scanErr;␊ |
1473 | ␉␉␉␉}␊ |
1474 | ␊ |
1475 | ␉␉␉␉fdiskID = index + 1;␊ |
1476 | ␉␉␉}␊ |
1477 | ␉␉}␊ |
1478 | ␉}␊ |
1479 | ␊ |
1480 | ␉if ( fdiskID == 0 )␊ |
1481 | ␉{␊ |
1482 | ␉␉verbose("[diskScanGPTBootVolumes] Error! No GPT protective MBR (fdisk=0xEE) partition found on this device, skipping.\n");␊ |
1483 | ␉␉goto scanErr;␊ |
1484 | ␉}␊ |
1485 | ␊ |
1486 | ␉//verbose("Attempting to read GPT\n");␊ |
1487 | ␊ |
1488 | ␉if ((error = readBytes(biosdev, 1, 0, BPS, buffer)) != 0)␊ |
1489 | ␉{␊ |
1490 | ␉␉verbose("[diskScanGPTBootVolumes] Error(%d)! Failed to read GPT header, skipping.\n", error);␊ |
1491 | ␉␉goto scanErr;␊ |
1492 | ␉}␊ |
1493 | ␊ |
1494 | ␉gpt_hdr *headerMap = buffer;␊ |
1495 | ␊ |
1496 | ␉char hdrSig[9];␊ |
1497 | ␉memcpy(hdrSig, headerMap->hdr_sig, 8);␊ |
1498 | ␉hdrSig[8] = 0;␊ |
1499 | ␊ |
1500 | ␉// Determine whether the partition header signature is present.␊ |
1501 | ␊ |
1502 | ␉if (memcmp(headerMap->hdr_sig, GPT_HDR_SIG, strlen(GPT_HDR_SIG)))␊ |
1503 | ␉{␊ |
1504 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Wrong GPT header signature '%s' or not present, skipping.\n", hdrSig);␊ |
1505 | ␉␉goto scanErr;␊ |
1506 | ␉}␊ |
1507 | ␊ |
1508 | ␉// Determine whether the partition header size is valid.␊ |
1509 | ␊ |
1510 | ␉UInt32 headerCheck = OSSwapLittleToHostInt32(headerMap->hdr_crc_self);␊ |
1511 | ␉UInt32 headerSize = OSSwapLittleToHostInt32(headerMap->hdr_size);␊ |
1512 | ␊ |
1513 | ␉if ((headerSize < offsetof(gpt_hdr, padding)) || (headerSize > BPS))␊ |
1514 | ␉{␊ |
1515 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Wrong GPT header size (=%d), skipping.\n", headerSize);␊ |
1516 | ␉␉goto scanErr;␊ |
1517 | ␉}␊ |
1518 | /*␊ |
1519 | ␉if ( headerSize > BPS )␊ |
1520 | ␉{␊ |
1521 | ␉␉verbose("Wrong GPT header size (%d)\n", headerSize);␊ |
1522 | ␉␉goto scanErr;␊ |
1523 | ␉}␊ |
1524 | */␊ |
1525 | ␉// Determine whether the partition header checksum is valid.␊ |
1526 | ␊ |
1527 | ␉headerMap->hdr_crc_self = 0;␊ |
1528 | ␉UInt32 headerCRC = 0;␊ |
1529 | ␊ |
1530 | ␉if ((headerCRC = crc32(0, headerMap, headerSize)) != headerCheck)␊ |
1531 | ␉{␊ |
1532 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Wrong GPT header CRC32 (=%d), skipping.\n", headerCheck);␊ |
1533 | ␉␉goto scanErr;␊ |
1534 | ␉}␊ |
1535 | ␊ |
1536 | ␉// Determine whether the partition entry size is valid.␊ |
1537 | ␊ |
1538 | ␉UInt64␉␉gptBlock␉= 0;␊ |
1539 | ␉UInt32␉␉gptCheck␉= 0;␊ |
1540 | ␉UInt32␉␉gptCount␉= 0;␊ |
1541 | ␉UInt32␉␉gptID␉␉= 0;␊ |
1542 | ␉gpt_ent␉␉*gptMap␉␉= NULL;␊ |
1543 | ␉UInt32␉␉gptSize␉␉= 0;␊ |
1544 | ␊ |
1545 | ␉gptBlock = OSSwapLittleToHostInt64(headerMap->hdr_lba_table);␊ |
1546 | ␉gptCheck = OSSwapLittleToHostInt32(headerMap->hdr_crc_table);␊ |
1547 | ␉gptCount = OSSwapLittleToHostInt32(headerMap->hdr_entries);␊ |
1548 | ␉gptSize = OSSwapLittleToHostInt32(headerMap->hdr_entsz);␊ |
1549 | ␊ |
1550 | ␉if ( gptSize < sizeof(gpt_ent) )␊ |
1551 | ␉{␊ |
1552 | ␉␉verbose("[diskScanGPTBootVolumes] Error! Wrong GPT partition entry size (=%d), skipping.\n", gptSize);␊ |
1553 | ␉␉goto scanErr;␊ |
1554 | ␉}␊ |
1555 | ␊ |
1556 | ␉// Allocate a buffer large enough to hold one map, rounded to a media block.␊ |
1557 | ␉free(buffer);␊ |
1558 | ␉buffer = NULL;␊ |
1559 | ␊ |
1560 | ␉UInt32 bufferSize = IORound(gptCount * gptSize, BPS);␊ |
1561 | ␉if (bufferSize == 0)␊ |
1562 | ␉{␊ |
1563 | ␉␉goto scanErr;␊ |
1564 | ␉}␊ |
1565 | ␉buffer = malloc(bufferSize);␊ |
1566 | ␉if (!buffer)␊ |
1567 | ␉{␊ |
1568 | goto scanErr;␊ |
1569 | ␉}␊ |
1570 | ␊ |
1571 | ␉if (readBytes(biosdev, gptBlock, 0, bufferSize, buffer) != 0)␊ |
1572 | ␉{␊ |
1573 | ␉␉goto scanErr;␊ |
1574 | ␉}␊ |
1575 | ␉//verbose("Read GPT\n");␊ |
1576 | ␊ |
1577 | ␉// Allocate a new map for this BIOS device and insert it into the chain␊ |
1578 | ␉map = malloc(sizeof(*map));␊ |
1579 | ␉if (!map)␊ |
1580 | ␉{␊ |
1581 | ␉␉goto scanErr;␊ |
1582 | ␉}␊ |
1583 | ␉map->biosdev = biosdev;␊ |
1584 | ␉map->bvr = NULL;␊ |
1585 | ␉map->bvrcnt = 0;␊ |
1586 | ␉map->next = gDiskBVMap;␊ |
1587 | ␉gDiskBVMap = map;␊ |
1588 | ␊ |
1589 | ␉// fdisk like partition type id.␊ |
1590 | ␉int fsType;␊ |
1591 | ␊ |
1592 | ␉BVRef bvr;␊ |
1593 | ␉unsigned int bvrFlags;␊ |
1594 | ␊ |
1595 | ␉for (gptID = 1; gptID <= gptCount; ++gptID)␊ |
1596 | ␉{␊ |
1597 | ␉␉bvr = NULL;␊ |
1598 | ␉␉bvrFlags = 0;␊ |
1599 | ␉␉fsType = 0;␊ |
1600 | ␊ |
1601 | ␉␉// size on disk can be larger than sizeof(gpt_ent)␊ |
1602 | ␉␉gptMap = (gpt_ent *)(buffer + ((gptID - 1) * gptSize));␊ |
1603 | ␊ |
1604 | ␉␉// NOTE: EFI_GUID's are in LE and we know we're on an x86.␊ |
1605 | ␉␉// The IOGUIDPartitionScheme.cpp code uses byte-based UUIDs, we don't.␊ |
1606 | ␊ |
1607 | ␉␉if (isPartitionUsed(gptMap))␊ |
1608 | ␉␉{␊ |
1609 | ␉␉␉// Getting fdisk like partition type.␊ |
1610 | ␉␉␉fsType = probeFileSystem(biosdev, gptMap->ent_lba_start);␊ |
1611 | ␊ |
1612 | ␉␉␉// turbo - save our booter partition␊ |
1613 | ␉␉␉// zef - only on original boot device␊ |
1614 | ␉␉␉if (efi_guid_compare(&GPT_EFISYS_GUID, (EFI_GUID const *)gptMap->ent_type) == 0)␊ |
1615 | ␉␉␉{␊ |
1616 | ␉␉␉␉switch (fsType)␊ |
1617 | ␉␉␉␉{␊ |
1618 | ␉␉␉␉␉case FDISK_HFS:␊ |
1619 | ␉␉␉␉␉␉if (readBootSector(biosdev, gptMap->ent_lba_start, (void *)0x7e00) == 0)␊ |
1620 | ␉␉␉␉␉␉{␊ |
1621 | ␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev,␊ |
1622 | ␉␉␉␉␉␉␉␉gptID,␊ |
1623 | ␉␉␉␉␉␉␉␉gptMap->ent_lba_start,␊ |
1624 | ␉␉␉␉␉␉␉␉gptMap,␊ |
1625 | ␉␉␉␉␉␉␉␉HFSInitPartition,␊ |
1626 | ␉␉␉␉␉␉␉␉HFSLoadFile,␊ |
1627 | ␉␉␉␉␉␉␉␉HFSReadFile,␊ |
1628 | ␉␉␉␉␉␉␉␉HFSGetDirEntry,␊ |
1629 | ␉␉␉␉␉␉␉␉HFSGetFileBlock,␊ |
1630 | ␉␉␉␉␉␉␉␉HFSGetUUID,␊ |
1631 | ␉␉␉␉␉␉␉␉HFSGetDescription,␊ |
1632 | ␉␉␉␉␉␉␉␉HFSFree,␊ |
1633 | ␉␉␉␉␉␉␉␉0,␊ |
1634 | ␉␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1635 | ␉␉␉␉␉␉␉␉kBVFlagEFISystem);␊ |
1636 | ␉␉␉␉␉␉}␊ |
1637 | ␉␉␉␉␉␉break;␊ |
1638 | ␊ |
1639 | ␉␉␉␉␉case FDISK_FAT32:␊ |
1640 | ␉␉␉␉␉␉if (testFAT32EFIBootSector(biosdev, gptMap->ent_lba_start, (void *)0x7e00) == 0)␊ |
1641 | ␉␉␉␉␉␉{␊ |
1642 | ␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1643 | ␉␉␉␉␉␉␉␉MSDOSInitPartition,␊ |
1644 | ␉␉␉␉␉␉␉␉MSDOSLoadFile,␊ |
1645 | ␉␉␉␉␉␉␉␉MSDOSReadFile,␊ |
1646 | ␉␉␉␉␉␉␉␉MSDOSGetDirEntry,␊ |
1647 | ␉␉␉␉␉␉␉␉MSDOSGetFileBlock,␊ |
1648 | ␉␉␉␉␉␉␉␉MSDOSGetUUID,␊ |
1649 | ␉␉␉␉␉␉␉␉MSDOSGetDescription,␊ |
1650 | ␉␉␉␉␉␉␉␉MSDOSFree,␊ |
1651 | ␉␉␉␉␉␉␉␉0,␊ |
1652 | ␉␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1653 | ␉␉␉␉␉␉␉␉kBVFlagEFISystem);␊ |
1654 | ␉␉␉␉␉␉}␊ |
1655 | ␉␉␉␉␉␉break;␊ |
1656 | ␊ |
1657 | ␉␉␉␉␉default:␊ |
1658 | ␉␉␉␉␉␉if (biosdev == gBIOSDev)␊ |
1659 | ␉␉␉␉␉␉{␊ |
1660 | ␉␉␉␉␉␉␉gBIOSBootVolume = bvr;␊ |
1661 | ␉␉␉␉␉␉}␊ |
1662 | ␉␉␉␉␉␉break;␊ |
1663 | ␉␉␉␉␉}␊ |
1664 | ␊ |
1665 | ␉␉␉␉␉if (bvr)␊ |
1666 | ␉␉␉␉␉{␊ |
1667 | ␉␉␉␉␉␉strlcpy(bvr->type_name, "EFI", BVSTRLEN);␊ |
1668 | ␉␉␉␉␉}␊ |
1669 | ␉␉␉␉}␊ |
1670 | ␊ |
1671 | ␉␉␉␉if ((efi_guid_compare(&GPT_BOOT_GUID, (EFI_GUID const *)gptMap->ent_type) == 0) || (efi_guid_compare(&GPT_HFS_GUID, (EFI_GUID const *)gptMap->ent_type) == 0))␊ |
1672 | ␉␉␉␉{␊ |
1673 | ␊ |
1674 | ␉␉␉␉␉bvrFlags = (efi_guid_compare(&GPT_BOOT_GUID, (EFI_GUID const *)gptMap->ent_type) == 0) ? kBVFlagBooter : 0;␊ |
1675 | ␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1676 | ␉␉␉␉␉␉HFSInitPartition,␊ |
1677 | ␉␉␉␉␉␉HFSLoadFile,␊ |
1678 | ␉␉␉␉␉␉HFSReadFile,␊ |
1679 | ␉␉␉␉␉␉HFSGetDirEntry,␊ |
1680 | ␉␉␉␉␉␉HFSGetFileBlock,␊ |
1681 | ␉␉␉␉␉␉HFSGetUUID,␊ |
1682 | ␉␉␉␉␉␉HFSGetDescription,␊ |
1683 | ␉␉␉␉␉␉HFSFree,␊ |
1684 | ␉␉␉␉␉␉0,␊ |
1685 | ␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1686 | ␉␉␉␉␉␉bvrFlags);␊ |
1687 | ␊ |
1688 | ␉␉␉␉␉␉if ( bvr )␊ |
1689 | ␉␉␉␉␉␉{␊ |
1690 | ␉␉␉␉␉␉␉strlcpy(bvr->type_name, (efi_guid_compare(&GPT_HFS_GUID, (EFI_GUID const *)gptMap->ent_type) == 0) ? "Apple_HFS" : "Apple_Boot", BVSTRLEN);␊ |
1691 | ␉␉␉␉␉␉}␊ |
1692 | ␉␉␉␉␉}␊ |
1693 | ␊ |
1694 | ␉␉␉␉␉// zef - foreign OS support␊ |
1695 | ␉␉␉␉␉if ((efi_guid_compare(&GPT_BASICDATA_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) || (efi_guid_compare(&GPT_BASICDATA2_GUID, (EFI_GUID const*)gptMap->ent_type) == 0))␊ |
1696 | ␉␉␉␉␉{␊ |
1697 | ␉␉␉␉␉␉switch (fsType)␊ |
1698 | ␉␉␉␉␉␉{␊ |
1699 | ␉␉␉␉␉␉␉case FDISK_NTFS:␊ |
1700 | ␉␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1701 | ␉␉␉␉␉␉␉␉␉0, 0, 0, 0, 0, NTFSGetUUID, NTFSGetDescription,␊ |
1702 | ␉␉␉␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1703 | ␊ |
1704 | ␉␉␉␉␉␉␉␉␉if (bvr)␊ |
1705 | ␉␉␉␉␉␉␉␉␉{␊ |
1706 | ␉␉␉␉␉␉␉␉␉␉strlcpy(bvr->type_name, "Microsoft Basic Data", BVSTRLEN);␊ |
1707 | ␉␉␉␉␉␉␉␉␉}␊ |
1708 | ␉␉␉␉␉␉␉␉break;␊ |
1709 | ␊ |
1710 | ␉␉␉␉␉␉␉case 0xE7: // exFAT␊ |
1711 | ␉␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1712 | ␉␉␉␉␉␉␉␉␉EXFATInitPartition,␊ |
1713 | ␉␉␉␉␉␉␉␉␉EXFATLoadFile,␊ |
1714 | ␉␉␉␉␉␉␉␉␉EXFATReadFile,␊ |
1715 | ␉␉␉␉␉␉␉␉␉EXFATGetDirEntry,␊ |
1716 | ␉␉␉␉␉␉␉␉␉EXFATGetFileBlock,␊ |
1717 | ␉␉␉␉␉␉␉␉␉EXFATGetUUID,␊ |
1718 | ␉␉␉␉␉␉␉␉␉EXFATGetDescription,␊ |
1719 | ␉␉␉␉␉␉␉␉␉EXFATFree,␊ |
1720 | ␉␉␉␉␉␉␉␉␉0,␊ |
1721 | ␉␉␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1722 | ␉␉␉␉␉␉␉␉␉0);␊ |
1723 | ␊ |
1724 | ␉␉␉␉␉␉␉␉␉if (bvr)␊ |
1725 | ␉␉␉␉␉␉␉␉␉{␊ |
1726 | ␉␉␉␉␉␉␉␉␉␉strlcpy(bvr->type_name, "Microsoft Basic Data", BVSTRLEN);␊ |
1727 | ␉␉␉␉␉␉␉␉␉}␊ |
1728 | ␉␉␉␉␉␉␉␉break;␊ |
1729 | ␊ |
1730 | ␉␉␉␉␉␉␉case FDISK_DOS12:␊ |
1731 | ␉␉␉␉␉␉␉case FDISK_DOS16B:␊ |
1732 | ␉␉␉␉␉␉␉case FDISK_FAT32:␊ |
1733 | ␉␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1734 | ␉␉␉␉␉␉␉␉␉MSDOSInitPartition,␊ |
1735 | ␉␉␉␉␉␉␉␉␉MSDOSLoadFile,␊ |
1736 | ␉␉␉␉␉␉␉␉␉MSDOSReadFile,␊ |
1737 | ␉␉␉␉␉␉␉␉␉MSDOSGetDirEntry,␊ |
1738 | ␉␉␉␉␉␉␉␉␉MSDOSGetFileBlock,␊ |
1739 | ␉␉␉␉␉␉␉␉␉MSDOSGetUUID,␊ |
1740 | ␉␉␉␉␉␉␉␉␉MSDOSGetDescription,␊ |
1741 | ␉␉␉␉␉␉␉␉␉MSDOSFree,␊ |
1742 | ␉␉␉␉␉␉␉␉␉0,␊ |
1743 | ␉␉␉␉␉␉␉␉␉kBIOSDevTypeHardDrive,␊ |
1744 | ␉␉␉␉␉␉␉␉␉0);␊ |
1745 | ␉␉␉␉␉␉␉␉break;␊ |
1746 | ␊ |
1747 | ␉␉␉␉␉␉␉case FDISK_LINUX:␊ |
1748 | ␉␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1749 | ␉␉␉␉␉␉␉␉␉0, 0, 0, 0, 0, EX2GetUUID, EX2GetDescription,␊ |
1750 | ␉␉␉␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1751 | ␉␉␉␉␉␉␉␉break;␊ |
1752 | ␊ |
1753 | ␉␉␉␉␉␉␉default:␊ |
1754 | ␉␉␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1755 | ␉␉␉␉␉␉␉␉␉0, 0, 0, 0, 0, 0, 0,␊ |
1756 | ␉␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1757 | ␉␉␉␉␉␉␉␉break;␊ |
1758 | ␉␉␉␉␉␉}␊ |
1759 | ␊ |
1760 | ␉␉␉␉␉␉if ( bvr )␊ |
1761 | ␉␉␉␉␉␉{␊ |
1762 | ␉␉␉␉␉␉␉strlcpy(bvr->type_name, (efi_guid_compare(&GPT_BASICDATA_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) ? "Microsoft Basic Data" : "Microsoft Reserved", BVSTRLEN);␊ |
1763 | ␉␉␉␉␉␉}␊ |
1764 | ␉␉␉␉␉}␊ |
1765 | ␊ |
1766 | ␉␉␉␉␉if ( bvr )␊ |
1767 | ␉␉␉␉␉{␊ |
1768 | ␉␉␉␉␉␉char stringuuid[100];␊ |
1769 | ␉␉␉␉␉␉efi_guid_unparse_upper((EFI_GUID *)gptMap->ent_uuid, stringuuid);␊ |
1770 | ␉␉␉␉␉␉verbose("[%d] %s | %s | %s | %s\n", bvr->part_no, stringuuid, bvr->type_name, getNameForValue(fdiskTypes, fsType), bvr->name);␊ |
1771 | ␊ |
1772 | ␉␉␉␉␉␉// Fixup bvr with the fake fdisk partition type.␊ |
1773 | ␉␉␉␉␉␉if (fsType > 0)␊ |
1774 | ␉␉␉␉␉␉{␊ |
1775 | ␉␉␉␉␉␉␉bvr->part_type = fsType;␊ |
1776 | ␉␉␉␉␉␉}␊ |
1777 | ␊ |
1778 | ␉␉␉␉␉␉bvr->next = map->bvr;␊ |
1779 | ␉␉␉␉␉␉map->bvr = bvr;␊ |
1780 | ␉␉␉␉␉␉++map->bvrcnt;␊ |
1781 | ␉␉␉␉␉}␊ |
1782 | ␉␉␉␉}␊ |
1783 | ␉␉␉}␊ |
1784 | ␊ |
1785 | scanErr:␊ |
1786 | ␉␉␉if ( buffer )␊ |
1787 | ␉␉␉{␊ |
1788 | ␉␉␉␉free(buffer);␊ |
1789 | ␉␉␉}␊ |
1790 | ␊ |
1791 | ␉␉␉if ( countPtr )␊ |
1792 | ␉␉␉{␊ |
1793 | ␉␉␉␉*countPtr = map ? map->bvrcnt : 0;␊ |
1794 | ␉␉␉}␊ |
1795 | ␊ |
1796 | ␉return map ? map->bvr : NULL;␊ |
1797 | }␊ |
1798 | ␊ |
1799 | //==============================================================================␊ |
1800 | ␊ |
1801 | static bool getOSVersion(BVRef bvr, char *str)␊ |
1802 | {␊ |
1803 | ␉bool valid = false;␉␊ |
1804 | ␉config_file_t systemVersion;␊ |
1805 | ␉char dirSpec[512];␊ |
1806 | ␊ |
1807 | ␉/*␊ |
1808 | ␉ * Only look for OS Version on HFS+␊ |
1809 | ␉ */␊ |
1810 | ␉if (bvr->fs_readfile != HFSReadFile)␊ |
1811 | ␉{␊ |
1812 | ␉␉return valid;␊ |
1813 | ␉}␊ |
1814 | ␊ |
1815 | ␉// OS X Recovery␊ |
1816 | ␉sprintf(dirSpec, "hd(%d,%d)/com.apple.recovery.boot/SystemVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1817 | ␊ |
1818 | ␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1819 | ␉{␊ |
1820 | ␉␉bvr->OSisInstaller = true;␊ |
1821 | ␉␉valid = true;␊ |
1822 | ␉}␊ |
1823 | ␉else␊ |
1824 | ␉{␊ |
1825 | ␉␉// OS X Standard␊ |
1826 | ␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/SystemVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1827 | ␊ |
1828 | ␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1829 | ␉␉{␊ |
1830 | ␉␉␉bvr->OSisServer = false;␊ |
1831 | ␉␉␉bvr->OSisInstaller = false;␊ |
1832 | ␉␉␉valid = true;␊ |
1833 | ␉␉}␊ |
1834 | ␉␉else␊ |
1835 | ␉␉{␊ |
1836 | ␉␉␉// OS X Server␊ |
1837 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/ServerVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1838 | ␊ |
1839 | ␉␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1840 | ␉␉␉{␊ |
1841 | ␉␉␉␉bvr->OSisServer = true;␊ |
1842 | ␉␉␉␉bvr->OSisInstaller = false;␊ |
1843 | ␉␉␉␉valid = true;␊ |
1844 | ␉␉␉}␊ |
1845 | /*␉␉␉else␊ |
1846 | ␉␉␉{␊ |
1847 | ␉␉␉␉sprintf(dirSpec, "hd(%d,%d)/.IAProductInfo", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1848 | ␊ |
1849 | ␉␉␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1850 | ␉␉␉␉{␊ |
1851 | ␉␉␉␉␉bvr->OSisInstaller = false;␊ |
1852 | ␉␉␉␉␉valid = true;␊ |
1853 | ␉␉␉␉}␊ |
1854 | ␉␉␉}␊ |
1855 | */␊ |
1856 | ␉␉}␊ |
1857 | ␉}␊ |
1858 | ␊ |
1859 | ␉const char *val = "";␊ |
1860 | ␉int len = 0;␊ |
1861 | ␉bvr->OSVersion[0] = 0;␊ |
1862 | ␊ |
1863 | ␉if (valid)␊ |
1864 | ␉{␊ |
1865 | ␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion) && len)␊ |
1866 | ␉␉{␊ |
1867 | ␉␉␉strncat(bvr->OSVersion, val, sizeof(bvr->OSVersion) - 1);␊ |
1868 | ␉␉}␊ |
1869 | ␉␉else␊ |
1870 | ␉␉{␊ |
1871 | ␉␉␉valid = false;␊ |
1872 | ␉␉}␊ |
1873 | ␉}␊ |
1874 | ␉␊ |
1875 | ␉if (!valid)␊ |
1876 | ␉{␊ |
1877 | ␉␉int fh = -1;␊ |
1878 | ␉␉sprintf(dirSpec, "hd(%d,%d)/.PhysicalMediaInstall", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1879 | ␉␉fh = open(dirSpec, 0);␊ |
1880 | ␊ |
1881 | ␉␉if (fh >= 0)␊ |
1882 | ␉␉{␊ |
1883 | ␉␉␉valid = true;␊ |
1884 | ␉␉␉bvr->OSisInstaller = true;␊ |
1885 | ␉␉␉strncat(bvr->OSVersion, "10.7", sizeof(bvr->OSVersion) - 1); // 10.7 +␊ |
1886 | ␉␉␉len = 4;␊ |
1887 | ␉␉␉close(fh);␊ |
1888 | ␉␉}␊ |
1889 | ␉␉else␊ |
1890 | ␉␉{␊ |
1891 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/.IAPhysicalMedia", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1892 | ␉␉␉fh = open(dirSpec, 0);␊ |
1893 | ␊ |
1894 | ␉␉␉if (fh >= 0)␊ |
1895 | ␉␉␉{␊ |
1896 | ␉␉␉␉valid = true;␊ |
1897 | ␉␉␉␉bvr->OSisInstaller = true;␊ |
1898 | ␉␉␉␉strncat(bvr->OSVersion, "10.9", sizeof(bvr->OSVersion) - 1); // 10.9 +␊ |
1899 | ␉␉␉␉len = 4;␊ |
1900 | ␉␉␉␉close(fh);␊ |
1901 | ␉␉␉}␊ |
1902 | ␉␉␉else␊ |
1903 | ␉␉␉{␊ |
1904 | ␉␉␉␉strncat(bvr->OSVersion, "Unknown", sizeof(bvr->OSVersion) - 1);␊ |
1905 | ␉␉␉␉len = 0;␊ |
1906 | ␉␉␉␉close(fh);␊ |
1907 | ␉␉␉}␊ |
1908 | ␉␉}␊ |
1909 | ␉}␊ |
1910 | ␊ |
1911 | ␉return valid;␊ |
1912 | }␊ |
1913 | ␊ |
1914 | //==============================================================================␊ |
1915 | ␊ |
1916 | static void scanFSLevelBVRSettings(BVRef chain)␊ |
1917 | {␊ |
1918 | ␉BVRef␉␉bvr;␊ |
1919 | ␉char␉␉dirSpec[512], fileSpec[512];␊ |
1920 | ␉char␉␉label[BVSTRLEN];␊ |
1921 | ␉int␉␉ret;␊ |
1922 | ␉long␉␉flags;␊ |
1923 | ␉u_int32_t␉time;␊ |
1924 | ␉int␉␉fh, fileSize, error;␊ |
1925 | ␊ |
1926 | ␉for (bvr = chain; bvr; bvr = bvr->next)␊ |
1927 | ␉{␊ |
1928 | ␉␉ret = -1;␊ |
1929 | ␉␉error = 0;␊ |
1930 | ␉␉// Check for alternate volume label on boot helper partitions.␊ |
1931 | ␉␉if (bvr->flags & kBVFlagBooter)␊ |
1932 | ␉␉{␊ |
1933 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1934 | ␉␉␉sprintf(fileSpec, "%s", ".disk_label.contentDetails");␊ |
1935 | ␉␉␉ret = GetFileInfo(dirSpec, fileSpec, &flags, &time);␊ |
1936 | ␉␉␉if (!ret)␊ |
1937 | ␉␉␉{␊ |
1938 | ␉␉␉␉fh = open(strcat(dirSpec, fileSpec), 0);␊ |
1939 | ␉␉␉␉fileSize = file_size(fh);␊ |
1940 | ␉␉␉␉if (fileSize > 0 && fileSize < BVSTRLEN)␊ |
1941 | ␉␉␉␉{␊ |
1942 | ␉␉␉␉␉if (read(fh, label, fileSize) != fileSize)␊ |
1943 | ␉␉␉␉␉{␊ |
1944 | ␉␉␉␉␉␉error = -1;␊ |
1945 | ␉␉␉␉␉}␊ |
1946 | ␉␉␉␉}␊ |
1947 | ␉␉␉␉else␊ |
1948 | ␉␉␉␉{␊ |
1949 | ␉␉␉␉␉error = -1;␊ |
1950 | ␉␉␉␉}␊ |
1951 | ␊ |
1952 | ␉␉␉␉close(fh);␊ |
1953 | ␊ |
1954 | ␉␉␉␉if (!error)␊ |
1955 | ␉␉␉␉{␊ |
1956 | ␉␉␉␉␉label[fileSize] = '\0';␊ |
1957 | ␉␉␉␉␉strcpy(bvr->altlabel, label);␊ |
1958 | ␉␉␉␉}␊ |
1959 | ␉␉␉}␊ |
1960 | ␉␉}␊ |
1961 | ␊ |
1962 | ␉␉// Check for SystemVersion.plist or ServerVersion.plist to determine if a volume hosts an installed system.␊ |
1963 | ␊ |
1964 | ␉␉if (bvr->flags & kBVFlagNativeBoot)␊ |
1965 | ␉␉{␊ |
1966 | ␉␉␉if (getOSVersion(bvr, bvr->OSVersion) == true)␊ |
1967 | ␉␉␉{␊ |
1968 | ␉␉␉␉bvr->flags |= kBVFlagSystemVolume;␊ |
1969 | ␉␉␉␉verbose("hd(%d,%d) %s Mac OS X %s\n", BIOS_DEV_UNIT(bvr), bvr->part_no, bvr->name, bvr->OSVersion);␊ |
1970 | ␉␉␉}␊ |
1971 | ␉␉}␊ |
1972 | ␊ |
1973 | ␉}␊ |
1974 | }␊ |
1975 | ␊ |
1976 | //==============================================================================␊ |
1977 | ␊ |
1978 | void rescanBIOSDevice(int biosdev)␊ |
1979 | {␊ |
1980 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(biosdev);␊ |
1981 | ␉if (oldMap == NULL)␊ |
1982 | ␉{␊ |
1983 | ␉␉return;␊ |
1984 | ␉}␊ |
1985 | ␊ |
1986 | ␉CacheReset();␊ |
1987 | ␉diskFreeMap(oldMap);␊ |
1988 | ␉oldMap = NULL;␊ |
1989 | ␉scanBootVolumes(biosdev, 0);␊ |
1990 | }␊ |
1991 | ␊ |
1992 | //==============================================================================␊ |
1993 | ␊ |
1994 | struct DiskBVMap* diskResetBootVolumes(int biosdev)␊ |
1995 | {␊ |
1996 | ␉struct DiskBVMap * map;␊ |
1997 | ␉struct DiskBVMap *prevMap = NULL;␊ |
1998 | ␉for ( map = gDiskBVMap; map; prevMap = map, map = map->next ) {␊ |
1999 | ␉␉if ( biosdev == map->biosdev ) {␊ |
2000 | ␉␉␉break;␊ |
2001 | ␉␉}␊ |
2002 | ␉}␊ |
2003 | ␊ |
2004 | ␉if(map != NULL) {␊ |
2005 | ␉␉verbose("Resetting BIOS device %xh\n", biosdev);␊ |
2006 | ␉␉// Reset the biosbuf cache␊ |
2007 | ␉␉cache_valid = false;␊ |
2008 | ␉␉if(map == gDiskBVMap)␊ |
2009 | ␉␉{␊ |
2010 | ␉␉␉gDiskBVMap = map->next;␊ |
2011 | ␉␉}␊ |
2012 | ␉␉else if(prevMap != NULL)␊ |
2013 | ␉␉{␊ |
2014 | ␉␉␉prevMap->next = map->next;␊ |
2015 | ␉␉}␊ |
2016 | ␉␉else␊ |
2017 | ␉␉{␊ |
2018 | ␉␉␉stop("diskResetBootVolumes error\n");␊ |
2019 | ␉␉␉return NULL;␊ |
2020 | ␉␉}␊ |
2021 | ␉}␊ |
2022 | ␉// Return the old map, either to be freed, or reinserted later␊ |
2023 | ␉return map;␊ |
2024 | }␊ |
2025 | ␊ |
2026 | //==============================================================================␊ |
2027 | ␊ |
2028 | // Frees a DiskBVMap and all of its BootVolume's␊ |
2029 | void diskFreeMap(struct DiskBVMap *map)␊ |
2030 | {␊ |
2031 | ␉if(map != NULL)␊ |
2032 | ␉{␊ |
2033 | ␉␉while(map->bvr != NULL)␊ |
2034 | ␉␉{␊ |
2035 | ␉␉␉BVRef bvr = map->bvr;␊ |
2036 | ␉␉␉map->bvr = bvr->next;␊ |
2037 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
2038 | ␉␉}␊ |
2039 | ␊ |
2040 | ␉␉free(map);␊ |
2041 | ␉}␊ |
2042 | }␊ |
2043 | ␊ |
2044 | //==============================================================================␊ |
2045 | ␊ |
2046 | BVRef diskScanBootVolumes(int biosdev, int * countPtr)␊ |
2047 | {␊ |
2048 | ␉struct DiskBVMap *map;␊ |
2049 | ␉BVRef bvr;␊ |
2050 | ␉int count = 0;␊ |
2051 | ␊ |
2052 | ␉// Find an existing mapping for this device.␊ |
2053 | ␊ |
2054 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
2055 | ␉{␊ |
2056 | ␉␉if (biosdev == map->biosdev)␊ |
2057 | ␉␉{␊ |
2058 | ␉␉␉count = map->bvrcnt;␊ |
2059 | ␉␉␉break;␊ |
2060 | ␉␉}␊ |
2061 | ␉}␊ |
2062 | ␊ |
2063 | ␉if (map == NULL)␊ |
2064 | ␉{␊ |
2065 | ␉␉bvr = diskScanGPTBootVolumes(biosdev, &count);␊ |
2066 | ␉␉if (bvr == NULL)␊ |
2067 | ␉␉{␊ |
2068 | ␉␉␉bvr = diskScanFDiskBootVolumes(biosdev, &count);␊ |
2069 | ␉␉}␊ |
2070 | ␊ |
2071 | ␉␉if (bvr == NULL)␊ |
2072 | ␉␉{␊ |
2073 | ␉␉␉bvr = diskScanAPMBootVolumes(biosdev, &count);␊ |
2074 | ␉␉}␊ |
2075 | ␊ |
2076 | ␉␉if (bvr)␊ |
2077 | ␉␉{␊ |
2078 | ␉␉␉scanFSLevelBVRSettings(bvr);␊ |
2079 | ␉␉}␊ |
2080 | ␉}␊ |
2081 | ␉else␊ |
2082 | ␉{␊ |
2083 | ␉␉bvr = map->bvr;␊ |
2084 | ␉}␊ |
2085 | ␊ |
2086 | ␉if (countPtr)␊ |
2087 | ␉{␊ |
2088 | ␉␉*countPtr += count;␊ |
2089 | ␉}␊ |
2090 | ␊ |
2091 | ␉return bvr;␊ |
2092 | }␊ |
2093 | ␊ |
2094 | //==============================================================================␊ |
2095 | ␊ |
2096 | BVRef getBVChainForBIOSDev(int biosdev)␊ |
2097 | {␊ |
2098 | ␉BVRef chain = NULL;␊ |
2099 | ␉struct DiskBVMap * map = NULL;␊ |
2100 | ␊ |
2101 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
2102 | ␉{␊ |
2103 | ␉␉if (map->biosdev == biosdev)␊ |
2104 | ␉␉{␊ |
2105 | ␉␉␉chain = map->bvr;␊ |
2106 | ␉␉␉break;␊ |
2107 | ␉␉}␊ |
2108 | ␉}␊ |
2109 | ␊ |
2110 | ␉return chain;␊ |
2111 | }␊ |
2112 | ␊ |
2113 | //==============================================================================␊ |
2114 | ␊ |
2115 | BVRef newFilteredBVChain(int minBIOSDev, int maxBIOSDev, unsigned int allowFlags, unsigned int denyFlags, int *count)␊ |
2116 | {␊ |
2117 | ␉BVRef chain = NULL;␊ |
2118 | ␉BVRef bvr = NULL;␊ |
2119 | ␉BVRef newBVR = NULL;␊ |
2120 | ␉BVRef prevBVR = NULL;␊ |
2121 | ␊ |
2122 | ␉struct DiskBVMap * map = NULL;␊ |
2123 | ␉int bvCount = 0;␊ |
2124 | ␊ |
2125 | ␉const char *raw = 0;␊ |
2126 | ␉char* val = 0;␊ |
2127 | ␉int len;␊ |
2128 | ␊ |
2129 | ␉getValueForKey(kHidePartition, &raw, &len, &bootInfo->chameleonConfig);␊ |
2130 | ␉if(raw)␊ |
2131 | ␉{␊ |
2132 | ␉␉val = XMLDecode(raw); ␊ |
2133 | ␉}␊ |
2134 | ␊ |
2135 | ␉/*␊ |
2136 | ␉ * Traverse gDISKBVmap to get references for␊ |
2137 | ␉ * individual bvr chains of each drive.␊ |
2138 | ␉ */␊ |
2139 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
2140 | ␉{␊ |
2141 | ␉␉for (bvr = map->bvr; bvr; bvr = bvr->next)␊ |
2142 | ␉␉{␊ |
2143 | ␉␉␉/*␊ |
2144 | ␉␉␉ * Save the last bvr.␊ |
2145 | ␉␉␉ */␊ |
2146 | ␉␉␉if (newBVR)␊ |
2147 | ␉␉␉{␊ |
2148 | ␉␉␉␉prevBVR = newBVR;␊ |
2149 | ␉␉␉}␊ |
2150 | ␊ |
2151 | ␉␉␉/*␊ |
2152 | ␉␉␉ * Allocate and copy the matched bvr entry into a new one.␊ |
2153 | ␉␉␉ */␊ |
2154 | ␉␉␉newBVR = (BVRef) malloc(sizeof(*newBVR));␊ |
2155 | ␉␉␉if (!newBVR)␊ |
2156 | ␉␉␉{␊ |
2157 | ␉␉␉␉continue;␊ |
2158 | ␉␉␉}␊ |
2159 | ␉␉␉bzero(newBVR,sizeof(*newBVR));␊ |
2160 | ␊ |
2161 | ␉␉␉bcopy(bvr, newBVR, sizeof(*newBVR));␊ |
2162 | ␊ |
2163 | ␉␉␉/*␊ |
2164 | ␉␉␉ * Adjust the new bvr's fields.␊ |
2165 | ␉␉␉ */␊ |
2166 | ␉␉␉newBVR->next = NULL;␊ |
2167 | ␉␉␉newBVR->filtered = true;␊ |
2168 | ␊ |
2169 | ␉␉␉if ( (!allowFlags || newBVR->flags & allowFlags)␊ |
2170 | ␉␉␉␉&& (!denyFlags || !(newBVR->flags & denyFlags) )␊ |
2171 | ␉␉␉␉&& (newBVR->biosdev >= minBIOSDev && newBVR->biosdev <= maxBIOSDev)␊ |
2172 | ␉␉␉␉)␊ |
2173 | ␉␉␉{␊ |
2174 | ␉␉␉␉newBVR->visible = true;␊ |
2175 | ␉␉␉}␊ |
2176 | ␊ |
2177 | ␉␉␉/*␊ |
2178 | ␉␉␉ * Looking for "Hide Partition" entries in 'hd(x,y)|uuid|"label" hd(m,n)|uuid|"label"' format,␊ |
2179 | ␉␉␉ * to be able to hide foreign partitions from the boot menu.␊ |
2180 | ␉␉␉ *␊ |
2181 | ␉␉␉ */␊ |
2182 | ␉␉␉if ( (newBVR->flags & kBVFlagForeignBoot) )␊ |
2183 | ␉␉␉{␊ |
2184 | ␉␉␉␉char *start, *next = val;␊ |
2185 | ␉␉␉␉long len = 0; ␊ |
2186 | ␉␉␉␉do␊ |
2187 | ␉␉␉␉{␊ |
2188 | ␉␉␉␉␉start = strbreak(next, &next, &len);␊ |
2189 | ␉␉␉␉␉if(len && matchVolumeToString(newBVR, start, len) )␊ |
2190 | ␉␉␉␉␉{␊ |
2191 | ␉␉␉␉␉␉newBVR->visible = false;␊ |
2192 | ␉␉␉␉␉}␊ |
2193 | ␉␉␉␉}␊ |
2194 | ␉␉␉␉while ( next && *next );␊ |
2195 | ␉␉␉}␊ |
2196 | ␊ |
2197 | ␉␉␉/*␊ |
2198 | ␉␉␉ * Use the first bvr entry as the starting chain pointer.␊ |
2199 | ␉␉␉ */␊ |
2200 | ␉␉␉if (!chain)␊ |
2201 | ␉␉␉{␊ |
2202 | ␉␉␉␉chain = newBVR;␊ |
2203 | ␉␉␉}␊ |
2204 | ␊ |
2205 | ␉␉␉/*␊ |
2206 | ␉␉␉ * Update the previous bvr's link pointer to use the new memory area.␊ |
2207 | ␉␉␉ */␊ |
2208 | ␉␉␉if (prevBVR)␊ |
2209 | ␉␉␉{␊ |
2210 | ␉␉␉␉prevBVR->next = newBVR;␊ |
2211 | ␉␉␉}␊ |
2212 | ␊ |
2213 | ␉␉␉if (newBVR->visible)␊ |
2214 | ␉␉␉{␊ |
2215 | ␉␉␉␉bvCount++;␊ |
2216 | ␉␉␉}␊ |
2217 | ␉␉}␊ |
2218 | ␉}␊ |
2219 | ␊ |
2220 | #if DEBUG //Azi: warning - too big for boot-log.. far too big.. i mean HUGE!! :P␊ |
2221 | ␉for (bvr = chain; bvr; bvr = bvr->next)␊ |
2222 | ␉{␊ |
2223 | ␉␉if (!bvr)␊ |
2224 | ␉␉{␊ |
2225 | ␉␉␉break;␊ |
2226 | ␉␉}␊ |
2227 | ␉␉printf(" bvr: %d, dev: %d, part: %d, flags: %d, vis: %d\n", bvr, bvr->biosdev, bvr->part_no, bvr->flags, bvr->visible);␊ |
2228 | ␉}␊ |
2229 | ␉pause("\n[DEBUG] count: %d. ", bvCount);␊ |
2230 | #endif␊ |
2231 | ␊ |
2232 | ␉*count = bvCount;␊ |
2233 | ␊ |
2234 | ␉free(val); ␊ |
2235 | ␉return chain;␊ |
2236 | }␊ |
2237 | ␊ |
2238 | //==============================================================================␊ |
2239 | ␊ |
2240 | int freeFilteredBVChain(const BVRef chain)␊ |
2241 | {␊ |
2242 | ␉int ret = 1;␊ |
2243 | ␉BVRef bvr = chain;␊ |
2244 | ␉BVRef nextBVR = NULL;␊ |
2245 | ␊ |
2246 | ␉while (bvr)␊ |
2247 | ␉{␊ |
2248 | ␉␉if (!bvr)␊ |
2249 | ␉␉{␊ |
2250 | ␉␉␉break;␊ |
2251 | ␉␉}␊ |
2252 | ␊ |
2253 | ␉␉nextBVR = bvr->next;␊ |
2254 | ␊ |
2255 | ␉␉if (bvr->filtered)␊ |
2256 | ␉␉{␊ |
2257 | ␉␉␉free(bvr);␊ |
2258 | ␉␉}␊ |
2259 | ␉␉else␊ |
2260 | ␉␉{␊ |
2261 | ␉␉␉ret = 0;␊ |
2262 | ␉␉␉break;␊ |
2263 | ␉␉}␊ |
2264 | ␊ |
2265 | ␉␉bvr = nextBVR;␊ |
2266 | ␉}␊ |
2267 | ␊ |
2268 | ␉return ret;␊ |
2269 | }␊ |
2270 | ␊ |
2271 | //==============================================================================␊ |
2272 | ␊ |
2273 | bool matchVolumeToString( BVRef bvr, const char* match, long matchLen)␊ |
2274 | {␊ |
2275 | ␉char testStr[128];␊ |
2276 | ␊ |
2277 | ␉if ( !bvr || !match || !*match)␊ |
2278 | ␉{␊ |
2279 | ␉␉return 0;␊ |
2280 | ␉}␊ |
2281 | ␊ |
2282 | ␉if ( bvr->biosdev < 0x80 || bvr->biosdev >= 0x100 )␊ |
2283 | ␉{␊ |
2284 | return 0;␊ |
2285 | ␉}␊ |
2286 | ␊ |
2287 | ␉// Try to match hd(x,y) first.␊ |
2288 | ␉sprintf(testStr, "hd(%d,%d)", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
2289 | ␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2290 | ␉{␊ |
2291 | ␉␉return true;␊ |
2292 | ␉}␊ |
2293 | ␊ |
2294 | ␉// Try to match volume UUID.␊ |
2295 | ␉if ( bvr->fs_getuuid && bvr->fs_getuuid(bvr, testStr) == 0)␊ |
2296 | ␉{␊ |
2297 | ␉␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2298 | ␉␉{␊ |
2299 | ␉␉␉return true;␊ |
2300 | ␉␉}␊ |
2301 | ␉}␊ |
2302 | ␊ |
2303 | ␉// Try to match volume label (always quoted).␊ |
2304 | ␉if ( bvr->description )␊ |
2305 | ␉{␊ |
2306 | ␉␉bvr->description(bvr, testStr, sizeof(testStr)-1);␊ |
2307 | ␉␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2308 | ␉␉{␊ |
2309 | ␉␉␉return true;␊ |
2310 | ␉␉}␊ |
2311 | ␉}␊ |
2312 | ␊ |
2313 | ␉return false;␊ |
2314 | }␊ |
2315 | ␊ |
2316 | //==============================================================================␊ |
2317 | ␊ |
2318 | /* If Rename Partition has defined an alias, then extract it for description purpose.␊ |
2319 | * The format for the rename string is the following:␊ |
2320 | * hd(x,y)|uuid|"label" "alias";hd(m,n)|uuid|"label" "alias"; etc...␊ |
2321 | */␊ |
2322 | ␊ |
2323 | bool getVolumeLabelAlias(BVRef bvr, char* str, long strMaxLen)␊ |
2324 | {␊ |
2325 | ␉char *aliasList, *entryStart, *entryNext;␊ |
2326 | ␊ |
2327 | ␉if ( !str || strMaxLen <= 0)␊ |
2328 | ␉{␊ |
2329 | ␉␉return false;␊ |
2330 | ␉}␊ |
2331 | ␊ |
2332 | ␉aliasList = XMLDecode(getStringForKey(kRenamePartition, &bootInfo->chameleonConfig));␊ |
2333 | ␉if ( !aliasList )␊ |
2334 | ␉{␊ |
2335 | ␉␉return false;␊ |
2336 | ␉}␊ |
2337 | ␊ |
2338 | ␉for ( entryStart = entryNext = aliasList; entryNext && *entryNext; entryStart = entryNext )␊ |
2339 | ␉{␊ |
2340 | ␉␉char *volStart, *volEnd, *aliasStart;␊ |
2341 | ␉␉long volLen, aliasLen;␊ |
2342 | ␊ |
2343 | ␉␉// Delimit current entry␊ |
2344 | ␉␉entryNext = strchr(entryStart, ';');␊ |
2345 | ␉␉if ( entryNext )␊ |
2346 | ␉␉{␊ |
2347 | ␉␉␉*entryNext = '\0';␊ |
2348 | ␉␉␉entryNext++;␊ |
2349 | ␉␉}␊ |
2350 | ␊ |
2351 | ␉␉volStart = strbreak(entryStart, &volEnd, &volLen);␊ |
2352 | ␉␉if(!volLen)␊ |
2353 | ␉␉{␊ |
2354 | ␉␉␉continue;␊ |
2355 | ␉␉}␊ |
2356 | ␊ |
2357 | ␉␉aliasStart = strbreak(volEnd, 0, &aliasLen);␊ |
2358 | ␉␉if(!aliasLen)␊ |
2359 | ␉␉{␊ |
2360 | ␉␉␉continue;␊ |
2361 | ␉␉}␊ |
2362 | ␊ |
2363 | ␉␉if ( matchVolumeToString(bvr, volStart, volLen) )␊ |
2364 | ␉␉{␊ |
2365 | ␉␉␉strncat(str, aliasStart, MIN(strMaxLen, aliasLen));␊ |
2366 | ␉␉␉free(aliasList);␊ |
2367 | ␊ |
2368 | ␉␉␉return true;␊ |
2369 | ␉␉}␊ |
2370 | ␉}␊ |
2371 | ␊ |
2372 | ␉free(aliasList);␊ |
2373 | ␉return false;␊ |
2374 | }␊ |
2375 | ␊ |
2376 | //==============================================================================␊ |
2377 | ␊ |
2378 | void getBootVolumeDescription( BVRef bvr, char * str, long strMaxLen, bool useDeviceDescription )␊ |
2379 | {␊ |
2380 | ␉unsigned char type;␊ |
2381 | ␉char *p = str;␊ |
2382 | ␊ |
2383 | ␉if(!bvr || !p || strMaxLen <= 0)␊ |
2384 | ␉{␊ |
2385 | ␉␉return;␊ |
2386 | ␉}␊ |
2387 | ␊ |
2388 | ␉type = (unsigned char) bvr->part_type;␊ |
2389 | ␊ |
2390 | ␉if (useDeviceDescription)␊ |
2391 | ␉{␊ |
2392 | ␉␉int len = getDeviceDescription(bvr, str);␊ |
2393 | ␉␉if(len >= strMaxLen)␊ |
2394 | ␉␉{␊ |
2395 | ␉␉␉return;␊ |
2396 | ␉␉}␊ |
2397 | ␊ |
2398 | ␉␉strcpy(str + len, bvr->OSisInstaller ? " (Installer) " : " ");␊ |
2399 | ␉␉len += bvr->OSisInstaller ? 13 : 1;␊ |
2400 | ␉␉strMaxLen -= len;␊ |
2401 | ␉␉p += len;␊ |
2402 | ␉}␊ |
2403 | ␊ |
2404 | ␉/* See if a partition rename is preferred */␊ |
2405 | ␉if (getVolumeLabelAlias(bvr, p, strMaxLen))␊ |
2406 | ␉{␊ |
2407 | ␉␉strncpy(bvr->label, p, strMaxLen); ␊ |
2408 | ␉␉return; // we're done here no need to seek for real name␊ |
2409 | ␉}␊ |
2410 | ␊ |
2411 | ␉// Get the volume label using filesystem specific functions or use the alternate volume label if available.␊ |
2412 | ␊ |
2413 | ␉if (*bvr->altlabel != '\0')␊ |
2414 | ␉{␊ |
2415 | ␉␉strncpy(p, bvr->altlabel, strMaxLen);␊ |
2416 | ␉}␊ |
2417 | ␉else if (bvr->description)␊ |
2418 | ␉{␊ |
2419 | ␉␉bvr->description(bvr, p, strMaxLen);␊ |
2420 | ␉}␊ |
2421 | ␊ |
2422 | ␉if (*p == '\0')␊ |
2423 | ␉{␊ |
2424 | ␉␉const char * name = getNameForValue( fdiskTypes, type );␊ |
2425 | ␊ |
2426 | ␉␉if (name == NULL)␊ |
2427 | ␉␉{␊ |
2428 | ␉␉␉name = bvr->type_name;␊ |
2429 | ␉␉}␊ |
2430 | ␊ |
2431 | ␉␉if (name == NULL)␊ |
2432 | ␉␉{␊ |
2433 | ␉␉␉sprintf(p, "TYPE %02X", type);␊ |
2434 | ␉␉}␊ |
2435 | ␉␉else␊ |
2436 | ␉␉{␊ |
2437 | ␉␉␉strncpy(p, name, strMaxLen);␊ |
2438 | ␉␉}␊ |
2439 | ␉}␊ |
2440 | ␊ |
2441 | ␉// Set the devices label␊ |
2442 | ␉sprintf(bvr->label, p);␊ |
2443 | }␊ |
2444 | ␊ |
2445 | ␊ |
2446 | //==============================================================================␊ |
2447 | ␊ |
2448 | int readBootSector(int biosdev, unsigned int secno, void *buffer)␊ |
2449 | {␊ |
2450 | ␉int error;␊ |
2451 | ␉struct disk_blk0 * bootSector = (struct disk_blk0 *)buffer;␊ |
2452 | ␊ |
2453 | ␉if (bootSector == NULL)␊ |
2454 | ␉{␊ |
2455 | ␉␉if (gBootSector == NULL)␊ |
2456 | ␉␉{␊ |
2457 | ␉␉␉gBootSector = (struct disk_blk0 *)malloc(sizeof(*gBootSector));␊ |
2458 | ␊ |
2459 | ␉␉␉if (gBootSector == NULL)␊ |
2460 | ␉␉␉{␊ |
2461 | ␉␉␉␉return -1;␊ |
2462 | ␉␉␉}␊ |
2463 | ␉␉}␊ |
2464 | ␊ |
2465 | ␉␉bootSector = gBootSector;␊ |
2466 | ␉}␊ |
2467 | ␊ |
2468 | ␉error = readBytes(biosdev, secno, 0, BPS, bootSector);␊ |
2469 | ␊ |
2470 | ␉if (error || bootSector->signature != DISK_SIGNATURE)␊ |
2471 | ␉{␊ |
2472 | ␉␉return -1;␊ |
2473 | ␉}␊ |
2474 | ␉return 0;␊ |
2475 | }␊ |
2476 | ␊ |
2477 | //==============================================================================␊ |
2478 | ␊ |
2479 | /*␊ |
2480 | * Format of boot1f32 block.␊ |
2481 | */␊ |
2482 | ␊ |
2483 | #define BOOT1F32_MAGIC "BOOT "␊ |
2484 | #define BOOT1F32_MAGICLEN 11␊ |
2485 | ␊ |
2486 | struct disk_boot1f32_blk␊ |
2487 | {␊ |
2488 | ␉unsigned char init[3];␊ |
2489 | ␉unsigned char fsheader[87];␊ |
2490 | ␉unsigned char magic[BOOT1F32_MAGICLEN];␊ |
2491 | ␉unsigned char bootcode[409];␊ |
2492 | ␉unsigned short signature;␊ |
2493 | };␊ |
2494 | ␊ |
2495 | //==============================================================================␊ |
2496 | ␊ |
2497 | int testFAT32EFIBootSector(int biosdev, unsigned int secno, void *buffer)␊ |
2498 | {␊ |
2499 | ␉struct disk_boot1f32_blk *bootSector = (struct disk_boot1f32_blk *)buffer;␊ |
2500 | ␉int error;␊ |
2501 | ␊ |
2502 | ␉if (bootSector == NULL)␊ |
2503 | ␉{␊ |
2504 | ␉␉if (gBootSector == NULL)␊ |
2505 | ␉␉{␊ |
2506 | ␉␉␉gBootSector = (struct disk_blk0 *)malloc(sizeof(*gBootSector));␊ |
2507 | ␉␉␉if ( gBootSector == NULL )␊ |
2508 | ␉␉␉{␊ |
2509 | ␉␉␉␉return -1;␊ |
2510 | ␉␉␉}␊ |
2511 | ␉␉}␊ |
2512 | ␉␉bootSector = (struct disk_boot1f32_blk *)gBootSector;␊ |
2513 | ␉}␊ |
2514 | ␊ |
2515 | ␉error = readBytes(biosdev, secno, 0, BPS, bootSector);␊ |
2516 | ␉if (error || bootSector->signature != DISK_SIGNATURE || strncmp((const char *)bootSector->magic, BOOT1F32_MAGIC, BOOT1F32_MAGICLEN) )␊ |
2517 | ␉{␊ |
2518 | ␉␉return -1;␊ |
2519 | ␉}␊ |
2520 | ␉return 0;␊ |
2521 | }␊ |
2522 | ␊ |
2523 | ␊ |
2524 | //==============================================================================␊ |
2525 | // Handle seek request from filesystem modules.␊ |
2526 | ␊ |
2527 | void diskSeek(BVRef bvr, long long position)␊ |
2528 | {␊ |
2529 | ␉bvr->fs_boff = position / BPS;␊ |
2530 | ␉bvr->fs_byteoff = position % BPS;␊ |
2531 | }␊ |
2532 | ␊ |
2533 | ␊ |
2534 | //==============================================================================␊ |
2535 | // Handle read request from filesystem modules.␊ |
2536 | ␊ |
2537 | int diskRead(BVRef bvr, long addr, long length)␊ |
2538 | {␊ |
2539 | ␉return readBytes(bvr->biosdev, bvr->fs_boff + bvr->part_boff, bvr->fs_byteoff, length, (void *) addr);␊ |
2540 | }␊ |
2541 | ␊ |
2542 | //==============================================================================␊ |
2543 | ␊ |
2544 | int rawDiskRead( BVRef bvr, unsigned int secno, void *buffer, unsigned int len )␊ |
2545 | {␊ |
2546 | ␉int secs;␊ |
2547 | ␉unsigned char *cbuf = (unsigned char *)buffer;␊ |
2548 | ␉unsigned int copy_len;␊ |
2549 | ␉int rc;␊ |
2550 | ␊ |
2551 | ␉if ((len & (BPS-1)) != 0)␊ |
2552 | ␉{␊ |
2553 | ␉␉error("raw disk read not sector aligned");␊ |
2554 | ␉␉return -1;␊ |
2555 | ␉}␊ |
2556 | ␉secno += bvr->part_boff;␊ |
2557 | ␊ |
2558 | ␉cache_valid = false;␊ |
2559 | ␊ |
2560 | ␉while (len > 0)␊ |
2561 | ␉{␊ |
2562 | ␉␉secs = len / BPS;␊ |
2563 | ␉␉if (secs > N_CACHE_SECS)␊ |
2564 | ␉␉{␊ |
2565 | ␉␉␉secs = N_CACHE_SECS;␊ |
2566 | ␉␉}␊ |
2567 | ␉␉copy_len = secs * BPS;␊ |
2568 | ␊ |
2569 | ␉␉//printf("rdr: ebiosread(%d, %d, %d)\n", bvr->biosdev, secno, secs);␊ |
2570 | ␉␉if ((rc = ebiosread(bvr->biosdev, secno, secs)) != 0)␊ |
2571 | ␉␉{␊ |
2572 | ␉␉␉/* Ignore corrected ECC errors */␊ |
2573 | ␉␉␉if (rc != ECC_CORRECTED_ERR)␊ |
2574 | ␉␉␉{␊ |
2575 | ␉␉␉␉error(" EBIOS read error: %s\n", bios_error(rc), rc);␊ |
2576 | ␉␉␉␉error(" Block %d Sectors %d\n", secno, secs);␊ |
2577 | ␉␉␉␉return rc;␊ |
2578 | ␉␉␉}␊ |
2579 | ␉␉}␊ |
2580 | ␉␉bcopy( trackbuf, cbuf, copy_len );␊ |
2581 | ␉␉len -= copy_len;␊ |
2582 | ␉␉cbuf += copy_len;␊ |
2583 | ␉␉secno += secs;␊ |
2584 | ␉␉spinActivityIndicator(secs);␊ |
2585 | ␉}␊ |
2586 | ␊ |
2587 | ␉return 0;␊ |
2588 | }␊ |
2589 | ␊ |
2590 | //==============================================================================␊ |
2591 | ␊ |
2592 | int rawDiskWrite( BVRef bvr, unsigned int secno, void *buffer, unsigned int len )␊ |
2593 | {␊ |
2594 | int secs;␊ |
2595 | unsigned char *cbuf = (unsigned char *)buffer;␊ |
2596 | unsigned int copy_len;␊ |
2597 | int rc;␊ |
2598 | ␊ |
2599 | ␉if ((len & (BPS-1)) != 0)␊ |
2600 | ␉{␊ |
2601 | ␉␉error("raw disk write not sector aligned");␊ |
2602 | ␉␉return -1;␊ |
2603 | ␉}␊ |
2604 | ␉secno += bvr->part_boff;␊ |
2605 | ␊ |
2606 | ␉cache_valid = false;␊ |
2607 | ␊ |
2608 | ␉while (len > 0)␊ |
2609 | ␉{␊ |
2610 | ␉␉secs = len / BPS;␊ |
2611 | ␉␉if (secs > N_CACHE_SECS)␊ |
2612 | ␉␉{␊ |
2613 | ␉␉␉secs = N_CACHE_SECS;␊ |
2614 | ␉␉}␊ |
2615 | ␉␉copy_len = secs * BPS;␊ |
2616 | ␊ |
2617 | ␉␉bcopy( cbuf, trackbuf, copy_len );␊ |
2618 | ␉␉//printf("rdr: ebioswrite(%d, %d, %d)\n", bvr->biosdev, secno, secs);␊ |
2619 | ␉␉if ((rc = ebioswrite(bvr->biosdev, secno, secs)) != 0)␊ |
2620 | ␉␉{␊ |
2621 | ␉␉␉error(" EBIOS write error: %s\n", bios_error(rc), rc);␊ |
2622 | ␉␉␉error(" Block %d Sectors %d\n", secno, secs);␊ |
2623 | ␉␉␉return rc;␊ |
2624 | ␉␉}␊ |
2625 | ␊ |
2626 | ␉␉len -= copy_len;␊ |
2627 | ␉␉cbuf += copy_len;␊ |
2628 | ␉␉secno += secs;␊ |
2629 | ␉␉spinActivityIndicator(secs);␊ |
2630 | ␉}␊ |
2631 | ␊ |
2632 | ␉return 0;␊ |
2633 | }␊ |
2634 | ␊ |
2635 | //==============================================================================␊ |
2636 | ␊ |
2637 | int diskIsCDROM(BVRef bvr)␊ |
2638 | {␊ |
2639 | ␉struct driveInfo di;␊ |
2640 | ␊ |
2641 | ␉if (getDriveInfo(bvr->biosdev, &di) == 0 && di.no_emulation)␊ |
2642 | ␉{␊ |
2643 | ␉␉return 1;␊ |
2644 | ␉}␊ |
2645 | ␉return 0;␊ |
2646 | }␊ |
2647 | ␊ |
2648 | //==============================================================================␊ |
2649 | ␊ |
2650 | int biosDevIsCDROM(int biosdev)␊ |
2651 | {␊ |
2652 | ␉struct driveInfo di;␊ |
2653 | ␊ |
2654 | ␉if (getDriveInfo(biosdev, &di) == 0 && di.no_emulation)␊ |
2655 | ␉{␊ |
2656 | ␉␉return 1;␊ |
2657 | ␉}␊ |
2658 | ␉return 0;␊ |
2659 | }␊ |
2660 | |