Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Bao | 06ca811 | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 17 | #include "ext4_utils/wipe.h" |
| 18 | |
| 19 | #include "ext4_utils/ext4_utils.h" |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 20 | |
Jaegeuk Kim | a2df793 | 2019-02-13 15:24:01 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 23 | #if WIPE_IS_SUPPORTED |
| 24 | |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 25 | #if defined(__linux__) |
| 26 | |
| 27 | #include <linux/fs.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | |
David Anderson | 166d867 | 2020-07-08 13:39:01 -0700 | [diff] [blame] | 30 | #include "helpers.h" |
| 31 | |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 32 | #ifndef BLKDISCARD |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 33 | #define BLKDISCARD _IO(0x12, 119) |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #ifndef BLKSECDISCARD |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 37 | #define BLKSECDISCARD _IO(0x12, 125) |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 38 | #endif |
| 39 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 40 | int wipe_block_device(int fd, s64 len) { |
| 41 | u64 range[2]; |
| 42 | int ret; |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 43 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 44 | if (!is_block_device_fd(fd)) { |
| 45 | // Wiping only makes sense on a block device. |
| 46 | return 0; |
| 47 | } |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 48 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 49 | 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 Kim | a2df793 | 2019-02-13 15:24:01 -0800 | [diff] [blame] | 61 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 62 | 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 Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 71 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 72 | return 0; |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 73 | } |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 74 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 75 | #else /* __linux__ */ |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 76 | #error "Missing block device wiping implementation for this platform!" |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 77 | #endif |
| 78 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 79 | #else /* WIPE_IS_SUPPORTED */ |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 80 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 81 | int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused))) { |
| 82 | /* Wiping is not supported on this platform. */ |
| 83 | return 1; |
David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Thiébaud Weksteen | 00f8223 | 2020-10-26 13:35:15 +0100 | [diff] [blame] | 86 | #endif /* WIPE_IS_SUPPORTED */ |