[rpi sdcard script] merging from crowds, and set default binary version to 'latest' 92/155292/1
authorJeonghoon Park <jh1979.park@samsung.com>
Fri, 13 Oct 2017 00:02:38 +0000 (09:02 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Fri, 13 Oct 2017 00:02:38 +0000 (09:02 +0900)
Change-Id: Idf48ca8eec23bcac44a0020707a7509ae15b4f04

tool/rpi_sdcard_setup.sh

index f66b463..d6effb7 100755 (executable)
 #! /bin/bash
+
 download_dir=download
 script_fusing=sd_fusing_rpi3.sh
 tizen_download_url=http://download.tizen.org/snapshots/tizen/unified/
 binary_prefix=tizen-unified
-binary_version=20170911.1
+default_binary_version=latest
 boot_img_name=iot-boot-arm64-rpi3
 platform_img_name=iot-headless-2parts-armv7l-rpi3
-usb_node=/dev/sdb
-tmp_rfs_dir=$download_dir/rootfs
 
-download_binaries() {
-if [ ! -d $download_dir ]; then 
-       mkdir -p $download_dir
-else
-       echo "download directory aleady exist"
-fi
+print_wget_exit_status() {
+       printf "wget returns error - "
+       case "$1" in
+       "1")
+               echo "Generic error code"
+               ;;
+       "2")
+               echo "Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’..."
+               ;;
+       "3")
+               echo "File I/O error"
+               ;;
+       "4")
+               echo "Network failure"
+               ;;
+       "5")
+               echo "SSL verification failure"
+               ;;
+       "6")
+               echo "Username/password authentication failure"
+               ;;
+       "7")
+               echo "Protocol errors"
+               ;;
+       "8")
+               echo "Server issued an error response"
+               ;;
+       *)
+               echo "Unknow exit code - $1 "
+               ;;
+       esac
+}
 
