• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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/bootconfig_args.h"
18 
19 #include <array>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "common/libs/utils/environment.h"
25 #include "common/libs/utils/files.h"
26 #include "host/libs/config/cuttlefish_config.h"
27 #include "host/libs/config/known_paths.h"
28 #include "host/libs/vm_manager/crosvm_manager.h"
29 #include "host/libs/vm_manager/qemu_manager.h"
30 #include "host/libs/vm_manager/vm_manager.h"
31 
32 namespace cuttlefish {
33 
34 using vm_manager::CrosvmManager;
35 using vm_manager::QemuManager;
36 
37 namespace {
38 
39 template <typename T>
AppendMapWithReplacement(T * destination,const T & source)40 void AppendMapWithReplacement(T* destination, const T& source) {
41   for (const auto& [k, v] : source) {
42     (*destination)[k] = v;
43   }
44 }
45 
46 // TODO(schuffelen): Move more of this into host/libs/vm_manager, as a
47 // substitute for the vm_manager comparisons.
VmManagerBootconfig(const CuttlefishConfig::InstanceSpecific & instance)48 Result<std::unordered_map<std::string, std::string>> VmManagerBootconfig(
49     const CuttlefishConfig::InstanceSpecific& instance) {
50   std::unordered_map<std::string, std::string> bootconfig_args;
51   if (instance.console()) {
52     bootconfig_args["androidboot.console"] = instance.console_dev();
53     bootconfig_args["androidboot.serialconsole"] = "1";
54   } else {
55     // Specify an invalid path under /dev, so the init process will disable the
56     // console service due to the console not being found. On physical devices,
57     // *and on older kernels* it is enough to not specify androidboot.console=
58     // *and* not specify the console= kernel command line parameter, because
59     // the console and kernel dmesg are muxed. However, on cuttlefish, we don't
60     // need to mux, and would prefer to retain the kernel dmesg logging, so we
61     // must work around init falling back to the check for /dev/console (which
62     // we'll always have).
63     //bootconfig_args["androidboot.console"] = "invalid";
64     // The bug above has been fixed in Android 14 and later so we can just
65     // specify androidboot.serialconsole=0 instead.
66     bootconfig_args["androidboot.serialconsole"] = "0";
67   }
68   return bootconfig_args;
69 }
70 
71 }  // namespace
72 
BootconfigArgsFromConfig(const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance)73 Result<std::unordered_map<std::string, std::string>> BootconfigArgsFromConfig(
74     const CuttlefishConfig& config,
75     const CuttlefishConfig::InstanceSpecific& instance) {
76   std::unordered_map<std::string, std::string> bootconfig_args;
77 
78   AppendMapWithReplacement(&bootconfig_args,
79                            CF_EXPECT(VmManagerBootconfig(instance)));
80 
81   auto vmm =
82       vm_manager::GetVmManager(config.vm_manager(), instance.target_arch());
83   AppendMapWithReplacement(&bootconfig_args,
84                            CF_EXPECT(vmm->ConfigureBootDevices(
85                                instance.virtual_disk_paths().size(),
86                                instance.hwcomposer() != kHwComposerNone)));
87 
88   AppendMapWithReplacement(&bootconfig_args,
89                            CF_EXPECT(vmm->ConfigureGraphics(instance)));
90 
91   bootconfig_args["androidboot.serialno"] = instance.serial_number();
92   bootconfig_args["androidboot.ddr_size"] =
93       std::to_string(instance.ddr_mem_mb()) + "MB";
94 
95   // TODO(b/131884992): update to specify multiple once supported.
96   const auto display_configs = instance.display_configs();
97   if (!display_configs.empty()) {
98     bootconfig_args["androidboot.lcd_density"] =
99         std::to_string(display_configs[0].dpi);
100   }
101 
102   bootconfig_args["androidboot.setupwizard_mode"] = instance.setupwizard_mode();
103 
104   bootconfig_args["androidboot.enable_bootanimation"] =
105       std::to_string(instance.enable_bootanimation());
106 
107   if (!instance.guest_enforce_security()) {
108     bootconfig_args["androidboot.selinux"] = "permissive";
109   }
110 
111   if (instance.tombstone_receiver_port()) {
112     bootconfig_args["androidboot.vsock_tombstone_port"] =
113         std::to_string(instance.tombstone_receiver_port());
114   }
115 
116   const auto enable_confui =
117       (config.vm_manager() == QemuManager::name() ? 0 : 1);
118   bootconfig_args["androidboot.enable_confirmationui"] =
119       std::to_string(enable_confui);
120 
121   if (instance.config_server_port()) {
122     bootconfig_args["androidboot.cuttlefish_config_server_port"] =
123         std::to_string(instance.config_server_port());
124   }
125 
126   if (instance.keyboard_server_port()) {
127     bootconfig_args["androidboot.vsock_keyboard_port"] =
128         std::to_string(instance.keyboard_server_port());
129   }
130 
131   if (instance.touch_server_port()) {
132     bootconfig_args["androidboot.vsock_touch_port"] =
133         std::to_string(instance.touch_server_port());
134   }
135 
136   if (instance.audiocontrol_server_port()) {
137     bootconfig_args["androidboot.vendor.audiocontrol.server.cid"] =
138         std::to_string(instance.vsock_guest_cid());
139     bootconfig_args["androidboot.vendor.audiocontrol.server.port"] =
140         std::to_string(instance.audiocontrol_server_port());
141   }
142 
143   if (!instance.enable_audio()) {
144     bootconfig_args["androidboot.audio.tinyalsa.ignore_output"] = "true";
145     bootconfig_args["androidboot.audio.tinyalsa.simulate_input"] = "true";
146   }
147 
148   if (instance.camera_server_port()) {
149     bootconfig_args["androidboot.vsock_camera_port"] =
150         std::to_string(instance.camera_server_port());
151     bootconfig_args["androidboot.vsock_camera_cid"] =
152         std::to_string(instance.vsock_guest_cid());
153   }
154 
155   if (instance.enable_modem_simulator() &&
156       instance.modem_simulator_ports() != "") {
157     bootconfig_args["androidboot.modem_simulator_ports"] =
158         instance.modem_simulator_ports();
159   }
160 
161   // Once all Cuttlefish kernel versions are at least 5.15, filename encryption
162   // will not need to be set conditionally. HCTR2 will always be available.
163   // At that point fstab.cf.f2fs.cts and fstab.cf.ext4.cts can be removed.
164   std::string fstab_suffix = fmt::format("cf.{}.{}", instance.userdata_format(),
165                                          instance.filename_encryption_mode());
166 
167   bootconfig_args["androidboot.fstab_suffix"] = fstab_suffix;
168 
169   bootconfig_args["androidboot.wifi_mac_prefix"] =
170       std::to_string(instance.wifi_mac_prefix());
171 
172   // Non-native architecture implies a significantly slower execution speed, so
173   // set a large timeout multiplier.
174   if (!IsHostCompatible(instance.target_arch())) {
175     bootconfig_args["androidboot.hw_timeout_multiplier"] = "50";
176   }
177 
178   // TODO(b/217564326): improve this checks for a hypervisor in the VM.
179   if (instance.target_arch() == Arch::X86 ||
180       instance.target_arch() == Arch::X86_64) {
181     bootconfig_args["androidboot.hypervisor.version"] =
182         "cf-" + config.vm_manager();
183     bootconfig_args["androidboot.hypervisor.vm.supported"] = "1";
184   } else {
185     bootconfig_args["androidboot.hypervisor.vm.supported"] = "0";
186   }
187   bootconfig_args["androidboot.hypervisor.protected_vm.supported"] = "0";
188   if (!instance.kernel_path().empty()) {
189     bootconfig_args["androidboot.kernel_hotswapped"] = "1";
190   }
191   if (!instance.initramfs_path().empty()) {
192     bootconfig_args["androidboot.ramdisk_hotswapped"] = "1";
193   }
194 
195   for (const std::string& kv : config.extra_bootconfig_args()) {
196     if (kv.empty()) {
197       continue;
198     }
199     const auto& parts = android::base::Split(kv, "=");
200     CF_EXPECT_EQ(parts.size(), 2,
201                  "Failed to parse --extra_bootconfig_args: \"" << kv << "\"");
202     bootconfig_args[parts[0]] = parts[1];
203   }
204 
205   return bootconfig_args;
206 }
207 
BootconfigArgsString(const std::unordered_map<std::string,std::string> & args,const std::string & separator)208 Result<std::string> BootconfigArgsString(
209     const std::unordered_map<std::string, std::string>& args,
210     const std::string& separator) {
211   std::vector<std::string> combined_args;
212   for (const auto& [k, v] : args) {
213     CF_EXPECT(!v.empty(), "Found empty bootconfig value for " << k);
214     combined_args.push_back(k + "=" + v);
215   }
216   return android::base::Join(combined_args, separator);
217 }
218 
219 }  // namespace cuttlefish
220