partition_utils.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <cutils/partition_utils.h>
  17. #include <fcntl.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/mount.h> /* for BLKGETSIZE */
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <cutils/properties.h>
  24. static int only_one_char(uint8_t *buf, int len, uint8_t c)
  25. {
  26. int i, ret;
  27. ret = 1;
  28. for (i=0; i<len; i++) {
  29. if (buf[i] != c) {
  30. ret = 0;
  31. break;
  32. }
  33. }
  34. return ret;
  35. }
  36. int partition_wiped(const char* source) {
  37. uint8_t buf[4096];
  38. int fd, ret;
  39. if ((fd = open(source, O_RDONLY)) < 0) {
  40. return 0;
  41. }
  42. ret = read(fd, buf, sizeof(buf));
  43. close(fd);
  44. if (ret != sizeof(buf)) {
  45. return 0;
  46. }
  47. /* Check for all zeros */
  48. if (only_one_char(buf, sizeof(buf), 0)) {
  49. return 1;
  50. }
  51. /* Check for all ones */
  52. if (only_one_char(buf, sizeof(buf), 0xff)) {
  53. return 1;
  54. }
  55. return 0;
  56. }