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

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

Archive Download this file



interactive