Skip to content

Commit

Permalink
Fix the issue that Linux WEB/GUI does not restore MBR when updating. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ventoy committed Oct 30, 2022
1 parent 7d90912 commit 553c853
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 24 deletions.
Binary file modified INSTALL/tool/aarch64/Plugson
Binary file not shown.
Binary file modified INSTALL/tool/aarch64/V2DServer
Binary file not shown.
Binary file modified INSTALL/tool/aarch64/Ventoy2Disk.gtk3
Binary file not shown.
Binary file modified INSTALL/tool/aarch64/Ventoy2Disk.qt5
Binary file not shown.
Binary file modified INSTALL/tool/i386/Plugson
Binary file not shown.
Binary file modified INSTALL/tool/i386/V2DServer
Binary file not shown.
Binary file modified INSTALL/tool/i386/Ventoy2Disk.gtk2
Binary file not shown.
Binary file modified INSTALL/tool/i386/Ventoy2Disk.gtk3
Binary file not shown.
Binary file modified INSTALL/tool/i386/Ventoy2Disk.qt5
Binary file not shown.
Binary file modified INSTALL/tool/mips64el/Plugson
Binary file not shown.
Binary file modified INSTALL/tool/mips64el/V2DServer
Binary file not shown.
Binary file modified INSTALL/tool/mips64el/Ventoy2Disk.gtk3
Binary file not shown.
Binary file modified INSTALL/tool/mips64el/Ventoy2Disk.qt5
Binary file not shown.
Binary file modified INSTALL/tool/x86_64/Plugson
Binary file not shown.
Binary file modified INSTALL/tool/x86_64/V2DServer
Binary file not shown.
Binary file modified INSTALL/tool/x86_64/Ventoy2Disk.gtk2
Binary file not shown.
Binary file modified INSTALL/tool/x86_64/Ventoy2Disk.gtk3
Binary file not shown.
Binary file modified INSTALL/tool/x86_64/Ventoy2Disk.qt5
Binary file not shown.
1 change: 1 addition & 0 deletions LinuxGUI/Ventoy2Disk/Core/ventoy_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int
const char * ventoy_get_local_version(void);
int ventoy_fill_gpt(uint64_t size, uint64_t reserve, int align4k, VTOY_GPT_INFO *gpt);
int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, MBR_HEAD *pMBR);
int VentoyGetLocalBootImg(MBR_HEAD *pMBR);

#endif /* __VENTOY_UTIL_H__ */

66 changes: 42 additions & 24 deletions LinuxGUI/Ventoy2Disk/Web/ventoy_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,45 @@ static int ventoy_write_gpt_part_table(int fd, uint64_t disksize, VTOY_GPT_INFO
return 0;
}

static int ventoy_mbr_need_update(ventoy_disk *disk, MBR_HEAD *mbr)
{
int update = 0;
int partition_style;
MBR_HEAD LocalMBR;

partition_style = disk->vtoydata.partition_style;
memcpy(mbr, &(disk->vtoydata.gptinfo.MBR), 512);

VentoyGetLocalBootImg(&LocalMBR);
memcpy(LocalMBR.BootCode + 0x180, mbr->BootCode + 0x180, 16);
if (partition_style)
{
LocalMBR.BootCode[92] = 0x22;
}

if (memcmp(LocalMBR.BootCode, mbr->BootCode, 440))
{
memcpy(mbr->BootCode, LocalMBR.BootCode, 440);
vlog("MBR boot code different, must update it.\n");
update = 1;
}

if (partition_style == 0 && mbr->PartTbl[0].Active == 0)
{
mbr->PartTbl[0].Active = 0x80;
mbr->PartTbl[1].Active = 0;
mbr->PartTbl[2].Active = 0;
mbr->PartTbl[3].Active = 0;
vlog("set MBR partition 1 active flag enabled\n");
update = 1;
}

return update;
}

static void * ventoy_update_thread(void *data)
{
int fd;
int updateMBR;
ssize_t len;
off_t offset;
MBR_HEAD MBR;
Expand Down Expand Up @@ -744,34 +779,17 @@ static void * ventoy_update_thread(void *data)
len = write(fd, disk->vtoydata.rsvdata, sizeof(disk->vtoydata.rsvdata));
vlog("Writing reserve data offset:%llu len:%llu ...\n", (_ull)offset, (_ull)len);

updateMBR = 0;
memcpy(&MBR, &(disk->vtoydata.gptinfo.MBR), 512);

if (disk->vtoydata.partition_style == 0 && MBR.PartTbl[0].Active == 0)
{
MBR.PartTbl[0].Active = 0x80;
MBR.PartTbl[1].Active = 0;
MBR.PartTbl[2].Active = 0;
MBR.PartTbl[3].Active = 0;
updateMBR = 1;
vlog("set MBR partition 1 active flag enabled\n");
}

if (MBR.BootCode[0x190] != 0x56 || MBR.BootCode[0x191] != 0x54)
{
vlog("set VT data %02x %02x\n", MBR.BootCode[0x190], MBR.BootCode[0x191]);
MBR.BootCode[0x190] = 0x56;
MBR.BootCode[0x191] = 0x54;
updateMBR = 1;
}

if (updateMBR)
if (ventoy_mbr_need_update(disk, &MBR))
{
offset = lseek(fd, 0, SEEK_SET);
len = write(fd, &MBR, 512);
vlog("update MBR offset:%llu len:%llu\n", (_ull)offset, (_ull)len);
}

else
{
vlog("No need to update MBR\n");
}

g_current_progress = PT_SYNC_DATA1;

vlog("fsync data1...\n");
Expand Down

0 comments on commit 553c853

Please sign in to comment.