1b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall/*
2b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * Copyright 2011, The Android Open Source Project
3b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall *
4b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * Licensed under the Apache License, Version 2.0 (the "License");
5b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * you may not use this file except in compliance with the License.
6b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * You may obtain a copy of the License at
7b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall *
8b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall *     http://www.apache.org/licenses/LICENSE-2.0
9b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall *
10b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * Unless required by applicable law or agreed to in writing, software
11b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * distributed under the License is distributed on an "AS IS" BASIS,
12b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * See the License for the specific language governing permissions and
14b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall * limitations under the License.
15b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall */
16b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
17b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <sys/types.h>
18b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <unistd.h>
19b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <fcntl.h>
20b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <sys/stat.h>
21b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <sys/ioctl.h>
22b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <sys/mount.h> /* for BLKGETSIZE */
23b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall#include <cutils/properties.h>
24b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
25b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrallstatic int only_one_char(char *buf, int len, char c)
26b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall{
27b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    int i, ret;
28b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
29b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    ret = 1;
30b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    for (i=0; i<len; i++) {
31b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall        if (buf[i] != c) {
32b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall            ret = 0;
33b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall            break;
34b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall        }
35b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    }
36b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    return ret;
37b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall}
38b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
39b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrallint partition_wiped(char *source)
40b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall{
41b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    char buf[4096];
42b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    int fd, ret, wiped;
43b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
44b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    if ((fd = open(source, O_RDONLY)) < 0) {
45b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall        return 0;
46b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    }
47b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
48b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    ret = read(fd, buf, sizeof(buf));
49b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    close(fd);
50b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
51b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    if (ret != sizeof(buf)) {
52b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall        return 0;
53b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    }
54b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
55b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    /* Check for all zeros */
56b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    if (only_one_char(buf, sizeof(buf), 0)) {
57b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall       return 1;
58b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    }
59b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
60b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    /* Check for all ones */
61b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    if (only_one_char(buf, sizeof(buf), 0xff)) {
62b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall       return 1;
63b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    }
64b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
65b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall    return 0;
66b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall}
67b05b0b5d3b01441d648782f552d9f7e2d7bfbbc1Ken Sumrall
68