jffs2reset.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/mount.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/reboot.h>
  17. #include <libubox/ulog.h>
  18. #include <fcntl.h>
  19. #include <dirent.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <getopt.h>
  26. #include <mtd/ubi-user.h>
  27. #include "libfstools/libfstools.h"
  28. #include "libfstools/volume.h"
  29. static int jffs2_mark(struct volume *v);
  30. static int
  31. ask_user(void)
  32. {
  33. ULOG_WARN("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
  34. if (getchar() != 'y')
  35. return -1;
  36. return 0;
  37. }
  38. static int jffs2_reset(struct volume *v, int reset, int keep)
  39. {
  40. char *mp;
  41. mp = find_mount_point(v->blk, 1);
  42. if (mp) {
  43. ULOG_INFO("%s is mounted as %s, only erasing files\n", v->blk, mp);
  44. fs_state_set("/overlay", FS_STATE_PENDING);
  45. overlay_delete(mp, keep);
  46. mount(mp, "/", NULL, MS_REMOUNT, 0);
  47. } else {
  48. ULOG_INFO("%s is not mounted\n", v->blk);
  49. return jffs2_mark(v);
  50. }
  51. if (reset) {
  52. sync();
  53. sleep(2);
  54. reboot(RB_AUTOBOOT);
  55. while (1)
  56. ;
  57. }
  58. return 0;
  59. }
  60. static int jffs2_mark(struct volume *v)
  61. {
  62. __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
  63. size_t sz;
  64. int fd;
  65. fd = open(v->blk, O_WRONLY);
  66. ULOG_INFO("%s will be erased on next mount\n", v->blk);
  67. if (!fd) {
  68. ULOG_ERR("opening %s failed\n", v->blk);
  69. return -1;
  70. }
  71. if (volume_identify(v) == FS_UBIFS) {
  72. uint64_t llz = 0;
  73. int ret = ioctl(fd, UBI_IOCVOLUP, &llz);
  74. close(fd);
  75. return ret;
  76. }
  77. sz = write(fd, &deadc0de, sizeof(deadc0de));
  78. close(fd);
  79. if (sz != 4) {
  80. ULOG_ERR("writing %s failed: %m\n", v->blk);
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. struct volume *v;
  88. int ch, yes = 0, reset = 0, keep = 0;
  89. while ((ch = getopt(argc, argv, "yrk")) != -1) {
  90. switch(ch) {
  91. case 'y':
  92. yes = 1;
  93. break;
  94. case 'r':
  95. reset = 1;
  96. break;
  97. case 'k':
  98. keep = 1;
  99. break;
  100. }
  101. }
  102. if (!yes && ask_user())
  103. return -1;
  104. /*
  105. * TODO: Currently this only checks if kernel supports OverlayFS. We
  106. * should check if there is a mount point using it with rootfs_data
  107. * as upperdir.
  108. */
  109. if (find_filesystem("overlay")) {
  110. ULOG_ERR("overlayfs not supported by kernel\n");
  111. return -1;
  112. }
  113. v = volume_find("rootfs_data");
  114. if (!v) {
  115. ULOG_ERR("MTD partition 'rootfs_data' not found\n");
  116. return -1;
  117. }
  118. volume_init(v);
  119. if (!strcmp(*argv, "jffs2mark"))
  120. return jffs2_mark(v);
  121. return jffs2_reset(v, reset, keep);
  122. }