Modules/InitRD - Fixed to not convert files into hex
[tpg/acess2.git] / Modules / Filesystems / InitRD / GenerateInitRD.php
index ab6eb60..af967d5 100644 (file)
@@ -10,9 +10,13 @@ $gOutput = <<<EOF
 
 EOF;
 
+$ACESSDIR = getenv("ACESSDIR");
+$ARCH = getenv("ARCH");
+
 $gInputFile = $argv[1];
 $gOutputFile = $argv[2];
-$gDepFile = ($argc > 3 ? $argv[3] : false);
+$gOutputLDOptsFile = $argv[3];
+$gDepFile = ($argc > 4 ? $argv[4] : false);
 
 $gDependencies = array();
 
@@ -24,6 +28,7 @@ $lStack = array( array("",array()) );
 foreach($lines as $line)
 {
        $line = trim($line);
+       // Directory
        if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
        {
                $new = array($matches[1], array());
@@ -31,16 +36,17 @@ foreach($lines as $line)
                $lDepth ++;
                continue;
        }
+       // End of a block
        if($line == "}")
        {
                $lDepth --;
                $lStack[$lDepth][1][] = array_pop($lStack);
                continue;
        }
+       // File
        if(preg_match('/^File\s+"([^"]+)"\s+"([^"]+)"$/', $line, $matches))
        {
                $lStack[$lDepth][1][] = array($matches[1], $matches[2]);
-               $gDependencies[] = $matches[2];
                continue;
        }
        echo "ERROR: $line\n";
@@ -60,11 +66,17 @@ function hd8($fp)
        return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
 }
 
+$inode = 0;
+$gSymFiles = array();
 function ProcessFolder($prefix, $items)
 {
-       global  $gOutput;
+       global  $gOutput, $gDependencies;
+       global  $ACESSDIR, $ARCH;
+       global  $inode;
+       global  $gSymFiles;
        foreach($items as $i=>$item)
        {
+               $inode ++;
                if(is_array($item[1]))
                {
                        ProcessFolder("{$prefix}_{$i}", $item[1]);
@@ -84,6 +96,7 @@ tVFS_Node {$prefix}_{$i} = {
        .ACLs = &gVFS_ACL_EveryoneRX,
        .Flags = VFS_FFLAG_DIRECTORY,
        .Size = $size,
+       .Inode = {$inode},
        .ImplPtr = {$prefix}_{$i}_entries,
        .ReadDir = InitRD_ReadDir,
        .FindDir = InitRD_FindDir
@@ -91,67 +104,63 @@ tVFS_Node {$prefix}_{$i} = {
 
 EOF;
                }
-               else {
-                       if(!file_exists($item[1])) {
-                               echo "ERROR: '{$item[1]}' does not exist\n", 
+               else
+               {
+                       $path = $item[1];
+                       
+                       // Parse path components
+                       $path = str_replace("__BIN__", "$ACESSDIR/Usermode/Output/$ARCH", $path);
+                       $path = str_replace("__FS__", "$ACESSDIR/Usermode/Filesystem", $path);
+                       $path = str_replace("__SRC__", "$ACESSDIR", $path);
+                       echo $path,"\n";
+                       // ---
+                       
+                       $gDependencies[] = $path;
+
+                       if(!file_exists($path)) {
+                               echo "ERROR: '{$path}' does not exist\n", 
                                exit(1);
                        }
-                       $size = filesize($item[1]);
+                       $size = filesize($path);
+       
+/*             
+                       $_sym = $prefix."_".$i."_data";
+                       $fp = fopen($path, "rb");
                        
-                       $fp = fopen($item[1], "rb");
-                       if(0)
-                       {
-                               $gOutput .= "Uint32 {$prefix}_{$i}_data[] = {\n";
-                               for( $j = 0; $j + 16 < $size; $j += 16 )
-                               {
-                                       $gOutput .= "\t";
-                                       $gOutput .= hd($fp).",".hd($fp).",";
-                                       $gOutput .= hd($fp).",".hd($fp).",\n";
-                               }
+                       $gOutput .= "Uint8 $_sym[] = {\n";
+                       for( $j = 0; $j + 16 < $size; $j += 16 ) {
                                $gOutput .= "\t";
-                               for( ; $j+3 < $size; $j += 4 )
-                               {
-                                       if( $j & 15 )   $gOutput .= ",";
-                                       $gOutput .= hd($fp);
-                               }
-                               if($j < $size) {
-                                       $tmp = "";
-                                       while($j ++ < $size)
-                                               $tmp .= fgetc($fp);
-                                       $tmp .= "\0\0\0";
-                                               $tmp = unpack("I", $tmp);
-                                       $gOutput .= "0x".dechex($tmp[1]);
-                               }
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",";
+                               $gOutput .= hd8($fp).",".hd8($fp).",\n";
                        }
-                       else
-                       {
-                               $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
-                               for( $j = 0; $j + 16 < $size; $j += 16 ) {
-                                       $gOutput .= "\t";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",";
-                                       $gOutput .= hd8($fp).",".hd8($fp).",\n";
-                               }
-                               $gOutput .= "\t";
-                               for( ; $j < $size; $j ++ ) {
-                                       if( $j & 15 )   $gOutput .= ",";
-                                       $gOutput .= hd8($fp);
-                               }
+                       $gOutput .= "\t";
+                       for( ; $j < $size; $j ++ ) {
+                               if( $j & 15 )   $gOutput .= ",";
+                               $gOutput .= hd8($fp);
                        }
                        fclose($fp);
                        $gOutput .= "\n};\n";
+*/
+                       
+//*
+                       $_sym = "_binary_".str_replace(array("/","-","."), "_", $path)."_start";
+                       $gOutput .= "extern Uint8 {$_sym}[];";
+                       $gSymFiles[] = $path;
+//*/
                        $gOutput .= <<<EOF
 tVFS_Node {$prefix}_{$i} = {
        .NumACLs = 1,
        .ACLs = &gVFS_ACL_EveryoneRX,
        .Flags = 0,
        .Size = $size,
-       .ImplPtr = {$prefix}_{$i}_data,
+       .Inode = {$inode},
+       .ImplPtr = $_sym,
        .Read = InitRD_ReadFile
 };
 
@@ -185,10 +194,47 @@ tVFS_Node gInitRD_RootNode = {
 };
 EOF;
 
+$gOutput .= <<<EOF
+
+tVFS_Node * const gInitRD_FileList[] = {
+&gInitRD_RootNode
+EOF;
+
+function PutNodePointers($prefix, $items)
+{
+       global $gOutput;
+       foreach($items as $i=>$item)
+       {
+               $gOutput .= ",&{$prefix}_{$i}";
+               if(is_array($item[1]))
+               {
+                       PutNodePointers("{$prefix}_{$i}", $item[1]);
+               }
+       }
+}
+
+PutNodePointers("gInitRD_Files", $lStack[0][1]);
+
+$gOutput .= <<<EOF
+};
+const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
+
+EOF;
+
+
 $fp = fopen($gOutputFile, "w");
 fputs($fp, $gOutput);
 fclose($fp);
 
+// - Create options call
+$fp = fopen($gOutputLDOptsFile, "w");
+fputs($fp, "--format binary\n");
+foreach($gSymFiles as $sym=>$file)
+{
+       fputs($fp, "$file\n");
+//     fputs($fp, "--defsym $sym=_binary_".$sym_filename."_start\n");
+}
+fclose($fp);
 
 if($gDepFile !== false)
 {

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