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

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