1
0
Fork 0
forked from len0rd/rockbox

Mainly internal changes to create a struct ipod_t instead of passing lots of parameters to all the functions. Also adds (untested) 3G support, plus a --write-firmware-bin option (requested by IPL people to write a kernel directly to the firmware partition).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12190 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dave Chapman 2007-02-04 11:42:11 +00:00
parent 7c9f4e3a3a
commit 31aa4524da
4 changed files with 414 additions and 378 deletions

View file

@ -47,36 +47,39 @@ void print_error(char* msg)
perror(msg);
}
int ipod_open(HANDLE* dh, char* diskname, int* sector_size, int silent)
int ipod_open(struct ipod_t* ipod, int silent)
{
*dh=open(diskname,O_RDONLY);
if (*dh < 0) {
if (!silent) perror(diskname);
ipod->dh=open(ipod->diskname,O_RDONLY);
if (ipod->dh < 0) {
if (!silent) perror(ipod->diskname);
return -1;
}
if(ioctl(*dh,IPOD_SECTORSIZE_IOCTL,sector_size) < 0) {
*sector_size=512;
if (!silent) fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n",*sector_size);
if(ioctl(ipod->dh,IPOD_SECTORSIZE_IOCTL,&ipod->sector_size) < 0) {
ipod->sector_size=512;
if (!silent) {
fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
,ipod->sector_size);
}
}
return 0;
}
int ipod_reopen_rw(HANDLE* dh, char* diskname)
int ipod_reopen_rw(struct ipod_t* ipod)
{
close(*dh);
*dh=open(diskname,O_RDWR);
if (*dh < 0) {
perror(diskname);
close(ipod->dh);
ipod->dh=open(ipod->diskname,O_RDWR);
if (ipod->dh < 0) {
perror(ipod->diskname);
return -1;
}
return 0;
}
int ipod_close(HANDLE dh)
int ipod_close(struct ipod_t* ipod)
{
close(dh);
close(ipod->dh);
return 0;
}
@ -89,11 +92,11 @@ int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize)
return 0;
}
int ipod_seek(HANDLE dh, unsigned long pos)
int ipod_seek(struct ipod_t* ipod, unsigned long pos)
{
off_t res;
res = lseek(dh, pos, SEEK_SET);
res = lseek(ipod->dh, pos, SEEK_SET);
if (res == -1) {
return -1;
@ -101,12 +104,12 @@ int ipod_seek(HANDLE dh, unsigned long pos)
return 0;
}
int ipod_read(HANDLE dh, unsigned char* buf, int nbytes)
int ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes)
{
return read(dh, buf, nbytes);
return read(ipod->dh, buf, nbytes);
}
int ipod_write(HANDLE dh, unsigned char* buf, int nbytes)
int ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes)
{
return write(dh, buf, nbytes);
return write(ipod->dh, buf, nbytes);
}

View file

