X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/dd2411d7d47fd72be78802cb28d6f0ef73785e9e..6fbc08cfcbfdf97fa2457739f47dced84d857799:/LinuxGUI/Ventoy2Disk/Web/ventoy_http.c diff --git a/LinuxGUI/Ventoy2Disk/Web/ventoy_http.c b/LinuxGUI/Ventoy2Disk/Web/ventoy_http.c index 77fcebf..88f0e95 100644 --- a/LinuxGUI/Ventoy2Disk/Web/ventoy_http.c +++ b/LinuxGUI/Ventoy2Disk/Web/ventoy_http.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -160,9 +161,10 @@ static int ventoy_http_save_cfg(void) { FILE *fp; - fp = fopen("./Ventoy2Disk.ini", "w"); + fp = fopen(g_ini_file, "w"); if (!fp) { + vlog("Failed to open %s code:%d\n", g_ini_file, errno); return 0; } @@ -180,7 +182,7 @@ static int ventoy_http_load_cfg(void) char line[256]; FILE *fp; - fp = fopen("./Ventoy2Disk.ini", "r"); + fp = fopen(g_ini_file, "r"); if (!fp) { return 0; @@ -493,6 +495,7 @@ static int VentoyProcSecureBoot(int SecureBoot) fl_remove("/EFI/BOOT/grubx64.efi"); fl_remove("/EFI/BOOT/grubx64_real.efi"); fl_remove("/EFI/BOOT/MokManager.efi"); + fl_remove("/EFI/BOOT/mmx64.efi"); fl_remove("/ENROLL_THIS_KEY_IN_MOKMANAGER.cer"); file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb"); @@ -680,6 +683,42 @@ 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; @@ -688,6 +727,7 @@ static void * ventoy_update_thread(void *data) MBR_HEAD MBR; ventoy_disk *disk = NULL; ventoy_thread_data *thread = (ventoy_thread_data *)data; + VTOY_GPT_INFO *pstGPT = NULL; vdebug("ventoy_update_thread run ...\n"); @@ -740,19 +780,47 @@ 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); - memcpy(&MBR, &(disk->vtoydata.gptinfo.MBR), 512); - if (disk->vtoydata.partition_style == 0 && MBR.PartTbl[0].Active == 0) + if (ventoy_mbr_need_update(disk, &MBR)) { - MBR.PartTbl[0].Active = 0x80; - MBR.PartTbl[1].Active = 0; - MBR.PartTbl[2].Active = 0; - MBR.PartTbl[3].Active = 0; - offset = lseek(fd, 0, SEEK_SET); len = write(fd, &MBR, 512); - vlog("set MBR partition 1 active flag enabled offset:%llu len:%llu\n", (_ull)offset, (_ull)len); + vlog("update MBR offset:%llu len:%llu\n", (_ull)offset, (_ull)len); + } + else + { + vlog("No need to update MBR\n"); + } + + + if (disk->vtoydata.partition_style) + { + pstGPT = (VTOY_GPT_INFO *)malloc(sizeof(VTOY_GPT_INFO)); + memset(pstGPT, 0, sizeof(VTOY_GPT_INFO)); + + offset = lseek(fd, 0, SEEK_SET); + len = read(fd, pstGPT, sizeof(VTOY_GPT_INFO)); + vlog("Read GPT table offset:%llu len:%llu ...\n", (_ull)offset, (_ull)len); + + if (pstGPT->PartTbl[1].Attr != 0x8000000000000000ULL) + { + vlog("Update EFI part attr from 0x%016llx to 0x%016llx\n", + pstGPT->PartTbl[1].Attr, 0x8000000000000000ULL); + + pstGPT->PartTbl[1].Attr = 0x8000000000000000ULL; + + pstGPT->Head.PartTblCrc = ventoy_crc32(pstGPT->PartTbl, sizeof(pstGPT->PartTbl)); + pstGPT->Head.Crc = 0; + pstGPT->Head.Crc = ventoy_crc32(&(pstGPT->Head), pstGPT->Head.Length); + ventoy_write_gpt_part_table(fd, disk->size_in_byte, pstGPT); + } + else + { + vlog("No need to update EFI part attr\n"); + } + free(pstGPT); } + g_current_progress = PT_SYNC_DATA1; vlog("fsync data1...\n"); @@ -1085,6 +1153,13 @@ static int ventoy_api_install(struct mg_connection *conn, VTOY_JSON *json) return 0; } + if (disk->is4kn) + { + vlog("disk %s is 4k native, not supported.\n", diskname); + ventoy_json_result(conn, VTOY_JSON_4KN_RET); + return 0; + } + scnprintf(path, "/sys/block/%s", diskname); if (access(path, F_OK) < 0) { @@ -1499,7 +1574,7 @@ int ventoy_http_start(const char *ip, const char *port) { "listening_ports", "24680", "document_root", "WebUI", - "error_log_file", VTOY_LOG_FILE, + "error_log_file", g_log_file, "request_timeout_ms", "10000", NULL };