blob: f94e5d9d438c9174f0d96ffc9535006ddc4c71f0 [file] [log] [blame]
Andres Morales68d4acd2014-07-01 19:40:41 -07001/*
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 <JNIHelp.h>
19#include <jni.h>
Henrik Baardc90ca482015-06-23 09:36:36 +020020#include <ScopedUtfChars.h>
Andres Morales68d4acd2014-07-01 19:40:41 -070021
22#include <utils/misc.h>
23#include <sys/ioctl.h>
24#include <sys/mount.h>
Andres Morales963295e2014-07-10 15:40:24 -070025#include <utils/Log.h>
26
Andres Morales68d4acd2014-07-01 19:40:41 -070027
28#include <inttypes.h>
29#include <fcntl.h>
Andres Morales963295e2014-07-10 15:40:24 -070030#include <errno.h>
31#include <string.h>
Andres Morales68d4acd2014-07-01 19:40:41 -070032
33namespace android {
34
35 uint64_t get_block_device_size(int fd)
36 {
37 uint64_t size = 0;
38 int ret;
39
40 ret = ioctl(fd, BLKGETSIZE64, &size);
41
42 if (ret)
43 return 0;
44
45 return size;
46 }
47
Andres Morales963295e2014-07-10 15:40:24 -070048 int wipe_block_device(int fd)
49 {
50 uint64_t range[2];
51 int ret;
52 uint64_t len = get_block_device_size(fd);
53
54 range[0] = 0;
55 range[1] = len;
56
57 if (range[1] == 0)
58 return 0;
59
60 ret = ioctl(fd, BLKSECDISCARD, &range);
61 if (ret < 0) {
62 ALOGE("Something went wrong secure discarding block: %s\n", strerror(errno));
63 range[0] = 0;
64 range[1] = len;
65 ret = ioctl(fd, BLKDISCARD, &range);
66 if (ret < 0) {
67 ALOGE("Discard failed: %s\n", strerror(errno));
68 return -1;
69 } else {
70 ALOGE("Wipe via secure discard failed, used non-secure discard instead\n");
71 return 0;
72 }
73
74 }
75
76 return ret;
77 }
78
79 static jlong com_android_server_PersistentDataBlockService_getBlockDeviceSize(JNIEnv *env, jclass, jstring jpath)
80 {
Henrik Baardc90ca482015-06-23 09:36:36 +020081 ScopedUtfChars path(env, jpath);
82 int fd = open(path.c_str(), O_RDONLY);
Andres Morales68d4acd2014-07-01 19:40:41 -070083
84 if (fd < 0)
85 return 0;
86
Lianwei Wang5c74bb32016-01-13 18:42:35 -080087 const uint64_t size = get_block_device_size(fd);
88
89 close(fd);
90
91 return size;
Andres Morales68d4acd2014-07-01 19:40:41 -070092 }
93
Andres Morales963295e2014-07-10 15:40:24 -070094 static int com_android_server_PersistentDataBlockService_wipe(JNIEnv *env, jclass, jstring jpath) {
Henrik Baardc90ca482015-06-23 09:36:36 +020095 ScopedUtfChars path(env, jpath);
96 int fd = open(path.c_str(), O_WRONLY);
Andres Morales963295e2014-07-10 15:40:24 -070097
98 if (fd < 0)
99 return 0;
100
Lianwei Wang5c74bb32016-01-13 18:42:35 -0800101 const int ret = wipe_block_device(fd);
102
103 close(fd);
104
105 return ret;
Andres Morales963295e2014-07-10 15:40:24 -0700106 }
107
Daniel Micay76f6a862015-09-19 17:31:01 -0400108 static const JNINativeMethod sMethods[] = {
Andres Morales68d4acd2014-07-01 19:40:41 -0700109 /* name, signature, funcPtr */
Andres Morales963295e2014-07-10 15:40:24 -0700110 {"nativeGetBlockDeviceSize", "(Ljava/lang/String;)J", (void*)com_android_server_PersistentDataBlockService_getBlockDeviceSize},
111 {"nativeWipe", "(Ljava/lang/String;)I", (void*)com_android_server_PersistentDataBlockService_wipe},
Andres Morales68d4acd2014-07-01 19:40:41 -0700112 };
113
114 int register_android_server_PersistentDataBlockService(JNIEnv* env)
115 {
116 return jniRegisterNativeMethods(env, "com/android/server/PersistentDataBlockService",
117 sMethods, NELEM(sMethods));
118 }
119
120} /* namespace android */