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