#!/bin/bash

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This utility uses cbootimage to signs a bootstub so that the Tegra boot ROM
# will load and run it.

# Include common CrOS utilities.
. "/usr/lib/crosutils/common.sh"

# Command line options
DEFINE_string bct "" "DDR memory timing BCT file."
DEFINE_string bootstub "" "Bootstub firmware image to sign."
DEFINE_string output "bootstub.bin" "Signed bootstub + BCT output file."
DEFINE_string text_base "0xe08000" "Load address and entry point for bootstub."
DEFINE_string config "" "Directory for temporary configuration files."

# Parse command line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

# Die on errors.
set -e

CROS_LOG_PREFIX="cros_sign_bootstub"

function construct_config() {
  local bct_file="$1"
  local bootstub="$2"
  local text_base="$3"

  echo "Version    = 1;"
  echo "Redundancy = 1;"
  echo "Bctfile    = ${bct_file};"
  echo "BootLoader = ${bootstub},${text_base},${text_base},Complete;"
}

###############################################################################

check_for_file "BCT" "      " "${FLAGS_bct}"
check_for_file "bootstub" " " "${FLAGS_bootstub}"

check_for_tool "cbootimage" "cbootimage"

if [ -z "${FLAGS_config}" ]; then
  config=$(mktemp -d)

  trap "rm -rf ${config}" EXIT
else
  mkdir -p "${FLAGS_config}"

  config=${FLAGS_config}
fi

construct_config \
  "${FLAGS_bct}" \
  "${FLAGS_bootstub}" \
  "${FLAGS_text_base}" > "${config}/boot.cfg"

cbootimage "${config}/boot.cfg" "${FLAGS_output}"
