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

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

Archive Download this file



interactive