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 | ␉␉bzero(bvr, sizeof(*bvr));␊ |
722 | ␊ |
723 | ␉␉bvr->biosdev = biosdev;␊ |
724 | ␉␉bvr->part_no = partno;␊ |
725 | ␉␉bvr->part_boff = blkoff;␊ |
726 | ␉␉bvr->fs_loadfile = loadFunc;␊ |
727 | ␉␉bvr->fs_readfile = readFunc;␊ |
728 | ␉␉bvr->fs_getdirentry = getdirFunc;␊ |
729 | ␉␉bvr->fs_getfileblock= getBlockFunc;␊ |
730 | ␉␉bvr->fs_getuuid = getUUIDFunc;␊ |
731 | ␉␉bvr->description = getDescriptionFunc;␊ |
732 | ␉␉bvr->type = type;␊ |
733 | ␉␉bvr->bv_free = bvFreeFunc;␊ |
734 | ␉␉// FIXME: UCS-2 -> UTF-8 the name␊ |
735 | ␉␉strlcpy(bvr->name, "----", DPISTRLEN);␊ |
736 | ␉␉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) ) {␊ |
737 | ␉␉␉strlcpy(bvr->type_name, "GPT HFS+", DPISTRLEN);␊ |
738 | ␉␉} else {␊ |
739 | ␉␉␉strlcpy(bvr->type_name, "GPT Unknown", DPISTRLEN);␊ |
740 | ␉␉}␊ |
741 | ␊ |
742 | ␉␉/*␊ |
743 | ␉␉if ( part->bootid & FDISK_ACTIVE ) {␊ |
744 | ␉␉␉bvr->flags |= kBVFlagPrimary;␊ |
745 | ␉␉}␊ |
746 | ␉␉*/␊ |
747 | ␊ |
748 | ␉␉// Probe the filesystem.␊ |
749 | ␊ |
750 | ␉␉if ( initFunc )␊ |
751 | ␉␉{␊ |
752 | ␉␉␉bvr->flags |= kBVFlagNativeBoot;␊ |
753 | ␊ |
754 | ␉␉␉if ( probe && initFunc( bvr ) != 0 )␊ |
755 | ␉␉␉{␊ |
756 | ␉␉␉␉// filesystem probe failed.␊ |
757 | ␊ |
758 | ␉␉␉␉DEBUG_DISK(("%s: failed probe on dev %x part %d\n", __FUNCTION__, biosdev, partno));␊ |
759 | ␊ |
760 | ␉␉␉␉(*bvr->bv_free)(bvr);␊ |
761 | ␉␉␉␉bvr = NULL;␊ |
762 | ␉␉␉}␊ |
763 | ␉␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
764 | ␉␉␉{␊ |
765 | ␉␉␉␉bvr->flags |= kBVFlagBootable;␊ |
766 | ␉␉␉}␊ |
767 | ␉␉}␊ |
768 | ␉␉else if ( readBootSector( biosdev, blkoff, (void *)0x7e00 ) == 0 )␊ |
769 | ␉␉{␊ |
770 | ␉␉␉bvr->flags |= kBVFlagForeignBoot;␊ |
771 | ␉␉}␊ |
772 | ␉␉else␊ |
773 | ␉␉{␊ |
774 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
775 | ␉␉␉bvr = NULL;␊ |
776 | ␉␉}␊ |
777 | ␉}␊ |
778 | ␉if (bvr)␊ |
779 | ␉{␊ |
780 | ␉␉bvr->flags |= bvrFlags;␊ |
781 | ␉}␊ |
782 | ␉return bvr;␊ |
783 | }␊ |
784 | ␊ |
785 | //==============================================================================␊ |
786 | ␊ |
787 | /* A note on partition numbers:␊ |
788 | * IOKit makes the primary partitions numbers 1-4, and then␊ |
789 | * extended partitions are numbered consecutively 5 and up.␊ |
790 | * So, for example, if you have two primary partitions and␊ |
791 | * one extended partition they will be numbered 1, 2, 5.␊ |
792 | */␊ |
793 | ␊ |
794 | static BVRef diskScanFDiskBootVolumes( int biosdev, int * countPtr )␊ |
795 | {␊ |
796 | const struct fdisk_part * part;␊ |
797 | struct DiskBVMap * map;␊ |
798 | int partno = -1;␊ |
799 | BVRef bvr;␊ |
800 | #if UFS_SUPPORT␊ |
801 | BVRef booterUFS = NULL;␊ |
802 | #endif␊ |
803 | int spc;␊ |
804 | struct driveInfo di;␊ |
805 | boot_drive_info_t *dp;␊ |
806 | ␊ |
807 | ␉/* Initialize disk info */␊ |
808 | ␊ |
809 | ␉if (getDriveInfo(biosdev, &di) != 0)␊ |
810 | ␉{␊ |
811 | ␉␉return NULL;␊ |
812 | ␉}␊ |
813 | ␊ |
814 | ␉dp = &di.di;␊ |
815 | ␉spc = (dp->params.phys_spt * dp->params.phys_heads);␊ |
816 | ␊ |
817 | ␉if (spc == 0)␊ |
818 | ␉{␊ |
819 | ␉␉/* This is probably a CD-ROM; punt on the geometry. */␊ |
820 | ␉␉spc = 1;␊ |
821 | ␉}␊ |
822 | ␊ |
823 | do {␊ |
824 | // Create a new mapping.␊ |
825 | ␊ |
826 | map = (struct DiskBVMap *) malloc( sizeof(*map) );␊ |
827 | if ( map )␊ |
828 | {␊ |
829 | map->biosdev = biosdev;␊ |
830 | map->bvr = NULL;␊ |
831 | map->bvrcnt = 0;␊ |
832 | map->next = gDiskBVMap;␊ |
833 | gDiskBVMap = map;␊ |
834 | ␊ |
835 | // Create a record for each partition found on the disk.␊ |
836 | ␊ |
837 | while ( getNextFDiskPartition( biosdev, &partno, &part ) )␊ |
838 | {␊ |
839 | DEBUG_DISK(("%s: part %d [%x]\n", __FUNCTION__,␊ |
840 | partno, part->systid));␊ |
841 | bvr = 0;␊ |
842 | ␊ |
843 | switch ( part->systid )␊ |
844 | {␊ |
845 | #if UFS_SUPPORT␊ |
846 | case FDISK_UFS:␊ |
847 | bvr = newFDiskBVRef(␊ |
848 | biosdev, partno,␊ |
849 | part->relsect + UFS_FRONT_PORCH/BPS,␊ |
850 | part,␊ |
851 | UFSInitPartition,␊ |
852 | UFSLoadFile,␊ |
853 | UFSReadFile,␊ |
854 | UFSGetDirEntry,␊ |
855 | UFSGetFileBlock,␊ |
856 | UFSGetUUID,␊ |
857 | UFSGetDescription,␊ |
858 | UFSFree,␊ |
859 | 0,␊ |
860 | kBIOSDevTypeHardDrive, 0);␊ |
861 | break;␊ |
862 | #endif␊ |
863 | ␊ |
864 | case FDISK_HFS:␊ |
865 | bvr = newFDiskBVRef(␊ |
866 | biosdev, partno,␊ |
867 | part->relsect,␊ |
868 | part,␊ |
869 | HFSInitPartition,␊ |
870 | HFSLoadFile,␊ |
871 | HFSReadFile,␊ |
872 | HFSGetDirEntry,␊ |
873 | HFSGetFileBlock,␊ |
874 | HFSGetUUID,␊ |
875 | HFSGetDescription,␊ |
876 | HFSFree,␊ |
877 | 0,␊ |
878 | kBIOSDevTypeHardDrive, 0);␊ |
879 | break;␊ |
880 | ␊ |
881 | // turbo - we want the booter type scanned also␊ |
882 | case FDISK_BOOTER:␊ |
883 | if (part->bootid & FDISK_ACTIVE)␊ |
884 | gBIOSBootVolume = newFDiskBVRef(␊ |
885 | biosdev, partno,␊ |
886 | part->relsect,␊ |
887 | part,␊ |
888 | HFSInitPartition,␊ |
889 | HFSLoadFile,␊ |
890 | HFSReadFile,␊ |
891 | HFSGetDirEntry,␊ |
892 | HFSGetFileBlock,␊ |
893 | HFSGetUUID,␊ |
894 | HFSGetDescription,␊ |
895 | HFSFree,␊ |
896 | 0,␊ |
897 | kBIOSDevTypeHardDrive, 0);␊ |
898 | break;␊ |
899 | ␊ |
900 | #if UFS_SUPPORT␊ |
901 | case FDISK_BOOTER:␊ |
902 | booterUFS = newFDiskBVRef(␊ |
903 | biosdev, partno,␊ |
904 | ((part->relsect + spc - 1) / spc) * spc,␊ |
905 | part,␊ |
906 | UFSInitPartition,␊ |
907 | UFSLoadFile,␊ |
908 | UFSReadFile,␊ |
909 | UFSGetDirEntry,␊ |
910 | UFSGetFileBlock,␊ |
911 | UFSGetUUID,␊ |
912 | UFSGetDescription,␊ |
913 | UFSFree,␊ |
914 | 0,␊ |
915 | kBIOSDevTypeHardDrive, 0);␊ |
916 | break;␊ |
917 | #endif␊ |
918 | ␊ |
919 | case FDISK_FAT32:␊ |
920 | case FDISK_DOS12:␊ |
921 | case FDISK_DOS16S:␊ |
922 | case FDISK_DOS16B:␊ |
923 | case FDISK_SMALLFAT32:␊ |
924 | case FDISK_DOS16SLBA:␊ |
925 | bvr = newFDiskBVRef(␊ |
926 | biosdev, partno,␊ |
927 | part->relsect,␊ |
928 | part,␊ |
929 | MSDOSInitPartition,␊ |
930 | MSDOSLoadFile,␊ |
931 | MSDOSReadFile,␊ |
932 | MSDOSGetDirEntry,␊ |
933 | MSDOSGetFileBlock,␊ |
934 | MSDOSGetUUID,␊ |
935 | MSDOSGetDescription,␊ |
936 | MSDOSFree,␊ |
937 | 0,␊ |
938 | kBIOSDevTypeHardDrive, 0);␊ |
939 | break;␊ |
940 | ␊ |
941 | case FDISK_NTFS:␊ |
942 | bvr = newFDiskBVRef(␊ |
943 | biosdev, partno,␊ |
944 | part->relsect,␊ |
945 | part,␊ |
946 | 0, 0, 0, 0, 0,␊ |
947 | NTFSGetUUID,␊ |
948 | NTFSGetDescription,␊ |
949 | (BVFree)free,␊ |
950 | 0, kBIOSDevTypeHardDrive, 0);␊ |
951 | break;␊ |
952 | ␊ |
953 | case FDISK_LINUX:␊ |
954 | bvr = newFDiskBVRef(␊ |
955 | biosdev, partno,␊ |
956 | part->relsect,␊ |
957 | part,␊ |
958 | 0, 0, 0, 0, 0,␊ |
959 | EX2GetUUID,␊ |
960 | EX2GetDescription,␊ |
961 | (BVFree)free,␊ |
962 | 0, kBIOSDevTypeHardDrive, 0);␊ |
963 | break;␊ |
964 | ␊ |
965 | case FDISK_BEFS:␊ |
966 | bvr = newFDiskBVRef(␊ |
967 | biosdev, partno,␊ |
968 | part->relsect,␊ |
969 | part,␊ |
970 | 0, 0, 0, 0, 0, 0,␊ |
971 | BeFSGetDescription,␊ |
972 | (BVFree)free,␊ |
973 | 0, kBIOSDevTypeHardDrive, 0);␊ |
974 | break;␊ |
975 | ␊ |
976 | case FDISK_FREEBSD:␊ |
977 | bvr = newFDiskBVRef(␊ |
978 | biosdev, partno,␊ |
979 | part->relsect,␊ |
980 | part,␊ |
981 | 0, 0, 0, 0, 0, 0,␊ |
982 | FreeBSDGetDescription,␊ |
983 | (BVFree)free,␊ |
984 | 0, kBIOSDevTypeHardDrive, 0);␊ |
985 | break;␊ |
986 | ␊ |
987 | case FDISK_OPENBSD:␊ |
988 | bvr = newFDiskBVRef(␊ |
989 | biosdev, partno,␊ |
990 | part->relsect,␊ |
991 | part,␊ |
992 | 0, 0, 0, 0, 0, 0,␊ |
993 | OpenBSDGetDescription,␊ |
994 | (BVFree)free,␊ |
995 | 0, kBIOSDevTypeHardDrive, 0);␊ |
996 | break;␊ |
997 | ␊ |
998 | default:␊ |
999 | bvr = newFDiskBVRef(␊ |
1000 | biosdev, partno,␊ |
1001 | part->relsect,␊ |
1002 | part,␊ |
1003 | 0, 0, 0, 0, 0, 0, 0,␊ |
1004 | (BVFree)free,␊ |
1005 | 0,␊ |
1006 | kBIOSDevTypeHardDrive, 0);␊ |
1007 | break;␊ |
1008 | }␊ |
1009 | ␊ |
1010 | if ( bvr )␊ |
1011 | {␊ |
1012 | bvr->next = map->bvr;␊ |
1013 | map->bvr = bvr;␊ |
1014 | map->bvrcnt++;␊ |
1015 | }␊ |
1016 | }␊ |
1017 | ␊ |
1018 | #if UFS_SUPPORT␊ |
1019 | // Booting from a CD with an UFS filesystem embedded␊ |
1020 | // in a booter partition.␊ |
1021 | ␊ |
1022 | ␉␉␉if ( booterUFS )␊ |
1023 | ␉␉␉{␊ |
1024 | ␉␉␉␉if ( map->bvrcnt == 0 )␊ |
1025 | ␉␉␉␉{␊ |
1026 | ␉␉␉␉␉map->bvr = booterUFS;␊ |
1027 | ␉␉␉␉␉map->bvrcnt++;␊ |
1028 | ␉␉␉␉}␊ |
1029 | ␉␉␉␉else␊ |
1030 | ␉␉␉␉{␊ |
1031 | ␉␉␉␉␉free( booterUFS );␊ |
1032 | ␉␉␉␉}␊ |
1033 | ␉␉␉}␊ |
1034 | #endif␊ |
1035 | ␉␉}␊ |
1036 | ␉} while (0);␊ |
1037 | ␊ |
1038 | ␉/*␊ |
1039 | ␉ * If no FDisk partition, then we will check for␊ |
1040 | ␉ * an Apple partition map elsewhere.␊ |
1041 | ␉ */␊ |
1042 | #if UNUSED␊ |
1043 | ␉if (map && map->bvrcnt == 0)␊ |
1044 | ␉{␊ |
1045 | ␉␉static struct fdisk_part cdpart;␊ |
1046 | ␉␉cdpart.systid = 0xCD;␊ |
1047 | ␊ |
1048 | ␉␉/* Let's try assuming we are on a hybrid HFS/ISO9660 CD. */␊ |
1049 | ␉␉bvr = newFDiskBVRef(␊ |
1050 | ␉␉␉biosdev, 0,␊ |
1051 | ␉␉␉0,␊ |
1052 | ␉␉␉&cdpart,␊ |
1053 | ␉␉␉HFSInitPartition,␊ |
1054 | ␉␉␉HFSLoadFile,␊ |
1055 | ␉␉␉HFSReadFile,␊ |
1056 | ␉␉␉HFSGetDirEntry,␊ |
1057 | ␉␉␉HFSGetFileBlock,␊ |
1058 | ␉␉␉HFSGetUUID,␊ |
1059 | ␉␉␉HFSGetDescription,␊ |
1060 | ␉␉␉HFSFree,␊ |
1061 | ␉␉␉0,␊ |
1062 | ␉␉␉kBIOSDevTypeHardDrive, 0);␊ |
1063 | ␉␉bvr->next = map->bvr;␊ |
1064 | ␉␉map->bvr = bvr;␊ |
1065 | ␉␉map->bvrcnt++;␊ |
1066 | ␉}␊ |
1067 | #endif␊ |
1068 | ␉// Actually this should always be true given the above code␊ |
1069 | ␉// (unless malloc failed above)␊ |
1070 | ␉if(map && map == gDiskBVMap)␊ |
1071 | ␉{␊ |
1072 | ␉␉// Don't leave a null map in the chain␊ |
1073 | ␉␉if(map->bvrcnt == 0 && map->bvr == NULL)␊ |
1074 | ␉␉{␊ |
1075 | ␉␉␉gDiskBVMap = map->next;␊ |
1076 | ␉␉␉free(map);␊ |
1077 | ␉␉␉map = NULL;␊ |
1078 | ␉␉}␊ |
1079 | ␉}␊ |
1080 | ␊ |
1081 | ␉if (countPtr) *countPtr = map ? map->bvrcnt : 0;␊ |
1082 | ␊ |
1083 | ␉␉return map ? map->bvr : NULL;␊ |
1084 | }␊ |
1085 | ␊ |
1086 | //==============================================================================␊ |
1087 | ␊ |
1088 | static BVRef diskScanAPMBootVolumes( int biosdev, int * countPtr )␊ |
1089 | {␊ |
1090 | ␉struct DiskBVMap * map;␊ |
1091 | ␉struct Block0 *block0_p;␊ |
1092 | ␉unsigned int blksize;␊ |
1093 | ␉unsigned int factor;␊ |
1094 | ␉void *buffer = malloc(BPS);␊ |
1095 | ␊ |
1096 | ␉if (!buffer)␊ |
1097 | ␉{␊ |
1098 | ␉␉return NULL;␊ |
1099 | ␉}␊ |
1100 | ␉bzero(buffer,BPS);␊ |
1101 | ␊ |
1102 | ␉/* Check for alternate block size */␊ |
1103 | ␉if (readBytes( biosdev, 0, 0, BPS, buffer ) != 0)␊ |
1104 | ␉{␊ |
1105 | ␉␉return NULL;␊ |
1106 | ␉}␊ |
1107 | ␉block0_p = buffer;␊ |
1108 | ␉if (OSSwapBigToHostInt16(block0_p->sbSig) == BLOCK0_SIGNATURE)␊ |
1109 | ␉{␊ |
1110 | ␉␉blksize = OSSwapBigToHostInt16(block0_p->sbBlkSize);␊ |
1111 | ␉␉if (blksize != BPS)␊ |
1112 | ␉␉{␊ |
1113 | ␉␉␉free(buffer);␊ |
1114 | ␉␉␉buffer = malloc(blksize);␊ |
1115 | ␉␉␉if (!buffer)␊ |
1116 | ␉␉␉{␊ |
1117 | ␉␉␉␉return NULL;␊ |
1118 | ␉␉␉}␊ |
1119 | ␉␉␉bzero(buffer,BPS);␊ |
1120 | ␉␉}␊ |
1121 | ␉␉factor = blksize / BPS;␊ |
1122 | ␉}␊ |
1123 | ␉else␊ |
1124 | ␉{␊ |
1125 | ␉␉blksize = BPS;␊ |
1126 | ␉␉factor = 1;␊ |
1127 | ␉}␊ |
1128 | ␊ |
1129 | ␉do␊ |
1130 | ␉{␊ |
1131 | ␉␉// Create a new mapping.␊ |
1132 | ␊ |
1133 | ␉␉map = (struct DiskBVMap *) malloc( sizeof(*map) );␊ |
1134 | ␉␉if ( map )␊ |
1135 | ␉␉{␊ |
1136 | ␉␉␉int error;␊ |
1137 | ␉␉␉DPME *dpme_p = (DPME *)buffer;␊ |
1138 | ␉␉␉UInt32 i, npart = UINT_MAX;␊ |
1139 | ␉␉␉BVRef bvr;␊ |
1140 | ␊ |
1141 | ␉␉␉map->biosdev = biosdev;␊ |
1142 | ␉␉␉map->bvr = NULL;␊ |
1143 | ␉␉␉map->bvrcnt = 0;␊ |
1144 | ␉␉␉map->next = gDiskBVMap;␊ |
1145 | ␉␉␉gDiskBVMap = map;␊ |
1146 | ␊ |
1147 | ␉␉␉for (i=0; i<npart; i++)␊ |
1148 | ␉␉␉{␊ |
1149 | ␉␉␉␉error = readBytes( biosdev, (kAPMSector + i) * factor, 0, blksize, buffer );␊ |
1150 | ␊ |
1151 | ␉␉␉␉if (error || OSSwapBigToHostInt16(dpme_p->dpme_signature) != DPME_SIGNATURE)␊ |
1152 | ␉␉␉␉{␊ |
1153 | ␉␉␉␉␉break;␊ |
1154 | ␉␉␉␉}␊ |
1155 | ␊ |
1156 | ␉␉␉␉if (i==0)␊ |
1157 | ␉␉␉␉{␊ |
1158 | ␉␉␉␉␉npart = OSSwapBigToHostInt32(dpme_p->dpme_map_entries);␊ |
1159 | ␉␉␉␉}␊ |
1160 | ␉␉␉␉/*␊ |
1161 | ␉␉␉␉printf("name = %s, %s%s %d -> %d [%d -> %d] {%d}\n",␊ |
1162 | ␉␉␉␉dpme.dpme_name, dpme.dpme_type, (dpme.dpme_flags & DPME_FLAGS_BOOTABLE) ? "(bootable)" : "",␊ |
1163 | ␉␉␉␉dpme.dpme_pblock_start, dpme.dpme_pblocks,␊ |
1164 | ␉␉␉␉dpme.dpme_lblock_start, dpme.dpme_lblocks,␊ |
1165 | ␉␉␉␉dpme.dpme_boot_block);␊ |
1166 | ␉␉␉␉*/␊ |
1167 | ␊ |
1168 | ␉␉␉␉if (strcmp(dpme_p->dpme_type, "Apple_HFS") == 0)␊ |
1169 | ␉␉␉␉{␊ |
1170 | ␉␉␉␉␉bvr = newAPMBVRef(biosdev,␊ |
1171 | ␉␉␉␉␉i,␊ |
1172 | ␉␉␉␉␉OSSwapBigToHostInt32(dpme_p->dpme_pblock_start) * factor,␊ |
1173 | ␉␉␉␉␉dpme_p,␊ |
1174 | ␉␉␉␉␉HFSInitPartition,␊ |
1175 | ␉␉␉␉␉HFSLoadFile,␊ |
1176 | ␉␉␉␉␉HFSReadFile,␊ |
1177 | ␉␉␉␉␉HFSGetDirEntry,␊ |
1178 | ␉␉␉␉␉HFSGetFileBlock,␊ |
1179 | ␉␉␉␉␉HFSGetUUID,␊ |
1180 | ␉␉␉␉␉HFSGetDescription,␊ |
1181 | ␉␉␉␉␉HFSFree,␊ |
1182 | ␉␉␉␉␉0,␊ |
1183 | ␉␉␉␉␉kBIOSDevTypeHardDrive, 0);␊ |
1184 | ␉␉␉␉␉bvr->next = map->bvr;␊ |
1185 | ␉␉␉␉␉map->bvr = bvr;␊ |
1186 | ␉␉␉␉␉map->bvrcnt++;␊ |
1187 | ␉␉␉␉}␊ |
1188 | ␉␉␉}␊ |
1189 | ␉␉}␊ |
1190 | ␉} while (0);␊ |
1191 | ␊ |
1192 | ␉free(buffer);␊ |
1193 | ␊ |
1194 | ␉if (countPtr) *countPtr = map ? map->bvrcnt : 0;␊ |
1195 | ␊ |
1196 | ␉return map ? map->bvr : NULL;␊ |
1197 | }␊ |
1198 | ␊ |
1199 | //==============================================================================␊ |
1200 | ␊ |
1201 | /*␊ |
1202 | * Trying to figure out the filsystem type of a given partition.␊ |
1203 | */␊ |
1204 | static int probeFileSystem(int biosdev, unsigned int blkoff)␊ |
1205 | {␊ |
1206 | ␉// detected filesystem type;␊ |
1207 | ␉int result = -1;␊ |
1208 | ␉int fatbits;␊ |
1209 | ␊ |
1210 | ␉// Allocating buffer for 4 sectors.␊ |
1211 | ␉const void * probeBuffer = malloc(PROBEFS_SIZE);␊ |
1212 | ␉if (probeBuffer == NULL)␊ |
1213 | ␉{␊ |
1214 | ␉␉goto exit;␊ |
1215 | ␉}␊ |
1216 | ␊ |
1217 | ␉// Reading first 4 sectors of current partition␊ |
1218 | ␉int error = readBytes(biosdev, blkoff, 0, PROBEFS_SIZE, (void *)probeBuffer);␊ |
1219 | ␊ |
1220 | ␉if (error)␊ |
1221 | ␉{␊ |
1222 | ␉␉goto exit;␊ |
1223 | ␉}␊ |
1224 | ␊ |
1225 | ␉if (HFSProbe(probeBuffer))␊ |
1226 | ␉{␊ |
1227 | ␉␉result = FDISK_HFS;␊ |
1228 | ␉}␊ |
1229 | ␉else if (EX2Probe(probeBuffer))␊ |
1230 | ␉{␊ |
1231 | ␉␉result = FDISK_LINUX;␊ |
1232 | ␉}␊ |
1233 | ␉else if (FreeBSDProbe(probeBuffer))␊ |
1234 | ␉{␊ |
1235 | ␉␉result = FDISK_FREEBSD;␊ |
1236 | ␉}␊ |
1237 | ␊ |
1238 | ␉else if (OpenBSDProbe(probeBuffer))␊ |
1239 | ␉{␊ |
1240 | ␉␉result = FDISK_OPENBSD;␊ |
1241 | ␉}␊ |
1242 | ␊ |
1243 | ␉else if (BeFSProbe(probeBuffer))␊ |
1244 | ␉{␊ |
1245 | ␉␉result = FDISK_BEFS;␊ |
1246 | ␉}␊ |
1247 | ␊ |
1248 | ␉else if (NTFSProbe(probeBuffer))␊ |
1249 | ␉{␊ |
1250 | ␉␉result = FDISK_NTFS;␊ |
1251 | ␉}␊ |
1252 | ␊ |
1253 | ␉else if ( (fatbits = MSDOSProbe(probeBuffer)) )␊ |
1254 | ␉{␊ |
1255 | ␉␉switch (fatbits)␊ |
1256 | ␉␉{␊ |
1257 | ␉␉␉case 32:␊ |
1258 | ␉␉␉default:␊ |
1259 | ␉␉␉␉result = FDISK_FAT32;␊ |
1260 | ␉␉␉␉break;␊ |
1261 | ␉␉␉case 16:␊ |
1262 | ␉␉␉␉result = FDISK_DOS16B;␊ |
1263 | ␉␉␉␉break;␊ |
1264 | ␉␉␉case 12:␊ |
1265 | ␉␉␉␉result = FDISK_DOS12;␊ |
1266 | ␉␉␉␉break;␊ |
1267 | ␉␉}␊ |
1268 | ␉}␊ |
1269 | ␉else␊ |
1270 | ␉{␊ |
1271 | ␉␉// Couldn't detect filesystem type␊ |
1272 | ␉␉result = 0;␊ |
1273 | ␉}␊ |
1274 | ␊ |
1275 | exit:␊ |
1276 | ␉if (probeBuffer != NULL) free((void *)probeBuffer);␊ |
1277 | ␉{␊ |
1278 | ␉␉return result;␊ |
1279 | ␉}␊ |
1280 | }␊ |
1281 | ␊ |
1282 | //==============================================================================␊ |
1283 | ␊ |
1284 | static bool isPartitionUsed(gpt_ent * partition)␊ |
1285 | {␊ |
1286 | ␊ |
1287 | ␉// Ask whether the given partition is used.␊ |
1288 | ␊ |
1289 | ␉return efi_guid_is_null((EFI_GUID const*)partition->ent_type) ? false : true;␊ |
1290 | }␊ |
1291 | ␊ |
1292 | //==============================================================================␊ |
1293 | ␊ |
1294 | static BVRef diskScanGPTBootVolumes(int biosdev, int * countPtr)␊ |
1295 | {␊ |
1296 | ␉struct DiskBVMap *map = NULL;␊ |
1297 | ␊ |
1298 | ␉void *buffer = malloc(BPS);␊ |
1299 | ␊ |
1300 | ␉int error;␊ |
1301 | ␉if ( (error = readBytes( biosdev, /*secno*/0, 0, BPS, buffer )) != 0)␊ |
1302 | ␉{␊ |
1303 | ␉␉verbose("Failed to read boot sector from BIOS device %02xh. Error=%d\n", biosdev, error);␊ |
1304 | ␉␉goto scanErr;␊ |
1305 | ␉}␊ |
1306 | ␉struct REAL_disk_blk0 *fdiskMap = buffer;␊ |
1307 | ␉if ( OSSwapLittleToHostInt16(fdiskMap->signature) != DISK_SIGNATURE )␊ |
1308 | ␉{␊ |
1309 | ␉␉verbose("Failed to find boot signature on BIOS device %02xh\n", biosdev);␊ |
1310 | ␉␉goto scanErr;␊ |
1311 | ␉}␊ |
1312 | ␊ |
1313 | ␉int fdiskID = 0;␊ |
1314 | ␉unsigned index;␊ |
1315 | ␉for ( index = 0; index < FDISK_NPART; index++ )␊ |
1316 | ␉{␊ |
1317 | ␉␉if ( fdiskMap->parts[index].systid )␊ |
1318 | ␉␉{␊ |
1319 | ␉␉␉if ( fdiskMap->parts[index].systid == 0xEE )␊ |
1320 | ␉␉␉{␊ |
1321 | ␉␉␉␉// Fail if two 0xEE partitions are present which␊ |
1322 | ␉␉␉␉// means the FDISK code will wind up parsing it.␊ |
1323 | ␉␉␉␉if ( fdiskID )␊ |
1324 | ␉␉␉␉{␊ |
1325 | ␉␉␉␉␉goto scanErr;␊ |
1326 | ␉␉␉␉}␊ |
1327 | ␊ |
1328 | ␉␉␉␉fdiskID = index + 1;␊ |
1329 | ␉␉␉}␊ |
1330 | ␉␉}␊ |
1331 | ␉}␊ |
1332 | ␊ |
1333 | ␉if ( fdiskID == 0 )␊ |
1334 | ␉{␊ |
1335 | ␉␉goto scanErr;␊ |
1336 | ␉}␊ |
1337 | ␊ |
1338 | ␉verbose("Attempting to read GPT\n");␊ |
1339 | ␊ |
1340 | ␉if(readBytes(biosdev, 1, 0, BPS, buffer) != 0)␊ |
1341 | ␉{␊ |
1342 | ␉␉goto scanErr;␊ |
1343 | ␉}␊ |
1344 | ␊ |
1345 | ␉gpt_hdr *headerMap = buffer;␊ |
1346 | ␊ |
1347 | ␉// Determine whether the partition header signature is present.␊ |
1348 | ␊ |
1349 | ␉if ( memcmp(headerMap->hdr_sig, GPT_HDR_SIG, strlen(GPT_HDR_SIG)) )␊ |
1350 | ␉{␊ |
1351 | ␉␉goto scanErr;␊ |
1352 | ␉}␊ |
1353 | ␊ |
1354 | ␉// Determine whether the partition header size is valid.␊ |
1355 | ␊ |
1356 | ␉UInt32 headerCheck = OSSwapLittleToHostInt32(headerMap->hdr_crc_self);␊ |
1357 | ␉UInt32 headerSize = OSSwapLittleToHostInt32(headerMap->hdr_size);␊ |
1358 | ␊ |
1359 | ␉if ( headerSize < offsetof(gpt_hdr, padding) )␊ |
1360 | ␉{␊ |
1361 | ␉␉goto scanErr;␊ |
1362 | ␉}␊ |
1363 | ␊ |
1364 | ␉if ( headerSize > BPS )␊ |
1365 | ␉{␊ |
1366 | ␉␉goto scanErr;␊ |
1367 | ␉}␊ |
1368 | ␊ |
1369 | ␉// Determine whether the partition header checksum is valid.␊ |
1370 | ␊ |
1371 | ␉headerMap->hdr_crc_self = 0;␊ |
1372 | ␊ |
1373 | ␉if ( crc32(0, headerMap, headerSize) != headerCheck )␊ |
1374 | ␉{␊ |
1375 | ␉␉goto scanErr;␊ |
1376 | ␉}␊ |
1377 | ␊ |
1378 | ␉// Determine whether the partition entry size is valid.␊ |
1379 | ␊ |
1380 | ␉UInt64 gptBlock = 0;␊ |
1381 | ␉UInt32 gptCheck = 0;␊ |
1382 | ␉UInt32 gptCount = 0;␊ |
1383 | ␉UInt32 gptID = 0;␊ |
1384 | ␉gpt_ent * gptMap = 0;␊ |
1385 | ␉UInt32 gptSize = 0;␊ |
1386 | ␊ |
1387 | ␉gptBlock = OSSwapLittleToHostInt64(headerMap->hdr_lba_table);␊ |
1388 | ␉gptCheck = OSSwapLittleToHostInt32(headerMap->hdr_crc_table);␊ |
1389 | ␉gptCount = OSSwapLittleToHostInt32(headerMap->hdr_entries);␊ |
1390 | ␉gptSize = OSSwapLittleToHostInt32(headerMap->hdr_entsz);␊ |
1391 | ␊ |
1392 | ␉if ( gptSize < sizeof(gpt_ent) )␊ |
1393 | ␉{␊ |
1394 | ␉␉goto scanErr;␊ |
1395 | ␉}␊ |
1396 | ␊ |
1397 | ␉// Allocate a buffer large enough to hold one map, rounded to a media block.␊ |
1398 | ␉free(buffer);␊ |
1399 | ␉buffer = NULL;␊ |
1400 | ␊ |
1401 | ␉UInt32 bufferSize = IORound(gptCount * gptSize, BPS);␊ |
1402 | ␉if (bufferSize == 0)␊ |
1403 | ␉{␊ |
1404 | ␉␉goto scanErr;␊ |
1405 | ␉}␊ |
1406 | ␉buffer = malloc(bufferSize);␊ |
1407 | ␉if (!buffer)␊ |
1408 | ␉{␊ |
1409 | ␉goto scanErr;␊ |
1410 | ␉}␊ |
1411 | ␊ |
1412 | ␉if (readBytes(biosdev, gptBlock, 0, bufferSize, buffer) != 0)␊ |
1413 | ␉{␊ |
1414 | ␉␉goto scanErr;␊ |
1415 | ␉}␊ |
1416 | ␉verbose("Read GPT\n");␊ |
1417 | ␊ |
1418 | ␉// Allocate a new map for this BIOS device and insert it into the chain␊ |
1419 | ␉map = malloc(sizeof(*map));␊ |
1420 | ␉if (!map)␊ |
1421 | ␉{␊ |
1422 | ␉␉goto scanErr;␊ |
1423 | ␉}␊ |
1424 | ␉map->biosdev = biosdev;␊ |
1425 | ␉map->bvr = NULL;␊ |
1426 | ␉map->bvrcnt = 0;␊ |
1427 | ␉map->next = gDiskBVMap;␊ |
1428 | ␉gDiskBVMap = map;␊ |
1429 | ␊ |
1430 | ␉// fdisk like partition type id.␊ |
1431 | ␉int fsType = 0;␊ |
1432 | ␊ |
1433 | ␉for(gptID = 1; gptID <= gptCount; ++gptID) {␊ |
1434 | ␉␉BVRef bvr = NULL;␊ |
1435 | ␉␉unsigned int bvrFlags = 0;␊ |
1436 | ␊ |
1437 | ␉␉// size on disk can be larger than sizeof(gpt_ent)␊ |
1438 | ␉␉gptMap = (gpt_ent *) ( buffer + ( (gptID - 1) * gptSize) );␊ |
1439 | ␊ |
1440 | ␉␉// NOTE: EFI_GUID's are in LE and we know we're on an x86.␊ |
1441 | ␉␉// The IOGUIDPartitionScheme.cpp code uses byte-based UUIDs, we don't.␊ |
1442 | ␊ |
1443 | ␉␉if (isPartitionUsed(gptMap)) {␊ |
1444 | ␉␉␉char stringuuid[100];␊ |
1445 | ␉␉␉efi_guid_unparse_upper((EFI_GUID*)gptMap->ent_type, stringuuid);␊ |
1446 | ␉␉␉verbose("Reading GPT partition %d, type %s\n", gptID, stringuuid);␊ |
1447 | ␊ |
1448 | ␉␉␉// Getting fdisk like partition type.␊ |
1449 | ␉␉␉fsType = probeFileSystem(biosdev, gptMap->ent_lba_start);␊ |
1450 | ␊ |
1451 | ␉␉␉␉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) ) {␊ |
1452 | ␉␉␉␉␉bvrFlags = (efi_guid_compare(&GPT_BOOT_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) ? kBVFlagBooter : 0;␊ |
1453 | ␉␉␉␉␉bvr = newGPTBVRef(biosdev,␊ |
1454 | ␉␉␉␉␉gptID,␊ |
1455 | ␉␉␉␉␉gptMap->ent_lba_start,␊ |
1456 | ␉␉␉␉␉gptMap,␊ |
1457 | ␉␉␉␉␉HFSInitPartition,␊ |
1458 | ␉␉␉␉␉HFSLoadFile,␊ |
1459 | ␉␉␉␉␉HFSReadFile,␊ |
1460 | ␉␉␉␉␉HFSGetDirEntry,␊ |
1461 | ␉␉␉␉␉HFSGetFileBlock,␊ |
1462 | ␉␉␉␉␉HFSGetUUID,␊ |
1463 | ␉␉␉␉␉HFSGetDescription,␊ |
1464 | ␉␉␉␉␉HFSFree,␊ |
1465 | ␉␉␉␉␉0,␊ |
1466 | ␉␉␉␉␉kBIOSDevTypeHardDrive, bvrFlags);␊ |
1467 | ␉␉␉␉}␊ |
1468 | ␊ |
1469 | ␉␉␉// zef - foreign OS support␊ |
1470 | ␉␉␉if ( (efi_guid_compare(&GPT_BASICDATA_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) ||␊ |
1471 | ␉␉␉(efi_guid_compare(&GPT_BASICDATA2_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) ) {␊ |
1472 | ␉␉␉␉switch (fsType)␊ |
1473 | ␉␉␉␉{␊ |
1474 | ␉␉␉␉␉case FDISK_NTFS:␊ |
1475 | ␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1476 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0, NTFSGetDescription,␊ |
1477 | ␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1478 | ␉␉␉␉␉␉break;␊ |
1479 | ␊ |
1480 | ␉␉␉␉␉case FDISK_LINUX:␊ |
1481 | ␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1482 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0, EX2GetDescription,␊ |
1483 | ␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1484 | ␉␉␉␉␉␉break;␊ |
1485 | ␊ |
1486 | ␉␉␉␉␉default:␊ |
1487 | ␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1488 | ␉␉␉␉␉␉0, 0, 0, 0, 0, 0, 0,␊ |
1489 | ␉␉␉␉␉␉(BVFree)free, 0, kBIOSDevTypeHardDrive, 0);␊ |
1490 | ␉␉␉␉␉␉break;␊ |
1491 | ␉␉␉␉}␊ |
1492 | ␊ |
1493 | ␉␉␉}␊ |
1494 | ␊ |
1495 | ␉␉␉// turbo - save our booter partition␊ |
1496 | ␉␉␉// zef - only on original boot device␊ |
1497 | ␉␉␉if ( (efi_guid_compare(&GPT_EFISYS_GUID, (EFI_GUID const*)gptMap->ent_type) == 0) ) {␊ |
1498 | ␉␉␉␉switch (fsType) {␊ |
1499 | ␉␉␉␉␉case FDISK_HFS:␊ |
1500 | ␉␉␉␉␉if (readBootSector( biosdev, gptMap->ent_lba_start, (void *)0x7e00 ) == 0) {␊ |
1501 | ␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1502 | ␉␉␉␉␉␉HFSInitPartition,␊ |
1503 | ␉␉␉␉␉␉HFSLoadFile,␊ |
1504 | ␉␉␉␉␉␉HFSReadFile,␊ |
1505 | ␉␉␉␉␉␉HFSGetDirEntry,␊ |
1506 | ␉␉␉␉␉␉HFSGetFileBlock,␊ |
1507 | ␉␉␉␉␉␉HFSGetUUID,␊ |
1508 | ␉␉␉␉␉␉HFSGetDescription,␊ |
1509 | ␉␉␉␉␉␉HFSFree,␊ |
1510 | ␉␉␉␉␉␉0, kBIOSDevTypeHardDrive, kBVFlagEFISystem);␊ |
1511 | ␉␉␉␉␉}␊ |
1512 | ␉␉␉␉␉break;␊ |
1513 | ␊ |
1514 | ␉␉␉␉␉case FDISK_FAT32:␊ |
1515 | ␉␉␉␉␉if (testFAT32EFIBootSector( biosdev, gptMap->ent_lba_start, (void *)0x7e00 ) == 0) {␊ |
1516 | ␉␉␉␉␉␉bvr = newGPTBVRef(biosdev, gptID, gptMap->ent_lba_start, gptMap,␊ |
1517 | ␉␉␉␉␉␉MSDOSInitPartition,␊ |
1518 | ␉␉␉␉␉␉MSDOSLoadFile,␊ |
1519 | ␉␉␉␉␉␉MSDOSReadFile,␊ |
1520 | ␉␉␉␉␉␉MSDOSGetDirEntry,␊ |
1521 | ␉␉␉␉␉␉MSDOSGetFileBlock,␊ |
1522 | ␉␉␉␉␉␉MSDOSGetUUID,␊ |
1523 | ␉␉␉␉␉␉MSDOSGetDescription,␊ |
1524 | ␉␉␉␉␉␉MSDOSFree,␊ |
1525 | ␉␉␉␉␉␉0, kBIOSDevTypeHardDrive, kBVFlagEFISystem);␊ |
1526 | ␉␉␉␉␉}␊ |
1527 | ␉␉␉␉␉break;␊ |
1528 | ␊ |
1529 | ␉␉␉␉␉default:␊ |
1530 | ␉␉␉␉␉if (biosdev == gBIOSDev) {␊ |
1531 | ␉␉␉␉␉␉gBIOSBootVolume = bvr;␊ |
1532 | ␉␉␉␉␉}␊ |
1533 | ␉␉␉␉␉break;␊ |
1534 | ␉␉␉␉}␊ |
1535 | ␉␉␉}␊ |
1536 | ␊ |
1537 | ␉␉␉if (bvr)␊ |
1538 | ␉␉␉{␊ |
1539 | ␉␉␉␉// Fixup bvr with the fake fdisk partition type.␊ |
1540 | ␉␉␉␉if (fsType > 0) {␊ |
1541 | ␉␉␉␉␉bvr->part_type = fsType;␊ |
1542 | ␉␉␉␉}␊ |
1543 | ␊ |
1544 | ␉␉␉␉bvr->next = map->bvr;␊ |
1545 | ␉␉␉␉map->bvr = bvr;␊ |
1546 | ␉␉␉␉++map->bvrcnt;␊ |
1547 | ␉␉␉}␊ |
1548 | ␊ |
1549 | ␉␉}␊ |
1550 | ␉}␊ |
1551 | ␊ |
1552 | ␉scanErr:␊ |
1553 | ␉if (buffer) {␊ |
1554 | ␉␉free(buffer);␊ |
1555 | ␉}␊ |
1556 | ␊ |
1557 | ␉if(map) {␊ |
1558 | ␉␉if(countPtr) *countPtr = map->bvrcnt;␊ |
1559 | ␉␉{␊ |
1560 | ␉␉␉return map->bvr;␊ |
1561 | ␉␉}␊ |
1562 | ␊ |
1563 | ␉} else {␊ |
1564 | ␉␉if(countPtr) *countPtr = 0;␊ |
1565 | ␉␉{␊ |
1566 | ␉␉␉return NULL;␊ |
1567 | ␉␉}␊ |
1568 | ␉}␊ |
1569 | }␊ |
1570 | ␊ |
1571 | //==============================================================================␊ |
1572 | ␊ |
1573 | static bool getOSVersion(BVRef bvr, char *str)␊ |
1574 | {␊ |
1575 | ␉bool valid = false;␉␊ |
1576 | ␉config_file_t systemVersion;␊ |
1577 | ␉char dirSpec[512];␊ |
1578 | ␊ |
1579 | ␉sprintf(dirSpec, "hd(%d,%d)/com.apple.recovery.boot/SystemVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1580 | ␊ |
1581 | ␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1582 | ␉{␊ |
1583 | ␉␉bvr->OSisInstaller = true;␊ |
1584 | ␉␉valid = true;␊ |
1585 | ␉}␊ |
1586 | ␊ |
1587 | ␉if (!valid)␊ |
1588 | ␉{␊ |
1589 | ␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/SystemVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1590 | ␊ |
1591 | ␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1592 | ␉␉{␊ |
1593 | ␉␉␉bvr->OSisInstaller = true;␊ |
1594 | ␉␉␉valid = true;␊ |
1595 | ␉␉}␊ |
1596 | ␉␉else␊ |
1597 | ␉␉{␊ |
1598 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/ServerVersion.plist", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1599 | ␊ |
1600 | ␉␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1601 | ␉␉␉{␊ |
1602 | ␉␉␉␉bvr->OSisServer = true;␊ |
1603 | ␉␉␉␉valid = true;␊ |
1604 | ␉␉␉}␊ |
1605 | /*␉␉␉else␊ |
1606 | ␉␉␉{␊ |
1607 | ␉␉␉␉sprintf(dirSpec, "hd(%d,%d)/.IAProductInfo", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1608 | ␊ |
1609 | ␉␉␉␉if (!loadConfigFile(dirSpec, &systemVersion))␊ |
1610 | ␉␉␉␉{␊ |
1611 | ␊ |
1612 | ␉␉␉␉}␊ |
1613 | ␉␉␉}␊ |
1614 | */␊ |
1615 | ␉␉}␊ |
1616 | ␉}␊ |
1617 | ␊ |
1618 | ␉if (valid)␊ |
1619 | ␉{␊ |
1620 | ␉␉const char *val;␊ |
1621 | ␉␉int len;␊ |
1622 | ␉␉␊ |
1623 | ␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
1624 | ␉␉{␊ |
1625 | ␉␉␉// getValueForKey uses const char for val␊ |
1626 | ␉␉␉// so copy it and trim␊ |
1627 | ␉␉␉*str = '\0';␊ |
1628 | ␉␉␉// crazybirdy␊ |
1629 | ␉␉␉if (len > 4 && (val[3] == '1'))␊ |
1630 | ␉␉␉{␊ |
1631 | ␉␉␉␉strncat(str, val, MIN(len, 5));␊ |
1632 | ␉␉␉}␊ |
1633 | ␉␉␉else␊ |
1634 | ␉␉␉{␊ |
1635 | ␉␉␉␉strncat(str, val, MIN(len, 4));␊ |
1636 | ␉␉␉}␊ |
1637 | ␉␉}␊ |
1638 | ␉␉else␊ |
1639 | ␉␉{␊ |
1640 | ␉␉␉valid = false;␊ |
1641 | ␉␉}␊ |
1642 | ␉}␊ |
1643 | ␉␊ |
1644 | ␉if(!valid)␊ |
1645 | ␉{␊ |
1646 | ␉␉int fh = -1;␊ |
1647 | ␉␉sprintf(dirSpec, "hd(%d,%d)/.PhysicalMediaInstall", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1648 | ␉␉fh = open(dirSpec, 0);␊ |
1649 | ␊ |
1650 | ␉␉if (fh >= 0)␊ |
1651 | ␉␉{␊ |
1652 | ␉␉␉valid = true;␊ |
1653 | ␉␉␉bvr->OSisInstaller = true;␊ |
1654 | ␉␉␉strcpy(bvr->OSVersion, "10.7"); // 10.7 +␊ |
1655 | ␉␉␉close(fh);␊ |
1656 | ␉␉}␊ |
1657 | ␉␉else␊ |
1658 | ␉␉{␊ |
1659 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/.IAPhysicalMedia", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1660 | ␉␉␉fh = open(dirSpec, 0);␊ |
1661 | ␊ |
1662 | ␉␉␉if (fh >= 0)␊ |
1663 | ␉␉␉{␊ |
1664 | ␉␉␉␉valid = true;␊ |
1665 | ␉␉␉␉bvr->OSisInstaller = true;␊ |
1666 | ␉␉␉␉strcpy(bvr->OSVersion, "10.9"); // 10.9 +␊ |
1667 | ␉␉␉}␊ |
1668 | ␉␉␉else␊ |
1669 | ␉␉␉{␊ |
1670 | ␉␉␉␉close(fh);␊ |
1671 | ␉␉␉}␊ |
1672 | ␉␉}␊ |
1673 | ␉}␊ |
1674 | ␉return valid;␊ |
1675 | }␊ |
1676 | ␊ |
1677 | //==============================================================================␊ |
1678 | ␊ |
1679 | static void scanFSLevelBVRSettings(BVRef chain)␊ |
1680 | {␊ |
1681 | ␉BVRef bvr;␊ |
1682 | ␉char dirSpec[512], fileSpec[512];␊ |
1683 | ␉char label[BVSTRLEN];␊ |
1684 | ␉int ret;␊ |
1685 | ␉long flags;␊ |
1686 | ␉u_int32_t time;␊ |
1687 | ␉int fh, fileSize, error;␊ |
1688 | ␊ |
1689 | ␉for (bvr = chain; bvr; bvr = bvr->next) {␊ |
1690 | ␉␉ret = -1;␊ |
1691 | ␉␉error = 0;␊ |
1692 | ␊ |
1693 | ␉␉//␊ |
1694 | ␉␉// Check for alternate volume label on boot helper partitions.␊ |
1695 | ␉␉//␊ |
1696 | ␉␉if (bvr->flags & kBVFlagBooter) {␊ |
1697 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/System/Library/CoreServices/", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1698 | ␉␉␉strcpy(fileSpec, ".disk_label.contentDetails");␊ |
1699 | ␉␉␉ret = GetFileInfo(dirSpec, fileSpec, &flags, &time);␊ |
1700 | ␉␉␉if (!ret) {␊ |
1701 | ␉␉␉␉fh = open(strcat(dirSpec, fileSpec), 0);␊ |
1702 | ␉␉␉␉fileSize = file_size(fh);␊ |
1703 | ␉␉␉␉if (fileSize > 0 && fileSize < BVSTRLEN) {␊ |
1704 | ␉␉␉␉␉if (read(fh, label, fileSize) != fileSize) {␊ |
1705 | ␉␉␉␉␉␉error = -1;␊ |
1706 | ␉␉␉␉␉}␊ |
1707 | ␉␉␉␉} else ␉{␊ |
1708 | ␉␉␉␉␉error = -1;␊ |
1709 | ␉␉␉␉}␊ |
1710 | ␊ |
1711 | ␉␉␉␉close(fh);␊ |
1712 | ␊ |
1713 | ␉␉␉␉if (!error)␊ |
1714 | ␉␉␉␉{␊ |
1715 | ␉␉␉␉␉label[fileSize] = '\0';␊ |
1716 | ␉␉␉␉␉strcpy(bvr->altlabel, label);␊ |
1717 | ␉␉␉␉}␊ |
1718 | ␉␉␉}␊ |
1719 | ␉␉}␊ |
1720 | ␊ |
1721 | ␉␉// Check for SystemVersion.plist or ServerVersion.plist to determine if a volume hosts an installed system.␊ |
1722 | ␊ |
1723 | ␉␉if (bvr->flags & kBVFlagNativeBoot)␊ |
1724 | ␉␉{␊ |
1725 | ␉␉␉if (getOSVersion(bvr,bvr->OSVersion) == true)␊ |
1726 | ␉␉␉{␊ |
1727 | ␉␉␉␉bvr->flags |= kBVFlagSystemVolume;␊ |
1728 | ␉␉␉}␊ |
1729 | ␉␉}␊ |
1730 | ␉}␊ |
1731 | }␊ |
1732 | ␊ |
1733 | //==============================================================================␊ |
1734 | ␊ |
1735 | void rescanBIOSDevice(int biosdev)␊ |
1736 | {␊ |
1737 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(biosdev);␊ |
1738 | ␉CacheReset();␊ |
1739 | ␉diskFreeMap(oldMap);␊ |
1740 | ␉oldMap = NULL;␊ |
1741 | ␉scanBootVolumes(biosdev, 0);␊ |
1742 | }␊ |
1743 | ␊ |
1744 | //==============================================================================␊ |
1745 | ␊ |
1746 | struct DiskBVMap* diskResetBootVolumes(int biosdev)␊ |
1747 | {␊ |
1748 | ␉struct DiskBVMap * map;␊ |
1749 | ␉struct DiskBVMap *prevMap = NULL;␊ |
1750 | ␉for ( map = gDiskBVMap; map; prevMap = map, map = map->next ) {␊ |
1751 | ␉␉if ( biosdev == map->biosdev ) {␊ |
1752 | ␉␉␉break;␊ |
1753 | ␉␉}␊ |
1754 | ␉}␊ |
1755 | ␊ |
1756 | ␉if(map != NULL) {␊ |
1757 | ␉␉verbose("Resetting BIOS device %xh\n", biosdev);␊ |
1758 | ␉␉// Reset the biosbuf cache␊ |
1759 | ␉␉cache_valid = false;␊ |
1760 | ␉␉if(map == gDiskBVMap) {␊ |
1761 | ␉␉␉gDiskBVMap = map->next;␊ |
1762 | ␉␉} else if(prevMap != NULL) {␊ |
1763 | ␉␉␉prevMap->next = map->next;␊ |
1764 | ␉␉} else ␉{␊ |
1765 | ␉␉␉stop("");␊ |
1766 | ␉␉}␊ |
1767 | ␉}␊ |
1768 | ␉// Return the old map, either to be freed, or reinserted later␊ |
1769 | ␉return map;␊ |
1770 | }␊ |
1771 | ␊ |
1772 | //==============================================================================␊ |
1773 | ␊ |
1774 | // Frees a DiskBVMap and all of its BootVolume's␊ |
1775 | void diskFreeMap(struct DiskBVMap *map)␊ |
1776 | {␊ |
1777 | ␉if(map != NULL)␊ |
1778 | ␉{␊ |
1779 | ␉␉while(map->bvr != NULL)␊ |
1780 | ␉␉{␊ |
1781 | ␉␉␉BVRef bvr = map->bvr;␊ |
1782 | ␉␉␉map->bvr = bvr->next;␊ |
1783 | ␉␉␉(*bvr->bv_free)(bvr);␊ |
1784 | ␉␉}␊ |
1785 | ␊ |
1786 | ␉␉free(map);␊ |
1787 | ␉}␊ |
1788 | }␊ |
1789 | ␊ |
1790 | //==============================================================================␊ |
1791 | ␊ |
1792 | BVRef diskScanBootVolumes(int biosdev, int * countPtr)␊ |
1793 | {␊ |
1794 | ␉struct DiskBVMap *map;␊ |
1795 | ␉BVRef bvr;␊ |
1796 | ␉int count = 0;␊ |
1797 | ␊ |
1798 | ␉// Find an existing mapping for this device.␊ |
1799 | ␊ |
1800 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
1801 | ␉{␊ |
1802 | ␉␉if (biosdev == map->biosdev)␊ |
1803 | ␉␉{␊ |
1804 | ␉␉␉count = map->bvrcnt;␊ |
1805 | ␉␉␉break;␊ |
1806 | ␉␉}␊ |
1807 | ␉}␊ |
1808 | ␊ |
1809 | ␉if (map == NULL)␊ |
1810 | ␉{␊ |
1811 | ␉␉bvr = diskScanGPTBootVolumes(biosdev, &count);␊ |
1812 | ␉␉if (bvr == NULL)␊ |
1813 | ␉␉{␊ |
1814 | ␉␉␉bvr = diskScanFDiskBootVolumes(biosdev, &count);␊ |
1815 | ␉␉}␊ |
1816 | ␉␉if (bvr == NULL)␊ |
1817 | ␉␉{␊ |
1818 | ␉␉␉bvr = diskScanAPMBootVolumes(biosdev, &count);␊ |
1819 | ␉␉}␊ |
1820 | ␉␉if (bvr)␊ |
1821 | ␉␉{␊ |
1822 | ␉␉␉scanFSLevelBVRSettings(bvr);␊ |
1823 | ␉␉}␊ |
1824 | ␉}␊ |
1825 | ␉else␊ |
1826 | ␉{␊ |
1827 | ␉␉bvr = map->bvr;␊ |
1828 | ␉}␊ |
1829 | ␉if (countPtr)␊ |
1830 | ␉{␊ |
1831 | ␉␉*countPtr += count;␊ |
1832 | ␉}␊ |
1833 | ␉return bvr;␊ |
1834 | }␊ |
1835 | ␊ |
1836 | //==============================================================================␊ |
1837 | ␊ |
1838 | BVRef getBVChainForBIOSDev(int biosdev)␊ |
1839 | {␊ |
1840 | ␉BVRef chain = NULL;␊ |
1841 | ␉struct DiskBVMap * map = NULL;␊ |
1842 | ␊ |
1843 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
1844 | ␉{␊ |
1845 | ␉␉if (map->biosdev == biosdev)␊ |
1846 | ␉␉{␊ |
1847 | ␉␉␉chain = map->bvr;␊ |
1848 | ␉␉␉break;␊ |
1849 | ␉␉}␊ |
1850 | ␉}␊ |
1851 | ␊ |
1852 | ␉return chain;␊ |
1853 | }␊ |
1854 | ␊ |
1855 | //==============================================================================␊ |
1856 | ␊ |
1857 | BVRef newFilteredBVChain(int minBIOSDev, int maxBIOSDev, unsigned int allowFlags, unsigned int denyFlags, int *count)␊ |
1858 | {␊ |
1859 | ␉BVRef chain = NULL;␊ |
1860 | ␉BVRef bvr = NULL;␊ |
1861 | ␉BVRef newBVR = NULL;␊ |
1862 | ␉BVRef prevBVR = NULL;␊ |
1863 | ␊ |
1864 | ␉struct DiskBVMap * map = NULL;␊ |
1865 | ␉int bvCount = 0;␊ |
1866 | ␊ |
1867 | ␉const char *raw = 0;␊ |
1868 | ␉char* val = 0;␊ |
1869 | ␉int len;␊ |
1870 | ␊ |
1871 | ␉getValueForKey(kHidePartition, &raw, &len, &bootInfo->chameleonConfig);␊ |
1872 | ␉if(raw)␊ |
1873 | ␉{␊ |
1874 | ␉␉val = XMLDecode(raw); ␊ |
1875 | ␉}␊ |
1876 | ␊ |
1877 | ␉/*␊ |
1878 | ␉ * Traverse gDISKBVmap to get references for␊ |
1879 | ␉ * individual bvr chains of each drive.␊ |
1880 | ␉ */␊ |
1881 | ␉for (map = gDiskBVMap; map; map = map->next)␊ |
1882 | ␉{␊ |
1883 | ␉␉for (bvr = map->bvr; bvr; bvr = bvr->next)␊ |
1884 | ␉␉{␊ |
1885 | ␉␉␉/*␊ |
1886 | ␉␉␉ * Save the last bvr.␊ |
1887 | ␉␉␉ */␊ |
1888 | ␉␉␉if (newBVR)␊ |
1889 | ␉␉␉{␊ |
1890 | ␉␉␉␉prevBVR = newBVR;␊ |
1891 | ␉␉␉}␊ |
1892 | ␊ |
1893 | ␉␉␉/*␊ |
1894 | ␉␉␉ * Allocate and copy the matched bvr entry into a new one.␊ |
1895 | ␉␉␉ */␊ |
1896 | ␉␉␉newBVR = (BVRef) malloc(sizeof(*newBVR));␊ |
1897 | ␉␉␉if (!newBVR)␊ |
1898 | ␉␉␉{␊ |
1899 | ␉␉␉␉continue;␊ |
1900 | ␉␉␉}␊ |
1901 | ␉␉␉bcopy(bvr, newBVR, sizeof(*newBVR));␊ |
1902 | ␊ |
1903 | ␉␉␉/*␊ |
1904 | ␉␉␉ * Adjust the new bvr's fields.␊ |
1905 | ␉␉␉ */␊ |
1906 | ␉␉␉newBVR->next = NULL;␊ |
1907 | ␉␉␉newBVR->filtered = true;␊ |
1908 | ␊ |
1909 | ␉␉␉if ( (!allowFlags || newBVR->flags & allowFlags)␊ |
1910 | ␉␉␉␉&& (!denyFlags || !(newBVR->flags & denyFlags) )␊ |
1911 | ␉␉␉␉&& (newBVR->biosdev >= minBIOSDev && newBVR->biosdev <= maxBIOSDev)␊ |
1912 | ␉␉␉␉) {␊ |
1913 | ␉␉␉␉newBVR->visible = true;␊ |
1914 | ␉␉␉}␊ |
1915 | ␊ |
1916 | ␉␉␉/*␊ |
1917 | ␉␉␉ * Looking for "Hide Partition" entries in 'hd(x,y)|uuid|"label" hd(m,n)|uuid|"label"' format,␊ |
1918 | ␉␉␉ * to be able to hide foreign partitions from the boot menu.␊ |
1919 | ␉␉␉ *␊ |
1920 | ␉␉␉ */␊ |
1921 | ␉␉␉if ( (newBVR->flags & kBVFlagForeignBoot) ) {␊ |
1922 | ␉␉␉␉char *start, *next = val;␊ |
1923 | ␉␉␉␉long len = 0; ␊ |
1924 | ␉␉␉␉do␊ |
1925 | ␉␉␉␉{␊ |
1926 | ␉␉␉␉␉start = strbreak(next, &next, &len);␊ |
1927 | ␉␉␉␉␉if(len && matchVolumeToString(newBVR, start, len) )␊ |
1928 | ␉␉␉␉␉{␊ |
1929 | ␉␉␉␉␉␉newBVR->visible = false;␊ |
1930 | ␉␉␉␉␉}␊ |
1931 | ␉␉␉␉}␊ |
1932 | ␉␉␉␉while ( next && *next );␊ |
1933 | ␉␉␉}␊ |
1934 | ␊ |
1935 | ␉␉␉/*␊ |
1936 | ␉␉␉ * Use the first bvr entry as the starting chain pointer.␊ |
1937 | ␉␉␉ */␊ |
1938 | ␉␉␉if (!chain) {␊ |
1939 | ␉␉␉␉chain = newBVR;␊ |
1940 | ␉␉␉}␊ |
1941 | ␊ |
1942 | ␉␉␉/*␊ |
1943 | ␉␉␉ * Update the previous bvr's link pointer to use the new memory area.␊ |
1944 | ␉␉␉ */␊ |
1945 | ␉␉␉if (prevBVR) {␊ |
1946 | ␉␉␉␉prevBVR->next = newBVR;␊ |
1947 | ␉␉␉}␊ |
1948 | ␊ |
1949 | ␉␉␉if (newBVR->visible) {␊ |
1950 | ␉␉␉␉bvCount++;␊ |
1951 | ␉␉␉}␊ |
1952 | ␉␉}␊ |
1953 | ␉}␊ |
1954 | ␊ |
1955 | #if DEBUG //Azi: warning - too big for boot-log.. far too big.. i mean HUGE!! :P␊ |
1956 | ␉for (bvr = chain; bvr; bvr = bvr->next)␊ |
1957 | ␉{␊ |
1958 | ␉␉printf(" bvr: %d, dev: %d, part: %d, flags: %d, vis: %d\n", bvr, bvr->biosdev, bvr->part_no, bvr->flags, bvr->visible);␊ |
1959 | ␉}␊ |
1960 | ␉printf("count: %d\n", bvCount);␊ |
1961 | ␉getchar();␊ |
1962 | #endif␊ |
1963 | ␊ |
1964 | ␉*count = bvCount;␊ |
1965 | ␊ |
1966 | ␉free(val); ␊ |
1967 | ␉return chain;␊ |
1968 | }␊ |
1969 | ␊ |
1970 | //==============================================================================␊ |
1971 | ␊ |
1972 | int freeFilteredBVChain(const BVRef chain)␊ |
1973 | {␊ |
1974 | ␉int ret = 1;␊ |
1975 | ␉BVRef bvr = chain;␊ |
1976 | ␉BVRef nextBVR = NULL;␊ |
1977 | ␊ |
1978 | ␉while (bvr)␊ |
1979 | ␉{␊ |
1980 | ␉␉nextBVR = bvr->next;␊ |
1981 | ␊ |
1982 | ␉␉if (bvr->filtered)␊ |
1983 | ␉␉{␊ |
1984 | ␉␉␉free(bvr);␊ |
1985 | ␉␉}␊ |
1986 | ␉␉else␊ |
1987 | ␉␉{␊ |
1988 | ␉␉␉ret = 0;␊ |
1989 | ␉␉␉break;␊ |
1990 | ␉␉}␊ |
1991 | ␊ |
1992 | ␉␉bvr = nextBVR;␊ |
1993 | ␉}␊ |
1994 | ␊ |
1995 | ␉return ret;␊ |
1996 | }␊ |
1997 | ␊ |
1998 | //==============================================================================␊ |
1999 | ␊ |
2000 | static const struct NamedValue fdiskTypes[] =␊ |
2001 | {␊ |
2002 | ␉{ FDISK_NTFS,␉␉"Windows NTFS" },␊ |
2003 | ␉{ FDISK_DOS12,␉␉"Windows FAT12" },␊ |
2004 | ␉{ FDISK_DOS16B,␉␉"Windows FAT16" },␊ |
2005 | ␉{ FDISK_DOS16S,␉␉"Windows FAT16" },␊ |
2006 | ␉{ FDISK_DOS16SLBA,␉"Windows FAT16" },␊ |
2007 | ␉{ FDISK_SMALLFAT32,␉"Windows FAT32" },␊ |
2008 | ␉{ FDISK_FAT32,␉␉"Windows FAT32" },␊ |
2009 | ␉{ FDISK_FREEBSD,␉"FreeBSD" },␊ |
2010 | ␉{ FDISK_OPENBSD,␉"OpenBSD" },␊ |
2011 | ␉{ FDISK_LINUX,␉␉"Linux" },␊ |
2012 | ␉{ FDISK_UFS,␉␉"Apple UFS" },␊ |
2013 | ␉{ FDISK_HFS,␉␉"Apple HFS" },␊ |
2014 | ␉{ FDISK_BOOTER,␉␉"Apple Boot/UFS" },␊ |
2015 | ␉{ FDISK_BEFS,␉␉"Haiku" },␊ |
2016 | ␉{ 0xCD,␉␉␉"CD-ROM" },␊ |
2017 | ␉{ 0x00,␉␉␉0 } /* must be last */␊ |
2018 | };␊ |
2019 | ␊ |
2020 | //==============================================================================␊ |
2021 | ␊ |
2022 | bool matchVolumeToString( BVRef bvr, const char* match, long matchLen)␊ |
2023 | {␊ |
2024 | ␉char testStr[128];␊ |
2025 | ␊ |
2026 | ␉if ( !bvr || !match || !*match)␊ |
2027 | ␉{␊ |
2028 | ␉␉return 0;␊ |
2029 | ␉}␊ |
2030 | ␊ |
2031 | ␉if ( bvr->biosdev < 0x80 || bvr->biosdev >= 0x100 )␊ |
2032 | ␉{␊ |
2033 | return 0;␊ |
2034 | ␉}␊ |
2035 | ␊ |
2036 | ␉// Try to match hd(x,y) first.␊ |
2037 | ␉sprintf(testStr, "hd(%d,%d)", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
2038 | ␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2039 | ␉{␊ |
2040 | ␉␉return true;␊ |
2041 | ␉}␊ |
2042 | ␊ |
2043 | ␉// Try to match volume UUID.␊ |
2044 | ␉if ( bvr->fs_getuuid && bvr->fs_getuuid(bvr, testStr) == 0)␊ |
2045 | ␉{␊ |
2046 | ␉␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2047 | ␉␉{␊ |
2048 | ␉␉␉return true;␊ |
2049 | ␉␉}␊ |
2050 | ␉}␊ |
2051 | ␊ |
2052 | ␉// Try to match volume label (always quoted).␊ |
2053 | ␉if ( bvr->description )␊ |
2054 | ␉{␊ |
2055 | ␉␉bvr->description(bvr, testStr, sizeof(testStr)-1);␊ |
2056 | ␉␉if ( matchLen ? !strncmp(match, testStr, matchLen) : !strcmp(match, testStr) )␊ |
2057 | ␉␉{␊ |
2058 | ␉␉␉return true;␊ |
2059 | ␉␉}␊ |
2060 | ␉}␊ |
2061 | ␊ |
2062 | ␉return false;␊ |
2063 | }␊ |
2064 | ␊ |
2065 | //==============================================================================␊ |
2066 | ␊ |
2067 | /* If Rename Partition has defined an alias, then extract it for description purpose.␊ |
2068 | * The format for the rename string is the following:␊ |
2069 | * hd(x,y)|uuid|"label" "alias";hd(m,n)|uuid|"label" "alias"; etc...␊ |
2070 | */␊ |
2071 | ␊ |
2072 | bool getVolumeLabelAlias(BVRef bvr, char* str, long strMaxLen)␊ |
2073 | {␊ |
2074 | ␉char *aliasList, *entryStart, *entryNext;␊ |
2075 | ␊ |
2076 | ␉if ( !str || strMaxLen <= 0)␊ |
2077 | ␉{␊ |
2078 | ␉␉return false;␊ |
2079 | ␉}␊ |
2080 | ␊ |
2081 | ␉aliasList = XMLDecode(getStringForKey(kRenamePartition, &bootInfo->chameleonConfig));␊ |
2082 | ␉if ( !aliasList )␊ |
2083 | ␉{␊ |
2084 | ␉␉return false;␊ |
2085 | ␉}␊ |
2086 | ␊ |
2087 | ␉for ( entryStart = entryNext = aliasList; entryNext && *entryNext; entryStart = entryNext )␊ |
2088 | ␉{␊ |
2089 | ␉␉char *volStart, *volEnd, *aliasStart;␊ |
2090 | ␉␉long volLen, aliasLen;␊ |
2091 | ␊ |
2092 | ␉␉// Delimit current entry␊ |
2093 | ␉␉entryNext = strchr(entryStart, ';');␊ |
2094 | ␉␉if ( entryNext )␊ |
2095 | ␉␉{␊ |
2096 | ␉␉␉*entryNext = '\0';␊ |
2097 | ␉␉␉entryNext++;␊ |
2098 | ␉␉}␊ |
2099 | ␊ |
2100 | ␉␉volStart = strbreak(entryStart, &volEnd, &volLen);␊ |
2101 | ␉␉if(!volLen)␊ |
2102 | ␉␉{␊ |
2103 | ␉␉␉continue;␊ |
2104 | ␉␉}␊ |
2105 | ␊ |
2106 | ␉␉aliasStart = strbreak(volEnd, 0, &aliasLen);␊ |
2107 | ␉␉if(!aliasLen)␊ |
2108 | ␉␉{␊ |
2109 | ␉␉␉continue;␊ |
2110 | ␉␉}␊ |
2111 | ␊ |
2112 | ␉␉if ( matchVolumeToString(bvr, volStart, volLen) )␊ |
2113 | ␉␉{␊ |
2114 | ␉␉␉strncat(str, aliasStart, MIN(strMaxLen, aliasLen));␊ |
2115 | ␉␉␉free(aliasList);␊ |
2116 | ␊ |
2117 | ␉␉␉return true;␊ |
2118 | ␉␉}␊ |
2119 | ␉}␊ |
2120 | ␊ |
2121 | ␉free(aliasList);␊ |
2122 | ␉return false;␊ |
2123 | }␊ |
2124 | ␊ |
2125 | //==============================================================================␊ |
2126 | ␊ |
2127 | void getBootVolumeDescription( BVRef bvr, char * str, long strMaxLen, bool useDeviceDescription )␊ |
2128 | {␊ |
2129 | ␉unsigned char type;␊ |
2130 | ␉char *p = str;␊ |
2131 | ␊ |
2132 | ␉if(!bvr || !p || strMaxLen <= 0)␊ |
2133 | ␉{␊ |
2134 | ␉␉return;␊ |
2135 | ␉}␊ |
2136 | ␊ |
2137 | ␉type = (unsigned char) bvr->part_type;␊ |
2138 | ␊ |
2139 | ␉if (useDeviceDescription)␊ |
2140 | ␉{␊ |
2141 | ␉␉int len = getDeviceDescription(bvr, str);␊ |
2142 | ␉␉if(len >= strMaxLen)␊ |
2143 | ␉␉{␊ |
2144 | ␉␉␉return;␊ |
2145 | ␉␉}␊ |
2146 | ␊ |
2147 | ␉␉strcpy(str + len, bvr->OSisInstaller ? " (Installer) " : " ");␊ |
2148 | ␉␉len += bvr->OSisInstaller ? 13 : 1;␊ |
2149 | ␉␉strMaxLen -= len;␊ |
2150 | ␉␉p += len;␊ |
2151 | ␉}␊ |
2152 | ␊ |
2153 | ␉/* See if a partition rename is preferred */␊ |
2154 | ␉if (getVolumeLabelAlias(bvr, p, strMaxLen))␊ |
2155 | ␉{␊ |
2156 | ␉␉strncpy(bvr->label, p, strMaxLen); ␊ |
2157 | ␉␉return; // we're done here no need to seek for real name␊ |
2158 | ␉}␊ |
2159 | ␊ |
2160 | ␉// Get the volume label using filesystem specific functions or use the alternate volume label if available.␊ |
2161 | ␊ |
2162 | ␉if (*bvr->altlabel != '\0')␊ |
2163 | ␉{␊ |
2164 | ␉␉strncpy(p, bvr->altlabel, strMaxLen);␊ |
2165 | ␉}␊ |
2166 | ␉else if (bvr->description)␊ |
2167 | ␉{␊ |
2168 | ␉␉bvr->description(bvr, p, strMaxLen);␊ |
2169 | ␉}␊ |
2170 | ␊ |
2171 | ␉if (*p == '\0')␊ |
2172 | ␉{␊ |
2173 | ␉␉const char * name = getNameForValue( fdiskTypes, type );␊ |
2174 | ␊ |
2175 | ␉␉if (name == NULL)␊ |
2176 | ␉␉{␊ |
2177 | ␉␉␉name = bvr->type_name;␊ |
2178 | ␉␉}␊ |
2179 | ␊ |
2180 | ␉␉if (name == NULL)␊ |
2181 | ␉␉{␊ |
2182 | ␉␉␉sprintf(p, "TYPE %02x", type);␊ |
2183 | ␉␉}␊ |
2184 | ␉␉else␊ |
2185 | ␉␉{␊ |
2186 | ␉␉␉strncpy(p, name, strMaxLen);␊ |
2187 | ␉␉}␊ |
2188 | ␉}␊ |
2189 | ␊ |
2190 | ␉// Set the devices label␊ |
2191 | ␉sprintf(bvr->label, p);␊ |
2192 | }␊ |
2193 | ␊ |
2194 | ␊ |
2195 | //==============================================================================␊ |
2196 | ␊ |
2197 | int readBootSector(int biosdev, unsigned int secno, void * buffer)␊ |
2198 | {␊ |
2199 | ␉int error;␊ |
2200 | ␉struct disk_blk0 * bootSector = (struct disk_blk0 *) buffer;␊ |
2201 | ␊ |
2202 | ␉if (bootSector == NULL)␊ |
2203 | ␉{␊ |
2204 | ␉␉if (gBootSector == NULL)␊ |
2205 | ␉␉{␊ |
2206 | ␉␉␉gBootSector = (struct disk_blk0 *) malloc(sizeof(*gBootSector));␊ |
2207 | ␊ |
2208 | ␉␉␉if (gBootSector == NULL)␊ |
2209 | ␉␉␉{␊ |
2210 | ␉␉␉␉return -1;␊ |
2211 | ␉␉␉}␊ |
2212 | ␉␉}␊ |
2213 | ␊ |
2214 | ␉␉bootSector = gBootSector;␊ |
2215 | ␉}␊ |
2216 | ␊ |
2217 | ␉error = readBytes(biosdev, secno, 0, BPS, bootSector);␊ |
2218 | ␊ |
2219 | ␉if (error || bootSector->signature != DISK_SIGNATURE)␊ |
2220 | ␉{␊ |
2221 | ␉␉return -1;␊ |
2222 | ␉}␊ |
2223 | ␉return 0;␊ |
2224 | }␊ |
2225 | ␊ |
2226 | //==============================================================================␊ |
2227 | ␊ |
2228 | /*␊ |
2229 | * Format of boot1f32 block.␊ |
2230 | */␊ |
2231 | ␊ |
2232 | #define BOOT1F32_MAGIC "BOOT "␊ |
2233 | #define BOOT1F32_MAGICLEN 11␊ |
2234 | ␊ |
2235 | struct disk_boot1f32_blk␊ |
2236 | {␊ |
2237 | ␉unsigned char init[3];␊ |
2238 | ␉unsigned char fsheader[87];␊ |
2239 | ␉unsigned char magic[BOOT1F32_MAGICLEN];␊ |
2240 | ␉unsigned char bootcode[409];␊ |
2241 | ␉unsigned short signature;␊ |
2242 | };␊ |
2243 | ␊ |
2244 | //==============================================================================␊ |
2245 | ␊ |
2246 | int testFAT32EFIBootSector( int biosdev, unsigned int secno, void * buffer )␊ |
2247 | {␊ |
2248 | ␉struct disk_boot1f32_blk * bootSector = (struct disk_boot1f32_blk *) buffer;␊ |
2249 | ␉int error;␊ |
2250 | ␊ |
2251 | ␉if ( bootSector == NULL )␊ |
2252 | ␉{␊ |
2253 | ␉␉if ( gBootSector == NULL )␊ |
2254 | ␉␉{␊ |
2255 | ␉␉␉gBootSector = (struct disk_blk0 *) malloc(sizeof(*gBootSector));␊ |
2256 | ␉␉␉if ( gBootSector == NULL )␊ |
2257 | ␉␉␉{␊ |
2258 | ␉␉␉␉return -1;␊ |
2259 | ␉␉␉}␊ |
2260 | ␉␉}␊ |
2261 | ␉␉bootSector = (struct disk_boot1f32_blk *) gBootSector;␊ |
2262 | ␉}␊ |
2263 | ␊ |
2264 | ␉error = readBytes( biosdev, secno, 0, BPS, bootSector );␊ |
2265 | ␉if ( error || bootSector->signature != DISK_SIGNATURE || strncmp((const char *)bootSector->magic, BOOT1F32_MAGIC, BOOT1F32_MAGICLEN) )␊ |
2266 | ␉{␊ |
2267 | ␉␉return -1;␊ |
2268 | ␉}␊ |
2269 | ␉return 0;␊ |
2270 | }␊ |
2271 | ␊ |
2272 | ␊ |
2273 | //==============================================================================␊ |
2274 | // Handle seek request from filesystem modules.␊ |
2275 | ␊ |
2276 | void diskSeek(BVRef bvr, long long position)␊ |
2277 | {␊ |
2278 | ␉bvr->fs_boff = position / BPS;␊ |
2279 | ␉bvr->fs_byteoff = position % BPS;␊ |
2280 | }␊ |
2281 | ␊ |
2282 | ␊ |
2283 | //==============================================================================␊ |
2284 | // Handle read request from filesystem modules.␊ |
2285 | ␊ |
2286 | int diskRead(BVRef bvr, long addr, long length)␊ |
2287 | {␊ |
2288 | ␉return readBytes(bvr->biosdev, bvr->fs_boff + bvr->part_boff, bvr->fs_byteoff, length, (void *) addr);␊ |
2289 | }␊ |
2290 | ␊ |
2291 | //==============================================================================␊ |
2292 | ␊ |
2293 | int rawDiskRead( BVRef bvr, unsigned int secno, void *buffer, unsigned int len )␊ |
2294 | {␊ |
2295 | ␉int secs;␊ |
2296 | ␉unsigned char *cbuf = (unsigned char *)buffer;␊ |
2297 | ␉unsigned int copy_len;␊ |
2298 | ␉int rc;␊ |
2299 | ␊ |
2300 | ␉if ((len & (BPS-1)) != 0)␊ |
2301 | ␉{␊ |
2302 | ␉␉error("raw disk read not sector aligned");␊ |
2303 | ␉␉return -1;␊ |
2304 | ␉}␊ |
2305 | ␉secno += bvr->part_boff;␊ |
2306 | ␊ |
2307 | ␉cache_valid = false;␊ |
2308 | ␊ |
2309 | ␉while (len > 0)␊ |
2310 | ␉{␊ |
2311 | ␉␉secs = len / BPS;␊ |
2312 | ␉␉if (secs > N_CACHE_SECS)␊ |
2313 | ␉␉{␊ |
2314 | ␉␉␉secs = N_CACHE_SECS;␊ |
2315 | ␉␉}␊ |
2316 | ␉␉copy_len = secs * BPS;␊ |
2317 | ␊ |
2318 | ␉␉//printf("rdr: ebiosread(%d, %d, %d)\n", bvr->biosdev, secno, secs);␊ |
2319 | ␉␉if ((rc = ebiosread(bvr->biosdev, secno, secs)) != 0)␊ |
2320 | ␉␉{␊ |
2321 | ␉␉␉/* Ignore corrected ECC errors */␊ |
2322 | ␉␉␉if (rc != ECC_CORRECTED_ERR)␊ |
2323 | ␉␉␉{␊ |
2324 | ␉␉␉␉error(" EBIOS read error: %s\n", bios_error(rc), rc);␊ |
2325 | ␉␉␉␉error(" Block %d Sectors %d\n", secno, secs);␊ |
2326 | ␉␉␉␉return rc;␊ |
2327 | ␉␉␉}␊ |
2328 | ␉␉}␊ |
2329 | ␉␉bcopy( trackbuf, cbuf, copy_len );␊ |
2330 | ␉␉len -= copy_len;␊ |
2331 | ␉␉cbuf += copy_len;␊ |
2332 | ␉␉secno += secs;␊ |
2333 | ␉␉spinActivityIndicator(secs);␊ |
2334 | ␉}␊ |
2335 | ␊ |
2336 | ␉return 0;␊ |
2337 | }␊ |
2338 | ␊ |
2339 | //==============================================================================␊ |
2340 | ␊ |
2341 | int rawDiskWrite( BVRef bvr, unsigned int secno, void *buffer, unsigned int len )␊ |
2342 | {␊ |
2343 | int secs;␊ |
2344 | unsigned char *cbuf = (unsigned char *)buffer;␊ |
2345 | unsigned int copy_len;␊ |
2346 | int rc;␊ |
2347 | ␊ |
2348 | ␉if ((len & (BPS-1)) != 0)␊ |
2349 | ␉{␊ |
2350 | ␉␉error("raw disk write not sector aligned");␊ |
2351 | ␉␉return -1;␊ |
2352 | ␉}␊ |
2353 | ␉secno += bvr->part_boff;␊ |
2354 | ␊ |
2355 | ␉cache_valid = false;␊ |
2356 | ␊ |
2357 | ␉while (len > 0)␊ |
2358 | ␉{␊ |
2359 | ␉␉secs = len / BPS;␊ |
2360 | ␉␉if (secs > N_CACHE_SECS)␊ |
2361 | ␉␉{␊ |
2362 | ␉␉␉secs = N_CACHE_SECS;␊ |
2363 | ␉␉}␊ |
2364 | ␉␉copy_len = secs * BPS;␊ |
2365 | ␊ |
2366 | ␉␉bcopy( cbuf, trackbuf, copy_len );␊ |
2367 | ␉␉//printf("rdr: ebioswrite(%d, %d, %d)\n", bvr->biosdev, secno, secs);␊ |
2368 | ␉␉if ((rc = ebioswrite(bvr->biosdev, secno, secs)) != 0)␊ |
2369 | ␉␉{␊ |
2370 | ␉␉␉error(" EBIOS write error: %s\n", bios_error(rc), rc);␊ |
2371 | ␉␉␉error(" Block %d Sectors %d\n", secno, secs);␊ |
2372 | ␉␉␉return rc;␊ |
2373 | ␉␉}␊ |
2374 | ␊ |
2375 | ␉␉len -= copy_len;␊ |
2376 | ␉␉cbuf += copy_len;␊ |
2377 | ␉␉secno += secs;␊ |
2378 | ␉␉spinActivityIndicator(secs);␊ |
2379 | ␉}␊ |
2380 | ␊ |
2381 | ␉return 0;␊ |
2382 | }␊ |
2383 | ␊ |
2384 | //==============================================================================␊ |
2385 | ␊ |
2386 | int diskIsCDROM(BVRef bvr)␊ |
2387 | {␊ |
2388 | ␉struct driveInfo di;␊ |
2389 | ␊ |
2390 | ␉if (getDriveInfo(bvr->biosdev, &di) == 0 && di.no_emulation)␊ |
2391 | ␉{␊ |
2392 | ␉␉return 1;␊ |
2393 | ␉}␊ |
2394 | ␉return 0;␊ |
2395 | }␊ |
2396 | ␊ |
2397 | //==============================================================================␊ |
2398 | ␊ |
2399 | int biosDevIsCDROM(int biosdev)␊ |
2400 | {␊ |
2401 | ␉struct driveInfo di;␊ |
2402 | ␊ |
2403 | ␉if (getDriveInfo(biosdev, &di) == 0 && di.no_emulation)␊ |
2404 | ␉{␊ |
2405 | ␉␉return 1;␊ |
2406 | ␉}␊ |
2407 | ␉return 0;␊ |
2408 | }␊ |
2409 | |