| 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 |  | 
| bohu | 39ed7af | 2018-10-09 06:06:56 -0700 | [diff] [blame] | 28 | static int only_one_char(uint8_t *buf, int len, uint8_t c) | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 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 |  | 
| Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 42 | int partition_wiped(const char* source) { | 
| bohu | 39ed7af | 2018-10-09 06:06:56 -0700 | [diff] [blame] | 43 |     uint8_t buf[4096]; | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 44 |     int fd, ret; | 
| Ken Sumrall | b05b0b5 | 2011-05-18 16:54:19 -0700 | [diff] [blame] | 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 | } |