xref: /netbsd-src/sys/arch/evbppc/compile/walnut-mkimg.sh (revision 75430fc28a6f4ccfe32d0e692f50bd3533959454)
1#!/bin/sh
2# $NetBSD: walnut-mkimg.sh,v 1.6 2024/12/11 00:06:58 maya Exp $
3
4# Convert an input to a TFTP image loadable by the IBM PowerPC OpenBIOS.
5
6magic=5394511	# IBM OpenBIOS magic number 0x0052504f
7start=0
8size=0
9overwrite=0
10
11if [ $# -ne 2 ] ; then
12	echo usage: $0 input image 1>&2
13	exit 1
14fi
15
16input=$1; shift
17output=$1; shift
18
19: ${OBJDUMP=objdump}
20: ${OBJCOPY=objcopy}
21: ${STAT=stat}
22: ${AWK=awk}
23: ${FILE=file}
24
25file=$( ${FILE} $input )
26case $file in
27*:\ ELF\ *)
28	start=`${OBJDUMP} -f ${input} | ${AWK} '/start address/ { print $NF }'`
29	start=`printf "%d" $start`
30	${OBJCOPY} -O binary ${input} ${input}.bin.$$
31	;;
32*)
33	case $file in
34	*\ [Ff]ile\ [Ss]ystem*|*\ [Ff]ilesystem*)
35		overwrite=1
36		;;
37	esac
38	cp ${input} ${input}.bin.$$
39	;;
40esac
41
42size=$(${STAT} -f '%z' ${input}.bin.$$)
43size=$(( ( $size + 511 ) / 512 ))
44
45enc()
46{
47	local _x=$1; shift
48	printf $( printf '\\x%x' $_x )
49}
50
51be32enc()
52{
53	local _x=$1; shift
54	enc $(( ( $_x >> 24 ) & 0xff ))
55	enc $(( ( $_x >> 16 ) & 0xff ))
56	enc $(( ( $_x >>  8 ) & 0xff ))
57	enc $(( ( $_x >>  0 ) & 0xff ))
58}
59
60{
61	be32enc $magic
62	be32enc $start
63	be32enc $size
64	be32enc 0
65	be32enc $start
66	be32enc 0
67	be32enc 0
68	be32enc 0
69} > ${input}.hdr.$$
70
71if [ $overwrite = 0 ]; then
72	cat ${input}.hdr.$$ ${input}.bin.$$ > ${output}
73else
74	cp ${input}.bin.$$ ${output}
75	dd if=${input}.hdr.$$ of=${output} conv=notrunc
76fi
77
78rm -f ${input}.hdr.$$ ${input}.bin.$$
79exit
80