forked from hn/ginlong-solis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-alios-ota-firmware.pl
executable file
·131 lines (119 loc) · 3.52 KB
/
decode-alios-ota-firmware.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl
#
# decode-alios-ota-firmware.pl -- Decode AliOs OTA firmware (application) file
#
# (C) 2023 Hajo Noerenberg
#
# Usage: decode-alios-otafirmware.pl solis-s3-app-1012F_ota.bin
#
# http://www.noerenberg.de/
# https://github.com/hn/ginlong-solis
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3.0 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
#
use strict;
use Digest::MD5 qw(md5);
my $signature = "81958711";
my $magic = "\xef\xef\xef\xef"; # OTA_BIN_TYPE_MAGIC_SINGLE
my $f = $ARGV[0];
open( IF, "<$f" ) || die( "Unable to open input file '$f': " . $! );
binmode(IF);
my $buf;
my $md5buf;
my $fsize = -s $f;
sub readbytes {
my ( $len, $desc ) = @_;
printf( " 0x%08x:", tell(IF) );
read( IF, $buf, $len ) == $len || die;
$md5buf .= $buf;
printf( " %-16s = ", $desc );
}
for ( my $i = 1 ; $i <= 2 ; $i++ ) {
printf( "# Segment %s\n", ( $i == 1 ) ? ".text" : ".data" );
readbytes( 8, "Signature" );
printf( "0x%s ", unpack( "H*", $buf ) );
if ( $buf eq $signature ) {
print "(OK)\n";
} else {
printf( "(INVALID, 0x%s)\n", unpack( "H*", $signature ) );
exit;
}
readbytes( 4, "Code length" );
my $len = unpack( "V", $buf );
printf( "%d\n", $len );
readbytes( 4, "Address" );
my $addr = unpack( "V", $buf );
printf( "0x%08x ", $addr );
if ( $addr >= hex("0x10000000") ) {
print "(RAM)\n";
} else {
print "(FLASH XIP)\n";
}
readbytes( 16, "Reserved" );
printf( "0x%s\n", unpack( "H*", $buf ) );
readbytes( $len, "Code" );
print "(byte code)\n\n";
}
print "# Segments checksum\n"; # AliOS-Things/platform/mcu/rtl8710bn/tools/checksum
my $fletcher = 0;
for ( my $i = 0 ; $i < length($md5buf) ; $i++ ) {
$fletcher = ( $fletcher + ord( substr( $md5buf, $i, 1 ) ) ) % 4294967296;
}
readbytes( 4, "Checksum" );
printf( "0x%s ", unpack( "H*", $buf ) );
if ( unpack( "V", $buf ) eq $fletcher ) {
print "(OK)\n";
} else {
printf( "(INVALID, 0x%x)\n", unpack( "V", pack( "N", $fletcher ) ) );
exit;
}
print "\n# OTA trailer\n"; # AliOS-Things/build/scripts/ota_gen_md5_bin.py , middleware/uagent/ota/ota_core/ble/ota_breeze.h
my $md5sum = md5($md5buf);
my $loc = tell(IF);
readbytes( 4, "Magic" );
printf( "0x%s ", unpack( "H*", $buf ) );
if ( $buf eq $magic ) {
print "(OK)\n";
} else {
printf( "(INVALID, 0x%x)\n", unpack( "H*", $magic ) );
exit;
}
readbytes( 4, "Size" );
my $osize = unpack( "V", $buf );
printf( "%d ", $osize );
if ( $osize == $loc ) {
print "(OK)\n";
} else {
printf( "(INVALID, %d)\n", $loc );
exit;
}
readbytes( 16, "MD5 checksum" );
printf( "0x%s ", unpack( "H*", $buf ) );
if ( $buf eq $md5sum ) {
print "(OK)\n";
} else {
printf( "(INVALID, 0x%s)\n", unpack( "H*", $md5sum ) );
exit;
}
readbytes( 4, "Reserved" );
printf( "0x%s\n", unpack( "H*", $buf ) );
print "\n# End of data\n";
$loc = tell(IF);
printf( " 0x%08x: %-16s = 0x%x = %d ", $loc, "Filesize", $fsize, $fsize );
if ( $loc == $fsize ) {
print "(OK)\n";
} else {
printf( "(INVALID, 0x%x)\n", $loc );
exit;
}