]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - VtoyTool/vtoydump.c
issue template update
[Ventoy.git] / VtoyTool / vtoydump.c
index ff3a4d260f4234a337bc3e2a819e66ccd05fc3aa..e58cc868eb27ca9c4d1984ea1b8fd402ce567a7c 100644 (file)
@@ -35,7 +35,9 @@
 #define IS_DIGIT(x) ((x) >= '0' && (x) <= '9')
 
 #ifndef USE_DIET_C
+#ifndef __mips__
 typedef unsigned long long uint64_t;
+#endif
 typedef unsigned int    uint32_t;
 typedef unsigned short  uint16_t;
 typedef unsigned char   uint8_t;
@@ -433,41 +435,22 @@ static int vtoy_printf_iso_path(ventoy_os_param *param)
     return 0;
 }
 
-static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
+static int vtoy_printf_fs(ventoy_os_param *param)
 {
-    int   cnt = 0;
-    char *path = param->vtoy_img_path;
-    const char *fs;
-
-    cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
-    if (cnt > 1)
+    const char *fs[] = 
     {
-        cnt = vtoy_find_disk_by_guid(param, diskname);
-    }
-    else if (cnt == 0)
-    {
-        cnt = vtoy_find_disk_by_guid(param, diskname);
-        debug("find 0 disk by size, try with guid cnt=%d...\n", cnt);
-    }
+        "exfat", "ntfs", "ext", "xfs", "udf", "fat"
+    };
 
-    if (param->vtoy_disk_part_type < ventoy_fs_max)
+    if (param->vtoy_disk_part_type < 6)
     {
-        fs = g_ventoy_fs[param->vtoy_disk_part_type];
+        printf("%s\n", fs[param->vtoy_disk_part_type]);
     }
     else
     {
-        fs = "unknown";
-    }
-
-    if (1 == cnt)
-    {
-        printf("/dev/%s#%s#%s\n", diskname, fs, path);
-        return 0;
-    }
-    else
-    {
-        return 1;
+        printf("unknown\n");
     }
+    return 0;
 }
 
 static int vtoy_check_device(ventoy_os_param *param, const char *device)
@@ -484,8 +467,7 @@ static int vtoy_check_device(ventoy_os_param *param, const char *device)
     debug("param->vtoy_disk_size=%llu size=%llu\n", 
         (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
 
-    if ((param->vtoy_disk_size == size || param->vtoy_disk_size == size + 512) && 
-        memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
+    if (memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
         memcmp(vtsig, param->vtoy_disk_signature, 4) == 0)
     {
         debug("<%s> is right ventoy disk\n", device);
@@ -498,6 +480,81 @@ static int vtoy_check_device(ventoy_os_param *param, const char *device)
     }
 }
 
+static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
+{
+    int fd, size;
+    int cnt = 0;
+    char *path = param->vtoy_img_path;
+    const char *fs;
+    char diskpath[256] = {0};
+    char sizebuf[64] = {0};
+    
+    cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
+    debug("find disk by size %llu, cnt=%d...\n", (unsigned long long)param->vtoy_disk_size, cnt);
+    if (1 == cnt)
+    {
+        if (vtoy_check_device(param, diskname) != 0)
+        {
+            cnt = 0;
+        }
+    }
+    else
+    {
+        cnt = vtoy_find_disk_by_guid(param, diskname);
+        debug("find disk by guid cnt=%d...\n", cnt);
+    }
+    
+    if (param->vtoy_disk_part_type < ventoy_fs_max)
+    {
+        fs = g_ventoy_fs[param->vtoy_disk_part_type];
+    }
+    else
+    {
+        fs = "unknown";
+    }
+
+    if (1 == cnt)
+    {
+        if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd"))
+        {
+            snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%sp2/size", diskname);
+        }
+        else
+        {
+            snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%s2/size", diskname);
+        }
+
+        if (access(diskpath, F_OK) >= 0)
+        {
+            debug("get part size from sysfs for %s\n", diskpath);
+
+            fd = open(diskpath, O_RDONLY | O_BINARY);
+            if (fd >= 0)
+            {
+                read(fd, sizebuf, sizeof(sizebuf));
+                size = (int)strtoull(sizebuf, NULL, 10);
+                close(fd);
+                if ((size != (64 * 1024)) && (size != (8 * 1024)))
+                {
+                    debug("sizebuf=<%s> size=%d\n", sizebuf, size);
+                    return 1;
+                }
+            }
+        }
+        else
+        {
+            debug("%s not exist \n", diskpath);
+        }
+
+        printf("/dev/%s#%s#%s\n", diskname, fs, path);
+        return 0;
+    }
+    else
+    {
+        return 1;
+    }
+}
+
 /*
  *  Find disk and image path from ventoy runtime data.
  *  By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
@@ -512,12 +569,13 @@ int vtoydump_main(int argc, char **argv)
     int rc;
     int ch;
     int print_path = 0;
+    int print_fs = 0;
     char filename[256] = {0};
     char diskname[256] = {0};
     char device[64] = {0};
     ventoy_os_param *param = NULL;
 
-    while ((ch = getopt(argc, argv, "c:f:p:v::")) != -1)
+    while ((ch = getopt(argc, argv, "c:f:p:s:v::")) != -1)
     {
         if (ch == 'f')
         {
@@ -536,6 +594,11 @@ int vtoydump_main(int argc, char **argv)
             print_path = 1;
             strncpy(filename, optarg, sizeof(filename) - 1);
         }
+        else if (ch == 's')
+        {
+            print_fs = 1;
+            strncpy(filename, optarg, sizeof(filename) - 1);
+        }
         else
         {
             fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
@@ -563,8 +626,20 @@ int vtoydump_main(int argc, char **argv)
     rc = vtoy_os_param_from_file(filename, param);
     if (rc)
     {
-        debug("ventoy os param not found %d\n", rc);
-        goto end;
+        debug("ventoy os param not found %d %d\n", rc, ENOENT);
+        if (ENOENT == rc)
+        {
+            debug("now try with file %s\n", "/ventoy/ventoy_os_param");
+            rc = vtoy_os_param_from_file("/ventoy/ventoy_os_param", param);
+            if (rc)
+            {
+                goto end;
+            }
+        }
+        else
+        {
+            goto end;            
+        }        
     }
 
     if (verbose)
@@ -576,6 +651,10 @@ int vtoydump_main(int argc, char **argv)
     {
         rc = vtoy_printf_iso_path(param);
     }
+    else if (print_fs)
+    {
+        rc = vtoy_printf_fs(param);
+    }
     else if (device[0])
     {
         rc = vtoy_check_device(param, device);