2014-06-11 12:59:49 +00:00
|
|
|
# Copyright (C) 2014 OpenWrt.org
|
|
|
|
#
|
|
|
|
|
|
|
|
. /lib/functions.sh
|
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
# 'kernel' partition or UBI volume on NAND contains the kernel
|
2016-11-20 02:33:03 +00:00
|
|
|
CI_KERNPART="${CI_KERNPART:-kernel}"
|
2014-07-14 14:53:15 +00:00
|
|
|
|
2014-06-24 18:13:56 +00:00
|
|
|
# 'ubi' partition on NAND contains UBI
|
2016-11-20 02:33:03 +00:00
|
|
|
CI_UBIPART="${CI_UBIPART:-ubi}"
|
2014-06-11 12:59:49 +00:00
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
# 'rootfs' UBI volume on NAND contains the rootfs
|
2019-01-01 00:15:34 +00:00
|
|
|
CI_ROOTPART="${CI_ROOTPART:-rootfs}"
|
|
|
|
|
2014-10-12 15:50:15 +00:00
|
|
|
ubi_mknod() {
|
|
|
|
local dir="$1"
|
|
|
|
local dev="/dev/$(basename $dir)"
|
|
|
|
|
|
|
|
[ -e "$dev" ] && return 0
|
|
|
|
|
|
|
|
local devid="$(cat $dir/dev)"
|
|
|
|
local major="${devid%%:*}"
|
|
|
|
local minor="${devid##*:}"
|
|
|
|
mknod "$dev" c $major $minor
|
|
|
|
}
|
|
|
|
|
2014-06-11 12:59:49 +00:00
|
|
|
nand_find_volume() {
|
|
|
|
local ubidevdir ubivoldir
|
|
|
|
ubidevdir="/sys/devices/virtual/ubi/$1"
|
|
|
|
[ ! -d "$ubidevdir" ] && return 1
|
|
|
|
for ubivoldir in $ubidevdir/${1}_*; do
|
|
|
|
[ ! -d "$ubivoldir" ] && continue
|
|
|
|
if [ "$( cat $ubivoldir/name )" = "$2" ]; then
|
|
|
|
basename $ubivoldir
|
2014-10-12 15:50:15 +00:00
|
|
|
ubi_mknod "$ubivoldir"
|
2014-06-11 12:59:49 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
nand_find_ubi() {
|
|
|
|
local ubidevdir ubidev mtdnum
|
|
|
|
mtdnum="$( find_mtd_index $1 )"
|
|
|
|
[ ! "$mtdnum" ] && return 1
|
|
|
|
for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
|
|
|
|
[ ! -d "$ubidevdir" ] && continue
|
|
|
|
cmtdnum="$( cat $ubidevdir/mtd_num )"
|
|
|
|
[ ! "$mtdnum" ] && continue
|
|
|
|
if [ "$mtdnum" = "$cmtdnum" ]; then
|
|
|
|
ubidev=$( basename $ubidevdir )
|
2014-10-12 15:50:15 +00:00
|
|
|
ubi_mknod "$ubidevdir"
|
2014-06-11 12:59:49 +00:00
|
|
|
echo $ubidev
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-07-13 20:51:17 +00:00
|
|
|
nand_get_magic_long() {
|
2014-06-16 18:55:14 +00:00
|
|
|
dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
|
2014-06-11 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
get_magic_long_tar() {
|
|
|
|
( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
|
2014-06-11 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
identify_magic() {
|
|
|
|
local magic=$1
|
2014-06-11 12:59:49 +00:00
|
|
|
case "$magic" in
|
|
|
|
"55424923")
|
|
|
|
echo "ubi"
|
|
|
|
;;
|
|
|
|
"31181006")
|
|
|
|
echo "ubifs"
|
|
|
|
;;
|
|
|
|
"68737173")
|
|
|
|
echo "squashfs"
|
|
|
|
;;
|
|
|
|
"d00dfeed")
|
|
|
|
echo "fit"
|
|
|
|
;;
|
|
|
|
"4349"*)
|
|
|
|
echo "combined"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "unknown $magic"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
identify() {
|
2014-07-13 20:51:17 +00:00
|
|
|
identify_magic $(nand_get_magic_long "$1" "${2:-0}")
|
2014-06-16 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
identify_tar() {
|
|
|
|
identify_magic $(get_magic_long_tar "$1" "$2")
|
|
|
|
}
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_restore_config() {
|
2022-04-15 10:18:22 +00:00
|
|
|
local ubidev=$( nand_find_ubi "$CI_UBIPART" )
|
2014-06-16 18:55:14 +00:00
|
|
|
local ubivol="$( nand_find_volume $ubidev rootfs_data )"
|
2022-04-15 13:11:52 +00:00
|
|
|
if [ ! "$ubivol" ]; then
|
2022-04-15 10:18:22 +00:00
|
|
|
ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
|
2022-04-15 13:11:52 +00:00
|
|
|
if [ ! "$ubivol" ]; then
|
|
|
|
echo "cannot find ubifs data volume"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
2014-06-16 18:55:14 +00:00
|
|
|
mkdir /tmp/new_root
|
|
|
|
if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
|
2022-04-15 13:11:52 +00:00
|
|
|
echo "cannot mount ubifs volume $ubivol"
|
2014-06-16 18:55:14 +00:00
|
|
|
rmdir /tmp/new_root
|
|
|
|
return 1
|
|
|
|
fi
|
2022-04-15 13:11:52 +00:00
|
|
|
if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
|
|
|
|
if umount /tmp/new_root; then
|
|
|
|
echo "configuration saved"
|
|
|
|
rmdir /tmp/new_root
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
umount /tmp/new_root
|
|
|
|
fi
|
|
|
|
echo "could not save configuration to ubifs volume $ubivol"
|
2014-06-16 18:55:14 +00:00
|
|
|
rmdir /tmp/new_root
|
2022-04-15 13:11:52 +00:00
|
|
|
return 1
|
2014-06-16 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 03:52:12 +00:00
|
|
|
nand_remove_ubiblock() {
|
|
|
|
local ubivol=$1
|
|
|
|
local ubiblk=ubiblock${ubivol:3}
|
|
|
|
if [ -e /dev/$ubiblk ]; then
|
|
|
|
echo "removing $ubiblk"
|
|
|
|
if ! ubiblock -r /dev/$ubivol; then
|
|
|
|
echo "cannot remove $ubiblk"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_upgrade_prepare_ubi() {
|
|
|
|
local rootfs_length="$1"
|
2014-06-19 14:13:41 +00:00
|
|
|
local rootfs_type="$2"
|
2021-02-17 15:17:49 +00:00
|
|
|
local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
|
2021-02-24 11:40:50 +00:00
|
|
|
[ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
|
2021-02-17 15:17:49 +00:00
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
local kernel_length="$3"
|
2014-06-19 14:13:41 +00:00
|
|
|
local has_env="${4:-0}"
|
2014-06-16 18:55:14 +00:00
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
[ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
|
2014-06-11 12:59:49 +00:00
|
|
|
if [ ! "$mtdnum" ]; then
|
|
|
|
echo "cannot find ubi mtd partition $CI_UBIPART"
|
|
|
|
return 1
|
|
|
|
fi
|
2014-06-16 18:55:14 +00:00
|
|
|
|
2014-06-11 12:59:49 +00:00
|
|
|
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
|
|
|
if [ ! "$ubidev" ]; then
|
|
|
|
ubiattach -m "$mtdnum"
|
|
|
|
ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
2014-06-16 18:55:14 +00:00
|
|
|
|
2022-04-15 03:44:26 +00:00
|
|
|
if [ ! "$ubidev" ]; then
|
|
|
|
ubiformat /dev/mtd$mtdnum -y
|
|
|
|
ubiattach -m "$mtdnum"
|
|
|
|
ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
|
|
|
|
|
|
|
if [ ! "$ubidev" ]; then
|
|
|
|
echo "cannot attach ubi mtd partition $CI_UBIPART"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$has_env" -gt 0 ]; then
|
|
|
|
ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
|
|
|
|
ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
|
|
|
|
fi
|
|
|
|
fi
|
2014-06-11 12:59:49 +00:00
|
|
|
fi
|
2014-06-16 18:55:14 +00:00
|
|
|
|
2022-04-15 10:18:22 +00:00
|
|
|
local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
|
|
|
|
local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
|
2014-06-11 12:59:49 +00:00
|
|
|
local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
|
2022-04-15 03:52:12 +00:00
|
|
|
[ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2022-04-15 03:52:12 +00:00
|
|
|
# remove ubiblocks
|
|
|
|
[ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
|
|
|
|
[ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
|
|
|
|
[ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
|
2014-06-11 12:59:49 +00:00
|
|
|
|
|
|
|
# kill volumes
|
2022-04-15 10:18:22 +00:00
|
|
|
[ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_KERNPART" || :
|
|
|
|
[ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_ROOTPART" || :
|
2022-04-13 13:51:34 +00:00
|
|
|
[ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || :
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2022-04-15 03:53:27 +00:00
|
|
|
# create kernel vol
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
if [ -n "$kernel_length" ]; then
|
2022-04-15 10:18:22 +00:00
|
|
|
if ! ubimkvol /dev/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
|
2014-06-11 12:59:49 +00:00
|
|
|
echo "cannot create kernel volume"
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-04-15 03:53:27 +00:00
|
|
|
# create rootfs vol
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
if [ -n "$rootfs_length" ]; then
|
2021-02-17 15:17:49 +00:00
|
|
|
local rootfs_size_param
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
if [ "$rootfs_type" = "ubifs" ]; then
|
2021-02-17 15:17:49 +00:00
|
|
|
rootfs_size_param="-m"
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
else
|
2021-02-17 15:17:49 +00:00
|
|
|
rootfs_size_param="-s $rootfs_length"
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
fi
|
2022-04-15 10:18:22 +00:00
|
|
|
if ! ubimkvol /dev/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
echo "cannot create rootfs volume"
|
|
|
|
return 1;
|
|
|
|
fi
|
2014-06-11 12:59:49 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-15 03:53:27 +00:00
|
|
|
# create rootfs_data vol for non-ubifs rootfs
|
2014-06-16 18:55:14 +00:00
|
|
|
if [ "$rootfs_type" != "ubifs" ]; then
|
2021-02-17 15:17:49 +00:00
|
|
|
local rootfs_data_size_param="-m"
|
2022-02-17 15:10:00 +00:00
|
|
|
if [ -n "$rootfs_data_max" ]; then
|
2021-02-17 15:17:49 +00:00
|
|
|
rootfs_data_size_param="-s $rootfs_data_max"
|
|
|
|
fi
|
|
|
|
if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
|
2022-02-17 15:10:00 +00:00
|
|
|
if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
|
|
|
|
echo "cannot initialize rootfs_data volume"
|
|
|
|
return 1
|
|
|
|
fi
|
2014-06-11 12:59:49 +00:00
|
|
|
fi
|
|
|
|
fi
|
2022-04-15 13:15:02 +00:00
|
|
|
|
2014-06-11 12:59:49 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_do_upgrade_success() {
|
|
|
|
local conf_tar="/tmp/sysupgrade.tgz"
|
2022-04-15 13:11:52 +00:00
|
|
|
if { [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"; } && sync; then
|
|
|
|
echo "sysupgrade successful"
|
|
|
|
fi
|
2016-08-02 20:29:31 +00:00
|
|
|
umount -a
|
2014-06-16 18:55:14 +00:00
|
|
|
reboot -f
|
|
|
|
}
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2014-11-24 19:14:17 +00:00
|
|
|
# Flash the UBI image to MTD partition
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_upgrade_ubinized() {
|
|
|
|
local ubi_file="$1"
|
|
|
|
local mtdnum="$(find_mtd_index "$CI_UBIPART")"
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
if [ ! "$mtdnum" ]; then
|
|
|
|
echo "cannot find mtd device $CI_UBIPART"
|
2016-08-02 20:29:31 +00:00
|
|
|
umount -a
|
2014-06-17 15:40:02 +00:00
|
|
|
reboot -f
|
2014-06-16 18:55:14 +00:00
|
|
|
fi
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
local mtddev="/dev/mtd${mtdnum}"
|
2022-04-15 09:42:51 +00:00
|
|
|
ubidetach -p "${mtddev}" || :
|
2014-06-16 18:55:14 +00:00
|
|
|
ubiformat "${mtddev}" -y -f "${ubi_file}"
|
|
|
|
ubiattach -p "${mtddev}"
|
2022-04-15 09:42:51 +00:00
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_do_upgrade_success
|
2014-06-11 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:14:17 +00:00
|
|
|
# Write the UBIFS image to UBI volume
|
2014-06-19 14:13:41 +00:00
|
|
|
nand_upgrade_ubifs() {
|
2020-05-10 14:20:55 +00:00
|
|
|
local rootfs_length=$( (cat $1 | wc -c) 2> /dev/null)
|
2014-06-11 12:59:49 +00:00
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "" ""
|
2017-05-03 06:27:40 +00:00
|
|
|
|
2014-06-19 14:13:41 +00:00
|
|
|
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
2022-04-15 10:18:22 +00:00
|
|
|
local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
|
2014-06-19 14:13:41 +00:00
|
|
|
ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
|
|
|
|
|
|
|
|
nand_do_upgrade_success
|
|
|
|
}
|
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
nand_upgrade_fit() {
|
|
|
|
local fit_file="$1"
|
|
|
|
local fit_length="$(wc -c < "$fit_file")"
|
|
|
|
|
|
|
|
nand_upgrade_prepare_ubi "" "" "$fit_length" "1"
|
|
|
|
|
|
|
|
local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
|
|
|
|
local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
|
|
|
|
ubiupdatevol /dev/$fit_ubivol -s $fit_length $fit_file
|
|
|
|
|
|
|
|
nand_do_upgrade_success
|
|
|
|
}
|
|
|
|
|
2014-06-19 14:13:41 +00:00
|
|
|
nand_upgrade_tar() {
|
2014-06-16 18:55:14 +00:00
|
|
|
local tar_file="$1"
|
2014-06-11 12:59:49 +00:00
|
|
|
|
2022-04-15 09:31:24 +00:00
|
|
|
local board_dir="$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
|
|
|
|
board_dir="${board_dir%/}"
|
2014-06-17 15:40:02 +00:00
|
|
|
|
2022-04-15 09:31:24 +00:00
|
|
|
local kernel_mtd kernel_length
|
|
|
|
if [ "$CI_KERNPART" != "none" ]; then
|
|
|
|
kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
|
|
|
|
kernel_length=$( (tar xf "$tar_file" "$board_dir/kernel" -O | wc -c) 2> /dev/null)
|
|
|
|
[ "$kernel_length" = 0 ] && kernel_length=
|
|
|
|
fi
|
|
|
|
local rootfs_length=$( (tar xf "$tar_file" "$board_dir/root" -O | wc -c) 2> /dev/null)
|
|
|
|
[ "$rootfs_length" = 0 ] && rootfs_length=
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
local rootfs_type
|
2022-04-15 09:31:24 +00:00
|
|
|
[ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root")"
|
2017-04-07 06:39:59 +00:00
|
|
|
|
2022-04-15 09:31:24 +00:00
|
|
|
local ubi_kernel_length
|
|
|
|
if [ "$kernel_length" ]; then
|
|
|
|
if [ "$kernel_mtd" ]; then
|
2022-05-02 08:33:01 +00:00
|
|
|
# On some devices, the raw kernel and ubi partitions overlap.
|
|
|
|
# These devices brick if the kernel partition is erased.
|
|
|
|
# Hence only invalidate kernel for now.
|
|
|
|
dd if=/dev/zero bs=4096 count=1 2>/dev/null | \
|
|
|
|
mtd write - "$CI_KERNPART"
|
2022-04-15 09:31:24 +00:00
|
|
|
else
|
|
|
|
ubi_kernel_length="$kernel_length"
|
|
|
|
fi
|
|
|
|
fi
|
2014-06-16 18:55:14 +00:00
|
|
|
local has_env=0
|
2022-04-15 09:31:24 +00:00
|
|
|
nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env"
|
2014-06-16 18:55:14 +00:00
|
|
|
|
|
|
|
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
2022-04-15 09:31:24 +00:00
|
|
|
if [ "$rootfs_length" ]; then
|
|
|
|
local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
|
|
|
|
tar xf "$tar_file" "$board_dir/root" -O | \
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
|
2022-04-15 09:31:24 +00:00
|
|
|
fi
|
|
|
|
if [ "$kernel_length" ]; then
|
|
|
|
if [ "$kernel_mtd" ]; then
|
|
|
|
tar xf "$tar_file" "$board_dir/kernel" -O | \
|
2022-05-02 08:33:01 +00:00
|
|
|
mtd write - "$CI_KERNPART"
|
2022-04-15 09:31:24 +00:00
|
|
|
else
|
|
|
|
local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
|
|
|
|
tar xf "$tar_file" "$board_dir/kernel" -O | \
|
|
|
|
ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
|
|
|
|
fi
|
|
|
|
fi
|
2022-04-15 03:59:49 +00:00
|
|
|
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_do_upgrade_success
|
2014-06-11 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:14:17 +00:00
|
|
|
# Recognize type of passed file and start the upgrade process
|
2017-04-21 22:54:50 +00:00
|
|
|
nand_do_upgrade() {
|
2022-04-15 10:20:28 +00:00
|
|
|
local file_type=$(identify "$1")
|
2014-06-19 14:13:41 +00:00
|
|
|
|
2022-04-15 09:42:51 +00:00
|
|
|
[ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
|
2014-06-19 14:13:41 +00:00
|
|
|
|
2022-04-15 10:20:28 +00:00
|
|
|
sync
|
2014-11-24 19:14:29 +00:00
|
|
|
case "$file_type" in
|
2022-04-15 10:20:28 +00:00
|
|
|
"fit") nand_upgrade_fit "$1";;
|
|
|
|
"ubi") nand_upgrade_ubinized "$1";;
|
|
|
|
"ubifs") nand_upgrade_ubifs "$1";;
|
|
|
|
*) nand_upgrade_tar "$1";;
|
2014-11-24 19:14:29 +00:00
|
|
|
esac
|
2014-06-19 14:13:41 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:14:17 +00:00
|
|
|
# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
|
|
|
|
# 3 types of files:
|
|
|
|
# 1) UBI - should contain an ubinized image, header is checked for the proper
|
|
|
|
# MAGIC
|
|
|
|
# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
|
|
|
|
# header is checked for the proper MAGIC
|
2014-11-28 19:13:24 +00:00
|
|
|
# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
|
2014-11-24 19:14:17 +00:00
|
|
|
# "CONTROL" file (at this point its content isn't verified)
|
|
|
|
#
|
|
|
|
# You usually want to call this function in platform_check_image.
|
|
|
|
#
|
2014-11-28 19:13:24 +00:00
|
|
|
# $(1): board name, used in case of passing TAR file
|
2014-11-24 19:14:17 +00:00
|
|
|
# $(2): file to be checked
|
2014-06-16 18:55:14 +00:00
|
|
|
nand_do_platform_check() {
|
|
|
|
local board_name="$1"
|
|
|
|
local tar_file="$2"
|
2020-05-10 14:20:55 +00:00
|
|
|
local control_length=$( (tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null)
|
2014-06-19 14:13:41 +00:00
|
|
|
local file_type="$(identify $2)"
|
|
|
|
|
image: add support for building FIT image with filesystem
Allow for single (external-data) FIT image to hold kernel, dtb and
squashfs. In that way, the bootloader verifies the system integrity
including the rootfs, because what's the point of checking that the
hash of the kernel is correct if it won't boot in case of squashfs
being corrupted? Better allow bootloader to check everything needed
to make it at least up to failsafe mode. As a positive side effect
this change also makes the sysupgrade process on nand potentially
much easier as it is now.
In short: mkimage has a parameter '-E' which allows generating FIT
images with 'external' data rather than embedding the data into the
device-tree blob itself. In this way, the FIT structure itself remains
small and can be parsed easily (rather than having to page around
megabytes of image content). This patch makes use of that and adds
support for adding sub-images of type 'filesystem' which are used to
store the squashfs. Now U-Boot can verify the whole OS and the new
partition parsers added in the Linux kernel can detect the filesystem
sub-images, create partitions for them, and select the active rootfs
volume based on the configuration in FIT (passing configuration via
device tree could be implemented easily at a later stage).
This new FIT partition parser works for NOR flash (on top of mtdblock),
NAND flash (on top of ubiblock) as well as classic block devices
(ie. eMMC, SDcard, SATA, NVME, ...).
It could even be used to mount such FIT images via `losetup -P` on a
user PC if this patch gets included in Linux upstream one day ;)
Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-02-15 14:37:17 +00:00
|
|
|
[ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
|
2014-06-16 18:55:14 +00:00
|
|
|
echo "Invalid sysupgrade file."
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|