Skip to content

Commit

Permalink
Merge pull request #2378 from JsphA/master
Browse files Browse the repository at this point in the history
Add kernel compression
  • Loading branch information
quajak authored Aug 15, 2022
2 parents 4a30847 + 6b9beda commit 98a5866
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
9 changes: 9 additions & 0 deletions source/Cosmos.Build.Tasks/CreateGrubConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class CreateGrubConfig: Task
[Required]
public string BinName { get; set; }

public string[] Modules { get; set; }

private string Indentation = " ";

public override bool Execute()
Expand All @@ -29,6 +31,13 @@ public override bool Execute()
using (var xWriter = File.CreateText(Path.Combine(TargetDirectory + "/boot/grub/", "grub.cfg")))
{
xWriter.WriteLine("set timeout=0");
if (Modules != null)
{
foreach (var module in Modules)
{
xWriter.WriteLine($"insmod {module}");
}
}
xWriter.WriteLine();
xWriter.WriteLine("menuentry '" + xLabelName + "' {");
WriteIndentedLine(xWriter, "multiboot2 /boot/" + xBinName);
Expand Down
43 changes: 43 additions & 0 deletions source/Cosmos.Build.Tasks/Gzip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.IO.Compression;
using System.Diagnostics;

namespace Cosmos.Build.Tasks
{
public class Gzip : Task
{
[Required]
public string BinFile { get; set; }

[Required]
public string OutputFile { get; set; }

public override bool Execute()
{
if (!File.Exists(BinFile))
{
Log.LogError($"'{BinFile}' is missing!");
return false;
}

var stopwatch = new Stopwatch();
stopwatch.Start();

using FileStream fileStream = File.Open(BinFile, FileMode.Open);
using FileStream compressedFileStream = File.Create(OutputFile);
using var gzip = new GZipStream(compressedFileStream, CompressionLevel.Optimal);
fileStream.CopyTo(gzip);

stopwatch.Stop();

int beforeMB = (int)(new FileInfo(BinFile).Length / 1024 / 1024);
int afterMB = (int)(new FileInfo(OutputFile).Length / 1024 / 1024);

Log.LogMessage(MessageImportance.High, $"Compressed '{BinFile}' with gzip in {stopwatch.ElapsedMilliseconds} ms ({beforeMB} MB -> {afterMB} MB)");

return true;
}
}
}
61 changes: 51 additions & 10 deletions source/Cosmos.Build.Tasks/build/Cosmos.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<PropertyGroup>
<BinFile Condition="'$(BinFile)' == ''">$(OutputPath)$(AssemblyName).bin</BinFile>
<BinGzFile Condition="'$(BinGzFile)' == ''">$(OutputPath)$(AssemblyName).bin.gz</BinGzFile>
<IsoFile Condition="'$(IsoFile)' == ''">$(OutputPath)$(AssemblyName).iso</IsoFile>
</PropertyGroup>

Expand Down Expand Up @@ -37,9 +38,10 @@
<DebugCom Condition="'$(DebugEnabled)' == 'False'">0</DebugCom>

<BinFormat Condition="'$(BinFormat)' == ''">ELF</BinFormat>
<CompressionType Condition="'$(CompressionType)' == ''">None</CompressionType>
<DebugMode Condition="'$(DebugMode)' == ''">Source</DebugMode>
<TraceMode Condition="'$(TraceMode)' == ''">User</TraceMode>
<ExtractMapFile Condition="'$(ExtractMapFile)' == ''">False</ExtractMapFile>
<ExtractMapFile Condition="'$(ExtractMapFile)' == ''">False</ExtractMapFile>
<StackCorruptionDetectionEnabled Condition="'$(StackCorruptionDetectionEnabled)' == ''">True</StackCorruptionDetectionEnabled>
<StackCorruptionDetectionLevel Condition="'$(StackCorruptionDetectionLevel)' == ''">MethodFooters</StackCorruptionDetectionLevel>
<IgnoreDebugStubAttribute Condition="'$(IgnoreDebugStubAttribute)' == ''">False</IgnoreDebugStubAttribute>
Expand All @@ -61,7 +63,7 @@

<CompileVBEMultiboot Condition="'$(CompileVBEMultiboot)' == ''">False</CompileVBEMultiboot>

</PropertyGroup>
</PropertyGroup>

<PropertyGroup>
<CosmosBuildTasksAssembly Condition="'$(CosmosBuildTasksAssembly)' == '' OR !Exists('$(CosmosBuildTasksAssembly)')">$(CosmosToolsPath)net48\Cosmos.Build.Tasks.dll</CosmosBuildTasksAssembly>
Expand Down Expand Up @@ -117,6 +119,7 @@
<UsingTask TaskName="Cosmos.Build.Tasks.IL2CPU" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.Launch" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.Ld" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.Gzip" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.MakeIso" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.Nasm" AssemblyFile="$(CosmosBuildTasksAssembly)" />
<UsingTask TaskName="Cosmos.Build.Tasks.ReadNasmMapToDebugInfo" AssemblyFile="$(CosmosBuildTasksAssembly)" />
Expand All @@ -135,6 +138,7 @@
Ld;
ExtractMapFromElfFile;
ReadNasmMapToDebugInfo;
Gzip;
MakeIso
</CosmosBuildDependsOn>
</PropertyGroup>
Expand Down Expand Up @@ -293,6 +297,28 @@

</Target>

<!--
================================================================================
Gzip
[IN]
$(BinFile) - a binary file.
[OUT]
$(BinGzFile) - a gzipped binary file.
================================================================================
-->
<Target Name="Gzip"
Inputs="$(BinFile)"
Outputs="$(BinGzFile)"
Condition="'$(CompressionType)' == 'Gzip'">

<Gzip BinFile="$(BinFile)"
OutputFile="$(BinGzFile)" />

</Target>

<!--
================================================================================
MakeISO
Expand All @@ -310,16 +336,17 @@
Outputs="$(IsoFile)"
Condition="'$(Deployment)' == 'ISO'">

<ItemGroup>
<_IsoCustomFiles Include="$(MSBuildProjectDirectory)\isoFiles\**\*.*"/>
</ItemGroup>
<ItemGroup>
<_IsoCustomFiles Include="$(MSBuildProjectDirectory)\isoFiles\**\*.*"/>
</ItemGroup>

<ItemGroup>
<_i386pcFile Include="$(GrubPath)boot\grub\i386-pc\*" />
</ItemGroup>

<ItemGroup>
<_IsoFile Include="$(BinFile)" />
<_IsoFile Include="$(BinGzFile)" Condition="'$(CompressionType)' == 'Gzip'" />
<_IsoFile Include="$(BinFile)" Condition="'$(CompressionType)' == 'None'" />
<_IsoFile Include="@(ContentToDeploy)" />
</ItemGroup>

Expand All @@ -339,9 +366,16 @@
<Copy SourceFiles="@(_i386pcFile)"
DestinationFolder="$(IntermediateIsoDirectory)\boot\grub\i386-pc\" />

<CreateGrubConfig TargetDirectory="$(IntermediateIsoDirectory)"
BinName="$([System.IO.Path]::GetFileName('$(BinFile)'))" />

<CreateGrubConfig TargetDirectory="$(IntermediateIsoDirectory)"
BinName="$([System.IO.Path]::GetFileName('$(BinGzFile)'))"
Modules="gzio"
Condition="'$(CompressionType)' == 'Gzip'" />

<CreateGrubConfig TargetDirectory="$(IntermediateIsoDirectory)"
BinName="$([System.IO.Path]::GetFileName('$(BinFile)'))"
Condition="'$(CompressionType)' == 'None'" />

<MakeIso IsoDirectory="$(IntermediateIsoDirectory)"
OutputFile="$(IsoFile)"
ToolPath="$(MkisofsToolPath)"
Expand Down Expand Up @@ -397,7 +431,8 @@
</ItemGroup>

<ItemGroup>
<_UsbFile Include="$(BinFile)" />
<_UsbFile Include="$(BinGzFile)" Condition="'$(CompressionType)' == 'Gzip'" />
<_UsbFile Include="$(BinFile)" Condition="'$(CompressionType)' == 'None'" />
<_UsbFile Include="@(ContentToDeploy)" />
</ItemGroup>

Expand All @@ -421,7 +456,13 @@
DestinationFolder="$(UsbPublishDrive)" />

<CreateGrubConfig TargetDirectory="$(UsbPublishDrive)"
BinName="$([System.IO.Path]::GetFileName('$(BinFile)'))" />
BinName="$([System.IO.Path]::GetFileName('$(BinGzFile)'))"
Modules="gzio"
Condition="'$(CompressionType)' == 'Gzip'" />

<CreateGrubConfig TargetDirectory="$(UsbPublishDrive)"
BinName="$([System.IO.Path]::GetFileName('$(BinFile)'))"
Condition="'$(CompressionType)' == 'None'" />

<CreateMbr TargetDrive="$(UsbPublishDrive)"
FormatDrive="$(UsbPublishFormatDrive)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
<EnumValue Name="Bin" DisplayName="Bin"/>
</EnumProperty>

<EnumProperty Name="CompressionType"
DisplayName="Compression"
Category="Compile"
Description="Apply compression to the kernel.">
<EnumValue Name="None" DisplayName="None"/>
<EnumValue Name="Gzip" DisplayName="gzip"/>
</EnumProperty>

<BoolProperty Name="CompileVBEMultiboot"
DisplayName="VBE Multiboot"
Description="Compile Cosmos with VBE information specified in Multiboot structure."
Expand Down

0 comments on commit 98a5866

Please sign in to comment.