xref: /freebsd/sys/tools/embed_mfs.sh (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1fcfdd827SRafal Jaworowski#!/bin/sh
2fcfdd827SRafal Jaworowski#
3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
4fe267a55SPedro F. Giffuni#
5fcfdd827SRafal Jaworowski# Copyright (C) 2008 The FreeBSD Project. All rights reserved.
6fcfdd827SRafal Jaworowski#
7fcfdd827SRafal Jaworowski# Redistribution and use in source and binary forms, with or without
8fcfdd827SRafal Jaworowski# modification, are permitted provided that the following conditions
9fcfdd827SRafal Jaworowski# are met:
10fcfdd827SRafal Jaworowski# 1. Redistributions of source code must retain the above copyright
11fcfdd827SRafal Jaworowski#   notice, this list of conditions and the following disclaimer.
12fcfdd827SRafal Jaworowski# 2. Redistributions in binary form must reproduce the above copyright
13fcfdd827SRafal Jaworowski#   notice, this list of conditions and the following disclaimer in the
14fcfdd827SRafal Jaworowski#   documentation and/or other materials provided with the distribution.
15fcfdd827SRafal Jaworowski#
16fcfdd827SRafal Jaworowski# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fcfdd827SRafal Jaworowski# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fcfdd827SRafal Jaworowski# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fcfdd827SRafal Jaworowski# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20fcfdd827SRafal Jaworowski# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fcfdd827SRafal Jaworowski# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fcfdd827SRafal Jaworowski# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fcfdd827SRafal Jaworowski# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fcfdd827SRafal Jaworowski# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fcfdd827SRafal Jaworowski# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fcfdd827SRafal Jaworowski# SUCH DAMAGE.
27fcfdd827SRafal Jaworowski#
28fcfdd827SRafal Jaworowski#
292dd51e16SEd Maste# Embed an MFS image into the kernel body or the loader body (expects space
302dd51e16SEd Maste# reserved via MD_ROOT_SIZE (kernel) or MD_IMAGE_SIZE (loader))
31fcfdd827SRafal Jaworowski#
322dd51e16SEd Maste# $1: kernel or loader filename
33fcfdd827SRafal Jaworowski# $2: MFS image filename
34fcfdd827SRafal Jaworowski#
35fcfdd827SRafal Jaworowski
360bf94a08SEd Masteif [ $# -ne 2 ]; then
370bf94a08SEd Maste	echo "usage: $(basename $0) target mfs_image"
380bf94a08SEd Maste	exit 0
390bf94a08SEd Mastefi
400bf94a08SEd Masteif [ ! -w "$1" ]; then
410bf94a08SEd Maste	echo $1 not writable
420bf94a08SEd Maste	exit 1
430bf94a08SEd Mastefi
440bf94a08SEd Maste
45f4c1f0b9SAdrian Chaddmfs_size=`stat -f '%z' $2 2> /dev/null`
46f4c1f0b9SAdrian Chadd# If we can't determine MFS image size - bail.
4797a7bf30SEd Masteif [ -z ${mfs_size} ]; then
4897a7bf30SEd Maste	echo "Can't determine MFS image size"
4997a7bf30SEd Maste	exit 1
5097a7bf30SEd Mastefi
51fcfdd827SRafal Jaworowski
522dd51e16SEd Masteerr_no_mfs="Can't locate mfs section within "
532dd51e16SEd Maste
5447b5c719SEd Masteif file -b $1 | grep -q '^ELF ..-bit .SB executable'; then
552dd51e16SEd Maste
560a502389SLi-Wen Hsu	sec_info=`elfdump -c $1 2> /dev/null | grep -A 5 -E "sh_name: oldmfs$"`
57f4c1f0b9SAdrian Chadd	# If we can't find the mfs section within the given kernel - bail.
5897a7bf30SEd Maste	if [ -z "${sec_info}" ]; then
5997a7bf30SEd Maste		echo "${err_no_mfs} $1"
6097a7bf30SEd Maste		exit 1
6197a7bf30SEd Maste	fi
62f4c1f0b9SAdrian Chadd
630a502389SLi-Wen Hsu	sec_size=`echo "${sec_info}" | awk '/sh_size/ {print $2}' 2>/dev/null`
642dd51e16SEd Maste	sec_start=`echo "${sec_info}" | \
652dd51e16SEd Maste	    awk '/sh_offset/ {print $2}' 2>/dev/null`
662dd51e16SEd Maste
672dd51e16SEd Masteelse
682dd51e16SEd Maste
692dd51e16SEd Maste	#try to find start byte of MFS start flag otherwise - bail.
702dd51e16SEd Maste	sec_start=`strings -at d $1 | grep "MFS Filesystem goes here"` || \
712dd51e16SEd Maste	    { echo "${err_no_mfs} $1"; exit 1; }
722dd51e16SEd Maste	sec_start=`echo ${sec_start} | awk '{print $1}'`
732dd51e16SEd Maste
742dd51e16SEd Maste	#try to find start byte of MFS end flag otherwise - bail.
752dd51e16SEd Maste	sec_end=`strings -at d $1 | \
762dd51e16SEd Maste	    grep "MFS Filesystem had better STOP here"` || \
772dd51e16SEd Maste	    { echo "${err_no_mfs} $1"; exit 1; }
782dd51e16SEd Maste	sec_end=`echo ${sec_end} | awk '{print $1}'`
792dd51e16SEd Maste
802dd51e16SEd Maste	#calculate MFS section size
812dd51e16SEd Maste	sec_size=`expr ${sec_end} - ${sec_start}`
822dd51e16SEd Maste
832dd51e16SEd Mastefi
84f4c1f0b9SAdrian Chadd
85f4c1f0b9SAdrian Chadd# If the mfs section size is smaller than the mfs image - bail.
8697a7bf30SEd Masteif [ ${sec_size} -lt ${mfs_size} ]; then
8797a7bf30SEd Maste	echo "MFS image too large"
8897a7bf30SEd Maste	exit 1
8997a7bf30SEd Mastefi
90f4c1f0b9SAdrian Chadd
91f4c1f0b9SAdrian Chadd# Dump the mfs image into the mfs section
92f4c1f0b9SAdrian Chadddd if=$2 ibs=8192 of=$1 obs=${sec_start} oseek=1 conv=notrunc 2> /dev/null && \
932dd51e16SEd Maste    echo "MFS image embedded into $1" && exit 0
94