Example #1
0
        public FirmwareBinary(byte[] data, string buildName)
        {
            /* stream is now owned/managed by this class */
            _stream   = new MemoryStream(data);
            BuildName = buildName;

            byte[] bArr = new byte[4];
            try
            {
                if (_stream.Read(bArr) != -1)
                {
                    _magic = (((long)bArr[2] & 255) << 16) | (((long)bArr[3] & 255) << 24) |
                             (((long)bArr[1] & 255) << 8) | (((long)bArr[0]) & 255);
                    if (_magic != FOTA_BIN_MAGIC)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.InvalidMagic,
                                                         Loc.Resolve("fw_fail_no_magic"));
                    }
                }

                if (_stream.Read(bArr) != -1)
                {
                    TotalSize = (((long)bArr[2] & 255) << 16) | (((long)bArr[3] & 255) << 24) |
                                (((long)bArr[1] & 255) << 8) | (((long)bArr[0]) & 255);
                    if (TotalSize == 0)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.SizeZero,
                                                         Loc.Resolve("fw_fail_size_null"));
                    }
                }

                if (_stream.Read(bArr) != -1)
                {
                    SegmentsCount = (((long)bArr[1] & 255) << 8) | (((long)bArr[3] & 255) << 24) |
                                    (((long)bArr[2] & 255) << 16) | (((long)bArr[0]) & 255);
                    if (SegmentsCount == 0)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.NoSegmentsFound,
                                                         Loc.Resolve("fw_fail_no_segments"));
                    }
                }

                Segments = new FirmwareSegment[SegmentsCount];
                for (var i = 0; i < SegmentsCount; i++)
                {
                    Segments[i] = new FirmwareSegment(i, SegmentsCount, _stream);
                }

                _stream.Seek(-4, SeekOrigin.End);
                _stream.Read(bArr, 0, 4);
                Crc32 = BitConverter.ToInt32(bArr);
            }
            catch (Exception ex) when(!(ex is FirmwareParseException))
            {
                _stream.Close();

                Log.Error($"FirmwareBinary: Failed to decode binary: {ex}");
                throw new FirmwareParseException(FirmwareParseException.ErrorCodes.Unknown,
                                                 $"{Loc.Resolve("fw_fail_unknown")}\n{ex}");
            }
        }
Example #2
0
        public FirmwareBinary(byte[] data, string buildName)
        {
            /* stream is now owned/managed by this class */
            _stream   = new MemoryStream(data);
            BuildName = buildName;

            byte[] bArr = new byte[4];
            try
            {
                if (_stream.Read(bArr) != -1)
                {
                    _magic = (((long)bArr[2] & 255) << 16) | (((long)bArr[3] & 255) << 24) |
                             (((long)bArr[1] & 255) << 8) | (((long)bArr[0]) & 255);
                    if (_magic != FOTA_BIN_MAGIC && _magic != FOTA_BIN_MAGIC_COMBINATION)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.InvalidMagic,
                                                         Loc.Resolve("fw_fail_no_magic"));
                    }

                    if (_magic == FOTA_BIN_MAGIC_COMBINATION)
                    {
                        // Notify bug tracker about this event and submit firmware build info
                        SentrySdk.CaptureMessage($"BCOM-Firmware discovered. Build: {buildName}, Content: {Convert.ToBase64String(data)}", SentryLevel.Info);

                        Log.Fatal("FirmwareBinary: Parsing internal debug firmware. " +
                                  "This is unsupported by this application as these binaries are not meant for retail devices.");
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.InvalidMagic,
                                                         Loc.Resolve("fw_fail_no_magic"));
                    }
                }

                if (_stream.Read(bArr) != -1)
                {
                    TotalSize = (((long)bArr[2] & 255) << 16) | (((long)bArr[3] & 255) << 24) |
                                (((long)bArr[1] & 255) << 8) | (((long)bArr[0]) & 255);
                    if (TotalSize == 0)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.SizeZero,
                                                         Loc.Resolve("fw_fail_size_null"));
                    }
                }

                if (_stream.Read(bArr) != -1)
                {
                    SegmentsCount = (((long)bArr[1] & 255) << 8) | (((long)bArr[3] & 255) << 24) |
                                    (((long)bArr[2] & 255) << 16) | (((long)bArr[0]) & 255);
                    if (SegmentsCount == 0)
                    {
                        _stream.Close();
                        throw new FirmwareParseException(FirmwareParseException.ErrorCodes.NoSegmentsFound,
                                                         Loc.Resolve("fw_fail_no_segments"));
                    }
                }

                Segments = new FirmwareSegment[SegmentsCount];
                for (var i = 0; i < SegmentsCount; i++)
                {
                    Segments[i] = new FirmwareSegment(i, SegmentsCount, _stream);
                }

                _stream.Seek(-4, SeekOrigin.End);
                _stream.Read(bArr, 0, 4);
                Crc32 = BitConverter.ToInt32(bArr);
            }
            catch (Exception ex) when(!(ex is FirmwareParseException))
            {
                _stream.Close();

                Log.Error($"FirmwareBinary: Failed to decode binary: {ex}");
                throw new FirmwareParseException(FirmwareParseException.ErrorCodes.Unknown,
                                                 $"{Loc.Resolve("fw_fail_unknown")}\n{ex}");
            }
        }