2 * Copyright (C) 2011, 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
24 #include <sys/ioctl.h>
28 typedef unsigned long long u64;
31 #define BLKDISCARD _IO(0x12,119)
35 #define BLKSECDISCARD _IO(0x12,125)
38 static u64 get_block_device_size(int fd)
43 ret = ioctl(fd, BLKGETSIZE64, &size);
51 static int wipe_block_device(int fd, u64 len, int secure)
65 ret = ioctl(fd, req, &range);
67 fprintf(stderr, "%s discard failed, errno = %d\n",
68 secure ? "Secure" : "Nonsecure", errno);
74 static void usage(void)
76 fprintf(stderr, "Usage: wipe_blkdev [-s] <partition>\n");
80 int main(int argc, char *argv[])
89 if ((argc != 2) && (argc != 3)) {
94 if (!strcmp(argv[1], "-s")) {
104 fd = open(devname, O_RDWR);
106 fprintf(stderr, "Cannot open device %s\n", devname);
110 if (fstat(fd, &statbuf) < 0) {
111 fprintf(stderr, "Cannot stat %s\n", devname);
115 if (!S_ISBLK(statbuf.st_mode)) {
116 fprintf(stderr, "%s is not a block device\n", devname);
120 len = get_block_device_size(fd);
123 fprintf(stderr, "Cannot get size of block device %s\n", devname);
127 ret = wipe_block_device(fd, len, secure);