#!/bin/sh # # Tool for fetching the X-Files from CMT # # Copyright (C) 2009 Nokia Corporation. All Rights Reserved. # # Author: Andras Domokos # if [ "$1" = "-h" ]; then echo "usage: xfetch [-h] [-a] [idx]" echo " idx is X-File index, no value means read the last one" echo " -a means read all the files" exit 1 fi if [ -z $(which pnsend) ]; then echo -e "\npnsend not found, install isa-utils package!\n" exit 1 fi MAXDATA=1024 if [ $MAXDATA -lt 64 ]; then echo "Internal error: MAXDATA smaller than the usable minimum of 64" exit 1 fi if [ $MAXDATA -gt 1024 ]; then echo "Internal error: MAXDATA bigger than the usable maximum of 1024" exit 1 fi PMM_GID=$(printf "%04x" 239) PMM_GID=$(echo $PMM_GID | sed -e 's/\(..\)\(..\)/\1 \2/') QSIZE=$(printf "%02x" 0x0c) RSIZE=$(printf "%02x" 0x0d) QREAD=$(printf "%02x" 4) RREAD=$(printf "%02x" 5) FNAME=$(printf "%02x" 13) OK=00 MAXIDX=0 while true; do local x x=$((MAXIDX+1)) x=$(printf "%04x" $x) x=$(echo $x | sed -e 's/\(..\)\(..\)/\1 \2/') size=$(pnsend -r 0x23 00 $QSIZE $PMM_GID $x 00 00 | grep "rcv: 00 $RSIZE $OK" | sed -e 's/rcv: .\{30\}\(..\) \(..\)/0x\1\2/') if [ -z "$size" ]; then break; fi MAXIDX=$((MAXIDX+1)) XLIST="$XLIST $MAXIDX" done if [ $MAXIDX -eq 0 ]; then echo "X-Files not available" exit 1 fi if [ -z "$1" ]; then lpos=$(pnsend -r 0x23 00 $QREAD $PMM_GID 00 00 00 00 00 00 00 00 00 00 00 02 | grep "rcv: 00 $RREAD $OK" | sed -e 's/rcv: .\{48\}\(..\) \(..\)\(.*\)/0x\2\1/') if [ -z "$lpos" ]; then echo "Unexpected response received, exiting..." exit 1 fi lpos=$((lpos)) XLIST=$lpos elif [ "$1" != "-a" ]; then idx=$(($1)) if [ $idx -lt 1 -o $idx -gt $MAXIDX ]; then echo "Invalid index, valid values are 1..$MAXIDX" exit 1 fi XLIST=$idx fi for idx in $XLIST; do echo "Fetching X-File $idx ..." IDX=$(printf "%04x" $idx) IDX=$(echo $IDX | sed -e 's/\(..\)\(..\)/\1 \2/') #echo "IDX = $IDX" size=$(pnsend -r 0x23 00 $QSIZE $PMM_GID $IDX 00 00 | grep "rcv: 00 $RSIZE $OK" | sed -e 's/rcv: .\{30\}\(..\) \(..\)/0x\1\2/') if [ -z "$size" ]; then echo "Unexpected response received, exiting..." exit 1 fi size=$((size)) #echo "size = $size" offs=0 i=0 echo -n -e "\rFetched $offs bytes" while [ $size -gt 0 ]; do chunk=$size if [ $size -gt $MAXDATA ]; then chunk=$MAXDATA fi OFFSET=$(printf "%08x" $offs | sed -e 's/^\(..\)\(..\)\(..\)\(..\)/\1 \2 \3 \4/') AMOUNT=$(printf "%08x" $chunk | sed -e 's/^\(..\)\(..\)\(..\)\(..\)/\1 \2 \3 \4/') data=$(pnsend -r 0x23 00 $QREAD $PMM_GID $IDX 00 00 $OFFSET $AMOUNT | grep "rcv: 00 $RREAD $OK" | sed -e 's/rcv: .\{48\}//') if [ -z "$data" ]; then echo -e "\nUnexpected response received, exiting..." exit 1 fi dlen=$((3*chunk-1)) data=$(echo $data | sed -e "s/^\(.\{$dlen\}\)\(.*\)/\1/") i=$((i+1)) if [ $i -eq 1 ]; then id=$(echo $data | sed -e 's/^\(..\)\(.*\)/\1/') if [ "$id" != "$FNAME" ]; then echo -e "\nX-File name is not present" exit 1 fi nlen=$(echo $data | sed -e 's/^\(..\) \(..\) \(..\) \(.*\)/0x\2\3/') nlen=$((3*nlen-1)) file=$(echo $data | sed -e "s/^\(.\{9\}\)\(.\{$nlen\}\)\(.*\)/\2/") file=$(eval $(echo $file | sed -e 's/ /\\x/g;s/^/echo -n -e \"\\x/;s/$/\"/')) if echo $file | grep -q ath; then file=${file}_0.XF fi > $file data=$(echo $data | sed -e "s/^\(.\{9\}\)\(.\{$nlen\}\)\(.*\)/\3/") fi if [ -n "$data" ]; then eval $(echo $data | sed -e 's/ /\\x/g;s/^/echo -n -e \"\\x/;s/$/\"/') >> $file fi size=$((size-chunk)) offs=$((offs+chunk)) echo -n -e "\rFetched $offs bytes" done echo -e "\nX-File created: $file ($((offs-nlen/3-3)) bytes)" done