fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / test / fat / ata-sim.c
blobd8ed07975931796ac8cd038a4f438dc42cde8ef4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "debug.h"
6 #define BLOCK_SIZE 512
8 static FILE* file;
10 void panicf( const char *fmt, ... );
12 int ata_read_sectors(unsigned long start, int count, void* buf)
14 if ( count > 1 )
15 DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n",
16 count, start, start+count-1);
17 else
18 DEBUGF("[Reading block 0x%lx]\n", start);
20 if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
21 perror("fseek");
22 return -1;
24 if(!fread(buf,BLOCK_SIZE,count,file)) {
25 DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
26 perror("fread");
27 panicf("Disk error\n");
29 return 0;
32 int ata_write_sectors(unsigned long start, int count, void* buf)
34 if ( count > 1 )
35 DEBUGF("[Writing %d blocks: 0x%lx to 0x%lx]\n",
36 count, start, start+count-1);
37 else
38 DEBUGF("[Writing block 0x%lx]\n", start);
40 if (start == 0)
41 panicf("Writing on sector 0!\n");
43 if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
44 perror("fseek");
45 return -1;
47 if(!fwrite(buf,BLOCK_SIZE,count,file)) {
48 DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
49 perror("fwrite");
50 panicf("Disk error\n");
52 return 0;
55 int ata_init(void)
57 char* filename = "disk.img";
58 /* check disk size */
59 file=fopen(filename,"rb+");
60 if(!file) {
61 fprintf(stderr, "read_disk() - Could not find \"%s\"\n",filename);
62 return -1;
64 return 0;
67 void ata_exit(void)
69 fclose(file);