• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
19 #include <array>
20 #include <cstdint>
21 #include <map>
22 #include <memory>
23 #include <optional>
24 #include <set>
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 
29 #include <fmt/ostream.h>
30 
31 #include "common/libs/utils/architecture.h"
32 #include "common/libs/utils/device_type.h"
33 #include "common/libs/utils/result.h"
34 #include "host/libs/config/config_constants.h"
35 #include "host/libs/config/config_fragment.h"
36 #include "host/libs/config/config_utils.h"
37 #include "host/libs/config/secure_hals.h"
38 
39 namespace Json {
40 class Value;
41 }
42 
43 namespace cuttlefish {
44 
45 enum class VmmMode {
46   kUnknown,
47   kCrosvm,
48   kGem5,
49   kQemu,
50 };
51 
52 std::ostream& operator<<(std::ostream&, VmmMode);
53 std::string ToString(VmmMode mode);
54 Result<VmmMode> ParseVmm(std::string_view);
55 
56 enum class ExternalNetworkMode {
57   kUnknown,
58   kTap,
59   kSlirp,
60 };
61 
62 std::ostream& operator<<(std::ostream&, ExternalNetworkMode);
63 Result<ExternalNetworkMode> ParseExternalNetworkMode(std::string_view);
64 
65 enum class GuestHwuiRenderer {
66   kUnknown,
67   kSkiaGl,
68   kSkiaVk,
69 };
70 std::ostream& operator<<(std::ostream&, GuestHwuiRenderer);
71 std::string ToString(GuestHwuiRenderer renderer);
72 Result<GuestHwuiRenderer> ParseGuestHwuiRenderer(std::string_view);
73 
74 enum class GuestRendererPreload {
75   kAuto,
76   kGuestDefault,
77   kEnabled,
78   kDisabled,
79 };
80 std::ostream& operator<<(std::ostream&, GuestRendererPreload);
81 std::string ToString(GuestRendererPreload);
82 Result<GuestRendererPreload> ParseGuestRendererPreload(std::string_view);
83 
84 // Holds the configuration of the cuttlefish instances.
85 class CuttlefishConfig {
86  public:
87   static const CuttlefishConfig* Get();
88   static std::unique_ptr<const CuttlefishConfig> GetFromFile(
89       const std::string& path);
90   static bool ConfigExists();
91 
92   CuttlefishConfig();
93   CuttlefishConfig(CuttlefishConfig&&);
94   ~CuttlefishConfig();
95   CuttlefishConfig& operator=(CuttlefishConfig&&);
96 
97   // Saves the configuration object in a file, it can then be read in other
98   // processes by passing the --config_file option.
99   bool SaveToFile(const std::string& file) const;
100   bool LoadFromFile(const char* file);
101 
102   bool SaveFragment(const ConfigFragment&);
103   bool LoadFragment(ConfigFragment&) const;
104 
105   std::string root_dir() const;
106   void set_root_dir(const std::string& root_dir);
107 
108   std::string instances_dir() const;
109   std::string InstancesPath(const std::string&) const;
110 
111   std::string assembly_dir() const;
112   std::string AssemblyPath(const std::string&) const;
113 
114   void set_instances_uds_dir(const std::string&);
115   std::string instances_uds_dir() const;
116   std::string InstancesUdsPath(const std::string&) const;
117 
118   void set_environments_dir(const std::string&);
119   std::string environments_dir() const;
120   std::string EnvironmentsPath(const std::string&) const;
121 
122   void set_environments_uds_dir(const std::string&);
123   std::string environments_uds_dir() const;
124   std::string EnvironmentsUdsPath(const std::string&) const;
125 
126   VmmMode vm_manager() const;
127   void set_vm_manager(VmmMode vmm);
128 
129   std::string ap_vm_manager() const;
130   void set_ap_vm_manager(const std::string& name);
131 
132   struct DisplayConfig {
133     int width;
134     int height;
135     int dpi;
136     int refresh_rate_hz;
137     std::string overlays;
138   };
139 
140   struct TouchpadConfig {
141     int width;
142     int height;
143 
144     static Json::Value Serialize(
145         const CuttlefishConfig::TouchpadConfig& config);
146     static TouchpadConfig Deserialize(const Json::Value& config_json);
147   };
148 
149   void set_secure_hals(const std::set<SecureHal>&);
150   Result<std::set<SecureHal>> secure_hals() const;
151 
152   void set_crosvm_binary(const std::string& crosvm_binary);
153   std::string crosvm_binary() const;
154 
155   void set_gem5_debug_flags(const std::string& gem5_debug_flags);
156   std::string gem5_debug_flags() const;
157 
158   void set_enable_host_uwb(bool enable_host_uwb);
159   bool enable_host_uwb() const;
160 
161   void set_enable_host_uwb_connector(bool enable_host_uwb);
162   bool enable_host_uwb_connector() const;
163 
164   void set_enable_host_bluetooth(bool enable_host_bluetooth);
165   bool enable_host_bluetooth() const;
166 
167   void set_enable_automotive_proxy(bool enable_automotive_proxy);
168   bool enable_automotive_proxy() const;
169 
170   // The vsock port used by vhal_proxy_server
171   void set_vhal_proxy_server_port(int port);
172   int vhal_proxy_server_port() const;
173 
174   // Bluetooth is enabled by bt_connector and rootcanal
175   void set_enable_host_bluetooth_connector(bool enable_host_bluetooth);
176   bool enable_host_bluetooth_connector() const;
177 
178   void set_enable_host_nfc(bool enable_host_nfc);
179   bool enable_host_nfc() const;
180 
181   void set_enable_host_nfc_connector(bool enable_host_nfc_connector);
182   bool enable_host_nfc_connector() const;
183 
184   void set_casimir_args(const std::string& casimir_args);
185   std::vector<std::string> casimir_args() const;
186   void set_casimir_instance_num(int casimir_instance_num);
187   int casimir_instance_num() const;
188   void set_casimir_nci_port(int port);
189   int casimir_nci_port() const;
190   void set_casimir_rf_port(int port);
191   int casimir_rf_port() const;
192 
193   // Flags for the set of radios that are connected to netsim
194   enum NetsimRadio {
195     Bluetooth = 0b00000001,
196     Wifi      = 0b00000010,
197     Uwb       = 0b00000100,
198   };
199 
200   void netsim_radio_enable(NetsimRadio flag);
201   bool netsim_radio_enabled(NetsimRadio flag) const;
202   void set_netsim_instance_num(int netsim_instance_num);
203   int netsim_instance_num() const;
204   // Netsim has a built-in connector to forward packets to another daemon based
205   // on instance number.  This is set in the serial launch case when FLAGS
206   // rootcanal_instance_num is specified. The non-netsim case uses
207   // bluetooth_connector and rootcanal_hci_port for the same purpose. Purposely
208   // restricted to legacy bluetooth serial invocation because new cases should
209   // use cvd.
210   int netsim_connector_instance_num() const;
211   void set_netsim_connector_instance_num(int netsim_instance_num);
212   void set_netsim_args(const std::string& netsim_args);
213   std::vector<std::string> netsim_args() const;
214 
215   enum class Answer {
216     kUnknown = 0,
217     kYes,
218     kNo,
219   };
220 
221   void set_enable_metrics(std::string enable_metrics);
222   CuttlefishConfig::Answer enable_metrics() const;
223 
224   void set_metrics_binary(const std::string& metrics_binary);
225   std::string metrics_binary() const;
226 
227   void set_extra_kernel_cmdline(const std::string& extra_cmdline);
228   std::vector<std::string> extra_kernel_cmdline() const;
229 
230   // A directory containing the SSL certificates for the signaling server
231   void set_webrtc_certs_dir(const std::string& certs_dir);
232   std::string webrtc_certs_dir() const;
233 
234   // The port for the webrtc signaling server. It's used by the signaling server
235   // to bind to it and by the webrtc process to connect to and register itself
236   void set_sig_server_port(int port);
237   int sig_server_port() const;
238 
239   // The address of the signaling server
240   void set_sig_server_address(const std::string& addr);
241   std::string sig_server_address() const;
242 
243   // The path section of the url where the webrtc process registers itself with
244   // the signaling server
245   void set_sig_server_path(const std::string& path);
246   std::string sig_server_path() const;
247 
248   // Whether the webrtc process should use a secure connection (WSS) to the
249   // signaling server.
250   void set_sig_server_secure(bool secure);
251   bool sig_server_secure() const;
252 
253   // Whether the webrtc process should attempt to verify the authenticity of the
254   // signaling server (reject self signed certificates)
255   void set_sig_server_strict(bool strict);
256   bool sig_server_strict() const;
257 
258   // Whether display composition is enabled for one or more displays
259   bool OverlaysEnabled() const;
260 
261   void set_host_tools_version(const std::map<std::string, uint32_t>&);
262   std::map<std::string, uint32_t> host_tools_version() const;
263 
264   void set_virtio_mac80211_hwsim(bool virtio_mac80211_hwsim);
265   bool virtio_mac80211_hwsim() const;
266 
267   void set_ap_rootfs_image(const std::string& path);
268   std::string ap_rootfs_image() const;
269 
270   void set_ap_kernel_image(const std::string& path);
271   std::string ap_kernel_image() const;
272 
273   void set_pica_uci_port(int pica_uci_port);
274   int pica_uci_port() const;
275 
276   void set_rootcanal_args(const std::string& rootcanal_args);
277   std::vector<std::string> rootcanal_args() const;
278 
279   void set_rootcanal_hci_port(int rootcanal_hci_port);
280   int rootcanal_hci_port() const;
281 
282   void set_rootcanal_link_port(int rootcanal_link_port);
283   int rootcanal_link_port() const;
284 
285   void set_rootcanal_link_ble_port(int rootcanal_link_ble_port);
286   int rootcanal_link_ble_port() const;
287 
288   void set_rootcanal_test_port(int rootcanal_test_port);
289   int rootcanal_test_port() const;
290 
291   // The path of an AP image in composite disk
292   std::string ap_image_dev_path() const;
293   void set_ap_image_dev_path(const std::string& dev_path);
294 
295   // path to the saved snapshot file(s)
296   std::string snapshot_path() const;
297   void set_snapshot_path(const std::string& snapshot_path);
298 
299   std::set<std::string> straced_host_executables() const;
300   void set_straced_host_executables(const std::set<std::string>& executables);
301 
302   std::string kvm_path() const;
303   void set_kvm_path(const std::string&);
304 
305   std::string vhost_vsock_path() const;
306   void set_vhost_vsock_path(const std::string&);
307 
308   bool IsCrosvm() const;
309 
310   class InstanceSpecific;
311   class MutableInstanceSpecific;
312 
313   MutableInstanceSpecific ForInstance(int instance_num);
314   InstanceSpecific ForInstance(int instance_num) const;
315   InstanceSpecific ForInstanceName(const std::string& name) const;
316   InstanceSpecific ForDefaultInstance() const;
317 
318   std::vector<InstanceSpecific> Instances() const;
319   std::vector<std::string> instance_dirs() const;
320 
321   void set_instance_names(const std::vector<std::string>& instance_names);
322   std::vector<std::string> instance_names() const;
323 
324   // A view into an existing CuttlefishConfig object for a particular instance.
325   class InstanceSpecific {
326     const CuttlefishConfig* config_;
327     std::string id_;
328     friend InstanceSpecific CuttlefishConfig::ForInstance(int num) const;
329     friend std::vector<InstanceSpecific> CuttlefishConfig::Instances() const;
330 
InstanceSpecific(const CuttlefishConfig * config,const std::string & id)331     InstanceSpecific(const CuttlefishConfig* config, const std::string& id)
332         : config_(config), id_(id) {}
333 
334     Json::Value* Dictionary();
335     const Json::Value* Dictionary() const;
336    public:
337     std::string serial_number() const;
338 
339     // Index of this instance within current configured group of VMs
340     int index() const;
341 
342     // If any of the following port numbers is 0, the relevant service is not
343     // running on the guest.
344 
345     // Port number for qemu to run a vnc server on the host
346     int qemu_vnc_server_port() const;
347     // Port number to connect to the tombstone receiver on the host
348     int tombstone_receiver_port() const;
349     // Port number to connect to the config server on the host
350     int config_server_port() const;
351     // Port number to connect to the audiocontrol server on the guest
352     int audiocontrol_server_port() const;
353     // Port number to connect to the adb server on the host
354     int adb_host_port() const;
355     // Port number to connect to the fastboot server on the host
356     int fastboot_host_port() const;
357     // Device-specific ID to distinguish modem simulators. Must be 4 digits.
358     int modem_simulator_host_id() const;
359     // Port number to connect to the gnss grpc proxy server on the host
360     int gnss_grpc_proxy_server_port() const;
361     std::string adb_ip_and_port() const;
362     // Port number to connect to the camera hal on the guest
363     int camera_server_port() const;
364     // Port number to connect to the lights hal on the guest
365     int lights_server_port() const;
366 
367     std::string adb_device_name() const;
368     std::string gnss_file_path() const;
369     std::string fixed_location_file_path() const;
370     std::string mobile_bridge_name() const;
371     std::string mobile_tap_name() const;
372     std::string mobile_mac() const;
373     std::string wifi_bridge_name() const;
374     std::string wifi_tap_name() const;
375     std::string wifi_mac() const;
376     bool use_bridged_wifi_tap() const;
377     std::string ethernet_tap_name() const;
378     std::string ethernet_bridge_name() const;
379     std::string ethernet_mac() const;
380     std::string ethernet_ipv6() const;
381     uint32_t session_id() const;
382     bool use_allocd() const;
383     int vsock_guest_cid() const;
384     std::string vsock_guest_group() const;
385     std::string uuid() const;
386     std::string instance_name() const;
387     std::string environment_name() const;
388     std::vector<std::string> virtual_disk_paths() const;
389 
390     // Returns the path to a file with the given name in the instance
391     // directory..
392     std::string PerInstancePath(const std::string& file_name) const;
393     std::string PerInstanceInternalPath(const std::string& file_name) const;
394     std::string PerInstanceLogPath(const std::string& file_name) const;
395 
396     std::string CrosvmSocketPath() const;
397     std::string OpenwrtCrosvmSocketPath() const;
398     std::string instance_dir() const;
399 
400     std::string instance_internal_dir() const;
401 
402     // Return the Unix domain socket path with given name. Because the
403     // length limitation of Unix domain socket name, it needs to be in
404     // the another directory than normal Instance path.
405     std::string PerInstanceUdsPath(const std::string& file_name) const;
406     std::string PerInstanceInternalUdsPath(const std::string& file_name) const;
407     std::string PerInstanceGrpcSocketPath(const std::string& socket_name) const;
408 
409     std::string instance_uds_dir() const;
410 
411     std::string instance_internal_uds_dir() const;
412 
413     std::string touch_socket_path(int touch_dev_idx) const;
414     std::string mouse_socket_path() const;
415     std::string rotary_socket_path() const;
416     std::string keyboard_socket_path() const;
417     std::string switches_socket_path() const;
418 
419     std::string access_kregistry_path() const;
420 
421     std::string hwcomposer_pmem_path() const;
422 
423     std::string pstore_path() const;
424 
425     std::string pflash_path() const;
426 
427     std::string console_path() const;
428 
429     std::string logcat_path() const;
430 
431     std::string kernel_log_pipe_name() const;
432 
433     std::string console_pipe_prefix() const;
434     std::string console_in_pipe_name() const;
435     std::string console_out_pipe_name() const;
436 
437     std::string gnss_pipe_prefix() const;
438     std::string gnss_in_pipe_name() const;
439     std::string gnss_out_pipe_name() const;
440 
441     std::string logcat_pipe_name() const;
442     std::string restore_adbd_pipe_name() const;
443 
444     std::string launcher_log_path() const;
445 
446     std::string launcher_monitor_socket_path() const;
447 
448     std::string sdcard_path() const;
449     std::string sdcard_overlay_path() const;
450 
451     std::string persistent_composite_disk_path() const;
452     std::string persistent_composite_overlay_path() const;
453     std::string persistent_ap_composite_disk_path() const;
454     std::string persistent_ap_composite_overlay_path() const;
455 
456     std::string os_composite_disk_path() const;
457 
458     std::string ap_composite_disk_path() const;
459 
460     std::string uboot_env_image_path() const;
461 
462     std::string ap_uboot_env_image_path() const;
463 
464     std::string ap_esp_image_path() const;
465 
466     std::string esp_image_path() const;
467 
468     std::string chromeos_state_image() const;
469 
470     std::string otheros_esp_grub_config() const;
471 
472     std::string ap_esp_grub_config() const;
473 
474     std::string audio_server_path() const;
475 
476     enum class BootFlow {
477       Android,
478       AndroidEfiLoader,
479       ChromeOs,
480       ChromeOsDisk,
481       Linux,
482       Fuchsia
483     };
484 
485     BootFlow boot_flow() const;
486 
487     enum class GuestOs { Android, ChromeOs, Linux, Fuchsia };
488 
guest_os()489     GuestOs guest_os() const {
490       switch (boot_flow()) {
491         case BootFlow::Android:
492         case BootFlow::AndroidEfiLoader:
493           return GuestOs::Android;
494         case BootFlow::ChromeOs:
495         case BootFlow::ChromeOsDisk:
496           return GuestOs::ChromeOs;
497         case BootFlow::Linux:
498           return GuestOs::Linux;
499         case BootFlow::Fuchsia:
500           return GuestOs::Fuchsia;
501           // Don't include a default case, this needs to fail when not all cases
502           // are covered.
503       }
504     }
505 
506     // modem simulator related
507     std::string modem_simulator_ports() const;
508 
509     // The device id the webrtc process should use to register with the
510     // signaling server
511     std::string webrtc_device_id() const;
512 
513     // The group id the webrtc process should use to register with the
514     // signaling server
515     std::string group_id() const;
516 
517     // Whether this instance should start the webrtc signaling server
518     bool start_webrtc_sig_server() const;
519 
520     // Whether to start a reverse proxy to the webrtc signaling server already
521     // running in the host
522     bool start_webrtc_sig_server_proxy() const;
523 
524     // Whether this instance should start a rootcanal instance
525     bool start_rootcanal() const;
526 
527     // Whether this instance should start a casimir instance
528     bool start_casimir() const;
529 
530     // Whether this instance should start a pica instance
531     bool start_pica() const;
532 
533     // Whether this instance should start a netsim instance
534     bool start_netsim() const;
535 
536     // TODO(b/288987294) Remove this when separating environment is done
537     // Whether this instance should start a wmediumd instance
538     bool start_wmediumd_instance() const;
539 
540     const Json::Value& mcu() const;
541 
542     enum class APBootFlow {
543       // Not starting AP at all (for example not the 1st instance)
544       None,
545       // Generating ESP and using U-BOOT to boot AP
546       Grub,
547       // Using legacy way to boot AP in case we cannot generate ESP image.
548       // Currently we have only one case when we cannot do it. When users
549       // have ubuntu bionic which doesn't have monolith binaris in the
550       // grub-efi-arm64-bin (for arm64) and grub-efi-ia32-bin (x86) deb packages.
551       // TODO(b/260337906): check is it possible to add grub binaries into the AOSP
552       // to deliver the proper grub environment
553       // TODO(b/260338443): use grub-mkimage from grub-common in case we cannot overcome
554       // legal issues
555       LegacyDirect
556     };
557     APBootFlow ap_boot_flow() const;
558 
559     bool crosvm_use_balloon() const;
560     bool crosvm_use_rng() const;
561     bool crosvm_simple_media_device() const;
562     std::string crosvm_v4l2_proxy() const;
563     bool use_pmem() const;
564 
565     // Wifi MAC address inside the guest
566     int wifi_mac_prefix() const;
567 
568     std::string factory_reset_protected_path() const;
569 
570     std::string persistent_bootconfig_path() const;
571 
572     // used for the persistent_composite_disk vbmeta
573     std::string vbmeta_path() const;
574 
575     std::string ap_vbmeta_path() const;
576 
577     std::string id() const;
578 
579     std::string gem5_binary_dir() const;
580 
581     std::string gem5_checkpoint_dir() const;
582 
583     // Serial console
584     bool console() const;
585     std::string console_dev() const;
586     bool enable_sandbox() const;
587     bool enable_virtiofs() const;
588 
589     // KGDB configuration for kernel debugging
590     bool kgdb() const;
591 
592     // TODO (b/163575714) add virtio console support to the bootloader so the
593     // virtio console path for the console device can be taken again. When that
594     // happens, this function can be deleted along with all the code paths it
595     // forces.
596     bool use_bootloader() const;
597 
598     DeviceType device_type() const;
599 
600     Arch target_arch() const;
601 
602     int cpus() const;
603 
604     std::string vcpu_config_path() const;
605 
606     std::string data_policy() const;
607 
608     int blank_data_image_mb() const;
609 
610     int gdb_port() const;
611 
612     std::vector<DisplayConfig> display_configs() const;
613     std::vector<TouchpadConfig> touchpad_configs() const;
614 
615     std::string grpc_socket_path() const;
616     int memory_mb() const;
617     int ddr_mem_mb() const;
618     std::string setupwizard_mode() const;
619     std::string userdata_format() const;
620     bool guest_enforce_security() const;
621     bool use_sdcard() const;
622     bool pause_in_bootloader() const;
623     bool run_as_daemon() const;
624     bool enable_audio() const;
625     bool enable_mouse() const;
626     std::optional<std::string> custom_keyboard_config() const;
627     const Json::Value& domkey_mapping_config() const;
628     bool enable_gnss_grpc_proxy() const;
629     bool enable_bootanimation() const;
630     bool enable_usb() const;
631     std::vector<std::string> extra_bootconfig_args() const;
632     bool record_screen() const;
633     std::string gem5_debug_file() const;
634     bool protected_vm() const;
635     bool mte() const;
636     std::string boot_slot() const;
637     bool fail_fast() const;
638     bool vhost_user_block() const;
639     std::string ti50_emulator() const;
640 
641     // Kernel and bootloader logging
642     bool enable_kernel_log() const;
643     bool vhost_net() const;
644     bool vhost_user_vsock() const;
645     int openthread_node_id() const;
646 
647     // Mobile network info (RIL)
648     std::string ril_dns() const;
649     std::string ril_ipaddr() const;
650     std::string ril_gateway() const;
651     std::string ril_broadcast() const;
652     uint8_t ril_prefixlen() const;
653 
654     bool enable_webrtc() const;
655     std::string webrtc_assets_dir() const;
656 
657     // The range of TCP ports available for webrtc sessions.
658     std::pair<uint16_t, uint16_t> webrtc_tcp_port_range() const;
659 
660     // The range of UDP ports available for webrtc sessions.
661     std::pair<uint16_t, uint16_t> webrtc_udp_port_range() const;
662 
663     bool smt() const;
664     std::string crosvm_binary() const;
665     std::string seccomp_policy_dir() const;
666     std::string qemu_binary_dir() const;
667 
668     // Configuration flags for a minimal device
669     bool enable_minimal_mode() const;
670     bool enable_modem_simulator() const;
671     int modem_simulator_instance_number() const;
672     int modem_simulator_sim_type() const;
673 
674     std::string gpu_mode() const;
675     std::string gpu_angle_feature_overrides_enabled() const;
676     std::string gpu_angle_feature_overrides_disabled() const;
677     std::string gpu_capture_binary() const;
678     std::string gpu_gfxstream_transport() const;
679     std::string gpu_renderer_features() const;
680     std::string gpu_context_types() const;
681     GuestHwuiRenderer guest_hwui_renderer() const;
682     GuestRendererPreload guest_renderer_preload() const;
683     std::string guest_vulkan_driver() const;
684     bool guest_uses_bgra_framebuffers() const;
685     std::string frames_socket_path() const;
686 
687     std::string gpu_vhost_user_mode() const;
688 
689     bool enable_gpu_udmabuf() const;
690     bool enable_gpu_vhost_user() const;
691     bool enable_gpu_external_blob() const;
692     bool enable_gpu_system_blob() const;
693 
694     std::string hwcomposer() const;
695 
696     bool restart_subprocesses() const;
697 
698     // android artifacts
699     std::string boot_image() const;
700     std::string new_boot_image() const;
701     std::string init_boot_image() const;
702     std::string data_image() const;
703     std::string new_data_image() const;
704     std::string super_image() const;
705     std::string new_super_image() const;
706     std::string misc_image() const;
707     std::string misc_info_txt() const;
708     std::string metadata_image() const;
709     std::string vendor_boot_image() const;
710     std::string new_vendor_boot_image() const;
711     std::string vbmeta_image() const;
712     std::string new_vbmeta_image() const;
713     std::string vbmeta_system_image() const;
714     std::string vbmeta_vendor_dlkm_image() const;
715     std::string new_vbmeta_vendor_dlkm_image() const;
716     std::string vbmeta_system_dlkm_image() const;
717     std::string new_vbmeta_system_dlkm_image() const;
718     std::string vvmtruststore_path() const;
719     std::string default_target_zip() const;
720     std::string system_target_zip() const;
721 
722     // otheros artifacts
723     std::string otheros_esp_image() const;
724 
725     // android efi loader flow
726     std::string android_efi_loader() const;
727 
728     // chromeos artifacts for otheros flow
729     std::string chromeos_disk() const;
730     std::string chromeos_kernel_path() const;
731     std::string chromeos_root_image() const;
732 
733     // linux artifacts for otheros flow
734     std::string linux_kernel_path() const;
735     std::string linux_initramfs_path() const;
736     std::string linux_root_image() const;
737 
738     std::string fuchsia_zedboot_path() const;
739     std::string fuchsia_multiboot_bin_path() const;
740     std::string fuchsia_root_image() const;
741 
742     std::string custom_partition_path() const;
743 
744     std::string hibernation_partition_image() const;
745 
746     int blank_metadata_image_mb() const;
747     int blank_sdcard_image_mb() const;
748     std::string bootloader() const;
749     std::string initramfs_path() const;
750     std::string kernel_path() const;
751     std::string guest_android_version() const;
752     bool bootconfig_supported() const;
753     std::string filename_encryption_mode() const;
754     ExternalNetworkMode external_network_mode() const;
755 
756     bool start_vhal_proxy_server() const;
757 
758     int audio_output_streams_count() const;
759 
760     bool enable_tap_devices() const;
761   };
762 
763   // A view into an existing CuttlefishConfig object for a particular instance.
764   class MutableInstanceSpecific {
765     CuttlefishConfig* config_;
766     std::string id_;
767     friend MutableInstanceSpecific CuttlefishConfig::ForInstance(int num);
768 
769     MutableInstanceSpecific(CuttlefishConfig* config, const std::string& id);
770 
771     Json::Value* Dictionary();
772    public:
773     void set_serial_number(const std::string& serial_number);
774     void set_qemu_vnc_server_port(int qemu_vnc_server_port);
775     void set_tombstone_receiver_port(int tombstone_receiver_port);
776     void set_config_server_port(int config_server_port);
777     void set_frames_server_port(int config_server_port);
778     void set_touch_server_port(int config_server_port);
779     void set_keyboard_server_port(int config_server_port);
780     void set_gatekeeper_vsock_port(int gatekeeper_vsock_port);
781     void set_keymaster_vsock_port(int keymaster_vsock_port);
782     void set_audiocontrol_server_port(int audiocontrol_server_port);
783     void set_lights_server_port(int lights_server_port);
784     void set_adb_host_port(int adb_host_port);
785     void set_modem_simulator_host_id(int modem_simulator_id);
786     void set_adb_ip_and_port(const std::string& ip_port);
787     void set_fastboot_host_port(int fastboot_host_port);
788     void set_camera_server_port(int camera_server_port);
789     void set_mobile_bridge_name(const std::string& mobile_bridge_name);
790     void set_mobile_tap_name(const std::string& mobile_tap_name);
791     void set_mobile_mac(const std::string& mac);
792     void set_wifi_bridge_name(const std::string& wifi_bridge_name);
793     void set_wifi_tap_name(const std::string& wifi_tap_name);
794     void set_wifi_mac(const std::string& mac);
795     void set_use_bridged_wifi_tap(bool use_bridged_wifi_tap);
796     void set_ethernet_tap_name(const std::string& ethernet_tap_name);
797     void set_ethernet_bridge_name(const std::string& set_ethernet_bridge_name);
798     void set_ethernet_mac(const std::string& mac);
799     void set_ethernet_ipv6(const std::string& ip);
800     void set_session_id(uint32_t session_id);
801     void set_use_allocd(bool use_allocd);
802     void set_vsock_guest_cid(int vsock_guest_cid);
803     void set_vsock_guest_group(const std::string& vsock_guest_group);
804     void set_uuid(const std::string& uuid);
805     void set_environment_name(const std::string& env_name);
806     // modem simulator related
807     void set_modem_simulator_ports(const std::string& modem_simulator_ports);
808     void set_virtual_disk_paths(const std::vector<std::string>& disk_paths);
809     void set_webrtc_device_id(const std::string& id);
810     void set_group_id(const std::string& id);
811     void set_start_webrtc_signaling_server(bool start);
812     void set_start_webrtc_sig_server_proxy(bool start);
813     void set_start_rootcanal(bool start);
814     void set_start_casimir(bool start);
815     void set_start_pica(bool start);
816     void set_start_netsim(bool start);
817     // TODO(b/288987294) Remove this when separating environment is done
818     void set_start_wmediumd_instance(bool start);
819     void set_mcu(const Json::Value &v);
820     void set_ap_boot_flow(InstanceSpecific::APBootFlow flow);
821     void set_crosvm_use_balloon(const bool use_balloon);
822     void set_crosvm_use_rng(const bool use_rng);
823     void set_crosvm_simple_media_device(const bool simple_media_device);
824     void set_crosvm_v4l2_proxy(const std::string v4l2_proxy);
825     void set_use_pmem(const bool use_pmem);
826     // Wifi MAC address inside the guest
827     void set_wifi_mac_prefix(const int wifi_mac_prefix);
828     // Gnss grpc proxy server port inside the host
829     void set_gnss_grpc_proxy_server_port(int gnss_grpc_proxy_server_port);
830     // Gnss grpc proxy local file path
831     void set_gnss_file_path(const std::string &gnss_file_path);
832     void set_fixed_location_file_path(
833         const std::string& fixed_location_file_path);
834     void set_gem5_binary_dir(const std::string& gem5_binary_dir);
835     void set_gem5_checkpoint_dir(const std::string& gem5_checkpoint_dir);
836     // Serial console
837     void set_console(bool console);
838     void set_enable_sandbox(const bool enable_sandbox);
839     void set_enable_virtiofs(const bool enable_virtiofs);
840     void set_kgdb(bool kgdb);
841     void set_device_type(DeviceType type);
842     void set_target_arch(Arch target_arch);
843     void set_cpus(int cpus);
844     void set_vcpu_config_path(const std::string& vcpu_config_path);
845     void set_data_policy(const std::string& data_policy);
846     void set_blank_data_image_mb(int blank_data_image_mb);
847     void set_gdb_port(int gdb_port);
848     void set_display_configs(const std::vector<DisplayConfig>& display_configs);
849     void set_touchpad_configs(
850         const std::vector<TouchpadConfig>& touchpad_configs);
851     void set_memory_mb(int memory_mb);
852     void set_ddr_mem_mb(int ddr_mem_mb);
853     Result<void> set_setupwizard_mode(const std::string& title);
854     void set_userdata_format(const std::string& userdata_format);
855     void set_guest_enforce_security(bool guest_enforce_security);
856     void set_use_sdcard(bool use_sdcard);
857     void set_pause_in_bootloader(bool pause_in_bootloader);
858     void set_run_as_daemon(bool run_as_daemon);
859     void set_enable_audio(bool enable);
860     void set_enable_mouse(bool enable);
861     void set_custom_keyboard_config(
862         const std::string& custom_keyboard_config_json_path);
863     void set_domkey_mapping_config(
864         const std::string& domkey_mapping_config_json_path);
865     void set_enable_usb(bool enable);
866     void set_enable_gnss_grpc_proxy(const bool enable_gnss_grpc_proxy);
867     void set_enable_bootanimation(const bool enable_bootanimation);
868     void set_extra_bootconfig_args(const std::string& extra_bootconfig_args);
869     void set_record_screen(bool record_screen);
870     void set_gem5_debug_file(const std::string& gem5_debug_file);
871     void set_protected_vm(bool protected_vm);
872     void set_mte(bool mte);
873     void set_boot_slot(const std::string& boot_slot);
874     void set_grpc_socket_path(const std::string& sockets);
875     void set_fail_fast(bool fail_fast);
876     void set_vhost_user_block(bool qemu_vhost_user_block);
877     void set_ti50_emulator(const std::string& ti50_emulator);
878 
879     // Kernel and bootloader logging
880     void set_enable_kernel_log(bool enable_kernel_log);
881 
882     void set_enable_webrtc(bool enable_webrtc);
883     void set_webrtc_assets_dir(const std::string& webrtc_assets_dir);
884 
885     // The range of TCP ports available for webrtc sessions.
886     void set_webrtc_tcp_port_range(std::pair<uint16_t, uint16_t> range);
887 
888     // The range of UDP ports available for webrtc sessions.
889     void set_webrtc_udp_port_range(std::pair<uint16_t, uint16_t> range);
890 
891     void set_smt(bool smt);
892     void set_crosvm_binary(const std::string& crosvm_binary);
893     void set_seccomp_policy_dir(const std::string& seccomp_policy_dir);
894     void set_qemu_binary_dir(const std::string& qemu_binary_dir);
895 
896     void set_vhost_net(bool vhost_net);
897     void set_vhost_user_vsock(bool vhost_user_vsock);
898 
899     void set_openthread_node_id(int node_id);
900 
901     // Mobile network (RIL)
902     void set_ril_dns(const std::string& ril_dns);
903     void set_ril_ipaddr(const std::string& ril_ipaddr);
904     void set_ril_gateway(const std::string& ril_gateway);
905     void set_ril_broadcast(const std::string& ril_broadcast);
906     void set_ril_prefixlen(uint8_t ril_prefixlen);
907 
908     // Configuration flags for a minimal device
909     void set_enable_minimal_mode(bool enable_minimal_mode);
910     void set_enable_modem_simulator(bool enable_modem_simulator);
911     void set_modem_simulator_instance_number(int instance_numbers);
912     void set_modem_simulator_sim_type(int sim_type);
913 
914     void set_gpu_mode(const std::string& name);
915     void set_gpu_angle_feature_overrides_enabled(const std::string& overrides);
916     void set_gpu_angle_feature_overrides_disabled(const std::string& overrides);
917     void set_gpu_capture_binary(const std::string&);
918     void set_gpu_gfxstream_transport(const std::string& transport);
919     void set_gpu_renderer_features(const std::string& features);
920     void set_gpu_context_types(const std::string& context_types);
921     void set_guest_hwui_renderer(GuestHwuiRenderer renderer);
922     void set_guest_renderer_preload(GuestRendererPreload preload);
923     void set_guest_vulkan_driver(const std::string& driver);
924     void set_guest_uses_bgra_framebuffers(bool uses_bgra);
925     void set_frames_socket_path(const std::string& driver);
926 
927     void set_enable_gpu_udmabuf(const bool enable_gpu_udmabuf);
928     void set_enable_gpu_vhost_user(const bool enable_gpu_vhost_user);
929     void set_enable_gpu_external_blob(const bool enable_gpu_external_blob);
930     void set_enable_gpu_system_blob(const bool enable_gpu_system_blob);
931 
932     void set_hwcomposer(const std::string&);
933 
934     void set_restart_subprocesses(bool restart_subprocesses);
935 
936     // system image files
937     void set_boot_image(const std::string& boot_image);
938     void set_new_boot_image(const std::string& new_boot_image);
939     void set_init_boot_image(const std::string& init_boot_image);
940     void set_data_image(const std::string& data_image);
941     void set_new_data_image(const std::string& new_data_image);
942     void set_super_image(const std::string& super_image);
943     void set_new_super_image(const std::string& super_image);
944     void set_misc_info_txt(const std::string& misc_image);
945     void set_vendor_boot_image(const std::string& vendor_boot_image);
946     void set_new_vendor_boot_image(const std::string& new_vendor_boot_image);
947     void set_vbmeta_image(const std::string& vbmeta_image);
948     void set_new_vbmeta_image(const std::string& new_vbmeta_image);
949     void set_vbmeta_system_image(const std::string& vbmeta_system_image);
950     void set_vbmeta_vendor_dlkm_image(
951         const std::string& vbmeta_vendor_dlkm_image);
952     void set_new_vbmeta_vendor_dlkm_image(
953         const std::string& vbmeta_vendor_dlkm_image);
954     void set_vbmeta_system_dlkm_image(
955         const std::string& vbmeta_system_dlkm_image);
956     void set_new_vbmeta_system_dlkm_image(
957         const std::string& vbmeta_system_dlkm_image);
958     void set_vvmtruststore_path(const std::string& vvmtruststore_path);
959     void set_default_target_zip(const std::string& default_target_zip);
960     void set_system_target_zip(const std::string& system_target_zip);
961     void set_otheros_esp_image(const std::string& otheros_esp_image);
962     void set_android_efi_loader(const std::string& android_efi_loader);
963     void set_chromeos_disk(const std::string& chromeos_disk);
964     void set_chromeos_kernel_path(const std::string& linux_kernel_path);
965     void set_chromeos_root_image(const std::string& linux_root_image);
966     void set_linux_kernel_path(const std::string& linux_kernel_path);
967     void set_linux_initramfs_path(const std::string& linux_initramfs_path);
968     void set_linux_root_image(const std::string& linux_root_image);
969     void set_fuchsia_zedboot_path(const std::string& fuchsia_zedboot_path);
970     void set_fuchsia_multiboot_bin_path(const std::string& fuchsia_multiboot_bin_path);
971     void set_fuchsia_root_image(const std::string& fuchsia_root_image);
972     void set_custom_partition_path(const std::string& custom_partition_path);
973     void set_blank_metadata_image_mb(int blank_metadata_image_mb);
974     void set_blank_sdcard_image_mb(int blank_sdcard_image_mb);
975     void set_bootloader(const std::string& bootloader);
976     void set_initramfs_path(const std::string& initramfs_path);
977     void set_kernel_path(const std::string& kernel_path);
978     void set_guest_android_version(const std::string& guest_android_version);
979     void set_bootconfig_supported(bool bootconfig_supported);
980     void set_filename_encryption_mode(const std::string& userdata_format);
981     void set_external_network_mode(ExternalNetworkMode network_mode);
982     void set_hibernation_partition_image(
983         const std::string& hibernation_partition_image);
984 
985     // Whether we should start vhal_proxy_server for the guest-side VHAL to
986     // connect to.
987     void set_start_vhal_proxy_server(bool enable_vhal_proxy_server);
988 
989     void set_audio_output_streams_count(int count);
990 
991     void set_enable_tap_devices(bool);
992 
993    private:
994     void SetPath(const std::string& key, const std::string& path);
995   };
996 
997   class EnvironmentSpecific;
998   class MutableEnvironmentSpecific;
999 
1000   MutableEnvironmentSpecific ForEnvironment(const std::string& envName);
1001   EnvironmentSpecific ForEnvironment(const std::string& envName) const;
1002 
1003   MutableEnvironmentSpecific ForDefaultEnvironment();
1004   EnvironmentSpecific ForDefaultEnvironment() const;
1005 
1006   std::vector<std::string> environment_dirs() const;
1007 
1008   class EnvironmentSpecific {
1009     friend EnvironmentSpecific CuttlefishConfig::ForEnvironment(
1010         const std::string&) const;
1011     friend EnvironmentSpecific CuttlefishConfig::ForDefaultEnvironment() const;
1012 
1013     const CuttlefishConfig* config_;
1014     std::string envName_;
1015 
EnvironmentSpecific(const CuttlefishConfig * config,const std::string & envName)1016     EnvironmentSpecific(const CuttlefishConfig* config,
1017                         const std::string& envName)
1018         : config_(config), envName_(envName) {}
1019 
1020     Json::Value* Dictionary();
1021     const Json::Value* Dictionary() const;
1022 
1023    public:
1024     std::string environment_name() const;
1025 
1026     std::string environment_uds_dir() const;
1027     std::string PerEnvironmentUdsPath(const std::string& file_name) const;
1028 
1029     std::string environment_dir() const;
1030     std::string PerEnvironmentPath(const std::string& file_name) const;
1031 
1032     std::string PerEnvironmentLogPath(const std::string& file_name) const;
1033 
1034     std::string PerEnvironmentGrpcSocketPath(
1035         const std::string& file_name) const;
1036 
1037     std::string control_socket_path() const;
1038     std::string launcher_log_path() const;
1039 
1040     std::string casimir_nci_socket_path() const;
1041     std::string casimir_rf_socket_path() const;
1042 
1043     // wmediumd related configs
1044     bool enable_wifi() const;
1045     bool start_wmediumd() const;
1046     std::string vhost_user_mac80211_hwsim() const;
1047     std::string wmediumd_api_server_socket() const;
1048     std::string wmediumd_config() const;
1049     int wmediumd_mac_prefix() const;
1050     int group_uuid() const;
1051   };
1052 
1053   class MutableEnvironmentSpecific {
1054     friend MutableEnvironmentSpecific CuttlefishConfig::ForEnvironment(
1055         const std::string&);
1056     friend MutableEnvironmentSpecific CuttlefishConfig::ForDefaultEnvironment();
1057 
1058     CuttlefishConfig* config_;
1059     std::string envName_;
1060 
MutableEnvironmentSpecific(CuttlefishConfig * config,const std::string & envName)1061     MutableEnvironmentSpecific(CuttlefishConfig* config,
1062                                const std::string& envName)
1063         : config_(config), envName_(envName) {}
1064 
1065     Json::Value* Dictionary();
1066 
1067    public:
1068     // wmediumd related configs
1069     void set_enable_wifi(const bool enable_wifi);
1070     void set_start_wmediumd(bool start);
1071     void set_vhost_user_mac80211_hwsim(const std::string& path);
1072     void set_wmediumd_api_server_socket(const std::string& path);
1073     void set_wmediumd_config(const std::string& path);
1074     void set_wmediumd_mac_prefix(int mac_prefix);
1075 
1076     void set_group_uuid(const int group_uuid);
1077   };
1078 
1079  private:
1080   std::unique_ptr<Json::Value> dictionary_;
1081 
1082   static CuttlefishConfig* BuildConfigImpl(const std::string& path);
1083 
1084   CuttlefishConfig(const CuttlefishConfig&) = delete;
1085   CuttlefishConfig& operator=(const CuttlefishConfig&) = delete;
1086 };
1087 
1088 // Whether the instance is restored from a snapshot. Stays true until the device
1089 // reboots.
1090 // When the device is booting, the config  init function checks if
1091 // "FLAGS_snapshot_path" is not empty, and if it isn't empty, a file called
1092 // "restore" will be created to keep track of the restore.
1093 // This is necessary because we don't want to
1094 // modify the config when the device boots, however we also want to only restore
1095 // once. Tracking via "restore" is necessary as a bug existed when checking if
1096 // "snapshot_path" existed during boot, where a restart or a powerwash of the
1097 // device would actually perform a restore instead of their respective actions.
1098 bool IsRestoring(const CuttlefishConfig&);
1099 
1100 // Vhost-user-vsock modes
1101 extern const char* const kVhostUserVsockModeAuto;
1102 extern const char* const kVhostUserVsockModeTrue;
1103 extern const char* const kVhostUserVsockModeFalse;
1104 
1105 // GPU modes
1106 extern const char* const kGpuModeAuto;
1107 extern const char* const kGpuModeCustom;
1108 extern const char* const kGpuModeDrmVirgl;
1109 extern const char* const kGpuModeGfxstream;
1110 extern const char* const kGpuModeGfxstreamGuestAngle;
1111 extern const char* const kGpuModeGfxstreamGuestAngleHostSwiftShader;
1112 extern const char* const kGpuModeGfxstreamGuestAngleHostLavapipe;
1113 extern const char* const kGpuModeGuestSwiftshader;
1114 extern const char* const kGpuModeNone;
1115 
1116 // GPU vhost user modes
1117 extern const char* const kGpuVhostUserModeAuto;
1118 extern const char* const kGpuVhostUserModeOn;
1119 extern const char* const kGpuVhostUserModeOff;
1120 
1121 // HwComposer modes
1122 extern const char* const kHwComposerAuto;
1123 extern const char* const kHwComposerDrm;
1124 extern const char* const kHwComposerRanchu;
1125 extern const char* const kHwComposerNone;
1126 }  // namespace cuttlefish
1127 
1128 #if FMT_VERSION >= 90000
1129 template <>
1130 struct fmt::formatter<cuttlefish::ExternalNetworkMode> : ostream_formatter {};
1131 template <>
1132 struct fmt::formatter<cuttlefish::VmmMode> : ostream_formatter {};
1133 #endif
1134