1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2007 Dave Chapman
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #if !defined(_WIN32) /* all non-Windows platforms supported are POSIX. */
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #if defined(linux) || defined (__linux)
34 #include <sys/mount.h>
35 #define SANSA_SECTORSIZE_IOCTL BLKSSZGET
36 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
37 || defined(__bsdi__) || defined(__DragonFly__)
39 #define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
40 #elif defined(__APPLE__) && defined(__MACH__)
42 #define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
44 #error No sector-size detection implemented for this platform
49 #if defined(__APPLE__) && defined(__MACH__)
50 static int sansa_unmount(struct sansa_t
* sansa
)
55 sprintf(cmd
, "/usr/sbin/diskutil unmount \"%ss1\"",sansa
->diskname
);
56 fprintf(stderr
,"[INFO] ");
62 perror("Unmount failed");
69 void sansa_print_error(char* msg
)
74 int sansa_open(struct sansa_t
* sansa
, int silent
)
76 sansa
->dh
=open(sansa
->diskname
,O_RDONLY
);
78 if (!silent
) perror(sansa
->diskname
);
79 if(errno
== EACCES
) return -2;
83 if(ioctl(sansa
->dh
,SANSA_SECTORSIZE_IOCTL
,&sansa
->sector_size
) < 0) {
84 sansa
->sector_size
=512;
86 fprintf(stderr
,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
94 int sansa_reopen_rw(struct sansa_t
* sansa
)
96 #if defined(__APPLE__) && defined(__MACH__)
97 if (sansa_unmount(sansa
) < 0)
102 sansa
->dh
=open(sansa
->diskname
,O_RDWR
);
104 perror(sansa
->diskname
);
110 int sansa_close(struct sansa_t
* sansa
)
116 int sansa_alloc_buffer(struct sansa_t
*sansa
, int bufsize
)
118 sansa
->sectorbuf
=malloc(bufsize
);
119 if (sansa
->sectorbuf
== NULL
) {
125 int sansa_dealloc_buffer(struct sansa_t
* sansa
)
127 if (sansa
->sectorbuf
== NULL
) {
130 free(sansa
->sectorbuf
);
131 sansa
->sectorbuf
= NULL
;
135 int sansa_seek(struct sansa_t
* sansa
, loff_t pos
)
139 res
= lseek64(sansa
->dh
, pos
, SEEK_SET
);
147 int sansa_read(struct sansa_t
* sansa
, unsigned char* buf
, int nbytes
)
149 return read(sansa
->dh
, buf
, nbytes
);
152 int sansa_write(struct sansa_t
* sansa
, int nbytes
)
154 return write(sansa
->dh
, sansa
->sectorbuf
, nbytes
);