xref: /freebsd/sys/tools/embed_mfs.sh (revision 0a5023893550da8537932e02225f239a3ba3652b)
1fcfdd827SRafal Jaworowski#!/bin/sh
2fcfdd827SRafal Jaworowski#
3fcfdd827SRafal Jaworowski# Copyright (C) 2008 The FreeBSD Project. All rights reserved.
4fcfdd827SRafal Jaworowski#
5fcfdd827SRafal Jaworowski# Redistribution and use in source and binary forms, with or without
6fcfdd827SRafal Jaworowski# modification, are permitted provided that the following conditions
7fcfdd827SRafal Jaworowski# are met:
8fcfdd827SRafal Jaworowski# 1. Redistributions of source code must retain the above copyright
9fcfdd827SRafal Jaworowski#   notice, this list of conditions and the following disclaimer.
10fcfdd827SRafal Jaworowski# 2. Redistributions in binary form must reproduce the above copyright
11fcfdd827SRafal Jaworowski#   notice, this list of conditions and the following disclaimer in the
12fcfdd827SRafal Jaworowski#   documentation and/or other materials provided with the distribution.
13fcfdd827SRafal Jaworowski#
14fcfdd827SRafal Jaworowski# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fcfdd827SRafal Jaworowski# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fcfdd827SRafal Jaworowski# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fcfdd827SRafal Jaworowski# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18fcfdd827SRafal Jaworowski# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fcfdd827SRafal Jaworowski# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fcfdd827SRafal Jaworowski# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fcfdd827SRafal Jaworowski# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fcfdd827SRafal Jaworowski# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fcfdd827SRafal Jaworowski# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fcfdd827SRafal Jaworowski# SUCH DAMAGE.
25fcfdd827SRafal Jaworowski#
26fcfdd827SRafal Jaworowski# $FreeBSD$
27fcfdd827SRafal Jaworowski#
28fcfdd827SRafal Jaworowski# Embed the MFS image into the kernel body (expects space reserved via
29fcfdd827SRafal Jaworowski# MD_ROOT_SIZE)
30fcfdd827SRafal Jaworowski#
31fcfdd827SRafal Jaworowski# $1: kernel filename
32fcfdd827SRafal Jaworowski# $2: MFS image filename
33fcfdd827SRafal Jaworowski#
34fcfdd827SRafal Jaworowski
35f4c1f0b9SAdrian Chaddmfs_size=`stat -f '%z' $2 2> /dev/null`
36f4c1f0b9SAdrian Chadd# If we can't determine MFS image size - bail.
37f4c1f0b9SAdrian Chadd[ -z ${mfs_size} ] && echo "Can't determine MFS image size" && exit 1
38fcfdd827SRafal Jaworowski
39*0a502389SLi-Wen Hsusec_info=`elfdump -c $1 2> /dev/null | grep -A 5 -E "sh_name: oldmfs$"`
40f4c1f0b9SAdrian Chadd# If we can't find the mfs section within the given kernel - bail.
41f4c1f0b9SAdrian Chadd[ -z "${sec_info}" ] && echo "Can't locate mfs section within kernel" && exit 1
42f4c1f0b9SAdrian Chadd
43*0a502389SLi-Wen Hsusec_size=`echo "${sec_info}" | awk '/sh_size/ {print $2}' 2> /dev/null`
44*0a502389SLi-Wen Hsusec_start=`echo "${sec_info}" | awk '/sh_offset/ {print $2}' 2> /dev/null`
45f4c1f0b9SAdrian Chadd
46f4c1f0b9SAdrian Chadd# If the mfs section size is smaller than the mfs image - bail.
47f4c1f0b9SAdrian Chadd[ ${sec_size} -lt ${mfs_size} ] && echo "MFS image too large" && exit 1
48f4c1f0b9SAdrian Chadd
49f4c1f0b9SAdrian Chadd# Dump the mfs image into the mfs section
50f4c1f0b9SAdrian Chadddd if=$2 ibs=8192 of=$1 obs=${sec_start} oseek=1 conv=notrunc 2> /dev/null && \
51f4c1f0b9SAdrian Chadd    echo "MFS image embedded into kernel" && exit 0
52