Root/
Source at commit HEAD created 5 years 6 days ago. By ifabio, Few update to kernelPatcher (Credits to CrazyBirdy) | |
---|---|
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 | * Copyright (c) 1988 Carnegie-Mellon University␊ |
29 | * Copyright (c) 1987 Carnegie-Mellon University␊ |
30 | * All rights reserved. The CMU software License Agreement specifies␊ |
31 | * the terms and conditions for use and redistribution.␊ |
32 | *␊ |
33 | */␊ |
34 | /*␊ |
35 | * HISTORY␊ |
36 | * Revision 2.3 88/08/08 13:47:07 rvb␊ |
37 | * Allocate buffers dynamically vs statically.␊ |
38 | * Now b[i] and i_fs and i_buf, are allocated dynamically.␊ |
39 | * boot_calloc(size) allocates and zeros a buffer rounded to a NPG␊ |
40 | * boundary.␊ |
41 | * Generalize boot spec to allow, xx()/mach, xx(n,[a..h])/mach,␊ |
42 | * xx([a..h])/mach, ...␊ |
43 | * Also default "xx" if unspecified and alloc just "/mach",␊ |
44 | * where everything is defaulted␊ |
45 | * Add routine, ptol(), to parse partition letters.␊ |
46 | *␊ |
47 | * Copyright (c) 1982, 1986 Regents of the University of California.␊ |
48 | * All rights reserved. The Berkeley software License Agreement␊ |
49 | * specifies the terms and conditions for redistribution.␊ |
50 | *␊ |
51 | *␉@(#)sys.c␉7.1 (Berkeley) 6/5/86␊ |
52 | *␊ |
53 | * Copyright 2007 VMware Inc. "Preboot" ramdisk support added by David Elliott␊ |
54 | *␊ |
55 | */␊ |
56 | ␊ |
57 | #include "libsaio.h"␊ |
58 | #include "boot.h"␊ |
59 | #include "bootstruct.h"␊ |
60 | #include "disk.h"␊ |
61 | #include "ramdisk.h"␊ |
62 | #include "xml.h"␊ |
63 | #include "sl.h"␊ |
64 | ␊ |
65 | #include <libkern/crypto/md5.h>␊ |
66 | //#include <uuid/uuid.h>␊ |
67 | ␊ |
68 | #if 0 /* No OS X release has ever included this. */␊ |
69 | #include <Kernel/uuid/namespace.h>␊ |
70 | #else␊ |
71 | // from our uuid/namespace.h (UFS and HFS uuids can live in the same space?)␊ |
72 | static unsigned char kFSUUIDNamespaceSHA1[] = {0xB3,0xE2,0x0F,0x39,0xF2,0x92,0x11,0xD6,0x97,0xA4,0x00,0x30,0x65,0x43,0xEC,0xAC};␊ |
73 | #endif␊ |
74 | ␊ |
75 | #ifndef DEBUG_SYS␊ |
76 | ␉#define DEBUG_SYS 0␊ |
77 | #endif␊ |
78 | ␊ |
79 | #if DEBUG_SYS␊ |
80 | ␉#define DBG(x...)␉printf(x)␊ |
81 | #else␊ |
82 | ␉#define DBG(x...)␉msglog(x)␊ |
83 | #endif␊ |
84 | ␊ |
85 | #ifndef FEATURE_LAST_BOOT␊ |
86 | ␉#define FEATURE_LAST_BOOT 0 // AllocateKernelMemory error with feature from 2562␊ |
87 | #endif␊ |
88 | ␊ |
89 | extern int multiboot_partition;␊ |
90 | extern int multiboot_partition_set;␊ |
91 | extern int multiboot_skip_partition;␊ |
92 | extern int multiboot_skip_partition_set;␊ |
93 | ␊ |
94 | struct devsw␊ |
95 | {␊ |
96 | ␉const char *name;␊ |
97 | ␉// size increased from char to short to handle non-BIOS internal devices␊ |
98 | ␉unsigned short biosdev;␊ |
99 | ␉int type;␊ |
100 | };␊ |
101 | ␊ |
102 | // Device entries must be ordered by bios device numbers. ␊ |
103 | static struct devsw devsw[] =␊ |
104 | {␊ |
105 | //␉{ "sd", 0x80,␉kBIOSDevTypeHardDrive }, /* DEV_SD */␊ |
106 | ␉{ "hd", 0x80,␉kBIOSDevTypeHardDrive }, /* DEV_HD */␊ |
107 | ␉{ "en", 0xE0,␉kBIOSDevTypeNetwork }, /* DEV_EN */␊ |
108 | ␉{ "rd", 0x100,␉kBIOSDevTypeHardDrive },␊ |
109 | ␉{ "bt", 0x101,␉kBIOSDevTypeHardDrive }, // turbo - type for booter partition␊ |
110 | ␉{ 0, 0 }␊ |
111 | };␊ |
112 | ␊ |
113 | // Pseudo BIOS devices␊ |
114 | enum {␊ |
115 | ␉kPseudoBIOSDevRAMDisk = 0x100,␊ |
116 | ␉kPseudoBIOSDevBooter = 0x101␊ |
117 | };␊ |
118 | ␊ |
119 | /*␊ |
120 | * Max number of file descriptors.␊ |
121 | */␊ |
122 | #define NFILES 6␊ |
123 | ␊ |
124 | static struct iob iob[NFILES];␊ |
125 | ␊ |
126 | void * gFSLoadAddress = 0;␊ |
127 | ␊ |
128 | #define LP '('␊ |
129 | #define RP ')'␊ |
130 | ␊ |
131 | int gBIOSDev;␊ |
132 | ␊ |
133 | // Turbo - save what we think is our original BIOS boot volume if we have one 0xab␊ |
134 | BVRef gBIOSBootVolume = NULL;␊ |
135 | BVRef gBootVolume;␊ |
136 | ␊ |
137 | //static BVRef getBootVolumeRef( const char * path, const char ** outPath );␊ |
138 | static BVRef newBootVolumeRef( int biosdev, int partno );␊ |
139 | ␊ |
140 | //==========================================================================␊ |
141 | // LoadVolumeFile - LOW-LEVEL FILESYSTEM FUNCTION.␊ |
142 | // Load the specified file from the specified volume␊ |
143 | // to the load buffer at LOAD_ADDR.␊ |
144 | // If the file is fat, load only the i386 portion.␊ |
145 | ␊ |
146 | long LoadVolumeFile(BVRef bvr, const char *filePath)␊ |
147 | {␊ |
148 | ␉long fileSize;␊ |
149 | ␊ |
150 | ␉// Read file into load buffer. The data in the load buffer will be␊ |
151 | ␉// overwritten by the next LoadFile() call.␊ |
152 | ␊ |
153 | ␉gFSLoadAddress = (void *) LOAD_ADDR;␊ |
154 | ␊ |
155 | ␉fileSize = bvr->fs_loadfile(bvr, (char *)filePath);␊ |
156 | ␊ |
157 | ␉// Return the size of the file, or -1 if load failed.␊ |
158 | ␊ |
159 | ␉return fileSize;␊ |
160 | }␊ |
161 | ␊ |
162 | //==========================================================================␊ |
163 | // LoadFile - LOW-LEVEL FILESYSTEM FUNCTION.␊ |
164 | // Load the specified file to the load buffer at LOAD_ADDR.␊ |
165 | // If the file is fat, load only the i386 portion.␊ |
166 | ␊ |
167 | long LoadFile(const char * fileSpec)␊ |
168 | {␊ |
169 | ␉const char * filePath;␊ |
170 | ␉BVRef bvr;␊ |
171 | ␊ |
172 | ␉// Resolve the boot volume from the file spec.␊ |
173 | ␊ |
174 | ␉if ((bvr = getBootVolumeRef(fileSpec, &filePath)) == NULL)␊ |
175 | ␉{␊ |
176 | ␉␉return -1;␊ |
177 | ␉}␊ |
178 | ␊ |
179 | ␉return LoadVolumeFile(bvr, filePath);␊ |
180 | }␊ |
181 | ␊ |
182 | //==========================================================================␊ |
183 | ␊ |
184 | long ReadFileAtOffset(const char * fileSpec, void *buffer, uint64_t offset, uint64_t length)␊ |
185 | {␊ |
186 | ␉const char *filePath;␊ |
187 | ␉BVRef bvr;␊ |
188 | ␊ |
189 | ␉if ((bvr = getBootVolumeRef(fileSpec, &filePath)) == NULL)␊ |
190 | ␉{␊ |
191 | ␉␉return -1;␊ |
192 | ␉}␊ |
193 | ␊ |
194 | ␉if (bvr->fs_readfile == NULL)␊ |
195 | ␉{␊ |
196 | ␉␉return -1;␊ |
197 | ␉}␊ |
198 | ␊ |
199 | ␉return bvr->fs_readfile(bvr, (char *)filePath, buffer, offset, length);␊ |
200 | }␊ |
201 | ␊ |
202 | //==========================================================================␊ |
203 | ␊ |
204 | long LoadThinFatFile(const char *fileSpec, void **binary)␊ |
205 | {␊ |
206 | ␉const char␉*filePath = "";␊ |
207 | ␉FSReadFile␉readFile;␊ |
208 | ␉BVRef␉␉bvr;␊ |
209 | ␉unsigned long␉length;␊ |
210 | ␉unsigned long␉length2;␊ |
211 | ␊ |
212 | ␉// Resolve the boot volume from the file spec.␊ |
213 | ␊ |
214 | ␉if ((bvr = getBootVolumeRef(fileSpec, &filePath)) == NULL)␊ |
215 | ␉{␊ |
216 | ␉␉return -1;␊ |
217 | ␉}␊ |
218 | ␊ |
219 | ␉*binary = (void *)kLoadAddr;␊ |
220 | ␊ |
221 | ␉// Read file into load buffer. The data in the load buffer will be␊ |
222 | ␉// overwritten by the next LoadFile() call.␊ |
223 | ␊ |
224 | ␉gFSLoadAddress = (void *) LOAD_ADDR;␊ |
225 | ␊ |
226 | ␉readFile = bvr->fs_readfile;␊ |
227 | ␊ |
228 | ␉if (readFile != NULL)␊ |
229 | ␉{␊ |
230 | ␉␉// Read the first 4096 bytes (fat header)␊ |
231 | ␉␉length = readFile(bvr, (char *)filePath, *binary, 0, 0x1000);␊ |
232 | ␊ |
233 | ␉␉if (length > 0)␊ |
234 | ␉␉{␊ |
235 | ␉␉␉if (ThinFatFile(binary, &length) == 0)␊ |
236 | ␉␉␉{␊ |
237 | ␉␉␉␉if (length == 0)␊ |
238 | ␉␉␉␉{␊ |
239 | ␉␉␉␉␉return 0;␊ |
240 | ␉␉␉␉}␊ |
241 | ␊ |
242 | ␉␉␉␉// We found a fat binary; read only the thin part␊ |
243 | ␉␉␉␉DBG("Fat Binary found. Reading thin part only...\n");␊ |
244 | ␉␉␉␉length = readFile(bvr, (char *)filePath, (void *)kLoadAddr, (unsigned long)(*binary) - kLoadAddr, length);␊ |
245 | ␉␉␉␉*binary = (void *)kLoadAddr;␊ |
246 | ␉␉␉}␊ |
247 | ␉␉␉else␊ |
248 | ␉␉␉{␊ |
249 | ␉␉␉␉// Not a fat binary; read the rest of the file␊ |
250 | ␉␉␉␉DBG("Thin Binary found. Reading rest of the file...\n");␊ |
251 | ␉␉␉␉length2 = readFile(bvr, (char *)filePath, (void *)(kLoadAddr + length), length, 0);␊ |
252 | ␊ |
253 | ␉␉␉␉if (length2 == -1)␊ |
254 | ␉␉␉␉{␊ |
255 | ␉␉␉␉␉return -1;␊ |
256 | ␉␉␉␉}␊ |
257 | ␊ |
258 | ␉␉␉␉length += length2;␊ |
259 | ␉␉␉}␊ |
260 | ␉␉}␊ |
261 | ␉}␊ |
262 | ␉else␊ |
263 | ␉{␊ |
264 | ␉␉length = bvr->fs_loadfile(bvr, (char *)filePath);␊ |
265 | ␊ |
266 | ␉␉if (length > 0)␊ |
267 | ␉␉{␊ |
268 | ␉␉␉ThinFatFile(binary, &length);␊ |
269 | ␉␉}␊ |
270 | ␉}␊ |
271 | ␊ |
272 | ␉return length;␊ |
273 | }␊ |
274 | ␊ |
275 | //==========================================================================␊ |
276 | ␊ |
277 | #if UNUSED␊ |
278 | long GetFSUUID(char *spec, char *uuidStr)␊ |
279 | {␊ |
280 | ␉BVRef bvr;␊ |
281 | ␉long rval = -1;␊ |
282 | ␉const char *devSpec;␊ |
283 | ␊ |
284 | ␉if ((bvr = getBootVolumeRef(spec, &devSpec)) == NULL) {␊ |
285 | ␉␉return -1;␊ |
286 | ␉}␊ |
287 | ␊ |
288 | ␉if(bvr->fs_getuuid)␊ |
289 | ␉{␊ |
290 | ␉␉rval = bvr->fs_getuuid(bvr, uuidStr);␊ |
291 | ␉}␊ |
292 | ␊ |
293 | ␉return rval;␊ |
294 | }␊ |
295 | #endif␊ |
296 | ␊ |
297 | //==========================================================================␊ |
298 | ␊ |
299 | // filesystem-specific getUUID functions call this shared string generator␊ |
300 | ␊ |
301 | long CreateUUIDString(uint8_t uubytes[], int nbytes, char *uuidStr)␊ |
302 | {␊ |
303 | ␉unsigned␉fmtbase, fmtidx, i;␊ |
304 | ␉uint8_t␉␉uuidfmt[] = { 4, 2, 2, 2, 6 };␊ |
305 | ␉char␉␉*p = uuidStr;␊ |
306 | ␉MD5_CTX␉␉md5c;␊ |
307 | ␉uint8_t␉␉mdresult[16];␊ |
308 | ␊ |
309 | ␉bzero(mdresult, sizeof(mdresult));␊ |
310 | ␊ |
311 | ␉// just like AppleFileSystemDriver␊ |
312 | ␉MD5Init(&md5c);␊ |
313 | ␉MD5Update(&md5c, kFSUUIDNamespaceSHA1, sizeof(kFSUUIDNamespaceSHA1));␊ |
314 | ␉MD5Update(&md5c, uubytes, nbytes);␊ |
315 | ␉MD5Final(mdresult, &md5c);␊ |
316 | ␊ |
317 | ␉// this UUID has been made version 3 style (i.e. via namespace)␊ |
318 | ␉// see "-uuid-urn-" IETF draft (which otherwise copies byte for byte)␊ |
319 | ␉mdresult[6] = 0x30 | ( mdresult[6] & 0x0F );␊ |
320 | ␉mdresult[8] = 0x80 | ( mdresult[8] & 0x3F );␊ |
321 | ␊ |
322 | ␊ |
323 | ␉// generate the text: e.g. 5EB1869F-C4FA-3502-BDEB-3B8ED5D87292␊ |
324 | ␉i = 0;␊ |
325 | ␉fmtbase = 0;␊ |
326 | ␊ |
327 | ␉for (fmtidx = 0; fmtidx < sizeof(uuidfmt); fmtidx++)␊ |
328 | ␉{␊ |
329 | ␉␉for (i = 0; i < uuidfmt[fmtidx]; i++)␊ |
330 | ␉␉{␊ |
331 | ␉␉␉uint8_t byte = mdresult[fmtbase + i];␊ |
332 | ␉␉␉char nib = byte >> 4;␊ |
333 | ␉␉␉*p = nib + '0'; // 0x4 -> '4'␊ |
334 | ␊ |
335 | ␉␉␉if (*p > '9')␊ |
336 | ␉␉␉{␊ |
337 | ␉␉␉␉*p = (nib - 9 + ('A'-1)); // 0xB -> 'B'␊ |
338 | ␉␉␉}␊ |
339 | ␊ |
340 | ␉␉␉p++;␊ |
341 | ␊ |
342 | ␉␉␉nib = byte & 0xf;␊ |
343 | ␉␉␉*p = nib + '0'; // 0x4 -> '4'␊ |
344 | ␊ |
345 | ␉␉␉if (*p > '9')␊ |
346 | ␉␉␉{␊ |
347 | ␉␉␉␉*p = (nib - 9 + ('A'-1)); // 0xB -> 'B'␊ |
348 | ␉␉␉}␊ |
349 | ␊ |
350 | ␉␉␉p++;␊ |
351 | ␉␉}␊ |
352 | ␊ |
353 | ␉␉fmtbase += i;␊ |
354 | ␊ |
355 | ␉␉if (fmtidx < sizeof(uuidfmt) - 1)␊ |
356 | ␉␉{␊ |
357 | ␉␉␉*(p++) = '-';␊ |
358 | ␉␉}␊ |
359 | ␉␉else␊ |
360 | ␉␉{␊ |
361 | ␉␉␉*p = '\0';␊ |
362 | ␉␉}␊ |
363 | ␉}␊ |
364 | ␊ |
365 | ␉return 0;␊ |
366 | }␊ |
367 | ␊ |
368 | ␊ |
369 | //==========================================================================␊ |
370 | // GetDirEntry - LOW-LEVEL FILESYSTEM FUNCTION.␊ |
371 | // Fetch the next directory entry for the given directory.␊ |
372 | ␊ |
373 | long GetDirEntry(const char * dirSpec, long long * dirIndex, const char ** name, long * flags, u_int32_t * time)␊ |
374 | {␊ |
375 | ␉const char␉*dirPath;␊ |
376 | ␉BVRef␉␉bvr;␊ |
377 | ␊ |
378 | ␉// Resolve the boot volume from the dir spec.␊ |
379 | ␊ |
380 | ␉if ((bvr = getBootVolumeRef(dirSpec, &dirPath)) == NULL)␊ |
381 | ␉{␊ |
382 | ␉␉return -1;␊ |
383 | ␉}␊ |
384 | ␊ |
385 | ␉if (bvr->fs_getdirentry)␊ |
386 | ␉{␊ |
387 | ␉␉// Returns 0 on success or -1 when there are no additional entries.␊ |
388 | ␉␉return bvr->fs_getdirentry( bvr,␊ |
389 | ␉␉␉/* dirPath */ (char *)dirPath,␊ |
390 | ␉␉␉/* dirIndex */ dirIndex,␊ |
391 | ␉␉␉/* dirEntry */ (char **)name, flags, time, 0, 0 );␊ |
392 | ␉}␊ |
393 | ␊ |
394 | ␉return -1;␊ |
395 | }␊ |
396 | ␊ |
397 | //==========================================================================␊ |
398 | // GetFileInfo - LOW-LEVEL FILESYSTEM FUNCTION.␊ |
399 | // Get attributes for the specified file.␊ |
400 | ␊ |
401 | static char *gMakeDirSpec;␊ |
402 | ␊ |
403 | long GetFileInfo(const char *dirSpec, const char *name, long *flags, u_int32_t *time)␊ |
404 | {␊ |
405 | ␉long long index = 0;␊ |
406 | ␉const char *entryName;␊ |
407 | ␊ |
408 | ␉if (gMakeDirSpec == 0)␊ |
409 | ␉{␊ |
410 | ␉␉gMakeDirSpec = (char *)malloc(1024);␊ |
411 | ␉}␊ |
412 | ␊ |
413 | ␉if (!dirSpec)␊ |
414 | ␉{␊ |
415 | ␉␉long␉idx;␊ |
416 | ␉␉long␉len;␊ |
417 | ␉␉len = strlen(name);␊ |
418 | ␊ |
419 | ␉␉for (idx = len; idx && (name[idx] != '/' && name[idx] != '\\'); idx--) {}␊ |
420 | ␊ |
421 | ␉␉if (idx == 0)␊ |
422 | ␉␉{␊ |
423 | ␉␉␉if(name[idx] == '/' || name[idx] == '\\')␊ |
424 | ␉␉␉{␊ |
425 | ␉␉␉␉++name; // todo: ensure other functions handel \ properly␊ |
426 | ␉␉␉}␊ |
427 | ␉␉␉gMakeDirSpec[0] = '/';␊ |
428 | ␉␉␉gMakeDirSpec[1] = '\0';␊ |
429 | ␉␉␉gMakeDirSpec[idx] = '\0';␊ |
430 | ␉␉}␊ |
431 | ␉␉else␊ |
432 | ␉␉{␊ |
433 | ␉␉␉idx++;␊ |
434 | ␉␉␉strncpy(gMakeDirSpec, name, idx);␊ |
435 | ␉␉␉gMakeDirSpec[idx] = '\0'; // ISSUE: http://forge.voodooprojects.org/p/chameleon/issues/270/␊ |
436 | ␉␉␉name += idx;␊ |
437 | ␉␉}␊ |
438 | ␊ |
439 | ␉␉dirSpec = gMakeDirSpec;␊ |
440 | ␉}␊ |
441 | ␊ |
442 | ␉while (GetDirEntry(dirSpec, &index, &entryName, flags, time) == 0)␊ |
443 | ␉{␊ |
444 | ␉␉if (strcmp(entryName, name) == 0)␊ |
445 | ␉␉{␊ |
446 | ␉␉␉return 0; // success␊ |
447 | ␉␉}␊ |
448 | ␉}␊ |
449 | ␊ |
450 | ␉return -1; // file not found␊ |
451 | }␊ |
452 | ␊ |
453 | //==============================================================================␊ |
454 | ␊ |
455 | long GetFileBlock(const char *fileSpec, unsigned long long *firstBlock)␊ |
456 | {␊ |
457 | ␉const char * filePath;␊ |
458 | ␉BVRef bvr;␊ |
459 | ␊ |
460 | ␉// Resolve the boot volume from the file spec.␊ |
461 | ␊ |
462 | ␉if ((bvr = getBootVolumeRef(fileSpec, &filePath)) == NULL)␊ |
463 | ␉{␊ |
464 | ␉␉// printf("Boot volume for '%s' is bogus\n", fileSpec);␊ |
465 | ␉␉return -1;␊ |
466 | ␉}␊ |
467 | ␊ |
468 | ␉return bvr->fs_getfileblock(bvr, (char *)filePath, firstBlock);␊ |
469 | }␊ |
470 | ␊ |
471 | //==========================================================================␊ |
472 | // GetFreeFD()␊ |
473 | ␊ |
474 | static int GetFreeFd(void)␊ |
475 | {␊ |
476 | ␉int␉fd;␊ |
477 | ␊ |
478 | ␉// Locate a free descriptor slot.␊ |
479 | ␉for (fd = 0; fd < NFILES; fd++)␊ |
480 | ␉{␊ |
481 | ␉␉if (iob[fd].i_flgs == 0)␊ |
482 | ␉␉{␊ |
483 | ␉␉␉return fd;␊ |
484 | ␉␉}␊ |
485 | ␉}␊ |
486 | ␉stop("Out of file descriptors");␊ |
487 | ␉// not reached␊ |
488 | ␉return -1;␊ |
489 | }␊ |
490 | ␊ |
491 | //==========================================================================␊ |
492 | // iob_from_fdesc()␊ |
493 | //␊ |
494 | // Return a pointer to an allocated 'iob' based on the file descriptor␊ |
495 | // provided. Returns NULL if the file descriptor given is invalid.␊ |
496 | ␊ |
497 | static struct iob * iob_from_fdesc(int fdesc)␊ |
498 | {␊ |
499 | ␉register struct iob * io;␊ |
500 | ␊ |
501 | ␉if (fdesc < 0 || fdesc >= NFILES || ((io = &iob[fdesc])->i_flgs & F_ALLOC) == 0)␊ |
502 | ␉{␊ |
503 | ␉␉return NULL;␊ |
504 | ␉}␊ |
505 | ␉else␊ |
506 | ␉{␊ |
507 | ␉␉return io;␊ |
508 | ␉}␊ |
509 | }␊ |
510 | ␊ |
511 | //==========================================================================␊ |
512 | // openmem()␊ |
513 | ␊ |
514 | int openmem(char *buf, int len)␊ |
515 | {␊ |
516 | ␉int fdesc;␊ |
517 | ␉struct iob *io;␊ |
518 | ␊ |
519 | ␉fdesc = GetFreeFd();␊ |
520 | ␉io = &iob[fdesc];␊ |
521 | ␉bzero(io, sizeof(*io));␊ |
522 | ␊ |
523 | ␉// Mark the descriptor as taken. Set the F_MEM flag to indicate␊ |
524 | ␉// that the file buffer is provided by the caller.␊ |
525 | ␊ |
526 | ␉io->i_flgs = F_ALLOC | F_MEM;␊ |
527 | ␉io->i_buf = buf;␊ |
528 | ␉io->i_filesize = len;␊ |
529 | ␊ |
530 | ␉return fdesc;␊ |
531 | }␊ |
532 | ␊ |
533 | //==========================================================================␊ |
534 | // open() - Open the file specified by 'path' for reading.␊ |
535 | ␊ |
536 | static int open_bvr(BVRef bvr, const char *filePath, int flags)␊ |
537 | {␊ |
538 | ␉struct iob␉*io;␊ |
539 | ␉int␉␉fdesc;␊ |
540 | ␉int␉␉i;␊ |
541 | ␊ |
542 | ␉if (bvr == NULL)␊ |
543 | ␉{␊ |
544 | ␉␉return -1;␊ |
545 | ␉}␊ |
546 | ␊ |
547 | ␉fdesc = GetFreeFd();␊ |
548 | ␉io = &iob[fdesc];␊ |
549 | ␉bzero(io, sizeof(*io));␊ |
550 | ␊ |
551 | ␉// Mark the descriptor as taken.␊ |
552 | ␉io->i_flgs = F_ALLOC;␊ |
553 | ␊ |
554 | ␉// Find the next available memory block in the download buffer.␊ |
555 | ␉io->i_buf = (char *) LOAD_ADDR;␊ |
556 | ␉for (i = 0; i < NFILES; i++)␊ |
557 | ␉{␊ |
558 | ␉␉if ((iob[i].i_flgs != F_ALLOC) || (i == fdesc))␊ |
559 | ␉␉{␊ |
560 | ␉␉␉continue;␊ |
561 | ␉␉}␊ |
562 | ␊ |
563 | ␉␉io->i_buf = MAX(iob[i].i_filesize + iob[i].i_buf, io->i_buf);␊ |
564 | ␉}␊ |
565 | ␊ |
566 | ␉// Load entire file into memory. Unnecessary open() calls must be avoided.␊ |
567 | ␉gFSLoadAddress = io->i_buf;␊ |
568 | ␉io->i_filesize = bvr->fs_loadfile(bvr, (char *)filePath);␊ |
569 | ␉if (io->i_filesize < 0)␊ |
570 | ␉{␊ |
571 | ␉␉close(fdesc);␊ |
572 | ␉␉return -1;␊ |
573 | ␉}␊ |
574 | ␉return fdesc;␊ |
575 | }␊ |
576 | ␊ |
577 | int open(const char *path, int flags)␊ |
578 | {␊ |
579 | ␉const char␉*filepath;␊ |
580 | ␉BVRef␉␉bvr;␊ |
581 | ␊ |
582 | ␉// Resolve the boot volume from the file spec.␊ |
583 | ␉if ((bvr = getBootVolumeRef(path, &filepath)) != NULL)␊ |
584 | ␉{␊ |
585 | ␉␉return open_bvr(bvr, filepath, flags);␊ |
586 | ␉}␊ |
587 | ␉return -1;␊ |
588 | }␊ |
589 | ␊ |
590 | //==========================================================================␊ |
591 | int open_bvdev(const char *bvd, const char *path, int flags)␊ |
592 | {␊ |
593 | ␉const struct devsw␉*dp;␊ |
594 | ␉const char␉␉*cp;␊ |
595 | ␉BVRef␉␉␉bvr;␊ |
596 | ␉int␉␉␉i;␊ |
597 | ␉int␉␉␉len;␊ |
598 | ␉int␉␉␉unit;␊ |
599 | ␉int␉␉␉partition;␊ |
600 | ␊ |
601 | ␉if ((i = open(path, flags)) >= 0)␊ |
602 | ␉{␊ |
603 | ␉␉return i;␊ |
604 | ␉}␊ |
605 | ␊ |
606 | ␉if (bvd == NULL || (len = strlen(bvd)) < 2)␊ |
607 | ␉{␊ |
608 | ␉␉return -1;␊ |
609 | ␉}␊ |
610 | ␊ |
611 | ␉if (path && path[2] == '(')␊ |
612 | ␉{␊ |
613 | ␉␉for (dp = &devsw[0]; dp->name; ++dp)␊ |
614 | ␉␉{␊ |
615 | ␉␉␉if (path[0] == dp->name[0] && path[1] == dp->name[1])␊ |
616 | ␉␉␉{␊ |
617 | ␉␉␉␉return -1;␊ |
618 | ␉␉␉}␊ |
619 | ␉␉}␊ |
620 | ␉}␊ |
621 | ␊ |
622 | ␉for (dp=devsw; dp->name; dp++)␊ |
623 | ␉{␊ |
624 | ␉␉if (bvd[0] == dp->name[0] && bvd[1] == dp->name[1])␊ |
625 | ␉␉{␊ |
626 | ␉␉␉unit = 0;␊ |
627 | ␉␉␉partition = 0;␊ |
628 | ␉␉␉/* get optional unit and partition */␊ |
629 | ␉␉␉if (len >= 5 && bvd[2] == '(') { /* min must be present xx(0) */␊ |
630 | ␉␉␉␉cp = &bvd[3];␊ |
631 | ␉␉␉␉i = 0;␊ |
632 | ␉␉␉␉while ((cp - path) < len && isdigit(*cp))␊ |
633 | ␉␉␉␉{␊ |
634 | ␉␉␉␉␉i = i * 10 + *cp++ - '0';␊ |
635 | ␉␉␉␉␉unit = i;␊ |
636 | ␉␉␉␉}␊ |
637 | ␉␉␉␉if (*cp++ == ',')␊ |
638 | ␉␉␉␉{␊ |
639 | ␉␉␉␉␉i = 0;␊ |
640 | ␉␉␉␉␉while ((cp - path) < len && isdigit(*cp))␊ |
641 | ␉␉␉␉␉{␊ |
642 | ␉␉␉␉␉␉i = i * 10 + *cp++ - '0';␊ |
643 | ␉␉␉␉␉␉partition = i;␊ |
644 | ␉␉␉␉␉}␊ |
645 | ␉␉␉␉}␊ |
646 | ␉␉␉}␊ |
647 | ␉␉␉bvr = newBootVolumeRef(dp->biosdev + unit, partition);␊ |
648 | ␊ |
649 | ␉␉␉return open_bvr(bvr, path, flags);␊ |
650 | ␉␉}␊ |
651 | ␉}␊ |
652 | ␉return -1;␊ |
653 | }␊ |
654 | ␊ |
655 | //==========================================================================␊ |
656 | // close() - Close a file descriptor.␊ |
657 | ␊ |
658 | int close(int fdesc)␊ |
659 | {␊ |
660 | ␉struct iob * io;␊ |
661 | ␊ |
662 | ␉if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
663 | ␉{␊ |
664 | ␉␉return (-1);␊ |
665 | ␉}␊ |
666 | ␊ |
667 | ␉io->i_flgs = 0;␊ |
668 | ␊ |
669 | ␉return 0;␊ |
670 | }␊ |
671 | ␊ |
672 | //==========================================================================␊ |
673 | // lseek() - Reposition the byte offset of the file descriptor from the␊ |
674 | // beginning of the file. Returns the relocated offset.␊ |
675 | ␊ |
676 | int b_lseek(int fdesc, int offset, int ptr)␊ |
677 | {␊ |
678 | ␉struct iob * io;␊ |
679 | ␊ |
680 | ␉if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
681 | ␉{␊ |
682 | ␉␉return (-1);␊ |
683 | ␉}␊ |
684 | ␊ |
685 | ␉io->i_offset = offset;␊ |
686 | ␊ |
687 | ␉return offset;␊ |
688 | }␊ |
689 | ␊ |
690 | //==========================================================================␊ |
691 | // tell() - Returns the byte offset of the file descriptor.␊ |
692 | ␊ |
693 | int tell(int fdesc)␊ |
694 | {␊ |
695 | ␉struct iob * io;␊ |
696 | ␊ |
697 | ␉if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
698 | ␉{␊ |
699 | ␉␉return 0;␊ |
700 | ␉}␊ |
701 | ␊ |
702 | ␉return io->i_offset;␊ |
703 | }␊ |
704 | ␊ |
705 | //==========================================================================␊ |
706 | // read() - Read up to 'count' bytes of data from the file descriptor␊ |
707 | // into the buffer pointed to by buf.␊ |
708 | ␊ |
709 | int read(int fdesc, char * buf, int count)␊ |
710 | {␊ |
711 | ␉struct iob * io;␊ |
712 | ␊ |
713 | ␉if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
714 | ␉{␊ |
715 | ␉␉return (-1);␊ |
716 | ␉}␊ |
717 | ␊ |
718 | ␉if ((io->i_offset + count) > (unsigned int)io->i_filesize)␊ |
719 | ␉{␊ |
720 | ␉␉count = io->i_filesize - io->i_offset;␊ |
721 | ␉}␊ |
722 | ␊ |
723 | ␉if (count <= 0)␊ |
724 | ␉{␊ |
725 | ␉␉return 0; // end of file␊ |
726 | ␉}␊ |
727 | ␊ |
728 | ␉bcopy(io->i_buf + io->i_offset, buf, count);␊ |
729 | ␊ |
730 | ␉io->i_offset += count;␊ |
731 | ␊ |
732 | ␉return count;␊ |
733 | }␊ |
734 | ␊ |
735 | //==========================================================================␊ |
736 | // write() - Write up to 'count' bytes of data to the file descriptor␊ |
737 | // from the buffer pointed to by buf.␊ |
738 | ␊ |
739 | int write(int fdesc, const char * buf, int count)␊ |
740 | {␊ |
741 | struct iob * io;␊ |
742 | ␊ |
743 | if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
744 | return (-1);␊ |
745 | ␊ |
746 | if ((io->i_offset + count) > (unsigned int)io->i_filesize)␊ |
747 | count = io->i_filesize - io->i_offset;␊ |
748 | ␊ |
749 | if (count <= 0)␊ |
750 | return 0; // end of file␊ |
751 | ␊ |
752 | bcopy(buf, io->i_buf + io->i_offset, count);␊ |
753 | ␊ |
754 | io->i_offset += count;␊ |
755 | ␊ |
756 | return count;␊ |
757 | }␊ |
758 | ␊ |
759 | //==========================================================================␊ |
760 | ␊ |
761 | int writebyte(int fdesc, char value)␊ |
762 | {␊ |
763 | ␉struct iob * io;␊ |
764 | ␊ |
765 | ␉if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
766 | return (-1);␊ |
767 | ␊ |
768 | if ((io->i_offset + 1) > (unsigned int)io->i_filesize)␊ |
769 | return 0; // end of file␊ |
770 | ␊ |
771 | io->i_buf[io->i_offset++] = value;␊ |
772 | ␊ |
773 | return 1;␊ |
774 | }␊ |
775 | ␊ |
776 | //==========================================================================␊ |
777 | ␊ |
778 | int writeint(int fdesc, int value)␊ |
779 | {␊ |
780 | struct iob * io;␊ |
781 | ␊ |
782 | if ((io = iob_from_fdesc(fdesc)) == NULL)␊ |
783 | return (-1);␊ |
784 | ␊ |
785 | if ((io->i_offset + 4) > (unsigned int)io->i_filesize)␊ |
786 | return 0; // end of file␊ |
787 | ␊ |
788 | bcopy(&value, io->i_buf + io->i_offset, 4);␊ |
789 | ␊ |
790 | io->i_offset += 4;␊ |
791 | ␊ |
792 | return 4;␊ |
793 | }␊ |
794 | ␊ |
795 | //==========================================================================␊ |
796 | // file_size() - Returns the size of the file described by the file␊ |
797 | // descriptor.␊ |
798 | ␊ |
799 | int file_size(int fdesc)␊ |
800 | {␊ |
801 | ␉struct iob * io;␊ |
802 | ␊ |
803 | ␉if ((io = iob_from_fdesc(fdesc)) == 0)␊ |
804 | ␉{␊ |
805 | ␉␉return 0;␊ |
806 | ␉}␊ |
807 | ␊ |
808 | ␉return io->i_filesize;␊ |
809 | }␊ |
810 | ␊ |
811 | //==========================================================================␊ |
812 | ␊ |
813 | struct dirstuff * vol_opendir(BVRef bvr, const char * path)␊ |
814 | {␊ |
815 | ␉struct dirstuff * dirp = 0;␊ |
816 | ␊ |
817 | ␉dirp = (struct dirstuff *) malloc(sizeof(struct dirstuff));␊ |
818 | ␊ |
819 | ␉if (dirp == NULL)␊ |
820 | ␉␉goto error;␊ |
821 | ␊ |
822 | ␉dirp->dir_path = newString(path);␊ |
823 | ␉if (dirp->dir_path == NULL)␊ |
824 | ␉␉goto error;␊ |
825 | ␊ |
826 | ␉dirp->dir_bvr = bvr;␊ |
827 | ␊ |
828 | ␉return dirp;␊ |
829 | ␊ |
830 | error:␊ |
831 | ␉closedir(dirp);␊ |
832 | ␊ |
833 | ␉return NULL;␊ |
834 | }␊ |
835 | ␊ |
836 | //==========================================================================␊ |
837 | ␊ |
838 | struct dirstuff *opendir(const char *path)␊ |
839 | {␊ |
840 | ␉struct dirstuff *dirp = 0;␊ |
841 | ␉const char *dirPath;␊ |
842 | ␉BVRef bvr;␊ |
843 | ␊ |
844 | ␉if ((bvr = getBootVolumeRef(path, &dirPath)) == NULL)␊ |
845 | ␉␉goto error;␊ |
846 | ␊ |
847 | ␉dirp = (struct dirstuff *)malloc(sizeof(struct dirstuff));␊ |
848 | ␉if (dirp == NULL)␊ |
849 | ␉␉goto error;␊ |
850 | ␊ |
851 | ␉dirp->dir_path = newString(dirPath);␊ |
852 | ␉if (dirp->dir_path == NULL)␊ |
853 | ␉␉goto error;␊ |
854 | ␊ |
855 | ␉dirp->dir_bvr = bvr;␊ |
856 | ␊ |
857 | return dirp;␊ |
858 | ␊ |
859 | error:␊ |
860 | ␉closedir(dirp);␊ |
861 | ␊ |
862 | ␉return NULL;␊ |
863 | }␊ |
864 | ␊ |
865 | //==========================================================================␊ |
866 | ␊ |
867 | int closedir(struct dirstuff * dirp)␊ |
868 | {␊ |
869 | ␉if (dirp)␊ |
870 | ␉{␊ |
871 | ␉␉if (dirp->dir_path)␊ |
872 | ␉␉{␊ |
873 | ␉␉␉free(dirp->dir_path);␊ |
874 | ␉␉}␊ |
875 | ␊ |
876 | ␉␉free(dirp);␊ |
877 | ␉}␊ |
878 | ␊ |
879 | ␉return 0;␊ |
880 | }␊ |
881 | ␊ |
882 | //==========================================================================␊ |
883 | ␊ |
884 | int readdir(struct dirstuff * dirp, const char ** name, long * flags, u_int32_t * time)␊ |
885 | {␊ |
886 | ␉return dirp->dir_bvr->fs_getdirentry(dirp->dir_bvr,␊ |
887 | ␉␉/* dirPath */ dirp->dir_path,␊ |
888 | ␉␉/* dirIndex */ &dirp->dir_index,␊ |
889 | ␉␉/* dirEntry */ (char **)name, flags, time, 0, 0);␊ |
890 | }␊ |
891 | ␊ |
892 | //==========================================================================␊ |
893 | ␊ |
894 | int readdir_ext(struct dirstuff * dirp, const char ** name, long * flags, u_int32_t * time, FinderInfo *finderInfo, long *infoValid)␊ |
895 | {␊ |
896 | ␉return dirp->dir_bvr->fs_getdirentry( dirp->dir_bvr,␊ |
897 | ␉␉␉/* dirPath */ dirp->dir_path,␊ |
898 | ␉␉␉/* dirIndex */ &dirp->dir_index,␊ |
899 | ␉␉␉/* dirEntry */ (char **)name,␊ |
900 | ␉␉␉␉␉flags, time,␊ |
901 | ␉␉␉␉␉finderInfo, infoValid);␊ |
902 | }␊ |
903 | ␊ |
904 | //==========================================================================␊ |
905 | ␊ |
906 | const char * systemConfigDir()␊ |
907 | {␊ |
908 | ␉if (gBootFileType == kNetworkDeviceType)␊ |
909 | ␉{␊ |
910 | ␉␉return "";␊ |
911 | ␉}␊ |
912 | ␉return "/Library/Preferences/SystemConfiguration";␊ |
913 | }␊ |
914 | ␊ |
915 | //==========================================================================␊ |
916 | ␊ |
917 | int gBootFileType;␊ |
918 | ␊ |
919 | void scanBootVolumes(int biosdev, int * count)␊ |
920 | {␊ |
921 | ␉BVRef bvr = 0;␊ |
922 | ␊ |
923 | ␉bvr = diskScanBootVolumes(biosdev, count);␊ |
924 | ␉if (bvr == NULL)␊ |
925 | ␉{␊ |
926 | ␉␉bvr = nbpScanBootVolumes(biosdev, count);␊ |
927 | ␉␉if (bvr != NULL)␊ |
928 | ␉␉{␊ |
929 | ␉␉␉gBootFileType = kNetworkDeviceType;␊ |
930 | ␉␉}␊ |
931 | ␉}␊ |
932 | ␉else␊ |
933 | ␉{␊ |
934 | ␉␉gBootFileType = kBlockDeviceType;␊ |
935 | ␉}␊ |
936 | }␊ |
937 | ␊ |
938 | //==========================================================================␊ |
939 | ␊ |
940 | void scanDisks(int biosdev, int *count)␊ |
941 | {␊ |
942 | ␉#define MAX_HDD_COUNT 32␊ |
943 | ␉int bvCount;␊ |
944 | ␉int hd = 0;␊ |
945 | ␊ |
946 | ␉// Testing up to MAX_HDD_COUNT hard drives.␊ |
947 | ␉while(!testBiosread(0x80 + hd, 0) && hd < MAX_HDD_COUNT)␊ |
948 | ␉{␊ |
949 | ␉␉bvCount = 0;␊ |
950 | ␉␉scanBootVolumes(0x80 + hd, &bvCount);␊ |
951 | ␉␉hd++;␊ |
952 | ␉}␊ |
953 | ␊ |
954 | ␉// Also scanning CD/DVD drive.␊ |
955 | ␉if (biosDevIsCDROM(gBIOSDev))␊ |
956 | ␉{␊ |
957 | ␉␉bvCount = 0;␊ |
958 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
959 | ␉}␊ |
960 | }␊ |
961 | ␊ |
962 | //==========================================================================␊ |
963 | ␊ |
964 | BVRef selectBootVolume(BVRef chain)␊ |
965 | {␊ |
966 | ␉bool filteredChain␉= false;␊ |
967 | ␉bool foundPrimary␉= false;␊ |
968 | ␊ |
969 | ␉BVRef bvr␉␉= NULL;␊ |
970 | ␉BVRef bvr1␉␉= NULL;␊ |
971 | ␉BVRef bvr2␉␉= NULL;␊ |
972 | ␊ |
973 | #if FEATURE_LAST_BOOT␊ |
974 | ␉char dirSpec[]␉␉= "hd(%d,%d)/";␊ |
975 | ␉char fileSpec[]␉␉= "Volumes";␊ |
976 | ␉char *label;␊ |
977 | ␉u_int32_t time;␊ |
978 | ␉u_int32_t lasttime␉= 0;␊ |
979 | ␉long flags;␊ |
980 | #endif␊ |
981 | ␊ |
982 | ␉if (chain->filtered)␊ |
983 | ␉{␊ |
984 | ␉␉filteredChain = true;␊ |
985 | ␉}␊ |
986 | ␊ |
987 | ␉if (multiboot_partition_set)␊ |
988 | ␉{␊ |
989 | ␉␉for ( bvr = chain; bvr; bvr = bvr->next )␊ |
990 | ␉␉{␊ |
991 | ␉␉␉if (!bvr)␊ |
992 | ␉␉␉{␊ |
993 | ␉␉␉␉break;␊ |
994 | ␉␉␉}␊ |
995 | ␊ |
996 | ␉␉␉if ( (bvr->part_no == multiboot_partition) && (bvr->biosdev == gBIOSDev) )␊ |
997 | ␉␉␉{␊ |
998 | #if FEATURE_LAST_BOOT␊ |
999 | ␉␉␉␉label = bvr->label[0] ? bvr->label : (bvr->altlabel[0] ? bvr->altlabel : (bvr->name[0] ? bvr->name : "Untitled"));␊ |
1000 | ␉␉␉␉DBG("Multiboot partition set: hd(%d,%d) '%s'\n", BIOS_DEV_UNIT(bvr), bvr->part_no, label);␊ |
1001 | #endif␊ |
1002 | ␉␉␉␉return bvr;␊ |
1003 | ␉␉␉}␊ |
1004 | ␉␉}␊ |
1005 | ␉}␊ |
1006 | ␊ |
1007 | #if 0␊ |
1008 | ␉DBG("multiboot_partition_set = %d\n", multiboot_partition_set);␊ |
1009 | ␉DBG("multiboot_partition = %d\n", multiboot_partition);␊ |
1010 | ␉DBG("multiboot_skip_partition_set = %d\n", multiboot_skip_partition_set);␊ |
1011 | ␉DBG("multiboot_skip_partition = %d\n", multiboot_skip_partition);␊ |
1012 | #endif␊ |
1013 | ␊ |
1014 | ␉/*␊ |
1015 | ␉ * Checking "Default Partition" key in system configuration - use format: hd(x,y), the volume UUID or label -␊ |
1016 | ␉ * to override the default selection.␊ |
1017 | ␉ * We accept only kBVFlagSystemVolume or kBVFlagForeignBoot volumes.␊ |
1018 | ␉ */␊ |
1019 | ␉char *val = XMLDecode(getStringForKey(kDefaultPartition, &bootInfo->chameleonConfig));␊ |
1020 | ␉if (val)␊ |
1021 | ␉{␊ |
1022 | ␉␉for ( bvr = chain; bvr; bvr = bvr->next )␊ |
1023 | ␉␉{␊ |
1024 | ␉␉␉if (!bvr)␊ |
1025 | ␉␉␉{␊ |
1026 | ␉␉␉␉break;␊ |
1027 | ␉␉␉}␊ |
1028 | ␊ |
1029 | ␉␉␉if (matchVolumeToString(bvr, val, false))␊ |
1030 | ␉␉␉{␊ |
1031 | ␉␉␉␉free(val);␊ |
1032 | #if FEATURE_LAST_BOOT␊ |
1033 | ␉␉␉␉label = bvr->label[0] ? bvr->label : (bvr->altlabel[0] ? bvr->altlabel : (bvr->name[0] ? bvr->name : "Untitled"));␊ |
1034 | ␉␉␉␉DBG("User default partition set: hd(%d,%d) '%s'\n", BIOS_DEV_UNIT(bvr), bvr->part_no, label);␊ |
1035 | #endif␊ |
1036 | ␉␉␉␉return bvr;␊ |
1037 | ␉␉␉}␊ |
1038 | ␉␉}␊ |
1039 | ␉␉free(val);␊ |
1040 | ␉}␊ |
1041 | ␊ |
1042 | #if FEATURE_LAST_BOOT // the above code cause "AllocateKernelMemory error"␊ |
1043 | ␉// Bungo: select last booted partition as the boot volume␊ |
1044 | ␉// TODO: support other OSes (foreign boot)␊ |
1045 | ␉for (bvr = chain; bvr; bvr = bvr->next)␊ |
1046 | ␉{␊ |
1047 | ␉␉if (bvr->flags & (kBVFlagSystemVolume | kBVFlagForeignBoot))␊ |
1048 | ␉␉{␊ |
1049 | ␉␉␉time = 0;␊ |
1050 | ␉␉␉flags = 0;␊ |
1051 | ␉␉␉sprintf(dirSpec, "hd(%d,%d)/", BIOS_DEV_UNIT(bvr), bvr->part_no);␊ |
1052 | ␉␉␉if ((GetFileInfo(dirSpec, fileSpec, &flags, &time) == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory))␊ |
1053 | ␉␉␉{␊ |
1054 | ␉␉␉␉if (time > lasttime)␊ |
1055 | ␉␉␉␉{␊ |
1056 | ␉␉␉␉␉lasttime = time;␊ |
1057 | ␉␉␉␉␉bvr1 = bvr;␊ |
1058 | ␉␉␉␉␉label = bvr1->label[0] ? bvr1->label : (bvr1->altlabel[0] ? bvr1->altlabel : (bvr1->name[0] ? bvr1->name : "Untitled"));␊ |
1059 | ␉␉␉␉}␊ |
1060 | ␉␉␉}␊ |
1061 | ␉␉}␊ |
1062 | ␉}␊ |
1063 | ␊ |
1064 | ␉if (bvr1)␊ |
1065 | ␉{␊ |
1066 | ␉␉DBG("Last booted volume: hd(%d,%d) '%s' set as default partition\n\n", BIOS_DEV_UNIT(bvr1), bvr1->part_no, label);␊ |
1067 | ␉␉return bvr1;␊ |
1068 | ␉}␊ |
1069 | ␉// Bungo: code below selects first partition in the chain (last partition on disk),␊ |
1070 | ␉// in my case Recovery HD, as boot volume, so I would prefer last booted partition␊ |
1071 | ␉// as default boot volume - see the code above␊ |
1072 | #endif␊ |
1073 | ␊ |
1074 | ␉/*␊ |
1075 | ␉ * Scannig the volume chain backwards and trying to find␊ |
1076 | ␉ * a HFS+ volume with valid boot record signature.␊ |
1077 | ␉ * If not found any active partition then we will␊ |
1078 | ␉ * select this volume as the boot volume.␊ |
1079 | ␉ */␊ |
1080 | ␉for ( bvr = chain; bvr; bvr = bvr->next )␊ |
1081 | ␉{␊ |
1082 | ␉␉if (!bvr)␊ |
1083 | ␉␉{␊ |
1084 | ␉␉␉break;␊ |
1085 | ␉␉}␊ |
1086 | ␊ |
1087 | ␉␉if (multiboot_skip_partition_set)␊ |
1088 | ␉␉{␊ |
1089 | ␉␉␉if (bvr->part_no == multiboot_skip_partition)␊ |
1090 | ␉␉␉{␊ |
1091 | ␉␉␉␉continue;␊ |
1092 | ␉␉␉}␊ |
1093 | ␉␉}␊ |
1094 | ␊ |
1095 | ␉␉if ( (bvr->flags & kBVFlagPrimary) && (bvr->biosdev == gBIOSDev) )␊ |
1096 | ␉␉{␊ |
1097 | ␉␉␉foundPrimary = true;␊ |
1098 | ␉␉}␊ |
1099 | ␊ |
1100 | ␉␉// zhell -- Undo a regression that was introduced from r491 to 492.␊ |
1101 | ␉␉// if gBIOSBootVolume is set already, no change is required␊ |
1102 | ␉␉if ( (bvr->flags & (kBVFlagBootable | kBVFlagSystemVolume))␊ |
1103 | ␉␉␉&& gBIOSBootVolume␊ |
1104 | ␉␉␉&& (!filteredChain || (filteredChain && bvr->visible))␊ |
1105 | ␉␉␉&& (bvr->biosdev == gBIOSDev) )␊ |
1106 | ␉␉{␊ |
1107 | ␉␉␉bvr2 = bvr;␊ |
1108 | ␉␉}␊ |
1109 | ␊ |
1110 | ␉␉// zhell -- if gBIOSBootVolume is NOT set, we use the "if" statement␊ |
1111 | ␉␉// from r491,␊ |
1112 | ␉␉if ( (bvr->flags & kBVFlagBootable)␊ |
1113 | ␉␉␉&& !gBIOSBootVolume␊ |
1114 | ␉␉␉&& (bvr->biosdev == gBIOSDev) )␊ |
1115 | ␉␉{␊ |
1116 | ␉␉␉bvr2 = bvr;␊ |
1117 | ␉␉}␊ |
1118 | ␉}␊ |
1119 | ␊ |
1120 | ␉/*␊ |
1121 | ␉ * Use the standrad method for selecting the boot volume.␊ |
1122 | ␉ */␊ |
1123 | ␉if (foundPrimary)␊ |
1124 | ␉{␊ |
1125 | ␉␉for ( bvr = chain; bvr; bvr = bvr->next )␊ |
1126 | ␉␉{␊ |
1127 | ␉␉␉if (!bvr)␊ |
1128 | ␉␉␉{␊ |
1129 | ␉␉␉␉break;␊ |
1130 | ␉␉␉}␊ |
1131 | ␊ |
1132 | ␉␉␉if ( (bvr->flags & kBVFlagNativeBoot) && (bvr->biosdev == gBIOSDev) )␊ |
1133 | ␉␉␉{␊ |
1134 | ␉␉␉␉bvr1 = bvr;␊ |
1135 | ␉␉␉}␊ |
1136 | ␉␉␉if ( (bvr->flags & kBVFlagPrimary) && (bvr->biosdev == gBIOSDev) )␊ |
1137 | ␉␉␉{␊ |
1138 | ␉␉␉␉bvr2 = bvr;␊ |
1139 | ␉␉␉}␊ |
1140 | ␉␉}␊ |
1141 | ␉}␊ |
1142 | ␊ |
1143 | ␉bvr = bvr2 ? bvr2 : (bvr1 ? bvr1 : chain);␊ |
1144 | #if FEATURE_LAST_BOOT␊ |
1145 | ␉label = bvr->label[0] ? bvr->label : (bvr->altlabel[0] ? bvr->altlabel : (bvr->name[0] ? bvr->name : "Untitled"));␊ |
1146 | ␉DBG("Default partition set: hd(%d,%d) '%s'\n", BIOS_DEV_UNIT(bvr), bvr->part_no, label);␊ |
1147 | #endif␊ |
1148 | ␉return bvr;␊ |
1149 | }␊ |
1150 | ␊ |
1151 | //==========================================================================␊ |
1152 | ␊ |
1153 | ␊ |
1154 | /*!␊ |
1155 | This is like boot2's gBootVolume except it is for the internal use of␊ |
1156 | libsaio to track which volume an unqualified path should be relative to.␊ |
1157 | This replaces bootInfo->kernDev as the carrier of this information.␊ |
1158 | */␊ |
1159 | static BVRef gRootVolume;␊ |
1160 | ␊ |
1161 | //==========================================================================␊ |
1162 | ␊ |
1163 | void setRootVolume(BVRef volume)␊ |
1164 | {␊ |
1165 | ␉gRootVolume = volume;␊ |
1166 | ␉// Veto non-native FS. Basically that means don't allow the root volume to␊ |
1167 | ␉// be set to a volume we can't read files from.␊ |
1168 | ␉if(gRootVolume != NULL && ((gRootVolume->flags & kBVFlagNativeBoot) == 0))␊ |
1169 | ␉{␊ |
1170 | ␉␉gRootVolume = NULL;␊ |
1171 | ␉}␊ |
1172 | }␊ |
1173 | ␊ |
1174 | //==========================================================================␊ |
1175 | ␊ |
1176 | void setBootGlobals(BVRef chain)␊ |
1177 | {␊ |
1178 | ␉// Record default boot device.␊ |
1179 | ␉gBootVolume = selectBootVolume(chain);␊ |
1180 | ␊ |
1181 | ␉// turbo - Save the ORIGINAL boot volume too for loading our mkext␊ |
1182 | ␉if (!gBIOSBootVolume)␊ |
1183 | ␉{␊ |
1184 | ␉␉gBIOSBootVolume = gBootVolume;␊ |
1185 | ␉}␊ |
1186 | ␊ |
1187 | ␉setRootVolume(gBootVolume);␊ |
1188 | }␊ |
1189 | ␊ |
1190 | //==========================================================================␊ |
1191 | /*!␊ |
1192 | Extracts the volume selector from the pathname, returns the selected␊ |
1193 | BVRef, and sets *outPath to the remainder of the path.␊ |
1194 | If the path did not include a volume selector then the current volume␊ |
1195 | is used. When called with a volume selector the current volume␊ |
1196 | is changed to the selected volume unless the volume selector is␊ |
1197 | that of a ramdisk.␊ |
1198 | */␊ |
1199 | BVRef getBootVolumeRef(const char *path, const char **outPath)␊ |
1200 | {␊ |
1201 | ␉const char␉*cp;␊ |
1202 | ␉BVRef bvr␉= gRootVolume;␊ |
1203 | ␉int biosdev␉= gBIOSDev;␊ |
1204 | ␊ |
1205 | ␉// Search for left parenthesis in the path specification.␊ |
1206 | ␊ |
1207 | ␉for (cp = path; *cp; cp++)␊ |
1208 | ␉{␊ |
1209 | ␉␉if (*cp == LP || *cp == '/')␊ |
1210 | ␉␉{␊ |
1211 | ␉␉␉break;␊ |
1212 | ␉␉}␊ |
1213 | ␉}␊ |
1214 | ␊ |
1215 | ␉if (*cp != LP) // no left paren found␊ |
1216 | ␉{␊ |
1217 | ␉␉cp = path;␊ |
1218 | ␉␉// Path is using the implicit current device so if there is␊ |
1219 | ␉␉// no current device, then we must fail.␊ |
1220 | ␉␉if (gRootVolume == NULL)␊ |
1221 | ␉␉{␊ |
1222 | ␉␉␉return NULL;␊ |
1223 | ␉␉}␊ |
1224 | ␉}␊ |
1225 | ␉else if ((cp - path) == 2) // found "xx("␊ |
1226 | ␉{␊ |
1227 | ␉␉const struct devsw *dp;␊ |
1228 | ␉␉const char *xp = path;␊ |
1229 | ␊ |
1230 | ␉␉int i;␊ |
1231 | ␉␉int unit = -1;␊ |
1232 | ␉␉int part = -1;␊ |
1233 | ␊ |
1234 | ␉␉cp++;␊ |
1235 | ␊ |
1236 | ␉␉// Check the 2 character device name pointed by 'xp'.␊ |
1237 | ␊ |
1238 | ␉␉for (dp = devsw; dp->name; dp++)␊ |
1239 | ␉␉{␊ |
1240 | ␉␉␉if ((xp[0] == dp->name[0]) && (xp[1] == dp->name[1]))␊ |
1241 | ␉␉␉{␊ |
1242 | ␉␉␉␉break;␉// Found matching entry.␊ |
1243 | ␉␉␉}␊ |
1244 | ␉␉}␊ |
1245 | ␊ |
1246 | ␉␉if (dp->name == NULL)␊ |
1247 | ␉␉{␊ |
1248 | ␉␉␉error("Unknown device '%c%c'\n", xp[0], xp[1]);␊ |
1249 | ␉␉␉return NULL;␊ |
1250 | ␉␉}␊ |
1251 | ␊ |
1252 | ␉␉// Extract the optional unit number from the specification.␊ |
1253 | ␉␉// hd(unit) or hd(unit, part).␊ |
1254 | ␊ |
1255 | ␉␉i = 0;␊ |
1256 | ␊ |
1257 | ␉␉while (*cp >= '0' && *cp <= '9')␊ |
1258 | ␉␉{␊ |
1259 | ␉␉␉i = i * 10 + *cp++ - '0';␊ |
1260 | ␉␉␉unit = i;␊ |
1261 | ␉␉}␊ |
1262 | ␊ |
1263 | ␉␉// Unit is no longer optional and never really was.␊ |
1264 | ␉␉// If the user failed to specify it then the unit number from the previous kernDev␊ |
1265 | ␉␉// would have been used which makes little sense anyway.␊ |
1266 | ␉␉// For example, if the user did fd()/foobar and the current root device was the␊ |
1267 | ␉␉// second hard disk (i.e. unit 1) then fd() would select the second floppy drive!␊ |
1268 | ␉␉if (unit == -1)␊ |
1269 | ␉␉{␊ |
1270 | ␉␉␉return NULL;␊ |
1271 | ␉␉}␊ |
1272 | ␊ |
1273 | ␉␉// Extract the optional partition number from the specification.␊ |
1274 | ␊ |
1275 | ␉␉if (*cp == ',')␊ |
1276 | ␉␉{␊ |
1277 | ␉␉␉part = atoi(++cp);␊ |
1278 | ␉␉}␊ |
1279 | ␊ |
1280 | ␉␉// If part is not specified part will be -1 whereas before it would have been␊ |
1281 | ␉␉// whatever the last partition was which makes about zero sense if the device␊ |
1282 | ␉␉// has been switched.␊ |
1283 | ␊ |
1284 | ␉␉// Skip past the right paren.␊ |
1285 | ␊ |
1286 | ␉␉for ( ; *cp && *cp != RP; cp++) /* LOOP */;␊ |
1287 | ␊ |
1288 | ␉␉if (*cp == RP)␊ |
1289 | ␉␉{␊ |
1290 | ␉␉␉cp++;␊ |
1291 | ␉␉}␊ |
1292 | ␊ |
1293 | ␉␉biosdev = dp->biosdev + unit;␊ |
1294 | ␊ |
1295 | ␉␉bvr = newBootVolumeRef(biosdev, part);␊ |
1296 | ␊ |
1297 | ␉␉if (bvr == NULL)␊ |
1298 | ␉␉{␊ |
1299 | ␉␉␉return NULL;␊ |
1300 | ␉␉}␊ |
1301 | ␉}␊ |
1302 | ␉else␊ |
1303 | ␉{␊ |
1304 | ␉␉// Bad device specifier, skip past the right paren.␊ |
1305 | ␊ |
1306 | ␉␉for (cp++; *cp && *cp != RP; cp++) /* LOOP */;␊ |
1307 | ␉␉if (*cp == RP)␊ |
1308 | ␉␉{␊ |
1309 | ␉␉␉cp++;␊ |
1310 | ␉␉}␊ |
1311 | ␊ |
1312 | ␉␉// If gRootVolume was NULL, then bvr will be NULL as well which␊ |
1313 | ␉␉// should be caught by the caller.␊ |
1314 | ␉}␊ |
1315 | ␊ |
1316 | ␉// Returns the file path following the device spec.␊ |
1317 | ␉// e.g. 'hd(1,b)mach_kernel' is reduced to 'mach_kernel'.␊ |
1318 | ␊ |
1319 | ␉*outPath = cp;␊ |
1320 | ␊ |
1321 | ␉return bvr;␊ |
1322 | }␊ |
1323 | ␊ |
1324 | //==========================================================================␊ |
1325 | // Function name is a misnomer as scanBootVolumes usually calls diskScanBootVolumes␊ |
1326 | // which caches the information. So it's only allocated on the first run.␊ |
1327 | static BVRef newBootVolumeRef( int biosdev, int partno )␊ |
1328 | {␊ |
1329 | ␉BVRef bvr, bvr1, bvrChain;␊ |
1330 | ␊ |
1331 | ␉bvr = bvr1 = NULL;␊ |
1332 | ␊ |
1333 | ␉// Try resolving "rd" and "bt" devices first.␊ |
1334 | ␉if (biosdev == kPseudoBIOSDevRAMDisk)␊ |
1335 | ␉{␊ |
1336 | ␉␉if (gRAMDiskVolume)␊ |
1337 | ␉␉{␊ |
1338 | ␉␉␉bvr1 = gRAMDiskVolume;␊ |
1339 | ␉␉}␊ |
1340 | ␉}␊ |
1341 | ␉else if (biosdev == kPseudoBIOSDevBooter)␊ |
1342 | ␉{␊ |
1343 | ␉␉if (gRAMDiskVolume != NULL && gRAMDiskBTAliased)␊ |
1344 | ␉␉{␊ |
1345 | ␉␉␉bvr1 = gRAMDiskVolume;␊ |
1346 | ␉␉}␊ |
1347 | ␉␉else␊ |
1348 | ␉␉{␊ |
1349 | ␉␉␉bvr1 = gBIOSBootVolume;␊ |
1350 | ␉␉}␊ |
1351 | ␉}␊ |
1352 | ␉else␊ |
1353 | ␉{␊ |
1354 | ␉␉// Fetch the volume list from the device.␊ |
1355 | #if 0␊ |
1356 | ␉␉scanBootVolumes( biosdev, NULL );␊ |
1357 | #endif␊ |
1358 | ␉␉bvrChain = getBVChainForBIOSDev(biosdev);␊ |
1359 | ␊ |
1360 | ␉␉// Look for a perfect match based on device and partition number.␊ |
1361 | ␊ |
1362 | ␉␉for ( bvr1 = NULL, bvr = bvrChain; bvr; bvr = bvr->next )␊ |
1363 | ␉␉{␊ |
1364 | ␉␉␉if ( ( bvr->flags & kBVFlagNativeBoot ) == 0 )␊ |
1365 | ␉␉␉{␊ |
1366 | ␉␉␉␉continue;␊ |
1367 | ␉␉␉}␊ |
1368 | ␊ |
1369 | ␉␉␉bvr1 = bvr;␊ |
1370 | ␊ |
1371 | ␉␉␉if ( bvr->part_no == partno )␊ |
1372 | ␉␉␉{␊ |
1373 | ␉␉␉␉break;␊ |
1374 | ␉␉␉}␊ |
1375 | ␉␉}␊ |
1376 | ␉}␊ |
1377 | ␊ |
1378 | ␉return bvr ? bvr : bvr1;␊ |
1379 | }␊ |
1380 | ␊ |
1381 | //==========================================================================␊ |
1382 | // getDeviceDescription() - Extracts unit number and partition number␊ |
1383 | // from bvr structure into "dw(u,p)" format.␊ |
1384 | // Returns length of the out string␊ |
1385 | int getDeviceDescription(BVRef bvr, char *str)␊ |
1386 | {␊ |
1387 | ␉if(!str)␊ |
1388 | ␉{␊ |
1389 | ␉␉return 0;␊ |
1390 | ␉}␊ |
1391 | ␊ |
1392 | ␉*str = '\0';␊ |
1393 | ␊ |
1394 | ␉if (bvr)␊ |
1395 | ␉{␊ |
1396 | ␉␉const struct devsw *dp = devsw;␊ |
1397 | ␉␉while(dp->name && bvr->biosdev >= dp->biosdev)␊ |
1398 | ␉␉{␊ |
1399 | ␉␉␉dp++;␊ |
1400 | ␉}␊ |
1401 | ␉␉dp--;␊ |
1402 | ␉␉if (dp->name)␊ |
1403 | ␉␉{␊ |
1404 | ␉␉␉return sprintf(str, "%s(%d,%d)", dp->name, bvr->biosdev - dp->biosdev, bvr->part_no);␊ |
1405 | ␉␉}␊ |
1406 | ␉}␊ |
1407 | ␊ |
1408 | ␉return 0;␊ |
1409 | }␊ |
1410 | ␊ |
1411 | //==========================================================================␊ |
1412 |