Split LK fastboot into a separate script

Refactor the flash.sh script based on feedback

To flash the board with LK fastboot implementation, the user needs to
run enable_lk_fastboot.sh first

The new scripts still supports different directory location and EVT1
board.

Change-Id: I78aa95cf789e891ccc2b11a1fc8d0fba62505c38
diff --git a/enable_lk_fastboot.sh b/enable_lk_fastboot.sh
new file mode 100755
index 0000000..9e5c610
--- /dev/null
+++ b/enable_lk_fastboot.sh
@@ -0,0 +1,129 @@
+#!/bin/bash
+#
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+DRAM_SIZE=2G
+
+function die {
+  echo "$@" >/dev/stderr
+  exit 1
+}
+
+function load_fastboot {
+  local files_dir="$1"
+  python3 "${files_dir}/flashtools/debug_board_reset.py" --rom &
+  sleep 1
+  pushd "${files_dir}"
+  python3 "${files_dir}/flashtools/fbtool.py" -f "${files_dir}/dl_addr.ini"
+  sleep 1
+  popd
+}
+
+function ensure_files_present {
+  local found_all_files=true
+  local files_dir="$1"; shift
+  local link_dir="$1"; shift
+  local files=(
+    "dl_addr.ini"
+    "flashtools/debug_board_reset.py"
+    "flashtools/dl_addr.ini"
+    "flashtools/fbtool.py"
+    "lk.bin"
+  )
+
+  if [[ "${PRODUCT_OUT}" == "${files_dir}" ]]; then
+    ln -sf "${ROOTDIR}/board/flashtools/dl_addr.ini" "${files_dir}/dl_addr.ini"
+    rm -f "${files_dir}/flashtools"
+    ln -sf "${ROOTDIR}/board/flashtools" "${files_dir}/flashtools"
+  fi
+
+  for file in "${files_dir}/*"; do
+    ln -sf ${file} "${link_dir}"
+  done
+
+  # LK needs to be handled separatedly for now.
+  if [[ -f "${files_dir}/lk_${DRAM_SIZE}.bin" ]]; then
+    ln -sf "${files_dir}/lk_${DRAM_SIZE}.bin" "${link_dir}/lk.bin"
+  else
+    echo "lk_${DRAM_SIZE}.bin is missing!"
+    found_all_files=""
+  fi
+
+  for file in "${files[@]}"; do
+    if [[ ! -f "${link_dir}/${file}" ]]; then
+      echo "${file} is missing!"
+      found_all_files=""
+    fi
+  done
+
+  if [[ -z "${found_all_files}" ]]; then
+    die "Required files are missing. Can not continue."
+  fi
+}
+
+function main {
+		local usage=$(cat <<EOF
+Usage: enable_lk_fastboot.sh [-d <files_dir>]
+  -d <files_dir>      Boot LK with files from <files_dir> (defaults to current dir)
+  -o                  Boot LK on the old boards.
+EOF
+)
+
+  local files_dir                 # -d <files_dir>
+  local lk_fastboot               # -l
+  local args=$(getopt hod: $*)
+  set -- $args
+
+  for i; do
+    case "$1" in
+      -d)  # files dir
+        files_dir="$2"
+        shift 2
+        ;;
+
+      -o)  # old board
+        DRAM_SIZE=1G
+        shift 1
+        ;;
+
+      --)  # end of args
+        shift
+        break
+        ;;
+
+      *)  # help
+        die "${usage}"
+        ;;
+
+    esac
+  done
+
+  if [[ -z "${files_dir}" ]]; then
+    if [[ -n "${PRODUCT_OUT}" ]]; then
+      files_dir="${PRODUCT_OUT}"
+    else
+      files_dir="${SCRIPT_DIR}"
+    fi
+  fi
+
+  link_dir=$(mktemp -d)
+  trap 'rm -rf ${link_dir}' EXIT
+  ensure_files_present "${files_dir}" "${link_dir}"
+
+  load_fastboot "${link_dir}"
+}
+
+main "$@"
diff --git a/flash.sh b/flash.sh
index 6dafd27..1d7cbf5 100755
--- a/flash.sh
+++ b/flash.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Copyright 2018 Google LLC
+# Copyright 2020 Google LLC
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -27,16 +27,6 @@
   $@ || die "Failed to execute '$@'"
 }
 
-function load_fastboot {
-  local files_dir="$1"
-  python3 "${files_dir}/flashtools/debug_board_reset.py" --rom &
-  sleep 1
-  pushd "${files_dir}"
-  python3 "${files_dir}/flashtools/fbtool.py" -f "${files_dir}/dl_addr.ini"
-  sleep 1
-  popd
-}
-
 function flash_partitions {
   local files_dir="$1"
 
@@ -58,40 +48,23 @@
   local files=(
     "bl2.img"
     "boot_arm64.img"
-    "dl_addr.ini"
     "fip.bin"
-    "flashtools/debug_board_reset.py"
-    "flashtools/dl_addr.ini"
-    "flashtools/fbtool.py"
-    "lk.bin"
     "partition-table.img"
     "rootfs_arm64.img"
     "u-boot-env.bin"
   )
 
-  if [[ "${PRODUCT_OUT}" == "${files_dir}" ]]; then
-    ln -sf "${ROOTDIR}/board/flashtools/dl_addr.ini" "${files_dir}/dl_addr.ini"
-    rm -f "${files_dir}/flashtools"
-    ln -sf "${ROOTDIR}/board/flashtools" "${files_dir}/flashtools"
-  fi
-
   for file in "${files_dir}/*"; do
     ln -sf ${file} "${link_dir}"
   done
 
-  # BL2 and LK need to be handled separatedly for now.
+  # BL2 needs to be handled separatedly for now.
   if [[ -f "${files_dir}/bl2_${DRAM_SIZE}.img" ]]; then
     ln -sf "${files_dir}/bl2_${DRAM_SIZE}.img" "${link_dir}/bl2.img"
   else
     echo "bl2_${DRAM_SIZE}.img is missing!"
     found_all_files=""
   fi
-  if [[ -f "${files_dir}/lk_${DRAM_SIZE}.bin" ]]; then
-    ln -sf "${files_dir}/lk_${DRAM_SIZE}.bin" "${link_dir}/lk.bin"
-  else
-    echo "lk_${DRAM_SIZE}.bin is missing!"
-    found_all_files=""
-  fi
 
   for file in "${files[@]}"; do
     if [[ ! -f "${link_dir}/${file}" ]]; then
@@ -138,8 +111,7 @@
 
 function main {
     local usage=$(cat <<EOF
-Usage: flash.sh [-d <files_dir>] [-s <serial>] [-r <detect_retries>] [-o] [-l]
-  -l                  load LK into ram for fastboot mode
+Usage: flash.sh [-d <files_dir>] [-s <serial>] [-r <detect_retries>] [-o]
   -d <files_dir>      flashes files from <files_dir> (defaults to current dir)
   -s <serial>         only flashes the board with the given fastboot serial number
   -r <detect_retries> number of times to retry waiting for a device (defaults to 0)
@@ -219,11 +191,6 @@
   trap 'rm -rf ${link_dir}' EXIT
   ensure_flash_files_present "${files_dir}" "${link_dir}"
 
-  if [[ -n "${lk_fastboot}" ]]; then
-    load_fastboot "${link_dir}"
-    exit 0
-  fi
-
   if [[ -n "${serial_number}" ]]; then
     detect_device_or_die "${detect_retries}" "${serial_number}"
   fi