1 /*
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 
17 #include <cutils/partition_utils.h>
18 
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/mount.h> /* for BLKGETSIZE */
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <cutils/properties.h>
27 
only_one_char(uint8_t * buf,int len,uint8_t c)28 static int only_one_char(uint8_t *buf, int len, uint8_t 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 
partition_wiped(const char * source)42 int partition_wiped(const char* source) {
43     uint8_t buf[4096];
44     int fd, ret;
45 
46     if ((fd = open(source, O_RDONLY)) < 0) {
47         return 0;
48     }
49 
50     ret = read(fd, buf, sizeof(buf));
51     close(fd);
52 
53     if (ret != sizeof(buf)) {
54         return 0;
55     }
56 
57     /* Check for all zeros */
58     if (only_one_char(buf, sizeof(buf), 0)) {
59        return 1;
60     }
61 
62     /* Check for all ones */
63     if (only_one_char(buf, sizeof(buf), 0xff)) {
64        return 1;
65     }
66 
67     return 0;
68 }
69