Check for permission denied error when trying to access the player and inform the...
[Rockbox.git] / rbutil / ipodpatcher / ipodio-posix.c
blobc62e0c827562c93190d645897788a92f26ed5f4f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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>
27 #include <sys/ioctl.h>
28 #include <errno.h>
30 #include "ipodio.h"
32 #if defined(linux) || defined (__linux)
33 #include <sys/mount.h>
34 #include <linux/hdreg.h>
35 #define IPOD_SECTORSIZE_IOCTL BLKSSZGET
37 static void get_geometry(struct ipod_t* ipod)
39 struct hd_geometry geometry;
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;
51 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
52 || defined(__bsdi__) || defined(__DragonFly__)
53 #include <sys/disk.h>
54 #define IPOD_SECTORSIZE_IOCTL DIOCGSECTORSIZE
56 /* TODO: Implement this function for BSD */
57 static void get_geometry(struct ipod_t* ipod)
59 /* Are these universal for all ipods? */
60 ipod->num_heads = 255;
61 ipod->sectors_per_track = 63;
64 #elif defined(__APPLE__) && defined(__MACH__)
65 #include <sys/disk.h>
66 #define IPOD_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
68 /* TODO: Implement this function for Mac OS X */
69 static void get_geometry(struct ipod_t* ipod)
71 /* Are these universal for all ipods? */
72 ipod->num_heads = 255;
73 ipod->sectors_per_track = 63;
76 #else
77 #error No sector-size detection implemented for this platform
78 #endif
80 #if defined(__APPLE__) && defined(__MACH__)
81 static int ipod_unmount(struct ipod_t* ipod)
83 char cmd[4096];
84 int res;
86 sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss2\"",ipod->diskname);
87 fprintf(stderr,"[INFO] ");
88 res = system(cmd);
90 if (res==0) {
91 return 0;
92 } else {
93 perror("Unmount failed");
94 return -1;
97 #endif
99 void print_error(char* msg)
101 perror(msg);
104 int ipod_open(struct ipod_t* ipod, int silent)
106 ipod->dh=open(ipod->diskname,O_RDONLY);
107 if (ipod->dh < 0) {
108 if (!silent) perror(ipod->diskname);
109 if(errno == EACCES) return -2;
110 else return -1;
113 /* Read information about the disk */
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);
123 get_geometry(ipod);
125 return 0;
129 int ipod_reopen_rw(struct ipod_t* ipod)
131 #if defined(__APPLE__) && defined(__MACH__)
132 if (ipod_unmount(ipod) < 0)
133 return -1;
134 #endif
136 close(ipod->dh);
137 ipod->dh=open(ipod->diskname,O_RDWR);
138 if (ipod->dh < 0) {
139 perror(ipod->diskname);
140 return -1;
142 return 0;
145 int ipod_close(struct ipod_t* ipod)
147 close(ipod->dh);
148 return 0;
151 int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize)
153 *sectorbuf=malloc(bufsize);
154 if (*sectorbuf == NULL) {
155 return -1;
157 return 0;
160 int ipod_seek(struct ipod_t* ipod, unsigned long pos)
162 off_t res;
164 res = lseek(ipod->dh, pos, SEEK_SET);
166 if (res == -1) {
167 return -1;
169 return 0;
172 ssize_t ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes)
174 return read(ipod->dh, buf, nbytes);
177 ssize_t ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes)
179 return write(ipod->dh, buf, nbytes);