util.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2019 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 "util.h"
  17. #include <sys/ioctl.h>
  18. #include <thread>
  19. #include <android-base/unique_fd.h>
  20. #include <linux/fs.h>
  21. namespace android {
  22. namespace fs_mgr {
  23. bool NibbleValue(const char& c, uint8_t* value) {
  24. CHECK(value != nullptr);
  25. switch (c) {
  26. case '0' ... '9':
  27. *value = c - '0';
  28. break;
  29. case 'a' ... 'f':
  30. *value = c - 'a' + 10;
  31. break;
  32. case 'A' ... 'F':
  33. *value = c - 'A' + 10;
  34. break;
  35. default:
  36. return false;
  37. }
  38. return true;
  39. }
  40. bool HexToBytes(uint8_t* bytes, size_t bytes_len, const std::string& hex) {
  41. CHECK(bytes != nullptr);
  42. if (hex.size() % 2 != 0) {
  43. return false;
  44. }
  45. if (hex.size() / 2 > bytes_len) {
  46. return false;
  47. }
  48. for (size_t i = 0, j = 0, n = hex.size(); i < n; i += 2, ++j) {
  49. uint8_t high;
  50. if (!NibbleValue(hex[i], &high)) {
  51. return false;
  52. }
  53. uint8_t low;
  54. if (!NibbleValue(hex[i + 1], &low)) {
  55. return false;
  56. }
  57. bytes[j] = (high << 4) | low;
  58. }
  59. return true;
  60. }
  61. std::string BytesToHex(const uint8_t* bytes, size_t bytes_len) {
  62. CHECK(bytes != nullptr);
  63. static const char* hex_digits = "0123456789abcdef";
  64. std::string hex;
  65. for (size_t i = 0; i < bytes_len; i++) {
  66. hex.push_back(hex_digits[(bytes[i] & 0xF0) >> 4]);
  67. hex.push_back(hex_digits[bytes[i] & 0x0F]);
  68. }
  69. return hex;
  70. }
  71. // TODO: remove duplicate code with fs_mgr_wait_for_file
  72. bool WaitForFile(const std::string& filename, const std::chrono::milliseconds relative_timeout,
  73. FileWaitMode file_wait_mode) {
  74. auto start_time = std::chrono::steady_clock::now();
  75. while (true) {
  76. int rv = access(filename.c_str(), F_OK);
  77. if (file_wait_mode == FileWaitMode::Exists) {
  78. if (!rv || errno != ENOENT) return true;
  79. } else if (file_wait_mode == FileWaitMode::DoesNotExist) {
  80. if (rv && errno == ENOENT) return true;
  81. }
  82. std::this_thread::sleep_for(50ms);
  83. auto now = std::chrono::steady_clock::now();
  84. auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
  85. if (time_elapsed > relative_timeout) return false;
  86. }
  87. }
  88. bool IsDeviceUnlocked() {
  89. std::string verified_boot_state;
  90. if (fs_mgr_get_boot_config("verifiedbootstate", &verified_boot_state)) {
  91. return verified_boot_state == "orange";
  92. }
  93. return false;
  94. }
  95. bool SetBlockDeviceReadOnly(const std::string& blockdev) {
  96. android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC)));
  97. if (fd < 0) {
  98. return false;
  99. }
  100. int ON = 1;
  101. return ioctl(fd, BLKROSET, &ON) == 0;
  102. }
  103. } // namespace fs_mgr
  104. } // namespace android