blob: 6f2c049f4a29bc5339fc48b7fba8a2549412356b [file] [log] [blame]
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +02001/* vi: set sw=4 ts=4: */
2/* Ported to busybox from mtd-utils.
3 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +02005 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01006//config:config FLASH_LOCK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02007//config: bool "flash_lock (2.1 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +01008//config: default n # doesn't build on Ubuntu 8.04
9//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020010//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.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010012//config:
13//config:config FLASH_UNLOCK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020014//config: bool "flash_unlock (1.3 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010015//config: default n # doesn't build on Ubuntu 8.04
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//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.
Pere Orga5bc8c002011-04-11 03:29:49 +020019
Denys Vlasenko205d48e2017-01-29 14:57:33 +010020// APPLET_ODDNAME:name main location suid_type help
21//applet:IF_FLASH_LOCK( APPLET_ODDNAME(flash_lock, flash_lock_unlock, BB_DIR_USR_SBIN, BB_SUID_DROP, flash_lock))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010022//applet:IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, BB_DIR_USR_SBIN, BB_SUID_DROP, flash_unlock))
Denys Vlasenko798b9452017-08-07 16:00:25 +020023/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010024
25//kbuild:lib-$(CONFIG_FLASH_LOCK) += flash_lock_unlock.o
26//kbuild:lib-$(CONFIG_FLASH_UNLOCK) += flash_lock_unlock.o
27
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage:#define flash_lock_trivial_usage
29//usage: "MTD_DEVICE OFFSET SECTORS"
30//usage:#define flash_lock_full_usage "\n\n"
31//usage: "Lock part or all of an MTD device. If SECTORS is -1, then all sectors\n"
32//usage: "will be locked, regardless of the value of OFFSET"
33//usage:
34//usage:#define flash_unlock_trivial_usage
35//usage: "MTD_DEVICE"
36//usage:#define flash_unlock_full_usage "\n\n"
37//usage: "Unlock an MTD device"
38
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +020039#include "libbb.h"
40#include <mtd/mtd-user.h>
41
42int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
44{
45 /* note: fields in these structs are 32-bits.
46 * apparently we can't win anything by using off_t
47 * or long long's for offset and/or sectors vars. */
48 struct mtd_info_user info;
49 struct erase_info_user lock;
50 unsigned long offset;
51 long sectors;
52 int fd;
53
54#define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
55
56 if (!argv[1])
57 bb_show_usage();
58
59 /* parse offset and number of sectors to lock */
60 offset = 0;
61 sectors = -1;
62 if (do_lock) {
63 if (!argv[2] || !argv[3])
64 bb_show_usage();
65 offset = xstrtoul(argv[2], 0);
66 sectors = xstrtol(argv[3], 0);
67 }
68
69 fd = xopen(argv[1], O_RDWR);
70
71 xioctl(fd, MEMGETINFO, &info);
72
73 lock.start = 0;
74 lock.length = info.size;
75 if (do_lock) {
76 unsigned long size = info.size - info.erasesize;
77 if (offset > size) {
78 bb_error_msg_and_die("%lx is beyond device size %lx\n",
79 offset, size);
80 }
81
82 if (sectors == -1) {
83 sectors = info.size / info.erasesize;
84 } else {
85// isn't this useless?
86 unsigned long num = info.size / info.erasesize;
87 if (sectors > num) {
88 bb_error_msg_and_die("%ld are too many "
89 "sectors, device only has "
90 "%ld\n", sectors, num);
91 }
92 }
93
94 lock.start = offset;
95 lock.length = sectors * info.erasesize;
96 xioctl(fd, MEMLOCK, &lock);
97 } else {
98 xioctl(fd, MEMUNLOCK, &lock);
99 }
100
101 return EXIT_SUCCESS;
102}