Initial commit

This commit is contained in:
domenico
2025-06-24 13:14:22 +02:00
commit 4002f145fc
9002 changed files with 1731834 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
#
# Copyright (C) 2018 Weijie Gao <hackpascal@gmail.com>
#
# Helper function to extract mac addresses from mtd part for Phicomm K2T
#
. /lib/functions.sh
. /lib/functions/system.sh
. /usr/share/libubox/jshn.sh
k2t_config_load() {
local mtd_blk=$(find_mtd_part config)
if [ -z "$mtd_blk" ]; then
echo "k2t_config_load: no mtd part named config" >&2
exit 1
fi
local json_size=$(dd if=$mtd_blk bs=1 count=8 2>/dev/null)
json_size="0x$json_size"
json_size=$((json_size))
if [ "$?" -ne 0 ]; then
echo "k2t_config_load: invalid json data size" >&2
exit 2
fi
if [ "$json_size" -eq 0 ]; then
echo "k2t_config_load: empty json data" >&2
exit 3
fi
local json_data=$(dd if=$mtd_blk bs=1 skip=8 count=$json_size 2>/dev/null)
json_load "$json_data"
}
k2t_get_mac() {
local old_ns
json_set_namespace "k2t" old_ns
if k2t_config_load; then
json_select "this_dev_info"
json_get_var val "$1"
json_select ..
fi
json_set_namespace old_ns
echo $val
}

View File

@@ -0,0 +1,29 @@
. /lib/functions.sh
preinit_set_mac_address() {
case $(board_name) in
avm,fritz1750e|\
avm,fritz450e|\
avm,fritzdvbc)
ip link set dev eth0 address $(fritz_tffs -n maca -i $(find_mtd_part "tffs (1)"))
;;
dlink,dap-2695-a1|\
dlink,dap-3662-a1)
ip link set dev eth0 address $(mtd_get_mac_ascii bdcfg "lanmac")
ip link set dev eth1 address $(mtd_get_mac_ascii bdcfg "wanmac")
;;
enterasys,ws-ap3705i)
ip link set dev eth0 address $(mtd_get_mac_ascii u-boot-env0 ethaddr)
;;
siemens,ws-ap3610)
ip link set dev eth0 address $(mtd_get_mac_ascii cfg1 ethaddr)
;;
zyxel,nbg6616)
ethaddr=$(mtd_get_mac_ascii u-boot-env ethaddr)
ip link set dev eth0 address $(macaddr_add $ethaddr 2)
ip link set dev eth1 address $(macaddr_add $ethaddr 3)
;;
esac
}
boot_hook_add preinit_main preinit_set_mac_address

View File

