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