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