OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / mtd-utils / flash_unlock.c
1 /*
2  * FILE flash_unlock.c
3  *
4  * This utility unlock all sectors of flash device.
5  *
6  */
7
8 #define PROGRAM_NAME "flash_unlock"
9
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <time.h>
15 #include <sys/ioctl.h>
16 #include <sys/mount.h>
17 #include <string.h>
18
19 #include <mtd/mtd-user.h>
20
21 int main(int argc, char *argv[])
22 {
23         int fd;
24         struct mtd_info_user mtdInfo;
25         struct erase_info_user mtdLockInfo;
26         int count;
27
28         /*
29          * Parse command line options
30          */
31         if(argc < 2)
32         {
33                 fprintf(stderr, "USAGE: %s <mtd device> <offset in hex> <block count in decimal number>\n", PROGRAM_NAME);
34                 exit(1);
35         }
36         else if(strncmp(argv[1], "/dev/mtd", 8) != 0)
37         {
38                 fprintf(stderr, "'%s' is not a MTD device.  Must specify mtd device: /dev/mtd?\n", argv[1]);
39                 exit(1);
40         }
41
42         fd = open(argv[1], O_RDWR);
43         if(fd < 0)
44         {
45                 fprintf(stderr, "Could not open mtd device: %s\n", argv[1]);
46                 exit(1);
47         }
48
49         if(ioctl(fd, MEMGETINFO, &mtdInfo))
50         {
51                 fprintf(stderr, "Could not get MTD device info from %s\n", argv[1]);
52                 close(fd);
53                 exit(1);
54         }
55
56         if (argc > 2)
57                 mtdLockInfo.start = strtol(argv[2], NULL, 0);
58         else
59                 mtdLockInfo.start = 0;
60
61         if (argc > 3) {
62                 count = strtol(argv[3], NULL, 0);
63                 mtdLockInfo.length = mtdInfo.erasesize * count;
64         } else {
65                 mtdLockInfo.length = mtdInfo.size - mtdInfo.erasesize;
66         }
67
68         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo))
69         {
70                 fprintf(stderr, "Could not unlock MTD device: %s\n", argv[1]);
71                 close(fd);
72                 exit(1);
73         }
74
75         return 0;
76 }