1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android_runtime/AndroidRuntime.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <jni.h>
22 #include <nativehelper/JNIHelp.h>
23 #include <nativehelper/ScopedUtfChars.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
27 #include <utils/Log.h>
28 #include <utils/misc.h>
29
30 namespace android {
31
get_block_device_size(int fd)32 uint64_t get_block_device_size(int fd) {
33 uint64_t size = 0;
34 int ret;
35
36 ret = ioctl(fd, BLKGETSIZE64, &size);
37
38 if (ret) return 0;
39
40 return size;
41 }
42
wipe_block_device(int fd)43 int wipe_block_device(int fd) {
44 uint64_t range[2];
45 int ret;
46 uint64_t len = get_block_device_size(fd);
47
48 range[0] = 0;
49 range[1] = len;
50
51 if (range[1] == 0) return 0;
52
53 ret = ioctl(fd, BLKSECDISCARD, &range);
54 if (ret < 0) {
55 ALOGE("Something went wrong secure discarding block: %s\n", strerror(errno));
56 range[0] = 0;
57 range[1] = len;
58 ret = ioctl(fd, BLKDISCARD, &range);
59 if (ret < 0) {
60 ALOGE("Discard failed: %s\n", strerror(errno));
61 return -1;
62 } else {
63 ALOGE("Wipe via secure discard failed, used non-secure discard instead\n");
64 return 0;
65 }
66 }
67
68 return ret;
69 }
70
com_android_server_pdb_PersistentDataBlockService_getBlockDeviceSize(JNIEnv * env,jclass,jstring jpath)71 static jlong com_android_server_pdb_PersistentDataBlockService_getBlockDeviceSize(JNIEnv *env,
72 jclass,
73 jstring jpath) {
74 ScopedUtfChars path(env, jpath);
75 int fd = open(path.c_str(), O_RDONLY);
76
77 if (fd < 0) return 0;
78
79 const uint64_t size = get_block_device_size(fd);
80
81 close(fd);
82
83 return size;
84 }
85
com_android_server_pdb_PersistentDataBlockService_wipe(JNIEnv * env,jclass,jstring jpath)86 static int com_android_server_pdb_PersistentDataBlockService_wipe(JNIEnv *env, jclass,
87 jstring jpath) {
88 ScopedUtfChars path(env, jpath);
89 int fd = open(path.c_str(), O_WRONLY);
90
91 if (fd < 0) return 0;
92
93 const int ret = wipe_block_device(fd);
94
95 close(fd);
96
97 return ret;
98 }
99
100 static const JNINativeMethod sMethods[] = {
101 /* name, signature, funcPtr */
102 {"nativeGetBlockDeviceSize", "(Ljava/lang/String;)J",
103 (void *)com_android_server_pdb_PersistentDataBlockService_getBlockDeviceSize},
104 {"nativeWipe", "(Ljava/lang/String;)I",
105 (void *)com_android_server_pdb_PersistentDataBlockService_wipe},
106 };
107
register_android_server_pdb_PersistentDataBlockService(JNIEnv * env)108 int register_android_server_pdb_PersistentDataBlockService(JNIEnv *env) {
109 return jniRegisterNativeMethods(env, "com/android/server/pdb/PersistentDataBlockService",
110 sMethods, NELEM(sMethods));
111 }
112
113 } /* namespace android */