flash_lock_unlock.c 2.9 KB

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