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 = 0x0; /* Disable logging */
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 /* store the mtd device for later use */
125 flashDev->genericDevice = mtd;
127 // Stuff to configure YAFFS
128 // Stuff to initialise anything special (eg lock semaphore).
129 yaffsfs_LocalInitialisation();
136 ramDev.nBytesPerChunk = 512;
137 ramDev.nChunksPerBlock = 32;
138 ramDev.nReservedBlocks = 2; // Set this smaller for RAM
139 ramDev.startBlock = 1; // Can't use block 0
140 ramDev.endBlock = 127; // Last block in 2MB.
141 ramDev.useNANDECC = 1;
142 ramDev.nShortOpCaches = 0; // Disable caching on this device.
143 ramDev.genericDevice = (void *) 0; // Used to identify the device in fstat.
144 ramDev.writeChunkWithTagsToNAND = yramdisk_WriteChunkWithTagsToNAND;
145 ramDev.readChunkWithTagsFromNAND = yramdisk_ReadChunkWithTagsFromNAND;
146 ramDev.eraseBlockInNAND = yramdisk_EraseBlockInNAND;
147 ramDev.initialiseNAND = yramdisk_InitialiseNAND;
150 bootDev.nBytesPerChunk = 612;
151 bootDev.nChunksPerBlock = 32;
152 bootDev.nReservedBlocks = 5;
153 bootDev.startBlock = 1; // Can't use block 0
154 bootDev.endBlock = 127; // Last block in 2MB.
155 bootDev.useNANDECC = 0; // use YAFFS's ECC
156 bootDev.nShortOpCaches = 10; // Use caches
157 bootDev.genericDevice = (void *) 1; // Used to identify the device in fstat.
158 bootDev.writeChunkToNAND = yflash_WriteChunkToNAND;
159 bootDev.readChunkFromNAND = yflash_ReadChunkFromNAND;
160 bootDev.eraseBlockInNAND = yflash_EraseBlockInNAND;
161 bootDev.initialiseNAND = yflash_InitialiseNAND;
165 flashDev->nReservedBlocks = 5;
166 // flashDev->nShortOpCaches = (options.no_cache) ? 0 : 10;
167 flashDev->nShortOpCaches = 10; // Use caches
168 flashDev->useNANDECC = 0; // do not use YAFFS's ECC
170 if (yaffsVersion == 2)
172 flashDev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
173 flashDev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
174 flashDev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
175 flashDev->queryNANDBlock = nandmtd2_QueryNANDBlock;
176 flashDev->spareBuffer = YMALLOC(mtd->oobsize);
177 flashDev->isYaffs2 = 1;
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
179 flashDev->nDataBytesPerChunk = mtd->writesize;
180 flashDev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
182 flashDev->nDataBytesPerChunk = mtd->oobblock;
183 flashDev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
185 nBlocks = mtd->size / mtd->erasesize;
187 flashDev->nCheckpointReservedBlocks = 10;
188 flashDev->startBlock = 0;
189 flashDev->endBlock = nBlocks - 1;
193 flashDev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
194 flashDev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
195 flashDev->isYaffs2 = 0;
196 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
197 flashDev->startBlock = 320;
198 flashDev->endBlock = nBlocks - 1;
199 flashDev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
200 flashDev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
203 /* ... and common functions */
204 flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
205 flashDev->initialiseNAND = nandmtd_InitialiseNAND;
207 yaffs_initialise(yaffsfs_config);
213 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
217 unsigned char buffer[100];
219 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
222 printf("Error opening file: %d\n", outh);
226 memset(buffer,bval,100);
233 yaffs_write(outh,buffer,i);
235 } while (sizeOfFile > 0);
241 void read_a_file(char *fn)
247 h = yaffs_open(fn, O_RDWR,0);
250 printf("File not found\n");
254 while(yaffs_read(h,&b,1)> 0)
268 void cmd_yaffs_mount(char *mp)
271 int retval = yaffs_mount(mp);
275 printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
278 static void checkMount(void)
282 cmd_yaffs_mount(MOUNT_POINT);
286 void cmd_yaffs_umount(char *mp)
289 if( yaffs_unmount(mp) == -1)
290 printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
293 void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
296 make_a_file(yaffsName,bval,sizeOfFile);
300 void cmd_yaffs_read_file(char *fn)
307 void cmd_yaffs_mread_file(char *fn, char *addr)
316 printf ("Copy %s to 0x%08x... ", fn, addr);
317 h = yaffs_open(fn, O_RDWR,0);
320 printf("File not found\n");
324 yaffs_read(h,addr,(int)s.st_size);
325 printf("\t[DONE]\n");
331 void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
336 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
339 printf("Error opening file: %d\n", outh);
342 yaffs_write(outh,addr,size);
348 void cmd_yaffs_ls(const char *mountpt, int longlist)
353 struct yaffs_stat stat;
357 d = yaffs_opendir(mountpt);
361 printf("opendir failed\n");
365 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
369 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
370 yaffs_stat(tempstr, &stat);
371 printf("%-25s\t%7d\n",de->d_name, stat.st_size);
375 printf("%s\n",de->d_name);
382 void cmd_yaffs_mkdir(const char *dir)
386 int retval = yaffs_mkdir(dir, 0);
389 printf("yaffs_mkdir returning error: %d\n", retval);
392 void cmd_yaffs_rmdir(const char *dir)
396 int retval = yaffs_rmdir(dir);
399 printf("yaffs_rmdir returning error: %d\n", retval);
402 void cmd_yaffs_rm(const char *path)
406 int retval = yaffs_unlink(path);
409 printf("yaffs_unlink returning error: %d\n", retval);
412 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
416 int retval = yaffs_rename(newPath, oldPath);
419 printf("yaffs_unlink returning error: %d\n", retval);