Skip to content

Commit

Permalink
lantiq: BTHOMEHUB5A - create install image
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias Kresin <dev@kresin.me>
  • Loading branch information
mkresin committed Nov 2, 2017
1 parent 444add1 commit 76e1aba
Show file tree
Hide file tree
Showing 17 changed files with 3,249 additions and 18 deletions.
2,402 changes: 2,402 additions & 0 deletions .config

Large diffs are not rendered by default.

Binary file added files/data/uboot-env_bt_patched.bin
Binary file not shown.
Binary file added files/data/uboot.bin
Binary file not shown.
36 changes: 36 additions & 0 deletions files/etc/banner
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*******************************************************************************
* Guide for installing LEDE onto a BT Home Hub 5 Type A & clones *
*******************************************************************************

You need an USB flash drive with at least 512MB free capacity, formatted as
FAT32, exFAT or ext2/3/4, attached to the Hub. The attached flash drive will be
accessible at /tmp/mounts/<directory>.

To install LEDE, the USB flash drive must have a copy of the file:
lede-17.01.4-lantiq-xrx200-BTHOMEHUBV5A-squashfs-sysupgrade.bin

To restore the stock Firmware, the USB flash drive must have a copy of the
nanddump backup file (hh5a.nanddump) containing the stock Firmware.

LIST OF COMMANDS

To create a backup of the installed firmware (Take care to not overwrite
any previously made backup):
nanddump --file /tmp/mounts/<directory>/hh5a.nanddump /dev/mtd4

To install LEDE:
prepare

To restore stock firmware:
restore

TO UPGRADE FROM OpenWrt OR LEDE PRIOR TO r2363-0e34459e6b:

Restore the stock firmware and do a fresh install of LEDE.

If you don't have a backup of the stock firmware from your Hub, use the
experimental 'migrate' script. It is offered with no guarantee of success.
You are strongly advised to restore the stock firmware.

Please scroll back to view ALL of the above instructions.

159 changes: 159 additions & 0 deletions files/usr/sbin/migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/sh
# Copyright (C) 2016 Mathias Kresin <dev@kresin.me>

. /lib/functions.sh

# use the u-boot and u-boot env from bt for plusnet hub one as well
UBOOT_FILE="/data/uboot.bin"
UBOOT_ENV_FILE="/data/uboot-env_bt_patched.bin"

# check for required binaries
BINARIES="flash_erase hexdump nanddump nandwrite ubiattach ubidetach"
for BINARY in ${BINARIES}; do
[ ! -x $(which ${BINARY}) ] && {
echo "${BINARY} not found - are you running the install image?" >&2
exit 1
}
done

# make sure expected partitions exist and are writable
PART_NAME="u-boot"
UBOOT_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UBOOT_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UBOOT_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - are you running the install image?" >&2
exit 1
}

PART_NAME="uboot-env"
UBOOT_ENV_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UBOOT_ENV_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UBOOT_ENV_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - are you running the install image?" >&2
exit 1
}

PART_NAME="unused"
UNUSED_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UNUSED_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UNUSED_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - are you running the install image?" >&2
exit 1
}

PART_NAME="ubi"
UBI_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UBI_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UBI_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - something is wrong!" >&2
exit 1
}

# make sure required files exist
[ ! -s "${UBOOT_FILE}" ] && {
echo "U-Boot file ${UBOOT_FILE} not found - are you running the install image?" >&2
exit 1
}

[ ! -s "${UBOOT_ENV_FILE}" ] && {
echo "U-Boot Enviroment file ${UBOOT_ENV_FILE} not found - are you running the install image?" >&2
exit 1
}

# make sure the required bits are present on the flash
[ "$(nanddump -q -s 0x81000 /dev/mtd${UBOOT_PART_INDEX} | hexdump -v -n 2 -e '1/1 "%02x"')" != "a55a" ] && {
echo "Caldata not found on flash. Sorry, there is nothing we can do for you!" >&2
exit 1
}

echo "This script should be only used as a LAST RESORT to migrate to the new LEDE"
echo "partition layout if you have no backup, or you cannot restore the stock firmware"
echo "for any other reason."
echo ""
echo "DISCLAIMER: Use this script at your own risk. Very limited testing has been"
echo " completed for this script. It is offered with no guarantee of"
echo " success."
echo ""
echo "You are strongly advised to use the 'restore' and 'prepare' scripts to"
echo "restore the stock firmware, and then to install LEDE."
echo ""
echo ""
echo "WARNING: DO NOT CONTINUE unless you have saved a backup of the current running"
echo " firmware to your COMPUTER or other storage device!"
echo ""
echo ""

