]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/fs.subproj/fs.c
BootX-59.1.1.tar.gz
[apple/bootx.git] / bootx.tproj / fs.subproj / fs.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * fs.c - Generic access to the file system modules.
27 *
28 * Copyright (c) 1999-2000 Apple Computer, Inc.
29 *
30 * DRI: Josh de Cesare
31 */
32
33 #include <sl.h>
34
35 typedef long (* FSLoadFile)(CICell ih, char *filePath);
36 typedef long (* FSGetDirEntry)(CICell ih, char *dirPath,
37 long *dirIndex, char **name,
38 long *flags, long *time);
39
40 #define kPartNet (0)
41 #define kPartHFS (1)
42 #define kPartUFS (2)
43 #define kPartExt2 (3)
44
45 struct PartInfo {
46 long partType;
47 CICell partIH;
48 FSLoadFile loadFile;
49 FSGetDirEntry getDirEntry;
50 char partName[1024];
51 };
52 typedef struct PartInfo PartInfo, *PartInfoPtr;
53
54 #define kNumPartInfos (16)
55 static PartInfo gParts[kNumPartInfos];
56 static char gMakeDirSpec[1024];
57
58 // Private function prototypes
59 long LookupPartition(char *devSpec);
60
61
62 // Public functions
63
64 long LoadFile(char *fileSpec)
65 {
66 char devSpec[256];
67 char *filePath;
68 FSLoadFile loadFile;
69 long ret, length, partIndex;
70
71 ret = ConvertFileSpec(fileSpec, devSpec, &filePath);
72 if ((ret == -1) || (filePath == NULL)) return -1;
73
74 // Get the partition index for devSpec.
75 partIndex = LookupPartition(devSpec);
76 if (partIndex == -1) return -1;
77
78 loadFile = gParts[partIndex].loadFile;
79 length = loadFile(gParts[partIndex].partIH, filePath);
80
81 // if (length == 0) return -1;
82
83 return length;
84 }
85
86 long GetFileInfo(char *dirSpec, char *name, long *flags, long *time)
87 {
88 long ret, index = 0;
89 char *curName;
90
91 if (!dirSpec) {
92 long idx, len;
93
94 len = strlen(name);
95
96 for (idx = len; idx && (name[idx] != '\\'); idx--) {}
97 idx++;
98 strncpy(gMakeDirSpec, name, idx);
99 name += idx;
100 dirSpec = gMakeDirSpec;
101 }
102
103 while (1) {
104 ret = GetDirEntry(dirSpec, &index, &curName, flags, time);
105 if (ret == -1) break;
106
107 if (!strcmp(name, curName)) break;
108 }
109
110 return ret;
111 }
112
113 long GetDirEntry(char *dirSpec, long *dirIndex, char **name,
114 long *flags, long *time)
115 {
116 char devSpec[256];
117 char *dirPath;
118 FSGetDirEntry getDirEntry;
119 long ret, partIndex;
120
121 ret = ConvertFileSpec(dirSpec, devSpec, &dirPath);
122 if ((ret == -1) || (dirPath == NULL)) return -1;
123
124 // Get the partition index for devSpec.
125 partIndex = LookupPartition(devSpec);
126 if (partIndex == -1) return -1;
127
128 getDirEntry = gParts[partIndex].getDirEntry;
129 ret = getDirEntry(gParts[partIndex].partIH, dirPath,
130 dirIndex, name, flags, time);
131
132 return ret;
133 }
134
135 long DumpDir(char *dirSpec)
136 {
137 long ret, flags, time, index = 0;
138 char *name;
139
140 printf("DumpDir on [%s]\n", dirSpec);
141
142 while (1) {
143 ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
144 if (ret == -1) break;
145
146 printf("%x %x [%s]\n", flags, time, name);
147 }
148
149 return 0;
150 }
151
152
153 // Private functions
154
155 long LookupPartition(char *devSpec)
156 {
157 CICell partIH;
158 long partIndex, partType;
159 long deviceType;
160
161 // See if the devSpec has already been opened.
162 for (partIndex = 0; partIndex < kNumPartInfos; partIndex++) {
163 if (!strcmp(gParts[partIndex].partName, devSpec)) break;
164 }
165
166 // If it has not been opened, do so now.
167 if (partIndex == kNumPartInfos) {
168 // Find a free slot.
169 for (partIndex = 0; partIndex < kNumPartInfos; partIndex++) {
170 if (gParts[partIndex].partIH == 0) break;
171 }
172 // No free slots, so return error.
173 if (partIndex == kNumPartInfos) return -1;
174
175 deviceType = GetDeviceType(devSpec);
176 switch (deviceType) {
177 case kNetworkDeviceType :
178 partIH = NetInitPartition(devSpec);
179 if (partIH == 0) return -1;
180 partType = kPartNet;
181 break;
182
183 case kBlockDeviceType :
184 printf("Opening partition [%s]...\n", devSpec);
185 partIH = Open(devSpec);
186 if (partIH == 0) {
187 printf("Failed to open partition [%s].\n", devSpec);
188 return -1;
189 }
190
191 // Find out what kind of partition it is.
192 if (HFSInitPartition(partIH) != -1) partType = kPartHFS;
193 else if (UFSInitPartition(partIH) != -1) partType = kPartUFS;
194 else if (Ext2InitPartition(partIH) != -1) partType = kPartExt2;
195 else return -1;
196 break;
197
198 default :
199 return -1;
200 }
201
202 gParts[partIndex].partIH = partIH;
203 gParts[partIndex].partType = partType;
204 strcpy(gParts[partIndex].partName, devSpec);
205
206 switch (partType) {
207 case kPartNet:
208 gParts[partIndex].loadFile = NetLoadFile;
209 gParts[partIndex].getDirEntry = NetGetDirEntry;
210 break;
211
212 case kPartHFS:
213 gParts[partIndex].loadFile = HFSLoadFile;
214 gParts[partIndex].getDirEntry = HFSGetDirEntry;
215 break;
216
217 case kPartUFS:
218 gParts[partIndex].loadFile = UFSLoadFile;
219 gParts[partIndex].getDirEntry = UFSGetDirEntry;
220 break;
221
222 case kPartExt2:
223 gParts[partIndex].loadFile = Ext2LoadFile;
224 gParts[partIndex].getDirEntry = Ext2GetDirEntry;
225 break;
226 }
227 }
228
229 return partIndex;
230 }