1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2007 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
31 #if defined(linux) || defined (__linux)
32 #include <sys/mount.h>
33 #include <linux/hdreg.h>
34 #define IPOD_SECTORSIZE_IOCTL BLKSSZGET
36 static void get_geometry(struct ipod_t
* ipod
)
38 struct hd_geometry geometry
;
40 if (!ioctl(ipod
->dh
, HDIO_GETGEO
, &geometry
)) {
41 /* never use geometry.cylinders - it is truncated */
42 ipod
->num_heads
= geometry
.heads
;
43 ipod
->sectors_per_track
= geometry
.sectors
;
46 ipod
->sectors_per_track
= 0;
50 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
51 || defined(__bsdi__) || defined(__DragonFly__)
53 #define IPOD_SECTORSIZE_IOCTL DIOCGSECTORSIZE
55 /* TODO: Implement this function for BSD */
56 static void get_geometry(struct ipod_t
* ipod
)
58 /* Are these universal for all ipods? */
59 ipod
->num_heads
= 255;
60 ipod
->sectors_per_track
= 63;
63 #elif defined(__APPLE__) && defined(__MACH__)
65 #define IPOD_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
67 /* TODO: Implement this function for Mac OS X */
68 static void get_geometry(struct ipod_t
* ipod
)
70 /* Are these universal for all ipods? */
71 ipod
->num_heads
= 255;
72 ipod
->sectors_per_track
= 63;
76 #error No sector-size detection implemented for this platform
79 #if defined(__APPLE__) && defined(__MACH__)
80 static int ipod_unmount(struct ipod_t
* ipod
)
85 sprintf(cmd
, "/usr/sbin/diskutil unmount \"%ss2\"",ipod
->diskname
);
86 fprintf(stderr
,"[INFO] ");
92 perror("Unmount failed");
98 void print_error(char* msg
)
103 int ipod_open(struct ipod_t
* ipod
, int silent
)
105 ipod
->dh
=open(ipod
->diskname
,O_RDONLY
);
107 if (!silent
) perror(ipod
->diskname
);
111 /* Read information about the disk */
113 if(ioctl(ipod
->dh
,IPOD_SECTORSIZE_IOCTL
,&ipod
->sector_size
) < 0) {
114 ipod
->sector_size
=512;
116 fprintf(stderr
,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
127 int ipod_reopen_rw(struct ipod_t
* ipod
)
129 #if defined(__APPLE__) && defined(__MACH__)
130 if (ipod_unmount(ipod
) < 0)
135 ipod
->dh
=open(ipod
->diskname
,O_RDWR
);
137 perror(ipod
->diskname
);
143 int ipod_close(struct ipod_t
* ipod
)
149 int ipod_alloc_buffer(unsigned char** sectorbuf
, int bufsize
)
151 *sectorbuf
=malloc(bufsize
);
152 if (*sectorbuf
== NULL
) {
158 int ipod_seek(struct ipod_t
* ipod
, unsigned long pos
)
162 res
= lseek(ipod
->dh
, pos
, SEEK_SET
);
170 ssize_t
ipod_read(struct ipod_t
* ipod
, unsigned char* buf
, int nbytes
)
172 return read(ipod
->dh
, buf
, nbytes
);
175 ssize_t
ipod_write(struct ipod_t
* ipod
, unsigned char* buf
, int nbytes
)
177 return write(ipod
->dh
, buf
, nbytes
);