Home | History | Annotate | Download | only in fs_mgr
      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 <string>
     18 
     19 #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 
     26 // Tries to get the given boot config value from kernel cmdline.
     27 // Returns true if successfully found, false otherwise.
     28 bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
     29     FS_MGR_CHECK(out_val != nullptr);
     30 
     31     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 
     45     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
     51 bool 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 
     65     // lastly, check the device tree
     66     if (is_dt_compatible()) {
     67         std::string file_name = get_android_dt_dir() + "/" + key;
     68         if (android::base::ReadFileToString(file_name, out_val)) {
     69             if (!out_val->empty()) {
     70                 out_val->pop_back();  // Trims the trailing '\0' out.
     71                 return true;
     72             }
     73         }
     74     }
     75 
     76     return false;
     77 }
     78