1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/libs/config/kernel_args.h"
18
19 #include <array>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23
24 #include "common/libs/utils/environment.h"
25 #include "common/libs/utils/files.h"
26 #include "host/libs/config/cuttlefish_config.h"
27 #include "host/libs/vm_manager/qemu_manager.h"
28 #include "host/libs/vm_manager/vm_manager.h"
29
30 namespace cuttlefish {
31
32 using vm_manager::QemuManager;
33
34 namespace {
35
36 template<typename T>
AppendVector(std::vector<T> * destination,const std::vector<T> & source)37 void AppendVector(std::vector<T>* destination, const std::vector<T>& source) {
38 destination->insert(destination->end(), source.begin(), source.end());
39 }
40
41 // TODO(schuffelen): Move more of this into host/libs/vm_manager, as a
42 // substitute for the vm_manager comparisons.
VmManagerKernelCmdline(const CuttlefishConfig & config)43 std::vector<std::string> VmManagerKernelCmdline(const CuttlefishConfig& config) {
44 std::vector<std::string> vm_manager_cmdline;
45 if (config.vm_manager() == QemuManager::name() || config.use_bootloader()) {
46 // crosvm sets up the console= earlycon= panic= flags for us if booting straight to
47 // the kernel, but QEMU and the bootloader via crosvm does not.
48 AppendVector(&vm_manager_cmdline, {"console=hvc0", "panic=-1"});
49 Arch target_arch = config.target_arch();
50 if (target_arch == Arch::Arm64 || target_arch == Arch::Arm) {
51 if (config.vm_manager() == QemuManager::name()) {
52 // To update the pl011 address:
53 // $ qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine dumpdtb=virt.dtb
54 // $ dtc -O dts -o virt.dts -I dtb virt.dtb
55 // In the virt.dts file, look for a uart node
56 vm_manager_cmdline.push_back(" earlycon=pl011,mmio32,0x9000000");
57 } else {
58 // Crosvm ARM only supports earlycon uart over mmio.
59 vm_manager_cmdline.push_back(" earlycon=uart8250,mmio,0x3f8");
60 }
61 } else {
62 // To update the uart8250 address:
63 // $ qemu-system-x86_64 -kernel bzImage -serial stdio | grep ttyS0
64 // Only 'io' mode works; mmio and mmio32 do not
65 vm_manager_cmdline.push_back("earlycon=uart8250,io,0x3f8");
66
67 if (config.vm_manager() == QemuManager::name()) {
68 // crosvm doesn't support ACPI PNP, but QEMU does. We need to disable
69 // it on QEMU so that the ISA serial ports aren't claimed by ACPI, so
70 // we can use serdev with platform devices instead
71 vm_manager_cmdline.push_back("pnpacpi=off");
72
73 // crosvm sets up the ramoops.xx= flags for us, but QEMU does not.
74 // See external/crosvm/x86_64/src/lib.rs
75 // this feature is not supported on aarch64
76 vm_manager_cmdline.push_back("ramoops.mem_address=0x100000000");
77 vm_manager_cmdline.push_back("ramoops.mem_size=0x200000");
78 vm_manager_cmdline.push_back("ramoops.console_size=0x80000");
79 vm_manager_cmdline.push_back("ramoops.record_size=0x80000");
80 vm_manager_cmdline.push_back("ramoops.dump_oops=1");
81 } else {
82 // crosvm requires these additional parameters on x86_64 in bootloader mode
83 AppendVector(&vm_manager_cmdline, {"pci=noacpi", "reboot=k"});
84 }
85 }
86 }
87
88 if (config.console() && config.kgdb()) {
89 AppendVector(&vm_manager_cmdline, {"kgdboc_earlycon", "kgdbcon",
90 "kgdboc=" + config.console_dev()});
91 }
92 return vm_manager_cmdline;
93 }
94
95 } // namespace
96
KernelCommandLineFromConfig(const CuttlefishConfig & config)97 std::vector<std::string> KernelCommandLineFromConfig(
98 const CuttlefishConfig& config) {
99 std::vector<std::string> kernel_cmdline;
100
101 AppendVector(&kernel_cmdline, VmManagerKernelCmdline(config));
102
103 if (config.enable_gnss_grpc_proxy()) {
104 kernel_cmdline.push_back("gnss_cmdline.serdev=serial8250/serial0/serial0-0");
105 kernel_cmdline.push_back("gnss_cmdline.type=0");
106 kernel_cmdline.push_back("serdev_ttyport.pdev_tty_port=ttyS1");
107 }
108
109 if (config.guest_audit_security()) {
110 kernel_cmdline.push_back("audit=1");
111 } else {
112 kernel_cmdline.push_back("audit=0");
113 }
114
115 AppendVector(&kernel_cmdline, config.extra_kernel_cmdline());
116
117 return kernel_cmdline;
118 }
119
120 } // namespace cuttlefish
121