forked from len0rd/rockbox
Scramble can now generate an nk.bin file, independent of the OF. These nk.bin files will only transfer using the sendfirm tool.
Also made the gigabeats.c file 64-bit and endian safe. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15838 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
d5430994ad
commit
db5206742e
1 changed files with 51 additions and 97 deletions
|
|
@ -19,6 +19,12 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
/* Entry point (and load address) for the main Rockbox bootloader */
|
||||||
|
#define BL_ENTRY_POINT 0x8a000000
|
||||||
|
|
||||||
|
|
||||||
static FILE * openinfile( const char * filename )
|
static FILE * openinfile( const char * filename )
|
||||||
{
|
{
|
||||||
|
|
@ -44,128 +50,76 @@ static FILE * openoutfile( const char * filename )
|
||||||
return F;
|
return F;
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned long calc_csum(const unsigned char* pb, int cb)
|
static uint32_t calc_csum(const unsigned char* pb, int cb)
|
||||||
{
|
{
|
||||||
unsigned long l = 0;
|
uint32_t l = 0;
|
||||||
while (cb--)
|
while (cb--)
|
||||||
l += *pb++;
|
l += *pb++;
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void put_uint32le(uint32_t x, unsigned char* p)
|
||||||
|
{
|
||||||
|
p[0] = x & 0xff;
|
||||||
|
p[1] = (x >> 8) & 0xff;
|
||||||
|
p[2] = (x >> 16) & 0xff;
|
||||||
|
p[3] = (x >> 24) & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
int gigabeat_s_code(char *infile, char *outfile)
|
int gigabeat_s_code(char *infile, char *outfile)
|
||||||
{
|
{
|
||||||
FILE *in, *out;
|
FILE *in, *out;
|
||||||
unsigned long size = 0;
|
unsigned int size;
|
||||||
unsigned long data;
|
unsigned int newsize;
|
||||||
int imagelength;
|
unsigned char* buf;
|
||||||
|
|
||||||
in = openinfile(infile);
|
in = openinfile(infile);
|
||||||
out = openoutfile(outfile);
|
out = openoutfile(outfile);
|
||||||
|
|
||||||
|
/* Step 1: Load the binary file into memory */
|
||||||
fseek(in, 0, SEEK_END);
|
fseek(in, 0, SEEK_END);
|
||||||
size = ftell(in);
|
size = ftell(in);
|
||||||
fseek(in, 0, SEEK_SET);
|
|
||||||
unsigned long *binptr = malloc(size);
|
/* 15 bytes for header, 16 for signature bypass,
|
||||||
if(binptr == NULL) {
|
* 12 for record header, 12 for footer */
|
||||||
|
newsize = 15 + 16 + 12 + size + 12;
|
||||||
|
buf = malloc(newsize);
|
||||||
|
if(buf == NULL) {
|
||||||
fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" );
|
fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
fread(binptr, size/4, 4, in);
|
fseek(in, 0, SEEK_SET);
|
||||||
/* 15 bytes for header, three 12 byte headers, the data for the first three
|
fread(buf + 43, size, 1, in);
|
||||||
* records, 12 byte header for code, code and the 12 byte footer
|
fclose(in);
|
||||||
* However, the original nk.bin's length doesn't correspond with
|
|
||||||
* the length of the file, so I don't know what's up...
|
|
||||||
*/
|
|
||||||
|
|
||||||
unsigned long header[2];
|
/* Step 2: Create the file header */
|
||||||
header[0] = 0x88200000;
|
sprintf(buf, "B000FF\n");
|
||||||
/* header[1] = 15 + 12 + 4 + 12 + 8 + 12 + 4 + 12 + size + 12; */
|
put_uint32le(0x88200000, buf+7);
|
||||||
header[1] = 0xCC0CD8; /* The bootloader checks this value and compares */
|
/* If the value below is too small, the update will attempt to flash.
|
||||||
fwrite("B000FF\n", 7, 1, out);
|
* Be careful when changing this (leaving it as is won't cause issues) */
|
||||||
fwrite(header, sizeof(header), 1, out);
|
put_uint32le(0xCC0CD8, buf+11);
|
||||||
|
|
||||||
unsigned long record[4];
|
|
||||||
unsigned long extra;
|
|
||||||
|
|
||||||
/*First record*/
|
/* Step 3: Add the signature bypass record */
|
||||||
record[0] = 0x88200000;
|
put_uint32le(0x88065A10, buf+15);
|
||||||
record[1] = 4;
|
put_uint32le(4, buf+19);
|
||||||
record[2] = 0x1eb;
|
put_uint32le(0xE3A00001, buf+27);
|
||||||
record[3] = 0xEA0003FE;
|
put_uint32le(calc_csum(buf+27,4), buf+23);
|
||||||
fwrite(record, sizeof(record), 1, out);
|
|
||||||
|
|
||||||
/*Second record*/
|
/* Step 4: Create a record for the actual code */
|
||||||
record[0] = 0x88200040;
|
put_uint32le(BL_ENTRY_POINT, buf+31);
|
||||||
record[1] = 8;
|
put_uint32le(size, buf+35);
|
||||||
record[2] = 0x3e9;
|
put_uint32le(calc_csum(buf + 43, size), buf+39);
|
||||||
record[3] = 0x43454345;
|
|
||||||
extra = 0x88EBF274;
|
|
||||||
fwrite(record, sizeof(record), 1, out);
|
|
||||||
fwrite(&extra, sizeof(extra), 1, out);
|
|
||||||
|
|
||||||
/*Third record*/
|
/* Step 5: Write the footer */
|
||||||
record[0] = 0x88200048;
|
put_uint32le(0, buf+newsize-12);
|
||||||
record[1] = 4;
|
put_uint32le(BL_ENTRY_POINT, buf+newsize-8);
|
||||||
record[2] = 0x231;
|
put_uint32le(0, buf+newsize-4);
|
||||||
record[3] = 0x00CBF274;
|
|
||||||
fwrite(record, sizeof(record), 1, out);
|
|
||||||
|
|
||||||
/*Signature bypass record*/
|
/* Step 6: Write the resulting file */
|
||||||
unsigned long magic = 0xE3A00001;
|
fwrite(buf, newsize, 1, out);
|
||||||
record[0] = 0x88065A10;
|
fclose(out);
|
||||||
record[1] = 4;
|
|
||||||
record[2] = calc_csum((unsigned char*)&magic,4);
|
|
||||||
record[3] = magic;
|
|
||||||
fwrite(record, sizeof(record), 1, out);
|
|
||||||
|
|
||||||
/*The actual code*/
|
|
||||||
header[0] = 0x88201000;
|
|
||||||
header[1] = size;
|
|
||||||
extra = calc_csum((unsigned char*)binptr, size);
|
|
||||||
fwrite(header, sizeof(header), 1, out);
|
|
||||||
fwrite(&extra, sizeof(extra), 1, out);
|
|
||||||
fwrite(binptr, size, 1, out);
|
|
||||||
|
|
||||||
/* Table of contents. It's a start, but it still won't boot.
|
|
||||||
* Looks like it needs the file/module info as well... */
|
|
||||||
binptr[0] = 0x02000000;
|
|
||||||
binptr[1] = 0x02000000;
|
|
||||||
binptr[2] = 0x88040000;
|
|
||||||
binptr[3] = 0x88076338;
|
|
||||||
binptr[4] = 0x1;
|
|
||||||
binptr[5] = 0x88080000;
|
|
||||||
binptr[6] = 0x8809C000;
|
|
||||||
binptr[7] = 0x88100000;
|
|
||||||
binptr[8] = 0x0;
|
|
||||||
binptr[9] = 0x0;
|
|
||||||
binptr[10] = 0x0;
|
|
||||||
binptr[11] = 0x0;
|
|
||||||
binptr[12] = 0x0;
|
|
||||||
binptr[13] = 0x0;
|
|
||||||
binptr[14] = 0x80808080;
|
|
||||||
binptr[15] = 0x0;
|
|
||||||
binptr[16] = 0x0;
|
|
||||||
binptr[17] = 0x201C2;
|
|
||||||
binptr[18] = 0x88050940;
|
|
||||||
binptr[19] = 0x0;
|
|
||||||
binptr[20] = 0x0;
|
|
||||||
header[0] = 0x88EBF274;
|
|
||||||
header[1] = 0x54;
|
|
||||||
extra = calc_csum((unsigned char*)binptr, 84);
|
|
||||||
fwrite(header, sizeof(header), 1, out);
|
|
||||||
fwrite(&extra, sizeof(extra), 1, out);
|
|
||||||
fwrite(binptr, 84, 1, out);
|
|
||||||
|
|
||||||
/*The footer*/
|
|
||||||
header[0] = 0;
|
|
||||||
header[1] = 0x88201000;
|
|
||||||
extra = 0;
|
|
||||||
fwrite(header, sizeof(header), 1, out);
|
|
||||||
fwrite(&extra, sizeof(extra), 1, out);
|
|
||||||
|
|
||||||
fprintf(stderr, "File processed successfully\n" );
|
fprintf(stderr, "File processed successfully\n" );
|
||||||
|
|
||||||
fclose(in);
|
|
||||||
fclose(out);
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue