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 | |
Dominik Riebeling | 38890ac | 2011-12-04 19:40:35 +0000 | [diff] [blame] | 22 | #if !defined(_WIN32) /* all non-Windows platforms supported are POSIX. */ |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/ioctl.h> |
Dominik Riebeling | 850c4f9 | 2008-05-11 16:58:02 +0000 | [diff] [blame] | 31 | #include <errno.h> |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 32 | |
| 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__) |
| 38 | #include <sys/disk.h> |
| 39 | #define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE |
| 40 | #elif defined(__APPLE__) && defined(__MACH__) |
| 41 | #include <sys/disk.h> |
| 42 | #define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE |
| 43 | #else |
| 44 | #error No sector-size detection implemented for this platform |
| 45 | #endif |
| 46 | |
| 47 | #include "sansaio.h" |
| 48 | |
Dave Chapman | ad4b886 | 2007-09-02 19:20:59 +0000 | [diff] [blame] | 49 | #if defined(__APPLE__) && defined(__MACH__) |
| 50 | static int sansa_unmount(struct sansa_t* sansa) |
| 51 | { |
| 52 | char cmd[4096]; |
| 53 | int res; |
| 54 | |
| 55 | sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname); |
| 56 | fprintf(stderr,"[INFO] "); |
| 57 | res = system(cmd); |
| 58 | |
| 59 | if (res==0) { |
| 60 | return 0; |
| 61 | } else { |
| 62 | perror("Unmount failed"); |
| 63 | return -1; |
| 64 | } |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 69 | void sansa_print_error(char* msg) |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 70 | { |
| 71 | perror(msg); |
| 72 | } |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 73 | |
| 74 | int sansa_open(struct sansa_t* sansa, int silent) |
| 75 | { |
| 76 | sansa->dh=open(sansa->diskname,O_RDONLY); |
| 77 | if (sansa->dh < 0) { |
| 78 | if (!silent) perror(sansa->diskname); |
Dominik Riebeling | 850c4f9 | 2008-05-11 16:58:02 +0000 | [diff] [blame] | 79 | if(errno == EACCES) return -2; |
| 80 | else return -1; |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) { |
| 84 | sansa->sector_size=512; |
| 85 | if (!silent) { |
| 86 | fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n" |
| 87 | ,sansa->sector_size); |
Bertrik Sikken | b7219b7 | 2008-08-31 09:44:45 +0000 | [diff] [blame] | 88 | } |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | int sansa_reopen_rw(struct sansa_t* sansa) |
| 95 | { |
Dave Chapman | ad4b886 | 2007-09-02 19:20:59 +0000 | [diff] [blame] | 96 | #if defined(__APPLE__) && defined(__MACH__) |
| 97 | if (sansa_unmount(sansa) < 0) |
| 98 | return -1; |
| 99 | #endif |
| 100 | |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 101 | close(sansa->dh); |
| 102 | sansa->dh=open(sansa->diskname,O_RDWR); |
| 103 | if (sansa->dh < 0) { |
| 104 | perror(sansa->diskname); |
| 105 | return -1; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int sansa_close(struct sansa_t* sansa) |
| 111 | { |
| 112 | close(sansa->dh); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Dominik Riebeling | 9c1ed84 | 2012-12-23 23:36:00 +0100 | [diff] [blame] | 116 | int sansa_alloc_buffer(struct sansa_t *sansa, int bufsize) |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 117 | { |
Dominik Riebeling | 9c1ed84 | 2012-12-23 23:36:00 +0100 | [diff] [blame] | 118 | sansa->sectorbuf=malloc(bufsize); |
| 119 | if (sansa->sectorbuf == NULL) { |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
Dominik Riebeling | 163ab46 | 2013-01-01 13:23:47 +0100 | [diff] [blame] | 125 | int sansa_dealloc_buffer(struct sansa_t* sansa) |
| 126 | { |
| 127 | if (sansa->sectorbuf == NULL) { |
| 128 | return -1; |
| 129 | } |
| 130 | free(sansa->sectorbuf); |
| 131 | sansa->sectorbuf = NULL; |
| 132 | return 0; |
| 133 | } |
| 134 | |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 135 | int sansa_seek(struct sansa_t* sansa, loff_t pos) |
| 136 | { |
| 137 | off_t res; |
| 138 | |
| 139 | res = lseek64(sansa->dh, pos, SEEK_SET); |
| 140 | |
| 141 | if (res == -1) { |
| 142 | return -1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes) |
| 148 | { |
| 149 | return read(sansa->dh, buf, nbytes); |
| 150 | } |
| 151 | |
Dominik Riebeling | 9c1ed84 | 2012-12-23 23:36:00 +0100 | [diff] [blame] | 152 | int sansa_write(struct sansa_t* sansa, int nbytes) |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 153 | { |
Dominik Riebeling | 9c1ed84 | 2012-12-23 23:36:00 +0100 | [diff] [blame] | 154 | return write(sansa->dh, sansa->sectorbuf, nbytes); |
Dave Chapman | e17043e | 2007-03-15 22:55:36 +0000 | [diff] [blame] | 155 | } |
Dominik Riebeling | 38890ac | 2011-12-04 19:40:35 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |