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