Random Noise

cat /dev/random >> /dev/dsp

Print TRX Header (Router Firmware)

leave a comment »


[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TRXHeader
{
    public UInt32 Magic;                 /* "HDR0" */
    public UInt32 Len;                   /* Length of file including header */
    public UInt32 CRC32;                 /* 32-bit CRC from flag_version to end of file */
    public UInt16 Flag;                  /* flags */
    public UInt16 Version;               /* version */
    
    public UInt32 LzmaLoaderOffset;
    public UInt32 KernelOffset;
    public UInt32 RootFSOffset;
    public UInt32 BinHeaderOffset;       /* Only valid for V2 */
};

void Main()
{
    PrintTRXHeader(@"ea9500.img");
}

public static void PrintTRXHeader(string path)
{
    using (FileStream fileStream = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read))
    {
        Console.WriteLine("\r\n************ {0} ***************\r\n", path);

        var header = fileStream.ReadStruct<TRXHeader>();
        Console.WriteLine("TRX Magic = 0x{0:X}", header.Magic);
        Console.WriteLine("File Length = {0}", header.Len);
        Console.WriteLine("CRC = 0x{0:X}", header.CRC32);
        Console.WriteLine("Flag = {0}", Convert.ToString(header.Flag, 2));
        Console.WriteLine("Version = {0}", header.Version);
        Console.WriteLine("LzmaLoaderOffset = 0x{0:X}", header.LzmaLoaderOffset);
        Console.WriteLine("KernelOffset = 0x{0:X}", header.KernelOffset);
        Console.WriteLine("RootFSOffset = 0x{0:X}", header.RootFSOffset);
        Console.WriteLine("BinHeaderOffset = 0x{0:X}\r\n", header.BinHeaderOffset);
    }
}

public static class StreamExtensions
{
    public static T ReadStruct<T>(this Stream stream) where T : struct
    {
        var sz = Marshal.SizeOf(typeof(T));
        var buffer = new byte[sz];
        stream.Read(buffer, 0, sz);
        var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        var structure = (T)Marshal.PtrToStructure(
            pinnedBuffer.AddrOfPinnedObject(), typeof(T));
        pinnedBuffer.Free();
        return structure;
    }
}

Advertisements

Written by Vivek Unune

February 1, 2017 at 8:34 pm

Posted in .NET, Hardware, openwrt

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: