3
0

flash_lock_unlock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Ported to busybox from mtd-utils.
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config FLASH_LOCK
  8. //config: bool "flash_lock (2.1 kb)"
  9. //config: default n # doesn't build on Ubuntu 8.04
  10. //config: help
  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.
  13. //config:
  14. //config:config FLASH_UNLOCK
  15. //config: bool "flash_unlock (1.3 kb)"
  16. //config: default n # doesn't build on Ubuntu 8.04
  17. //config: help
  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.
  20. // 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))
  22. //applet:IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, BB_DIR_USR_SBIN, BB_SUID_DROP, flash_unlock))
  23. /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
  24. //kbuild:lib-$(CONFIG_FLASH_LOCK) += flash_lock_unlock.o
  25. //kbuild:lib-$(CONFIG_FLASH_UNLOCK) += flash_lock_unlock.o
  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. #include "libbb.h"
  37. #include <mtd/mtd-user.h>
  38. int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. /* note: fields in these structs are 32-bits.
  42. * apparently we can't win anything by using off_t
  43. * or long long's for offset and/or sectors vars. */
  44. struct mtd_info_user info;
  45. struct erase_info_user lock;
  46. unsigned long offset;
  47. long sectors;
  48. int fd;
  49. #define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
  50. if (!argv[1])
  51. bb_show_usage();
  52. /* parse offset and number of sectors to lock */
  53. offset = 0;
  54. sectors = -1;
  55. if (do_lock) {
  56. if (!argv[2] || !argv[3])
  57. bb_show_usage();
  58. offset = xstrtoul(argv[2], 0);
  59. sectors = xstrtol(argv[3], 0);
  60. }
  61. fd = xopen(argv[1], O_RDWR);
  62. xioctl(fd, MEMGETINFO, &info);
  63. lock.start = 0;
  64. lock.length = info.size;
  65. if (do_lock) {
  66. unsigned long size = info.size - info.erasesize;
  67. if (offset > size) {
  68. bb_error_msg_and_die("%lx is beyond device size %lx\n",
  69. offset, size);
  70. }
  71. if (sectors == -1) {
  72. sectors = info.size / info.erasesize;
  73. } else {
  74. // isn't this useless?
  75. unsigned long num = info.size / info.erasesize;
  76. if (sectors > num) {
  77. bb_error_msg_and_die("%ld are too many "
  78. "sectors, device only has "
  79. "%ld\n", sectors, num);
  80. }
  81. }
  82. lock.start = offset;
  83. lock.length = sectors * info.erasesize;
  84. xioctl(fd, MEMLOCK, &lock);
  85. } else {
  86. xioctl(fd, MEMUNLOCK, &lock);
  87. }
  88. return EXIT_SUCCESS;
  89. }