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 
17 #pragma once
18 
19 #include <chrono>
20 #include <string>
21 #include <vector>
22 
23 #ifdef HOST_TEST
24 #include <base/logging.h>
25 #else
26 #include <android-base/logging.h>
27 #endif
28 
29 #include <android-base/result.h>
30 
31 using android::base::ErrnoError;
32 using android::base::Result;
33 
34 #define FS_AVB_TAG "[libfs_avb]"
35 
36 // Logs a message to kernel
37 #define LINFO LOG(INFO) << FS_AVB_TAG
38 #define LWARNING LOG(WARNING) << FS_AVB_TAG
39 #define LERROR LOG(ERROR) << FS_AVB_TAG
40 #define LFATAL LOG(FATAL) << FS_AVB_TAG
41 
42 // Logs a message with strerror(errno) at the end
43 #define PINFO PLOG(INFO) << FS_AVB_TAG
44 #define PWARNING PLOG(WARNING) << FS_AVB_TAG
45 #define PERROR PLOG(ERROR) << FS_AVB_TAG
46 #define PFATAL PLOG(FATAL) << FS_AVB_TAG
47 
48 extern bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
49 
50 using namespace std::chrono_literals;
51 
52 namespace android {
53 namespace fs_mgr {
54 
55 bool NibbleValue(const char& c, uint8_t* value);
56 
57 bool HexToBytes(uint8_t* bytes, size_t bytes_len, const std::string& hex);
58 
59 std::string BytesToHex(const uint8_t* bytes, size_t bytes_len);
60 
61 enum class FileWaitMode { Exists, DoesNotExist };
62 bool WaitForFile(const std::string& filename, const std::chrono::milliseconds relative_timeout,
63                  FileWaitMode wait_mode = FileWaitMode::Exists);
64 
65 bool IsDeviceUnlocked();
66 
67 bool SetBlockDeviceReadOnly(const std::string& blockdev);
68 
69 // Returns a list of file under the dir, no order is guaranteed.
70 Result<std::vector<std::string>> ListFiles(const std::string& dir);
71 
72 }  // namespace fs_mgr
73 }  // namespace android
74