2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * yaffscfg.c The configuration for the "direct" use of yaffs.
16 * This is set up for u-boot.
18 * This version now uses the ydevconfig mechanism to set up partitions.
27 #include "yaffs_packedtags2.h"
28 #include "yaffs_mtdif.h"
29 #include "yaffs_mtdif2.h"
30 #include "yaffs_uboot_glue.h"
38 unsigned yaffs_trace_mask = 0x0; /* Disable logging */
39 static int yaffs_errno;
42 void yaffs_bug_fn(const char *fn, int n)
44 printf("yaffs bug at %s:%d\n", fn, n);
47 void *yaffsfs_malloc(size_t x)
52 void yaffsfs_free(void *x)
57 void yaffsfs_SetError(int err)
62 int yaffsfs_GetLastError(void)
68 int yaffsfs_GetError(void)
73 void yaffsfs_Lock(void)
77 void yaffsfs_Unlock(void)
81 __u32 yaffsfs_CurrentTime(void)
86 void *yaffs_malloc(size_t size)
91 void yaffs_free(void *ptr)
96 void yaffsfs_LocalInitialisation(void)
102 static const char *yaffs_file_type_str(struct yaffs_stat *stat)
104 switch (stat->st_mode & S_IFMT) {
105 case S_IFREG: return "regular file";
106 case S_IFDIR: return "directory";
107 case S_IFLNK: return "symlink";
108 default: return "unknown";
112 static const char *yaffs_error_str(void)
114 int error = yaffsfs_GetLastError();
120 case EBUSY: return "Busy";
121 case ENODEV: return "No such device";
122 case EINVAL: return "Invalid parameter";
123 case ENFILE: return "Too many open files";
124 case EBADF: return "Bad handle";
125 case EACCES: return "Wrong permissions";
126 case EXDEV: return "Not on same device";
127 case ENOENT: return "No such entry";
128 case ENOSPC: return "Device full";
129 case EROFS: return "Read only file system";
130 case ERANGE: return "Range error";
131 case ENOTEMPTY: return "Not empty";
132 case ENAMETOOLONG: return "Name too long";
133 case ENOMEM: return "Out of memory";
134 case EFAULT: return "Fault";
135 case EEXIST: return "Name exists";
136 case ENOTDIR: return "Not a directory";
137 case EISDIR: return "Not permitted on a directory";
138 case ELOOP: return "Symlink loop";
139 case 0: return "No error";
140 default: return "Unknown error";
144 extern nand_info_t nand_info[];
146 int cmd_yaffs_tracemask(unsigned set, unsigned mask)
149 yaffs_trace_mask = mask;
151 printf("yaffs trace mask: %08x\n", yaffs_trace_mask);
155 static int yaffs_regions_overlap(int a, int b, int x, int y)
157 return (a <= x && x <= b) ||
158 (a <= y && y <= b) ||
159 (x <= a && a <= y) ||
163 int cmd_yaffs_devconfig(char *_mp, int flash_dev,
164 int start_block, int end_block)
166 struct mtd_info *mtd = NULL;
167 struct yaffs_dev *dev = NULL;
168 struct yaffs_dev *chk;
170 struct nand_chip *chip;
172 dev = calloc(1, sizeof(*dev));
175 mtd = &nand_info[flash_dev];
179 printf("Failed to allocate memory\n");
183 if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
184 printf("Flash device invalid\n");
189 end_block = mtd->size / mtd->erasesize - 1;
191 if (end_block < start_block) {
192 printf("Bad start/end\n");
198 /* Check for any conflicts */
201 chk = yaffs_next_dev();
204 if (strcmp(chk->param.name, mp) == 0) {
205 printf("Mount point name already used\n");
208 if (chk->driver_context == mtd &&
209 yaffs_regions_overlap(
210 chk->param.start_block, chk->param.end_block,
211 start_block, end_block)) {
212 printf("Region overlaps with partition %s\n",
219 /* Seems sane, so configure */
220 memset(dev, 0, sizeof(*dev));
221 dev->param.name = mp;
222 dev->driver_context = mtd;
223 dev->param.start_block = start_block;
224 dev->param.end_block = end_block;
225 dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
226 dev->param.total_bytes_per_chunk = mtd->writesize;
227 dev->param.is_yaffs2 = 1;
228 dev->param.use_nand_ecc = 1;
229 dev->param.n_reserved_blocks = 5;
230 if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
231 dev->param.inband_tags = 1;
232 dev->param.n_caches = 10;
233 dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
234 dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
235 dev->param.erase_fn = nandmtd_EraseBlockInNAND;
236 dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
237 dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
238 dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
240 yaffs_add_device(dev);
242 printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
243 mp, flash_dev, start_block, end_block,
244 dev->param.inband_tags ? "using inband tags" : "");
253 int cmd_yaffs_dev_ls(void)
255 struct yaffs_dev *dev;
262 dev = yaffs_next_dev();
266 ((unsigned) dev->driver_context - (unsigned) nand_info)/
267 sizeof(nand_info[0]);
268 printf("%-10s %5d 0x%05x 0x%05x %s",
269 dev->param.name, flash_dev,
270 dev->param.start_block, dev->param.end_block,
271 dev->param.inband_tags ? "using inband tags, " : "");
273 free_space = yaffs_freespace(dev->param.name);
275 printf("not mounted\n");
277 printf("free 0x%x\n", free_space);
284 int make_a_file(char *yaffsName, char bval, int sizeOfFile)
289 unsigned char buffer[100];
291 outh = yaffs_open(yaffsName,
292 O_CREAT | O_RDWR | O_TRUNC,
295 printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
299 memset(buffer, bval, 100);
302 while (written < sizeOfFile) {
303 i = sizeOfFile - written;
307 if (yaffs_write(outh, buffer, i) != i)
314 return (written == sizeOfFile) ? 0 : -1;
317 int read_a_file(char *fn)
323 h = yaffs_open(fn, O_RDWR, 0);
325 printf("File not found\n");
329 while (yaffs_read(h, &b, 1) > 0) {
343 int cmd_yaffs_mount(char *mp)
345 int retval = yaffs_mount(mp);
347 printf("Error mounting %s, return value: %d, %s\n", mp,
348 yaffsfs_GetError(), yaffs_error_str());
353 int cmd_yaffs_umount(char *mp)
355 int retval = yaffs_unmount(mp);
358 printf("Error umounting %s, return value: %d, %s\n", mp,
359 yaffsfs_GetError(), yaffs_error_str());
363 int cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
365 return make_a_file(yaffsName, bval, sizeOfFile);
369 int cmd_yaffs_read_file(char *fn)
371 return read_a_file(fn);
375 int cmd_yaffs_mread_file(char *fn, char *addr)
385 printf("Copy %s to 0x%p... ", fn, addr);
386 h = yaffs_open(fn, O_RDWR, 0);
388 printf("File not found\n");
392 read_size = (int) s.st_size;
393 yaffs_read(h, addr, read_size);
394 printf("\t[DONE]\n");
398 sprintf(buf,"%x", read_size);
399 setenv("filesize", buf);
405 int cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
410 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
412 printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
416 wrote = yaffs_write(outh, addr, size);
420 printf("only wrote %d (0x%x) bytes\n", wrote, wrote);
429 int cmd_yaffs_ls(const char *mountpt, int longlist)
433 struct yaffs_dirent *de;
434 struct yaffs_stat stat;
437 d = yaffs_opendir(mountpt);
440 printf("opendir failed, %s\n", yaffs_error_str());
444 for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
446 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
447 yaffs_lstat(tempstr, &stat);
448 printf("%-25s\t%7ld",
453 yaffs_file_type_str(&stat));
455 printf("%s\n", de->d_name);
464 int cmd_yaffs_check(const char *fname, const char *type)
468 struct yaffs_stat stat;
470 ret = yaffs_stat(fname, &stat);
472 printf("%s not found\n", fname);
476 printf("%s is a %s\n", fname, yaffs_file_type_str(&stat));
478 if (strcmp(type, "REG") == 0 &&
479 (stat.st_mode & S_IFMT) != S_IFREG)
482 if (strcmp(type, "DIR") == 0 &&
483 (stat.st_mode & S_IFMT) != S_IFDIR)
487 printf("check ok\n");
489 printf("check failed\n");
495 int cmd_yaffs_mkdir(const char *dir)
497 int retval = yaffs_mkdir(dir, 0);
500 printf("yaffs_mkdir returning error: %d, %s\n",
501 retval, yaffs_error_str());
507 int cmd_yaffs_rmdir(const char *dir)
509 int retval = yaffs_rmdir(dir);
512 printf("yaffs_rmdir returning error: %d, %s\n",
513 retval, yaffs_error_str());
519 int cmd_yaffs_rm(const char *path)
521 int retval = yaffs_unlink(path);
524 printf("yaffs_unlink returning error: %d, %s\n",
525 retval, yaffs_error_str());
532 int cmd_yaffs_mv(const char *oldPath, const char *newPath)
534 int retval = yaffs_rename(newPath, oldPath);
537 printf("yaffs_unlink returning error: %d, %s\n",
538 retval, yaffs_error_str());