• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
18 #include <iterator>
19 #include <string>
20 #include <vector>
21 
22 #include <android-base/file.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <android-base/properties.h>
26 
27 #include "fs_mgr_priv.h"
28 
fs_mgr_parse_cmdline(const std::string & cmdline)29 std::vector<std::pair<std::string, std::string>> fs_mgr_parse_cmdline(const std::string& cmdline) {
30     static constexpr char quote = '"';
31 
32     std::vector<std::pair<std::string, std::string>> result;
33     size_t base = 0;
34     while (true) {
35         // skip quoted spans
36         auto found = base;
37         while (((found = cmdline.find_first_of(" \"", found)) != cmdline.npos) &&
38                (cmdline[found] == quote)) {
39             // unbalanced quote is ok
40             if ((found = cmdline.find(quote, found + 1)) == cmdline.npos) break;
41             ++found;
42         }
43         std::string piece;
44         auto source = cmdline.substr(base, found - base);
45         std::remove_copy(source.begin(), source.end(),
46                          std::back_insert_iterator<std::string>(piece), quote);
47         auto equal_sign = piece.find('=');
48         if (equal_sign == piece.npos) {
49             if (!piece.empty()) {
50                 // no difference between <key> and <key>=
51                 result.emplace_back(std::move(piece), "");
52             }
53         } else {
54             result.emplace_back(piece.substr(0, equal_sign), piece.substr(equal_sign + 1));
55         }
56         if (found == cmdline.npos) break;
57         base = found + 1;
58     }
59 
60     return result;
61 }
62 
fs_mgr_parse_proc_bootconfig(const std::string & cmdline)63 std::vector<std::pair<std::string, std::string>> fs_mgr_parse_proc_bootconfig(
64         const std::string& cmdline) {
65     static constexpr char quote = '"';
66 
67     std::vector<std::pair<std::string, std::string>> result;
68     for (auto& line : android::base::Split(cmdline, "\n")) {
69         line.erase(std::remove(line.begin(), line.end(), quote), line.end());
70         auto equal_sign = line.find('=');
71         if (equal_sign == line.npos) {
72             if (!line.empty()) {
73                 // no difference between <key> and <key>=
74                 result.emplace_back(std::move(line), "");
75             }
76         } else {
77             result.emplace_back(android::base::Trim(line.substr(0, equal_sign)),
78                                 android::base::Trim(line.substr(equal_sign + 1)));
79         }
80     }
81 
82     return result;
83 }
84 
fs_mgr_get_boot_config_from_bootconfig(const std::string & bootconfig,const std::string & android_key,std::string * out_val)85 bool fs_mgr_get_boot_config_from_bootconfig(const std::string& bootconfig,
86                                             const std::string& android_key, std::string* out_val) {
87     FS_MGR_CHECK(out_val != nullptr);
88 
89     const std::string bootconfig_key("androidboot." + android_key);
90     for (const auto& [key, value] : fs_mgr_parse_proc_bootconfig(bootconfig)) {
91         if (key == bootconfig_key) {
92             *out_val = value;
93             return true;
94         }
95     }
96 
97     *out_val = "";
98     return false;
99 }
100 
fs_mgr_get_boot_config_from_kernel(const std::string & cmdline,const std::string & android_key,std::string * out_val)101 bool fs_mgr_get_boot_config_from_kernel(const std::string& cmdline, const std::string& android_key,
102                                         std::string* out_val) {
103     FS_MGR_CHECK(out_val != nullptr);
104 
105     const std::string cmdline_key("androidboot." + android_key);
106     for (const auto& [key, value] : fs_mgr_parse_cmdline(cmdline)) {
107         if (key == cmdline_key) {
108             *out_val = value;
109             return true;
110         }
111     }
112 
113     *out_val = "";
114     return false;
115 }
116 
117 // Tries to get the given boot config value from bootconfig.
118 // Returns true if successfully found, false otherwise.
fs_mgr_get_boot_config_from_bootconfig_source(const std::string & key,std::string * out_val)119 bool fs_mgr_get_boot_config_from_bootconfig_source(const std::string& key, std::string* out_val) {
120     std::string bootconfig;
121     if (!android::base::ReadFileToString("/proc/bootconfig", &bootconfig)) return false;
122     if (!bootconfig.empty() && bootconfig.back() == '\n') {
123         bootconfig.pop_back();
124     }
125     return fs_mgr_get_boot_config_from_bootconfig(bootconfig, key, out_val);
126 }
127 
128 // Tries to get the given boot config value from kernel cmdline.
129 // Returns true if successfully found, false otherwise.
fs_mgr_get_boot_config_from_kernel_cmdline(const std::string & key,std::string * out_val)130 bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
131     std::string cmdline;
132     if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) return false;
133     if (!cmdline.empty() && cmdline.back() == '\n') {
134         cmdline.pop_back();
135     }
136     return fs_mgr_get_boot_config_from_kernel(cmdline, key, out_val);
137 }
138 
139 // Tries to get the boot config value in device tree, properties and
140 // kernel cmdline (in that order).  Returns 'true' if successfully
141 // found, 'false' otherwise.
fs_mgr_get_boot_config(const std::string & key,std::string * out_val)142 bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val) {
143     FS_MGR_CHECK(out_val != nullptr);
144 
145     // firstly, check the device tree
146     if (is_dt_compatible()) {
147         std::string file_name = get_android_dt_dir() + "/" + key;
148         if (android::base::ReadFileToString(file_name, out_val)) {
149             if (!out_val->empty()) {
150                 out_val->pop_back();  // Trims the trailing '\0' out.
151                 return true;
152             }
153         }
154     }
155 
156     // next, check if we have "ro.boot" property already
157     *out_val = android::base::GetProperty("ro.boot." + key, "");
158     if (!out_val->empty()) {
159         return true;
160     }
161 
162     // next, check if we have the property in bootconfig
163     if (fs_mgr_get_boot_config_from_bootconfig_source(key, out_val)) {
164         return true;
165     }
166 
167     // finally, fallback to kernel cmdline, properties may not be ready yet
168     if (fs_mgr_get_boot_config_from_kernel_cmdline(key, out_val)) {
169         return true;
170     }
171 
172     return false;
173 }
174