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 "host/libs/config/kernel_args.h"
18 
19 #include <array>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "host/libs/config/cuttlefish_config.h"
25 #include "host/libs/vm_manager/vm_manager.h"
26 
27 using cuttlefish::vm_manager::VmManager;
28 
29 template<typename T>
AppendVector(std::vector<T> * destination,const std::vector<T> & source)30 static void AppendVector(std::vector<T>* destination, const std::vector<T>& source) {
31   destination->insert(destination->end(), source.begin(), source.end());
32 }
33 
34 template<typename S, typename T>
concat(const S & s,const T & t)35 static std::string concat(const S& s, const T& t) {
36   std::ostringstream os;
37   os << s << t;
38   return os.str();
39 }
40 
mac_to_str(const std::array<unsigned char,6> & mac)41 static std::string mac_to_str(const std::array<unsigned char, 6>& mac) {
42   std::ostringstream stream;
43   stream << std::hex << (int) mac[0];
44   for (int i = 1; i < 6; i++) {
45     stream << ":" << std::hex << (int) mac[i];
46   }
47   return stream.str();
48 }
49 
KernelCommandLineFromConfig(const cuttlefish::CuttlefishConfig & config)50 std::vector<std::string> KernelCommandLineFromConfig(const cuttlefish::CuttlefishConfig& config) {
51   auto instance = config.ForDefaultInstance();
52   std::vector<std::string> kernel_cmdline;
53 
54   AppendVector(&kernel_cmdline, config.vm_manager_kernel_cmdline());
55   AppendVector(&kernel_cmdline, config.boot_image_kernel_cmdline());
56   AppendVector(&kernel_cmdline,
57                VmManager::ConfigureGpuMode(config.vm_manager(), config.gpu_mode()));
58   AppendVector(&kernel_cmdline, VmManager::ConfigureBootDevices(config.vm_manager()));
59 
60   kernel_cmdline.push_back(concat("androidboot.serialno=", instance.serial_number()));
61   kernel_cmdline.push_back(concat("androidboot.lcd_density=", config.dpi()));
62   kernel_cmdline.push_back(concat(
63       "androidboot.setupwizard_mode=", config.setupwizard_mode()));
64   if (!config.use_bootloader()) {
65     std::string slot_suffix;
66     if (config.boot_slot().empty()) {
67       slot_suffix = "_a";
68     } else {
69       slot_suffix = "_" + config.boot_slot();
70     }
71     kernel_cmdline.push_back(concat("androidboot.slot_suffix=", slot_suffix));
72   }
73   kernel_cmdline.push_back(concat("loop.max_part=", config.loop_max_part()));
74   if (!config.guest_enforce_security()) {
75     kernel_cmdline.push_back("androidboot.selinux=permissive");
76   }
77   if (config.guest_audit_security()) {
78     kernel_cmdline.push_back("audit=1");
79   } else {
80     kernel_cmdline.push_back("audit=0");
81   }
82   if (config.guest_force_normal_boot()) {
83     kernel_cmdline.push_back("androidboot.force_normal_boot=1");
84   }
85 
86   if (config.enable_tombstone_receiver() && instance.tombstone_receiver_port()) {
87     kernel_cmdline.push_back("androidboot.tombstone_transmit=1");
88     kernel_cmdline.push_back(concat("androidboot.vsock_tombstone_port=", instance.tombstone_receiver_port()));
89   } else {
90     kernel_cmdline.push_back("androidboot.tombstone_transmit=0");
91   }
92 
93   if (instance.config_server_port()) {
94     kernel_cmdline.push_back(concat("androidboot.cuttlefish_config_server_port=", instance.config_server_port()));
95   }
96 
97   if (config.tpm_binary() != "" && instance.tpm_port()) {
98     kernel_cmdline.push_back(concat("androidboot.tpm_vsock_port=", instance.tpm_port()));
99   }
100 
101   if (instance.keyboard_server_port()) {
102     kernel_cmdline.push_back(concat("androidboot.vsock_keyboard_port=", instance.keyboard_server_port()));
103   }
104 
105   if (instance.touch_server_port()) {
106     kernel_cmdline.push_back(concat("androidboot.vsock_touch_port=", instance.touch_server_port()));
107   }
108 
109   if (instance.frames_server_port()) {
110     kernel_cmdline.push_back(concat("androidboot.vsock_frames_port=", instance.frames_server_port()));
111   }
112 
113   kernel_cmdline.push_back(concat("androidboot.vsock_keymaster_port=",
114                                   instance.keymaster_vsock_port()));
115 
116   kernel_cmdline.push_back(concat("androidboot.vsock_gatekeeper_port=",
117                                   instance.gatekeeper_vsock_port()));
118 
119   if (config.enable_modem_simulator() &&
120       instance.modem_simulator_ports() != "") {
121     kernel_cmdline.push_back(concat("androidboot.modem_simulator_ports=",
122                                     instance.modem_simulator_ports()));
123   }
124 
125   // TODO(b/158131610): Set this in crosvm instead
126   kernel_cmdline.push_back(concat("androidboot.wifi_mac_address=",
127                                   mac_to_str(instance.wifi_mac_address())));
128 
129   kernel_cmdline.push_back("androidboot.verifiedbootstate=orange");
130 
131   AppendVector(&kernel_cmdline, config.extra_kernel_cmdline());
132 
133   return kernel_cmdline;
134 }
135