sq5bpf / k5prog

Quansheng UV-K5 EEPROM programmer
GNU General Public License v3.0
164 stars 36 forks source link

write flash packet: data length field inconsistency #29

Open qrp73 opened 6 months ago

qrp73 commented 6 months ago

I'm analyze source code and found some strange inconsistency.

Your k5prog utility uses write flash packet with data length fields 0x00,0x01, see this line: https://github.com/sq5bpf/k5prog/blob/241ab18b61f6d8933fecf60643fe94322fbf4198/k5prog.c#L698

len=UVK5_FLASH_BLOCKSIZE=0x100;

writeflash[12]=len&0xff;
writeflash[13]=(len>>8)&0xff;

But uvtool flasher uses this packet with length in reversed order 0x01,0x00, see this line: https://github.com/whosmatt/uvmod/blob/72eecd083e50a7b2f12404d49ee157d3d200841b/js/tool_patcher.js#L178 https://github.com/egzumer/uvtools/blob/7148b01f11357cb28e74fff3a9591b12a656ea13/js/tool_patcher.js#L178

// the length is fixed to 0x100 bytes
    const length_msb = 0x01;
    const length_lsb = 0x00;

    return new Uint8Array([0x19, 0x5, 0xc, 0x1, <...>, length_msb, length_lsb, 0x0, 0x0, ...data]); 

Could you please help to understand what is going on here? And which version is correct? Thanks

sq5bpf commented 6 months ago

actually if you look in k5prog.c at the comment on line 679:

/* 0x19 0x5 0xc 0x1 0x8a 0x8d 0x9f 0x1d

i did write length_msb length_lsb :)

so i think that you've found a bug. will try to debug it next week

qrp73 commented 5 months ago

I wrote my own tool for read/write eeprom and upload firmware: https://github.com/qrp73/K5TOOL It has detailed protocol log, support firmware image in packed and unpacked formats, and has UV-K5 bootloader simulator which allows to use it as UV-K5 simulator to use original firmware updater and analyze protocol in the log.

I tested original firmware updater with simulator and found that your version for length field is correct and uvtool version is mistaken.

qrp73 commented 3 months ago

it appears that both variants are incorrect. The packet contains packet number, not offset... And it uses Little Endian... So k5prog and other tools using incorrect packet structure, but it appears that these mistakes in most cases don't affect firmware upload procedure :)