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]"
2016-11-10 20:35:02 -05:00
echo " - or build from source [github.com/linux-sunxi/sunxi-tools]"
2016-10-27 18:40:12 -04:00
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( ) {
2016-11-28 18:38:15 -05:00
local RC = 0
2016-10-27 18:40:12 -04:00
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
2016-11-02 17:58:29 -04:00
if [ " $FLAVOR " = "pocketchip" ] ; then
2016-11-08 17:33:10 -05:00
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
2016-10-31 23:10:12 -04:00
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
2016-11-10 20:35:02 -05:00
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
2016-10-31 23:10:12 -04:00
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
2016-10-27 18:40:12 -04:00
echo "setenv video-mode" >> $ubootcmds
2016-10-31 23:10:12 -04:00
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
2016-11-08 17:33:10 -05:00
echo "setenv bootdelay 1" >> " ${ UBOOT_SCRIPT_SRC } "
2016-10-27 18:40:12 -04:00
else
2016-11-08 17:33:10 -05:00
echo "setenv bootpaths 'initrd noinitrd'" >> $ubootcmds
2016-11-10 20:35:02 -05:00
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
2016-11-08 17:33:10 -05:00
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
2016-10-27 18:40:12 -04:00
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
2016-11-28 18:38:15 -05:00
mkimage -A arm -T script -C none -n " flash $FLAVOR " -d $ubootcmds $ubootscr || RC = 1
2016-10-27 18:40:12 -04:00
if ! wait_for_fel; then
echo "ERROR: please make sure CHIP is connected and jumpered in FEL mode"
2016-11-28 18:38:15 -05:00
RC = 1
2016-10-27 18:40:12 -04:00
fi
2016-11-28 18:38:15 -05:00
$FEL spl $IMAGESDIR /sunxi-spl.bin || RC = 1
2016-10-27 18:40:12 -04:00
# wait for DRAM initialization to complete
sleep 1
2016-11-28 18:38:15 -05:00
$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
2016-10-27 18:40:12 -04:00
if wait_for_fastboot; then
2016-11-28 18:38:15 -05:00
fastboot -i 0x1f3a -u flash UBI $IMAGESDIR /chip-$nand_erasesize -$nand_writesize .ubi.sparse || RC = 1
2016-10-27 18:40:12 -04:00
else
echo "failed to flash the UBI image"
2016-11-28 18:38:15 -05:00
RC = 1
2016-10-27 18:40:12 -04:00
fi
rm -rf $tmpdir
2016-11-28 18:38:15 -05:00
return $RC
2016-10-27 18:40:12 -04:00
}
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
}