Kernel - Cleaning up useless message spam
[tpg/acess2.git] / KernelLand / 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         if($line[0] == "#")     continue;
32         // Directory
33         if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
34         {
35                 $new = array($matches[1], array());
36                 array_push($lStack, $new);
37                 $lDepth ++;
38                 continue;
39         }
40         // End of a block
41         if($line == "}")
42         {
43                 $lDepth --;
44                 $lStack[$lDepth][1][] = array_pop($lStack);
45                 continue;
46         }
47         // File
48         if(preg_match('/^File\s+"([^"]+)"\s+"([^"]+)"$/', $line, $matches))
49         {
50                 $lStack[$lDepth][1][] = array($matches[1], $matches[2]);
51                 continue;
52         }
53         echo "ERROR: $line\n";
54         exit(0);
55 }
56
57 function hd($fp)
58 {
59         //return "0x".str_pad( dechex(ord(fgetc($fp))), 8, "0", STR_PAD_LEFT );
60         $val = unpack("I", fread($fp, 4));
61         //print_r($val);        exit -1;
62         return "0x".dechex($val[1]);
63 }
64
65 function hd8($fp)
66 {
67         return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
68 }
69
70 $inode = 0;
71 $gSymFiles = array();
72 function ProcessFolder($prefix, $items)
73 {
74         global  $gOutput, $gDependencies;
75         global  $ACESSDIR, $ARCH;
76         global  $inode;
77         global  $gSymFiles;
78         foreach($items as $i=>$item)
79         {
80                 $inode ++;
81                 if(is_array($item[1]))
82                 {
83                         ProcessFolder("{$prefix}_{$i}", $item[1]);
84                         
85                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
86                         foreach($item[1] as $j=>$child)
87                         {
88                                 if($j)  $gOutput .= ",\n";
89                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
90                         }
91                         $gOutput .= "\n};\n";
92                         
93                         $size = count($item[1]);
94                         $gOutput .= <<<EOF
95 tVFS_Node {$prefix}_{$i} = {
96         .NumACLs = 1,
97         .ACLs = &gVFS_ACL_EveryoneRX,
98         .Flags = VFS_FFLAG_DIRECTORY,
99         .Size = $size,
100         .Inode = {$inode},
101         .ImplPtr = {$prefix}_{$i}_entries,
102         .Type = &gInitRD_DirType
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                         $_sym = "_binary_".str_replace(array("/","-","."), "_", $path)."_start";
127                         $gOutput .= "extern Uint8 {$_sym}[];";
128                         $gSymFiles[] = $path;
129                         $gOutput .= <<<EOF
130 tVFS_Node {$prefix}_{$i} = {
131         .NumACLs = 1,
132         .ACLs = &gVFS_ACL_EveryoneRX,
133         .Flags = 0,
134         .Size = $size,
135         .Inode = {$inode},
136         .ImplPtr = $_sym,
137         .Type = &gInitRD_FileType
138 };
139
140 EOF;
141                 }
142         }
143 }
144
145 //print_r($lStack);
146 //exit(1);
147
148 ProcessFolder("gInitRD_Files", $lStack[0][1]);
149
150 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
151 foreach($lStack[0][1] as $j=>$child)
152 {
153         if($j)  $gOutput .= ",\n";
154         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
155 }
156 $gOutput .= "\n};\n";
157 $nRootFiles = count($lStack[0][1]);
158 $gOutput .= <<<EOF
159 tVFS_Node gInitRD_RootNode = {
160         .NumACLs = 1,
161         .ACLs = &gVFS_ACL_EveryoneRX,
162         .Flags = VFS_FFLAG_DIRECTORY,
163         .Size = $nRootFiles,
164         .ImplPtr = gInitRD_Root_Files,
165         .Type = &gInitRD_DirType
166 };
167 EOF;
168
169 $gOutput .= <<<EOF
170
171 tVFS_Node * const gInitRD_FileList[] = {
172 &gInitRD_RootNode
173 EOF;
174
175 function PutNodePointers($prefix, $items)
176 {
177         global $gOutput;
178         foreach($items as $i=>$item)
179         {
180                 $gOutput .= ",&{$prefix}_{$i}";
181                 if(is_array($item[1]))
182                 {
183                         PutNodePointers("{$prefix}_{$i}", $item[1]);
184                 }
185         }
186 }
187
188 PutNodePointers("gInitRD_Files", $lStack[0][1]);
189
190 $gOutput .= <<<EOF
191 };
192 const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
193
194 EOF;
195
196
197 $fp = fopen($gOutputFile, "w");
198 fputs($fp, $gOutput);
199 fclose($fp);
200
201 // - Create options call
202 $fp = fopen($gOutputLDOptsFile, "w");
203 fputs($fp, "--format binary\n");
204 foreach($gSymFiles as $sym=>$file)
205 {
206         fputs($fp, "$file\n");
207 //      fputs($fp, "--defsym $sym=_binary_".$sym_filename."_start\n");
208 }
209 fclose($fp);
210
211 if($gDepFile !== false)
212 {
213         $fp = fopen($gDepFile, "w");
214         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
215         fputs($fp, $line);
216         fclose($fp);
217 }
218
219 ?>

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