+static grub_err_t ventoy_cmd_push_last_entry(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ g_ventoy_last_entry_back = g_ventoy_last_entry;
+ g_ventoy_last_entry = -1;
+
+ return 0;
+}
+
+static grub_err_t ventoy_cmd_pop_last_entry(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ g_ventoy_last_entry = g_ventoy_last_entry_back;
+
+ return 0;
+}
+
+static int ventoy_lib_module_callback(const char *filename, const struct grub_dirhook_info *info, void *data)
+{
+ const char *pos = filename + 1;
+
+ if (info->dir)
+ {
+ while (*pos)
+ {
+ if (*pos == '.')
+ {
+ if ((*(pos - 1) >= '0' && *(pos - 1) <= '9') && (*(pos + 1) >= '0' && *(pos + 1) <= '9'))
+ {
+ grub_strncpy((char *)data, filename, 128);
+ return 1;
+ }
+ }
+ pos++;
+ }
+ }
+
+ return 0;
+}
+
+static grub_err_t ventoy_cmd_lib_module_ver(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int rc = 1;
+ char *device_name = NULL;
+ grub_device_t dev = NULL;
+ grub_fs_t fs = NULL;
+ char buf[128] = {0};
+
+ (void)ctxt;
+
+ if (argc != 3)
+ {
+ debug("ventoy_cmd_lib_module_ver, invalid param num %d\n", argc);
+ return 1;
+ }
+
+ debug("ventoy_cmd_lib_module_ver %s %s %s\n", args[0], args[1], args[2]);
+
+ device_name = grub_file_get_device_name(args[0]);
+ if (!device_name)
+ {
+ debug("grub_file_get_device_name failed, %s\n", args[0]);
+ goto end;
+ }
+
+ dev = grub_device_open(device_name);
+ if (!dev)
+ {
+ debug("grub_device_open failed, %s\n", device_name);
+ goto end;
+ }
+
+ fs = grub_fs_probe(dev);
+ if (!fs)
+ {
+ debug("grub_fs_probe failed, %s\n", device_name);
+ goto end;
+ }
+
+ fs->fs_dir(dev, args[1], ventoy_lib_module_callback, buf);
+
+ if (buf[0])
+ {
+ ventoy_set_env(args[2], buf);
+ }
+
+ rc = 0;
+
+end:
+
+ check_free(device_name, grub_free);
+ check_free(dev, grub_device_close);
+
+ return rc;
+}
+