Skip to content

Commit 8341909

Browse files
Motorola SREC IO plugin: fix multiple bugs (#5726)
* Fix multiple bugs * Add srec write test * Use PFMTxxx defines
1 parent 384bebe commit 8341909

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

librz/io/p/io_srec.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static void write_S3_record(FILE *fd, ut32 address, const ut8 *buffer, ut16 size
4444
fprintf(fd, "%02x", buffer[j]);
4545
}
4646

47-
fprintf(fd, "%02x\n", checksum);
47+
fprintf(fd, "%02" PFMT32x "\n", ~checksum & UT8_MAX);
4848
}
4949

5050
static st32 __write(RzIO *io, RzIODesc *fd, const ut8 *buf, size_t count) {
@@ -88,7 +88,7 @@ static st32 __write(RzIO *io, RzIODesc *fd, const ut8 *buf, size_t count) {
8888
address = sparse->from + offset;
8989
n_bytes = SREC_SIZE;
9090
if (offset + SREC_SIZE > size) {
91-
n_bytes = size - offset;
91+
n_bytes = size - offset + 1;
9292
}
9393
write_S3_record(out, address, sparse->data + offset, n_bytes);
9494
}
@@ -290,6 +290,12 @@ static bool srecord_parse(RzBuffer *buf, char *str) {
290290
RZ_LOG_ERROR("srec:parse(): invalid terminator hexadecimal address 16-bit at line %d\n", line);
291291
goto fail;
292292
}
293+
294+
eol = strchr(str + 1, 'S');
295+
if (eol) {
296+
*eol = 0;
297+
}
298+
293299
record_addr &= 0xffff;
294300
cksum = byte_count;
295301
cksum += record_addr & 0xff;
@@ -373,11 +379,6 @@ static bool srecord_parse(RzBuffer *buf, char *str) {
373379
}
374380
record_addr &= 0xffffffff;
375381

376-
eol = strchr(str + 1, 'S');
377-
if (eol) {
378-
*eol = 0;
379-
}
380-
381382
cksum = byte_count;
382383
cksum += record_addr & 0xff;
383384
cksum += (record_addr >> 8) & 0xff;
@@ -448,7 +449,6 @@ static bool srecord_parse(RzBuffer *buf, char *str) {
448449
RZ_LOG_ERROR("srec:parse(): checksum test failed (calculated %02x, but expected %02x) at line %d\n", cksum, cksum_expect, line);
449450
goto fail;
450451
}
451-
452452
str = strchr(str + 1, 'S');
453453
} while (str);
454454

test/db/io/srec

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ EXPECT=<<EOF
1111
EOF
1212
RUN
1313

14-
1514
NAME=wikipedia_example.srec
1615
FILE=srec://bins/srec/wikipedia_example.srec
1716
CMDS=<<EOF
@@ -27,7 +26,6 @@ EXPECT=<<EOF
2726
EOF
2827
RUN
2928

30-
3129
NAME=address16.srec
3230
FILE=srec://bins/srec/address16.srec
3331
CMDS=<<EOF
@@ -54,7 +52,6 @@ EXPECT=<<EOF
5452
EOF
5553
RUN
5654

57-
5855
NAME=address24.srec
5956
FILE=srec://bins/srec/address24.srec
6057
CMDS=<<EOF
@@ -66,7 +63,6 @@ EXPECT=<<EOF
6663
EOF
6764
RUN
6865

69-
7066
NAME=address32.srec
7167
FILE=srec://bins/srec/address32.srec
7268
CMDS=<<EOF
@@ -78,7 +74,6 @@ EXPECT=<<EOF
7874
EOF
7975
RUN
8076

81-
8277
NAME=arm1.bin.srec
8378
FILE=srec://bins/srec/arm1.bin.srec
8479
CMDS=<<EOF
@@ -160,3 +155,57 @@ NX true
160155
0x00085634 0x6394 ---------- 0x6394 0x0 ---- .strtab STRTAB
161156
EOF
162157
RUN
158+
159+
NAME=srec write
160+
FILE=--
161+
CMDS=<<EOF
162+
mkdir tmp
163+
cp bins/srec/address16.srec ./tmp/test-write.srec
164+
o+ srec://./tmp/test-write.srec
165+
x 272
166+
w hello
167+
o--
168+
o srec://./tmp/test-write.srec
169+
x 272
170+
o--
171+
rm ./tmp/test-write.srec
172+
EOF
173+
EXPECT=<<EOF
174+
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
175+
0x00000000 00ff 0004 0004 0004 0004 0004 0004 0004 ................
176+
0x00000010 0004 0004 0004 0004 0004 0004 0004 0004 ................
177+
0x00000020 0004 0004 0004 0004 0004 0004 0004 0004 ................
178+
0x00000030 0004 0004 0004 0004 0004 0004 0004 0004 ................
179+
0x00000040 0004 0004 0004 0004 0004 0004 0004 0004 ................
180+
0x00000050 0004 0004 0004 0004 0004 0004 0004 0004 ................
181+
0x00000060 0004 0004 0004 0004 0004 0004 0004 0004 ................
182+
0x00000070 0004 0004 0004 0004 0004 0004 0004 0004 ................
183+
0x00000080 0004 0004 0004 0004 0004 0004 0004 0004 ................
184+
0x00000090 0004 0004 0004 0004 0004 0004 0004 0004 ................
185+
0x000000a0 0004 0004 0004 0004 0004 0004 0004 0004 ................
186+
0x000000b0 0004 0004 0004 0004 0004 0004 0004 0004 ................
187+
0x000000c0 0004 0004 0004 0004 0004 0004 0004 0004 ................
188+
0x000000d0 0004 0004 0004 0004 0004 0004 0004 0004 ................
189+
0x000000e0 0004 0004 0004 0004 0004 0004 0004 0004 ................
190+
0x000000f0 0004 0004 0004 0004 0004 0004 0004 0004 ................
191+
0x00000100 ffff ffff ffff ffff ffff ffff ffff ffff ................
192+
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
193+
0x00000000 6865 6c6c 6f04 0004 0004 0004 0004 0004 hello...........
194+
0x00000010 0004 0004 0004 0004 0004 0004 0004 0004 ................
195+
0x00000020 0004 0004 0004 0004 0004 0004 0004 0004 ................
196+
0x00000030 0004 0004 0004 0004 0004 0004 0004 0004 ................
197+
0x00000040 0004 0004 0004 0004 0004 0004 0004 0004 ................
198+
0x00000050 0004 0004 0004 0004 0004 0004 0004 0004 ................
199+
0x00000060 0004 0004 0004 0004 0004 0004 0004 0004 ................
200+
0x00000070 0004 0004 0004 0004 0004 0004 0004 0004 ................
201+
0x00000080 0004 0004 0004 0004 0004 0004 0004 0004 ................
202+
0x00000090 0004 0004 0004 0004 0004 0004 0004 0004 ................
203+
0x000000a0 0004 0004 0004 0004 0004 0004 0004 0004 ................
204+
0x000000b0 0004 0004 0004 0004 0004 0004 0004 0004 ................
205+
0x000000c0 0004 0004 0004 0004 0004 0004 0004 0004 ................
206+
0x000000d0 0004 0004 0004 0004 0004 0004 0004 0004 ................
207+
0x000000e0 0004 0004 0004 0004 0004 0004 0004 0004 ................
208+
0x000000f0 0004 0004 0004 0004 0004 0004 0004 0004 ................
209+
0x00000100 ffff ffff ffff ffff ffff ffff ffff ffff ................
210+
EOF
211+
RUN

0 commit comments

Comments
 (0)