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