/// <summary> /// 由固件文件路径生成固件对象的构造函数 /// </summary> /// <param name="path">固件文件路径</param> public tFirmware(string path) { int bufSize = 300 * 1024; FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufSize); BinaryReader reader = new BinaryReader(stream); // 解析固件文件头信息 byte[] data = reader.ReadBytes(FW_HEADER_SIZE); FwDesc.u32MagicWord = (UInt32)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3])); FwDesc.verFw.u8Main = data[4]; FwDesc.verFw.u8Sub = data[5]; FwDesc.verFw.u8Rev = data[6]; FwDesc.verFw.u8Build = data[7]; FwDesc.u32Size = (UInt32)((data[8] << 24) | (data[9] << 16) | (data[10] << 8) | (data[11])); //FwDesc.u16BlockNum != (UInt16)((data[12] << 8) | data[13]); FwDesc.u16BlockNum = (UInt16)(FwDesc.u32Size / FwDesc.u8BlockSize); if (FwDesc.u32Size % FwDesc.u8BlockSize != 0) { ++FwDesc.u16BlockNum; } //FwDesc.u8BlockSize = data[14]; FwDesc.u32MagicWord = (UInt32)((data[15] << 24) | (data[16] << 16) | (data[17] << 8) | (data[18])); // 解析固件数据 DicFwBlockDat = new Dictionary <ushort, tFwBlockDat>(FwDesc.u16BlockNum); data = reader.ReadBytes((int)FwDesc.u32Size); for (int i = 0; i < FwDesc.u16BlockNum; i++) { tFwBlockDat newFwBlockDat = new tFwBlockDat(); if (i < FwDesc.u16BlockNum - 1) { newFwBlockDat.size = FwDesc.u8BlockSize; } else { newFwBlockDat.size = (UInt16)(FwDesc.u32Size - (FwDesc.u16BlockNum - 1) * FwDesc.u8BlockSize); } newFwBlockDat.data = new byte[newFwBlockDat.size]; Array.Copy(data, i * FwDesc.u8BlockSize, newFwBlockDat.data, 0, newFwBlockDat.size); DicFwBlockDat.Add((UInt16)i, newFwBlockDat); } }
/// <summary> /// 由固件文件数据生成固件对象的构造函数 /// </summary> /// <param name="fileDat">固件文件数据</param> public tFirmware(byte[] fileDat) { if (fileDat == null || fileDat.Length <= FW_HEADER_SIZE) { throw new Exception("Firmware data error"); } FwDesc.u32MagicWord = (UInt32)((fileDat[0] << 24) | (fileDat[1] << 16) | (fileDat[2] << 8) | (fileDat[3])); FwDesc.verFw.u8Main = fileDat[4]; FwDesc.verFw.u8Sub = fileDat[5]; FwDesc.verFw.u8Rev = fileDat[6]; FwDesc.verFw.u8Build = fileDat[7]; FwDesc.u32Size = (UInt32)((fileDat[8] << 24) | (fileDat[9] << 16) | (fileDat[10] << 8) | (fileDat[11])); //FwDesc.u16BlockNum != (UInt16)((data[12] << 8) | data[13]); FwDesc.u16BlockNum = (UInt16)(FwDesc.u32Size / FwDesc.u8BlockSize); if (FwDesc.u32Size % FwDesc.u8BlockSize != 0) { ++FwDesc.u16BlockNum; } //FwDesc.u8BlockSize = data[14]; FwDesc.u32EntryPoint = (UInt32)((fileDat[15] << 24) | (fileDat[16] << 16) | (fileDat[17] << 8) | (fileDat[18])); // 解析固件数据 DicFwBlockDat = new Dictionary <ushort, tFwBlockDat>(FwDesc.u16BlockNum); for (int i = 0; i < FwDesc.u16BlockNum; i++) { tFwBlockDat newFwBlockDat = new tFwBlockDat(); if (i < FwDesc.u16BlockNum - 1) { newFwBlockDat.size = FwDesc.u8BlockSize; } else { newFwBlockDat.size = (UInt16)(FwDesc.u32Size - (FwDesc.u16BlockNum - 1) * FwDesc.u8BlockSize); } newFwBlockDat.data = new byte[newFwBlockDat.size]; Array.Copy(fileDat, 19 + i * FwDesc.u8BlockSize, newFwBlockDat.data, 0, newFwBlockDat.size); DicFwBlockDat.Add((UInt16)i, newFwBlockDat); } }
/// <summary> /// 复制构造函数 /// </summary> /// <param name="fw">复制的对象</param> public tFirmware(tFirmware fw) { FwDesc.u32MagicWord = fw.FwDesc.u32MagicWord; FwDesc.verFw.u8Main = fw.FwDesc.verFw.u8Main; FwDesc.verFw.u8Sub = fw.FwDesc.verFw.u8Sub; FwDesc.verFw.u8Rev = fw.FwDesc.verFw.u8Rev; FwDesc.verFw.u8Build = fw.FwDesc.verFw.u8Build; FwDesc.u32Size = fw.FwDesc.u32Size; FwDesc.u8BlockSize = fw.FwDesc.u8BlockSize; FwDesc.u16BlockNum = fw.FwDesc.u16BlockNum; FwDesc.u32EntryPoint = fw.FwDesc.u32EntryPoint; DicFwBlockDat = new Dictionary <ushort, tFwBlockDat>(); for (ushort i = 0; i < fw.DicFwBlockDat.Count; i++) { tFwBlockDat fwBlkDat = new tFwBlockDat(); fwBlkDat.size = fw.DicFwBlockDat[i].size; fwBlkDat.data = new byte[fwBlkDat.size]; Array.Copy(fw.DicFwBlockDat[i].data, fwBlkDat.data, fwBlkDat.size); DicFwBlockDat.Add(i, fwBlkDat); } }