Skip to content

Commit a710023

Browse files
AsgharSoAsgharSo
authored andcommitted
added read and write pit
1 parent 1de01b5 commit a710023

15 files changed

Lines changed: 1239 additions & 32 deletions

OdinSharpLib/Cmd.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using OdinSharpLib.Port;
2+
using OdinSharpLib.structs;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace OdinSharpLib
10+
{
11+
public class Cmd
12+
{
13+
public byte[] BufferRead = new byte[8];
14+
15+
private long GetVariant(byte[] responseBuff)
16+
{
17+
return (BitConverter.ToInt32(responseBuff, 4) & 0xFFFF0000L) >> 16;
18+
}
19+
20+
private byte[] GetCmdBuff(SamsungLokeCommand loke)
21+
{
22+
byte[] array = new byte[1024];
23+
Array.Copy(BitConverter.GetBytes(loke.Cmd), 0, array, 0, 4);
24+
Array.Copy(BitConverter.GetBytes(loke.SeqCmd), 0, array, 4, 4);
25+
if (loke.Cmd == 100)
26+
{
27+
Array.Copy(BitConverter.GetBytes(loke.BinaryType), 0, array, 8, 8);
28+
}
29+
else
30+
{
31+
Array.Copy(BitConverter.GetBytes((int)loke.BinaryType), 0, array, 8, 4);
32+
Array.Copy(BitConverter.GetBytes(loke.SizeWritten), 0, array, 12, 4);
33+
}
34+
Array.Copy(BitConverter.GetBytes(loke.Unknown), 0, array, 16, 4);
35+
Array.Copy(BitConverter.GetBytes(loke.DeviceId), 0, array, 20, 4);
36+
Array.Copy(BitConverter.GetBytes(loke.Identifier), 0, array, 24, 4);
37+
Array.Copy(BitConverter.GetBytes(loke.SessionEnd), 0, array, 28, 4);
38+
Array.Copy(BitConverter.GetBytes(loke.EfsClear), 0, array, 32, 4);
39+
Array.Copy(BitConverter.GetBytes(loke.BootUpdate), 0, array, 36, 4);
40+
return array;
41+
}
42+
public async Task<bool> LOKE_SendCMD(device device,SamsungLokeCommand Cmd, bool readresp = true)
43+
{
44+
byte[] cmdBuff = GetCmdBuff(Cmd);
45+
await device.WritePort(cmdBuff, cmdBuff.Length);
46+
Array.Clear(BufferRead, 0, 8);
47+
if (!readresp)
48+
{
49+
return true;
50+
}
51+
var num = device.Port.Read(BufferRead, 0, 8);
52+
if (num == 8 && this.BufferRead[0] != byte.MaxValue)
53+
{
54+
return true;
55+
}
56+
throw new Exception("Invalid LOKE response: 0xFF");
57+
}
58+
59+
public async Task<bool> LOKE_Initialize(device device, long totalFileSize)
60+
{
61+
SamsungLokeCommand command = new SamsungLokeCommand(0x64, 0, 5L);
62+
if (await LOKE_SendCMD(device, command))
63+
{
64+
long variant = GetVariant(BufferRead);
65+
if (variant == 5L)
66+
{
67+
command = new SamsungLokeCommand(0x64, 12);
68+
await LOKE_SendCMD( device, command, false);
69+
}
70+
71+
if (totalFileSize != 0)
72+
{
73+
if (variant == 2L)
74+
{
75+
command = new SamsungLokeCommand(0x64, 2);
76+
await LOKE_SendCMD(device,command);
77+
}
78+
if (variant == 3L || variant == 4L || variant == 5L)
79+
{
80+
command = new SamsungLokeCommand(0x64, 5, 1048576L);
81+
await LOKE_SendCMD(device, command);
82+
}
83+
84+
command = new SamsungLokeCommand(0x64, 2, totalFileSize);
85+
await LOKE_SendCMD(device, command);
86+
if (variant == 4L)
87+
{
88+
int i = 0;
89+
while (i < 3)
90+
{
91+
command = new SamsungLokeCommand(0x69, i);
92+
await LOKE_SendCMD(device, command);
93+
int num = i + 1;
94+
i = num;
95+
}
96+
}
97+
98+
}
99+
100+
}
101+
return true;
102+
}
103+
104+
}
105+
}

OdinSharpLib/Odin.cs

