Skip to content

Commit

Permalink
Beta android reader update. Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Sep 14, 2022
1 parent 6078819 commit 8e4432c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 30 deletions.
8 changes: 4 additions & 4 deletions NyaFs/Filesystem/Cpio/CpioFsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ private void Init()
var F = new Types.CpioNode(Raw);

if (!FI.IsTrailer)
{
Nodes.Add(F);
else
break;

Offset += FI.FullFileBlockSize;
}
Offset += FI.FullFileBlockSize;
}
else
break;
Expand Down Expand Up @@ -122,7 +122,7 @@ public FilesystemEntry[] ReadDir(string Path)
if(Pos == 0)
{
Pos = Pos + Path.Length + 1;
if(UPath.IndexOf('/', Pos) < 0)
if((Pos < UPath.Length) && UPath.IndexOf('/', Pos) < 0)
{
Res.Add(new FilesystemEntry(N.FsType, UPath, N.UserId, N.GroupId, N.HexMode, Convert.ToUInt32(N.Content.Length)));
}
Expand Down
4 changes: 2 additions & 2 deletions NyaFs/Filesystem/Universal/Items/SymLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class SymLink : FilesystemItem

public SymLink(string Filename, uint User, uint Group, uint Mode, string Target) : base(Types.FilesystemItemType.SymLink, Filename, User, Group, Mode)
{
this.Target = Target;
this.Target = (Target == null) ? "" : Target;
}

public override string ToString()
{
return $"LINK {Filename} {User}:{Group} {Mode:x03} => {Target}";
}
public override long Size => Target.Length;
public override long Size => Target?.Length ?? 0;
}
}
22 changes: 22 additions & 0 deletions NyaFs/ImageFormat/Composite/AndroidImageWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.ImageFormat.Composite
{
class AndroidImageWriter
{
private readonly string Path;

public AndroidImageWriter(string Path)
{
this.Path = Path;
}

public bool Write(BaseImageBlob Blob)
{

return false;
}
}
}
27 changes: 27 additions & 0 deletions NyaFs/ImageFormat/Elements/Dtb/Reader/AndroidReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ public override void ReadToDevTree(DeviceTree Dst)
if (Image.IsMagicCorrect)
{
// TODO: detect, is dtb present in image
if(Image.HeaderVersion == 2)
{
// v2 contains Dtb
var Imagev2 = new Types.Android.AndroidImagev2(Image.getPacket());

var Raw = Imagev2.Dtbo;
if(Raw.Length > 0)
{
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 android image.");
}
}
else if(Image.HeaderVersion < 2)
{
// No dtb...
}
else
{
// Different image format!..
throw new NotImplementedException("Android image v3-4 are not supported now!");
}
}
}
}
Expand Down
46 changes: 30 additions & 16 deletions NyaFs/ImageFormat/Elements/Fs/Reader/AndroidReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,42 @@ public override void ReadToFs(LinuxFilesystem Dst)
{
if (Image.IsMagicCorrect)
{
// Is legacy image...
uint Magic = Image.Ramdisk.ReadUInt32(0);
if (Magic == 0x56190527)
var Version = Image.HeaderVersion;
if(Version < 3)
{
// Parse as legacy
var Reader = new LegacyReader(Image.Ramdisk);
Reader.ReadToFs(Dst);
ReadToFsv0(Dst);
}
else
{
var Comp = (Magic == 0x04224D18)
? Types.CompressionType.IH_COMP_LZ4
: Helper.FitHelper.DetectCompression(Image.Ramdisk);
// Different image format!..
throw new NotImplementedException("Android image v3-4 are not supported now!");
}
}
}
private void ReadToFsv0(LinuxFilesystem Dst)
{
var RD = Image.Ramdisk;
// Is legacy image...
uint Magic = RD.ReadUInt32(0);
if (Magic == 0x56190527)
{
// Parse as legacy
var Reader = new LegacyReader(RD);
Reader.ReadToFs(Dst);
}
else
{
var Comp = (Magic == 0x04224D18)
? Types.CompressionType.IH_COMP_LZ4
: Helper.FitHelper.DetectCompression(RD);

var Uncompressed = Helper.FitHelper.GetDecompressedData(Image.Ramdisk, Comp);
var Uncompressed = Helper.FitHelper.GetDecompressedData(RD, Comp);

Dst.Info.Compression = Comp;
Dst.Info.Type = Types.ImageType.IH_TYPE_RAMDISK;
Dst.Info.OperatingSystem = Types.OS.IH_OS_LINUX;
Dst.Info.DataLoadAddress = Image.RamdiskAddress;
DetectAndRead(Dst, Uncompressed);
}
Dst.Info.Compression = Comp;
Dst.Info.Type = Types.ImageType.IH_TYPE_RAMDISK;
Dst.Info.OperatingSystem = Types.OS.IH_OS_LINUX;
Dst.Info.DataLoadAddress = Image.RamdiskAddress;
DetectAndRead(Dst, Uncompressed);
}
}
}
Expand Down
26 changes: 22 additions & 4 deletions NyaFs/ImageFormat/Elements/Kernel/Reader/AndroidReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class AndroidReader : Reader
Types.Android.LegacyAndroidImage Image;

