From 6bd0463d3403c5c3dfe8ef5efe4ff74ceb86eeda Mon Sep 17 00:00:00 2001 From: longpanda Date: Wed, 21 Apr 2021 19:40:54 +0800 Subject: [PATCH] Support openwrt IMG file --- .../grub-2.04/grub-core/ventoy/ventoy.c | 101 +++++++++++++++++- .../grub-2.04/grub-core/ventoy/ventoy_def.h | 2 +- IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh | 3 + IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh | 39 +++++-- INSTALL/grub/grub.cfg | 70 ++++++++++-- INSTALL/ventoy_pack.sh | 1 + 6 files changed, 197 insertions(+), 19 deletions(-) diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c index 33f09142..396b3764 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c @@ -2503,6 +2503,7 @@ int ventoy_check_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, { grub_uint32_t i = 0; grub_uint64_t total = 0; + grub_uint64_t fileblk = 0; ventoy_img_chunk *chunk = NULL; for (i = 0; i < chunklist->cur_chunk; i++) @@ -2518,9 +2519,17 @@ int ventoy_check_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, total += chunk->disk_end_sector + 1 - chunk->disk_start_sector; } - if (total != ((file->size + 511) / 512)) + fileblk = (file->size + 511) / 512; + + if (total != fileblk) { - debug("Invalid total: %llu %llu\n", (ulonglong)total, (ulonglong)((file->size + 511) / 512)); + debug("Invalid total: %llu %llu\n", (ulonglong)total, (ulonglong)fileblk); + if ((file->size % 512) && (total + 1 == fileblk)) + { + debug("maybe img file to be processed.\n"); + return 0; + } + return 1; } @@ -3501,6 +3510,84 @@ static grub_err_t ventoy_cmd_check_secureboot_var(grub_extcmd_context_t ctxt, in } #endif +static grub_err_t ventoy_cmd_img_check_range(grub_extcmd_context_t ctxt, int argc, char **args) +{ + int i; + int ret = 1; + grub_file_t file; + grub_uint64_t FileSectors = 0; + ventoy_gpt_info *gpt = NULL; + ventoy_part_table *pt = NULL; + grub_uint8_t zeroguid[16] = {0}; + + (void)ctxt; + (void)argc; + + file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]); + if (!file) + { + debug("failed to open file %s\n", args[0]); + return 1; + } + + if (file->size % 512) + { + debug("unaligned file size: %llu\n", (ulonglong)file->size); + goto out; + } + + gpt = grub_zalloc(sizeof(ventoy_gpt_info)); + if (!gpt) + { + goto out; + } + + FileSectors = file->size / 512; + + grub_file_read(file, gpt, sizeof(ventoy_gpt_info)); + if (grub_strncmp(gpt->Head.Signature, "EFI PART", 8) == 0) + { + debug("This is EFI partition table\n"); + + for (i = 0; i < 128; i++) + { + if (grub_memcmp(gpt->PartTbl[i].PartGuid, zeroguid, 16)) + { + if (FileSectors < gpt->PartTbl[i].LastLBA) + { + debug("out of range: part[%d] LastLBA:%llu FileSectors:%llu\n", i, + (ulonglong)gpt->PartTbl[i].LastLBA, (ulonglong)FileSectors); + goto out; + } + } + } + } + else + { + debug("This is MBR partition table\n"); + + for (i = 0; i < 4; i++) + { + pt = gpt->MBR.PartTbl + i; + if (FileSectors < pt->StartSectorId + pt->SectorCount) + { + debug("out of range: part[%d] LastLBA:%llu FileSectors:%llu\n", i, + (ulonglong)(pt->StartSectorId + pt->SectorCount), + (ulonglong)FileSectors); + goto out; + } + } + } + + ret = 0; + +out: + grub_file_close(file); + grub_check_free(gpt); + grub_errno = GRUB_ERR_NONE; + return ret; +} + static grub_err_t ventoy_cmd_clear_key(grub_extcmd_context_t ctxt, int argc, char **args) { int i; @@ -3876,6 +3963,8 @@ static grub_err_t ventoy_cmd_get_fs_label(grub_extcmd_context_t ctxt, int argc, (void)ctxt; + debug("get fs label for %s\n", args[0]); + if (argc != 2) { debug("ventoy_cmd_get_fs_label, invalid param num %d\n", argc); @@ -3897,19 +3986,20 @@ static grub_err_t ventoy_cmd_get_fs_label(grub_extcmd_context_t ctxt, int argc, } fs = grub_fs_probe(dev); - if (!fs) + if (NULL == fs || NULL == fs->fs_label) { - debug("grub_fs_probe failed, %s\n", device_name); + debug("grub_fs_probe failed, %s %p %p\n", device_name, fs, fs->fs_label); goto end; } fs->fs_label(dev, &label); if (label) { + debug("label=<%s>\n", label); ventoy_set_env(args[1], label); grub_free(label); } - + rc = 0; end: @@ -4415,6 +4505,7 @@ static cmd_para ventoy_cmds[] = { "vt_acpi_param", ventoy_cmd_acpi_param, 0, NULL, "", "", NULL }, { "vt_check_secureboot_var", ventoy_cmd_check_secureboot_var, 0, NULL, "", "", NULL }, { "vt_clear_key", ventoy_cmd_clear_key, 0, NULL, "", "", NULL }, + { "vt_img_check_range", ventoy_cmd_img_check_range, 0, NULL, "", "", NULL }, }; diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h index 7e9204d6..e6135199 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h @@ -284,7 +284,7 @@ extern char g_arch_mode_suffix[64]; extern int g_ventoy_debug; void ventoy_debug(const char *fmt, ...); -#define debug(fmt, ...) if (g_ventoy_debug) ventoy_debug("[VTOY]: "fmt, __VA_ARGS__) +#define debug(fmt, args...) if (g_ventoy_debug) ventoy_debug("[VTOY]: "fmt, ##args) #define vtoy_ssprintf(buf, pos, fmt, ...) \ pos += grub_snprintf(buf + pos, VTOY_MAX_SCRIPT_BUF - pos, fmt, __VA_ARGS__) diff --git a/IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh b/IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh index 722c1ca3..416ba510 100644 --- a/IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh +++ b/IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh @@ -45,6 +45,9 @@ echo -n $vtDM > /ventoy/vtDM ventoy_create_dev_ventoy_part mdev -s +mkdir /ventoy_rdroot +mount /dev/ventoy2 /ventoy_rdroot + PATH=$VTPATH_OLD set_ventoy_hook_finish diff --git a/IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh b/IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh index d9785854..7f822de2 100644 --- a/IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh +++ b/IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh @@ -19,12 +19,39 @@ . $VTOY_PATH/hook/ventoy-os-lib.sh -$BUSYBOX_PATH/mkdir /sys -$BUSYBOX_PATH/mount -t proc proc /proc -$BUSYBOX_PATH/mount -t sysfs sys /sys +VTPATH_OLD=$PATH; PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH -$BUSYBOX_PATH/mdev -s +wrt_insmod() { + kbit=$1 + kv=$(uname -r) + + echo "insmod $kv $kbit" >> $VTOY_PATH/log + + [ -f /ventoy_openwrt/$kv/$kbit/dax.ko ] && insmod /ventoy_openwrt/$kv/$kbit/dax.ko > /dev/null 2>&1 + [ -f /ventoy_openwrt/$kv/$kbit/dm-mod.ko ] && insmod /ventoy_openwrt/$kv/$kbit/dm-mod.ko > /dev/null 2>&1 +} -#$BUSYBOX_PATH/sh $VTOY_PATH/loop/openwrt/ventoy-disk.sh -exec $BUSYBOX_PATH/sh +mkdir /sys +mount -t sysfs sys /sys +mdev -s + + +if [ -f /ventoy_openwrt.xz ]; then + tar xf /ventoy_openwrt.xz -C / + rm -f /ventoy_openwrt.xz +fi + + +if uname -m | egrep -q "amd64|x86_64"; then + wrt_insmod 64 +else + wrt_insmod generic + if lsmod | grep -q 'dm-mod'; then + echo "insmod generic failed" >> $VTOY_PATH/log + else + wrt_insmod legacy + fi +fi + +sh $VTOY_PATH/loop/openwrt/ventoy-disk.sh diff --git a/INSTALL/grub/grub.cfg b/INSTALL/grub/grub.cfg index d5bd4654..770a4e2d 100644 --- a/INSTALL/grub/grub.cfg +++ b/INSTALL/grub/grub.cfg @@ -361,7 +361,13 @@ function ventoy_get_truenas_ver { } function ventoy_get_midnightbsd_ver { - set vt_freebsd_ver=11.x + if vt_str_begin "$vt_volume_id" "1_"; then + set vt_freebsd_ver=11.x + elif vt_str_begin "$vt_volume_id" "2_"; then + set vt_freebsd_ver=2.x + elif vt_str_begin "$vt_volume_id" "3_"; then + set vt_freebsd_ver=3.x + fi } function ventoy_freebsd_proc { @@ -419,6 +425,11 @@ function ventoy_freebsd_proc { set vt_freebsd_ver=9.x fi + + if [ -e (loop)/usr/freebsd-dist/cloninst.sh ]; then + set vtFreeBsdDistro=ClonOS + fi + set vt_freebsd_bit=64 for file in "/boot/kernel/kernel" "/boot/kernel/kernel.gz"; do if [ -e (loop)/$file ]; then @@ -440,7 +451,7 @@ function ventoy_freebsd_proc { fi if [ -n "${vtdebug_flag}" ]; then - echo "This is FreeBSD $vt_freebsd_ver ${vt_freebsd_bit}bit" + echo "This is $vtFreeBsdDistro $vt_freebsd_ver ${vt_freebsd_bit}bit" fi unset vt_unix_mod_path @@ -451,10 +462,6 @@ function ventoy_freebsd_proc { fi done - if [ -e (loop)/usr/freebsd-dist/cloninst.sh ]; then - set vtFreeBsdDistro=ClonOS - fi - vt_unix_replace_ko $vt_unix_mod_path (vtunix)/ventoy_unix/$vtFreeBsdDistro/geom_ventoy_ko/$vt_freebsd_ver/$vt_freebsd_bit/geom_ventoy.ko.xz vt_unix_replace_conf FreeBSD "${1}${chosen_path}" } @@ -1520,6 +1527,51 @@ function ventoy_img_batocera { vt_unset_boot_opt } +function ventoy_img_openwrt { + if [ -e (vtimghd,2)/lib64 ]; then + set ventoy_busybox_ver=64 + fi + + if [ ! -f ${vtoy_iso_part}/ventoy/ventoy_openwrt.xz ]; then + ventoy_gui_console + echo -e "\n ventoy_openwrt.xz not found. Please refer https://www.ventoy.net/en/doc_openwrt.html.\n" + echo -e " 未找到 ventoy_openwrt.xz 文件。请参考 https://www.ventoy.net/cn/doc_openwrt.html\n" + echo -e "\n press ENTER to exit (请按 回车 键返回) ..." + read vtInputKey + ventoy_cli_console + return + fi + + if vt_img_check_range "${vtoy_iso_part}${vt_chosen_path}"; then + ventoy_debug_pause + else + ventoy_gui_console + echo -e "\n IMG file need processed. Please refer https://www.ventoy.net/en/doc_openwrt.html.\n" + echo -e " 此 IMG 文件必须处理之后才能支持。请参考 https://www.ventoy.net/cn/doc_openwrt.html\n" + echo -e "\n press ENTER to exit (请按 回车 键返回) ..." + read vtInputKey + ventoy_cli_console + return + fi + + + vt_load_cpio $vtoy_path "${vt_chosen_path}" ${vtoy_iso_part} "busybox=$ventoy_busybox_ver" + vt_trailer_cpio ${vtoy_iso_part} "${vt_chosen_path}" noinit + + vt_img_extra_initrd_append ${vtoy_iso_part}/ventoy/ventoy_openwrt.xz + + + #boot image file + vt_set_boot_opt rdinit=/vtoy/vtoy ventoyos=openwrt + vt_img_hook_root + + set root=(vtimghd,1) + configfile (vtimghd,1)/boot/grub/grub.cfg + + vt_img_unhook_root + vt_unset_boot_opt +} + function ventoy_img_tails { vt_load_cpio $vtoy_path "${vt_chosen_path}" ${vtoy_iso_part} "busybox=$ventoy_busybox_ver" vt_trailer_cpio ${vtoy_iso_part} "${vt_chosen_path}" noinit @@ -1580,8 +1632,9 @@ function img_common_menuentry { vt_img_extra_initrd_reset + vt_get_fs_label (vtimghd,1) vtImgHd1Label - if [ -d (vtimghd,2)/lib ]; then + if [ -d (vtimghd,2)/lib ]; then vt_get_fs_label (vtimghd,2) vtImgHd2Label fi @@ -1589,6 +1642,7 @@ function img_common_menuentry { vt_1st_line (vtimghd,1)/etc/hostname vtImgHostname fi + if [ -e (vtimghd,1)/easy.sfs ]; then ventoy_img_easyos elif [ -e (vtimghd,1)/volumio.initrd ]; then @@ -1609,6 +1663,8 @@ function img_common_menuentry { ventoy_img_recalbox elif [ -f (vtimghd,2)/loader/entries/ubos.conf ]; then ventoy_img_ubos + elif [ -f (vtimghd,2)/etc/openwrt_version ]; then + ventoy_img_openwrt elif [ -f (vtimghd,1)/efi/boot/mt86.png ]; then if [ "$grub_platform" = "pc" ]; then img_unsupport_tip diff --git a/INSTALL/ventoy_pack.sh b/INSTALL/ventoy_pack.sh index 3fddd9ad..577a23a0 100644 --- a/INSTALL/ventoy_pack.sh +++ b/INSTALL/ventoy_pack.sh @@ -82,6 +82,7 @@ cp $OPT ./tool/i386/mount.exfat-fuse $tmpmnt/tool/mount.exfat-fuse_i386 cp $OPT ./tool/x86_64/mount.exfat-fuse $tmpmnt/tool/mount.exfat-fuse_x86_64 cp $OPT ./tool/aarch64/mount.exfat-fuse $tmpmnt/tool/mount.exfat-fuse_aarch64 + rm -f $tmpmnt/grub/i386-pc/*.img