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/vm_manager/crosvm_manager.h"
28 #include "host/libs/vm_manager/qemu_manager.h"
29 #include "host/libs/vm_manager/vm_manager.h"
30
31 namespace cuttlefish {
32
33 using vm_manager::CrosvmManager;
34 using vm_manager::QemuManager;
35
36 namespace {
37
38 template <typename T>
AppendVector(std::vector<T> * destination,const std::vector<T> & source)39 void AppendVector(std::vector<T>* destination, const std::vector<T>& source) {
40 destination->insert(destination->end(), source.begin(), source.end());
41 }
42
43 template <typename S, typename T>
concat(const S & s,const T & t)44 std::string concat(const S& s, const T& t) {
45 std::ostringstream os;
46 os << s << t;
47 return os.str();
48 }
49
mac_to_str(const std::array<unsigned char,6> & mac)50 std::string mac_to_str(const std::array<unsigned char, 6>& mac) {
51 std::ostringstream stream;
52 stream << std::hex << (int)mac[0];
53 for (int i = 1; i < 6; i++) {
54 stream << ":" << std::hex << (int)mac[i];
55 }
56 return stream.str();
57 }
58
59 // TODO(schuffelen): Move more of this into host/libs/vm_manager, as a
60 // substitute for the vm_manager comparisons.
VmManagerBootconfig(const CuttlefishConfig & config)61 std::vector<std::string> VmManagerBootconfig(const CuttlefishConfig& config) {
62 std::vector<std::string> vm_manager_cmdline;
63 if (config.console()) {
64 vm_manager_cmdline.push_back("androidboot.console=" + config.console_dev());
65 } else {
66 // Specify an invalid path under /dev, so the init process will disable the
67 // console service due to the console not being found. On physical devices,
68 // it is enough to not specify androidboot.console= *and* not specify the
69 // console= kernel command line parameter, because the console and kernel
70 // dmesg are muxed. However, on cuttlefish, we don't need to mux, and would
71 // prefer to retain the kernel dmesg logging, so we must work around init
72 // falling back to the check for /dev/console (which we'll always have).
73 vm_manager_cmdline.push_back("androidboot.console=invalid");
74 }
75 return vm_manager_cmdline;
76 }
77
78 } // namespace
79
BootconfigArgsFromConfig(const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance)80 std::vector<std::string> BootconfigArgsFromConfig(
81 const CuttlefishConfig& config,
82 const CuttlefishConfig::InstanceSpecific& instance) {
83 std::vector<std::string> bootconfig_args;
84
85 AppendVector(&bootconfig_args, VmManagerBootconfig(config));
86 auto vmm = vm_manager::GetVmManager(config.vm_manager(), config.target_arch());
87 bootconfig_args.push_back(
88 vmm->ConfigureBootDevices(instance.virtual_disk_paths().size()));
89 AppendVector(&bootconfig_args, vmm->ConfigureGpuMode(config.gpu_mode()));
90
91 bootconfig_args.push_back(
92 concat("androidboot.serialno=", instance.serial_number()));
93 bootconfig_args.push_back(concat("androidboot.lcd_density=", config.dpi()));
94 bootconfig_args.push_back(
95 concat("androidboot.setupwizard_mode=", config.setupwizard_mode()));
96 if (!config.guest_enforce_security()) {
97 bootconfig_args.push_back("androidboot.selinux=permissive");
98 }
99
100 if (instance.tombstone_receiver_port()) {
101 bootconfig_args.push_back(concat("androidboot.vsock_tombstone_port=",
102 instance.tombstone_receiver_port()));
103 }
104
105 if (instance.config_server_port()) {
106 bootconfig_args.push_back(
107 concat("androidboot.cuttlefish_config_server_port=",
108 instance.config_server_port()));
109 }
110
111 if (instance.keyboard_server_port()) {
112 bootconfig_args.push_back(concat("androidboot.vsock_keyboard_port=",
113 instance.keyboard_server_port()));
114 }
115
116 if (instance.touch_server_port()) {
117 bootconfig_args.push_back(
118 concat("androidboot.vsock_touch_port=", instance.touch_server_port()));
119 }
120
121 if (config.enable_vehicle_hal_grpc_server() &&
122 instance.vehicle_hal_server_port() &&
123 FileExists(config.vehicle_hal_grpc_server_binary())) {
124 constexpr int vehicle_hal_server_cid = 2;
125 bootconfig_args.push_back(concat(
126 "androidboot.vendor.vehiclehal.server.cid=", vehicle_hal_server_cid));
127 bootconfig_args.push_back(
128 concat("androidboot.vendor.vehiclehal.server.port=",
129 instance.vehicle_hal_server_port()));
130 }
131
132 if (instance.audiocontrol_server_port()) {
133 bootconfig_args.push_back(
134 concat("androidboot.vendor.audiocontrol.server.cid=",
135 instance.vsock_guest_cid()));
136 bootconfig_args.push_back(
137 concat("androidboot.vendor.audiocontrol.server.port=",
138 instance.audiocontrol_server_port()));
139 }
140
141 if (instance.frames_server_port()) {
142 bootconfig_args.push_back(concat("androidboot.vsock_frames_port=",
143 instance.frames_server_port()));
144 }
145
146 if (config.enable_modem_simulator() &&
147 instance.modem_simulator_ports() != "") {
148 bootconfig_args.push_back(concat("androidboot.modem_simulator_ports=",
149 instance.modem_simulator_ports()));
150 }
151
152 // TODO(b/158131610): Set this in crosvm instead
153 bootconfig_args.push_back(concat("androidboot.wifi_mac_address=",
154 mac_to_str(instance.wifi_mac_address())));
155
156 bootconfig_args.push_back("androidboot.verifiedbootstate=orange");
157
158 // Non-native architecture implies a significantly slower execution speed, so
159 // set a large timeout multiplier.
160 if (!IsHostCompatible(config.target_arch())) {
161 bootconfig_args.push_back("androidboot.hw_timeout_multiplier=50");
162 }
163
164 // TODO(b/173815685): Create an extra_bootconfig flag and add it to bootconfig
165
166 return bootconfig_args;
167 }
168
169 } // namespace cuttlefish
170