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