| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/partition_utils.h> | 
 | 18 |  | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 20 | #include <sys/ioctl.h> | 
 | 21 | #include <sys/mount.h> /* for BLKGETSIZE */ | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> | 
 | 23 | #include <sys/types.h> | 
 | 24 | #include <unistd.h> | 
 | 25 |  | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> | 
 | 27 |  | 
 | 28 | static 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 |  | 
 | 42 | int partition_wiped(char *source) | 
 | 43 | { | 
 | 44 |     char buf[4096]; | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 45 |     int fd, ret; | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 46 |  | 
 | 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 |  |