2006-12-13 08:57:06 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-02-05 01:20:20 +00:00
|
|
|
* Copyright (C) 2006-2007 Dave Chapman
|
2006-12-13 08:57:06 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-12-13 09:02:18 +00:00
|
|
|
#include <inttypes.h>
|
2006-12-13 08:57:06 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "parttypes.h"
|
2006-12-13 09:02:18 +00:00
|
|
|
#include "ipodio.h"
|
|
|
|
|
2007-02-06 21:00:56 +00:00
|
|
|
#define VERSION "0.8svn"
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-14 18:41:03 +00:00
|
|
|
int verbose = 0;
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
/* The following string appears at the start of the firmware partition */
|
|
|
|
static const char *apple_stop_sign = "{{~~ /-----\\ "\
|
|
|
|
"{{~~ / \\ "\
|
|
|
|
"{{~~| | "\
|
|
|
|
"{{~~| S T O P | "\
|
|
|
|
"{{~~| | "\
|
|
|
|
"{{~~ \\ / "\
|
|
|
|
"{{~~ \\-----/ "\
|
|
|
|
"Copyright(C) 200"\
|
|
|
|
"1 Apple Computer"\
|
|
|
|
", Inc.----------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"----------------"\
|
|
|
|
"---------------";
|
|
|
|
|
2006-12-13 08:57:06 +00:00
|
|
|
/* Windows requires the buffer for disk I/O to be aligned in memory on a
|
|
|
|
multiple of the disk volume size - so we use a single global variable
|
2006-12-13 09:02:18 +00:00
|
|
|
and initialise it with ipod_alloc_buf()
|
2006-12-13 08:57:06 +00:00
|
|
|
*/
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
/* Size of buffer for disk I/O - 8MB is large enough for any version
|
|
|
|
of the Apple firmware, but not the Nano's RSRC image. */
|
|
|
|
#define BUFFER_SIZE 8*1024*1024
|
2006-12-13 08:57:06 +00:00
|
|
|
unsigned char* sectorbuf;
|
|
|
|
|
|
|
|
char* get_parttype(int pt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static char unknown[]="Unknown";
|
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
if (pt == -1) {
|
|
|
|
return "HFS/HFS+";
|
|
|
|
}
|
|
|
|
|
2006-12-13 08:57:06 +00:00
|
|
|
i=0;
|
|
|
|
while (parttypes[i].name != NULL) {
|
|
|
|
if (parttypes[i].type == pt) {
|
|
|
|
return (parttypes[i].name);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t filesize(int fd) {
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (fstat(fd,&buf) < 0) {
|
|
|
|
perror("[ERR] Checking filesize of input file");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return(buf.st_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Partition table parsing code taken from Rockbox */
|
|
|
|
|
2006-12-13 08:59:07 +00:00
|
|
|
#define MAX_SECTOR_SIZE 2048
|
2006-12-13 08:57:06 +00:00
|
|
|
#define SECTOR_SIZE 512
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
unsigned short static inline le2ushort(unsigned char* buf)
|
|
|
|
{
|
|
|
|
unsigned short res = (buf[1] << 8) | buf[0];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
int static inline le2int(unsigned char* buf)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
int static inline be2int(unsigned char* buf)
|
|
|
|
{
|
|
|
|
int32_t res = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
int static inline getint16le(char* buf)
|
|
|
|
{
|
|
|
|
int16_t res = (buf[1] << 8) | buf[0];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
void static inline short2le(unsigned short val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = val & 0xFF;
|
|
|
|
addr[1] = (val >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void static inline 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;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define BYTES2INT32(array,pos)\
|
|
|
|
((long)array[pos] | ((long)array[pos+1] << 8 ) |\
|
2006-12-13 08:57:06 +00:00
|
|
|
((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
void display_partinfo(struct ipod_t* ipod)
|
2006-12-13 08:57:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-02-04 11:42:11 +00:00
|
|
|
double sectors_per_MB = (1024.0*1024.0)/ipod->sector_size;
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
|
2006-12-13 08:57:06 +00:00
|
|
|
for ( i = 0; i < 4; i++ ) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->pinfo[i].start != 0) {
|
2006-12-13 09:02:18 +00:00
|
|
|
printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
|
|
|
|
i,
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->pinfo[i].start,
|
|
|
|
ipod->pinfo[i].start+ipod->pinfo[i].size-1,
|
|
|
|
ipod->pinfo[i].size/sectors_per_MB,
|
|
|
|
get_parttype(ipod->pinfo[i].type),
|
|
|
|
ipod->pinfo[i].type);
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int read_partinfo(struct ipod_t* ipod, int silent)
|
2006-12-13 08:57:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long count;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
count = ipod_read(ipod,sectorbuf, ipod->sector_size);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
if (count <= 0) {
|
|
|
|
print_error(" Error reading from disk: ");
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
if ((sectorbuf[510] == 0x55) && (sectorbuf[511] == 0xaa)) {
|
|
|
|
/* DOS partition table */
|
|
|
|
if ((memcmp(§orbuf[71],"iPod",4) != 0) &&
|
|
|
|
(memcmp(§orbuf[0x40],"This is your Apple iPod. You probably do not want to boot from it!",66) != 0) ) {
|
|
|
|
if (!silent) fprintf(stderr,"[ERR] Drive is not an iPod, aborting\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
ipod->macpod = 0;
|
|
|
|
/* parse partitions */
|
|
|
|
for ( i = 0; i < 4; i++ ) {
|
|
|
|
unsigned char* ptr = sectorbuf + 0x1be + 16*i;
|
|
|
|
ipod->pinfo[i].type = ptr[4];
|
|
|
|
ipod->pinfo[i].start = BYTES2INT32(ptr, 8);
|
|
|
|
ipod->pinfo[i].size = BYTES2INT32(ptr, 12);
|
|
|
|
|
|
|
|
/* extended? */
|
|
|
|
if ( ipod->pinfo[i].type == 5 ) {
|
|
|
|
/* not handled yet */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((sectorbuf[0] == 'E') && (sectorbuf[1] == 'R')) {
|
|
|
|
/* Apple Partition Map */
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
/* APM parsing code based on the check_mac_partitions() function in
|
|
|
|
ipodloader2 - written by Thomas Tempelmann and released
|
|
|
|
under the GPL. */
|
|
|
|
|
|
|
|
int blkNo = 1;
|
|
|
|
int partBlkCount = 1;
|
|
|
|
int partBlkSizMul = sectorbuf[2] / 2;
|
|
|
|
|
|
|
|
int pmMapBlkCnt; /* # of blks in partition map */
|
|
|
|
int pmPyPartStart; /* physical start blk of partition */
|
|
|
|
int pmPartBlkCnt; /* # of blks in this partition */
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
ipod->macpod = 1;
|
|
|
|
|
|
|
|
memset(ipod->pinfo,0,sizeof(ipod->pinfo));
|
|
|
|
|
|
|
|
while (blkNo <= partBlkCount) {
|
|
|
|
if (ipod_seek(ipod, blkNo * partBlkSizMul * 512) < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Seek failed whilst reading APM\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = ipod_read(ipod, sectorbuf, ipod->sector_size);
|
|
|
|
|
|
|
|
if (count <= 0) {
|
|
|
|
print_error(" Error reading from disk: ");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if it's a partition entry */
|
|
|
|
if ((sectorbuf[0] != 'P') || (sectorbuf[1] != 'M')) {
|
|
|
|
/* end of partition table -> leave the loop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the interesting entries */
|
|
|
|
pmMapBlkCnt = be2int(sectorbuf + 4);
|
|
|
|
pmPyPartStart = be2int(sectorbuf + 8);
|
|
|
|
pmPartBlkCnt = be2int(sectorbuf + 12);
|
|
|
|
|
|
|
|
/* update the number of part map blocks */
|
|
|
|
partBlkCount = pmMapBlkCnt;
|
|
|
|
|
|
|
|
if (strncmp((char*)(sectorbuf + 48), "Apple_MDFW", 32)==0) {
|
|
|
|
/* A Firmware partition */
|
|
|
|
ipod->pinfo[i].start = pmPyPartStart;
|
|
|
|
ipod->pinfo[i].size = pmPartBlkCnt;
|
|
|
|
ipod->pinfo[i].type = 0;
|
|
|
|
i++;
|
|
|
|
} else if (strncmp((char*)(sectorbuf + 48), "Apple_HFS", 32)==0) {
|
|
|
|
/* A HFS partition */
|
|
|
|
ipod->pinfo[i].start = pmPyPartStart;
|
|
|
|
ipod->pinfo[i].size = pmPartBlkCnt;
|
|
|
|
ipod->pinfo[i].type = -1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
blkNo++; /* read next partition map entry */
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
2007-02-05 18:29:39 +00:00
|
|
|
} else {
|
|
|
|
if (!silent) fprintf(stderr,"[ERR] Bad boot sector signature\n");
|
|
|
|
return -1;
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->start = ipod->pinfo[0].start*ipod->sector_size;
|
2006-12-13 08:57:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int read_partition(struct ipod_t* ipod, int outfile)
|
2006-12-13 08:57:06 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
unsigned long n;
|
|
|
|
int bytesleft;
|
|
|
|
int chunksize;
|
2007-02-04 11:42:11 +00:00
|
|
|
int count = ipod->pinfo[0].size;
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start) < 0) {
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"[INFO] Writing %d sectors to output file\n",count);
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
bytesleft = count * ipod->sector_size;
|
2006-12-13 08:57:06 +00:00
|
|
|
while (bytesleft > 0) {
|
|
|
|
if (bytesleft > BUFFER_SIZE) {
|
|
|
|
chunksize = BUFFER_SIZE;
|
|
|
|
} else {
|
|
|
|
chunksize = bytesleft;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n = ipod_read(ipod, sectorbuf, chunksize);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
if (n < 0) {
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < chunksize) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"[ERR] Short read in disk_read() - requested %d, got %lu\n",
|
|
|
|
chunksize,n);
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesleft -= n;
|
|
|
|
|
|
|
|
res = write(outfile,sectorbuf,n);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
perror("[ERR] write in disk_read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res != n) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Short write - requested %lu, received %d - aborting.\n",n,res);
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Done.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int write_partition(struct ipod_t* ipod, int infile)
|
2006-12-13 08:57:06 +00:00
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
int n;
|
|
|
|
int bytesread;
|
|
|
|
int byteswritten = 0;
|
|
|
|
int eof;
|
|
|
|
int padding = 0;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start) < 0) {
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Writing input file to device\n");
|
|
|
|
bytesread = 0;
|
|
|
|
eof = 0;
|
|
|
|
while (!eof) {
|
|
|
|
n = read(infile,sectorbuf,BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
perror("[ERR] read in disk_write");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < BUFFER_SIZE) {
|
|
|
|
eof = 1;
|
|
|
|
/* We need to pad the last write to a multiple of SECTOR_SIZE */
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n % ipod->sector_size) != 0) {
|
|
|
|
padding = (ipod->sector_size-(n % ipod->sector_size));
|
2006-12-13 08:57:06 +00:00
|
|
|
n += padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesread += n;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
res = ipod_write(ipod, sectorbuf, n);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
print_error(" Error writing to disk: ");
|
2006-12-13 08:57:06 +00:00
|
|
|
fprintf(stderr,"Bytes written: %d\n",byteswritten);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res != n) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] Short write - requested %d, received %lu - aborting.\n",n,res);
|
2006-12-13 08:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
byteswritten += res;
|
|
|
|
}
|
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[INFO] Wrote %d bytes plus %d bytes padding.\n",
|
|
|
|
byteswritten-padding,padding);
|
2006-12-13 08:57:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
void print_macpod_warning(void)
|
|
|
|
{
|
|
|
|
printf("[INFO] ************************************************************************\n");
|
|
|
|
printf("[INFO] *** WARNING FOR ROCKBOX USERS\n");
|
|
|
|
printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n");
|
|
|
|
printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n");
|
|
|
|
printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n");
|
|
|
|
printf("[INFO] ************************************************************************\n");
|
|
|
|
}
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
void print_usage(void)
|
|
|
|
{
|
2006-12-17 23:00:15 +00:00
|
|
|
fprintf(stderr,"Usage: ipodpatcher --scan\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
#ifdef __WIN32__
|
2006-12-17 23:00:15 +00:00
|
|
|
fprintf(stderr," or ipodpatcher DISKNO [action]\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
#else
|
2006-12-17 23:00:15 +00:00
|
|
|
fprintf(stderr," or ipodpatcher device [action]\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
#endif
|
|
|
|
fprintf(stderr,"\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"Where [action] is one of the following options:\n");
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr," -l, --list\n");
|
|
|
|
fprintf(stderr," -r, --read-partition bootpartition.bin\n");
|
|
|
|
fprintf(stderr," -w, --write-partition bootpartition.bin\n");
|
|
|
|
fprintf(stderr," -rf, --read-firmware filename.ipod\n");
|
|
|
|
fprintf(stderr," -wf, --write-firmware filename.ipod\n");
|
|
|
|
fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n");
|
|
|
|
fprintf(stderr," -a, --add-bootloader filename.ipod\n");
|
|
|
|
fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n");
|
|
|
|
fprintf(stderr," -d, --delete-bootloader\n");
|
2006-12-13 08:57:06 +00:00
|
|
|
fprintf(stderr,"\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
#ifdef __WIN32__
|
2006-12-13 08:57:06 +00:00
|
|
|
fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n");
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
|
2006-12-13 08:57:06 +00:00
|
|
|
fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n");
|
|
|
|
fprintf(stderr,"can identify it as being an ipod.\n");
|
|
|
|
fprintf(stderr,"\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
#else
|
2007-02-04 11:42:11 +00:00
|
|
|
#if defined(linux) || defined (__linux)
|
2006-12-13 09:02:18 +00:00
|
|
|
fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n");
|
2007-02-04 23:41:47 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n");
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
|
|
|
fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n");
|
|
|
|
#endif
|
2006-12-13 09:02:18 +00:00
|
|
|
fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n");
|
|
|
|
fprintf(stderr,"an ipod.\n");
|
|
|
|
#endif
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
NONE,
|
|
|
|
SHOW_INFO,
|
2006-12-14 01:24:05 +00:00
|
|
|
LIST_IMAGES,
|
2006-12-14 18:41:03 +00:00
|
|
|
DELETE_BOOTLOADER,
|
|
|
|
ADD_BOOTLOADER,
|
|
|
|
READ_FIRMWARE,
|
|
|
|
WRITE_FIRMWARE,
|
2006-12-14 01:24:05 +00:00
|
|
|
READ_PARTITION,
|
|
|
|
WRITE_PARTITION
|
2006-12-13 09:02:18 +00:00
|
|
|
};
|
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
#define DOT_IPOD 0
|
|
|
|
#define DOT_BIN 1
|
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
char* ftypename[] = { "OSOS", "RSRC", "AUPD", "HIBE" };
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int diskmove(struct ipod_t* ipod, int delta)
|
2006-12-14 18:41:03 +00:00
|
|
|
{
|
|
|
|
int src_start;
|
|
|
|
int src_end;
|
|
|
|
int bytesleft;
|
|
|
|
int chunksize;
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
src_start = ipod->ipod_directory[1].devOffset + ipod->sector_size;
|
|
|
|
src_end = (ipod->ipod_directory[ipod->nimages-1].devOffset + ipod->sector_size +
|
|
|
|
ipod->ipod_directory[ipod->nimages-1].len +
|
|
|
|
(ipod->sector_size-1)) & ~(ipod->sector_size-1);
|
2006-12-14 18:41:03 +00:00
|
|
|
bytesleft = src_end - src_start;
|
|
|
|
|
|
|
|
if (verbose) {
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"[INFO] Need to move images 2-%d forward %08x bytes\n", ipod->nimages,delta);
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[VERB] src_start = %08x\n",src_start);
|
|
|
|
fprintf(stderr,"[VERB] src_end = %08x\n",src_end);
|
2006-12-15 09:55:21 +00:00
|
|
|
fprintf(stderr,"[VERB] dest_start = %08x\n",src_start+delta);
|
|
|
|
fprintf(stderr,"[VERB] dest_end = %08x\n",src_end+delta);
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[VERB] bytes to copy = %08x\n",bytesleft);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (bytesleft > 0) {
|
|
|
|
if (bytesleft <= BUFFER_SIZE) {
|
|
|
|
chunksize = bytesleft;
|
|
|
|
} else {
|
|
|
|
chunksize = BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) {
|
2006-12-15 09:55:21 +00:00
|
|
|
fprintf(stderr,"[VERB] Copying %08x bytes from %08x to %08x (absolute %08x to %08x)\n",
|
2006-12-14 18:41:03 +00:00
|
|
|
chunksize,
|
2006-12-15 09:55:21 +00:00
|
|
|
src_end-chunksize,
|
|
|
|
src_end-chunksize+delta,
|
2007-02-04 11:42:11 +00:00
|
|
|
(unsigned int)(ipod->start+src_end-chunksize),
|
|
|
|
(unsigned int)(ipod->start+src_end-chunksize+delta));
|
2006-12-14 18:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start+src_end-chunksize) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_read(ipod,sectorbuf,chunksize)) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
perror("[ERR] Write failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < chunksize) {
|
|
|
|
fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n",
|
|
|
|
i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start+src_end-chunksize+delta) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_write(ipod,sectorbuf,chunksize)) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
perror("[ERR] Write failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < chunksize) {
|
|
|
|
fprintf(stderr,"[ERR] Short write - requested %d bytes, received %d\n"
|
|
|
|
,i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-15 09:55:21 +00:00
|
|
|
src_end -= chunksize;
|
2006-12-14 18:41:03 +00:00
|
|
|
bytesleft -= chunksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int add_bootloader(struct ipod_t* ipod, char* filename, int type)
|
2006-12-14 18:41:03 +00:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int i;
|
2006-12-15 15:52:08 +00:00
|
|
|
int x;
|
2006-12-14 18:41:03 +00:00
|
|
|
int n;
|
|
|
|
int infile;
|
|
|
|
int paddedlength;
|
|
|
|
int entryOffset;
|
|
|
|
int delta = 0;
|
|
|
|
unsigned long chksum=0;
|
|
|
|
unsigned long filechksum=0;
|
|
|
|
unsigned char header[8]; /* Header for .ipod file */
|
|
|
|
|
|
|
|
infile=open(filename,O_RDONLY);
|
|
|
|
if (infile < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't open input file %s\n",filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
if (type==DOT_IPOD) {
|
2007-02-04 11:42:11 +00:00
|
|
|
/* First check that the input file is the correct type for this ipod. */
|
2006-12-21 21:34:46 +00:00
|
|
|
n = read(infile,header,8);
|
|
|
|
if (n < 8) {
|
|
|
|
fprintf(stderr,"[ERR] Failed to read header from %s\n",filename);
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (memcmp(header+4, ipod->modelname,4)!=0) {
|
2006-12-21 21:34:46 +00:00
|
|
|
fprintf(stderr,"[ERR] Model name in input file (%c%c%c%c) doesn't match ipod model (%s)\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
header[4],header[5],header[6],header[7], ipod->modelname);
|
2006-12-21 21:34:46 +00:00
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
filechksum = be2int(header);
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
length=filesize(infile)-8;
|
|
|
|
} else {
|
|
|
|
length=filesize(infile);
|
|
|
|
}
|
2007-02-04 11:42:11 +00:00
|
|
|
paddedlength=(length+ipod->sector_size-1)&~(ipod->sector_size-1);
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Now read our bootloader - we need to check it before modifying the partition*/
|
|
|
|
n = read(infile,sectorbuf,length);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't read input file\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
if (type==DOT_IPOD) {
|
|
|
|
/* Calculate and confirm bootloader checksum */
|
2007-02-04 11:42:11 +00:00
|
|
|
chksum = ipod->modelnum;
|
2006-12-21 21:34:46 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
if (chksum == filechksum) {
|
|
|
|
fprintf(stderr,"[INFO] Checksum OK in %s\n",filename);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[ERR] Checksum in %s failed check\n",filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->ipod_directory[0].entryOffset>0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
/* Keep the same entryOffset */
|
2007-02-04 11:42:11 +00:00
|
|
|
entryOffset = ipod->ipod_directory[0].entryOffset;
|
2006-12-14 18:41:03 +00:00
|
|
|
} else {
|
2007-02-04 11:42:11 +00:00
|
|
|
entryOffset = (ipod->ipod_directory[0].len+ipod->sector_size-1)&~(ipod->sector_size-1);
|
2006-12-14 18:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entryOffset+paddedlength > BUFFER_SIZE) {
|
|
|
|
fprintf(stderr,"[ERR] Input file too big for buffer\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) {
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"[VERB] Original firmware begins at 0x%08x\n", ipod->ipod_directory[0].devOffset + ipod->sector_size);
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[VERB] New entryOffset will be 0x%08x\n",entryOffset);
|
|
|
|
fprintf(stderr,"[VERB] End of bootloader will be at 0x%08x\n",entryOffset+paddedlength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have enough space */
|
|
|
|
/* TODO: Check the size of the partition. */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->nimages > 1) {
|
2007-02-04 13:04:23 +00:00
|
|
|
if ((ipod->ipod_directory[0].devOffset+entryOffset+paddedlength) >=
|
|
|
|
ipod->ipod_directory[1].devOffset) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[INFO] Moving images to create room for new firmware...\n");
|
2007-02-04 11:42:11 +00:00
|
|
|
delta = ipod->ipod_directory[0].devOffset + entryOffset+paddedlength
|
|
|
|
- ipod->ipod_directory[1].devOffset;
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (diskmove(ipod, delta) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
close(infile);
|
|
|
|
fprintf(stderr,"[ERR] Image movement failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We have moved the partitions, now we can write our bootloader */
|
|
|
|
|
|
|
|
/* Firstly read the original firmware into sectorbuf */
|
|
|
|
fprintf(stderr,"[INFO] Reading original firmware...\n");
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_read(ipod,sectorbuf,entryOffset)) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
perror("[ERR] Read failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < entryOffset) {
|
|
|
|
fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n"
|
|
|
|
,i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-21 21:34:46 +00:00
|
|
|
/* Now read our bootloader - we need to seek back to the start */
|
|
|
|
lseek(infile,(type == DOT_IPOD ? 8 : 0),SEEK_SET);
|
2006-12-14 18:41:03 +00:00
|
|
|
n = read(infile,sectorbuf+entryOffset,length);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't read input file\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(infile);
|
|
|
|
|
|
|
|
/* Calculate new checksum for combined image */
|
|
|
|
chksum = 0;
|
|
|
|
for (i=0;i<entryOffset + length; i++) {
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write the combined firmware image to the disk */
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_write(ipod,sectorbuf,entryOffset+paddedlength)) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
perror("[ERR] Write failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < (entryOffset+paddedlength)) {
|
|
|
|
fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n"
|
|
|
|
,i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Wrote %d bytes to firmware partition\n",entryOffset+paddedlength);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
x = ipod->diroffset % ipod->sector_size;
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Read directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n=ipod_read(ipod, sectorbuf, ipod->sector_size);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Directory read failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Update entries for image 0 */
|
2006-12-15 15:52:08 +00:00
|
|
|
int2le(entryOffset+length,sectorbuf+x+16);
|
|
|
|
int2le(entryOffset,sectorbuf+x+24);
|
|
|
|
int2le(chksum,sectorbuf+x+28);
|
2007-02-04 11:42:11 +00:00
|
|
|
int2le(0xffffffff,sectorbuf+x+36); /* loadAddr */
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Update devOffset entries for other images, if we have moved them */
|
|
|
|
if (delta > 0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
for (i=1;i<ipod->nimages;i++) {
|
2006-12-15 15:52:08 +00:00
|
|
|
int2le(le2int(sectorbuf+x+i*40+12)+delta,sectorbuf+x+i*40+12);
|
2006-12-14 18:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) {
|
2007-02-04 23:41:47 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek to %d failed\n", (int)(ipod->start+ipod->diroffset-x));
|
2007-02-04 11:42:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n=ipod_write(ipod, sectorbuf, ipod->sector_size);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Directory write failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int delete_bootloader(struct ipod_t* ipod)
|
2006-12-14 01:24:05 +00:00
|
|
|
{
|
2006-12-14 18:41:03 +00:00
|
|
|
int length;
|
|
|
|
int i;
|
2006-12-15 15:52:08 +00:00
|
|
|
int x;
|
2006-12-14 18:41:03 +00:00
|
|
|
int n;
|
|
|
|
unsigned long chksum=0; /* 32 bit checksum - Rockbox .ipod style*/
|
|
|
|
|
|
|
|
/* Removing the bootloader involves adjusting the "length",
|
|
|
|
"chksum" and "entryOffset" values in the osos image's directory
|
|
|
|
entry. */
|
|
|
|
|
|
|
|
/* Firstly check we have a bootloader... */
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->ipod_directory[0].entryOffset == 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] No bootloader found.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
length = ipod->ipod_directory[0].entryOffset;
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Read the firmware so we can calculate the checksum */
|
|
|
|
fprintf(stderr,"[INFO] Reading firmware (%d bytes)\n",length);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
i = (length+ipod->sector_size-1) & ~(ipod->sector_size-1);
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n",
|
|
|
|
length,i);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_read(ipod,sectorbuf,i)) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < i) {
|
|
|
|
fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n",
|
|
|
|
i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
chksum = 0;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write back the updated directory entry */
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Updating firmware checksum\n");
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
x = ipod->diroffset % ipod->sector_size;
|
2006-12-15 15:52:08 +00:00
|
|
|
|
2006-12-14 18:41:03 +00:00
|
|
|
/* Read directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
|
2006-12-14 18:41:03 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n=ipod_read(ipod, sectorbuf, ipod->sector_size);
|
2006-12-14 18:41:03 +00:00
|
|
|
if (n < 0) { return -1; }
|
|
|
|
|
|
|
|
/* Update entries for image 0 */
|
2006-12-15 15:52:08 +00:00
|
|
|
int2le(length,sectorbuf+x+16);
|
|
|
|
int2le(0,sectorbuf+x+24);
|
|
|
|
int2le(chksum,sectorbuf+x+28);
|
2006-12-14 18:41:03 +00:00
|
|
|
|
|
|
|
/* Write directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
|
|
|
|
n=ipod_write(ipod, sectorbuf, ipod->sector_size);
|
2006-12-14 18:41:03 +00:00
|
|
|
if (n < 0) { return -1; }
|
|
|
|
|
|
|
|
return 0;
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int write_firmware(struct ipod_t* ipod, char* filename, int type)
|
2006-12-14 01:24:05 +00:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int i;
|
2006-12-15 15:52:08 +00:00
|
|
|
int x;
|
2006-12-14 01:24:05 +00:00
|
|
|
int n;
|
|
|
|
int infile;
|
|
|
|
int newsize;
|
|
|
|
int bytesavailable;
|
|
|
|
unsigned long chksum=0;
|
|
|
|
unsigned long filechksum=0;
|
|
|
|
unsigned char header[8]; /* Header for .ipod file */
|
|
|
|
|
|
|
|
/* First check that the input file is the correct type for this ipod. */
|
|
|
|
infile=open(filename,O_RDONLY);
|
|
|
|
if (infile < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't open input file %s\n",filename);
|
2006-12-14 18:41:03 +00:00
|
|
|
return -1;
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (type==DOT_IPOD) {
|
|
|
|
n = read(infile,header,8);
|
|
|
|
if (n < 8) {
|
|
|
|
fprintf(stderr,"[ERR] Failed to read header from %s\n",filename);
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (memcmp(header+4, ipod->modelname,4)!=0) {
|
|
|
|
fprintf(stderr,"[ERR] Model name in input file (%c%c%c%c) doesn't match ipod model (%s)\n",
|
|
|
|
header[4],header[5],header[6],header[7], ipod->modelname);
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
filechksum = be2int(header);
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
length = filesize(infile)-8;
|
|
|
|
} else {
|
|
|
|
length = filesize(infile);
|
|
|
|
}
|
|
|
|
newsize=(length+ipod->sector_size-1)&~(ipod->sector_size-1);
|
2006-12-14 01:24:05 +00:00
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Padding input file from 0x%08x to 0x%08x bytes\n",
|
|
|
|
length,newsize);
|
|
|
|
|
|
|
|
if (newsize > BUFFER_SIZE) {
|
|
|
|
fprintf(stderr,"[ERR] Input file too big for buffer\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have enough space */
|
|
|
|
/* TODO: Check the size of the partition. */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->nimages > 1) {
|
|
|
|
bytesavailable=ipod->ipod_directory[1].devOffset-ipod->ipod_directory[0].devOffset;
|
2006-12-14 01:24:05 +00:00
|
|
|
if (bytesavailable < newsize) {
|
|
|
|
fprintf(stderr,"[INFO] Moving images to create room for new firmware...\n");
|
|
|
|
|
|
|
|
/* TODO: Implement image movement */
|
|
|
|
fprintf(stderr,"[ERR] Image movement not yet implemented.\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Reading input file...\n");
|
|
|
|
/* We now know we have enough space, so write it. */
|
|
|
|
memset(sectorbuf+length,0,newsize-length);
|
|
|
|
n = read(infile,sectorbuf,length);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't read input file\n");
|
|
|
|
close(infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(infile);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (type==DOT_IPOD) {
|
|
|
|
chksum = ipod->modelnum;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (chksum == filechksum) {
|
|
|
|
fprintf(stderr,"[INFO] Checksum OK in %s\n",filename);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[ERR] Checksum in %s failed check\n",filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] Seek failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_write(ipod,sectorbuf,newsize)) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
perror("[ERR] Write failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < newsize) {
|
|
|
|
fprintf(stderr,"[ERR] Short write - requested %d bytes, received %d\n"
|
|
|
|
,i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fprintf(stderr,"[INFO] Wrote %d bytes to firmware partition\n",n);
|
|
|
|
|
|
|
|
/* Now we need to update the "len", "entryOffset" and "chksum" fields */
|
|
|
|
chksum = 0;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
x = ipod->diroffset % ipod->sector_size;
|
2006-12-15 15:52:08 +00:00
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
/* Read directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n=ipod_read(ipod, sectorbuf, ipod->sector_size);
|
2006-12-14 01:24:05 +00:00
|
|
|
if (n < 0) { return -1; }
|
|
|
|
|
|
|
|
/* Update entries for image 0 */
|
2006-12-15 15:52:08 +00:00
|
|
|
int2le(length,sectorbuf+x+16);
|
|
|
|
int2le(0,sectorbuf+x+24);
|
|
|
|
int2le(chksum,sectorbuf+x+28);
|
2006-12-14 01:24:05 +00:00
|
|
|
|
|
|
|
/* Write directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
|
|
|
|
n=ipod_write(ipod, sectorbuf, ipod->sector_size);
|
2006-12-14 01:24:05 +00:00
|
|
|
if (n < 0) { return -1; }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int read_firmware(struct ipod_t* ipod, char* filename)
|
2006-12-14 01:24:05 +00:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int i;
|
|
|
|
int outfile;
|
|
|
|
int n;
|
|
|
|
unsigned long chksum=0; /* 32 bit checksum - Rockbox .ipod style*/
|
|
|
|
unsigned char header[8]; /* Header for .ipod file */
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->ipod_directory[0].entryOffset != 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
/* We have a bootloader... */
|
2007-02-04 11:42:11 +00:00
|
|
|
length = ipod->ipod_directory[0].entryOffset;
|
2006-12-14 01:24:05 +00:00
|
|
|
} else {
|
2007-02-04 11:42:11 +00:00
|
|
|
length = ipod->ipod_directory[0].len;
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Reading firmware (%d bytes)\n",length);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
i = (length+ipod->sector_size-1) & ~(ipod->sector_size-1);
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n",
|
|
|
|
length,i);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((n = ipod_read(ipod,sectorbuf,i)) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < i) {
|
|
|
|
fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n",
|
|
|
|
i,n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
chksum = ipod->modelnum;
|
2006-12-14 01:24:05 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
chksum += sectorbuf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
int2be(chksum,header);
|
2007-02-04 11:42:11 +00:00
|
|
|
memcpy(header+4, ipod->modelname,4);
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2006-12-14 10:16:10 +00:00
|
|
|
outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666);
|
2006-12-14 01:24:05 +00:00
|
|
|
if (outfile < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Couldn't open file %s\n",filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(outfile,header,8);
|
2006-12-14 10:16:10 +00:00
|
|
|
write(outfile,sectorbuf,length);
|
2006-12-14 01:24:05 +00:00
|
|
|
close(outfile);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int read_directory(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
int n;
|
2006-12-15 15:52:08 +00:00
|
|
|
int x;
|
2006-12-13 09:02:18 +00:00
|
|
|
unsigned char* p;
|
2007-02-04 11:42:11 +00:00
|
|
|
unsigned short version;
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-02-06 21:00:56 +00:00
|
|
|
ipod->nimages=0;
|
|
|
|
|
2006-12-14 01:24:05 +00:00
|
|
|
/* Read firmware partition header (first 512 bytes of disk - but
|
|
|
|
let's read a whole sector) */
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start) < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Seek to 0x%08x in read_directory() failed.\n",
|
|
|
|
(unsigned int)(ipod->start));
|
2006-12-15 15:52:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n=ipod_read(ipod, sectorbuf, ipod->sector_size);
|
2006-12-15 15:52:08 +00:00
|
|
|
if (n < 0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"[ERR] ipod_read(ipod,buf,0x%08x) failed in read_directory()\n", ipod->sector_size);
|
2006-12-15 15:52:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
if (memcmp(sectorbuf,apple_stop_sign,sizeof(apple_stop_sign))!=0) {
|
2007-02-06 21:00:56 +00:00
|
|
|
fprintf(stderr,"[ERR] Firmware partition doesn't contain Apple copyright, aborting.\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(sectorbuf+0x100,"]ih[",4)!=0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] Bad firmware directory\n");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
version = le2ushort(sectorbuf+0x10a);
|
|
|
|
if ((version != 2) && (version != 3)) {
|
|
|
|
fprintf(stderr,"[ERR] Unknown firmware format version %04x\n",
|
|
|
|
version);
|
|
|
|
}
|
|
|
|
ipod->diroffset=le2int(sectorbuf+0x104) + 0x200;
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-15 15:52:08 +00:00
|
|
|
/* diroffset may not be sector-aligned */
|
2007-02-04 11:42:11 +00:00
|
|
|
x = ipod->diroffset % ipod->sector_size;
|
2006-12-15 15:52:08 +00:00
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
/* Read directory */
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Seek to diroffset (%08x) failed.\n",(unsigned)ipod->diroffset);
|
2006-12-15 15:52:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
n=ipod_read(ipod, sectorbuf, ipod->sector_size);
|
2006-12-15 15:52:08 +00:00
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,"[ERR] Read of directory failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-15 15:52:08 +00:00
|
|
|
p = sectorbuf + x;
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
while ((ipod->nimages < MAX_IMAGES) && (p < (sectorbuf + x + 400)) &&
|
2006-12-14 01:24:05 +00:00
|
|
|
(memcmp(p,"!ATA",4)==0)) {
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
|
|
|
if (memcmp(p,"soso",4)==0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].ftype=FTYPE_OSOS;
|
2006-12-13 09:02:18 +00:00
|
|
|
} else if (memcmp(p,"crsr",4)==0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].ftype=FTYPE_RSRC;
|
2006-12-13 09:02:18 +00:00
|
|
|
} else if (memcmp(p,"dpua",4)==0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].ftype=FTYPE_AUPD;
|
2006-12-13 09:02:18 +00:00
|
|
|
} else if (memcmp(p,"ebih",4)==0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].ftype=FTYPE_HIBE;
|
2006-12-13 09:02:18 +00:00
|
|
|
} else {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] Unknown image type %c%c%c%c\n",
|
|
|
|
p[0],p[1],p[2],p[3]);
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].id=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].devOffset=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].len=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].addr=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].entryOffset=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].chksum=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].vers=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[ipod->nimages].loadAddr=le2int(p);
|
2006-12-13 09:02:18 +00:00
|
|
|
p+=4;
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->nimages++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ipod->nimages > 1) && (version==2)) {
|
|
|
|
/* The 3g firmware image doesn't appear to have a version, so
|
|
|
|
let's make one up... Note that this is never written back to the
|
|
|
|
ipod, so it's OK to do. */
|
|
|
|
|
|
|
|
if (ipod->ipod_directory[0].vers == 0) { ipod->ipod_directory[0].vers = 3; }
|
|
|
|
|
|
|
|
ipod->fwoffset = ipod->start;
|
|
|
|
} else {
|
|
|
|
ipod->fwoffset = ipod->start + ipod->sector_size;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
2007-02-04 11:42:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int list_images(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-12-14 18:41:03 +00:00
|
|
|
if (verbose) {
|
|
|
|
printf(" Type id devOffset len addr entryOffset chksum vers loadAddr devOffset+len\n");
|
2007-02-04 11:42:11 +00:00
|
|
|
for (i = 0 ; i < ipod->nimages; i++) {
|
2006-12-14 18:41:03 +00:00
|
|
|
printf("%d - %s 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",i,
|
2007-02-04 11:42:11 +00:00
|
|
|
ftypename[ipod->ipod_directory[i].ftype],
|
|
|
|
ipod->ipod_directory[i].id,
|
|
|
|
ipod->ipod_directory[i].devOffset,
|
|
|
|
ipod->ipod_directory[i].len,
|
|
|
|
ipod->ipod_directory[i].addr,
|
|
|
|
ipod->ipod_directory[i].entryOffset,
|
|
|
|
ipod->ipod_directory[i].chksum,
|
|
|
|
ipod->ipod_directory[i].vers,
|
|
|
|
ipod->ipod_directory[i].loadAddr,
|
|
|
|
ipod->ipod_directory[i].devOffset+((ipod->ipod_directory[i].len+ipod->sector_size-1)&~(ipod->sector_size-1)));
|
2006-12-14 18:41:03 +00:00
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("Listing firmware partition contents:\n");
|
|
|
|
printf("\n");
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
for (i = 0 ; i < ipod->nimages; i++) {
|
2006-12-13 09:02:18 +00:00
|
|
|
printf("Image %d:\n",i+1);
|
2007-02-04 11:42:11 +00:00
|
|
|
switch(ipod->ipod_directory[i].ftype) {
|
2006-12-13 09:02:18 +00:00
|
|
|
case FTYPE_OSOS:
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->ipod_directory[i].entryOffset==0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
printf(" Main firmware - %d bytes\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[i].len);
|
2006-12-13 09:02:18 +00:00
|
|
|
} else {
|
2006-12-14 01:24:05 +00:00
|
|
|
printf(" Main firmware - %d bytes\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[i].entryOffset);
|
2006-12-14 01:24:05 +00:00
|
|
|
printf(" Third-party bootloader - %d bytes\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->ipod_directory[i].len-ipod->ipod_directory[i].entryOffset);
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-12-14 01:24:05 +00:00
|
|
|
printf(" %s - %d bytes\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
ftypename[ipod->ipod_directory[i].ftype],
|
|
|
|
ipod->ipod_directory[i].len);
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int getmodel(struct ipod_t* ipod, int ipod_version)
|
2006-12-17 23:00:15 +00:00
|
|
|
{
|
|
|
|
switch (ipod_version) {
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0x02:
|
|
|
|
ipod->modelstr="3rd Generation";
|
|
|
|
ipod->modelnum = 7;
|
|
|
|
ipod->modelname = "ip3g";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0x40:
|
|
|
|
ipod->modelstr="1st Generation Mini";
|
|
|
|
ipod->modelnum = 9;
|
|
|
|
ipod->modelname = "mini";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0x50:
|
|
|
|
ipod->modelstr="4th Generation";
|
|
|
|
ipod->modelnum = 8;
|
|
|
|
ipod->modelname = "ip4g";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0x60:
|
|
|
|
ipod->modelstr="Photo/Color";
|
|
|
|
ipod->modelnum = 3;
|
|
|
|
ipod->modelname = "ipco";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0x70:
|
|
|
|
ipod->modelstr="2nd Generation Mini";
|
|
|
|
ipod->modelnum = 11;
|
|
|
|
ipod->modelname = "mn2g";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0xc0:
|
|
|
|
ipod->modelstr="1st Generation Nano";
|
|
|
|
ipod->modelnum = 4;
|
|
|
|
ipod->modelname = "nano";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
2007-02-04 11:42:11 +00:00
|
|
|
case 0xb0:
|
|
|
|
ipod->modelstr="Video (aka 5th Generation)";
|
|
|
|
ipod->modelnum = 5;
|
|
|
|
ipod->modelname = "ipvd";
|
2006-12-17 23:00:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->modelname = NULL;
|
|
|
|
ipod->modelnum = 0;
|
2006-12-17 23:00:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_scan(struct ipod_t* ipod)
|
2006-12-17 23:00:15 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n = 0;
|
|
|
|
int ipod_version;
|
2007-02-06 21:00:56 +00:00
|
|
|
char last_ipod[4096];
|
2006-12-17 23:00:15 +00:00
|
|
|
|
|
|
|
printf("[INFO] Scanning disk devices...\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
for (i = 0; i <= 25 ; i++) {
|
2006-12-17 23:00:15 +00:00
|
|
|
#ifdef __WIN32__
|
2007-02-04 11:42:11 +00:00
|
|
|
sprintf(ipod->diskname,"\\\\.\\PhysicalDrive%d",i);
|
2006-12-17 23:00:15 +00:00
|
|
|
#elif defined(linux) || defined (__linux)
|
2007-02-04 11:42:11 +00:00
|
|
|
sprintf(ipod->diskname,"/dev/sd%c",'a'+i);
|
2006-12-17 23:00:15 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
|
|
|
|
|| defined(__bsdi__) || defined(__DragonFly__)
|
2007-02-04 11:42:11 +00:00
|
|
|
sprintf(ipod->diskname,"/dev/da%d",i);
|
2006-12-17 23:00:15 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
2007-02-04 11:42:11 +00:00
|
|
|
sprintf(ipod->diskname,"/dev/disk%d",i);
|
2006-12-17 23:00:15 +00:00
|
|
|
#else
|
|
|
|
#error No disk paths defined for this platform
|
|
|
|
#endif
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_open(ipod, 1) < 0) {
|
2006-12-17 23:00:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (read_partinfo(ipod,1) < 0) {
|
2006-12-17 23:00:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if ((ipod->pinfo[0].start==0) || (ipod->pinfo[0].type != 0)) {
|
2006-12-17 23:00:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (read_directory(ipod) < 0) {
|
2006-12-17 23:00:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod_version=(ipod->ipod_directory[0].vers>>8);
|
|
|
|
if (getmodel(ipod,ipod_version) < 0) {
|
2006-12-17 23:00:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
2007-02-05 18:29:39 +00:00
|
|
|
printf("[INFO] Ipod found - %s (\"%s\") - disk device %d\n",
|
|
|
|
ipod->modelstr,ipod->macpod ? "macpod" : "winpod",i);
|
2006-12-17 23:00:15 +00:00
|
|
|
#else
|
2007-02-05 18:29:39 +00:00
|
|
|
printf("[INFO] Ipod found - %s (\"%s\") - %s\n",
|
|
|
|
ipod->modelstr,ipod->macpod ? "macpod" : "winpod",ipod->diskname);
|
2006-12-17 23:00:15 +00:00
|
|
|
#endif
|
2007-02-06 21:00:56 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
if (ipod->macpod) {
|
|
|
|
print_macpod_warning();
|
|
|
|
}
|
2006-12-17 23:00:15 +00:00
|
|
|
n++;
|
2007-02-06 21:00:56 +00:00
|
|
|
strcpy(last_ipod,ipod->diskname);
|
2007-02-06 22:48:30 +00:00
|
|
|
ipod_close(ipod);
|
2006-12-17 23:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n==0) {
|
|
|
|
fprintf(stderr,"[ERR] No ipods found.\n");
|
2007-02-06 21:00:56 +00:00
|
|
|
} else if (n==1) {
|
|
|
|
/* Remember the disk name */
|
|
|
|
strcpy(ipod->diskname,last_ipod);
|
2006-12-17 23:00:15 +00:00
|
|
|
}
|
2007-02-06 21:00:56 +00:00
|
|
|
return n;
|
2006-12-17 23:00:15 +00:00
|
|
|
}
|
2006-12-14 01:24:05 +00:00
|
|
|
|
2006-12-13 08:57:06 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int i;
|
2007-02-06 21:00:56 +00:00
|
|
|
int n;
|
2006-12-14 01:24:05 +00:00
|
|
|
int infile, outfile;
|
|
|
|
unsigned int inputsize;
|
|
|
|
char* filename;
|
2006-12-13 09:02:18 +00:00
|
|
|
int action = SHOW_INFO;
|
2006-12-21 21:34:46 +00:00
|
|
|
int type;
|
2007-02-04 11:42:11 +00:00
|
|
|
struct ipod_t ipod;
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-05 01:20:20 +00:00
|
|
|
fprintf(stderr,"ipodpatcher v" VERSION " - (C) Dave Chapman 2006-2007\n");
|
2006-12-13 08:57:06 +00:00
|
|
|
fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n");
|
|
|
|
fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
|
2006-12-17 23:00:15 +00:00
|
|
|
|
2007-02-06 21:00:56 +00:00
|
|
|
if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
|
2006-12-13 08:57:06 +00:00
|
|
|
print_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-12-17 23:00:15 +00:00
|
|
|
if (ipod_alloc_buffer(§orbuf,BUFFER_SIZE) < 0) {
|
|
|
|
fprintf(stderr,"Failed to allocate memory buffer\n");
|
|
|
|
}
|
|
|
|
|
2007-02-06 21:00:56 +00:00
|
|
|
if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod_scan(&ipod);
|
2006-12-17 23:00:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-06 21:00:56 +00:00
|
|
|
/* If the first parameter doesn't start with -, then we interpret it as a device */
|
|
|
|
if ((argc > 1) && (argv[1][0] != '-')) {
|
|
|
|
ipod.diskname[0]=0;
|
2006-12-13 09:02:18 +00:00
|
|
|
#ifdef __WIN32__
|
2007-02-06 21:00:56 +00:00
|
|
|
snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
|
2006-12-13 09:02:18 +00:00
|
|
|
#else
|
2007-02-06 21:00:56 +00:00
|
|
|
strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname));
|
2006-12-13 09:02:18 +00:00
|
|
|
#endif
|
2007-02-06 21:00:56 +00:00
|
|
|
i = 2;
|
|
|
|
} else {
|
|
|
|
/* Autoscan for ipods */
|
|
|
|
n = ipod_scan(&ipod);
|
|
|
|
if (n==0) {
|
|
|
|
fprintf(stderr,"[ERR] No ipods found, aborting\n");
|
|
|
|
return 0;
|
|
|
|
} else if (n > 1) {
|
|
|
|
fprintf(stderr,"[ERR] %d ipods found, aborting\n",n);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
i = 1;
|
|
|
|
}
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-13 08:57:06 +00:00
|
|
|
while (i < argc) {
|
2006-12-13 09:02:18 +00:00
|
|
|
if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
|
|
|
|
action = LIST_IMAGES;
|
|
|
|
i++;
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if ((strcmp(argv[i],"-d")==0) ||
|
|
|
|
(strcmp(argv[i],"--delete-bootloader")==0)) {
|
|
|
|
action = DELETE_BOOTLOADER;
|
2006-12-14 01:24:05 +00:00
|
|
|
i++;
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if ((strcmp(argv[i],"-a")==0) ||
|
|
|
|
(strcmp(argv[i],"--add-bootloader")==0)) {
|
|
|
|
action = ADD_BOOTLOADER;
|
2006-12-21 21:34:46 +00:00
|
|
|
type = DOT_IPOD;
|
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-ab")==0) ||
|
|
|
|
(strcmp(argv[i],"--add-bootloader-bin")==0)) {
|
|
|
|
action = ADD_BOOTLOADER;
|
|
|
|
type = DOT_BIN;
|
2006-12-14 01:24:05 +00:00
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-rf")==0) ||
|
2006-12-14 18:41:03 +00:00
|
|
|
(strcmp(argv[i],"--read-firmware")==0)) {
|
|
|
|
action = READ_FIRMWARE;
|
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-wf")==0) ||
|
|
|
|
(strcmp(argv[i],"--write-firmware")==0)) {
|
|
|
|
action = WRITE_FIRMWARE;
|
2007-02-04 11:42:11 +00:00
|
|
|
type = DOT_IPOD;
|
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-wfb")==0) ||
|
|
|
|
(strcmp(argv[i],"--write-firmware-bin")==0)) {
|
|
|
|
action = WRITE_FIRMWARE;
|
|
|
|
type = DOT_BIN;
|
2006-12-14 01:24:05 +00:00
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-r")==0) ||
|
|
|
|
(strcmp(argv[i],"--read-partition")==0)) {
|
|
|
|
action = READ_PARTITION;
|
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
|
|
|
} else if ((strcmp(argv[i],"-w")==0) ||
|
|
|
|
(strcmp(argv[i],"--write-partition")==0)) {
|
|
|
|
action = WRITE_PARTITION;
|
|
|
|
i++;
|
|
|
|
if (i == argc) { print_usage(); return 1; }
|
|
|
|
filename=argv[i];
|
|
|
|
i++;
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if ((strcmp(argv[i],"-v")==0) ||
|
|
|
|
(strcmp(argv[i],"--verbose")==0)) {
|
|
|
|
verbose++;
|
|
|
|
i++;
|
2006-12-13 08:57:06 +00:00
|
|
|
} else {
|
2006-12-13 09:02:18 +00:00
|
|
|
print_usage(); return 1;
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod.diskname[0]==0) {
|
2006-12-13 08:57:06 +00:00
|
|
|
print_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_open(&ipod, 0) < 0) {
|
2006-12-13 09:02:18 +00:00
|
|
|
return 1;
|
2006-12-13 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname);
|
|
|
|
fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size);
|
2006-12-13 08:59:07 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (read_partinfo(&ipod,0) < 0) {
|
2006-12-13 08:57:06 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
display_partinfo(&ipod);
|
2006-12-13 08:57:06 +00:00
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod.pinfo[0].start==0) {
|
2006-12-13 09:02:18 +00:00
|
|
|
fprintf(stderr,"[ERR] No partition 0 on disk:\n");
|
2007-02-04 11:42:11 +00:00
|
|
|
display_partinfo(&ipod);
|
2006-12-13 08:57:06 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
read_directory(&ipod);
|
|
|
|
|
|
|
|
if (ipod.nimages <= 0) {
|
|
|
|
fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages);
|
2006-12-13 09:02:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) {
|
2006-12-17 23:00:15 +00:00
|
|
|
fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n",
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod.ipod_directory[0].vers);
|
2006-12-17 23:00:15 +00:00
|
|
|
return -1;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr,
|
|
|
|
ipod.macpod ? "macpod" : "winpod");
|
2006-12-17 23:00:15 +00:00
|
|
|
|
2007-02-05 18:29:39 +00:00
|
|
|
if (ipod.macpod) {
|
|
|
|
print_macpod_warning();
|
|
|
|
}
|
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
if (action==LIST_IMAGES) {
|
2007-02-04 11:42:11 +00:00
|
|
|
list_images(&ipod);
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if (action==DELETE_BOOTLOADER) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_reopen_rw(&ipod) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod.ipod_directory[0].entryOffset==0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] No bootloader detected.\n");
|
|
|
|
} else {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (delete_bootloader(&ipod)==0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[INFO] Bootloader removed.\n");
|
2006-12-14 18:41:03 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if (action==ADD_BOOTLOADER) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_reopen_rw(&ipod) < 0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (add_bootloader(&ipod, filename, type)==0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[ERR] --add-bootloader failed.\n");
|
|
|
|
}
|
|
|
|
} else if (action==WRITE_FIRMWARE) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_reopen_rw(&ipod) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (write_firmware(&ipod, filename,type)==0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename);
|
2006-12-14 01:24:05 +00:00
|
|
|
} else {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] --write-firmware failed.\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
2006-12-14 18:41:03 +00:00
|
|
|
} else if (action==READ_FIRMWARE) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (read_firmware(&ipod, filename)==0) {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
|
2006-12-14 01:24:05 +00:00
|
|
|
} else {
|
2006-12-14 18:41:03 +00:00
|
|
|
fprintf(stderr,"[ERR] --read-firmware failed.\n");
|
2006-12-14 01:24:05 +00:00
|
|
|
}
|
|
|
|
} else if (action==READ_PARTITION) {
|
2007-01-29 20:17:59 +00:00
|
|
|
outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
|
2006-12-13 08:57:06 +00:00
|
|
|
if (outfile < 0) {
|
|
|
|
perror(filename);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (read_partition(&ipod, outfile) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] --read-partition failed.\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename);
|
|
|
|
}
|
2006-12-13 08:57:06 +00:00
|
|
|
close(outfile);
|
2006-12-14 01:24:05 +00:00
|
|
|
} else if (action==WRITE_PARTITION) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod_reopen_rw(&ipod) < 0) {
|
2006-12-13 09:02:18 +00:00
|
|
|
return 5;
|
2006-12-13 08:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
infile = open(filename,O_RDONLY|O_BINARY);
|
|
|
|
if (infile < 0) {
|
|
|
|
perror(filename);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check filesize is <= partition size */
|
|
|
|
inputsize=filesize(infile);
|
|
|
|
if (inputsize > 0) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize);
|
2007-02-04 11:42:11 +00:00
|
|
|
if (write_partition(&ipod,infile) < 0) {
|
2006-12-14 01:24:05 +00:00
|
|
|
fprintf(stderr,"[ERR] --write-partition failed.\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[INFO] %s restored to partition\n",filename);
|
|
|
|
}
|
2006-12-13 08:57:06 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(infile);
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod_close(&ipod);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2006-12-13 08:57:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|