3
0

flash_lock_unlock.c 1.6 KB

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