2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 * yaffs_ramdisk.c: yaffs ram disk component
15 * This provides a ram disk under yaffs.
16 * NB this is not intended for NAND emulation.
17 * Use this with dev->use_nand_ecc enabled, then ECC overheads are not required.
21 #include "yaffs_trace.h"
23 #include "yaffs_ramdisk.h"
24 #include "yaffs_guts.h"
25 #include "yaffs_packedtags1.h"
31 #define BLOCK_SIZE (32 * 528)
32 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
40 u8 data[528]; // Data + spare
45 yramdisk_page page[32]; // The pages in the block
53 yramdisk_block **block;
57 static yramdisk_device ramdisk;
59 static int CheckInit(struct yaffs_dev *dev)
61 static int initialised = 0;
76 ramdisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
78 ramdisk.block = malloc(sizeof(yramdisk_block *) * ramdisk.nBlocks);
80 if(!ramdisk.block) return 0;
82 for(i=0; i <ramdisk.nBlocks; i++)
84 ramdisk.block[i] = NULL;
87 for(i=0; i <ramdisk.nBlocks && !fail; i++)
89 if((ramdisk.block[i] = malloc(sizeof(yramdisk_block))) == 0)
95 yramdisk_erase(dev,i);
102 for(i = 0; i < nAllocated; i++)
104 kfree(ramdisk.block[i]);
106 kfree(ramdisk.block);
108 yaffs_trace(YAFFS_TRACE_ALWAYS,
109 "Allocation failed, could only allocate %dMB of %dMB requested.\n",
110 nAllocated/64,ramdisk.nBlocks * 528);
118 int yramdisk_wr_chunk(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
132 memcpy(ramdisk.block[blk]->page[pg].data,data,512);
138 struct yaffs_packed_tags1 pt;
140 yaffs_pack_tags1(&pt,tags);
141 memcpy(&ramdisk.block[blk]->page[pg].data[512],&pt,sizeof(pt));
149 int yramdisk_rd_chunk(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
163 memcpy(data,ramdisk.block[blk]->page[pg].data,512);
169 struct yaffs_packed_tags1 pt;
171 memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(pt));
172 yaffs_unpack_tags1(tags,&pt);
180 int yramdisk_check_chunk_erased(struct yaffs_dev *dev,int nand_chunk)
193 for(i = 0; i < 528; i++)
195 if(ramdisk.block[blk]->page[pg].data[i] != 0xFF)
205 int yramdisk_erase(struct yaffs_dev *dev, int blockNumber)
210 if(blockNumber < 0 || blockNumber >= ramdisk.nBlocks)
212 yaffs_trace(YAFFS_TRACE_ALWAYS,
213 "Attempt to erase non-existant block %d\n",
219 memset(ramdisk.block[blockNumber],0xFF,sizeof(yramdisk_block));
225 int yramdisk_initialise(struct yaffs_dev *dev)
229 //dev->use_nand_ecc = 1; // force on use_nand_ecc which gets faked.
230 // This saves us doing ECC checks.