• 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_boot_config(const std::string & cmdline)29 std::vector<std::pair<std::string, std::string>> fs_mgr_parse_boot_config(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_get_boot_config_from_kernel(const std::string & cmdline,const std::string & android_key,std::string * out_val)63 bool fs_mgr_get_boot_config_from_kernel(const std::string& cmdline, const std::string& android_key,
64                                         std::string* out_val) {
65     FS_MGR_CHECK(out_val != nullptr);
66 
67     const std::string cmdline_key("androidboot." + android_key);
68     for (const auto& [key, value] : fs_mgr_parse_boot_config(cmdline)) {
69         if (key == cmdline_key) {
70             *out_val = value;
71             return true;
72         }
73     }
74 
75     *out_val = "";
76     return false;
77 }
78 
79 // Tries to get the given boot config value from kernel cmdline.
80 // Returns true if successfully found, false otherwise.
fs_mgr_get_boot_config_from_kernel_cmdline(const std::string & key,std::string * out_val)81 bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
82     std::string cmdline;
83     if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) return false;
84     if (!cmdline.empty() && cmdline.back() == '\n') {
85         cmdline.pop_back();
86     }
87     return fs_mgr_get_boot_config_from_kernel(cmdline, key, out_val);
88 }
89 
90 // Tries to get the boot config value in device tree, properties and
91 // kernel cmdline (in that order).  Returns 'true' if successfully
92 // found, 'false' otherwise.
fs_mgr_get_boot_config(const std::string & key,std::string * out_val)93 bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val) {
94     FS_MGR_CHECK(out_val != nullptr);
95 
96     // firstly, check the device tree
97     if (is_dt_compatible()) {
98         std::string file_name = get_android_dt_dir() + "/" + key;
99         if (android::base::ReadFileToString(file_name, out_val)) {
100             if (!out_val->empty()) {
101                 out_val->pop_back();  // Trims the trailing '\0' out.
102                 return true;
103             }
104         }
105     }
106 
107     // next, check if we have "ro.boot" property already
108     *out_val = android::base::GetProperty("ro.boot." + key, "");
109     if (!out_val->empty()) {
110         return true;
111     }
112 
113     // finally, fallback to kernel cmdline, properties may not be ready yet
114     if (fs_mgr_get_boot_config_from_kernel_cmdline(key, out_val)) {
115         return true;
116     }
117 
118     return false;
119 }
120