Merge branch 'driver-refactoring' into new-driver-refactoring
[yaffs2.git] / direct / u-boot / fs / yaffs2 / yaffs_uboot_glue.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
15  * yaffscfg.c  The configuration for the "direct" use of yaffs.
16  *
17  * This is set up for u-boot.
18  *
19  * This version now uses the ydevconfig mechanism to set up partitions.
20  */
21
22 #include <common.h>
23
24 #include <config.h>
25 #include "nand.h"
26 #include "yaffscfg.h"
27 #include "yaffsfs.h"
28 #include "yaffs_packedtags2.h"
29 #include "yaffs_mtdif.h"
30 #include "yaffs_mtdif2.h"
31 #include "yaffs_uboot_glue.h"
32
33 #if 0
34 #include <errno.h>
35 #else
36 #include "malloc.h"
37 #endif
38
39 unsigned yaffs_trace_mask = 0x0; /* Disable logging */
40 static int yaffs_errno;
41
42
43 void yaffs_bug_fn(const char *fn, int n)
44 {
45         printf("yaffs bug at %s:%d\n", fn, n);
46 }
47
48 void *yaffsfs_malloc(size_t x)
49 {
50         return malloc(x);
51 }
52
53 void yaffsfs_free(void *x)
54 {
55         free(x);
56 }
57
58 void yaffsfs_SetError(int err)
59 {
60         yaffs_errno = err;
61 }
62
63 int yaffsfs_GetLastError(void)
64 {
65         return yaffs_errno;
66 }
67
68
69 int yaffsfs_GetError(void)
70 {
71         return yaffs_errno;
72 }
73
74 void yaffsfs_Lock(void)
75 {
76 }
77
78 void yaffsfs_Unlock(void)
79 {
80 }
81
82 __u32 yaffsfs_CurrentTime(void)
83 {
84         return 0;
85 }
86
87 void *yaffs_malloc(size_t size)
88 {
89         return malloc(size);
90 }
91
92 void yaffs_free(void *ptr)
93 {
94         free(ptr);
95 }
96
97 void yaffsfs_LocalInitialisation(void)
98 {
99         /* No locking used */
100 }
101
102
103 static const char *yaffs_file_type_str(struct yaffs_stat *stat)
104 {
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";
110         }
111 }
112
113 static const char *yaffs_error_str(void)
114 {
115         int error = yaffsfs_GetLastError();
116
117         if (error < 0)
118                 error = -error;
119
120         switch (error) {
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";
142         }
143 }
144
145 extern nand_info_t nand_info[];
146
147 int cmd_yaffs_tracemask(unsigned set, unsigned mask)
148 {
149         if (set)
150                 yaffs_trace_mask = mask;
151
152         printf("yaffs trace mask: %08x\n", yaffs_trace_mask);
153         return 0;
154 }
155
156 static int yaffs_regions_overlap(int a, int b, int x, int y)
157 {
158         return  (a <= x && x <= b) ||
159                 (a <= y && y <= b) ||
160                 (x <= a && a <= y) ||
161                 (x <= b && b <= y);
162 }
163
164 int cmd_yaffs_devconfig(char *_mp, int flash_dev,
165                         int start_block, int end_block)
166 {
167         struct mtd_info *mtd = NULL;
168         struct yaffs_dev *dev = NULL;
169         struct yaffs_dev *chk;
170         char *mp = NULL;
171         struct nand_chip *chip;
172
173         dev = calloc(1, sizeof(*dev));
174         mp = strdup(_mp);
175
176         mtd = &nand_info[flash_dev];
177
178         if (!dev || !mp) {
179                 /* Alloc error */
180                 printf("Failed to allocate memory\n");
181                 goto err;
182         }
183
184         if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
185                 printf("Flash device invalid\n");
186                 goto err;
187         }
188
189         if (end_block == 0)
190                 end_block = mtd->size / mtd->erasesize - 1;
191
192         if (end_block < start_block) {
193                 printf("Bad start/end\n");
194                 goto err;
195         }
196
197         chip =  mtd->priv;
198
199         /* Check for any conflicts */
200         yaffs_dev_rewind();
201         while (1) {
202                 chk = yaffs_next_dev();
203                 if (!chk)
204                         break;
205                 if (strcmp(chk->param.name, mp) == 0) {
206                         printf("Mount point name already used\n");
207                         goto err;
208                 }
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",
214                                 chk->param.name);
215                         goto err;
216                 }
217
218         }
219
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;
240
241         yaffs_add_device(dev);
242
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" : "");
246         return 0;
247
248 err:
249         free(dev);
250         free(mp);
251         return -1;
252 }
253
254 int cmd_yaffs_dev_ls(void)
255 {
256         struct yaffs_dev *dev;
257         int flash_dev;
258         int free_space;
259
260         yaffs_dev_rewind();
261
262         while (1) {
263                 dev = yaffs_next_dev();
264                 if (!dev)
265                         break;
266                 flash_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, " : "");
273
274                 free_space = yaffs_freespace(dev->param.name);
275                 if (free_space < 0)
276                         printf("not mounted\n");
277                 else
278                         printf("free 0x%x\n", free_space);
279
280         }
281
282         return 0;
283 }
284
285 int make_a_file(char *yaffsName, char bval, int sizeOfFile)
286 {
287         int outh;
288         int i;
289         int written;
290         unsigned char buffer[100];
291
292         outh = yaffs_open(yaffsName,
293                                 O_CREAT | O_RDWR | O_TRUNC,
294                                 S_IREAD | S_IWRITE);
295         if (outh < 0) {
296                 printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
297                 return -1;
298         }
299
300         memset(buffer, bval, 100);
301
302         written = 0;
303         while (written < sizeOfFile) {
304                 i = sizeOfFile - written;
305                 if (i > 100)
306                         i = 100;
307
308                 if (yaffs_write(outh, buffer, i) != i)
309                         break;
310                 written += i;
311         }
312
313         yaffs_close(outh);
314
315         return (written == sizeOfFile) ? 0 : -1;
316 }
317
318 int read_a_file(char *fn)
319 {
320         int h;
321         int i = 0;
322         unsigned char b;
323
324         h = yaffs_open(fn, O_RDWR, 0);
325         if (h < 0) {
326                 printf("File not found\n");
327                 return -1;
328         }
329
330         while (yaffs_read(h, &b, 1) > 0) {
331                 printf("%02x ", b);
332                 i++;
333                 if (i > 32) {
334                         printf("\n");
335                         i = 0;;
336                 }
337         }
338         printf("\n");
339         yaffs_close(h);
340
341         return 0;
342 }
343
344 int cmd_yaffs_mount(char *mp)
345 {
346         int retval = yaffs_mount(mp);
347         if (retval < 0)
348                 printf("Error mounting %s, return value: %d, %s\n", mp,
349                         yaffsfs_GetError(), yaffs_error_str());
350         return retval;
351 }
352
353
354 int cmd_yaffs_umount(char *mp)
355 {
356         int retval = yaffs_unmount(mp);
357
358         if (retval < 0)
359                 printf("Error umounting %s, return value: %d, %s\n", mp,
360                         yaffsfs_GetError(), yaffs_error_str());
361         return retval;
362 }
363
364 int cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
365 {
366         return make_a_file(yaffsName, bval, sizeOfFile);
367 }
368
369
370 int cmd_yaffs_read_file(char *fn)
371 {
372         return read_a_file(fn);
373 }
374
375
376 int cmd_yaffs_mread_file(char *fn, char *addr)
377 {
378         int h;
379         int retval = 0;
380         struct yaffs_stat s;
381         int read_size;
382         char buf[16];
383
384         yaffs_stat(fn, &s);
385
386         printf("Copy %s to 0x%p... ", fn, addr);
387         h = yaffs_open(fn, O_RDWR, 0);
388         if (h < 0) {
389                 printf("File not found\n");
390                 retval = -1;
391                 read_size = 0;
392         } else {
393                 read_size = (int) s.st_size;
394                 yaffs_read(h, addr, read_size);
395                 printf("\t[DONE]\n");
396
397                 yaffs_close(h);
398         }
399         sprintf(buf,"%x", read_size);
400         setenv("filesize", buf);
401
402         return retval;
403 }
404
405
406 int cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
407 {
408         int outh;
409         int wrote;
410
411         outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
412         if (outh < 0) {
413                 printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
414                 return -1;
415         }
416
417         wrote = yaffs_write(outh, addr, size);
418         yaffs_close(outh);
419
420         if(wrote != size) {
421                 printf("only wrote %d (0x%x) bytes\n", wrote, wrote);
422                 return -1;
423         }
424         return 0;
425
426
427 }
428
429
430 int cmd_yaffs_ls(const char *mountpt, int longlist)
431 {
432         int i;
433         yaffs_DIR *d;
434         struct yaffs_dirent *de;
435         struct yaffs_stat stat;
436         char tempstr[255];
437
438         d = yaffs_opendir(mountpt);
439
440         if (!d) {
441                 printf("opendir failed, %s\n", yaffs_error_str());
442                 return -1;
443         }
444
445         for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
446                 if (longlist) {
447                         sprintf(tempstr, "%s/%s", mountpt, de->d_name);
448                         yaffs_lstat(tempstr, &stat);
449                         printf("%-25s\t%7ld",
450                                         de->d_name,
451                                         (long)stat.st_size);
452                         printf(" %5d %s\n",
453                                         stat.st_ino,
454                                         yaffs_file_type_str(&stat));
455                 } else {
456                         printf("%s\n", de->d_name);
457                 }
458         }
459
460         yaffs_closedir(d);
461
462         return 0;
463 }
464
465 int cmd_yaffs_check(const char *fname, const char *type)
466 {
467         int retval = 0;
468         int ret;
469         struct yaffs_stat stat;
470
471         ret = yaffs_stat(fname, &stat);
472         if (ret < 0) {
473                 printf("%s not found\n", fname);
474                 return -1;
475         }
476
477         printf("%s is a %s\n", fname, yaffs_file_type_str(&stat));
478
479         if (strcmp(type, "REG") == 0 &&
480             (stat.st_mode & S_IFMT) != S_IFREG)
481                 retval = -1;
482
483         if (strcmp(type, "DIR") == 0 &&
484             (stat.st_mode & S_IFMT) != S_IFDIR)
485                 retval = -1;
486
487         if (retval == 0)
488                 printf("check ok\n");
489         else
490                 printf("check failed\n");
491
492         return retval;
493 }
494
495
496 int cmd_yaffs_mkdir(const char *dir)
497 {
498         int retval = yaffs_mkdir(dir, 0);
499
500         if (retval < 0) {
501                 printf("yaffs_mkdir returning error: %d, %s\n",
502                         retval, yaffs_error_str());
503                 return -1;
504         }
505         return 0;
506 }
507
508 int cmd_yaffs_rmdir(const char *dir)
509 {
510         int retval = yaffs_rmdir(dir);
511
512         if (retval < 0) {
513                 printf("yaffs_rmdir returning error: %d, %s\n",
514                         retval, yaffs_error_str());
515                 return -1;
516         }
517         return 0;
518 }
519
520 int cmd_yaffs_rm(const char *path)
521 {
522         int retval = yaffs_unlink(path);
523
524         if (retval < 0) {
525                 printf("yaffs_unlink returning error: %d, %s\n",
526                         retval, yaffs_error_str());
527                 return -1;
528         }
529
530         return 0;
531 }
532
533 int cmd_yaffs_mv(const char *oldPath, const char *newPath)
534 {
535         int retval = yaffs_rename(newPath, oldPath);
536
537         if (retval < 0) {
538                 printf("yaffs_unlink returning error: %d, %s\n",
539                         retval, yaffs_error_str());
540                 return -1;
541         }
542
543         return 0;
544 }