#include <sys/types.h>
#include <sys/mount.h>
#include <linux/fs.h>
+#include <linux/limits.h>
#include <dirent.h>
#include <pthread.h>
#include <ventoy_define.h>
#include <ventoy_http.h>
#include "fat_filelib.h"
+static char *g_pub_out_buf = NULL;
+static int g_pub_out_max = 0;
+
static pthread_mutex_t g_api_mutex;
static char g_cur_language[128];
static int g_cur_part_style = 0;
+static int g_cur_show_all = 0;
static char g_cur_server_token[64];
static struct mg_context *g_ventoy_http_ctx = NULL;
static char g_cur_process_diskname[64];
static char g_cur_process_type[64];
-static int g_cur_process_result = 0;
-static PROGRESS_POINT g_current_progress = PT_FINISH;
+static volatile int g_cur_process_result = 0;
+static volatile PROGRESS_POINT g_current_progress = PT_FINISH;
static int ventoy_load_mbr_template(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;
}
- fprintf(fp, "[Ventoy]\nLanguage=%s\nPartStyle=%d\n", g_cur_language, g_cur_part_style);
+ fprintf(fp, "[Ventoy]\nLanguage=%s\nPartStyle=%d\nShowAllDevice=%d\n",
+ g_cur_language, g_cur_part_style, g_cur_show_all);
fclose(fp);
return 0;
char line[256];
FILE *fp;
- fp = fopen("./Ventoy2Disk.ini", "r");
+ fp = fopen(g_ini_file, "r");
if (!fp)
{
return 0;
{
g_cur_part_style = (int)strtol(line + strlen("PartStyle="), NULL, 10);
}
+ else if (strncmp(line, "ShowAllDevice=", strlen("ShowAllDevice=")) == 0)
+ {
+ g_cur_show_all = (int)strtol(line + strlen("ShowAllDevice="), NULL, 10);
+ }
}
fclose(fp);
static int ventoy_json_result(struct mg_connection *conn, const char *err)
{
- mg_printf(conn,
- "HTTP/1.1 200 OK \r\n"
- "Content-Type: application/json\r\n"
- "Content-Length: %d\r\n"
- "\r\n%s",
- (int)strlen(err), err);
+ if (conn)
+ {
+ mg_printf(conn,
+ "HTTP/1.1 200 OK \r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: %d\r\n"
+ "\r\n%s",
+ (int)strlen(err), err);
+ }
+ else
+ {
+ memcpy(g_pub_out_buf, err, (int)strlen(err) + 1);
+ }
+
return 0;
}
static int ventoy_json_buffer(struct mg_connection *conn, const char *json_buf, int json_len)
{
- mg_printf(conn,
- "HTTP/1.1 200 OK \r\n"
- "Content-Type: application/json\r\n"
- "Content-Length: %d\r\n"
- "\r\n%s",
- json_len, json_buf);
+ if (conn)
+ {
+ mg_printf(conn,
+ "HTTP/1.1 200 OK \r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: %d\r\n"
+ "\r\n%s",
+ json_len, json_buf);
+ }
+ else
+ {
+ if (json_len >= g_pub_out_max)
+ {
+ vlog("json buffer overflow\n");
+ }
+ else
+ {
+ memcpy(g_pub_out_buf, json_buf, json_len);
+ g_pub_out_buf[json_len] = 0;
+ }
+ }
+
return 0;
}
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");
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;
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");
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");
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)
{
return 0;
}
+int ventoy_func_handler(const char *jsonstr, char *jsonbuf, int buflen)
+{
+ int i;
+ const char *method = NULL;
+ VTOY_JSON *json = NULL;
+
+ g_pub_out_buf = jsonbuf;
+ g_pub_out_max = buflen;
+
+ json = vtoy_json_create();
+ if (JSON_SUCCESS == vtoy_json_parse(json, jsonstr))
+ {
+ pthread_mutex_lock(&g_api_mutex);
+
+ method = vtoy_json_get_string_ex(json->pstChild, "method");
+ for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
+ {
+ if (method && strcmp(method, g_ventoy_json_cb[i].method) == 0)
+ {
+ g_ventoy_json_cb[i].callback(NULL, json->pstChild);
+ break;
+ }
+ }
+
+ pthread_mutex_unlock(&g_api_mutex);
+ }
+ else
+ {
+ ventoy_json_result(NULL, VTOY_JSON_INVALID_RET);
+ }
+
+ vtoy_json_destroy(json);
+ return 0;
+}
+
static int ventoy_request_handler(struct mg_connection *conn)
{
int post_data_len;
{
"listening_ports", "24680",
"document_root", "WebUI",
- "error_log_file", VTOY_LOG_FILE,
+ "error_log_file", g_log_file,
"request_timeout_ms", "10000",
NULL
};
g_efi_part_raw_img = NULL;
}
+
+const char * ventoy_code_get_cur_language(void)
+{
+ return g_cur_language;
+}
+
+int ventoy_code_get_cur_part_style(void)
+{
+ return g_cur_part_style;
+}
+
+void ventoy_code_set_cur_part_style(int style)
+{
+ pthread_mutex_lock(&g_api_mutex);
+
+ g_cur_part_style = style;
+ ventoy_http_save_cfg();
+
+ pthread_mutex_unlock(&g_api_mutex);
+}
+
+int ventoy_code_get_cur_show_all(void)
+{
+ return g_cur_show_all;
+}
+
+void ventoy_code_set_cur_show_all(int show_all)
+{
+ pthread_mutex_lock(&g_api_mutex);
+
+ g_cur_show_all = show_all;
+ ventoy_http_save_cfg();
+
+ pthread_mutex_unlock(&g_api_mutex);
+}
+
+void ventoy_code_set_cur_language(const char *lang)
+{
+ pthread_mutex_lock(&g_api_mutex);
+
+ scnprintf(g_cur_language, "%s", lang);
+ ventoy_http_save_cfg();
+
+ pthread_mutex_unlock(&g_api_mutex);
+}
+
+void ventoy_code_refresh_device(void)
+{
+ if (g_current_progress == PT_FINISH)
+ {
+ g_disk_num = 0;
+ ventoy_disk_enumerate_all();
+ }
+}
+
+int ventoy_code_is_busy(void)
+{
+ return (g_current_progress == PT_FINISH) ? 0 : 1;
+}
+
+int ventoy_code_get_percent(void)
+{
+ return g_current_progress * 100 / PT_FINISH;
+}
+
+int ventoy_code_get_result(void)
+{
+ return g_cur_process_result;
+}
+
+void ventoy_code_save_cfg(void)
+{
+ ventoy_http_save_cfg();
+}