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