]>
git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/fs.subproj/ufs.c
ae86f763b55fe0117b73b7032cc80c82d8658da6
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * ufs.c - File System Module for UFS.
25 * Copyright (c) 1998-2004 Apple Computer, Inc.
32 #include "ufs_byteorder.h"
34 typedef struct dinode Inode
, *InodePtr
;
36 // Private function prototypes
38 static char *ReadBlock(long fragNum
, long fragOffset
, long length
,
39 char *buffer
, long cache
);
40 static long ReadInode(long inodeNum
, InodePtr inode
, long *flags
, long *time
);
41 static long ResolvePathToInode(char *filePath
, long *flags
,
42 InodePtr fileInode
, InodePtr dirInode
);
43 static long ReadDirEntry(InodePtr dirInode
, long *fileInodeNum
,
44 long *dirIndex
, char **name
);
45 static long FindFileInDir(char *fileName
, long *flags
,
46 InodePtr fileInode
, InodePtr dirInode
);
47 static char *ReadFileBlock(InodePtr fileInode
, long fragNum
, long blockOffset
,
48 long length
, char *buffer
, long cache
);
49 static long ReadFile(InodePtr fileInode
, long *length
,
50 void *base
, long offset
);
53 static CICell gCurrentIH
;
54 static long long gPartitionBase
;
55 static char gFSBuf
[SBSIZE
];
56 static struct fs
*gFS
;
57 static struct ufslabel gUFSLabel
; // for UUID
58 static long gBlockSize
;
59 static long gBlockSizeOld
;
60 static long gFragSize
;
61 static long gFragsPerBlock
;
62 static char *gTempBlock
;
63 static char gTempName
[MAXNAMLEN
+ 1];
64 static char gTempName2
[MAXNAMLEN
+ 1];
65 static Inode gRootInode
;
66 static Inode gFileInode
;
70 long UFSInitPartition(CICell ih
)
74 if (ih
== gCurrentIH
) return 0;
76 printf("UFSInitPartition: %x\n", ih
);
80 // Assume UFS starts at the beginning of the device
83 // read the disk label to get the UUID
84 // (rumor has it that UFS headers can be either-endian on disk; hopefully
85 // that isn't true for this UUID field).
86 Seek(ih
, gPartitionBase
+ UFS_LABEL_OFFSET
);
87 ret
= Read(ih
, (long)&gUFSLabel
, UFS_LABEL_SIZE
);
88 if(ret
!= UFS_LABEL_SIZE
)
89 bzero(&gUFSLabel
, UFS_LABEL_SIZE
);
91 // Look for the Super Block
92 Seek(ih
, gPartitionBase
+ SBOFF
);
93 Read(ih
, (long)gFSBuf
, SBSIZE
);
95 gFS
= (struct fs
*)gFSBuf
;
96 //printf("looking for UFS magic ... \n");
97 if (gFS
->fs_magic
!= FS_MAGIC
) {
100 //printf("continuing w/UFS\n");
102 // Calculate the block size and set up the block cache.
103 gBlockSize
= gFS
->fs_bsize
;
104 gFragSize
= gFS
->fs_fsize
;
105 gFragsPerBlock
= gBlockSize
/ gFragSize
;
107 if (gBlockSizeOld
<= gBlockSize
) {
108 gTempBlock
= AllocateBootXMemory(gBlockSize
);
111 CacheInit(ih
, gBlockSize
);
113 gBlockSizeOld
= gBlockSize
;
117 // Read the Root Inode
118 ReadInode(ROOTINO
, &gRootInode
, 0, 0);
124 long UFSGetUUID(CICell ih
, char *uuidStr
)
126 long long uuid
= gUFSLabel
.ul_uuid
;
128 if (UFSInitPartition(ih
) == -1) return -1;
129 if (uuid
== 0LL) return -1;
131 return CreateUUIDString((uint8_t*)(&uuid
), sizeof(uuid
), uuidStr
);
134 long UFSLoadFile(CICell ih
, char *filePath
)
136 return UFSReadFile(ih
, filePath
, (void *)kLoadAddr
, 0, 0);
139 long UFSReadFile(CICell ih
, char *filePath
, void *base
,
140 unsigned long offset
, unsigned long length
)
144 if (UFSInitPartition(ih
) == -1) return -1;
146 printf("%s UFS file: [%s] from %x.\n",
147 (((offset
== 0) && (length
== 0)) ? "Loading" : "Reading"),
150 // Skip one or two leading '\'.
151 if (*filePath
== '\\') filePath
++;
152 if (*filePath
== '\\') filePath
++;
153 ret
= ResolvePathToInode(filePath
, &flags
, &gFileInode
, &gRootInode
);
154 if ((ret
== -1) || ((flags
& kFileTypeMask
) != kFileTypeFlat
)) return -1;
156 if (flags
& (kOwnerNotRoot
| kPermGroupWrite
| kPermOtherWrite
)) {
157 printf("%s: permissions incorrect\n", filePath
);
161 ret
= ReadFile(&gFileInode
, &length
, base
, offset
);
162 if (ret
== -1) return -1;
168 long UFSGetDirEntry(CICell ih
, char *dirPath
, long *dirIndex
,
169 char **name
, long *flags
, long *time
)
171 long ret
, fileInodeNum
, dirFlags
;
174 if (UFSInitPartition(ih
) == -1) return -1;
176 // Skip a leading '\' if present
177 if (*dirPath
== '\\') dirPath
++;
178 if (*dirPath
== '\\') dirPath
++;
179 ret
= ResolvePathToInode(dirPath
, &dirFlags
, &gFileInode
, &gRootInode
);
180 if ((ret
== -1) || ((dirFlags
& kFileTypeMask
) != kFileTypeDirectory
))
183 ret
= ReadDirEntry(&gFileInode
, &fileInodeNum
, dirIndex
, name
);
184 if (ret
!= 0) return ret
;
186 ReadInode(fileInodeNum
, &tmpInode
, flags
, time
);
193 static char *ReadBlock(long fragNum
, long blockOffset
, long length
,
194 char *buffer
, long cache
)
199 blockNum
= fragNum
/ gFragsPerBlock
;
200 fragNum
-= blockNum
* gFragsPerBlock
;
202 blockOffset
+= fragNum
* gFragSize
;
204 offset
= gPartitionBase
+ 1ULL * blockNum
* gBlockSize
;
206 if (cache
&& ((blockOffset
+ length
) <= gBlockSize
)) {
207 CacheRead(gCurrentIH
, gTempBlock
, offset
, gBlockSize
, 1);
208 if (buffer
!= 0) bcopy(gTempBlock
+ blockOffset
, buffer
, length
);
209 else buffer
= gTempBlock
+ blockOffset
;
211 offset
+= blockOffset
;
212 CacheRead(gCurrentIH
, buffer
, offset
, length
, 0);
219 static long ReadInode(long inodeNum
, InodePtr inode
, long *flags
, long *time
)
221 long fragNum
= ino_to_fsba(gFS
, inodeNum
);
222 long blockOffset
= ino_to_fsbo(gFS
, inodeNum
) * sizeof(Inode
);
224 ReadBlock(fragNum
, blockOffset
, sizeof(Inode
), (char *)inode
, 1);
226 if (time
!= 0) *time
= inode
->di_mtime
;
229 switch (inode
->di_mode
& IFMT
) {
230 case IFREG
: *flags
= kFileTypeFlat
; break;
231 case IFDIR
: *flags
= kFileTypeDirectory
; break;
232 case IFLNK
: *flags
= kFileTypeLink
; break;
233 default : *flags
= kFileTypeUnknown
; break;
236 *flags
|= inode
->di_mode
& kPermMask
;
238 if (inode
->di_uid
!= 0) {
239 static int nwarnings
= 0;
240 if(nwarnings
++ < 25) // so we don't warn for all in an Extensions walk
241 printf("non-root file owner detected: %d\n", inode
->di_uid
);
242 *flags
|= kOwnerNotRoot
;
250 static long ResolvePathToInode(char *filePath
, long *flags
,
251 InodePtr fileInode
, InodePtr dirInode
)
256 // if filePath is empty the we want this directory.
257 if (*filePath
== '\0') {
258 bcopy((char *)dirInode
, (char *)fileInode
, sizeof(Inode
));
262 // Copy the file name to gTempName
264 while ((filePath
[cnt
] != '\\') && (filePath
[cnt
] != '\0')) cnt
++;
265 strncpy(gTempName
, filePath
, cnt
);
267 // Move restPath to the right place.
268 if (filePath
[cnt
] != '\0') cnt
++;
269 restPath
= filePath
+ cnt
;
271 // gTempName is a name in the current Dir.
272 // restPath is the rest of the path if any.
274 ret
= FindFileInDir(gTempName
, flags
, fileInode
, dirInode
);
275 if (ret
== -1) return -1;
277 if ((*restPath
!= '\0') && ((*flags
& kFileTypeMask
) == kFileTypeDirectory
))
278 ret
= ResolvePathToInode(restPath
, flags
, fileInode
, fileInode
);
284 static long ReadDirEntry(InodePtr dirInode
, long *fileInodeNum
,
285 long *dirIndex
, char **name
)
290 long dirBlockNum
, dirBlockOffset
;
295 dirBlockOffset
= index
% DIRBLKSIZ
;
296 dirBlockNum
= index
/ DIRBLKSIZ
;
298 buffer
= ReadFileBlock(dirInode
, dirBlockNum
, 0, DIRBLKSIZ
, 0, 1);
299 if (buffer
== 0) return -1;
301 dir
= (struct direct
*)(buffer
+ dirBlockOffset
);
302 *dirIndex
+= dir
->d_reclen
;
304 if (dir
->d_ino
!= 0) break;
306 if (dirBlockOffset
!= 0) return -1;
309 *fileInodeNum
= dir
->d_ino
;
310 *name
= strncpy(gTempName2
, dir
->d_name
, dir
->d_namlen
);
316 static long FindFileInDir(char *fileName
, long *flags
,
317 InodePtr fileInode
, InodePtr dirInode
)
319 long ret
, inodeNum
, index
= 0;
323 ret
= ReadDirEntry(dirInode
, &inodeNum
, &index
, &name
);
324 if (ret
== -1) return -1;
326 if (strcmp(fileName
, name
) == 0) break;
329 ReadInode(inodeNum
, fileInode
, flags
, 0);
335 static char *ReadFileBlock(InodePtr fileInode
, long fragNum
, long blockOffset
,
336 long length
, char *buffer
, long cache
)
338 long fragCount
, blockNum
;
339 long diskFragNum
, indFragNum
, indBlockOff
, refsPerBlock
;
342 fragCount
= (fileInode
->di_size
+ gFragSize
- 1) / gFragSize
;
343 if (fragNum
>= fragCount
) return 0;
345 refsPerBlock
= gBlockSize
/ sizeof(ufs_daddr_t
);
347 blockNum
= fragNum
/ gFragsPerBlock
;
348 fragNum
-= blockNum
* gFragsPerBlock
;
350 // Get Direct Block Number.
351 if (blockNum
< NDADDR
) {
352 diskFragNum
= fileInode
->di_db
[blockNum
];
356 // Get Single Indirect Fragment Number.
357 if (blockNum
< refsPerBlock
) {
358 indFragNum
= fileInode
->di_ib
[0];
360 blockNum
-= refsPerBlock
;
362 // Get Double Indirect Fragment Number.
363 if (blockNum
< (refsPerBlock
* refsPerBlock
)) {
364 indFragNum
= fileInode
->di_ib
[1];
366 blockNum
-= refsPerBlock
* refsPerBlock
;
368 // Get Triple Indirect Fragment Number.
369 indFragNum
= fileInode
->di_ib
[2];
371 indBlock
= ReadBlock(indFragNum
, 0, gBlockSize
, 0, 1);
372 indBlockOff
= blockNum
/ (refsPerBlock
* refsPerBlock
);
373 blockNum
%= (refsPerBlock
* refsPerBlock
);
374 indFragNum
= ((ufs_daddr_t
*)indBlock
)[indBlockOff
];
377 indBlock
= ReadBlock(indFragNum
, 0, gBlockSize
, 0, 1);
378 indBlockOff
= blockNum
/ refsPerBlock
;
379 blockNum
%= refsPerBlock
;
380 indFragNum
= ((ufs_daddr_t
*)indBlock
)[indBlockOff
];
383 indBlock
= ReadBlock(indFragNum
, 0, gBlockSize
, 0, 1);
384 diskFragNum
= ((ufs_daddr_t
*)indBlock
)[blockNum
];
387 buffer
= ReadBlock(diskFragNum
+fragNum
, blockOffset
, length
, buffer
, cache
);
393 static long ReadFile(InodePtr fileInode
, long *length
, void *base
, long offset
)
395 long bytesLeft
, curSize
, curFrag
;
396 char *buffer
, *curAddr
= (char *)base
;
398 bytesLeft
= fileInode
->di_size
;
400 if (offset
> bytesLeft
) {
401 printf("Offset is too large.\n");
405 if ((*length
== 0) || ((offset
+ *length
) > bytesLeft
)) {
406 *length
= bytesLeft
- offset
;
409 if (*length
> kLoadSize
) {
410 printf("File is too large.\n");
415 curFrag
= (offset
/ gBlockSize
) * gFragsPerBlock
;
416 offset
%= gBlockSize
;
419 curSize
= gBlockSize
;
420 if (curSize
> bytesLeft
) curSize
= bytesLeft
;
421 if (offset
!= 0) curSize
-= offset
;
423 buffer
= ReadFileBlock(fileInode
, curFrag
, offset
, curSize
, curAddr
, 0);
424 if (buffer
== 0) break;
426 if (offset
!= 0) offset
= 0;
428 curFrag
+= gFragsPerBlock
;
430 bytesLeft
-= curSize
;