busybox/miscutils/flash_lock_unlock.c
<<
>>
Prefs
   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 */
   6//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.
  19
  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))
  22
  23//kbuild:lib-$(CONFIG_FLASH_LOCK) += flash_lock_unlock.o
  24//kbuild:lib-$(CONFIG_FLASH_UNLOCK) += flash_lock_unlock.o
  25
  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"
  31//usage:
  32//usage:#define flash_unlock_trivial_usage
  33//usage:       "MTD_DEVICE"
  34//usage:#define flash_unlock_full_usage "\n\n"
  35//usage:       "Unlock an MTD device"
  36
  37#include "libbb.h"
  38#include <mtd/mtd-user.h>
  39
  40int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
  42{
  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;
  48        unsigned long offset;
  49        long sectors;
  50        int fd;
  51
  52#define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
  53
  54        if (!argv[1])
  55                bb_show_usage();
  56
  57        /* parse offset and number of sectors to lock */
  58        offset = 0;
  59        sectors = -1;
  60        if (do_lock) {
  61                if (!argv[2] || !argv[3])
  62                        bb_show_usage();
  63                offset = xstrtoul(argv[2], 0);
  64                sectors = xstrtol(argv[3], 0);
  65        }
  66
  67        fd = xopen(argv[1], O_RDWR);
  68
  69        xioctl(fd, MEMGETINFO, &info);
  70
  71        lock.start = 0;
  72        lock.length = info.size;
  73        if (do_lock) {
  74                unsigned long size = info.size - info.erasesize;
  75                if (offset > size) {
  76                        bb_error_msg_and_die("%lx is beyond device size %lx\n",
  77                                        offset, size);
  78                }
  79
  80                if (sectors == -1) {
  81                        sectors = info.size / info.erasesize;
  82                } else {
  83// isn't this useless?
  84                        unsigned long num = info.size / info.erasesize;
  85                        if (sectors > num) {
  86                                bb_error_msg_and_die("%ld are too many "
  87                                                "sectors, device only has "
  88                                                "%ld\n", sectors, num);
  89                        }
  90                }
  91
  92                lock.start = offset;
  93                lock.length = sectors * info.erasesize;
  94                xioctl(fd, MEMLOCK, &lock);
  95        } else {
  96                xioctl(fd, MEMUNLOCK, &lock);
  97        }
  98
  99        return EXIT_SUCCESS;
 100}
 101