]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - LinuxGUI/Ventoy2Disk/Core/ventoy_util.c
1.1.07 release
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Core / ventoy_util.c
index 562c3a093c3ac49cd641cc2846ccf47a39521066..cbee36a83533aac1e721ad71939fa003b58ca24a 100644 (file)
@@ -164,11 +164,40 @@ end:
     return mount;
 }
 
+static int ventoy_mount_path_escape(char *src, char *dst, int len)
+{
+    int i = 0;
+    int n = 0;
+    
+    dst[len - 1] = 0;
+
+    for (i = 0; i < len - 1; i++)
+    {
+        if (src[i] == '\\' && src[i + 1] == '0' && src[i + 2] == '4' && src[i + 3] == '0')
+        {
+            dst[n++] = ' ';
+            i += 3;
+        }
+        else
+        {
+            dst[n++] = src[i];
+        }
+    
+        if (src[i] == 0)
+        {
+            break;
+        }
+    }
+
+    return 0;
+}
+
 int ventoy_try_umount_disk(const char *devpath)
 {
     int rc;
     int len;
-    char line[512];
+    char line[1024];
+    char mntpt[1024];
     char *pos1 = NULL;
     char *pos2 = NULL;
     FILE *fp = NULL;
@@ -193,14 +222,15 @@ int ventoy_try_umount_disk(const char *devpath)
                     *pos2 = 0;
                 }
 
-                rc = umount(pos1 + 1);
+                ventoy_mount_path_escape(pos1 + 1, mntpt, sizeof(mntpt));                
+                rc = umount(mntpt);
                 if (rc)
                 {
-                    vdebug("umount %s %s [ failed ] error:%d\n", devpath, pos1 + 1, errno);                                        
+                    vdebug("umount <%s> <%s> [ failed ] error:%d\n", devpath, mntpt, errno);                                        
                 }
                 else
                 {
-                    vdebug("umount %s %s [ success ]\n", devpath, pos1 + 1);
+                    vdebug("umount <%s> <%s> [ success ]\n", devpath, mntpt);
                 }
             }
         }
@@ -210,7 +240,6 @@ int ventoy_try_umount_disk(const char *devpath)
     return 0;
 }
 
-
 int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen)
 {
     int FileSize;
@@ -299,6 +328,7 @@ static int VentoyFillProtectMBR(uint64_t DiskSizeBytes, MBR_HEAD *pMBR)
     vdebug("Disk signature: 0x%08x\n", DiskSignature);
 
     memcpy(pMBR->BootCode + 0x1B8, &DiskSignature, 4);
+    memcpy(pMBR->BootCode + 0x180, &Guid, 16);
 
     DiskSectorCount = DiskSizeBytes / 512 - 1;
     if (DiskSectorCount > 0xFFFFFFFF)
@@ -347,6 +377,7 @@ static int ventoy_fill_gpt_partname(uint16_t Name[36], const char *asciiName)
 int ventoy_fill_gpt(uint64_t size, uint64_t reserve, int align4k, VTOY_GPT_INFO *gpt)
 {
     uint64_t ReservedSector = 33;
+    uint64_t ModSectorCount = 0;
     uint64_t Part1SectorCount = 0;
     uint64_t DiskSectorCount = size / 512;
     VTOY_GPT_HDR *Head = &gpt->Head;
@@ -362,18 +393,28 @@ int ventoy_fill_gpt(uint64_t size, uint64_t reserve, int align4k, VTOY_GPT_INFO
         ReservedSector += reserve / 512;
     }
 
+    Part1SectorCount = DiskSectorCount - ReservedSector - (VTOYEFI_PART_BYTES / 512) - 2048;
+
+    ModSectorCount = (Part1SectorCount % 8);
+    if (ModSectorCount)
+    {
+        vlog("Part1SectorCount:%llu is not aligned by 4KB (%llu)\n", (_ull)Part1SectorCount, (_ull)ModSectorCount);
+    }
+
     // check aligned with 4KB
     if (align4k)
     {
-        if (DiskSectorCount % 8)
+        if (ModSectorCount)
+        {
+            vdebug("Disk need to align with 4KB %u\n", (uint32_t)ModSectorCount);
+            Part1SectorCount -= ModSectorCount;
+        }
+        else
         {
-            vdebug("Disk need to align with 4KB %u\n", (uint32_t)(DiskSectorCount % 8));
-            ReservedSector += (DiskSectorCount % 8);
+            vdebug("no need to align with 4KB\n");
         }
     }
 
-    Part1SectorCount = DiskSectorCount - ReservedSector - (VTOYEFI_PART_BYTES / 512) - 2048;
-
     memcpy(Head->Signature, "EFI PART", 8);
     Head->Version[2] = 0x01;
     Head->Length = 92;
@@ -401,7 +442,7 @@ int ventoy_fill_gpt(uint64_t size, uint64_t reserve, int align4k, VTOY_GPT_INFO
     ventoy_gen_preudo_uuid(&(Table[1].PartGuid));
     Table[1].StartLBA = Table[0].LastLBA + 1;
     Table[1].LastLBA = Table[1].StartLBA + VTOYEFI_PART_BYTES / 512 - 1;
-    Table[1].Attr = 0x8000000000000001ULL;
+    Table[1].Attr = 0x8000000000000000ULL;
     ventoy_fill_gpt_partname(Table[1].Name, "VTOYEFI");
 
 #if 0
@@ -461,7 +502,7 @@ int VentoyFillMBRLocation(uint64_t DiskSizeInBytes, uint32_t StartSectorId, uint
     return 0;
 }
 
-int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, int PartStyle, MBR_HEAD *pMBR)
+int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, MBR_HEAD *pMBR)
 {
     ventoy_guid Guid;
     uint32_t DiskSignature;
@@ -479,6 +520,7 @@ int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, int PartStyle,
     vdebug("Disk signature: 0x%08x\n", DiskSignature);
 
     memcpy(pMBR->BootCode + 0x1B8, &DiskSignature, 4);
+    memcpy(pMBR->BootCode + 0x180, &Guid, 16);
 
     if (size / 512 > 0xFFFFFFFF)
     {
@@ -498,11 +540,6 @@ int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, int PartStyle,
                ReservedSector = (uint32_t)(reserve / 512);
        }
 
-    if (PartStyle)
-    {
-        ReservedSector += 33; // backup GPT part table
-    }
-
     // check aligned with 4KB
     if (align4k)
     {
@@ -512,11 +549,14 @@ int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, int PartStyle,
             vlog("Disk need to align with 4KB %u\n", (uint32_t)(sectors % 8));
             ReservedSector += (uint32_t)(sectors % 8);
         }
+        else
+        {
+            vdebug("no need to align with 4KB\n");
+        }
     }
 
        vlog("ReservedSector: %u\n", ReservedSector);
 
-    
     //Part1
     PartStartSector = VTOYIMG_PART_START_SECTOR;
        PartSectorCount = DiskSectorCount - ReservedSector - VTOYEFI_PART_BYTES / 512 - PartStartSector;
@@ -539,3 +579,68 @@ int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, int PartStyle,
     return 0;
 }
 
