yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / direct / test-framework / yaffs_fileem.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * This provides a YAFFS nand emulation on a file.
15  * This is only intended as test code to test persistence etc.
16  */
17
18 #include "yportenv.h"
19 #include "yaffs_trace.h"
20
21 #include "yaffs_flashif.h"
22 #include "yaffs_guts.h"
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29
30
31 #define SIZE_IN_MB 16
32
33 #define BLOCK_SIZE (32 * 528)
34 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
35
36
37
38 typedef struct
39 {
40         u8 data[528]; // Data + spare
41 } yflash_Page;
42
43 typedef struct
44 {
45         yflash_Page page[32]; // The pages in the block
46
47 } yflash_Block;
48
49
50
51 typedef struct
52 {
53         int handle;
54         int nBlocks;
55 } yflash_Device;
56
57 static yflash_Device filedisk;
58
59 static int  CheckInit(struct yaffs_dev *dev)
60 {
61         static int initialised = 0;
62         int i;
63         int fSize;
64         int written;
65
66         yflash_Page p;
67
68         (void)dev;
69
70         if(initialised)
71         {
72                 return YAFFS_OK;
73         }
74
75         initialised = 1;
76
77
78         filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
79
80         filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
81
82         if(filedisk.handle < 0)
83         {
84                 perror("Failed to open yaffs emulation file");
85                 return YAFFS_FAIL;
86         }
87
88
89         fSize = lseek(filedisk.handle,0,SEEK_END);
90
91         if(fSize < SIZE_IN_MB * 1024 * 1024)
92         {
93                 printf("Creating yaffs emulation file\n");
94
95                 lseek(filedisk.handle,0,SEEK_SET);
96
97                 memset(&p,0xff,sizeof(yflash_Page));
98
99                 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
100                 {
101                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
102
103                         if(written != sizeof(yflash_Page))
104                         {
105                                 printf("Write failed\n");
106                                 return YAFFS_FAIL;
107                         }
108                 }
109         }
110
111         return 1;
112 }
113
114 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
115 {
116         int written;
117
118         CheckInit(dev);
119
120         if(data)
121         {
122                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
123                 written = write(filedisk.handle,data,512);
124
125                 if(written != 512) return YAFFS_FAIL;
126         }
127
128         if(spare)
129         {
130                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
131                 written = write(filedisk.handle,spare,16);
132
133                 if(written != 16) return YAFFS_FAIL;
134         }
135
136
137         return YAFFS_OK;
138
139 }
140
141
142 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
143 {
144         int nread;
145
146         CheckInit(dev);
147
148         if(data)
149         {
150                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
151                 nread = read(filedisk.handle,data,512);
152
153                 if(nread != 512) return YAFFS_FAIL;
154         }
155
156         if(spare)
157         {
158                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
159                 nread= read(filedisk.handle,spare,16);
160
161                 if(nread != 16) return YAFFS_FAIL;
162         }
163
164
165         return YAFFS_OK;
166
167 }
168
169
170 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
171 {
172         int i;
173
174         CheckInit(dev);
175
176         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
177         {
178                 yaffs_trace(YAFFS_TRACE_ALWAYS,
179                         "Attempt to erase non-existant block %d\n",
180                         blockNumber);
181                 return YAFFS_FAIL;
182         }
183         else
184         {
185
186                 yflash_Page pg;
187
188                 memset(&pg,0xff,sizeof(yflash_Page));
189
190                 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
191
192                 for(i = 0; i < 32; i++)
193                 {
194                         write(filedisk.handle,&pg,528);
195                 }
196                 return YAFFS_OK;
197         }
198
199 }
200
201 int yflash_InitialiseNAND(struct yaffs_dev *dev)
202 {
203         (void) dev;
204
205         return YAFFS_OK;
206 }
207
208