read -p "Please enter YESIHAVEABACKUP to continue: " CONFIRMATION
[ "${CONFIRMATION}" != "YESIHAVEABACKUP" ] && exit 0

set -o pipefail

echo ""
echo "STARTING migration..."

echo "DETACHING ubi partition..."
# hide any error messages to not confuse users in case the ubi volume isn't at
# the expected position or doesn't exist at all
ubidetach -m "${UBI_PART_INDEX}" 2>/dev/null

echo ""
echo "FORMATING ubi partition"
ubiformat "/dev/mtd${UBI_PART_INDEX}" -y

NAND_UBI_DEV_ID=0
echo ""
echo "ATTACHING ubi partition to ubi dev number ${NAND_UBI_DEV_ID} using"
echo "19 reserved blocks for bad block handling"
ubiattach -m "${UBI_PART_INDEX}" -d "${NAND_UBI_DEV_ID}" --max-beb-per1024=19

UBI_VOL_NAME="caldata"
echo ""
echo "COPYING caldata to ${UBI_VOL_NAME} ubi volume"
UBI_VOL_SIZE=129024
UBI_VOL_ID=3
ubimkvol /dev/ubi"${NAND_UBI_DEV_ID}" -n "${UBI_VOL_ID}" -N "${UBI_VOL_NAME}" -s "${UBI_VOL_SIZE}"
nanddump -s 0x80000 -l 0x20000 "/dev/mtd${UBOOT_PART_INDEX}" | \
ubiupdatevol /dev/ubi"${NAND_UBI_DEV_ID}"_"${UBI_VOL_ID}" -s "${UBI_VOL_SIZE}" -

echo ""
echo "RESTORING u-boot partition from ${UBOOT_FILE}"
flash_erase "/dev/mtd${UBOOT_PART_INDEX}" 0 0
nandwrite -m "/dev/mtd${UBOOT_PART_INDEX}" "${UBOOT_FILE}"

echo ""
echo "RESTORING u-boot env partition from ${UBOOT_ENV_FILE} (unlocked u-boot console and update bootcmd)"
flash_erase "/dev/mtd${UBOOT_ENV_PART_INDEX}" 0 0
nandwrite -m "/dev/mtd${UBOOT_ENV_PART_INDEX}" "${UBOOT_ENV_FILE}"

echo ""
echo "FORMATING unused partition"
flash_erase "/dev/mtd${UNUSED_PART_INDEX}" 0 0

echo ""
echo "Migration finished!"
echo ""
echo "Your attached USB media is mounted as a subdirectory at /tmp/mounts/"
echo ""
echo "To install LEDE, run:"
echo ""
echo " sysupgrade /tmp/mounts/<directory>/lede-17.01.4-lantiq-xrx200-BTHOMEHUBV5A-squashfs-sysupgrade.bin"
echo ""
echo "Sysupgrade will automatically restart your device when it has finished."
echo "If the CFG-04 UART prompt appears, power-cycle your device!"
echo "The hub should now boot up using LEDE."
168 changes: 168 additions & 0 deletions files/usr/sbin/prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/bin/sh
# Copyright (C) 2016 Mathias Kresin <dev@kresin.me>

. /lib/functions.sh

# remove debug output from serial console
echo 5 > /proc/sys/kernel/printk

# check for required binaries
BINARIES="insmod flash_erase nandwrite ubirmvol nanddump openssl nandwrite fw_printenv fw_setenv"
for BINARY in ${BINARIES}; do
[ ! -x $(which ${BINARY}) ] && {
echo "${BINARY} not found - are you running the install image?" >&2
exit 1
}
done

# make sure expected partitions exist and are writable
PART_NAME="ubi"
UBI_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UBI_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UBI_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - something is wrong!" >&2
exit 1
}

PART_NAME="uboot-env"
UBOOT_ENV_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${UBOOT_ENV_PART_INDEX}" ] && {
echo "${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

[ $(cat /sys/class/block/mtdblock${UBOOT_ENV_PART_INDEX}/ro) -eq 1 ] && {
echo "${PART_NAME} partition not writable - are you running the install image?" >&2
exit 1
}

# cleanup nandsim state
rmmod nandsim &>/dev/null

# create 16 MByte virtual nand chip in RAM
insmod nandsim first_id_byte=0x20 second_id_byte=0x33 parts=8 >/dev/null
PART_NAME="NAND simulator partition 0"
NANDSIM_UBOOT_ENV_PART_INDEX=$(find_mtd_index "${PART_NAME}")
[ -z "${NANDSIM_UBOOT_ENV_PART_INDEX}" ] && {
echo "ERROR ${PART_NAME} partition not found - something is wrong!" >&2
exit 1
}

