Friday, September 19, 2008

The Diff Ninjutsu (Part 2)

In the previous installment of this series, I haven't show how we can use diff to do the automatic checking of the applied patch. Now. let's look at a sample shell script that do just that.

#!/bin/bash
#
# Script to apply patches to Realtek v1.4 ADK, SDK and CMK in order to
# upgrade it to version 1.4c (with latest v1.4c patches)
#
# Author: Darmawan Salihun
#
# Date: 26-08-2008
#

##### ASSUMPTIONS a.k.a PRECONDITION ####################################
#
# The following directories must have been decompressed prior to the
# execution of this script!
#
# CMK_1_4b_ROOT_DIR=rtl8186-cmk-1.4b
# CMK_1_4c_ROOT_DIR=rtl8186-cmk-1.4c-release
# ADK_ROOT_DIR=rtl8186-adk-1.4
# SDK_ROOT_DIR=rtl8186-sdk-1.4
# CMK_1_4c_PATCH_DIR=patch/files
#
#########################################################################


# directories definition
CMK_1_4b_ROOT_DIR=rtl8186-cmk-1.4b
CMK_1_4b_ADK_PATCH_DIR=$CMK_1_4b_ROOT_DIR/patch/ADK/AP
CMK_1_4b_SDK_PATCH_DIR=$CMK_1_4b_ROOT_DIR/patch/SDK/linux-2.4.18

CMK_1_4c_ROOT_DIR=rtl8186-cmk-1.4c-release
CMK_1_4c_ADK_PATCH_DIR=$CMK_1_4c_ROOT_DIR/patch/ADK/AP
CMK_1_4c_SDK_PATCH_DIR=$CMK_1_4c_ROOT_DIR/patch/SDK/linux-2.4.18
CMK_1_4c_CONFIG_DIR=$CMK_1_4c_ROOT_DIR/config
CMK_1_4c_LINUX_TOOLS=$CMK_1_4c_ROOT_DIR/tools/LINUX/tools.tar.gz

ADK_ROOT_DIR=rtl8186-adk-1.4
ADK_ASP_DIR=$ADK_ROOT_DIR/rtl8186-ASP-1.4
SDK_ROOT_DIR=rtl8186-sdk-1.4
SDK_LINUX_DIR=$SDK_ROOT_DIR/rtl8186-linux-1.4
SDK_TOOLCHAIN=$SDK_ROOT_DIR/rtl8186-toolchain-1.0.tar.gz
SDK_BOOTCODE=$SDK_ROOT_DIR/rtl8186-btcode-1.4a.tar.gz

CMK_1_4c_PATCH_DIR=patch/files
ANTEK_RTL_SDK_DIR=antek-rtl8186-sdk

# Guard against error during execution
set -e

#
# step 1: prepare target directory (Antek RTL8186 SDK)
#
echo "Preparing target directory.."
rm -rf $ANTEK_RTL_SDK_DIR
mkdir -v $ANTEK_RTL_SDK_DIR


#
# step 2: prepare original ADK and SDK v1.4
#
echo "Preparing original ADK and SDK.."
rm -rf $ADK_ASP_DIR
tar xzf $ADK_ROOT_DIR/rtl8186-ASP-1.4.tar.gz -C $ADK_ROOT_DIR

rm -rf $SDK_LINUX_DIR
tar xzf $SDK_ROOT_DIR/rtl8186-linux-1.4.tar.gz -C $SDK_ROOT_DIR


#
# step 3: copy original ADK and SDK v1.4 to target directory
#
echo "Copying original ADK and SDK.."
cp -r --remove-destination $ADK_ASP_DIR/AP $ANTEK_RTL_SDK_DIR
cp -r --remove-destination $SDK_LINUX_DIR/linux-2.4.18 $ANTEK_RTL_SDK_DIR


