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