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