2002-03-28 14:34:13 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2002 - 2007 by Björn Stenberg
|
2002-03-28 14:34:13 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2002-03-28 14:34:13 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-07-22 13:21:50 +00:00
|
|
|
#include <stdbool.h>
|
2005-11-14 18:58:30 +00:00
|
|
|
#include <string.h>
|
2005-01-24 09:01:48 +00:00
|
|
|
#include "iriver.h"
|
2006-08-30 23:47:09 +00:00
|
|
|
#include "gigabeat.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "gigabeats.h"
|
2006-08-30 23:17:04 +00:00
|
|
|
#include "mi4.h"
|
2007-09-30 12:49:43 +00:00
|
|
|
#include "telechips.h"
|
2008-04-09 11:53:28 +00:00
|
|
|
#include "creative.h"
|
2008-03-18 20:58:52 +00:00
|
|
|
#include "iaudio_bl_flash.h"
|
2005-01-24 09:01:48 +00:00
|
|
|
|
2006-03-27 08:31:34 +00:00
|
|
|
int iaudio_encode(char *iname, char *oname, char *idstring);
|
2006-07-22 13:21:50 +00:00
|
|
|
int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc);
|
2009-07-17 14:30:42 +00:00
|
|
|
int ccpmp_encode(char *iname, char *oname);
|
2005-12-27 23:14:49 +00:00
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
enum
|
|
|
|
{
|
2005-09-14 11:15:59 +00:00
|
|
|
ARCHOS_PLAYER, /* and V1 recorder */
|
2005-09-14 07:48:45 +00:00
|
|
|
ARCHOS_V2RECORDER,
|
|
|
|
ARCHOS_FMRECORDER,
|
|
|
|
ARCHOS_ONDIO_SP,
|
|
|
|
ARCHOS_ONDIO_FM
|
|
|
|
};
|
|
|
|
|
2008-04-09 13:07:50 +00:00
|
|
|
static unsigned int size_limit[] =
|
2005-09-14 07:48:45 +00:00
|
|
|
{
|
|
|
|
0x32000, /* ARCHOS_PLAYER */
|
|
|
|
0x64000, /* ARCHOS_V2RECORDER */
|
|
|
|
0x64000, /* ARCHOS_FMRECORDER */
|
|
|
|
0x64000, /* ARCHOS_ONDIO_SP */
|
|
|
|
0x64000 /* ARCHOS_ONDIO_FM */
|
|
|
|
};
|
|
|
|
|
2006-07-21 16:16:48 +00:00
|
|
|
void short2le(unsigned short val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = val & 0xFF;
|
|
|
|
addr[1] = (val >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
2007-03-22 15:53:37 +00:00
|
|
|
unsigned int le2int(unsigned char* buf)
|
|
|
|
{
|
2007-03-22 15:59:58 +00:00
|
|
|
unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
2007-03-22 15:53:37 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
void int2le(unsigned int val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = val & 0xFF;
|
|
|
|
addr[1] = (val >> 8) & 0xff;
|
|
|
|
addr[2] = (val >> 16) & 0xff;
|
|
|
|
addr[3] = (val >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void int2be(unsigned int val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = (val >> 24) & 0xff;
|
|
|
|
addr[1] = (val >> 16) & 0xff;
|
|
|
|
addr[2] = (val >> 8) & 0xff;
|
|
|
|
addr[3] = val & 0xFF;
|
|
|
|
}
|
|
|
|
|
2008-03-18 20:58:52 +00:00
|
|
|
void short2be(unsigned short val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = (val >> 8) & 0xff;
|
|
|
|
addr[1] = val & 0xFF;
|
|
|
|
}
|
|
|
|
|
2005-01-24 09:01:48 +00:00
|
|
|
void usage(void)
|
|
|
|
{
|
|
|
|
printf("usage: scramble [options] <input file> <output file> [xor string]\n");
|
|
|
|
printf("options:\n"
|
|
|
|
"\t-fm Archos FM recorder format\n"
|
|
|
|
"\t-v2 Archos V2 recorder format\n"
|
|
|
|
"\t-ofm Archos Ondio FM recorder format\n"
|
|
|
|
"\t-osp Archos Ondio SP format\n"
|
|
|
|
"\t-neo SSI Neo format\n"
|
|
|
|
"\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n"
|
|
|
|
"\t-iriver iRiver format\n"
|
2006-03-27 08:31:34 +00:00
|
|
|
"\t-iaudiox5 iAudio X5 format\n"
|
|
|
|
"\t-iaudiox5v iAudio X5V format\n"
|
2007-02-28 07:42:03 +00:00
|
|
|
"\t-iaudiom5 iAudio M5 format\n"
|
2008-04-10 21:16:00 +00:00
|
|
|
"\t-iaudiom3 iAudio M3 format\n");
|
|
|
|
printf("\t-ipod3g ipod firmware partition format (3rd Gen)\n"
|
2006-07-22 13:21:50 +00:00
|
|
|
"\t-ipod4g ipod firmware partition format (4th Gen, Mini, Nano, Photo/Color)\n"
|
|
|
|
"\t-ipod5g ipod firmware partition format (5th Gen - aka Video)\n"
|
2008-04-09 11:53:28 +00:00
|
|
|
"\t-creative=X Creative firmware structure format\n"
|
2008-05-14 13:51:47 +00:00
|
|
|
"\t (X values: zvm, zvm60, zenvision\n"
|
|
|
|
"\t zenv, zen\n");
|
|
|
|
printf("\t-gigabeat Toshiba Gigabeat F/X format\n"
|
2007-09-21 15:51:53 +00:00
|
|
|
"\t-gigabeats Toshiba Gigabeat S format\n"
|
2006-08-30 23:17:04 +00:00
|
|
|
"\t-mi4v2 PortalPlayer .mi4 format (revision 010201)\n"
|
|
|
|
"\t-mi4v3 PortalPlayer .mi4 format (revision 010301)\n"
|
2007-02-23 22:45:00 +00:00
|
|
|
"\t-mi4r Sandisk Rhapsody .mi4 format\n"
|
2008-04-10 21:16:00 +00:00
|
|
|
"\t All mi4 options take two optional arguments:\n");
|
|
|
|
printf("\t -model=XXXX where XXXX is the model id string\n"
|
2007-03-14 13:51:05 +00:00
|
|
|
"\t -type=XXXX where XXXX is a string indicating the \n"
|
|
|
|
"\t type of binary, eg. RBOS, RBBL\n"
|
2007-09-30 12:49:43 +00:00
|
|
|
"\t-tcc=X Telechips generic firmware format (X values: sum, crc)\n"
|
2005-11-07 22:20:55 +00:00
|
|
|
"\t-add=X Rockbox generic \"add-up\" checksum format\n"
|
2007-02-23 19:38:38 +00:00
|
|
|
"\t (X values: h100, h120, h140, h300, ipco, nano, ipvd, mn2g\n"
|
2008-03-12 20:35:42 +00:00
|
|
|
"\t ip3g, ip4g, mini, iax5, iam5, iam3, h10, h10_5gb,\n"
|
2008-05-21 03:55:17 +00:00
|
|
|
"\t tpj2, c200, e200, giga, gigs, m100, m500, d2,\n");
|
2009-06-01 12:37:25 +00:00
|
|
|
printf("\t 9200, 1630, ldax, m200, c100, clip, e2v2, m2v4,\n"
|
2009-06-20 10:54:13 +00:00
|
|
|
"\t fuze, c2v2, clv2, y820, y920, y925, x747, 747p, \n"
|
|
|
|
"\t x777)\n");
|
2008-04-10 21:16:00 +00:00
|
|
|
printf("\nNo option results in Archos standard player/recorder format.\n");
|
2005-01-24 09:01:48 +00:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
int main (int argc, char** argv)
|
|
|
|
{
|
2008-04-15 07:39:21 +00:00
|
|
|
unsigned long length,i,slen=0;
|
2002-03-28 14:34:13 +00:00
|
|
|
unsigned char *inbuf,*outbuf;
|
|
|
|
unsigned short crc=0;
|
2005-01-28 10:28:34 +00:00
|
|
|
unsigned long chksum=0; /* 32 bit checksum */
|
2002-12-19 15:00:40 +00:00
|
|
|
unsigned char header[24];
|
2005-11-14 18:58:30 +00:00
|
|
|
char *iname = argv[1];
|
|
|
|
char *oname = argv[2];
|
2008-04-09 13:07:50 +00:00
|
|
|
char *xorstring=NULL;
|
2002-12-19 15:00:40 +00:00
|
|
|
int headerlen = 6;
|
2002-03-28 14:34:13 +00:00
|
|
|
FILE* file;
|
2008-04-09 13:07:50 +00:00
|
|
|
int version=0;
|
2005-11-07 22:20:55 +00:00
|
|
|
unsigned long modelnum;
|
|
|
|
char modelname[5];
|
2005-09-14 07:48:45 +00:00
|
|
|
int model_id;
|
2007-09-30 12:49:43 +00:00
|
|
|
enum { none, scramble, xor, tcc_sum, tcc_crc, add } method = scramble;
|
2008-08-03 19:32:14 +00:00
|
|
|
bool creative_enable_ciff;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
model_id = ARCHOS_PLAYER;
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
if (argc < 3) {
|
2005-01-24 09:01:48 +00:00
|
|
|
usage();
|
2002-03-28 14:34:13 +00:00
|
|
|
}
|
2002-12-19 15:00:40 +00:00
|
|
|
|
2003-10-27 18:04:28 +00:00
|
|
|
if(!strcmp(argv[1], "-fm")) {
|
2002-12-19 15:00:40 +00:00
|
|
|
headerlen = 24;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2003-10-27 18:04:28 +00:00
|
|
|
version = 4;
|
2005-09-14 07:48:45 +00:00
|
|
|
model_id = ARCHOS_FMRECORDER;
|
2003-10-27 18:04:28 +00:00
|
|
|
}
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
else if(!strcmp(argv[1], "-v2")) {
|
2003-10-27 18:04:28 +00:00
|
|
|
headerlen = 24;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
version = 2;
|
2005-09-14 07:48:45 +00:00
|
|
|
model_id = ARCHOS_V2RECORDER;
|
2002-12-19 15:00:40 +00:00
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
2004-09-08 21:21:22 +00:00
|
|
|
else if(!strcmp(argv[1], "-ofm")) {
|
|
|
|
headerlen = 24;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
version = 8;
|
2005-09-14 07:48:45 +00:00
|
|
|
model_id = ARCHOS_ONDIO_FM;
|
2004-09-08 21:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if(!strcmp(argv[1], "-osp")) {
|
|
|
|
headerlen = 24;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
version = 16;
|
2005-09-14 07:48:45 +00:00
|
|
|
model_id = ARCHOS_ONDIO_SP;
|
2004-09-08 21:21:22 +00:00
|
|
|
}
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
else if(!strcmp(argv[1], "-neo")) {
|
|
|
|
headerlen = 17;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2004-01-08 21:34:52 +00:00
|
|
|
method = none;
|
|
|
|
}
|
|
|
|
else if(!strncmp(argv[1], "-mm=", 4)) {
|
|
|
|
headerlen = 16;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
method = xor;
|
|
|
|
version = argv[1][4];
|
|
|
|
if (argc > 4)
|
|
|
|
xorstring = argv[4];
|
|
|
|
else {
|
|
|
|
printf("Multimedia needs an xor string\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
}
|
2007-09-30 12:49:43 +00:00
|
|
|
else if(!strncmp(argv[1], "-tcc=", 4)) {
|
|
|
|
headerlen = 0;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
|
|
|
|
if(!strcmp(&argv[1][5], "sum"))
|
|
|
|
method = tcc_sum;
|
|
|
|
else if(!strcmp(&argv[1][5], "crc"))
|
|
|
|
method = tcc_crc;
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "unsupported TCC method: %s\n", &argv[1][5]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
else if(!strncmp(argv[1], "-add=", 5)) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
method = add;
|
|
|
|
|
|
|
|
if(!strcmp(&argv[1][5], "h120"))
|
2005-11-07 22:20:55 +00:00
|
|
|
modelnum = 0;
|
2005-01-24 13:14:14 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "h140"))
|
2005-11-07 22:20:55 +00:00
|
|
|
modelnum = 0; /* the same as the h120 */
|
2005-01-24 13:14:14 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "h100"))
|
2005-11-07 22:20:55 +00:00
|
|
|
modelnum = 1;
|
2005-01-24 13:14:14 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "h300"))
|
2005-11-07 22:20:55 +00:00
|
|
|
modelnum = 2;
|
|
|
|
else if(!strcmp(&argv[1][5], "ipco"))
|
|
|
|
modelnum = 3;
|
|
|
|
else if(!strcmp(&argv[1][5], "nano"))
|
|
|
|
modelnum = 4;
|
2005-12-18 13:04:00 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "ipvd"))
|
|
|
|
modelnum = 5;
|
2006-01-12 00:35:50 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "fp7x"))
|
|
|
|
modelnum = 6;
|
2006-02-05 18:30:50 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "ip3g"))
|
|
|
|
modelnum = 7;
|
|
|
|
else if(!strcmp(&argv[1][5], "ip4g"))
|
|
|
|
modelnum = 8;
|
2006-02-21 15:01:25 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "mini"))
|
|
|
|
modelnum = 9;
|
2006-03-21 14:40:07 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "iax5"))
|
|
|
|
modelnum = 10;
|
2006-03-30 17:29:21 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "mn2g"))
|
|
|
|
modelnum = 11;
|
2006-08-28 08:11:32 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "h10"))
|
|
|
|
modelnum = 13;
|
|
|
|
else if(!strcmp(&argv[1][5], "h10_5gb"))
|
|
|
|
modelnum = 14;
|
2006-08-31 19:19:35 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "tpj2"))
|
|
|
|
modelnum = 15;
|
2006-12-19 11:33:53 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "e200"))
|
|
|
|
modelnum = 16;
|
2007-02-28 07:42:03 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "iam5"))
|
|
|
|
modelnum = 17;
|
2007-04-21 04:48:20 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "giga"))
|
|
|
|
modelnum = 18;
|
2007-07-26 23:02:37 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "1g2g"))
|
|
|
|
modelnum = 19;
|
2007-09-06 03:28:58 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "c200"))
|
|
|
|
modelnum = 20;
|
2007-09-30 17:12:20 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "gigs"))
|
2007-09-21 15:51:53 +00:00
|
|
|
modelnum = 21;
|
2007-09-30 17:12:20 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "m500"))
|
|
|
|
modelnum = 22;
|
2008-01-09 07:24:43 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "m100"))
|
|
|
|
modelnum = 23;
|
2008-03-03 23:43:19 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "d2"))
|
|
|
|
modelnum = 24;
|
2008-03-12 20:35:42 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "iam3"))
|
|
|
|
modelnum = 25;
|
2008-09-22 19:15:18 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "m200"))
|
2008-10-02 18:08:41 +00:00
|
|
|
modelnum = 29;
|
2009-06-01 12:37:25 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "c100"))
|
|
|
|
modelnum = 30;
|
2008-06-27 18:40:25 +00:00
|
|
|
else if(!strcmp(&argv[1][5], "1630")) /* Philips HDD1630 */
|
|
|
|
modelnum = 31;
|
2008-09-06 17:50:59 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "i7"))
|
2008-10-02 18:08:41 +00:00
|
|
|
modelnum = 32;
|
|
|
|
else if (!strcmp(&argv[1][5], "ldax"))
|
|
|
|
modelnum = 33;
|
|
|
|
else if(!strcmp(&argv[1][5], "9200")) /* Philips SA9200 */
|
|
|
|
modelnum = 34;
|
2008-10-12 19:34:47 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "clip"))
|
2008-10-19 15:21:47 +00:00
|
|
|
modelnum = 40;
|
2008-10-12 19:34:47 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "e2v2"))
|
2008-10-19 15:21:47 +00:00
|
|
|
modelnum = 41;
|
2008-11-11 00:25:34 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "m2v4"))
|
2008-10-19 15:21:47 +00:00
|
|
|
modelnum = 42;
|
2008-11-01 10:29:23 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "fuze"))
|
|
|
|
modelnum = 43;
|
2008-12-24 04:10:18 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "c2v2"))
|
|
|
|
modelnum = 44;
|
2009-02-09 10:02:38 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "x747"))
|
|
|
|
modelnum = 45;
|
2009-06-20 10:54:13 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "747p"))
|
|
|
|
modelnum = 54;
|
|
|
|
else if (!strcmp(&argv[1][5], "x777"))
|
|
|
|
modelnum = 61;
|
2009-05-25 21:10:45 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "y820")) /* Samsung YH-820 */
|
|
|
|
modelnum = 57;
|
|
|
|
else if (!strcmp(&argv[1][5], "y920")) /* Samsung YH-920 */
|
|
|
|
modelnum = 58;
|
|
|
|
else if (!strcmp(&argv[1][5], "y925")) /* Samsung YH-925 */
|
|
|
|
modelnum = 59;
|
2009-05-28 16:22:08 +00:00
|
|
|
else if (!strcmp(&argv[1][5], "clv2")) /* Sansa Clipv2 */
|
|
|
|
modelnum = 60;
|
2005-01-24 13:14:14 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "unsupported model: %s\n", &argv[1][5]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* we store a 4-letter model name too, for humans */
|
2005-11-07 22:20:55 +00:00
|
|
|
strcpy(modelname, &argv[1][5]);
|
|
|
|
chksum = modelnum; /* start checksum calcs with this */
|
2005-01-24 13:14:14 +00:00
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
2005-01-24 09:01:48 +00:00
|
|
|
else if(!strcmp(argv[1], "-iriver")) {
|
|
|
|
/* iRiver code dealt with in the iriver.c code */
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2008-06-18 22:30:59 +00:00
|
|
|
return (iriver_encode(iname, oname, FALSE) != 0) ? -1 : 0;
|
2005-01-24 09:01:48 +00:00
|
|
|
}
|
2006-08-30 23:47:09 +00:00
|
|
|
else if(!strcmp(argv[1], "-gigabeat")) {
|
|
|
|
/* iRiver code dealt with in the iriver.c code */
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
gigabeat_code(iname, oname);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-21 15:51:53 +00:00
|
|
|
else if(!strcmp(argv[1], "-gigabeats")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2009-05-25 21:42:44 +00:00
|
|
|
return gigabeat_s_code(iname, oname);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2006-03-27 08:31:34 +00:00
|
|
|
else if(!strcmp(argv[1], "-iaudiox5")) {
|
2005-12-27 23:14:49 +00:00
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2006-03-27 08:31:34 +00:00
|
|
|
return iaudio_encode(iname, oname, "COWON_X5_FW");
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[1], "-iaudiox5v")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return iaudio_encode(iname, oname, "COWON_X5V_FW");
|
2005-12-27 23:14:49 +00:00
|
|
|
}
|
2007-02-28 07:42:03 +00:00
|
|
|
else if(!strcmp(argv[1], "-iaudiom5")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return iaudio_encode(iname, oname, "COWON_M5_FW");
|
|
|
|
}
|
2008-03-12 20:35:42 +00:00
|
|
|
else if(!strcmp(argv[1], "-iaudiom3")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return iaudio_encode(iname, oname, "COWON_M3_FW");
|
|
|
|
}
|
2006-07-22 13:21:50 +00:00
|
|
|
else if(!strcmp(argv[1], "-ipod3g")) {
|
2006-07-21 16:16:48 +00:00
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2006-07-22 13:21:50 +00:00
|
|
|
return ipod_encode(iname, oname, 2, false); /* Firmware image v2 */
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[1], "-ipod4g")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return ipod_encode(iname, oname, 3, false); /* Firmware image v3 */
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[1], "-ipod5g")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return ipod_encode(iname, oname, 3, true); /* Firmware image v3 */
|
2006-07-21 16:16:48 +00:00
|
|
|
}
|
2009-07-17 14:30:42 +00:00
|
|
|
else if(!strncmp(argv[1], "-creative=", 10)) {
|
2008-08-03 19:32:14 +00:00
|
|
|
if(!strcmp(argv[2], "-no-ciff"))
|
|
|
|
{
|
|
|
|
creative_enable_ciff = false;
|
|
|
|
iname = argv[3];
|
|
|
|
oname = argv[4];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
creative_enable_ciff = true;
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
}
|
2008-04-09 11:53:28 +00:00
|
|
|
if(!strcmp(&argv[1][10], "zvm"))
|
2008-08-03 19:32:14 +00:00
|
|
|
return zvm_encode(iname, oname, ZENVISIONM, creative_enable_ciff);
|
2008-04-09 11:53:28 +00:00
|
|
|
else if(!strcmp(&argv[1][10], "zvm60"))
|
2008-08-03 19:32:14 +00:00
|
|
|
return zvm_encode(iname, oname, ZENVISIONM60, creative_enable_ciff);
|
2008-04-09 11:53:28 +00:00
|
|
|
else if(!strcmp(&argv[1][10], "zenvision"))
|
2008-08-03 19:32:14 +00:00
|
|
|
return zvm_encode(iname, oname, ZENVISION, creative_enable_ciff);
|
2008-04-09 11:53:28 +00:00
|
|
|
else if(!strcmp(&argv[1][10], "zenv"))
|
2008-08-03 19:32:14 +00:00
|
|
|
return zvm_encode(iname, oname, ZENV, creative_enable_ciff);
|
2008-04-24 20:08:28 +00:00
|
|
|
else if(!strcmp(&argv[1][10], "zen"))
|
2008-08-03 19:32:14 +00:00
|
|
|
return zvm_encode(iname, oname, ZEN, creative_enable_ciff);
|
|
|
|
else
|
|
|
|
{
|
2008-04-09 11:53:28 +00:00
|
|
|
fprintf(stderr, "unsupported Creative device: %s\n", &argv[1][10]);
|
|
|
|
return 2;
|
|
|
|
}
|
2008-02-18 19:08:29 +00:00
|
|
|
}
|
2009-07-17 14:30:42 +00:00
|
|
|
else if(!strcmp(argv[1], "-ccpmp")) {
|
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
|
|
|
return ccpmp_encode(iname, oname);
|
|
|
|
}
|
2007-03-14 13:51:05 +00:00
|
|
|
else if(!strncmp(argv[1], "-mi4", 4)) {
|
|
|
|
int mi4magic;
|
|
|
|
char model[4] = "";
|
|
|
|
char type[4] = "";
|
|
|
|
|
|
|
|
if(!strcmp(&argv[1][4], "v2")) {
|
|
|
|
mi4magic = MI4_MAGIC_DEFAULT;
|
|
|
|
version = 0x00010201;
|
|
|
|
}
|
|
|
|
else if(!strcmp(&argv[1][4], "v3")) {
|
|
|
|
mi4magic = MI4_MAGIC_DEFAULT;
|
|
|
|
version = 0x00010301;
|
|
|
|
}
|
|
|
|
else if(!strcmp(&argv[1][4], "r")) {
|
|
|
|
mi4magic = MI4_MAGIC_R;
|
|
|
|
version = 0x00010301;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf( "Invalid mi4 version: %s\n", &argv[1][4]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-23 22:45:00 +00:00
|
|
|
iname = argv[2];
|
|
|
|
oname = argv[3];
|
2007-03-14 13:51:05 +00:00
|
|
|
|
|
|
|
if(!strncmp(argv[2], "-model=", 7)) {
|
|
|
|
iname = argv[3];
|
|
|
|
oname = argv[4];
|
|
|
|
strncpy(model, &argv[2][7], 4);
|
|
|
|
|
|
|
|
if(!strncmp(argv[3], "-type=", 6)) {
|
|
|
|
iname = argv[4];
|
|
|
|
oname = argv[5];
|
|
|
|
strncpy(type, &argv[3][6], 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mi4_encode(iname, oname, version, mi4magic, model, type);
|
2006-08-30 23:17:04 +00:00
|
|
|
}
|
2005-01-24 09:01:48 +00:00
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
/* open file */
|
2002-12-19 15:00:40 +00:00
|
|
|
file = fopen(iname,"rb");
|
2002-03-28 14:34:13 +00:00
|
|
|
if (!file) {
|
2002-12-19 15:00:40 +00:00
|
|
|
perror(iname);
|
2002-03-28 14:34:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
length = ftell(file);
|
2003-04-12 21:17:47 +00:00
|
|
|
length = (length + 3) & ~3; /* Round up to nearest 4 byte boundary */
|
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
if ((method == scramble) &&
|
|
|
|
((length + headerlen) >= size_limit[model_id])) {
|
2008-04-09 13:07:50 +00:00
|
|
|
printf("error: firmware image is %ld bytes while max size is %u!\n",
|
2005-12-06 13:44:01 +00:00
|
|
|
length + headerlen,
|
|
|
|
size_limit[model_id]);
|
2003-04-14 09:59:47 +00:00
|
|
|
fclose(file);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-14 07:48:45 +00:00
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
inbuf = malloc(length);
|
2004-01-08 21:34:52 +00:00
|
|
|
if (method == xor)
|
|
|
|
outbuf = malloc(length*2);
|
2005-01-24 13:14:14 +00:00
|
|
|
else if(method == add)
|
|
|
|
outbuf = malloc(length + 8);
|
2004-01-08 21:34:52 +00:00
|
|
|
else
|
|
|
|
outbuf = malloc(length);
|
2002-03-28 14:34:13 +00:00
|
|
|
if ( !inbuf || !outbuf ) {
|
|
|
|
printf("out of memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
if(length> 4) {
|
|
|
|
/* zero-fill the last 4 bytes to make sure there's no rubbish there
|
|
|
|
when we write the size-aligned file later */
|
|
|
|
memset(outbuf+length-4, 0, 4);
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
|
|
/* read file */
|
|
|
|
i=fread(inbuf,1,length,file);
|
|
|
|
if ( !i ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
perror(iname);
|
2002-03-28 14:34:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
switch (method)
|
|
|
|
{
|
2005-01-24 13:14:14 +00:00
|
|
|
case add:
|
2005-01-28 12:20:20 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += inbuf[i];
|
2005-01-24 13:14:14 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-01-08 21:34:52 +00:00
|
|
|
case scramble:
|
|
|
|
slen = length/4;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
unsigned long addr = (i >> 2) + ((i % 4) * slen);
|
|
|
|
unsigned char data = inbuf[i];
|
|
|
|
data = ~((data << 1) | ((data >> 7) & 1)); /* poor man's ROL */
|
|
|
|
outbuf[addr] = data;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xor:
|
|
|
|
/* "compress" */
|
|
|
|
slen = 0;
|
|
|
|
for (i=0; i<length; i++) {
|
|
|
|
if (!(i&7))
|
|
|
|
outbuf[slen++] = 0xff; /* all data is uncompressed */
|
|
|
|
outbuf[slen++] = inbuf[i];
|
|
|
|
}
|
|
|
|
break;
|
2008-04-09 13:07:50 +00:00
|
|
|
case none:
|
|
|
|
default:
|
|
|
|
/* dummy case just to silence picky compilers */
|
|
|
|
break;
|
2002-03-28 14:34:13 +00:00
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
|
2007-09-30 12:49:43 +00:00
|
|
|
if((method == none) || (method == scramble) || (method == xor)) {
|
2005-01-24 13:14:14 +00:00
|
|
|
/* calculate checksum */
|
|
|
|
for (i=0;i<length;i++)
|
|
|
|
crc += inbuf[i];
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
2002-12-19 15:00:40 +00:00
|
|
|
memset(header, 0, sizeof header);
|
2004-01-08 21:34:52 +00:00
|
|
|
switch (method)
|
|
|
|
{
|
2005-01-24 13:14:14 +00:00
|
|
|
case add:
|
|
|
|
{
|
2005-01-28 10:28:34 +00:00
|
|
|
int2be(chksum, header); /* checksum, big-endian */
|
2005-11-07 22:20:55 +00:00
|
|
|
memcpy(&header[4], modelname, 4); /* 4 bytes model name */
|
2005-01-24 13:14:14 +00:00
|
|
|
memcpy(outbuf, inbuf, length); /* the input buffer to output*/
|
|
|
|
headerlen = 8;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-30 12:49:43 +00:00
|
|
|
|
|
|
|
case tcc_sum:
|
|
|
|
memcpy(outbuf, inbuf, length); /* the input buffer to output*/
|
|
|
|
telechips_encode_sum(outbuf, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tcc_crc:
|
|
|
|
memcpy(outbuf, inbuf, length); /* the input buffer to output*/
|
|
|
|
telechips_encode_crc(outbuf, length);
|
|
|
|
break;
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
case scramble:
|
|
|
|
if (headerlen == 6) {
|
|
|
|
int2be(length, header);
|
|
|
|
header[4] = (crc >> 8) & 0xff;
|
|
|
|
header[5] = crc & 0xff;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
header[0] =
|
|
|
|
header[1] =
|
|
|
|
header[2] =
|
|
|
|
header[3] = 0xff; /* ??? */
|
|
|
|
|
|
|
|
header[6] = (crc >> 8) & 0xff;
|
|
|
|
header[7] = crc & 0xff;
|
|
|
|
|
|
|
|
header[11] = version;
|
|
|
|
|
|
|
|
header[15] = headerlen; /* really? */
|
|
|
|
|
|
|
|
int2be(length, &header[20]);
|
|
|
|
}
|
|
|
|
break;
|
2003-12-12 14:07:28 +00:00
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
case xor:
|
|
|
|
{
|
|
|
|
int xorlen = strlen(xorstring);
|
2002-12-19 15:00:40 +00:00
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
/* xor data */
|
|
|
|
for (i=0; i<slen; i++)
|
|
|
|
outbuf[i] ^= xorstring[i & (xorlen-1)];
|
|
|
|
|
2004-03-17 23:43:42 +00:00
|
|
|
/* calculate checksum */
|
|
|
|
for (i=0; i<slen; i++)
|
|
|
|
crc += outbuf[i];
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
header[0] = header[2] = 'Z';
|
|
|
|
header[1] = header[3] = version;
|
|
|
|
int2le(length, &header[4]);
|
|
|
|
int2le(slen, &header[8]);
|
|
|
|
int2le(crc, &header[12]);
|
|
|
|
length = slen;
|
|
|
|
break;
|
2003-12-12 14:07:28 +00:00
|
|
|
}
|
2004-01-08 21:34:52 +00:00
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
#define MY_FIRMWARE_TYPE "Rockbox"
|
|
|
|
#define MY_HEADER_VERSION 1
|
2004-01-08 21:34:52 +00:00
|
|
|
default:
|
2005-11-14 18:58:30 +00:00
|
|
|
strncpy((char *)header, MY_FIRMWARE_TYPE,9);
|
2004-01-08 21:34:52 +00:00
|
|
|
header[9]='\0'; /*shouldn't have to, but to be SURE */
|
|
|
|
header[10]=MY_HEADER_VERSION&0xFF;
|
|
|
|
header[11]=(crc>>8)&0xFF;
|
|
|
|
header[12]=crc&0xFF;
|
|
|
|
int2be(sizeof(header), &header[12]);
|
|
|
|
break;
|
2002-12-19 15:00:40 +00:00
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
|
|
/* write file */
|
2002-12-19 15:00:40 +00:00
|
|
|
file = fopen(oname,"wb");
|
2002-03-28 14:34:13 +00:00
|
|
|
if ( !file ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
perror(oname);
|
2002-03-28 14:34:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-09-30 12:49:43 +00:00
|
|
|
if (headerlen > 0) {
|
|
|
|
if ( !fwrite(header,headerlen,1,file) ) {
|
|
|
|
perror(oname);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
}
|
|
|
|
if ( !fwrite(outbuf,length,1,file) ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
perror(oname);
|
2002-03-28 14:34:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
free(inbuf);
|
|
|
|
free(outbuf);
|
|
|
|
|
2003-04-14 09:59:47 +00:00
|
|
|
return 0;
|
2002-03-28 14:34:13 +00:00
|
|
|
}
|
2005-12-27 23:14:49 +00:00
|
|
|
|
2006-03-27 08:31:34 +00:00
|
|
|
int iaudio_encode(char *iname, char *oname, char *idstring)
|
2005-12-27 23:14:49 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int length;
|
|
|
|
FILE *file;
|
|
|
|
unsigned char *outbuf;
|
|
|
|
int i;
|
|
|
|
unsigned char sum = 0;
|
|
|
|
|
|
|
|
file = fopen(iname, "rb");
|
|
|
|
if (!file) {
|
|
|
|
perror(iname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
length = ftell(file);
|
|
|
|
|
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
outbuf = malloc(length+0x1030);
|
|
|
|
|
|
|
|
if ( !outbuf ) {
|
|
|
|
printf("out of memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fread(outbuf+0x1030, 1, length, file);
|
2008-04-09 13:07:50 +00:00
|
|
|
if(len < (size_t) length) {
|
2005-12-27 23:14:49 +00:00
|
|
|
perror(iname);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(outbuf, 0, 0x1030);
|
2006-03-27 08:31:34 +00:00
|
|
|
strcpy((char *)outbuf, idstring);
|
2008-03-18 20:58:52 +00:00
|
|
|
memcpy(outbuf+0x20, iaudio_bl_flash,
|
|
|
|
BMPWIDTH_iaudio_bl_flash * (BMPHEIGHT_iaudio_bl_flash/8) * 2);
|
|
|
|
short2be(BMPWIDTH_iaudio_bl_flash, &outbuf[0x10]);
|
|
|
|
short2be((BMPHEIGHT_iaudio_bl_flash/8), &outbuf[0x12]);
|
|
|
|
outbuf[0x19] = 2;
|
2005-12-27 23:14:49 +00:00
|
|
|
|
|
|
|
for(i = 0; i < length;i++)
|
|
|
|
sum += outbuf[0x1030 + i];
|
|
|
|
|
|
|
|
int2be(length, &outbuf[0x1024]);
|
|
|
|
outbuf[0x102b] = sum;
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
file = fopen(oname, "wb");
|
|
|
|
if (!file) {
|
|
|
|
perror(oname);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fwrite(outbuf, 1, length+0x1030, file);
|
2008-04-09 13:07:50 +00:00
|
|
|
if(len < (size_t)length) {
|
2005-12-27 23:14:49 +00:00
|
|
|
perror(oname);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
2008-04-09 13:07:50 +00:00
|
|
|
return 0;
|
2005-12-27 23:14:49 +00:00
|
|
|
}
|
2006-07-21 16:16:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Create an ipod firmware partition image
|
|
|
|
|
|
|
|
fw_ver = 2 for 3rd Gen ipods, 3 for all later ipods including 5g.
|
|
|
|
|
|
|
|
This function doesn't yet handle the Broadcom resource image for the 5g,
|
|
|
|
so the resulting images won't be usable.
|
|
|
|
|
|
|
|
This has also only been tested on an ipod Photo
|
|
|
|
*/
|
|
|
|
|
2006-07-22 13:21:50 +00:00
|
|
|
int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc)
|
2006-07-21 16:16:48 +00:00
|
|
|
{
|
|
|
|
static const char *apple_stop_sign = "{{~~ /-----\\ "\
|
|
|
|
"{{~~ / \\ "\
|
|
|
|
"{{~~| | "\
|
|
|
|
"{{~~| S T O P | "\
|
|
|
|
"{{~~| | "\
|
|
|
|
"{{~~ \\ / "\
|
|
|
|
"{{~~ \\-----/ "\
|
|
|
|
"Copyright(C) 200"\
|
|
|
|
"1 Apple Computer"\
|
|
|
|
", Inc.----------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"---------------";
|
|
|
|
size_t len;
|
|
|
|
int length;
|
2006-07-22 13:21:50 +00:00
|
|
|
int rsrclength;
|
|
|
|
int rsrcoffset;
|
2006-07-21 16:16:48 +00:00
|
|
|
FILE *file;
|
2006-07-22 13:21:50 +00:00
|
|
|
unsigned int sum = 0;
|
|
|
|
unsigned int rsrcsum = 0;
|
2006-07-21 16:16:48 +00:00
|
|
|
unsigned char *outbuf;
|
2006-07-22 13:21:50 +00:00
|
|
|
int bufsize;
|
2006-07-21 16:16:48 +00:00
|
|
|
int i;
|
2006-07-22 13:21:50 +00:00
|
|
|
|
2006-07-21 16:16:48 +00:00
|
|
|
file = fopen(iname, "rb");
|
|
|
|
if (!file) {
|
|
|
|
perror(iname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
length = ftell(file);
|
|
|
|
|
2006-07-22 13:21:50 +00:00
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
|
|
|
|
bufsize=(length+0x4600);
|
|
|
|
if (fake_rsrc) {
|
|
|
|
bufsize = (bufsize + 0x400) & ~0x200;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf = malloc(bufsize);
|
2006-07-21 16:16:48 +00:00
|
|
|
|
|
|
|
if ( !outbuf ) {
|
|
|
|
printf("out of memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fread(outbuf+0x4600, 1, length, file);
|
2008-04-09 13:07:50 +00:00
|
|
|
if(len < (size_t)length) {
|
2006-07-21 16:16:48 +00:00
|
|
|
perror(iname);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* Calculate checksum for later use in header */
|
|
|
|
for(i = 0x4600; i < 0x4600+length;i++)
|
|
|
|
sum += outbuf[i];
|
|
|
|
|
|
|
|
/* Clear the header area to zero */
|
|
|
|
memset(outbuf, 0, 0x4600);
|
|
|
|
|
|
|
|
/* APPLE STOP SIGN */
|
|
|
|
strcpy((char *)outbuf, apple_stop_sign);
|
|
|
|
|
|
|
|
/* VOLUME HEADER */
|
|
|
|
memcpy(&outbuf[0x100],"]ih[",4); /* Magic */
|
|
|
|
int2le(0x4000, &outbuf[0x104]); /* Firmware offset relative to 0x200 */
|
|
|
|
short2le(0x10c, &outbuf[0x108]); /* Location of extended header */
|
|
|
|
short2le(fw_ver, &outbuf[0x10a]);
|
|
|
|
|
|
|
|
/* Firmware Directory - "osos" entry */
|
|
|
|
memcpy(&outbuf[0x4200],"!ATAsoso",8); /* dev and type */
|
|
|
|
int2le(0, &outbuf[0x4208]); /* id */
|
|
|
|
int2le(0x4400, &outbuf[0x420c]); /* devOffset */
|
|
|
|
int2le(length, &outbuf[0x4210]); /* Length of firmware */
|
|
|
|
int2le(0x10000000, &outbuf[0x4214]); /* Addr */
|
|
|
|
int2le(0, &outbuf[0x4218]); /* Entry Offset */
|
|
|
|
int2le(sum, &outbuf[0x421c]); /* Checksum */
|
|
|
|
int2le(0x00006012, &outbuf[0x4220]); /* vers - 0x6012 is a guess */
|
|
|
|
int2le(0xffffffff, &outbuf[0x4224]); /* LoadAddr - for flash images */
|
|
|
|
|
2006-07-22 13:21:50 +00:00
|
|
|
/* "rsrc" entry (if applicable) */
|
|
|
|
if (fake_rsrc) {
|
|
|
|
rsrcoffset=(length+0x4600+0x200) & ~0x200;
|
|
|
|
rsrclength=0x200;
|
|
|
|
rsrcsum=0;
|
|
|
|
|
|
|
|
memcpy(&outbuf[0x4228],"!ATAcrsr",8); /* dev and type */
|
|
|
|
int2le(0, &outbuf[0x4230]); /* id */
|
|
|
|
int2le(rsrcoffset, &outbuf[0x4234]); /* devOffset */
|
|
|
|
int2le(rsrclength, &outbuf[0x4238]); /* Length of firmware */
|
|
|
|
int2le(0x10000000, &outbuf[0x423c]); /* Addr */
|
|
|
|
int2le(0, &outbuf[0x4240]); /* Entry Offset */
|
|
|
|
int2le(rsrcsum, &outbuf[0x4244]); /* Checksum */
|
|
|
|
int2le(0x0000b000, &outbuf[0x4248]); /* vers */
|
|
|
|
int2le(0xffffffff, &outbuf[0x424c]); /* LoadAddr - for flash images */
|
|
|
|
}
|
|
|
|
|
2006-07-21 16:16:48 +00:00
|
|
|
file = fopen(oname, "wb");
|
|
|
|
if (!file) {
|
|
|
|
perror(oname);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fwrite(outbuf, 1, length+0x4600, file);
|
2008-04-09 13:07:50 +00:00
|
|
|
if(len < (size_t)length) {
|
2006-07-21 16:16:48 +00:00
|
|
|
perror(oname);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
2006-08-31 10:09:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-07-21 16:16:48 +00:00
|
|
|
}
|
2008-02-18 19:08:29 +00:00
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
#define CCPMP_SIZE 0x500000
|
|
|
|
int ccpmp_encode(char *iname, char *oname)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int length;
|
|
|
|
FILE *file;
|
|
|
|
unsigned char *outbuf;
|
|
|
|
|
|
|
|
file = fopen(iname, "rb");
|
|
|
|
if (!file) {
|
|
|
|
perror(iname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
length = ftell(file);
|
|
|
|
|
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
|
|
|
|
outbuf = malloc(CCPMP_SIZE);
|
|
|
|
|
|
|
|
if ( !outbuf ) {
|
|
|
|
printf("out of memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fread(outbuf, 1, length, file);
|
|
|
|
if(len < (size_t)length) {
|
|
|
|
perror(iname);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* Clear the tail area to 0xFF */
|
|
|
|
memset(&outbuf[length], 0xFF, CCPMP_SIZE - length);
|
|
|
|
|
|
|
|
/* Header */
|
|
|
|
int2le(length, &outbuf[0x4]);
|
|
|
|
|
|
|
|
file = fopen(oname, "wb");
|
|
|
|
if (!file) {
|
|
|
|
perror(oname);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = fwrite(outbuf, 1, CCPMP_SIZE, file);
|
|
|
|
if(len < (size_t)length) {
|
|
|
|
perror(oname);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|