blob: 003496d5b1a7e9ac99991271a1ac8a56013a625e [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
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.
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))
23
24//kbuild:lib-$(CONFIG_FLASH_LOCK) += flash_lock_unlock.o
25//kbuild:lib-$(CONFIG_FLASH_UNLOCK) += flash_lock_unlock.o
26
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage:#define flash_lock_trivial_usage
28//usage: "MTD_DEVICE OFFSET SECTORS"
29//usage:#define flash_lock_full_usage "\n\n"
30//usage: "Lock part or all of an MTD device. If SECTORS is -1, then all sectors\n"
31//usage: "will be locked, regardless of the value of OFFSET"
32//usage:
33//usage:#define flash_unlock_trivial_usage
34//usage: "MTD_DEVICE"
35//usage:#define flash_unlock_full_usage "\n\n"
36//usage: "Unlock an MTD device"
37
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +020038#include "libbb.h"
39#include <mtd/mtd-user.h>
40
41int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
43{
44 /* note: fields in these structs are 32-bits.
45 * apparently we can't win anything by using off_t
46 * or long long's for offset and/or sectors vars. */
47 struct mtd_info_user info;
48 struct erase_info_user lock;
49 unsigned long offset;
50 long sectors;
51 int fd;
52
53#define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
54
55 if (!argv[1])
56 bb_show_usage();
57
58 /* parse offset and number of sectors to lock */
59 offset = 0;
60 sectors = -1;
61 if (do_lock) {
62 if (!argv[2] || !argv[3])
63 bb_show_usage();
64 offset = xstrtoul(argv[2], 0);
65 sectors = xstrtol(argv[3], 0);
66 }
67
68 fd = xopen(argv[1], O_RDWR);
69
70 xioctl(fd, MEMGETINFO, &info);
71
72 lock.start = 0;
73 lock.length = info.size;
74 if (do_lock) {
75 unsigned long size = info.size - info.erasesize;
76 if (offset > size) {
77 bb_error_msg_and_die("%lx is beyond device size %lx\n",
78 offset, size);
79 }
80
81 if (sectors == -1) {
82 sectors = info.size / info.erasesize;
83 } else {
84// isn't this useless?
85 unsigned long num = info.size / info.erasesize;
86 if (sectors > num) {
87 bb_error_msg_and_die("%ld are too many "
88 "sectors, device only has "
89 "%ld\n", sectors, num);
90 }
91 }
92
93 lock.start = offset;
94 lock.length = sectors * info.erasesize;
95 xioctl(fd, MEMLOCK, &lock);
96 } else {
97 xioctl(fd, MEMUNLOCK, &lock);
98 }
99
100 return EXIT_SUCCESS;
101}