<http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=transmogrification>Ok, so I've been slaving away over my (not so) little script, and it's about time for another release.

Changes

- Utility now has the ability to flash the splash image as per request

- Utility can now also backup your device to flashable rootfs / kernel images (david might like this for building FDOM images)

- Configurification is loaded on startup and saved in /etc/frutil (you can only modify settings if you run it as root)

- more idiot proofing - uses 'which' to find dfu-util, checks to ensure it's being run as root, checks that zenity is installed on the host, checks that you have mkfs.jffs2 installed before backing up, checks that you have pv installed and acts accordingly (by either using it or not during the backup [this is not properly tested because I don't have pv, please report]), and possibly other stuff I've forgotten about.

- new name to reflect the new abilities

Notes:

- I mentioned using dialog instead of zenity for a faster and more consistent text-only interface. I investigated this, but dialog's file selection dialog is so awful that I reconsidered and stuck with zenity. if anybody knows of a decent text-based alternative like dialog but with a better file selection dialog, I'd be interested to hear about it

- I recommend setting up an ssh authorized key before doing the backup, otherwise you'll be prompted multiple times to enter the device's root password. To do this, run 'ssh-keygen' on your host machine and then copy the contents of ~/.ssh/id_rsa.pub on your host into /home/root/authorized keys on your device. This also means you don't have to enter your password to ssh anymore.

- I'm taking suggestions for names. I've gone with 'frutil', but 'neotool' also appeals to me. I was calling it 'uberscript' there for a while, but I'm not a big fan of that...

- I'll get around to making a wiki page for this and linking to it from the flash and backup pages, but I won't complain if somebody beats me to it. A copy is available for download at http://users.on.net/~antisol/frutil

Thoughts for future versions / when I have time:

- ability to specify the names of the backup images (currently hardcoded to ./FreeRunner_rootfs_Backup_`date +%Y%m%d%H%M`.jffs2 and ./FreeRunner_Kernel_Backup_`date +%Y%m%d%H%M`.bin)

- ability to bypass the zenity menus via command-line parameters, i.e: frutil --flash kernel ./kernelfile.bin rootfs ./rootfs.jffs

- another method of bypassing menus - you create symbolic links called 'neoflash' and 'neobackup', and the utility checks $0 on start and goes straight to the applicable function, the same way busybox works

- maybe ability to auto-download more things from downloads.openmoko.org, like the latest kernel / image?

- maybe addressbook management? ability to import contacts into the device? to do this I'd have to find a way to determine which distro is installed and act accordingly.

- things people suggest!
*
*As always, suggestions, comments, reports of catastrophic failure, beer, job offers, sportscars, and millions of dollars are all appreciated. ;)

enjoy!

-Dale
#!/bin/bash

#########################################################
# FreeRunner UberSkript 1.0
# By AntiSol, antisol (at) internode (dot) on (dot) net
# GNU GPL licensed
# loosely based on a script by 'rorschach'
# Thanks to:
#       Charles Pax (suggestion to fetch dfu-util)
#       David Samblas (info on 'which' command)
#########################################################

settings_file='/etc/frutil'
tmp_error_log_path='/tmp/flash-error-log'

if [ -z "`which zenity 2>/dev/null`" ]; then
        echo "it looks like zenity isn't installed, and this tool requires it!"
        exit
fi      

#function detect_FR {
#       tmp=`ping -c 1 192.168.0.202`
#       if [ $? != 0 ]; then 
#               echo no freerunner found!
#       else 
#               echo Freerunner found at 192.168.0.1
#       fi
#}

function find_dfu {
        #use 'which'...
        dfutils_path=`which dfu-util 2>/dev/null`
        
        if [ -z $dfutils_path ]; then
                #look in current directory...
                if [ -x './dfu-util' ]; then
                        dfutils_path='./dfu-util'
                fi
        fi

}

function check_dfu {

        if [ ! -x "$dfutils_path" ]; then
                                
                find_dfu
        
                if [ -z "$dfutils_path" ]; then
                        #not found. 
                        zenity --title "dfu-util not found" --question --text 
"The dfu utility could not be found, or is not executable. Do you want to 
automagically download it from downloads.openmoko.org?"
                        
                        if [ $? = 0 ]; then
                                clear
                                echo 'Downloading dfu-util...'
                                rm ./dfu-util > /dev/null 2>1
                                wget 
http://downloads.openmoko.org/daily/dfu-util
                                chmod a+x ./dfu-util >/dev/null
                                dfutils_path='./dfu-util'
                        else
                                dfutils_path=$(zenity --file-selection 
--title="Locate the dfu-util executable:" --filename="$dfutils_path")
                                
                        fi
                        
                        if [ -z "$dfutils_path" ]; then
                                echo "Cancelled!"
                                exit
                        fi
                        #check selection...
                        check_dfu
                fi
        fi
}

