Why is the Sysupgrade uncoditionally writing images to partitions?

I have a nbg7815. My device ended up with unbootable kernel twice with self compiled openwrt for no reason. After a bit of research, it turned out that Sysupgrade doesn't check if the kernel image fits to the boot partition before writing, which leads to a soft brick.

I had to write a small script to check kernel image.

❯ sh scripts/check-kernel-size.sh /media/storage/devices/OpenWRT/nbg7815/openwrt/bin/targets/qualcommax/ipq807x/openwrt-qualcommax-ipq807x-zyxel_nbg7815-squashfs-sysupgrade.bin
image:    /media/storage/devices/OpenWRT/nbg7815/openwrt/bin/targets/qualcommax/ipq807x/openwrt-qualcommax-ipq807x-zyxel_nbg7815-squashfs-sysupgrade.bin
kernel:   6157904 bytes
limit:    6291456 bytes
headroom: 133552 bytes
OK: kernel fits

Devices like nbg7815, p2812 have very little headroom in their boot partitions. Shouldn't Sysupgrade check the size before writing?

If I remember right from the time where ARV7519RW22 was officially supported, that check is done, but is done at compile time, not at sysupgrade.

If you can compile with no errors perhaps the definition of the max size for the kernel is wrong or missing for your device.

You are right, KERNEL_SIZE := missing for nbg7815 in target/linux/qualcommax/image/ipq807x.mk

define Device/zyxel_nbg7815
	$(call Device/FitImage)
	$(call Device/EmmcImage)
	DEVICE_VENDOR := ZYXEL
	DEVICE_MODEL := NBG7815
	DEVICE_DTS_CONFIG := config@nbg7815
	SOC := ipq8074
	DEVICE_PACKAGES := kmod-fs-f2fs f2fs-tools ipq-wifi-zyxel_nbg7815 kmod-ath11k-pci \
		kmod-hci-uart kmod-hwmon-tmp103 kmod-hwmon-gpiofan \
		kmod-leds-lp5569 \
		kmod-fs-ext4 e2fsprogs fdisk blkid block-mount jsonfilter
endef

You could do a PR https://github.com/openwrt/openwrt/pulls setting it. Make sure 6291456 bytes is the real limit.

sent :+1: https://github.com/openwrt/openwrt/pull/26476

Mark your post as the solution of the thread.

But I still think it's better for sysupgrade to check the target partition size before writing.

I'm not sure if originally that design is because of any kind of reasoning or is just because the develop of sysupgrade evolved as is.

Just to say, if someone mod hacks it's device controlling in sysupgrade would only difficult while being only controlled at compilation time a person who increased the internal storage could just compile a custom firmware that simply works for him with more kernel_size space.

I have this check for nbg7815, but I think sysupgrade needs a more generic solution https://github.com/sinancetinkaya-org/openwrt/commit/5c5080b260019e28beaec7002c250e609b14d640

Interesting detecting dynamically the space, your script makes my argument to not be valid.

Not.

This is true only for generic build for original device.

Imagine you’ve replaced a memory chip with one that has a larger capacity, and now you want to upgrade to a "large" kernel image, sysupgrade might write everything without checking the current memory capacity—you need to verify this yourself.

That's what the script done by @sinancetinkaya does, it checks the real size of the device so it doesn't matter if you modded it.