Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Dave Chapman | 4a81291 | 2007-03-15 23:02:37 +0000 | [diff] [blame] | 8 | * $Id$ |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2006-2007 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/ioctl.h> |
Dominik Riebeling | 850c4f9 | 2008-05-11 16:58:02 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 31 | |
| 32 | #if defined(linux) || defined (__linux) |
| 33 | #include <sys/mount.h> |
| 34 | #define SANSA_SECTORSIZE_IOCTL BLKSSZGET |
| 35 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \ |
| 36 | || defined(__bsdi__) || defined(__DragonFly__) |
| 37 | #include <sys/disk.h> |
| 38 | #define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE |
| 39 | #elif defined(__APPLE__) && defined(__MACH__) |
| 40 | #include <sys/disk.h> |
| 41 | #define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE |
| 42 | #else |
| 43 | #error No sector-size detection implemented for this platform |
| 44 | #endif |
| 45 | |
| 46 | #include "sansaio.h" |
| 47 | |
Dave Chapman | ad4b886 | 2007-09-02 19:20:59 +0000 | [diff] [blame] | 48 | #if defined(__APPLE__) && defined(__MACH__) |
| 49 | static int sansa_unmount(struct sansa_t* sansa) |
| 50 | { |
| 51 | char cmd[4096]; |
| 52 | int res; |
| 53 | |
| 54 | sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname); |
| 55 | fprintf(stderr,"[INFO] "); |
| 56 | res = system(cmd); |
| 57 | |
| 58 | if (res==0) { |
| 59 | return 0; |
| 60 | } else { |
| 61 | perror("Unmount failed"); |
| 62 | return -1; |
| 63 | } |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | |
Dominik Wenger | dde262b | 2007-05-03 20:07:57 +0000 | [diff] [blame] | 68 | #ifndef RBUTIL |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 69 | void print_error(char* msg) |
| 70 | { |
| 71 | perror(msg); |
| 72 | } |
Dominik Wenger | dde262b | 2007-05-03 20:07:57 +0000 | [diff] [blame] | 73 | #endif |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 74 | |
| 75 | int sansa_open(struct sansa_t* sansa, int silent) |
| 76 | { |
| 77 | sansa->dh=open(sansa->diskname,O_RDONLY); |
| 78 | if (sansa->dh < 0) { |
| 79 | if (!silent) perror(sansa->diskname); |
Dominik Riebeling | 850c4f9 | 2008-05-11 16:58:02 +0000 | [diff] [blame] | 80 | if(errno == EACCES) return -2; |
| 81 | else return -1; |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) { |
| 85 | sansa->sector_size=512; |
| 86 | if (!silent) { |
| 87 | fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n" |
| 88 | ,sansa->sector_size); |
| 89 | } |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | int sansa_reopen_rw(struct sansa_t* sansa) |
| 96 | { |
Dave Chapman | ad4b886 | 2007-09-02 19:20:59 +0000 | [diff] [blame] | 97 | #if defined(__APPLE__) && defined(__MACH__) |
| 98 | if (sansa_unmount(sansa) < 0) |
| 99 | return -1; |
| 100 | #endif |
| 101 | |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 102 | close(sansa->dh); |
| 103 | sansa->dh=open(sansa->diskname,O_RDWR); |
| 104 | if (sansa->dh < 0) { |
| 105 | perror(sansa->diskname); |
| 106 | return -1; |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int sansa_close(struct sansa_t* sansa) |
| 112 | { |
| 113 | close(sansa->dh); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize) |
| 118 | { |
| 119 | *sectorbuf=malloc(bufsize); |
| 120 | if (*sectorbuf == NULL) { |
| 121 | return -1; |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int sansa_seek(struct sansa_t* sansa, loff_t pos) |
| 127 | { |
| 128 | off_t res; |
| 129 | |
| 130 | res = lseek64(sansa->dh, pos, SEEK_SET); |
| 131 | |
| 132 | if (res == -1) { |
| 133 | return -1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes) |
| 139 | { |
| 140 | return read(sansa->dh, buf, nbytes); |
| 141 | } |
| 142 | |
| 143 | int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes) |
| 144 | { |
| 145 | return write(sansa->dh, buf, nbytes); |
| 146 | } |