diff --git a/chip-fel-flash.sh b/chip-fel-flash.sh index 1c8d77f..9a1b0e9 100755 --- a/chip-fel-flash.sh +++ b/chip-fel-flash.sh @@ -6,6 +6,8 @@ source $SCRIPTDIR/common.sh FEL=fel +METHOD=${METHOD:-fel} + echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" NAND_ERASE_BB=false @@ -30,6 +32,8 @@ UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" UBI_MEM_ADDR=0x4b000000 UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` +PAGE_SIZE=16384 +OOB_SIZE=1664 prepare_images() { local in=$SPL @@ -39,17 +43,22 @@ prepare_images() { rm $out fi - # The BROM cannot read 16K pages: it only reads 8k of data at most. - # Split the SPL image in 8k chunks and pad each chunk with 8k of random - # data to limit the impact of repeated patterns on the MLC chip. + echo ${SCRIPTDIR}/spl-image-builder -d -r 3 -u 4096 -o 1664 -p 16384 -c 1024 -s 64 $in $out - dd if=$in of=$out bs=8k count=1 skip=0 conv=sync - dd if=/dev/urandom of=$out bs=8k count=1 seek=1 conv=sync - dd if=$in of=$out bs=8k count=1 skip=1 seek=2 conv=sync - dd if=/dev/urandom of=$out bs=8k count=1 seek=3 conv=sync - dd if=$in of=$out bs=8k count=1 skip=2 seek=4 conv=sync - dd if=/dev/urandom of=$out bs=8k count=1 seek=5 conv=sync - PADDED_SPL_SIZE=`filesize $out | xargs printf "0x%08x"` + if [[ ! -e ${SCRIPTDIR}/spl-image-builder ]]; then + pushd ${SCRIPTDIR} + make + popd + fi + + ${SCRIPTDIR}/spl-image-builder -d -r 3 -u 4096 -o 1664 -p 16384 -c 1024 -s 64 $in $out + + #PADDED_SPL_SIZE in pages + PADDED_SPL_SIZE=$(filesize $out) + PADDED_SPL_SIZE=$(($PADDED_SPL_SIZE / ($PAGE_SIZE + $OOB_SIZE))) + PADDED_SPL_SIZE=$(echo $PADDED_SPL_SIZE | xargs printf "0x%08x") + echo "filesize= $(filesize $out)" + echo "PADDED_SPL_SIZE=$PADDED_SPL_SIZE" # Align the u-boot image on a page boundary dd if=$UBOOT of=$PADDED_UBOOT bs=16k conv=sync @@ -63,45 +72,103 @@ prepare_uboot_script() { else echo "nand erase 0x0 0x200000000" > $UBOOT_SCRIPT_SRC fi - echo "sunxi_nand config spl" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x0 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x100000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x200000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x300000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x400000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x500000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x600000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC - echo "nand write $SPL_MEM_ADDR 0x700000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC + echo "echo sunxi_nand config spl" >> $UBOOT_SCRIPT_SRC + echo "sunxi_nand config spl" >> $UBOOT_SCRIPT_SRC + echo "echo nand write.raw $SPL_MEM_ADDR 0x0 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC + echo "nand write.raw $SPL_MEM_ADDR 0x0 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC + echo "echo nand write.raw $SPL_MEM_ADDR 0x400000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC + echo "nand write.raw $SPL_MEM_ADDR 0x400000 $PADDED_SPL_SIZE" >> $UBOOT_SCRIPT_SRC echo "sunxi_nand config default" >> $UBOOT_SCRIPT_SRC echo "nand write $UBOOT_MEM_ADDR 0x800000 $PADDED_UBOOT_SIZE" >> $UBOOT_SCRIPT_SRC echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw earlyprintk ubi.mtd=4" >> $UBOOT_SCRIPT_SRC echo "setenv bootcmd 'source \${scriptaddr}; nand slc-mode on; mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r - \$fdt_addr_r'" >> $UBOOT_SCRIPT_SRC echo "saveenv" >> $UBOOT_SCRIPT_SRC - echo "nand slc-mode on" >> $UBOOT_SCRIPT_SRC - echo "nand write.trimffs $UBI_MEM_ADDR 0x1000000 $UBI_SIZE" >> $UBOOT_SCRIPT_SRC - echo "mw \${scriptaddr} 0x0" >> $UBOOT_SCRIPT_SRC - echo "boot" >> $UBOOT_SCRIPT_SRC + if [[ "${METHOD}" == "fel" ]]; then + echo "nand slc-mode on" >> $UBOOT_SCRIPT_SRC + echo "nand write.trimffs $UBI_MEM_ADDR 0x1000000 $UBI_SIZE" >> $UBOOT_SCRIPT_SRC + echo "mw \${scriptaddr} 0x0" >> $UBOOT_SCRIPT_SRC + echo "boot" >> $UBOOT_SCRIPT_SRC + else + echo "echo going to fastboot mode" >>$UBOOT_SCRIPT_SRC + echo "fastboot" >>$UBOOT_SCRIPT_SRC + echo "echo " >>$UBOOT_SCRIPT_SRC + echo "echo *****************[ BOOT ]*****************" >>$UBOOT_SCRIPT_SRC + echo "echo " >>$UBOOT_SCRIPT_SRC + echo "echo " >>$UBOOT_SCRIPT_SRC + echo "boot" >>$UBOOT_SCRIPT_SRC + fi mkimage -A arm -T script -C none -n "flash CHIP" -d $UBOOT_SCRIPT_SRC $UBOOT_SCRIPT } + +############################################################## +# main +############################################################## +while getopts "f" opt; do + case $opt in + f) + echo "fastboot enabled" + METHOD=fastboot + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + + echo == preparing images == prepare_images prepare_uboot_script echo == upload the SPL to SRAM and execute it == +if ! wait_for_fel; then + echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" +fi ${FEL} spl $SPL sleep 1 # wait for DRAM initialization to complete -echo == upload images == +echo == upload spl == ${FEL} write $SPL_MEM_ADDR $PADDED_SPL -${FEL} write $UBOOT_MEM_ADDR $PADDED_UBOOT -${FEL} write $UBI_MEM_ADDR $UBI + +echo == upload u-boot == +${FEL} write $UBOOT_MEM_ADDR $PADDED_UBOOT + +echo == upload u-boot script == ${FEL} write $UBOOT_SCRIPT_MEM_ADDR $UBOOT_SCRIPT -echo == execute the main u-boot binary == -${FEL} exe $UBOOT_MEM_ADDR +if [[ "${METHOD}" == "fel" ]]; then + echo == upload ubi == + ${FEL} --progress write $UBI_MEM_ADDR $UBI -rm -rf $TMPDIR + echo == execute the main u-boot binary == + ${FEL} exe $UBOOT_MEM_ADDR + + echo == write ubi == +else + echo == execute the main u-boot binary == + ${FEL} exe $UBOOT_MEM_ADDR + + echo == waiting for fastboot == + if wait_for_fastboot; then + fastboot -S 0 -u flash UBI ${BUILDROOT_OUTPUT_DIR}/images/rootfs.ubi + fastboot continue + else + rm -rf ${TMPDIR} + exit 1 + fi +fi + +if ! wait_for_linuxboot; then + echo "ERROR: could not flash" + rm -rf ${TMPDIR} + exit 1 +else + ${SCRIPTDIR}/verify.sh +fi + +rm -rf ${TMPDIR} diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 06ad8fc..cfb58a7 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -1,11 +1,8 @@ #!/bin/bash -FW_DIR="$(pwd)/.firmware" -FW_IMAGE_DIR="${FW_DIR}/images" -S3_URL="https://s3-ap-northeast-1.amazonaws.com/stak-images/firmware/chip/stable/latest" - -LATEST_URL="" FLASH_SCRIPT=./chip-fel-flash.sh +WHAT=buildroot +BRANCH=stable function require_directory { if [[ ! -d "${1}" ]]; then @@ -13,36 +10,36 @@ function require_directory { fi } +function s3_md5 { + local URL=$1 + curl -sLI $URL |grep ETag|sed -e 's/.*"\([a-fA-F0-9]\+\)["-]*.*/\1/;' +} + function cache_download { - if [[ ! -f "${1}/${2}" ]]; then + local DEST_DIR=${1} + local SRC_URL=${2} + local FILE=${3} - if [[ -z "$LATEST_URL" ]]; then - LATEST_URL="$(wget -q -O- ${S3_URL})" - echo "LATEST_URL=$LATEST_URL" - if [[ -z "${LATEST_URL}" ]]; then - echo "error: could not get URL for latest build from ${S3_URL} - check internet connection" - exit 1 - fi - - BUILD=${LATEST_URL%%/} - BUILD=${BUILD##*/} - echo "BUILD=$BUILD" - - if [[ -z "${BUILD}" ]]; then - echo "error: could not extract build number from build" - exit 1 - fi + if [[ -f "${DEST_DIR}/${FILE}" ]]; then + echo "${DEST_DIR}/${FILE} exists... comparing to ${SRC_URL}/${FILE}" + local S3_MD5=$(s3_md5 ${SRC_URL}/${FILE}) + local MD5=$(md5sum ${DEST_DIR}/${FILE} | cut -d\ -f1) + echo "MD5: ${MD5}" + echo "S3_MD5: ${S3_MD5}" + if [[ "${S3_MD5}" != "${MD5}" ]]; then + echo "md5sum differs" + rm ${DEST_DIR}/${FILE} + wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}" + else + echo "file already downloaded" fi - - echo "$BUILD" > ${FW_IMAGE_DIR}/build - - wget -P "${FW_IMAGE_DIR}" "${LATEST_URL}images/${2}" || - exit 1 + else + wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}" fi } -while getopts "uf" opt; do +while getopts "ufd" opt; do case $opt in u) echo "updating cache" @@ -52,7 +49,11 @@ while getopts "uf" opt; do ;; f) echo "fastboot enabled" - FLASH_SCRIPT=./chip-fel-fastboot.sh + FLASH_SCRIPT_OPTION="-f" + ;; + d) + echo "debian selected" + WHAT="debian" ;; \?) echo "Invalid option: -$OPTARG" >&2 @@ -61,13 +62,41 @@ while getopts "uf" opt; do esac done -require_directory "${FW_IMAGE_DIR}" -cache_download "${FW_IMAGE_DIR}" rootfs.ubi -cache_download "${FW_IMAGE_DIR}" sun5i-r8-chip.dtb -cache_download "${FW_IMAGE_DIR}" sunxi-spl.bin -cache_download "${FW_IMAGE_DIR}" uboot-env.bin -cache_download "${FW_IMAGE_DIR}" zImage -cache_download "${FW_IMAGE_DIR}" u-boot-dtb.bin -BUILDROOT_OUTPUT_DIR="${FW_DIR}" ${FLASH_SCRIPT} +FW_DIR="$(pwd)/.firmware" +FW_IMAGE_DIR="${FW_DIR}/images" +BASE_URL="http://opensource.nextthing.co/chip" +S3_URL="${BASE_URL}/${WHAT}/${BRANCH}/latest" + +ROOTFS_URL="$(wget -q -O- ${S3_URL})" || (echo "ERROR: cannot reach ${S3_URL}" && exit 1) +if [[ -z "${ROOTFS_URL}" ]]; then + echo "error: could not get URL for latest build from ${S3_URL} - check internet connection" + exit 1 +fi + +if [[ "${WHAT}" == "buildroot" ]]; then + BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" + BUILD=${BR_BUILD} + ROOTFS_URL="${ROOTFS_URL}/images" + BR_URL="${ROOTFS_URL}" +else + BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/br_build)" + BR_URL="${BASE_URL}/buildroot/${BRANCH}/${BR_BUILD}/images" + BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" +fi + +echo "ROOTFS_URL=${ROOTFS_URL}" +echo "BUILD=${BUILD}" +echo "BR_URL=${BR_URL}" +echo "BR_BUILD=${BR_BUILD}" + +require_directory "${FW_IMAGE_DIR}" +cache_download "${FW_IMAGE_DIR}" ${ROOTFS_URL} rootfs.ubi +cache_download "${FW_IMAGE_DIR}" ${BR_URL} sun5i-r8-chip.dtb +cache_download "${FW_IMAGE_DIR}" ${BR_URL} sunxi-spl.bin +cache_download "${FW_IMAGE_DIR}" ${BR_URL} uboot-env.bin +cache_download "${FW_IMAGE_DIR}" ${BR_URL} zImage +cache_download "${FW_IMAGE_DIR}" ${BR_URL} u-boot-dtb.bin + +BUILDROOT_OUTPUT_DIR="${FW_DIR}" ${FLASH_SCRIPT} ${FLASH_SCRIPT_OPTION} exit $? diff --git a/common.sh b/common.sh index 258f69f..7bc8149 100755 --- a/common.sh +++ b/common.sh @@ -2,6 +2,7 @@ TIMEOUT=30 +#------------------------------------------------------------ onMac() { if [ "$(uname)" == "Darwin" ]; then return 0; @@ -10,6 +11,7 @@ onMac() { fi } +#------------------------------------------------------------ filesize() { if onMac; then stat -f "%z" $1 @@ -17,3 +19,53 @@ filesize() { stat --printf="%s" $1 fi } + +#------------------------------------------------------------ +wait_for_fastboot() { + echo -n "waiting for fastboot..."; + for ((i=$TIMEOUT; i>0; i--)) { + if [[ ! -z "$(fastboot devices)" ]]; then + echo "OK"; + return 0; + fi + echo -n "."; + sleep 1 + } + + echo "TIMEOUT"; + return 1 +} + +#------------------------------------------------------------ +wait_for_fel() { + echo -n "waiting for fel..."; + for ((i=$TIMEOUT; i>0; i--)) { + if ${FEL} ver 2>/dev/null >/dev/null; then + echo "OK" + return 0; + fi + echo -n "."; + sleep 1 + } + + echo "TIMEOUT"; + return 1 +} + +#------------------------------------------------------------ +wait_for_linuxboot() { + local TIMEOUT=100 + echo -n "flashing..."; + for ((i=$TIMEOUT; i>0; i--)) { + if lsusb |grep -q "0525:a4a7"; then + echo "OK" + return 0; + fi + echo -n "."; + sleep 3 + } + + echo "TIMEOUT"; + return 1 +} + diff --git a/verify.sh b/verify.sh index 6d48937..1e2ecdb 100755 --- a/verify.sh +++ b/verify.sh @@ -4,6 +4,7 @@ import io import sys import serial import re +import time #------------------------------------------------------------------ def answer_prompt(sio,prompt_to_wait_for,answer_to_write,send_cr=True): @@ -18,23 +19,30 @@ def answer_prompt(sio,prompt_to_wait_for,answer_to_write,send_cr=True): while not len(d)==0: d = sio.read(2000); data += d - print '-' * 50 - print ' %d bytes read' % (len(data)) - print '-' * 50 + time.sleep(1) +# print '-' * 50 +# print ' %d bytes read' % (len(data)) +# print '-' * 50 - print data + #print data + line='' while not prompt_found: d = sio.read(100); data += d - print '-' * 50 - print ' %d bytes read' % (len(data)) - print '-' * 50 - if(data[:-1].endswith(prompt_to_wait_for)): +# print '-' * 50 +# print ' %d bytes read' % (len(data)) +# print '-' * 50 +# print data +# print '-' * 50 + if len(data.split())>0: + line=data.split()[-1] +# print "matching [%s] against [%s]" % (line,prompt_to_wait_for) + if(re.match(prompt_to_wait_for,line,re.M)): sio.write(unicode(answer_to_write+'\n')) - print '-' * 50 - print ' detected [%s] ' % prompt_to_wait_for - print '-' * 50 +# print '-' * 50 +# print ' detected [%s] ' % prompt_to_wait_for +# print '-' * 50 prompt_found = True else: if send_cr: @@ -49,14 +57,14 @@ def scanfor(sio,regexp_to_scan_for,answer_to_write): data = '' while not prompt_found: data += sio.read(100); - print '-' * 50 - print ' %d bytes read' % (len(data)) - print '-' * 50 - print data +# print '-' * 50 +# print ' %d bytes read' % (len(data)) +# print '-' * 50 +# print data if re.search(regexp_to_scan_for,data): - print '-' * 50 - print ' detected [%s] ' % regexp_to_scan_for - print '-' * 50 +# print '-' * 50 +# print ' detected [%s] ' % regexp_to_scan_for +# print '-' * 50 sio.write(unicode(answer_to_write+'\n')) prompt_found = True sio.flush() @@ -72,26 +80,34 @@ def main(): else: serial_port='/dev/ttyACM0'; - print 'reading from %s:' % serial_port + #print 'reading from %s:' % serial_port ser = serial.Serial(serial_port,115200, timeout=1); sio = io.TextIOWrapper(io.BufferedRWPair(ser,ser)) #login - answer_prompt(sio,'login:','root') - answer_prompt(sio,'Password:','chip',False) - answer_prompt(sio,'#','hwtest') - d=scanfor(sio,r'.*### [^#]+ ###.*','poweroff') - - if re.search(r'.*### ALL TESTS PASSED ###.*',d): - print "---> TESTS PASSED" - ser.close(); - return 0 + print "login...", + sys.stdout.flush() + answer_prompt(sio,'.*login:','chip',True) + print "OK\npassword...", + sys.stdout.flush() + answer_prompt(sio,'.*Password:','chip',False) + print "OK\npoweroff...", + sys.stdout.flush() + answer_prompt(sio,'.*[\$#]','sudo poweroff') + answer_prompt(sio,'.*:','chip') + time.sleep(2) + print "OK\n", + #d=scanfor(sio,r'.*### [^#]+ ###.*','poweroff') +# if re.search(r'.*### ALL TESTS PASSED ###.*',d): +# print "---> TESTS PASSED" +# ser.close(); +# return 0 ser.close(); - print "---> TESTS FAILED" +# print "---> TESTS FAILED" return 1