summaryrefslogtreecommitdiff
path: root/mtd_debug.c
blob: 2a24177d5be91ef9c72ff683bf329a975a15f8c9 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

/*
 * Copyright (c) 2d3D, Inc.
 * Written by Abraham vd Merwe <abraham@2d3d.co.za>
 * All rights reserved.
 *
 * $Id: mtd_debug.c,v 1.5 2004/05/05 11:57:55 dwmw2 Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of other contributors
 *	  may be used to endorse or promote products derived from this software
 *	  without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mtd/mtd-user.h>

/*
 * MEMGETINFO
 */
static int getmeminfo (int fd,struct mtd_info_user *mtd)
{
   return (ioctl (fd,MEMGETINFO,mtd));
}

/*
 * MEMERASE
 */
static int memerase (int fd,struct erase_info_user *erase)
{
   return (ioctl (fd,MEMERASE,erase));
}

/*
 * MEMGETREGIONCOUNT
 * MEMGETREGIONINFO
 */
static int getregions (int fd,struct region_info_user *regions,int *n)
{
   int i,err;
   err = ioctl (fd,MEMGETREGIONCOUNT,n);
   if (err) return (err);
   for (i = 0; i < *n; i++)
	 {
		regions[i].regionindex = i;
		err = ioctl (fd,MEMGETREGIONINFO,&regions[i]);
		if (err) return (err);
	 }
   return (0);
}

int erase_flash (int fd,u_int32_t offset,u_int32_t bytes)
{
   int err;
   struct erase_info_user erase;
   erase.start = offset;
   erase.length = bytes;
   err = memerase (fd,&erase);
   if (err < 0)
	 {
		perror ("MEMERASE");
		return (1);
	 }
   fprintf (stderr,"Erased %d bytes from address 0x%.8x in flash\n",bytes,offset);
   return (0);
}

void printsize (u_int32_t x)
{
   int i;
   static const char *flags = "KMGT";
   printf ("%u ",x);
   for (i = 0; x >= 1024 && flags[i] != '\0'; i++) x /= 1024;
   i--;
   if (i >= 0) printf ("(%u%c)",x,flags[i]);
}

int flash_to_file (int fd,u_int32_t offset,size_t len,const char *filename)
{
   u_int8_t *buf = NULL;
   int outfd,err;
   int size = len * sizeof (u_int8_t);
   int n = len;

   if (offset != lseek (fd,offset,SEEK_SET))
	 {
		perror ("lseek()");
		goto err0;
	 }
   outfd = creat (filename,O_WRONLY);
   if (outfd < 0)
	 {
		perror ("creat()");
		goto err1;
	 }

retry:
   if ((buf = (u_int8_t *) malloc (size)) == NULL)
	 {
#define BUF_SIZE	(64 * 1024 * sizeof (u_int8_t))
		fprintf (stderr, "%s: malloc(%#x)\n", __FUNCTION__, size);
		if (size != BUF_SIZE) {
			size = BUF_SIZE;
			fprintf (stderr, "%s: trying buffer size %#x\n", __FUNCTION__, size);
			goto retry;
		}
		perror ("malloc()");
		goto err0;
	 }
 do {
   if (n <= size)
	   size = n;
   err = read (fd,buf,size);
   if (err < 0)
	 {
		fprintf (stderr, "%s: read, size %#x, n %#x\n", __FUNCTION__, size, n);
		perror ("read()");
		goto err2;
	 }
   err = write (outfd,buf,size);
   if (err < 0)
	 {
		fprintf (stderr, "%s: write, size %#x, n %#x\n", __FUNCTION__, size, n);
		perror ("write()");
		goto err2;
	 }
   if (err != size)
	 {
		fprintf (stderr,"Couldn't copy entire buffer to %s. (%d/%d bytes copied)\n",filename,err,size);
		goto err2;
	 }
   n -= size;
   } while (n > 0);

   if (buf != NULL)
   free (buf);
   close (outfd);
   printf ("Copied %d bytes from address 0x%.8x in flash to %s\n",len,offset,filename);
   return (0);

   err2:
   close (outfd);
   err1:
   if (buf != NULL)
   free (buf);
   err0:
   return (1);
}

int file_to_flash (int fd,u_int32_t offset,u_int32_t len,const char *filename)
{
   u_int8_t *buf = NULL;
   FILE *fp;
   int err;
   int size = len * sizeof (u_int8_t);
   int n = len;

   if (offset != lseek (fd,offset,SEEK_SET))
	 {
		perror ("lseek()");
		return (1);
	 }
   if ((fp = fopen (filename,"r")) == NULL)
	 {
		perror ("fopen()");
		return (1);
	 }
retry:
   if ((buf = (u_int8_t *) malloc (size)) == NULL)
	 {
		fprintf (stderr, "%s: malloc(%#x) failed\n", __FUNCTION__, size);
		if (size != BUF_SIZE) {
			size = BUF_SIZE;
			fprintf (stderr, "%s: trying buffer size %#x\n", __FUNCTION__, size);
			goto retry;
		}
		perror ("malloc()");
		fclose (fp);
		return (1);
	 }
 do {
   if (n <= size)
	   size = n;
   if (fread (buf,size,1,fp) != 1 || ferror (fp))
	 {
		fprintf (stderr, "%s: fread, size %#x, n %#x\n", __FUNCTION__, size, n);
		perror ("fread()");
		free (buf);
		fclose (fp);
		return (1);
	 }
   err = write (fd,buf,size);
   if (err < 0)
	 {
		fprintf (stderr, "%s: write, size %#x, n %#x\n", __FUNCTION__, size, n);
		perror ("write()");
		free (buf);
		fclose (fp);
		return (1);
	 }
   n -= size;
} while (n > 0);

   if (buf != NULL)
   free (buf);
   fclose (fp);
   printf ("Copied %d bytes from %s to address 0x%.8x in flash\n",len,filename,offset);
   return (0);
}