echo ""
echo "This script will erase parts of the Nand flash memory on this Hub, and then"
echo "prepare it for running Sysupgrade."
echo ""
echo ""
echo "WARNING: DO NOT CONTINUE unless you have saved a backup of the original firmware"
echo " to your COMPUTER or other storage device!"
echo ""
echo ""
echo "DO NOT POWER OFF THE DEVICE DURING THIS PROCEDURE."
echo ""
echo ""

read -p "Please enter YESIHAVEABACKUP to continue: " CONFIRMATION
[ "${CONFIRMATION}" != "YESIHAVEABACKUP" ] && exit 0

set -o pipefail

echo ""
echo "Starting installation..."

# configure fw_printenv
echo "/dev/mtd${NANDSIM_UBOOT_ENV_PART_INDEX} 0x0000 0x20000 0x4000" > /etc/fw_env.config

# BT BusinessHub 5a
# Plusnet Hub One
# BT HomeHub 5a
KNOWN_KEYS="B1D3B4BD0970FCA9261E23C2170DB8E52407C71DAC06098678C0885BC7E68CCD \
86fd5557bab554172a97eaec65680745d4ec2efe723decb956a50bd9bc13e1a8 \
3E4CA8114D15BFC653B2BF9519EF2B94200E30345503B125C1D0BE776698B950"

for key in $KNOWN_KEYS; do
echo "TRYING to decrypt u-boot env with key $key"

# decrypt aes-256-cbc encrypted u-boot env and store to temporary mtd
# blockdevice to manipulate it using the u-boot env tools
flash_erase -q "/dev/mtd${NANDSIM_UBOOT_ENV_PART_INDEX}" 0 0

nanddump -q "/dev/mtd${UBOOT_ENV_PART_INDEX}" | \
openssl enc -d -aes-256-cbc \
-K $key \
-iv 00000000000000000000000000000000 -nopad | \
nandwrite -q "/dev/mtd${NANDSIM_UBOOT_ENV_PART_INDEX}"

BOOTCMD=$(fw_printenv -n bootcmd 2>/dev/null)
[ -n "$BOOTCMD" ] && {
echo "SUCCESSFULLY decrypted u-boot env"
break
}
done

if [ -z "$BOOTCMD" ]
then
echo "WARNING: U-boot environment could not be decrypted!"
echo ""
echo "'bootcmd' will be reset to default values. When the HH5A starts up after"
echo "installing LEDE, the boot process will halt at the VR9# u-boot prompt."
echo ""
echo "To update 'bootcmd' variable refer to the guide on the web page hosting the"
echo "install image."
echo ""
flash_erase "/dev/mtd${UBOOT_ENV_PART_INDEX}" 0 0

read -p "Press any key to continue..."
else
fw_printenv -n bootcmd_openrg &>/dev/null && {
echo "ERROR bootcmd_openrg already exists. Please restore backup and start again" >&2
exit 1
}

echo ""
echo "BACKING UP original bootcmd in u-boot env"
fw_setenv bootcmd_openrg "$BOOTCMD"

echo "SETTING custom bootcmd in u-boot env"
fw_setenv bootcmd 'ubi part UBI; ubi read $(loadaddr) kernel; bootm $(loadaddr)'

echo "UNLOCKING u-boot prompt"
fw_setenv silent

echo ""
echo "FLASHING updated encrypted u-boot env"
flash_erase "/dev/mtd${UBOOT_ENV_PART_INDEX}" 0 0
nanddump -q "/dev/mtd${NANDSIM_UBOOT_ENV_PART_INDEX}" | \
openssl enc -aes-256-cbc \
-K $key \
-iv 00000000000000000000000000000000 -nopad | \
nandwrite -m "/dev/mtd${UBOOT_ENV_PART_INDEX}"
fi

# cleanup nandsim state
rmmod nandsim &>/dev/null

echo ""
echo "REMOVING ubi volume OpenRG..."
ubirmvol /dev/ubi0 --name=OpenRG

echo ""
echo "REMOVING ubi volume FFS..."
ubirmvol /dev/ubi0 --name=FFS

echo ""
echo ""
echo "Preparation completed!"
echo ""
echo "Your attached USB media is mounted as a subdirectory at /tmp/mounts/"
echo ""
echo "To install LEDE, run:"
echo ""
echo " sysupgrade /tmp/mounts/<directory>/lede-17.01.4-lantiq-xrx200-BTHOMEHUBV5A-squashfs-sysupgrade.bin"
echo ""
echo "Sysupgrade will automatically restart your device when it has finished."
echo "If the CFG-04 UART prompt appears, power-cycle your device!"
echo "The hub should now boot up using LEDE."
Loading

0 comments on commit 76e1aba

Please sign in to comment.