yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / direct / test-framework / yaffs_nor_drv.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 is an interface module for handling NOR in yaffs1 mode.
15  */
16
17 /* This code is intended to be used with "regular" NOR that is bit-modifyable.
18  *
19  * Each "chunk" is a contguous area of 512 + 16 bytes.
20  * This makes 248 such chunks with some space left over where a format markerr
21  * is stored.
22  */
23
24 #include "yaffs_nor_drv.h"
25
26 #include "yportenv.h"
27 #include "yaffs_trace.h"
28
29 #include "yaffs_flashif.h"
30 #include "yaffs_guts.h"
31
32 /* Tunable data */
33 #define DATA_BYTES_PER_CHUNK    512
34 #define SPARE_BYTES_PER_CHUNK   16
35 #define BLOCK_SIZE_IN_BYTES     (128*1024)
36 #define BYTES_IN_DEVICE         (4*1024*1024)
37
38 #define BYTES_PER_CHUNK         (DATA_BYTES_PER_CHUNK + SPARE_BYTES_PER_CHUNK)
39 #define SPARE_AREA_OFFSET       DATA_BYTES_PER_CHUNK
40 #define CHUNKS_PER_BLOCK (BLOCK_SIZE_IN_BYTES/BYTES_PER_CHUNK)
41
42 #define BLOCKS_IN_DEVICE        (BYTES_IN_DEVICE/BLOCK_SIZE_IN_BYTES)
43
44 #define YNOR_PREMARKER          (0xF6)
45 #define YNOR_POSTMARKER         (0xF0)
46
47 #define FORMAT_OFFSET           (CHUNKS_PER_BLOCK * BYTES_PER_CHUNK)
48 #define FORMAT_VALUE            0x1234
49
50 #if 1
51
52 /* Compile this for a simulation */
53 #include "ynorsim.h"
54
55 static struct nor_sim *nor_sim;
56
57 #define nor_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-nor", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
58 #define nor_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
59 #define nor_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
60 #define nor_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
61 #define nor_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
62 #define DEVICE_BASE     ynorsim_get_base(nor_sim)
63
64 #else
65
66 /* Compile this to hook up to read hardware */
67 #include "../blob/yflashrw.h"
68 #define nor_drv_FlashInit()  do{} while(0)
69 #define nor_drv_FlashDeinit() do {} while(0)
70 #define nor_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
71 #define nor_drv_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords)
72 #define nor_drv_FlashEraseBlock(addr)         Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
73 #define DEVICE_BASE     (32 * 1024 * 1024)
74 #endif
75
76
77 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
78 {
79         u8 *addr;
80
81         dev=dev;
82
83         addr = (u8*)DEVICE_BASE;
84         addr += blockNumber * BLOCK_SIZE_IN_BYTES;
85
86         return (u32 *) addr;
87 }
88
89 static u32 *Block2FormatAddr(struct yaffs_dev *dev, int blockNumber)
90 {
91         u8 *addr;
92
93         addr = (u8*) Block2Addr(dev,blockNumber);
94         addr += FORMAT_OFFSET;
95
96         return (u32 *)addr;
97 }
98
99 static u32 *Chunk2DataAddr(struct yaffs_dev *dev, int chunk_id)
100 {
101         unsigned block;
102         unsigned chunkInBlock;
103         u8 *addr;
104
105         block = chunk_id/dev->param.chunks_per_block;
106         chunkInBlock = chunk_id % dev->param.chunks_per_block;
107
108         addr = (u8*) Block2Addr(dev,block);
109         addr += chunkInBlock * BYTES_PER_CHUNK;
110
111         return (u32 *)addr;
112 }
113
114 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev, int chunk_id)
115 {
116         u8 *addr;
117
118         addr = (u8*) Chunk2DataAddr(dev, chunk_id);
119         addr += SPARE_AREA_OFFSET;
120         return (u32 *)addr;
121 }
122
123
124 static void nor_drv_AndBytes(u8*target, const u8 *src, int nbytes)
125 {
126         while(nbytes > 0){
127                 *target &= *src;
128                 target++;
129                 src++;
130                 nbytes--;
131         }
132 }
133
134 static int nor_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
135                                     const u8 *data, int data_len,
136                                     const u8 *oob, int oob_len)
137 {
138         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
139         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
140
141         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
142         struct yaffs_spare tmpSpare;
143
144         (void) oob_len;
145
146         /* We should only be getting called for one of 3 reasons:
147          * Writing a chunk: data and spare will not be NULL
148          * Writing a deletion marker: data will be NULL, spare not NULL
149          * Writing a bad block marker: data will be NULL, spare not NULL
150          */
151
152         if(sizeof(struct yaffs_spare) != SPARE_BYTES_PER_CHUNK)
153                 BUG();
154
155         if(data && oob) {
156                 if(spare->page_status != 0xff)
157                         BUG();
158                 /* Write a pre-marker */
159                 memset(&tmpSpare,0xff,sizeof(tmpSpare));
160                 tmpSpare.page_status = YNOR_PREMARKER;
161                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
162
163                 /* Write the data */
164                 nor_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ sizeof(u32));
165
166                 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
167
168                 /* Write the real tags, but override the premarker*/
169                 tmpSpare.page_status = YNOR_PREMARKER;
170                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
171
172                 /* Write a post-marker */
173                 tmpSpare.page_status = YNOR_POSTMARKER;
174                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/sizeof(u32));
175
176         } else if(spare){
177                 /* This has to be a read-modify-write operation to handle NOR-ness */
178
179                 nor_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
180
181                 nor_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
182
183                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
184         } else {
185                 BUG();
186         }
187
188         return YAFFS_OK;
189 }
190
191 static int nor_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
192                                         u8 *data, int data_len,
193                                         u8 *oob, int oob_len,
194                                         enum yaffs_ecc_result *ecc_result)
195 {
196         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
197
198         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
199         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
200
201         (void) data_len;
202
203         if (data) {
204                 nor_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / sizeof(u32));
205         }
206
207         if (oob) {
208                 nor_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ sizeof(u32));
209
210                 /* If the page status is YNOR_POSTMARKER then it was written properly
211                  * so change that to 0xFF so that the rest of yaffs is happy.
212                  */
213                 if(spare->page_status == YNOR_POSTMARKER)
214                         spare->page_status = 0xff;
215                 else if(spare->page_status != 0xff &&
216                         (spare->page_status | YNOR_PREMARKER) != 0xff)
217                         spare->page_status = YNOR_PREMARKER;
218         }
219
220         if(ecc_result)
221                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
222
223         return YAFFS_OK;
224
225 }
226
227
228 static int nor_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
229 {
230         u32 *blockAddr = Block2Addr(dev,blockNumber);
231         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
232         u32 formatValue = FORMAT_VALUE;
233
234         nor_drv_FlashEraseBlock(blockAddr);
235         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
236
237         return YAFFS_OK;
238 }
239
240 static int nor_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
241 {
242         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
243         u32 formatValue = 0;
244
245         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
246
247         return YAFFS_OK;
248 }
249
250 static int nor_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
251 {
252         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
253         u32 formatValue;
254
255
256         nor_drv_FlashRead32(formatAddr,&formatValue,1);
257
258         return (formatValue == FORMAT_VALUE);
259 }
260
261 static int nor_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
262 {
263
264         if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
265         {
266                 yaffs_trace(YAFFS_TRACE_ALWAYS,
267                         "Attempt to erase non-existant block %d\n",
268                         blockNumber);
269                 return YAFFS_FAIL;
270         }
271         else
272         {
273                 nor_drv_UnformatBlock(dev,blockNumber);
274                 nor_drv_FormatBlock(dev,blockNumber);
275                 return YAFFS_OK;
276         }
277
278 }
279
280 static int nor_drv_InitialiseNAND(struct yaffs_dev *dev)
281 {
282         u32 i;
283
284         nor_drv_FlashInit();
285         /* Go through the blocks formatting them if they are not formatted */
286         for(i = dev->param.start_block; i <= dev->param.end_block; i++){
287                 if(!nor_drv_IsBlockFormatted(dev,i)){
288                         nor_drv_FormatBlock(dev,i);
289                 }
290         }
291         return YAFFS_OK;
292 }
293
294 static int nor_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
295 {
296         dev=dev;
297
298         nor_drv_FlashDeinit();
299
300         return YAFFS_OK;
301 }
302
303
304 struct yaffs_dev *yaffs_nor_install_drv(const char *name)
305 {
306
307         struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
308         char *name_copy = strdup(name);
309         struct yaffs_param *param;
310         struct yaffs_driver *drv;
311
312
313         if(!dev || !name_copy) {
314                 free(name_copy);
315                 free(dev);
316                 return NULL;
317         }
318
319         param = &dev->param;
320         drv = &dev->drv;
321
322         memset(dev, 0, sizeof(*dev));
323
324         param->name = name_copy;
325
326         param->total_bytes_per_chunk = DATA_BYTES_PER_CHUNK;
327         param->chunks_per_block = CHUNKS_PER_BLOCK;
328         param->n_reserved_blocks = 2;
329         param->start_block = 0; // Can use block 0
330         param->end_block = BLOCKS_IN_DEVICE - 1; // Last block
331         param->use_nand_ecc = 0; // use YAFFS's ECC
332         param->disable_soft_del = 1;
333
334         drv->drv_write_chunk_fn = nor_drv_WriteChunkToNAND;
335         drv->drv_read_chunk_fn = nor_drv_ReadChunkFromNAND;
336         drv->drv_erase_fn = nor_drv_EraseBlockInNAND;
337         drv->drv_initialise_fn = nor_drv_InitialiseNAND;
338         drv->drv_deinitialise_fn = nor_drv_Deinitialise_flash_fn;
339
340         param->n_caches = 10;
341         param->disable_soft_del = 1;
342
343         dev->driver_context = (void *) nor_sim;
344
345         yaffs_add_device(dev);
346
347         return NULL;
348 }