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