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.
6 //config:config FLASH_LOCK
7 //config: bool "flash_lock"
8 //config: default n # doesn't build on Ubuntu 8.04
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.
13 //config:config FLASH_UNLOCK
14 //config: bool "flash_unlock"
15 //config: default n # doesn't build on Ubuntu 8.04
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.
20 //applet:IF_FLASH_LOCK(APPLET_ODDNAME(flash_lock, flash_lock_unlock, BB_DIR_USR_SBIN, BB_SUID_DROP, flash_lock))
21 //applet:IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, BB_DIR_USR_SBIN, BB_SUID_DROP, flash_unlock))
23 //kbuild:lib-$(CONFIG_FLASH_LOCK) += flash_lock_unlock.o
24 //kbuild:lib-$(CONFIG_FLASH_UNLOCK) += flash_lock_unlock.o
26 //usage:#define flash_lock_trivial_usage
27 //usage: "MTD_DEVICE OFFSET SECTORS"
28 //usage:#define flash_lock_full_usage "\n\n"
29 //usage: "Lock part or all of an MTD device. If SECTORS is -1, then all sectors\n"
30 //usage: "will be locked, regardless of the value of OFFSET"
32 //usage:#define flash_unlock_trivial_usage
34 //usage:#define flash_unlock_full_usage "\n\n"
35 //usage: "Unlock an MTD device"
38 #include <mtd/mtd-user.h>
40 int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
43 /* note: fields in these structs are 32-bits.
44 * apparently we can't win anything by using off_t
45 * or long long's for offset and/or sectors vars. */
46 struct mtd_info_user info;
47 struct erase_info_user lock;
52 #define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
57 /* parse offset and number of sectors to lock */
61 if (!argv[2] || !argv[3])
63 offset = xstrtoul(argv[2], 0);
64 sectors = xstrtol(argv[3], 0);
67 fd = xopen(argv[1], O_RDWR);
69 xioctl(fd, MEMGETINFO, &info);
72 lock.length = info.size;
74 unsigned long size = info.size - info.erasesize;
76 bb_error_msg_and_die("%lx is beyond device size %lx\n",
81 sectors = info.size / info.erasesize;
83 // isn't this useless?
84 unsigned long num = info.size / info.erasesize;
86 bb_error_msg_and_die("%ld are too many "
87 "sectors, device only has "
88 "%ld\n", sectors, num);
93 lock.length = sectors * info.erasesize;
94 xioctl(fd, MEMLOCK, &lock);
96 xioctl(fd, MEMUNLOCK, &lock);