#
# step 4: upgrade v1.4 to v1.4b in target directory
#
echo "Upgrading ADK and SDK to v1.4b.."
cp -r --remove-destination $CMK_1_4b_ADK_PATCH_DIR/* $ANTEK_RTL_SDK_DIR/AP
cp -r --remove-destination $CMK_1_4b_SDK_PATCH_DIR/* $ANTEK_RTL_SDK_DIR/linux-2.4.18


#
# step 5: check the validity of the applied ADK and SDK v1.4b patch
#

#
# Check ADK patch validity
#
echo "Checking ADK v1.4b patch validity.."
find $CMK_1_4b_ADK_PATCH_DIR -type f -name '*' > files.txt

# Eliminate CMK_1_4b_ADK_PATCH_DIR/ from filenames path
cat /dev/null > new_files.txt
while read FILENAME
do
echo $FILENAME | sed -e "s%$CMK_1_4b_ADK_PATCH_DIR/%%g" >> new_files.txt # Note: sed uses % as delimiter
done < files.txt

rm -f files.txt

# Merge list of files into single line,
# i.e. as the value of TARGETS variable
while read FILENAME
do
TARGETS="$TARGETS $FILENAME"
done < new_files.txt

rm -f new_files.txt


for i in $TARGETS;
do
SRC=$CMK_1_4b_ADK_PATCH_DIR/$i
DEST=$ANTEK_RTL_SDK_DIR/AP/$i
diff $SRC $DEST > result.txt

if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done


#
# Reset reused variables!!!
#
TARGETS=""
FILENAME=""
SRC=""
DEST=""

#
# Check SDK patch validity
#
echo "Checking SDK 1.4b patch validity.."
find $CMK_1_4b_SDK_PATCH_DIR -type f -name '*' > files.txt

# Eliminate CMK_1_4b_SDK_PATCH_DIR/ from filenames path
cat /dev/null > new_files.txt
while read FILENAME
do
echo $FILENAME | sed -e "s%$CMK_1_4b_SDK_PATCH_DIR/%%g" >> new_files.txt # Note: sed uses % as delimiter
done < files.txt

rm -f files.txt

# Merge list of files into single line,
# i.e. as the value of TARGETS variable
while read FILENAME
do
TARGETS="$TARGETS $FILENAME"
done < new_files.txt

rm -f new_files.txt

for i in $TARGETS;
do
SRC=$CMK_1_4b_SDK_PATCH_DIR/$i
DEST=$ANTEK_RTL_SDK_DIR/linux-2.4.18/$i
diff $SRC $DEST > result.txt

if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done

#
# Reset reused variables!!!
#
TARGETS=""
FILENAME=""
SRC=""
DEST=""


#
# step 6: patch _the ADK and SDK v1.4c patch files_ with latest patches
#
CMK_1_4c_PATCH_TARGETS="patch/SDK/linux-2.4.18/rtl8186/wireless_ag_net.o patch/ADK/linux-2.4.18/drivers/net/wireless_ag/ieee802_mib.h patch/SDK/linux-2.4.18/drivers/net/wireless_ag/ieee802_mib.h patch/SDK/l
inux-2.4.18/rtl8186.tar.gz patch/ADK/AP/mkimg"


for i in $CMK_1_4c_PATCH_TARGETS;
do
DEST=$CMK_1_4c_ROOT_DIR/$i
SRC=`basename $i`
SRC=$CMK_1_4c_PATCH_DIR/$SRC
cp -f $SRC $DEST
done

#
# step 7: check the patch for ADK and SDK v1.4c patch files
#
for i in $CMK_1_4c_PATCH_TARGETS;
do
DEST=$CMK_1_4c_ROOT_DIR/$i
SRC=`basename $i`
SRC=$CMK_1_4c_PATCH_DIR/$SRC

diff $SRC $DEST > result.txt
if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done


#
# Reset reused variables!!!
#
TARGETS=""
FILENAME=""
SRC=""
DEST=""


#
# step 8: upgrade ADK and SDK v1.4b to patched ADK and SDK v1.4c in target dir
#
echo "Upgrading ADK and SDK v1.4b to v1.4c.."
cp -r --remove-destination $CMK_1_4c_ADK_PATCH_DIR/* $ANTEK_RTL_SDK_DIR/AP
cp -r --remove-destination $CMK_1_4c_SDK_PATCH_DIR/* $ANTEK_RTL_SDK_DIR/linux-2.4.18

#
# check ADK v1.4c patch validity
#

echo "Checking ADK v1.4c patch validity.."
find $CMK_1_4c_ADK_PATCH_DIR -type f -name '*' > files.txt

# Eliminate CMK_1_4c_ADK_PATCH_DIR/ from filenames path
cat /dev/null > new_files.txt
while read FILENAME
do
echo $FILENAME | sed -e "s%$CMK_1_4c_ADK_PATCH_DIR/%%g" >> new_files.txt # Note: sed uses % as delimiter
done < files.txt

rm -f files.txt

# Merge list of files into single line,
# i.e. as the value of TARGETS variable
while read FILENAME
do
TARGETS="$TARGETS $FILENAME"
done < new_files.txt

rm -f new_files.txt


for i in $TARGETS;
do
SRC=$CMK_1_4c_ADK_PATCH_DIR/$i
DEST=$ANTEK_RTL_SDK_DIR/AP/$i
diff $SRC $DEST > result.txt

if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done


#
# Reset reused variables!!!
#
TARGETS=""
FILENAME=""
SRC=""
DEST=""

#
# Check SDK v1.4c patch validity
#
echo "Checking SDK v1.4c patch validity.."
find $CMK_1_4c_SDK_PATCH_DIR -type f -name '*' > files.txt


# Eliminate CMK_1_4c_SDK_PATCH_DIR/ from filenames path
cat /dev/null > new_files.txt
while read FILENAME
do
echo $FILENAME | sed -e "s%$CMK_1_4c_SDK_PATCH_DIR/%%g" >> new_files.txt # Note: sed uses % as delimiter
done < files.txt

rm -f files.txt

# Merge list of files into single line,
# i.e. as the value of TARGETS variable
while read FILENAME
do
TARGETS="$TARGETS $FILENAME"
done < new_files.txt

rm -f new_files.txt

for i in $TARGETS;
do
SRC=$CMK_1_4c_SDK_PATCH_DIR/$i
DEST=$ANTEK_RTL_SDK_DIR/linux-2.4.18/$i
diff $SRC $DEST > result.txt

if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done



mkdir -v $ANTEK_RTL_SDK_DIR/tool
cp -rf $CMK_1_4c_CONFIG_DIR $ANTEK_RTL_SDK_DIR/tool
tar xzf $CMK_1_4c_LINUX_TOOLS -C $ANTEK_RTL_SDK_DIR/tool/

cp -vf $SDK_TOOLCHAIN $ANTEK_RTL_SDK_DIR/tool
cp -vf $SDK_BOOTCODE $ANTEK_RTL_SDK_DIR/tool

# inform that we're done
echo "Realtek RTL8186 SDK v1.4c installed."


That's one hell of a script. Nonetheless, the most important part is the automatic patch-checking routine such as:

echo "Checking SDK v1.4c patch validity.."
find $CMK_1_4c_SDK_PATCH_DIR -type f -name '*' > files.txt

# Eliminate CMK_1_4c_SDK_PATCH_DIR/ from filenames path
cat /dev/null > new_files.txt
while read FILENAME
do
echo $FILENAME | sed -e "s%$CMK_1_4c_SDK_PATCH_DIR/%%g" >> new_files.txt # Note: sed uses % as delimiter
done < files.txt

rm -f files.txt

# Merge list of files into single line,
# i.e. as the value of TARGETS variable
while read FILENAME
do
TARGETS="$TARGETS $FILENAME"
done < new_files.txt

rm -f new_files.txt

for i in $TARGETS;
do
SRC=$CMK_1_4c_SDK_PATCH_DIR/$i
DEST=$ANTEK_RTL_SDK_DIR/linux-2.4.18/$i
diff $SRC $DEST > result.txt

if [ -s result.txt ]; then
echo "Error. $SRC and $DEST differ"
fi
rm -f result.txt
done

As you can see, the script first create list of files to be diff-ed and then iterate the diff-ing process on each of those file, creating a diff file (result.txt) in the process. If the size of result.txt is bigger than zero, it means there is at least one difference which means the patch is not correctly applied.

There you have it, an automatic patch-checking script. In the next installment of this series, I will tidy up the code and make some functions because the current version is a quick'n'dirty script.
Post a Comment

1 comment:

Anonymous said...

Hello.

I`m Evgeniy Manachkin. Developer Wive-NG freeware opensource router firmware http://wive-ng.sf.net .

Please send me lastest Realtek SDK or lastest binary kernel modules for wlan interface.

Send pleas to e-mail: sfstudio@mail.ru

P.S. How to get lastest SDK ? It`s free ?