]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
Support openwrt IMG file
authorlongpanda <admin@ventoy.net>
Wed, 21 Apr 2021 11:40:54 +0000 (19:40 +0800)
committerlongpanda <admin@ventoy.net>
Wed, 21 Apr 2021 11:40:54 +0000 (19:40 +0800)
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h
IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh
IMG/cpio/ventoy/loop/openwrt/ventoy-hook.sh
INSTALL/grub/grub.cfg
INSTALL/ventoy_pack.sh

index 33f09142dedcb6205e7277cc4b53d112aeda9427..396b376438bdea8c3f3d887de293fee0e4db1f44 100644 (file)
@@ -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_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++)
     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;
     }
 
         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;
     }
 
         return 1;
     }
 
@@ -3501,6 +3510,84 @@ static grub_err_t ventoy_cmd_check_secureboot_var(grub_extcmd_context_t ctxt, in
 }
 #endif
 
 }
 #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;
 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;
 
     
     (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);
     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);
     }
 
     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)
     {
         goto end;
     }
 
     fs->fs_label(dev, &label);
     if (label)
     {
+        debug("label=<%s>\n", label);
         ventoy_set_env(args[1], label);
         grub_free(label);
     }
         ventoy_set_env(args[1], label);
         grub_free(label);
     }
-    
+
     rc = 0;
     
 end:
     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_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 },
 
 };
 
 
 };
 
index 7e9204d637fe16982037f6d82f746bb28905d6b0..e61351991ddb9c27b089e1dc56cc18be7dd4e28d 100644 (file)
@@ -284,7 +284,7 @@ extern char g_arch_mode_suffix[64];
 
 extern int g_ventoy_debug;
 void ventoy_debug(const char *fmt, ...);
 
 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__)
 
 #define vtoy_ssprintf(buf, pos, fmt, ...) \
     pos += grub_snprintf(buf + pos, VTOY_MAX_SCRIPT_BUF - pos, fmt, __VA_ARGS__)
index 722c1ca3b467ee95f2f02039bbd0c08ace52bafa..416ba5108de73512bad39b4d5a74965e36dd5ce7 100644 (file)
@@ -45,6 +45,9 @@ echo -n $vtDM > /ventoy/vtDM
 ventoy_create_dev_ventoy_part
 mdev -s
 
 ventoy_create_dev_ventoy_part
 mdev -s
 
+mkdir /ventoy_rdroot
+mount /dev/ventoy2 /ventoy_rdroot
+
 PATH=$VTPATH_OLD
 
 set_ventoy_hook_finish
 PATH=$VTPATH_OLD
 
 set_ventoy_hook_finish
index d978585451db9f1b826a0062e6a79bcc66742741..7f822de2c9d44aa78f7c420afeddced0e9120b66 100644 (file)
 
 . $VTOY_PATH/hook/ventoy-os-lib.sh
 
 
 . $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
index d5bd4654e06a10c90b32fb191bd9b2e989ad69fc..770a4e2d10286e4ab53368da6a3a1d680c5220a7 100644 (file)
@@ -361,7 +361,13 @@ function ventoy_get_truenas_ver {
 }
 
 function ventoy_get_midnightbsd_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 {
 }
 
 function ventoy_freebsd_proc {
@@ -419,6 +425,11 @@ function ventoy_freebsd_proc {
         set vt_freebsd_ver=9.x
     fi
     
         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    
     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
     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
     fi
     
     unset vt_unix_mod_path
@@ -451,10 +462,6 @@ function ventoy_freebsd_proc {
         fi
     done
     
         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}"
 }
     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
 }
 
     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
 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_img_extra_initrd_reset
 
+
     vt_get_fs_label (vtimghd,1) vtImgHd1Label
     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
     
         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
     
         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
     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
         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
     elif [ -f (vtimghd,1)/efi/boot/mt86.png ]; then 
         if [ "$grub_platform" = "pc" ]; then
             img_unsupport_tip
index 3fddd9ad25dfcf0b5634ca4455c253020a0750e0..577a23a0409af66353d1df221cc91f32762bb094 100644 (file)
@@ -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
 
 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
 
 
 rm -f $tmpmnt/grub/i386-pc/*.img