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
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.
30 #include "yaffs_packedtags2.h"
31 #include "yaffs_mtdif.h"
32 #include "yaffs_mtdif2.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 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)
155 return (a <= x && x <= b) ||
156 (a <= y && y <= b) ||
157 (x <= a && a <= y) ||
161 void cmd_yaffs_devconfig(char *_mp, int flash_dev,
162 int start_block, int end_block)
164 struct mtd_info *mtd = NULL;
165 struct yaffs_dev *dev = NULL;
166 struct yaffs_dev *chk;
168 struct nand_chip *chip;
170 mtd = get_nand_dev_by_index(flash_dev);
172 pr_err("\nno NAND devices available\n");
176 dev = calloc(1, sizeof(*dev));
181 printf("Failed to allocate memory\n");
185 if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
186 printf("Flash device invalid\n");
191 end_block = lldiv(mtd->size, mtd->erasesize - 1);
193 if (end_block < start_block) {
194 printf("Bad start/end\n");
198 chip = mtd_to_nand(mtd);
200 /* Check for any conflicts */
203 chk = yaffs_next_dev();
206 if (strcmp(chk->param.name, mp) == 0) {
207 printf("Mount point name already used\n");
210 if (chk->driver_context == mtd &&
211 yaffs_regions_overlap(
212 chk->param.start_block, chk->param.end_block,
213 start_block, end_block)) {
214 printf("Region overlaps with partition %s\n",
221 /* Seems sane, so configure */
222 memset(dev, 0, sizeof(*dev));
223 dev->param.name = mp;
224 dev->driver_context = mtd;
225 dev->param.start_block = start_block;
226 dev->param.end_block = end_block;
227 dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
228 dev->param.total_bytes_per_chunk = mtd->writesize;
229 dev->param.is_yaffs2 = 1;
230 dev->param.use_nand_ecc = 1;
231 dev->param.n_reserved_blocks = 5;
232 if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
233 dev->param.inband_tags = 1;
234 dev->param.n_caches = 10;
235 dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
236 dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
237 dev->param.erase_fn = nandmtd_EraseBlockInNAND;
238 dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
239 dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
240 dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
242 yaffs_add_device(dev);
244 printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
245 mp, flash_dev, start_block, end_block,
246 dev->param.inband_tags ? "using inband tags" : "");
254 void cmd_yaffs_dev_ls(void)
256 struct yaffs_dev *dev;
263 dev = yaffs_next_dev();
266 flash_dev = nand_mtd_to_devnum(dev->driver_context);
267 printf("%-10s %5d 0x%05x 0x%05x %s",
268 dev->param.name, flash_dev,
269 dev->param.start_block, dev->param.end_block,
270 dev->param.inband_tags ? "using inband tags, " : "");
272 free_space = yaffs_freespace(dev->param.name);
274 printf("not mounted\n");
276 printf("free 0x%x\n", free_space);
281 void make_a_file(char *yaffsName, char bval, int sizeOfFile)
285 unsigned char buffer[100];
287 outh = yaffs_open(yaffsName,
288 O_CREAT | O_RDWR | O_TRUNC,
291 printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
295 memset(buffer, bval, 100);
303 yaffs_write(outh, buffer, i);
305 } while (sizeOfFile > 0);
311 void read_a_file(char *fn)
317 h = yaffs_open(fn, O_RDWR, 0);
319 printf("File not found\n");
323 while (yaffs_read(h, &b, 1) > 0) {
335 void cmd_yaffs_mount(char *mp)
337 int retval = yaffs_mount(mp);
339 printf("Error mounting %s, return value: %d, %s\n", mp,
340 yaffsfs_GetError(), yaffs_error_str());
344 void cmd_yaffs_umount(char *mp)
346 if (yaffs_unmount(mp) == -1)
347 printf("Error umounting %s, return value: %d, %s\n", mp,
348 yaffsfs_GetError(), yaffs_error_str());
351 void cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
353 make_a_file(yaffsName, bval, sizeOfFile);
357 void cmd_yaffs_read_file(char *fn)
363 void cmd_yaffs_mread_file(char *fn, char *addr)
370 printf("Copy %s to 0x%p... ", fn, addr);
371 h = yaffs_open(fn, O_RDWR, 0);
373 printf("File not found\n");
377 yaffs_read(h, addr, (int)s.st_size);
378 printf("\t[DONE]\n");
384 void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
388 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
390 printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
392 yaffs_write(outh, addr, size);
398 void cmd_yaffs_ls(const char *mountpt, int longlist)
402 struct yaffs_dirent *de;
403 struct yaffs_stat stat;
406 d = yaffs_opendir(mountpt);
409 printf("opendir failed, %s\n", yaffs_error_str());
413 for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
415 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
416 yaffs_lstat(tempstr, &stat);
417 printf("%-25s\t%7ld",
422 yaffs_file_type_str(&stat));
424 printf("%s\n", de->d_name);
432 void cmd_yaffs_mkdir(const char *dir)
434 int retval = yaffs_mkdir(dir, 0);
437 printf("yaffs_mkdir returning error: %d, %s\n",
438 retval, yaffs_error_str());
441 void cmd_yaffs_rmdir(const char *dir)
443 int retval = yaffs_rmdir(dir);
446 printf("yaffs_rmdir returning error: %d, %s\n",
447 retval, yaffs_error_str());
450 void cmd_yaffs_rm(const char *path)
452 int retval = yaffs_unlink(path);
455 printf("yaffs_unlink returning error: %d, %s\n",
456 retval, yaffs_error_str());
459 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
461 int retval = yaffs_rename(newPath, oldPath);
464 printf("yaffs_unlink returning error: %d, %s\n",
465 retval, yaffs_error_str());