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