yaffs Added more tests to direct/timothy_tests/quick_tests
[yaffs2.git] / direct / yaffs_nandif.c
index 6216b86..9a22f7f 100644 (file)
-\r
-/*\r
- * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.\r
- *\r
- * Copyright (C) 2002-2007 Aleph One Ltd.\r
- *   for Toby Churchill Ltd and Brightstar Engineering\r
- *\r
- * Created by Charles Manning <charles@aleph1.co.uk>\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License version 2 as\r
- * published by the Free Software Foundation.\r
- */\r
-\r
-\r
-\r
-\r
-#include "yportenv.h"\r
-#include "yaffs_guts.h"\r
-#include "devextras.h"\r
-\r
-\r
-#include "yaffs_nandif.h"\r
-#include "yaffs_packedtags2.h"\r
-\r
-\r
-#if 0\r
-\r
-\r
-static unsigned char *DevBufferIn(yaffs_Device *dev)\r
-{\r
-       yfsd_WinCEDevice *cedev = (yfsd_WinCEDevice *)(dev->genericDevice);\r
-       return cedev->bufferIn;\r
-}\r
-static unsigned char *DevBufferOut(yaffs_Device *dev)\r
-{\r
-       yfsd_WinCEDevice *cedev = (yfsd_WinCEDevice *)(dev->genericDevice);\r
-       return cedev->bufferOut;\r
-}\r
-\r
-static unsigned DevBufferSize(yaffs_Device *dev)\r
-{\r
-       yfsd_WinCEDevice *cedev = (yfsd_WinCEDevice *)(dev->genericDevice);\r
-       return cedev->bufferSize;\r
-}\r
-\r
-#endif\r
-\r
-/* NB For use with inband tags....\r
- * We assume that the data buffer is of size totalBytersPerChunk so that we can also\r
- * use it to load the tags.\r
- */\r
-int ynandif_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,\r
-                                     const __u8 * data,\r
-                                     const yaffs_ExtendedTags * tags)\r
-{\r
-\r
-       int retval = 0;\r
-       yaffs_PackedTags2 pt;\r
-       void *spare;\r
-       unsigned spareSize = 0;\r
-\r
-       unsigned char *bufferIn   = DevBufferIn(dev);\r
-       unsigned char *bufferOut  = DevBufferOut(dev);\r
-       unsigned       bufferSize = DevBufferSize(dev);\r
-\r
-       T(YAFFS_TRACE_MTD,\r
-         (TSTR\r
-          ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"\r
-           TENDSTR), chunkInNAND, data, tags));\r
-           \r
-\r
-       /* For yaffs2 writing there must be both data and tags.\r
-        * If we're using inband tags, then the tags are stuffed into\r
-        * the end of the data buffer.\r
-        */\r
-\r
-       if(dev->inbandTags){\r
-               yaffs_PackedTags2TagsPart *pt2tp;\r
-               pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);\r
-               yaffs_PackTags2TagsPart(pt2tp,tags);\r
-               spare = NULL;\r
-               spareSize = 0;\r
-       }\r
-       else{\r
-               yaffs_PackTags2(&pt, tags);\r
-               spare = &pt;\r
-               spareSize = sizeof(yaffs_PackedTags2);\r
-       }\r
-       \r
-       yramsim_WritePage(chunkInNAND,\r
-                                         data, dev->totalBytesPerChunk, spare, spareSize);\r
-\r
-       return YAFFS_OK;\r
-}\r
-\r
-int ynandif_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,\r
-                                      __u8 * data, yaffs_ExtendedTags * tags)\r
-{\r
-       yaffs_PackedTags2 pt;\r
-       int localData = 0;\r
-       void *spare = NULL;\r
-       unsigned spareSize;\r
-       int eccStatus; //0 = ok, 1 = fixed, -1 = unfixed\r
-\r
-       unsigned char *bufferIn   = DevBufferIn(dev);\r
-       unsigned char *bufferOut  = DevBufferOut(dev);\r
-       unsigned       bufferSize = DevBufferSize(dev);\r
-\r
-       T(YAFFS_TRACE_MTD,\r
-         (TSTR\r
-          ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"\r
-           TENDSTR), chunkInNAND, data, tags));\r
-           \r
-       if(!tags){\r
-               spare = NULL;\r
-               spareSize = 0;\r
-       }else if(dev->inbandTags){\r
-               \r
-               if(!data) {\r
-                       localData = 1;\r
-                       data = yaffs_GetTempBuffer(dev,__LINE__);\r
-               }\r
-               spare = NULL;\r
-               spareSize = 0;\r
-       }\r
-       else {\r
-               spare = &pt;\r
-               spareSize = sizeof(yaffs_PackedTags2);\r
-       }\r
-\r
-       yramsim_ReadPage(chunkInNAND,\r
-                                        data,data ? dev->totalBytesPerChunk : 0,\r
-                                        spare,spareSize,\r
-                                        &eccStatus);\r
-\r
-\r
-\r
-       if(dev->inbandTags){\r
-               if(tags){\r
-                       yaffs_PackedTags2TagsPart * pt2tp;\r
-                       pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];    \r
-                       yaffs_UnpackTags2TagsPart(tags,pt2tp);\r
-               }\r
-       }\r
-       else {\r
-               if (tags){\r
-                       yaffs_UnpackTags2(tags, &pt);\r
-               }\r
-       }\r
-\r
-       if(tags && tags->chunkUsed){\r
-               if(eccStatus == 0)\r
-                       tags->eccResult = YAFFS_ECC_RESULT_NO_ERROR;\r
-               else if(eccStatus < 0)\r
-                       tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;\r
-               else\r
-                       tags->eccResult = YAFFS_ECC_RESULT_FIXED;\r
-       }\r
-\r
-       if(localData)\r
-               yaffs_ReleaseTempBuffer(dev,data,__LINE__);\r
-       \r
-       return YAFFS_OK;\r
-}\r
-\r
-int ynandif_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockId)\r
-{\r
-\r
-       yramsim_MarkBlockBad(blockId);\r
-\r
-       return YAFFS_OK;\r
-}\r
-\r
-int ynandif_EraseBlockInNAND(struct yaffs_DeviceStruct *dev, int blockId)\r
-{\r
-\r
-       yramsim_EraseBlock(blockId);\r
-\r
-       return YAFFS_OK;\r
-}\r
-\r
-\r
-static int ynandif_IsBlockOk(struct yaffs_DeviceStruct *dev, int blockId)\r
-{\r
-       return yramsim_CheckBlockOk(blockId);\r
-}\r
-\r
-int ynandif_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockId, yaffs_BlockState *state, __u32 *sequenceNumber)\r
-{\r
-       unsigned chunkNo;\r
-       yaffs_ExtendedTags tags;\r
-\r
-       *sequenceNumber = 0;\r
-       \r
-       chunkNo = blockId * dev->nChunksPerBlock;\r
-       \r
-       if(!ynandif_IsBlockOk(dev,blockId)){\r
-               *state = YAFFS_BLOCK_STATE_DEAD;\r
-       } \r
-       else \r
-       {\r
-               ynandif_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);\r
-\r
-               if(!tags.chunkUsed)\r
-               {\r
-                       *state = YAFFS_BLOCK_STATE_EMPTY;\r
-               }\r
-               else \r
-               {\r
-                       *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;\r
-                       *sequenceNumber = tags.sequenceNumber;\r
-               }\r
-       }\r
-\r
-       return YAFFS_OK;\r
-}\r
-\r
-\r
-int ynandif_GetGeometry(yaffs_Device *dev, ynandif_Geometry *geometry)\r
-{\r
-\r
-       yramsim_Geometry g;\r
-\r
-       yramsim_GetGeometry(&g);\r
-       geometry->startBlock = g.startBlock;\r
-       geometry->endBlock = g.endBlock;\r
-       geometry->dataSize = g.dataSize;\r
-       geometry->spareSize = g.spareSize;\r
-       geometry->pagesPerBlock = g.pagesPerBlock;\r
-       geometry->hasECC = g.hasECC;\r
-       geometry->inbandTags = g.inbandTags;\r
-       geometry->useYaffs2 = g.useYaffs2;\r
-\r
-       return YAFFS_OK;\r
-\r
-}\r
-\r
-int ynandif_InitialiseNAND(yaffs_Device *dev)\r
-{\r
-\r
-       yramsim_Initialise();\r
-\r
-       return YAFFS_OK;\r
-}\r
+/*
+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
+ *
+ * Copyright (C) 2002-2010 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.
+ */
+
+#include "yportenv.h"
+#include "yaffs_guts.h"
+
+
+#include "yaffs_nandif.h"
+#include "yaffs_packedtags2.h"
+
+#include "yramsim.h"
+
+#include "yaffs_trace.h"
+#include "yaffsfs.h"
+
+
+/* NB For use with inband tags....
+ * We assume that the data buffer is of size totalBytersPerChunk so that we can also
+ * use it to load the tags.
+ */
+int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev * dev, int nand_chunk,
+                                     const u8 * data,
+                                     const struct yaffs_ext_tags * tags)
+{
+
+       int retval = 0;
+       struct yaffs_packed_tags2 pt;
+       void *spare;
+       unsigned spareSize = 0;
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       T(YAFFS_TRACE_MTD,
+         (TSTR
+          ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
+           TENDSTR), nand_chunk, data, tags));
+           
+
+       /* For yaffs2 writing there must be both data and tags.
+        * If we're using inband tags, then the tags are stuffed into
+        * the end of the data buffer.
+        */
+
+       if(dev->param.inband_tags){
+               struct yaffs_packed_tags2_tags_only *pt2tp;
+               pt2tp = (struct yaffs_packed_tags2_tags_only *)(data + dev->data_bytes_per_chunk);
+               yaffs_pack_tags2_tags_only(pt2tp,tags);
+               spare = NULL;
+               spareSize = 0;
+       }
+       else{
+               yaffs_pack_tags2(&pt, tags,!dev->param.no_tags_ecc);
+               spare = &pt;
+               spareSize = sizeof(struct yaffs_packed_tags2);
+       }
+       
+       retval = geometry->writeChunk(dev,nand_chunk,
+                                         data, dev->param.total_bytes_per_chunk, spare, spareSize);
+
+       return retval;
+}
+
+int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev * dev, int nand_chunk,
+                                      u8 * data, struct yaffs_ext_tags * tags)
+{
+       struct yaffs_packed_tags2 pt;
+       int localData = 0;
+       void *spare = NULL;
+       unsigned spareSize;
+       int retval = 0;
+       int eccStatus; //0 = ok, 1 = fixed, -1 = unfixed
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       T(YAFFS_TRACE_MTD,
+         (TSTR
+          ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
+           TENDSTR), nand_chunk, data, tags));
+           
+       if(!tags){
+               spare = NULL;
+               spareSize = 0;
+       }else if(dev->param.inband_tags){
+               
+               if(!data) {
+                       localData = 1;
+                       data = yaffs_get_temp_buffer(dev,__LINE__);
+               }
+               spare = NULL;
+               spareSize = 0;
+       }
+       else {
+               spare = &pt;
+               spareSize = sizeof(struct yaffs_packed_tags2);
+       }
+
+       retval = geometry->readChunk(dev,nand_chunk,
+                                        data,
+                                        data ? dev->param.total_bytes_per_chunk : 0,
+                                        spare,spareSize,
+                                        &eccStatus);
+
+       if(dev->param.inband_tags){
+               if(tags){
+                       struct yaffs_packed_tags2_tags_only * pt2tp;
+                       pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];        
+                       yaffs_unpack_tags2_tags_only(tags,pt2tp);
+               }
+       }
+       else {
+               if (tags){
+                       yaffs_unpack_tags2(tags, &pt,!dev->param.no_tags_ecc);
+               }
+       }
+
+       if(tags && tags->chunk_used){
+               if(eccStatus < 0 || 
+                  tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED)
+                       tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
+               else if(eccStatus > 0 ||
+                            tags->ecc_result == YAFFS_ECC_RESULT_FIXED)
+                       tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
+               else
+                       tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
+       }
+
+       if(localData)
+               yaffs_release_temp_buffer(dev,data,__LINE__);
+       
+       return retval;
+}
+
+int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId)
+{
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       return geometry->markBlockBad(dev,blockId);
+}
+
+int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId)
+{
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       return geometry->eraseBlock(dev,blockId);
+
+}
+
+
+static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId)
+{
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       return geometry->checkBlockOk(dev,blockId);
+}
+
+int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId, enum yaffs_block_state *state, u32 *seq_number)
+{
+       unsigned chunkNo;
+       struct yaffs_ext_tags tags;
+
+       *seq_number = 0;
+       
+       chunkNo = blockId * dev->param.chunks_per_block;
+       
+       if(!ynandif_IsBlockOk(dev,blockId)){
+               *state = YAFFS_BLOCK_STATE_DEAD;
+       } 
+       else 
+       {
+               ynandif_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
+
+               if(!tags.chunk_used)
+               {
+                       *state = YAFFS_BLOCK_STATE_EMPTY;
+               }
+               else 
+               {
+                       *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
+                       *seq_number = tags.seq_number;
+               }
+       }
+
+       return YAFFS_OK;
+}
+
+
+int ynandif_InitialiseNAND(struct yaffs_dev *dev)
+{
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       geometry->initialise(dev);
+
+       return YAFFS_OK;
+}
+
+int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev)
+{
+       ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
+
+       geometry->deinitialise(dev);
+
+       return YAFFS_OK;
+}
+
+
+struct yaffs_dev * 
+       yaffs_add_dev_from_geometry(const YCHAR *name,
+                                       const ynandif_Geometry *geometry)
+{
+       YCHAR *clonedName = YMALLOC(sizeof(YCHAR) * (yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH)+1));
+       struct yaffs_dev *dev = YMALLOC(sizeof(struct yaffs_dev));
+
+       if(dev && clonedName){
+               memset(dev,0,sizeof(struct yaffs_dev));
+               yaffs_strcpy(clonedName,name);
+
+               dev->param.name = clonedName;
+               dev->param.write_chunk_tags_fn  = ynandif_WriteChunkWithTagsToNAND;
+               dev->param.read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND;
+               dev->param.erase_fn          = ynandif_EraseBlockInNAND;
+               dev->param.initialise_flash_fn            = ynandif_InitialiseNAND;
+               dev->param.query_block_fn            = ynandif_QueryNANDBlock;
+               dev->param.bad_block_fn          = ynandif_MarkNANDBlockBad;
+               dev->param.n_caches                        = 20;
+               dev->param.start_block                = geometry->start_block;
+               dev->param.end_block                  = geometry->end_block;
+               dev->param.total_bytes_per_chunk                   = geometry->dataSize;
+               dev->param.spare_bytes_per_chunk                   = geometry->spareSize;
+               dev->param.inband_tags                             = geometry->inband_tags;
+               dev->param.chunks_per_block                = geometry->pagesPerBlock;
+               dev->param.use_nand_ecc                            = geometry->hasECC;
+               dev->param.is_yaffs2                               = geometry->useYaffs2;
+               dev->param.n_reserved_blocks               = 5;
+               dev->driver_context                        = (void *)geometry;
+
+               yaffs_add_device(dev);
+
+               return dev;
+       }
+
+       if(dev)
+               YFREE(dev);
+       if(clonedName)
+               YFREE(clonedName);
+
+       return NULL;
+}