blob: 823b16289588c0d96079be9865fd167ab2b0325a [file] [log] [blame]
Ken Sumrallb05b0b52011-05-18 16:54:19 -07001/*
2 * Copyright 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
Ken Sumrallb05b0b52011-05-18 16:54:19 -070017#include <fcntl.h>
Ken Sumrallb05b0b52011-05-18 16:54:19 -070018#include <sys/ioctl.h>
19#include <sys/mount.h> /* for BLKGETSIZE */
Mark Salyzyn12717162014-04-29 15:49:14 -070020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Ken Sumrallb05b0b52011-05-18 16:54:19 -070024#include <cutils/properties.h>
25
26static int only_one_char(char *buf, int len, char c)
27{
28 int i, ret;
29
30 ret = 1;
31 for (i=0; i<len; i++) {
32 if (buf[i] != c) {
33 ret = 0;
34 break;
35 }
36 }
37 return ret;
38}
39
40int partition_wiped(char *source)
41{
42 char buf[4096];
Mark Salyzyn12717162014-04-29 15:49:14 -070043 int fd, ret;
Ken Sumrallb05b0b52011-05-18 16:54:19 -070044
45 if ((fd = open(source, O_RDONLY)) < 0) {
46 return 0;
47 }
48
49 ret = read(fd, buf, sizeof(buf));
50 close(fd);
51
52 if (ret != sizeof(buf)) {
53 return 0;
54 }
55
56 /* Check for all zeros */
57 if (only_one_char(buf, sizeof(buf), 0)) {
58 return 1;
59 }
60
61 /* Check for all ones */
62 if (only_one_char(buf, sizeof(buf), 0xff)) {
63 return 1;
64 }
65
66 return 0;
67}
68