]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/fs.subproj/fs.c
9a236fc79a00bc8299d16a4fd980d2227672aee7
[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 * 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.
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 * fs.c - Generic access to the file system modules.
24 *
25 * Copyright (c) 1999-2000 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
29
30 #include <sl.h>
31
32 typedef long (* FSLoadFile)(CICell ih, char *filePath);
33 typedef long (* FSGetDirEntry)(CICell ih, char *dirPath,
34 long *dirIndex, char **name,
35 long *flags, long *time);
36
37 #define kPartNet (0)
38 #define kPartHFS (1)
39 #define kPartUFS (2)
40 #define kPartExt2 (3)
41
42 struct PartInfo {
43 long partType;
44 CICell partIH;
45 FSLoadFile loadFile;
46 FSGetDirEntry getDirEntry;
47 char partName[1024];
48 };
49 typedef struct PartInfo PartInfo, *PartInfoPtr;
50
51 #define kNumPartInfos (16)
52 static PartInfo gParts[kNumPartInfos];
53
54 // Private function prototypes
55 long LookupPartition(char *devSpec);
56
57
58 // Public functions
59
60 long LoadFile(char *fileSpec)
61 {
62 char devSpec[256];
63 char *filePath;
64 FSLoadFile loadFile;
65 long ret, length, partIndex;
66
67 ret = ConvertFileSpec(fileSpec, devSpec, &filePath);
68 if ((ret == -1) || (filePath == NULL)) return -1;
69
70 // Get the partition index for devSpec.
71 partIndex = LookupPartition(devSpec);
72 if (partIndex == -1) return -1;
73
74 loadFile = gParts[partIndex].loadFile;
75 length = loadFile(gParts[partIndex].partIH, filePath);
76
77 // if (length == 0) return -1;
78
79 return length;
80 }
81
82 long CopyFile(char *fileSpec, char **addr, long *length)
83 {
84 long len;
85 char *file;
86
87 len = LoadFile(fileSpec);
88 if (len == -1) return -1;
89
90 file = malloc(len + 2);
91 if (file == NULL) return -1;
92
93 strncpy(file, (char *)kLoadAddr, len);
94
95 // Just to make sure.
96 file[len] = '\0';
97 file[len + 1] = '\0';
98
99 *addr = file;
100 *length = len;
101
102 return 0;
103 }
104
105 long GetFileInfo(char *dirSpec, char *name, long *flags, long *time)
106 {
107 long ret, index = 0;
108 char *curName;
109
110 while (1) {
111 ret = GetDirEntry(dirSpec, &index, &curName, flags, time);
112 if (ret == -1) break;
113
114 if (!strcmp(name, curName)) break;
115 }
116
117 return ret;
118 }
119
120 long GetDirEntry(char *dirSpec, long *dirIndex, char **name,
121 long *flags, long *time)
122 {
123 char devSpec[256];
124 char *dirPath;
125 FSGetDirEntry getDirEntry;
126 long ret, partIndex;
127
128 ret = ConvertFileSpec(dirSpec, devSpec, &dirPath);
129 if ((ret == -1) || (dirPath == NULL)) return -1;
130
131 // Get the partition index for devSpec.
132 partIndex = LookupPartition(devSpec);
133 if (partIndex == -1) return -1;
134
135 getDirEntry = gParts[partIndex].getDirEntry;
136 ret = getDirEntry(gParts[partIndex].partIH, dirPath,
137 dirIndex, name, flags, time);
138
139 return ret;
140 }
141
142 long DumpDir(char *dirSpec)
143 {
144 long ret, flags, time, index = 0;
145 char *name;
146
147 printf("DumpDir on [%s]\n", dirSpec);
148
149 while (1) {
150 ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
151 if (ret == -1) break;
152
153 printf("%x %x [%s]\n", flags, time, name);
154 }
155
156 return 0;
157 }
158
159
160 // Private functions
161
162 long LookupPartition(char *devSpec)
163 {
164 CICell partIH;
165 long partIndex, partType;
166 long deviceType;
167
168 // See if the devSpec has already been opened.
169 for (partIndex = 0; partIndex < kNumPartInfos; partIndex++) {
170 if (!strcmp(gParts[partIndex].partName, devSpec)) break;
171 }
172
173 // If it has not been opened, do so now.
174 if (partIndex == kNumPartInfos) {
175 // Find a free slot.
176 for (partIndex = 0; partIndex < kNumPartInfos; partIndex++) {
177 if (gParts[partIndex].partIH == 0) break;
178 }
179 // No free slots, so return error.
180 if (partIndex == kNumPartInfos) return -1;
181
182 deviceType = GetDeviceType(devSpec);
183 switch (deviceType) {
184 case kNetworkDeviceType :
185 partIH = NetInitPartition(devSpec);
186 if (partIH == 0) return -1;
187 partType = kPartNet;
188 break;
189
190 case kBlockDeviceType :
191 printf("Opening partition [%s]...\n", devSpec);
192 partIH = Open(devSpec);
193 if (partIH == 0) {
194 printf("Failed to open partition [%s].\n", devSpec);
195 return -1;
196 }
197
198 // Find out what kind of partition it is.
199 if (HFSInitPartition(partIH) != -1) partType = kPartHFS;
200 else if (UFSInitPartition(partIH) != -1) partType = kPartUFS;
201 else if (Ext2InitPartition(partIH) != -1) partType = kPartExt2;
202 else return -1;
203 break;
204
205 default :
206 return -1;
207 }
208
209 gParts[partIndex].partIH = partIH;
210 gParts[partIndex].partType = partType;
211 strcpy(gParts[partIndex].partName, devSpec);
212
213 switch (partType) {
214 case kPartNet:
215 gParts[partIndex].loadFile = NetLoadFile;
216 gParts[partIndex].getDirEntry = NetGetDirEntry;
217 break;
218
219 case kPartHFS:
220 gParts[partIndex].loadFile = HFSLoadFile;
221 gParts[partIndex].getDirEntry = HFSGetDirEntry;
222 break;
223
224 case kPartUFS:
225 gParts[partIndex].loadFile = UFSLoadFile;
226 gParts[partIndex].getDirEntry = UFSGetDirEntry;
227 break;
228
229 case kPartExt2:
230 gParts[partIndex].loadFile = Ext2LoadFile;
231 gParts[partIndex].getDirEntry = Ext2GetDirEntry;
232 break;
233 }
234 }
235
236 return partIndex;
237 }