package/mtd-utils: update to 1.4.5
[packages.git] / utils / mtd-utils / patches / 100-mtd-debug-add-bad-block-counter.patch
1 --- a/mtd_debug.c
2 +++ b/mtd_debug.c
3 @@ -238,6 +238,7 @@ int showinfo (int fd)
4         int i,err,n;
5         struct mtd_info_user mtd;
6         static struct region_info_user region[1024];
7 +       int iNumOfBadBlocks = 0;
8  
9         err = getmeminfo (fd,&mtd);
10         if (err < 0)
11 @@ -330,6 +331,11 @@ int showinfo (int fd)
12         printf ("\nmtd.oobsize = ");
13         printsize (mtd.oobsize);
14  
15 +       printf ("\nmtd.badblockscount = ");
16 +       iNumOfBadBlocks = get_bb_number(fd, &mtd);
17 +       if (iNumOfBadBlocks > -1)
18 +               printf ("%d", iNumOfBadBlocks);
19 +
20         printf ("\n"
21                         "regions = %d\n"
22                         "\n",
23 @@ -349,6 +355,50 @@ int showinfo (int fd)
24         return (0);
25  }
26  
27 +int get_bb_number(int fd, struct mtd_info_user *meminfo)
28 +{
29 +       int isNAND = (meminfo->type == MTD_NANDFLASH);
30 +       int ibbCounter = 0;
31 +       /* Last 4 blocks of any MTD device are protected and
32 +          MTD reports them as badblocks. */
33 +       int usablesize = meminfo->size - (4 * meminfo->erasesize);
34 +       erase_info_t erase;
35 +       erase.length = meminfo->erasesize;
36 +
37 +       for (erase.start = 0;
38 +            erase.start < usablesize;
39 +            erase.start += meminfo->erasesize)
40 +       {
41 +               loff_t offset = erase.start;
42 +               int ret = ioctl(fd, MEMGETBADBLOCK, &offset);
43 +
44 +               if (ret > 0)
45 +               {
46 +                       ibbCounter++;
47 +                       continue;
48 +               }
49 +               else if (ret < 0)
50 +               {
51 +                       if (errno == EOPNOTSUPP)
52 +                       {
53 +                               if (isNAND)
54 +                               {
55 +                                       printf("Bad block check not available");
56 +                                       return -1;
57 +                               }
58 +                       }
59 +                       else
60 +                       {
61 +                               printf("MTD get bad block failed: %s",
62 +                                       strerror(errno));
63 +                               return -1;
64 +                       }
65 +               }
66 +       }
67 +
68 +       return ibbCounter;
69 +}
70 +
71  void showusage(void)
72  {
73         fprintf (stderr,