[2725] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /* Ported to busybox from mtd-utils.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
| 5 | */
|
---|
[3232] | 6 |
|
---|
| 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"
|
---|
| 12 | //usage:
|
---|
| 13 | //usage:#define flash_unlock_trivial_usage
|
---|
| 14 | //usage: "MTD_DEVICE"
|
---|
| 15 | //usage:#define flash_unlock_full_usage "\n\n"
|
---|
| 16 | //usage: "Unlock an MTD device"
|
---|
| 17 |
|
---|
[2725] | 18 | #include "libbb.h"
|
---|
| 19 | #include <mtd/mtd-user.h>
|
---|
| 20 |
|
---|
| 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)
|
---|
| 23 | {
|
---|
| 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;
|
---|
| 29 | unsigned long offset;
|
---|
| 30 | long sectors;
|
---|
| 31 | int fd;
|
---|
| 32 |
|
---|
| 33 | #define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
|
---|
| 34 |
|
---|
| 35 | if (!argv[1])
|
---|
| 36 | bb_show_usage();
|
---|
| 37 |
|
---|
| 38 | /* parse offset and number of sectors to lock */
|
---|
| 39 | offset = 0;
|
---|
| 40 | sectors = -1;
|
---|
| 41 | if (do_lock) {
|
---|
| 42 | if (!argv[2] || !argv[3])
|
---|
| 43 | bb_show_usage();
|
---|
| 44 | offset = xstrtoul(argv[2], 0);
|
---|
| 45 | sectors = xstrtol(argv[3], 0);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | fd = xopen(argv[1], O_RDWR);
|
---|
| 49 |
|
---|
| 50 | xioctl(fd, MEMGETINFO, &info);
|
---|
| 51 |
|
---|
| 52 | lock.start = 0;
|
---|
| 53 | lock.length = info.size;
|
---|
| 54 | if (do_lock) {
|
---|
| 55 | unsigned long size = info.size - info.erasesize;
|
---|
| 56 | if (offset > size) {
|
---|
| 57 | bb_error_msg_and_die("%lx is beyond device size %lx\n",
|
---|
| 58 | offset, size);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if (sectors == -1) {
|
---|
| 62 | sectors = info.size / info.erasesize;
|
---|
| 63 | } else {
|
---|
| 64 | // isn't this useless?
|
---|
| 65 | unsigned long num = info.size / info.erasesize;
|
---|
| 66 | if (sectors > num) {
|
---|
| 67 | bb_error_msg_and_die("%ld are too many "
|
---|
| 68 | "sectors, device only has "
|
---|
| 69 | "%ld\n", sectors, num);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | lock.start = offset;
|
---|
| 74 | lock.length = sectors * info.erasesize;
|
---|
| 75 | xioctl(fd, MEMLOCK, &lock);
|
---|
| 76 | } else {
|
---|
| 77 | xioctl(fd, MEMUNLOCK, &lock);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return EXIT_SUCCESS;
|
---|
| 81 | }
|
---|