2006-12-13 09:02:18 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-02-05 01:20:20 +00:00
|
|
|
* Copyright (C) 2006-2007 Dave Chapman
|
2006-12-13 09:02:18 +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>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2006-12-13 20:26:44 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2007-06-16 22:32:57 +00:00
|
|
|
#include "ipodio.h"
|
|
|
|
|
2006-12-13 20:26:44 +00:00
|
|
|
#if defined(linux) || defined (__linux)
|
2006-12-15 00:09:48 +00:00
|
|
|
#include <sys/mount.h>
|
2007-06-16 22:32:57 +00:00
|
|
|
#include <linux/hdreg.h>
|
2006-12-15 00:09:48 +00:00
|
|
|
#define IPOD_SECTORSIZE_IOCTL BLKSSZGET
|
2007-06-16 22:32:57 +00:00
|
|
|
|
|
|
|
static void get_geometry(struct ipod_t* ipod)
|
|
|
|
{
|
|
|
|
struct hd_geometry geometry;
|
|
|
|
|
|
|
|
if (!ioctl(ipod->dh, HDIO_GETGEO, &geometry)) {
|
|
|
|
/* never use geometry.cylinders - it is truncated */
|
|
|
|
ipod->num_heads = geometry.heads;
|
|
|
|
ipod->sectors_per_track = geometry.sectors;
|
|
|
|
} else {
|
|
|
|
ipod->num_heads = 0;
|
|
|
|
ipod->sectors_per_track = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-13 20:26:44 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
|
|
|
|
|| defined(__bsdi__) || defined(__DragonFly__)
|
2006-12-15 00:09:48 +00:00
|
|
|
#include <sys/disk.h>
|
|
|
|
#define IPOD_SECTORSIZE_IOCTL DIOCGSECTORSIZE
|
2007-06-16 22:32:57 +00:00
|
|
|
|
|
|
|
/* TODO: Implement this function for BSD */
|
|
|
|
static void get_geometry(struct ipod_t* ipod)
|
|
|
|
{
|
|
|
|
/* Are these universal for all ipods? */
|
|
|
|
ipod->num_heads = 255;
|
|
|
|
ipod->sectors_per_track = 63;
|
|
|
|
}
|
|
|
|
|
2006-12-13 20:26:44 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
2006-12-15 00:09:48 +00:00
|
|
|
#include <sys/disk.h>
|
|
|
|
#define IPOD_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
|
2007-06-16 22:32:57 +00:00
|
|
|
|
|
|
|
/* TODO: Implement this function for Mac OS X */
|
|
|
|
static void get_geometry(struct ipod_t* ipod)
|
|
|
|
{
|
|
|
|
/* Are these universal for all ipods? */
|
|
|
|
ipod->num_heads = 255;
|
|
|
|
ipod->sectors_per_track = 63;
|
|
|
|
}
|
|
|
|
|
2006-12-13 20:26:44 +00:00
|
|
|
#else
|
|
|
|
#error No sector-size detection implemented for this platform
|
|
|
|
#endif
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
void print_error(char* msg)
|
|
|
|
{
|
|
|
|
perror(msg);
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_open(struct ipod_t* ipod, int silent)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
ipod->dh=open(ipod->diskname,O_RDONLY);
|
|
|
|
if (ipod->dh < 0) {
|
|
|
|
if (!silent) perror(ipod->diskname);
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-06-16 22:32:57 +00:00
|
|
|
/* Read information about the disk */
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-12-13 20:26:44 +00:00
|
|
|
}
|
2007-06-16 22:32:57 +00:00
|
|
|
|
|
|
|
get_geometry(ipod);
|
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_reopen_rw(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
close(ipod->dh);
|
|
|
|
ipod->dh=open(ipod->diskname,O_RDWR);
|
|
|
|
if (ipod->dh < 0) {
|
|
|
|
perror(ipod->diskname);
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_close(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
close(ipod->dh);
|
2006-12-13 09:02:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize)
|
|
|
|
{
|
|
|
|
*sectorbuf=malloc(bufsize);
|
|
|
|
if (*sectorbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_seek(struct ipod_t* ipod, unsigned long pos)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
off_t res;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
res = lseek(ipod->dh, pos, SEEK_SET);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-29 21:19:14 +00:00
|
|
|
ssize_t ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
return read(ipod->dh, buf, nbytes);
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
2007-07-29 21:19:14 +00:00
|
|
|
ssize_t ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
return write(ipod->dh, buf, nbytes);
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|