Skip to content

uboot: support mainline FIT image #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions uboot-mtk-20220606/board/mediatek/common/boot_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
int boot_from_mem(ulong data_load_addr)
{
char cmd[64];
const char *bootconf = env_get("bootconf");

snprintf(cmd, sizeof(cmd), "bootm 0x%lx", data_load_addr);
if (bootconf && strlen(bootconf) > 0)
snprintf(cmd, sizeof(cmd), "bootm 0x%lx#%s", data_load_addr, bootconf);
else
snprintf(cmd, sizeof(cmd), "bootm 0x%lx", data_load_addr);

return run_command(cmd, 0);
}
Expand Down Expand Up @@ -57,7 +61,16 @@ static int _boot_from_mmc(u32 dev, struct mmc *mmc, u64 offset)
#endif
#if defined(CONFIG_FIT)
case IMAGE_FORMAT_FIT:
size = fit_get_size((const void *)data_load_addr);
size = fit_get_totalsize((const void *)data_load_addr);
if (size <= 0x2000) {
/* Load FDT header into memory */
ret = _mmc_read(mmc, offset, (void *)data_load_addr, size);
if (ret)
return ret;

/* Read whole FIT image */
size = fit_get_totalsize((const void *)data_load_addr);
}
break;
#endif
default:
Expand Down
49 changes: 49 additions & 0 deletions uboot-mtk-20220606/board/mediatek/common/ubi_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,50 @@ static int write_ubi2_tar_image(const void *data, size_t size,
return ret;
}

static int write_ubi_fit_image(const void *data, size_t size,
struct mtd_info *mtd)
{
int ret;

ret = mount_ubi(mtd);
if (ret)
return ret;

if (!find_ubi_volume("fit")) {
/* ubi is dirty, erase ubi and recreate volumes */
umount_ubi();
ret = mtd_erase_generic(mtd, 0, mtd->size);
if (ret)
return ret;

ret = mount_ubi(mtd);
if (ret)
return ret;

ret = create_ubi_volume("ubootenv", 0x100000, -1, false);
if (ret)
goto out;

ret = create_ubi_volume("ubootenv2", 0x100000, -1, false);
if (ret)
goto out;
}

/* Remove this volume first in case of no enough PEBs */
remove_ubi_volume("rootfs_data");

ret = update_ubi_volume("fit", -1, data, size);
if (ret)
goto out;

ret = create_ubi_volume("rootfs_data", 0, -1, true);

out:
umount_ubi();

return ret;
}

static int boot_from_ubi(struct mtd_info *mtd)
{
ulong data_load_addr;
Expand All @@ -528,6 +572,8 @@ static int boot_from_ubi(struct mtd_info *mtd)
return ret;

ret = read_ubi_volume("kernel", (void *)data_load_addr, 0);
if (ret == -ENODEV)
ret = read_ubi_volume("fit", (void *)data_load_addr, 0);
if (ret)
return ret;

Expand Down Expand Up @@ -577,6 +623,9 @@ int ubi_upgrade_image(const void *data, size_t size)
} else {
ret = parse_image_ram(data, size, mtd->erasesize, &ii);

if (ii.header_type == HEADER_FIT)
return write_ubi_fit_image(data, size, mtd);

if (!ret && ii.type == IMAGE_UBI2)
return mtd_update_generic(mtd, data, size);

Expand Down
3 changes: 2 additions & 1 deletion uboot-mtk-20220606/board/mediatek/common/verify_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ static bool verify_kernel_fit(const void *fit, size_t size,
}

/* Find kernel image node */
kernel_noffset = fit_conf_get_prop_node(fit, default_noffset, "kernel");
kernel_noffset = fit_conf_get_prop_node(fit, default_noffset, "kernel",
IH_PHASE_NONE);

debug("%s: kernel_noffset = 0x%x\n", __func__, kernel_noffset);

Expand Down
24 changes: 23 additions & 1 deletion uboot-mtk-20220606/board/mediatek/mt7981/bootmenu_emmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <asm/global_data.h>
#include <command.h>
#include <fdtdec.h>
#include <image.h>
#include <linux/sizes.h>
#include <errno.h>
#include <dm/ofnode.h>
Expand Down Expand Up @@ -37,6 +38,7 @@ DECLARE_GLOBAL_DATA_PTR;
#define GPT_MAX_SIZE (34 * 512)

#define PART_FIP_NAME "fip"
#define PART_PRODUCTION_NAME "production"
#define PART_KERNEL_NAME "kernel"
#define PART_ROOTFS_NAME "rootfs"

Expand Down Expand Up @@ -145,6 +147,20 @@ int write_firmware(void *priv, const struct data_part_entry *dpe,
u32 slot;
#endif /* CONFIG_MTK_DUAL_BOOT */

/* FIT image logic */
if (genimg_get_format(data) == IMAGE_FORMAT_FIT) {
ret = write_part(PART_PRODUCTION_NAME, data, size, true);
if (ret)
return ret;

/* Mark rootfs_data unavailable */
rootfs_data_offs = (size + ROOTDEV_OVERLAY_ALIGN - 1) &
(~(ROOTDEV_OVERLAY_ALIGN - 1));
erase_part(PART_PRODUCTION_NAME, rootfs_data_offs, SZ_512K);

return ret;
}

ret = parse_tar_image(data, size, &kernel_data, &kernel_size,
&rootfs_data, &rootfs_size);
if (ret)
Expand Down Expand Up @@ -273,7 +289,13 @@ int board_boot_default(void)

return dual_boot_mmc(&mbd);
#else
return boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_KERNEL_NAME);
int ret;

ret = boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_KERNEL_NAME);
if (ret == -ENODEV)
return boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_PRODUCTION_NAME);

