blob: 6735d6cc8cb04af9a49c93d1b1bf2edd68948410 [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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/partition_utils.h>
18
Ken Sumrallb05b0b52011-05-18 16:54:19 -070019#include <fcntl.h>
Ken Sumrallb05b0b52011-05-18 16:54:19 -070020#include <sys/ioctl.h>
21#include <sys/mount.h> /* for BLKGETSIZE */
Mark Salyzyn12717162014-04-29 15:49:14 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Ken Sumrallb05b0b52011-05-18 16:54:19 -070026#include <cutils/properties.h>
27
28static int only_one_char(char *buf, int len, char c)
29{
30 int i, ret;
31
32 ret = 1;
33 for (i=0; i<len; i++) {
34 if (buf[i] != c) {
35 ret = 0;
36 break;
37 }
38 }
39 return ret;
40}
41
42int partition_wiped(char *source)
43{
44 char buf[4096];
Mark Salyzyn12717162014-04-29 15:49:14 -070045 int fd, ret;
Ken Sumrallb05b0b52011-05-18 16:54:19 -070046
47 if ((fd = open(source, O_RDONLY)) < 0) {
48 return 0;
49 }
50
51 ret = read(fd, buf, sizeof(buf));
52 close(fd);
53
54 if (ret != sizeof(buf)) {
55 return 0;
56 }
57
58 /* Check for all zeros */
59 if (only_one_char(buf, sizeof(buf), 0)) {
60 return 1;
61 }
62
63 /* Check for all ones */
64 if (only_one_char(buf, sizeof(buf), 0xff)) {
65 return 1;
66 }
67
68 return 0;
69}
70