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