Lines changed: 209 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using OdinSharpLib.Port;
1+
using OdinSharpLib.Pit;
2+
using OdinSharpLib.Port;
23
using OdinSharpLib.structs;
34
using OdinSharpLib.util;
45
using System;
56
using System.Collections.Generic;
6-
using System.IO.Ports;
7+
using System.Diagnostics;
8+
using System.IO;
79
using System.Linq;
8-
using System.Text;
910
using System.Text.RegularExpressions;
1011
using System.Threading.Tasks;
1112
using static OdinSharpLib.util.utils;
@@ -14,22 +15,19 @@ namespace OdinSharpLib
1415
{
1516
public class Odin
1617
{
17-
18-
/// <summary>
19-
/// connected device
20-
/// </summary>
18+
public Cmd cmd = new Cmd();
2119
public device Device = new device();
20+
public Tar tar = new Tar();
21+
public PITData PitTool = new PITData();
2222

23-
/// <summary>
24-
/// delegate log
25-
/// </summary>
2623
public event LogDelegate Log;
2724

2825
/// <summary>
2926
/// Finding samsung devices download mode port
3027
/// </summary>
3128
/// <returns>serialport information
3229
public async Task<ItypePort> FindDownloadModePort() => await PortComm.FindDownloadModePort();
30+
public async Task<bool> LOKE_Initialize(long totalFileSize) => await cmd.LOKE_Initialize(this.Device, totalFileSize);
3331

3432

3533
/// <summary>
@@ -191,5 +189,206 @@ public void StopOperations()
191189
{
192190
utils.Stop = true;
193191
}
192+
193+
194+
195+
public async Task<bool> PDAToNormal()
196+
{
197+
try
198+
{
199+
SamsungLokeCommand cmd2 = new SamsungLokeCommand(103);
200+
await cmd.LOKE_SendCMD(this.Device,cmd2);
201+
cmd2.SeqCmd = 1;
202+
await cmd.LOKE_SendCMD(this.Device, cmd2);
203+
}
204+
catch { }
205+
var watch = new Stopwatch();
206+
try
207+
{
208+
watch.Start();
209+
do
210+
{
211+
if (!Device.Port.IsOpen)
212+
{
213+
return true;
214+
}
215+
} while (watch.ElapsedMilliseconds < 60000);
216+
}
217+
finally
218+
{
219+
watch.Stop();
220+
}
221+
return !Device.Port.IsOpen;
222+
}
223+
224+
public async Task<Result> Write_Pit(byte[] pit)
225+
{
226+
var Result = new Result { error = "Failed RePartition" };
227+
try
228+
{
229+
SamsungLokeCommand command = new SamsungLokeCommand(101);
230+
if (await cmd.LOKE_SendCMD(Device, command))
231+
{
232+
command = new SamsungLokeCommand(101, 2, pit.Length);
233+
if (await cmd.LOKE_SendCMD(Device, command))
234+
{
235+
await Device.WritePort(pit, pit.Length);
236+
cmd.BufferRead = await Device.ReadPort(8);
237+
if (cmd.BufferRead[0] != byte.MaxValue)
238+
{
239+
command = new SamsungLokeCommand(101, 3, pit.Length);
240+
await cmd.LOKE_SendCMD(Device, command);
241+
Result.status = true;
242+
}
243+
244+
}
245+
}
246+
}
247+
catch (Exception ex)
248+
{
249+
Result.error = ex.Message;
250+
}
251+
252+
return Result;
253+
}
254+
255+
public async Task<Result> Write_Pit(string File)
256+
{
257+
var Result = new Result { error = "Failed RePartition" };
258+
try
259+
{
260+
byte[] pit = new byte[] { };
261+
var Extension = Path.GetExtension(File).ToLower();
262+
if (Extension == ".tar" || Extension == ".md5")
263+
{
264+
265+
var TarInfo = tar.TarInformation(File);
266+
var pitname = TarInfo.ToList().Find(item => item.Filename.ToLower().EndsWith(".pit"));
267+
if (pitname != null)
268+
{
269+
pit = await tar.ExtractFileFromTar(File, pitname.Filename);
270+
}
271+
else
272+
{
273+
Result.error = "Cannot Find Pit In Tar";
274+
return Result;
275+
}
276+
}
277+
else if (Extension == ".pit")
278+
{
279+
pit = System.IO.File.ReadAllBytes(File);
280+
}
281+
else
282+
{
283+
Result.error = "Pit Is Invalid";
284+
return Result;
285+
}
286+
287+
SamsungLokeCommand command = new SamsungLokeCommand(101);
288+
if (await cmd.LOKE_SendCMD(Device, command))
289+
{
290+
command = new SamsungLokeCommand(101, 2, pit.Length);
291+
if (await cmd.LOKE_SendCMD(Device, command))
292+
{
293+
await Device.WritePort(pit, pit.Length);
294+
cmd.BufferRead = await Device.ReadPort(8);
295+
if (cmd.BufferRead[0] != byte.MaxValue)
296+
{
297+
command = new SamsungLokeCommand(101, 3, pit.Length);
298+
await cmd.LOKE_SendCMD(Device, command);
299+
Result.status = true;
300+
}
301+
302+
}
303+
}
304+
305+
}
306+
catch (Exception ex)
307+
{
308+
Result.error = ex.Message;
309+
}
310+
311+
return Result;
312+
}
313+
314+
public async Task<ReadPitResult> Read_Pit()
315+
{
316+
var Result = new ReadPitResult();
317+
try
318+
{
319+
using (var PitStream = new MemoryStream())
320+
{
321+
byte[] array = new byte[1025];
322+
byte[] array2 = new byte[4097];
323+
string currentDirectory = Environment.CurrentDirectory;
324+
var cmd = new SamsungLokeCommand(0x65, 1);
325+
if (await this.cmd.LOKE_SendCMD(Device,cmd))
326+
{
327+
// 4 to 8
328+
var pitSize = new byte[] { this.cmd.BufferRead[7], this.cmd.BufferRead[6], this.cmd.BufferRead[5], this.cmd.BufferRead[4] };
329+
long num = (long)Convert.ToInt32(BitConverter.ToString(pitSize).Replace("-",""), 16);
330+
int num2 = (int)(unchecked((double)num / 500.0 + 1.0));
331+
int num3 = 0;
332+
int num4 = num2 - 1;
333+
for (int i = 0; i <= num4; i++)
334+
{
335+
int num5;
336+
if (num - unchecked((long)num3) >= 500L)
337+
{
338+
num5 = 500;
339+
}
340+
else
341+
{
342+
num5 = (int)(num - unchecked((long)num3));
343+
}
344+
int num6 = 0;
345+
do
346+
{
347+
array[num6] = 0;
348+
num6++;
349+
}
350+
while (num6 <= 1023);
351+
num6 = 0;
352+
do
353+
{
354+
array2[num6] = 0;
355+
num6++;
356+
}
357+
while (num6 <= 4096);
358+
array[0] = 101;
359+
array[1] = 0;
360+
array[2] = 0;
361+
array[3] = 0;
362+
array[4] = 2;
363+
array[5] = 0;
364+
array[6] = 0;
365+
array[7] = 0;
366+
array[8] = (byte)(i % 256);
367+
array[9] = (byte)(i / 256.0);
368+
array[10] = (byte)(i / 65536.0);
369+
array[11] = (byte)(i / 16777216.0);
370+
num3 += num5;
371+
await this.Device.WritePort(array, 1024);
372+
int num7 = this.Device.Port.Read(array2, 0, num5);
373+
PitStream.Write(array2, 0, num5);
374+
}
375+
}
376+
byte[] sData = PitStream.ToArray();
377+
Result.Result = true;
378+
Result.data = sData;
379+
if (PitTool.UNPACK_PIT(sData))
380+
{
381+
Result.Pit = PitTool.xPIT_Entry.ToList();
382+
}
383+
}
384+
}
385+
catch (Exception e)
386+
{
387+
Result.error = e.Message;
388+
}
389+
return Result;
390+
}
391+
392+
194393
}
195394
}

OdinSharpLib/OdinSharpLib.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34+
<Reference Include="Microsoft.VisualBasic" />
3435
<Reference Include="System" />
3536
<Reference Include="System.Core" />
3637
<Reference Include="System.Management" />
@@ -42,12 +43,20 @@
4243
<Reference Include="System.Xml" />
4344
</ItemGroup>
4445
<ItemGroup>
46+
<Compile Include="Cmd.cs" />
4547
<Compile Include="Odin.cs" />
48+
<Compile Include="Pit\PITData.cs" />
49+
<Compile Include="Pit\TPIT_Entry.cs" />
4650
<Compile Include="Port\device.cs" />
4751
<Compile Include="Port\PortComm.cs" />
4852
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="structs\FileFlash.cs" />
4954
<Compile Include="structs\ItypePort.cs" />
5055
<Compile Include="structs\PortType.cs" />
56+
<Compile Include="structs\ReadPitResult.cs" />
57+
<Compile Include="structs\Result.cs" />
58+
<Compile Include="structs\SamsungLokeCommand.cs" />
59+
<Compile Include="util\Tar.cs" />
5160
<Compile Include="util\utils.cs" />
5261
</ItemGroup>
5362
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)