Add more descriptive comment for using the yaffs commands.
[yaffs2.git] / direct / u-boot / fs / yaffs2 / yaffscfg.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 #if 0
32 #include <errno.h>
33 #else
34 #include "malloc.h"
35 #endif
36
37 unsigned yaffs_trace_mask = 0x0; /* Disable logging */
38 static int yaffs_errno = 0;
39
40
41
42 void *yaffsfs_malloc(size_t x)
43 {
44         return malloc(x);
45 }
46
47 void yaffsfs_free(void *x)
48 {
49         free(x);
50 }
51
52 void yaffsfs_SetError(int err)
53 {
54         //Do whatever to set error
55         yaffs_errno = err;
56 }
57
58 int yaffsfs_GetLastError(void)
59 {
60         return yaffs_errno;
61 }
62
63
64 int yaffsfs_GetError(void)
65 {
66         return yaffs_errno;
67 }
68
69 void yaffsfs_Lock(void)
70 {
71 }
72
73 void yaffsfs_Unlock(void)
74 {
75 }
76
77 __u32 yaffsfs_CurrentTime(void)
78 {
79         return 0;
80 }
81
82 void *yaffs_malloc(size_t size)
83 {
84         return malloc(size);
85 }
86
87 void yaffs_free(void *ptr)
88 {
89         free(ptr);
90 }
91
92 void yaffsfs_LocalInitialisation(void)
93 {
94         // Define locking semaphore.
95 }
96
97 extern nand_info_t nand_info[];
98
99
100 void cmd_yaffs_devconfig(char *_mp, int flash_dev, int start_block, int end_block)
101 {
102         struct mtd_info *mtd = NULL;
103         struct yaffs_dev *dev;
104         char *mp;
105         
106         dev = calloc(1, sizeof(*dev));
107         mp = strdup(_mp);
108         
109         mtd = &nand_info[flash_dev];
110         if(!dev || !mp) {
111                 /* Alloc error */
112                 return;
113         }
114
115         if(end_block < start_block)
116                 end_block = mtd->size / mtd->erasesize;
117
118         memset(dev, 0, sizeof(*dev));
119         dev->param.name = mp;
120         dev->driver_context = mtd;
121         dev->param.start_block = start_block;
122         dev->param.end_block = end_block;
123         dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
124         dev->param.total_bytes_per_chunk = mtd->writesize;
125         dev->param.is_yaffs2 = 1;
126         dev->param.use_nand_ecc = 1;
127         dev->param.n_reserved_blocks = 5;
128         dev->param.inband_tags = 0;
129         dev->param.n_caches = 10;
130         dev->param.write_chunk_tags_fn = nandmtd2_WriteChunkWithTagsToNAND;
131         dev->param.read_chunk_tags_fn = nandmtd2_ReadChunkWithTagsFromNAND;
132         dev->param.erase_fn = nandmtd_EraseBlockInNAND;
133         dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
134         dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
135         dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
136         
137         yaffs_add_device(dev);
138 }
139         
140
141
142 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
143 {
144         int outh;
145         int i;
146         unsigned char buffer[100];
147
148         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
149         if (outh < 0)
150         {
151                 printf("Error opening file: %d\n", outh);
152                 return;
153         }
154
155         memset(buffer,bval,100);
156
157         do{
158                 i = sizeOfFile;
159                 if(i > 100) i = 100;
160                 sizeOfFile -= i;
161
162                 yaffs_write(outh,buffer,i);
163
164         } while (sizeOfFile > 0);
165
166
167         yaffs_close(outh);
168 }
169
170 void read_a_file(char *fn)
171 {
172         int h;
173         int i = 0;
174         unsigned char b;
175
176         h = yaffs_open(fn, O_RDWR,0);
177         if(h<0)
178         {
179                 printf("File not found\n");
180                 return;
181         }
182
183         while(yaffs_read(h,&b,1)> 0)
184         {
185                 printf("%02x ",b);
186                 i++;
187                 if(i > 32)
188                 {
189                    printf("\n");
190                    i = 0;;
191                  }
192         }
193         printf("\n");
194         yaffs_close(h);
195 }
196
197 void cmd_yaffs_mount(char *mp)
198 {
199         int retval = yaffs_mount(mp);
200         if( retval < 0)
201                 printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
202 }
203
204
205 void cmd_yaffs_umount(char *mp)
206 {
207         if( yaffs_unmount(mp) == -1)
208                 printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
209 }
210
211 void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
212 {
213         make_a_file(yaffsName,bval,sizeOfFile);
214 }
215
216
217 void cmd_yaffs_read_file(char *fn)
218 {
219         read_a_file(fn);
220 }
221
222
223 void cmd_yaffs_mread_file(char *fn, char *addr)
224 {
225         int h;
226         struct yaffs_stat s;
227
228         yaffs_stat(fn,&s);
229
230         printf ("Copy %s to 0x%p... ", fn, addr);
231         h = yaffs_open(fn, O_RDWR,0);
232         if(h<0)
233         {
234                 printf("File not found\n");
235                 return;
236         }
237
238         yaffs_read(h,addr,(int)s.st_size);
239         printf("\t[DONE]\n");
240
241         yaffs_close(h);
242 }
243
244
245 void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
246 {
247         int outh;
248
249         outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
250         if (outh < 0)
251         {
252                 printf("Error opening file: %d\n", outh);
253         }
254
255         yaffs_write(outh,addr,size);
256
257         yaffs_close(outh);
258 }
259
260
261 void cmd_yaffs_ls(const char *mountpt, int longlist)
262 {
263         int i;
264         yaffs_DIR *d;
265         yaffs_dirent *de;
266         struct yaffs_stat stat;
267         char tempstr[255];
268
269         d = yaffs_opendir(mountpt);
270
271         if(!d)
272         {
273                 printf("opendir failed\n");
274         }
275         else
276         {
277                 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
278                 {
279                         if (longlist)
280                         {
281                                 sprintf(tempstr, "%s/%s", mountpt, de->d_name);
282                                 yaffs_stat(tempstr, &stat);
283                                 printf("%-25s\t%7ld\n",de->d_name, stat.st_size);
284                         }
285                         else
286                         {
287                                 printf("%s\n",de->d_name);
288                         }
289                 }
290         }
291 }
292
293
294 void cmd_yaffs_mkdir(const char *dir)
295 {
296         int retval = yaffs_mkdir(dir, 0);
297
298         if ( retval < 0)
299                 printf("yaffs_mkdir returning error: %d\n", retval);
300 }
301
302 void cmd_yaffs_rmdir(const char *dir)
303 {
304         int retval = yaffs_rmdir(dir);
305
306         if ( retval < 0)
307                 printf("yaffs_rmdir returning error: %d\n", retval);
308 }
309
310 void cmd_yaffs_rm(const char *path)
311 {
312         int retval = yaffs_unlink(path);
313
314         if ( retval < 0)
315                 printf("yaffs_unlink returning error: %d\n", retval);
316 }
317
318 void cmd_yaffs_mv(const char *oldPath, const char *newPath)
319 {
320         int retval = yaffs_rename(newPath, oldPath);
321
322         if ( retval < 0)
323                 printf("yaffs_unlink returning error: %d\n", retval);
324 }