Modules/InitRD - Added AxWin3 to initrd
[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                         $path = str_replace("__SRC__", "$ACESSDIR", $path);
112                         echo $path,"\n";
113                         // ---
114                         
115                         $gDependencies[] = $path;
116
117                         if(!file_exists($path)) {
118                                 echo "ERROR: '{$path}' does not exist\n", 
119                                 exit(1);
120                         }
121                         $size = filesize($path);
122                         
123                         $fp = fopen($path, "rb");
124                         
125                         $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
126                         for( $j = 0; $j + 16 < $size; $j += 16 ) {
127                                 $gOutput .= "\t";
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).",";
135                                 $gOutput .= hd8($fp).",".hd8($fp).",\n";
136                         }
137                         $gOutput .= "\t";
138                         for( ; $j < $size; $j ++ ) {
139                                 if( $j & 15 )   $gOutput .= ",";
140                                 $gOutput .= hd8($fp);
141                         }
142                         fclose($fp);
143                         $gOutput .= "\n};\n";
144                         $gOutput .= <<<EOF
145 tVFS_Node {$prefix}_{$i} = {
146         .NumACLs = 1,
147         .ACLs = &gVFS_ACL_EveryoneRX,
148         .Flags = 0,
149         .Size = $size,
150         .Inode = {$inode},
151         .ImplPtr = {$prefix}_{$i}_data,
152         .Read = InitRD_ReadFile
153 };
154
155 EOF;
156                 }
157         }
158 }
159
160 //print_r($lStack);
161 //exit(1);
162
163 ProcessFolder("gInitRD_Files", $lStack[0][1]);
164
165 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
166 foreach($lStack[0][1] as $j=>$child)
167 {
168         if($j)  $gOutput .= ",\n";
169         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
170 }
171 $gOutput .= "\n};\n";
172 $nRootFiles = count($lStack[0][1]);
173 $gOutput .= <<<EOF
174 tVFS_Node gInitRD_RootNode = {
175         .NumACLs = 1,
176         .ACLs = &gVFS_ACL_EveryoneRX,
177         .Flags = VFS_FFLAG_DIRECTORY,
178         .Size = $nRootFiles,
179         .ImplPtr = gInitRD_Root_Files,
180         .ReadDir = InitRD_ReadDir,
181         .FindDir = InitRD_FindDir
182 };
183 EOF;
184
185 $gOutput .= <<<EOF
186
187 tVFS_Node * const gInitRD_FileList[] = {
188 &gInitRD_RootNode
189 EOF;
190
191 function PutNodePointers($prefix, $items)
192 {
193         global $gOutput;
194         foreach($items as $i=>$item)
195         {
196                 $gOutput .= ",&{$prefix}_{$i}";
197                 if(is_array($item[1]))
198                 {
199                         PutNodePointers("{$prefix}_{$i}", $item[1]);
200                 }
201         }
202 }
203
204 PutNodePointers("gInitRD_Files", $lStack[0][1]);
205
206 $gOutput .= <<<EOF
207 };
208 const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
209
210 EOF;
211
212
213 $fp = fopen($gOutputFile, "w");
214 fputs($fp, $gOutput);
215 fclose($fp);
216
217
218 if($gDepFile !== false)
219 {
220         $fp = fopen($gDepFile, "w");
221         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
222         fputs($fp, $line);
223         fclose($fp);
224 }
225
226 ?>

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