blob: 445c97391528ef712f68a9f39340da12df869dd1 [file] [log] [blame]
Colin Crossc2470652011-01-26 16:39:46 -08001/*
2 * Copyright (C) 2011 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
Tao Bao06ca8112016-10-05 12:44:18 -070017#include "ext4_utils/wipe.h"
18
19#include "ext4_utils/ext4_utils.h"
Colin Crossc2470652011-01-26 16:39:46 -080020
Jaegeuk Kima2df7932019-02-13 15:24:01 -080021#include <android-base/file.h>
22
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020023#if WIPE_IS_SUPPORTED
24
Colin Crossc2470652011-01-26 16:39:46 -080025#if defined(__linux__)
26
27#include <linux/fs.h>
28#include <sys/ioctl.h>
29
David Anderson166d8672020-07-08 13:39:01 -070030#include "helpers.h"
31
Colin Crossc2470652011-01-26 16:39:46 -080032#ifndef BLKDISCARD
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010033#define BLKDISCARD _IO(0x12, 119)
Colin Crossc2470652011-01-26 16:39:46 -080034#endif
35
36#ifndef BLKSECDISCARD
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010037#define BLKSECDISCARD _IO(0x12, 125)
Colin Crossc2470652011-01-26 16:39:46 -080038#endif
39
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010040int wipe_block_device(int fd, s64 len) {
41 u64 range[2];
42 int ret;
Colin Crossc2470652011-01-26 16:39:46 -080043
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010044 if (!is_block_device_fd(fd)) {
45 // Wiping only makes sense on a block device.
46 return 0;
47 }
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020048
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010049 range[0] = 0;
50 range[1] = len;
51 ret = ioctl(fd, BLKSECDISCARD, &range);
52 if (ret < 0) {
53 range[0] = 0;
54 range[1] = len;
55 ret = ioctl(fd, BLKDISCARD, &range);
56 if (ret < 0) {
57 warn("Discard failed\n");
58 return 1;
59 } else {
60 char buf[4096] = {0};
Jaegeuk Kima2df7932019-02-13 15:24:01 -080061
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010062 if (!android::base::WriteFully(fd, buf, 4096)) {
63 warn("Writing zeros failed\n");
64 return 1;
65 }
66 fsync(fd);
67 warn("Wipe via secure discard failed, used discard instead\n");
68 return 0;
69 }
70 }
Colin Crossc2470652011-01-26 16:39:46 -080071
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010072 return 0;
Colin Crossc2470652011-01-26 16:39:46 -080073}
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020074
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010075#else /* __linux__ */
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020076#error "Missing block device wiping implementation for this platform!"
Colin Crossc2470652011-01-26 16:39:46 -080077#endif
78
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010079#else /* WIPE_IS_SUPPORTED */
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020080
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010081int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused))) {
82 /* Wiping is not supported on this platform. */
83 return 1;
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +020084}
85
Thiébaud Weksteen00f82232020-10-26 13:35:15 +010086#endif /* WIPE_IS_SUPPORTED */