int showinfo (int fd)
{
   int i,err,n;
   struct mtd_info_user mtd;
   static struct region_info_user region[1024];

   err = getmeminfo (fd,&mtd);
   if (err < 0)
	 {
		perror ("MEMGETINFO");
		return (1);
	 }

   err = getregions (fd,region,&n);
   if (err < 0)
	 {
		perror ("MEMGETREGIONCOUNT");
		return (1);
	 }

   printf ("mtd.type = ");
   switch (mtd.type)
	 {
	  case MTD_ABSENT:
		printf ("MTD_ABSENT");
		break;
	  case MTD_RAM:
		printf ("MTD_RAM");
		break;
	  case MTD_ROM:
		printf ("MTD_ROM");
		break;
	  case MTD_NORFLASH:
		printf ("MTD_NORFLASH");
		break;
	  case MTD_NANDFLASH:
		printf ("MTD_NANDFLASH");
		break;
	  default:
		printf ("(unknown type - new MTD API maybe?)");
	 }

   printf ("\nmtd.flags = ");
   if (mtd.flags == MTD_CAP_ROM)
	 printf ("MTD_CAP_ROM");
   else if (mtd.flags == MTD_CAP_RAM)
	 printf ("MTD_CAP_RAM");
   else if (mtd.flags == MTD_CAP_NORFLASH)
	 printf ("MTD_CAP_NORFLASH");
   else if (mtd.flags == MTD_CAP_NANDFLASH)
	 printf ("MTD_CAP_NANDFLASH");
   else if (mtd.flags == MTD_WRITEABLE)
	 printf ("MTD_WRITEABLE");
   else
	 {
		int first = 1;
		static struct
		  {
			 const char *name;
			 int value;
		  } flags[] =
		  {
			 { "MTD_CLEAR_BITS", MTD_CLEAR_BITS },
			 { "MTD_SET_BITS", MTD_SET_BITS },
			 { "MTD_ECC", MTD_ECC },
			 { NULL, -1 }
		  };
		for (i = 0; flags[i].name != NULL; i++)
		  if (mtd.flags & flags[i].value)
			{
			   if (first)
				 {
					printf (flags[i].name);
					first = 0;
				 }
			   else printf (" | %s",flags[i].name);
			}
	 }

   printf ("\nmtd.size = ");
   printsize (mtd.size);

   printf ("\nmtd.erasesize = ");
   printsize (mtd.erasesize);

   printf ("\nmtd.writesize = ");
   printsize (mtd.writesize);

   printf ("\nmtd.oobsize = ");
   printsize (mtd.oobsize);

   printf ("\nmtd.ecctype = ");
   switch (mtd.ecctype)
	 {
	  case MTD_ECC_NONE:
		printf ("MTD_ECC_NONE");
		break;
	  case MTD_ECC_RS_DiskOnChip:
		printf ("MTD_ECC_RS_DiskOnChip");
		break;
	  case MTD_ECC_SW:
		printf ("MTD_ECC_SW");
		break;
	  default:
		printf ("(unknown ECC type - new MTD API maybe?)");
	 }

   printf ("\n"
		   "regions = %d\n"
		   "\n",
		   n);

   for (i = 0; i < n; i++)
	 {
		printf ("region[%d].offset = 0x%.8x\n"
				"region[%d].erasesize = ",
				i,region[i].offset,i);
		printsize (region[i].erasesize);
		printf ("\nregion[%d].numblocks = %d\n"
				"region[%d].regionindex = %d\n",
				i,region[i].numblocks,
				i,region[i].regionindex);
	 }
   return (0);
}

void showusage (const char *progname)
{
   fprintf (stderr,
			"usage: %s info <device>\n"
			"       %s read <device> <offset> <len> <dest-filename>\n"
			"       %s write <device> <offset> <len> <source-filename>\n"
			"       %s erase <device> <offset> <len>\n",
			progname,
			progname,
			progname,
			progname);
   exit (1);
}

#define OPT_INFO	1
#define OPT_READ	2
#define OPT_WRITE	3
#define OPT_ERASE	4

int main (int argc,char *argv[])
{
   const char *progname;
   int err = 0,fd,option = OPT_INFO;
   int open_flag;
   (progname = strrchr (argv[0],'/')) ? progname++ : (progname = argv[0]);

   /* parse command-line options */
   if (argc == 3 && !strcmp (argv[1],"info"))
	 option = OPT_INFO;
   else if (argc == 6 && !strcmp (argv[1],"read"))
	 option = OPT_READ;
   else if (argc == 6 && !strcmp (argv[1],"write"))
	 option = OPT_WRITE;
   else if (argc == 5 && !strcmp (argv[1],"erase"))
	 option = OPT_ERASE;
   else
	 showusage (progname);

   /* open device */
   open_flag = (option==OPT_INFO || option==OPT_READ) ? O_RDONLY : O_RDWR;
   if ((fd = open (argv[2],O_SYNC | open_flag)) < 0)
	 {
		perror ("open()");
		exit (1);
	 }

   switch (option)
	 {
	  case OPT_INFO:
		showinfo (fd);
		break;
	  case OPT_READ:
		err = flash_to_file (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0),argv[5]);
		break;
	  case OPT_WRITE:
		err = file_to_flash (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0),argv[5]);
		break;
	  case OPT_ERASE:
		err = erase_flash (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0));
		break;
	 }

   /* close device */
   if (close (fd) < 0)
	 perror ("close()");

   exit (err);
}