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