Add files to hook up yaffs direct to u-boot
[yaffs2.git] / direct / u-boot / fs / yaffs2 / yaffscfg.c
diff --git a/direct/u-boot/fs/yaffs2/yaffscfg.c b/direct/u-boot/fs/yaffs2/yaffscfg.c
new file mode 100644 (file)
index 0000000..49bd596
--- /dev/null
@@ -0,0 +1,324 @@
+/*
+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
+ *
+ * Copyright (C) 2002-2007 Aleph One Ltd.
+ *   for Toby Churchill Ltd and Brightstar Engineering
+ *
+ * Created by Charles Manning <charles@aleph1.co.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+/*
+ * yaffscfg.c  The configuration for the "direct" use of yaffs.
+ *
+ * This is set up for u-boot.
+ *
+ * This version now uses the ydevconfig mechanism to set up partitions.
+ */
+
+#include <common.h>
+
+#include <config.h>
+#include "nand.h"
+#include "yaffscfg.h"
+#include "yaffsfs.h"
+#include "yaffs_packedtags2.h"
+#include "yaffs_mtdif.h"
+#include "yaffs_mtdif2.h"
+#if 0
+#include <errno.h>
+#else
+#include "malloc.h"
+#endif
+
+unsigned yaffs_trace_mask = 0x0; /* Disable logging */
+static int yaffs_errno = 0;
+
+
+
+void *yaffsfs_malloc(size_t x)
+{
+       return malloc(x);
+}
+
+void yaffsfs_free(void *x)
+{
+       free(x);
+}
+
+void yaffsfs_SetError(int err)
+{
+       //Do whatever to set error
+       yaffs_errno = err;
+}
+
+int yaffsfs_GetLastError(void)
+{
+       return yaffs_errno;
+}
+
+
+int yaffsfs_GetError(void)
+{
+       return yaffs_errno;
+}
+
+void yaffsfs_Lock(void)
+{
+}
+
+void yaffsfs_Unlock(void)
+{
+}
+
+__u32 yaffsfs_CurrentTime(void)
+{
+       return 0;
+}
+
+void *yaffs_malloc(size_t size)
+{
+       return malloc(size);
+}
+
+void yaffs_free(void *ptr)
+{
+       free(ptr);
+}
+
+void yaffsfs_LocalInitialisation(void)
+{
+       // Define locking semaphore.
+}
+
+extern nand_info_t nand_info[];
+
+
+void cmd_yaffs_devconfig(char *_mp, int flash_dev, int start_block, int end_block)
+{
+       struct mtd_info *mtd = NULL;
+       struct yaffs_dev *dev;
+       char *mp;
+       
+       dev = calloc(1, sizeof(*dev));
+       mp = strdup(_mp);
+       
+       mtd = &nand_info[flash_dev];
+       if(!dev || !mp) {
+               /* Alloc error */
+               return;
+       }
+
+       if(end_block < start_block)
+               end_block = mtd->size / mtd->erasesize;
+
+       memset(dev, 0, sizeof(*dev));
+       dev->param.name = mp;
+       dev->driver_context = mtd;
+       dev->param.start_block = start_block;
+       dev->param.end_block = end_block;
+       dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
+       dev->param.total_bytes_per_chunk = mtd->writesize;
+       dev->param.is_yaffs2 = 1;
+       dev->param.use_nand_ecc = 1;
+       dev->param.n_reserved_blocks = 5;
+       dev->param.inband_tags = 0;
+       dev->param.n_caches = 10;
+       dev->param.write_chunk_tags_fn = nandmtd2_WriteChunkWithTagsToNAND;
+       dev->param.read_chunk_tags_fn = nandmtd2_ReadChunkWithTagsFromNAND;
+       dev->param.erase_fn = nandmtd_EraseBlockInNAND;
+       dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
+       dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
+       dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
+       
+       yaffs_add_device(dev);
+}
+       
+
+
+void make_a_file(char *yaffsName,char bval,int sizeOfFile)
+{
+       int outh;
+       int i;
+       unsigned char buffer[100];
+
+       outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
+       if (outh < 0)
+       {
+               printf("Error opening file: %d\n", outh);
+               return;
+       }
+
+       memset(buffer,bval,100);
+
+       do{
+               i = sizeOfFile;
+               if(i > 100) i = 100;
+               sizeOfFile -= i;
+
+               yaffs_write(outh,buffer,i);
+
+       } while (sizeOfFile > 0);
+
+
+       yaffs_close(outh);
+}
+
+void read_a_file(char *fn)
+{
+       int h;
+       int i = 0;
+       unsigned char b;
+
+       h = yaffs_open(fn, O_RDWR,0);
+       if(h<0)
+       {
+               printf("File not found\n");
+               return;
+       }
+
+       while(yaffs_read(h,&b,1)> 0)
+       {
+               printf("%02x ",b);
+               i++;
+               if(i > 32)
+               {
+                  printf("\n");
+                  i = 0;;
+                }
+       }
+       printf("\n");
+       yaffs_close(h);
+}
+
+void cmd_yaffs_mount(char *mp)
+{
+       int retval = yaffs_mount(mp);
+       if( retval < 0)
+               printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
+}
+
+
+void cmd_yaffs_umount(char *mp)
+{
+       if( yaffs_unmount(mp) == -1)
+               printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
+}
+
+void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
+{
+       make_a_file(yaffsName,bval,sizeOfFile);
+}
+
+
+void cmd_yaffs_read_file(char *fn)
+{
+       read_a_file(fn);
+}
+
+
+void cmd_yaffs_mread_file(char *fn, char *addr)
+{
+       int h;
+       struct yaffs_stat s;
+
+       yaffs_stat(fn,&s);
+
+       printf ("Copy %s to 0x%p... ", fn, addr);
+       h = yaffs_open(fn, O_RDWR,0);
+       if(h<0)
+       {
+               printf("File not found\n");
+               return;
+       }
+
+       yaffs_read(h,addr,(int)s.st_size);
+       printf("\t[DONE]\n");
+
+       yaffs_close(h);
+}
+
+
+void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
+{
+       int outh;
+
+       outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
+       if (outh < 0)
+       {
+               printf("Error opening file: %d\n", outh);
+       }
+
+       yaffs_write(outh,addr,size);
+
+       yaffs_close(outh);
+}
+
+
+void cmd_yaffs_ls(const char *mountpt, int longlist)
+{
+       int i;
+       yaffs_DIR *d;
+       yaffs_dirent *de;
+       struct yaffs_stat stat;
+       char tempstr[255];
+
+       d = yaffs_opendir(mountpt);
+
+       if(!d)
+       {
+               printf("opendir failed\n");
+       }
+       else
+       {
+               for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
+               {
+                       if (longlist)
+                       {
+                               sprintf(tempstr, "%s/%s", mountpt, de->d_name);
+                               yaffs_stat(tempstr, &stat);
+                               printf("%-25s\t%7ld\n",de->d_name, stat.st_size);
+                       }
+                       else
+                       {
+                               printf("%s\n",de->d_name);
+                       }
+               }
+       }
+}
+
+
+void cmd_yaffs_mkdir(const char *dir)
+{
+       int retval = yaffs_mkdir(dir, 0);
+
+       if ( retval < 0)
+               printf("yaffs_mkdir returning error: %d\n", retval);
+}
+
+void cmd_yaffs_rmdir(const char *dir)
+{
+       int retval = yaffs_rmdir(dir);
+
+       if ( retval < 0)
+               printf("yaffs_rmdir returning error: %d\n", retval);
+}
+
+void cmd_yaffs_rm(const char *path)
+{
+       int retval = yaffs_unlink(path);
+
+       if ( retval < 0)
+               printf("yaffs_unlink returning error: %d\n", retval);
+}
+
+void cmd_yaffs_mv(const char *oldPath, const char *newPath)
+{
+       int retval = yaffs_rename(newPath, oldPath);
+
+       if ( retval < 0)
+               printf("yaffs_unlink returning error: %d\n", retval);
+}