2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * yaffscfg.c The configuration for the "direct" use of yaffs.
17 * This file is intended to be modified to your requirements.
18 * There is no need to redistribute this file.
28 #include "yaffs_packedtags2.h"
29 #include "yaffs_mtdif.h"
30 #include "yaffs_mtdif2.h"
37 unsigned yaffs_traceMask = 0xFFFFFFFF;
38 static int yaffs_errno = 0;
40 void yaffsfs_SetError(int err)
42 //Do whatever to set error
46 int yaffsfs_GetError(void)
51 void yaffsfs_Lock(void)
55 void yaffsfs_Unlock(void)
59 __u32 yaffsfs_CurrentTime(void)
64 void *yaffs_malloc(size_t size)
69 void yaffs_free(void *ptr)
74 void yaffsfs_LocalInitialisation(void)
76 // Define locking semaphore.
81 // /boot 2MB boot disk (flash)
82 // /flash 14MB flash disk (flash)
83 // NB Though /boot and /flash occupy the same physical device they
84 // are still disticnt "yaffs_Devices. You may think of these as "partitions"
85 // using non-overlapping areas in the same device.
88 #include "yaffs_ramdisk.h"
89 #include "yaffs_flashif.h"
91 static int isMounted = 0;
92 #define MOUNT_POINT "/flash"
93 extern nand_info_t nand_info[];
97 static yaffs_Device ramDev;
98 static yaffs_Device bootDev;
99 static yaffs_Device flashDev;
102 static yaffsfs_DeviceConfiguration yaffsfs_config[] = {
106 { "/boot", &bootDev},
107 { "/flash", &flashDev},
111 {(void *)0,(void *)0}
115 int yaffs_StartUp(void)
117 struct mtd_info *mtd = &nand_info[0];
118 int yaffsVersion = 2;
121 yaffs_Device *flashDev = calloc(1, sizeof(yaffs_Device));
122 yaffsfs_config[0].dev = flashDev;
124 // Stuff to configure YAFFS
125 // Stuff to initialise anything special (eg lock semaphore).
126 yaffsfs_LocalInitialisation();
133 ramDev.nBytesPerChunk = 512;
134 ramDev.nChunksPerBlock = 32;
135 ramDev.nReservedBlocks = 2; // Set this smaller for RAM
136 ramDev.startBlock = 1; // Can't use block 0
137 ramDev.endBlock = 127; // Last block in 2MB.
138 ramDev.useNANDECC = 1;
139 ramDev.nShortOpCaches = 0; // Disable caching on this device.
140 ramDev.genericDevice = (void *) 0; // Used to identify the device in fstat.
141 ramDev.writeChunkWithTagsToNAND = yramdisk_WriteChunkWithTagsToNAND;
142 ramDev.readChunkWithTagsFromNAND = yramdisk_ReadChunkWithTagsFromNAND;
143 ramDev.eraseBlockInNAND = yramdisk_EraseBlockInNAND;
144 ramDev.initialiseNAND = yramdisk_InitialiseNAND;
147 bootDev.nBytesPerChunk = 612;
148 bootDev.nChunksPerBlock = 32;
149 bootDev.nReservedBlocks = 5;
150 bootDev.startBlock = 1; // Can't use block 0
151 bootDev.endBlock = 127; // Last block in 2MB.
152 bootDev.useNANDECC = 0; // use YAFFS's ECC
153 bootDev.nShortOpCaches = 10; // Use caches
154 bootDev.genericDevice = (void *) 1; // Used to identify the device in fstat.
155 bootDev.writeChunkToNAND = yflash_WriteChunkToNAND;
156 bootDev.readChunkFromNAND = yflash_ReadChunkFromNAND;
157 bootDev.eraseBlockInNAND = yflash_EraseBlockInNAND;
158 bootDev.initialiseNAND = yflash_InitialiseNAND;
162 flashDev->nReservedBlocks = 5;
163 // flashDev->nShortOpCaches = (options.no_cache) ? 0 : 10;
164 flashDev->nShortOpCaches = 10; // Use caches
165 flashDev->useNANDECC = 0; // do not use YAFFS's ECC
167 if (yaffsVersion == 2)
169 flashDev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
170 flashDev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
171 flashDev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
172 flashDev->queryNANDBlock = nandmtd2_QueryNANDBlock;
173 flashDev->spareBuffer = YMALLOC(mtd->oobsize);
174 flashDev->isYaffs2 = 1;
175 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
176 flashDev->nDataBytesPerChunk = mtd->writesize;
177 flashDev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
179 flashDev->nDataBytesPerChunk = mtd->oobblock;
180 flashDev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
182 nBlocks = mtd->size / mtd->erasesize;
184 flashDev->nCheckpointReservedBlocks = 10;
185 flashDev->startBlock = 0;
186 flashDev->endBlock = nBlocks - 1;
190 flashDev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
191 flashDev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
192 flashDev->isYaffs2 = 0;
193 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
194 flashDev->startBlock = 320;
195 flashDev->endBlock = nBlocks - 1;
196 flashDev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
197 flashDev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
200 /* ... and common functions */
201 flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
202 flashDev->initialiseNAND = nandmtd_InitialiseNAND;
204 yaffs_initialise(yaffsfs_config);
210 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
214 unsigned char buffer[100];
216 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
219 printf("Error opening file: %d\n", outh);
223 memset(buffer,bval,100);
230 yaffs_write(outh,buffer,i);
232 } while (sizeOfFile > 0);
238 void read_a_file(char *fn)
244 h = yaffs_open(fn, O_RDWR,0);
247 printf("File not found\n");
251 while(yaffs_read(h,&b,1)> 0)
265 void cmd_yaffs_mount(char *mp)
268 int retval = yaffs_mount(mp);
272 printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
275 static void checkMount(void)
279 cmd_yaffs_mount(MOUNT_POINT);
283 void cmd_yaffs_umount(char *mp)
286 if( yaffs_unmount(mp) == -1)
287 printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
290 void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
293 make_a_file(yaffsName,bval,sizeOfFile);
297 void cmd_yaffs_read_file(char *fn)
304 void cmd_yaffs_mread_file(char *fn, char *addr)
313 printf ("Copy %s to 0x%08x... ", fn, addr);
314 h = yaffs_open(fn, O_RDWR,0);
317 printf("File not found\n");
321 yaffs_read(h,addr,(int)s.st_size);
322 printf("\t[DONE]\n");
328 void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
333 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
336 printf("Error opening file: %d\n", outh);
339 yaffs_write(outh,addr,size);
345 void cmd_yaffs_ls(const char *mountpt, int longlist)
350 struct yaffs_stat stat;
354 d = yaffs_opendir(mountpt);
358 printf("opendir failed\n");
362 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
366 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
367 yaffs_stat(tempstr, &stat);
368 printf("%-25s\t%7d\n",de->d_name, stat.st_size);
372 printf("%s\n",de->d_name);
379 void cmd_yaffs_mkdir(const char *dir)
383 int retval = yaffs_mkdir(dir, 0);
386 printf("yaffs_mkdir returning error: %d\n", retval);
389 void cmd_yaffs_rmdir(const char *dir)
393 int retval = yaffs_rmdir(dir);
396 printf("yaffs_rmdir returning error: %d\n", retval);
399 void cmd_yaffs_rm(const char *path)
403 int retval = yaffs_unlink(path);
406 printf("yaffs_unlink returning error: %d\n", retval);
409 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
413 int retval = yaffs_rename(newPath, oldPath);
416 printf("yaffs_unlink returning error: %d\n", retval);