Skip to content

Commit

Permalink
Loading compressed dtb
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Sep 5, 2022
1 parent 8c8402c commit 46a2921
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
37 changes: 37 additions & 0 deletions NyaFs/ImageFormat/Elements/Dtb/Reader/ArchiveReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.ImageFormat.Elements.Dtb.Reader
{
class ArchiveReader : Reader
{
byte[] Data = null;
Types.CompressionType Compression;

public ArchiveReader(string Filename, Types.CompressionType Compression)
{
Data = System.IO.File.ReadAllBytes(Filename);
this.Compression = Compression;
}

public ArchiveReader(byte[] Data)
{
this.Data = Data;
}

public override void ReadToDevTree(DeviceTree Dst)
{
byte[] Raw = Helper.FitHelper.GetDecompressedData(Data, Compression);

var Dtb = new FlattenedDeviceTree.Reader.FDTReader(Raw);
if (Dtb.Correct)
{
var Reader = new DtbReader(Raw);
Reader.ReadToDevTree(Dst);
}
else
Log.Error(0, $"Unknown device tree format in compressed image.");
}
}
}
10 changes: 9 additions & 1 deletion NyaFs/ImageFormat/Elements/Dtb/Reader/DtbReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ namespace NyaFs.ImageFormat.Elements.Dtb.Reader
{
public class DtbReader : Reader
{
byte[] Data;
string Path;

public DtbReader(string Path)
{
this.Path = Path;
this.Data = System.IO.File.ReadAllBytes(Path);
}

public DtbReader(byte[] Data)
{
this.Path = "loaded image";
this.Data = Data;
}

/// <summary>
Expand All @@ -19,7 +27,7 @@ public DtbReader(string Path)
/// <param name="Dst"></param>
public override void ReadToDevTree(DeviceTree Dst)
{
var Dtb = new FlattenedDeviceTree.Reader.FDTReader(Path);
var Dtb = new FlattenedDeviceTree.Reader.FDTReader(Data);
if (Dtb.Correct)
{
Dst.DevTree = Dtb.Read();
Expand Down
16 changes: 13 additions & 3 deletions NyaFs/Processor/Scripting/Commands/Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class Load : ScriptStepGenerator
public Load() : base("load")
{
AddConfig(new Configs.ImageScriptArgsConfig(0, "kernel",
new string[] { "gz", "gzip", "lzma", "lz4", "lzo", "legacy", "bzip2", "bz2", "fit", "android", "raw" }));
new string[] { "gz", "gzip", "lzma", "lz4", "bz2", "lzo", "zstd", "bzip2", "bz2", "raw", "legacy", "fit", "android" }));

AddConfig(new Configs.ImageScriptArgsConfig(1, "ramfs",
new string[] { "cpio", "gz", "gzip", "lzma", "lz4", "bz2", "lzo", "zstd", "bzip2", "bz2", "legacy", "fit", "ext2", "android", "squashfs" }));
new string[] { "gz", "gzip", "lzma", "lz4", "bz2", "lzo", "zstd", "bzip2", "bz2", "fit", "android", "legacy", "cpio", "ext2", "squashfs" }));


AddConfig(new Configs.ImageScriptArgsConfig(2, "devtree",
new string[] { "dtb", "fit" }));
new string[] { "gz", "gzip", "lzma", "lz4", "bz2", "lzo", "zstd", "bzip2", "bz2", "dtb", "fit" }));

AddConfig(new ScriptArgsConfig(3, new ScriptArgsParam[] { new Params.FsPathScriptArgsParam() }));

Expand Down Expand Up @@ -182,6 +182,16 @@ private ImageFormat.Elements.Dtb.Reader.Reader GetDtbReader()
{
case "dtb": return new ImageFormat.Elements.Dtb.Reader.DtbReader(Path);
case "fit": return new ImageFormat.Elements.Dtb.Reader.FitReader(Path);
case "lz4":
case "lzma":
case "gz":
case "gzip":
case "bz2":
case "bzip2":
case "zstd":
case "lzo":
var CompressionType = Helper.ArchiveHelper.GetCompressionFormat(Format);
return new ImageFormat.Elements.Dtb.Reader.ArchiveReader(Path, CompressionType);
case "dts":
default:
return null;
Expand Down
2 changes: 2 additions & 0 deletions NyaFs/Processor/Scripting/Helper/ArchiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private static Tuple<string, string> TryDecompressArchive(byte[] Data, string Na
return new Tuple<string, string>("ramfs", Name);
if(Res.Item1 == "kernel")
return new Tuple<string, string>("kernel", Name);
if (Res.Item1 == "devtree")
return new Tuple<string, string>("devtree", Name);
}
return null;
}
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ Load fs from legacy image:
```
load <filename.legacy> ramfs legacy
```
Load fs from compressed file (cpio or ext2 image):
Load fs from compressed file (cpio, ext2, squashfs image):
```
load <filename.ct> ramfs <compression>
```
(compression) is "gzip", "lz4", "lzma", "bzip2", "zstd"
(compression) is "gzip", "lz4", "lzma", "bzip2", "zstd", "lzo"

Load fs from cpio file:
```
Expand All @@ -118,7 +118,7 @@ Load kernel from archived image:
```
load <filename.ct> kernel <compression>
```
(compression) is "gzip", "lz4", "lzma", "bzip2", "zstd"
(compression) is "gzip", "lz4", "lzma", "bzip2", "zstd", "lzo"

Load kernel from legacy (uImage, zImage) image:
```
Expand All @@ -130,6 +130,12 @@ Load device tree from dtb:
```
load <filename.dtb> devtree dtb
```

Load device tree from compressed file (dtb image):
```
load <filename.ct> devtree <compression>
```
(compression) is "gzip", "lz4", "lzma", "bzip2", "zstd", "lzo"
## Commands for image saving
To store images to legacy or FIT format, there is need to specify os/arch of these images, if they was loaded from images without such info (cpio, gz).

Expand Down

0 comments on commit 46a2921

Please sign in to comment.