+static grub_err_t ventoy_cmd_dump_menu(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc == 0)
+ {
+ grub_printf("List Mode: CurLen:%d MaxLen:%u\n", g_list_script_pos, VTOY_MAX_SCRIPT_BUF);
+ grub_printf("%s", g_list_script_buf);
+ }
+ else
+ {
+ grub_printf("Tree Mode: CurLen:%d MaxLen:%u\n", g_tree_script_pos, VTOY_MAX_SCRIPT_BUF);
+ grub_printf("%s", g_tree_script_buf);
+ }
+
+ return 0;
+}
+
+static grub_err_t ventoy_cmd_check_mode(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc != 1)
+ {
+ return 1;
+ }
+
+ if (args[0][0] == '0')
+ {
+ return g_ventoy_memdisk_mode ? 0 : 1;
+ }
+ else if (args[0][0] == '1')
+ {
+ return g_ventoy_iso_raw ? 0 : 1;
+ }
+ else if (args[0][0] == '2')
+ {
+ return g_ventoy_iso_uefi_drv ? 0 : 1;
+ }
+
+ return 1;
+}
+
+static grub_err_t ventoy_cmd_dynamic_menu(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ char memfile[128] = {0};
+
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc == 0)
+ {
+ grub_script_execute_sourcecode(g_list_script_buf);
+ }
+ else
+ {
+ g_ventoy_last_entry = -1;
+ grub_snprintf(memfile, sizeof(memfile), "configfile mem:0x%llx:size:%d",
+ (ulonglong)(ulong)g_tree_script_buf, g_tree_script_pos);
+ grub_script_execute_sourcecode(memfile);
+ }
+
+ return 0;
+}
+
+static grub_err_t ventoy_cmd_find_bootable_hdd(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int id = 0;
+ int find = 0;
+ grub_disk_t disk;
+ const char *isopath = NULL;
+ char hdname[32];
+ ventoy_mbr_head mbr;
+
+ (void)ctxt;
+ (void)argc;
+
+ if (argc != 1)
+ {
+ return grub_error(GRUB_ERR_BAD_ARGUMENT, "Usage: %s variable\n", cmd_raw_name);
+ }
+
+ isopath = grub_env_get("iso_path");
+ if (!isopath)
+ {
+ debug("isopath is null %p\n", isopath);
+ return 0;
+ }
+
+ debug("isopath is %s\n", isopath);
+
+ for (id = 0; id < 30 && (find == 0); id++)
+ {
+ grub_snprintf(hdname, sizeof(hdname), "hd%d,", id);
+ if (grub_strstr(isopath, hdname))
+ {
+ debug("skip %s ...\n", hdname);
+ continue;
+ }
+
+ grub_snprintf(hdname, sizeof(hdname), "hd%d", id);
+
+ disk = grub_disk_open(hdname);
+ if (!disk)
+ {
+ debug("%s not exist\n", hdname);
+ break;
+ }
+
+ grub_memset(&mbr, 0, sizeof(mbr));
+ if (0 == grub_disk_read(disk, 0, 0, 512, &mbr))
+ {
+ if (mbr.Byte55 == 0x55 && mbr.ByteAA == 0xAA)
+ {
+ if (mbr.PartTbl[0].Active == 0x80 || mbr.PartTbl[1].Active == 0x80 ||
+ mbr.PartTbl[2].Active == 0x80 || mbr.PartTbl[3].Active == 0x80)
+ {
+
+ grub_env_set(args[0], hdname);
+ find = 1;
+ }
+ }
+ debug("%s is %s\n", hdname, find ? "bootable" : "NOT bootable");
+ }
+ else
+ {
+ debug("read %s failed\n", hdname);
+ }
+
+ grub_disk_close(disk);
+ }
+
+ return 0;
+}
+