factoryreset.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <libgen.h>
  27. #include <mtd/ubi-user.h>
  28. #include "libfstools/libfstools.h"
  29. #include "libfstools/volume.h"
  30. static int jffs2_mark(struct volume *v);
  31. static int
  32. ask_user(void)
  33. {
  34. ULOG_WARN("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
  35. if (getchar() != 'y')
  36. return -1;
  37. return 0;
  38. }
  39. static int jffs2_reset(struct volume *v, int reset, int keep)
  40. {
  41. char *mp;
  42. mp = find_mount_point(v->blk, 1);
  43. if (mp) {
  44. ULOG_INFO("%s is mounted as %s, only erasing files\n", v->blk, mp);
  45. fs_state_set("/overlay", FS_STATE_PENDING);
  46. overlay_delete(mp, keep);
  47. mount(mp, "/", NULL, MS_REMOUNT, 0);
  48. } else {
  49. ULOG_INFO("%s is not mounted\n", v->blk);
  50. return jffs2_mark(v);
  51. }
  52. if (reset) {
  53. sync();
  54. sleep(2);
  55. reboot(RB_AUTOBOOT);
  56. while (1)
  57. ;
  58. }
  59. return 0;
  60. }
  61. static int jffs2_mark(struct volume *v)
  62. {
  63. __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
  64. size_t sz;
  65. int fd;
  66. fd = open(v->blk, O_WRONLY);
  67. ULOG_INFO("%s will be erased on next mount\n", v->blk);
  68. if (!fd) {
  69. ULOG_ERR("opening %s failed\n", v->blk);
  70. return -1;
  71. }
  72. if (volume_identify(v) == FS_UBIFS) {
  73. uint64_t llz = 0;
  74. int ret = ioctl(fd, UBI_IOCVOLUP, &llz);
  75. close(fd);
  76. return ret;
  77. }
  78. sz = write(fd, &deadc0de, sizeof(deadc0de));
  79. close(fd);
  80. if (sz != 4) {
  81. ULOG_ERR("writing %s failed: %m\n", v->blk);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. struct volume *v;
  89. int ch, yes = 0, reset = 0, keep = 0;
  90. if (!strcmp(basename(argv[0]), "jffs2reset"))
  91. ULOG_ERR("jffs2reset will be deprected, please use factoryreset going forward\n");
  92. while ((ch = getopt(argc, argv, "yrk")) != -1) {
  93. switch(ch) {
  94. case 'y':
  95. yes = 1;
  96. break;
  97. case 'r':
  98. reset = 1;
  99. break;
  100. case 'k':
  101. keep = 1;
  102. break;
  103. }
  104. }
  105. if (!yes && ask_user())
  106. return -1;
  107. /*
  108. * TODO: Currently this only checks if kernel supports OverlayFS. We
  109. * should check if there is a mount point using it with rootfs_data
  110. * as upperdir.
  111. */
  112. if (find_filesystem("overlay")) {
  113. ULOG_ERR("overlayfs not supported by kernel\n");
  114. return -1;
  115. }
  116. v = volume_find("rootfs_data");
  117. if (!v) {
  118. ULOG_ERR("MTD partition 'rootfs_data' not found\n");
  119. return -1;
  120. }
  121. volume_init(v);
  122. if (!strcmp(*argv, "jffs2mark"))
  123. return jffs2_mark(v);
  124. return jffs2_reset(v, reset, keep);
  125. }