287abaafd33afbc9a7104e4a92b1b79a8aa53ec0
[oweals/busybox.git] / miscutils / flash_lock_unlock.c
1 /* vi: set sw=4 ts=4: */
2 /* Ported to busybox from mtd-utils.
3  *
4  * Licensed under GPLv2, see file LICENSE in this source tree.
5  */
6 //config:config FLASH_LOCK
7 //config:       bool "flash_lock"
8 //config:       default n  # doesn't build on Ubuntu 8.04
9 //config:       help
10 //config:         The flash_lock binary from mtd-utils as of git head 5ec0c10d0. This
11 //config:         utility locks part or all of the flash device.
12 //config:
13 //config:config FLASH_UNLOCK
14 //config:       bool "flash_unlock"
15 //config:       default n  # doesn't build on Ubuntu 8.04
16 //config:       help
17 //config:         The flash_unlock binary from mtd-utils as of git head 5ec0c10d0. This
18 //config:         utility unlocks part or all of the flash device.
19
20 //usage:#define flash_lock_trivial_usage
21 //usage:       "MTD_DEVICE OFFSET SECTORS"
22 //usage:#define flash_lock_full_usage "\n\n"
23 //usage:       "Lock part or all of an MTD device. If SECTORS is -1, then all sectors\n"
24 //usage:       "will be locked, regardless of the value of OFFSET"
25 //usage:
26 //usage:#define flash_unlock_trivial_usage
27 //usage:       "MTD_DEVICE"
28 //usage:#define flash_unlock_full_usage "\n\n"
29 //usage:       "Unlock an MTD device"
30
31 #include "libbb.h"
32 #include <mtd/mtd-user.h>
33
34 int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
35 int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
36 {
37         /* note: fields in these structs are 32-bits.
38          * apparently we can't win anything by using off_t
39          * or long long's for offset and/or sectors vars. */
40         struct mtd_info_user info;
41         struct erase_info_user lock;
42         unsigned long offset;
43         long sectors;
44         int fd;
45
46 #define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
47
48         if (!argv[1])
49                 bb_show_usage();
50
51         /* parse offset and number of sectors to lock */
52         offset = 0;
53         sectors = -1;
54         if (do_lock) {
55                 if (!argv[2] || !argv[3])
56                         bb_show_usage();
57                 offset = xstrtoul(argv[2], 0);
58                 sectors = xstrtol(argv[3], 0);
59         }
60
61         fd = xopen(argv[1], O_RDWR);
62
63         xioctl(fd, MEMGETINFO, &info);
64
65         lock.start = 0;
66         lock.length = info.size;
67         if (do_lock) {
68                 unsigned long size = info.size - info.erasesize;
69                 if (offset > size) {
70                         bb_error_msg_and_die("%lx is beyond device size %lx\n",
71                                         offset, size);
72                 }
73
74                 if (sectors == -1) {
75                         sectors = info.size / info.erasesize;
76                 } else {
77 // isn't this useless?
78                         unsigned long num = info.size / info.erasesize;
79                         if (sectors > num) {
80                                 bb_error_msg_and_die("%ld are too many "
81                                                 "sectors, device only has "
82                                                 "%ld\n", sectors, num);
83                         }
84                 }
85
86                 lock.start = offset;
87                 lock.length = sectors * info.erasesize;
88                 xioctl(fd, MEMLOCK, &lock);
89         } else {
90                 xioctl(fd, MEMUNLOCK, &lock);
91         }
92
93         return EXIT_SUCCESS;
94 }