2015-09-12 20:08:01 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
2015-09-20 12:42:16 -04:00
|
|
|
TIMEOUT=30
|
2016-10-27 18:40:12 -04:00
|
|
|
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
|
2015-09-17 03:26:38 -04:00
|
|
|
|
2015-10-21 17:26:59 -04:00
|
|
|
#------------------------------------------------------------
|
2015-09-12 20:08:01 -04:00
|
|
|
onMac() {
|
|
|
|
if [ "$(uname)" == "Darwin" ]; then
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:26:59 -04:00
|
|
|
#------------------------------------------------------------
|
2015-09-12 20:08:01 -04:00
|
|
|
filesize() {
|
|
|
|
if onMac; then
|
2015-09-14 22:29:33 -04:00
|
|
|
stat -f "%z" $1
|
2015-09-12 20:08:01 -04:00
|
|
|
else
|
2015-09-12 23:15:36 -04:00
|
|
|
stat --printf="%s" $1
|
2015-09-12 20:08:01 -04:00
|
|
|
fi
|
|
|
|
}
|
2015-10-21 17:26:59 -04:00
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
wait_for_fastboot() {
|
|
|
|
echo -n "waiting for fastboot...";
|
|
|
|
for ((i=$TIMEOUT; i>0; i--)) {
|
2015-12-03 23:07:01 -05:00
|
|
|
if [[ ! -z "$(fastboot -i 0x1f3a $@ devices)" ]]; then
|
2015-10-21 17:26:59 -04:00
|
|
|
echo "OK";
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
echo -n ".";
|
|
|
|
sleep 1
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "TIMEOUT";
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
wait_for_fel() {
|
|
|
|
echo -n "waiting for fel...";
|
|
|
|
for ((i=$TIMEOUT; i>0; i--)) {
|
2015-12-03 23:07:01 -05:00
|
|
|
if ${FEL} $@ ver 2>/dev/null >/dev/null; then
|
2015-10-21 17:26:59 -04:00
|
|
|
echo "OK"
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
echo -n ".";
|
|
|
|
sleep 1
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "TIMEOUT";
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:30:12 -04:00
|
|
|
#------------------------------------------------------------
|
2016-10-27 18:40:12 -04:00
|
|
|
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"
|
2016-10-27 19:30:12 -04:00
|
|
|
exit 1
|
2016-10-27 18:40:12 -04:00
|
|
|
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"
|
2016-10-27 19:30:12 -04:00
|
|
|
exit 1
|
2016-10-27 18:40:12 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:30:12 -04:00
|
|
|
#------------------------------------------------------------
|
2016-10-27 18:40:12 -04:00
|
|
|
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"
|
2016-10-27 19:30:12 -04:00
|
|
|
exit 1
|
2016-10-27 18:40:12 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:26:59 -04:00
|
|
|
#------------------------------------------------------------
|
|
|
|
wait_for_linuxboot() {
|
|
|
|
local TIMEOUT=100
|
|
|
|
echo -n "flashing...";
|
|
|
|
for ((i=$TIMEOUT; i>0; i--)) {
|
2016-09-11 06:16:08 -04:00
|
|
|
if lsusb |grep -q "0525:a4a7" ||
|
|
|
|
lsusb |grep -q "0525:a4aa"; then
|
2015-10-21 17:26:59 -04:00
|
|
|
echo "OK"
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
echo -n ".";
|
|
|
|
sleep 3
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "TIMEOUT";
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:30:12 -04:00
|
|
|
#------------------------------------------------------------
|
2016-09-11 06:16:08 -04:00
|
|
|
ready_to_roll() {
|
|
|
|
|
|
|
|
echo -e "\n\nFLASH VERIFICATION COMPLETE.\n\n"
|
|
|
|
|
|
|
|
echo " # # #"
|
|
|
|
echo " #########"
|
|
|
|
echo "### ###"
|
|
|
|
echo " # {#} #"
|
2016-10-27 18:40:12 -04:00
|
|
|
echo "### '%######"
|
2016-09-11 06:16:08 -04:00
|
|
|
echo " # #"
|
|
|
|
echo "### ###"
|
|
|
|
echo " ########"
|
|
|
|
echo " # # #"
|
|
|
|
|
|
|
|
echo -e "\n\nCHIP is ready to roll!\n\n"
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|