#!/bin/bash
# -----------------------------------------------------------------------------
#   file	gen_its
#   brief       generate uboot FIT image source file (its file)
#   usage	gen_its <config file>
#
#   Generate the source file for a uboot FIT image. The parameters for the file
#   are read from the config file. The result is print to stdout.
#   Max 10 images can be added to the FIT image.
#
#   return	0: no error
#         	1: error
#
#   created     17.01.2013  STW/G.Waibel (georg.waibel@sensor-technik.de)
# -----------------------------------------------------------------------------


# write its file header
write_intro() 
{
	description=$1

	printf "/*\n"
	printf " * Auto-generated update image source file - DO NOT EDIT\n"
	printf " * generated: $(date)\n"
	printf " */\n\n"

	printf "/dts-v1/;\n"
	printf "/ {\n"
	printf "\tdescription = \"$description\";\n"
	printf "\t#address-cells = <1>;\n\n"
	printf "\timages {\n"
}

# write its file tail
write_outtro() 
{
	printf "\t};\n\n"
	printf "};\n\n"
}

# add its image node
write_body()
{
	name=$1
	description=$2
	data_file=$3
	img_type=$4
	load_addr=$5
	erase_size=$6

	if [ $# -ne 6 ]; then
		printf "Error: Too less parameters for function write_body\n\n" >&2
		exit 1
	fi
	name_len=${#name}
	if [ $name_len -ge 16 ] || [ $name_len -eq 0 ]; then
		printf "Error: Node name \"$name\" too long (max 15 chars)\n\n" >&2
		exit 1
	fi
	if [ ! $data_file ] || [ $data_file == "" ]; then
		printf "Error: Image data file not defined for node \"$name\"\n\n" >&2
		exit 1
	fi
	if [ ! $img_type ] || [ $img_type == "" ]; then
		#printf "Image type not specified for \"$name\", defaulting to \"firmware\"\n" >&2
		img_type="firmware"
	fi
	if [ $img_type != "script" ]; then
		if [ ! $load_addr ] || [ $load_addr == "" ]; then
			printf "Error: Load address not defined for node \"$name\"\n\n" >&2
			exit 1
		fi
	fi
		
	printf "\t\t$name {\n"	
	printf "\t\t\tdescription = \"$description\";\n"
	printf "\t\t\tdata = /incbin/(\"$data_file\");\n"
	printf "\t\t\ttype = \"$img_type\";\n"
	printf "\t\t\tcompression = \"none\";\n"
	if [ $load_addr ] && [ $load_addr != "" ] && [ $load_addr != "n.a." ]; then
			printf "\t\t\tload = <$load_addr>;\n"
	fi
	if [ $erase_size ] && [ $erase_size != "" ]; then
		printf "\t\t\terasesize = <$erase_size>;\n"
	fi
	printf "\t\t\thash@1 {\n"
	printf "\t\t\t\talgo = \"sha1\";\n"
	printf "\t\t\t};\n"
	printf "\t\t};\n"
}



# -----------------------------------------------------------------------------
# script starts here:
# -----------------------------------------------------------------------------

script_name=`basename $0`
script_dir=`dirname $0`
config_file=$1

# pull in user configuration
if [ ! $config_file ] || [ $config_file == "" ]; then
	printf "Error: No configuration file specified\n" >&2
	printf "Usage: $script_name <config_file>\n\n" >&2
	exit 1
fi
if [ ! -e $config_file ]; then
	printf "Error: Configuration file does not exist\n\n" >&2
	exit 1
fi
source $config_file

# write head to its file
write_intro "$image_description"

# add password node to its file
if [ ! $password ] || [ $password == "" ]; then
	printf "Error: Password not defined\n\n" >&2
	exit 1
fi
printf "$password" > pswd.tmp
write_body "pswd" "Password node" "pswd.tmp" "firmware" "n.a." ""

# add all image nodes to its file
for ((i=1; i<=10; i++));
do
	eval name=\${img${i}_name}
	eval description=\${img${i}_description}
	eval data_file=\${img${i}_file}
	eval type=\${img${i}_type}
	eval addr=\${img${i}_addr}
	eval size=\${img${i}_size}

	# only for image type=script:
	# check if script is in image format, if not make image
	if [ $type ] && [ $type == "script" ]; then
		$script_dir/mkimage -l $data_file 1>/dev/null 2>&1
		if [ $? != 0 ]; then
			$script_dir/mkimage -T script -n "uboot_script" -d $data_file script.img >&2
			if [ $? != 0 ]; then
				printf "Error: Could not make script image\n\n"
				exit 1
			fi
			data_file="script.img"
		fi
	fi

	# add node only if node name is specified
	if [ $name ] && [ $name != "" ]; then
		write_body "$name" "$description" "$data_file" "$type" "$addr" "$size"
	fi
done

# write tail to its file
write_outtro

exit 0

