-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEbr.cs
More file actions
66 lines (53 loc) · 2.02 KB
/
Copy pathEbr.cs
File metadata and controls
66 lines (53 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using MtkClient.Library.Utils;
namespace MtkClient.Library
{
public class EbrPartition
{
public byte Status { get; set; }
public byte Type { get; set; }
public uint FirstLba { get; set; }
public uint SectorCount { get; set; }
public uint NextEbrLba { get; set; }
public EbrPartition(byte[] data, int offset = 0)
{
if (data.Length < offset + 16) return;
Status = data[offset];
Type = data[offset + 4];
FirstLba = BitConverter.ToUInt32(data, offset + 8);
SectorCount = BitConverter.ToUInt32(data, offset + 12);
}
}
public class Ebr : LogBase
{
public List<EbrPartition> Partitions { get; } = new List<EbrPartition>();
public const ushort EBR_SIGNATURE = 0xAA55;
public Ebr(byte[] data, uint startLba = 0, LogLevel logLevel = LogLevel.Info)
{
InitLogger(logLevel);
Parse(data, startLba);
}
private void Parse(byte[] data, uint startLba)
{
if (data == null || data.Length < 512) return;
uint currentLba = startLba;
while (true)
{
int offset = (int)(currentLba * 512);
if (offset + 512 > data.Length) break;
var sig = BitConverter.ToUInt16(data, offset + 510);
if (sig != EBR_SIGNATURE) break;
var partition = new EbrPartition(data, offset + 446);
if (partition.Type == 0) break;
partition.FirstLba += currentLba;
Partitions.Add(partition);
var nextEntry = new EbrPartition(data, offset + 462);
if (nextEntry.Type == 0 || nextEntry.FirstLba == 0) break;
nextEntry.NextEbrLba = startLba + nextEntry.FirstLba;
currentLba = nextEntry.NextEbrLba;
}
Info($"EBR parsed: {Partitions.Count} logical partitions");
}
}
}