Andrew Ryabinin | 097352a | 2012-10-10 12:17:48 +0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id:$ |
| 9 | * |
| 10 | * Copyright (C) 2012 by Andrew Ryabinin |
| 11 | * |
| 12 | * major portion of code is taken from firmware/test/fat/ata-sim.c |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version 2 |
| 17 | * of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 | * KIND, either express or implied. |
| 21 | * |
| 22 | ****************************************************************************/ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdarg.h> |
| 28 | #include "debug.h" |
| 29 | #include "dir.h" |
| 30 | |
| 31 | #define BLOCK_SIZE 512 |
| 32 | |
| 33 | static FILE* file; |
| 34 | |
| 35 | extern char *img_filename; |
| 36 | |
| 37 | void mutex_init(struct mutex* l) {} |
| 38 | void mutex_lock(struct mutex* l) {} |
| 39 | void mutex_unlock(struct mutex* l) {} |
| 40 | |
| 41 | void panicf( char *fmt, ...) { |
| 42 | va_list ap; |
| 43 | va_start( ap, fmt ); |
| 44 | fprintf(stderr,"***PANIC*** "); |
| 45 | vfprintf(stderr, fmt, ap ); |
| 46 | va_end( ap ); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | void debugf(const char *fmt, ...) { |
| 51 | va_list ap; |
| 52 | va_start( ap, fmt ); |
| 53 | fprintf(stderr,"DEBUGF: "); |
| 54 | vfprintf( stderr, fmt, ap ); |
| 55 | va_end( ap ); |
| 56 | } |
| 57 | |
| 58 | void ldebugf(const char* file, int line, const char *fmt, ...) { |
| 59 | va_list ap; |
| 60 | va_start( ap, fmt ); |
| 61 | fprintf( stderr, "%s:%d ", file, line ); |
| 62 | vfprintf( stderr, fmt, ap ); |
| 63 | va_end( ap ); |
| 64 | } |
| 65 | |
| 66 | int storage_read_sectors(unsigned long start, int count, void* buf) |
| 67 | { |
| 68 | if ( count > 1 ) |
| 69 | DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n", |
| 70 | count, start, start+count-1); |
| 71 | else |
| 72 | DEBUGF("[Reading block 0x%lx]\n", start); |
| 73 | |
| 74 | if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) { |
| 75 | perror("fseek"); |
| 76 | return -1; |
| 77 | } |
| 78 | if(!fread(buf,BLOCK_SIZE,count,file)) { |
| 79 | DEBUGF("ata_write_sectors(0x%lx, 0x%x, %p)\n", start, count, buf ); |
| 80 | perror("fread"); |
| 81 | panicf("Disk error\n"); |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int storage_write_sectors(unsigned long start, int count, void* buf) |
| 87 | { |
| 88 | if ( count > 1 ) |
| 89 | DEBUGF("[Writing %d blocks: 0x%lx to 0x%lx]\n", |
| 90 | count, start, start+count-1); |
| 91 | else |
| 92 | DEBUGF("[Writing block 0x%lx]\n", start); |
| 93 | |
| 94 | if (start == 0) |
| 95 | panicf("Writing on sector 0!\n"); |
| 96 | |
| 97 | if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) { |
| 98 | perror("fseek"); |
| 99 | return -1; |
| 100 | } |
| 101 | if(!fwrite(buf,BLOCK_SIZE,count,file)) { |
| 102 | DEBUGF("ata_write_sectors(0x%lx, 0x%x, %p)\n", start, count, buf ); |
| 103 | perror("fwrite"); |
| 104 | panicf("Disk error\n"); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int ata_init(void) |
| 110 | { |
| 111 | file=fopen(img_filename,"rb+"); |
| 112 | if(!file) { |
| 113 | fprintf(stderr, "read_disk() - Could not find \"%s\"\n",img_filename); |
| 114 | return -1; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | void ata_exit(void) |
| 120 | { |
| 121 | fclose(file); |
| 122 | } |