1 /*
2 * Copyright (C) 2017 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 #include <android-base/file.h>
18 #include <android-base/stringprintf.h>
19 #include <android-base/strings.h>
20 #include <android-base/properties.h>
21
22 #include "fs_mgr_priv.h"
23
24 // Tries to get the boot config value in properties, kernel cmdline and
25 // device tree (in that order). returns 'true' if successfully found, 'false'
26 // otherwise
fs_mgr_get_boot_config(const std::string & key,std::string * out_val)27 bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val) {
28 FS_MGR_CHECK(out_val != nullptr);
29
30 // first check if we have "ro.boot" property already
31 *out_val = android::base::GetProperty("ro.boot." + key, "");
32 if (!out_val->empty()) {
33 return true;
34 }
35
36 // fallback to kernel cmdline, properties may not be ready yet
37 std::string cmdline;
38 std::string cmdline_key("androidboot." + key);
39 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
40 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
41 std::vector<std::string> pieces = android::base::Split(entry, "=");
42 if (pieces.size() == 2) {
43 if (pieces[0] == cmdline_key) {
44 *out_val = pieces[1];
45 return true;
46 }
47 }
48 }
49 }
50
51 // lastly, check the device tree
52 if (is_dt_compatible()) {
53 std::string file_name = kAndroidDtDir + "/" + key;
54 // DT entries terminate with '\0' but so do the properties
55 if (android::base::ReadFileToString(file_name, out_val)) {
56 return true;
57 }
58
59 LINFO << "Error finding '" << key << "' in device tree";
60 }
61
62 return false;
63 }
64