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"
31 #include "yaffs_uboot_glue.h"
39 unsigned yaffs_trace_mask = 0x0; /* Disable logging */
40 static int yaffs_errno;
43 void yaffs_bug_fn(const char *fn, int n)
45 printf("yaffs bug at %s:%d\n", fn, n);
48 void *yaffsfs_malloc(size_t x)
53 void yaffsfs_free(void *x)
58 void yaffsfs_SetError(int err)
63 int yaffsfs_GetLastError(void)
69 int yaffsfs_GetError(void)
74 void yaffsfs_Lock(void)
78 void yaffsfs_Unlock(void)
82 __u32 yaffsfs_CurrentTime(void)
87 void *yaffs_malloc(size_t size)
92 void yaffs_free(void *ptr)
97 void yaffsfs_LocalInitialisation(void)
103 static const char *yaffs_file_type_str(struct yaffs_stat *stat)
105 switch (stat->st_mode & S_IFMT) {
106 case S_IFREG: return "regular file";
107 case S_IFDIR: return "directory";
108 case S_IFLNK: return "symlink";
109 default: return "unknown";
113 static const char *yaffs_error_str(void)
115 int error = yaffsfs_GetLastError();
121 case EBUSY: return "Busy";
122 case ENODEV: return "No such device";
123 case EINVAL: return "Invalid parameter";
124 case ENFILE: return "Too many open files";
125 case EBADF: return "Bad handle";
126 case EACCES: return "Wrong permissions";
127 case EXDEV: return "Not on same device";
128 case ENOENT: return "No such entry";
129 case ENOSPC: return "Device full";
130 case EROFS: return "Read only file system";
131 case ERANGE: return "Range error";
132 case ENOTEMPTY: return "Not empty";
133 case ENAMETOOLONG: return "Name too long";
134 case ENOMEM: return "Out of memory";
135 case EFAULT: return "Fault";
136 case EEXIST: return "Name exists";
137 case ENOTDIR: return "Not a directory";
138 case EISDIR: return "Not permitted on a directory";
139 case ELOOP: return "Symlink loop";
140 case 0: return "No error";
141 default: return "Unknown error";
145 extern nand_info_t nand_info[];
147 int cmd_yaffs_tracemask(unsigned set, unsigned mask)
150 yaffs_trace_mask = mask;
152 printf("yaffs trace mask: %08x\n", yaffs_trace_mask);
156 static int yaffs_regions_overlap(int a, int b, int x, int y)
158 return (a <= x && x <= b) ||
159 (a <= y && y <= b) ||
160 (x <= a && a <= y) ||
164 int cmd_yaffs_devconfig(char *_mp, int flash_dev,
165 int start_block, int end_block)
167 struct mtd_info *mtd = NULL;
168 struct yaffs_dev *dev = NULL;
169 struct yaffs_dev *chk;
171 struct nand_chip *chip;
173 dev = calloc(1, sizeof(*dev));
176 mtd = &nand_info[flash_dev];
180 printf("Failed to allocate memory\n");
184 if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
185 printf("Flash device invalid\n");
190 end_block = mtd->size / mtd->erasesize - 1;
192 if (end_block < start_block) {
193 printf("Bad start/end\n");
199 /* Check for any conflicts */
202 chk = yaffs_next_dev();
205 if (strcmp(chk->param.name, mp) == 0) {
206 printf("Mount point name already used\n");
209 if (chk->driver_context == mtd &&
210 yaffs_regions_overlap(
211 chk->param.start_block, chk->param.end_block,
212 start_block, end_block)) {
213 printf("Region overlaps with partition %s\n",
220 /* Seems sane, so configure */
221 memset(dev, 0, sizeof(*dev));
222 dev->param.name = mp;
223 dev->driver_context = mtd;
224 dev->param.start_block = start_block;
225 dev->param.end_block = end_block;
226 dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
227 dev->param.total_bytes_per_chunk = mtd->writesize;
228 dev->param.is_yaffs2 = 1;
229 dev->param.use_nand_ecc = 1;
230 dev->param.n_reserved_blocks = 5;
231 if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
232 dev->param.inband_tags = 1;
233 dev->param.n_caches = 10;
234 dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
235 dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
236 dev->param.erase_fn = nandmtd_EraseBlockInNAND;
237 dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
238 dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
239 dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
241 yaffs_add_device(dev);
243 printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
244 mp, flash_dev, start_block, end_block,
245 dev->param.inband_tags ? "using inband tags" : "");
254 int cmd_yaffs_dev_ls(void)
256 struct yaffs_dev *dev;
263 dev = yaffs_next_dev();
267 ((unsigned) dev->driver_context - (unsigned) nand_info)/
268 sizeof(nand_info[0]);
269 printf("%-10s %5d 0x%05x 0x%05x %s",
270 dev->param.name, flash_dev,
271 dev->param.start_block, dev->param.end_block,
272 dev->param.inband_tags ? "using inband tags, " : "");
274 free_space = yaffs_freespace(dev->param.name);
276 printf("not mounted\n");
278 printf("free 0x%x\n", free_space);
285 int make_a_file(char *yaffsName, char bval, int sizeOfFile)
290 unsigned char buffer[100];
292 outh = yaffs_open(yaffsName,
293 O_CREAT | O_RDWR | O_TRUNC,
296 printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
300 memset(buffer, bval, 100);
303 while (written < sizeOfFile) {
304 i = sizeOfFile - written;
308 if (yaffs_write(outh, buffer, i) != i)
315 return (written == sizeOfFile) ? 0 : -1;
318 int read_a_file(char *fn)
324 h = yaffs_open(fn, O_RDWR, 0);
326 printf("File not found\n");
330 while (yaffs_read(h, &b, 1) > 0) {
344 int cmd_yaffs_mount(char *mp)
346 int retval = yaffs_mount(mp);
348 printf("Error mounting %s, return value: %d, %s\n", mp,
349 yaffsfs_GetError(), yaffs_error_str());
354 int cmd_yaffs_umount(char *mp)
356 int retval = yaffs_unmount(mp);
359 printf("Error umounting %s, return value: %d, %s\n", mp,
360 yaffsfs_GetError(), yaffs_error_str());
364 int cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
366 return make_a_file(yaffsName, bval, sizeOfFile);
370 int cmd_yaffs_read_file(char *fn)
372 return read_a_file(fn);
376 int cmd_yaffs_mread_file(char *fn, char *addr)
386 printf("Copy %s to 0x%p... ", fn, addr);
387 h = yaffs_open(fn, O_RDWR, 0);
389 printf("File not found\n");
393 read_size = (int) s.st_size;
394 yaffs_read(h, addr, read_size);
395 printf("\t[DONE]\n");
399 sprintf(buf,"%x", read_size);
400 setenv("filesize", buf);
406 int cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
411 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
413 printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
417 wrote = yaffs_write(outh, addr, size);
421 printf("only wrote %d (0x%x) bytes\n", wrote, wrote);
430 int cmd_yaffs_ls(const char *mountpt, int longlist)
434 struct yaffs_dirent *de;
435 struct yaffs_stat stat;
438 d = yaffs_opendir(mountpt);
441 printf("opendir failed, %s\n", yaffs_error_str());
445 for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
447 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
448 yaffs_lstat(tempstr, &stat);
449 printf("%-25s\t%7ld",
454 yaffs_file_type_str(&stat));
456 printf("%s\n", de->d_name);
465 int cmd_yaffs_check(const char *fname, const char *type)
469 struct yaffs_stat stat;
471 ret = yaffs_stat(fname, &stat);
473 printf("%s not found\n", fname);
477 printf("%s is a %s\n", fname, yaffs_file_type_str(&stat));
479 if (strcmp(type, "REG") == 0 &&
480 (stat.st_mode & S_IFMT) != S_IFREG)
483 if (strcmp(type, "DIR") == 0 &&
484 (stat.st_mode & S_IFMT) != S_IFDIR)
488 printf("check ok\n");
490 printf("check failed\n");
496 int cmd_yaffs_mkdir(const char *dir)
498 int retval = yaffs_mkdir(dir, 0);
501 printf("yaffs_mkdir returning error: %d, %s\n",
502 retval, yaffs_error_str());
508 int cmd_yaffs_rmdir(const char *dir)
510 int retval = yaffs_rmdir(dir);
513 printf("yaffs_rmdir returning error: %d, %s\n",
514 retval, yaffs_error_str());
520 int cmd_yaffs_rm(const char *path)
522 int retval = yaffs_unlink(path);
525 printf("yaffs_unlink returning error: %d, %s\n",
526 retval, yaffs_error_str());
533 int cmd_yaffs_mv(const char *oldPath, const char *newPath)
535 int retval = yaffs_rename(newPath, oldPath);
538 printf("yaffs_unlink returning error: %d, %s\n",
539 retval, yaffs_error_str());