*** empty log message ***
[yaffs/.git] / utils / mkyaffs.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * mkyaffs.c Format a chunk of NAND for YAFFS.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  *  Acknowledgement:
15  *  This file is crafted from nandtest.c by  Miguel Freitas (miguel@cetuc.puc-rio.br)
16  *  and Steven J. Hill (sjhill@cotw.com)
17  *
18  * Overview:
19  * Formatting a YAFFS device is very simple. Just erase all undamaged blocks. 
20  * NB Don't erase blocks maked as damaged.
21  */
22
23 #define _GNU_SOURCE
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <asm/types.h>
34 #include <linux/config.h>
35 #include <linux/mtd/mtd.h>
36
37
38 // countBits is a quick way of counting the number of bits in a byte.
39 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
40
41 static const char countBits[256] =
42 {
43 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
44 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
45 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
46 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
47 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
49 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
50 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
51 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
52 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
53 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
54 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
55 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
56 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
57 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
58 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
59 };
60
61 /*
62  * Buffer arrays used for running tests
63  */
64
65 unsigned char oobbuf[16];
66 unsigned char imgpage[528];
67
68
69 /*
70  * Main program
71  */
72 int main(int argc, char **argv)
73 {
74         unsigned long addr;
75         unsigned long offset;
76         int bs, fd, i;
77         int img=-1;
78         int imglen = 0;
79         struct mtd_oob_buf oob = {0, 16, (unsigned char *) &oobbuf};
80         mtd_info_t meminfo;
81         erase_info_t erase;
82
83         /* Make sure a device was specified */
84         if(argc < 2) {
85                 printf("usage: %s <mtdname> [image name]\n", argv[0]);
86                 exit(1);
87         }
88         
89         if(argc > 2 && 
90            (img = open(argv[2],O_RDONLY)) == -1)        {
91                 perror("opening image file");
92                 exit(1);
93         }
94         
95         if(img >= 0){
96            imglen = lseek(img,0,SEEK_END);
97            if(imglen %528){
98                 printf("Image not a multiple of 528 bytes\n");
99                 exit(1);
100            }
101         }
102         
103         lseek(img,0,SEEK_SET);
104
105         /* Open the device */
106         if((fd = open(argv[1], O_RDWR)) == -1) {
107                 perror("opening flash");
108                 exit(1);
109         }
110
111         /* Fill in MTD device capability structure */
112         if(ioctl(fd, MEMGETINFO, &meminfo) != 0) {
113                 perror("MEMGETINFO");
114                 close(fd);
115                 exit(1);
116         }
117
118         /* Make sure device page sizes are valid */
119         if( !(meminfo.oobsize == 16 && meminfo.oobblock == 512)) 
120         {
121                 printf("Unknown flash (not normal NAND)\n");
122                 close(fd);
123                 exit(1);
124         }
125         
126         if(imglen >= 0 &&
127            (imglen/528 +32)*512 > meminfo.size){
128                 printf("Image is too big for NAND\n");
129                 exit(1);
130         }
131         
132         
133         printf("Erasing and programming NAND\n");
134         for(addr = 0; addr < meminfo.size; addr += meminfo.erasesize)
135         {
136                 /* Read the OOB data to determine if the block is valid.
137                  * If the block is damaged, then byte 5 of the OOB data will
138                  * have at least 2 zero bits.
139                  */
140                 oob.start = addr;
141                 oob.length = 16;
142                 oob.ptr = oobbuf;
143                 if (ioctl(fd, MEMREADOOB, &oob) != 0) 
144                 {
145                         perror("ioctl(MEMREADOOB)");
146                         close(fd);
147                         exit(1);
148                 }
149                 
150                 if(countBits[oobbuf[5]] < 7)
151                 {
152                         printf("Block at 0x08%lx is damaged and is not being formatted\n",addr);
153                 }
154                 else
155                 {
156                         /* Erase this block */
157                         erase.start = addr;
158                         erase.length = meminfo.erasesize;
159                         printf("Erasing block at 0x08%lx\n",addr);
160                         if(ioctl(fd, MEMERASE, &erase) != 0) 
161                         {
162                                 perror("\nMTD Erase failure\n");
163                                 close(fd);
164                                 exit(1);
165                         }
166                         
167                         /* Do some programming, but not in the first block */
168                         
169                         if(addr){
170                                 for(offset = 0; offset <meminfo.erasesize; offset+=512)
171                                 {
172                                         if(read(img,imgpage,528) == 528){
173                                                 lseek(fd,addr+offset,SEEK_SET);
174                                                 write(fd,imgpage,512);
175                                                 oob.start = addr+offset;
176                                                 oob.length=16;
177                                                 oob.ptr=&imgpage[512];
178                                                 ioctl(fd,MEMWRITEOOB,&oob);
179                                         }
180                                 }
181                         }
182                         
183                 }
184
185         }
186
187
188
189         /* All the tests succeeded */
190         printf("OK\n");
191         close(fd);
192         return 0;
193 }
194