11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Runtime . InteropServices ;
45
56namespace BK7231Flasher
67{
@@ -157,13 +158,16 @@ public static uint RotateLeft(uint value, int bits)
157158 {
158159 0x1F08 , // Common Beken bootloaders, 1.0.8, 1.0.13 etc.
159160 0x48 , // Tuya 1.0.1 N or 1.0.5 T
161+ //0x20F8, // Tuya_A9_Cam_SC-10024_V1.0.0_(VGA卡片机-schemaID-000003uanz)_keyph8vwhadwsx3a_u0kmecmr7j8roazz_NEO_SPI_1.0.4.bin
160162 } ;
161163
164+ internal static uint FAL_PART_MAGIC_WORD = 0x45503130 ;
165+
162166 public static byte [ ] UnCRC ( byte [ ] bytes )
163167 {
164168 var decrc = new List < byte > ( ) ;
165169 int i = 0 ;
166- for ( i = 0 ; i < bytes . Length - 34 ; i += 34 )
170+ for ( i = 0 ; i < bytes . Length ; i += 34 )
167171 {
168172 var packet = new byte [ 32 ] ;
169173 Array . Copy ( bytes , i , packet , 0 , 32 ) ;
@@ -183,21 +187,16 @@ public static byte[] CRCBeken(byte[] bytes)
183187 var crc = new List < byte > ( ) ;
184188 int i = 0 ;
185189 var packet = new byte [ 34 ] ;
186- for ( i = 0 ; i < bytes . Length - 32 ; i += 32 )
190+ for ( int j = 0 ; j < packet . Length ; j ++ )
191+ packet [ j ] = 0xFF ;
192+ for ( i = 0 ; i < bytes . Length ; i += 32 )
187193 {
188194 Array . Copy ( bytes , i , packet , 0 , 32 ) ;
189195 var computedcrc = CRC16 . Compute ( CRC16Type . CMS , packet , 0 , 32 ) ;
190196 packet [ 32 ] = ( byte ) ( computedcrc >> 8 ) ;
191197 packet [ 33 ] = ( byte ) computedcrc ;
192198 crc . AddRange ( packet ) ;
193199 }
194- for ( int j = 0 ; j < packet . Length ; j ++ )
195- packet [ j ] = 0xFF ;
196- Array . Copy ( bytes , i , packet , 0 , bytes . Length - i ) ;
197- var finalcrc = CRC16 . Compute ( CRC16Type . CMS , packet ) ;
198- packet [ 32 ] = ( byte ) ( finalcrc >> 8 ) ;
199- packet [ 33 ] = ( byte ) finalcrc ;
200- crc . AddRange ( packet ) ;
201200 return crc . ToArray ( ) ;
202201 }
203202
@@ -261,6 +260,25 @@ public static byte[] EncryptDecryptBekenFW(uint[] keys, uint[] data, uint startA
261260 }
262261 return U32ToU8 ( crypted . ToArray ( ) ) ;
263262 }
263+
264+ public static unsafe FALPartition64 BytesToFAL64 ( byte [ ] data )
265+ {
266+ fixed( byte * pdata = data )
267+ {
268+ return Marshal . PtrToStructure < FALPartition64 > ( ( IntPtr ) pdata ) ;
269+ }
270+ }
271+
272+ public static byte [ ] FAL64ToBytes ( FALPartition64 data )
273+ {
274+ var num = Marshal . SizeOf ( data ) ;
275+ var ptr = Marshal . AllocHGlobal ( num ) ;
276+ Marshal . StructureToPtr ( data , ptr , false ) ;
277+ var array = new byte [ num ] ;
278+ Marshal . Copy ( ptr , array , 0 , num ) ;
279+ Marshal . FreeHGlobal ( ptr ) ;
280+ return array ;
281+ }
264282 }
265283
266284 // vibe-coded
@@ -469,5 +487,18 @@ public uint EncryptU32(uint addr, uint data)
469487 uint pnout = pn32_v ^ ( ( pn15_v << 16 ) | ( pn16_v & 0xFFFF ) ) ^ random ;
470488 return data ^ pnout ;
471489 }
472- }
490+ } ;
491+
492+ [ StructLayout ( LayoutKind . Sequential , Pack = 1 , CharSet = CharSet . Ansi ) ]
493+ public struct FALPartition64
494+ {
495+ public uint magic_word ;
496+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 24 ) ]
497+ public string name ;
498+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 24 ) ]
499+ public string flash_name ;
500+ public uint offset ;
501+ public uint len ;
502+ public uint crc32 ;
503+ } ;
473504}
0 commit comments