summaryrefslogtreecommitdiff
path: root/firmware/test/fat/ata-sim.c
blob: aac7fba0f4ff923b190d662d2bd964b73408fb1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "panic.h"

#define BLOCK_SIZE 512

static FILE* file;

int ata_read_sectors(unsigned long start, int count, void* buf)
{
    if ( count > 1 )
        DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n",
               count, start, start+count-1); 
    else
        DEBUGF("[Reading block 0x%lx]\n", start); 

    if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
        perror("fseek");
        return -1;
    }
    if(!fread(buf,BLOCK_SIZE,count,file)) {
        DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
        perror("fread");
        panicf("Disk error\n");
    }
    return 0;
}

int ata_write_sectors(unsigned long start, int count, void* buf)
{
    if ( count > 1 )
        DEBUGF("[Writing %d blocks: 0x%lx to 0x%lx]\n",
               count, start, start+count-1); 
    else
        DEBUGF("[Writing block 0x%lx]\n", start); 

    if (start == 0)
        panicf("Writing on sector 0!\n");

    if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
        perror("fseek");
        return -1;
    }
    if(!fwrite(buf,BLOCK_SIZE,count,file)) {
        DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
        perror("fwrite");
        panicf("Disk error\n");
    }
    return 0;
}

int ata_init(char* filename)
{
    if (!filename)
        filename = "disk.img";
    /* check disk size */
    file=fopen(filename,"r+");
    if(!file) {
        fprintf(stderr, "read_disk() - Could not find \"%s\"\n",filename);
        return -1;
    }
    return 0;
}

void ata_exit(void)
{
    fclose(file);
}