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