#!/bin/bash
#
# Copyright (c) 2001 Red Hat, Inc. All rights reserved.
#
# This software may be freely redistributed under the terms of the GNU
# public license.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Karsten Hopp <karsten@redhat.de>
#  based on the perl-version by Oliver Paukstadt
#                              <oliver.paukstadt@millenux.com>
#
#
# Hack for replacing the .modinfo section
# in the oco modules by a new one with
# a different kernel_version string.
# This is needed for loading the module
# into different kernels without having
# to use insmod's --force option.
#

infile=$2
outfile=$3
kernel=`echo $1 | awk -F '-' '{ print $1 }'`

objdump --section=.modinfo --full-contents $infile | awk -v kver=$1 -v kernel=$kernel '
BEGIN {
   getline
   while (!/Contents of section \.modinfo:/) { getline }
   getline line1
   getline line2
   getline line3
   line1 = substr(line1,7,36)
   line2 = substr(line2,7,36)
   line3 = substr(line3,7,36)
   longline = line1 line2 line3
   decode(longline)
   if(index(decline,kver) == 0) {   # only change version if needed
      gsub(kernel,kver,decline)
   }
   while(getline) {
      decode(substr($0,7,36))
   }
}

function hextodec(character) {
   if(character=="0") { return  0; }
   if(character=="1") { return  1; }
   if(character=="2") { return  2; }
   if(character=="3") { return  3; }
   if(character=="4") { return  4; }
   if(character=="5") { return  5; }
   if(character=="6") { return  6; }
   if(character=="7") { return  7; }
   if(character=="8") { return  8; }
   if(character=="9") { return  9; }
   if(character=="a") { return 10; }
   if(character=="b") { return 11; }
   if(character=="c") { return 12; }
   if(character=="d") { return 13; }
   if(character=="e") { return 14; }
   if(character=="f") { return 15; }
}

function decode(line) {
   i=1
   gsub(" ","",line)
   while(i<length(line)) {
      h = hextodec(substr(line,i,1))*16 + hextodec(substr(line,i+1,1))
      decline=decline sprintf("%c", h)
      i = i+2
   }
}

END { print decline; }
' > /tmp/modinfo$$
objcopy -R .modinfo --add-section=.modinfo=/tmp/modinfo$$ $infile $outfile
rm -f /tmp/modinfo$$ 2>/dev/null

