mkamsboot/rbutil/amsinfo : do not try to detect the model of a given Sansa AMS OF

The field we thought was representative of the model is not, it has
changed in the past for fuzev1 and fuzev2.
For example the value 0x23 is found in 2 old fuzev1 OF versions, and in
the c200v2 OF

The only reliable way to detect the model of a given OF is by using the
built-in list of md5sums.

Modify mkamsboot and rbutilqt to load the rockbox bootloader first, and
then check if the model in the bootloader corresponds to the model of
the known md5sum of the given OF.

That way we can continue to present the user with a list of known OF
versions in case the OF is unknown to mkamsboot

Also explicit the dependency of main.c on mkamsboot.h in case the
prototypes change
Correct the header's description not updated in r21648

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26248 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Rafaël Carré 2010-05-24 10:06:52 +00:00
parent eae2464e9a
commit ff6b0425e4
6 changed files with 60 additions and 116 deletions

View file

@ -47,9 +47,9 @@ OBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(SOURCES)))
LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(LIBSOURCES)))
EXTRADEPS := $(LIBUCL)
# explicit dependencies on dualboot.{c,h}
$(OBJDIR)mkamsboot.o: dualboot.h dualboot.c mkamsboot.c
$(OBJDIR)main.o: dualboot.h dualboot.c main.c
# explicit dependencies on dualboot.{c,h} and mkamsboot.h
$(OBJDIR)mkamsboot.o: dualboot.h dualboot.c mkamsboot.c mkamsboot.h
$(OBJDIR)main.o: dualboot.h dualboot.c main.c mkamsboot.h
$(OBJDIR)%.o: %.c
@echo CC $< $

View file