+int ventoy_fill_mbr_4k(uint64_t size, uint64_t reserve, int align4k, MBR_HEAD *pMBR)
+{
+    ventoy_guid Guid;
+    uint32_t DiskSignature;
+    uint32_t DiskSectorCount;
+    uint32_t PartSectorCount;
+    uint32_t PartStartSector;
+       uint32_t ReservedSector;
+
+    VentoyGetLocalBootImg(pMBR);
+
+    ventoy_gen_preudo_uuid(&Guid);
+
+    memcpy(&DiskSignature, &Guid, sizeof(uint32_t));
+
+    vdebug("Disk signature: 0x%08x\n", DiskSignature);
+
+    memcpy(pMBR->BootCode + 0x1B8, &DiskSignature, 4);
+    memcpy(pMBR->BootCode + 0x180, &Guid, 16);
+
+    if (size / 4096 > 0xFFFFFFFF)
+    {
+        DiskSectorCount = 0xFFFFFFFF;
+    }
+    else
+    {
+        DiskSectorCount = (uint32_t)(size / 4096);
+    }
+
+       if (reserve <= 0)
+       {
+               ReservedSector = 0;
+       }
+       else
+       {
+               ReservedSector = (uint32_t)(reserve / 4096);
+       }
+
+    // check aligned with 4KB
+    vdebug("no need to align with 4KB for 4K native disk\n");
+
+       vlog("ReservedSector: %u\n", ReservedSector);
+
+    //Part1
+    PartStartSector = VTOYIMG_PART_START_SECTOR >> 3;
+       PartSectorCount = DiskSectorCount - ReservedSector - VTOYEFI_PART_BYTES / 4096 - PartStartSector;
+    VentoyFillMBRLocation(size, PartStartSector, PartSectorCount, pMBR->PartTbl);
+
+    pMBR->PartTbl[0].Active = 0x80; // bootable
+    pMBR->PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS
+
+    //Part2
+    PartStartSector += PartSectorCount;
+    PartSectorCount = VTOYEFI_PART_BYTES / 4096;
+    VentoyFillMBRLocation(size, PartStartSector, PartSectorCount, pMBR->PartTbl + 1);
+
+    pMBR->PartTbl[1].Active = 0x00; 
+    pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition
+
+    pMBR->Byte55 = 0x55;
+    pMBR->ByteAA = 0xAA;
+
+    return 0;
+}
+