2015-12-02 18:18:54 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
|
|
|
source $SCRIPTDIR/common.sh
|
|
|
|
|
|
|
|
RUN_ALL_STAGES=true
|
|
|
|
NUM_STAGES=5
|
|
|
|
stage=()
|
|
|
|
|
|
|
|
for i in $(seq 0 $NUM_STAGES); do
|
|
|
|
stage+=(false)
|
|
|
|
done
|
|
|
|
##############################################################
|
|
|
|
# main
|
|
|
|
##############################################################
|
2015-12-02 21:10:09 -05:00
|
|
|
TEMP=`getopt -o "sflu:n:" --long "fel,fastboot,loop-after-flash,stage:,chip-path:" -- "$@"`
|
2015-12-02 18:18:54 -05:00
|
|
|
eval set -- "$TEMP"
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
case $1 in
|
|
|
|
-s|--fel)
|
|
|
|
echo "fel method enabled"
|
|
|
|
METHOD=fel
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-f|--fastboot)
|
|
|
|
echo "fastboot method enabled"
|
|
|
|
METHOD=fastboot
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-l|--loop-after-flash)
|
|
|
|
echo "factory mode remain in u-boot after flashing"
|
|
|
|
AFTER_FLASHING=loop
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-u)
|
|
|
|
BUILDROOT_OUTPUT_DIR="${2}"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--stage)
|
|
|
|
if [ "${2}" -ge 0 -a "${2}" -le $NUM_STAGES ]; then
|
|
|
|
echo "Enabling stage ${2}"
|
|
|
|
stage[${2}]=true
|
|
|
|
RUN_ALL_STAGES=false
|
|
|
|
else
|
|
|
|
echo "Unknown stage: ${2}"
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2015-12-02 21:10:09 -05:00
|
|
|
-n|--chip-path)
|
|
|
|
CHIP_PATH=$2
|
2015-12-03 22:59:15 -05:00
|
|
|
FEL_DEVICE="--dev $(udevadm info ${CHIP_PATH} | grep "E\: BUSNUM" | awk -F "=" '{ print $NF }'| sed 's/^0*//'):$(udevadm info ${CHIP_PATH} | grep "E\: DEVNUM" | awk -F "=" '{ print $NF }'| sed 's/^0*//')"
|
2015-12-02 21:10:09 -05:00
|
|
|
FASTBOOT_PORT="-s usb:$(udevadm info ${CHIP_PATH} | grep "P\:" | awk -F "/" '{print $NF}')"
|
|
|
|
echo "Fastboot port: ${FASTBOOT_PORT}"
|
|
|
|
echo "FEL device: ${FEL_DEVICE}"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2015-12-02 18:18:54 -05:00
|
|
|
--)
|
|
|
|
shift; break;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: -$1" >&2
|
|
|
|
exit 1
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
log() {
|
|
|
|
echo " ${FEL_DEVICE}: $@"
|
|
|
|
}
|
2015-12-02 19:33:52 -05:00
|
|
|
BUILDROOT_OUTPUT_DIR="${BUILDROOT_OUTPUT_DIR:-$(pwd)/.firmware}"
|
2015-12-04 21:20:31 -05:00
|
|
|
log "BUILDROOT_OUTPUT_DIR = $BUILDROOT_OUTPUT_DIR"
|
2015-12-02 18:18:54 -05:00
|
|
|
|
|
|
|
if ${RUN_ALL_STAGES}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "Running all stages"
|
2015-12-02 18:18:54 -05:00
|
|
|
for i in $(seq 0 $NUM_STAGES); do
|
|
|
|
stage[i]=true
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2015-12-02 21:10:09 -05:00
|
|
|
FEL="fel ${FEL_DEVICE}"
|
2015-12-02 18:18:54 -05:00
|
|
|
|
|
|
|
METHOD=${METHOD:-fastboot}
|
|
|
|
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="${BUILDROOT_OUTPUT_DIR}/tmp"
|
|
|
|
PADDED_SPL="${BUILDROOT_OUTPUT_DIR}/images/sunxi-spl-with-ecc.bin"
|
|
|
|
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"
|
|
|
|
SPARSE_UBI="${TMPDIR}/rootfs.ubi.sparse"
|
|
|
|
UBI_MEM_ADDR=0x4b000000
|
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
log "Temporary directory: ${TMPDIR}"
|
2015-12-02 19:33:52 -05:00
|
|
|
if [ ! -d "${TMPDIR}" ]; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "Making directory: ${TMPDIR}"
|
2015-12-02 18:18:54 -05:00
|
|
|
mkdir -p "${TMPDIR}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
UBI_SIZE=`filesize $UBI | xargs printf "0x%08x"`
|
|
|
|
PAGE_SIZE=16384
|
|
|
|
OOB_SIZE=1664
|
|
|
|
|
|
|
|
prepare_images() {
|
|
|
|
#PADDED_SPL_SIZE in pages
|
|
|
|
if [[ ! -e "${PADDED_SPL}" ]]; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "ERROR: can not read ${PADDED_SPL}"
|
2015-12-02 18:18:54 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
PADDED_SPL_SIZE=$(filesize "${PADDED_SPL}")
|
|
|
|
PADDED_SPL_SIZE=$(($PADDED_SPL_SIZE / ($PAGE_SIZE + $OOB_SIZE)))
|
|
|
|
PADDED_SPL_SIZE=$(echo $PADDED_SPL_SIZE | xargs printf "0x%08x")
|
2015-12-04 21:20:31 -05:00
|
|
|
log "PADDED_SPL_SIZE=$PADDED_SPL_SIZE"
|
2015-12-02 18:18:54 -05:00
|
|
|
|
|
|
|
# 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"`
|
2015-12-04 21:20:31 -05:00
|
|
|
log "UBOOT_SIZE=${UBOOT_SIZE}"
|
|
|
|
log "PADDED_UBOOT_SIZE=${PADDED_UBOOT_SIZE}"
|
2015-12-02 18:18:54 -05:00
|
|
|
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 "echo nand write.raw.noverify $SPL_MEM_ADDR 0x0 $PADDED_SPL_SIZE" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "nand write.raw.noverify $SPL_MEM_ADDR 0x0 $PADDED_SPL_SIZE" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "echo nand write.raw.noverify $SPL_MEM_ADDR 0x400000 $PADDED_SPL_SIZE" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "nand write.raw.noverify $SPL_MEM_ADDR 0x400000 $PADDED_SPL_SIZE" >> "${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 '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'" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "setenv fel_booted 0" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
|
|
|
|
echo "echo Enabling Splash" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "setenv stdout serial" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "setenv stderr serial" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "setenv splashpos m,m" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
|
|
|
|
echo "echo Configuring Video Mode"
|
|
|
|
echo "setenv video-mode sunxi:640x480-24@60,monitor=composite-ntsc,overscan_x=40,overscan_y=20" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
|
|
|
|
echo "saveenv" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
|
|
|
|
if [[ "${METHOD}" == "fel" ]]; then
|
|
|
|
echo "nand write.slc-mode.trimffs $UBI_MEM_ADDR 0x1000000 $UBI_SIZE" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "mw \${scriptaddr} 0x0" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
else
|
|
|
|
echo "echo going to fastboot mode" >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "fastboot 0" >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${AFTER_FLASHING}" == "boot" ]]; then
|
|
|
|
echo "echo " >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "echo *****************[ BOOT ]*****************" >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "echo " >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "boot" >> "${UBOOT_SCRIPT_SRC}"
|
|
|
|
else
|
|
|
|
echo "echo " >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "echo *****************[ FLASHING DONE ]*****************" >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "echo " >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
echo "while true; do; sleep 10; done;" >>"${UBOOT_SCRIPT_SRC}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkimage -A arm -T script -C none -n "flash CHIP" -d "${UBOOT_SCRIPT_SRC}" "${UBOOT_SCRIPT}"
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_error() {
|
|
|
|
ERR=$?
|
|
|
|
ERRCODE=$1
|
|
|
|
if [ "${ERR}" != "0" ]; then
|
|
|
|
if [ -z "${ERR}" ]; then
|
|
|
|
exit ${ERR}
|
|
|
|
else
|
|
|
|
exit ${ERRCODE}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-12-02 19:33:52 -05:00
|
|
|
if ${stage[0]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== preparing images =="
|
2015-12-03 21:02:15 -05:00
|
|
|
if [ ! -f "${PADDED_UBOOT}" ]; then
|
2015-12-04 21:20:31 -05:00
|
|
|
prepare_images > /dev/null
|
2015-12-03 21:02:15 -05:00
|
|
|
fi
|
|
|
|
if [ ! -f "${UBOOT_SCRIPT}" ]; then
|
2015-12-04 21:20:31 -05:00
|
|
|
prepare_uboot_script > /dev/null
|
2015-12-03 21:02:15 -05:00
|
|
|
fi
|
2015-12-02 18:18:54 -05:00
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== upload the SPL to SRAM and execute it =="
|
2015-12-02 18:18:54 -05:00
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
${FEL} spl "${SPL}" > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 128
|
2015-12-02 19:33:52 -05:00
|
|
|
sleep 1 # wait for DRAM initialization to complete
|
2015-12-02 18:18:54 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if ${stage[1]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== upload spl =="
|
|
|
|
${FEL} write $SPL_MEM_ADDR "${PADDED_SPL}" > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 129
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ${stage[2]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== upload u-boot =="
|
|
|
|
${FEL} write $UBOOT_MEM_ADDR "${PADDED_UBOOT}" > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 130
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ${stage[3]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== upload u-boot script =="
|
2015-12-02 18:18:54 -05:00
|
|
|
${FEL} write $UBOOT_SCRIPT_MEM_ADDR "${UBOOT_SCRIPT}" || ( echo "ERROR: could not write ${UBOOT_SCRIPT}" && exit $? )
|
|
|
|
assert_error 131
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${METHOD}" == "fel" ]]; then
|
|
|
|
|
|
|
|
if ${stage[4]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== upload ubi =="
|
|
|
|
${FEL} write $UBI_MEM_ADDR "${UBI}" > /dev/null
|
|
|
|
assert_error 132
|
2015-12-02 18:18:54 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ${stage[5]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== execute the main u-boot binary =="
|
|
|
|
${FEL} exe $UBOOT_MEM_ADDR > /dev/null
|
|
|
|
assert_error 133
|
2015-12-02 18:18:54 -05:00
|
|
|
fi
|
2015-12-04 21:20:31 -05:00
|
|
|
# rm -rf "${TMPDIR}"
|
2015-12-02 18:18:54 -05:00
|
|
|
else
|
|
|
|
if ${stage[4]}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== execute the main u-boot binary =="
|
|
|
|
${FEL} exe $UBOOT_MEM_ADDR > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 132
|
|
|
|
fi
|
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
if ${stage[5]}; then
|
|
|
|
log "== creating sparse image =="
|
2015-12-03 21:02:15 -05:00
|
|
|
if [ ! -f "${SPARSE_UBI}" ]; then
|
2015-12-04 21:20:31 -05:00
|
|
|
img2simg ${UBI} ${SPARSE_UBI} $((2*1024*1024)) &> /dev/null
|
2015-12-03 21:02:15 -05:00
|
|
|
fi
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 133
|
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
log "== waiting for fastboot =="
|
2015-12-03 21:02:15 -05:00
|
|
|
if wait_for_fastboot ${FASTBOOT_PORT}; then
|
2015-12-04 21:20:31 -05:00
|
|
|
fastboot ${FASTBOOT_PORT} -i 0x1f3a -u flash UBI ${SPARSE_UBI} > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 134
|
|
|
|
|
2015-12-04 21:20:31 -05:00
|
|
|
fastboot ${FASTBOOT_PORT} -i 0x1f3a continue > /dev/null
|
2015-12-02 18:18:54 -05:00
|
|
|
assert_error 135
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-12-04 21:20:31 -05:00
|
|
|
# rm -rf "${TMPDIR}"
|
2015-12-02 18:18:54 -05:00
|
|
|
fi
|
|
|
|
fi
|