blob: 22abc883d26f155074744c18afb53a6b294f012c [file] [log] [blame]
Dave Chapmane17043e2007-03-15 22:55:36 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Dave Chapman4a812912007-03-15 23:02:37 +00008 * $Id$
Dave Chapmane17043e2007-03-15 22:55:36 +00009 *
10 * Copyright (C) 2006-2007 Dave Chapman
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>
27#include <sys/ioctl.h>
28
29#if defined(linux) || defined (__linux)
30#include <sys/mount.h>
31#define SANSA_SECTORSIZE_IOCTL BLKSSZGET
32#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
33 || defined(__bsdi__) || defined(__DragonFly__)
34#include <sys/disk.h>
35#define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
36#elif defined(__APPLE__) && defined(__MACH__)
37#include <sys/disk.h>
38#define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
39#else
40 #error No sector-size detection implemented for this platform
41#endif
42
43#include "sansaio.h"
44
Dave Chapmanad4b8862007-09-02 19:20:59 +000045#if defined(__APPLE__) && defined(__MACH__)
46static int sansa_unmount(struct sansa_t* sansa)
47{
48 char cmd[4096];
49 int res;
50
51 sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname);
52 fprintf(stderr,"[INFO] ");
53 res = system(cmd);
54
55 if (res==0) {
56 return 0;
57 } else {
58 perror("Unmount failed");
59 return -1;
60 }
61}
62#endif
63
64
Dominik Wengerdde262b2007-05-03 20:07:57 +000065#ifndef RBUTIL
Dave Chapmane17043e2007-03-15 22:55:36 +000066void print_error(char* msg)
67{
68 perror(msg);
69}
Dominik Wengerdde262b2007-05-03 20:07:57 +000070#endif
Dave Chapmane17043e2007-03-15 22:55:36 +000071
72int sansa_open(struct sansa_t* sansa, int silent)
73{
74 sansa->dh=open(sansa->diskname,O_RDONLY);
75 if (sansa->dh < 0) {
76 if (!silent) perror(sansa->diskname);
77 return -1;
78 }
79
80 if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) {
81 sansa->sector_size=512;
82 if (!silent) {
83 fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
84 ,sansa->sector_size);
85 }
86 }
87 return 0;
88}
89
90
91int sansa_reopen_rw(struct sansa_t* sansa)
92{
Dave Chapmanad4b8862007-09-02 19:20:59 +000093#if defined(__APPLE__) && defined(__MACH__)
94 if (sansa_unmount(sansa) < 0)
95 return -1;
96#endif
97
Dave Chapmane17043e2007-03-15 22:55:36 +000098 close(sansa->dh);
99 sansa->dh=open(sansa->diskname,O_RDWR);
100 if (sansa->dh < 0) {
101 perror(sansa->diskname);
102 return -1;
103 }
104 return 0;
105}
106
107int sansa_close(struct sansa_t* sansa)
108{
109 close(sansa->dh);
110 return 0;
111}
112
113int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
114{
115 *sectorbuf=malloc(bufsize);
116 if (*sectorbuf == NULL) {
117 return -1;
118 }
119 return 0;
120}
121
122int sansa_seek(struct sansa_t* sansa, loff_t pos)
123{
124 off_t res;
125
126 res = lseek64(sansa->dh, pos, SEEK_SET);
127
128 if (res == -1) {
129 return -1;
130 }
131 return 0;
132}
133
134int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
135{
136 return read(sansa->dh, buf, nbytes);
137}
138
139int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
140{
141 return write(sansa->dh, buf, nbytes);
142}