}\r
\r
\r
-static DWORD GetVentoyVolumeName(int PhyDrive, UINT32 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)\r
+static DWORD GetVentoyVolumeName(int PhyDrive, UINT64 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)\r
{\r
size_t len;\r
BOOL bRet;\r
\r
PartOffset = 512ULL * StartSectorId;\r
\r
- Log("GetVentoyVolumeName PhyDrive %d SectorStart:%u PartOffset:%llu", PhyDrive, StartSectorId, (ULONGLONG)PartOffset);\r
+ Log("GetVentoyVolumeName PhyDrive %d SectorStart:%llu PartOffset:%llu", PhyDrive, (ULONGLONG)StartSectorId, (ULONGLONG)PartOffset);\r
\r
hVolume = FindFirstVolumeA(VolumeName, sizeof(VolumeName));\r
if (hVolume == INVALID_HANDLE_VALUE)\r
}\r
\r
\r
-int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen)\r
+int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen, BOOL *pSecureBoot)\r
{\r
int rc = 0;\r
HANDLE hDrive;\r
+ void *flfile;\r
\r
hDrive = GetPhysicalHandle(pDriveInfo->PhyDrive, FALSE, FALSE, FALSE);\r
if (hDrive == INVALID_HANDLE_VALUE)\r
if (rc == 0)\r
{\r
Log("VentoyVerInPhyDrive %d is <%s>...", pDriveInfo->PhyDrive, VerBuf);\r
+\r
+ flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");\r
+ if (flfile)\r
+ {\r
+ *pSecureBoot = TRUE;\r
+ fl_fclose(flfile);\r
+ }\r
}\r
\r
fl_shutdown();\r
free(filebuf);\r
}\r
}\r
+\r
+ file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");\r
+ Log("Open ventoy efi file %p ", file);\r
+ if (file)\r
+ {\r
+ fl_fseek(file, 0, SEEK_END);\r
+ size = (int)fl_ftell(file);\r
+ fl_fseek(file, 0, SEEK_SET);\r
+\r
+ Log("ventoy efi file size %d ...", size);\r
+\r
+ filebuf = (char *)malloc(size);\r
+ if (filebuf)\r
+ {\r
+ fl_fread(filebuf, 1, size, file);\r
+ }\r
+\r
+ fl_fclose(file);\r
+\r
+ Log("Now delete all efi files ...");\r
+ fl_remove("/EFI/BOOT/BOOTIA32.EFI");\r
+ fl_remove("/EFI/BOOT/grubia32.efi");\r
+ fl_remove("/EFI/BOOT/grubia32_real.efi");\r
+ fl_remove("/EFI/BOOT/mmia32.efi"); \r
+\r
+ file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");\r
+ Log("Open bootia32 efi file %p ", file);\r
+ if (file)\r
+ {\r
+ if (filebuf)\r
+ {\r
+ fl_fwrite(filebuf, 1, size, file);\r
+ }\r
+\r
+ fl_fflush(file);\r
+ fl_fclose(file);\r
+ }\r
+\r
+ if (filebuf)\r
+ {\r
+ free(filebuf);\r
+ }\r
+ }\r
+\r
}\r
else\r
{\r
goto End;\r
}\r
\r
- // clear first and last 1MB space\r
- pTmpBuf = malloc(SIZE_1MB);\r
+ // clear first and last 2MB space\r
+ pTmpBuf = malloc(SIZE_2MB);\r
if (!pTmpBuf)\r
{\r
Log("Failed to alloc memory.");\r
rc = 1;\r
goto End;\r
}\r
- memset(pTmpBuf, 0, SIZE_1MB); \r
+ memset(pTmpBuf, 0, SIZE_2MB);\r
\r
SET_FILE_POS(512);\r
- bRet = WriteFile(hDrive, pTmpBuf, SIZE_1MB - 512, &dwSize, NULL);\r
+ bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB - 512, &dwSize, NULL);\r
Log("Write fisrt 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
if (!bRet)\r
{\r
goto End;\r
}\r
\r
- SET_FILE_POS(SIZE_1MB);\r
- bRet = WriteFile(hDrive, pTmpBuf, SIZE_1MB, &dwSize, NULL);\r
+ SET_FILE_POS(pPhyDrive->SizeInBytes - SIZE_2MB);\r
+ bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB, &dwSize, NULL);\r
Log("Write 2nd 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
if (!bRet)\r
{\r
}\r
\r
SET_FILE_POS(0);\r
- bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);\r
- Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
- if (!bRet)\r
+\r
+ if (pPhyDrive->SizeInBytes > 2199023255552ULL)\r
{\r
- rc = 1;\r
- goto End;\r
- }\r
+ VTOY_GPT_INFO *pGptInfo;\r
+ VTOY_GPT_HDR BackupHead;\r
+ LARGE_INTEGER liCurrentPosition;\r
+\r
+ pGptInfo = (VTOY_GPT_INFO *)pTmpBuf;\r
\r
- //clear boot code and partition table (reserved disk signature)\r
- memset(MBR.BootCode, 0, 440);\r
- memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));\r
+ VentoyFillWholeGpt(pPhyDrive->SizeInBytes, pGptInfo);\r
\r
- VentoyFillLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);\r
+ SET_FILE_POS(pPhyDrive->SizeInBytes - 512);\r
+ VentoyFillBackupGptHead(pGptInfo, &BackupHead);\r
+ if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))\r
+ {\r
+ rc = 1;\r
+ Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+ goto End;\r
+ }\r
\r
- MBR.PartTbl[0].Active = 0x00; // bootable\r
- MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS\r
+ SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);\r
+ if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))\r
+ {\r
+ rc = 1;\r
+ Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+ goto End;\r
+ }\r
\r
- SET_FILE_POS(0);\r
- bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);\r
- Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
- if (!bRet)\r
+ SET_FILE_POS(0);\r
+ if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))\r
+ {\r
+ rc = 1;\r
+ Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+ goto End;\r
+ }\r
+\r
+ Log("Write GPT Info OK ...");\r
+ }\r
+ else\r
{\r
- rc = 1;\r
- goto End;\r
+ bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);\r
+ Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
+ if (!bRet)\r
+ {\r
+ rc = 1;\r
+ goto End;\r
+ }\r
+\r
+ //clear boot code and partition table (reserved disk signature)\r
+ memset(MBR.BootCode, 0, 440);\r
+ memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));\r
+\r
+ VentoyFillMBRLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);\r
+\r
+ MBR.PartTbl[0].Active = 0x00; // bootable\r
+ MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS\r
+\r
+ SET_FILE_POS(0);\r
+ bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);\r
+ Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
+ if (!bRet)\r
+ {\r
+ rc = 1;\r
+ goto End;\r
+ }\r
}\r
\r
Log("Clear Ventoy successfully finished");\r
if (state != 1)\r
{\r
Log("need to mount ventoy part1...");\r
- if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[0].StartSectorId, DriveLetters, sizeof(DriveLetters), FALSE))\r
+ if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, 2048, DriveLetters, sizeof(DriveLetters), FALSE))\r
{\r
DriveName[0] = MountDrive;\r
bRet = SetVolumeMountPointA(DriveName, DriveLetters);\r
CHAR DriveLetters[MAX_PATH] = { 0 };\r
MBR_HEAD MBR;\r
VTOY_GPT_INFO *pGptInfo = NULL;\r
+ UINT64 Part1StartSector = 0;\r
+ UINT64 Part1SectorCount = 0;\r
+ UINT64 Part2StartSector = 0;\r
+\r
\r
Log("InstallVentoy2PhyDrive %s PhyDrive%d <<%s %s %dGB>>",\r
PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,\r
\r
PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);\r
\r
- VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);//also used to format 1st partition in GPT mode\r
if (PartStyle)\r
{\r
VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);\r
+ Part1StartSector = pGptInfo->PartTbl[0].StartLBA;\r
+ Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;\r
+ Part2StartSector = pGptInfo->PartTbl[1].StartLBA;\r
+ }\r
+ else\r
+ {\r
+ VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);\r
+ Part1StartSector = MBR.PartTbl[0].StartSectorId;\r
+ Part1SectorCount = MBR.PartTbl[0].SectorCount;\r
+ Part2StartSector = MBR.PartTbl[1].StartSectorId;\r
}\r
\r
Log("Lock disk for clean ............................. ");\r
//Refresh Drive Layout\r
DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);\r
\r
- disk_io_set_param(hDrive, MBR.PartTbl[0].StartSectorId + MBR.PartTbl[0].SectorCount);\r
+ disk_io_set_param(hDrive, Part1StartSector + Part1SectorCount);// include the 2048 sector gap\r
\r
PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);\r
\r
goto End;\r
}\r
\r
+ \r
+\r
PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);\r
Log("Writing part2 FAT img ...");\r
- if (0 != FormatPart2Fat(hDrive, MBR.PartTbl[1].StartSectorId))\r
+ \r
+ if (0 != FormatPart2Fat(hDrive, Part2StartSector))\r
{\r
Log("FormatPart2Fat failed.");\r
rc = 1;\r
}\r
\r
PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);\r
- Log("Writting Boot Image ............................. ");\r
+ Log("Writing Boot Image ............................. ");\r
if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)\r
{\r
Log("WriteGrubStage1ToPhyDrive failed.");\r
}\r
\r
PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);\r
- Log("Writting Partition Table ........................ ");\r
+ Log("Writing Partition Table ........................ ");\r
SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);\r
\r
if (PartStyle)\r
}\r
\r
Log("Write GPT Info OK ...");\r
+ memcpy(&(pPhyDrive->MBR), &(pGptInfo->MBR), 512);\r
}\r
else\r
{\r
goto End;\r
}\r
Log("Write MBR OK ...");\r
+ memcpy(&(pPhyDrive->MBR), &MBR, 512);\r
}\r
- \r
\r
//Refresh Drive Layout\r
DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);\r
if (state != 1)\r
{\r
Log("need to mount ventoy part1...");\r
- if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[0].StartSectorId, DriveLetters, sizeof(DriveLetters), FALSE))\r
+ \r
+ if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, Part1StartSector, DriveLetters, sizeof(DriveLetters), FALSE))\r
{\r
DriveName[0] = MountDrive;\r
bRet = SetVolumeMountPointA(DriveName, DriveLetters);\r
MBR_HEAD BootImg;\r
MBR_HEAD MBR;\r
VTOY_GPT_INFO *pGptInfo = NULL;\r
+ UINT8 ReservedData[4096];\r
\r
Log("UpdateVentoy2PhyDrive %s PhyDrive%d <<%s %s %dGB>>",\r
pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,\r
Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);\r
}\r
\r
+ //Read Reserved Data\r
+ SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);\r
+ ReadFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);\r
+\r
GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));\r
\r
if (DriveLetters[0] == 0)\r
\r
Log("Lock volume for update .......................... ");\r
hVolume = INVALID_HANDLE_VALUE;\r
- Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[1].StartSectorId, DriveLetters, sizeof(DriveLetters), TRUE);\r
+ Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, StartSector, DriveLetters, sizeof(DriveLetters), TRUE);\r
if (ERROR_SUCCESS == Status)\r
{\r
Log("Now lock and dismount volume <%s>", DriveLetters);\r
\r
if (!TryWritePart2(hDrive, StartSector))\r
{\r
- ForceMBR = TRUE;\r
- Log("Try write failed, now delete partition 2...");\r
+ if (pPhyDrive->PartStyle == 0)\r
+ {\r
+ ForceMBR = TRUE;\r
+ Log("Try write failed, now delete partition 2...");\r
\r
- CHECK_CLOSE_HANDLE(hDrive);\r
+ CHECK_CLOSE_HANDLE(hDrive);\r
\r
- Log("Now delete partition 2...");\r
- DeletePartitions(pPhyDrive->PhyDrive, TRUE);\r
+ Log("Now delete partition 2...");\r
+ DeletePartitions(pPhyDrive->PhyDrive, TRUE);\r
\r
- hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);\r
- if (hDrive == INVALID_HANDLE_VALUE)\r
- {\r
- Log("Failed to GetPhysicalHandle for write.");\r
- rc = 1;\r
- goto End;\r
- }\r
+ hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);\r
+ if (hDrive == INVALID_HANDLE_VALUE)\r
+ {\r
+ Log("Failed to GetPhysicalHandle for write.");\r
+ rc = 1;\r
+ goto End;\r
+ }\r
+ }\r
}\r
\r
-\r
PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);\r
\r
Log("Write Ventoy to disk ............................ ");\r
goto End;\r
}\r
\r
+ //write reserved data\r
+ SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN); \r
+ bRet = WriteFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);\r
+ Log("Write resv data ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);\r
+\r
// Boot Image\r
VentoyGetLocalBootImg(&BootImg);\r
\r