summaryrefslogtreecommitdiff
path: root/direct/basic-test/yaffs_fileem.c
blob: 267e1341bc0d3d09805a94adb94b6e5a89d65372 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2011 Aleph One Ltd.
 *   for Toby Churchill Ltd and Brightstar Engineering
 *
 * Created by Charles Manning <charles@aleph1.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * This provides a YAFFS nand emulation on a file.
 * This is only intended as test code to test persistence etc.
 */

const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.7 2010-02-18 01:18:04 charles Exp $";


#include "yportenv.h"
#include "yaffs_trace.h"

#include "yaffs_flashif.h"
#include "yaffs_guts.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> 



#define SIZE_IN_MB 16

#define BLOCK_SIZE (32 * 528)
#define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))



typedef struct 
{
	u8 data[528]; // Data + spare
} yflash_Page;

typedef struct
{
	yflash_Page page[32]; // The pages in the block
	
} yflash_Block;



typedef struct
{
	int handle;
	int nBlocks;
} yflash_Device;

static yflash_Device filedisk;

static int  CheckInit(struct yaffs_dev *dev)
{
	static int initialised = 0;
	
	int i;

	
	int fSize;
	int written;
	
	yflash_Page p;
	
	if(initialised) 
	{
		return YAFFS_OK;
	}

	initialised = 1;
	
	
	filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
	
	filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
	
	if(filedisk.handle < 0)
	{
		perror("Failed to open yaffs emulation file");
		return YAFFS_FAIL;
	}
	
	
	fSize = lseek(filedisk.handle,0,SEEK_END);
	
	if(fSize < SIZE_IN_MB * 1024 * 1024)
	{
		printf("Creating yaffs emulation file\n");
		
		lseek(filedisk.handle,0,SEEK_SET);
		
		memset(&p,0xff,sizeof(yflash_Page));
		
		for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
		{
			written = write(filedisk.handle,&p,sizeof(yflash_Page));
			
			if(written != sizeof(yflash_Page))
			{
				printf("Write failed\n");
				return YAFFS_FAIL;
			}
		}		
	}
	
	return 1;
}

int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
{
	int written;

	CheckInit(dev);
	
	
	
	if(data)
	{
		lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
		written = write(filedisk.handle,data,512);
		
		if(written != 512) return YAFFS_FAIL;
	}
	
	if(spare)
	{
		lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
		written = write(filedisk.handle,spare,16);
		
		if(written != 16) return YAFFS_FAIL;
	}
	

	return YAFFS_OK;	

}


int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
{
	int nread;

	CheckInit(dev);
	
	
	
	if(data)
	{
		lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
		nread = read(filedisk.handle,data,512);
		
		if(nread != 512) return YAFFS_FAIL;
	}
	
	if(spare)
	{
		lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
		nread= read(filedisk.handle,spare,16);
		
		if(nread != 16) return YAFFS_FAIL;
	}
	

	return YAFFS_OK;	

}


int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
{

	int i;
		
	CheckInit(dev);
	
	if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
	{
		yaffs_trace(YAFFS_TRACE_ALWAYS,
			"Attempt to erase non-existant block %d\n",
			blockNumber);
		return YAFFS_FAIL;
	}
	else
	{
	
		yflash_Page pg;
		
		memset(&pg,0xff,sizeof(yflash_Page));
		
		lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
		
		for(i = 0; i < 32; i++)
		{
			write(filedisk.handle,&pg,528);
		}
		return YAFFS_OK;
	}
	
}

int yflash_InitialiseNAND(struct yaffs_dev *dev)
{
	
	return YAFFS_OK;
}