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 is set up for u-boot.
19 * This version now uses the ydevconfig mechanism to set up partitions.
28 #include "yaffs_packedtags2.h"
29 #include "yaffs_mtdif.h"
30 #include "yaffs_mtdif2.h"
37 unsigned yaffs_trace_mask = 0x0; /* Disable logging */
38 static int yaffs_errno = 0;
41 void yaffs_bug_fn(const char *fn, int n)
43 printf("yaffs bug at %s:%d\n", fn, n);
46 void *yaffsfs_malloc(size_t x)
51 void yaffsfs_free(void *x)
56 void yaffsfs_SetError(int err)
61 int yaffsfs_GetLastError(void)
67 int yaffsfs_GetError(void)
72 void yaffsfs_Lock(void)
76 void yaffsfs_Unlock(void)
80 __u32 yaffsfs_CurrentTime(void)
85 void *yaffs_malloc(size_t size)
90 void yaffs_free(void *ptr)
95 void yaffsfs_LocalInitialisation(void)
101 static const char *yaffs_file_type_str(struct yaffs_stat *stat)
103 switch(stat->st_mode & S_IFMT) {
104 case S_IFREG: return "regular file";
105 case S_IFDIR: return "directory";
106 case S_IFLNK: return "symlink";
107 default: return "unknown";
111 static const char *yaffs_error_str(void)
113 int error = yaffsfs_GetLastError();
119 case EBUSY: return "Busy";
120 case ENODEV: return "No such device";
121 case EINVAL: return "Invalid parameter";
122 case ENFILE: return "Too many open files";
123 case EBADF: return "Bad handle";
124 case EACCES: return "Wrong permissions";
125 case EXDEV: return "Not on same device";
126 case ENOENT: return "No such entry";
127 case ENOSPC: return "Device full";
128 case EROFS: return "Read only file system";
129 case ERANGE: return "Range error";
130 case ENOTEMPTY: return "Not empty";
131 case ENAMETOOLONG: return "Name too long";
132 case ENOMEM: return "Out of memory";
133 case EFAULT: return "Fault";
134 case EEXIST: return "Name exists";
135 case ENOTDIR: return "Not a directory";
136 case EISDIR: return "Not permitted on a directory";
137 case ELOOP: return "Symlink loop";
138 case 0: return "No error";
139 default: return "Unknown error";
143 extern nand_info_t nand_info[];
145 void cmd_yaffs_tracemask(unsigned set, unsigned mask)
148 yaffs_trace_mask = mask;
150 printf("yaffs trace mask: %08x\n",yaffs_trace_mask);
153 static int yaffs_regions_overlap(int a, int b, int x, int y)
156 (a <= x && x <= b) ||
157 (a <= y && y <= b) ||
158 (x <= a && a <= y) ||
162 void cmd_yaffs_devconfig(char *_mp, int flash_dev,
163 int start_block, int end_block)
165 struct mtd_info *mtd = NULL;
166 struct yaffs_dev *dev = NULL;
167 struct yaffs_dev *chk;
169 struct nand_chip *chip;
171 dev = calloc(1, sizeof(*dev));
174 mtd = &nand_info[flash_dev];
178 printf("Failed to allocate memory\n");
182 if(flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
183 printf("Flash device invalid\n");
188 end_block = mtd->size / mtd->erasesize - 1;
190 if (end_block < start_block) {
191 printf("Bad start/end\n");
197 /* Check for any conflicts */
200 chk = yaffs_next_dev();
203 if(strcmp(chk->param.name, mp) == 0) {
204 printf("Mount point name already used\n");
207 if(chk->driver_context == mtd &&
208 yaffs_regions_overlap(
209 chk->param.start_block, chk->param.end_block,
210 start_block, end_block)) {
211 printf("Region overlaps with partition %s\n",
218 /* Seems sane, so configure */
219 memset(dev, 0, sizeof(*dev));
220 dev->param.name = mp;
221 dev->driver_context = mtd;
222 dev->param.start_block = start_block;
223 dev->param.end_block = end_block;
224 dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
225 dev->param.total_bytes_per_chunk = mtd->writesize;
226 dev->param.is_yaffs2 = 1;
227 dev->param.use_nand_ecc = 1;
228 dev->param.n_reserved_blocks = 5;
229 if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
230 dev->param.inband_tags =1;
231 dev->param.n_caches = 10;
232 dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
233 dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
234 dev->param.erase_fn = nandmtd_EraseBlockInNAND;
235 dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
236 dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
237 dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
239 yaffs_add_device(dev);
241 printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
242 mp, flash_dev, start_block, end_block,
243 dev->param.inband_tags ? "using inband tags" : "");
251 void cmd_yaffs_dev_ls(void)
253 struct yaffs_dev *dev;
260 dev = yaffs_next_dev();
264 ((unsigned) dev->driver_context - (unsigned) nand_info)/
265 sizeof(nand_info[0]);
266 printf("%-10s %5d 0x%05x 0x%05x %s",
267 dev->param.name, flash_dev,
268 dev->param.start_block, dev->param.end_block,
269 dev->param.inband_tags ? "using inband tags, " : "");
271 free_space = yaffs_freespace(dev->param.name);
273 printf("not mounted\n");
275 printf("free 0x%x\n", free_space);
280 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
284 unsigned char buffer[100];
286 outh = yaffs_open(yaffsName,
287 O_CREAT | O_RDWR | O_TRUNC,
290 printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
294 memset(buffer,bval,100);
301 yaffs_write(outh,buffer,i);
303 } while (sizeOfFile > 0);
309 void read_a_file(char *fn)
315 h = yaffs_open(fn, O_RDWR,0);
318 printf("File not found\n");
322 while(yaffs_read(h,&b,1)> 0)
336 void cmd_yaffs_mount(char *mp)
338 int retval = yaffs_mount(mp);
340 printf("Error mounting %s, return value: %d, %s\n", mp,
341 yaffsfs_GetError(), yaffs_error_str());
345 void cmd_yaffs_umount(char *mp)
347 if( yaffs_unmount(mp) == -1)
348 printf("Error umounting %s, return value: %d, %s\n", mp,
349 yaffsfs_GetError(), yaffs_error_str());
352 void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
354 make_a_file(yaffsName,bval,sizeOfFile);
358 void cmd_yaffs_read_file(char *fn)
364 void cmd_yaffs_mread_file(char *fn, char *addr)
371 printf ("Copy %s to 0x%p... ", fn, addr);
372 h = yaffs_open(fn, O_RDWR,0);
375 printf("File not found\n");
379 yaffs_read(h,addr,(int)s.st_size);
380 printf("\t[DONE]\n");
386 void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
390 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
393 printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
396 yaffs_write(outh,addr,size);
402 void cmd_yaffs_ls(const char *mountpt, int longlist)
407 struct yaffs_stat stat;
410 d = yaffs_opendir(mountpt);
413 printf("opendir failed, %s\n", yaffs_error_str());
417 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
419 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
420 yaffs_lstat(tempstr, &stat);
421 printf("%-25s\t%7ld",
426 yaffs_file_type_str(&stat));
428 printf("%s\n",de->d_name);
436 void cmd_yaffs_mkdir(const char *dir)
438 int retval = yaffs_mkdir(dir, 0);
441 printf("yaffs_mkdir returning error: %d, %s\n",
442 retval, yaffs_error_str());
445 void cmd_yaffs_rmdir(const char *dir)
447 int retval = yaffs_rmdir(dir);
450 printf("yaffs_rmdir returning error: %d, %s\n",
451 retval, yaffs_error_str());
454 void cmd_yaffs_rm(const char *path)
456 int retval = yaffs_unlink(path);
459 printf("yaffs_unlink returning error: %d, %s\n",
460 retval, yaffs_error_str());
463 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
465 int retval = yaffs_rename(newPath, oldPath);
468 printf("yaffs_unlink returning error: %d, %s\n",
469 retval, yaffs_error_str());