@@ -0,0 +1,105 @@
# The U-Boot loader with the datachk patchset for dualbooting requires image
# sizes and checksums to be provided in the U-Boot environment.
# The devices come with 2 main partitions - while one is active
# sysupgrade will flash the other. The boot order is changed to boot the
# newly flashed partition. If the new partition can't be booted due to
# upgrade failures the previously used partition is loaded.
platform_do_upgrade_dualboot_datachk() {
local tar_file="$1"
local restore_backup
local primary_kernel_mtd
local setenv_script="/tmp/fw_env_upgrade"
local inactive_mtd="$(find_mtd_index $PART_NAME)"
local inactive_offset="$(cat /sys/class/mtd/mtd${inactive_mtd}/offset)"
local total_size="$(cat /sys/class/mtd/mtd${inactive_mtd}/size)"
local flash_start_mem=0x9f000000
# detect to which flash region the new image is written to.
#
# 1. check what is the mtd index for the first flash region on this
# device
# 2. check if the target partition ("inactive") has the mtd index of
# the first flash region
#
# - when it is: the new bootseq will be 1,2 and the first region is
# modified
# - when it isnt: bootseq will be 2,1 and the second region is
# modified
#
# The detection has to be done via the hardcoded mtd partition because
# the current boot might be done with the fallback region. Let us
# assume that the current bootseq is 1,2. The bootloader detected that
# the image in flash region 1 is corrupt and thus switches to flash
# region 2. The bootseq in the u-boot-env is now still the same and
# the sysupgrade code can now only rely on the actual mtd indexes and
# not the bootseq variable to detect the currently booted flash
# region/image.
#
# In the above example, an implementation which uses bootseq ("1,2") to
# detect the currently booted image would assume that region 1 is booted
# and then overwrite the variables for the wrong flash region (aka the
# one which isn't modified). This could result in a device which doesn't
# boot anymore to Linux until it was reflashed with ap51-flash.
local next_boot_part="1"
case "$(board_name)" in
plasmacloud,pa300|\
plasmacloud,pa300e)
primary_kernel_mtd=3
;;
*)
echo "failed to detect primary kernel mtd partition for board"
return 1
;;
esac
[ "$inactive_mtd" = "$primary_kernel_mtd" ] || next_boot_part="2"
local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
board_dir=${board_dir%/}
local kernel_length=$(tar xf $tar_file ${board_dir}/kernel -O | wc -c)
local rootfs_length=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
# rootfs without EOF marker
rootfs_length=$((rootfs_length-4))
local kernel_md5=$(tar xf $tar_file ${board_dir}/kernel -O | md5sum); kernel_md5="${kernel_md5%% *}"
# md5 checksum of rootfs with EOF marker
local rootfs_md5=$(tar xf $tar_file ${board_dir}/root -O | dd bs=1 count=$rootfs_length | md5sum); rootfs_md5="${rootfs_md5%% *}"
#
# add tar support to get_image() to use default_do_upgrade() instead?
#
# take care of restoring a saved config
[ -n "$UPGRADE_BACKUP" ] && restore_backup="${MTD_CONFIG_ARGS} -j ${UPGRADE_BACKUP}"
mtd -q erase inactive
tar xf $tar_file ${board_dir}/root -O | mtd -n -p $kernel_length $restore_backup write - $PART_NAME
tar xf $tar_file ${board_dir}/kernel -O | mtd -n write - $PART_NAME
# prepare new u-boot env
if [ "$next_boot_part" = "1" ]; then
echo "bootseq 1,2" > $setenv_script
else
echo "bootseq 2,1" > $setenv_script
fi
printf "kernel_size_%i %i\n" $next_boot_part $((kernel_length / 1024)) >> $setenv_script
printf "vmlinux_start_addr 0x%08x\n" $((flash_start_mem + inactive_offset)) >> $setenv_script
printf "vmlinux_size 0x%08x\n" ${kernel_length} >> $setenv_script
printf "vmlinux_checksum %s\n" ${kernel_md5} >> $setenv_script
printf "rootfs_size_%i %i\n" $next_boot_part $(((total_size-kernel_length) / 1024)) >> $setenv_script
printf "rootfs_start_addr 0x%08x\n" $((flash_start_mem+inactive_offset+kernel_length)) >> $setenv_script
printf "rootfs_size 0x%08x\n" ${rootfs_length} >> $setenv_script
printf "rootfs_checksum %s\n" ${rootfs_md5} >> $setenv_script
# store u-boot env changes
mkdir -p /var/lock
fw_setenv -s $setenv_script || {
echo "failed to update U-Boot environment"
return 1
}
}

View File

@@ -0,0 +1,64 @@
# U-Boot with the datachk patchset requires image sizes, offsets,
# and checksums to be provided in the U-Boot environment.
# This script is based on the dualboot version for devices that come with 2 OS partitions.
# For Senao boards with a "failsafe" partition image, the process is almost the same.
# Instead of booting a secondary instalation on checksum failure,
# the failsafe image is booted instead.
# These boards also use the OKLI lzma kernel loader and mtd-concat
# So the kernel check is for the loader, the rootfs check is for kernel + rootfs
platform_do_upgrade_failsafe_datachk() {
local setenv_script="/tmp/fw_env_upgrade"
local flash_base=0x9f000000
local kernel_mtd=$(find_mtd_index ${KERNEL_PART:-kernel})
local rootfs_mtd=$(find_mtd_index ${ROOTFS_PART:-rootfs})
local kernel_offset=$(cat /sys/class/mtd/mtd${kernel_mtd}/offset)
local rootfs_offset=$(cat /sys/class/mtd/mtd${rootfs_mtd}/offset)
if [ -n "$IMAGE_LIST" ]; then
KERNEL_FILE=$($IMAGE_LIST | grep $KERNEL_FILE)
ROOTFS_FILE=$($IMAGE_LIST | grep $ROOTFS_FILE)
fi
local kernel_size=$($IMAGE_CMD $KERNEL_FILE | wc -c)
local rootfs_size=$($IMAGE_CMD $ROOTFS_FILE | wc -c)
# rootfs without JFFS2
local rootfs_blocks=$((rootfs_size / 4096))
rootfs_size=$((rootfs_blocks * 4096))
local kernel_md5=$($IMAGE_CMD $KERNEL_FILE | md5sum | cut -d ' ' -f1)
local rootfs_md5=$($IMAGE_CMD $ROOTFS_FILE | dd bs=4k count=$rootfs_blocks iflag=fullblock | md5sum | cut -d ' ' -f1)
# prepare new u-boot-env vars
printf "vmlinux_start_addr 0x%08x\n" $((flash_base + kernel_offset)) >> $setenv_script
printf "vmlinux_size 0x%08x\n" ${kernel_size} >> $setenv_script
printf "vmlinux_checksum %s\n" ${kernel_md5} >> $setenv_script
printf "rootfs_start_addr 0x%08x\n" $((flash_base + rootfs_offset)) >> $setenv_script
printf "rootfs_size 0x%08x\n" ${rootfs_size} >> $setenv_script
printf "rootfs_checksum %s\n" ${rootfs_md5} >> $setenv_script
# store u-boot-env
mkdir -p /var/lock
fw_setenv -s $setenv_script || {
echo 'failed to update U-Boot environment'
exit 1
}
# sysupgrade
sleep 2
sync
echo 3 > /proc/sys/vm/drop_caches
$IMAGE_CMD $KERNEL_FILE | mtd $MTD_ARGS write - ${KERNEL_PART:-kernel}
sleep 2
sync
if [ -n "$UPGRADE_BACKUP" ]; then
$IMAGE_CMD $ROOTFS_FILE | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j $UPGRADE_BACKUP write - ${ROOTFS_PART:-rootfs}
else
$IMAGE_CMD $ROOTFS_FILE | mtd $MTD_ARGS write - ${ROOTFS_PART:-rootfs}
fi
}

