Extend modify_kernel_command_line() feature to grub and syslinux (reland)

For grub and syslinux this feature is a bit inferior to depthcharge
since it requires a full ./build_image, whereas for depthcharge a much
faster bin/cros_make_image_bootable build shortcut is enough.  Initial
attempts to implement at bin/cros_make_image_bootable time proved too
complex and brittle, involving: sed-wrangling, inconsistencies between
grub and syslinux in the installer, etc.

Some more background information in chromium:461595 #c20

With this change, in order to simplify the script, the --enable_serial
flag now defaults to "tty2". Also, the 'quiet' and 'debug' log options
are removed. There is already --loglevel flag that can be used to set
appropriate kernel log-level by boards.

TEST=Two systems are required to test both GRUB and syslinux:
- one with EFI to test GRUB
- one with SeaBIOS and depthcharge to test:
   - syslinux
   - Verified Boot (unaffected)

By choosing two systems compatible enough with each other, a single
${BOARD} can be used and build time saved.

Firstly, make sure this does not introduce any regression: images before
and after this CL both boot with the exact same /proc/cmdline and config
files (assuming they have no build_kernel_image.sh customization)

Next, actually use the feature: before building the image(s), create
one build_kernel_image.sh file per BOARD used. For instance:

cat <<"EOF" >src/overlays/overlay-${BOARD}/scripts/build_kernel_image.sh
modify_kernel_command_line() {
  # Use special characters to try to trigger quoting bugs if any.
 printf '%s\n' 'HELLO_ * " &_TEST_0003" '  >> "$1"

Board overlays can be defined in various places. The second most common
location is: src/private-overlays/overlay-${BOARD}-private/scripts/.
You may need to create the scripts/ directory.

Build and boot the image from USB (or SD). Make sure /proc/cmdline has:
- "cros_legacy" when booting USB with SeaBIOS (Ctrl-L)
- "cros_efi" when booting USB with EFI
- the HELLO_... test content above in both cases.

Inspect the /boot directory and make sure content is as expected.
Mount ESP partition 12 and inspect it too. This command is useful:
  diff -ru /boot/ /tmp/sdb12/

Next test the installer. Do NOT proceed and test the installer if any
previous testing above failed in any way no matter how small.  To
install run either:
  chromeos-install --dst /dev/sdX --target_bios efi
  chromeos-install --dst /dev/sdX --target_bios legacy
depending on the BOARD (/dev/sdX is usually: /dev/sda)

Reboot without the USB stick and run the same inspections above again,
now on the just installed hard drive. On the ${BOARD} with
depthcharge test with and without SeaBIOS.

Reverted-Change-Id: I207bed7b71f92c39229f218364c1dee63c4dcad2

Change-Id: Ibce7c0391191e2b318ff74dee936308b853b99a9
Signed-off-by: Marc Herbert <marc.herbert@intel.com>
Reviewed-on: https://chromium-review.googlesource.com/305740
Commit-Ready: Aditya Kali <adityakali@google.com>
Tested-by: Aditya Kali <adityakali@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index f86b4b0..79f4b63 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -267,6 +267,7 @@
   ${BUILD_LIBRARY_DIR}/create_legacy_bootloader_templates.sh \
     --arch=${ARCH} \
+    --board=${BOARD} \
     --to="${root_fs_dir}"/boot \
     --boot_args="${FLAGS_boot_args}" \
     --enable_serial="${FLAGS_enable_serial}" \
diff --git a/build_library/create_legacy_bootloader_templates.sh b/build_library/create_legacy_bootloader_templates.sh
index 059b6b3..ec36cb3 100755
--- a/build_library/create_legacy_bootloader_templates.sh
+++ b/build_library/create_legacy_bootloader_templates.sh
@@ -16,6 +16,8 @@
 # Flags.
 DEFINE_string arch "x86" \
   "The boot architecture: arm or x86. (Default: x86)"
+DEFINE_string board "" \
+  "Board we're building for."
 DEFINE_string to "/tmp/boot" \
   "Path to populate with bootloader templates (Default: /tmp/boot)"
 DEFINE_string boot_args "" \
@@ -24,8 +26,8 @@
   "Default all bootloaders to NOT use boot cache."
 DEFINE_boolean enable_rootfs_verification ${FLAGS_FALSE} \
   "Controls if verity is used for root filesystem checking (Default: false)"
-DEFINE_string enable_serial "" \
-  "Enable serial port for printks. Example values: ttyS0"
+DEFINE_string enable_serial "tty2" \
+  "Enable serial port for printks. Example values: ttyS0 (Default: tty2)"
 DEFINE_integer loglevel 7 \
   "The loglevel to add to the kernel command line."
 DEFINE_integer verity_error_behavior 3 \
@@ -50,15 +52,46 @@
-# Common kernel command-line args
-common_args="init=/sbin/init boot=local rootwait ro noresume noswap"
-common_args="${common_args} loglevel=${FLAGS_loglevel} ${FLAGS_boot_args}"
+# Common kernel command-line args. Write them to a temporary config_file so that
+# boards can modify them if needed.
+# TODO: This code to support modifying kernel command line by boards is very
+# similar to the one in build_kernel_image.sh. This could be refactored into a
+# common place. Until then it needs to be kept consistent.
+config_file="$(mktemp legacy_config_XXXXXXXXXX.txt)"
+cleanup() {
+  rm -f "${config_file}"
+trap cleanup EXIT
-if [[ -n "${FLAGS_enable_serial}" ]]; then
-  common_args="${common_args} console=${FLAGS_enable_serial} debug"
-  common_args="${common_args} console=tty2 quiet"
+cat <<EOF > "${config_file}"
+# Support optional, board-specific kernel parameters.
+# Intended to be overridden by boards that wish to add to the command
+# line. Same code in build_kernel_image.sh; this one here is for grub
+# and syslinux.
+# $1 - output file containing boot args.
+modify_kernel_command_line() {
+  :
+. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
+load_board_specific_script "build_kernel_image.sh"
+modify_kernel_command_line "${config_file}"
+# Read back the config_file; translate newlines to space
+common_args="$(tr "\n" " " < "${config_file}")"
+trap - EXIT
 # Common verified boot command-line args