Another optimization for the OF in the mi4 format.

What it does:
- removes unnecessary file operations for the OF (one lseek() and one read() per one key),
- simplifies the code and reduces the code size.

Speedup is not noticeable but theoretically some is.

Change-Id: I43a6dd21d3af48ea8d3b27d676c84b2084c0b88c
Reviewed-on: http://gerrit.rockbox.org/287
Tested-by: Szymon Dziok <b0hoon@o2.pl>
Reviewed-by: Thomas Martitz <kugel@rockbox.org>
Reviewed-by: Szymon Dziok <b0hoon@o2.pl>
This commit is contained in:
Szymon Dziok 2012-07-01 15:14:55 +02:00
parent d594b36133
commit 95fe4eb3e0
1 changed files with 10 additions and 34 deletions

View File

@ -235,21 +235,12 @@ static void tea_decrypt_buf(unsigned char* src, unsigned char* dest, size_t n, u
}
}
static inline bool tea_test_key(unsigned char magic_enc[8], uint32_t * key, int unaligned)
{
unsigned char magic_dec[8];
tea_decrypt_buf(magic_enc, magic_dec, 8, key);
return (le2int(&magic_dec[4*unaligned]) == 0xaa55aa55);
}
static int tea_find_key(struct mi4header_t *mi4header, int fd)
static int tea_find_key(struct mi4header_t *mi4header, unsigned char* buf)
{
unsigned int i;
int rc;
uint32_t key[4];
uint32_t keyinc;
unsigned char magic_enc[8];
unsigned char magic_dec[8];
int key_found = -1;
unsigned int magic_location = mi4header->length-4;
int unaligned = 0;
@ -260,12 +251,6 @@ static int tea_find_key(struct mi4header_t *mi4header, int fd)
magic_location -= 4;
}
/* Load encrypted magic 0xaa55aa55 to check key */
lseek(fd, MI4_HEADER_SIZE + magic_location, SEEK_SET);
rc = read(fd, magic_enc, 8);
if(rc < 8 )
return EREAD_IMAGE_FAILED;
printf("Searching for key:");
for (i=0; i < NUM_KEYS && (key_found<0) ; i++) {
@ -281,10 +266,13 @@ static int tea_find_key(struct mi4header_t *mi4header, int fd)
if (key[1]==0) key[2]++;
if (key[2]==0) key[3]++;
if (tea_test_key(magic_enc,key,unaligned))
/* Decrypt putative magic */
tea_decrypt_buf(&buf[magic_location], magic_dec, 8, key);
if (le2int(&magic_dec[4*unaligned]) == 0xaa55aa55)
{
key_found = i;
printf("%s...found", tea_keytable[i].name);
printf("%s...found", tea_keytable[i].name);
} else {
/* printf("%s...failed", tea_keytable[i].name); */
}
@ -336,33 +324,25 @@ int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
/* Load firmware file */
lseek(fd, MI4_HEADER_SIZE, SEEK_SET);
rc = read(fd, buf, mi4header.mi4size-MI4_HEADER_SIZE);
close(fd);
if(rc < (int)mi4header.mi4size-MI4_HEADER_SIZE)
{
close(fd);
return EREAD_IMAGE_FAILED;
}
/* Check CRC32 to see if we have a valid file */
sum = chksum_crc32 (buf, mi4header.mi4size - MI4_HEADER_SIZE);
printf("Calculated CRC32: %x", sum);
if(sum != mi4header.crc32)
{
close(fd);
return EBAD_CHKSUM;
}
if( (mi4header.plaintext + MI4_HEADER_SIZE) != mi4header.mi4size)
{
/* Load encrypted firmware */
int key_index = tea_find_key(&mi4header, fd);
int key_index = tea_find_key(&mi4header, buf);
if (key_index < 0)
{
close(fd);
return EINVALID_FORMAT;
}
/* Plaintext part is already loaded */
buf += mi4header.plaintext;
@ -376,13 +356,9 @@ int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
/* Check decryption was successfull */
if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55)
{
close(fd);
return EREAD_IMAGE_FAILED;
}
}
close(fd);
return EOK;
}