From 488b3db547a09b25eac212e77ccb64ef81f8ce3f Mon Sep 17 00:00:00 2001 From: Dominik Wenger Date: Sat, 28 Jul 2007 14:38:25 +0000 Subject: rbutilQt: bootloader installation routines for iriver players. Bootloader routines are now completed. They need heavy testing. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14040 a1c6a512-1295-4272-9138-f99709370657 --- rbutil/rbutilqt/irivertools/h100sums.h | 23 ++ rbutil/rbutilqt/irivertools/h120sums.h | 23 ++ rbutil/rbutilqt/irivertools/h300sums.h | 17 + rbutil/rbutilqt/irivertools/irivertools.cpp | 532 ++++++++++++++++++++++++++++ rbutil/rbutilqt/irivertools/irivertools.h | 69 ++++ rbutil/rbutilqt/irivertools/md5sum.cpp | 295 +++++++++++++++ rbutil/rbutilqt/irivertools/md5sum.h | 50 +++ 7 files changed, 1009 insertions(+) create mode 100644 rbutil/rbutilqt/irivertools/h100sums.h create mode 100644 rbutil/rbutilqt/irivertools/h120sums.h create mode 100644 rbutil/rbutilqt/irivertools/h300sums.h create mode 100644 rbutil/rbutilqt/irivertools/irivertools.cpp create mode 100644 rbutil/rbutilqt/irivertools/irivertools.h create mode 100644 rbutil/rbutilqt/irivertools/md5sum.cpp create mode 100644 rbutil/rbutilqt/irivertools/md5sum.h (limited to 'rbutil/rbutilqt/irivertools') diff --git a/rbutil/rbutilqt/irivertools/h100sums.h b/rbutil/rbutilqt/irivertools/h100sums.h new file mode 100644 index 0000000000..ebbdcc28db --- /dev/null +++ b/rbutil/rbutilqt/irivertools/h100sums.h @@ -0,0 +1,23 @@ +/* Checksums of firmwares for ihp_100 */ +/* order: unpatched, patched */ + +/* 1.63eu */ +{"86103cb05658970b43ce1f40f93d53a8", "4938420d83aa6da3764d33e5a008c8d4"}, +/* 1.63k */ +{"70127fa9ee69afab7645297fbb61636d", "044b737ddb74436353e290d3fbcc3333"}, +/* 1.63us */ +{"7a504a450c76a0dda757fdb6b5531b22", "e51883ed89d49f5677b0a976aef7b154"}, +/* 1.65eu */ +{"478dc657b97e77d1b4944ef26c3dcb8e", "528917d9e5b34a3e5bcac8fe1f4bd7a9"}, +/* 1.65k */ +{"97ba82fb8099bb23ca0c78fc119f8cce", "ef24f69a679e5eba2216045cb24d8b15"}, +/* 1.65us */ +{"d3725865e0948cd5f604b00db2ec89aa", "76d83812b9e1856f768ba913eeba44e1"}, +/* 1.66eu */ +{"fc59f742fe383da3e4d17a660c46afb9", "2aa9d16d3e166a7575fc652db8588daa"}, +/* 1.66k */ +{"22d214401994c276ec6878288caa0dba", "a87dbe604ea98d035450a34b6e184a08"}, +/* 1.66us */ +{"0ae247f567aeafcafecf823a50cbf33e", "f7668e8949f1a78e8ecd685db78b6824"}, +/* 1.66jp */ +{"8bfc8ae5805c270597232efeafade5fd", "72613b75c5196f63e99e41b7a88d7241"}, diff --git a/rbutil/rbutilqt/irivertools/h120sums.h b/rbutil/rbutilqt/irivertools/h120sums.h new file mode 100644 index 0000000000..09da891ae1 --- /dev/null +++ b/rbutil/rbutilqt/irivertools/h120sums.h @@ -0,0 +1,23 @@ +/* Checksums of firmwares for ihp_120 */ +/* order: unpatched, patched */ + +/* 1.63eu */ +{"14488347a171480c63c94bc7b885225d", "15f09130a1aa02c25f820e8cc68259a4"}, +/* 1.63k */ +{"3401fe8845e569156abfaddf05ca7771", "55e4cb20e36f4da06ad80b31aaaa3054"}, +/* 1.63us */ +{"d9078209105c186cee5246055fdb99c9", "dbdd012a3d821d26f907879cca71e9f4"}, +/* 1.65eu */ +{"c9e71aac4a498f1e2f0e684c2d554ea1", "43454df30c176e55d0df7e3c48e67785"}, +/* 1.65k */ +{"360c0c565266f84e9bca610c596f3207", "d8cb7d9f586186bf9780ee761fc8a677"}, +/* 1.65us */ +{"b9e516d4b8a0265605f46f254897bfb0", "99c5666e990dc782b9daefcb1a087ec0"}, +/* 1.66eu */ +{"a094999b41781f4322a57b9be7fa4534", "e26d2574f39cee0adcdd54fb9f316293"}, +/* 1.66k */ +{"79daba973bb31b60b4b87c2ef497587d", "e76121ba9efe72ecfbb6392eaefe6d96"}, +/* 1.66us */ +{"7fa020a3104c76fbbfcb8313e287dbe2", "12194678a2fdd0814d2e0bb57c8e8e6e"}, +/* 1.66jp */ +{"271d151eb6586929da24b3458ea965fe", "51716393ea0605d225e70a6be29dfdbd"}, diff --git a/rbutil/rbutilqt/irivertools/h300sums.h b/rbutil/rbutilqt/irivertools/h300sums.h new file mode 100644 index 0000000000..1c6581a6d1 --- /dev/null +++ b/rbutil/rbutilqt/irivertools/h300sums.h @@ -0,0 +1,17 @@ +/* Checksums of firmwares for ihp_300 */ +/* order: unpatched, patched */ + +/* 1.28eu */ +{"0b2083d37f24899b82d21a14d2b38060", "9cd7e291a66f55335c619d63f3a7634b"}, +/* 1.28k */ +{"c6f854ae3d8f48e8982819a4b5302fb0", "90967247ba3f1bcb257432a4d78553bf"}, +/* 1.28jp */ +{"7fcd7bca6b98a34134e2518a616f4e85", "cfd22c20e473727148f11f83d0028fb3"}, +/* 1.29eu */ +{"27d90fc316709c096979ab24c914ee31", "5982302507d57c2a96e9480c242b5de0"}, +/* 1.29k */ +{"44416d97737fc47cd417a64d44064768", "1303e22ef5b1af866aa4def03d6c5f5a"}, +/* 1.29jp */ +{"1ac242c645572a0a5de99ae2b23453b8", "ce433c404ff7531f5852ce3cb61143ee"}, +/* 1.30eu */ +{"8d2d775b018f0532235d38f5317ae10c", "7fb49041294b3e6aea3894218d6d64cf"}, diff --git a/rbutil/rbutilqt/irivertools/irivertools.cpp b/rbutil/rbutilqt/irivertools/irivertools.cpp new file mode 100644 index 0000000000..f2cc59a8c2 --- /dev/null +++ b/rbutil/rbutilqt/irivertools/irivertools.cpp @@ -0,0 +1,532 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Module: rbutil + * File: irivertools.cpp + * + * Copyright (C) 2007 Dominik Wenger + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "irivertools.h" + + +const unsigned char munge[] = { + 0x7a, 0x36, 0xc4, 0x43, 0x49, 0x6b, 0x35, 0x4e, 0xa3, 0x46, 0x25, 0x84, + 0x4d, 0x73, 0x74, 0x61 +}; + +const unsigned char header_modify[] = "* IHPFIRM-DECODED "; + +const char * const models[] = { "iHP-100", "iHP-120/iHP-140", "H300 series", + NULL }; + +/* aligns with models array; expected min firmware size */ +const unsigned int firmware_minsize[] = { 0x100000, 0x100000, 0x200000 }; +/* aligns with models array; expected max firmware size */ +const unsigned int firmware_maxsize[] = { 0x200000, 0x200000, 0x400000 }; + +const unsigned char header[][16] = { + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, + { 0x20, 0x03, 0x08, 0x27, 0x24, 0x00, 0x02, 0x30, 0x19, 0x17, 0x65, 0x73, + 0x85, 0x32, 0x83, 0x22 }, + { 0x20, 0x04, 0x03, 0x27, 0x20, 0x50, 0x01, 0x70, 0x80, 0x30, 0x80, 0x06, + 0x30, 0x19, 0x17, 0x65 } +}; + +/* begin mkboot.c excerpt */ +unsigned char image[0x400000 + 0x220 + 0x400000/0x200]; + +bool mkboot(QString infile, QString outfile,QString bootloader,int origin,Ui::InstallProgressFrm* dp) +{ + int i; + int len,bllen; + int actual_length, total_length, binary_length, num_chksums; + + memset(image, 0xff, sizeof(image)); + + /* First, read the iriver original firmware into the image */ + QFile f(infile); + if(!f.open(QIODevice::ReadOnly)) + { + dp->listProgress->addItem("Could not open: %1" + infile); + return false; + } + i = f.read((char*)image,16); + if(i < 16) { + dp->listProgress->addItem("reading header failed"); + return false; + } + + /* This is the length of the binary image without the scrambling + overhead (but including the ESTFBINR header) */ + binary_length = image[4] + (image[5] << 8) + + (image[6] << 16) + (image[7] << 24); + + /* Read the rest of the binary data, but not the checksum block */ + len = binary_length+0x200-16; + i = f.read((char*)image+16, len); + if(i < len) { + dp->listProgress->addItem("reading firmware failed"); + return false; + } + + f.close(); + /* Now, read the boot loader into the image */ + f.setFileName(bootloader); + if(!f.open(QIODevice::ReadOnly)) + { + dp->listProgress->addItem("Could not open: %1" + bootloader); + return false; + } + + bllen = f.size(); + + i = f.read((char*)image+0x220 + origin, bllen); + if(i < bllen) { + dp->listProgress->addItem("reading bootloader failed"); + return false; + } + + f.close(); + f.setFileName(outfile); + if(!f.open(QIODevice::WriteOnly)) + { + dp->listProgress->addItem("Could not open: %1" + outfile); + return false; + } + + /* Patch the reset vector to start the boot loader */ + image[0x220 + 4] = image[origin + 0x220 + 4]; + image[0x220 + 5] = image[origin + 0x220 + 5]; + image[0x220 + 6] = image[origin + 0x220 + 6]; + image[0x220 + 7] = image[origin + 0x220 + 7]; + + /* This is the actual length of the binary, excluding all headers */ + actual_length = origin + bllen; + + /* Patch the ESTFBINR header */ + image[0x20c] = (actual_length >> 24) & 0xff; + image[0x20d] = (actual_length >> 16) & 0xff; + image[0x20e] = (actual_length >> 8) & 0xff; + image[0x20f] = actual_length & 0xff; + + image[0x21c] = (actual_length >> 24) & 0xff; + image[0x21d] = (actual_length >> 16) & 0xff; + image[0x21e] = (actual_length >> 8) & 0xff; + image[0x21f] = actual_length & 0xff; + + /* This is the length of the binary, including the ESTFBINR header and + rounded up to the nearest 0x200 boundary */ + binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00; + + /* The number of checksums, i.e number of 0x200 byte blocks */ + num_chksums = binary_length / 0x200; + + /* The total file length, including all headers and checksums */ + total_length = binary_length + num_chksums + 0x200; + + /* Patch the scrambler header with the new length info */ + image[0] = total_length & 0xff; + image[1] = (total_length >> 8) & 0xff; + image[2] = (total_length >> 16) & 0xff; + image[3] = (total_length >> 24) & 0xff; + + image[4] = binary_length & 0xff; + image[5] = (binary_length >> 8) & 0xff; + image[6] = (binary_length >> 16) & 0xff; + image[7] = (binary_length >> 24) & 0xff; + + image[8] = num_chksums & 0xff; + image[9] = (num_chksums >> 8) & 0xff; + image[10] = (num_chksums >> 16) & 0xff; + image[11] = (num_chksums >> 24) & 0xff; + + i = f.write((char*)image,total_length); + if(i < total_length) { + dp->listProgress->addItem("writing bootloader failed"); + return false; + } + + f.close(); + + return true; +} + +/* end mkboot.c excerpt */ + + +int intable(char *md5, struct sumpairs *table, int len) +{ + int i; + for (i = 0; i < len; i++) { + if (strncmp(md5, table[i].unpatched, 32) == 0) { + return i; + } + } + return -1; +} + + + + +static int testheader( const unsigned char * const data ) +{ + const unsigned char * const d = data+16; + const char * const * m = models; + int index = 0; + while( *m ) + { + if( memcmp( header[ index ], d, 16 ) == 0 ) + return index; + index++; + m++; + }; + return -1; +}; + +static void modifyheader( unsigned char * data ) +{ + const unsigned char * h = header_modify; + int i; + for( i=0; i<512; i++ ) + { + if( *h == '\0' ) + h = header_modify; + *data++ ^= *h++; + }; +}; + +int iriver_decode(QString infile_name, QString outfile_name, unsigned int modify, + enum striptype stripmode,Ui::InstallProgressFrm* dp ) +{ + QFile infile(infile_name); + QFile outfile(outfile_name); + int i = -1; + unsigned char headerdata[512]; + unsigned long dwLength1, dwLength2, dwLength3, fp = 0; + unsigned char blockdata[16+16]; + unsigned char out[16]; + unsigned char newmunge; + signed long lenread; + int s = 0; + unsigned char * pChecksums, * ppChecksums = 0; + unsigned char ck; + + + if(!infile.open(QIODevice::ReadOnly)) + { + dp->listProgress->addItem("Could not open: %1" + infile_name); + return -1; + } + if(!outfile.open(QIODevice::WriteOnly)) + { + dp->listProgress->addItem("Could not open: %1" + outfile_name); + return -1; + } + lenread = infile.read( (char*)headerdata, 512); + if( lenread != 512 ) + { + dp->listProgress->addItem("This doesn't look like a valid encrypted iHP" + "firmware - reason: header length."); + infile.close(); + outfile.close(); + return -1; + }; + + i = testheader( headerdata ); + if( i == -1 ) + { + dp->listProgress->addItem("This firmware is for an unknown model, or is not" + " a valid encrypted iHP firmware."); + infile.close(); + outfile.close(); + return -1; + }; + fprintf( stderr, "Model %s\n", models[ i ] ); + + dwLength1 = headerdata[0] | (headerdata[1]<<8) | + (headerdata[2]<<16) | (headerdata[3]<<24); + dwLength2 = headerdata[4] | (headerdata[5]<<8) | + (headerdata[6]<<16) | (headerdata[7]<<24); + dwLength3 = headerdata[8] | (headerdata[9]<<8) | + (headerdata[10]<<16) | (headerdata[11]<<24); + + if( dwLength1 < firmware_minsize[ i ] || + dwLength1 > firmware_maxsize[ i ] || + dwLength2 < firmware_minsize[ i ] || + dwLength2 > dwLength1 || + dwLength3 > dwLength1 || + dwLength2>>9 != dwLength3 || + dwLength2+dwLength3+512 != dwLength1 ) + { + dp->listProgress->addItem("This doesn't look like a valid encrypted " + "iHP firmware - reason: file 'length' data."); + infile.close(); + outfile.close(); + return -1; + }; + + pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); + + if( modify ) + { + modifyheader( headerdata ); + }; + + if( stripmode == STRIP_NONE ) + outfile.write( (char*)headerdata, 512); + + memset( blockdata, 0, 16 ); + + ck = 0; + while( ( fp < dwLength2 ) && + ( lenread = infile.read( (char*)blockdata+16, 16) == 16) ) + { + fp += 16; + + for( i=0; i<16; ++i ) + { + newmunge = blockdata[16+i] ^ munge[i]; + out[i] = newmunge ^ blockdata[i]; + blockdata[i] = newmunge; + ck += out[i]; + } + + if( fp > ESTF_SIZE || stripmode != STRIP_HEADER_CHECKSUM_ESTF ) + { + outfile.write( (char*)out+4, 12); + outfile.write( (char*)out, 4); + } + else + { + if( ESTF_SIZE - fp < 16 ) + { + memcpy( out+4, blockdata+16, 12 ); + memcpy( out, blockdata+28, 4 ); + outfile.write((char*) blockdata+16+ESTF_SIZE-fp, ESTF_SIZE-fp); + } + } + + + if( s == 496 ) + { + s = 0; + memset( blockdata, 0, 16 ); + *ppChecksums++ = ck; + ck = 0; + } + else + s+=16; + }; + + if( fp != dwLength2 ) + { + dp->listProgress->addItem("This doesn't look like a valid encrypted " + "iHP firmware - reason: 'length2' mismatch."); + infile.close(); + outfile.close(); + return -1; + }; + + fp = 0; + ppChecksums = pChecksums; + while( ( fp < dwLength3 ) && + ( lenread = infile.read((char*) blockdata, 32 ) ) > 0 ) + { + fp += lenread; + if( stripmode == STRIP_NONE ) + outfile.write((char*) blockdata, lenread ); + if( memcmp( ppChecksums, blockdata, lenread ) != 0 ) + { + dp->listProgress->addItem("This doesn't look like a valid encrypted " + "iHP firmware - reason: Checksum mismatch!"); + infile.close(); + outfile.close(); + return -1; + }; + ppChecksums += lenread; + }; + + if( fp != dwLength3 ) + { + dp->listProgress->addItem("This doesn't look like a valid encrypted " + "iHP firmware - reason: 'length3' mismatch."); + infile.close(); + outfile.close(); + return -1; + }; + + + fprintf( stderr, "File decoded correctly and all checksums matched!\n" ); + switch( stripmode ) + { + default: + case STRIP_NONE: + fprintf(stderr, "Output file contains all headers and " + "checksums\n"); + break; + case STRIP_HEADER_CHECKSUM: + fprintf( stderr, "NB: output file contains only ESTFBINR header" + " and decoded firmware code\n" ); + break; + case STRIP_HEADER_CHECKSUM_ESTF: + fprintf( stderr, "NB: output file contains only raw decoded " + "firmware code\n" ); + break; + }; + + infile.close(); + outfile.close(); + return 0; + +}; + +int iriver_encode(QString infile_name, QString outfile_name, unsigned int modify,Ui::InstallProgressFrm* dp ) +{ + QFile infile(infile_name); + QFile outfile(outfile_name); + int i = -1; + unsigned char headerdata[512]; + unsigned long dwLength1, dwLength2, dwLength3, fp = 0; + unsigned char blockdata[16+16]; + unsigned char out[16]; + unsigned char newmunge; + signed long lenread; + int s = 0; + unsigned char * pChecksums, * ppChecksums; + unsigned char ck; + + if(!infile.open(QIODevice::ReadOnly)) + { + dp->listProgress->addItem("Could not open: %1" + infile_name); + return -1; + } + if(!outfile.open(QIODevice::WriteOnly)) + { + dp->listProgress->addItem("Could not open: %1" + outfile_name); + return -1; + } + + lenread = infile.read((char*) headerdata, 512 ); + if( lenread != 512 ) + { + dp->listProgress->addItem("This doesn't look like a valid decoded " + "iHP firmware - reason: header length."); + infile.close(); + outfile.close(); + }; + + if( modify ) + { + modifyheader( headerdata ); /* reversible */ + }; + + i = testheader( headerdata ); + if( i == -1 ) + { + dp->listProgress->addItem("This firmware is for an unknown model, or is not" + " a valid decoded iHP firmware."); + infile.close(); + outfile.close(); + }; + fprintf( stderr, "Model %s\n", models[ i ] ); + + dwLength1 = headerdata[0] | (headerdata[1]<<8) | + (headerdata[2]<<16) | (headerdata[3]<<24); + dwLength2 = headerdata[4] | (headerdata[5]<<8) | + (headerdata[6]<<16) | (headerdata[7]<<24); + dwLength3 = headerdata[8] | (headerdata[9]<<8) | + (headerdata[10]<<16) | (headerdata[11]<<24); + + if( dwLength1 < firmware_minsize[i] || + dwLength1 > firmware_maxsize[i] || + dwLength2 < firmware_minsize[i] || + dwLength2 > dwLength1 || + dwLength3 > dwLength1 || + dwLength2+dwLength3+512 != dwLength1 ) + { + dp->listProgress->addItem("This doesn't look like a valid decoded " + "iHP firmware - reason:file 'length' data."); + infile.close(); + outfile.close(); + }; + + pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); + + outfile.write( (char*)headerdata, 512); + + memset( blockdata, 0, 16 ); + ck = 0; + while( ( fp < dwLength2 ) && + ( lenread = infile.read((char*) blockdata+16, 16) ) == 16 ) + { + fp += 16; + for( i=0; i<16; ++i ) + { + newmunge = blockdata[16+((12+i)&0xf)] ^ blockdata[i]; + out[i] = newmunge ^ munge[i]; + ck += blockdata[16+i]; + blockdata[i] = newmunge; + }; + outfile.write( (char*)out, 16); + + if( s == 496 ) + { + s = 0; + memset( blockdata, 0, 16 ); + *ppChecksums++ = ck; + ck = 0; + } + else + s+=16; + }; + + if( fp != dwLength2 ) + { + dp->listProgress->addItem("This doesn't look like a valid decoded " + "iHP firmware - reason: 'length1' mismatch."); + infile.close(); + outfile.close(); + }; + + /* write out remainder w/out applying descrambler */ + fp = 0; + lenread = dwLength3; + ppChecksums = pChecksums; + while( ( fp < dwLength3) && + ( lenread = outfile.write((char*) ppChecksums, lenread) ) > 0 ) + { + fp += lenread; + ppChecksums += lenread; + lenread = dwLength3 - fp; + }; + + if( fp != dwLength3 ) + { + dp->listProgress->addItem("This doesn't look like a valid decoded " + "iHP firmware - 'length2' mismatch."); + infile.close(); + outfile.close(); + }; + + fprintf( stderr, "File encoded successfully and checksum table built!\n" ); + + infile.close(); + outfile.close(); + return 0; + +}; + + + diff --git a/rbutil/rbutilqt/irivertools/irivertools.h b/rbutil/rbutilqt/irivertools/irivertools.h new file mode 100644 index 0000000000..54809d7645 --- /dev/null +++ b/rbutil/rbutilqt/irivertools/irivertools.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Module: rbutil + * File: irivertools.h + * + * Copyright (C) 2007 Dominik Wenger + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + + +#ifndef IRIVERTOOLS_H_INCLUDED +#define IRIVERTOOLS_H_INCLUDED + +#include + +#include "md5sum.h" +#include "ui_installprogressfrm.h" + +#define ESTF_SIZE 32 + +struct sumpairs { + char *unpatched; + char *patched; +}; + +/* precalculated checksums for H110/H115 */ +static struct sumpairs h100pairs[] = { +#include "h100sums.h" +}; + +/* precalculated checksums for H120/H140 */ +static struct sumpairs h120pairs[] = { +#include "h120sums.h" +}; + +/* precalculated checksums for H320/H340 */ +static struct sumpairs h300pairs[] = { +#include "h300sums.h" +}; + + +enum striptype +{ + STRIP_NONE, + STRIP_HEADER_CHECKSUM, + STRIP_HEADER_CHECKSUM_ESTF +}; + +/* protos for iriver.c */ + +int intable(char *md5, struct sumpairs *table, int len); + +bool mkboot(QString infile, QString outfile,QString bootloader,int origin,Ui::InstallProgressFrm* dp); +int iriver_decode(QString infile_name, QString outfile_name, unsigned int modify, + enum striptype stripmode,Ui::InstallProgressFrm* dp ); +int iriver_encode(QString infile_name, QString outfile_name, unsigned int modify,Ui::InstallProgressFrm* dp); + +#endif // IRIVERTOOLS_H_INCLUDED diff --git a/rbutil/rbutilqt/irivertools/md5sum.cpp b/rbutil/rbutilqt/irivertools/md5sum.cpp new file mode 100644 index 0000000000..f4d25e67ef --- /dev/null +++ b/rbutil/rbutilqt/irivertools/md5sum.cpp @@ -0,0 +1,295 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Module: rbutil + * File: md5sum.cpp + * + * Copyright (C) 2007 Dominik Wenger + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + + +/* + * RFC 1321 compliant MD5 implementation + * + * Copyright (C) 2001-2003 Christophe Devine + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "md5sum.h" + + +#define GET_UINT32(n,b,i) \ +{ \ + (n) = ( (uint32) (b)[(i) ] ) \ + | ( (uint32) (b)[(i) + 1] << 8 ) \ + | ( (uint32) (b)[(i) + 2] << 16 ) \ + | ( (uint32) (b)[(i) + 3] << 24 ); \ +} + +#define PUT_UINT32(n,b,i) \ +{ \ + (b)[(i) ] = (uint8) ( (n) ); \ + (b)[(i) + 1] = (uint8) ( (n) >> 8 ); \ + (b)[(i) + 2] = (uint8) ( (n) >> 16 ); \ + (b)[(i) + 3] = (uint8) ( (n) >> 24 ); \ +} + +void md5_starts( md5_context *ctx ) +{ + ctx->total[0] = 0; + ctx->total[1] = 0; + + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; +} + +void md5_process( md5_context *ctx, uint8 data[64] ) +{ + uint32 X[16], A, B, C, D; + + GET_UINT32( X[0], data, 0 ); + GET_UINT32( X[1], data, 4 ); + GET_UINT32( X[2], data, 8 ); + GET_UINT32( X[3], data, 12 ); + GET_UINT32( X[4], data, 16 ); + GET_UINT32( X[5], data, 20 ); + GET_UINT32( X[6], data, 24 ); + GET_UINT32( X[7], data, 28 ); + GET_UINT32( X[8], data, 32 ); + GET_UINT32( X[9], data, 36 ); + GET_UINT32( X[10], data, 40 ); + GET_UINT32( X[11], data, 44 ); + GET_UINT32( X[12], data, 48 ); + GET_UINT32( X[13], data, 52 ); + GET_UINT32( X[14], data, 56 ); + GET_UINT32( X[15], data, 60 ); + +#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) + +#define P(a,b,c,d,k,s,t) \ +{ \ + a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ +} + + A = ctx->state[0]; + B = ctx->state[1]; + C = ctx->state[2]; + D = ctx->state[3]; + +#define F(x,y,z) (z ^ (x & (y ^ z))) + + P( A, B, C, D, 0, 7, 0xD76AA478 ); + P( D, A, B, C, 1, 12, 0xE8C7B756 ); + P( C, D, A, B, 2, 17, 0x242070DB ); + P( B, C, D, A, 3, 22, 0xC1BDCEEE ); + P( A, B, C, D, 4, 7, 0xF57C0FAF ); + P( D, A, B, C, 5, 12, 0x4787C62A ); + P( C, D, A, B, 6, 17, 0xA8304613 ); + P( B, C, D, A, 7, 22, 0xFD469501 ); + P( A, B, C, D, 8, 7, 0x698098D8 ); + P( D, A, B, C, 9, 12, 0x8B44F7AF ); + P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); + P( B, C, D, A, 11, 22, 0x895CD7BE ); + P( A, B, C, D, 12, 7, 0x6B901122 ); + P( D, A, B, C, 13, 12, 0xFD987193 ); + P( C, D, A, B, 14, 17, 0xA679438E ); + P( B, C, D, A, 15, 22, 0x49B40821 ); + +#undef F + +#define F(x,y,z) (y ^ (z & (x ^ y))) + + P( A, B, C, D, 1, 5, 0xF61E2562 ); + P( D, A, B, C, 6, 9, 0xC040B340 ); + P( C, D, A, B, 11, 14, 0x265E5A51 ); + P( B, C, D, A, 0, 20, 0xE9B6C7AA ); + P( A, B, C, D, 5, 5, 0xD62F105D ); + P( D, A, B, C, 10, 9, 0x02441453 ); + P( C, D, A, B, 15, 14, 0xD8A1E681 ); + P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); + P( A, B, C, D, 9, 5, 0x21E1CDE6 ); + P( D, A, B, C, 14, 9, 0xC33707D6 ); + P( C, D, A, B, 3, 14, 0xF4D50D87 ); + P( B, C, D, A, 8, 20, 0x455A14ED ); + P( A, B, C, D, 13, 5, 0xA9E3E905 ); + P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); + P( C, D, A, B, 7, 14, 0x676F02D9 ); + P( B, C, D, A, 12, 20, 0x8D2A4C8A ); + +#undef F + +#define F(x,y,z) (x ^ y ^ z) + + P( A, B, C, D, 5, 4, 0xFFFA3942 ); + P( D, A, B, C, 8, 11, 0x8771F681 ); + P( C, D, A, B, 11, 16, 0x6D9D6122 ); + P( B, C, D, A, 14, 23, 0xFDE5380C ); + P( A, B, C, D, 1, 4, 0xA4BEEA44 ); + P( D, A, B, C, 4, 11, 0x4BDECFA9 ); + P( C, D, A, B, 7, 16, 0xF6BB4B60 ); + P( B, C, D, A, 10, 23, 0xBEBFBC70 ); + P( A, B, C, D, 13, 4, 0x289B7EC6 ); + P( D, A, B, C, 0, 11, 0xEAA127FA ); + P( C, D, A, B, 3, 16, 0xD4EF3085 ); + P( B, C, D, A, 6, 23, 0x04881D05 ); + P( A, B, C, D, 9, 4, 0xD9D4D039 ); + P( D, A, B, C, 12, 11, 0xE6DB99E5 ); + P( C, D, A, B, 15, 16, 0x1FA27CF8 ); + P( B, C, D, A, 2, 23, 0xC4AC5665 ); + +#undef F + +#define F(x,y,z) (y ^ (x | ~z)) + + P( A, B, C, D, 0, 6, 0xF4292244 ); + P( D, A, B, C, 7, 10, 0x432AFF97 ); + P( C, D, A, B, 14, 15, 0xAB9423A7 ); + P( B, C, D, A, 5, 21, 0xFC93A039 ); + P( A, B, C, D, 12, 6, 0x655B59C3 ); + P( D, A, B, C, 3, 10, 0x8F0CCC92 ); + P( C, D, A, B, 10, 15, 0xFFEFF47D ); + P( B, C, D, A, 1, 21, 0x85845DD1 ); + P( A, B, C, D, 8, 6, 0x6FA87E4F ); + P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); + P( C, D, A, B, 6, 15, 0xA3014314 ); + P( B, C, D, A, 13, 21, 0x4E0811A1 ); + P( A, B, C, D, 4, 6, 0xF7537E82 ); + P( D, A, B, C, 11, 10, 0xBD3AF235 ); + P( C, D, A, B, 2, 15, 0x2AD7D2BB ); + P( B, C, D, A, 9, 21, 0xEB86D391 ); + +#undef F +#undef S +#undef P + + + ctx->state[0] += A; + ctx->state[1] += B; + ctx->state[2] += C; + ctx->state[3] += D; +} + +void md5_update( md5_context *ctx, uint8 *input, uint32 length ) +{ + uint32 left, fill; + + if( ! length ) return; + + left = ctx->total[0] & 0x3F; + fill = 64 - left; + + ctx->total[0] += length; + ctx->total[0] &= 0xFFFFFFFF; + + if( ctx->total[0] < length ) + ctx->total[1]++; + + if( left && length >= fill ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, fill ); + md5_process( ctx, ctx->buffer ); + length -= fill; + input += fill; + left = 0; + } + + while( length >= 64 ) + { + md5_process( ctx, input ); + length -= 64; + input += 64; + } + + if( length ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, length ); + } +} + +static uint8 md5_padding[64] = +{ + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +void md5_finish( md5_context *ctx, uint8 digest[16] ) +{ + uint32 last, padn; + uint32 high, low; + uint8 msglen[8]; + + high = ( ctx->total[0] >> 29 ) + | ( ctx->total[1] << 3 ); + low = ( ctx->total[0] << 3 ); + + PUT_UINT32( low, msglen, 0 ); + PUT_UINT32( high, msglen, 4 ); + + last = ctx->total[0] & 0x3F; + padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + + md5_update( ctx, md5_padding, padn ); + md5_update( ctx, msglen, 8 ); + + PUT_UINT32( ctx->state[0], digest, 0 ); + PUT_UINT32( ctx->state[1], digest, 4 ); + PUT_UINT32( ctx->state[2], digest, 8 ); + PUT_UINT32( ctx->state[3], digest, 12 ); +} + +int FileMD5(QString name, char *md5) +{ + int i, read; + md5_context ctx; + unsigned char md5sum[16]; + unsigned char block[32768]; + + QFile file(name); + + if (!file.open(QIODevice::ReadOnly)) { + return 0; + } + md5_starts(&ctx); + while ( !file.atEnd() ) { + read = file.read((char*)block, sizeof(block)); + md5_update(&ctx, block, read); + } + file.close(); + md5_finish(&ctx, md5sum); + for (i = 0; i < 16; ++i) + { + sprintf(md5 + 2*i, "%02x", md5sum[i]); + } + return 1; +} diff --git a/rbutil/rbutilqt/irivertools/md5sum.h b/rbutil/rbutilqt/irivertools/md5sum.h new file mode 100644 index 0000000000..ed22bdbb49 --- /dev/null +++ b/rbutil/rbutilqt/irivertools/md5sum.h @@ -0,0 +1,50 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Module: rbutil + * File: md5sum.h + * + * Copyright (C) 2007 Dominik Wenger + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + + +#ifndef MD5SUM_H_INCLUDED +#define MD5SUM_H_INCLUDED + +#ifndef uint8 +#define uint8 unsigned char +#endif + + +#ifndef uint32 +#define uint32 unsigned long int +#endif + +#include + +typedef struct +{ + uint32 total[2]; + uint32 state[4]; + uint8 buffer[64]; +} +md5_context; + +void md5_starts( md5_context *ctx ); +void md5_update( md5_context *ctx, uint8 *input, uint32 length ); +void md5_finish( md5_context *ctx, uint8 digest[16] ); + +int FileMD5(QString name, char *md5); + +#endif // MD5SUM_H_INCLUDED -- cgit