Root/data/qi_lb60/scripts/reflash_ben.sh

1#!/bin/bash
2# version of me
3__VERSION__="2011-03-02"
4
5# use 'http' to download and flash images, use 'file' to flash images present in the <WORKING_DIR>
6PROTOCOL="http"
7
8# NanoNote images Version
9VERSION="latest"
10
11# working directory
12WORKING_DIR="${HOME}/.qi/nanonote/ben/${VERSION}"
13
14# URL to images ($URL/$VERSION/$[images])
15BASE_URL_HTTP="http://downloads.qi-hardware.com/software/images/NanoNote/Ben"
16
17# names of images
18LOADER="openwrt-xburst-qi_lb60-u-boot.bin"
19KERNEL="openwrt-xburst-qi_lb60-uImage.bin"
20ROOTFS="openwrt-xburst-qi_lb60-root.ubi"
21
22# options for reflash bootloader, kernel, rootfs
23B="FALSE"
24K="FALSE"
25R="FALSE"
26ALL="TRUE"
27
28while getopts d:l:hbkr OPTIONS
29do
30    case $OPTIONS in
31    d)
32        VERSION=$OPTARG # override version by first argument if passed
33        ;;
34    l)
35        WORKING_DIR=$OPTARG
36        PROTOCOL="file"
37        VERSION="Local"
38        ;;
39    b)
40    ALL="FALSE"
41        B="TRUE"
42        ;;
43    k)
44    ALL="FALSE"
45        K="TRUE"
46        ;;
47    r)
48    ALL="FALSE"
49        R="TRUE"
50        ;;
51    *)
52        echo "\
53
54Usage: $0 [-d <version>] [-l <path to local images>] [-h]
55     -d <> I will download and flash a specific version of OpenWrt images
56
57     -l <> I will flash images present in <arg>
58            (missing files will be skipped)
59
60     -h you already found out
61
62without any arguments, I will download and flash the latest OpenWrt images
63(includes bootloader, kernel and rootfs)
64
65OpenWrt reflash script for qi-hardware Ben NanoNote
66written by: Mirko Vogt (mirko.vogt@sharism.cc)
67            Xiangfu Liu (xiangfu@sharism.cc)
68
69                                                     version: ${__VERSION__}
70Please report bugs to developer@lists.qi-hardware.com"
71        exit 1
72        ;;
73    esac
74done
75
76if [ "$ALL" == "TRUE" ]; then
77    B="TRUE"
78    K="TRUE"
79    R="TRUE"
80fi
81
82# where the verbose output goes to
83LOG_FILE="${WORKING_DIR}/log.txt"
84
85# create working directory
86mkdir -p ${WORKING_DIR}
87# purge logfile if exists
88date > "${LOG_FILE}"
89
90log() {
91    echo -e "$1"
92    echo -e "$1" >> "${LOG_FILE}"
93}
94
95abort() {
96    log "==="
97    log "fatal error occured - ABORTED"
98    log "==="
99    log "$1"
100    log "==="
101    log "Before reporting this as a bug"
102    log "Please ensure you're using the latest available version of this reflash script"
103    log "http://downloads.qi-hardware.com/software/images/NanoNote/Ben/reflash_ben.sh"
104    exit 1
105}
106
107progress_prepare () {
108    WIDTH=$(tput cols)
109    HEIGHT=$(tput lines)
110    i=1
111    DONE=0
112    FITFR=0
113    echo "Done:"
114    echo -n "["
115    tput cup $HEIGHT $WIDTH; echo -n "]"
116}
117
118progress_draw () {
119    ILINE="$1"
120    if [[ "$ILINE" =~ It\ will\ cause\ [[:digit:]]+\ times\ buffer\ transfer\.$ ]]; then
121        TOTAL=${ILINE#*cause\ }
122        TOTAL=${TOTAL%\ times*}
123        FIT=$(echo "($WIDTH-2)/$TOTAL" | bc -l)
124        tput cup $(( $HEIGHT-2 )) 7; echo -n 0/$TOTAL
125    fi
126    if [[ "$ILINE" =~ Checking\ [[:digit:]]+\ bytes\.\.\.\ Comparing\ [[:digit:]]+\ bytes\ -\ SUCCESS$ || "$ILINE" =~ Checking\ [[:digit:]]+\ bytes\.\.\.\ no\ check\!\ End\ at\ Page\:\ [[:digit:]]+ ]]; then
127        FITFR=$(echo $FITFR+$FIT | bc -l)
128        ((DONE++))
129        tput cup $(( $HEIGHT-2 )) 7; echo -n $DONE/$TOTAL
130        if [ $(echo "$FITFR >= 1" | bc) -eq 1 ]; then
131            tput cup $HEIGHT $i;
132            i=$(( $i+${FITFR%.*} ))
133            for j in $(seq 1 ${FITFR%.*}); do echo -n "#"; done
134            FITFR=0.${FITFR#*.}
135        fi
136    fi
137}
138
139progress_finish () {
140    tput cup $HEIGHT $WIDTH
141    echo
142    tmp=$(<"${LOG_FILE}.err")
143    cat "${LOG_FILE}.err" >> "${LOG_FILE}"
144}
145
146[ "$(whoami)" == "root" ] || abort "this script must be run as root"
147
148log "working dir: ${WORKING_DIR}"
149log "chosen method: ${PROTOCOL}"
150test ${VERSION} && log "chosen version: ${VERSION}"
151log "==="
152
153if [ "$PROTOCOL" == "http" ]; then
154
155    MD5SUMS_SERVER=$(wget -O - ${BASE_URL_HTTP}/${VERSION}/md5sums 2> /dev/null | grep -E "(${LOADER}|${KERNEL}|${ROOTFS})" | sort)
156    [ "${MD5SUMS_SERVER}" ] || abort "can't fetch files from server"
157    
158    MD5SUMS_LOCAL=$( (cd "${WORKING_DIR}" ; md5sum --binary "${LOADER}" "${KERNEL}" "${ROOTFS}" 2> /dev/null) | sort )
159
160    if [ "${MD5SUMS_SERVER}" == "${MD5SUMS_LOCAL}" ]; then
161        log "present files are identical to the ones on the server - do not download them again"
162    else
163        rm -f "${WORKING_DIR}/${LOADER}" "${WORKING_DIR}/${KERNEL}" "${WORKING_DIR}/${ROOTFS}"
164    if [ "$B" == "TRUE" ]; then
165        log "fetching bootloader..."
166        wget \
167            -a "${LOG_FILE}" \
168            -P "${WORKING_DIR}" \
169            "${BASE_URL_HTTP}/${VERSION}/${LOADER}"
170    fi
171    if [ "$K" == "TRUE" ]; then
172        log "fetching kernel..."
173        wget \
174            -a "${LOG_FILE}" \
175            -P "${WORKING_DIR}" \
176            "${BASE_URL_HTTP}/${VERSION}/${KERNEL}"
177    fi
178    if [ "$R" == "TRUE" ]; then
179        log "try fetching .ubi.bz2 rootfs..."
180        wget \
181            -a "${LOG_FILE}" \
182            -P "${WORKING_DIR}" \
183            "${BASE_URL_HTTP}/${VERSION}/${ROOTFS}.bz2" && \
184            (cd ${WORKING_DIR}; bzip2 -d ${ROOTFS}.bz2)
185
186        if [ "$?" != "0" ]; then
187            log "fetching .ubi rootfs..."
188            wget \
189            -a "${LOG_FILE}" \
190            -P "${WORKING_DIR}" \
191            "${BASE_URL_HTTP}/${VERSION}/${ROOTFS}"
192        fi
193    fi
194    fi
195fi
196
197log "booting device..."
198usbboot -c "boot" >> "${LOG_FILE}" || abort "can't boot device - xburst-tools setup correctly? device in boot-mode? device connected?"
199
200if [ "$B" == "TRUE" ]; then
201    log "flashing bootloader..."
202    progress_prepare
203    while read ILINE
204        do progress_draw "$ILINE"
205    done< <(usbboot -c "nprog 0 ${WORKING_DIR}/${LOADER} 0 0 -n" 2>"${LOG_FILE}.err" | tee -a "${LOG_FILE}")
206    progress_finish
207    test "${tmp}" && abort "error while flashing bootloader:\n${tmp}"
208fi
209if [ "$K" == "TRUE" ]; then
210    log "flashing kernel..."
211    progress_prepare
212    while read ILINE
213        do progress_draw "$ILINE"
214    done< <(usbboot -c "nprog 1024 ${WORKING_DIR}/${KERNEL} 0 0 -n" 2>"${LOG_FILE}.err" | tee -a "${LOG_FILE}")
215    progress_finish
216    test "${tmp}" && abort "error while flashing kernel:\n${tmp}"
217fi
218if [ "$R" == "TRUE" ]; then
219    log "erase nand rootfs partition..."
220    usbboot -c "nerase 16 1024 0 0" >> "${LOG_FILE}" 2>&1
221    log "flashing rootfs..."
222    progress_prepare
223    while read ILINE
224        do progress_draw "$ILINE"
225    done< <(usbboot -c "nprog 2048 ${WORKING_DIR}/${ROOTFS} 0 0 -n" 2>"${LOG_FILE}.err" | tee -a "${LOG_FILE}")
226    progress_finish
227    test "${tmp}" && abort "error while flashing rootfs:\n${tmp}"
228fi
229
230if [ "$ALL" == "TRUE" ]; then
231    log "reboot device..."
232    usbboot -c "reset" >> "${LOG_FILE}" 2>&1
233fi
234
235log "done"
236

Archive Download this file



interactive