removed - more testing
[yaffs/.git] / yaffs_fileem.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_fileem.c  NAND emulation on top of files
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15  //yaffs_fileem.c
16
17 #include "yaffs_nandif.h"
18 #include "yaffs_guts.h"
19 #include "yaffsinterface.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28
29 #define FILE_SIZE_IN_MEG 2
30
31 #define BLOCK_SIZE (32 * 528)
32 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
33 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
34 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
35
36
37 static int h;
38 static __u8 ffChunk[528];
39
40
41
42 static void  CheckInit(void)
43 {
44         static int initialised = 0;
45
46         int length;
47
48         
49         if(!initialised)
50         {
51                 memset(ffChunk,0xFF,528);
52                 
53                 h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
54                 if(h < 0)
55                 {
56                         perror("Fatal error opening yaffs emulation file");
57                         exit(1);
58                 }
59                 initialised = 1;
60                 
61                 length = lseek(h,0,SEEK_END);
62                 if(length !=  FILE_SIZE_IN_BYTES)
63                 {
64                         // Create file contents
65                         int i;
66                         
67                         printf("Creating emulation file...\n");
68                         for(i = 0; i < FILE_SIZE_IN_BLOCKS; i++)
69                         {
70                                 yaffs_EraseBlockInNAND(i);
71                         }
72                 }
73         }
74 }
75
76 int yaffs_WriteChunkToNAND(int chunkInNAND,__u8 *data, yaffs_Spare *spare)
77 {
78         int pos;
79         
80         pos = chunkInNAND * 528;
81         
82         CheckInit();
83         
84         
85         if(data)
86         {
87                 lseek(h,pos,SEEK_SET);
88                 write(h,data,512);
89         }
90         
91         pos += 512;
92         
93         if(spare)
94         {
95                 lseek(h,pos,SEEK_SET);
96                 write(h,spare,16);      
97         }
98
99         return YAFFS_OK;
100 }
101
102
103 int yaffs_ReadChunkFromNAND(int chunkInNAND, __u8 *data, yaffs_Spare *spare)
104 {
105         int pos;
106
107         pos = chunkInNAND * 528;
108         
109         
110         CheckInit();
111         
112         if(data)
113         {
114                 lseek(h,pos,SEEK_SET);
115                 read(h,data,512);
116         }
117         
118         pos += 512;
119         
120         if(spare)
121         {
122                 lseek(h,pos,SEEK_SET);
123                 read(h,spare,16);       
124         }
125
126         return YAFFS_OK;
127 }
128
129
130 int yaffs_EraseBlockInNAND(int blockInNAND)
131 {
132         int i;
133         
134         CheckInit();
135         
136         printf("Erasing block %d\n",blockInNAND);
137         
138         lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET);
139         for(i = 0; i < 32; i++)
140         {
141                 write(h,ffChunk,528);
142         }
143         return YAFFS_OK;
144 }
145