3
0

flash_lock_unlock.c 2.1 KB

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