#!/bin/sh1#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (C) 2008 The FreeBSD Project. All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#27#28# Embed an MFS image into the kernel body or the loader body (expects space29# reserved via MD_ROOT_SIZE (kernel) or MD_IMAGE_SIZE (loader))30#31# $1: kernel or loader filename32# $2: MFS image filename33#3435if [ $# -ne 2 ]; then36echo "usage: $(basename $0) target mfs_image"37exit 038fi39if [ ! -w "$1" ]; then40echo $1 not writable41exit 142fi4344mfs_size=`stat -f '%z' $2 2> /dev/null`45# If we can't determine MFS image size - bail.46if [ -z ${mfs_size} ]; then47echo "Can't determine MFS image size"48exit 149fi5051err_no_mfs="Can't locate mfs section within "5253if file -b $1 | grep -q '^ELF ..-bit .SB executable'; then5455sec_info=`elfdump -c $1 2> /dev/null | grep -A 5 -E "sh_name: oldmfs$"`56# If we can't find the mfs section within the given kernel - bail.57if [ -z "${sec_info}" ]; then58echo "${err_no_mfs} $1"59exit 160fi6162sec_size=`echo "${sec_info}" | awk '/sh_size/ {print $2}' 2>/dev/null`63sec_start=`echo "${sec_info}" | \64awk '/sh_offset/ {print $2}' 2>/dev/null`6566else6768#try to find start byte of MFS start flag otherwise - bail.69sec_start=`strings -at d $1 | grep "MFS Filesystem goes here"` || \70{ echo "${err_no_mfs} $1"; exit 1; }71sec_start=`echo ${sec_start} | awk '{print $1}'`7273#try to find start byte of MFS end flag otherwise - bail.74sec_end=`strings -at d $1 | \75grep "MFS Filesystem had better STOP here"` || \76{ echo "${err_no_mfs} $1"; exit 1; }77sec_end=`echo ${sec_end} | awk '{print $1}'`7879#calculate MFS section size80sec_size=`expr ${sec_end} - ${sec_start}`8182fi8384# If the mfs section size is smaller than the mfs image - bail.85if [ ${sec_size} -lt ${mfs_size} ]; then86echo "MFS image too large"87exit 188fi8990# Dump the mfs image into the mfs section91dd if=$2 ibs=8192 of=$1 obs=${sec_start} oseek=1 conv=notrunc 2> /dev/null && \92echo "MFS image embedded into $1" && exit 0939495