@ -59,6 +59,7 @@ int main(int argc, char* argv[])
int rb_packedsize;
int patchable;
int totalsize;
int model;
char errstr[200];
struct md5sums sum;
char md5sum[33]; /* 32 digits + \0 */
@ -81,11 +82,21 @@ int main(int argc, char* argv[])
bootfile = argv[2];
outfile = argv[3];
/* Load bootloader file */
rb_packed = load_rockbox_file(bootfile, &model, &bootloader_size,
&rb_packedsize, errstr, sizeof(errstr));
if (rb_packed == NULL) {
fprintf(stderr, "%s", errstr);
fprintf(stderr, "[ERR] Could not load %s\n", bootfile);
return 1;
}
/* Load original firmware file */
buf = load_of_file(infile, &len, &sum,
buf = load_of_file(infile, model, &len, &sum,
&firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));
if (buf == NULL) {
free(rb_packed);
fprintf(stderr, "%s", errstr);
fprintf(stderr, "[ERR] Could not load %s\n", infile);
return 1;
@ -96,17 +107,6 @@ int main(int argc, char* argv[])
model_names[sum.model], hw_revisions[sum.model], sum.version);
/* Load bootloader file */
rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size,
&rb_packedsize, errstr, sizeof(errstr));
if (rb_packed == NULL) {
fprintf(stderr, "%s", errstr);
fprintf(stderr, "[ERR] Could not load %s\n", bootfile);
free(buf);
free(of_packed);
return 1;
}
printf("[INFO] Firmware patching has begun !\n\n");
fprintf(stderr, "[INFO] Original firmware size: %d bytes\n",

View file

@ -323,30 +323,6 @@ static uint32_t calc_checksum(unsigned char* buf, uint32_t n)
return sum;
}
static int get_model(int model_id)
{
switch(model_id) {
case 0x1e:
return MODEL_FUZE;
case 0x22:
return MODEL_CLIP;
case 0x23:
return MODEL_C200V2;
case 0x24:
return MODEL_E200V2;
case 0x25:
return MODEL_M200V4;
case 0x27:
return MODEL_CLIPV2;
case 0x28:
return MODEL_CLIPPLUS;
case 0x70:
return MODEL_FUZEV2;
}
return MODEL_UNKNOWN;
}
/* Compress using nrv2e algorithm : Thumb decompressor fits in 168 bytes ! */
static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize)
{
@ -388,7 +364,7 @@ static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize)
/* Loads a Sansa AMS Original Firmware file into memory */
unsigned char* load_of_file(
char* filename, off_t* bufsize, struct md5sums *sum,
char* filename, int model, off_t* bufsize, struct md5sums *sum,
int* firmware_size, unsigned char** of_packed,
int* of_packedsize, char* errstr, int errstrsize)
{
@ -397,7 +373,6 @@ unsigned char* load_of_file(
off_t n;
unsigned int i=0;
uint32_t checksum;
int model_id;
unsigned int last_word;
fd = open(filename, O_RDONLY|O_BINARY);
@ -425,21 +400,20 @@ unsigned char* load_of_file(
if (i < NUM_MD5S) {
*sum = sansasums[i];
if(sum->model != model) {
ERROR("[ERR] OF File provided is %sv%d version %s, not for %sv%d\n",
model_names[sum->model], hw_revisions[sum->model],
sum->version, model_names[model], hw_revisions[model]
);
}
} else {
int fw_version = (get_uint32le(&buf[0x204]) == 0x0000f000) ? 2 : 1;
model_id = buf[(fw_version == 2) ? 0x219 : 0x215];
sum->model = get_model(model_id);
/* OF unknown, give a list of tested versions for the requested model */
if (sum->model == MODEL_UNKNOWN)
ERROR("[ERR] Unknown firmware model (v%d) - model id 0x%02x\n",
fw_version, model_id);
#if 1 /* comment to test new OFs */
char tested_versions[100];
tested_versions[0] = '\0';
for (i = 0; i < NUM_MD5S ; i++)
if (sansasums[i].model == sum->model) {
if (sansasums[i].model == model) {
if (tested_versions[0] != '\0') {
strncat(tested_versions, ", ",
sizeof(tested_versions) - strlen(tested_versions) - 1);
@ -449,9 +423,8 @@ unsigned char* load_of_file(
}
ERROR("[ERR] Original firmware unknown, please try an other version." \
" Tested %s versions are : %s\n",
model_names[sum->model], tested_versions);
#endif
" Tested %sv%d versions are : %s\n",
model_names[model], hw_revisions[model], tested_versions);
}
/* TODO: Do some more sanity checks on the OF image. Some images (like
@ -484,7 +457,7 @@ error:
/* Loads a rockbox bootloader file into memory */
unsigned char* load_rockbox_file(
char* filename, int model, int* bufsize, int* rb_packedsize,
char* filename, int *model, int* bufsize, int* rb_packedsize,
char* errstr, int errstrsize)
{
int fd;
@ -504,10 +477,12 @@ unsigned char* load_rockbox_file(
if (n != sizeof(header))
ERROR("[ERR] Could not read file %s\n", filename);
/* Check for correct model string */
if (memcmp(rb_model_names[model], header + 4, 4)!=0)
ERROR("[ERR] Expected model name \"%s\" in %s, not \"%4.4s\"\n",
rb_model_names[model], filename, (char*)header+4);
for(*model = 0; *model < NUM_MODELS; (*model)++)
if (memcmp(rb_model_names[*model], header + 4, 4) == 0)
break;
if(*model == NUM_MODELS)
ERROR("[ERR] Model name \"%4.4s\" unknown. Is this really a rockbox bootloader?\n", header + 4);
*bufsize = filesize(fd) - sizeof(header);
@ -521,7 +496,7 @@ unsigned char* load_rockbox_file(
ERROR("[ERR] Could not read file %s\n", filename);
/* Check checksum */
sum = rb_model_num[model];
sum = rb_model_num[*model];
for (i = 0; i < *bufsize; i++) {
/* add 8 unsigned bits but keep a 32 bit sum */
sum += buf[i];

View file

@ -43,6 +43,9 @@ enum {
MODEL_C200V2,
MODEL_CLIPPLUS,
MODEL_FUZEV2,
/* new models go here */
NUM_MODELS
};
@ -65,8 +68,7 @@ extern const int bootloader_sizes[];
* ARGUMENTS
*
* filename : bootloader file to load
* model : a 4 characters string representing the Sansa model
* ("fuze", "clip", "e2v2", "m2v4", or "c2v2")
* model : will be set to this bootloader's model
* bootloader_size : set to the uncompressed bootloader size
* rb_packed_size : set to the size of compressed bootloader
* errstr : provided buffer to store an eventual error
@ -78,7 +80,7 @@ extern const int bootloader_sizes[];
*/
unsigned char* load_rockbox_file(
char* filename, int model, int* bootloader_size, int* rb_packedsize,
char* filename, int *model, int* bootloader_size, int* rb_packedsize,
char* errstr, int errstrsize);
@ -89,10 +91,9 @@ unsigned char* load_rockbox_file(
* ARGUMENTS
*
* filename : firmware file to load
* model : desired player's model
* bufsize : set to firmware file size
* md5sum : set to file md5sum, must be at least 33 bytes long
* model : set to firmware model (MODEL_XXX)
* fw_version : set to firmware format version (1 or 2)
* firmware_size : set to firmware block's size
* of_packed : pointer to allocated memory containing the compressed
* original firmware block
@ -106,7 +107,7 @@ unsigned char* load_rockbox_file(
*/
unsigned char* load_of_file(
char* filename, off_t* bufsize, struct md5sums *sum,
char* filename, int model, off_t* bufsize, struct md5sums *sum,
int* firmware_size, unsigned char** of_packed,
int* of_packedsize, char* errstr, int errstrsize);

View file

@ -72,6 +72,7 @@ void BootloaderInstallAms::installStage2(void)
struct md5sums sum;
char md5sum[33]; /* 32 hex digits, plus terminating zero */
int n;
int model;
int firmware_size;
int bootloader_size;
int patchable;
@ -84,33 +85,33 @@ void BootloaderInstallAms::installStage2(void)
QString bootfile = m_tempfile.fileName();
m_tempfile.close();
/* Load original firmware file */
buf = load_of_file(m_offile.toLocal8Bit().data(), &len,&sum,&firmware_size,
&of_packed,&of_packedsize,errstr,sizeof(errstr));
if (buf == NULL)
{
qDebug() << "[BootloaderInstallAms] could not load OF: " << m_offile;
emit logItem(errstr, LOGERROR);
emit logItem(tr("Could not load %1").arg(m_offile), LOGERROR);
emit done(true);
return;
}
/* Load bootloader file */
rb_packed = load_rockbox_file(bootfile.toLocal8Bit().data(), sum.model,
rb_packed = load_rockbox_file(bootfile.toLocal8Bit().data(), &model,
&bootloader_size,&rb_packedsize,
errstr,sizeof(errstr));
if (rb_packed == NULL)
if (rb_packed == NULL)
{
qDebug() << "[BootloaderInstallAms] could not load bootloader: " << bootfile;
emit logItem(errstr, LOGERROR);
emit logItem(tr("Could not load %1").arg(bootfile), LOGERROR);
free(buf);
free(of_packed);
emit done(true);
return;
}
/* Load original firmware file */
buf = load_of_file(m_offile.toLocal8Bit().data(), model, &len, &sum,
&firmware_size, &of_packed ,&of_packedsize,
errstr, sizeof(errstr));
if (buf == NULL)
{
qDebug() << "[BootloaderInstallAms] could not load OF: " << m_offile;
emit logItem(errstr, LOGERROR);
emit logItem(tr("Could not load %1").arg(m_offile), LOGERROR);
free(rb_packed);
emit done(true);
return;
}
/* check total size */
patchable = check_sizes(sum.model, rb_packedsize, bootloader_size,
of_packedsize, firmware_size, &totalsize, errstr, sizeof(errstr));

View file

@ -66,7 +66,7 @@ uint8_t *buf; /* file content */
/* 1st block description */
uint32_t idx,checksum,bs_multiplier,firmware_sz;
uint32_t unknown_4_1; uint8_t unknown_1,id; uint16_t unknown_2;
uint32_t unknown_4_1; uint16_t unknown_1, unknown_2;
uint32_t unknown_4_2,unknown_4_3;
static void *xmalloc(size_t s) /* malloc helper */
@ -76,28 +76,6 @@ static void *xmalloc(size_t s) /* malloc helper */
return r;
}
/* known models */
static const char * model(uint8_t id)
{
switch(id)
{
case 0x1E: return "FUZE"; break;
case 0x22: return "CLIP"; break;
case 0x23: return "C200"; break;
case 0x24: return "E200"; break;
case 0x25: return "M200"; break;
case 0x27: return "CLV2"; break;
case 0x28: return "CLI+"; break;
case 0x70:
case 0x6d: return "FUZ2"; break;
default:
printf("Unknown ID 0x%x\n", id);
assert(id == 0x1E || (id >= 0x22 && id <= 0x28));
return "UNKNOWN!";
}
}
/* checksums the firmware (the firmware header contains the verification) */
static uint32_t do_checksum(void)
{
@ -142,8 +120,7 @@ static void check(void)
assert(bs_multiplier << 9 == PAD_TO_BOUNDARY(firmware_sz)); /* 0x200 * bs_multiplier */
unknown_4_1 = get32le(0x10 + shift);
unknown_1 = buf[0x14 + shift];
id = buf[0x15 + shift];
unknown_1 = get16le(0x14 + shift);
unknown_2 = get16le(0x16 + shift);
unknown_4_2 = get32le(0x18 + shift);
unknown_4_3 = get32le(0x1c + shift);
@ -170,9 +147,6 @@ static void check(void)
color(GREEN);
printf("1 Unknown %x\n",unknown_1);
color(GREEN);
printf("1 Model ID %x (%s)\n",id,model(id));
color(GREEN);
printf("2 Unknown (should be 0) %x\n",unknown_2);
assert(unknown_2 == 0);
@ -185,13 +159,6 @@ static void check(void)
printf("4 Unknown (should be 1) %x\n",unknown_4_3);
assert(unknown_4_3 == 1);
/* rest of the block is padded with 0xff */
for(i=0x20 + shift;i<0x200 - shift;i++)
assert(buf[i]==0xff /* normal case */ ||
((id==0x1e||id==0x24) && ( /* Fuze or E200 */
(i>=0x3c && i<=0x3f && get32le(0x3c)==0x00005000)
)));
/* the 2nd block is identical, except that the 1st byte has been incremented */
assert(buf[0x0]==0&&buf[0x200]==1);
assert(!memcmp(&buf[1],&buf[0x201],0x1FF - shift));