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