blob: 3beb34d14592d2c0cfa532520405626b240dc81b [file] [log] [blame]
William Juulc051bbe2007-11-15 11:13:05 +01001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
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.
12 */
13
14/*
15 * yaffscfg.c The configuration for the "direct" use of yaffs.
16 *
17 * This file is intended to be modified to your requirements.
18 * There is no need to redistribute this file.
19 */
20
William Juule24ebad2007-11-15 12:23:57 +010021/* XXX U-BOOT XXX */
22#include <common.h>
23
24#include <config.h>
25#include "nand.h"
William Juulc051bbe2007-11-15 11:13:05 +010026#include "yaffscfg.h"
27#include "yaffsfs.h"
William Juule24ebad2007-11-15 12:23:57 +010028#include "yaffs_packedtags2.h"
29#include "yaffs_mtdif.h"
30#include "yaffs_mtdif2.h"
31#if 0
William Juulc051bbe2007-11-15 11:13:05 +010032#include <errno.h>
William Juule24ebad2007-11-15 12:23:57 +010033#else
34#include "malloc.h"
35#endif
William Juulc051bbe2007-11-15 11:13:05 +010036
37unsigned yaffs_traceMask = 0xFFFFFFFF;
William Juule24ebad2007-11-15 12:23:57 +010038static int yaffs_errno = 0;
William Juulc051bbe2007-11-15 11:13:05 +010039
40void yaffsfs_SetError(int err)
41{
42 //Do whatever to set error
William Juule24ebad2007-11-15 12:23:57 +010043 yaffs_errno = err;
44}
45
46int yaffsfs_GetError(void)
47{
48 return yaffs_errno;
William Juulc051bbe2007-11-15 11:13:05 +010049}
50
51void yaffsfs_Lock(void)
52{
53}
54
55void yaffsfs_Unlock(void)
56{
57}
58
59__u32 yaffsfs_CurrentTime(void)
60{
61 return 0;
62}
63
64void *yaffs_malloc(size_t size)
65{
66 return malloc(size);
67}
68
69void yaffs_free(void *ptr)
70{
71 free(ptr);
72}
73
74void yaffsfs_LocalInitialisation(void)
75{
76 // Define locking semaphore.
77}
78
79// Configuration for:
80// /ram 2MB ramdisk
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.
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020086//
William Juulc051bbe2007-11-15 11:13:05 +010087
88#include "yaffs_ramdisk.h"
89#include "yaffs_flashif.h"
90
William Juule24ebad2007-11-15 12:23:57 +010091static int isMounted = 0;
92#define MOUNT_POINT "/flash"
93extern nand_info_t nand_info[];
94
95/* XXX U-BOOT XXX */
96#if 0
William Juulc051bbe2007-11-15 11:13:05 +010097static yaffs_Device ramDev;
98static yaffs_Device bootDev;
99static yaffs_Device flashDev;
William Juule24ebad2007-11-15 12:23:57 +0100100#endif
William Juulc051bbe2007-11-15 11:13:05 +0100101
102static yaffsfs_DeviceConfiguration yaffsfs_config[] = {
William Juule24ebad2007-11-15 12:23:57 +0100103/* XXX U-BOOT XXX */
104#if 0
William Juulc051bbe2007-11-15 11:13:05 +0100105 { "/ram", &ramDev},
106 { "/boot", &bootDev},
107 { "/flash", &flashDev},
William Juule24ebad2007-11-15 12:23:57 +0100108#else
109 { MOUNT_POINT, 0},
110#endif
William Juulc051bbe2007-11-15 11:13:05 +0100111 {(void *)0,(void *)0}
112};
113
114
115int yaffs_StartUp(void)
116{
William Juule24ebad2007-11-15 12:23:57 +0100117 struct mtd_info *mtd = &nand_info[0];
118 int yaffsVersion = 2;
119 int nBlocks;
120
121 yaffs_Device *flashDev = calloc(1, sizeof(yaffs_Device));
122 yaffsfs_config[0].dev = flashDev;
123
William Juulc051bbe2007-11-15 11:13:05 +0100124 // Stuff to configure YAFFS
125 // Stuff to initialise anything special (eg lock semaphore).
126 yaffsfs_LocalInitialisation();
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200127
William Juulc051bbe2007-11-15 11:13:05 +0100128 // Set up devices
129
William Juule24ebad2007-11-15 12:23:57 +0100130/* XXX U-BOOT XXX */
131#if 0
William Juulc051bbe2007-11-15 11:13:05 +0100132 // /ram
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
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200137 ramDev.endBlock = 127; // Last block in 2MB.
William Juulc051bbe2007-11-15 11:13:05 +0100138 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;
145
146 // /boot
147 bootDev.nBytesPerChunk = 612;
148 bootDev.nChunksPerBlock = 32;
149 bootDev.nReservedBlocks = 5;
150 bootDev.startBlock = 1; // Can't use block 0
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200151 bootDev.endBlock = 127; // Last block in 2MB.
William Juulc051bbe2007-11-15 11:13:05 +0100152 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;
William Juule24ebad2007-11-15 12:23:57 +0100159#endif
William Juulc051bbe2007-11-15 11:13:05 +0100160
161 // /flash
William Juule24ebad2007-11-15 12:23:57 +0100162 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
166
167 if (yaffsVersion == 2)
168 {
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;
178#else
179 flashDev->nDataBytesPerChunk = mtd->oobblock;
180 flashDev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
181#endif
182 nBlocks = mtd->size / mtd->erasesize;
183
184 flashDev->nCheckpointReservedBlocks = 10;
185 flashDev->startBlock = 0;
186 flashDev->endBlock = nBlocks - 1;
187 }
188 else
189 {
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;
198 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200199
William Juule24ebad2007-11-15 12:23:57 +0100200 /* ... and common functions */
201 flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
202 flashDev->initialiseNAND = nandmtd_InitialiseNAND;
William Juulc051bbe2007-11-15 11:13:05 +0100203
204 yaffs_initialise(yaffsfs_config);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200205
William Juulc051bbe2007-11-15 11:13:05 +0100206 return 0;
207}
208
William Juule24ebad2007-11-15 12:23:57 +0100209
210void make_a_file(char *yaffsName,char bval,int sizeOfFile)
211{
212 int outh;
213 int i;
214 unsigned char buffer[100];
215
216 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
217 if (outh < 0)
218 {
219 printf("Error opening file: %d\n", outh);
220 return;
221 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200222
William Juule24ebad2007-11-15 12:23:57 +0100223 memset(buffer,bval,100);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200224
William Juule24ebad2007-11-15 12:23:57 +0100225 do{
226 i = sizeOfFile;
227 if(i > 100) i = 100;
228 sizeOfFile -= i;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200229
William Juule24ebad2007-11-15 12:23:57 +0100230 yaffs_write(outh,buffer,i);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200231
William Juule24ebad2007-11-15 12:23:57 +0100232 } while (sizeOfFile > 0);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200233
234
William Juule24ebad2007-11-15 12:23:57 +0100235 yaffs_close(outh);
236}
237
238void read_a_file(char *fn)
239{
240 int h;
241 int i = 0;
242 unsigned char b;
243
244 h = yaffs_open(fn, O_RDWR,0);
245 if(h<0)
246 {
247 printf("File not found\n");
248 return;
249 }
250
251 while(yaffs_read(h,&b,1)> 0)
252 {
253 printf("%02x ",b);
254 i++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200255 if(i > 32)
William Juule24ebad2007-11-15 12:23:57 +0100256 {
257 printf("\n");
258 i = 0;;
259 }
260 }
261 printf("\n");
262 yaffs_close(h);
263}
264
265void cmd_yaffs_mount(char *mp)
266{
267 yaffs_StartUp();
268 int retval = yaffs_mount(mp);
269 if( retval != -1)
270 isMounted = 1;
271 else
272 printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
273}
274
275static void checkMount(void)
276{
277 if( !isMounted )
278 {
279 cmd_yaffs_mount(MOUNT_POINT);
280 }
281}
William Juulc051bbe2007-11-15 11:13:05 +0100282
William Juule24ebad2007-11-15 12:23:57 +0100283void cmd_yaffs_umount(char *mp)
284{
285 checkMount();
286 if( yaffs_unmount(mp) == -1)
287 printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
288}
289
290void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
291{
292 checkMount();
293 make_a_file(yaffsName,bval,sizeOfFile);
294}
William Juulc051bbe2007-11-15 11:13:05 +0100295
296
William Juule24ebad2007-11-15 12:23:57 +0100297void cmd_yaffs_read_file(char *fn)
298{
299 checkMount();
300 read_a_file(fn);
301}
302
303
304void cmd_yaffs_mread_file(char *fn, char *addr)
305{
306 int h;
307 struct yaffs_stat s;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200308
William Juule24ebad2007-11-15 12:23:57 +0100309 checkMount();
310
311 yaffs_stat(fn,&s);
312
313 printf ("Copy %s to 0x%08x... ", fn, addr);
314 h = yaffs_open(fn, O_RDWR,0);
315 if(h<0)
316 {
317 printf("File not found\n");
318 return;
319 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200320
William Juule24ebad2007-11-15 12:23:57 +0100321 yaffs_read(h,addr,(int)s.st_size);
322 printf("\t[DONE]\n");
323
324 yaffs_close(h);
325}
326
327
328void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
329{
330 int outh;
331
332 checkMount();
333 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
334 if (outh < 0)
335 {
336 printf("Error opening file: %d\n", outh);
337 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200338
William Juule24ebad2007-11-15 12:23:57 +0100339 yaffs_write(outh,addr,size);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200340
William Juule24ebad2007-11-15 12:23:57 +0100341 yaffs_close(outh);
342}
343
344
345void cmd_yaffs_ls(const char *mountpt, int longlist)
346{
347 int i;
348 yaffs_DIR *d;
349 yaffs_dirent *de;
350 struct yaffs_stat stat;
351 char tempstr[255];
352
353 checkMount();
354 d = yaffs_opendir(mountpt);
355
356 if(!d)
357 {
358 printf("opendir failed\n");
359 }
360 else
361 {
362 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
363 {
364 if (longlist)
365 {
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);
369 }
370 else
371 {
372 printf("%s\n",de->d_name);
373 }
374 }
375 }
376}
377
378
379void cmd_yaffs_mkdir(const char *dir)
380{
381 checkMount();
382
383 int retval = yaffs_mkdir(dir, 0);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200384
William Juule24ebad2007-11-15 12:23:57 +0100385 if ( retval < 0)
386 printf("yaffs_mkdir returning error: %d\n", retval);
387}
388
389void cmd_yaffs_rmdir(const char *dir)
390{
391 checkMount();
392
393 int retval = yaffs_rmdir(dir);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200394
William Juule24ebad2007-11-15 12:23:57 +0100395 if ( retval < 0)
396 printf("yaffs_rmdir returning error: %d\n", retval);
397}
398
399void cmd_yaffs_rm(const char *path)
400{
401 checkMount();
402
403 int retval = yaffs_unlink(path);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200404
William Juule24ebad2007-11-15 12:23:57 +0100405 if ( retval < 0)
406 printf("yaffs_unlink returning error: %d\n", retval);
407}
408
409void cmd_yaffs_mv(const char *oldPath, const char *newPath)
410{
411 checkMount();
412
413 int retval = yaffs_rename(newPath, oldPath);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200414
William Juule24ebad2007-11-15 12:23:57 +0100415 if ( retval < 0)
416 printf("yaffs_unlink returning error: %d\n", retval);
417}