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