summaryrefslogtreecommitdiff
path: root/firmware/test/fat/ata-sim.c
blob: d8c28d94671abe622db4eda35ec1e44919b23526 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ata.h"

#define BLOCK_SIZE 512

static FILE* file;

int ata_read_sectors(unsigned long start, unsigned char count, void* buf)
{
    if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
        perror("fseek");
        return -1;
    }
    if(!fread(buf,BLOCK_SIZE,count,file)) {
        printf("Failed reading %d blocks starting at block %ld\n",count,start); 
        perror("fread");
        return -1;
    }
    return 0;
}

int ata_write_sectors(unsigned long start, unsigned char count, void* buf)
{
    if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
        perror("fseek");
        return -1;
    }
    if(!fwrite(buf,BLOCK_SIZE,count,file)) {
        perror("fwrite");
        return -1;
    }
    return 0;
}

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

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