1- using OdinSharpLib . Port ;
1+ using OdinSharpLib . Pit ;
2+ using OdinSharpLib . Port ;
23using OdinSharpLib . structs ;
34using OdinSharpLib . util ;
45using System ;
56using System . Collections . Generic ;
6- using System . IO . Ports ;
7+ using System . Diagnostics ;
8+ using System . IO ;
79using System . Linq ;
8- using System . Text ;
910using System . Text . RegularExpressions ;
1011using System . Threading . Tasks ;
1112using 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}
0 commit comments