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

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