public AndroidReader(string Filename) : this(System.IO.File.ReadAllBytes(Filename)) { }

public AndroidReader(byte[] Raw)
{
Image = new Types.Android.LegacyAndroidImage(Raw);
Expand All @@ -18,11 +19,28 @@ public override void ReadToKernel(LinuxKernel Dst)
{
if(Image.IsMagicCorrect)
{
Dst.Image = Image.Kernel;
Dst.Info.Compression = Types.CompressionType.IH_COMP_NONE;
Dst.Info.DataLoadAddress = Image.KernelAddress;
Dst.Info.EntryPointAddress = Image.KernelAddress;
var Version = Image.HeaderVersion;
if (Version < 3)
{
ReadToKernelv0(Dst);
}
else
{
// Different image format!..
throw new NotImplementedException("Android image v3-4 are not supported now!");
}
}
}

private void ReadToKernelv0(LinuxKernel Dst)
{


// TODO: detect image format...
Dst.Info.Compression = Types.CompressionType.IH_COMP_NONE;
Dst.Info.DataLoadAddress = Image.KernelAddress;
Dst.Info.EntryPointAddress = Image.KernelAddress;
Dst.Image = Image.Kernel;
}
}
}
41 changes: 41 additions & 0 deletions NyaFs/ImageFormat/Types/Android/AndroidImagev1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Extension.Array;
using Extension.Packet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NyaFs.ImageFormat.Types.Android
{
class AndroidImagev1 : LegacyAndroidImage
{
public AndroidImagev1(byte[] Raw) : base(Raw)
{

}

/// <summary>
/// Recovery dtb size in bytes
/// </summary>
public uint RecoveryDtboSize
{
get { return ReadUInt32(0x660); }
set { WriteUInt32(0x660, value); }
}

/// <summary>
/// Recovery dtb address
/// </summary>
public ulong RecoveryDtboAddress
{
get { return ReadUInt64(0x664); }
set { WriteUInt64(0x664, value); }
}

public override long HeaderSize => ReadUInt32(0x66C);

protected long RecoveryDtboOffset => (SecondOffset + SecondSize).GetAligned(PageSize);

public byte[] RecoveryDtbo => ReadArray(RecoveryDtboOffset, RecoveryDtboSize);
}
}
39 changes: 39 additions & 0 deletions NyaFs/ImageFormat/Types/Android/AndroidImagev2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Extension.Array;
using Extension.Packet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NyaFs.ImageFormat.Types.Android
{
class AndroidImagev2 : AndroidImagev1
{
public AndroidImagev2(byte[] Raw) : base(Raw)
{

}

/// <summary>
/// dtb size in bytes
/// </summary>
public uint DtboSize
{
get { return ReadUInt32(0x670); }
set { WriteUInt32(0x670, value); }
}

/// <summary>
/// dtb address
/// </summary>
public ulong DtboAddress
{
get { return ReadUInt64(0x674); }
set { WriteUInt64(0x674, value); }
}

protected long DtboOffset => (RecoveryDtboOffset + RecoveryDtboSize).GetAligned(PageSize);

public byte[] Dtbo => ReadArray(DtboOffset, DtboSize);
}
}
10 changes: 6 additions & 4 deletions NyaFs/ImageFormat/Types/Android/LegacyAndroidImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public uint RamdiskAddress
}

/// <summary>
/// Second size in bytes
/// Second bootloader size in bytes
/// </summary>
public uint SecondSize
{
Expand All @@ -68,7 +68,7 @@ public uint SecondSize
}

/// <summary>
/// Second address
/// Second bootloader address
/// </summary>
public uint SecondAddress
{
Expand Down Expand Up @@ -157,11 +157,13 @@ public string ExtraAgrs

public uint KernelBase => KernelAddress - 0x8000;

private long KernelOffset => HeaderSize.GetAligned(PageSize);
private long RamdiskOffset => (KernelOffset + KernelSize).GetAligned(PageSize);
protected long KernelOffset => HeaderSize.GetAligned(PageSize);
protected long RamdiskOffset => (KernelOffset + KernelSize).GetAligned(PageSize);
protected long SecondOffset => (RamdiskOffset + RamdiskSize).GetAligned(PageSize);

public byte[] Kernel => ReadArray(KernelOffset, KernelSize);
public byte[] Ramdisk => ReadArray(RamdiskOffset, RamdiskSize);
public byte[] Second => ReadArray(SecondOffset, SecondSize);

public HashType DetectedHashType
{
Expand Down

0 comments on commit 8e4432c

Please sign in to comment.