Change kernel partition

Hello!
Each openwrt version upgrade I have an issue with kernel. There is mtd layout:

root@OpenWrt:~# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00080000 00020000 "Bootloader"
mtd1: 00040000 00020000 "Config"
mtd2: 00040000 00020000 "Bdata"
mtd3: 00040000 00020000 "factory"
mtd4: 00040000 00020000 "crash"
mtd5: 00040000 00020000 "crash_syslog"
mtd6: 00040000 00020000 "reserved0"
mtd7: 00400000 00020000 "kernel_stock"
mtd8: 00400000 00020000 "kernel"
mtd9: 07580000 00020000 "ubi"

So my router uses kernel_stock from mtd7 but sysupgrade install new kernel into mtd8. Each time I have to manually write new kernel into mtd7. Is there a way to change kernel partition?

It would be very helpful to know the make and model of the device you are having issues with ...

Searching for your partition map with a search engine leads me to believe that your device is a Xiaomi Mi Router 3 Pro or similar. I suppose you didn't follow the installation instructions thoroughly that contained some commands to change the partition the firmware is booted from. Please refer to the Wiki page for the instructions, I assume the following works.

fw_setenv flag_try_sys1_failed 1
fw_setenv flag_try_sys2_failed 0
fw_setenv flag_boot_success 0

NB: I don't have the device, this may or may not work or even brick your router. If you have a different device, chances are this is not going to work.

1 Like

@andyboeh omg, sorry, forgot to specify device name.

Hostname	OpenWrt
Model	Xiaomi Mi Router 3G
Architecture	MediaTek MT7621 ver:1 eco:3
Target Platform	ramips/mt7621
Firmware Version	OpenWrt 22.03.5 r20134-5f15225c1e / LuCI openwrt-22.03 branch git-23.119.80898-65ef406
Kernel Version	5.10.176	

this is the instruction for my router: https://openwrt.org/toh/xiaomi/mir3g

I believe i did everything, but it is possible I missed something, it was long time ago. So basically i need to run these two commands?

nvram set flag_try_sys1_failed=1
nvram commit

No, this only works on the stock image, OpenWrt does not have thr nvram command. You have to use fw_setenv instead, see the commands I posted before. It's then only the first command on your device.

1 Like

Is there a way to verify that system uses kernel from mtd8?

idk, I'm not familiar with Xiaomi devices.

well. After upgrading to 23.05 it happened again.

root@OpenWrt:/lib/modules# uname -a
Linux OpenWrt 5.10.176 #0 SMP Thu Apr 27 20:28:15 2023 mips GNU/Linux
root@OpenWrt:/lib/modules# ls
5.15.134
root@OpenWrt:/lib/modules# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00080000 00020000 "Bootloader"
mtd1: 00040000 00020000 "Config"
mtd2: 00040000 00020000 "Bdata"
mtd3: 00040000 00020000 "factory"
mtd4: 00040000 00020000 "crash"
mtd5: 00040000 00020000 "crash_syslog"
mtd6: 00040000 00020000 "reserved0"
mtd7: 00400000 00020000 "kernel_stock"
mtd8: 00400000 00020000 "kernel"
mtd9: 07580000 00020000 "ubi"

So it again is booting from mtd7.
Why?

and there is no such commands anymore:

root@OpenWrt:/lib/modules# fw_setenv flag_try_sys1_failed 1
-ash: fw_setenv: not found
root@OpenWrt:/tmp# dd if=/dev/mtd7 of=/tmp/kernel.mtd7
8192+0 records in
8192+0 records out
root@OpenWrt:/tmp# dd if=/dev/mtd8 of=/tmp/kernel.mtd8
8192+0 records in
8192+0 records out
root@OpenWrt:/tmp# head -n 1 kernel.mtd7
'V��dJ��)B���h}�MIPS OpenWrt Linux-5.10.176��@��@h�@@	<)5%@  9`�@�H�@X�@��<%#@���	<)%��
root@OpenWrt:/tmp# head -n 1 kernel.mtd8
'V���e$t*���2���MIPS OpenWrt Linux-5.15.134��@��@h�@@	<)5%@  9`�@�H�@X�@��<%#@���	<)%��

So it is booting from mtd7. Why? How to change that?

From the dts:

		/* uboot expects to find kernels at 0x200000 & 0x600000
		 * referred to as system 1 & system 2 respectively.
		 * a kernel is considered suitable for handing control over
		 * if its linux magic number exists & uImage CRC are correct.
		 * If either of those conditions fail, a matching sys'n'_fail flag
		 * is set in uboot env & a restart performed in the hope that the
		 * alternate kernel is okay.
		 * if neither kernel checksums ok and both are marked failed, system 2
		 * is booted anyway.
		 *
		 * Note uboot's tftp flash install writes the transferred
		 * image to both kernel partitions.
		 */

Did you write anything to the first kernel partition? Maybe during your experiments? Did you do a TFTP install before?

The problem seems to be that there is a valid kernel in the first partition which is used. You might want to try to copy over the valid kernel to the old kernel partition, but be prepared to attach serial in case something goes wrong.

@andyboeh not sure, I used instructions from the site to install openwrt and thats it.

Right now I did the following:

dd if=/dev/mtd8 of=/tmp/kernel.mtd8
mtd write /tmp/kernel.mtd8 mtd7

Now it still boots from mtd7 but with proper kernel.
The problems is only during upgrade. Sysupgrade always writes kernel new into mtd8, but my router always boots from mtd7 for some reason.

Yes, that's what I meant. And as described above, this is due to mtd7 containing a valid kernel image. You could try to destroy it, but there is a chance that it won't boot anymore and that serial access is required.

you need the uboot-envtools package for this.

@andyboeh is there a way to change boot order? :slight_smile:

Idk, I don't have this device, sorry.