[tools][aemu] Add "echo" option to aemu command builder.

r.android.com/1557217 adds a new option to -chardev arguments
to enable/disable echo of stdio chardevs to AEMU.

Our bringup tests use the device for serial output and needs
to disable stdio echo. So we add this option to aemu command
builder.

TEST=aemu_test
     bringup.x64 on botanist (with r.android.com/1557217 patched to AEMU)

Change-Id: I161ad8c0f67109aa003c5848730c4ffd4ca32b8e
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/474319
Commit-Queue: Yilong Li <liyl@google.com>
Reviewed-by: Yuan Zhi <yuanzhi@google.com>
Reviewed-by: Nathan Mulcahey <nmulcahey@google.com>
diff --git a/tools/qemu/aemu.go b/tools/qemu/aemu.go
index 6b23a38..0e5ea72 100644
--- a/tools/qemu/aemu.go
+++ b/tools/qemu/aemu.go
@@ -5,6 +5,7 @@
 package qemu
 
 import (
+	"fmt"
 	"sort"
 	"strings"
 )
@@ -34,6 +35,19 @@
 	return a
 }
 
+func (a *AEMUCommandBuilder) AddSerial(c Chardev) {
+	args := []string{"stdio", fmt.Sprintf("id=%s", c.ID)}
+	if c.Logfile != "" {
+		args = append(args, fmt.Sprintf("logfile=%s", c.Logfile))
+	}
+	if !c.Signal {
+		args = append(args, "signal=off")
+	}
+	args = append(args, "echo=off")
+	a.SetFlag("-chardev", strings.Join(args, ","))
+	a.SetFlag("-serial", fmt.Sprintf("chardev:%s", c.ID))
+}
+
 func (a *AEMUCommandBuilder) SetFeature(feature string) {
 	a.features = append(a.features, feature)
 }
diff --git a/tools/qemu/aemu_test.go b/tools/qemu/aemu_test.go
index e98d276..008ea4b 100644
--- a/tools/qemu/aemu_test.go
+++ b/tools/qemu/aemu_test.go
@@ -181,4 +181,41 @@
 			"-append", "infra.foo=bar kernel.serial=legacy"},
 		err: nil,
 	}, cmd, err)
+
+	b.AddSerial(
+		Chardev{
+			ID:      "char0",
+			Logfile: "logfile.txt",
+			Signal:  false,
+		},
+	)
+
+	cmd, err = b.Build()
+	check(t, expected{
+		cmd: []string{
+			"./bin/emulator",
+			"-feature", "GLDirectMem,KVM,VirtioInput,Vulkan",
+			"-gpu", "swiftshader_indirect",
+			"-no-window",
+			"-fuchsia",
+			"-kernel", "./data/qemu-kernel",
+			"-initrd", "./data/zircon-a",
+			"-vga", "none",
+			"-device", "virtio-keyboard-pci",
+			"-device", "virtio_input_multi_touch_pci_1",
+			"-machine", "virt-2.12,gic-version=host",
+			"-cpu", "host",
+			"-enable-kvm",
+			"-m", "4096",
+			"-smp", "4",
+			"-object", "iothread,id=iothread-otherdisk",
+			"-drive", "id=otherdisk,file=./data/otherdisk,format=raw,if=none,cache=unsafe,aio=threads",
+			"-device", "virtio-blk-pci,drive=otherdisk,iothread=iothread-otherdisk,addr=04.2",
+			"-netdev", "user,id=net0",
+			"-device", "virtio-net-pci,mac=52:54:00:63:5e:7a,netdev=net0",
+			"-chardev", "stdio,id=char0,logfile=logfile.txt,signal=off,echo=off",
+			"-serial", "chardev:char0",
+			"-append", "infra.foo=bar kernel.serial=legacy"},
+		err: nil,
+	}, cmd, err)
 }