blob: 9c5d3f3bbd58bbf041ca1cdbd1918104a88265bb [file] [log] [blame]
Sandeep Patil9de748f2017-02-16 19:15:29 -08001/*
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
Bowgo Tsaif5596552017-07-13 15:05:08 +080017#include <string>
18
Sandeep Patil9de748f2017-02-16 19:15:29 -080019#include <android-base/file.h>
20#include <android-base/stringprintf.h>
21#include <android-base/strings.h>
22#include <android-base/properties.h>
23
24#include "fs_mgr_priv.h"
25
Yu Ningc01022a2017-07-26 17:54:08 +080026// Tries to get the given boot config value from kernel cmdline.
27// Returns true if successfully found, false otherwise.
28bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
Sandeep Patil9de748f2017-02-16 19:15:29 -080029 FS_MGR_CHECK(out_val != nullptr);
30
Sandeep Patil9de748f2017-02-16 19:15:29 -080031 std::string cmdline;
32 std::string cmdline_key("androidboot." + key);
33 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
34 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
35 std::vector<std::string> pieces = android::base::Split(entry, "=");
36 if (pieces.size() == 2) {
37 if (pieces[0] == cmdline_key) {
38 *out_val = pieces[1];
39 return true;
40 }
41 }
42 }
43 }
44
Yu Ningc01022a2017-07-26 17:54:08 +080045 return false;
46}
47
48// Tries to get the boot config value in properties, kernel cmdline and
49// device tree (in that order). returns 'true' if successfully found, 'false'
50// otherwise
51bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val) {
52 FS_MGR_CHECK(out_val != nullptr);
53
54 // first check if we have "ro.boot" property already
55 *out_val = android::base::GetProperty("ro.boot." + key, "");
56 if (!out_val->empty()) {
57 return true;
58 }
59
60 // fallback to kernel cmdline, properties may not be ready yet
61 if (fs_mgr_get_boot_config_from_kernel_cmdline(key, out_val)) {
62 return true;
63 }
64
Sandeep Patil9de748f2017-02-16 19:15:29 -080065 // lastly, check the device tree
Sandeep Patile396c602017-02-24 11:04:49 -080066 if (is_dt_compatible()) {
Yu Ningc01022a2017-07-26 17:54:08 +080067 std::string file_name = get_android_dt_dir() + "/" + key;
Sandeep Patil9de748f2017-02-16 19:15:29 -080068 if (android::base::ReadFileToString(file_name, out_val)) {
Bowgo Tsaif5596552017-07-13 15:05:08 +080069 if (!out_val->empty()) {
70 out_val->pop_back(); // Trims the trailing '\0' out.
71 return true;
72 }
Sandeep Patil9de748f2017-02-16 19:15:29 -080073 }
Sandeep Patil9de748f2017-02-16 19:15:29 -080074 }
75
76 return false;
77}