1#!/bin/bash
2#
3# Copyright (C) 2014 CompuLab, Ltd.
4# Author: Nikita Kiryanov <nikita@compulab.co.il>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16usage()
17{
18	cat >&2 <<EOF
19Usage: $0 <spl image path> <u-boot image path> [-o <path to output file>]
20
21DESCRIPTION
22	This script combines the SPL and U-Boot binaries into a single
23	cm-fx6-firmware image that can be written into on-board SPI flash or SD
24	card.
25
26	The spl binary is placed at 1K offset in the cm-fx6-firmware image,
27	while u-boot is placed in a 64K offset in the cm-fx6-firmware image.
28	The resulting image should be written in offset 0 of the chosen storage.
29	
30OPTIONS
31	-o	Normally the script's output is a file called "cm-fx6-firmware",
32		located in the script's directory. Use this option to provide a
33		different output location and filename.
34EOF
35}
36
37OUTPUT_FILE=cm-fx6-firmware
38
39spl_path="$1"
40uboot_path="$2"
41if [[ -z "$spl_path" || -z "$uboot_path" ]] ; then
42	usage
43	exit 1
44fi
45
46shift
47shift
48
49if [ "$#" -ge  1 ]; then
50	case "$1" in
51	-o)
52		shift
53		OUTPUT_FILE="$1"
54		break
55		;;
56	*)
57		echo "Unknown option $1"
58		usage
59		exit 1
60		;;
61	esac
62fi
63
64echo -e "\nGenerating firmware image from the following binaries:"
65echo -e ">>> SPL: `grep -oazE "SPL [0-9]+\.[0-9]+.* \(... +[0-9]+ [0-9]+ - [0-9]+:[0-9]+:[0-9]+\)" $spl_path`"
66echo -e ">>> U-Boot: `grep -oazE "U-Boot [0-9]+\.[0-9]+.* \(... +[0-9]+ [0-9]+ - [0-9]+:[0-9]+:[0-9]+\)" $uboot_path`\n"
67read -p "Press any key to continue or Ctrl-C to abort"
68
69dd if=/dev/zero count=500 bs=1K | tr '\000' '\377' > "$OUTPUT_FILE"
70dd if="$spl_path" of="$OUTPUT_FILE" bs=1K seek=1 conv=notrunc
71dd if="$uboot_path" of="$OUTPUT_FILE" bs=1K seek=64 conv=notrunc
72
73