diff options
Diffstat (limited to 'rbutil/sansapatcher/sansaio-posix.c')
-rw-r--r-- | rbutil/sansapatcher/sansaio-posix.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/rbutil/sansapatcher/sansaio-posix.c b/rbutil/sansapatcher/sansaio-posix.c new file mode 100644 index 0000000000..a8d529c20b --- /dev/null +++ b/rbutil/sansapatcher/sansaio-posix.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id: ipodio-posix.c 12263 2007-02-10 19:49:43Z dave $ | ||
9 | * | ||
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 | |||
45 | void print_error(char* msg) | ||
46 | { | ||
47 | perror(msg); | ||
48 | } | ||
49 | |||
50 | int sansa_open(struct sansa_t* sansa, int silent) | ||
51 | { | ||
52 | sansa->dh=open(sansa->diskname,O_RDONLY); | ||
53 | if (sansa->dh < 0) { | ||
54 | if (!silent) perror(sansa->diskname); | ||
55 | return -1; | ||
56 | } | ||
57 | |||
58 | if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) { | ||
59 | sansa->sector_size=512; | ||
60 | if (!silent) { | ||
61 | fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n" | ||
62 | ,sansa->sector_size); | ||
63 | } | ||
64 | } | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | |||
69 | int sansa_reopen_rw(struct sansa_t* sansa) | ||
70 | { | ||
71 | close(sansa->dh); | ||
72 | sansa->dh=open(sansa->diskname,O_RDWR); | ||
73 | if (sansa->dh < 0) { | ||
74 | perror(sansa->diskname); | ||
75 | return -1; | ||
76 | } | ||
77 | return 0; | ||
78 | } | ||
79 | |||
80 | int sansa_close(struct sansa_t* sansa) | ||
81 | { | ||
82 | close(sansa->dh); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize) | ||
87 | { | ||
88 | *sectorbuf=malloc(bufsize); | ||
89 | if (*sectorbuf == NULL) { | ||
90 | return -1; | ||
91 | } | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | int sansa_seek(struct sansa_t* sansa, loff_t pos) | ||
96 | { | ||
97 | off_t res; | ||
98 | |||
99 | res = lseek64(sansa->dh, pos, SEEK_SET); | ||
100 | |||
101 | if (res == -1) { | ||
102 | return -1; | ||
103 | } | ||
104 | return 0; | ||
105 | } | ||
106 | |||
107 | int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes) | ||
108 | { | ||
109 | return read(sansa->dh, buf, nbytes); | ||
110 | } | ||
111 | |||
112 | int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes) | ||
113 | { | ||
114 | return write(sansa->dh, buf, nbytes); | ||
115 | } | ||