Merge branch 'next' of git://git.denx.de/u-boot-avr32
[platform/kernel/u-boot.git] / fs / yaffs2 / yaffscfg.c
1 /*
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
21 /* XXX U-BOOT XXX */
22 #include <common.h>
23
24 #include <config.h>
25 #include "nand.h"
26 #include "yaffscfg.h"
27 #include "yaffsfs.h"
28 #include "yaffs_packedtags2.h"
29 #include "yaffs_mtdif.h"
30 #include "yaffs_mtdif2.h"
31 #if 0
32 #include <errno.h>
33 #else
34 #include "malloc.h"
35 #endif
36
37 unsigned yaffs_traceMask = 0xFFFFFFFF;
38 static int yaffs_errno = 0;
39
40 void yaffsfs_SetError(int err)
41 {
42         //Do whatever to set error
43         yaffs_errno = err;
44 }
45
46 int yaffsfs_GetError(void)
47 {
48         return yaffs_errno;
49 }
50
51 void yaffsfs_Lock(void)
52 {
53 }
54
55 void yaffsfs_Unlock(void)
56 {
57 }
58
59 __u32 yaffsfs_CurrentTime(void)
60 {
61         return 0;
62 }
63
64 void *yaffs_malloc(size_t size)
65 {
66         return malloc(size);
67 }
68
69 void yaffs_free(void *ptr)
70 {
71         free(ptr);
72 }
73
74 void 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.
86 //
87
88 #include "yaffs_ramdisk.h"
89 #include "yaffs_flashif.h"
90
91 static int isMounted = 0;
92 #define MOUNT_POINT "/flash"
93 extern nand_info_t nand_info[];
94
95 /* XXX U-BOOT XXX */
96 #if 0
97 static yaffs_Device ramDev;
98 static yaffs_Device bootDev;
99 static yaffs_Device flashDev;
100 #endif
101
102 static yaffsfs_DeviceConfiguration yaffsfs_config[] = {
103 /* XXX U-BOOT XXX */
104 #if 0
105         { "/ram", &ramDev},
106         { "/boot", &bootDev},
107         { "/flash", &flashDev},
108 #else
109         { MOUNT_POINT, 0},
110 #endif
111         {(void *)0,(void *)0}
112 };
113
114
115 int yaffs_StartUp(void)
116 {
117         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
124         // Stuff to configure YAFFS
125         // Stuff to initialise anything special (eg lock semaphore).
126         yaffsfs_LocalInitialisation();
127
128         // Set up devices
129
130 /* XXX U-BOOT XXX */
131 #if 0
132         // /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
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;
145
146         // /boot
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;
159 #endif
160
161                 // /flash
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
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         }
199
200         /* ... and common functions */
201         flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
202         flashDev->initialiseNAND = nandmtd_InitialiseNAND;
203
204         yaffs_initialise(yaffsfs_config);
205
206         return 0;
207 }
208
209
210 void 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         }
222
223         memset(buffer,bval,100);
224
225         do{
226                 i = sizeOfFile;
227                 if(i > 100) i = 100;
228                 sizeOfFile -= i;
229
230                 yaffs_write(outh,buffer,i);
231
232         } while (sizeOfFile > 0);
233
234
235         yaffs_close(outh);
236 }
237
238 void 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++;
255                 if(i > 32)
256                 {
257                    printf("\n");
258                    i = 0;;
259                  }
260         }
261         printf("\n");
262         yaffs_close(h);
263 }
264
265 void 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
275 static void checkMount(void)
276 {
277         if( !isMounted )
278         {
279                 cmd_yaffs_mount(MOUNT_POINT);
280         }
281 }
282
283 void 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
290 void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
291 {
292         checkMount();
293         make_a_file(yaffsName,bval,sizeOfFile);
294 }
295
296
297 void cmd_yaffs_read_file(char *fn)
298 {
299         checkMount();
300         read_a_file(fn);
301 }
302
303
304 void cmd_yaffs_mread_file(char *fn, char *addr)
305 {
306         int h;
307         struct yaffs_stat s;
308
309         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         }
320
321         yaffs_read(h,addr,(int)s.st_size);
322         printf("\t[DONE]\n");
323
324         yaffs_close(h);
325 }
326
327
328 void 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         }
338
339         yaffs_write(outh,addr,size);
340
341         yaffs_close(outh);
342 }
343
344
345 void 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
379 void cmd_yaffs_mkdir(const char *dir)
380 {
381         checkMount();
382
383         int retval = yaffs_mkdir(dir, 0);
384
385         if ( retval < 0)
386                 printf("yaffs_mkdir returning error: %d\n", retval);
387 }
388
389 void cmd_yaffs_rmdir(const char *dir)
390 {
391         checkMount();
392
393         int retval = yaffs_rmdir(dir);
394
395         if ( retval < 0)
396                 printf("yaffs_rmdir returning error: %d\n", retval);
397 }
398
399 void cmd_yaffs_rm(const char *path)
400 {
401         checkMount();
402
403         int retval = yaffs_unlink(path);
404
405         if ( retval < 0)
406                 printf("yaffs_unlink returning error: %d\n", retval);
407 }
408
409 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
410 {
411         checkMount();
412
413         int retval = yaffs_rename(newPath, oldPath);
414
415         if ( retval < 0)
416                 printf("yaffs_unlink returning error: %d\n", retval);
417 }