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 17 #include "ext4_utils/wipe.h" 18 19 #include "ext4_utils/ext4_utils.h" 20 21 #include <android-base/file.h> 22 23 #if WIPE_IS_SUPPORTED 24 25 #if defined(__linux__) 26 27 #include <linux/fs.h> 28 #include <sys/ioctl.h> 29 30 #ifndef BLKDISCARD 31 #define BLKDISCARD _IO(0x12,119) 32 #endif 33 34 #ifndef BLKSECDISCARD 35 #define BLKSECDISCARD _IO(0x12,125) 36 #endif 37 38 int wipe_block_device(int fd, s64 len) 39 { 40 u64 range[2]; 41 int ret; 42 43 if (!is_block_device_fd(fd)) { 44 // Wiping only makes sense on a block device. 45 return 0; 46 } 47 48 range[0] = 0; 49 range[1] = len; 50 ret = ioctl(fd, BLKSECDISCARD, &range); 51 if (ret < 0) { 52 range[0] = 0; 53 range[1] = len; 54 ret = ioctl(fd, BLKDISCARD, &range); 55 if (ret < 0) { 56 warn("Discard failed\n"); 57 return 1; 58 } else { 59 char buf[4096] = {0}; 60 61 if (!android::base::WriteFully(fd, buf, 4096)) { 62 warn("Writing zeros failed\n"); 63 return 1; 64 } 65 fsync(fd); 66 warn("Wipe via secure discard failed, used discard instead\n"); 67 return 0; 68 } 69 } 70 71 return 0; 72 } 73 74 #else /* __linux__ */ 75 #error "Missing block device wiping implementation for this platform!" 76 #endif 77 78 #else /* WIPE_IS_SUPPORTED */ 79 80 int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused))) 81 { 82 /* Wiping is not supported on this platform. */ 83 return 1; 84 } 85 86 #endif /* WIPE_IS_SUPPORTED */ 87