]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - VtoyTool/vtoydump.c
Updated Spanish Translation (#1039)
[Ventoy.git] / VtoyTool / vtoydump.c
index d33151a44029c1fde8a5c621ddd11c71e7d226d7..72b78151262fe3ab6b21fea17371f380edc84a22 100644 (file)
@@ -435,23 +435,57 @@ static int vtoy_printf_iso_path(ventoy_os_param *param)
     return 0;
 }
 
+static int vtoy_check_device(ventoy_os_param *param, const char *device)
+{
+    unsigned long long size; 
+    uint8_t vtguid[16] = {0};
+    uint8_t vtsig[4] = {0};
+
+    debug("vtoy_check_device for <%s>\n", device);
+
+    size = vtoy_get_disk_size_in_byte(device);
+    vtoy_get_disk_guid(device, vtguid, vtsig);
+
+    debug("param->vtoy_disk_size=%llu size=%llu\n", 
+        (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
+
+    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);
+        return 0;
+    }
+    else
+    {
+        debug("<%s> is NOT right ventoy disk\n", device);
+        return 1;
+    }
+}
+
 static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
 {
-    int   cnt = 0;
+    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);
-    if (cnt > 1)
+    debug("find disk by size %llu, cnt=%d...\n", (unsigned long long)param->vtoy_disk_size, cnt);
+    if (1 == cnt)
     {
-        cnt = vtoy_find_disk_by_guid(param, diskname);
+        if (vtoy_check_device(param, diskname) != 0)
+        {
+            cnt = 0;
+        }
     }
-    else if (cnt == 0)
+    else
     {
         cnt = vtoy_find_disk_by_guid(param, diskname);
-        debug("find 0 disk by size, try with guid cnt=%d...\n", cnt);
+        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];
@@ -463,38 +497,42 @@ static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
 
     if (1 == cnt)
     {
-        printf("/dev/%s#%s#%s\n", diskname, fs, path);
-        return 0;
-    }
-    else
-    {
-        return 1;
-    }
-}
-
-static int vtoy_check_device(ventoy_os_param *param, const char *device)
-{
-    unsigned long long size; 
-    uint8_t vtguid[16] = {0};
-    uint8_t vtsig[4] = {0};
-
-    debug("vtoy_check_device for <%s>\n", device);
+        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);
+        }
 
-    size = vtoy_get_disk_size_in_byte(device);
-    vtoy_get_disk_guid(device, vtguid, vtsig);
+        if (access(diskpath, F_OK) >= 0)
+        {
+            debug("get part size from sysfs for %s\n", diskpath);
 
-    debug("param->vtoy_disk_size=%llu size=%llu\n", 
-        (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
+            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);
+        }
 
-    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);
+        printf("/dev/%s#%s#%s\n", diskname, fs, path);
         return 0;
     }
     else
     {
-        debug("<%s> is NOT right ventoy disk\n", device);
         return 1;
     }
 }