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

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