Root/nanonote-files/data/qi_lb60/scripts/reflash_ben.sh

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

Archive Download this file



interactive