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 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Dominik Riebeling | e23adb5 | 2011-12-07 20:06:48 +0000 | [diff] [blame] | 22 | #if !defined(_WIN32) /* all non-Windows platforms are considered POSIX. */ |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 23 | |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
Dominik Riebeling | 194b2ca | 2008-05-04 11:59:04 +0000 | [diff] [blame] | 32 | #include <errno.h> |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 33 | |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 34 | #include "ipodio.h" |
| 35 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 36 | #if defined(linux) || defined (__linux) |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 37 | #include <sys/mount.h> |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 38 | #include <linux/hdreg.h> |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 39 | #include <scsi/scsi_ioctl.h> |
| 40 | #include <scsi/sg.h> |
| 41 | |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 42 | #define IPOD_SECTORSIZE_IOCTL BLKSSZGET |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 43 | |
| 44 | static void get_geometry(struct ipod_t* ipod) |
| 45 | { |
| 46 | struct hd_geometry geometry; |
| 47 | |
| 48 | if (!ioctl(ipod->dh, HDIO_GETGEO, &geometry)) { |
| 49 | /* never use geometry.cylinders - it is truncated */ |
| 50 | ipod->num_heads = geometry.heads; |
| 51 | ipod->sectors_per_track = geometry.sectors; |
| 52 | } else { |
| 53 | ipod->num_heads = 0; |
| 54 | ipod->sectors_per_track = 0; |
| 55 | } |
| 56 | } |
| 57 | |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 58 | /* Linux SCSI Inquiry code based on the documentation and example code from |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 59 | http://www.ibm.com/developerworks/linux/library/l-scsi-api/index.html |
| 60 | */ |
| 61 | |
| 62 | int ipod_scsi_inquiry(struct ipod_t* ipod, int page_code, |
| 63 | unsigned char* buf, int bufsize) |
| 64 | { |
| 65 | unsigned char cdb[6]; |
| 66 | struct sg_io_hdr hdr; |
| 67 | unsigned char sense_buffer[255]; |
| 68 | |
| 69 | memset(&hdr, 0, sizeof(hdr)); |
| 70 | |
| 71 | hdr.interface_id = 'S'; /* this is the only choice we have! */ |
| 72 | hdr.flags = SG_FLAG_LUN_INHIBIT; /* this would put the LUN to 2nd byte of cdb*/ |
| 73 | |
| 74 | /* Set xfer data */ |
| 75 | hdr.dxferp = buf; |
| 76 | hdr.dxfer_len = bufsize; |
| 77 | |
| 78 | /* Set sense data */ |
| 79 | hdr.sbp = sense_buffer; |
| 80 | hdr.mx_sb_len = sizeof(sense_buffer); |
| 81 | |
| 82 | /* Set the cdb format */ |
| 83 | cdb[0] = 0x12; |
| 84 | cdb[1] = 1; /* Enable Vital Product Data (EVPD) */ |
| 85 | cdb[2] = page_code & 0xff; |
| 86 | cdb[3] = 0; |
| 87 | cdb[4] = 0xff; |
| 88 | cdb[5] = 0; /* For control filed, just use 0 */ |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 89 | |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 90 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; |
| 91 | hdr.cmdp = cdb; |
| 92 | hdr.cmd_len = 6; |
| 93 | |
| 94 | int ret = ioctl(ipod->dh, SG_IO, &hdr); |
| 95 | |
| 96 | if (ret < 0) { |
| 97 | return -1; |
| 98 | } else { |
| 99 | return 0; |
| 100 | } |
| 101 | } |
| 102 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 103 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \ |
| 104 | || defined(__bsdi__) || defined(__DragonFly__) |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 105 | #include <sys/disk.h> |
| 106 | #define IPOD_SECTORSIZE_IOCTL DIOCGSECTORSIZE |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 107 | |
| 108 | /* TODO: Implement this function for BSD */ |
| 109 | static void get_geometry(struct ipod_t* ipod) |
| 110 | { |
| 111 | /* Are these universal for all ipods? */ |
| 112 | ipod->num_heads = 255; |
| 113 | ipod->sectors_per_track = 63; |
| 114 | } |
| 115 | |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 116 | int ipod_scsi_inquiry(struct ipod_t* ipod, int page_code, |
| 117 | unsigned char* buf, int bufsize) |
| 118 | { |
| 119 | /* TODO: Implement for BSD */ |
Dominik Riebeling | 93eefba | 2009-11-29 19:20:38 +0000 | [diff] [blame] | 120 | (void)ipod; |
| 121 | (void)page_code; |
| 122 | (void)buf; |
| 123 | (void)bufsize; |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 127 | #elif defined(__APPLE__) && defined(__MACH__) |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 128 | /* OS X IOKit includes don't like VERSION being defined! */ |
| 129 | #undef VERSION |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 130 | #include <sys/disk.h> |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 131 | #include <CoreFoundation/CoreFoundation.h> |
| 132 | #include <IOKit/IOKitLib.h> |
Dominik Riebeling | 56a9255 | 2014-03-08 18:34:35 +0100 | [diff] [blame] | 133 | #include <IOKit/scsi/SCSITaskLib.h> |
| 134 | #include <IOKit/scsi/SCSICommandOperationCodes.h> |
Dave Chapman | 49e016c | 2006-12-15 00:09:48 +0000 | [diff] [blame] | 135 | #define IPOD_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 136 | |
| 137 | /* TODO: Implement this function for Mac OS X */ |
| 138 | static void get_geometry(struct ipod_t* ipod) |
| 139 | { |
| 140 | /* Are these universal for all ipods? */ |
| 141 | ipod->num_heads = 255; |
| 142 | ipod->sectors_per_track = 63; |
| 143 | } |
| 144 | |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 145 | int ipod_scsi_inquiry(struct ipod_t* ipod, int page_code, |
| 146 | unsigned char* buf, int bufsize) |
| 147 | { |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 148 | /* OS X doesn't allow to simply send out a SCSI inquiry request but |
| 149 | * requires registering an interface handler first. |
| 150 | * Currently this is done on each inquiry request which is somewhat |
| 151 | * inefficient but the current ipodpatcher API doesn't really fit here. |
| 152 | * Based on the documentation in Apple's document |
| 153 | * "SCSI Architecture Model Device Interface Guide". |
| 154 | * |
| 155 | * WARNING: this code currently doesn't take the selected device into |
| 156 | * account. It simply looks for an Ipod on the system and uses |
| 157 | * the first match. |
| 158 | */ |
Dominik Riebeling | 93eefba | 2009-11-29 19:20:38 +0000 | [diff] [blame] | 159 | (void)ipod; |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 160 | int result = 0; |
| 161 | /* first, create a dictionary to match the device. This is needed to get the |
| 162 | * service. */ |
| 163 | CFMutableDictionaryRef match_dict; |
| 164 | match_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL); |
| 165 | if(match_dict == NULL) |
| 166 | return -1; |
| 167 | |
| 168 | /* set value to match. In case of the Ipod this is "iPodUserClientDevice". */ |
| 169 | CFMutableDictionaryRef sub_dict; |
| 170 | sub_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL); |
Dominik Riebeling | 084d57c | 2010-12-19 21:21:28 +0000 | [diff] [blame] | 171 | if(sub_dict == NULL) |
| 172 | return -1; |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 173 | CFDictionarySetValue(sub_dict, CFSTR(kIOPropertySCSITaskDeviceCategory), |
| 174 | CFSTR("iPodUserClientDevice")); |
| 175 | CFDictionarySetValue(match_dict, CFSTR(kIOPropertyMatchKey), sub_dict); |
| 176 | |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 177 | /* get an iterator for searching for the service. */ |
| 178 | kern_return_t kr; |
| 179 | io_iterator_t iterator = IO_OBJECT_NULL; |
| 180 | /* get matching services from IO registry. Consumes one reference to |
| 181 | * the dictionary, so no need to release that. */ |
| 182 | kr = IOServiceGetMatchingServices(kIOMasterPortDefault, match_dict, &iterator); |
| 183 | |
| 184 | if(!iterator | (kr != kIOReturnSuccess)) |
| 185 | return -1; |
| 186 | |
| 187 | /* get interface and obtain exclusive access */ |
| 188 | SInt32 score; |
| 189 | HRESULT herr; |
| 190 | kern_return_t err; |
| 191 | IOCFPlugInInterface **plugin_interface = NULL; |
| 192 | SCSITaskDeviceInterface **interface = NULL; |
| 193 | io_service_t device = IO_OBJECT_NULL; |
| 194 | device = IOIteratorNext(iterator); |
| 195 | |
| 196 | err = IOCreatePlugInInterfaceForService(device, kIOSCSITaskDeviceUserClientTypeID, |
| 197 | kIOCFPlugInInterfaceID, &plugin_interface, |
| 198 | &score); |
| 199 | |
| 200 | if(err != noErr) { |
| 201 | return -1; |
| 202 | } |
| 203 | /* query the plugin interface for task interface */ |
| 204 | herr = (*plugin_interface)->QueryInterface(plugin_interface, |
| 205 | CFUUIDGetUUIDBytes(kIOSCSITaskDeviceInterfaceID), (LPVOID*)&interface); |
| 206 | if(herr != S_OK) { |
| 207 | IODestroyPlugInInterface(plugin_interface); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | err = (*interface)->ObtainExclusiveAccess(interface); |
| 212 | if(err != noErr) { |
| 213 | (*interface)->Release(interface); |
| 214 | IODestroyPlugInInterface(plugin_interface); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | /* do the inquiry */ |
| 219 | SCSITaskInterface **task = NULL; |
| 220 | |
| 221 | task = (*interface)->CreateSCSITask(interface); |
| 222 | if(task != NULL) { |
| 223 | kern_return_t err; |
| 224 | SCSITaskStatus task_status; |
| 225 | IOVirtualRange* range; |
| 226 | SCSI_Sense_Data sense_data; |
| 227 | SCSICommandDescriptorBlock cdb; |
| 228 | UInt64 transfer_count = 0; |
| 229 | memset(buf, 0, bufsize); |
| 230 | /* allocate virtual range for buffer. */ |
| 231 | range = (IOVirtualRange*) malloc(sizeof(IOVirtualRange)); |
| 232 | memset(&sense_data, 0, sizeof(sense_data)); |
| 233 | memset(cdb, 0, sizeof(cdb)); |
| 234 | /* set up range. address is buffer address, length is request size. */ |
| 235 | range->address = (IOVirtualAddress)buf; |
| 236 | range->length = bufsize; |
| 237 | /* setup CDB */ |
| 238 | cdb[0] = 0x12; /* inquiry */ |
| 239 | cdb[1] = 1; |
| 240 | cdb[2] = page_code; |
| 241 | cdb[4] = bufsize; |
| 242 | |
| 243 | /* set cdb in task */ |
| 244 | err = (*task)->SetCommandDescriptorBlock(task, cdb, kSCSICDBSize_6Byte); |
| 245 | if(err != kIOReturnSuccess) { |
| 246 | result = -1; |
| 247 | goto cleanup; |
| 248 | } |
| 249 | err = (*task)->SetScatterGatherEntries(task, range, 1, bufsize, |
| 250 | kSCSIDataTransfer_FromTargetToInitiator); |
| 251 | if(err != kIOReturnSuccess) { |
| 252 | result = -1; |
| 253 | goto cleanup; |
| 254 | } |
| 255 | /* set timeout */ |
| 256 | err = (*task)->SetTimeoutDuration(task, 10000); |
| 257 | if(err != kIOReturnSuccess) { |
| 258 | result = -1; |
| 259 | goto cleanup; |
| 260 | } |
| 261 | |
| 262 | /* request data */ |
| 263 | err = (*task)->ExecuteTaskSync(task, &sense_data, &task_status, &transfer_count); |
| 264 | if(err != kIOReturnSuccess) { |
| 265 | result = -1; |
| 266 | goto cleanup; |
| 267 | } |
| 268 | /* cleanup */ |
| 269 | free(range); |
| 270 | |
| 271 | /* release task interface */ |
| 272 | (*task)->Release(task); |
| 273 | } |
| 274 | else { |
| 275 | result = -1; |
| 276 | } |
| 277 | cleanup: |
| 278 | /* cleanup interface */ |
| 279 | (*interface)->ReleaseExclusiveAccess(interface); |
| 280 | (*interface)->Release(interface); |
| 281 | IODestroyPlugInInterface(plugin_interface); |
| 282 | |
| 283 | return result; |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 286 | #else |
| 287 | #error No sector-size detection implemented for this platform |
| 288 | #endif |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 289 | |
Dave Chapman | f225f04 | 2007-09-01 22:55:37 +0000 | [diff] [blame] | 290 | #if defined(__APPLE__) && defined(__MACH__) |
| 291 | static int ipod_unmount(struct ipod_t* ipod) |
| 292 | { |
| 293 | char cmd[4096]; |
| 294 | int res; |
| 295 | |
| 296 | sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss2\"",ipod->diskname); |
| 297 | fprintf(stderr,"[INFO] "); |
| 298 | res = system(cmd); |
| 299 | |
| 300 | if (res==0) { |
| 301 | return 0; |
| 302 | } else { |
| 303 | perror("Unmount failed"); |
| 304 | return -1; |
| 305 | } |
| 306 | } |
| 307 | #endif |
| 308 | |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 309 | void ipod_print_error(char* msg) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 310 | { |
| 311 | perror(msg); |
| 312 | } |
| 313 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 314 | int ipod_open(struct ipod_t* ipod, int silent) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 315 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 316 | ipod->dh=open(ipod->diskname,O_RDONLY); |
| 317 | if (ipod->dh < 0) { |
| 318 | if (!silent) perror(ipod->diskname); |
Dominik Riebeling | 194b2ca | 2008-05-04 11:59:04 +0000 | [diff] [blame] | 319 | if(errno == EACCES) return -2; |
| 320 | else return -1; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 323 | /* Read information about the disk */ |
| 324 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 325 | if(ioctl(ipod->dh,IPOD_SECTORSIZE_IOCTL,&ipod->sector_size) < 0) { |
Dave Chapman | 4280640 | 2010-01-28 09:37:05 +0000 | [diff] [blame] | 326 | ipod->sector_size=512; |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 327 | if (!silent) { |
| 328 | fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n" |
| 329 | ,ipod->sector_size); |
Dominik Riebeling | c8d13b6 | 2010-01-30 17:02:37 +0000 | [diff] [blame] | 330 | } |
Dave Chapman | 8280c8c | 2006-12-13 20:26:44 +0000 | [diff] [blame] | 331 | } |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 332 | |
| 333 | get_geometry(ipod); |
| 334 | |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 339 | int ipod_reopen_rw(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 340 | { |
Dave Chapman | f225f04 | 2007-09-01 22:55:37 +0000 | [diff] [blame] | 341 | #if defined(__APPLE__) && defined(__MACH__) |
| 342 | if (ipod_unmount(ipod) < 0) |
| 343 | return -1; |
| 344 | #endif |
| 345 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 346 | close(ipod->dh); |
| 347 | ipod->dh=open(ipod->diskname,O_RDWR); |
| 348 | if (ipod->dh < 0) { |
| 349 | perror(ipod->diskname); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 350 | return -1; |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 355 | int ipod_close(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 356 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 357 | close(ipod->dh); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 361 | int ipod_alloc_buffer(struct ipod_t* ipod, int bufsize) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 362 | { |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 363 | ipod->sectorbuf = malloc(bufsize); |
| 364 | if (ipod->sectorbuf== NULL) { |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 365 | return -1; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
Dominik Riebeling | b63d429 | 2013-01-01 11:04:21 +0100 | [diff] [blame] | 370 | int ipod_dealloc_buffer(struct ipod_t* ipod) |
| 371 | { |
| 372 | if (ipod->sectorbuf == NULL) { |
| 373 | return -1; |
| 374 | } |
| 375 | free(ipod->sectorbuf); |
| 376 | ipod->sectorbuf = NULL; |
| 377 | return 0; |
| 378 | } |
| 379 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 380 | int ipod_seek(struct ipod_t* ipod, unsigned long pos) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 381 | { |
| 382 | off_t res; |
| 383 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 384 | res = lseek(ipod->dh, pos, SEEK_SET); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 385 | |
| 386 | if (res == -1) { |
| 387 | return -1; |
| 388 | } |
| 389 | return 0; |
| 390 | } |
| 391 | |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 392 | ssize_t ipod_read(struct ipod_t* ipod, int nbytes) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 393 | { |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 394 | if(ipod->sectorbuf == NULL) { |
| 395 | return -1; |
| 396 | } |
| 397 | return read(ipod->dh, ipod->sectorbuf, nbytes); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 400 | ssize_t ipod_write(struct ipod_t* ipod, int nbytes) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 401 | { |
Dominik Riebeling | 24e37dd | 2012-12-23 23:30:57 +0100 | [diff] [blame] | 402 | if(ipod->sectorbuf == NULL) { |
| 403 | return -1; |
| 404 | } |
| 405 | return write(ipod->dh, ipod->sectorbuf, nbytes); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 406 | } |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 407 | |
Dominik Riebeling | e23adb5 | 2011-12-07 20:06:48 +0000 | [diff] [blame] | 408 | #endif |
| 409 | |