flash_unlock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * flash_{lock,unlock}
  3. *
  4. * utilities for locking/unlocking sectors of flash devices
  5. */
  6. enum flash_lock_request {
  7. REQUEST_LOCK,
  8. REQUEST_UNLOCK,
  9. REQUEST_ISLOCKED,
  10. };
  11. #ifndef PROGRAM_NAME
  12. #define PROGRAM_NAME "flash_unlock"
  13. #define DEFAULT_REQUEST REQUEST_UNLOCK
  14. #else
  15. #define DEFAULT_REQUEST REQUEST_LOCK
  16. #endif
  17. #include <getopt.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <time.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mount.h>
  25. #include <string.h>
  26. #include "common.h"
  27. #include <mtd/mtd-user.h>
  28. static const char *flash_msg[] = {
  29. [ REQUEST_LOCK ] = "lock",
  30. [ REQUEST_UNLOCK ] = "unlock",
  31. [ REQUEST_ISLOCKED ] = "check lock status",
  32. };
  33. static NORETURN void usage(int status)
  34. {
  35. fprintf(status ? stderr : stdout,
  36. "Utility to lock, unlock, or check the lock status of the flash.\n"
  37. "Default action: %s\n"
  38. "\n"
  39. "Usage: %s [options] [--] <mtd device> [offset [block count]]\n"
  40. "\n"
  41. "Options:\n"
  42. " -h --help Display this help and exit\n"
  43. " -V --version Display version information and exit\n"
  44. " -i --islocked Check if flash region is locked\n"
  45. " -l --lock Lock a region of flash\n"
  46. " -u --unlock Unlock a region of flash\n"
  47. "\n"
  48. "If offset is not specified, it defaults to 0.\n"
  49. "If block count is not specified, it defaults to all blocks.\n"
  50. "A block count of -1 means all blocks.\n",
  51. flash_msg[DEFAULT_REQUEST],
  52. PROGRAM_NAME);
  53. exit(status);
  54. }
  55. static const char short_opts[] = "hiluV";
  56. static const struct option long_opts[] = {
  57. { "help", no_argument, 0, 'h' },
  58. { "islocked", no_argument, 0, 'i' },
  59. { "lock", no_argument, 0, 'l' },
  60. { "unlock", no_argument, 0, 'u' },
  61. { "version", no_argument, 0, 'V' },
  62. { NULL, 0, 0, 0 },
  63. };
  64. /* Program arguments */
  65. static const char *dev, *offs_s, *count_s;
  66. static enum flash_lock_request req = DEFAULT_REQUEST;
  67. static void process_args(int argc, char *argv[])
  68. {
  69. int arg_idx;
  70. int req_set = 0;
  71. for (;;) {
  72. int c;
  73. c = getopt_long(argc, argv, short_opts, long_opts, NULL);
  74. if (c == EOF)
  75. break;
  76. switch (c) {
  77. case 'h':
  78. usage(EXIT_SUCCESS);
  79. case 'i':
  80. req = REQUEST_ISLOCKED;
  81. req_set++;
  82. break;
  83. case 'l':
  84. req = REQUEST_LOCK;
  85. req_set++;
  86. break;
  87. case 'u':
  88. req = REQUEST_UNLOCK;
  89. req_set++;
  90. break;
  91. case 'V':
  92. common_print_version();
  93. exit(0);
  94. default:
  95. usage(EXIT_FAILURE);
  96. }
  97. }
  98. if (req_set > 1) {
  99. errmsg("cannot specify more than one lock/unlock/islocked option");
  100. usage(EXIT_FAILURE);
  101. }
  102. arg_idx = optind;
  103. /* Sanity checks */
  104. if (argc - arg_idx < 1) {
  105. errmsg("too few arguments");
  106. usage(EXIT_FAILURE);
  107. } else if (argc - arg_idx > 3) {
  108. errmsg("too many arguments");
  109. usage(EXIT_FAILURE);
  110. }
  111. /* First non-option argument */
  112. dev = argv[arg_idx++];
  113. /* Second non-option argument */
  114. if (arg_idx < argc)
  115. offs_s = argv[arg_idx++];
  116. else
  117. offs_s = NULL;
  118. /* Third non-option argument */
  119. if (arg_idx < argc)
  120. count_s = argv[arg_idx++];
  121. else
  122. count_s = NULL;
  123. }
  124. int main(int argc, char *argv[])
  125. {
  126. int fd, request;
  127. struct mtd_info_user mtdInfo;
  128. struct erase_info_user mtdLockInfo;
  129. long count;
  130. int ret = 0;
  131. process_args(argc, argv);
  132. /* Get the device info to compare to command line sizes */
  133. fd = open(dev, O_RDWR);
  134. if (fd < 0)
  135. sys_errmsg_die("could not open: %s", dev);
  136. if (ioctl(fd, MEMGETINFO, &mtdInfo))
  137. sys_errmsg_die("could not get mtd info: %s", dev);
  138. /* Make sure user options are valid */
  139. if (offs_s) {
  140. mtdLockInfo.start = simple_strtol(offs_s, &ret);
  141. if (ret)
  142. errmsg_die("bad offset");
  143. } else {
  144. mtdLockInfo.start = 0;
  145. }
  146. if (mtdLockInfo.start >= mtdInfo.size)
  147. errmsg_die("%#x is beyond device size %#x",
  148. mtdLockInfo.start, mtdInfo.size);
  149. if (count_s) {
  150. count = simple_strtol(count_s, &ret);
  151. if (ret)
  152. errmsg_die("bad count");
  153. if (count == -1)
  154. mtdLockInfo.length = mtdInfo.size;
  155. else
  156. mtdLockInfo.length = mtdInfo.erasesize * count;
  157. } else {
  158. mtdLockInfo.length = mtdInfo.size;
  159. }
  160. if (mtdLockInfo.start + mtdLockInfo.length > mtdInfo.size)
  161. errmsg_die("range is more than device supports: %#x + %#x > %#x",
  162. mtdLockInfo.start, mtdLockInfo.length, mtdInfo.size);
  163. /* Finally do the operation */
  164. switch (req) {
  165. case REQUEST_LOCK:
  166. request = MEMLOCK;
  167. break;
  168. case REQUEST_UNLOCK:
  169. request = MEMUNLOCK;
  170. break;
  171. case REQUEST_ISLOCKED:
  172. request = MEMISLOCKED;
  173. break;
  174. default:
  175. errmsg_die("unknown request type: %d", req);
  176. break;
  177. }
  178. ret = ioctl(fd, request, &mtdLockInfo);
  179. if (ret < 0)
  180. sys_errmsg_die("could not %s device: %s\n",
  181. flash_msg[req], dev);
  182. if (req == REQUEST_ISLOCKED) {
  183. printf("Device: %s\n", dev);
  184. printf("Start: %#0x\n", mtdLockInfo.start);
  185. printf("Len: %#0x\n", mtdLockInfo.length);
  186. printf("Lock status: %s\n", ret ? "locked" : "unlocked");
  187. printf("Return code: %d\n", ret);
  188. }
  189. return 0;
  190. }