return ret;
#endif /* CONFIG_MTK_DUAL_BOOT */
}

Expand Down
2 changes: 1 addition & 1 deletion uboot-mtk-20220606/board/mediatek/mt7981/bootmenu_ubi.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static int write_flash_image(void *priv, const struct data_part_entry *dpe,
static int erase_env(void *priv, const struct data_part_entry *dpe,
const void *data, size_t size)
{
#if !defined(CONFIG_MTK_SECURE_BOOT) && !defined(CONFIG_ENV_IS_NOWHERE)
#if !defined(CONFIG_MTK_SECURE_BOOT) && defined(CONFIG_ENV_IS_IN_MTD)
struct mtd_info *mtd;

ubi_probe_mtd_devices();
Expand Down
53 changes: 53 additions & 0 deletions uboot-mtk-20220606/board/mediatek/mt7981/mt7981_rfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <env.h>
#include <init.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <linux/libfdt.h>

#include <mtd.h>
#include <linux/mtd/mtd.h>
Expand Down Expand Up @@ -60,3 +62,54 @@ int board_nmbm_init(void)

return 0;
}

#define MT7981_BOOT_NOR 0
#define MT7981_BOOT_SPIM_NAND 1 /* ToDo: fallback to SD */
#define MT7981_BOOT_EMMC 2
#define MT7981_BOOT_SNFI_NAND 3 /* ToDo (treated as SD) */

int ft_system_setup(void *blob, struct bd_info *bd)
{
const u32 *media_handle_p;
int chosen, len, ret;
const char *media;
u32 media_handle;

switch ((readl(0x11d006f0) & 0xc0) >> 6) {
case MT7981_BOOT_NOR:
media = "rootdisk-nor";
break
;;
case MT7981_BOOT_SPIM_NAND:
media = "rootdisk-spim-nand";
break
;;
case MT7981_BOOT_EMMC:
media = "rootdisk-emmc";
break
;;
case MT7981_BOOT_SNFI_NAND:
media = "rootdisk-sd";
break
;;
}

chosen = fdt_path_offset(blob, "/chosen");
if (chosen <= 0)
return 0;

media_handle_p = fdt_getprop(blob, chosen, media, &len);
if (media_handle_p <= 0 || len != 4)
return 0;

media_handle = *media_handle_p;
ret = fdt_setprop(blob, chosen, "rootdisk", &media_handle, sizeof(media_handle));
if (ret) {
printf("cannot set media phandle %s as rootdisk /chosen node\n", media);
return ret;
}

printf("set /chosen/rootdisk to bootrom media: %s (phandle 0x%08x)\n", media, fdt32_to_cpu(media_handle));

return 0;
}
24 changes: 23 additions & 1 deletion uboot-mtk-20220606/board/mediatek/mt7986/bootmenu_emmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <asm/global_data.h>
#include <command.h>
#include <fdtdec.h>
#include <image.h>
#include <linux/sizes.h>
#include <errno.h>
#include <dm/ofnode.h>
Expand Down Expand Up @@ -37,6 +38,7 @@ DECLARE_GLOBAL_DATA_PTR;
#define GPT_MAX_SIZE (34 * 512)

#define PART_FIP_NAME "fip"
#define PART_PRODUCTION_NAME "production"
#define PART_KERNEL_NAME "kernel"
#define PART_ROOTFS_NAME "rootfs"

Expand Down Expand Up @@ -145,6 +147,20 @@ int write_firmware(void *priv, const struct data_part_entry *dpe,
u32 slot;
#endif /* CONFIG_MTK_DUAL_BOOT */

/* FIT image logic */
if (genimg_get_format(data) == IMAGE_FORMAT_FIT) {
ret = write_part(PART_PRODUCTION_NAME, data, size, true);
if (ret)
return ret;

/* Mark rootfs_data unavailable */
rootfs_data_offs = (size + ROOTDEV_OVERLAY_ALIGN - 1) &
(~(ROOTDEV_OVERLAY_ALIGN - 1));
erase_part(PART_PRODUCTION_NAME, rootfs_data_offs, SZ_512K);

return ret;
}

ret = parse_tar_image(data, size, &kernel_data, &kernel_size,
&rootfs_data, &rootfs_size);
if (ret)
Expand Down Expand Up @@ -273,7 +289,13 @@ int board_boot_default(void)

return dual_boot_mmc(&mbd);
#else
return boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_KERNEL_NAME);
int ret;

ret = boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_KERNEL_NAME);
if (ret == -ENODEV)
return boot_from_mmc_partition(EMMC_DEV_INDEX, 0, PART_PRODUCTION_NAME);

