2008-10-11 13:11:47 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2008-10-11 13:13:44 +00:00
|
|
|
* $Id$
|
2008-10-11 13:11:47 +00:00
|
|
|
*
|
|
|
|
* mkamsboot.c - a tool for merging bootloader code into an Sansa V2
|
|
|
|
* (AMS) firmware file
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Dave Chapman
|
|
|
|
*
|
|
|
|
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-05-11 18:29:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
Insert a Rockbox bootloader into a Sansa AMS original firmware file.
|
|
|
|
|
|
|
|
Layout of a Sansa AMS original firmware file:
|
|
|
|
|
|
|
|
---------------------- 0x0
|
|
|
|
| HEADER |
|
|
|
|
|----------------------| 0x400
|
|
|
|
| FIRMWARE BLOCK |
|
|
|
|
|----------------------| 0x400 + firmware block size
|
|
|
|
| LIBRARIES/DATA |
|
|
|
|
---------------------- END
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-02 12:37:47 +00:00
|
|
|
We replace the main firmware block (bytes 0x400..0x400+firmware_size)
|
|
|
|
as follows:
|
|
|
|
|
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
---------------------- 0x0
|
|
|
|
| |
|
|
|
|
| Dual-boot code |
|
|
|
|
| |
|
|
|
|
|----------------------|
|
|
|
|
| EMPTY SPACE |
|
|
|
|
|----------------------|
|
|
|
|
| |
|
|
|
|
| compressed RB image |
|
|
|
|
| |
|
|
|
|
|----------------------|
|
|
|
|
| |
|
|
|
|
| compressed OF image |
|
|
|
|
| |
|
|
|
|
|----------------------|
|
|
|
|
| UCL unpack function |
|
|
|
|
----------------------
|
2008-10-02 12:37:47 +00:00
|
|
|
|
|
|
|
This entire block fits into the space previously occupied by the main
|
2008-10-11 11:35:59 +00:00
|
|
|
firmware block - the space saved by compressing the OF image is used
|
2009-05-28 18:27:08 +00:00
|
|
|
to store the compressed version of the Rockbox bootloader.
|
|
|
|
|
|
|
|
On version 1 firmwares, the OF image is typically about 120KB, which allows
|
|
|
|
us to store a Rockbox bootloader with an uncompressed size of about 60KB-70KB.
|
|
|
|
Version 2 firmwares are bigger and are stored in SDRAM (instead of IRAM).
|
|
|
|
In both cases, the RAM we are using is mapped at offset 0x0.
|
2008-10-02 12:37:47 +00:00
|
|
|
|
|
|
|
mkamsboot then corrects the checksums and writes a new legal firmware
|
|
|
|
file which can be installed on the device.
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
When the Sansa device boots, this firmware block is loaded to RAM at
|
|
|
|
address 0x0 and executed.
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
Firstly, the dual-boot code will copy the UCL unpack function to the
|
|
|
|
end of RAM.
|
|
|
|
|
|
|
|
Then, depending on the detection of the dual-boot keypress, either the
|
|
|
|
OF image or the Rockbox image is copied to the end of RAM (just before
|
2009-05-28 18:27:08 +00:00
|
|
|
the ucl unpack function) and uncompressed to the start of RAM.
|
2008-10-11 11:35:59 +00:00
|
|
|
|
|
|
|
Finally, the ucl unpack function branches to address 0x0, passing
|
|
|
|
execution to the uncompressed firmware.
|
2008-10-01 09:15:44 +00:00
|
|
|
|
|
|
|
|
2008-05-11 18:29:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
#include <ucl/ucl.h>
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
#include "mkamsboot.h"
|
|
|
|
|
2008-10-30 00:13:29 +00:00
|
|
|
#include "md5.h"
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Header for ARM code binaries */
|
|
|
|
#include "dualboot.h"
|
2008-05-11 18:29:53 +00:00
|
|
|
|
|
|
|
/* Win32 compatibility */
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2010-01-13 03:05:29 +00:00
|
|
|
/* 4 for m200, 2 for e200/c200, 1 or 2 for fuze/clip, 1 for clip+ */
|
2009-11-04 20:58:40 +00:00
|
|
|
const unsigned short hw_revisions[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = 1,
|
|
|
|
[MODEL_CLIP] = 1,
|
|
|
|
[MODEL_CLIPV2] = 2,
|
|
|
|
[MODEL_E200V2] = 2,
|
|
|
|
[MODEL_M200V4] = 4,
|
|
|
|
[MODEL_C200V2] = 2,
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= 1,
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = 2,
|
2009-07-05 02:30:33 +00:00
|
|
|
};
|
|
|
|
|
2010-01-13 03:05:29 +00:00
|
|
|
/* version 2 is used in Clipv2, Clip+ and Fuzev2 firmwares */
|
2009-11-04 20:58:40 +00:00
|
|
|
const unsigned short fw_revisions[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = 1,
|
|
|
|
[MODEL_CLIP] = 1,
|
|
|
|
[MODEL_CLIPV2] = 2,
|
|
|
|
[MODEL_E200V2] = 1,
|
|
|
|
[MODEL_M200V4] = 1,
|
|
|
|
[MODEL_C200V2] = 1,
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= 2,
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = 2,
|
2009-07-05 02:30:33 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Descriptive name of these models */
|
2009-11-04 20:58:40 +00:00
|
|
|
const char* model_names[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = "Fuze",
|
|
|
|
[MODEL_CLIP] = "Clip",
|
|
|
|
[MODEL_CLIPV2] = "Clip",
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= "Clip+",
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_E200V2] = "e200",
|
|
|
|
[MODEL_M200V4] = "m200",
|
|
|
|
[MODEL_C200V2] = "c200",
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = "Fuze",
|
2008-10-11 11:35:59 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Dualboot functions for these models */
|
|
|
|
static const unsigned char* bootloaders[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = dualboot_fuze,
|
|
|
|
[MODEL_CLIP] = dualboot_clip,
|
|
|
|
[MODEL_CLIPV2] = dualboot_clipv2,
|
|
|
|
[MODEL_E200V2] = dualboot_e200v2,
|
|
|
|
[MODEL_M200V4] = dualboot_m200v4,
|
|
|
|
[MODEL_C200V2] = dualboot_c200v2,
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= dualboot_clipplus,
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = dualboot_fuzev2,
|
2008-10-11 11:35:59 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Size of dualboot functions for these models */
|
2009-11-04 20:58:40 +00:00
|
|
|
const int bootloader_sizes[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = sizeof(dualboot_fuze),
|
|
|
|
[MODEL_CLIP] = sizeof(dualboot_clip),
|
|
|
|
[MODEL_CLIPV2] = sizeof(dualboot_clipv2),
|
|
|
|
[MODEL_E200V2] = sizeof(dualboot_e200v2),
|
|
|
|
[MODEL_M200V4] = sizeof(dualboot_m200v4),
|
|
|
|
[MODEL_C200V2] = sizeof(dualboot_c200v2),
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= sizeof(dualboot_clipplus),
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = sizeof(dualboot_fuzev2),
|
2008-10-11 11:35:59 +00:00
|
|
|
};
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-12 19:34:47 +00:00
|
|
|
/* Model names used in the Rockbox header in ".sansa" files - these match the
|
|
|
|
-add parameter to the "scramble" tool */
|
2009-05-28 18:27:08 +00:00
|
|
|
static const char* rb_model_names[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = "fuze",
|
|
|
|
[MODEL_CLIP] = "clip",
|
|
|
|
[MODEL_CLIPV2] = "clv2",
|
|
|
|
[MODEL_E200V2] = "e2v2",
|
|
|
|
[MODEL_M200V4] = "m2v4",
|
|
|
|
[MODEL_C200V2] = "c2v2",
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_CLIPPLUS]= "cli+",
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = "fuz2",
|
2008-10-12 19:34:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Model numbers used to initialise the checksum in the Rockbox header in
|
|
|
|
".sansa" files - these are the same as MODEL_NUMBER in config-target.h */
|
2009-05-28 18:27:08 +00:00
|
|
|
static const int rb_model_num[] = {
|
2009-07-05 02:30:33 +00:00
|
|
|
[MODEL_FUZE] = 43,
|
|
|
|
[MODEL_CLIP] = 40,
|
|
|
|
[MODEL_CLIPV2] = 60,
|
|
|
|
[MODEL_E200V2] = 41,
|
|
|
|
[MODEL_M200V4] = 42,
|
2010-01-13 03:05:29 +00:00
|
|
|
[MODEL_C200V2] = 44,
|
|
|
|
[MODEL_CLIPPLUS]= 66,
|
2010-02-26 13:55:17 +00:00
|
|
|
[MODEL_FUZEV2] = 68,
|
2008-10-30 00:13:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Checksums of unmodified original firmwares - for safety, and device
|
|
|
|
detection */
|
|
|
|
static struct md5sums sansasums[] = {
|
|
|
|
/* NOTE: Different regional versions of the firmware normally only
|
|
|
|
differ in the filename - the md5sums are identical */
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
/* model version md5 */
|
|
|
|
{ MODEL_E200V2, "3.01.11", "e622ca8cb6df423f54b8b39628a1f0a3" },
|
|
|
|
{ MODEL_E200V2, "3.01.14", "2c1d0383fc3584b2cc83ba8cc2243af6" },
|
|
|
|
{ MODEL_E200V2, "3.01.16", "12563ad71b25a1034cf2092d1e0218c4" },
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
{ MODEL_FUZE, "1.01.11", "cac8ffa03c599330ac02c4d41de66166" },
|
|
|
|
{ MODEL_FUZE, "1.01.15", "df0e2c1612727f722c19a3c764cff7f2" },
|
|
|
|
{ MODEL_FUZE, "1.01.22", "5aff5486fe8dd64239cc71eac470af98" },
|
|
|
|
{ MODEL_FUZE, "1.02.26", "7c632c479461c48c8833baed74eb5e4f" },
|
2009-10-10 11:21:15 +00:00
|
|
|
{ MODEL_FUZE, "1.02.28", "5b34260f6470e75f702a9c6825471752" },
|
2010-01-21 14:34:04 +00:00
|
|
|
{ MODEL_FUZE, "1.02.31", "66d01b37462a5ef7ccc6ad37188b4235" },
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
{ MODEL_C200V2, "3.02.05", "b6378ebd720b0ade3fad4dc7ab61c1a5" },
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
{ MODEL_M200V4, "4.00.45", "82e3194310d1514e3bbcd06e84c4add3" },
|
|
|
|
{ MODEL_M200V4, "4.01.08-A", "fc9dd6116001b3e6a150b898f1b091f0" },
|
|
|
|
{ MODEL_M200V4, "4.01.08-E", "d3fb7d8ec8624ee65bc99f8dab0e2369" },
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
{ MODEL_CLIP, "1.01.17", "12caad785d506219d73f538772afd99e" },
|
|
|
|
{ MODEL_CLIP, "1.01.18", "d720b266bd5afa38a198986ef0508a45" },
|
|
|
|
{ MODEL_CLIP, "1.01.20", "236d8f75189f468462c03f6d292cf2ac" },
|
|
|
|
{ MODEL_CLIP, "1.01.29", "c12711342169c66e209540cd1f27cd26" },
|
|
|
|
{ MODEL_CLIP, "1.01.30", "f2974d47c536549c9d8259170f1dbe4d" },
|
|
|
|
{ MODEL_CLIP, "1.01.32", "d835d12342500732ffb9c4ee54abec15" },
|
2010-03-16 02:11:04 +00:00
|
|
|
{ MODEL_CLIP, "1.01.35", "b4d0edb3b8f2a4e8eee0a344f7f8e480" },
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
{ MODEL_CLIPV2, "2.01.16", "c57fb3fcbe07c2c9b360f060938f80cb" },
|
2010-01-13 03:05:29 +00:00
|
|
|
{ MODEL_CLIPV2, "2.01.32", "0ad3723e52022509089d938d0fbbf8c5" },
|
2010-03-22 08:32:04 +00:00
|
|
|
{ MODEL_CLIPV2, "2.01.35", "a3cbbd22b9508d7f8a9a1a39acc342c2" },
|
2010-01-13 03:05:29 +00:00
|
|
|
|
|
|
|
{ MODEL_CLIPPLUS, "01.02.09", "656d38114774c2001dc18e6726df3c5d" },
|
2010-04-17 12:17:01 +00:00
|
|
|
{ MODEL_CLIPPLUS, "01.02.13", "5f89872b79ef440b0e5ee3a7a44328b2" },
|
2010-02-26 13:55:17 +00:00
|
|
|
|
|
|
|
{ MODEL_FUZEV2, "2.01.17", "8b85fb05bf645d08a4c8c3e344ec9ebe" },
|
|
|
|
{ MODEL_FUZEV2, "2.02.26", "d4f6f85c3e4a8ea8f2e5acc421641801" },
|
2010-03-28 02:58:00 +00:00
|
|
|
{ MODEL_FUZEV2, "2.03.31", "74fb197ccd51707388f3b233402186a6" },
|
2008-10-30 00:13:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0]))
|
2008-05-11 18:29:53 +00:00
|
|
|
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
static unsigned int model_memory_size(int model)
|
|
|
|
{
|
|
|
|
if(model == MODEL_CLIPV2)
|
|
|
|
{
|
|
|
|
/* The decompressed Clipv2 OF is around 380kB.
|
|
|
|
* Since it doesn't fit in the 0x50000 bytes IRAM, the OF starts
|
|
|
|
* with DRAM mapped at 0x0
|
|
|
|
*
|
|
|
|
* We could use all the available memory (supposedly 8MB)
|
|
|
|
* but 1MB ought to be enough for our use
|
|
|
|
*/
|
|
|
|
return 1 << 20;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* The OF boots with IRAM (320kB) mapped at 0x0 */
|
|
|
|
return 320 << 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-15 13:04:21 +00:00
|
|
|
int firmware_revision(int model)
|
|
|
|
{
|
|
|
|
return fw_revisions[model];
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
static off_t filesize(int fd)
|
|
|
|
{
|
2008-05-11 18:29:53 +00:00
|
|
|
struct stat buf;
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (fstat(fd, &buf) < 0) {
|
2008-05-11 18:29:53 +00:00
|
|
|
perror("[ERR] Checking filesize of input file");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return(buf.st_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
static uint32_t get_uint32le(unsigned char* p)
|
|
|
|
{
|
2008-05-11 18:29:53 +00:00
|
|
|
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
static uint32_t get_uint32be(unsigned char* p)
|
|
|
|
{
|
2008-10-12 19:34:47 +00:00
|
|
|
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
static void put_uint32le(unsigned char* p, uint32_t x)
|
|
|
|
{
|
2008-05-11 18:29:53 +00:00
|
|
|
p[0] = x & 0xff;
|
|
|
|
p[1] = (x >> 8) & 0xff;
|
|
|
|
p[2] = (x >> 16) & 0xff;
|
|
|
|
p[3] = (x >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
void calc_MD5(unsigned char* buf, int len, char *md5str)
|
|
|
|
{
|
2008-10-30 00:13:29 +00:00
|
|
|
int i;
|
|
|
|
md5_context ctx;
|
|
|
|
unsigned char md5sum[16];
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2008-10-30 00:13:29 +00:00
|
|
|
md5_starts(&ctx);
|
|
|
|
md5_update(&ctx, buf, len);
|
|
|
|
md5_finish(&ctx, md5sum);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; ++i)
|
|
|
|
sprintf(md5str + 2*i, "%02x", md5sum[i]);
|
|
|
|
}
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Calculate a simple checksum used in Sansa Original Firmwares */
|
2009-05-28 21:19:21 +00:00
|
|
|
static uint32_t calc_checksum(unsigned char* buf, uint32_t n)
|
|
|
|
{
|
2008-05-11 18:29:53 +00:00
|
|
|
uint32_t sum = 0;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i=0;i<n;i+=4)
|
|
|
|
sum += get_uint32le(buf + i);
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:19:21 +00:00
|
|
|
static int get_model(int model_id)
|
|
|
|
{
|
2009-05-28 18:27:08 +00:00
|
|
|
switch(model_id) {
|
2008-10-11 11:35:59 +00:00
|
|
|
case 0x1e:
|
|
|
|
return MODEL_FUZE;
|
|
|
|
case 0x22:
|
|
|
|
return MODEL_CLIP;
|
|
|
|
case 0x23:
|
2008-12-24 04:23:52 +00:00
|
|
|
return MODEL_C200V2;
|
2008-10-11 11:35:59 +00:00
|
|
|
case 0x24:
|
2008-12-24 04:23:52 +00:00
|
|
|
return MODEL_E200V2;
|
2008-10-11 11:35:59 +00:00
|
|
|
case 0x25:
|
2008-12-24 04:23:52 +00:00
|
|
|
return MODEL_M200V4;
|
2008-10-11 11:35:59 +00:00
|
|
|
case 0x27:
|
|
|
|
return MODEL_CLIPV2;
|
2010-01-13 03:05:29 +00:00
|
|
|
case 0x28:
|
|
|
|
return MODEL_CLIPPLUS;
|
2010-02-26 13:55:17 +00:00
|
|
|
case 0x70:
|
|
|
|
return MODEL_FUZEV2;
|
2008-10-11 11:35:59 +00:00
|
|
|
}
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
return MODEL_UNKNOWN;
|
2008-05-11 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Compress using nrv2e algorithm : Thumb decompressor fits in 168 bytes ! */
|
2009-05-28 21:19:21 +00:00
|
|
|
static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize)
|
|
|
|
{
|
2008-10-11 11:35:59 +00:00
|
|
|
int maxsize;
|
|
|
|
unsigned char* outbuf;
|
|
|
|
int r;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* The following formula comes from the UCL documentation */
|
|
|
|
maxsize = insize + (insize / 8) + 256;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* Allocate some memory for the output buffer */
|
|
|
|
outbuf = malloc(maxsize);
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (outbuf == NULL)
|
2008-10-11 11:35:59 +00:00
|
|
|
return NULL;
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
r = ucl_nrv2e_99_compress(
|
|
|
|
(const ucl_bytep) inbuf,
|
|
|
|
(ucl_uint) insize,
|
|
|
|
(ucl_bytep) outbuf,
|
|
|
|
(ucl_uintp) outsize,
|
|
|
|
0, 10, NULL, NULL);
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (r != UCL_E_OK || *outsize > maxsize) {
|
2008-10-11 11:35:59 +00:00
|
|
|
/* this should NEVER happen, and implies memory corruption */
|
|
|
|
fprintf(stderr, "internal error - compression failed: %d\n", r);
|
|
|
|
free(outbuf);
|
|
|
|
return NULL;
|
2008-05-11 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
return outbuf;
|
|
|
|
}
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
#define ERROR(format, ...) \
|
|
|
|
do { \
|
|
|
|
snprintf(errstr, errstrsize, format, __VA_ARGS__); \
|
|
|
|
goto error; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Loads a Sansa AMS Original Firmware file into memory */
|
|
|
|
unsigned char* load_of_file(
|
2009-07-05 02:30:33 +00:00
|
|
|
char* filename, off_t* bufsize, struct md5sums *sum,
|
|
|
|
int* firmware_size, unsigned char** of_packed,
|
2009-05-28 21:19:21 +00:00
|
|
|
int* of_packedsize, char* errstr, int errstrsize)
|
|
|
|
{
|
2008-10-11 11:35:59 +00:00
|
|
|
int fd;
|
2009-05-28 18:27:08 +00:00
|
|
|
unsigned char* buf =NULL;
|
2008-10-11 11:35:59 +00:00
|
|
|
off_t n;
|
2009-05-28 18:27:08 +00:00
|
|
|
unsigned int i=0;
|
|
|
|
uint32_t checksum;
|
|
|
|
int model_id;
|
|
|
|
unsigned int last_word;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
fd = open(filename, O_RDONLY|O_BINARY);
|
|
|
|
if (fd < 0)
|
2009-05-28 18:27:08 +00:00
|
|
|
ERROR("[ERR] Could not open %s for reading\n", filename);
|
2008-10-01 09:15:44 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
*bufsize = filesize(fd);
|
2008-10-01 09:15:44 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
buf = malloc(*bufsize);
|
2009-05-28 18:27:08 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
ERROR("[ERR] Could not allocate memory for %s\n", filename);
|
2008-10-01 09:15:44 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
n = read(fd, buf, *bufsize);
|
2008-10-01 18:28:55 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (n != *bufsize)
|
|
|
|
ERROR("[ERR] Could not read file %s\n", filename);
|
|
|
|
|
|
|
|
/* check the file */
|
|
|
|
|
|
|
|
/* Calculate MD5 checksum of OF */
|
2009-07-05 02:30:33 +00:00
|
|
|
calc_MD5(buf, *bufsize, sum->md5);
|
2009-05-28 18:27:08 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, sum->md5) != 0))
|
2009-05-28 18:27:08 +00:00
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i < NUM_MD5S) {
|
2009-07-05 02:30:33 +00:00
|
|
|
*sum = sansasums[i];
|
2009-05-28 18:27:08 +00:00
|
|
|
} else {
|
2009-07-05 02:30:33 +00:00
|
|
|
int fw_version = (get_uint32le(&buf[0x204]) == 0x0000f000) ? 2 : 1;
|
|
|
|
model_id = buf[(fw_version == 2) ? 0x219 : 0x215];
|
|
|
|
sum->model = get_model(model_id);
|
|
|
|
|
|
|
|
if (sum->model == MODEL_UNKNOWN)
|
2009-06-04 16:31:11 +00:00
|
|
|
ERROR("[ERR] Unknown firmware model (v%d) - model id 0x%02x\n",
|
2009-07-05 02:30:33 +00:00
|
|
|
fw_version, model_id);
|
2009-06-04 16:31:11 +00:00
|
|
|
|
|
|
|
#if 1 /* comment to test new OFs */
|
|
|
|
char tested_versions[100];
|
|
|
|
tested_versions[0] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_MD5S ; i++)
|
2009-07-05 02:30:33 +00:00
|
|
|
if (sansasums[i].model == sum->model) {
|
2009-06-04 16:31:11 +00:00
|
|
|
if (tested_versions[0] != '\0') {
|
|
|
|
strncat(tested_versions, ", ",
|
|
|
|
sizeof(tested_versions) - strlen(tested_versions) - 1);
|
|
|
|
}
|
|
|
|
strncat(tested_versions, sansasums[i].version,
|
|
|
|
sizeof(tested_versions) - strlen(tested_versions) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR("[ERR] Original firmware unknown, please try an other version." \
|
|
|
|
" Tested %s versions are : %s\n",
|
2009-07-05 02:30:33 +00:00
|
|
|
model_names[sum->model], tested_versions);
|
2009-06-04 16:31:11 +00:00
|
|
|
#endif
|
2008-10-01 18:28:55 +00:00
|
|
|
}
|
2008-10-01 09:15:44 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* TODO: Do some more sanity checks on the OF image. Some images (like
|
|
|
|
m200v4) dont have a checksum at the end, only padding (0xdeadbeef). */
|
|
|
|
last_word = *bufsize - 4;
|
|
|
|
checksum = get_uint32le(buf + last_word);
|
|
|
|
if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word))
|
|
|
|
ERROR("%s", "[ERR] Whole file checksum failed\n");
|
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
if (bootloaders[sum->model] == NULL)
|
|
|
|
ERROR("[ERR] Unsupported model - \"%s\"\n", model_names[sum->model]);
|
2009-05-28 18:27:08 +00:00
|
|
|
|
|
|
|
/* Get the firmware size */
|
2009-07-05 02:30:33 +00:00
|
|
|
if (fw_revisions[sum->model] == 1)
|
2009-05-28 18:27:08 +00:00
|
|
|
*firmware_size = get_uint32le(&buf[0x0c]);
|
2009-07-05 02:30:33 +00:00
|
|
|
else if (fw_revisions[sum->model] == 2)
|
2009-05-28 18:27:08 +00:00
|
|
|
*firmware_size = get_uint32le(&buf[0x10]);
|
|
|
|
|
|
|
|
/* Compress the original firmware image */
|
|
|
|
*of_packed = uclpack(buf + 0x400, *firmware_size, of_packedsize);
|
|
|
|
if (*of_packed == NULL)
|
|
|
|
ERROR("[ERR] Could not compress %s\n", filename);
|
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
return buf;
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
error:
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-11 11:35:59 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Loads a rockbox bootloader file into memory */
|
|
|
|
unsigned char* load_rockbox_file(
|
|
|
|
char* filename, int model, int* bufsize, int* rb_packedsize,
|
2009-05-28 21:19:21 +00:00
|
|
|
char* errstr, int errstrsize)
|
|
|
|
{
|
2008-10-12 19:34:47 +00:00
|
|
|
int fd;
|
2009-05-28 18:27:08 +00:00
|
|
|
unsigned char* buf = NULL;
|
|
|
|
unsigned char* packed = NULL;
|
2008-10-12 19:34:47 +00:00
|
|
|
unsigned char header[8];
|
|
|
|
uint32_t sum;
|
|
|
|
off_t n;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY|O_BINARY);
|
|
|
|
if (fd < 0)
|
2009-05-28 18:27:08 +00:00
|
|
|
ERROR("[ERR] Could not open %s for reading\n", filename);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
|
|
|
/* Read Rockbox header */
|
|
|
|
n = read(fd, header, sizeof(header));
|
2009-05-28 18:27:08 +00:00
|
|
|
if (n != sizeof(header))
|
|
|
|
ERROR("[ERR] Could not read file %s\n", filename);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
|
|
|
/* Check for correct model string */
|
2009-05-28 18:27:08 +00:00
|
|
|
if (memcmp(rb_model_names[model], header + 4, 4)!=0)
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
ERROR("[ERR] Expected model name \"%s\" in %s, not \"%4.4s\"\n",
|
|
|
|
rb_model_names[model], filename, (char*)header+4);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
|
|
|
*bufsize = filesize(fd) - sizeof(header);
|
|
|
|
|
|
|
|
buf = malloc(*bufsize);
|
2009-05-28 18:27:08 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
ERROR("[ERR] Could not allocate memory for %s\n", filename);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
|
|
|
n = read(fd, buf, *bufsize);
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (n != *bufsize)
|
|
|
|
ERROR("[ERR] Could not read file %s\n", filename);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
|
|
|
/* Check checksum */
|
|
|
|
sum = rb_model_num[model];
|
|
|
|
for (i = 0; i < *bufsize; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
sum += buf[i];
|
|
|
|
}
|
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
if (sum != get_uint32be(header))
|
|
|
|
ERROR("[ERR] Checksum mismatch in %s\n", filename);
|
2008-10-12 19:34:47 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
packed = uclpack(buf, *bufsize, rb_packedsize);
|
|
|
|
if(packed == NULL)
|
|
|
|
ERROR("[ERR] Could not compress %s\n", filename);
|
2008-10-30 00:13:29 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
free(buf);
|
|
|
|
return packed;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
error:
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-11 11:35:59 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
#undef ERROR
|
2008-10-11 11:35:59 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Patches a Sansa AMS Original Firmware file */
|
|
|
|
void patch_firmware(
|
2009-07-05 02:30:33 +00:00
|
|
|
int model, int fw_revision, int firmware_size, unsigned char* buf,
|
2009-05-28 18:27:08 +00:00
|
|
|
int len, unsigned char* of_packed, int of_packedsize,
|
2009-05-28 21:19:21 +00:00
|
|
|
unsigned char* rb_packed, int rb_packedsize)
|
|
|
|
{
|
2009-05-28 18:27:08 +00:00
|
|
|
unsigned char *p;
|
|
|
|
uint32_t sum, filesum;
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
uint32_t ucl_dest;
|
2009-05-28 18:27:08 +00:00
|
|
|
unsigned int i;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-01 09:15:44 +00:00
|
|
|
/* Zero the original firmware area - not needed, but helps debugging */
|
|
|
|
memset(buf + 0x400, 0, firmware_size);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* Insert dual-boot bootloader at offset 0 */
|
|
|
|
memcpy(buf + 0x400, bootloaders[model], bootloader_sizes[model]);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* We are filling the firmware buffer backwards from the end */
|
|
|
|
p = buf + 0x400 + firmware_size;
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* 1 - UCL unpack function */
|
2008-11-14 22:16:22 +00:00
|
|
|
p -= sizeof(nrv2e_d8);
|
|
|
|
memcpy(p, nrv2e_d8, sizeof(nrv2e_d8));
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* 2 - Compressed copy of original firmware */
|
|
|
|
p -= of_packedsize;
|
|
|
|
memcpy(p, of_packed, of_packedsize);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* 3 - Compressed copy of Rockbox bootloader */
|
|
|
|
p -= rb_packedsize;
|
|
|
|
memcpy(p, rb_packed, rb_packedsize);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2009-05-28 18:27:08 +00:00
|
|
|
/* Write the locations of the various images to the variables at the
|
2008-10-11 11:35:59 +00:00
|
|
|
start of the dualboot image - we save the location of the last byte
|
|
|
|
in each image, along with the size in bytes */
|
|
|
|
|
|
|
|
/* UCL unpack function */
|
2008-10-11 12:02:23 +00:00
|
|
|
put_uint32le(&buf[0x420], firmware_size - 1);
|
2008-11-14 22:16:22 +00:00
|
|
|
put_uint32le(&buf[0x424], sizeof(nrv2e_d8));
|
2008-10-11 11:35:59 +00:00
|
|
|
|
|
|
|
/* Compressed original firmware image */
|
2008-11-14 22:16:22 +00:00
|
|
|
put_uint32le(&buf[0x428], firmware_size - sizeof(nrv2e_d8) - 1);
|
2008-10-11 11:35:59 +00:00
|
|
|
put_uint32le(&buf[0x42c], of_packedsize);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2008-10-11 11:35:59 +00:00
|
|
|
/* Compressed Rockbox image */
|
2009-05-28 18:27:08 +00:00
|
|
|
put_uint32le(&buf[0x430], firmware_size - sizeof(nrv2e_d8) - of_packedsize
|
|
|
|
- 1);
|
2008-10-11 11:35:59 +00:00
|
|
|
put_uint32le(&buf[0x434], rb_packedsize);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
ucl_dest = model_memory_size(model) - 1; /* last byte of memory */
|
|
|
|
put_uint32le(&buf[0x438], ucl_dest);
|
2008-10-11 11:35:59 +00:00
|
|
|
|
|
|
|
/* Update the firmware block checksum */
|
2009-05-28 18:27:08 +00:00
|
|
|
sum = calc_checksum(buf + 0x400, firmware_size);
|
2008-05-11 18:29:53 +00:00
|
|
|
|
2009-07-05 02:30:33 +00:00
|
|
|
if (fw_revision == 1) {
|
2008-10-11 11:35:59 +00:00
|
|
|
put_uint32le(&buf[0x04], sum);
|
|
|
|
put_uint32le(&buf[0x204], sum);
|
2009-07-05 02:30:33 +00:00
|
|
|
} else if (fw_revision == 2) {
|
2008-10-11 11:35:59 +00:00
|
|
|
put_uint32le(&buf[0x08], sum);
|
|
|
|
put_uint32le(&buf[0x208], sum);
|
|
|
|
|
|
|
|
/* Update the header checksums */
|
|
|
|
put_uint32le(&buf[0x1fc], calc_checksum(buf, 0x1fc));
|
|
|
|
put_uint32le(&buf[0x3fc], calc_checksum(buf + 0x200, 0x1fc));
|
|
|
|
}
|
2008-05-11 18:29:53 +00:00
|
|
|
|
|
|
|
/* Update the whole-file checksum */
|
|
|
|
filesum = 0;
|
2008-10-01 09:15:44 +00:00
|
|
|
for (i=0;i < (unsigned)len - 4; i+=4)
|
2008-05-11 18:29:53 +00:00
|
|
|
filesum += get_uint32le(&buf[i]);
|
|
|
|
|
2008-10-01 09:15:44 +00:00
|
|
|
put_uint32le(buf + len - 4, filesum);
|
2009-05-28 18:27:08 +00:00
|
|
|
}
|
2008-05-11 18:29:53 +00:00
|
|
|
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
/* returns != 0 if the firmware can be safely patched */
|
|
|
|
int check_sizes(int model, int rb_packed_size, int rb_unpacked_size,
|
|
|
|
int of_packed_size, int of_unpacked_size, int *total_size,
|
|
|
|
char *errstr, int errstrsize)
|
2009-05-28 21:19:21 +00:00
|
|
|
{
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
unsigned int packed_size = bootloader_sizes[model] + sizeof(nrv2e_d8) +
|
|
|
|
of_packed_size + rb_packed_size;
|
2009-05-28 18:27:08 +00:00
|
|
|
|
mkamsboot: prevents 2 potential problems
We checked if the new firmware block (bootloader+ucl function+packed
bootloader & OF) fit in the OF file, but not if it would run properly.
For example the Clipv2 OF is bigger than 0x50000 bytes uncompressed, but
it fitted in this space when packed and concatenated to a packed
bootloader + ucl function and dualboot code (but we use 1MB of RAM and
not 0x50000 anyway).
Now we check that both bootloader and OF are small enough to be unpacked
at runtime: the unpacked data must be smaller than available memory and
not overlap with ucl function and packed data (although the unpacked and
packed data could probably overlap a bit, I don't know how to calculate
this and this could be quite complex).
total_size() is replaced by check_sizes() which will perform all the
checks and set an error string if the firmware can't be patched.
(both mkamsboot and rbutilqt modified accordingly)
The second problem is that dualboot.S assumed r3 and r5 were left
untouched in the device specific checks. This was undocumented and very
error prone when modifying these checks.
r3 is the last byte of packed copy (bootloader or OF)
r5 is the entry point of uclunpack function derived from r3, so move r5
calculation after the device specific code.
Even if r3 is currently unused in the device specific code, we store it
in memory after copying the ucl function, when it points to the last byte
of packed data (not yet copied at this point since we didn't chose if we
boot the OF or the bootloader), and restore it just before using it so no
restriction is placed on registers usage in device specific code.
Add a new variable ucl_dest in dualboot.S set by mkamsboot.c, which
represents the last bound of buffer where we copy the ucl function, and
then the packed data (bootloader or OF).
RAM_SIZE definition is moved from dualboot.S to mkamsboot.c new
model_memory_size(), where it is a bit better documented.
Tested on e200v2 and Clip+
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24772 a1c6a512-1295-4272-9138-f99709370657
2010-02-19 14:10:26 +00:00
|
|
|
/* how much memory is available */
|
|
|
|
unsigned int memory_size = model_memory_size(model);
|
|
|
|
|
|
|
|
/* the memory used when unpacking the OF */
|
|
|
|
unsigned int ram_of = sizeof(nrv2e_d8) + of_packed_size + of_unpacked_size;
|
|
|
|
|
|
|
|
/* the memory used when unpacking the bootloader */
|
|
|
|
unsigned int ram_rb = sizeof(nrv2e_d8) + rb_packed_size + rb_unpacked_size;
|
|
|
|
|
|
|
|
*total_size = packed_size;
|
|
|
|
|
|
|
|
#define ERROR(format, ...) \
|
|
|
|
do { \
|
|
|
|
snprintf(errstr, errstrsize, format, __VA_ARGS__); \
|
|
|
|
return 0; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* will packed data fit in the OF file ? */
|
|
|
|
if(packed_size > of_unpacked_size)
|
|
|
|
ERROR(
|
|
|
|
"[ERR] Packed data (%d bytes) doesn't fit in the firmware "
|
|
|
|
"(%d bytes)\n", packed_size, of_unpacked_size
|
|
|
|
);
|
|
|
|
|
|
|
|
else if(ram_rb > memory_size)
|
|
|
|
ERROR("[ERR] Rockbox can't be unpacked at runtime, needs %d bytes "
|
|
|
|
"of memory and only %d available\n", ram_rb, memory_size
|
|
|
|
);
|
|
|
|
|
|
|
|
else if(ram_of > memory_size)
|
|
|
|
ERROR("[ERR] OF can't be unpacked at runtime, needs %d bytes "
|
|
|
|
"of memory and only %d available\n", ram_of, memory_size
|
|
|
|
);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
#undef ERROR
|
|
|
|
}
|