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

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