Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/ufs.c

1/*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 2.0 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * ufs.c - File System Module for UFS.
24 *
25 * Copyright (c) 1998-2002 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
29#include "sl.h"
30
31#include "ufs.h"
32#include "ufs_byteorder.h"
33
34#if !defined(MAXNAMLEN) && defined(UFSMAXNAMLEN)
35#define MAXNAMLEN UFSMAXNAMLEN
36#endif
37
38typedef struct dinode Inode, *InodePtr;
39
40// Private function prototypes
41
42static char *ReadBlock(long fragNum, long fragOffset, long length,
43 char *buffer, long cache);
44static long ReadInode(long inodeNum, InodePtr inode, long *flags, long *time);
45static long ResolvePathToInode(char *filePath, long *flags,
46 InodePtr fileInode, InodePtr dirInode);
47static long ReadDirEntry(InodePtr dirInode, long *fileInodeNum,
48 long long *dirIndex, char **name);
49static long FindFileInDir(char *fileName, long *flags,
50 InodePtr fileInode, InodePtr dirInode);
51static char *ReadFileBlock(InodePtr fileInode, long fragNum, long blockOffset,
52 long length, char *buffer, long cache);
53static long ReadFile(InodePtr fileInode, uint64_t *length, void *base, uint64_t offset);
54
55#define kDevBlockSize (0x200) // Size of each disk block.
56#define kDiskLabelBlock (15) // Block the DL is in.
57
58#ifdef __i386__
59
60static CICell gCurrentIH;
61static long long gPartitionBase;
62static char *gULBuf;
63static char *gFSBuf;
64static struct fs *gFS;
65#if !BOOT1
66static struct ufslabel gUFSLabel; // for UUID
67#endif
68static long gBlockSize;
69static long gFragSize;
70static long gFragsPerBlock;
71static char *gTempBlock;
72static char *gTempName;
73static char *gTempName2;
74static InodePtr gRootInodePtr;
75static InodePtr gFileInodePtr;
76
77#else /* !__i386__ */
78
79static CICell gCurrentIH;
80static long long gPartitionBase;
81static char gDLBuf[8192];
82static char gFSBuf[SBSIZE];
83static struct fs *gFS;
84#if !BOOT1
85static struct ufslabel gUFSLabel; // for UUID
86#endif
87static long gBlockSize;
88static long gFragSize;
89static long gFragsPerBlock;
90static char *gTempBlock;
91static char gTempName[MAXNAMLEN + 1];
92static char gTempName2[MAXNAMLEN + 1];
93static Inode _gRootInode;
94static Inode _gFileInode;
95static InodePtr gRootInodePtr = &_gRootInode;
96static InodePtr gFileInodePtr = &_gFileInode;
97
98#endif /* !__i386__ */
99
100// Public functions
101
102void UFSFree(CICell ih)
103{
104 if(gCurrentIH == ih)
105 gCurrentIH = 0;
106 free(ih);
107}
108
109long UFSInitPartition( CICell ih )
110{
111#if !BOOT1
112 long ret;
113#endif
114
115 if (ih == gCurrentIH) {
116#ifdef __i386__
117 CacheInit(ih, gBlockSize);
118#endif
119 return 0;
120 }
121
122#if !BOOT1
123 verbose("UFSInitPartition: %x\n", ih);
124#endif
125
126 gCurrentIH = 0;
127
128#ifdef __i386__
129 if (!gULBuf) gULBuf = (char *) malloc(UFS_LABEL_SIZE);
130 if (!gFSBuf) gFSBuf = (char *) malloc(SBSIZE);
131 if (!gTempName) gTempName = (char *) malloc(MAXNAMLEN + 1);
132 if (!gTempName2) gTempName2 = (char *) malloc(MAXNAMLEN + 1);
133 if (!gRootInodePtr) gRootInodePtr = (InodePtr) malloc(sizeof(Inode));
134 if (!gFileInodePtr) gFileInodePtr = (InodePtr) malloc(sizeof(Inode));
135 if (!gULBuf || !gFSBuf || !gTempName || !gTempName2 ||
136 !gRootInodePtr || !gFileInodePtr) return -1;
137#endif
138
139 // Assume there is no Disk Label
140 gPartitionBase = 0;
141
142#if !BOOT1
143 // read the disk label to get the UUID
144 // (rumor has it that UFS headers can be either-endian on disk; hopefully
145 // that isn't true for this UUID field).
146 Seek(ih, gPartitionBase + UFS_LABEL_OFFSET);
147 ret = Read(ih, (long)&gUFSLabel, UFS_LABEL_SIZE);
148 if(ret != 0)
149 bzero(&gUFSLabel, UFS_LABEL_SIZE);
150#endif /* !BOOT1 */
151
152 // Look for the Super Block
153 Seek(ih, gPartitionBase + SBOFF);
154 Read(ih, (long)gFSBuf, SBSIZE);
155
156 gFS = (struct fs *)gFSBuf;
157 byte_swap_superblock(gFS);
158
159 if (gFS->fs_magic != FS_MAGIC) {
160 return -1;
161 }
162
163ih->modTime = gFS->fs_time;
164
165 // Calculate the block size and set up the block cache.
166 gBlockSize = gFS->fs_bsize;
167 gFragSize = gFS->fs_fsize;
168 gFragsPerBlock = gBlockSize / gFragSize;
169 if (gTempBlock != 0) free(gTempBlock);
170 gTempBlock = malloc(gBlockSize);
171 CacheInit(ih, gBlockSize);
172
173 gCurrentIH = ih;
174
175 // Read the Root Inode
176 ReadInode(ROOTINO, gRootInodePtr, 0, 0);
177
178 return 0;
179}
180
181#if !BOOT1
182
183long UFSGetUUID(CICell ih, char *uuidStr, long strMaxLen)
184{
185 (void)strMaxLen;
186
187 long long uuid = gUFSLabel.ul_uuid;
188
189 if (UFSInitPartition(ih) == -1) return -1;
190 if (uuid == 0LL) return -1;
191
192 return CreateUUIDString((uint8_t*)(&uuid), sizeof(uuid), uuidStr, strMaxLen);
193}
194
195#endif /* !BOOT1 */
196
197long UFSLoadFile( CICell ih, char * filePath )
198{
199return UFSReadFile(ih, filePath, (void *)(uint32_t)get_env(envgFSLoadAddress), 0, 0);
200
201}
202
203long UFSReadFile( CICell ih, char * filePath, void * base, uint64_t offset, uint64_t length )
204{
205 long ret, flags;
206
207#if !BOOT1
208 verbose("Loading UFS file: [%s] from %x.\n", filePath, (unsigned)ih);
209#endif
210
211 if (UFSInitPartition(ih) == -1) return -1;
212
213 // Skip one or two leading '/'.
214 if (*filePath == '/') filePath++;
215 if (*filePath == '/') filePath++;
216
217 ret = ResolvePathToInode(filePath, &flags, gFileInodePtr, gRootInodePtr);
218 if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) return -1;
219
220 ret = ReadFile(gFileInodePtr, &length, base, offset);
221 if (ret == -1) return -1;
222
223 return length;
224}
225
226#ifndef BOOT1
227
228long UFSGetDirEntry( CICell ih, char * dirPath, long long * dirIndex,
229 char ** name, long * flags, long * time,
230 FinderInfo * finderInfo, long * infoValid)
231{
232 long ret, fileInodeNum, dirFlags;
233 Inode tmpInode;
234
235 if (UFSInitPartition(ih) == -1) return -1;
236
237 if (infoValid) *infoValid = 0;
238
239 // Skip a leading '/' if present
240 if (*dirPath == '/') dirPath++;
241 if (*dirPath == '/') dirPath++;
242
243 ret = ResolvePathToInode(dirPath, &dirFlags, gFileInodePtr, gRootInodePtr);
244 if ((ret == -1) || ((dirFlags & kFileTypeMask) != kFileTypeDirectory))
245 return -1;
246
247 ret = ReadDirEntry(gFileInodePtr, &fileInodeNum, dirIndex, name);
248 if (ret != 0) return ret;
249
250 ReadInode(fileInodeNum, &tmpInode, flags, time);
251
252 return 0;
253}
254
255void
256UFSGetDescription(CICell ih, char *str, long strMaxLen)
257{
258 if (UFSInitPartition(ih) == -1) { return; }
259
260 struct ufslabel *ul;
261
262 // Look for the Disk Label
263 Seek(ih, 1ULL * UFS_LABEL_OFFSET);
264 Read(ih, (long)gULBuf, UFS_LABEL_SIZE);
265
266 ul = (struct ufslabel *)gULBuf;
267
268 unsigned char magic_bytes[] = UFS_LABEL_MAGIC;
269 int i;
270 unsigned char *p = (unsigned char *)&ul->ul_magic;
271
272 for (i=0; i<sizeof(magic_bytes); i++, p++) {
273 if (*p != magic_bytes[i])
274 return;
275 }
276 strncpy(str, (const char *)ul->ul_name, strMaxLen);
277}
278
279long
280UFSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)
281{
282 long ret, flags;
283
284 if (UFSInitPartition(ih) == -1) return -1;
285
286 // Skip one or two leading '/'.
287 if (*filePath == '/') filePath++;
288 if (*filePath == '/') filePath++;
289
290 ret = ResolvePathToInode(filePath, &flags, gFileInodePtr, gRootInodePtr);
291 if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) return -1;
292
293 *firstBlock = (gPartitionBase + 1ULL * gFileInodePtr->di_db[0] * gBlockSize) / 512ULL;
294
295 return 0;
296}
297
298
299#endif /* !BOOT1 */
300
301// Private functions
302
303static char * ReadBlock( long fragNum, long blockOffset, long length,
304 char * buffer, long cache )
305{
306 long long offset;
307 long blockNum;
308
309 blockNum = fragNum / gFragsPerBlock;
310 fragNum -= blockNum * gFragsPerBlock;
311
312 blockOffset += fragNum * gFragSize;
313
314 offset = gPartitionBase + 1ULL * blockNum * gBlockSize;
315
316 if (cache && ((blockOffset + length) <= gBlockSize)) {
317 CacheRead(gCurrentIH, gTempBlock, offset, gBlockSize, 1);
318 if (buffer != 0) bcopy(gTempBlock + blockOffset, buffer, length);
319 else buffer = gTempBlock + blockOffset;
320 } else {
321 offset += blockOffset;
322 CacheRead(gCurrentIH, buffer, offset, length, 0);
323 }
324
325 return buffer;
326}
327
328static long ReadInode( long inodeNum, InodePtr inode, long * flags, long * time )
329{
330 long fragNum = ino_to_fsba(gFS, inodeNum);
331 long blockOffset = ino_to_fsbo(gFS, inodeNum) * sizeof(Inode);
332
333 ReadBlock(fragNum, blockOffset, sizeof(Inode), (char *)inode, 1);
334 byte_swap_dinode_in(inode);
335
336 if (time != 0) *time = inode->di_mtime;
337
338 if (flags != 0) {
339 switch (inode->di_mode & IFMT) {
340 case IFREG: *flags = kFileTypeFlat; break;
341 case IFDIR: *flags = kFileTypeDirectory; break;
342 case IFLNK: *flags = kFileTypeLink; break;
343 default : *flags = kFileTypeUnknown; break;
344 }
345
346 *flags |= inode->di_mode & kPermMask;
347
348 if (inode->di_uid != 0) *flags |= kOwnerNotRoot;
349 }
350
351 return 0;
352}
353
354static long ResolvePathToInode( char * filePath, long * flags,
355 InodePtr fileInode, InodePtr dirInode )
356{
357 char * restPath;
358 long ret, cnt;
359
360 // if filePath is empty the we want this directory.
361 if (*filePath == '\0') {
362 bcopy((char *)dirInode, (char *)fileInode, sizeof(Inode));
363 return 0;
364 }
365
366 // Copy the file name to gTempName
367 cnt = 0;
368 while ((filePath[cnt] != '/') && (filePath[cnt] != '\0')) cnt++;
369 strlcpy(gTempName, filePath, cnt+1);
370
371 // Move restPath to the right place.
372 if (filePath[cnt] != '\0') cnt++;
373 restPath = filePath + cnt;
374
375 // gTempName is a name in the current Dir.
376 // restPath is the rest of the path if any.
377
378 ret = FindFileInDir(gTempName, flags, fileInode, dirInode);
379 if (ret == -1) return -1;
380
381 if ((*restPath != '\0') && ((*flags & kFileTypeMask) == kFileTypeDirectory))
382 ret = ResolvePathToInode(restPath, flags, fileInode, fileInode);
383
384 return ret;
385}
386
387static long ReadDirEntry( InodePtr dirInode, long * fileInodeNum,
388 long long * dirIndex, char ** name )
389{
390 struct direct *dir;
391 char *buffer;
392 long long index;
393 long dirBlockNum, dirBlockOffset;
394
395 while (1) {
396 index = *dirIndex;
397
398 dirBlockOffset = (long) (index % DIRBLKSIZ);
399 dirBlockNum = (long) (index / DIRBLKSIZ);
400
401 buffer = ReadFileBlock(dirInode, dirBlockNum, 0, DIRBLKSIZ, 0, 1);
402 if (buffer == 0) return -1;
403
404 dir = (struct direct *)(buffer + dirBlockOffset);
405 byte_swap_dir_block_in((char *)dir, 1);
406
407 *dirIndex += dir->d_reclen;
408
409 if (dir->d_ino != 0) break;
410
411 if (dirBlockOffset != 0) return -1;
412 }
413
414 *fileInodeNum = dir->d_ino;
415 *name = strlcpy(gTempName2, dir->d_name, dir->d_namlen+1);
416
417 return 0;
418}
419
420static long FindFileInDir( char * fileName, long * flags,
421 InodePtr fileInode, InodePtr dirInode )
422{
423 long ret, inodeNum;
424 long long index = 0;
425 char *name;
426
427 while (1) {
428 ret = ReadDirEntry(dirInode, &inodeNum, &index, &name);
429 if (ret == -1) return -1;
430
431 if (strcmp(fileName, name) == 0) break;
432 }
433
434 ReadInode(inodeNum, fileInode, flags, 0);
435
436 return 0;
437}
438
439static char * ReadFileBlock( InodePtr fileInode, long fragNum, long blockOffset,
440 long length, char * buffer, long cache )
441{
442 long fragCount, blockNum;
443 long diskFragNum, indFragNum, indBlockOff, refsPerBlock;
444 char *indBlock;
445
446 fragCount = (fileInode->di_size + gFragSize - 1) / gFragSize;
447 if (fragNum >= fragCount) return 0;
448
449 refsPerBlock = gBlockSize / sizeof(ufs_daddr_t);
450
451 blockNum = fragNum / gFragsPerBlock;
452 fragNum -= blockNum * gFragsPerBlock;
453
454 // Get Direct Block Number.
455 if (blockNum < NDADDR) {
456 diskFragNum = fileInode->di_db[blockNum];
457 } else {
458 blockNum -= NDADDR;
459
460 // Get Single Indirect Fragment Number.
461 if (blockNum < refsPerBlock) {
462 indFragNum = fileInode->di_ib[0];
463 } else {
464 blockNum -= refsPerBlock;
465
466 // Get Double Indirect Fragment Number.
467 if (blockNum < (refsPerBlock * refsPerBlock)) {
468 indFragNum = fileInode->di_ib[1];
469 } else {
470 blockNum -= refsPerBlock * refsPerBlock;
471
472 // Get Triple Indirect Fragment Number.
473 indFragNum = fileInode->di_ib[2];
474
475 indBlock = ReadBlock(indFragNum, 0, gBlockSize, 0, 1);
476 indBlockOff = blockNum / (refsPerBlock * refsPerBlock);
477 blockNum %= (refsPerBlock * refsPerBlock);
478 indFragNum = SWAP_BE32(((ufs_daddr_t *)indBlock)[indBlockOff]);
479 }
480
481 indBlock = ReadBlock(indFragNum, 0, gBlockSize, 0, 1);
482 indBlockOff = blockNum / refsPerBlock;
483 blockNum %= refsPerBlock;
484 indFragNum = SWAP_BE32(((ufs_daddr_t *)indBlock)[indBlockOff]);
485 }
486
487 indBlock = ReadBlock(indFragNum, 0, gBlockSize, 0, 1);
488 diskFragNum = SWAP_BE32(((ufs_daddr_t *)indBlock)[blockNum]);
489 }
490
491 buffer = ReadBlock(diskFragNum+fragNum, blockOffset, length, buffer, cache);
492
493 return buffer;
494}
495
496static long ReadFile( InodePtr fileInode, uint64_t * length, void * base, uint64_t offset )
497{
498 long bytesLeft, curSize, curFrag;
499 char *buffer, *curAddr = (char *)base;
500
501 bytesLeft = fileInode->di_size;
502
503 if (offset > bytesLeft) {
504 printf("Offset is too large.\n");
505 return -1;
506 }
507
508 if ((*length == 0) || ((offset + *length) > bytesLeft)) {
509 *length = bytesLeft - offset;
510 }
511/*
512 if (bytesLeft > kLoadSize) {
513 printf("File is too large.\n");
514 return -1;
515 }
516*/
517 bytesLeft = *length;
518 curFrag = (offset / gBlockSize) * gFragsPerBlock;
519 offset %= gBlockSize;
520
521 while (bytesLeft) {
522 curSize = gBlockSize;
523 if (curSize > bytesLeft) curSize = bytesLeft;
524 if ((offset + curSize) > gBlockSize) curSize = (gBlockSize - offset);
525
526 buffer = ReadFileBlock(fileInode, curFrag, offset, curSize, curAddr, 0);
527 if (buffer == 0) break;
528
529 if (offset != 0) offset = 0;
530
531 curFrag += gFragsPerBlock;
532 curAddr += curSize;
533 bytesLeft -= curSize;
534 }
535
536 return bytesLeft;
537}

Archive Download this file

Revision: 2057