Major build system changes
[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                 $gDependencies[] = $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 function ProcessFolder($prefix, $items)
70 {
71         global  $gOutput;
72         global  $ACESSDIR, $ARCH;
73         foreach($items as $i=>$item)
74         {
75                 if(is_array($item[1]))
76                 {
77                         ProcessFolder("{$prefix}_{$i}", $item[1]);
78                         
79                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
80                         foreach($item[1] as $j=>$child)
81                         {
82                                 if($j)  $gOutput .= ",\n";
83                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
84                         }
85                         $gOutput .= "\n};\n";
86                         
87                         $size = count($item[1]);
88                         $gOutput .= <<<EOF
89 tVFS_Node {$prefix}_{$i} = {
90         .NumACLs = 1,
91         .ACLs = &gVFS_ACL_EveryoneRX,
92         .Flags = VFS_FFLAG_DIRECTORY,
93         .Size = $size,
94         .ImplPtr = {$prefix}_{$i}_entries,
95         .ReadDir = InitRD_ReadDir,
96         .FindDir = InitRD_FindDir
97 };
98
99 EOF;
100                 }
101                 else
102                 {
103                         $path = $item[1];
104                         
105                         // Parse path components
106                         $path = str_replace("__BIN__", "$ACESSDIR/Usermode/Output/$ARCH", $path);
107                         $path = str_replace("__FS__", "$ACESSDIR/Usermode/Filesystem", $path);
108                         echo $path,"\n";
109                         // ---
110
111                         if(!file_exists($path)) {
112                                 echo "ERROR: '{$path}' does not exist\n", 
113                                 exit(1);
114                         }
115                         $size = filesize($path);
116                         
117                         $fp = fopen($path, "rb");
118                         
119                         $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
120                         for( $j = 0; $j + 16 < $size; $j += 16 ) {
121                                 $gOutput .= "\t";
122                                 $gOutput .= hd8($fp).",".hd8($fp).",";
123                                 $gOutput .= hd8($fp).",".hd8($fp).",";
124                                 $gOutput .= hd8($fp).",".hd8($fp).",";
125                                 $gOutput .= hd8($fp).",".hd8($fp).",";
126                                 $gOutput .= hd8($fp).",".hd8($fp).",";
127                                 $gOutput .= hd8($fp).",".hd8($fp).",";
128                                 $gOutput .= hd8($fp).",".hd8($fp).",";
129                                 $gOutput .= hd8($fp).",".hd8($fp).",\n";
130                         }
131                         $gOutput .= "\t";
132                         for( ; $j < $size; $j ++ ) {
133                                 if( $j & 15 )   $gOutput .= ",";
134                                 $gOutput .= hd8($fp);
135                         }
136                         fclose($fp);
137                         $gOutput .= "\n};\n";
138                         $gOutput .= <<<EOF
139 tVFS_Node {$prefix}_{$i} = {
140         .NumACLs = 1,
141         .ACLs = &gVFS_ACL_EveryoneRX,
142         .Flags = 0,
143         .Size = $size,
144         .ImplPtr = {$prefix}_{$i}_data,
145         .Read = InitRD_ReadFile
146 };
147
148 EOF;
149                 }
150         }
151 }
152
153 //print_r($lStack);
154 //exit(1);
155
156 ProcessFolder("gInitRD_Files", $lStack[0][1]);
157
158 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
159 foreach($lStack[0][1] as $j=>$child)
160 {
161         if($j)  $gOutput .= ",\n";
162         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
163 }
164 $gOutput .= "\n};\n";
165 $nRootFiles = count($lStack[0][1]);
166 $gOutput .= <<<EOF
167 tVFS_Node gInitRD_RootNode = {
168         .NumACLs = 1,
169         .ACLs = &gVFS_ACL_EveryoneRX,
170         .Flags = VFS_FFLAG_DIRECTORY,
171         .Size = $nRootFiles,
172         .ImplPtr = gInitRD_Root_Files,
173         .ReadDir = InitRD_ReadDir,
174         .FindDir = InitRD_FindDir
175 };
176 EOF;
177
178 $fp = fopen($gOutputFile, "w");
179 fputs($fp, $gOutput);
180 fclose($fp);
181
182
183 if($gDepFile !== false)
184 {
185         $fp = fopen($gDepFile, "w");
186         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
187         fputs($fp, $line);
188         fclose($fp);
189 }
190
191 ?>

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