summaryrefslogtreecommitdiff
path: root/recipes/nandlogical/files/nandlogical.c
blob: d88d1c212feb3789e45c60253f485b7b05604868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
   NAND logical utility for Sharp Zaurus SL-C7x0/860/7500/Cxx00
   version 1.0
   Copyright 2006 Alexander Chukov <sash@pdaXrom.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 */

#define _GNU_SOURCE
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <asm/types.h>
#include <mtd/mtd-user.h>

int fd;

unsigned char *readbuf;
unsigned char *oobbuf;
unsigned long *log2phy;

struct mtd_oob_buf oob = {0, 16, NULL};

struct nand_oobinfo none_oobinfo = {
	.useecc = MTD_NANDECC_OFF,
};

#define NAND_LOGICAL_SIZE (7 * 1024 * 1024)

/////////////////////////////////////////////////////////////////////
// oob structure
/////////////////////////////////////////////////////////////////////

#define NAND_NOOB_LOGADDR_00		8
#define NAND_NOOB_LOGADDR_01		9
#define NAND_NOOB_LOGADDR_10		10
#define NAND_NOOB_LOGADDR_11		11
#define NAND_NOOB_LOGADDR_20		12
#define NAND_NOOB_LOGADDR_21		13

static uint nand_get_logical_no(unsigned char *oob)
{
    unsigned short us,bit;
    int par;
    int good0, good1;

    if(oob[NAND_NOOB_LOGADDR_00] == oob[NAND_NOOB_LOGADDR_10] &&
       oob[NAND_NOOB_LOGADDR_01] == oob[NAND_NOOB_LOGADDR_11]){
	good0 = NAND_NOOB_LOGADDR_00;
	good1 = NAND_NOOB_LOGADDR_01;
    }else
    if(oob[NAND_NOOB_LOGADDR_10] == oob[NAND_NOOB_LOGADDR_20] &&
       oob[NAND_NOOB_LOGADDR_11] == oob[NAND_NOOB_LOGADDR_21]){
	good0 = NAND_NOOB_LOGADDR_10;
	good1 = NAND_NOOB_LOGADDR_11;
    }else
    if(oob[NAND_NOOB_LOGADDR_20] == oob[NAND_NOOB_LOGADDR_00] &&
       oob[NAND_NOOB_LOGADDR_21] == oob[NAND_NOOB_LOGADDR_01]){
	good0 = NAND_NOOB_LOGADDR_20;
	good1 = NAND_NOOB_LOGADDR_21;
    }else{
	return (uint)-1;
    }

    us = (((unsigned short)(oob[good0]) & 0x00ff) << 0) |
	 (((unsigned short)(oob[good1]) & 0x00ff) << 8);

    par = 0;
    for(bit = 0x0001; bit != 0; bit <<= 1){
	if(us & bit){
	    par++;
	}
    }
    if(par & 1){
	return (uint)-2;
    }

    if(us == 0xffff){
	return 0xffff;
    }else{
	return ((us & 0x07fe) >> 1);
    }
}

static void nand_set_logical_no(uint log_no, unsigned char *oob)
{
    unsigned short us,bit;
    int par;

    us = (((log_no & 0x03ff) << 1) | 0x1000);

    par = 0;
    for(bit = 0x0001; bit != 0; bit <<= 1){
	if(us & bit){
	    par++;
	}
    }
    if(par & 1){
	us |= 0x0001;
    }

    oob[NAND_NOOB_LOGADDR_00] = (unsigned char)((us & 0x00ff) >> 0);
    oob[NAND_NOOB_LOGADDR_01] = (unsigned char)((us & 0xff00) >> 8);
    oob[NAND_NOOB_LOGADDR_10] = oob[NAND_NOOB_LOGADDR_00];
    oob[NAND_NOOB_LOGADDR_11] = oob[NAND_NOOB_LOGADDR_01];
    oob[NAND_NOOB_LOGADDR_20] = oob[NAND_NOOB_LOGADDR_00];
    oob[NAND_NOOB_LOGADDR_21] = oob[NAND_NOOB_LOGADDR_01];
}