-if [ ! -e $download_dir/$script_fusing ]; then
-       wget https://git.tizen.org/cgit/platform/kernel/u-boot/plain/scripts/tizen/sd_fusing_rpi3.sh?h=tizen --output-document=$download_dir/$script_fusing
-fi
+get_latest_version() {
+       binary_version=$(wget --spider -r -l1 -np -nd -nv -A "*.tar.gz" -e robots=off http://download.tizen.org/snapshots/tizen/unified/latest/images/standard/iot-boot-arm64-rpi3/ 2>&1 | grep tar.gz | awk -F '[/ ]' '{for (i=1;i<=NF;i++) print $i}' | grep tar.gz | awk -F '[_]' '{print $2}')
 
-if [ ! -e $download_dir/$script_fusing ]; then
-       echo "[ERROR] fail to download fusing script"
-       exit 1
-fi
+       if [ "$binary_version" == "" ]; then
+               echo "fail to get latest version"
+               binary_version=$default_binary_version
+               echo "use default version : $binary_version"
+       fi
+}
 
-chmod 755 $download_dir/$script_fusing
+download_binary_version() {
+       local __version=$1
+       boot_img=${binary_prefix}_${__version}_${boot_img_name}.tar.gz
+       platform_img=${binary_prefix}_${__version}_${platform_img_name}.tar.gz
 
-if [ ! -e $download_dir/brcmfmac43430-sdio.bin ]; then
-       wget https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm80211/brcm/brcmfmac43430-sdio.bin -P $download_dir
-fi
+       if [ -e $download_dir/$boot_img ]; then
+               echo "Already downloaded [${boot_img}]"
+       else
+               wget ${tizen_download_url}${binary_prefix}_${__version}/images/standard/${boot_img_name}/$boot_img -P $download_dir
+       fi
+    ret=$?
+    if ! [ "$ret" == 0 ]; then
+        print_wget_exit_status $ret
+               echo "[ERROR] fail to download [${boot_img}]"
+        exit 1
+    fi
 
-if [ ! -e $download_dir/brcmfmac43430-sdio.bin ]; then
-       echo "[ERROR] fail to download [brcmfmac43430-sdio.bin]"
-       exit 1
-fi
+       if [ -e $download_dir/$platform_img ]; then
+               echo "Already downloaded [${platform_img}]"
+       else
+               wget ${tizen_download_url}${binary_prefix}_${__version}/images/standard/${platform_img_name}/$platform_img -P $download_dir
+       fi
+    ret=$?
+    if ! [ "$ret" == 0 ]; then
+        print_wget_exit_status $ret
+               echo "[ERROR] fail to download [${platform_img}]"
+        exit 1
+    fi
+}
 
-if [ ! -e $download_dir/brcmfmac43430-sdio.txt ]; then
-       wget https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm80211/brcm/brcmfmac43430-sdio.txt -P $download_dir
-fi
-if [ ! -e $download_dir/brcmfmac43430-sdio.txt ]; then
-       echo "[ERROR] fail to download [brcmfmac43430-sdio.txt]"
-       exit 1
-fi
+download_fusing_script() {
+       if [ -e $download_dir/$script_fusing ]; then
+               echo "Already downloaded [$script_fusing]"
+       else
+               wget https://git.tizen.org/cgit/platform/kernel/linux-rpi3/plain/scripts/sd_fusing_rpi3.sh?h=submit/tizen/20170725.223437 --output-document=$download_dir/$script_fusing
+       fi
+       ret=$?
+       if ! [ "$ret" == 0 ]; then
+               print_wget_exit_status $ret
+               echo "[ERROR] fail to download [$script_fusing]"
+               rm $download_dir/$script_fusing
+               exit
+       fi
 
-if [ ! -e $download_dir/BCM43430A1.hcd ]; then
-       wget https://github.com/OpenELEC/misc-firmware/raw/master/firmware/brcm/BCM43430A1.hcd -P $download_dir
-fi
-if [ ! -e $download_dir/BCM43430A1.hcd ]; then
-       echo "[ERROR] fail to download [BCM43430A1.hcd]"
-       exit 1
-fi
+       chmod 755 $download_dir/$script_fusing
+}
 
-if [ ! -e $download_dir/$boot_img ]; then
-       wget ${tizen_download_url}${binary_prefix}_${binary_version}/images/standard/${boot_img_name}/$boot_img -P $download_dir
-fi
-if [ ! -e $download_dir/$boot_img ]; then
-       echo "[ERROR] fail to download [${boot_img}]"
-       exit 1
-fi
+download_firmware() {
+       if [ -e $download_dir/brcmfmac43430-sdio.bin ]; then
+               echo "Already downloaded [brcmfmac43430-sdio.bin]"
+       else
+               wget https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm80211/brcm/brcmfmac43430-sdio.bin -P $download_dir
+       fi
+    ret=$?
+    if ! [ "$ret" == 0 ]; then
+        print_wget_exit_status $ret
+               echo "[ERROR] fail to download [brcmfmac43430-sdio.bin]"
+        exit 1
+    fi
 
-if [ ! -e $download_dir/$platform_img ]; then
-       wget ${tizen_download_url}${binary_prefix}_${binary_version}/images/standard/${platform_img_name}/$platform_img -P $download_dir
-fi
-if [ ! -e $download_dir/$platform_img ]; then
-       echo "[ERROR] fail to download [${platform_img}]"
-       exit 1
-fi
+       if [ -e $download_dir/brcmfmac43430-sdio.txt ]; then
+               echo "Already downloaded [brcmfmac43430-sdio.txt]"
+       else
+               wget https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm80211/brcm/brcmfmac43430-sdio.txt -P $download_dir
+       fi
+    ret=$?
+    if ! [ "$ret" == 0 ]; then
+        print_wget_exit_status $ret
+               echo "[ERROR] fail to download [brcmfmac43430-sdio.txt]"
+        exit 1
+    fi
 
+       if [ -e $download_dir/BCM43430A1.hcd ]; then
+               echo "Already downloaded [BCM43430A1.hcd]"
+       else
+               wget https://github.com/OpenELEC/misc-firmware/raw/master/firmware/brcm/BCM43430A1.hcd -P $download_dir
+       fi
+    ret=$?
+    if ! [ "$ret" == 0 ]; then
+        print_wget_exit_status $ret
+               echo "[ERROR] fail to download [BCM43430A1.hcd]"
+        exit 1
+    fi
+}
+
+download_binaries() {
+       if [ ! -d $download_dir ]; then
+               mkdir -p $download_dir
+       fi
+
+       if command -v wget > /dev/null; then
+               echo "Detected wget..."
+       else
+               echo "Installing wget..."
+               sudo apt-get install -q -y wget
+       fi
+
+       download_firmware
+
+       if [ "${binary_version}" = "latest" ]; then
+               get_latest_version
+       fi
+
+       download_binary_version ${binary_version}
 }
 
 fusing_sdcard() {
-sudo /bin/bash $download_dir/$script_fusing -d $usb_node --format
-sudo /bin/bash $download_dir/$script_fusing -d $usb_node -b $download_dir/$boot_img
-sudo /bin/bash $download_dir/$script_fusing -d $usb_node -b $download_dir/$platform_img
+       download_fusing_script
+
+       if command -v pv > /dev/null; then
+               echo "Detected pv..."
+       else
+               echo "Installing pv..."
+               sudo apt-get install -q -y pv
+       fi
+
+       sudo $download_dir/$script_fusing -d $usb_node --format
+       sudo $download_dir/$script_fusing -d $usb_node -b $download_dir/$boot_img
+       sudo $download_dir/$script_fusing -d $usb_node -b $download_dir/$platform_img
 }
 
 copy_firmware() {
-mkdir -p $tmp_rfs_dir
-sudo mount ${usb_node}2 $tmp_rfs_dir
-sudo mkdir -p $tmp_rfs_dir/usr/etc/bluetooth
-sudo cp $download_dir/BCM43430A1.hcd $tmp_rfs_dir/usr/etc/bluetooth
-sudo mkdir -p $tmp_rfs_dir/usr/lib/firmware/brcm
-sudo cp $download_dir/brcmfmac43430-sdio.* $tmp_rfs_dir/usr/lib/firmware/brcm
-sync
-sudo umount $tmp_rfs_dir
-rmdir $tmp_rfs_dir
-}
+       local __tmp_rfs_dir=$download_dir/rootfs
 
-display_help() {
-    echo "Usage: $0 [dev node of usb] [binary version]"
-    echo "example : $0 /dev/sdb 20170824.1" 
-    exit 1
-}
+       if [ ! -e $download_dir/brcmfmac43430-sdio.bin ]; then
+               echo "Failed to copy firmware - [brcmfmac43430-sdio.bin] is not exist"
 
+               exit 1
+       fi
+       if [ ! -e $download_dir/brcmfmac43430-sdio.txt ]; then
+               echo "Failed to copy firmware - [brcmfmac43430-sdio.txt] is not exist"
+               exit 1
+       fi
+       if [ ! -e $download_dir/BCM43430A1.hcd ]; then
+               echo "Failed to copy firmware - [BCM43430A1.hcd] is not exist"
+               exit 1
+       fi
 
-################################
-#set -x
+       mkdir -p $__tmp_rfs_dir
+       sudo mount ${usb_node}2 $__tmp_rfs_dir
+       sudo mkdir -p $__tmp_rfs_dir/usr/etc/bluetooth
+       sudo cp $download_dir/BCM43430A1.hcd $__tmp_rfs_dir/usr/etc/bluetooth
+       sudo mkdir -p $__tmp_rfs_dir/usr/lib/firmware/brcm
+       sudo cp $download_dir/brcmfmac43430-sdio.* $__tmp_rfs_dir/usr/lib/firmware/brcm
+       sync
+       sudo umount $__tmp_rfs_dir
+       rmdir $__tmp_rfs_dir
+}
+
+receive_device_nodes() {
+       local _node_result=$1
+       local node_list=`lsblk -o NAME,RM,TYPE,MODEL | grep 'disk' | grep -w '1' --color=never`
+       local node_num=`echo "${node_list}" | wc -l`
+       local node_default=`echo ${node_list} | awk '{print $1}'`
 
-#if [ $# -lt 2 ] ; then
-#      display_help
-#fi
-
-echo "########### Your Storage Device nodes ############"
-echo
-lsblk
-echo
-echo "##################################################"
-echo
-echo "Please enter device node of usb [press enter key to use default($usb_node)] : "
-read input_node
-if [ "$input_node" = "" ]; then
-       echo "###########  use default dev node : ${usb_node}"
-else
-       if [ "$input_node" = "/dev/sda" ]; then
-               echo "[ERROR] /dev/sda is your main storage"
+       if [ "$node_list" = "" ]; then
+               echo "You have no removable storage now, check it and try again"
+               echo "Bye Bye ~"
                exit 1
        fi
-       usb_node=${input_node}
-       echo "###########  use dev node : $usb_node"
-fi
 
-echo "Please enter binary version of usb [press enter key to use default($binary_version)] : "
-read input_version
-if [ "$input_version" = "" ]; then
-       echo "###########  use default version : $binary_version"
-else
-       binary_version=$input_version
-       echo "###########  use binary version : $binary_version"
-fi
+       echo
+       echo "######################################################"
+       echo "You have ${node_num} Removable Storage Device(s) as follows"
+       echo "${node_list}"
+       echo "######################################################"
+       echo
+       echo " ** Warning **  Be Careful to select device node, All existing data in that storage will be lost."
+       echo
+       echo "Please type device node of usb [press enter key to use default(${node_default})] : "
+       read input_node
+       if [ "$input_node" = "" ]; then
+               eval $_node_result=/dev/${node_default}
+       else
+               if [ "$input_node" = "sda" ]; then
+                       echo "[Warning] 'sda' is normally main storage"
+                       echo "          If you REALLY want use 'sda' type it again or type other one"
+                       read input_node
+               fi
+               eval $_node_result=/dev/${input_node}
+       fi
+}
+
+receive_binary_version() {
+       echo
+       echo "Please type binary version of tizen [press enter key to use default($default_binary_version)] : "
+       read input_version
+       if [ "$input_version" = "" ]; then
+               binary_version=$default_binary_version
+       else
+               binary_version=$input_version
+       fi
+}
 
-boot_img=${binary_prefix}_${binary_version}_${boot_img_name}.tar.gz
-platform_img=${binary_prefix}_${binary_version}_${platform_img_name}.tar.gz
+main() {
+       receive_device_nodes usb_node
+       echo "###########  use dev node : $usb_node"
+       if [ ! -b ${usb_node} ]; then
+               echo "[ERROR] Selected storage[$usb_node] is not exist"
+               exit 1
+       fi
 
-download_binaries
+       receive_binary_version
+       echo "###########  use binary version : $binary_version"
 
-sleep 1
+       download_binaries
+       sleep 1
+       fusing_sdcard
+       copy_firmware
+}
 
-fusing_sdcard
-copy_firmware
+############ Start Here
+#set -x
+if [ "${1}" != "--source-only" ]; then
+    main "${@}"
+fi