From 0769db9f9bdaa7dffae047dc969dd2f41aa52a6e Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 7 Mar 2016 21:38:56 -0800 Subject: [PATCH 01/50] Added modified fel-flash script for new image format --- chip-local-flash.sh | 144 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100755 chip-local-flash.sh diff --git a/chip-local-flash.sh b/chip-local-flash.sh new file mode 100755 index 0000000..d3966f5 --- /dev/null +++ b/chip-local-flash.sh @@ -0,0 +1,144 @@ +#!/bin/bash + +set -x + +SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source $SCRIPTDIR/common.sh + +############################################################## +# main +############################################################## +while getopts "flpu:" opt; do + case $opt in + f) + echo "fastboot enabled" + METHOD=fastboot + ;; + l) + echo "factory mode remain in u-boot after flashing" + AFTER_FLASHING=loop + ;; + u) + BUILDROOT_OUTPUT_DIR="${OPTARG}" + ;; + p) + POCKET_CHIP=true + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + +echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" + +FEL=fel + +METHOD=${METHOD:-fel} +AFTER_FLASHING=${AFTER_FLASHING:-wait} + + +NAND_ERASE_BB=false +if [ "$1" == "erase-bb" ]; then + NAND_ERASE_BB=true +fi + +PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin +TMPDIR=`mktemp -d -t chipflashXXXXXX` +PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" +PADDED_SPL_SIZE=0 +UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" +UBOOT_SCRIPT_MEM_ADDR=0x43100000 +UBOOT_SCRIPT_SRC="$TMPDIR/uboot.cmds" +SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" +SPL_MEM_ADDR=0x43000000 +UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" +PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" +PADDED_UBOOT_SIZE=0x400000 +UBOOT_MEM_ADDR=0x4a000000 +UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" +SPARSE_UBI="${TMPDIR}/rootfs.ubi.sparse" +UBI_MEM_ADDR=0x4b000000 + +UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` +PAGE_SIZE=16384 +OOB_SIZE=1664 + +assert_error() { + ERR=$? + ERRCODE=$1 + if [ "${ERR}" != "0" ]; then + if [ -z "${ERR}" ]; then + exit ${ERR} + else + exit ${ERRCODE} + fi + fi +} + +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}" +assert_error 128 + +sleep 1 # wait for DRAM initialization to complete + +echo == upload spl == +${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) +assert_error 129 + +echo == upload u-boot == +${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) +assert_error 130 + +echo == upload u-boot script == +${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) +assert_error 131 + +if [[ "${METHOD}" == "fel" ]]; then + echo == upload ubi == + ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" + + 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 + assert_error 132 + + echo == creating sparse image == +# img2simg ${UBI} ${SPARSE_UBI} $((2*1024*1024)) + assert_error 133 + + echo == waiting for fastboot == + if wait_for_fastboot; then + fastboot -i 0x1f3a -u flash UBI $UBI + assert_error 134 + + fastboot -i 0x1f3a continue + assert_error 135 + else + rm -rf "${TMPDIR}" + exit 1 + fi +fi + +if [[ "${METHOD}" == "fel" ]]; then + if ! wait_for_linuxboot; then + echo "ERROR: could not flash": + rm -rf ${TMPDIR} + exit 1 + else + ${SCRIPTDIR}/verify.sh + fi +fi +rm -rf "${TMPDIR}" From 82f22eebdf809ea26dda63b293ea11a5258a9b30 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 14 Mar 2016 10:33:15 -0700 Subject: [PATCH 02/50] Decreased OOB_SIZE to match new SPL_ECC_STRENGTH --- chip-local-flash.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index d3966f5..9cc5b3c 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -63,7 +63,7 @@ UBI_MEM_ADDR=0x4b000000 UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` PAGE_SIZE=16384 -OOB_SIZE=1664 +OOB_SIZE=1280 assert_error() { ERR=$? From 307e4b628921fd53dc16926c57ba49d62fbc2d9e Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 14 Mar 2016 10:46:30 -0700 Subject: [PATCH 03/50] Created separate flashing script for 4GB toshiba nand --- chip-TOSH4GB-flash.sh | 144 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100755 chip-TOSH4GB-flash.sh diff --git a/chip-TOSH4GB-flash.sh b/chip-TOSH4GB-flash.sh new file mode 100755 index 0000000..9cc5b3c --- /dev/null +++ b/chip-TOSH4GB-flash.sh @@ -0,0 +1,144 @@ +#!/bin/bash + +set -x + +SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source $SCRIPTDIR/common.sh + +############################################################## +# main +############################################################## +while getopts "flpu:" opt; do + case $opt in + f) + echo "fastboot enabled" + METHOD=fastboot + ;; + l) + echo "factory mode remain in u-boot after flashing" + AFTER_FLASHING=loop + ;; + u) + BUILDROOT_OUTPUT_DIR="${OPTARG}" + ;; + p) + POCKET_CHIP=true + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + +echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" + +FEL=fel + +METHOD=${METHOD:-fel} +AFTER_FLASHING=${AFTER_FLASHING:-wait} + + +NAND_ERASE_BB=false +if [ "$1" == "erase-bb" ]; then + NAND_ERASE_BB=true +fi + +PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin +TMPDIR=`mktemp -d -t chipflashXXXXXX` +PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" +PADDED_SPL_SIZE=0 +UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" +UBOOT_SCRIPT_MEM_ADDR=0x43100000 +UBOOT_SCRIPT_SRC="$TMPDIR/uboot.cmds" +SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" +SPL_MEM_ADDR=0x43000000 +UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" +PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" +PADDED_UBOOT_SIZE=0x400000 +UBOOT_MEM_ADDR=0x4a000000 +UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" +SPARSE_UBI="${TMPDIR}/rootfs.ubi.sparse" +UBI_MEM_ADDR=0x4b000000 + +UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` +PAGE_SIZE=16384 +OOB_SIZE=1280 + +assert_error() { + ERR=$? + ERRCODE=$1 + if [ "${ERR}" != "0" ]; then + if [ -z "${ERR}" ]; then + exit ${ERR} + else + exit ${ERRCODE} + fi + fi +} + +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}" +assert_error 128 + +sleep 1 # wait for DRAM initialization to complete + +echo == upload spl == +${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) +assert_error 129 + +echo == upload u-boot == +${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) +assert_error 130 + +echo == upload u-boot script == +${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) +assert_error 131 + +if [[ "${METHOD}" == "fel" ]]; then + echo == upload ubi == + ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" + + 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 + assert_error 132 + + echo == creating sparse image == +# img2simg ${UBI} ${SPARSE_UBI} $((2*1024*1024)) + assert_error 133 + + echo == waiting for fastboot == + if wait_for_fastboot; then + fastboot -i 0x1f3a -u flash UBI $UBI + assert_error 134 + + fastboot -i 0x1f3a continue + assert_error 135 + else + rm -rf "${TMPDIR}" + exit 1 + fi +fi + +if [[ "${METHOD}" == "fel" ]]; then + if ! wait_for_linuxboot; then + echo "ERROR: could not flash": + rm -rf ${TMPDIR} + exit 1 + else + ${SCRIPTDIR}/verify.sh + fi +fi +rm -rf "${TMPDIR}" From 9f1b8360343e84e8078db9afc321b1aa2d6dc7b4 Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 16 Mar 2016 15:49:33 -0700 Subject: [PATCH 04/50] Removed unnecessary options --- chip-local-flash.sh | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 9cc5b3c..41187ed 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -8,22 +8,15 @@ source $SCRIPTDIR/common.sh ############################################################## # main ############################################################## -while getopts "flpu:" opt; do +while getopts "fu:" opt; do case $opt in f) echo "fastboot enabled" METHOD=fastboot ;; - l) - echo "factory mode remain in u-boot after flashing" - AFTER_FLASHING=loop - ;; u) BUILDROOT_OUTPUT_DIR="${OPTARG}" ;; - p) - POCKET_CHIP=true - ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 From 49de8d03378295f262415228578c14de1777c56d Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 17 Mar 2016 12:32:07 -0700 Subject: [PATCH 05/50] Added update features --- chip-local-flash.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 41187ed..eefc44a 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -5,15 +5,32 @@ set -x SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh +BUILDROOT_OUTPUT_DIR=".new/firmware" +FIRMWARE_DIR=".new/firmware" + +DL_METHOD=fel +DL_FLAVOR=serv +DL_DIST=rootfs +DL_DIR=".dl" + ############################################################## # main ############################################################## -while getopts "fu:" opt; do +while getopts "fdnu:" opt; do case $opt in f) echo "fastboot enabled" + DL_METHOD=fb METHOD=fastboot ;; + d) + echo "desktop selected" + DL_FLAVOR=desk + ;; + n) + echo "next enabled" + DL_DIST=testing-rootfs + ;; u) BUILDROOT_OUTPUT_DIR="${OPTARG}" ;; @@ -26,6 +43,42 @@ done echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" +function require_directory { + if [[ ! -d "${1}" ]]; then + mkdir -p "${1}" + fi +} + +function dl_check { + wget -O $DL_DIR/latest.md5\ + opensource.nextthing.co/chippian/$DL_DIST/latest.md5 + + pushd $DL_DIR + if [[ $(cat latest.md5 | grep "`md5sum img-$DL_FLAVOR-$DL_METHOD.tar.gz`")\ + && -d "img-$DL_FLAVOR-$DL_METHOD/images" ]]; then + echo "Cached files located" + echo "Staging for flashing" + cp -R img-$DL_FLAVOR-$DL_METHOD/images ../$FIRMWARE_DIR/ + else + echo "New image available" + rm -rf img-$DL_FLAVOR-$DL_METHOD* + + wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz + + echo "Extracting.." + tar -xf img-$DL_FLAVOR-$DL_METHOD.tar.gz + echo "Staging for flashing" + cp -R img-$DL_FLAVOR-$DL_METHOD/images ../$FIRMWARE_DIR/ + fi + popd +} + + +require_directory "$FIRMWARE_DIR" +require_directory "$DL_DIR" + +dl_check + FEL=fel METHOD=${METHOD:-fel} From e5ca20eca9af98b3b767655c59d6742c977d4aa9 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 17 Mar 2016 14:48:56 -0700 Subject: [PATCH 06/50] Added ignore definitions for new cache directories --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 40de291..ea5641e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .firmware +.dl +.new From e74619bae56d1b18c85e600311d4fa47639c4114 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 21 Mar 2016 19:05:29 -0700 Subject: [PATCH 07/50] Add pocketchip flashing script --- chip-local-flash.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index eefc44a..1356124 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -16,7 +16,7 @@ DL_DIR=".dl" ############################################################## # main ############################################################## -while getopts "fdnu:" opt; do +while getopts "fdpnu:" opt; do case $opt in f) echo "fastboot enabled" @@ -27,6 +27,10 @@ while getopts "fdnu:" opt; do echo "desktop selected" DL_FLAVOR=desk ;; + p) + echo "desktop selected" + DL_FLAVOR=pocket + ;; n) echo "next enabled" DL_DIST=testing-rootfs From c581f8d3b5ec10fba2fd1b6c5e28b85144a01997 Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 20 Apr 2016 16:10:21 -0700 Subject: [PATCH 08/50] Added method for flashing hdmi image --- chip-local-flash.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 1356124..321e703 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -31,6 +31,10 @@ while getopts "fdpnu:" opt; do echo "desktop selected" DL_FLAVOR=pocket ;; + h) + echo "desktop selected" + DL_FLAVOR=hdmi-fixture-rootfs + ;; n) echo "next enabled" DL_DIST=testing-rootfs From a06b8baaba94bd1610d44f11009d2a7ab6842fa1 Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 20 Apr 2016 17:46:25 -0700 Subject: [PATCH 09/50] Fix bash possible options --- chip-local-flash.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 321e703..ccad884 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -16,7 +16,7 @@ DL_DIR=".dl" ############################################################## # main ############################################################## -while getopts "fdpnu:" opt; do +while getopts "fdphnu:" opt; do case $opt in f) echo "fastboot enabled" From 87d5d1d9b5b3f564f2fa1fc8dd75da8c5f3271ee Mon Sep 17 00:00:00 2001 From: Alex Kaplan Date: Thu, 19 May 2016 16:02:25 -0700 Subject: [PATCH 10/50] added support for PocketCHIP --- chip-local-flash.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index ccad884..364d809 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -4,18 +4,18 @@ set -x SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh +source $SCRIPTDIR/env.sh +export AWS_DEFAULT_REGION=us-west-2 BUILDROOT_OUTPUT_DIR=".new/firmware" FIRMWARE_DIR=".new/firmware" DL_METHOD=fel +METHOD=fel DL_FLAVOR=serv DL_DIST=rootfs DL_DIR=".dl" -############################################################## -# main -############################################################## while getopts "fdphnu:" opt; do case $opt in f) @@ -49,6 +49,10 @@ while getopts "fdphnu:" opt; do esac done +if [[ "${DL_FLAVOR}" == "pocket" ]] && [[ "${DL_DIST}" == "testing-rootfs" ]]; then + DL_FLAVOR="pocket-next" +fi + echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" function require_directory { @@ -70,8 +74,12 @@ function dl_check { else echo "New image available" rm -rf img-$DL_FLAVOR-$DL_METHOD* - - wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz + + if [[ "${DL_FLAVOR}"=="pocket" || "${DL_FLAVOR}"=="pocket-next" ]]; then + aws s3 cp s3://opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz . || exit 1 + else + wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 + fi echo "Extracting.." tar -xf img-$DL_FLAVOR-$DL_METHOD.tar.gz From 37f013c835165f8d2ffbc28be27194be7d98245a Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 19 May 2016 18:33:22 -0700 Subject: [PATCH 11/50] Fix comparison to not use aws for non-PC builds --- chip-local-flash.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 364d809..796d0af 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -75,7 +75,7 @@ function dl_check { echo "New image available" rm -rf img-$DL_FLAVOR-$DL_METHOD* - if [[ "${DL_FLAVOR}"=="pocket" || "${DL_FLAVOR}"=="pocket-next" ]]; then + if [[ "${DL_FLAVOR}" == "pocket" || "${DL_FLAVOR}" == "pocket-next" ]]; then aws s3 cp s3://opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz . || exit 1 else wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 From 4a492dd1b36d29c72a3675a5591ca302685fe9d2 Mon Sep 17 00:00:00 2001 From: Alex Kaplan Date: Fri, 20 May 2016 12:46:10 -0700 Subject: [PATCH 12/50] added env.sh to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ea5641e..ed00df5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .firmware .dl .new +env.sh From 2cb5ef6ba990228b85a1a598ca65db3f3dedd0b2 Mon Sep 17 00:00:00 2001 From: Alex Kaplan Date: Fri, 20 May 2016 12:55:40 -0700 Subject: [PATCH 13/50] removed unused files --- Makefile | 10 - chip-fel-fastboot.sh | 132 ---- spl-image-builder.c | 1730 ------------------------------------------ 3 files changed, 1872 deletions(-) delete mode 100644 Makefile delete mode 100755 chip-fel-fastboot.sh delete mode 100644 spl-image-builder.c diff --git a/Makefile b/Makefile deleted file mode 100644 index e162873..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -CC=$(CROSS_COMPILE)gcc - -spl-image-builder: spl-image-builder.o - $(CC) -o $@ $< - -all: spl-image-builder - -clean: - rm -rf *.o - rm spl-image-builder diff --git a/chip-fel-fastboot.sh b/chip-fel-fastboot.sh deleted file mode 100755 index 8cdd67b..0000000 --- a/chip-fel-fastboot.sh +++ /dev/null @@ -1,132 +0,0 @@ -#!/bin/bash - -SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) - -source $SCRIPTDIR/common.sh - -FEL=fel - -echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" - -NAND_ERASE_BB=false -if [ "$1" == "erase-bb" ]; then - NAND_ERASE_BB=true -fi - -PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin -TMPDIR=`mktemp -d -t chipflashXXXXXX` -PADDED_SPL="$TMPDIR/sunxi-padded-spl" -PADDED_SPL_SIZE=0 -UBOOT_SCRIPT="$TMPDIR/uboot.scr" -UBOOT_SCRIPT_MEM_ADDR=0x43100000 -UBOOT_SCRIPT_SRC="$TMPDIR/uboot.cmds" -SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" -SPL_MEM_ADDR=0x43000000 -UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" -PADDED_UBOOT="$TMPDIR/padded-uboot" -PADDED_UBOOT_SIZE=0xc0000 -UBOOT_MEM_ADDR=0x4a000000 -UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" -UBI_MEM_ADDR=0x4b000000 - -UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` - -prepare_images() { - local in=$SPL - local out=$PADDED_SPL - - if [ -e "$out" ]; then - 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. - - 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"` - - # Align the u-boot image on a page boundary - dd if=$UBOOT of=$PADDED_UBOOT bs=16k conv=sync - UBOOT_SIZE=`filesize $PADDED_UBOOT | xargs printf "0x%08x"` - dd if=/dev/urandom of=$PADDED_UBOOT seek=$((UBOOT_SIZE / 0x4000)) bs=16k count=$(((PADDED_UBOOT_SIZE - UBOOT_SIZE) / 0x4000)) -} - -prepare_uboot_script() { - if [ "$NAND_ERASE_BB" = true ] ; then - echo "nand scrub -y 0x0 0x200000000" > "${UBOOT_SCRIPT_SRC}" - 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 "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 "echo going to fastboot mode" >>"${UBOOT_SCRIPT_SRC}" - echo "fastboot 0" >>"${UBOOT_SCRIPT_SRC}" - echo "echo " >>"${UBOOT_SCRIPT_SRC}" - echo "echo *****************[ BOOT ]*****************" >>"${UBOOT_SCRIPT_SRC}" - echo "echo " >>"${UBOOT_SCRIPT_SRC}" - echo "boot" >>"${UBOOT_SCRIPT_SRC}" - - mkimage -A arm -T script -C none -n "flash CHIP" -d "$UBOOT_SCRIPT_SRC" "$UBOOT_SCRIPT" -} - -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 -} - -echo == preparing images == -prepare_images -prepare_uboot_script - -echo == upload the SPL to SRAM and execute it == -${FEL} spl $SPL - -sleep 1 # wait for DRAM initialization to complete - -echo == upload spl == -${FEL} write $SPL_MEM_ADDR "$PADDED_SPL" -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 - -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 diff --git a/spl-image-builder.c b/spl-image-builder.c deleted file mode 100644 index 9c23c05..0000000 --- a/spl-image-builder.c +++ /dev/null @@ -1,1730 +0,0 @@ -/* - * Generic binary BCH encoding/decoding library - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 as published by - * the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright © 2011 Parrot S.A. - * - * Author: Ivan Djelic - * - * Description: - * - * This library provides runtime configurable encoding/decoding of binary - * Bose-Chaudhuri-Hocquenghem (BCH) codes. - * - * Call init_bch to get a pointer to a newly allocated bch_control structure for - * the given m (Galois field order), t (error correction capability) and - * (optional) primitive polynomial parameters. - * - * Call encode_bch to compute and store ecc parity bytes to a given buffer. - * Call decode_bch to detect and locate errors in received data. - * - * On systems supporting hw BCH features, intermediate results may be provided - * to decode_bch in order to skip certain steps. See decode_bch() documentation - * for details. - * - * Option CONFIG_BCH_CONST_PARAMS can be used to force fixed values of - * parameters m and t; thus allowing extra compiler optimizations and providing - * better (up to 2x) encoding performance. Using this option makes sense when - * (m,t) are fixed and known in advance, e.g. when using BCH error correction - * on a particular NAND flash device. - * - * Algorithmic details: - * - * Encoding is performed by processing 32 input bits in parallel, using 4 - * remainder lookup tables. - * - * The final stage of decoding involves the following internal steps: - * a. Syndrome computation - * b. Error locator polynomial computation using Berlekamp-Massey algorithm - * c. Error locator root finding (by far the most expensive step) - * - * In this implementation, step c is not performed using the usual Chien search. - * Instead, an alternative approach described in [1] is used. It consists in - * factoring the error locator polynomial using the Berlekamp Trace algorithm - * (BTA) down to a certain degree (4), after which ad hoc low-degree polynomial - * solving techniques [2] are used. The resulting algorithm, called BTZ, yields - * much better performance than Chien search for usual (m,t) values (typically - * m >= 13, t < 32, see [1]). - * - * [1] B. Biswas, V. Herbert. Efficient root finding of polynomials over fields - * of characteristic 2, in: Western European Workshop on Research in Cryptology - * - WEWoRC 2009, Graz, Austria, LNCS, Springer, July 2009, to appear. - * [2] [Zin96] V.A. Zinoviev. On the solution of equations of degree 10 over - * finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(CONFIG_BCH_CONST_PARAMS) -#define GF_M(_p) (CONFIG_BCH_CONST_M) -#define GF_T(_p) (CONFIG_BCH_CONST_T) -#define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1) -#else -#define GF_M(_p) ((_p)->m) -#define GF_T(_p) ((_p)->t) -#define GF_N(_p) ((_p)->n) -#endif - -#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) - -#define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32) -#define BCH_ECC_BYTES(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8) - -#ifndef dbg -#define dbg(_fmt, args...) do {} while (0) -#endif - -#define cpu_to_be32 htobe32 -#define kfree free -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) - -#define BCH_PRIMITIVE_POLY 0x5803 - -struct image_info { - int ecc_strength; - int ecc_step_size; - int page_size; - int oob_size; - int usable_page_size; - int repeat; - int disable_scrambler; - const char *source; - const char *dest; -}; - -/** - * struct bch_control - BCH control structure - * @m: Galois field order - * @n: maximum codeword size in bits (= 2^m-1) - * @t: error correction capability in bits - * @ecc_bits: ecc exact size in bits, i.e. generator polynomial degree (<=m*t) - * @ecc_bytes: ecc max size (m*t bits) in bytes - * @a_pow_tab: Galois field GF(2^m) exponentiation lookup table - * @a_log_tab: Galois field GF(2^m) log lookup table - * @mod8_tab: remainder generator polynomial lookup tables - * @ecc_buf: ecc parity words buffer - * @ecc_buf2: ecc parity words buffer - * @xi_tab: GF(2^m) base for solving degree 2 polynomial roots - * @syn: syndrome buffer - * @cache: log-based polynomial representation buffer - * @elp: error locator polynomial - * @poly_2t: temporary polynomials of degree 2t - */ -struct bch_control { - unsigned int m; - unsigned int n; - unsigned int t; - unsigned int ecc_bits; - unsigned int ecc_bytes; -/* private: */ - uint16_t *a_pow_tab; - uint16_t *a_log_tab; - uint32_t *mod8_tab; - uint32_t *ecc_buf; - uint32_t *ecc_buf2; - unsigned int *xi_tab; - unsigned int *syn; - int *cache; - struct gf_poly *elp; - struct gf_poly *poly_2t[4]; -}; - -static int fls(int x) -{ - int r = 32; - - if (!x) - return 0; - if (!(x & 0xffff0000u)) { - x <<= 16; - r -= 16; - } - if (!(x & 0xff000000u)) { - x <<= 8; - r -= 8; - } - if (!(x & 0xf0000000u)) { - x <<= 4; - r -= 4; - } - if (!(x & 0xc0000000u)) { - x <<= 2; - r -= 2; - } - if (!(x & 0x80000000u)) { - x <<= 1; - r -= 1; - } - return r; -} - -/* - * represent a polynomial over GF(2^m) - */ -struct gf_poly { - unsigned int deg; /* polynomial degree */ - unsigned int c[0]; /* polynomial terms */ -}; - -/* given its degree, compute a polynomial size in bytes */ -#define GF_POLY_SZ(_d) (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int)) - -/* polynomial of degree 1 */ -struct gf_poly_deg1 { - struct gf_poly poly; - unsigned int c[2]; -}; - -/* - * same as encode_bch(), but process input data one byte at a time - */ -static void encode_bch_unaligned(struct bch_control *bch, - const unsigned char *data, unsigned int len, - uint32_t *ecc) -{ - int i; - const uint32_t *p; - const int l = BCH_ECC_WORDS(bch)-1; - - while (len--) { - p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); - - for (i = 0; i < l; i++) - ecc[i] = ((ecc[i] << 8)|(ecc[i+1] >> 24))^(*p++); - - ecc[l] = (ecc[l] << 8)^(*p); - } -} - -/* - * convert ecc bytes to aligned, zero-padded 32-bit ecc words - */ -static void load_ecc8(struct bch_control *bch, uint32_t *dst, - const uint8_t *src) -{ - uint8_t pad[4] = {0, 0, 0, 0}; - unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; - - for (i = 0; i < nwords; i++, src += 4) - dst[i] = (src[0] << 24)|(src[1] << 16)|(src[2] << 8)|src[3]; - - memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); - dst[nwords] = (pad[0] << 24)|(pad[1] << 16)|(pad[2] << 8)|pad[3]; -} - -/* - * convert 32-bit ecc words to ecc bytes - */ -static void store_ecc8(struct bch_control *bch, uint8_t *dst, - const uint32_t *src) -{ - uint8_t pad[4]; - unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; - - for (i = 0; i < nwords; i++) { - *dst++ = (src[i] >> 24); - *dst++ = (src[i] >> 16) & 0xff; - *dst++ = (src[i] >> 8) & 0xff; - *dst++ = (src[i] >> 0) & 0xff; - } - pad[0] = (src[nwords] >> 24); - pad[1] = (src[nwords] >> 16) & 0xff; - pad[2] = (src[nwords] >> 8) & 0xff; - pad[3] = (src[nwords] >> 0) & 0xff; - memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); -} - -/** - * encode_bch - calculate BCH ecc parity of data - * @bch: BCH control structure - * @data: data to encode - * @len: data length in bytes - * @ecc: ecc parity data, must be initialized by caller - * - * The @ecc parity array is used both as input and output parameter, in order to - * allow incremental computations. It should be of the size indicated by member - * @ecc_bytes of @bch, and should be initialized to 0 before the first call. - * - * The exact number of computed ecc parity bits is given by member @ecc_bits of - * @bch; it may be less than m*t for large values of t. - */ -static void encode_bch(struct bch_control *bch, const uint8_t *data, - unsigned int len, uint8_t *ecc) -{ - const unsigned int l = BCH_ECC_WORDS(bch)-1; - unsigned int i, mlen; - unsigned long m; - uint32_t w, r[l+1]; - const uint32_t * const tab0 = bch->mod8_tab; - const uint32_t * const tab1 = tab0 + 256*(l+1); - const uint32_t * const tab2 = tab1 + 256*(l+1); - const uint32_t * const tab3 = tab2 + 256*(l+1); - const uint32_t *pdata, *p0, *p1, *p2, *p3; - - if (ecc) { - /* load ecc parity bytes into internal 32-bit buffer */ - load_ecc8(bch, bch->ecc_buf, ecc); - } else { - memset(bch->ecc_buf, 0, sizeof(r)); - } - - /* process first unaligned data bytes */ - m = ((unsigned long)data) & 3; - if (m) { - mlen = (len < (4-m)) ? len : 4-m; - encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); - data += mlen; - len -= mlen; - } - - /* process 32-bit aligned data words */ - pdata = (uint32_t *)data; - mlen = len/4; - data += 4*mlen; - len -= 4*mlen; - memcpy(r, bch->ecc_buf, sizeof(r)); - - /* - * split each 32-bit word into 4 polynomials of weight 8 as follows: - * - * 31 ...24 23 ...16 15 ... 8 7 ... 0 - * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt - * tttttttt mod g = r0 (precomputed) - * zzzzzzzz 00000000 mod g = r1 (precomputed) - * yyyyyyyy 00000000 00000000 mod g = r2 (precomputed) - * xxxxxxxx 00000000 00000000 00000000 mod g = r3 (precomputed) - * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt mod g = r0^r1^r2^r3 - */ - while (mlen--) { - /* input data is read in big-endian format */ - w = r[0]^cpu_to_be32(*pdata++); - p0 = tab0 + (l+1)*((w >> 0) & 0xff); - p1 = tab1 + (l+1)*((w >> 8) & 0xff); - p2 = tab2 + (l+1)*((w >> 16) & 0xff); - p3 = tab3 + (l+1)*((w >> 24) & 0xff); - - for (i = 0; i < l; i++) - r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i]; - - r[l] = p0[l]^p1[l]^p2[l]^p3[l]; - } - memcpy(bch->ecc_buf, r, sizeof(r)); - - /* process last unaligned bytes */ - if (len) - encode_bch_unaligned(bch, data, len, bch->ecc_buf); - - /* store ecc parity bytes into original parity buffer */ - if (ecc) - store_ecc8(bch, ecc, bch->ecc_buf); -} - -static inline int modulo(struct bch_control *bch, unsigned int v) -{ - const unsigned int n = GF_N(bch); - while (v >= n) { - v -= n; - v = (v & n) + (v >> GF_M(bch)); - } - return v; -} - -/* - * shorter and faster modulo function, only works when v < 2N. - */ -static inline int mod_s(struct bch_control *bch, unsigned int v) -{ - const unsigned int n = GF_N(bch); - return (v < n) ? v : v-n; -} - -static inline int deg(unsigned int poly) -{ - /* polynomial degree is the most-significant bit index */ - return fls(poly)-1; -} - -static inline int parity(unsigned int x) -{ - /* - * public domain code snippet, lifted from - * http://www-graphics.stanford.edu/~seander/bithacks.html - */ - x ^= x >> 1; - x ^= x >> 2; - x = (x & 0x11111111U) * 0x11111111U; - return (x >> 28) & 1; -} - -/* Galois field basic operations: multiply, divide, inverse, etc. */ - -static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, - unsigned int b) -{ - return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ - bch->a_log_tab[b])] : 0; -} - -static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) -{ - return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; -} - -static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, - unsigned int b) -{ - return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ - GF_N(bch)-bch->a_log_tab[b])] : 0; -} - -static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) -{ - return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; -} - -static inline unsigned int a_pow(struct bch_control *bch, int i) -{ - return bch->a_pow_tab[modulo(bch, i)]; -} - -static inline int a_log(struct bch_control *bch, unsigned int x) -{ - return bch->a_log_tab[x]; -} - -static inline int a_ilog(struct bch_control *bch, unsigned int x) -{ - return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); -} - -/* - * compute 2t syndromes of ecc polynomial, i.e. ecc(a^j) for j=1..2t - */ -static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, - unsigned int *syn) -{ - int i, j, s; - unsigned int m; - uint32_t poly; - const int t = GF_T(bch); - - s = bch->ecc_bits; - - /* make sure extra bits in last ecc word are cleared */ - m = ((unsigned int)s) & 31; - if (m) - ecc[s/32] &= ~((1u << (32-m))-1); - memset(syn, 0, 2*t*sizeof(*syn)); - - /* compute v(a^j) for j=1 .. 2t-1 */ - do { - poly = *ecc++; - s -= 32; - while (poly) { - i = deg(poly); - for (j = 0; j < 2*t; j += 2) - syn[j] ^= a_pow(bch, (j+1)*(i+s)); - - poly ^= (1 << i); - } - } while (s > 0); - - /* v(a^(2j)) = v(a^j)^2 */ - for (j = 0; j < t; j++) - syn[2*j+1] = gf_sqr(bch, syn[j]); -} - -static void gf_poly_copy(struct gf_poly *dst, struct gf_poly *src) -{ - memcpy(dst, src, GF_POLY_SZ(src->deg)); -} - -static int compute_error_locator_polynomial(struct bch_control *bch, - const unsigned int *syn) -{ - const unsigned int t = GF_T(bch); - const unsigned int n = GF_N(bch); - unsigned int i, j, tmp, l, pd = 1, d = syn[0]; - struct gf_poly *elp = bch->elp; - struct gf_poly *pelp = bch->poly_2t[0]; - struct gf_poly *elp_copy = bch->poly_2t[1]; - int k, pp = -1; - - memset(pelp, 0, GF_POLY_SZ(2*t)); - memset(elp, 0, GF_POLY_SZ(2*t)); - - pelp->deg = 0; - pelp->c[0] = 1; - elp->deg = 0; - elp->c[0] = 1; - - /* use simplified binary Berlekamp-Massey algorithm */ - for (i = 0; (i < t) && (elp->deg <= t); i++) { - if (d) { - k = 2*i-pp; - gf_poly_copy(elp_copy, elp); - /* e[i+1](X) = e[i](X)+di*dp^-1*X^2(i-p)*e[p](X) */ - tmp = a_log(bch, d)+n-a_log(bch, pd); - for (j = 0; j <= pelp->deg; j++) { - if (pelp->c[j]) { - l = a_log(bch, pelp->c[j]); - elp->c[j+k] ^= a_pow(bch, tmp+l); - } - } - /* compute l[i+1] = max(l[i]->c[l[p]+2*(i-p]) */ - tmp = pelp->deg+k; - if (tmp > elp->deg) { - elp->deg = tmp; - gf_poly_copy(pelp, elp_copy); - pd = d; - pp = 2*i; - } - } - /* di+1 = S(2i+3)+elp[i+1].1*S(2i+2)+...+elp[i+1].lS(2i+3-l) */ - if (i < t-1) { - d = syn[2*i+2]; - for (j = 1; j <= elp->deg; j++) - d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); - } - } - dbg("elp=%s\n", gf_poly_str(elp)); - return (elp->deg > t) ? -1 : (int)elp->deg; -} - -/* - * solve a m x m linear system in GF(2) with an expected number of solutions, - * and return the number of found solutions - */ -static int solve_linear_system(struct bch_control *bch, unsigned int *rows, - unsigned int *sol, int nsol) -{ - const int m = GF_M(bch); - unsigned int tmp, mask; - int rem, c, r, p, k, param[m]; - - k = 0; - mask = 1 << m; - - /* Gaussian elimination */ - for (c = 0; c < m; c++) { - rem = 0; - p = c-k; - /* find suitable row for elimination */ - for (r = p; r < m; r++) { - if (rows[r] & mask) { - if (r != p) { - tmp = rows[r]; - rows[r] = rows[p]; - rows[p] = tmp; - } - rem = r+1; - break; - } - } - if (rem) { - /* perform elimination on remaining rows */ - tmp = rows[p]; - for (r = rem; r < m; r++) { - if (rows[r] & mask) - rows[r] ^= tmp; - } - } else { - /* elimination not needed, store defective row index */ - param[k++] = c; - } - mask >>= 1; - } - /* rewrite system, inserting fake parameter rows */ - if (k > 0) { - p = k; - for (r = m-1; r >= 0; r--) { - if ((r > m-1-k) && rows[r]) - /* system has no solution */ - return 0; - - rows[r] = (p && (r == param[p-1])) ? - p--, 1u << (m-r) : rows[r-p]; - } - } - - if (nsol != (1 << k)) - /* unexpected number of solutions */ - return 0; - - for (p = 0; p < nsol; p++) { - /* set parameters for p-th solution */ - for (c = 0; c < k; c++) - rows[param[c]] = (rows[param[c]] & ~1)|((p >> c) & 1); - - /* compute unique solution */ - tmp = 0; - for (r = m-1; r >= 0; r--) { - mask = rows[r] & (tmp|1); - tmp |= parity(mask) << (m-r); - } - sol[p] = tmp >> 1; - } - return nsol; -} - -/* - * this function builds and solves a linear system for finding roots of a degree - * 4 affine monic polynomial X^4+aX^2+bX+c over GF(2^m). - */ -static int find_affine4_roots(struct bch_control *bch, unsigned int a, - unsigned int b, unsigned int c, - unsigned int *roots) -{ - int i, j, k; - const int m = GF_M(bch); - unsigned int mask = 0xff, t, rows[16] = {0,}; - - j = a_log(bch, b); - k = a_log(bch, a); - rows[0] = c; - - /* buid linear system to solve X^4+aX^2+bX+c = 0 */ - for (i = 0; i < m; i++) { - rows[i+1] = bch->a_pow_tab[4*i]^ - (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ - (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); - j++; - k += 2; - } - /* - * transpose 16x16 matrix before passing it to linear solver - * warning: this code assumes m < 16 - */ - for (j = 8; j != 0; j >>= 1, mask ^= (mask << j)) { - for (k = 0; k < 16; k = (k+j+1) & ~j) { - t = ((rows[k] >> j)^rows[k+j]) & mask; - rows[k] ^= (t << j); - rows[k+j] ^= t; - } - } - return solve_linear_system(bch, rows, roots, 4); -} - -/* - * compute root r of a degree 1 polynomial over GF(2^m) (returned as log(1/r)) - */ -static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, - unsigned int *roots) -{ - int n = 0; - - if (poly->c[0]) - /* poly[X] = bX+c with c!=0, root=c/b */ - roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ - bch->a_log_tab[poly->c[1]]); - return n; -} - -/* - * compute roots of a degree 2 polynomial over GF(2^m) - */ -static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, - unsigned int *roots) -{ - int n = 0, i, l0, l1, l2; - unsigned int u, v, r; - - if (poly->c[0] && poly->c[1]) { - - l0 = bch->a_log_tab[poly->c[0]]; - l1 = bch->a_log_tab[poly->c[1]]; - l2 = bch->a_log_tab[poly->c[2]]; - - /* using z=a/bX, transform aX^2+bX+c into z^2+z+u (u=ac/b^2) */ - u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); - /* - * let u = sum(li.a^i) i=0..m-1; then compute r = sum(li.xi): - * r^2+r = sum(li.(xi^2+xi)) = sum(li.(a^i+Tr(a^i).a^k)) = - * u + sum(li.Tr(a^i).a^k) = u+a^k.Tr(sum(li.a^i)) = u+a^k.Tr(u) - * i.e. r and r+1 are roots iff Tr(u)=0 - */ - r = 0; - v = u; - while (v) { - i = deg(v); - r ^= bch->xi_tab[i]; - v ^= (1 << i); - } - /* verify root */ - if ((gf_sqr(bch, r)^r) == u) { - /* reverse z=a/bX transformation and compute log(1/r) */ - roots[n++] = modulo(bch, 2*GF_N(bch)-l1- - bch->a_log_tab[r]+l2); - roots[n++] = modulo(bch, 2*GF_N(bch)-l1- - bch->a_log_tab[r^1]+l2); - } - } - return n; -} - -/* - * compute roots of a degree 3 polynomial over GF(2^m) - */ -static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, - unsigned int *roots) -{ - int i, n = 0; - unsigned int a, b, c, a2, b2, c2, e3, tmp[4]; - - if (poly->c[0]) { - /* transform polynomial into monic X^3 + a2X^2 + b2X + c2 */ - e3 = poly->c[3]; - c2 = gf_div(bch, poly->c[0], e3); - b2 = gf_div(bch, poly->c[1], e3); - a2 = gf_div(bch, poly->c[2], e3); - - /* (X+a2)(X^3+a2X^2+b2X+c2) = X^4+aX^2+bX+c (affine) */ - c = gf_mul(bch, a2, c2); /* c = a2c2 */ - b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ - a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ - - /* find the 4 roots of this affine polynomial */ - if (find_affine4_roots(bch, a, b, c, tmp) == 4) { - /* remove a2 from final list of roots */ - for (i = 0; i < 4; i++) { - if (tmp[i] != a2) - roots[n++] = a_ilog(bch, tmp[i]); - } - } - } - return n; -} - -/* - * compute roots of a degree 4 polynomial over GF(2^m) - */ -static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, - unsigned int *roots) -{ - int i, l, n = 0; - unsigned int a, b, c, d, e = 0, f, a2, b2, c2, e4; - - if (poly->c[0] == 0) - return 0; - - /* transform polynomial into monic X^4 + aX^3 + bX^2 + cX + d */ - e4 = poly->c[4]; - d = gf_div(bch, poly->c[0], e4); - c = gf_div(bch, poly->c[1], e4); - b = gf_div(bch, poly->c[2], e4); - a = gf_div(bch, poly->c[3], e4); - - /* use Y=1/X transformation to get an affine polynomial */ - if (a) { - /* first, eliminate cX by using z=X+e with ae^2+c=0 */ - if (c) { - /* compute e such that e^2 = c/a */ - f = gf_div(bch, c, a); - l = a_log(bch, f); - l += (l & 1) ? GF_N(bch) : 0; - e = a_pow(bch, l/2); - /* - * use transformation z=X+e: - * z^4+e^4 + a(z^3+ez^2+e^2z+e^3) + b(z^2+e^2) +cz+ce+d - * z^4 + az^3 + (ae+b)z^2 + (ae^2+c)z+e^4+be^2+ae^3+ce+d - * z^4 + az^3 + (ae+b)z^2 + e^4+be^2+d - * z^4 + az^3 + b'z^2 + d' - */ - d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; - b = gf_mul(bch, a, e)^b; - } - /* now, use Y=1/X to get Y^4 + b/dY^2 + a/dY + 1/d */ - if (d == 0) - /* assume all roots have multiplicity 1 */ - return 0; - - c2 = gf_inv(bch, d); - b2 = gf_div(bch, a, d); - a2 = gf_div(bch, b, d); - } else { - /* polynomial is already affine */ - c2 = d; - b2 = c; - a2 = b; - } - /* find the 4 roots of this affine polynomial */ - if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { - for (i = 0; i < 4; i++) { - /* post-process roots (reverse transformations) */ - f = a ? gf_inv(bch, roots[i]) : roots[i]; - roots[i] = a_ilog(bch, f^e); - } - n = 4; - } - return n; -} - -/* - * build monic, log-based representation of a polynomial - */ -static void gf_poly_logrep(struct bch_control *bch, - const struct gf_poly *a, int *rep) -{ - int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); - - /* represent 0 values with -1; warning, rep[d] is not set to 1 */ - for (i = 0; i < d; i++) - rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; -} - -/* - * compute polynomial Euclidean division remainder in GF(2^m)[X] - */ -static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, - const struct gf_poly *b, int *rep) -{ - int la, p, m; - unsigned int i, j, *c = a->c; - const unsigned int d = b->deg; - - if (a->deg < d) - return; - - /* reuse or compute log representation of denominator */ - if (!rep) { - rep = bch->cache; - gf_poly_logrep(bch, b, rep); - } - - for (j = a->deg; j >= d; j--) { - if (c[j]) { - la = a_log(bch, c[j]); - p = j-d; - for (i = 0; i < d; i++, p++) { - m = rep[i]; - if (m >= 0) - c[p] ^= bch->a_pow_tab[mod_s(bch, - m+la)]; - } - } - } - a->deg = d-1; - while (!c[a->deg] && a->deg) - a->deg--; -} - -/* - * compute polynomial Euclidean division quotient in GF(2^m)[X] - */ -static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, - const struct gf_poly *b, struct gf_poly *q) -{ - if (a->deg >= b->deg) { - q->deg = a->deg-b->deg; - /* compute a mod b (modifies a) */ - gf_poly_mod(bch, a, b, NULL); - /* quotient is stored in upper part of polynomial a */ - memcpy(q->c, &a->c[b->deg], (1+q->deg)*sizeof(unsigned int)); - } else { - q->deg = 0; - q->c[0] = 0; - } -} - -/* - * compute polynomial GCD (Greatest Common Divisor) in GF(2^m)[X] - */ -static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, - struct gf_poly *b) -{ - struct gf_poly *tmp; - - dbg("gcd(%s,%s)=", gf_poly_str(a), gf_poly_str(b)); - - if (a->deg < b->deg) { - tmp = b; - b = a; - a = tmp; - } - - while (b->deg > 0) { - gf_poly_mod(bch, a, b, NULL); - tmp = b; - b = a; - a = tmp; - } - - dbg("%s\n", gf_poly_str(a)); - - return a; -} - -/* - * Given a polynomial f and an integer k, compute Tr(a^kX) mod f - * This is used in Berlekamp Trace algorithm for splitting polynomials - */ -static void compute_trace_bk_mod(struct bch_control *bch, int k, - const struct gf_poly *f, struct gf_poly *z, - struct gf_poly *out) -{ - const int m = GF_M(bch); - int i, j; - - /* z contains z^2j mod f */ - z->deg = 1; - z->c[0] = 0; - z->c[1] = bch->a_pow_tab[k]; - - out->deg = 0; - memset(out, 0, GF_POLY_SZ(f->deg)); - - /* compute f log representation only once */ - gf_poly_logrep(bch, f, bch->cache); - - for (i = 0; i < m; i++) { - /* add a^(k*2^i)(z^(2^i) mod f) and compute (z^(2^i) mod f)^2 */ - for (j = z->deg; j >= 0; j--) { - out->c[j] ^= z->c[j]; - z->c[2*j] = gf_sqr(bch, z->c[j]); - z->c[2*j+1] = 0; - } - if (z->deg > out->deg) - out->deg = z->deg; - - if (i < m-1) { - z->deg *= 2; - /* z^(2(i+1)) mod f = (z^(2^i) mod f)^2 mod f */ - gf_poly_mod(bch, z, f, bch->cache); - } - } - while (!out->c[out->deg] && out->deg) - out->deg--; - - dbg("Tr(a^%d.X) mod f = %s\n", k, gf_poly_str(out)); -} - -/* - * factor a polynomial using Berlekamp Trace algorithm (BTA) - */ -static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, - struct gf_poly **g, struct gf_poly **h) -{ - struct gf_poly *f2 = bch->poly_2t[0]; - struct gf_poly *q = bch->poly_2t[1]; - struct gf_poly *tk = bch->poly_2t[2]; - struct gf_poly *z = bch->poly_2t[3]; - struct gf_poly *gcd; - - dbg("factoring %s...\n", gf_poly_str(f)); - - *g = f; - *h = NULL; - - /* tk = Tr(a^k.X) mod f */ - compute_trace_bk_mod(bch, k, f, z, tk); - - if (tk->deg > 0) { - /* compute g = gcd(f, tk) (destructive operation) */ - gf_poly_copy(f2, f); - gcd = gf_poly_gcd(bch, f2, tk); - if (gcd->deg < f->deg) { - /* compute h=f/gcd(f,tk); this will modify f and q */ - gf_poly_div(bch, f, gcd, q); - /* store g and h in-place (clobbering f) */ - *h = &((struct gf_poly_deg1 *)f)[gcd->deg].poly; - gf_poly_copy(*g, gcd); - gf_poly_copy(*h, q); - } - } -} - -/* - * find roots of a polynomial, using BTZ algorithm; see the beginning of this - * file for details - */ -static int find_poly_roots(struct bch_control *bch, unsigned int k, - struct gf_poly *poly, unsigned int *roots) -{ - int cnt; - struct gf_poly *f1, *f2; - - switch (poly->deg) { - /* handle low degree polynomials with ad hoc techniques */ - case 1: - cnt = find_poly_deg1_roots(bch, poly, roots); - break; - case 2: - cnt = find_poly_deg2_roots(bch, poly, roots); - break; - case 3: - cnt = find_poly_deg3_roots(bch, poly, roots); - break; - case 4: - cnt = find_poly_deg4_roots(bch, poly, roots); - break; - default: - /* factor polynomial using Berlekamp Trace Algorithm (BTA) */ - cnt = 0; - if (poly->deg && (k <= GF_M(bch))) { - factor_polynomial(bch, k, poly, &f1, &f2); - if (f1) - cnt += find_poly_roots(bch, k+1, f1, roots); - if (f2) - cnt += find_poly_roots(bch, k+1, f2, roots+cnt); - } - break; - } - return cnt; -} - -#if defined(USE_CHIEN_SEARCH) -/* - * exhaustive root search (Chien) implementation - not used, included only for - * reference/comparison tests - */ -static int chien_search(struct bch_control *bch, unsigned int len, - struct gf_poly *p, unsigned int *roots) -{ - int m; - unsigned int i, j, syn, syn0, count = 0; - const unsigned int k = 8*len+bch->ecc_bits; - - /* use a log-based representation of polynomial */ - gf_poly_logrep(bch, p, bch->cache); - bch->cache[p->deg] = 0; - syn0 = gf_div(bch, p->c[0], p->c[p->deg]); - - for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { - /* compute elp(a^i) */ - for (j = 1, syn = syn0; j <= p->deg; j++) { - m = bch->cache[j]; - if (m >= 0) - syn ^= a_pow(bch, m+j*i); - } - if (syn == 0) { - roots[count++] = GF_N(bch)-i; - if (count == p->deg) - break; - } - } - return (count == p->deg) ? count : 0; -} -#define find_poly_roots(_p, _k, _elp, _loc) chien_search(_p, len, _elp, _loc) -#endif /* USE_CHIEN_SEARCH */ - -/** - * decode_bch - decode received codeword and find bit error locations - * @bch: BCH control structure - * @data: received data, ignored if @calc_ecc is provided - * @len: data length in bytes, must always be provided - * @recv_ecc: received ecc, if NULL then assume it was XORed in @calc_ecc - * @calc_ecc: calculated ecc, if NULL then calc_ecc is computed from @data - * @syn: hw computed syndrome data (if NULL, syndrome is calculated) - * @errloc: output array of error locations - * - * Returns: - * The number of errors found, or -EBADMSG if decoding failed, or -EINVAL if - * invalid parameters were provided - * - * Depending on the available hw BCH support and the need to compute @calc_ecc - * separately (using encode_bch()), this function should be called with one of - * the following parameter configurations - - * - * by providing @data and @recv_ecc only: - * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc) - * - * by providing @recv_ecc and @calc_ecc: - * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc) - * - * by providing ecc = recv_ecc XOR calc_ecc: - * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc) - * - * by providing syndrome results @syn: - * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc) - * - * Once decode_bch() has successfully returned with a positive value, error - * locations returned in array @errloc should be interpreted as follows - - * - * if (errloc[n] >= 8*len), then n-th error is located in ecc (no need for - * data correction) - * - * if (errloc[n] < 8*len), then n-th error is located in data and can be - * corrected with statement data[errloc[n]/8] ^= 1 << (errloc[n] % 8); - * - * Note that this function does not perform any data correction by itself, it - * merely indicates error locations. - */ -static int decode_bch(struct bch_control *bch, - const uint8_t *data, unsigned int len, - const uint8_t *recv_ecc, const uint8_t *calc_ecc, - const unsigned int *syn, unsigned int *errloc) -{ - const unsigned int ecc_words = BCH_ECC_WORDS(bch); - unsigned int nbits; - int i, err, nroots; - uint32_t sum; - - /* sanity check: make sure data length can be handled */ - if (8*len > (bch->n-bch->ecc_bits)) - return -EINVAL; - - /* if caller does not provide syndromes, compute them */ - if (!syn) { - if (!calc_ecc) { - /* compute received data ecc into an internal buffer */ - if (!data || !recv_ecc) - return -EINVAL; - encode_bch(bch, data, len, NULL); - } else { - /* load provided calculated ecc */ - load_ecc8(bch, bch->ecc_buf, calc_ecc); - } - /* load received ecc or assume it was XORed in calc_ecc */ - if (recv_ecc) { - load_ecc8(bch, bch->ecc_buf2, recv_ecc); - /* XOR received and calculated ecc */ - for (i = 0, sum = 0; i < (int)ecc_words; i++) { - bch->ecc_buf[i] ^= bch->ecc_buf2[i]; - sum |= bch->ecc_buf[i]; - } - if (!sum) - /* no error found */ - return 0; - } - compute_syndromes(bch, bch->ecc_buf, bch->syn); - syn = bch->syn; - } - - err = compute_error_locator_polynomial(bch, syn); - if (err > 0) { - nroots = find_poly_roots(bch, 1, bch->elp, errloc); - if (err != nroots) - err = -1; - } - if (err > 0) { - /* post-process raw error locations for easier correction */ - nbits = (len*8)+bch->ecc_bits; - for (i = 0; i < err; i++) { - if (errloc[i] >= nbits) { - err = -1; - break; - } - errloc[i] = nbits-1-errloc[i]; - errloc[i] = (errloc[i] & ~7)|(7-(errloc[i] & 7)); - } - } - return (err >= 0) ? err : -EBADMSG; -} - -/* - * generate Galois field lookup tables - */ -static int build_gf_tables(struct bch_control *bch, unsigned int poly) -{ - unsigned int i, x = 1; - const unsigned int k = 1 << deg(poly); - - /* primitive polynomial must be of degree m */ - if (k != (1u << GF_M(bch))) - return -1; - - for (i = 0; i < GF_N(bch); i++) { - bch->a_pow_tab[i] = x; - bch->a_log_tab[x] = i; - if (i && (x == 1)) - /* polynomial is not primitive (a^i=1 with 0a_pow_tab[GF_N(bch)] = 1; - bch->a_log_tab[0] = 0; - - return 0; -} - -/* - * compute generator polynomial remainder tables for fast encoding - */ -static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) -{ - int i, j, b, d; - uint32_t data, hi, lo, *tab; - const int l = BCH_ECC_WORDS(bch); - const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); - const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); - - memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); - - for (i = 0; i < 256; i++) { - /* p(X)=i is a small polynomial of weight <= 8 */ - for (b = 0; b < 4; b++) { - /* we want to compute (p(X).X^(8*b+deg(g))) mod g(X) */ - tab = bch->mod8_tab + (b*256+i)*l; - data = i << (8*b); - while (data) { - d = deg(data); - /* subtract X^d.g(X) from p(X).X^(8*b+deg(g)) */ - data ^= g[0] >> (31-d); - for (j = 0; j < ecclen; j++) { - hi = (d < 31) ? g[j] << (d+1) : 0; - lo = (j+1 < plen) ? - g[j+1] >> (31-d) : 0; - tab[j] ^= hi|lo; - } - } - } - } -} - -/* - * build a base for factoring degree 2 polynomials - */ -static int build_deg2_base(struct bch_control *bch) -{ - const int m = GF_M(bch); - int i, j, r; - unsigned int sum, x, y, remaining, ak = 0, xi[m]; - - /* find k s.t. Tr(a^k) = 1 and 0 <= k < m */ - for (i = 0; i < m; i++) { - for (j = 0, sum = 0; j < m; j++) - sum ^= a_pow(bch, i*(1 << j)); - - if (sum) { - ak = bch->a_pow_tab[i]; - break; - } - } - /* find xi, i=0..m-1 such that xi^2+xi = a^i+Tr(a^i).a^k */ - remaining = m; - memset(xi, 0, sizeof(xi)); - - for (x = 0; (x <= GF_N(bch)) && remaining; x++) { - y = gf_sqr(bch, x)^x; - for (i = 0; i < 2; i++) { - r = a_log(bch, y); - if (y && (r < m) && !xi[r]) { - bch->xi_tab[r] = x; - xi[r] = 1; - remaining--; - dbg("x%d = %x\n", r, x); - break; - } - y ^= ak; - } - } - /* should not happen but check anyway */ - return remaining ? -1 : 0; -} - -static void *bch_alloc(size_t size, int *err) -{ - void *ptr; - - ptr = malloc(size); - if (ptr == NULL) - *err = 1; - return ptr; -} - -/* - * compute generator polynomial for given (m,t) parameters. - */ -static uint32_t *compute_generator_polynomial(struct bch_control *bch) -{ - const unsigned int m = GF_M(bch); - const unsigned int t = GF_T(bch); - int n, err = 0; - unsigned int i, j, nbits, r, word, *roots; - struct gf_poly *g; - uint32_t *genpoly; - - g = bch_alloc(GF_POLY_SZ(m*t), &err); - roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); - genpoly = bch_alloc(DIV_ROUND_UP(m*t+1, 32)*sizeof(*genpoly), &err); - - if (err) { - kfree(genpoly); - genpoly = NULL; - goto finish; - } - - /* enumerate all roots of g(X) */ - memset(roots , 0, (bch->n+1)*sizeof(*roots)); - for (i = 0; i < t; i++) { - for (j = 0, r = 2*i+1; j < m; j++) { - roots[r] = 1; - r = mod_s(bch, 2*r); - } - } - /* build generator polynomial g(X) */ - g->deg = 0; - g->c[0] = 1; - for (i = 0; i < GF_N(bch); i++) { - if (roots[i]) { - /* multiply g(X) by (X+root) */ - r = bch->a_pow_tab[i]; - g->c[g->deg+1] = 1; - for (j = g->deg; j > 0; j--) - g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; - - g->c[0] = gf_mul(bch, g->c[0], r); - g->deg++; - } - } - /* store left-justified binary representation of g(X) */ - n = g->deg+1; - i = 0; - - while (n > 0) { - nbits = (n > 32) ? 32 : n; - for (j = 0, word = 0; j < nbits; j++) { - if (g->c[n-1-j]) - word |= 1u << (31-j); - } - genpoly[i++] = word; - n -= nbits; - } - bch->ecc_bits = g->deg; - -finish: - kfree(g); - kfree(roots); - - return genpoly; -} - -/** - * free_bch - free the BCH control structure - * @bch: BCH control structure to release - */ -static void free_bch(struct bch_control *bch) -{ - unsigned int i; - - if (bch) { - kfree(bch->a_pow_tab); - kfree(bch->a_log_tab); - kfree(bch->mod8_tab); - kfree(bch->ecc_buf); - kfree(bch->ecc_buf2); - kfree(bch->xi_tab); - kfree(bch->syn); - kfree(bch->cache); - kfree(bch->elp); - - for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) - kfree(bch->poly_2t[i]); - - kfree(bch); - } -} - -/** - * init_bch - initialize a BCH encoder/decoder - * @m: Galois field order, should be in the range 5-15 - * @t: maximum error correction capability, in bits - * @prim_poly: user-provided primitive polynomial (or 0 to use default) - * - * Returns: - * a newly allocated BCH control structure if successful, NULL otherwise - * - * This initialization can take some time, as lookup tables are built for fast - * encoding/decoding; make sure not to call this function from a time critical - * path. Usually, init_bch() should be called on module/driver init and - * free_bch() should be called to release memory on exit. - * - * You may provide your own primitive polynomial of degree @m in argument - * @prim_poly, or let init_bch() use its default polynomial. - * - * Once init_bch() has successfully returned a pointer to a newly allocated - * BCH control structure, ecc length in bytes is given by member @ecc_bytes of - * the structure. - */ -static struct bch_control *init_bch(int m, int t, unsigned int prim_poly) -{ - int err = 0; - unsigned int i, words; - uint32_t *genpoly; - struct bch_control *bch = NULL; - - const int min_m = 5; - const int max_m = 15; - - /* default primitive polynomials */ - static const unsigned int prim_poly_tab[] = { - 0x25, 0x43, 0x83, 0x11d, 0x211, 0x409, 0x805, 0x1053, 0x201b, - 0x402b, 0x8003, - }; - -#if defined(CONFIG_BCH_CONST_PARAMS) - if ((m != (CONFIG_BCH_CONST_M)) || (t != (CONFIG_BCH_CONST_T))) { - printk(KERN_ERR "bch encoder/decoder was configured to support " - "parameters m=%d, t=%d only!\n", - CONFIG_BCH_CONST_M, CONFIG_BCH_CONST_T); - goto fail; - } -#endif - if ((m < min_m) || (m > max_m)) - /* - * values of m greater than 15 are not currently supported; - * supporting m > 15 would require changing table base type - * (uint16_t) and a small patch in matrix transposition - */ - goto fail; - - /* sanity checks */ - if ((t < 1) || (m*t >= ((1 << m)-1))) - /* invalid t value */ - goto fail; - - /* select a primitive polynomial for generating GF(2^m) */ - if (prim_poly == 0) - prim_poly = prim_poly_tab[m-min_m]; - - bch = malloc(sizeof(*bch)); - if (bch == NULL) - goto fail; - - memset(bch, 0, sizeof(*bch)); - - bch->m = m; - bch->t = t; - bch->n = (1 << m)-1; - words = DIV_ROUND_UP(m*t, 32); - bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); - bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); - bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); - bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); - bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); - bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); - bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); - bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); - bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); - bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); - - for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) - bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); - - if (err) - goto fail; - - err = build_gf_tables(bch, prim_poly); - if (err) - goto fail; - - /* use generator polynomial for computing encoding tables */ - genpoly = compute_generator_polynomial(bch); - if (genpoly == NULL) - goto fail; - - build_mod8_tables(bch, genpoly); - kfree(genpoly); - - err = build_deg2_base(bch); - if (err) - goto fail; - - return bch; - -fail: - free_bch(bch); - return NULL; -} - -static void swap_bits(uint8_t *buf, int len) -{ - uint8_t res = 0; - int i, j; - - for (j = 0; j < len; j++) { - uint8_t byte = buf[j]; - - buf[j] = 0; - for (i = 0; i < 8; i++) { - if (byte & (1 << i)) - buf[j] |= (1 << (7 - i)); - } - } -} - -static uint16_t lfsr_step(uint16_t state, int count) -{ - state &= 0x7fff; - while (count--) - state = ((state >> 1) | - ((((state >> 0) ^ (state >> 1)) & 1) << 14)) & 0x7fff; - - return state; -} - -static uint16_t default_scrambler_seeds[] = { - 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72, - 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436, - 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d, - 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130, - 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56, - 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55, - 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb, - 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17, - 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62, - 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064, - 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126, - 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e, - 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3, - 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b, - 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d, - 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db, -}; - -static uint16_t brom_scrambler_seeds[] = { 0x4a80 }; - -static void scramble(const struct image_info *info, - int page, uint8_t *data, int datalen) -{ - int seedidx = page % ARRAY_SIZE(default_scrambler_seeds); - uint16_t state; - int i; - - /* First pass: scramble data with the BROM seed */ - state = lfsr_step(brom_scrambler_seeds[0], 15); - for (i = 0; i < datalen; i++) { - uint8_t old = data[i]; - data[i] ^= state; - state = lfsr_step(state, 8); - } - - if (info->disable_scrambler) - return; - - /* - * Second pass: scramble data with the default seed to - * to revert the default randomization used by Linux. - */ - state = lfsr_step(default_scrambler_seeds[seedidx], 15); - for (i = 0; i < datalen; i++) { - data[i] ^= state; - state = lfsr_step(state, 8); - } -} - -static int create_image(const struct image_info *info) -{ - struct bch_control *bch; - uint8_t *data, *ecc, *buffer; - int eccbytes = DIV_ROUND_UP(info->ecc_strength * 14, 8); - int pagepadsize = info->page_size - info->usable_page_size; - int data_steps = info->usable_page_size / info->ecc_step_size; - int usable_page_size = data_steps * - (info->ecc_step_size + 4 + eccbytes); - int page = 0, page_offset = 0; - FILE *src, *dst, *rnd; - int repeat = info->repeat; - - bch = init_bch(14, info->ecc_strength, BCH_PRIMITIVE_POLY); - if (!bch) { - printf("Failed to init the BCH engine\n"); - return -1; - } - - buffer = malloc(info->page_size + info->oob_size); - if (!buffer) { - printf("Failed to allocate the NAND page buffer\n"); - return -1; - } - - memset(buffer, 0xff, info->page_size + info->oob_size); - - src = fopen(info->source, "r"); - if (!src) { - printf("Failed to open source file (%s)\n", info->source); - return -1; - } - - dst = fopen(info->dest, "w"); - if (!dst) { - printf("Failed to open dest file (%s)\n", info->dest); - return -1; - } - - rnd = fopen("/dev/urandom", "r"); - if (!rnd) { - printf("Failed to open /dev/urandom\n"); - return -1; - } - - if (usable_page_size < info->page_size + info->oob_size) { - fread(buffer + usable_page_size, 1, - info->page_size + info->oob_size - usable_page_size, - rnd); - memset(buffer + info->page_size, 0xff, 2); - } - - while (1) { - size_t cnt, srcpadsize; - - cnt = info->ecc_step_size; - - data = buffer + page_offset; - ecc = buffer + page_offset + info->ecc_step_size + 4; - cnt = fread(data, 1, info->ecc_step_size, src); - if (!cnt && !feof(src)) { - printf("Failed to read data from the source\n"); - return -1; - } - - /* - * If page_offset != 0 continue (fill the rest of the page with - * random data) to make sure the entire page has valid ecc data - */ - if (page_offset == 0 && cnt == 0) { - if (!repeat--) - break; - - memset(buffer, 0xff, info->page_size + info->oob_size); - while (page % 0x40) { - fwrite(buffer, - info->page_size + info->oob_size, - 1, dst); - page++; - } - - page_offset = 0; - fseek(src, 0, SEEK_SET); - continue; - } - - srcpadsize = info->ecc_step_size - cnt; - if (srcpadsize) - fread(data + cnt, 1, srcpadsize, rnd); - - memset(ecc, 0, eccbytes); - swap_bits(data, info->ecc_step_size + 4); - encode_bch(bch, data, info->ecc_step_size + 4, ecc); - swap_bits(data, info->ecc_step_size + 4); - swap_bits(ecc, eccbytes); - scramble(info, page, data, info->ecc_step_size + 4 + eccbytes); - - page_offset += info->ecc_step_size + 4 + eccbytes; - if (page_offset >= usable_page_size) { - fwrite(buffer, info->page_size + info->oob_size, 1, dst); - memset(buffer, 0xff, info->page_size + info->oob_size); - page++; - page_offset = 0; - if (usable_page_size < info->page_size + info->oob_size) { - fread(buffer + usable_page_size, 1, - info->page_size + info->oob_size - - usable_page_size, - rnd); - memset(buffer + info->page_size, 0xff, 2); - } - } - } - - return 0; -} - -static void display_help(int status) -{ - fprintf(status == EXIT_SUCCESS ? stdout : stderr, -"Usage: spl-image-builder [OPTIONS] source-image output-image\n" -"Creates an image fitting the BROM requirements (randomizer and ECC config).\n" -"\n" -"-h --help Display this help and exit\n" -"-s strength --ecc-strength=size ECC strength\n" -"-c size --ecc-step-size=size ECC step size\n" -"-p size --page-size=size Page size\n" -"-o size --oob-size=size OOB size\n" -"-u size --usable-page-size=size Usable page size\n" -"-r num --repeat=num Number of redundant SPL images\n" -"-d --disable-scrambler Disable the second scrambling pass\n" -"\n"); - exit(status); -} - -int main(int argc, char **argv) -{ - struct image_info info; - - memset(&info, 0, sizeof(info)); - /* - * Process user arguments - */ - for (;;) { - int option_index = 0; - static const char *short_options = "jNqu"; - static const struct option long_options[] = { - {"help", no_argument, 0, 0}, - {"ecc-strength", required_argument, 0, 's'}, - {"ecc-step-size", required_argument, 0, 'c'}, - {"page-size", required_argument, 0, 'p'}, - {"oob-size", required_argument, 0, 'o'}, - {"usable-page-size", required_argument, 0, 'u'}, - {"repeat", required_argument, 0, 'r'}, - {"disable-scrambler", no_argument, 0, 'd'}, - {0, 0, 0, 0}, - }; - - int c = getopt_long(argc, argv, "s:c:p:o:u:r:d", - long_options, &option_index); - if (c == EOF) - break; - - switch (c) { - case 'h': - display_help(0); - break; - case 's': - info.ecc_strength = strtol(optarg, NULL, 0); - break; - case 'c': - info.ecc_step_size = strtol(optarg, NULL, 0); - break; - case 'p': - info.page_size = strtol(optarg, NULL, 0); - break; - case 'o': - info.oob_size = strtol(optarg, NULL, 0); - break; - case 'u': - info.usable_page_size = strtol(optarg, NULL, 0); - break; - case 'r': - info.repeat = strtol(optarg, NULL, 0); - if (info.repeat > 7) - info.repeat = 7; - break; - case 'd': - info.disable_scrambler = 1; - break; - case '?': - display_help(-1); - break; - } - } - - if ((argc - optind) != 2) - display_help(-1); - - info.source = argv[optind]; - info.dest = argv[optind + 1]; - - return create_image(&info); -} From 33b4c3a9b3fbc9c4a8aff57b4f1279c59db87d5f Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 13 Jun 2016 19:20:12 -0700 Subject: [PATCH 14/50] Push update-firmware to legacy-update to make way for new flashing protocol. --- chip-legacy-update.sh | 159 ++++++++++++++++++++++++++++++++++++++++ chip-update-firmware.sh | 80 ++++++++------------ 2 files changed, 190 insertions(+), 49 deletions(-) create mode 100755 chip-legacy-update.sh diff --git a/chip-legacy-update.sh b/chip-legacy-update.sh new file mode 100755 index 0000000..d6f2c64 --- /dev/null +++ b/chip-legacy-update.sh @@ -0,0 +1,159 @@ +#!/bin/bash + +SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source $SCRIPTDIR/common.sh + +if ! wait_for_fel; then + echo "ERROR: please jumper your CHIP in FEL mode then power on" + exit 1 +fi + + +FLASH_SCRIPT=./chip-fel-flash.sh +WHAT=buildroot +BRANCH=stable + +function require_directory { + if [[ ! -d "${1}" ]]; then + mkdir -p "${1}" + fi +} + +function s3_md5 { + local URL=$1 + curl -sLI $URL |grep ETag|sed -e 's/.*"\([a-fA-F0-9]\+\)["-]*.*/\1/;' +} + +function cache_download { + local DEST_DIR=${1} + local SRC_URL=${2} + local FILE=${3} + + 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} + if ! wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}"; then + echo "download of ${SRC_URL}/${FILE} failed!" + exit $? + fi + else + echo "file already downloaded" + fi + else + if ! wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}"; then + echo "download of ${SRC_URL}/${FILE} failed!" + exit $? + fi + fi +} + + +while getopts "ufdpb:w:B:" opt; do + case $opt in + u) + echo "updating cache" + if [[ -d "$FW_IMAGE_DIR" ]]; then + rm -rf $FW_IMAGE_DIR + fi + ;; + f) + echo "fastboot enabled" + FLASH_SCRIPT_OPTION="-f" + ;; + B) + BUILD="$OPTARG" + echo "BUILD = ${BUILD}" + ;; + b) + BRANCH="$OPTARG" + echo "BRANCH = ${BRANCH}" + ;; + w) + WHAT="$OPTARG" + echo "WHAT = ${WHAT}" + ;; + d) + echo "debian selected" + WHAT="debian" + ;; + p) + echo "PocketC.H.I.P selected" + WHAT="pocketchip" + BUILD=123 + FLASH_SCRIPT=./chip-fel-flash.sh -p + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + + +FW_DIR="$(pwd)/.firmware" +FW_IMAGE_DIR="${FW_DIR}/images" +BASE_URL="http://opensource.nextthing.co/chip" +S3_URL="${BASE_URL}/${WHAT}/${BRANCH}/latest" + + + +if [[ -z "$BUILD" ]]; then + 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 +else + ROOTFS_URL="${S3_URL%latest}$BUILD" +fi + +case "${WHAT}" in + "buildroot") + BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" + BUILD=${BR_BUILD} + ROOTFS_URL="${ROOTFS_URL}/images" + BR_URL="${ROOTFS_URL}" + ;; + "debian") + BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/br_build)" + BR_URL="${BASE_URL}/buildroot/${BRANCH%-gui}/${BR_BUILD}/images" + BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" + ;; + "pocketchip") + BR_BUILD=123 + BUILD=123 + ROOTFS_URL="http://opensource.nextthing.co/pocketchip" + BR_URL="$ROOTFS_URL" + ;; +esac + +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} sunxi-spl-with-ecc.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} || echo "ERROR: could not flash" && exit 1 + +#if ! wait_for_linuxboot; then +# echo "ERROR: could not flash" +# exit 1 +#else +# ${SCRIPTDIR}/verify.sh +#fi + +exit $? diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index d6f2c64..f71b2d8 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -103,57 +103,39 @@ S3_URL="${BASE_URL}/${WHAT}/${BRANCH}/latest" -if [[ -z "$BUILD" ]]; then - 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 [[ ! -z "$BUILD" ]]; then + case "${WHAT}" in + "buildroot") + if [[ "$BUILD" -lt "74" ]] && [[ "${BRANCH}" == "stable" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + if [[ "$BUILD" -lt "60" ]] && [[ "${BRANCH}" == "next" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + ;; + "debian") + if [[ "$BUILD" -lt "47" ]] && [[ "${BRANCH}" == "stable" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + if [[ "$BUILD" -lt "148" ]] && [[ "${BRANCH}" == "next" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + if [[ "$BUILD" -lt "4" ]] && [[ "${BRANCH}" == "stable-gui" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + if [[ "$BUILD" -lt "148" ]] && [[ "${BRANCH}" == "next-gui" ]]; then + ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 + exit 0 + fi + ;; + esac else ROOTFS_URL="${S3_URL%latest}$BUILD" fi -case "${WHAT}" in - "buildroot") - BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" - BUILD=${BR_BUILD} - ROOTFS_URL="${ROOTFS_URL}/images" - BR_URL="${ROOTFS_URL}" - ;; - "debian") - BR_BUILD="$(wget -q -O- ${ROOTFS_URL}/br_build)" - BR_URL="${BASE_URL}/buildroot/${BRANCH%-gui}/${BR_BUILD}/images" - BUILD="$(wget -q -O- ${ROOTFS_URL}/build)" - ;; - "pocketchip") - BR_BUILD=123 - BUILD=123 - ROOTFS_URL="http://opensource.nextthing.co/pocketchip" - BR_URL="$ROOTFS_URL" - ;; -esac - -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} sunxi-spl-with-ecc.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} || echo "ERROR: could not flash" && exit 1 - -#if ! wait_for_linuxboot; then -# echo "ERROR: could not flash" -# exit 1 -#else -# ${SCRIPTDIR}/verify.sh -#fi - exit $? From 8cf52b335a24da08cae18e1bd4b20a4d74a304d6 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 23 Jun 2016 19:44:39 -0700 Subject: [PATCH 15/50] Make le better, fix le problems --- chip-update-firmware.sh | 264 +++++++++++++++++++++++----------------- 1 file changed, 153 insertions(+), 111 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index f71b2d8..91a7d9a 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -1,92 +1,43 @@ #!/bin/bash +set -x + SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh -if ! wait_for_fel; then - echo "ERROR: please jumper your CHIP in FEL mode then power on" - exit 1 -fi +BUILDROOT_OUTPUT_DIR=".new/firmware" +FIRMWARE_DIR=".new/firmware" +DL_URL="http://opensource.nextthing.co/chip/images" -FLASH_SCRIPT=./chip-fel-flash.sh -WHAT=buildroot +METHOD=fel +FLAVOR=buildroot BRANCH=stable +DL_DIR=".dl" -function require_directory { - if [[ ! -d "${1}" ]]; then - mkdir -p "${1}" - fi -} - -function s3_md5 { - local URL=$1 - curl -sLI $URL |grep ETag|sed -e 's/.*"\([a-fA-F0-9]\+\)["-]*.*/\1/;' -} - -function cache_download { - local DEST_DIR=${1} - local SRC_URL=${2} - local FILE=${3} - - 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} - if ! wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}"; then - echo "download of ${SRC_URL}/${FILE} failed!" - exit $? - fi - else - echo "file already downloaded" - fi - else - if ! wget -P "${FW_IMAGE_DIR}" "${SRC_URL}/${FILE}"; then - echo "download of ${SRC_URL}/${FILE} failed!" - exit $? - fi - fi -} - - -while getopts "ufdpb:w:B:" opt; do +while getopts "fsdpb:" opt; do case $opt in - u) - echo "updating cache" - if [[ -d "$FW_IMAGE_DIR" ]]; then - rm -rf $FW_IMAGE_DIR - fi - ;; f) echo "fastboot enabled" - FLASH_SCRIPT_OPTION="-f" + METHOD=fb ;; - B) - BUILD="$OPTARG" - echo "BUILD = ${BUILD}" - ;; - b) - BRANCH="$OPTARG" - echo "BRANCH = ${BRANCH}" - ;; - w) - WHAT="$OPTARG" - echo "WHAT = ${WHAT}" + s) + echo "server selected" + FLAVOR=serv ;; d) - echo "debian selected" - WHAT="debian" + echo "desktop selected" + METHOD=fb ##must fastboot + FLAVOR=desk ;; p) - echo "PocketC.H.I.P selected" - WHAT="pocketchip" - BUILD=123 - FLASH_SCRIPT=./chip-fel-flash.sh -p + echo "pocketchip selected" + METHOD=fb ##must fastboot + FLAVOR=pocket + ;; + b) + echo "${BRANCH} branch selected" + BRANCH="$OPTARG" ;; \?) echo "Invalid option: -$OPTARG" >&2 @@ -95,47 +46,138 @@ while getopts "ufdpb:w:B:" opt; do esac done +function require_directory { + if [[ ! -d "${1}" ]]; then + mkdir -p "${1}" + fi +} -FW_DIR="$(pwd)/.firmware" -FW_IMAGE_DIR="${FW_DIR}/images" -BASE_URL="http://opensource.nextthing.co/chip" -S3_URL="${BASE_URL}/${WHAT}/${BRANCH}/latest" +function dl_check { + CACHENUM=$(curl $DL_URL/$BRANCH/$FLAVOR/latest) + + if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz" ]]; then + echo "New image available" + + rm -rf $BRANCH-$FLAVOR-$METHOD* + + echo "Downloading.." + pushd $DL_DIR + if ! wget -O $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz\ + $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/img-$FLAVOR-$METHOD.tar.gz; then + echo "download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed!" + exit $? + fi + + echo "Extracting.." + tar -xf $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz + mv img-* $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} + + echo "Staging for flashing" + cp -R $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}/images ../$FIRMWARE_DIR/ + popd + else + pushd $DL_DIR + echo "Cached files located" + echo "Staging for flashing" + cp -R $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}/images ../$FIRMWARE_DIR/ + popd + fi +} + +FEL=fel + +METHOD=${METHOD:-fel} +AFTER_FLASHING=${AFTER_FLASHING:-wait} - -if [[ ! -z "$BUILD" ]]; then - case "${WHAT}" in - "buildroot") - if [[ "$BUILD" -lt "74" ]] && [[ "${BRANCH}" == "stable" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - if [[ "$BUILD" -lt "60" ]] && [[ "${BRANCH}" == "next" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - ;; - "debian") - if [[ "$BUILD" -lt "47" ]] && [[ "${BRANCH}" == "stable" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - if [[ "$BUILD" -lt "148" ]] && [[ "${BRANCH}" == "next" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - if [[ "$BUILD" -lt "4" ]] && [[ "${BRANCH}" == "stable-gui" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - if [[ "$BUILD" -lt "148" ]] && [[ "${BRANCH}" == "next-gui" ]]; then - ./chip-legacy-update.sh $@ || echo "ERROR: could not flash" && exit 1 - exit 0 - fi - ;; - esac -else - ROOTFS_URL="${S3_URL%latest}$BUILD" +NAND_ERASE_BB=false +if [ "$1" == "erase-bb" ]; then + NAND_ERASE_BB=true fi -exit $? +PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin +PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" +PADDED_SPL_SIZE=0 +UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" +UBOOT_SCRIPT_MEM_ADDR=0x43100000 +SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" +SPL_MEM_ADDR=0x43000000 +UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" +PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" +UBOOT_MEM_ADDR=0x4a000000 +UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" +UBI_MEM_ADDR=0x4b000000 + +assert_error() { + ERR=$? + ERRCODE=$1 + if [ "${ERR}" != "0" ]; then + if [ -z "${ERR}" ]; then + exit ${ERR} + else + exit ${ERRCODE} + fi + fi +} + +echo == preparing images == +require_directory "$FIRMWARE_DIR" +require_directory "$DL_DIR" +dl_check + +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}" +assert_error 128 + +sleep 1 # wait for DRAM initialization to complete + +echo == upload spl == +${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) +assert_error 129 + +echo == upload u-boot == +${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) +assert_error 130 + +echo == upload u-boot script == +${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) +assert_error 131 + +if [[ "${METHOD}" == "fel" ]]; then + echo == upload ubi == + ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" + + 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 + assert_error 132 + + echo == waiting for fastboot == + if wait_for_fastboot; then + fastboot -i 0x1f3a -u flash UBI $UBI + assert_error 134 + + fastboot -i 0x1f3a continue + assert_error 135 + else + exit 1 + fi +fi + +if [[ "${METHOD}" == "fel" ]]; then + if ! wait_for_linuxboot; then + echo "ERROR: could not flash": + exit 1 + else + ${SCRIPTDIR}/verify.sh + fi +fi + + From dcbd2ff5509702f47c1321c11d15891d8bd43fba Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 11 Jul 2016 17:26:14 -0700 Subject: [PATCH 16/50] Add option to flash from local download --- chip-update-firmware.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 91a7d9a..062a2f7 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -14,8 +14,9 @@ METHOD=fel FLAVOR=buildroot BRANCH=stable DL_DIR=".dl" +NO_LIM="lim" -while getopts "fsdpb:" opt; do +while getopts "fsdpb:l:" opt; do case $opt in f) echo "fastboot enabled" @@ -36,8 +37,14 @@ while getopts "fsdpb:" opt; do FLAVOR=pocket ;; b) - echo "${BRANCH} branch selected" BRANCH="$OPTARG" + echo "${BRANCH} branch selected" + ;; + l) + LOC=1 + BUILDROOT_OUTPUT_DIR="$OPTARG" + FIRMWARE_DIR="$OPTARG" + echo "${BUILDROOT_OUTPUT_DIR} directory selected" ;; \?) echo "Invalid option: -$OPTARG" >&2 @@ -120,10 +127,14 @@ assert_error() { fi } -echo == preparing images == -require_directory "$FIRMWARE_DIR" -require_directory "$DL_DIR" -dl_check +if [[ -z $LOC ]]; then + + echo == preparing images == + require_directory "$FIRMWARE_DIR" + require_directory "$DL_DIR" + dl_check + +fi echo == upload the SPL to SRAM and execute it == if ! wait_for_fel; then From d1d15e8655b3da3eb4fae612b3122afd53153366 Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 24 Aug 2016 17:34:50 -0700 Subject: [PATCH 17/50] Fix pocketchip option --- chip-local-flash.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 796d0af..9171acb 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -76,7 +76,7 @@ function dl_check { rm -rf img-$DL_FLAVOR-$DL_METHOD* if [[ "${DL_FLAVOR}" == "pocket" || "${DL_FLAVOR}" == "pocket-next" ]]; then - aws s3 cp s3://opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz . || exit 1 + aws s3 cp s3://opensource.nextthing.co/chippian/rootfs/img-$DL_FLAVOR-$DL_METHOD.tar.gz . || exit 1 else wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 fi From b41c88a4941130c171995b0553b909835b30ee40 Mon Sep 17 00:00:00 2001 From: computermouth Date: Sun, 11 Sep 2016 03:16:08 -0700 Subject: [PATCH 18/50] Chat/ppp verification and fancy completion message --- chip-fel-flash.sh | 3 + chip-update-firmware.sh | 4 +- common.sh | 21 +++++- verify.sh | 155 ++++++++++++---------------------------- verify.sh.chat | 8 +++ 5 files changed, 78 insertions(+), 113 deletions(-) create mode 100644 verify.sh.chat diff --git a/chip-fel-flash.sh b/chip-fel-flash.sh index 0896fbd..e11a631 100755 --- a/chip-fel-flash.sh +++ b/chip-fel-flash.sh @@ -218,3 +218,6 @@ if [[ "${METHOD}" == "fel" ]]; then fi fi rm -rf "${TMPDIR}" + +ready_to_roll + diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 062a2f7..a6d5fbb 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -1,7 +1,5 @@ #!/bin/bash -set -x - SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh @@ -191,4 +189,4 @@ if [[ "${METHOD}" == "fel" ]]; then fi fi - +ready_to_roll diff --git a/common.sh b/common.sh index b619f54..7b1eca7 100755 --- a/common.sh +++ b/common.sh @@ -58,7 +58,8 @@ wait_for_linuxboot() { local TIMEOUT=100 echo -n "flashing..."; for ((i=$TIMEOUT; i>0; i--)) { - if lsusb |grep -q "0525:a4a7"; then + if lsusb |grep -q "0525:a4a7" || + lsusb |grep -q "0525:a4aa"; then echo "OK" return 0; fi @@ -70,3 +71,21 @@ wait_for_linuxboot() { return 1 } +ready_to_roll() { + + echo -e "\n\nFLASH VERIFICATION COMPLETE.\n\n" + + echo " # # #" + echo " #########" + echo "### ###" + echo " # {#} #" + echo "### '\######" + echo " # #" + echo "### ###" + echo " ########" + echo " # # #" + + echo -e "\n\nCHIP is ready to roll!\n\n" + + return 0 +} diff --git a/verify.sh b/verify.sh index 2a918c0..38934ef 100755 --- a/verify.sh +++ b/verify.sh @@ -1,118 +1,55 @@ -#!/usr/bin/env python +#!/bin/bash -import io -import sys -import serial -import re -import time - -#------------------------------------------------------------------ -def answer_prompt(sio,prompt_to_wait_for,answer_to_write,send_cr=True): -#------------------------------------------------------------------ - sio.flush() - prompt_found = False - data = '' - #if send_cr: - #sio.write(unicode('\n')) - - d='something' - while not len(d)==0: - d = sio.read(2000); - data += d - time.sleep(1) -# print '-' * 50 -# print ' %d bytes read' % (len(data)) -# print '-' * 50 - - #print data - - line='' - while not prompt_found: - d = sio.read(100); - data += d -# 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 - prompt_found = True - else: - if send_cr: - sio.write(unicode('\n')) - sio.flush() - #sys.stdin.readline() - -#------------------------------------------------------------------ -def scanfor(sio,regexp_to_scan_for,answer_to_write): -#------------------------------------------------------------------ - prompt_found = False - data = '' - while not prompt_found: - data += sio.read(100); -# 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 - sio.write(unicode(answer_to_write+'\n')) - prompt_found = True - sio.flush() - return data +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPT_NAME="$(basename $0)" +CHAT_SCRIPT="${SCRIPT_DIR}/${SCRIPT_NAME}.chat" +CHAT_BIN="$(which chat)" +CHAT_BIN="${CHAT_BIN:-/usr/sbin/chat}" -#------------------------------------------------------------------ -def main(): -#------------------------------------------------------------------ +UART_DEVICE=ttyACM0 +GETTY_UART_SERVICE="serial-getty@${UART_DEVICE}.service" +GETTY_DISABLED=0 - if( len(sys.argv)>1 ): - serial_port=sys.argv[1] - else: - serial_port='/dev/ttyACM0'; +export TIMEOUT=3 - #print 'reading from %s:' % serial_port +#echo "DUT_UART_RUN=$DUT_UART_RUN" - ser = serial.Serial(serial_port,115200, timeout=1); - sio = io.TextIOWrapper(io.BufferedRWPair(ser,ser)) - - #login - - 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" - return 0 +while getopts "t:" opt; do + case $opt in + t) + TIMEOUT="${OPTARG}" + echo "timeout set to ${TIMEOUT}" + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done +shift $((OPTIND-1)) +export DUT_UART_PARAMETER="$@" +if [[ "$(systemctl is-active $GETTY_UART_SERVICE)" == "active" ]]; then + echo "stopping $GETTY_UART_SERVICE" + systemctl stop $GETTY_UART_SERVICE + GETTY_DISABLED=1 +fi -#------------------------------------------------------------------ -if __name__ == "__main__": -#------------------------------------------------------------------ - exit( main() ) +[[ -r "${CHAT_SCRIPT}" ]] || (echo "ERROR: can not read ${CHAT_SCRIPT}" && exit 1) +[[ -r "${CHAT_BIN}" ]] || (echo -e "ERROR: ${CHAT_BIN} not found\n -- 'sudo apt-get install ppp'" && exit 1) + +for i in `seq 1 3`; +do + echo -e "Waiting for serial gadget...Attempt(${i}/3)" + /usr/sbin/chat -t $TIMEOUT -E -V -f "${CHAT_SCRIPT}" /dev/${UART_DEVICE}\ + && break\ + || (echo -e "ERROR: failed to verify\n" && exit 1) +done +echo "SUCCESS: CHIP is powering down" + +if [[ ${GETTY_DISABLED} == 1 ]]; then + echo "starting $GETTY_UART_SERVICE" + systemctl start $GETTY_UART_SERVICE +fi diff --git a/verify.sh.chat b/verify.sh.chat new file mode 100644 index 0000000..73c9f67 --- /dev/null +++ b/verify.sh.chat @@ -0,0 +1,8 @@ +SAY "\n [ACM] Trying to login..." +TIMEOUT 40 +'' \n ogin:-\n-login: root\n assword: chip\n '# ' '' +SAY "\n [ACM] Successfully logged in" +TIMEOUT 40 +'# ' '/sbin/poweroff' +SAY "\n [ACM] Powering off..." +TIMEOUT 40 From c9d4953c9bdc52a6afa562bd8efe3c0eddd1798b Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 24 Oct 2016 15:14:00 -0700 Subject: [PATCH 19/50] Remove aws creds requirement for pocketchip --- chip-local-flash.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/chip-local-flash.sh b/chip-local-flash.sh index 9171acb..b179a46 100755 --- a/chip-local-flash.sh +++ b/chip-local-flash.sh @@ -4,8 +4,6 @@ set -x SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh -source $SCRIPTDIR/env.sh -export AWS_DEFAULT_REGION=us-west-2 BUILDROOT_OUTPUT_DIR=".new/firmware" FIRMWARE_DIR=".new/firmware" @@ -76,7 +74,7 @@ function dl_check { rm -rf img-$DL_FLAVOR-$DL_METHOD* if [[ "${DL_FLAVOR}" == "pocket" || "${DL_FLAVOR}" == "pocket-next" ]]; then - aws s3 cp s3://opensource.nextthing.co/chippian/rootfs/img-$DL_FLAVOR-$DL_METHOD.tar.gz . || exit 1 + wget opensource.nextthing.co/chippian/rootfs/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 else wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 fi From fd388104fb716916f9aa0fccc3d873a657776283 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 27 Oct 2016 15:40:12 -0700 Subject: [PATCH 20/50] Initial commit --- chip-TOSH4GB-flash.sh | 144 ------------------------- chip-create-nand-images.sh | 137 ++++++++++++++++++++++++ chip-flash-nand-images.sh | 11 ++ chip-local-flash.sh | 204 ----------------------------------- chip-update-firmware.sh | 211 +++++++++++++------------------------ common.sh | 119 ++++++++++++++++++++- 6 files changed, 337 insertions(+), 489 deletions(-) delete mode 100755 chip-TOSH4GB-flash.sh create mode 100755 chip-create-nand-images.sh create mode 100755 chip-flash-nand-images.sh delete mode 100755 chip-local-flash.sh diff --git a/chip-TOSH4GB-flash.sh b/chip-TOSH4GB-flash.sh deleted file mode 100755 index 9cc5b3c..0000000 --- a/chip-TOSH4GB-flash.sh +++ /dev/null @@ -1,144 +0,0 @@ -#!/bin/bash - -set -x - -SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) -source $SCRIPTDIR/common.sh - -############################################################## -# main -############################################################## -while getopts "flpu:" opt; do - case $opt in - f) - echo "fastboot enabled" - METHOD=fastboot - ;; - l) - echo "factory mode remain in u-boot after flashing" - AFTER_FLASHING=loop - ;; - u) - BUILDROOT_OUTPUT_DIR="${OPTARG}" - ;; - p) - POCKET_CHIP=true - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - exit 1 - ;; - esac -done - -echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" - -FEL=fel - -METHOD=${METHOD:-fel} -AFTER_FLASHING=${AFTER_FLASHING:-wait} - - -NAND_ERASE_BB=false -if [ "$1" == "erase-bb" ]; then - NAND_ERASE_BB=true -fi - -PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin -TMPDIR=`mktemp -d -t chipflashXXXXXX` -PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" -PADDED_SPL_SIZE=0 -UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" -UBOOT_SCRIPT_MEM_ADDR=0x43100000 -UBOOT_SCRIPT_SRC="$TMPDIR/uboot.cmds" -SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" -SPL_MEM_ADDR=0x43000000 -UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" -PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" -PADDED_UBOOT_SIZE=0x400000 -UBOOT_MEM_ADDR=0x4a000000 -UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" -SPARSE_UBI="${TMPDIR}/rootfs.ubi.sparse" -UBI_MEM_ADDR=0x4b000000 - -UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` -PAGE_SIZE=16384 -OOB_SIZE=1280 - -assert_error() { - ERR=$? - ERRCODE=$1 - if [ "${ERR}" != "0" ]; then - if [ -z "${ERR}" ]; then - exit ${ERR} - else - exit ${ERRCODE} - fi - fi -} - -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}" -assert_error 128 - -sleep 1 # wait for DRAM initialization to complete - -echo == upload spl == -${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) -assert_error 129 - -echo == upload u-boot == -${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) -assert_error 130 - -echo == upload u-boot script == -${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) -assert_error 131 - -if [[ "${METHOD}" == "fel" ]]; then - echo == upload ubi == - ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" - - 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 - assert_error 132 - - echo == creating sparse image == -# img2simg ${UBI} ${SPARSE_UBI} $((2*1024*1024)) - assert_error 133 - - echo == waiting for fastboot == - if wait_for_fastboot; then - fastboot -i 0x1f3a -u flash UBI $UBI - assert_error 134 - - fastboot -i 0x1f3a continue - assert_error 135 - else - rm -rf "${TMPDIR}" - exit 1 - fi -fi - -if [[ "${METHOD}" == "fel" ]]; then - if ! wait_for_linuxboot; then - echo "ERROR: could not flash": - rm -rf ${TMPDIR} - exit 1 - else - ${SCRIPTDIR}/verify.sh - fi -fi -rm -rf "${TMPDIR}" diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh new file mode 100755 index 0000000..848cf5e --- /dev/null +++ b/chip-create-nand-images.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +FEL=sunxi-fel + +SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source $SCRIPTDIR/common.sh + +UBOOTDIR="$1" +ROOTFSTAR="$2" +OUTPUTDIR="$3" + +# build the UBI image +prepare_ubi() { + local tmpdir=`mktemp -d -t chip-ubi-XXXXXX` + local rootfs=$tmpdir/rootfs + local ubifs=$tmpdir/rootfs.ubifs + local ubicfg=$tmpdir/ubi.cfg + local outputdir="$1" + local rootfstar="$2" + local nandtype="$3" + local maxlebcount="$4" + local eraseblocksize="$5" + local pagesize="$6" + local subpagesize="$7" + local ebsize=`printf %x $eraseblocksize` + local psize=`printf %x $pagesize` + local ubi=$outputdir/chip-$ebsize-$psize.ubi + local sparseubi=$outputdir/chip-$ebsize-$psize.ubi.sparse + local mlcopts="" + + if [ -z $subpagesize ]; then + subpagesize=$pagesize + fi + + if [ "$nandtype" = "mlc" ]; then + lebsize=$((eraseblocksize/2-$pagesize*2)) + mlcopts="-M dist3" + elif [ $subpagesize -lt $pagesize ]; then + lebsize=$((eraseblocksize-pagesize)) + else + lebsize=$((eraseblocksize-pagesize*2)) + fi + + mkdir -p $rootfs + tar -xf $rootfstar -C $rootfs + mkfs.ubifs -d $rootfs -m $pagesize -e $lebsize -c $maxlebcount -o $ubifs + echo "[rootfs] +mode=ubi +vol_id=0 +vol_type=dynamic +vol_name=rootfs +vol_alignment=1 +vol_flags=autoresize +image=$ubifs" > $ubicfg + + ubinize -o $ubi -p $eraseblocksize -m $pagesize -s $subpagesize $mlcopts $ubicfg + img2simg $ubi $sparseubi $eraseblocksize + rm -rf $tmpdir +} + +# build the SPL image +prepare_spl() { + local tmpdir=`mktemp -d -t chip-spl-XXXXXX` + local outputdir=$1 + local spl=$2 + local eraseblocksize=$3 + local pagesize=$4 + local oobsize=$5 + local repeat=$((eraseblocksize/pagesize/64)) + local nandspl=$tmpdir/nand-spl.bin + local nandpaddedspl=$tmpdir/nand-padded-spl.bin + local ebsize=`printf %x $eraseblocksize` + local psize=`printf %x $pagesize` + local osize=`printf %x $oobsize` + local nandrepeatedspl=$outputdir/spl-$ebsize-$psize-$osize.bin + local padding=$tmpdir/padding + local splpadding=$tmpdir/nand-spl-padding + + sunxi-nand-image-builder -c 64/1024 -p $pagesize -o $oobsize -u 1024 -e $eraseblocksize -b -s $spl $nandspl + + local splsize=`filesize $nandspl` + local paddingsize=$((64-(splsize/(pagesize+oobsize)))) + local i=0 + + while [ $i -lt $repeat ]; do + dd if=/dev/urandom of=$padding bs=1024 count=$paddingsize + sunxi-nand-image-builder -c 64/1024 -p $pagesize -o $oobsize -u 1024 -e $eraseblocksize -b -s $padding $splpadding + cat $nandspl $splpadding > $nandpaddedspl + + if [ "$i" -eq "0" ]; then + cat $nandpaddedspl > $nandrepeatedspl + else + cat $nandpaddedspl >> $nandrepeatedspl + fi + + i=$((i+1)) + done + + rm -rf $tmpdir +} + +# build the bootloader image +prepare_uboot() { + local outputdir=$1 + local uboot=$2 + local eraseblocksize=$3 + local ebsize=`printf %x $eraseblocksize` + local paddeduboot=$outputdir/uboot-$ebsize.bin + + dd if=$uboot of=$paddeduboot bs=$eraseblocksize conv=sync +} + +## copy the source images in the output dir ## +mkdir -p $OUTPUTDIR +cp $UBOOTDIR/spl/sunxi-spl.bin $OUTPUTDIR/ +cp $UBOOTDIR/u-boot-dtb.bin $OUTPUTDIR/ +cp $ROOTFSTAR $OUTPUTDIR/ + +## prepare ubi images ## +# Toshiba SLC image: +prepare_ubi $OUTPUTDIR $ROOTFSTAR "slc" 2048 262144 4096 1024 +# Toshiba/Hynix MLC image: +prepare_ubi $OUTPUTDIR $ROOTFSTAR "mlc" 4096 4194304 16384 16384 + +## prepare spl images ## +# Toshiba SLC image: +prepare_spl $OUTPUTDIR $UBOOTDIR/spl/sunxi-spl.bin 262144 4096 256 +# Toshiba MLC image: +prepare_spl $OUTPUTDIR $UBOOTDIR/spl/sunxi-spl.bin 4194304 16384 1280 +# Hynix MLC image: +prepare_spl $OUTPUTDIR $UBOOTDIR/spl/sunxi-spl.bin 4194304 16384 1664 + +## prepare uboot images ## +# Toshiba SLC image: +prepare_uboot $OUTPUTDIR $UBOOTDIR/u-boot-dtb.bin 262144 +# Toshiba/Hynix MLC image: +prepare_uboot $OUTPUTDIR $UBOOTDIR/u-boot-dtb.bin 4194304 diff --git a/chip-flash-nand-images.sh b/chip-flash-nand-images.sh new file mode 100755 index 0000000..9393460 --- /dev/null +++ b/chip-flash-nand-images.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source $SCRIPTDIR/common.sh + +IMAGESDIR="$1" +ERASEMODE="$2" +PLATFORM="$3" + +detect_nand +flash_images diff --git a/chip-local-flash.sh b/chip-local-flash.sh deleted file mode 100755 index b179a46..0000000 --- a/chip-local-flash.sh +++ /dev/null @@ -1,204 +0,0 @@ -#!/bin/bash - -set -x - -SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) -source $SCRIPTDIR/common.sh - -BUILDROOT_OUTPUT_DIR=".new/firmware" -FIRMWARE_DIR=".new/firmware" - -DL_METHOD=fel -METHOD=fel -DL_FLAVOR=serv -DL_DIST=rootfs -DL_DIR=".dl" - -while getopts "fdphnu:" opt; do - case $opt in - f) - echo "fastboot enabled" - DL_METHOD=fb - METHOD=fastboot - ;; - d) - echo "desktop selected" - DL_FLAVOR=desk - ;; - p) - echo "desktop selected" - DL_FLAVOR=pocket - ;; - h) - echo "desktop selected" - DL_FLAVOR=hdmi-fixture-rootfs - ;; - n) - echo "next enabled" - DL_DIST=testing-rootfs - ;; - u) - BUILDROOT_OUTPUT_DIR="${OPTARG}" - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - exit 1 - ;; - esac -done - -if [[ "${DL_FLAVOR}" == "pocket" ]] && [[ "${DL_DIST}" == "testing-rootfs" ]]; then - DL_FLAVOR="pocket-next" -fi - -echo "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR" - -function require_directory { - if [[ ! -d "${1}" ]]; then - mkdir -p "${1}" - fi -} - -function dl_check { - wget -O $DL_DIR/latest.md5\ - opensource.nextthing.co/chippian/$DL_DIST/latest.md5 - - pushd $DL_DIR - if [[ $(cat latest.md5 | grep "`md5sum img-$DL_FLAVOR-$DL_METHOD.tar.gz`")\ - && -d "img-$DL_FLAVOR-$DL_METHOD/images" ]]; then - echo "Cached files located" - echo "Staging for flashing" - cp -R img-$DL_FLAVOR-$DL_METHOD/images ../$FIRMWARE_DIR/ - else - echo "New image available" - rm -rf img-$DL_FLAVOR-$DL_METHOD* - - if [[ "${DL_FLAVOR}" == "pocket" || "${DL_FLAVOR}" == "pocket-next" ]]; then - wget opensource.nextthing.co/chippian/rootfs/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 - else - wget opensource.nextthing.co/chippian/$DL_DIST/img-$DL_FLAVOR-$DL_METHOD.tar.gz|| exit 1 - fi - - echo "Extracting.." - tar -xf img-$DL_FLAVOR-$DL_METHOD.tar.gz - echo "Staging for flashing" - cp -R img-$DL_FLAVOR-$DL_METHOD/images ../$FIRMWARE_DIR/ - fi - popd -} - - -require_directory "$FIRMWARE_DIR" -require_directory "$DL_DIR" - -dl_check - -FEL=fel - -METHOD=${METHOD:-fel} -AFTER_FLASHING=${AFTER_FLASHING:-wait} - - -NAND_ERASE_BB=false -if [ "$1" == "erase-bb" ]; then - NAND_ERASE_BB=true -fi - -PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin -TMPDIR=`mktemp -d -t chipflashXXXXXX` -PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" -PADDED_SPL_SIZE=0 -UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" -UBOOT_SCRIPT_MEM_ADDR=0x43100000 -UBOOT_SCRIPT_SRC="$TMPDIR/uboot.cmds" -SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" -SPL_MEM_ADDR=0x43000000 -UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" -PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" -PADDED_UBOOT_SIZE=0x400000 -UBOOT_MEM_ADDR=0x4a000000 -UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" -SPARSE_UBI="${TMPDIR}/rootfs.ubi.sparse" -UBI_MEM_ADDR=0x4b000000 - -UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"` -PAGE_SIZE=16384 -OOB_SIZE=1280 - -assert_error() { - ERR=$? - ERRCODE=$1 - if [ "${ERR}" != "0" ]; then - if [ -z "${ERR}" ]; then - exit ${ERR} - else - exit ${ERRCODE} - fi - fi -} - -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}" -assert_error 128 - -sleep 1 # wait for DRAM initialization to complete - -echo == upload spl == -${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) -assert_error 129 - -echo == upload u-boot == -${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) -assert_error 130 - -echo == upload u-boot script == -${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) -assert_error 131 - -if [[ "${METHOD}" == "fel" ]]; then - echo == upload ubi == - ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" - - 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 - assert_error 132 - - echo == creating sparse image == -# img2simg ${UBI} ${SPARSE_UBI} $((2*1024*1024)) - assert_error 133 - - echo == waiting for fastboot == - if wait_for_fastboot; then - fastboot -i 0x1f3a -u flash UBI $UBI - assert_error 134 - - fastboot -i 0x1f3a continue - assert_error 135 - else - rm -rf "${TMPDIR}" - exit 1 - fi -fi - -if [[ "${METHOD}" == "fel" ]]; then - if ! wait_for_linuxboot; then - echo "ERROR: could not flash": - rm -rf ${TMPDIR} - exit 1 - else - ${SCRIPTDIR}/verify.sh - fi -fi -rm -rf "${TMPDIR}" diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index a6d5fbb..c90847d 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -3,46 +3,60 @@ SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh -BUILDROOT_OUTPUT_DIR=".new/firmware" -FIRMWARE_DIR=".new/firmware" +DL_DIR=".dl" +IMAGESDIR=".new/firmware/images" DL_URL="http://opensource.nextthing.co/chip/images" -METHOD=fel -FLAVOR=buildroot +FLAVOR=server BRANCH=stable -DL_DIR=".dl" -NO_LIM="lim" -while getopts "fsdpb:l:" opt; do +PROBES=(spl-40000-1000-100.bin + spl-400000-4000-500.bin + spl-400000-4000-680.bin + sunxi-spl.bin + u-boot-dtb.bin + uboot-40000.bin + uboot-400000.bin) + +UBI_PREFIX="chip" +UBI_SUFFIX="ubi.sparse" +UBI_TYPE="400000-4000" + +while getopts "sgpbhB:" opt; do case $opt in - f) - echo "fastboot enabled" - METHOD=fb - ;; s) echo "server selected" - FLAVOR=serv + FLAVOR=server ;; - d) - echo "desktop selected" - METHOD=fb ##must fastboot - FLAVOR=desk + g) + echo "gui selected" + FLAVOR=gui ;; p) echo "pocketchip selected" - METHOD=fb ##must fastboot - FLAVOR=pocket + FLAVOR=pocketchip ;; b) + echo "buildroot selected" + FLAVOR=buildroot + ;; + B) BRANCH="$OPTARG" echo "${BRANCH} branch selected" ;; - l) - LOC=1 - BUILDROOT_OUTPUT_DIR="$OPTARG" - FIRMWARE_DIR="$OPTARG" - echo "${BUILDROOT_OUTPUT_DIR} directory selected" + h) + echo "" + echo "Help" + echo "" + echo " -s -- Server [Debian + Headless]" + echo " -g -- GUI [Debian + XFCE]" + echo " -p -- PocketCHIP" + echo " -b -- Buildroot" + echo " -B -- Branch(optional) [eg. -B testing]" + echo "" + echo "" + exit 0 ;; \?) echo "Invalid option: -$OPTARG" >&2 @@ -57,136 +71,55 @@ function require_directory { fi } -function dl_check { +function dl_probe { + CACHENUM=$(curl $DL_URL/$BRANCH/$FLAVOR/latest) - if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz" ]]; then + if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then echo "New image available" - rm -rf $BRANCH-$FLAVOR-$METHOD* + rm -rf $DL_DIR/$BRANCH-$FLAVOR* + + mkdir -p $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} echo "Downloading.." - pushd $DL_DIR - if ! wget -O $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz\ - $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/img-$FLAVOR-$METHOD.tar.gz; then + for FILE in ${PROBES[@]}; do + if ! wget $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$FILE; then + echo "download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed!" + exit $? + fi + done + popd + else + echo "Cached probe files located" + fi + + echo "Staging for NAND probe" + ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR + rm ${IMAGESDIR}/ubi_type + detect_nand + + if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then + echo "Downloading new UBI, this will be cached for future flashes." + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} + if ! wget $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then echo "download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed!" exit $? fi - - echo "Extracting.." - tar -xf $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}.tar.gz - mv img-* $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} - - echo "Staging for flashing" - cp -R $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}/images ../$FIRMWARE_DIR/ popd else - pushd $DL_DIR - echo "Cached files located" - echo "Staging for flashing" - cp -R $BRANCH-$FLAVOR-$METHOD-b${CACHENUM}/images ../$FIRMWARE_DIR/ - popd + echo "Cached UBI located" fi } -FEL=fel +echo == preparing images == +require_directory "$IMAGESDIR" +rm -rf ${IMAGESDIR} +require_directory "$DL_DIR" +dl_probe -METHOD=${METHOD:-fel} -AFTER_FLASHING=${AFTER_FLASHING:-wait} - - -NAND_ERASE_BB=false -if [ "$1" == "erase-bb" ]; then - NAND_ERASE_BB=true -fi - -PATH=$PATH:$BUILDROOT_OUTPUT_DIR/host/usr/bin -PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin" -PADDED_SPL_SIZE=0 -UBOOT_SCRIPT="${BUILDROOT_OUTPUT_DIR}/images/uboot.scr" -UBOOT_SCRIPT_MEM_ADDR=0x43100000 -SPL="$BUILDROOT_OUTPUT_DIR/images/sunxi-spl.bin" -SPL_MEM_ADDR=0x43000000 -UBOOT="$BUILDROOT_OUTPUT_DIR/images/u-boot-dtb.bin" -PADDED_UBOOT="$BUILDROOT_OUTPUT_DIR/images/padded-u-boot" -UBOOT_MEM_ADDR=0x4a000000 -UBI="$BUILDROOT_OUTPUT_DIR/images/rootfs.ubi" -UBI_MEM_ADDR=0x4b000000 - -assert_error() { - ERR=$? - ERRCODE=$1 - if [ "${ERR}" != "0" ]; then - if [ -z "${ERR}" ]; then - exit ${ERR} - else - exit ${ERRCODE} - fi - fi -} - -if [[ -z $LOC ]]; then - - echo == preparing images == - require_directory "$FIRMWARE_DIR" - require_directory "$DL_DIR" - dl_check - -fi - -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}" -assert_error 128 - -sleep 1 # wait for DRAM initialization to complete - -echo == upload spl == -${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" || ( echo "ERROR: could not write ${PADDED_SPL}" && exit $? ) -assert_error 129 - -echo == upload u-boot == -${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" || ( echo "ERROR: could not write ${PADDED_UBOOT}" && exit $? ) -assert_error 130 - -echo == upload u-boot script == -${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? ) -assert_error 131 - -if [[ "${METHOD}" == "fel" ]]; then - echo == upload ubi == - ${FEL} --progress write $UBI_MEM_ADDR "${UBI}" - - 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 - assert_error 132 - - echo == waiting for fastboot == - if wait_for_fastboot; then - fastboot -i 0x1f3a -u flash UBI $UBI - assert_error 134 - - fastboot -i 0x1f3a continue - assert_error 135 - else - exit 1 - fi -fi - -if [[ "${METHOD}" == "fel" ]]; then - if ! wait_for_linuxboot; then - echo "ERROR: could not flash": - exit 1 - else - ${SCRIPTDIR}/verify.sh - fi -fi +flash_images +rm ${IMAGESDIR}/ubi_type ready_to_roll diff --git a/common.sh b/common.sh index 7b1eca7..c6c4387 100755 --- a/common.sh +++ b/common.sh @@ -1,7 +1,21 @@ #!/bin/bash TIMEOUT=30 -FEL=fel +FEL=sunxi-fel +SPLMEMADDR=0x43000000 +UBOOTMEMADDR=0x4a000000 +UBOOTSCRMEMADDR=0x43100000 +nand_erasesize=400000 +nand_writesize=4000 +nand_oobsize=680 + +if [[ -z $(which $FEL) ]]; then + echo " Error: Unable to locate FEL utility." + echo " Install FEL with:" + echo " CHIP-SDK setup script [github.com/NextThingCo/CHIP-SDK]" + echo " - or build from source [github.com/NextThingCo/sunxi-tools]" + exit 1 +fi #------------------------------------------------------------ onMac() { @@ -53,6 +67,107 @@ wait_for_fel() { return 1 } +detect_nand() { + local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` + local ubootcmds=$tmpdir/uboot.cmds + local ubootscr=$tmpdir/uboot.scr + + echo "nand info +env export -t -s 0x100 0x7c00 nand_erasesize nand_writesize nand_oobsize +reset" > $ubootcmds + mkimage -A arm -T script -C none -n "detect NAND" -d $ubootcmds $ubootscr + + if ! wait_for_fel; then + echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + fi + + $FEL spl $IMAGESDIR/sunxi-spl.bin + # wait for DRAM initialization to complete + sleep 1 + + $FEL write $UBOOTMEMADDR $IMAGESDIR/u-boot-dtb.bin + $FEL write $UBOOTSCRMEMADDR $ubootscr + $FEL exe $UBOOTMEMADDR + + if ! wait_for_fel; then + echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + fi + + $FEL read 0x7c00 0x100 $tmpdir/nand-info + + echo "NAND detected:" + cat $tmpdir/nand-info + UBI_TYPE="$(cat $tmpdir/nand-info | awk -F= '/erase/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/write/ {print $2}')" + echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type + source $tmpdir/nand-info + + rm -rf $tmpdir +} + +flash_images() { + local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` + local ubootcmds=$tmpdir/uboot.cmds + local ubootscr=$tmpdir/uboot.scr + local ubootsize=`filesize $IMAGESDIR/uboot-$nand_erasesize.bin | xargs printf "0x%08x"` + local pagespereb=`echo $((nand_erasesize/nand_writesize)) | xargs printf "%x"` + local sparseubi=$tmpdir/ubi.sparse + + if [ "x$ERASEMODE" = "xscrub" ]; then + echo "nand scrub.chip -y" > $ubootcmds + else + echo "nand erase.chip" > $ubootcmds + fi + + echo "nand write.raw.noverify $SPLMEMADDR 0x0 $pagespereb" >> $ubootcmds + echo "nand write.raw.noverify $SPLMEMADDR 0x400000 $pagespereb" >> $ubootcmds + echo "nand write $UBOOTMEMADDR 0x800000 $ubootsize" >> $ubootcmds + echo "setenv mtdparts mtdparts=sunxi-nand.0:4m(spl),4m(spl-backup),4m(uboot),4m(env),-(UBI)" >> $ubootcmds + echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw earlyprintk ubi.mtd=4" >> $ubootcmds + echo "setenv bootcmd 'gpio set PB2; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo '(FEL boot)'; source \${scriptaddr}; fi; 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'" >> $ubootcmds + echo "setenv fel_booted 0" >> $ubootcmds + + echo "echo Enabling Splash" >> $ubootcmds + echo "setenv stdout serial" >> $ubootcmds + echo "setenv stderr serial" >> $ubootcmds + echo "setenv splashpos m,m" >> $ubootcmds + + echo "echo Configuring Video Mode" >> $ubootcmds + if [ "$PLATFORM" = "PocketCHIP" ]; then + echo "setenv video-mode" >> $ubootcmds + else + echo "setenv video-mode sunxi:640x480-24@60,monitor=composite-ntsc,overscan_x=40,overscan_y=20" >> $ubootcmds + fi + + echo "saveenv" >> $ubootcmds + + echo "echo going to fastboot mode" >> $ubootcmds + echo "fastboot 0" >> $ubootcmds + echo "reset" >> $ubootcmds + + mkimage -A arm -T script -C none -n "flash $PLATFORM" -d $ubootcmds $ubootscr + + if ! wait_for_fel; then + echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + fi + + $FEL spl $IMAGESDIR/sunxi-spl.bin + # wait for DRAM initialization to complete + sleep 1 + + $FEL write $UBOOTMEMADDR $IMAGESDIR/uboot-$nand_erasesize.bin + $FEL write $SPLMEMADDR $IMAGESDIR/spl-$nand_erasesize-$nand_writesize-$nand_oobsize.bin + $FEL write $UBOOTSCRMEMADDR $ubootscr + $FEL exe $UBOOTMEMADDR + + if wait_for_fastboot; then + fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize.ubi.sparse + else + echo "failed to flash the UBI image" + fi + + rm -rf $tmpdir +} + #------------------------------------------------------------ wait_for_linuxboot() { local TIMEOUT=100 @@ -79,7 +194,7 @@ ready_to_roll() { echo " #########" echo "### ###" echo " # {#} #" - echo "### '\######" + echo "### '%######" echo " # #" echo "### ###" echo " ########" From 43ae8f730c627eb905467aafa3f050856d9c8807 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 27 Oct 2016 16:30:12 -0700 Subject: [PATCH 21/50] Formatting --- chip-update-firmware.sh | 49 ++++++++++++++++++++++------------------- common.sh | 6 +++++ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index c90847d..cbceddd 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -8,6 +8,8 @@ IMAGESDIR=".new/firmware/images" DL_URL="http://opensource.nextthing.co/chip/images" +WGET="wget -q --show-progress" + FLAVOR=server BRANCH=stable @@ -26,28 +28,28 @@ UBI_TYPE="400000-4000" while getopts "sgpbhB:" opt; do case $opt in s) - echo "server selected" + echo "== Server selected ==" FLAVOR=server ;; g) - echo "gui selected" + echo "== Gui selected ==" FLAVOR=gui ;; p) - echo "pocketchip selected" + echo "== Pocketchip selected ==" FLAVOR=pocketchip ;; b) - echo "buildroot selected" + echo "== Buildroot selected ==" FLAVOR=buildroot ;; B) BRANCH="$OPTARG" - echo "${BRANCH} branch selected" + echo "== ${BRANCH} branch selected ==" ;; h) echo "" - echo "Help" + echo "== help ==" echo "" echo " -s -- Server [Debian + Headless]" echo " -g -- GUI [Debian + XFCE]" @@ -59,7 +61,7 @@ while getopts "sgpbhB:" opt; do exit 0 ;; \?) - echo "Invalid option: -$OPTARG" >&2 + echo "== Invalid option: -$OPTARG ==" >&2 exit 1 ;; esac @@ -73,43 +75,44 @@ function require_directory { function dl_probe { - CACHENUM=$(curl $DL_URL/$BRANCH/$FLAVOR/latest) + CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then - echo "New image available" + echo "== New image available ==" rm -rf $DL_DIR/$BRANCH-$FLAVOR* mkdir -p $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} - pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null - echo "Downloading.." + echo "== Downloading.. ==" for FILE in ${PROBES[@]}; do - if ! wget $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$FILE; then - echo "download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed!" + if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$FILE; then + echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" exit $? fi done - popd + popd > /dev/null else - echo "Cached probe files located" + echo "== Cached probe files located ==" fi - echo "Staging for NAND probe" + echo "== Staging for NAND probe ==" ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR - rm ${IMAGESDIR}/ubi_type + if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi + detect_nand if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then - echo "Downloading new UBI, this will be cached for future flashes." - pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} - if ! wget $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then - echo "download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed!" + echo "== Downloading new UBI, this will be cached for future flashes. ==" + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null + if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then + echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" exit $? fi - popd + popd > /dev/null else - echo "Cached UBI located" + echo "== Cached UBI located ==" fi } diff --git a/common.sh b/common.sh index c6c4387..577d898 100755 --- a/common.sh +++ b/common.sh @@ -67,6 +67,7 @@ wait_for_fel() { return 1 } +#------------------------------------------------------------ detect_nand() { local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds @@ -79,6 +80,7 @@ reset" > $ubootcmds if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + exit 1 fi $FEL spl $IMAGESDIR/sunxi-spl.bin @@ -91,6 +93,7 @@ reset" > $ubootcmds if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + exit 1 fi $FEL read 0x7c00 0x100 $tmpdir/nand-info @@ -104,6 +107,7 @@ reset" > $ubootcmds rm -rf $tmpdir } +#------------------------------------------------------------ flash_images() { local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds @@ -148,6 +152,7 @@ flash_images() { if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + exit 1 fi $FEL spl $IMAGESDIR/sunxi-spl.bin @@ -186,6 +191,7 @@ wait_for_linuxboot() { return 1 } +#------------------------------------------------------------ ready_to_roll() { echo -e "\n\nFLASH VERIFICATION COMPLETE.\n\n" From 90b95e6d2e9d82984eaf5a20ad4262b78fb0f155 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 31 Oct 2016 13:29:58 -0700 Subject: [PATCH 22/50] Add build number specifier --- chip-update-firmware.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index cbceddd..4b69dd6 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000" -while getopts "sgpbhB:" opt; do +while getopts "sgpbhB:N:" opt; do case $opt in s) echo "== Server selected ==" @@ -47,6 +47,10 @@ while getopts "sgpbhB:" opt; do BRANCH="$OPTARG" echo "== ${BRANCH} branch selected ==" ;; + N) + CACHENUM="$OPTARG" + echo "== Build number ${CACHENUM} selected ==" + ;; h) echo "" echo "== help ==" @@ -75,8 +79,10 @@ function require_directory { function dl_probe { - CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) - + if [ -z $CACHENUM ]; then + CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) + fi + if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then echo "== New image available ==" From 5d2625e6142ebe7255c39198ac23ba916b7162b8 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 31 Oct 2016 18:10:45 -0700 Subject: [PATCH 23/50] Nand type setting --- chip-update-firmware.sh | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 4b69dd6..5b46e9b 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000" -while getopts "sgpbhB:N:" opt; do +while getopts "sgpbhB:N:F:" opt; do case $opt in s) echo "== Server selected ==" @@ -51,15 +51,21 @@ while getopts "sgpbhB:N:" opt; do CACHENUM="$OPTARG" echo "== Build number ${CACHENUM} selected ==" ;; + F) + FORMAT="$OPTARG" + echo "== Format ${FORMAT} selected ==" + ;; h) echo "" - echo "== help ==" + echo "== Help ==" echo "" echo " -s -- Server [Debian + Headless]" echo " -g -- GUI [Debian + XFCE]" echo " -p -- PocketCHIP" echo " -b -- Buildroot" echo " -B -- Branch(optional) [eg. -B testing]" + echo " -N -- Build#(optional) [eg. -N 150]" + echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" echo "" echo "" exit 0 @@ -107,7 +113,34 @@ function dl_probe { ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi - detect_nand + if [ -z $FORMAT ]; then + detect_nand + else + case $FORMAT in + "Hynix_8G_MLC") + export nand_erasesize=400000 + export nand_oobsize=680 + export nand_writesize=4000 + UBI_TYPE="400000-4000" + ;; + "Toshiba_4G_MLC") + export nand_erasesize=400000 + export nand_oobsize=500 + export nand_writesize=4000 + UBI_TYPE="400000-4000" + ;; + "Toshiba_512M_MLC") + export nand_erasesize=40000 + export nand_oobsize=100 + export nand_writesize=1000 + UBI_TYPE="400000-1000" + ;; + \?) + echo "== Invalid format: $FORMAT ==" >&2 + exit 1 + ;; + esac + fi if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then echo "== Downloading new UBI, this will be cached for future flashes. ==" From 02fee4d567a33d2486dce7df024b6d6fd4cb88f2 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 31 Oct 2016 20:10:12 -0700 Subject: [PATCH 24/50] Load the dips --- common.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common.sh b/common.sh index 577d898..564b742 100755 --- a/common.sh +++ b/common.sh @@ -137,7 +137,16 @@ flash_images() { echo "echo Configuring Video Mode" >> $ubootcmds if [ "$PLATFORM" = "PocketCHIP" ]; then + echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw ubi.mtd=4 quiet lpj=501248 loglevel=3 splash plymouth.ignore-serial-consoles" >> $ubootcmds + echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds + echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds + echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds + echo "setenv boot_noinitrd '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'" >> $ubootcmds echo "setenv video-mode" >> $ubootcmds + echo "setenv dip_addr_r 0x43400000" >> $ubootcmds + echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds + echo "setenv dip_overlay_cmd 'if test -n \"\${dip_overlay_name}\"; then ubifsload \$dip_addr_r \$dip_overlay_dir/\$dip_overlay_name; fi'" >> $ubootcmds + echo "setenv fel_booted 0" >> $ubootcmds else echo "setenv video-mode sunxi:640x480-24@60,monitor=composite-ntsc,overscan_x=40,overscan_y=20" >> $ubootcmds fi From dff8a1fdace0ac604056d1b8ee8361b815bb1f3c Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 2 Nov 2016 14:58:29 -0700 Subject: [PATCH 25/50] Tabs to spaces, proper ubootscr flavor detection --- chip-update-firmware.sh | 137 +++++++++++++++++++++------------------- common.sh | 4 +- 2 files changed, 73 insertions(+), 68 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 5b46e9b..0e1fbde 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000" -while getopts "sgpbhB:N:F:" opt; do +while getopts "sgpbfhB:N:F:" opt; do case $opt in s) echo "== Server selected ==" @@ -43,6 +43,10 @@ while getopts "sgpbhB:N:F:" opt; do echo "== Buildroot selected ==" FLAVOR=buildroot ;; + f) + echo "== Force clean and download ==" + rm -rf .dl/ .new/ + ;; B) BRANCH="$OPTARG" echo "== ${BRANCH} branch selected ==" @@ -63,6 +67,7 @@ while getopts "sgpbhB:N:F:" opt; do echo " -g -- GUI [Debian + XFCE]" echo " -p -- PocketCHIP" echo " -b -- Buildroot" + echo " -f -- Force clean" echo " -B -- Branch(optional) [eg. -B testing]" echo " -N -- Build#(optional) [eg. -N 150]" echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" @@ -85,74 +90,74 @@ function require_directory { function dl_probe { - if [ -z $CACHENUM ]; then - CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) - fi + if [ -z $CACHENUM ]; then + CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) + fi - if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then - echo "== New image available ==" - - rm -rf $DL_DIR/$BRANCH-$FLAVOR* - - mkdir -p $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} - pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null - - echo "== Downloading.. ==" - for FILE in ${PROBES[@]}; do - if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$FILE; then - echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" - exit $? - fi - done - popd > /dev/null - else - echo "== Cached probe files located ==" - fi + if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then + echo "== New image available ==" + + rm -rf $DL_DIR/$BRANCH-$FLAVOR* + + mkdir -p $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null + + echo "== Downloading.. ==" + for FILE in ${PROBES[@]}; do + if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$FILE; then + echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" + exit $? + fi + done + popd > /dev/null + else + echo "== Cached probe files located ==" + fi - echo "== Staging for NAND probe ==" - ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR - if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi + echo "== Staging for NAND probe ==" + ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR + if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi + + if [ -z $FORMAT ]; then + detect_nand + else + case $FORMAT in + "Hynix_8G_MLC") + export nand_erasesize=400000 + export nand_oobsize=680 + export nand_writesize=4000 + UBI_TYPE="400000-4000" + ;; + "Toshiba_4G_MLC") + export nand_erasesize=400000 + export nand_oobsize=500 + export nand_writesize=4000 + UBI_TYPE="400000-4000" + ;; + "Toshiba_512M_MLC") + export nand_erasesize=40000 + export nand_oobsize=100 + export nand_writesize=1000 + UBI_TYPE="400000-1000" + ;; + \?) + echo "== Invalid format: $FORMAT ==" >&2 + exit 1 + ;; + esac + fi - if [ -z $FORMAT ]; then - detect_nand - else - case $FORMAT in - "Hynix_8G_MLC") - export nand_erasesize=400000 - export nand_oobsize=680 - export nand_writesize=4000 - UBI_TYPE="400000-4000" - ;; - "Toshiba_4G_MLC") - export nand_erasesize=400000 - export nand_oobsize=500 - export nand_writesize=4000 - UBI_TYPE="400000-4000" - ;; - "Toshiba_512M_MLC") - export nand_erasesize=40000 - export nand_oobsize=100 - export nand_writesize=1000 - UBI_TYPE="400000-1000" - ;; - \?) - echo "== Invalid format: $FORMAT ==" >&2 - exit 1 - ;; - esac - fi - - if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then - echo "== Downloading new UBI, this will be cached for future flashes. ==" - pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null - if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then - echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" - exit $? - fi - popd > /dev/null - else - echo "== Cached UBI located ==" - fi + if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then + echo "== Downloading new UBI, this will be cached for future flashes. ==" + pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null + if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then + echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" + exit $? + fi + popd > /dev/null + else + echo "== Cached UBI located ==" + fi } echo == preparing images == diff --git a/common.sh b/common.sh index 564b742..bc69469 100755 --- a/common.sh +++ b/common.sh @@ -136,7 +136,7 @@ flash_images() { echo "setenv splashpos m,m" >> $ubootcmds echo "echo Configuring Video Mode" >> $ubootcmds - if [ "$PLATFORM" = "PocketCHIP" ]; then + if [ "$FLAVOR" = "pocketchip" ]; then echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw ubi.mtd=4 quiet lpj=501248 loglevel=3 splash plymouth.ignore-serial-consoles" >> $ubootcmds echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds @@ -157,7 +157,7 @@ flash_images() { echo "fastboot 0" >> $ubootcmds echo "reset" >> $ubootcmds - mkimage -A arm -T script -C none -n "flash $PLATFORM" -d $ubootcmds $ubootscr + mkimage -A arm -T script -C none -n "flash $FLAVOR" -d $ubootcmds $ubootscr if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" From 684e8fc3dee627e7b8620d7d2381e15df84f5c7a Mon Sep 17 00:00:00 2001 From: computermouth Date: Tue, 8 Nov 2016 14:33:10 -0800 Subject: [PATCH 26/50] Fix fel booted pocketchip --- common.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common.sh b/common.sh index bc69469..f7bd282 100755 --- a/common.sh +++ b/common.sh @@ -122,6 +122,7 @@ flash_images() { echo "nand erase.chip" > $ubootcmds fi + echo "env default -a" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x0 $pagespereb" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x400000 $pagespereb" >> $ubootcmds echo "nand write $UBOOTMEMADDR 0x800000 $ubootsize" >> $ubootcmds @@ -137,6 +138,11 @@ flash_images() { echo "echo Configuring Video Mode" >> $ubootcmds if [ "$FLAVOR" = "pocketchip" ]; then + + echo "setenv clear_fastboot 'i2c mw 0x34 0x4 0x00 4;'" >> $ubootcmds + echo "setenv write_fastboot 'i2c mw 0x34 0x4 66 1; i2c mw 0x34 0x5 62 1; i2c mw 0x34 0x6 30 1; i2c mw 0x34 0x7 00 1'" >> $ubootcmds + echo "setenv test_fastboot 'i2c read 0x34 0x4 4 0x80200000; if itest.s *0x80200000 -eq fb0; then echo (Fastboot); i2c mw 0x34 0x4 0x00 4; fastboot 0; fi'" >> $ubootcmds + echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw ubi.mtd=4 quiet lpj=501248 loglevel=3 splash plymouth.ignore-serial-consoles" >> $ubootcmds echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds @@ -147,7 +153,17 @@ flash_images() { echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds echo "setenv dip_overlay_cmd 'if test -n \"\${dip_overlay_name}\"; then ubifsload \$dip_addr_r \$dip_overlay_dir/\$dip_overlay_name; fi'" >> $ubootcmds echo "setenv fel_booted 0" >> $ubootcmds + echo "setenv bootdelay 1" >> "${UBOOT_SCRIPT_SRC}" else + + echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds + echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds + echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds + echo "setenv boot_noinitrd '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'" >> $ubootcmds + echo "setenv dip_addr_r 0x43400000" >> $ubootcmds + echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds + echo "setenv dip_overlay_cmd 'if test -n \"\${dip_overlay_name}\"; then ubifsload \$dip_addr_r \$dip_overlay_dir/\$dip_overlay_name; fi'" >> $ubootcmds + echo "setenv video-mode sunxi:640x480-24@60,monitor=composite-ntsc,overscan_x=40,overscan_y=20" >> $ubootcmds fi From c13c3ad2d0a3a424fa62ecbaf7a5f3c3b895bda2 Mon Sep 17 00:00:00 2001 From: computermouth Date: Tue, 8 Nov 2016 18:15:59 -0800 Subject: [PATCH 27/50] Remove unnecessary defaulting --- common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common.sh b/common.sh index f7bd282..5c96e28 100755 --- a/common.sh +++ b/common.sh @@ -122,7 +122,7 @@ flash_images() { echo "nand erase.chip" > $ubootcmds fi - echo "env default -a" >> $ubootcmds + ## echo "env default -a" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x0 $pagespereb" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x400000 $pagespereb" >> $ubootcmds echo "nand write $UBOOTMEMADDR 0x800000 $ubootsize" >> $ubootcmds From cc0eb2418f5cfc1d140f0c19dcf55c84da76b4e3 Mon Sep 17 00:00:00 2001 From: computermouth Date: Wed, 9 Nov 2016 18:54:53 -0800 Subject: [PATCH 28/50] Remove unnecessary file removal --- chip-update-firmware.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 0e1fbde..e726a2f 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -167,6 +167,5 @@ require_directory "$DL_DIR" dl_probe flash_images -rm ${IMAGESDIR}/ubi_type ready_to_roll From 983a59dd5fe40de0daed44cada048bd01bfae7c7 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 10 Nov 2016 17:35:02 -0800 Subject: [PATCH 29/50] Add no limit option --- chip-update-firmware.sh | 6 +++++- common.sh | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index e726a2f..f80f17f 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000" -while getopts "sgpbfhB:N:F:" opt; do +while getopts "sgpbfnhB:N:F:" opt; do case $opt in s) echo "== Server selected ==" @@ -47,6 +47,10 @@ while getopts "sgpbfhB:N:F:" opt; do echo "== Force clean and download ==" rm -rf .dl/ .new/ ;; + n) + echo "== No Limit mode ==" + NO_LIMIT="while itest.b *0x80400000 -ne 03; do i2c mw 0x34 0x30 0x03; i2c read 0x34 0x30 1 0x80400000; done; " + ;; B) BRANCH="$OPTARG" echo "== ${BRANCH} branch selected ==" diff --git a/common.sh b/common.sh index 5c96e28..9cc80f6 100755 --- a/common.sh +++ b/common.sh @@ -13,7 +13,7 @@ if [[ -z $(which $FEL) ]]; then echo " Error: Unable to locate FEL utility." echo " Install FEL with:" echo " CHIP-SDK setup script [github.com/NextThingCo/CHIP-SDK]" - echo " - or build from source [github.com/NextThingCo/sunxi-tools]" + echo " - or build from source [github.com/linux-sunxi/sunxi-tools]" exit 1 fi @@ -145,7 +145,7 @@ flash_images() { echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw ubi.mtd=4 quiet lpj=501248 loglevel=3 splash plymouth.ignore-serial-consoles" >> $ubootcmds echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds - echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds + echo "setenv bootcmd '${NO_LIMIT}run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds echo "setenv boot_noinitrd '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'" >> $ubootcmds echo "setenv video-mode" >> $ubootcmds @@ -157,7 +157,7 @@ flash_images() { else echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds - echo "setenv bootcmd 'run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds + echo "setenv bootcmd '${NO_LIMIT}run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds echo "setenv boot_noinitrd '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'" >> $ubootcmds echo "setenv dip_addr_r 0x43400000" >> $ubootcmds From 6b331c430652d15982ba16a6bd1ab72bf753ae10 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 28 Nov 2016 15:38:15 -0800 Subject: [PATCH 30/50] Add in error reporting for UBI flash --- chip-update-firmware.sh | 20 ++++++++++++++------ common.sh | 22 +++++++++++++--------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index f80f17f..f33da80 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -93,7 +93,7 @@ function require_directory { } function dl_probe { - + if [ -z $CACHENUM ]; then CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) fi @@ -117,7 +117,7 @@ function dl_probe { else echo "== Cached probe files located ==" fi - + echo "== Staging for NAND probe ==" ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi @@ -150,7 +150,7 @@ function dl_probe { ;; esac fi - + if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then echo "== Downloading new UBI, this will be cached for future flashes. ==" pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null @@ -170,6 +170,14 @@ rm -rf ${IMAGESDIR} require_directory "$DL_DIR" dl_probe -flash_images - -ready_to_roll +##pass +flash_images && ready_to_roll || ( + ##fail + echo -e "\n FLASH VERIFICATION FAILED.\n\n" + echo -e "\tTROUBLESHOOTING:\n" + echo -e "\tIs the FEL pin connected to GND?" + echo -e "\tHave you tried turning it off and turning it on again?" + echo -e "\tDid you run the setup script in CHIP-SDK?" + echo -e "\tDownload could be corrupt, it can be re-downloaded by adding the '-f' flag." + echo -e "\n\n" +) diff --git a/common.sh b/common.sh index 9cc80f6..a6b998b 100755 --- a/common.sh +++ b/common.sh @@ -109,6 +109,8 @@ reset" > $ubootcmds #------------------------------------------------------------ flash_images() { + local RC=0 + local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds local ubootscr=$tmpdir/uboot.scr @@ -122,7 +124,6 @@ flash_images() { echo "nand erase.chip" > $ubootcmds fi - ## echo "env default -a" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x0 $pagespereb" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x400000 $pagespereb" >> $ubootcmds echo "nand write $UBOOTMEMADDR 0x800000 $ubootsize" >> $ubootcmds @@ -173,29 +174,32 @@ flash_images() { echo "fastboot 0" >> $ubootcmds echo "reset" >> $ubootcmds - mkimage -A arm -T script -C none -n "flash $FLAVOR" -d $ubootcmds $ubootscr + mkimage -A arm -T script -C none -n "flash $FLAVOR" -d $ubootcmds $ubootscr || RC=1 if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" - exit 1 + RC=1 fi - $FEL spl $IMAGESDIR/sunxi-spl.bin + $FEL spl $IMAGESDIR/sunxi-spl.bin || RC=1 # wait for DRAM initialization to complete sleep 1 - $FEL write $UBOOTMEMADDR $IMAGESDIR/uboot-$nand_erasesize.bin - $FEL write $SPLMEMADDR $IMAGESDIR/spl-$nand_erasesize-$nand_writesize-$nand_oobsize.bin - $FEL write $UBOOTSCRMEMADDR $ubootscr - $FEL exe $UBOOTMEMADDR + $FEL write $UBOOTMEMADDR $IMAGESDIR/uboot-$nand_erasesize.bin || RC=1 + $FEL write $SPLMEMADDR $IMAGESDIR/spl-$nand_erasesize-$nand_writesize-$nand_oobsize.bin || RC=1 + $FEL write $UBOOTSCRMEMADDR $ubootscr || RC=1 + $FEL exe $UBOOTMEMADDR || RC=1 if wait_for_fastboot; then - fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize.ubi.sparse + fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize.ubi.sparse || RC=1 else echo "failed to flash the UBI image" + RC=1 fi rm -rf $tmpdir + + return $RC } #------------------------------------------------------------ From 7c4258145c669cba6e414494de58608a7ef7e3a1 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 28 Nov 2016 17:08:58 -0800 Subject: [PATCH 31/50] Add errors for detect nand --- chip-update-firmware.sh | 15 +++++++++++++-- common.sh | 23 ++++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index f33da80..0912fe2 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -8,7 +8,7 @@ IMAGESDIR=".new/firmware/images" DL_URL="http://opensource.nextthing.co/chip/images" -WGET="wget -q --show-progress" +WGET="wget -q" FLAVOR=server BRANCH=stable @@ -168,7 +168,18 @@ echo == preparing images == require_directory "$IMAGESDIR" rm -rf ${IMAGESDIR} require_directory "$DL_DIR" -dl_probe + +##pass +dl_probe || ( + ##fail + echo -e "\n FLASH VERIFICATION FAILED.\n\n" + echo -e "\tTROUBLESHOOTING:\n" + echo -e "\tIs the FEL pin connected to GND?" + echo -e "\tHave you tried turning it off and turning it on again?" + echo -e "\tDid you run the setup script in CHIP-SDK?" + echo -e "\tDownload could be corrupt, it can be re-downloaded by adding the '-f' flag." + echo -e "\n\n" +) ##pass flash_images && ready_to_roll || ( diff --git a/common.sh b/common.sh index a6b998b..fe6faf2 100755 --- a/common.sh +++ b/common.sh @@ -69,6 +69,8 @@ wait_for_fel() { #------------------------------------------------------------ detect_nand() { + local RC=0 + local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds local ubootscr=$tmpdir/uboot.scr @@ -76,35 +78,38 @@ detect_nand() { echo "nand info env export -t -s 0x100 0x7c00 nand_erasesize nand_writesize nand_oobsize reset" > $ubootcmds - mkimage -A arm -T script -C none -n "detect NAND" -d $ubootcmds $ubootscr + mkimage -A arm -T script -C none -n "detect NAND" -d $ubootcmds $ubootscr || RC=1 if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" + RC=1 exit 1 fi - $FEL spl $IMAGESDIR/sunxi-spl.bin + $FEL spl $IMAGESDIR/sunxi-spl.bin || RC=1 # wait for DRAM initialization to complete sleep 1 - $FEL write $UBOOTMEMADDR $IMAGESDIR/u-boot-dtb.bin - $FEL write $UBOOTSCRMEMADDR $ubootscr - $FEL exe $UBOOTMEMADDR + $FEL write $UBOOTMEMADDR $IMAGESDIR/u-boot-dtb.bin || RC=1 + $FEL write $UBOOTSCRMEMADDR $ubootscr || RC=1 + $FEL exe $UBOOTMEMADDR || RC=1 if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" exit 1 fi - $FEL read 0x7c00 0x100 $tmpdir/nand-info + $FEL read 0x7c00 0x100 $tmpdir/nand-info || RC=1 echo "NAND detected:" - cat $tmpdir/nand-info + cat $tmpdir/nand-info || RC=1 UBI_TYPE="$(cat $tmpdir/nand-info | awk -F= '/erase/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/write/ {print $2}')" - echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type - source $tmpdir/nand-info + echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type || RC=1 + source $tmpdir/nand-info || RC=1 rm -rf $tmpdir + + return $RC } #------------------------------------------------------------ From 8e170577eb269664640d93ca99d8c9810d2c3baf Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 28 Nov 2016 17:15:19 -0800 Subject: [PATCH 32/50] Get rid of silent update to support ubuntu14.04 --- chip-update-firmware.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 0912fe2..455331f 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -8,7 +8,7 @@ IMAGESDIR=".new/firmware/images" DL_URL="http://opensource.nextthing.co/chip/images" -WGET="wget -q" +WGET="wget" FLAVOR=server BRANCH=stable From aa2d5c1fa8d5136c3f0c0bfd3bda926738b04f84 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 28 Nov 2016 18:17:49 -0800 Subject: [PATCH 33/50] Exit upon probe fail --- chip-update-firmware.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 455331f..e79f32b 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -179,6 +179,7 @@ dl_probe || ( echo -e "\tDid you run the setup script in CHIP-SDK?" echo -e "\tDownload could be corrupt, it can be re-downloaded by adding the '-f' flag." echo -e "\n\n" + exit 1 ) ##pass From 2166a2871ad1fba9f9fc0aaa5b5e5b5698a825d2 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 28 Nov 2016 18:54:48 -0800 Subject: [PATCH 34/50] Fix file descriptor variable --- common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common.sh b/common.sh index fe6faf2..407a584 100755 --- a/common.sh +++ b/common.sh @@ -159,7 +159,7 @@ flash_images() { echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds echo "setenv dip_overlay_cmd 'if test -n \"\${dip_overlay_name}\"; then ubifsload \$dip_addr_r \$dip_overlay_dir/\$dip_overlay_name; fi'" >> $ubootcmds echo "setenv fel_booted 0" >> $ubootcmds - echo "setenv bootdelay 1" >> "${UBOOT_SCRIPT_SRC}" + echo "setenv bootdelay 1" >> $ubootcmds else echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds From e5a6b2726a2bbef2d0f024d5c3b374b3448d87ad Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 14:36:10 -0800 Subject: [PATCH 35/50] Create images with different sized UBIs --- chip-create-nand-images.sh | 28 ++++++++++++++++++++++------ chip-update-firmware.sh | 8 ++++---- common.sh | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index 848cf5e..a007455 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -22,10 +22,12 @@ prepare_ubi() { local eraseblocksize="$5" local pagesize="$6" local subpagesize="$7" + local oobsize="$8" local ebsize=`printf %x $eraseblocksize` local psize=`printf %x $pagesize` - local ubi=$outputdir/chip-$ebsize-$psize.ubi - local sparseubi=$outputdir/chip-$ebsize-$psize.ubi.sparse + local osize=`printf %x $oobsize` + local ubi=$outputdir/chip-$ebsize-$psize-$osize.ubi + local sparseubi=$outputdir/chip-$ebsize-$psize-$osize.ubi.sparse local mlcopts="" if [ -z $subpagesize ]; then @@ -40,6 +42,17 @@ prepare_ubi() { else lebsize=$((eraseblocksize-pagesize*2)) fi + + if [ "$oobsize" = "100" ]; then + #TOSH_512_SLC + volsize="448MiB" + elif [ "$oobsize" = "500" ]; then + #TOSH_4GB_MLC + volsize="3584MiB" + else + #HYNI_8GB_MLC + volsize="7168MiB" + fi mkdir -p $rootfs tar -xf $rootfstar -C $rootfs @@ -47,12 +60,13 @@ prepare_ubi() { echo "[rootfs] mode=ubi vol_id=0 +vol_size=$volsize vol_type=dynamic vol_name=rootfs vol_alignment=1 -vol_flags=autoresize image=$ubifs" > $ubicfg + ubinize -o $ubi -p $eraseblocksize -m $pagesize -s $subpagesize $mlcopts $ubicfg img2simg $ubi $sparseubi $eraseblocksize rm -rf $tmpdir @@ -118,9 +132,11 @@ cp $ROOTFSTAR $OUTPUTDIR/ ## prepare ubi images ## # Toshiba SLC image: -prepare_ubi $OUTPUTDIR $ROOTFSTAR "slc" 2048 262144 4096 1024 -# Toshiba/Hynix MLC image: -prepare_ubi $OUTPUTDIR $ROOTFSTAR "mlc" 4096 4194304 16384 16384 +prepare_ubi $OUTPUTDIR $ROOTFSTAR "slc" 2048 262144 4096 1024 256 +# Toshiba MLC image: +prepare_ubi $OUTPUTDIR $ROOTFSTAR "mlc" 4096 4194304 16384 16384 1280 +# Hynix MLC image: +prepare_ubi $OUTPUTDIR $ROOTFSTAR "mlc" 4096 4194304 16384 16384 1664 ## prepare spl images ## # Toshiba SLC image: diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index e79f32b..7844d0d 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -23,7 +23,7 @@ PROBES=(spl-40000-1000-100.bin UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" -UBI_TYPE="400000-4000" +UBI_TYPE="400000-4000-680" while getopts "sgpbfnhB:N:F:" opt; do case $opt in @@ -130,19 +130,19 @@ function dl_probe { export nand_erasesize=400000 export nand_oobsize=680 export nand_writesize=4000 - UBI_TYPE="400000-4000" + UBI_TYPE="400000-4000-680" ;; "Toshiba_4G_MLC") export nand_erasesize=400000 export nand_oobsize=500 export nand_writesize=4000 - UBI_TYPE="400000-4000" + UBI_TYPE="400000-4000-500" ;; "Toshiba_512M_MLC") export nand_erasesize=40000 export nand_oobsize=100 export nand_writesize=1000 - UBI_TYPE="400000-1000" + UBI_TYPE="400000-1000-100" ;; \?) echo "== Invalid format: $FORMAT ==" >&2 diff --git a/common.sh b/common.sh index 407a584..168ce1a 100755 --- a/common.sh +++ b/common.sh @@ -103,7 +103,7 @@ reset" > $ubootcmds echo "NAND detected:" cat $tmpdir/nand-info || RC=1 - UBI_TYPE="$(cat $tmpdir/nand-info | awk -F= '/erase/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/write/ {print $2}')" + UBI_TYPE="$(cat $tmpdir/nand-info | awk -F= '/erase/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/write/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/oob/ {print $2}')" echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type || RC=1 source $tmpdir/nand-info || RC=1 @@ -196,7 +196,7 @@ flash_images() { $FEL exe $UBOOTMEMADDR || RC=1 if wait_for_fastboot; then - fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize.ubi.sparse || RC=1 + fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize-$nand_oobsize.ubi.sparse || RC=1 else echo "failed to flash the UBI image" RC=1 From cbf73ee968a625c71def58d3517e48a53b0221e3 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 15:09:40 -0800 Subject: [PATCH 36/50] Print content --- chip-create-nand-images.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index a007455..ed0fcde 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x FEL=sunxi-fel From ecb5a48d2b493ea055915741e5e3926129142a8f Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 15:42:47 -0800 Subject: [PATCH 37/50] Autoresize for chippro --- chip-create-nand-images.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index ed0fcde..fb48cf0 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -1,4 +1,4 @@ -#!/bin/bash -x +#!/bin/bash FEL=sunxi-fel @@ -45,13 +45,13 @@ prepare_ubi() { if [ "$oobsize" = "100" ]; then #TOSH_512_SLC - volsize="448MiB" + volspec="vol_flags=autoresize" elif [ "$oobsize" = "500" ]; then #TOSH_4GB_MLC - volsize="3584MiB" + volspec="vol_size=3584MiB" else #HYNI_8GB_MLC - volsize="7168MiB" + volpec="vol_size=7168MiB" fi mkdir -p $rootfs @@ -60,7 +60,7 @@ prepare_ubi() { echo "[rootfs] mode=ubi vol_id=0 -vol_size=$volsize +$vol_spec vol_type=dynamic vol_name=rootfs vol_alignment=1 From 07b178d1b8e387eeca18b03ad1e77c24693b1872 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 16:19:29 -0800 Subject: [PATCH 38/50] Fixed volspec spelling errors --- chip-create-nand-images.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index fb48cf0..d1e37b4 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -51,7 +51,7 @@ prepare_ubi() { volspec="vol_size=3584MiB" else #HYNI_8GB_MLC - volpec="vol_size=7168MiB" + volspec="vol_size=7168MiB" fi mkdir -p $rootfs From c0ef52d2e8f3943308b604ec5b572a1fff0fb998 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 16:30:55 -0800 Subject: [PATCH 39/50] Check hex value instead of string --- chip-create-nand-images.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index d1e37b4..fbe76fb 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -43,10 +43,10 @@ prepare_ubi() { lebsize=$((eraseblocksize-pagesize*2)) fi - if [ "$oobsize" = "100" ]; then + if [ "$osize" = "100" ]; then #TOSH_512_SLC volspec="vol_flags=autoresize" - elif [ "$oobsize" = "500" ]; then + elif [ "$osize" = "500" ]; then #TOSH_4GB_MLC volspec="vol_size=3584MiB" else From 1f6a5f22d66d2728d2eb619c76e689c73d68974e Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 16:31:34 -0800 Subject: [PATCH 40/50] set x and check hex values --- chip-create-nand-images.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index fbe76fb..7261039 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x FEL=sunxi-fel From 53d7eedf72da9bd102fddec7665fcb5d74a0d40f Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 16:39:53 -0800 Subject: [PATCH 41/50] If size doesn't match, error and quit --- chip-create-nand-images.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index 7261039..6073d37 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -49,9 +49,12 @@ prepare_ubi() { elif [ "$osize" = "500" ]; then #TOSH_4GB_MLC volspec="vol_size=3584MiB" - else + elif [ "$osize" = "680" ]; #HYNI_8GB_MLC volspec="vol_size=7168MiB" + else + echo "Unable to acquire appropriate volume size or flags, quitting!" + exit 1 fi mkdir -p $rootfs From 2569ac1bc5c0777cdd686a5fdff549e232c548c3 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 16:44:40 -0800 Subject: [PATCH 42/50] Forgot a 'then' --- chip-create-nand-images.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index 6073d37..5df351a 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -49,7 +49,7 @@ prepare_ubi() { elif [ "$osize" = "500" ]; then #TOSH_4GB_MLC volspec="vol_size=3584MiB" - elif [ "$osize" = "680" ]; + elif [ "$osize" = "680" ]; then #HYNI_8GB_MLC volspec="vol_size=7168MiB" else From d6059470fbb2b63c4f4c7d6655c03c1248723b20 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 17:06:13 -0800 Subject: [PATCH 43/50] Fixed variable name --- chip-create-nand-images.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chip-create-nand-images.sh b/chip-create-nand-images.sh index 5df351a..f3e7c5c 100755 --- a/chip-create-nand-images.sh +++ b/chip-create-nand-images.sh @@ -63,7 +63,7 @@ prepare_ubi() { echo "[rootfs] mode=ubi vol_id=0 -$vol_spec +$volspec vol_type=dynamic vol_name=rootfs vol_alignment=1 From 6b2086eb6f30f81166780179c10fd58071bb27e1 Mon Sep 17 00:00:00 2001 From: computermouth Date: Thu, 1 Dec 2016 19:55:18 -0800 Subject: [PATCH 44/50] Add no limit to help, save ubootscr --- chip-update-firmware.sh | 1 + common.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 7844d0d..8a5d1f8 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -72,6 +72,7 @@ while getopts "sgpbfnhB:N:F:" opt; do echo " -p -- PocketCHIP" echo " -b -- Buildroot" echo " -f -- Force clean" + echo " -n -- No limit [enable greater power draw]" echo " -B -- Branch(optional) [eg. -B testing]" echo " -N -- Build#(optional) [eg. -N 150]" echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" diff --git a/common.sh b/common.sh index 168ce1a..42f35a8 100755 --- a/common.sh +++ b/common.sh @@ -118,7 +118,7 @@ flash_images() { local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds - local ubootscr=$tmpdir/uboot.scr + local ubootscr=$IMAGESDIR/uboot.scr local ubootsize=`filesize $IMAGESDIR/uboot-$nand_erasesize.bin | xargs printf "0x%08x"` local pagespereb=`echo $((nand_erasesize/nand_writesize)) | xargs printf "%x"` local sparseubi=$tmpdir/ubi.sparse From 0a91687a6d5fefa6084ccf57938d85a2e5d69022 Mon Sep 17 00:00:00 2001 From: computermouth Date: Fri, 2 Dec 2016 16:15:24 -0800 Subject: [PATCH 45/50] Boot loop, unless specified otherwise --- chip-update-firmware.sh | 7 ++++++- common.sh | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 8a5d1f8..6ca92b1 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000-680" -while getopts "sgpbfnhB:N:F:" opt; do +while getopts "sgpbfnrhB:N:F:" opt; do case $opt in s) echo "== Server selected ==" @@ -51,6 +51,10 @@ while getopts "sgpbfnhB:N:F:" opt; do echo "== No Limit mode ==" NO_LIMIT="while itest.b *0x80400000 -ne 03; do i2c mw 0x34 0x30 0x03; i2c read 0x34 0x30 1 0x80400000; done; " ;; + r) + echo "== Reset after flash ==" + RESET_COMMAND="reset" + ;; B) BRANCH="$OPTARG" echo "== ${BRANCH} branch selected ==" @@ -73,6 +77,7 @@ while getopts "sgpbfnhB:N:F:" opt; do echo " -b -- Buildroot" echo " -f -- Force clean" echo " -n -- No limit [enable greater power draw]" + echo " -r -- Reset [reset device after flash]" echo " -B -- Branch(optional) [eg. -B testing]" echo " -N -- Build#(optional) [eg. -N 150]" echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" diff --git a/common.sh b/common.sh index 42f35a8..8e02494 100755 --- a/common.sh +++ b/common.sh @@ -177,7 +177,12 @@ flash_images() { echo "echo going to fastboot mode" >> $ubootcmds echo "fastboot 0" >> $ubootcmds - echo "reset" >> $ubootcmds + + if [ -z $RESET_COMMAND ]; then + RESET_COMMAND="while true; do; sleep 10; done;" + fi + + echo "$RESET_COMMAND" >> $ubootcmds mkimage -A arm -T script -C none -n "flash $FLAVOR" -d $ubootcmds $ubootscr || RC=1 @@ -197,6 +202,7 @@ flash_images() { if wait_for_fastboot; then fastboot -i 0x1f3a -u flash UBI $IMAGESDIR/chip-$nand_erasesize-$nand_writesize-$nand_oobsize.ubi.sparse || RC=1 + fastboot -i 0x1f3a continue > /dev/null else echo "failed to flash the UBI image" RC=1 From 9211092c4aa914ee9f5050f9a24a3e01d06d1226 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 5 Dec 2016 16:56:05 -0800 Subject: [PATCH 46/50] Added local flash option --- chip-update-firmware.sh | 39 ++++++++++++++++++++++++++++----------- common.sh | 26 ++++++++++++-------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 6ca92b1..5179600 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -25,7 +25,7 @@ UBI_PREFIX="chip" UBI_SUFFIX="ubi.sparse" UBI_TYPE="400000-4000-680" -while getopts "sgpbfnrhB:N:F:" opt; do +while getopts "sgpbfnrhB:N:F:L:" opt; do case $opt in s) echo "== Server selected ==" @@ -67,6 +67,10 @@ while getopts "sgpbfnrhB:N:F:" opt; do FORMAT="$OPTARG" echo "== Format ${FORMAT} selected ==" ;; + L) + LOCALDIR="$OPTARG" + echo "== Local directory '${LOCALDIR}' selected ==" + ;; h) echo "" echo "== Help ==" @@ -81,6 +85,7 @@ while getopts "sgpbfnrhB:N:F:" opt; do echo " -B -- Branch(optional) [eg. -B testing]" echo " -N -- Build#(optional) [eg. -N 150]" echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" + echo " -L -- Local (optional) [eg. -L ../img/buildroot/]" echo "" echo "" exit 0 @@ -104,7 +109,7 @@ function dl_probe { CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) fi - if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]]; then + if [[ ! -d "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}" ]] && [[ -z $LOCALDIR ]]; then echo "== New image available ==" rm -rf $DL_DIR/$BRANCH-$FLAVOR* @@ -121,52 +126,64 @@ function dl_probe { done popd > /dev/null else - echo "== Cached probe files located ==" + echo "== Local/cached probe files located ==" fi echo "== Staging for NAND probe ==" - ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR + if [ -z LOCALDIR ];then + ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR + else + ln -s ../../$LOCALDIR $IMAGESDIR + fi + if [[ -f ${IMAGESDIR}/ubi_type ]]; then rm ${IMAGESDIR}/ubi_type; fi if [ -z $FORMAT ]; then - detect_nand + detect_nand || exit 1 else case $FORMAT in "Hynix_8G_MLC") export nand_erasesize=400000 export nand_oobsize=680 export nand_writesize=4000 - UBI_TYPE="400000-4000-680" ;; "Toshiba_4G_MLC") export nand_erasesize=400000 export nand_oobsize=500 export nand_writesize=4000 - UBI_TYPE="400000-4000-500" ;; "Toshiba_512M_MLC") export nand_erasesize=40000 export nand_oobsize=100 export nand_writesize=1000 - UBI_TYPE="400000-1000-100" ;; \?) echo "== Invalid format: $FORMAT ==" >&2 exit 1 ;; esac + UBI_TYPE="$nand_erasesize-$nand_writesize-$nand_oobsize" fi - if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then + if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]] && [ -z $LOCALDIR ]; then echo "== Downloading new UBI, this will be cached for future flashes. ==" pushd $DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM} > /dev/null if ! $WGET $DL_URL/$BRANCH/$FLAVOR/${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX; then echo "!! download of $BRANCH-$FLAVOR-$METHOD-b${CACHENUM} failed !!" - exit $? + exit $? fi popd > /dev/null - else + else + if [ -z $LOCALDIR ]; then echo "== Cached UBI located ==" + else + if [[ ! -f "$IMAGESDIR/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]]; then + echo "Could not locate UBI files" + exit 1 + else + echo "== Cached UBI located ==" + fi + fi fi } diff --git a/common.sh b/common.sh index 8e02494..5084c2e 100755 --- a/common.sh +++ b/common.sh @@ -69,7 +69,6 @@ wait_for_fel() { #------------------------------------------------------------ detect_nand() { - local RC=0 local tmpdir=`mktemp -d -t chip-uboot-script-XXXXXX` local ubootcmds=$tmpdir/uboot.cmds @@ -78,38 +77,37 @@ detect_nand() { echo "nand info env export -t -s 0x100 0x7c00 nand_erasesize nand_writesize nand_oobsize reset" > $ubootcmds - mkimage -A arm -T script -C none -n "detect NAND" -d $ubootcmds $ubootscr || RC=1 + mkimage -A arm -T script -C none -n "detect NAND" -d $ubootcmds $ubootscr || return 1 if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" - RC=1 - exit 1 + return 1 fi - $FEL spl $IMAGESDIR/sunxi-spl.bin || RC=1 + $FEL spl $IMAGESDIR/sunxi-spl.bin || return 1 # wait for DRAM initialization to complete sleep 1 - $FEL write $UBOOTMEMADDR $IMAGESDIR/u-boot-dtb.bin || RC=1 - $FEL write $UBOOTSCRMEMADDR $ubootscr || RC=1 - $FEL exe $UBOOTMEMADDR || RC=1 + $FEL write $UBOOTMEMADDR $IMAGESDIR/u-boot-dtb.bin || return 1 + $FEL write $UBOOTSCRMEMADDR $ubootscr || return 1 + $FEL exe $UBOOTMEMADDR || return 1 if ! wait_for_fel; then echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode" - exit 1 + return 1 fi - $FEL read 0x7c00 0x100 $tmpdir/nand-info || RC=1 + $FEL read 0x7c00 0x100 $tmpdir/nand-info || return 1 echo "NAND detected:" - cat $tmpdir/nand-info || RC=1 + cat $tmpdir/nand-info || return 1 UBI_TYPE="$(cat $tmpdir/nand-info | awk -F= '/erase/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/write/ {print $2}')-$(cat $tmpdir/nand-info | awk -F= '/oob/ {print $2}')" - echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type || RC=1 - source $tmpdir/nand-info || RC=1 + echo "${UBI_TYPE}" > $IMAGESDIR/ubi_type || return 1 + source $tmpdir/nand-info || return 1 rm -rf $tmpdir - return $RC + return 0 } #------------------------------------------------------------ From f4efba15146bc15a79dbaa5bb375e2647579ffd6 Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 5 Dec 2016 17:49:22 -0800 Subject: [PATCH 47/50] Add provision for .dtb detection --- chip-update-firmware.sh | 2 +- common.sh | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 5179600..4bd2733 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -130,7 +130,7 @@ function dl_probe { fi echo "== Staging for NAND probe ==" - if [ -z LOCALDIR ];then + if [ -z $LOCALDIR ];then ln -s ../../$DL_DIR/${BRANCH}-${FLAVOR}-b${CACHENUM}/ $IMAGESDIR else ln -s ../../$LOCALDIR $IMAGESDIR diff --git a/common.sh b/common.sh index 5084c2e..befead6 100755 --- a/common.sh +++ b/common.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash TIMEOUT=30 FEL=sunxi-fel @@ -126,13 +126,19 @@ flash_images() { else echo "nand erase.chip" > $ubootcmds fi + + if [ "$nand_oobsize" = "100" ];then + DTB_NAME="ntc-gr8-crumb.dtb" + else + DTB_NAME="sun5i-r8-chip.dtb" + fi echo "nand write.raw.noverify $SPLMEMADDR 0x0 $pagespereb" >> $ubootcmds echo "nand write.raw.noverify $SPLMEMADDR 0x400000 $pagespereb" >> $ubootcmds echo "nand write $UBOOTMEMADDR 0x800000 $ubootsize" >> $ubootcmds echo "setenv mtdparts mtdparts=sunxi-nand.0:4m(spl),4m(spl-backup),4m(uboot),4m(env),-(UBI)" >> $ubootcmds echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw earlyprintk ubi.mtd=4" >> $ubootcmds - echo "setenv bootcmd 'gpio set PB2; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo '(FEL boot)'; source \${scriptaddr}; fi; 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'" >> $ubootcmds + echo "setenv bootcmd 'gpio set PB2; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo '(FEL boot)'; source \${scriptaddr}; fi; mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/$DTB_NAME; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r - \$fdt_addr_r'" >> $ubootcmds echo "setenv fel_booted 0" >> $ubootcmds echo "echo Enabling Splash" >> $ubootcmds @@ -150,8 +156,8 @@ flash_images() { echo "setenv bootargs root=ubi0:rootfs rootfstype=ubifs rw ubi.mtd=4 quiet lpj=501248 loglevel=3 splash plymouth.ignore-serial-consoles" >> $ubootcmds echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds echo "setenv bootcmd '${NO_LIMIT}run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds - echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds - echo "setenv boot_noinitrd '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'" >> $ubootcmds + echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/$DTB_NAME; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds + echo "setenv boot_noinitrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/$DTB_NAME; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r - \$fdt_addr_r'" >> $ubootcmds echo "setenv video-mode" >> $ubootcmds echo "setenv dip_addr_r 0x43400000" >> $ubootcmds echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds @@ -162,8 +168,8 @@ flash_images() { echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds echo "setenv bootcmd '${NO_LIMIT}run test_fastboot; if test -n \${fel_booted} && test -n \${scriptaddr}; then echo (FEL boot); source \${scriptaddr}; fi; for path in \${bootpaths}; do run boot_\$path; done'" >> $ubootcmds - echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/sun5i-r8-chip.dtb; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds - echo "setenv boot_noinitrd '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'" >> $ubootcmds + echo "setenv boot_initrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/$DTB_NAME; ubifsload 0x44000000 /boot/initrd.uimage; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r 0x44000000 \$fdt_addr_r'" >> $ubootcmds + echo "setenv boot_noinitrd 'mtdparts; ubi part UBI; ubifsmount ubi0:rootfs; ubifsload \$fdt_addr_r /boot/$DTB_NAME; ubifsload \$kernel_addr_r /boot/zImage; bootz \$kernel_addr_r - \$fdt_addr_r'" >> $ubootcmds echo "setenv dip_addr_r 0x43400000" >> $ubootcmds echo "setenv dip_overlay_dir /lib/firmware/nextthingco/chip/early" >> $ubootcmds echo "setenv dip_overlay_cmd 'if test -n \"\${dip_overlay_name}\"; then ubifsload \$dip_addr_r \$dip_overlay_dir/\$dip_overlay_name; fi'" >> $ubootcmds From 08b1d61ac5c221dd5adae446f98f3673e56dd770 Mon Sep 17 00:00:00 2001 From: computermouth Date: Fri, 9 Dec 2016 13:01:04 -0800 Subject: [PATCH 48/50] Add provisions for flashing local dir --- chip-update-firmware.sh | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 4bd2733..6996d9a 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -x + SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh @@ -75,17 +77,17 @@ while getopts "sgpbfnrhB:N:F:L:" opt; do echo "" echo "== Help ==" echo "" - echo " -s -- Server [Debian + Headless]" - echo " -g -- GUI [Debian + XFCE]" - echo " -p -- PocketCHIP" - echo " -b -- Buildroot" - echo " -f -- Force clean" + echo " -s -- Server [Debian + Headless] " + echo " -g -- GUI [Debian + XFCE] " + echo " -p -- PocketCHIP [CHIP on the go!] " + echo " -b -- Buildroot [Tiny, but powerful] " + echo " -f -- Force clean [re-download if applicable]" echo " -n -- No limit [enable greater power draw]" - echo " -r -- Reset [reset device after flash]" - echo " -B -- Branch(optional) [eg. -B testing]" - echo " -N -- Build#(optional) [eg. -N 150]" - echo " -F -- Format(optional) [eg. -F Toshiba_4G_MLC]" - echo " -L -- Local (optional) [eg. -L ../img/buildroot/]" + echo " -r -- Reset [reset device after flash] " + echo " -B -- Branch [eg. -B testing] " + echo " -N -- Build# [eg. -N 150] " + echo " -F -- Format [eg. -F Toshiba_4G_MLC] " + echo " -L -- Local [eg. -L ../img/buildroot/] " echo "" echo "" exit 0 @@ -105,7 +107,7 @@ function require_directory { function dl_probe { - if [ -z $CACHENUM ]; then + if [ -z $CACHENUM ] && [ -z $LOCALDIR ]; then CACHENUM=$(curl -s $DL_URL/$BRANCH/$FLAVOR/latest) fi @@ -143,6 +145,7 @@ function dl_probe { else case $FORMAT in "Hynix_8G_MLC") + echo hello export nand_erasesize=400000 export nand_oobsize=680 export nand_writesize=4000 @@ -152,17 +155,19 @@ function dl_probe { export nand_oobsize=500 export nand_writesize=4000 ;; - "Toshiba_512M_MLC") + "Toshiba_512M_SLC") + echo correct export nand_erasesize=40000 export nand_oobsize=100 export nand_writesize=1000 ;; - \?) - echo "== Invalid format: $FORMAT ==" >&2 + *) + echo "== Invalid format: $FORMAT ==" exit 1 - ;; + ;; esac UBI_TYPE="$nand_erasesize-$nand_writesize-$nand_oobsize" + echo $UBI_TYPE > ${IMAGESDIR}/ubi_type fi if [[ ! -f "$DL_DIR/$BRANCH-$FLAVOR-b${CACHENUM}/$UBI_PREFIX-$UBI_TYPE.$UBI_SUFFIX" ]] && [ -z $LOCALDIR ]; then From eb5d5f207f06b74171dbdfbf143f21b65c2313e7 Mon Sep 17 00:00:00 2001 From: computermouth Date: Fri, 9 Dec 2016 14:56:56 -0800 Subject: [PATCH 49/50] Don't set -x --- chip-update-firmware.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/chip-update-firmware.sh b/chip-update-firmware.sh index 6996d9a..6d57bf0 100755 --- a/chip-update-firmware.sh +++ b/chip-update-firmware.sh @@ -1,7 +1,5 @@ #!/bin/bash -set -x - SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source $SCRIPTDIR/common.sh From b1b00dd0b7e27ecaf9f1d3b5283089c131eb2f0c Mon Sep 17 00:00:00 2001 From: computermouth Date: Mon, 12 Dec 2016 15:48:28 -0800 Subject: [PATCH 50/50] Updated some copy --- README.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c13cf2a..b5a0c8e 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,11 @@ A collection of scripts for working with CHIP ## Requirements 1) [sunxi-tools](https://github.com/linux-sunxi/sunxi-tools.git) 2) **uboot-tools** from your package manager +2) **mtd-utils-mlc** from our repository (https://github.com/nextthingco/chip-mtd-utils) [for creating images] ## Included Tools ### chip-update-firmware -This tool is used to download the latest firmware release for CHIP and run **chip-fel-flash** with the newest firmware. - -### chip-fel-flash -This tool is used to flash a local firmware image to a connected CHIP over FEL - -### chip-fel-upload -This tool is used to upload uboot, a linux kernel and an initramfs and launch into it +This tool is used to download and flash the latest firmware release for CHIP. The tool also now only supports fastboot flashing. +### chip-create-nand-images +This tool is used to generate local firmware images for CHIP and CHIP Pro.