void scan_logical(int blocks, int erasesize)
{
    int i;
    unsigned long offset;
    int ret = 1;
    for (i = 0; i < blocks; i++)
	log2phy[i] = (uint) -1;
    offset = 0;
    for (i = 0; i < blocks; i++) {
	oob.start = offset;
	ret = ioctl(fd, MEMREADOOB, &oob);

	//ret = nand_read_raw(nand, oobuf, offset, nand->writesize, nand->oobsize);
	if (!ret) {
    	    int log_no = nand_get_logical_no(oobbuf);
	    if (((int)log_no >= 0) && (log_no < blocks)) {
		log2phy[log_no] = offset;
		//printf("NAND logical - %08X -> %04X\n", offset, log_no * erasesize);
	    } else {
		//printf("NAND logical - %08X - skip (%x)\n", offset, log_no);
	    }
	} else {
	    //printf("NAND logical - offset %x read OOB problem\n", offset);
	}
	offset += erasesize;
    }
}

unsigned long add_logical(unsigned long ofs, int blocks, int erasesize, int bs)
{
    erase_info_t erase;
    unsigned long offset = 0;
    int i;
    int ret;

    erase.length = erasesize;

    for (i = 0; i < blocks; i++) {
	oob.start = offset;
	ret = ioctl(fd, MEMREADOOB, &oob);

	if (!ret) {
    	    int log_no = nand_get_logical_no(oobbuf);

//printf("-- %x\n", log_no);

	    if ((short)log_no == -1) {
		int j = 0;
		{
		loff_t offs = offset;
		erase.start = offset;
		int ret = ioctl(fd, MEMGETBADBLOCK, &offs);
		if (ret > 0) {
		    printf ("\nSkipping bad block at 0x%08x\n", erase.start);
		    goto nextblock;
		} else if (ret < 0) {
			perror("ioctl(MEMGETBADBLOCK)");
			exit(1);
		} else {
			printf("%x - no bad block\n", offset);
		}
		
		if (ioctl(fd, MEMERASE, &erase) != 0) {
		    perror("ioctl(MEMERASE)");
		    goto nextblock;
		}

		//printf("%x - erased\n", offset);

		}

		//printf("NAND logical - found free block %x, mapped as %x\n", offset, ofs);

		log2phy[ofs / erasesize] = offset;

		return offset;
	    } else {
		//fprintf(stderr, "found: %x\n", log_no);
	    }
	} else {
	    perror ("ioctl(MEMREADOOB)");
	}
    
    nextblock:
        offset += erasesize;
    }
    
    return (unsigned long)-1;
}

void usage(void)
{
	fprintf(stderr, "Usage:\nnandlogical <mtd char device> READ|WRITE <start> <length> <file>\n");
	exit(1);
}

