Modules/InitRD - Added inode number lookup support
[tpg/acess2.git] / Modules / Filesystems / InitRD / GenerateInitRD.php
1 <?php
2 $lGenDate = date("Y-m-d H:i");
3 $gOutput = <<<EOF
4 /*
5  * Acess2 InitRD
6  * InitRD Data
7  * Generated $lGenDate
8  */
9 #include "initrd.h"
10
11 EOF;
12
13 $ACESSDIR = getenv("ACESSDIR");
14 $ARCH = getenv("ARCH");
15
16 $gInputFile = $argv[1];
17 $gOutputFile = $argv[2];
18 $gDepFile = ($argc > 3 ? $argv[3] : false);
19
20 $gDependencies = array();
21
22 $lines = file($argv[1]);
23
24 $lDepth = 0;
25 $lTree = array();
26 $lStack = array( array("",array()) );
27 foreach($lines as $line)
28 {
29         $line = trim($line);
30         // Directory
31         if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
32         {
33                 $new = array($matches[1], array());
34                 array_push($lStack, $new);
35                 $lDepth ++;
36                 continue;
37         }
38         // End of a block
39         if($line == "}")
40         {
41                 $lDepth --;
42                 $lStack[$lDepth][1][] = array_pop($lStack);
43                 continue;
44         }
45         // File
46         if(preg_match('/^File\s+"([^"]+)"\s+"([^"]+)"$/', $line, $matches))
47         {
48                 $lStack[$lDepth][1][] = array($matches[1], $matches[2]);
49                 continue;
50         }
51         echo "ERROR: $line\n";
52         exit(0);
53 }
54
55 function hd($fp)
56 {
57         //return "0x".str_pad( dechex(ord(fgetc($fp))), 8, "0", STR_PAD_LEFT );
58         $val = unpack("I", fread($fp, 4));
59         //print_r($val);        exit -1;
60         return "0x".dechex($val[1]);
61 }
62
63 function hd8($fp)
64 {
65         return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
66 }
67
68 $inode = 0;
69 function ProcessFolder($prefix, $items)
70 {
71         global  $gOutput, $gDependencies;
72         global  $ACESSDIR, $ARCH;
73         global  $inode;
74         foreach($items as $i=>$item)
75         {
76                 $inode ++;
77                 if(is_array($item[1]))
78                 {
79                         ProcessFolder("{$prefix}_{$i}", $item[1]);
80                         
81                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
82                         foreach($item[1] as $j=>$child)
83                         {
84                                 if($j)  $gOutput .= ",\n";
85                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
86                         }
87                         $gOutput .= "\n};\n";
88                         
89                         $size = count($item[1]);
90                         $gOutput .= <<<EOF
91 tVFS_Node {$prefix}_{$i} = {
92         .NumACLs = 1,
93         .ACLs = &gVFS_ACL_EveryoneRX,
94         .Flags = VFS_FFLAG_DIRECTORY,
95         .Size = $size,
96         .Inode = {$inode},
97         .ImplPtr = {$prefix}_{$i}_entries,
98         .ReadDir = InitRD_ReadDir,
99         .FindDir = InitRD_FindDir
100 };
101
102 EOF;
103                 }
104                 else
105                 {
106                         $path = $item[1];
107                         
108                         // Parse path components
109                         $path = str_replace("__BIN__", "$ACESSDIR/Usermode/Output/$ARCH", $path);
110                         $path = str_replace("__FS__", "$ACESSDIR/Usermode/Filesystem", $path);
111                         echo $path,"\n";
112                         // ---
113                         
114                         $gDependencies[] = $path;
115
116                         if(!file_exists($path)) {
117                                 echo "ERROR: '{$path}' does not exist\n", 
118                                 exit(1);
119                         }
120                         $size = filesize($path);
121                         
122                         $fp = fopen($path, "rb");
123                         
124                         $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
125                         for( $j = 0; $j + 16 < $size; $j += 16 ) {
126                                 $gOutput .= "\t";
127                                 $gOutput .= hd8($fp).",".hd8($fp).",";
128                                 $gOutput .= hd8($fp).",".hd8($fp).",";
129                                 $gOutput .= hd8($fp).",".hd8($fp).",";
130                                 $gOutput .= hd8($fp).",".hd8($fp).",";
131                                 $gOutput .= hd8($fp).",".hd8($fp).",";
132                                 $gOutput .= hd8($fp).",".hd8($fp).",";
133                                 $gOutput .= hd8($fp).",".hd8($fp).",";
134                                 $gOutput .= hd8($fp).",".hd8($fp).",\n";
135                         }
136                         $gOutput .= "\t";
137                         for( ; $j < $size; $j ++ ) {
138                                 if( $j & 15 )   $gOutput .= ",";
139                                 $gOutput .= hd8($fp);
140                         }
141                         fclose($fp);
142                         $gOutput .= "\n};\n";
143                         $gOutput .= <<<EOF
144 tVFS_Node {$prefix}_{$i} = {
145         .NumACLs = 1,
146         .ACLs = &gVFS_ACL_EveryoneRX,
147         .Flags = 0,
148         .Size = $size,
149         .Inode = {$inode},
150         .ImplPtr = {$prefix}_{$i}_data,
151         .Read = InitRD_ReadFile
152 };
153
154 EOF;
155                 }
156         }
157 }
158
159 //print_r($lStack);
160 //exit(1);
161
162 ProcessFolder("gInitRD_Files", $lStack[0][1]);
163
164 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
165 foreach($lStack[0][1] as $j=>$child)
166 {
167         if($j)  $gOutput .= ",\n";
168         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
169 }
170 $gOutput .= "\n};\n";
171 $nRootFiles = count($lStack[0][1]);
172 $gOutput .= <<<EOF
173 tVFS_Node gInitRD_RootNode = {
174         .NumACLs = 1,
175         .ACLs = &gVFS_ACL_EveryoneRX,
176         .Flags = VFS_FFLAG_DIRECTORY,
177         .Size = $nRootFiles,
178         .ImplPtr = gInitRD_Root_Files,
179         .ReadDir = InitRD_ReadDir,
180         .FindDir = InitRD_FindDir
181 };
182 EOF;
183
184 $gOutput .= <<<EOF
185
186 tVFS_Node * const gInitRD_FileList[] = {
187 &gInitRD_RootNode
188 EOF;
189
190 function PutNodePointers($prefix, $items)
191 {
192         global $gOutput;
193         foreach($items as $i=>$item)
194         {
195                 $gOutput .= ",&{$prefix}_{$i}";
196                 if(is_array($item[1]))
197                 {
198                         PutNodePointers("{$prefix}_{$i}", $item[1]);
199                 }
200         }
201 }
202
203 PutNodePointers("gInitRD_Files", $lStack[0][1]);
204
205 $gOutput .= <<<EOF
206 };
207 const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
208
209 EOF;
210
211
212 $fp = fopen($gOutputFile, "w");
213 fputs($fp, $gOutput);
214 fclose($fp);
215
216
217 if($gDepFile !== false)
218 {
219         $fp = fopen($gDepFile, "w");
220         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
221         fputs($fp, $line);
222         fclose($fp);
223 }
224
225 ?>

UCC git Repository :: git.ucc.asn.au