function perform_flash {
        # $1 - what we're flashing (for display)
        # $2 - dfu-params
        # $3 - file to flash

        echo
        echo "*** Flashing $1 with $3 ..."
        echo

        $dfutils_path $2 $3 2> $tmp_error_log_path
        # add the following line to the previous line to get back the zenity 
progress dialog:
        # | zenity --progress --pulsate --percentage=0 --title "Flashing in 
progress.." --auto-close

        if [ "${PIPESTATUS[0]}" != "0" ];then
                zenity --error --text "Some error occured while flashing 
$1.\n\nError message:\n`cat $tmp_error_log_path`\n\nAny other pending flashes 
will be aborted."
                return 1
        else
                #sleep to allow dfu to reset...
                sleep 2
        fi
}

function flash {

        if [ "$user" != root ]; then
                zenity --error --text "You can only flash your device if you 
are root!"
                return
        fi
        
        check_dfu

        ans=$(zenity  --list --text "What do you wanna flash?" --title 
"dfu-util gui" --height=300 --checklist --column "Pick" --column "Option" TRUE 
Root-Filesystem TRUE Kernel FALSE Bootloader FALSE Splash)

        if [ -z "$ans" ]; then
                #handle cancel / no selection...
                #echo "Nothing to do!"
                return
        fi

        txt="The flashing will now start.\n\nNow is the time to connect your 
Neo is to the USB-Device and boot it into the Nor (for kernel and rootfs) or 
Nand (for u-boot) screen before proceeding.\nRemember that flashing a large 
image like a rootfs will take some time so don't abort it.\n\n"

        if [ -n "`echo $ans | grep Root-Filesystem`" ];then
                du_param_r='-a rootfs -R -D'
                rootfs=True
                img_file_r=$(zenity --file-selection --title="Select an 
image-file for the ROOTFS:")
                if [ "$img_file_r" = "" ]; then
                        #echo "Cancelled!"
                        return
                fi
                txt="$txt - Flashing rootfs with $img_file_r \n\n"
        fi

        if [ -n "`echo $ans | grep Kernel`" ];then
                du_param_k='-a kernel -R -D'
                kernel=True
                img_file_k=$(zenity --file-selection --title="Select an 
image-file for the KERNEL:")
                if [ "$img_file_k" = "" ]; then
                        #echo "Cancelled!"
                        return
                fi
                txt="$txt - Flashing kernel with $img_file_k \n\n"
        fi

        if [ -n "` echo $ans | grep Bootloader`" ];then
                du_param_u='-a u-boot -R -D'
                uboot=True
                img_file_u=$(zenity --file-selection --title="Select an 
image-file for UBOOT:")
                if [ "$img_file_u" = "" ]; then
                        #echo "Cancelled!"
                        return
                fi
                txt="$txt - Flashing uboot with $img_file_u \n\n"
        fi
        
        if [ -n "` echo $ans | grep Splash`" ];then
                du_param_s='-a splash -R -D'
                splash=True
                img_file_s=$(zenity --file-selection --title="Select an 
image-file for SPLASH:")
                if [ "$img_file_s" = "" ]; then
                        #echo "Cancelled!"
                        return
                fi
                txt="$txt - Flashing splash with $img_file_s \n\n"
        fi

        touch $tmp_error_log_path

        zenity --question --text "$txt"
        
        if [ $? != 0 ]; then 
                return
        fi

        if [ "$rootfs" = True ]; then
                perform_flash rootfs "$du_param_r" "$img_file_r"
                if [ $? != 0 ]; then 
                        return
                fi
        fi

        if [ "$kernel" = True ]; then
                perform_flash kernel "$du_param_k" "$img_file_k"
                if [ $? != 0 ]; then 
                        return
                fi
        fi

        if [ "$uboot" = True ]; then
                perform_flash uboot "$du_param_u" "$img_file_u"
                if [ $? != 0 ]; then 
                        return
                fi
        fi

        if [ "$splash" = True ]; then
                perform_flash splash "$du_param_s" "$img_file_s"
                if [ $? != 0 ]; then 
                        return
                fi
        fi

        rm $tmp_error_log_path

}

function show_menu {
        ans=`zenity --title "FreeRunner Management Utility" --text "Choose an 
option" --list --height=300 --width=400 --column "" --column "" 1 "Setup this 
tool" 2 "Flash your FreeRunner" 3 "Backup Your Freerunner" 4 "Quit"`
}

function setup_menu {
        sans=`zenity --title "FreeRunner Management Utility" --text "Choose an 
option" --list --height=300 --width=400  --column "" --column "" 1 "Locate 
dfu-util ($dfutils_path)" 2 "Set your device's IP ($FRIP)" 3 "Save These 
Settings" 4 "Back to Main Menu"`
}


function setup {

        setup_menu
        while [ -n "$sans" ]; do

        case "$sans"
        in
                1) 
                        check_dfu
                        zenity --question --text "The dfu-utility was located 
at $dfutils_path, is this correct?"
                        if [ $? != 0 ]; then
                                dfutils_path=$(zenity --file-selection 
--title="Locate the dfu-util executable:" --filename="$dfutils_path")
                                check_dfu
                        fi
                        ;;
                2) 
                        FRIP=$(zenity --entry --text "Enter the IP / hostname 
for your freerunner:" --entry-text "$FRIP")
                        ;;
                3) 
                #zenity --info --text "Save";;
                if [ "$user" != root ]; then
                        zenity --error --text "You are not root, cannot save 
settings!"
                else
                        echo "dfutils_path='$dfutils_path'" > $settings_file
                        echo "FRIP='$FRIP'" >> $settings_file           
                        zenity --info --text "Settings saved to $settings_file" 
--title "Done."
                fi;;

                 *) return;;
        esac

        setup_menu

done    

}

function backup {

        FSIMG="./FreeRunner_rootfs_Backup_`date +%Y%m%d%H%M`.jffs2"
        KIMAGE="./FreeRunner_Kernel_Backup_`date +%Y%m%d%H%M`.bin"

        if [ -z "`ssh [EMAIL PROTECTED] 'ls /home/root/.ssh/authorized_keys 
2>/dev/null'`" ]; then
                zenity --question --text "it is recommended that you set up an 
authorized key on your device to eliminate the need to enter a password.\n\nIf 
you do not do this, you will be prompted for the device's password repeatedly 
during this procedure.\n\nThis is done by running 'ssh-keygen' on your host, 
and then copying the contents of ~/.ssh/id_rsa.pub on your host into 
home/root/.ssh/authorized_keys on your device. See the man pages for 'ssh' and 
'ssh-keygen' for more info.\n\nDo you want to cancel the backup and do this 
now?\n(press cancel to proceed with the backup anyway)"
                #maybe even set it up automatically(?)
                if [ $? = 0 ]; then
                        return
                fi
        fi

        clear
        echo "Backing up Device at $FRIP"
        echo 'Checking device for mkfs-jffs...'
        if [ -z "`ssh [EMAIL PROTECTED] 'opkg status mkfs-jffs2'`" ]; then
                zenity --error --text 'prerequisite mkfs-jffs2 is not installed 
on the device! \n\n do "opkg install mkfs-jffs2" and try again.'
                return
        fi

        echo Remounting flash at /var/tmp/root...
        ssh [EMAIL PROTECTED] "mkdir /var/tmp/root; mount -t jffs2 
/dev/mtdblock6 /var/tmp/root"
        #ssh [EMAIL PROTECTED] "mkdir /var/tmp/root"
        #ssh [EMAIL PROTECTED] "mount -t jffs2 /dev/mtdblock6 /var/tmp/root"
        echo 'Performing rootfs backup... (this will take a while)'
        
        if [ -z "`which pv 2>/dev/null`" ]; then        
                #pv is not installed, so don't use it...
                ssh [EMAIL PROTECTED] "mkfs.jffs2 -d /var/tmp/root -e 128 --pad 
--no-cleanmarkers -x lzo" > $FSIMG
        else
                ssh [EMAIL PROTECTED] "mkfs.jffs2 -d /var/tmp/root -e 128 --pad 
--no-cleanmarkers -x lzo" | pv -W > $FSIMG
        fi
                
        echo Backing up Kernel image...
        scp [EMAIL PROTECTED]:/boot/uImage $KIMAGE
        echo Cleanup...
        ssh [EMAIL PROTECTED] "umount /var/tmp/root; rmdir /var/tmp/root"
        #ssh [EMAIL PROTECTED] "umount /var/tmp/root"
        #ssh [EMAIL PROTECTED] "rmdir /var/tmp/root"
        
        zenity --info --title "Done." --text "Done!\n\n- rootfs backed up to 
$FSIMG\n- Kernel backed up to $KIMAGE"

}

##############################

#check user...
user=`whoami`

if [ "$user" != root ]; then
        zenity --title "You are not root!" --question --text 'This utility 
should be run as root. You can proceed, but you will not be able to save your 
settings or flash your FreeRunner.\n\n Do you want to continue?'
        
        if [ $? != 0 ]; then
                echo "exiting."
                exit
        fi;
fi

#load settings file if it exists...
if [ -e $settings_file ]; then
        echo "loading settings from $settings_file ..."
        . $settings_file
fi

check_dfu

#main Menu...

show_menu

while [ -n "$ans" ]; do

        case "$ans"
        in
                1) setup;;
                2) flash;;
                3) backup;;

                *) exit;;
        esac

        show_menu

done



_______________________________________________
Openmoko community mailing list
community@lists.openmoko.org
http://lists.openmoko.org/mailman/listinfo/community

Reply via email to