int main(int argc, char *argv[])
{
	mtd_info_t meminfo;
	char *mtddev;
	int blocks;
	unsigned long start_addr;
	unsigned long end_addr;
	unsigned long length;
	unsigned long ofs;
	int bs;
	int ofd;
	int func_write = 0;
	int oobinfochanged = 0;
	struct nand_oobinfo old_oobinfo;

	if (argc < 6)
	    usage();

	if (strcmp(argv[2], "WRITE") == 0)
	    func_write = 1;
	else if (strcmp(argv[2], "READ") != 0)
	    usage();
	
	mtddev = argv[1];
	start_addr = strtoul(argv[3], NULL, 0);
	length = strtoul(argv[4], NULL, 0);

	if (func_write) {
	    if ((ofd = open(argv[5], O_RDONLY)) == -1) {
		perror("open input file");
		exit(1);
	    }
	    /* Open MTD device */
	    if ((fd = open(mtddev, O_RDWR)) == -1) {
	        perror("open flash");
		exit (1);
	    }
	} else {
	    if ((ofd = open(argv[5], O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
		perror ("open outfile");
		exit(1);
	    }
	    /* Open MTD device */
	    if ((fd = open(mtddev, O_RDONLY)) == -1) {
		perror("open flash");
	        exit (1);
	    }
	}

	/* Fill in MTD device capability structure */
	if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
	    perror("MEMGETINFO");
	    close(fd);
	    exit (1);
	}

	/* Make sure device page sizes are valid */
	if (!(meminfo.oobsize == 64 && meminfo.writesize == 2048) &&
	    !(meminfo.oobsize == 16 && meminfo.writesize == 512) &&
	    !(meminfo.oobsize == 8 && meminfo.writesize == 256)) {
	    fprintf(stderr, "Unknown flash (not normal NAND)\n");
	    close(fd);
	    exit(1);
	}

	//printf("erasesize %x\nwritesize %x\noobsize %x\nsize %x\n", meminfo.erasesize, meminfo.writesize, meminfo.oobsize, meminfo.size);
	
	blocks = NAND_LOGICAL_SIZE / meminfo.erasesize;
	log2phy = (unsigned long *) malloc(blocks * sizeof(unsigned long));
	readbuf = (char *)malloc(meminfo.erasesize);
	oobbuf = (char *)malloc(meminfo.writesize);	
	oob.ptr = oobbuf;

	scan_logical(blocks, meminfo.erasesize);
	
	//printf("Start: %x\nEnd: %x\n", start_addr, length);
	
	end_addr = start_addr + length;
	bs = meminfo.writesize;

	for (ofs = start_addr; ofs < end_addr ; ofs+=bs) {
	    int new_logical_added = 0;
	    int offset = log2phy[ofs / meminfo.erasesize];
	    
	    if ((int)offset < 0 && func_write) {
		//printf("add logical block...\n");
		offset = add_logical(ofs, blocks, meminfo.erasesize, bs);
		new_logical_added = 1;
	    }
	    
	    if ((int)offset < 0) {
	        printf("NAND logical - offset %08X not found\n", ofs);
		goto closeall;
	    }

	    offset += ofs % meminfo.erasesize;

	    //printf(":%x\n", offset);

	    if (func_write) {
		int len;
		memset(readbuf, 0xff, bs);
		len = read(ofd, readbuf, bs);
		if (len > 0) {
#if 1
			if (ofs % meminfo.erasesize == 0) {
			    int j;
			    erase_info_t erase;
			    erase.start = offset;
			    erase.length = meminfo.erasesize;
			    if (ioctl(fd, MEMERASE, &erase) != 0) {
				printf("ioctl(MEMERASE) %x\n", offset);
				//goto nextblock;
			    }
			    //printf("Erased\n");

			    for (j = 0; j < meminfo.erasesize; j+=bs) {
				int log_no;
				oob.start = offset + j;
				oob.length = 16;

				memset(oobbuf, 0xff, 16);
				nand_set_logical_no(ofs / meminfo.erasesize, oobbuf);
				if (ioctl(fd, MEMWRITEOOB, &oob) != 0) {
				    //perror ("ioctl(MEMWRITEOOB)");
				    printf("NAND logical add - MEMWRITEOOB error %x\n", offset + j);
				    exit(1);
				}
				memset(oobbuf, 0xff, 16);
				if (ioctl(fd, MEMREADOOB, &oob) != 0) {
				    perror ("ioctl(MEMREADOOB)");
				    exit(1);
				}
				log_no = nand_get_logical_no(oobbuf);
				//printf("%x:1 write %x, read %x\n", offset + j, ofs / meminfo.erasesize, log_no);
			    }

			}
#endif
			if (pwrite(fd, readbuf, bs, offset) != bs) {
				perror ("pwrite");
				goto closeall;
			}

		} else
		    break;
	    } else {
		if (pread(fd, readbuf, bs, offset) != bs) {
		    perror("pread");
		    goto closeall;
		}
		write(ofd, readbuf, ((end_addr - ofs) < bs)?(end_addr - ofs):bs);
	    }
	}

 closeall:

	free(log2phy);
	free(readbuf);
	free(oobbuf);
	close(fd);
	close(ofd);
	
	return 0;
}