Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
Dave Chapman | e332f4c | 2007-02-05 01:20:20 +0000 | [diff] [blame] | 10 | * Copyright (C) 2006-2007 Dave Chapman |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 11 | * |
| 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. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Dominik Riebeling | 194b2ca | 2008-05-04 11:59:04 +0000 | [diff] [blame^] | 28 | #include <errno.h> |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 29 | |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 30 | #include "ipodio.h" |
| 31 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 32 | #if defined(linux) || defined (__linux) |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 33 | #include <sys/mount.h> |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 34 | #include <linux/hdreg.h> |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 35 | #define IPOD_SECTORSIZE_IOCTL BLKSSZGET |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 36 | |
| 37 | static void get_geometry(struct ipod_t* ipod) |
| 38 | { |
| 39 | struct hd_geometry geometry; |
| 40 | |
| 41 | if (!ioctl(ipod->dh, HDIO_GETGEO, &geometry)) { |
| 42 | /* never use geometry.cylinders - it is truncated */ |
| 43 | ipod->num_heads = geometry.heads; |
| 44 | ipod->sectors_per_track = geometry.sectors; |
| 45 | } else { |
| 46 | ipod->num_heads = 0; |
| 47 | ipod->sectors_per_track = 0; |
| 48 | } |
| 49 | } |
| 50 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 51 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \ |
| 52 | || defined(__bsdi__) || defined(__DragonFly__) |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 53 | #include <sys/disk.h> |
| 54 | #define IPOD_SECTORSIZE_IOCTL DIOCGSECTORSIZE |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 55 | |
| 56 | /* TODO: Implement this function for BSD */ |
| 57 | static void get_geometry(struct ipod_t* ipod) |
| 58 | { |
| 59 | /* Are these universal for all ipods? */ |
| 60 | ipod->num_heads = 255; |
| 61 | ipod->sectors_per_track = 63; |
| 62 | } |
| 63 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 64 | #elif defined(__APPLE__) && defined(__MACH__) |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 65 | #include <sys/disk.h> |
| 66 | #define IPOD_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 67 | |
| 68 | /* TODO: Implement this function for Mac OS X */ |
| 69 | static void get_geometry(struct ipod_t* ipod) |
| 70 | { |
| 71 | /* Are these universal for all ipods? */ |
| 72 | ipod->num_heads = 255; |
| 73 | ipod->sectors_per_track = 63; |
| 74 | } |
| 75 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 76 | #else |
| 77 | #error No sector-size detection implemented for this platform |
| 78 | #endif |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 79 | |
Dave Chapman | f225f04 | 2007-09-01 22:55:37 +0000 | [diff] [blame] | 80 | #if defined(__APPLE__) && defined(__MACH__) |
| 81 | static int ipod_unmount(struct ipod_t* ipod) |
| 82 | { |
| 83 | char cmd[4096]; |
| 84 | int res; |
| 85 | |
| 86 | sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss2\"",ipod->diskname); |
| 87 | fprintf(stderr,"[INFO] "); |
| 88 | res = system(cmd); |
| 89 | |
| 90 | if (res==0) { |
| 91 | return 0; |
| 92 | } else { |
| 93 | perror("Unmount failed"); |
| 94 | return -1; |
| 95 | } |
| 96 | } |
| 97 | #endif |
| 98 | |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 99 | void print_error(char* msg) |
| 100 | { |
| 101 | perror(msg); |
| 102 | } |
| 103 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 104 | int ipod_open(struct ipod_t* ipod, int silent) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 105 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 106 | ipod->dh=open(ipod->diskname,O_RDONLY); |
| 107 | if (ipod->dh < 0) { |
| 108 | if (!silent) perror(ipod->diskname); |
Dominik Riebeling | 194b2ca | 2008-05-04 11:59:04 +0000 | [diff] [blame^] | 109 | if(errno == EACCES) return -2; |
| 110 | else return -1; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 113 | /* Read information about the disk */ |
| 114 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 115 | if(ioctl(ipod->dh,IPOD_SECTORSIZE_IOCTL,&ipod->sector_size) < 0) { |
| 116 | ipod->sector_size=512; |
| 117 | if (!silent) { |
| 118 | fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n" |
| 119 | ,ipod->sector_size); |
| 120 | } |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 121 | } |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 122 | |
| 123 | get_geometry(ipod); |
| 124 | |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 129 | int ipod_reopen_rw(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 130 | { |
Dave Chapman | f225f04 | 2007-09-01 22:55:37 +0000 | [diff] [blame] | 131 | #if defined(__APPLE__) && defined(__MACH__) |
| 132 | if (ipod_unmount(ipod) < 0) |
| 133 | return -1; |
| 134 | #endif |
| 135 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 136 | close(ipod->dh); |
| 137 | ipod->dh=open(ipod->diskname,O_RDWR); |
| 138 | if (ipod->dh < 0) { |
| 139 | perror(ipod->diskname); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 140 | return -1; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 145 | int ipod_close(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 146 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 147 | close(ipod->dh); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize) |
| 152 | { |
| 153 | *sectorbuf=malloc(bufsize); |
| 154 | if (*sectorbuf == NULL) { |
| 155 | return -1; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 160 | int ipod_seek(struct ipod_t* ipod, unsigned long pos) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 161 | { |
| 162 | off_t res; |
| 163 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 164 | res = lseek(ipod->dh, pos, SEEK_SET); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 165 | |
| 166 | if (res == -1) { |
| 167 | return -1; |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
Dave Chapman | 2cc80f5 | 2007-07-29 21:19:14 +0000 | [diff] [blame] | 172 | ssize_t ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 173 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 174 | return read(ipod->dh, buf, nbytes); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Dave Chapman | 2cc80f5 | 2007-07-29 21:19:14 +0000 | [diff] [blame] | 177 | ssize_t ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 178 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 179 | return write(ipod->dh, buf, nbytes); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 180 | } |