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