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