@ -66,27 +66,27 @@ void print_error(char* msg)
LocalFree(pMsgBuf);
}
int ipod_open(HANDLE* dh, char* diskname, int* sector_size, int silent)
int ipod_open(struct ipod_t* ipod, int silent)
{
DISK_GEOMETRY_EX diskgeometry_ex;
DISK_GEOMETRY diskgeometry;
unsigned long n;
*dh = CreateFile(diskname, GENERIC_READ,
ipod->dh = CreateFile(ipod->diskname, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
if (*dh == INVALID_HANDLE_VALUE) {
if (ipod->dh == INVALID_HANDLE_VALUE) {
if (!silent) print_error(" Error opening disk: ");
return -1;
}
if (!lock_volume(*dh)) {
if (!lock_volume(ipod->dh)) {
if (!silent) print_error(" Error locking disk: ");
return -1;
}
if (!DeviceIoControl(*dh,
if (!DeviceIoControl(ipod->dh,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
@ -94,7 +94,7 @@ int ipod_open(HANDLE* dh, char* diskname, int* sector_size, int silent)
sizeof(diskgeometry_ex),
&n,
NULL)) {
if (!DeviceIoControl(*dh,
if (!DeviceIoControl(ipod->dh,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
0,
@ -105,31 +105,31 @@ int ipod_open(HANDLE* dh, char* diskname, int* sector_size, int silent)
if (!silent) print_error(" Error reading disk geometry: ");
return -1;
} else {
*sector_size=diskgeometry.BytesPerSector;
ipod->sector_size=diskgeometry.BytesPerSector;
}
} else {
*sector_size=diskgeometry_ex.Geometry.BytesPerSector;
ipod->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
}
return 0;
}
int ipod_reopen_rw(HANDLE* dh, char* diskname)
int ipod_reopen_rw(struct ipod_t* ipod)
{
/* Close existing file and re-open for writing */
unlock_volume(*dh);
CloseHandle(*dh);
unlock_volume(ipod->dh);
CloseHandle(ipod->dh);
*dh = CreateFile(diskname, GENERIC_READ | GENERIC_WRITE,
ipod->dh = CreateFile(ipod->diskname, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
if (*dh == INVALID_HANDLE_VALUE) {
if (ipod->dh == INVALID_HANDLE_VALUE) {
print_error(" Error opening disk: ");
return -1;
}
if (!lock_volume(*dh)) {
if (!lock_volume(ipod->dh)) {
print_error(" Error locking disk: ");
return -1;
}
@ -137,10 +137,10 @@ int ipod_reopen_rw(HANDLE* dh, char* diskname)
return 0;
}
int ipod_close(HANDLE dh)
int ipod_close(struct ipod_t* ipod)
{
unlock_volume(dh);
CloseHandle(dh);
unlock_volume(ipod->dh);
CloseHandle(ipod->dh);
return 0;
}
@ -156,20 +156,20 @@ int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize)
return 0;
}
int ipod_seek(HANDLE dh, unsigned long pos)
int ipod_seek(struct ipod_t* ipod, unsigned long pos)
{
if (SetFilePointer(dh, pos, NULL, FILE_BEGIN)==0xffffffff) {
if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) {
print_error(" Seek error ");
return -1;
}
return 0;
}
int ipod_read(HANDLE dh, unsigned char* buf, int nbytes)
int ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes)
{
unsigned long count;
if (!ReadFile(dh, buf, nbytes, &count, NULL)) {
if (!ReadFile(ipod->dh, buf, nbytes, &count, NULL)) {
print_error(" Error reading from disk: ");
return -1;
}
@ -177,11 +177,11 @@ int ipod_read(HANDLE dh, unsigned char* buf, int nbytes)
return count;
}
int ipod_write(HANDLE dh, unsigned char* buf, int nbytes)
int ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes)
{
unsigned long count;
if (!WriteFile(dh, buf, nbytes, &count, NULL)) {
if (!WriteFile(ipod->dh, buf, nbytes, &count, NULL)) {
print_error(" Error writing to disk: ");
return -1;
}

View file

@ -20,6 +20,8 @@
#ifndef __IPODIO_H
#define __IPODIO_H
#include <stdint.h>
#ifdef __WIN32__
#include <windows.h>
#else
@ -27,13 +29,56 @@
#define O_BINARY 0
#endif
/* The maximum number of images in a firmware partition - a guess... */
#define MAX_IMAGES 10
enum firmwaretype_t {
FTYPE_OSOS = 0,
FTYPE_RSRC,
FTYPE_AUPD,
FTYPE_HIBE
};
struct ipod_directory_t {
enum firmwaretype_t ftype;
int id;
uint32_t devOffset; /* Offset of image relative to one sector into bootpart*/
uint32_t len;
uint32_t addr;
uint32_t entryOffset;
uint32_t chksum;
uint32_t vers;
uint32_t loadAddr;
};
struct partinfo_t {
unsigned long start; /* first sector (LBA) */
unsigned long size; /* number of sectors */
unsigned char type;
};
struct ipod_t {
HANDLE dh;
char diskname[4096];
int sector_size;
struct ipod_directory_t ipod_directory[MAX_IMAGES];
int nimages;
off_t diroffset;
off_t start; /* Offset in bytes of firmware partition from start of disk */
off_t fwoffset; /* Offset in bytes of start of firmware images from start of disk */
struct partinfo_t pinfo[4];
int modelnum;
char* modelname;
char* modelstr;
};
void print_error(char* msg);
int ipod_open(HANDLE* dh, char* diskname, int* sector_size, int silent);
int ipod_reopen_rw(HANDLE* dh, char* diskname);
int ipod_close(HANDLE dh);
int ipod_seek(HANDLE dh, unsigned long pos);
int ipod_read(HANDLE dh, unsigned char* buf, int nbytes);
int ipod_write(HANDLE dh, unsigned char* buf, int nbytes);
int ipod_open(struct ipod_t* ipod, int silent);
int ipod_reopen_rw(struct ipod_t* ipod);
int ipod_close(struct ipod_t* ipod);
int ipod_seek(struct ipod_t* ipod, unsigned long pos);
int ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes);
int ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes);
int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize);
#endif

File diff suppressed because it is too large Load diff