1 /* vi: set sw=4 ts=4: */
2 /* Ported to busybox from mtd-utils.
4 * Licensed under GPLv2, see file LICENSE in this source tree.
7 //usage:#define flash_lock_trivial_usage
8 //usage: "MTD_DEVICE OFFSET SECTORS"
9 //usage:#define flash_lock_full_usage "\n\n"
10 //usage: "Lock part or all of an MTD device. If SECTORS is -1, then all sectors\n"
11 //usage: "will be locked, regardless of the value of OFFSET"
13 //usage:#define flash_unlock_trivial_usage
15 //usage:#define flash_unlock_full_usage "\n\n"
16 //usage: "Unlock an MTD device"
19 #include <mtd/mtd-user.h>
21 int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22 int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
24 /* note: fields in these structs are 32-bits.
25 * apparently we can't win anything by using off_t
26 * or long long's for offset and/or sectors vars. */
27 struct mtd_info_user info;
28 struct erase_info_user lock;
33 #define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
38 /* parse offset and number of sectors to lock */
42 if (!argv[2] || !argv[3])
44 offset = xstrtoul(argv[2], 0);
45 sectors = xstrtol(argv[3], 0);
48 fd = xopen(argv[1], O_RDWR);
50 xioctl(fd, MEMGETINFO, &info);
53 lock.length = info.size;
55 unsigned long size = info.size - info.erasesize;
57 bb_error_msg_and_die("%lx is beyond device size %lx\n",
62 sectors = info.size / info.erasesize;
64 // isn't this useless?
65 unsigned long num = info.size / info.erasesize;
67 bb_error_msg_and_die("%ld are too many "
68 "sectors, device only has "
69 "%ld\n", sectors, num);
74 lock.length = sectors * info.erasesize;
75 xioctl(fd, MEMLOCK, &lock);
77 xioctl(fd, MEMUNLOCK, &lock);