示例#1
0
文件: firmware.cs 项目: junimp/TL866
        public void Open(string UpdateDat_Path)
        {
            IsValid = false;
            FileStream fsin = null;

            try
            {
                fsin = File.OpenRead(UpdateDat_Path);
            }
            catch
            {
                if (fsin != null)
                {
                    fsin.Close();
                }
                throw new Exception("Error opening file " + UpdateDat_Path);
            }
            if (fsin.Length != UPDATE_DAT_SIZE)
            {
                fsin.Close();
                throw new Exception(UpdateDat_Path + "\nFile size error!");
            }
            byte[] inbuffer = new byte[fsin.Length + 1];
            fsin.Read(inbuffer, 0, (int)fsin.Length);
            fsin.Close();

            m_eraseA  = inbuffer[9];
            m_eraseCS = inbuffer[17];
            Version   = inbuffer[0];

            //Decrypt firmwares (stage 1)
            m_firmwareA  = new byte[ENCRYPTED_FIRMWARE_SIZE];
            m_firmwareCS = new byte[ENCRYPTED_FIRMWARE_SIZE];
            for (uint i = 0; i < ENCRYPTED_FIRMWARE_SIZE; i++)
            {
                m_firmwareA[i] = (byte)(inbuffer[0xA1C + i] ^
                                        inbuffer[
                                            0x118 +
                                            ((BitConverter.ToInt32(inbuffer, 0x14) + i) & 0x3FF)] ^
                                        inbuffer[0x18 + ((i / 80) & 0xFF)]);
                m_firmwareCS[i] = (byte)(inbuffer[0x2671C + i] ^
                                         inbuffer[
                                             0x61C +
                                             ((BitConverter.ToInt32(inbuffer, 0x518) + i) & 0x3FF)] ^
                                         inbuffer[0x51C + ((i / 80) & 0xFF)]);
            }

            //Check if decryption was O.K.
            CRC32 crc32 = new CRC32();
            uint  crca  = ~crc32.GetCRC32(m_firmwareA, 0xFFFFFFFF);
            uint  crccs = ~crc32.GetCRC32(m_firmwareCS, 0xFFFFFFFF);

            if (crca != BitConverter.ToUInt32(inbuffer, 4) || crccs != BitConverter.ToUInt32(inbuffer, 12))
            {
                throw new Exception(UpdateDat_Path + "\nData CRC error!");
            }
            //Check if decrypted firmware signatures are O.K.
            if (BitConverter.ToUInt32(GetUnencryptedFirmware((int)FIRMWARE_TYPE.FIRMWARE_A),
                                      FIRMWARE_SIGNATURE_OFFSET) != FIRMWARE_SIGNATURE ||
                BitConverter.ToUInt32(GetUnencryptedFirmware((int)FIRMWARE_TYPE.FIRMWARE_CS),
                                      FIRMWARE_SIGNATURE_OFFSET) != FIRMWARE_SIGNATURE)
            {
                throw new Exception("Firmware decryption error!");
            }
            IsValid = true;
        }