summaryrefslogtreecommitdiffstats
path: root/rbutil/sansapatcher/sansaio-posix.c
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2007-03-15 22:55:36 +0000
committerDave Chapman <dave@dchapman.com>2007-03-15 22:55:36 +0000
commite17043ead72180ffa18a3b926cbabe09b5e3f903 (patch)
tree02f708ccb7a417c3f6d1a99bd9fdd3ea2941d6c5 /rbutil/sansapatcher/sansaio-posix.c
parenta42070df8566baa4a95fc04f2205a1e0aa240e00 (diff)
downloadrockbox-e17043ead72180ffa18a3b926cbabe09b5e3f903.tar.gz
rockbox-e17043ead72180ffa18a3b926cbabe09b5e3f903.zip
Initial commit of sansapatcher - an installation tool for the Sansa E200.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12792 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/sansapatcher/sansaio-posix.c')
-rw-r--r--rbutil/sansapatcher/sansaio-posix.c115
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 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id: ipodio-posix.c 12263 2007-02-10 19:49:43Z dave $
+ *
+ * Copyright (C) 2006-2007 Dave Chapman
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#if defined(linux) || defined (__linux)
+#include <sys/mount.h>
+#define SANSA_SECTORSIZE_IOCTL BLKSSZGET
+#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
+ || defined(__bsdi__) || defined(__DragonFly__)
+#include <sys/disk.h>
+#define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
+#elif defined(__APPLE__) && defined(__MACH__)
+#include <sys/disk.h>
+#define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
+#else
+ #error No sector-size detection implemented for this platform
+#endif
+
+#include "sansaio.h"
+
+void print_error(char* msg)
+{
+ perror(msg);
+}
+
+int sansa_open(struct sansa_t* sansa, int silent)
+{
+ sansa->dh=open(sansa->diskname,O_RDONLY);
+ if (sansa->dh < 0) {
+ if (!silent) perror(sansa->diskname);
+ return -1;
+ }
+
+ if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) {
+ sansa->sector_size=512;
+ if (!silent) {
+ fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
+ ,sansa->sector_size);
+ }
+ }
+ return 0;
+}
+
+
+int sansa_reopen_rw(struct sansa_t* sansa)
+{
+ close(sansa->dh);
+ sansa->dh=open(sansa->diskname,O_RDWR);
+ if (sansa->dh < 0) {
+ perror(sansa->diskname);
+ return -1;
+ }
+ return 0;
+}
+
+int sansa_close(struct sansa_t* sansa)
+{
+ close(sansa->dh);
+ return 0;
+}
+
+int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
+{
+ *sectorbuf=malloc(bufsize);
+ if (*sectorbuf == NULL) {
+ return -1;
+ }
+ return 0;
+}
+
+int sansa_seek(struct sansa_t* sansa, loff_t pos)
+{
+ off_t res;
+
+ res = lseek64(sansa->dh, pos, SEEK_SET);
+
+ if (res == -1) {
+ return -1;
+ }
+ return 0;
+}
+
+int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
+{
+ return read(sansa->dh, buf, nbytes);
+}
+
+int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
+{
+ return write(sansa->dh, buf, nbytes);
+}