View File

@@ -0,0 +1,114 @@
# The U-Boot loader of the OpenMesh devices requires image sizes and
# checksums to be provided in the U-Boot environment.
# The OpenMesh devices come with 2 main partitions - while one is active
# sysupgrade will flash the other. The boot order is changed to boot the
# newly flashed partition. If the new partition can't be booted due to
# upgrade failures the previously used partition is loaded.
cfg_value_get()
{
local cfg=$1 cfg_opt
local section=$2 our_section=0
local param=$3 our_param=
for cfg_opt in $cfg; do
[ "$cfg_opt" = "[$section]" ] && our_section=1 && continue
[ "$our_section" = "1" ] || continue
our_param=${cfg_opt%%=*}
[ "$param" = "$our_param" ] && echo ${cfg_opt##*=} && break
done
}
platform_do_upgrade_openmesh()
{
local img_path="$1"
local restore_backup
local setenv_script="/tmp/fw_env_upgrade"
local inactive_mtd="$(find_mtd_index $PART_NAME)"
local inactive_offset="$(cat /sys/class/mtd/mtd${inactive_mtd}/offset)"
local total_size="$(cat /sys/class/mtd/mtd${inactive_mtd}/size)"
local total_kbs=$((total_size / 1024))
local flash_start_mem=0x9f000000
local data_offset=$((64 * 1024))
# detect to which flash region the new image is written to.
#
# 1. check what is the mtd index for the first flash region on this
# device
# 2. check if the target partition ("inactive") has the mtd index of
# the first flash region
#
# - when it is: the new bootseq will be 1,2 and the first region is
# modified
# - when it isn't: bootseq will be 2,1 and the second region is
# modified
#
# The detection has to be done via the hardcoded mtd partition because
# the current boot might be done with the fallback region. Let us
# assume that the current bootseq is 1,2. The bootloader detected that
# the image in flash region 1 is corrupt and thus switches to flash
# region 2. The bootseq in the u-boot-env is now still the same and
# the sysupgrade code can now only rely on the actual mtd indexes and
# not the bootseq variable to detect the currently booted flash
# region/image.
#
# In the above example, an implementation which uses bootseq ("1,2") to
# detect the currently booted image would assume that region 1 is booted
# and then overwrite the variables for the wrong flash region (aka the
# one which isn't modified). This could result in a device which doesn't
# boot anymore to Linux until it was reflashed with ap51-flash.
local next_boot_part="1"
local primary_kernel_mtd="3"
[ "$inactive_mtd" = "$primary_kernel_mtd" ] || next_boot_part="2"
local cfg_size=$(dd if="$img_path" bs=8 skip=70 count=1 iflag=skip_bytes 2>/dev/null)
local cfg_length=$((0x$cfg_size))
local cfg_content=$(dd if="$img_path" bs=$cfg_length skip=$data_offset count=1 iflag=skip_bytes 2>/dev/null)
local kernel_size=$(dd if="$img_path" bs=8 skip=142 count=1 iflag=skip_bytes 2>/dev/null)
local kernel_length=$((0x$kernel_size))
local kernel_kbs=$((kernel_length / 1024))
local kernel_md5=$(cfg_value_get "$cfg_content" "vmlinux" "md5sum")
local rootfs_size=$(dd if="$img_path" bs=8 skip=214 count=1 iflag=skip_bytes 2>/dev/null)
local rootfs_length=$((0x$rootfs_size))
local rootfs_md5=$(cfg_value_get "$cfg_content" "rootfs" "md5sum")
local rootfs_checksize=$(cfg_value_get "$cfg_content" "rootfs" "checksize")
# take care of restoring a saved config
[ -n "$UPGRADE_BACKUP" ] && restore_backup="${MTD_CONFIG_ARGS} -j ${UPGRADE_BACKUP}"
# write image parts
mtd -q erase inactive
dd if="$img_path" bs=1 skip=$((data_offset + cfg_length + kernel_length)) count=$rootfs_length 2>&- | \
mtd -n -p $kernel_length $restore_backup write - $PART_NAME
dd if="$img_path" bs=1024 skip=$((data_offset + cfg_length)) count=$kernel_kbs iflag=skip_bytes 2>&- | \
mtd -n write - $PART_NAME
# prepare new u-boot env
if [ "$next_boot_part" = "1" ]; then
echo "bootseq 1,2" > $setenv_script
else
echo "bootseq 2,1" > $setenv_script
fi
printf "kernel_size_%i %i\n" $next_boot_part $kernel_kbs >> $setenv_script
printf "vmlinux_start_addr 0x%08x\n" $((flash_start_mem + inactive_offset)) >> $setenv_script
printf "vmlinux_size 0x%08x\n" ${kernel_length} >> $setenv_script
printf "vmlinux_checksum %s\n" ${kernel_md5} >> $setenv_script
printf "rootfs_size_%i %i\n" $next_boot_part $((total_kbs - kernel_kbs)) >> $setenv_script
printf "rootfs_start_addr 0x%08x\n" $((flash_start_mem+inactive_offset+kernel_length)) >> $setenv_script
printf "rootfs_size %s\n" $rootfs_checksize >> $setenv_script
printf "rootfs_checksum %s\n" ${rootfs_md5} >> $setenv_script
# store u-boot env changes
mkdir -p /var/lock
fw_setenv -s $setenv_script || {
echo "failed to update U-Boot environment"
return 1
}
}

View File

@@ -0,0 +1,100 @@
#
# Copyright (C) 2011 OpenWrt.org
#
PART_NAME=firmware
REQUIRE_IMAGE_METADATA=1
RAMFS_COPY_BIN='fw_printenv fw_setenv'
RAMFS_COPY_DATA='/etc/fw_env.config /var/lock/fw_printenv.lock'
redboot_fis_do_upgrade() {
local append
local sysup_file="$1"
local kern_part="$2"
local magic=$(get_magic_word "$sysup_file")
if [ "$magic" = "4349" ]; then
local kern_length=0x$(dd if="$sysup_file" bs=2 skip=1 count=4 2>/dev/null)
[ -f "$UPGRADE_BACKUP" ] && append="-j $UPGRADE_BACKUP"
dd if="$sysup_file" bs=64k skip=1 2>/dev/null | \
mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
elif [ "$magic" = "7379" ]; then
local board_dir=$(tar tf $sysup_file | grep -m 1 '^sysupgrade-.*/$')
local kern_length=$(tar xf $sysup_file ${board_dir}kernel -O | wc -c)
[ -f "$UPGRADE_BACKUP" ] && append="-j $UPGRADE_BACKUP"
tar xf $sysup_file ${board_dir}kernel ${board_dir}root -O | \
mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
else
echo "Unknown image, aborting!"
return 1
fi
}
platform_check_image() {
return 0
}
platform_do_upgrade() {
local board=$(board_name)
case "$board" in
adtran,bsap1800-v2|\
adtran,bsap1840)
redboot_fis_do_upgrade "$1" vmlinux_2
;;
allnet,all-wap02860ac|\
engenius,eap1200h|\
engenius,eap300-v2|\
engenius,eap600|\
engenius,ecb600|\
engenius,ens202ext-v1|\
engenius,enstationac-v1)
IMAGE_LIST="tar tzf $1"
IMAGE_CMD="tar xzOf $1"
KERNEL_PART="loader"
ROOTFS_PART="fwconcat0"
KERNEL_FILE="uImage-lzma.bin"
ROOTFS_FILE="root.squashfs"
platform_do_upgrade_failsafe_datachk "$1"
;;
jjplus,ja76pf2)
redboot_fis_do_upgrade "$1" linux
;;
openmesh,a40|\
openmesh,a60|\
openmesh,mr600-v1|\
openmesh,mr600-v2|\
openmesh,mr900-v1|\
openmesh,mr900-v2|\
openmesh,mr1750-v1|\
openmesh,mr1750-v2|\
openmesh,om2p-v2|\
openmesh,om2p-v4|\
openmesh,om2p-hs-v1|\
openmesh,om2p-hs-v2|\
openmesh,om2p-hs-v3|\
openmesh,om2p-hs-v4|\
openmesh,om2p-lc|\
openmesh,om5p)
PART_NAME="inactive"
platform_do_upgrade_openmesh "$1"
;;
plasmacloud,pa300|\
plasmacloud,pa300e)
PART_NAME="inactive"
platform_do_upgrade_dualboot_datachk "$1"
;;
ubnt,routerstation|\
ubnt,routerstation-pro)
redboot_fis_do_upgrade "$1" kernel
;;
*)
default_do_upgrade "$1"
;;
esac
}