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