1 /*
2  * Copyright (C) 2018 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 #pragma once
17 
18 #include <array>
19 #include <memory>
20 #include <string>
21 #include <set>
22 #include <vector>
23 
24 namespace Json {
25 class Value;
26 }
27 
28 namespace vsoc {
29 
30 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1";
31 constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
32 constexpr char kVsocUserPrefix[] = "vsoc-";
33 constexpr char kBootStartedMessage[] ="VIRTUAL_DEVICE_BOOT_STARTED";
34 constexpr char kBootCompletedMessage[] = "VIRTUAL_DEVICE_BOOT_COMPLETED";
35 constexpr char kBootFailedMessage[] = "VIRTUAL_DEVICE_BOOT_FAILED";
36 constexpr char kMobileNetworkConnectedMessage[] =
37     "VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED";
38 constexpr char kWifiConnectedMessage[] =
39     "VIRTUAL_DEVICE_NETWORK_WIFI_CONNECTED";
40 constexpr char kInternalDirName[] = "internal";
41 
42 
43 enum class AdbMode {
44   VsockTunnel,
45   VsockHalfTunnel,
46   NativeVsock,
47   Unknown,
48 };
49 
50 // Holds the configuration of the cuttlefish instances.
51 class CuttlefishConfig {
52  public:
53   static const CuttlefishConfig* Get();
54 
55   CuttlefishConfig();
56   CuttlefishConfig(CuttlefishConfig&&);
57   ~CuttlefishConfig();
58   CuttlefishConfig& operator=(CuttlefishConfig&&);
59 
60   // Saves the configuration object in a file, it can then be read in other
61   // processes by passing the --config_file option.
62   bool SaveToFile(const std::string& file) const;
63 
64   std::string assembly_dir() const;
65   void set_assembly_dir(const std::string& assembly_dir);
66 
67   std::string AssemblyPath(const std::string&) const;
68 
69   std::string composite_disk_path() const;
70 
71   std::string vm_manager() const;
72   void set_vm_manager(const std::string& name);
73 
74   std::string gpu_mode() const;
75   void set_gpu_mode(const std::string& name);
76 
77   std::string serial_number() const;
78   void set_serial_number(const std::string& serial_number);
79 
80   std::string wayland_socket() const;
81   void set_wayland_socket(const std::string& path);
82 
83   std::string x_display() const;
84   void set_x_display(const std::string& address);
85 
86   int cpus() const;
87   void set_cpus(int cpus);
88 
89   int memory_mb() const;
90   void set_memory_mb(int memory_mb);
91 
92   int dpi() const;
93   void set_dpi(int dpi);
94 
95   int x_res() const;
96   void set_x_res(int x_res);
97 
98   int y_res() const;
99   void set_y_res(int y_res);
100 
101   int refresh_rate_hz() const;
102   void set_refresh_rate_hz(int refresh_rate_hz);
103 
104   // Returns kernel image extracted from the boot image or the user-provided one
105   // if given by command line to the launcher. This function should not be used
106   // to get the kernel image the vmm should boot, GetKernelImageToUse() should
107   // be used instead.
108   std::string kernel_image_path() const;
109   void set_kernel_image_path(const std::string& kernel_image_path);
110 
111   bool decompress_kernel() const;
112   void set_decompress_kernel(bool decompress_kernel);
113 
114   // Returns the path to the kernel image that should be given to the vm manager
115   // to boot, takes into account whether the original image was decompressed or
116   // not.
GetKernelImageToUse()117   std::string GetKernelImageToUse() const {
118     return decompress_kernel() ? decompressed_kernel_image_path()
119                                : kernel_image_path();
120   }
121 
122   std::string decompressed_kernel_image_path() const;
123   void set_decompressed_kernel_image_path(const std::string& path);
124 
125   bool use_unpacked_kernel() const;
126   void set_use_unpacked_kernel(bool use_unpacked_kernel);
127 
128   std::string gdb_flag() const;
129   void set_gdb_flag(const std::string& gdb);
130 
131   std::string ramdisk_image_path() const;
132   void set_ramdisk_image_path(const std::string& ramdisk_image_path);
133 
134   std::string initramfs_path() const;
135   void set_initramfs_path(const std::string& initramfs_path);
136 
137   std::string final_ramdisk_path() const;
138   void set_final_ramdisk_path(const std::string& final_ramdisk_path);
139 
140   std::string vendor_ramdisk_image_path() const;
141   void set_vendor_ramdisk_image_path(const std::string&
142     vendor_ramdisk_image_path);
143 
144   bool deprecated_boot_completed() const;
145   void set_deprecated_boot_completed(bool deprecated_boot_completed);
146 
147   std::string logcat_receiver_binary() const;
148   void set_logcat_receiver_binary(const std::string& binary);
149 
150   std::string config_server_binary() const;
151   void set_config_server_binary(const std::string& binary);
152 
153   void set_cuttlefish_env_path(const std::string& path);
154   std::string cuttlefish_env_path() const;
155 
156   void set_adb_mode(const std::set<std::string>& modes);
157   std::set<AdbMode> adb_mode() const;
158 
159   void set_setupwizard_mode(const std::string& title);
160   std::string setupwizard_mode() const;
161 
162   void set_qemu_binary(const std::string& qemu_binary);
163   std::string qemu_binary() const;
164 
165   void set_crosvm_binary(const std::string& crosvm_binary);
166   std::string crosvm_binary() const;
167 
168   void set_console_forwarder_binary(const std::string& crosvm_binary);
169   std::string console_forwarder_binary() const;
170 
171   void set_kernel_log_monitor_binary(
172       const std::string& kernel_log_monitor_binary);
173   std::string kernel_log_monitor_binary() const;
174 
175   void set_enable_vnc_server(bool enable_vnc_server);
176   bool enable_vnc_server() const;
177 
178   void set_vnc_server_binary(const std::string& vnc_server_binary);
179   std::string vnc_server_binary() const;
180 
181   void set_enable_webrtc(bool enable_webrtc);
182   bool enable_webrtc() const;
183 
184   void set_webrtc_binary(const std::string& webrtc_binary);
185   std::string webrtc_binary() const;
186 
187   void set_webrtc_assets_dir(const std::string& webrtc_binary);
188   std::string webrtc_assets_dir() const;
189 
190   void set_webrtc_public_ip(const std::string& webrtc_public_ip);
191   std::string webrtc_public_ip() const;
192 
193   void set_webrtc_enable_adb_websocket(bool enable);
194   bool webrtc_enable_adb_websocket() const;
195 
196   void set_enable_vehicle_hal_grpc_server(bool enable_vhal_server);
197   bool enable_vehicle_hal_grpc_server() const;
198 
199   void set_vehicle_hal_grpc_server_binary(const std::string& vhal_server_binary);
200   std::string vehicle_hal_grpc_server_binary() const;
201 
202   void set_restart_subprocesses(bool restart_subprocesses);
203   bool restart_subprocesses() const;
204 
205   void set_run_adb_connector(bool run_adb_connector);
206   bool run_adb_connector() const;
207 
208   void set_adb_connector_binary(const std::string& adb_connector_binary);
209   std::string adb_connector_binary() const;
210 
211   void set_socket_vsock_proxy_binary(const std::string& binary);
212   std::string socket_vsock_proxy_binary() const;
213 
214   void set_run_as_daemon(bool run_as_daemon);
215   bool run_as_daemon() const;
216 
217   void set_data_policy(const std::string& data_policy);
218   std::string data_policy() const;
219 
220   void set_blank_data_image_mb(int blank_data_image_mb);
221   int blank_data_image_mb() const;
222 
223   void set_blank_data_image_fmt(const std::string& blank_data_image_fmt);
224   std::string blank_data_image_fmt() const;
225 
226   void set_logcat_mode(const std::string& mode);
227   std::string logcat_mode() const;
228 
229   void set_enable_tombstone_receiver(bool enable_tombstone_receiver);
230   bool enable_tombstone_receiver() const;
231 
232   void set_tombstone_receiver_binary(const std::string& binary);
233   std::string tombstone_receiver_binary() const;
234 
235   void set_use_bootloader(bool use_bootloader);
236   bool use_bootloader() const;
237 
238   void set_bootloader(const std::string& bootloader_path);
239   std::string bootloader() const;
240 
241   void set_boot_slot(const std::string& boot_slot);
242   std::string boot_slot() const;
243 
244   void set_loop_max_part(int loop_max_part);
245   int loop_max_part() const;
246 
247   void set_guest_enforce_security(bool guest_enforce_security);
248   bool guest_enforce_security() const;
249 
250   void set_guest_audit_security(bool guest_audit_security);
251   bool guest_audit_security() const;
252 
253   void set_guest_force_normal_boot(bool guest_force_normal_boot);
254   bool guest_force_normal_boot() const;
255 
256   void set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline);
257   std::vector<std::string> boot_image_kernel_cmdline() const;
258 
259   void set_extra_kernel_cmdline(std::string extra_cmdline);
260   std::vector<std::string> extra_kernel_cmdline() const;
261 
262   void set_webrtc_certs_dir(const std::string& certs_dir);
263   std::string webrtc_certs_dir() const;
264 
265   void set_dialog_certs_dir(const std::string& certs_dir);
266   std::string dialog_certs_dir() const;
267 
268   class InstanceSpecific;
269   class MutableInstanceSpecific;
270 
271   MutableInstanceSpecific ForInstance(int instance_num);
272   InstanceSpecific ForInstance(int instance_num) const;
273   InstanceSpecific ForDefaultInstance() const;
274 
275   std::vector<InstanceSpecific> Instances() const;
276 
277   // A view into an existing CuttlefishConfig object for a particular instance.
278   class InstanceSpecific {
279     const CuttlefishConfig* config_;
280     std::string id_;
281     friend InstanceSpecific CuttlefishConfig::ForInstance(int num) const;
282     friend InstanceSpecific CuttlefishConfig::ForDefaultInstance() const;
283     friend std::vector<InstanceSpecific> CuttlefishConfig::Instances() const;
284 
InstanceSpecific(const CuttlefishConfig * config,const std::string & id)285     InstanceSpecific(const CuttlefishConfig* config, const std::string& id)
286         : config_(config), id_(id) {}
287 
288     Json::Value* Dictionary();
289     const Json::Value* Dictionary() const;
290   public:
291     std::string serial_number() const;
292     int vnc_server_port() const;
293     // Port number to connect to the vehicle HAL server on the host
294     int vehicle_hal_server_port() const;
295     int host_port() const;
296     std::string adb_ip_and_port() const;
297     std::string adb_device_name() const;
298     std::string device_title() const;
299     std::string mobile_bridge_name() const;
300     std::string mobile_tap_name() const;
301     std::string wifi_tap_name() const;
302     int vsock_guest_cid() const;
303     std::string uuid() const;
304     std::string instance_name() const;
305     std::vector<std::string> virtual_disk_paths() const;
306 
307     // Returns the path to a file with the given name in the instance directory..
308     std::string PerInstancePath(const char* file_name) const;
309     std::string PerInstanceInternalPath(const char* file_name) const;
310 
311     std::string instance_dir() const;
312 
313     std::string instance_internal_dir() const;
314 
315     std::string touch_socket_path() const;
316     std::string keyboard_socket_path() const;
317     std::string frames_socket_path() const;
318 
319     std::string access_kregistry_path() const;
320 
321     std::string console_path() const;
322 
323     std::string logcat_path() const;
324 
325     std::string kernel_log_pipe_name() const;
326 
327     std::string console_pipe_name() const;
328 
329     std::string launcher_log_path() const;
330 
331     std::string launcher_monitor_socket_path() const;
332 
333     std::string sdcard_path() const;
334 
335     // Wifi MAC address inside the guest
336     std::array<unsigned char, 6> wifi_mac_address() const;
337   };
338 
339   // A view into an existing CuttlefishConfig object for a particular instance.
340   class MutableInstanceSpecific {
341     CuttlefishConfig* config_;
342     std::string id_;
343     friend MutableInstanceSpecific CuttlefishConfig::ForInstance(int num);
344 
MutableInstanceSpecific(CuttlefishConfig * config,const std::string & id)345     MutableInstanceSpecific(CuttlefishConfig* config, const std::string& id)
346         : config_(config), id_(id) {}
347 
348     Json::Value* Dictionary();
349   public:
350     void set_serial_number(const std::string& serial_number);
351     void set_vnc_server_port(int vnc_server_port);
352     void set_vehicle_hal_server_port(int vehicle_server_port);
353     void set_host_port(int host_port);
354     void set_adb_ip_and_port(const std::string& ip_port);
355     void set_device_title(const std::string& title);
356     void set_mobile_bridge_name(const std::string& mobile_bridge_name);
357     void set_mobile_tap_name(const std::string& mobile_tap_name);
358     void set_wifi_tap_name(const std::string& wifi_tap_name);
359     void set_vsock_guest_cid(int vsock_guest_cid);
360     void set_uuid(const std::string& uuid);
361     void set_instance_dir(const std::string& instance_dir);
362     void set_virtual_disk_paths(const std::vector<std::string>& disk_paths);
363     // Wifi MAC address inside the guest
364     void set_wifi_mac_address(const std::array<unsigned char, 6>&);
365   };
366 
367  private:
368   std::unique_ptr<Json::Value> dictionary_;
369 
370   void SetPath(const std::string& key, const std::string& path);
371   bool LoadFromFile(const char* file);
372   static CuttlefishConfig* BuildConfigImpl();
373 
374   CuttlefishConfig(const CuttlefishConfig&) = delete;
375   CuttlefishConfig& operator=(const CuttlefishConfig&) = delete;
376 };
377 
378 // Returns the instance number as obtained from the CUTTLEFISH_INSTANCE
379 // environment variable or the username.
380 int GetInstance();
381 // Returns a path where the launhcer puts a link to the config file which makes
382 // it easily discoverable regardless of what vm manager is in use
383 std::string GetGlobalConfigFileLink();
384 
385 // These functions modify a given base value to make it different accross
386 // different instances by appending the instance id in case of strings or adding
387 // it in case of integers.
388 std::string ForCurrentInstance(const char* prefix);
389 int ForCurrentInstance(int base);
390 
391 // Returns a random serial number appended to a given prefix.
392 std::string RandomSerialNumber(const std::string& prefix);
393 
394 std::string GetDefaultPerInstanceDir();
395 std::string GetDefaultMempath();
396 int GetDefaultPerInstanceVsockCid();
397 
398 std::string DefaultHostArtifactsPath(const std::string& file);
399 std::string DefaultGuestImagePath(const std::string& file);
400 std::string DefaultEnvironmentPath(const char* environment_key,
401                                    const char* default_value,
402                                    const char* path);
403 
404 // Whether the host supports qemu
405 bool HostSupportsQemuCli();
406 bool HostSupportsVsock();
407 
408 // GPU modes
409 extern const char* const kGpuModeGuestSwiftshader;
410 extern const char* const kGpuModeDrmVirgl;
411 extern const char* const kGpuModeGfxStream;
412 }  // namespace vsoc
413