blob: e307ffcc209ab2c66de9a1a3fae7fe3e966e693a [file] [log] [blame]
Cody Schuffelen147b88e2019-09-09 16:00:11 -07001#include "host/commands/assemble_cvd/flags.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08002
Jorge E. Moreirae049b792019-12-18 18:17:48 -08003#include <algorithm>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08004#include <iostream>
Ram Muthiahaad97c52019-08-14 17:05:01 -07005#include <fstream>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08006
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -08007#include <android-base/strings.h>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08008#include <gflags/gflags.h>
9#include <glog/logging.h>
10
11#include "common/libs/utils/environment.h"
12#include "common/libs/utils/files.h"
Cody Schuffelen147b88e2019-09-09 16:00:11 -070013#include "host/commands/assemble_cvd/boot_image_unpacker.h"
14#include "host/commands/assemble_cvd/data_image.h"
15#include "host/commands/assemble_cvd/image_aggregator.h"
16#include "host/commands/assemble_cvd/assembler_defs.h"
Cody Schuffelenb737ef52019-09-25 14:54:38 -070017#include "host/commands/assemble_cvd/super_image_mixer.h"
Cody Schuffelen605e6852019-10-16 16:22:24 -070018#include "host/libs/config/fetcher_config.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080019#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080020#include "host/libs/vm_manager/qemu_manager.h"
21#include "host/libs/vm_manager/vm_manager.h"
22
Cody Schuffelen47c57852019-12-13 13:50:50 -080023using vsoc::ForCurrentInstance;
Cody Schuffelene71fa352019-09-10 16:11:58 -070024using cvd::AssemblerExitCodes;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080025
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080026DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090027DEFINE_string(metadata_image, "", "Location of the metadata partition image "
28 "to be generated.");
29DEFINE_int32(blank_metadata_image_mb, 16,
30 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080031DEFINE_int32(cpus, 2, "Virtual CPU count.");
32DEFINE_string(data_image, "", "Location of the data partition image.");
33DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
34 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
35 "'always_create'.");
36DEFINE_int32(blank_data_image_mb, 0,
37 "The size of the blank data image to generate, MB.");
Cody Schuffelenbdca7b82019-12-04 20:09:22 +000038DEFINE_string(blank_data_image_fmt, "ext4",
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080039 "The fs format for the blank data image. Used with mkfs.");
40DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000041 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080042
43DEFINE_int32(x_res, 720, "Width of the screen in pixels");
44DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
45DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
46DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080047DEFINE_string(kernel_path, "",
48 "Path to the kernel. Overrides the one from the boot image");
Ram Muthiahaad97c52019-08-14 17:05:01 -070049DEFINE_string(initramfs_path, "", "Path to the initramfs");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080050DEFINE_bool(decompress_kernel, false,
Jorge E. Moreira36294042019-06-07 15:23:18 -070051 "Whether to decompress the kernel image.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080052DEFINE_string(extra_kernel_cmdline, "",
53 "Additional flags to put on the kernel command line");
54DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070055DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen79d4c192019-12-04 15:13:26 -080056 "Whether to run in enforcing mode (non permissive).");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080057DEFINE_bool(guest_audit_security, true,
58 "Whether to log security audits.");
Alistair Delva33b861f2019-12-26 12:23:59 -080059DEFINE_bool(guest_force_normal_boot, true,
60 "Whether to force the boot sequence to skip recovery.");
Yifan Hong19d713e2019-05-01 14:12:07 -070061DEFINE_string(boot_image, "",
62 "Location of cuttlefish boot image. If empty it is assumed to be "
63 "boot.img in the directory specified by -system_image_dir.");
Ram Muthiah1aa02f82019-10-22 20:26:28 +000064DEFINE_string(vendor_boot_image, "",
65 "Location of cuttlefish vendor boot image. If empty it is assumed to "
66 "be vendor_boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080067DEFINE_int32(memory_mb, 2048,
68 "Total amount of memory available for guest, MB.");
Cody Schuffelen47c57852019-12-13 13:50:50 -080069DEFINE_string(mobile_interface, ForCurrentInstance("cvd-mbr-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080070 "Network interface to use for mobile networking");
Cody Schuffelen47c57852019-12-13 13:50:50 -080071DEFINE_string(mobile_tap_name, ForCurrentInstance("cvd-mtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080072 "The name of the tap interface to use for mobile");
Cody Schuffelen47c57852019-12-13 13:50:50 -080073DEFINE_string(serial_number, ForCurrentInstance("CUTTLEFISHCVD"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080074 "Serial number to use for the device");
Cody Schuffelenab6d3452019-12-13 15:54:27 -080075DEFINE_string(assembly_dir,
76 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_assembly",
77 "A directory to put generated files common between instances");
78DEFINE_string(instance_dir,
79 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime",
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080080 "A directory to put all instance specific files");
81DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000082 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080083 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070084DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070085 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
86 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070087
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080088DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
89 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -080090DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -070091DEFINE_string(misc_image, "",
92 "Location of the misc partition image. If the image does not "
93 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -070094DEFINE_string(composite_disk, "", "Location of the composite disk image. "
95 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080096
97DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
98 " host kernel. This is only used during transition of our clients."
99 " Will be deprecated soon.");
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800100DEFINE_bool(start_vnc_server, false, "Whether to start the vnc server process.");
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800101
102DEFINE_bool(start_webrtc, false, "Whether to start the webrtc process.");
103
104DEFINE_string(
105 webrtc_assets_dir,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800106 vsoc::DefaultHostArtifactsPath("usr/share/webrtc/assets"),
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800107 "Path to WebRTC webpage assets.");
108
109DEFINE_string(
110 webrtc_certs_dir,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800111 vsoc::DefaultHostArtifactsPath("usr/share/webrtc/certs"),
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800112 "Path to WebRTC certificates directory.");
113
114DEFINE_string(
115 webrtc_public_ip,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800116 "127.0.0.1",
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800117 "Public IPv4 address of your server, a.b.c.d format");
118
119DEFINE_bool(
120 webrtc_enable_adb_websocket,
121 false,
122 "If enabled, exposes local adb service through a websocket.");
123
Cody Schuffelen47c57852019-12-13 13:50:50 -0800124DEFINE_int32(vnc_server_port, ForCurrentInstance(6444),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800125 "The port on which the vnc server should listen");
Cody Schuffelen10743332019-04-15 16:50:49 -0700126DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800127 "Mode for ADB connection."
Cody Schuffelen63d10052019-02-26 12:21:53 -0800128 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
129 "'native_vsock' for a direct connection to the guest ADB over "
130 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
131 "the guest ADB server, or a comma separated list of types as in "
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800132 "'native_vsock,vsock_half_tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800133DEFINE_bool(run_adb_connector, true,
134 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000135 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen47c57852019-12-13 13:50:50 -0800136DEFINE_string(wifi_tap_name, ForCurrentInstance("cvd-wtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800137 "The name of the tap interface to use for wifi");
138DEFINE_int32(vsock_guest_cid,
139 vsoc::GetDefaultPerInstanceVsockCid(),
140 "Guest identifier for vsock. Disabled if under 3.");
141
Cody Schuffelen47c57852019-12-13 13:50:50 -0800142DEFINE_string(uuid, vsoc::ForCurrentInstance(vsoc::kDefaultUuidPrefix),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800143 "UUID to use for the device. Random if not specified");
144DEFINE_bool(daemon, false,
145 "Run cuttlefish in background, the launcher exits on boot "
146 "completed/failed");
147
148DEFINE_string(device_title, "", "Human readable name for the instance, "
149 "used by the vnc_server for its server title");
150DEFINE_string(setupwizard_mode, "DISABLED",
151 "One of DISABLED,OPTIONAL,REQUIRED");
152
153DEFINE_string(qemu_binary,
154 "/usr/bin/qemu-system-x86_64",
155 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800156DEFINE_string(crosvm_binary,
157 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
158 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800159DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800160DEFINE_string(logcat_mode, "", "How to send android's log messages from "
161 "guest to host. One of [serial, vsock]");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700162DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700163 "both the guest and the host");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700164DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
165DEFINE_string(bootloader, "", "Bootloader binary path");
David Anderson637bfd62019-09-26 13:23:21 -0700166DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
167 "the slot will be chosen based on the misc partition if using a "
168 "bootloader. It will default to 'a' if empty and not using a "
169 "bootloader.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700170
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800171namespace {
172
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700173const std::string kKernelDefaultPath = "kernel";
174const std::string kInitramfsImg = "initramfs.img";
175const std::string kRamdiskConcatExt = ".concat";
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700176
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800177bool ResolveInstanceFiles() {
178 if (FLAGS_system_image_dir.empty()) {
179 LOG(ERROR) << "--system_image_dir must be specified.";
180 return false;
181 }
182
183 // If user did not specify location of either of these files, expect them to
184 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800185 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
186 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
187 google::FlagSettingMode::SET_FLAGS_DEFAULT);
188 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
189 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
190 google::FlagSettingMode::SET_FLAGS_DEFAULT);
191 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
192 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
193 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900194 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
195 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
196 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800197 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
198 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
199 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700200 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
201 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
202 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700203 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
204 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
205 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000206 std::string default_vendor_boot_image = FLAGS_system_image_dir
207 + "/vendor_boot.img";
208 SetCommandLineOptionWithMode("vendor_boot_image",
209 default_vendor_boot_image.c_str(),
210 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800211
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800212 return true;
213}
214
215std::string GetCuttlefishEnvPath() {
216 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
217}
218
Cody Schuffelen2168c242019-12-13 16:15:27 -0800219std::string GetLegacyConfigFilePath(const vsoc::CuttlefishConfig& config) {
220 return config.ForDefaultInstance().PerInstancePath("cuttlefish_config.json");
221}
222
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700223int GetHostPort() {
224 constexpr int kFirstHostPort = 6520;
Cody Schuffelen47c57852019-12-13 13:50:50 -0800225 return vsoc::ForCurrentInstance(kFirstHostPort);
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700226}
227
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800228int NumStreamers() {
229 auto start_flags = {FLAGS_start_vnc_server, FLAGS_start_webrtc};
230 return std::count(start_flags.begin(), start_flags.end(), true);
231}
232
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800233// Initializes the config object and saves it to file. It doesn't return it, all
234// further uses of the config should happen through the singleton
235bool InitializeCuttlefishConfiguration(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700236 const cvd::BootImageUnpacker& boot_image_unpacker,
237 const cvd::FetcherConfig& fetcher_config) {
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800238 // At most one streamer can be started.
239 CHECK(NumStreamers() <= 1);
240
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800241 vsoc::CuttlefishConfig tmp_config_obj;
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800242 tmp_config_obj.set_assembly_dir(FLAGS_assembly_dir);
Cody Schuffelen47c57852019-12-13 13:50:50 -0800243 auto instance = tmp_config_obj.ForDefaultInstance();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800244 // Set this first so that calls to PerInstancePath below are correct
Cody Schuffeleneb242c42019-12-13 15:16:50 -0800245 instance.set_instance_dir(FLAGS_instance_dir);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800246 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
247 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
248 return false;
249 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700250 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
251 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
252 return false;
253 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800254 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700255 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800256 if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(),
257 tmp_config_obj.gpu_mode()).empty()) {
Greg Hartmana3c552d2019-03-28 18:20:48 -0700258 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
259 " does not work with vm_manager=" << FLAGS_vm_manager;
260 return false;
261 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800262
Cody Schuffelen47c57852019-12-13 13:50:50 -0800263 instance.set_serial_number(FLAGS_serial_number);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800264
265 tmp_config_obj.set_cpus(FLAGS_cpus);
266 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
267
268 tmp_config_obj.set_dpi(FLAGS_dpi);
269 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
270 tmp_config_obj.set_x_res(FLAGS_x_res);
271 tmp_config_obj.set_y_res(FLAGS_y_res);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800272 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
273 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -0800274 std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ",");
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800275 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelen47c57852019-12-13 13:50:50 -0800276 instance.set_host_port(GetHostPort());
277 instance.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800278
Cody Schuffelen47c57852019-12-13 13:50:50 -0800279 instance.set_device_title(FLAGS_device_title);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700280 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
281 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
282 if (foreign_kernel.size()) {
283 tmp_config_obj.set_kernel_image_path(foreign_kernel);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800284 tmp_config_obj.set_use_unpacked_kernel(false);
285 } else {
286 tmp_config_obj.set_kernel_image_path(
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800287 tmp_config_obj.AssemblyPath(kKernelDefaultPath.c_str()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800288 tmp_config_obj.set_use_unpacked_kernel(true);
289 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800290 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
291 if (FLAGS_decompress_kernel) {
292 tmp_config_obj.set_decompressed_kernel_image_path(
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800293 tmp_config_obj.AssemblyPath("vmlinux"));
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800294 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800295
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800296 auto ramdisk_path = tmp_config_obj.AssemblyPath("ramdisk.img");
297 auto vendor_ramdisk_path = tmp_config_obj.AssemblyPath("vendor_ramdisk.img");
Cody Schuffelen1e645542019-11-19 15:44:22 -0800298 if (!boot_image_unpacker.HasRamdiskImage()) {
299 LOG(INFO) << "A ramdisk is required, but the boot image did not have one.";
300 return false;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800301 }
302
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800303 tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
304 tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part);
305 tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security);
306 tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security);
Alistair Delva33b861f2019-12-26 12:23:59 -0800307 tmp_config_obj.set_guest_force_normal_boot(FLAGS_guest_force_normal_boot);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800308 tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800309
Cody Schuffelen2ced6f12019-11-19 15:54:10 -0800310 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700311
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800312 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Ram Muthiahd512c132019-10-23 17:20:45 -0700313 tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path);
314
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700315 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
316 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
317 if (foreign_kernel.size() && !foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700318 // If there's a kernel that's passed in without an initramfs, that implies
319 // user error or a kernel built with no modules. In either case, let's
320 // choose to avoid loading the modules from the vendor ramdisk which are
321 // built for the default cf kernel. Once boot occurs, user error will
322 // become obvious.
323 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
324 } else {
325 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700326 if(foreign_ramdisk.size()) {
327 tmp_config_obj.set_initramfs_path(foreign_ramdisk);
Ram Muthiahd512c132019-10-23 17:20:45 -0700328 }
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700329 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800330
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800331 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800332 tmp_config_obj.set_logcat_receiver_binary(
333 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"));
334 tmp_config_obj.set_config_server_binary(
335 vsoc::DefaultHostArtifactsPath("bin/config_server"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800336
Cody Schuffelen47c57852019-12-13 13:50:50 -0800337 instance.set_mobile_bridge_name(FLAGS_mobile_interface);
338 instance.set_mobile_tap_name(FLAGS_mobile_tap_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800339
Cody Schuffelen47c57852019-12-13 13:50:50 -0800340 instance.set_wifi_tap_name(FLAGS_wifi_tap_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800341
Cody Schuffelen47c57852019-12-13 13:50:50 -0800342 instance.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800343
Cody Schuffelen47c57852019-12-13 13:50:50 -0800344 instance.set_uuid(FLAGS_uuid);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345
346 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800347 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800348 tmp_config_obj.set_console_forwarder_binary(
349 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"));
350 tmp_config_obj.set_kernel_log_monitor_binary(
351 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800352
353 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800354 tmp_config_obj.set_vnc_server_binary(
355 vsoc::DefaultHostArtifactsPath("bin/vnc_server"));
Cody Schuffelen47c57852019-12-13 13:50:50 -0800356 instance.set_vnc_server_port(FLAGS_vnc_server_port);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800357
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800358 tmp_config_obj.set_enable_webrtc(FLAGS_start_webrtc);
359 tmp_config_obj.set_webrtc_binary(
360 vsoc::DefaultHostArtifactsPath("bin/webRTC"));
361 tmp_config_obj.set_webrtc_assets_dir(FLAGS_webrtc_assets_dir);
362 tmp_config_obj.set_webrtc_public_ip(FLAGS_webrtc_public_ip);
363 tmp_config_obj.set_webrtc_certs_dir(FLAGS_webrtc_certs_dir);
364
365 tmp_config_obj.set_webrtc_enable_adb_websocket(
366 FLAGS_webrtc_enable_adb_websocket);
367
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800368 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
369 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800370 tmp_config_obj.set_adb_connector_binary(
371 vsoc::DefaultHostArtifactsPath("bin/adb_connector"));
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800372 tmp_config_obj.set_socket_vsock_proxy_binary(
373 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800374 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800375
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800376 tmp_config_obj.set_data_policy(FLAGS_data_policy);
377 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
378 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
379
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800380 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800381
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700382 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800383 tmp_config_obj.set_tombstone_receiver_binary(
384 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"));
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700385
Cody Schuffelen1300f122019-05-28 18:24:34 -0700386 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
387 tmp_config_obj.set_bootloader(FLAGS_bootloader);
388
David Anderson637bfd62019-09-26 13:23:21 -0700389 if (!FLAGS_boot_slot.empty()) {
390 tmp_config_obj.set_boot_slot(FLAGS_boot_slot);
391 }
392
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800393 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
394
395 auto config_file = GetConfigFilePath(tmp_config_obj);
396 auto config_link = vsoc::GetGlobalConfigFileLink();
397 // Save the config object before starting any host process
398 if (!tmp_config_obj.SaveToFile(config_file)) {
399 LOG(ERROR) << "Unable to save config object";
400 return false;
401 }
Cody Schuffelen2168c242019-12-13 16:15:27 -0800402 auto legacy_config_file = GetLegacyConfigFilePath(tmp_config_obj);
403 if (!tmp_config_obj.SaveToFile(legacy_config_file)) {
404 LOG(ERROR) << "Unable to save legacy config object";
405 return false;
406 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800407 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
408 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
409 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
410 << ": " << strerror(errno);
411 return false;
412 }
413
414 return true;
415}
416
417void SetDefaultFlagsForQemu() {
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800418 // TODO(b/144119457) Use the serial port.
419 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800420 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800421}
422
423void SetDefaultFlagsForCrosvm() {
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800424 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
425 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800426}
427
428bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800429 google::ParseCommandLineNonHelpFlags(argc, argv, true);
430 bool invalid_manager = false;
431 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
432 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800433 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
434 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800435 } else {
436 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
437 << std::endl;
438 invalid_manager = true;
439 }
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800440 if (NumStreamers() == 0) {
441 // This makes the vnc server the default streamer unless the user requests
442 // another via a --star_<streamer> flag, while at the same time it's
443 // possible to run without any streamer by setting --start_vnc_server=false.
444 SetCommandLineOptionWithMode("start_vnc_server", "true",
445 google::FlagSettingMode::SET_FLAGS_DEFAULT);
446 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800447 google::HandleCommandLineHelpFlags();
448 if (invalid_manager) {
449 return false;
450 }
451 // Set the env variable to empty (in case the caller passed a value for it).
452 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
453
454 return ResolveInstanceFiles();
455}
456
457bool CleanPriorFiles() {
458 // Everything on the instance directory
459 std::string prior_files = FLAGS_instance_dir + "/*";
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800460 // Everything in the assembly directory
461 prior_files += " " + FLAGS_assembly_dir + "/*";
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800462 // The environment file
463 prior_files += " " + GetCuttlefishEnvPath();
464 // The global link to the config file
465 prior_files += " " + vsoc::GetGlobalConfigFileLink();
466 LOG(INFO) << "Assuming prior files of " << prior_files;
467 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
468 int rval = std::system(fuser_cmd.c_str());
469 // fuser returns 0 if any of the files are open
470 if (WEXITSTATUS(rval) == 0) {
471 LOG(ERROR) << "Clean aborted: files are in use";
472 return false;
473 }
474 std::string clean_command = "rm -rf " + prior_files;
475 rval = std::system(clean_command.c_str());
476 if (WEXITSTATUS(rval) != 0) {
477 LOG(ERROR) << "Remove of files failed";
478 return false;
479 }
480 return true;
481}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800482
483bool DecompressKernel(const std::string& src, const std::string& dst) {
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800484 cvd::Command decomp_cmd(vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"));
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800485 decomp_cmd.AddParameter(src);
486 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
487 if (!output_file->IsOpen()) {
488 LOG(ERROR) << "Unable to create decompressed image file: "
489 << output_file->StrError();
490 return false;
491 }
492 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
Cody Schuffelene5670872019-12-10 15:04:59 -0800493 auto decomp_proc = decomp_cmd.Start();
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800494 return decomp_proc.Started() && decomp_proc.Wait() == 0;
495}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700496
497void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
498 auto adb_modes = config.adb_mode();
499 adb_modes.erase(vsoc::AdbMode::Unknown);
500 if (adb_modes.size() < 1) {
501 LOG(INFO) << "ADB not enabled";
502 }
503}
504
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800505} // namespace
506
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700507namespace {
508
509std::vector<ImagePartition> disk_config() {
510 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700511 partitions.push_back(ImagePartition {
512 .label = "super",
513 .image_file_path = FLAGS_super_image,
514 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700515 partitions.push_back(ImagePartition {
516 .label = "userdata",
517 .image_file_path = FLAGS_data_image,
518 });
519 partitions.push_back(ImagePartition {
520 .label = "cache",
521 .image_file_path = FLAGS_cache_image,
522 });
523 partitions.push_back(ImagePartition {
524 .label = "metadata",
525 .image_file_path = FLAGS_metadata_image,
526 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700527 partitions.push_back(ImagePartition {
528 .label = "boot",
529 .image_file_path = FLAGS_boot_image,
530 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700531 partitions.push_back(ImagePartition {
532 .label = "misc",
533 .image_file_path = FLAGS_misc_image
534 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700535 return partitions;
536}
537
538bool ShouldCreateCompositeDisk() {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700539 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
540 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
541 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
542 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
543 return true;
544 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700545 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
546 for (auto& partition : disk_config()) {
547 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
548 if (partition_age >= composite_age) {
549 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
550 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
551 return true;
552 }
553 }
554 return false;
555}
556
Ram Muthiah3db0a562019-08-26 13:35:19 -0700557bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
558 const std::string& ramdisk_b_path) {
559 // clear out file of any pre-existing content
560 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
561 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
562 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700563
Ram Muthiah3db0a562019-08-26 13:35:19 -0700564 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700565 return false;
566 }
567
Ram Muthiah3db0a562019-08-26 13:35:19 -0700568 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700569 return true;
570}
571
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700572void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700573 if (FLAGS_composite_disk.empty()) {
574 LOG(FATAL) << "asked to create composite disk, but path was empty";
575 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700576 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
Cody Schuffelena08d9dd2019-12-16 17:55:40 -0800577 std::string header_path = config.AssemblyPath("gpt_header.img");
578 std::string footer_path = config.AssemblyPath("gpt_footer.img");
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700579 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
580 } else {
581 aggregate_image(disk_config(), FLAGS_composite_disk);
582 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700583}
584
585} // namespace
586
Cody Schuffelen605e6852019-10-16 16:22:24 -0700587const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700588 int* argc, char*** argv, cvd::FetcherConfig fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800589 if (!ParseCommandLineFlags(argc, argv)) {
590 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700591 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800592 }
593
594 // Clean up prior files before saving the config file (doing it after would
595 // delete it)
596 if (!CleanPriorFiles()) {
597 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700598 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800599 }
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800600 // Create assembly directory if it doesn't exist.
601 if (!cvd::DirectoryExists(FLAGS_assembly_dir.c_str())) {
602 LOG(INFO) << "Setting up " << FLAGS_assembly_dir;
603 if (mkdir(FLAGS_assembly_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
604 LOG(ERROR) << "Failed to create assembly directory: "
605 << FLAGS_assembly_dir << ". Error: " << errno;
606 exit(AssemblerExitCodes::kAssemblyDirCreationError);
607 }
608 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800609 // Create instance directory if it doesn't exist.
610 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
611 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
612 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
613 LOG(ERROR) << "Failed to create instance directory: "
614 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700615 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800616 }
617 }
618
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700619 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
620 if (!cvd::DirectoryExists(internal_dir)) {
621 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
622 0) {
623 LOG(ERROR) << "Failed to create internal instance directory: "
624 << internal_dir << ". Error: " << errno;
625 exit(AssemblerExitCodes::kInstanceDirCreationError);
626 }
627 }
628
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800629 if (!cvd::FileHasContent(FLAGS_boot_image)) {
630 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
631 exit(cvd::kCuttlefishConfigurationInitError);
632 }
633
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000634 if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) {
635 LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image;
636 exit(cvd::kCuttlefishConfigurationInitError);
637 }
638
639 auto boot_img_unpacker =
640 cvd::BootImageUnpacker::FromImages(FLAGS_boot_image,
641 FLAGS_vendor_boot_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800642
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700643 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config)) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800644 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700645 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800646 }
647 // Do this early so that the config object is ready for anything that needs it
648 auto config = vsoc::CuttlefishConfig::Get();
649 if (!config) {
650 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700651 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800652 }
653
654 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000655 config->vendor_ramdisk_image_path(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800656 config->use_unpacked_kernel()
657 ? config->kernel_image_path()
658 : "")) {
659 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700660 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800661 }
662
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000663 // TODO(134522463) as part of the bootloader refactor, repack the vendor boot
664 // image and use the bootloader to load both the boot and vendor ramdisk.
665 // Until then, this hack to get gki modules into cuttlefish will suffice.
666
667 // If a vendor ramdisk comes in via this mechanism, let it supercede the one
668 // in the vendor boot image. This flag is what kernel presubmit testing uses
669 // to pass in the kernel ramdisk.
Ram Muthiahd512c132019-10-23 17:20:45 -0700670
671 // If no kernel is passed in or an initramfs is made available, the default
672 // vendor boot ramdisk or the initramfs provided should be appended to the
673 // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to
674 // safe to assume that the kernel was built with no modules and expects no
675 // modules for cf to run properly.
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700676 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
677 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
678 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
679 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
680 if(!foreign_kernel.size() || foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700681 const std::string& vendor_ramdisk_path =
682 config->initramfs_path().size() ? config->initramfs_path()
683 : config->vendor_ramdisk_image_path();
684 if(!ConcatRamdisks(config->final_ramdisk_path(),
685 config->ramdisk_image_path(), vendor_ramdisk_path)) {
686 LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk";
687 exit(AssemblerExitCodes::kInitRamFsConcatError);
688 }
Ram Muthiahaad97c52019-08-14 17:05:01 -0700689 }
690
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800691 if (config->decompress_kernel()) {
692 if (!DecompressKernel(config->kernel_image_path(),
693 config->decompressed_kernel_image_path())) {
694 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700695 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800696 }
697 }
698
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800699 ValidateAdbModeFlag(*config);
700
Yifan Hong19d713e2019-05-01 14:12:07 -0700701 // Create misc if necessary
702 if (!InitializeMiscImage(FLAGS_misc_image)) {
703 exit(cvd::kCuttlefishConfigurationInitError);
704 }
705
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800706 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700707 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800708 exit(cvd::kCuttlefishConfigurationInitError);
709 }
710
Cody Schuffelen69c19592019-05-31 16:00:30 -0700711 if (!cvd::FileExists(FLAGS_metadata_image)) {
712 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
713 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900714
Kenny Root1f50ee62020-01-24 12:06:19 -0800715 if (!cvd::FileExists(config->ForDefaultInstance().access_kregistry_path())) {
716 CreateBlankImage(config->ForDefaultInstance().access_kregistry_path(), 1,
717 "none", "64K");
718 }
719
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700720 if (SuperImageNeedsRebuilding(fetcher_config, *config)) {
721 if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) {
722 LOG(ERROR) << "Super image rebuilding requested but could not be completed.";
723 exit(cvd::kCuttlefishConfigurationInitError);
724 }
725 }
726
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700727 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700728 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700729 }
730
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800731 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700732 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800733 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800734 LOG(ERROR) << "File not found: " << file;
735 exit(cvd::kCuttlefishConfigurationInitError);
736 }
737 }
738
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800739 return config;
740}
741
742std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen2168c242019-12-13 16:15:27 -0800743 return config.AssemblyPath("cuttlefish_config.json");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800744}