-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.cs
More file actions
127 lines (116 loc) · 3.5 KB
/
Copy pathPartition.cs
File metadata and controls
127 lines (116 loc) · 3.5 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;
using MtkClient.Library.Config;
using MtkClient.Library.Connection;
using MtkClient.Library.Partitions;
using MtkClient.Library.Utils;
namespace MtkClient.Library
{
public class PartitionManager : LogBase
{
private readonly MtkConfig _config;
public Gpt GptTable { get; set; }
public Mbr MbrTable { get; set; }
public Pmt PmtTable { get; set; }
public Bpi BpiTable { get; set; }
public PartitionManager(MtkConfig config, LogLevel logLevel = LogLevel.Info)
{
InitLogger(logLevel);
_config = config;
}
public bool ParsePartitionTable(byte[] data, string type = "GPT")
{
if (data == null || data.Length < 512)
{
Error("Insufficient data for partition table");
return false;
}
switch (type.ToUpper())
{
case "GPT":
return ParseGpt(data);
case "MBR":
return ParseMbr(data);
case "PMT":
return ParsePmt(data);
case "BPI":
return ParseBpi(data);
default:
Error($"Unknown partition type: {type}");
return false;
}
}
private bool ParseGpt(byte[] data)
{
try
{
Func<long, int, byte[]> readFunc = (off, len) =>
{
if (off + len > data.Length) return null;
var result = new byte[len];
Array.Copy(data, (int)off, result, 0, len);
return result;
};
GptTable = new Gpt(readFunc, data.Length);
return GptTable.Partitions.Count > 0;
}
catch (Exception ex)
{
Error($"GPT parse failed: {ex.Message}");
return false;
}
}
private bool ParseMbr(byte[] data)
{
try
{
MbrTable = new Mbr(data, _config.LogLevel);
return MbrTable.IsValid;
}
catch (Exception ex)
{
Error($"MBR parse failed: {ex.Message}");
return false;
}
}
private bool ParsePmt(byte[] data)
{
try
{
PmtTable = new Pmt(data, _config.LogLevel);
return PmtTable.Partitions.Count > 0;
}
catch (Exception ex)
{
Error($"PMT parse failed: {ex.Message}");
return false;
}
}
private bool ParseBpi(byte[] data)
{
try
{
BpiTable = new Bpi(data, _config.LogLevel);
return BpiTable.Partitions.Count > 0;
}
catch (Exception ex)
{
Error($"BPI parse failed: {ex.Message}");
return false;
}
}
public GptPartition FindPartition(string name)
{
return GptTable?.FindPartition(name);
}
public string PrintPartitions()
{
if (GptTable != null)
return GptTable.PrintGpt();
if (MbrTable != null)
return MbrTable.PrintMbr();
return "No partition table loaded";
}
}
}