From 55618f7bd99916215dd0e7f6e903c9465ba4d1ef Mon Sep 17 00:00:00 2001 From: hanwckf Date: Sat, 2 Sep 2023 14:35:54 +0800 Subject: [PATCH] uboot-202307: add multi mtd layout support --- .../board/mediatek/Kconfig | 8 ++ .../board/mediatek/common/Makefile | 2 + .../board/mediatek/common/mtd_helper.c | 84 +++++++++++++++++-- .../board/mediatek/common/mtd_layout.c | 82 ++++++++++++++++++ uboot-mtk-20230718-09eda825/cmd/Kconfig | 5 ++ uboot-mtk-20230718-09eda825/cmd/Makefile | 2 + uboot-mtk-20230718-09eda825/cmd/showlayout.c | 31 +++++++ 7 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_layout.c create mode 100644 uboot-mtk-20230718-09eda825/cmd/showlayout.c diff --git a/uboot-mtk-20230718-09eda825/board/mediatek/Kconfig b/uboot-mtk-20230718-09eda825/board/mediatek/Kconfig index e21f6ac6d..ceb796c5b 100644 --- a/uboot-mtk-20230718-09eda825/board/mediatek/Kconfig +++ b/uboot-mtk-20230718-09eda825/board/mediatek/Kconfig @@ -156,6 +156,14 @@ config MTK_UBI_SUPPORT endif # MEDIATEK_BOOTMENU +config MEDIATEK_MULTI_MTD_LAYOUT + bool "Enable multi mtd layout support" + select SYS_MTDPARTS_RUNTIME + select CMD_SHOW_MTD_LAYOUT + depends on !MTK_BOOTMENU_MMC + depends on !MTK_DUAL_BOOT + default n + menuconfig MTK_SECURE_BOOT bool "Enable secure boot framework" select FIT_SIGNATURE diff --git a/uboot-mtk-20230718-09eda825/board/mediatek/common/Makefile b/uboot-mtk-20230718-09eda825/board/mediatek/common/Makefile index bffc671c1..cb43de7ca 100644 --- a/uboot-mtk-20230718-09eda825/board/mediatek/common/Makefile +++ b/uboot-mtk-20230718-09eda825/board/mediatek/common/Makefile @@ -15,6 +15,8 @@ ifdef CONFIG_MMC obj-$(CONFIG_MEDIATEK_BOOTMENU) += mmc_helper.o endif +obj-$(CONFIG_MEDIATEK_MULTI_MTD_LAYOUT) += mtd_layout.o + obj-$(CONFIG_MEDIATEK_BOOTMENU) += cmd_mtkupgrade.o cmd_mtkload.o \ cmd_mtkboardboot.o cmd_mtkautoboot.o diff --git a/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_helper.c b/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_helper.c index 01373fcf9..b40e5b399 100644 --- a/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_helper.c +++ b/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_helper.c @@ -549,7 +549,8 @@ static int mount_ubi(struct mtd_info *mtd, bool create) if (ret) return ret; - ret = ubi_init(); + ubi_exit(); + ret = ubi_part(mtd->name, NULL); } if (ret) { @@ -633,6 +634,45 @@ static int read_ubi_volume(const char *volume, void *buff, size_t size) return ubi_volume_read((char *)volume, buff, size); } +#ifdef CONFIG_MEDIATEK_MULTI_MTD_LAYOUT +static int write_ubi2_tar_image_separate(const void *data, size_t size, + struct mtd_info *mtd_kernel, struct mtd_info *mtd_rootfs) +{ + const void *kernel_data, *rootfs_data; + size_t kernel_size, rootfs_size; + int ret; + + ret = parse_tar_image(data, size, &kernel_data, &kernel_size, + &rootfs_data, &rootfs_size); + if (ret) + return ret; + + ret = mount_ubi(mtd_kernel, true); + if (ret) + return ret; + + ret = update_ubi_volume(PART_KERNEL_NAME, -1, kernel_data, kernel_size); + if (ret) + goto out; + + ret = mount_ubi(mtd_rootfs, true); + if (ret) + return ret; + + /* Remove this volume first in case of no enough PEBs */ + remove_ubi_volume(PART_ROOTFS_DATA_NAME); + + ret = update_ubi_volume(PART_ROOTFS_NAME, -1, rootfs_data, rootfs_size); + if (ret) + goto out; + + ret = create_ubi_volume(PART_ROOTFS_DATA_NAME, 0, -1, true); + +out: + return ret; +} +#endif + static int write_ubi1_tar_image(const void *data, size_t size, struct mtd_info *mtd_kernel, struct mtd_info *mtd_ubi) @@ -984,9 +1024,14 @@ int mtd_upgrade_image(const void *data, size_t size) struct owrt_image_info ii; struct mtd_info *mtd; int ret; - + const char *ubi_flash_part = PART_UBI_NAME; #ifdef CONFIG_CMD_UBI struct mtd_info *mtd_kernel; +#ifdef CONFIG_MEDIATEK_MULTI_MTD_LAYOUT + struct mtd_info *mtd_ubikernel, *mtd_ubirootfs; + const char *ubi_kernel_part; + const char *ubi_rootfs_part; +#endif #endif /* CONFIG_CMD_UBI */ gen_mtd_probe_devices(); @@ -998,7 +1043,13 @@ int mtd_upgrade_image(const void *data, size_t size) else mtd_kernel = NULL; - mtd = get_mtd_device_nm(PART_UBI_NAME); +#ifdef CONFIG_MEDIATEK_MULTI_MTD_LAYOUT + ubi_flash_part = env_get("factory_part"); + if (!ubi_flash_part) + ubi_flash_part = PART_UBI_NAME; +#endif + + mtd = get_mtd_device_nm(ubi_flash_part); if (!IS_ERR_OR_NULL(mtd)) { put_mtd_device(mtd); @@ -1020,8 +1071,24 @@ int mtd_upgrade_image(const void *data, size_t size) !IS_ENABLED(CONFIG_MTK_DUAL_BOOT)) return mtd_update_generic(mtd, data, size, true); - if (!ret && ii.type == IMAGE_TAR) + if (!ret && ii.type == IMAGE_TAR) { +#ifdef CONFIG_MEDIATEK_MULTI_MTD_LAYOUT + ubi_kernel_part = env_get("sysupgrade_kernel_ubipart"); + ubi_rootfs_part = env_get("sysupgrade_rootfs_ubipart"); + if (ubi_kernel_part && ubi_rootfs_part) { + mtd_ubikernel = get_mtd_device_nm(ubi_kernel_part); + mtd_ubirootfs = get_mtd_device_nm(ubi_rootfs_part); + if (!IS_ERR_OR_NULL(mtd_ubikernel) && !IS_ERR_OR_NULL(mtd_ubirootfs)) { + put_mtd_device(mtd_ubikernel); + put_mtd_device(mtd_ubirootfs); + return write_ubi2_tar_image_separate(data, size, mtd_ubikernel, mtd_ubirootfs); + } else { + return -ENOTSUPP; + } + } +#endif return write_ubi2_tar_image(data, size, mtd); + } } } #endif /* CONFIG_CMD_UBI */ @@ -1046,6 +1113,7 @@ int mtd_upgrade_image(const void *data, size_t size) int mtd_boot_image(void) { struct mtd_info *mtd; + const char *ubi_boot_part = PART_UBI_NAME; #ifdef CONFIG_CMD_UBI struct mtd_info *mtd_kernel; @@ -1060,7 +1128,13 @@ int mtd_boot_image(void) else mtd_kernel = NULL; - mtd = get_mtd_device_nm(PART_UBI_NAME); +#ifdef CONFIG_MEDIATEK_MULTI_MTD_LAYOUT + ubi_boot_part = env_get("ubi_boot_part"); + if (!ubi_boot_part) + ubi_boot_part = PART_UBI_NAME; +#endif + + mtd = get_mtd_device_nm(ubi_boot_part); if (!IS_ERR_OR_NULL(mtd)) { put_mtd_device(mtd); diff --git a/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_layout.c b/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_layout.c new file mode 100644 index 000000000..ae9335376 --- /dev/null +++ b/uboot-mtk-20230718-09eda825/board/mediatek/common/mtd_layout.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0+ + +#include +#include +#include +#include +#include +#include +#include + +static ofnode ofnode_get_mtd_layout(const char *layout_label) +{ + ofnode node, layout; + const char *label; + + node = ofnode_path("/mtd-layout"); + if (!ofnode_valid(node)) { + return ofnode_null(); + } + + if (!ofnode_get_child_count(node)) { + return ofnode_null(); + } + + ofnode_for_each_subnode(layout, node) { + label = ofnode_read_string(layout, "label"); + if (!strcmp(layout_label, label)) { + return layout; + } + } + + return ofnode_null(); +} + +const char *get_mtd_layout_label(void) +{ + const char *layout_label = NULL; + + if (gd->flags & GD_FLG_ENV_READY) + layout_label = env_get("mtd_layout_label"); + + if (!layout_label) + layout_label = "default"; + + return layout_label; +} + +void board_mtdparts_default(const char **mtdids, const char **mtdparts) +{ + const char *ids = NULL; + const char *parts = NULL; + const char *boot_part = NULL; + const char *factory_part = NULL; + const char *sysupgrade_kernel_ubipart = NULL; + const char *sysupgrade_rootfs_ubipart = NULL; + const char *cmdline = NULL; + ofnode layout_node; + + layout_node = ofnode_get_mtd_layout(get_mtd_layout_label()); + + if (ofnode_valid(layout_node)) { + ids = ofnode_read_string(layout_node, "mtdids"); + parts = ofnode_read_string(layout_node, "mtdparts"); + boot_part = ofnode_read_string(layout_node, "boot_part"); + factory_part = ofnode_read_string(layout_node, "factory_part"); + sysupgrade_kernel_ubipart = ofnode_read_string(layout_node, "sysupgrade_kernel_ubipart"); + sysupgrade_rootfs_ubipart = ofnode_read_string(layout_node, "sysupgrade_rootfs_ubipart"); + cmdline = ofnode_read_string(layout_node, "cmdline"); + } + + if (ids && parts) { + *mtdids = ids; + *mtdparts = parts; + //printf("%s: mtdids=%s & mtdparts=%s\n", __func__, ids, parts); + } + + env_set("bootargs", cmdline); + env_set("ubi_boot_part", boot_part); + env_set("factory_part", factory_part); + env_set("sysupgrade_kernel_ubipart", sysupgrade_kernel_ubipart); + env_set("sysupgrade_rootfs_ubipart", sysupgrade_rootfs_ubipart); +} diff --git a/uboot-mtk-20230718-09eda825/cmd/Kconfig b/uboot-mtk-20230718-09eda825/cmd/Kconfig index ac821ff26..bb1f5e021 100644 --- a/uboot-mtk-20230718-09eda825/cmd/Kconfig +++ b/uboot-mtk-20230718-09eda825/cmd/Kconfig @@ -2885,4 +2885,9 @@ config CMD_GL_BTN depends on BUTTON default n select POLLER + +config CMD_SHOW_MTD_LAYOUT + bool "Show mtd layout command" + depends on OF_LIBFDT + default n endmenu diff --git a/uboot-mtk-20230718-09eda825/cmd/Makefile b/uboot-mtk-20230718-09eda825/cmd/Makefile index 1496f4a13..d6a0fd5ab 100644 --- a/uboot-mtk-20230718-09eda825/cmd/Makefile +++ b/uboot-mtk-20230718-09eda825/cmd/Makefile @@ -229,6 +229,8 @@ obj-$(CONFIG_CMD_SCP03) += scp03.o obj-$(CONFIG_CMD_GL_BTN) += glbtn.o led_blink.o +obj-$(CONFIG_CMD_SHOW_MTD_LAYOUT) += showlayout.o + obj-$(CONFIG_ARM) += arm/ obj-$(CONFIG_RISCV) += riscv/ obj-$(CONFIG_SANDBOX) += sandbox/ diff --git a/uboot-mtk-20230718-09eda825/cmd/showlayout.c b/uboot-mtk-20230718-09eda825/cmd/showlayout.c new file mode 100644 index 000000000..3845cea64 --- /dev/null +++ b/uboot-mtk-20230718-09eda825/cmd/showlayout.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0+ + +#include +#include +#include + +static int do_showlayout(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + ofnode node, layout; + + node = ofnode_path("/mtd-layout"); + + if (ofnode_valid(node) && ofnode_get_child_count(node)) { + ofnode_for_each_subnode(layout, node) { + printf("mtd label: %s, mtdids: %s, mtdparts: %s\n", + ofnode_read_string(layout, "label"), + ofnode_read_string(layout, "mtdids"), + ofnode_read_string(layout, "mtdparts")); + } + } else { + printf("get mtd layout failed!\n"); + } + + return CMD_RET_SUCCESS; +} + +U_BOOT_CMD( + showlayout, 2, 0, do_showlayout, + "Show mtd layout", + "" +);