Kernel/ARMv7 - Fixed not using ASIDs
[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         .ReadDir = InitRD_ReadDir,
102         .FindDir = InitRD_FindDir
103 };
104
105 EOF;
106                 }
107                 else
108                 {
109                         $path = $item[1];
110                         
111                         // Parse path components
112                         $path = str_replace("__BIN__", "$ACESSDIR/Usermode/Output/$ARCH", $path);
113                         $path = str_replace("__FS__", "$ACESSDIR/Usermode/Filesystem", $path);
114                         $path = str_replace("__SRC__", "$ACESSDIR", $path);
115                         echo $path,"\n";
116                         // ---
117                         
118                         $gDependencies[] = $path;
119
120                         if(!file_exists($path)) {
121                                 echo "ERROR: '{$path}' does not exist\n", 
122                                 exit(1);
123                         }
124                         $size = filesize($path);
125         
126 /*              
127                         $_sym = $prefix."_".$i."_data";
128                         $fp = fopen($path, "rb");
129                         
130                         $gOutput .= "Uint8 $_sym[] = {\n";
131                         for( $j = 0; $j + 16 < $size; $j += 16 ) {
132                                 $gOutput .= "\t";
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).",";
140                                 $gOutput .= hd8($fp).",".hd8($fp).",\n";
141                         }
142                         $gOutput .= "\t";
143                         for( ; $j < $size; $j ++ ) {
144                                 if( $j & 15 )   $gOutput .= ",";
145                                 $gOutput .= hd8($fp);
146                         }
147                         fclose($fp);
148                         $gOutput .= "\n};\n";
149 */
150                         
151 //*
152                         $_sym = "_binary_".str_replace(array("/","-","."), "_", $path)."_start";
153                         $gOutput .= "extern Uint8 {$_sym}[];";
154                         $gSymFiles[] = $path;
155 //*/
156                         $gOutput .= <<<EOF
157 tVFS_Node {$prefix}_{$i} = {
158         .NumACLs = 1,
159         .ACLs = &gVFS_ACL_EveryoneRX,
160         .Flags = 0,
161         .Size = $size,
162         .Inode = {$inode},
163         .ImplPtr = $_sym,
164         .Read = InitRD_ReadFile
165 };
166
167 EOF;
168                 }
169         }
170 }
171
172 //print_r($lStack);
173 //exit(1);
174
175 ProcessFolder("gInitRD_Files", $lStack[0][1]);
176
177 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
178 foreach($lStack[0][1] as $j=>$child)
179 {
180         if($j)  $gOutput .= ",\n";
181         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
182 }
183 $gOutput .= "\n};\n";
184 $nRootFiles = count($lStack[0][1]);
185 $gOutput .= <<<EOF
186 tVFS_Node gInitRD_RootNode = {
187         .NumACLs = 1,
188         .ACLs = &gVFS_ACL_EveryoneRX,
189         .Flags = VFS_FFLAG_DIRECTORY,
190         .Size = $nRootFiles,
191         .ImplPtr = gInitRD_Root_Files,
192         .ReadDir = InitRD_ReadDir,
193         .FindDir = InitRD_FindDir
194 };
195 EOF;
196
197 $gOutput .= <<<EOF
198
199 tVFS_Node * const gInitRD_FileList[] = {
200 &gInitRD_RootNode
201 EOF;
202
203 function PutNodePointers($prefix, $items)
204 {
205         global $gOutput;
206         foreach($items as $i=>$item)
207         {
208                 $gOutput .= ",&{$prefix}_{$i}";
209                 if(is_array($item[1]))
210                 {
211                         PutNodePointers("{$prefix}_{$i}", $item[1]);
212                 }
213         }
214 }
215
216 PutNodePointers("gInitRD_Files", $lStack[0][1]);
217
218 $gOutput .= <<<EOF
219 };
220 const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
221
222 EOF;
223
224
225 $fp = fopen($gOutputFile, "w");
226 fputs($fp, $gOutput);
227 fclose($fp);
228
229 // - Create options call
230 $fp = fopen($gOutputLDOptsFile, "w");
231 fputs($fp, "--format binary\n");
232 foreach($gSymFiles as $sym=>$file)
233 {
234         fputs($fp, "$file\n");
235 //      fputs($fp, "--defsym $sym=_binary_".$sym_filename."_start\n");
236 }
237 fclose($fp);
238
239 if($gDepFile !== false)
240 {
241         $fp = fopen($gDepFile, "w");
242         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
243         fputs($fp, $line);
244         fclose($fp);
245 }
246
247 ?>

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