return ret;
#endif /* CONFIG_MTK_DUAL_BOOT */
}

Expand Down
2 changes: 1 addition & 1 deletion uboot-mtk-20220606/board/mediatek/mt7986/bootmenu_ubi.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static int write_flash_image(void *priv, const struct data_part_entry *dpe,
static int erase_env(void *priv, const struct data_part_entry *dpe,
const void *data, size_t size)
{
#if !defined(CONFIG_MTK_SECURE_BOOT) && !defined(CONFIG_ENV_IS_NOWHERE)
#if !defined(CONFIG_MTK_SECURE_BOOT) && defined(CONFIG_ENV_IS_IN_MTD)
struct mtd_info *mtd;

ubi_probe_mtd_devices();
Expand Down
53 changes: 53 additions & 0 deletions uboot-mtk-20220606/board/mediatek/mt7986/mt7986_rfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <env.h>
#include <init.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <linux/libfdt.h>

#include <mtd.h>
#include <linux/mtd/mtd.h>
Expand Down Expand Up @@ -62,3 +64,54 @@ int board_nmbm_init(void)

return 0;
}

#define MT7986_BOOT_NOR 0
#define MT7986_BOOT_SPIM_NAND 1
#define MT7986_BOOT_EMMC 2
#define MT7986_BOOT_SNFI_NAND 3

int ft_system_setup(void *blob, struct bd_info *bd)
{
const u32 *media_handle_p;
int chosen, len, ret;
const char *media;
u32 media_handle;

switch ((readl(0x1001f6f0) & 0x300) >> 8) {
case MT7986_BOOT_NOR:
media = "rootdisk-nor";
break
;;
case MT7986_BOOT_SPIM_NAND:
media = "rootdisk-spim-nand";
break
;;
case MT7986_BOOT_EMMC:
media = "rootdisk-emmc";
break
;;
case MT7986_BOOT_SNFI_NAND:
media = "rootdisk-sd";
break
;;
}

chosen = fdt_path_offset(blob, "/chosen");
if (chosen <= 0)
return 0;

media_handle_p = fdt_getprop(blob, chosen, media, &len);
if (media_handle_p <= 0 || len != 4)
return 0;

media_handle = *media_handle_p;
ret = fdt_setprop(blob, chosen, "rootdisk", &media_handle, sizeof(media_handle));
if (ret) {
printf("cannot set media phandle %s as rootdisk /chosen node\n", media);
return ret;
}

printf("set /chosen/rootdisk to bootrom media: %s (phandle 0x%08x)\n", media, fdt32_to_cpu(media_handle));

return 0;
}
Loading