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