Subject: Re: [PATCH] Provide mechanism for editing builtin command-line in zImage binary.

From: Michal Ostrowski <[email-masked]>
Date: Mon, 29 May 2006 22:27:02 -0400

For reference, below is a shell script I use to edit the command-line
embedded in a zImage.

-- 
Michal Ostrowski <[email-masked]>

#!/bin/bash
#
# Copyright (C) 2006 Michal Ostrowski <[email-masked]>, IBM Corp.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

usage(){
    echo 'set_builtin_cmdline [--cmd <file>] <input_obj> [<output_obj>]'
    echo '    Sets the builtin command line embedded in an object file.'
    echo
    echo '    If <output_obj> is not specified, <input_obj> is modified'
    echo '    in place.'
    echo '    If --cmd <file> argument is present contents of embedded'
    echo '    command line will be copied from <file>, otherwise from stdin.'
    exit 1
}

cmd_file=

while case "$#" in 0) break ;; esac
do
  
        case "$1" in
        --cmd)
		case "$#" in
		    1)
			usage ;;
		    *)
			cmd_file="$2";
			shift;;
		esac;;
        *)
                set x "$@"
                shift
                break ;;
        esac
        shift
done

if [ "$#" -lt 1 ]; then
    echo "No input object specified." ; 
    usage;
fi

if [ "$#" -gt 2 ]; then
    echo "Unrecognized arguments: $@";
    usage;
fi

infile=$1
shift;

if [ ! -r "$infile" ] ; then 
    echo "Can't read '$infile'."; 
    usage;
fi

if [ "$#" -eq 0 ] ; then
    outfile=$infile;
else
    outfile=$1;
    if ! cp $infile $outfile ;  then
	echo "Can't create output: '$outfile' $?" 
	usage;
    fi
    shift;
fi

offset=$(objdump -h  $infile | \
         gawk -- '{if($2=="__builtin_cmdline") {print strtonum("0x" $6);}}')
size=$(objdump -h  $infile | \
       gawk -- '{if($2=="__builtin_cmdline") {print strtonum("0x" $3);}}')

if [ "$cmd_file" ] ; then
    [ -r "$cmd_file" ] || (echo "Can't read '$cmd_file'." ; usage);
    cmdline=$(cat $cmd_file);
else
    cmdline=$(cat);
fi

if [ "x$offset" != "x" ] ; then 
    if [ "$offset" -ne 0  ] ; then
	set -e
	# Zero the destination buffer first
	exec 2>/dev/null
	dd if=/dev/zero of=$outfile bs=1 seek=$offset conv=notrunc count=$size
	echo -n "$cmdline" | \
	    dd of=$outfile bs=1 seek=$offset conv=notrunc count=$size
	exit $?
    fi
fi