]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Web/ventoy_http.c
Fix the issue that VTOY_LINUX_REMOUNT option does not take effect in latest openSUSE...
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Web / ventoy_http.c
1 /******************************************************************************
2 * ventoy_http.c ---- ventoy http
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/mount.h>
33 #include <linux/fs.h>
34 #include <linux/limits.h>
35 #include <dirent.h>
36 #include <pthread.h>
37 #include <ventoy_define.h>
38 #include <ventoy_json.h>
39 #include <ventoy_util.h>
40 #include <ventoy_disk.h>
41 #include <ventoy_http.h>
42 #include "fat_filelib.h"
43
44 static char *g_pub_out_buf = NULL;
45 static int g_pub_out_max = 0;
46
47 static pthread_mutex_t g_api_mutex;
48 static char g_cur_language[128];
49 static int g_cur_part_style = 0;
50 static int g_cur_show_all = 0;
51 static char g_cur_server_token[64];
52 static struct mg_context *g_ventoy_http_ctx = NULL;
53
54 static uint32_t g_efi_part_offset = 0;
55 static uint8_t *g_efi_part_raw_img = NULL;
56 static uint8_t *g_grub_stg1_raw_img = NULL;
57
58 static char g_cur_process_diskname[64];
59 static char g_cur_process_type[64];
60 static volatile int g_cur_process_result = 0;
61 static volatile PROGRESS_POINT g_current_progress = PT_FINISH;
62
63 static int ventoy_load_mbr_template(void)
64 {
65 FILE *fp = NULL;
66
67 fp = fopen("boot/boot.img", "rb");
68 if (fp == NULL)
69 {
70 vlog("Failed to open file boot/boot.img\n");
71 return 1;
72 }
73
74 fread(g_mbr_template, 1, 512, fp);
75 fclose(fp);
76
77 ventoy_gen_preudo_uuid(g_mbr_template + 0x180);
78 return 0;
79 }
80
81 static int ventoy_disk_xz_flush(void *src, unsigned int size)
82 {
83 memcpy(g_efi_part_raw_img + g_efi_part_offset, src, size);
84 g_efi_part_offset += size;
85
86 g_current_progress = PT_LOAD_DISK_IMG + (g_efi_part_offset / SIZE_1MB);
87 return (int)size;
88 }
89
90 static int ventoy_unxz_efipart_img(void)
91 {
92 int rc;
93 int inlen;
94 int xzlen;
95 void *xzbuf = NULL;
96 uint8_t *buf = NULL;
97
98 rc = ventoy_read_file_to_buf(VENTOY_FILE_DISK_IMG, 0, &xzbuf, &xzlen);
99 vdebug("read disk.img.xz rc:%d len:%d\n", rc, xzlen);
100
101 if (g_efi_part_raw_img)
102 {
103 buf = g_efi_part_raw_img;
104 }
105 else
106 {
107 buf = malloc(VTOYEFI_PART_BYTES);
108 if (!buf)
109 {
110 check_free(xzbuf);
111 return 1;
112 }
113 }
114
115 g_efi_part_offset = 0;
116 g_efi_part_raw_img = buf;
117
118 rc = unxz(xzbuf, xzlen, NULL, ventoy_disk_xz_flush, buf, &inlen, NULL);
119 vdebug("ventoy_unxz_efipart_img len:%d rc:%d unxzlen:%u\n", inlen, rc, g_efi_part_offset);
120
121 check_free(xzbuf);
122 return 0;
123 }
124
125 static int ventoy_unxz_stg1_img(void)
126 {
127 int rc;
128 int inlen;
129 int xzlen;
130 void *xzbuf = NULL;
131 uint8_t *buf = NULL;
132
133 rc = ventoy_read_file_to_buf(VENTOY_FILE_STG1_IMG, 0, &xzbuf, &xzlen);
134 vdebug("read core.img.xz rc:%d len:%d\n", rc, xzlen);
135
136 if (g_grub_stg1_raw_img)
137 {
138 buf = g_grub_stg1_raw_img;
139 }
140 else
141 {
142 buf = zalloc(SIZE_1MB);
143 if (!buf)
144 {
145 check_free(xzbuf);
146 return 1;
147 }
148 }
149
150 rc = unxz(xzbuf, xzlen, NULL, NULL, buf, &inlen, NULL);
151 vdebug("ventoy_unxz_stg1_img len:%d rc:%d\n", inlen, rc);
152
153 g_grub_stg1_raw_img = buf;
154
155 check_free(xzbuf);
156 return 0;
157 }
158
159
160 static int ventoy_http_save_cfg(void)
161 {
162 FILE *fp;
163
164 fp = fopen(g_ini_file, "w");
165 if (!fp)
166 {
167 vlog("Failed to open %s code:%d\n", g_ini_file, errno);
168 return 0;
169 }
170
171 fprintf(fp, "[Ventoy]\nLanguage=%s\nPartStyle=%d\nShowAllDevice=%d\n",
172 g_cur_language, g_cur_part_style, g_cur_show_all);
173
174 fclose(fp);
175 return 0;
176 }
177
178 static int ventoy_http_load_cfg(void)
179 {
180 int i;
181 int len;
182 char line[256];
183 FILE *fp;
184
185 fp = fopen(g_ini_file, "r");
186 if (!fp)
187 {
188 return 0;
189 }
190
191 while (fgets(line, sizeof(line), fp))
192 {
193 len = (int)strlen(line);
194 for (i = len - 1; i >= 0; i--)
195 {
196 if (line[i] == ' ' || line[i] == '\t' || line[i] == '\r' || line[i] == '\n')
197 {
198 line[i] = 0;
199 }
200 else
201 {
202 break;
203 }
204 }
205
206 len = (int)strlen("Language=");
207 if (strncmp(line, "Language=", len) == 0)
208 {
209 scnprintf(g_cur_language, "%s", line + len);
210 }
211 else if (strncmp(line, "PartStyle=", strlen("PartStyle=")) == 0)
212 {
213 g_cur_part_style = (int)strtol(line + strlen("PartStyle="), NULL, 10);
214 }
215 else if (strncmp(line, "ShowAllDevice=", strlen("ShowAllDevice=")) == 0)
216 {
217 g_cur_show_all = (int)strtol(line + strlen("ShowAllDevice="), NULL, 10);
218 }
219 }
220
221 fclose(fp);
222 return 0;
223 }
224
225
226 static int ventoy_json_result(struct mg_connection *conn, const char *err)
227 {
228 if (conn)
229 {
230 mg_printf(conn,
231 "HTTP/1.1 200 OK \r\n"
232 "Content-Type: application/json\r\n"
233 "Content-Length: %d\r\n"
234 "\r\n%s",
235 (int)strlen(err), err);
236 }
237 else
238 {
239 memcpy(g_pub_out_buf, err, (int)strlen(err) + 1);
240 }
241
242 return 0;
243 }
244
245 static int ventoy_json_buffer(struct mg_connection *conn, const char *json_buf, int json_len)
246 {
247 if (conn)
248 {
249 mg_printf(conn,
250 "HTTP/1.1 200 OK \r\n"
251 "Content-Type: application/json\r\n"
252 "Content-Length: %d\r\n"
253 "\r\n%s",
254 json_len, json_buf);
255 }
256 else
257 {
258 if (json_len >= g_pub_out_max)
259 {
260 vlog("json buffer overflow\n");
261 }
262 else
263 {
264 memcpy(g_pub_out_buf, json_buf, json_len);
265 g_pub_out_buf[json_len] = 0;
266 }
267 }
268
269 return 0;
270 }
271
272 static int ventoy_api_sysinfo(struct mg_connection *conn, VTOY_JSON *json)
273 {
274 int busy = 0;
275 int pos = 0;
276 int buflen = 0;
277 char buf[512];
278
279 (void)json;
280
281 busy = (g_current_progress == PT_FINISH) ? 0 : 1;
282
283 buflen = sizeof(buf) - 1;
284 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
285 VTOY_JSON_FMT_OBJ_BEGIN();
286 VTOY_JSON_FMT_STRN("token", g_cur_server_token);
287 VTOY_JSON_FMT_STRN("language", g_cur_language);
288 VTOY_JSON_FMT_STRN("ventoy_ver", ventoy_get_local_version());
289 VTOY_JSON_FMT_UINT("partstyle", g_cur_part_style);
290 VTOY_JSON_FMT_BOOL("busy", busy);
291 VTOY_JSON_FMT_STRN("process_disk", g_cur_process_diskname);
292 VTOY_JSON_FMT_STRN("process_type", g_cur_process_type);
293 VTOY_JSON_FMT_OBJ_END();
294 VTOY_JSON_FMT_END(pos);
295
296 ventoy_json_buffer(conn, buf, pos);
297 return 0;
298 }
299
300 static int ventoy_api_get_percent(struct mg_connection *conn, VTOY_JSON *json)
301 {
302 int pos = 0;
303 int buflen = 0;
304 int percent = 0;
305 char buf[128];
306
307 (void)json;
308
309 percent = g_current_progress * 100 / PT_FINISH;
310
311 buflen = sizeof(buf) - 1;
312 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
313 VTOY_JSON_FMT_OBJ_BEGIN();
314 VTOY_JSON_FMT_STRN("result", g_cur_process_result ? "failed" : "success");
315 VTOY_JSON_FMT_STRN("process_disk", g_cur_process_diskname);
316 VTOY_JSON_FMT_STRN("process_type", g_cur_process_type);
317 VTOY_JSON_FMT_UINT("percent", percent);
318 VTOY_JSON_FMT_OBJ_END();
319 VTOY_JSON_FMT_END(pos);
320
321 ventoy_json_buffer(conn, buf, pos);
322 return 0;
323 }
324
325 static int ventoy_api_set_language(struct mg_connection *conn, VTOY_JSON *json)
326 {
327 const char *lang = NULL;
328
329 lang = vtoy_json_get_string_ex(json, "language");
330 if (lang)
331 {
332 scnprintf(g_cur_language, "%s", lang);
333 ventoy_http_save_cfg();
334 }
335
336 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
337 return 0;
338 }
339
340 static int ventoy_api_set_partstyle(struct mg_connection *conn, VTOY_JSON *json)
341 {
342 int ret;
343 int style = 0;
344
345 ret = vtoy_json_get_int(json, "partstyle", &style);
346 if (JSON_SUCCESS == ret)
347 {
348 if ((style == 0) || (style == 1))
349 {
350 g_cur_part_style = style;
351 ventoy_http_save_cfg();
352 }
353 }
354
355 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
356 return 0;
357 }
358
359 static int ventoy_clean_disk(int fd, uint64_t size)
360 {
361 int zerolen;
362 ssize_t len;
363 off_t offset;
364 void *buf = NULL;
365
366 vdebug("ventoy_clean_disk fd:%d size:%llu\n", fd, (_ull)size);
367
368 zerolen = 64 * 1024;
369 buf = zalloc(zerolen);
370 if (!buf)
371 {
372 vlog("failed to alloc clean buffer\n");
373 return 1;
374 }
375
376 offset = lseek(fd, 0, SEEK_SET);
377 len = write(fd, buf, zerolen);
378 vdebug("write disk at off:%llu writelen:%lld datalen:%d\n", (_ull)offset, (_ll)len, zerolen);
379
380 offset = lseek(fd, size - zerolen, SEEK_SET);
381 len = write(fd, buf, zerolen);
382 vdebug("write disk at off:%llu writelen:%lld datalen:%d\n", (_ull)offset, (_ll)len, zerolen);
383
384 fsync(fd);
385
386 free(buf);
387 return 0;
388 }
389
390 static int ventoy_write_legacy_grub(int fd, int partstyle)
391 {
392 ssize_t len;
393 off_t offset;
394
395 if (partstyle)
396 {
397 vlog("Write GPT stage1 ...\n");
398
399 offset = lseek(fd, 512 * 34, SEEK_SET);
400
401 g_grub_stg1_raw_img[500] = 35;//update blocklist
402 len = write(fd, g_grub_stg1_raw_img, SIZE_1MB - 512 * 34);
403
404 vlog("lseek offset:%llu(%u) writelen:%llu(%u)\n", (_ull)offset, 512 * 34, (_ull)len, SIZE_1MB - 512 * 34);
405 if (SIZE_1MB - 512 * 34 != len)
406 {
407 vlog("write length error\n");
408 return 1;
409 }
410 }
411 else
412 {
413 vlog("Write MBR stage1 ...\n");
414 offset = lseek(fd, 512, SEEK_SET);
415 len = write(fd, g_grub_stg1_raw_img, SIZE_1MB - 512);
416
417 vlog("lseek offset:%llu(%u) writelen:%llu(%u)\n", (_ull)offset, 512, (_ull)len, SIZE_1MB - 512);
418 if (SIZE_1MB - 512 != len)
419 {
420 vlog("write length error\n");
421 return 1;
422 }
423 }
424
425 return 0;
426 }
427
428 static int VentoyFatMemRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
429 {
430 uint32 i;
431 uint32 offset;
432
433 for (i = 0; i < SectorCount; i++)
434 {
435 offset = (Sector + i) * 512;
436 memcpy(Buffer + i * 512, g_efi_part_raw_img + offset, 512);
437 }
438
439 return 1;
440 }
441
442 static int VentoyFatMemWrite(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
443 {
444 uint32 i;
445 uint32 offset;
446
447 for (i = 0; i < SectorCount; i++)
448 {
449 offset = (Sector + i) * 512;
450 memcpy(g_efi_part_raw_img + offset, Buffer + i * 512, 512);
451 }
452
453 return 1;
454 }
455
456 static int VentoyProcSecureBoot(int SecureBoot)
457 {
458 int rc = 0;
459 int size;
460 char *filebuf = NULL;
461 void *file = NULL;
462
463 vlog("VentoyProcSecureBoot %d ...\n", SecureBoot);
464
465 if (SecureBoot)
466 {
467 vlog("Secure boot is enabled ...\n");
468 return 0;
469 }
470
471 fl_init();
472
473 if (0 == fl_attach_media(VentoyFatMemRead, VentoyFatMemWrite))
474 {
475 file = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
476 vlog("Open ventoy efi file %p \n", file);
477 if (file)
478 {
479 fl_fseek(file, 0, SEEK_END);
480 size = (int)fl_ftell(file);
481 fl_fseek(file, 0, SEEK_SET);
482
483 vlog("ventoy efi file size %d ...\n", size);
484
485 filebuf = (char *)malloc(size);
486 if (filebuf)
487 {
488 fl_fread(filebuf, 1, size, file);
489 }
490
491 fl_fclose(file);
492
493 vlog("Now delete all efi files ...\n");
494 fl_remove("/EFI/BOOT/BOOTX64.EFI");
495 fl_remove("/EFI/BOOT/grubx64.efi");
496 fl_remove("/EFI/BOOT/grubx64_real.efi");
497 fl_remove("/EFI/BOOT/MokManager.efi");
498 fl_remove("/EFI/BOOT/mmx64.efi");
499 fl_remove("/ENROLL_THIS_KEY_IN_MOKMANAGER.cer");
500
501 file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
502 vlog("Open bootx64 efi file %p \n", file);
503 if (file)
504 {
505 if (filebuf)
506 {
507 fl_fwrite(filebuf, 1, size, file);
508 }
509
510 fl_fflush(file);
511 fl_fclose(file);
512 }
513
514 if (filebuf)
515 {
516 free(filebuf);
517 }
518 }
519
520 file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");
521 vlog("Open ventoy efi file %p\n", file);
522 if (file)
523 {
524 fl_fseek(file, 0, SEEK_END);
525 size = (int)fl_ftell(file);
526 fl_fseek(file, 0, SEEK_SET);
527
528 vlog("ventoy efi file size %d ...\n", size);
529
530 filebuf = (char *)malloc(size);
531 if (filebuf)
532 {
533 fl_fread(filebuf, 1, size, file);
534 }
535
536 fl_fclose(file);
537
538 vlog("Now delete all efi files ...\n");
539 fl_remove("/EFI/BOOT/BOOTIA32.EFI");
540 fl_remove("/EFI/BOOT/grubia32.efi");
541 fl_remove("/EFI/BOOT/grubia32_real.efi");
542 fl_remove("/EFI/BOOT/mmia32.efi");
543
544 file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");
545 vlog("Open bootia32 efi file %p\n", file);
546 if (file)
547 {
548 if (filebuf)
549 {
550 fl_fwrite(filebuf, 1, size, file);
551 }
552
553 fl_fflush(file);
554 fl_fclose(file);
555 }
556
557 if (filebuf)
558 {
559 free(filebuf);
560 }
561 }
562
563 }
564 else
565 {
566 rc = 1;
567 }
568
569 fl_shutdown();
570
571 return rc;
572 }
573
574 static int ventoy_check_efi_part_data(int fd, uint64_t offset)
575 {
576 int i;
577 ssize_t len;
578 char *buf;
579
580 buf = malloc(SIZE_1MB);
581 if (!buf)
582 {
583 return 0;
584 }
585
586 lseek(fd, offset, SEEK_SET);
587 for (i = 0; i < 32; i++)
588 {
589 len = read(fd, buf, SIZE_1MB);
590 if (len != SIZE_1MB || memcmp(buf, g_efi_part_raw_img + i * SIZE_1MB, SIZE_1MB))
591 {
592 vlog("part2 data check failed i=%d len:%llu\n", i, (_ull)len);
593 return 1;
594 }
595
596 g_current_progress = PT_CHECK_PART2 + (i / 4);
597 }
598
599 return 0;
600 }
601
602 static int ventoy_write_efipart(int fd, uint64_t offset, uint32_t secureboot)
603 {
604 int i;
605 ssize_t len;
606
607 vlog("Formatting part2 EFI offset:%llu ...\n", (_ull)offset);
608 lseek(fd, offset, SEEK_SET);
609
610 VentoyProcSecureBoot((int)secureboot);
611
612 g_current_progress = PT_WRITE_VENTOY_START;
613 for (i = 0; i < 32; i++)
614 {
615 len = write(fd, g_efi_part_raw_img + i * SIZE_1MB, SIZE_1MB);
616 vlog("write disk writelen:%lld datalen:%d [ %s ]\n",
617 (_ll)len, SIZE_1MB, (len == SIZE_1MB) ? "success" : "failed");
618
619 if (len != SIZE_1MB)
620 {
621 vlog("failed to format part2 EFI\n");
622 return 1;
623 }
624
625 g_current_progress = PT_WRITE_VENTOY_START + i / 4;
626 }
627
628 return 0;
629 }
630
631 static int VentoyFillBackupGptHead(VTOY_GPT_INFO *pInfo, VTOY_GPT_HDR *pHead)
632 {
633 uint64_t LBA;
634 uint64_t BackupLBA;
635
636 memcpy(pHead, &pInfo->Head, sizeof(VTOY_GPT_HDR));
637
638 LBA = pHead->EfiStartLBA;
639 BackupLBA = pHead->EfiBackupLBA;
640
641 pHead->EfiStartLBA = BackupLBA;
642 pHead->EfiBackupLBA = LBA;
643 pHead->PartTblStartLBA = BackupLBA + 1 - 33;
644
645 pHead->Crc = 0;
646 pHead->Crc = ventoy_crc32(pHead, pHead->Length);
647
648 return 0;
649 }
650
651 static int ventoy_write_gpt_part_table(int fd, uint64_t disksize, VTOY_GPT_INFO *gpt)
652 {
653 ssize_t len;
654 off_t offset;
655 VTOY_GPT_HDR BackupHead;
656
657 VentoyFillBackupGptHead(gpt, &BackupHead);
658
659 offset = lseek(fd, disksize - 512, SEEK_SET);
660 len = write(fd, &BackupHead, sizeof(VTOY_GPT_HDR));
661 vlog("write backup gpt part table off:%llu len:%llu\n", (_ull)offset, (_ull)len);
662 if (offset != disksize - 512 || len != sizeof(VTOY_GPT_HDR))
663 {
664 return 1;
665 }
666
667 offset = lseek(fd, disksize - 512 * 33, SEEK_SET);
668 len = write(fd, gpt->PartTbl, sizeof(gpt->PartTbl));
669 vlog("write main gpt part table off:%llu len:%llu\n", (_ull)offset, (_ull)len);
670 if (offset != disksize - 512 * 33 || len != sizeof(gpt->PartTbl))
671 {
672 return 1;
673 }
674
675 offset = lseek(fd, 0, SEEK_SET);
676 len = write(fd, gpt, sizeof(VTOY_GPT_INFO));
677 vlog("write gpt part head off:%llu len:%llu\n", (_ull)offset, (_ull)len);
678 if (offset != 0 || len != sizeof(VTOY_GPT_INFO))
679 {
680 return 1;
681 }
682
683 return 0;
684 }
685
686 static int ventoy_mbr_need_update(ventoy_disk *disk, MBR_HEAD *mbr)
687 {
688 int update = 0;
689 int partition_style;
690 MBR_HEAD LocalMBR;
691
692 partition_style = disk->vtoydata.partition_style;
693 memcpy(mbr, &(disk->vtoydata.gptinfo.MBR), 512);
694
695 VentoyGetLocalBootImg(&LocalMBR);
696 memcpy(LocalMBR.BootCode + 0x180, mbr->BootCode + 0x180, 16);
697 if (partition_style)
698 {
699 LocalMBR.BootCode[92] = 0x22;
700 }
701
702 if (memcmp(LocalMBR.BootCode, mbr->BootCode, 440))
703 {
704 memcpy(mbr->BootCode, LocalMBR.BootCode, 440);
705 vlog("MBR boot code different, must update it.\n");
706 update = 1;
707 }
708
709 if (partition_style == 0 && mbr->PartTbl[0].Active == 0)
710 {
711 mbr->PartTbl[0].Active = 0x80;
712 mbr->PartTbl[1].Active = 0;
713 mbr->PartTbl[2].Active = 0;
714 mbr->PartTbl[3].Active = 0;
715 vlog("set MBR partition 1 active flag enabled\n");
716 update = 1;
717 }
718
719 return update;
720 }
721
722 static void * ventoy_update_thread(void *data)
723 {
724 int fd;
725 ssize_t len;
726 off_t offset;
727 MBR_HEAD MBR;
728 ventoy_disk *disk = NULL;
729 ventoy_thread_data *thread = (ventoy_thread_data *)data;
730
731 vdebug("ventoy_update_thread run ...\n");
732
733 fd = thread->diskfd;
734 disk = thread->disk;
735
736 g_current_progress = PT_PRAPARE_FOR_CLEAN;
737 vdebug("check disk %s\n", disk->disk_name);
738 if (ventoy_is_disk_mounted(disk->disk_path))
739 {
740 vlog("disk is mounted, now try to unmount it ...\n");
741 ventoy_try_umount_disk(disk->disk_path);
742 }
743
744 if (ventoy_is_disk_mounted(disk->disk_path))
745 {
746 vlog("%s is mounted and can't umount!\n", disk->disk_path);
747 goto err;
748 }
749 else
750 {
751 vlog("disk is not mounted now, we can do continue ...\n");
752 }
753
754 g_current_progress = PT_LOAD_CORE_IMG;
755 ventoy_unxz_stg1_img();
756
757 g_current_progress = PT_LOAD_DISK_IMG;
758 ventoy_unxz_efipart_img();
759
760 g_current_progress = PT_FORMAT_PART2;
761
762 vlog("Formatting part2 EFI ...\n");
763 if (0 != ventoy_write_efipart(fd, disk->vtoydata.part2_start_sector * 512, thread->secure_boot))
764 {
765 vlog("Failed to format part2 efi ...\n");
766 goto err;
767 }
768
769 g_current_progress = PT_WRITE_STG1_IMG;
770
771 vlog("Writing legacy grub ...\n");
772 if (0 != ventoy_write_legacy_grub(fd, disk->vtoydata.partition_style))
773 {
774 vlog("ventoy_write_legacy_grub failed ...\n");
775 goto err;
776 }
777
778 offset = lseek(fd, 512 * 2040, SEEK_SET);
779 len = write(fd, disk->vtoydata.rsvdata, sizeof(disk->vtoydata.rsvdata));
780 vlog("Writing reserve data offset:%llu len:%llu ...\n", (_ull)offset, (_ull)len);
781
782 if (ventoy_mbr_need_update(disk, &MBR))
783 {
784 offset = lseek(fd, 0, SEEK_SET);
785 len = write(fd, &MBR, 512);
786 vlog("update MBR offset:%llu len:%llu\n", (_ull)offset, (_ull)len);
787 }
788 else
789 {
790 vlog("No need to update MBR\n");
791 }
792
793 g_current_progress = PT_SYNC_DATA1;
794
795 vlog("fsync data1...\n");
796 fsync(fd);
797 vtoy_safe_close_fd(fd);
798
799 g_current_progress = PT_SYNC_DATA2;
800
801 vlog("====================================\n");
802 vlog("====== ventoy update success ======\n");
803 vlog("====================================\n");
804 goto end;
805
806 err:
807 g_cur_process_result = 1;
808 vtoy_safe_close_fd(fd);
809
810 end:
811 g_current_progress = PT_FINISH;
812
813 check_free(thread);
814
815 return NULL;
816 }
817
818 static void * ventoy_install_thread(void *data)
819 {
820 int fd;
821 ssize_t len;
822 off_t offset;
823 MBR_HEAD MBR;
824 ventoy_disk *disk = NULL;
825 VTOY_GPT_INFO *gpt = NULL;
826 ventoy_thread_data *thread = (ventoy_thread_data *)data;
827 uint64_t Part1StartSector = 0;
828 uint64_t Part1SectorCount = 0;
829 uint64_t Part2StartSector = 0;
830
831 vdebug("ventoy_install_thread run ...\n");
832
833 fd = thread->diskfd;
834 disk = thread->disk;
835
836 g_current_progress = PT_PRAPARE_FOR_CLEAN;
837 vdebug("check disk %s\n", disk->disk_name);
838 if (ventoy_is_disk_mounted(disk->disk_path))
839 {
840 vlog("disk is mounted, now try to unmount it ...\n");
841 ventoy_try_umount_disk(disk->disk_path);
842 }
843
844 if (ventoy_is_disk_mounted(disk->disk_path))
845 {
846 vlog("%s is mounted and can't umount!\n", disk->disk_path);
847 goto err;
848 }
849 else
850 {
851 vlog("disk is not mounted now, we can do continue ...\n");
852 }
853
854 g_current_progress = PT_DEL_ALL_PART;
855 ventoy_clean_disk(fd, disk->size_in_byte);
856
857 g_current_progress = PT_LOAD_CORE_IMG;
858 ventoy_unxz_stg1_img();
859
860 g_current_progress = PT_LOAD_DISK_IMG;
861 ventoy_unxz_efipart_img();
862
863 if (thread->partstyle)
864 {
865 vdebug("Fill GPT part table\n");
866 gpt = zalloc(sizeof(VTOY_GPT_INFO));
867 ventoy_fill_gpt(disk->size_in_byte, thread->reserveBytes, thread->align4kb, gpt);
868 Part1StartSector = gpt->PartTbl[0].StartLBA;
869 Part1SectorCount = gpt->PartTbl[0].LastLBA - Part1StartSector + 1;
870 Part2StartSector = gpt->PartTbl[1].StartLBA;
871 }
872 else
873 {
874 vdebug("Fill MBR part table\n");
875 ventoy_fill_mbr(disk->size_in_byte, thread->reserveBytes, thread->align4kb, &MBR);
876 Part1StartSector = MBR.PartTbl[0].StartSectorId;
877 Part1SectorCount = MBR.PartTbl[0].SectorCount;
878 Part2StartSector = MBR.PartTbl[1].StartSectorId;
879 }
880
881 vlog("Part1StartSector:%llu Part1SectorCount:%llu Part2StartSector:%llu\n",
882 (_ull)Part1StartSector, (_ull)Part1SectorCount, (_ull)Part2StartSector);
883
884 if (thread->partstyle != disk->partstyle)
885 {
886 vlog("Wait for format part1 (partstyle changed) ...\n");
887 sleep(1);
888 }
889
890 g_current_progress = PT_FORMAT_PART1;
891 vlog("Formatting part1 exFAT %s ...\n", disk->disk_path);
892 if (0 != mkexfat_main(disk->disk_path, fd, Part1SectorCount))
893 {
894 vlog("Failed to format exfat ...\n");
895 goto err;
896 }
897
898 g_current_progress = PT_FORMAT_PART2;
899 vlog("Formatting part2 EFI ...\n");
900 if (0 != ventoy_write_efipart(fd, Part2StartSector * 512, thread->secure_boot))
901 {
902 vlog("Failed to format part2 efi ...\n");
903 goto err;
904 }
905
906 g_current_progress = PT_WRITE_STG1_IMG;
907 vlog("Writing legacy grub ...\n");
908 if (0 != ventoy_write_legacy_grub(fd, thread->partstyle))
909 {
910 vlog("ventoy_write_legacy_grub failed ...\n");
911 goto err;
912 }
913
914 g_current_progress = PT_SYNC_DATA1;
915 vlog("fsync data1...\n");
916 fsync(fd);
917 vtoy_safe_close_fd(fd);
918
919 /* reopen for check part2 data */
920 vlog("Checking part2 efi data %s ...\n", disk->disk_path);
921 g_current_progress = PT_CHECK_PART2;
922 fd = open(disk->disk_path, O_RDONLY | O_BINARY);
923 if (fd < 0)
924 {
925 vlog("failed to open %s for check fd:%d err:%d\n", disk->disk_path, fd, errno);
926 goto err;
927 }
928
929 if (0 == ventoy_check_efi_part_data(fd, Part2StartSector * 512))
930 {
931 vlog("efi part data check success\n");
932 }
933 else
934 {
935 vlog("efi part data check failed\n");
936 goto err;
937 }
938
939 vtoy_safe_close_fd(fd);
940
941 /* reopen for write part table */
942 g_current_progress = PT_WRITE_PART_TABLE;
943 vlog("Writting Partition Table style:%d...\n", thread->partstyle);
944
945 fd = open(disk->disk_path, O_RDWR | O_BINARY);
946 if (fd < 0)
947 {
948 vlog("failed to open %s for part table fd:%d err:%d\n", disk->disk_path, fd, errno);
949 goto err;
950 }
951
952 if (thread->partstyle)
953 {
954 ventoy_write_gpt_part_table(fd, disk->size_in_byte, gpt);
955 }
956 else
957 {
958 offset = lseek(fd, 0, SEEK_SET);
959 len = write(fd, &MBR, 512);
960 vlog("Writting MBR Partition Table %llu %llu\n", (_ull)offset, (_ull)len);
961 if (offset != 0 || len != 512)
962 {
963 goto err;
964 }
965 }
966
967 g_current_progress = PT_SYNC_DATA2;
968 vlog("fsync data2...\n");
969 fsync(fd);
970 vtoy_safe_close_fd(fd);
971
972
973 vlog("====================================\n");
974 vlog("====== ventoy install success ======\n");
975 vlog("====================================\n");
976 goto end;
977
978 err:
979 g_cur_process_result = 1;
980 vtoy_safe_close_fd(fd);
981
982 end:
983 g_current_progress = PT_FINISH;
984
985 check_free(gpt);
986 check_free(thread);
987
988 return NULL;
989 }
990
991 static int ventoy_api_clean(struct mg_connection *conn, VTOY_JSON *json)
992 {
993 int i = 0;
994 int fd = 0;
995 ventoy_disk *disk = NULL;
996 const char *diskname = NULL;
997 char path[128];
998
999 if (g_current_progress != PT_FINISH)
1000 {
1001 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
1002 return 0;
1003 }
1004
1005 diskname = vtoy_json_get_string_ex(json, "disk");
1006 if (diskname == NULL)
1007 {
1008 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1009 return 0;
1010 }
1011
1012 for (i = 0; i < g_disk_num; i++)
1013 {
1014 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
1015 {
1016 disk = g_disk_list + i;
1017 break;
1018 }
1019 }
1020
1021 if (disk == NULL)
1022 {
1023 vlog("disk %s not found\n", diskname);
1024 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1025 return 0;
1026 }
1027
1028 scnprintf(path, "/sys/block/%s", diskname);
1029 if (access(path, F_OK) < 0)
1030 {
1031 vlog("File %s not exist anymore\n", path);
1032 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1033 return 0;
1034 }
1035
1036 vlog("==================================\n");
1037 vlog("===== ventoy clean %s =====\n", disk->disk_path);
1038 vlog("==================================\n");
1039
1040 if (ventoy_is_disk_mounted(disk->disk_path))
1041 {
1042 vlog("disk is mounted, now try to unmount it ...\n");
1043 ventoy_try_umount_disk(disk->disk_path);
1044 }
1045
1046 if (ventoy_is_disk_mounted(disk->disk_path))
1047 {
1048 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1049 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1050 return 0;
1051 }
1052 else
1053 {
1054 vlog("disk is not mounted now, we can do the clean ...\n");
1055 }
1056
1057 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1058 if (fd < 0)
1059 {
1060 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1061 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1062 return 0;
1063 }
1064
1065 vdebug("start clean %s ...\n", disk->disk_model);
1066 ventoy_clean_disk(fd, disk->size_in_byte);
1067
1068 vtoy_safe_close_fd(fd);
1069
1070 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1071 return 0;
1072 }
1073
1074 static int ventoy_api_install(struct mg_connection *conn, VTOY_JSON *json)
1075 {
1076 int i = 0;
1077 int ret = 0;
1078 int fd = 0;
1079 uint32_t align4kb = 0;
1080 uint32_t style = 0;
1081 uint32_t secure_boot = 0;
1082 uint64_t reserveBytes = 0;
1083 ventoy_disk *disk = NULL;
1084 const char *diskname = NULL;
1085 const char *reserve_space = NULL;
1086 ventoy_thread_data *thread = NULL;
1087 char path[128];
1088
1089 if (g_current_progress != PT_FINISH)
1090 {
1091 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
1092 return 0;
1093 }
1094
1095 diskname = vtoy_json_get_string_ex(json, "disk");
1096 reserve_space = vtoy_json_get_string_ex(json, "reserve_space");
1097 ret += vtoy_json_get_uint(json, "partstyle", &style);
1098 ret += vtoy_json_get_uint(json, "secure_boot", &secure_boot);
1099 ret += vtoy_json_get_uint(json, "align_4kb", &align4kb);
1100
1101 if (diskname == NULL || reserve_space == NULL || ret != JSON_SUCCESS)
1102 {
1103 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1104 return 0;
1105 }
1106
1107 reserveBytes = (uint64_t)strtoull(reserve_space, NULL, 10);
1108
1109 for (i = 0; i < g_disk_num; i++)
1110 {
1111 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
1112 {
1113 disk = g_disk_list + i;
1114 break;
1115 }
1116 }
1117
1118 if (disk == NULL)
1119 {
1120 vlog("disk %s not found\n", diskname);
1121 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1122 return 0;
1123 }
1124
1125 if (disk->is4kn)
1126 {
1127 vlog("disk %s is 4k native, not supported.\n", diskname);
1128 ventoy_json_result(conn, VTOY_JSON_4KN_RET);
1129 return 0;
1130 }
1131
1132 scnprintf(path, "/sys/block/%s", diskname);
1133 if (access(path, F_OK) < 0)
1134 {
1135 vlog("File %s not exist anymore\n", path);
1136 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1137 return 0;
1138 }
1139
1140 if (disk->size_in_byte > 2199023255552ULL && style == 0)
1141 {
1142 vlog("disk %s is more than 2TB and GPT is needed\n", path);
1143 ventoy_json_result(conn, VTOY_JSON_MBR_2TB_RET);
1144 return 0;
1145 }
1146
1147 if ((reserveBytes + VTOYEFI_PART_BYTES * 2) > disk->size_in_byte)
1148 {
1149 vlog("reserve space %llu is too big for disk %s %llu\n", (_ull)reserveBytes, path, (_ull)disk->size_in_byte);
1150 ventoy_json_result(conn, VTOY_JSON_INVALID_RSV_RET);
1151 return 0;
1152 }
1153
1154 vlog("==================================================================================\n");
1155 vlog("===== ventoy install %s style:%s secureboot:%u align4K:%u reserve:%llu =========\n",
1156 disk->disk_path, (style ? "GPT" : "MBR"), secure_boot, align4kb, (_ull)reserveBytes);
1157 vlog("==================================================================================\n");
1158
1159 if (ventoy_is_disk_mounted(disk->disk_path))
1160 {
1161 vlog("disk is mounted, now try to unmount it ...\n");
1162 ventoy_try_umount_disk(disk->disk_path);
1163 }
1164
1165 if (ventoy_is_disk_mounted(disk->disk_path))
1166 {
1167 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1168 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1169 return 0;
1170 }
1171 else
1172 {
1173 vlog("disk is not mounted now, we can do the install ...\n");
1174 }
1175
1176 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1177 if (fd < 0)
1178 {
1179 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1180 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1181 return 0;
1182 }
1183
1184 vdebug("start install thread %s ...\n", disk->disk_model);
1185 thread = zalloc(sizeof(ventoy_thread_data));
1186 if (!thread)
1187 {
1188 vtoy_safe_close_fd(fd);
1189 vlog("failed to alloc thread data err:%d\n", errno);
1190 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1191 return 0;
1192 }
1193
1194 g_current_progress = PT_START;
1195 g_cur_process_result = 0;
1196 scnprintf(g_cur_process_type, "%s", "install");
1197 scnprintf(g_cur_process_diskname, "%s", disk->disk_name);
1198
1199 thread->disk = disk;
1200 thread->diskfd = fd;
1201 thread->align4kb = align4kb;
1202 thread->partstyle = style;
1203 thread->secure_boot = secure_boot;
1204 thread->reserveBytes = reserveBytes;
1205
1206 mg_start_thread(ventoy_install_thread, thread);
1207
1208 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1209 return 0;
1210 }
1211
1212 static int ventoy_api_update(struct mg_connection *conn, VTOY_JSON *json)
1213 {
1214 int i = 0;
1215 int ret = 0;
1216 int fd = 0;
1217 uint32_t secure_boot = 0;
1218 ventoy_disk *disk = NULL;
1219 const char *diskname = NULL;
1220 ventoy_thread_data *thread = NULL;
1221 char path[128];
1222
1223 if (g_current_progress != PT_FINISH)
1224 {
1225 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
1226 return 0;
1227 }
1228
1229 diskname = vtoy_json_get_string_ex(json, "disk");
1230 ret += vtoy_json_get_uint(json, "secure_boot", &secure_boot);
1231 if (diskname == NULL || ret != JSON_SUCCESS)
1232 {
1233 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1234 return 0;
1235 }
1236
1237 for (i = 0; i < g_disk_num; i++)
1238 {
1239 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
1240 {
1241 disk = g_disk_list + i;
1242 break;
1243 }
1244 }
1245
1246 if (disk == NULL)
1247 {
1248 vlog("disk %s not found\n", diskname);
1249 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1250 return 0;
1251 }
1252
1253 if (disk->vtoydata.ventoy_valid == 0)
1254 {
1255 vlog("disk %s is not ventoy disk\n", diskname);
1256 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1257 return 0;
1258 }
1259
1260 scnprintf(path, "/sys/block/%s", diskname);
1261 if (access(path, F_OK) < 0)
1262 {
1263 vlog("File %s not exist anymore\n", path);
1264 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1265 return 0;
1266 }
1267
1268 vlog("==========================================================\n");
1269 vlog("===== ventoy update %s new_secureboot:%u =========\n", disk->disk_path, secure_boot);
1270 vlog("==========================================================\n");
1271
1272 vlog("%s version:%s partstyle:%u oldsecureboot:%u reserve:%llu\n",
1273 disk->disk_path, disk->vtoydata.ventoy_ver,
1274 disk->vtoydata.partition_style,
1275 disk->vtoydata.secure_boot_flag,
1276 (_ull)(disk->vtoydata.preserved_space)
1277 );
1278
1279 if (ventoy_is_disk_mounted(disk->disk_path))
1280 {
1281 vlog("disk is mounted, now try to unmount it ...\n");
1282 ventoy_try_umount_disk(disk->disk_path);
1283 }
1284
1285 if (ventoy_is_disk_mounted(disk->disk_path))
1286 {
1287 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1288 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1289 return 0;
1290 }
1291 else
1292 {
1293 vlog("disk is not mounted now, we can do the update ...\n");
1294 }
1295
1296 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1297 if (fd < 0)
1298 {
1299 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1300 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1301 return 0;
1302 }
1303
1304 vdebug("start update thread %s ...\n", disk->disk_model);
1305 thread = zalloc(sizeof(ventoy_thread_data));
1306 if (!thread)
1307 {
1308 vtoy_safe_close_fd(fd);
1309 vlog("failed to alloc thread data err:%d\n", errno);
1310 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1311 return 0;
1312 }
1313
1314 g_current_progress = PT_START;
1315 g_cur_process_result = 0;
1316 scnprintf(g_cur_process_type, "%s", "update");
1317 scnprintf(g_cur_process_diskname, "%s", disk->disk_name);
1318
1319 thread->disk = disk;
1320 thread->diskfd = fd;
1321 thread->secure_boot = secure_boot;
1322
1323 mg_start_thread(ventoy_update_thread, thread);
1324
1325 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1326 return 0;
1327 }
1328
1329
1330 static int ventoy_api_refresh_device(struct mg_connection *conn, VTOY_JSON *json)
1331 {
1332 (void)json;
1333
1334 if (g_current_progress == PT_FINISH)
1335 {
1336 g_disk_num = 0;
1337 ventoy_disk_enumerate_all();
1338 }
1339
1340 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1341 return 0;
1342 }
1343
1344 static int ventoy_api_get_dev_list(struct mg_connection *conn, VTOY_JSON *json)
1345 {
1346 int i = 0;
1347 int rc = 0;
1348 int pos = 0;
1349 int buflen = 0;
1350 uint32_t alldev = 0;
1351 char *buf = NULL;
1352 ventoy_disk *cur = NULL;
1353
1354 rc = vtoy_json_get_uint(json, "alldev", &alldev);
1355 if (JSON_SUCCESS != rc)
1356 {
1357 alldev = 0;
1358 }
1359
1360 buflen = g_disk_num * 1024;
1361 buf = (char *)malloc(buflen + 1024);
1362 if (!buf)
1363 {
1364 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1365 return 0;
1366 }
1367
1368 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1369 VTOY_JSON_FMT_OBJ_BEGIN();
1370 VTOY_JSON_FMT_KEY("list");
1371 VTOY_JSON_FMT_ARY_BEGIN();
1372
1373 for (i = 0; i < g_disk_num; i++)
1374 {
1375 cur = g_disk_list + i;
1376
1377 if (alldev == 0 && cur->type != VTOY_DEVICE_USB)
1378 {
1379 continue;
1380 }
1381
1382 VTOY_JSON_FMT_OBJ_BEGIN();
1383 VTOY_JSON_FMT_STRN("name", cur->disk_name);
1384 VTOY_JSON_FMT_STRN("model", cur->disk_model);
1385 VTOY_JSON_FMT_STRN("size", cur->human_readable_size);
1386 VTOY_JSON_FMT_UINT("vtoy_valid", cur->vtoydata.ventoy_valid);
1387 VTOY_JSON_FMT_STRN("vtoy_ver", cur->vtoydata.ventoy_ver);
1388 VTOY_JSON_FMT_UINT("vtoy_secure_boot", cur->vtoydata.secure_boot_flag);
1389 VTOY_JSON_FMT_UINT("vtoy_partstyle", cur->vtoydata.partition_style);
1390 VTOY_JSON_FMT_OBJ_ENDEX();
1391 }
1392
1393 VTOY_JSON_FMT_ARY_END();
1394 VTOY_JSON_FMT_OBJ_END();
1395 VTOY_JSON_FMT_END(pos);
1396
1397 ventoy_json_buffer(conn, buf, pos);
1398 return 0;
1399 }
1400
1401 static JSON_CB g_ventoy_json_cb[] =
1402 {
1403 { "sysinfo", ventoy_api_sysinfo },
1404 { "sel_language", ventoy_api_set_language },
1405 { "sel_partstyle", ventoy_api_set_partstyle },
1406 { "refresh_device", ventoy_api_refresh_device },
1407 { "get_dev_list", ventoy_api_get_dev_list },
1408 { "install", ventoy_api_install },
1409 { "update", ventoy_api_update },
1410 { "clean", ventoy_api_clean },
1411 { "get_percent", ventoy_api_get_percent },
1412 };
1413
1414 static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
1415 {
1416 int i;
1417 const char *token = NULL;
1418 const char *method = NULL;
1419
1420 method = vtoy_json_get_string_ex(json, "method");
1421 if (!method)
1422 {
1423 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1424 return 0;
1425 }
1426
1427 if (strcmp(method, "sysinfo"))
1428 {
1429 token = vtoy_json_get_string_ex(json, "token");
1430 if (token == NULL || strcmp(token, g_cur_server_token))
1431 {
1432 ventoy_json_result(conn, VTOY_JSON_TOKEN_ERR_RET);
1433 return 0;
1434 }
1435 }
1436
1437 for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
1438 {
1439 if (strcmp(method, g_ventoy_json_cb[i].method) == 0)
1440 {
1441 g_ventoy_json_cb[i].callback(conn, json);
1442 break;
1443 }
1444 }
1445
1446 return 0;
1447 }
1448
1449 int ventoy_func_handler(const char *jsonstr, char *jsonbuf, int buflen)
1450 {
1451 int i;
1452 const char *method = NULL;
1453 VTOY_JSON *json = NULL;
1454
1455 g_pub_out_buf = jsonbuf;
1456 g_pub_out_max = buflen;
1457
1458 json = vtoy_json_create();
1459 if (JSON_SUCCESS == vtoy_json_parse(json, jsonstr))
1460 {
1461 pthread_mutex_lock(&g_api_mutex);
1462
1463 method = vtoy_json_get_string_ex(json->pstChild, "method");
1464 for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
1465 {
1466 if (method && strcmp(method, g_ventoy_json_cb[i].method) == 0)
1467 {
1468 g_ventoy_json_cb[i].callback(NULL, json->pstChild);
1469 break;
1470 }
1471 }
1472
1473 pthread_mutex_unlock(&g_api_mutex);
1474 }
1475 else
1476 {
1477 ventoy_json_result(NULL, VTOY_JSON_INVALID_RET);
1478 }
1479
1480 vtoy_json_destroy(json);
1481 return 0;
1482 }
1483
1484 static int ventoy_request_handler(struct mg_connection *conn)
1485 {
1486 int post_data_len;
1487 int post_buf_len;
1488 VTOY_JSON *json = NULL;
1489 char *post_data_buf = NULL;
1490 const struct mg_request_info *ri = NULL;
1491 char stack_buf[512];
1492
1493 ri = mg_get_request_info(conn);
1494
1495 if (strcmp(ri->uri, "/vtoy/json") == 0)
1496 {
1497 if (ri->content_length > 500)
1498 {
1499 post_data_buf = malloc(ri->content_length + 4);
1500 post_buf_len = ri->content_length + 1;
1501 }
1502 else
1503 {
1504 post_data_buf = stack_buf;
1505 post_buf_len = sizeof(stack_buf);
1506 }
1507
1508 post_data_len = mg_read(conn, post_data_buf, post_buf_len);
1509 post_data_buf[post_data_len] = 0;
1510
1511 json = vtoy_json_create();
1512 if (JSON_SUCCESS == vtoy_json_parse(json, post_data_buf))
1513 {
1514 pthread_mutex_lock(&g_api_mutex);
1515 ventoy_json_handler(conn, json->pstChild);
1516 pthread_mutex_unlock(&g_api_mutex);
1517 }
1518 else
1519 {
1520 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1521 }
1522
1523 vtoy_json_destroy(json);
1524
1525 if (post_data_buf != stack_buf)
1526 {
1527 free(post_data_buf);
1528 }
1529 return 1;
1530 }
1531 else
1532 {
1533 return 0;
1534 }
1535 }
1536
1537 int ventoy_http_start(const char *ip, const char *port)
1538 {
1539 uint8_t uuid[16];
1540 char addr[128];
1541 struct mg_callbacks callbacks;
1542 const char *options[] =
1543 {
1544 "listening_ports", "24680",
1545 "document_root", "WebUI",
1546 "error_log_file", g_log_file,
1547 "request_timeout_ms", "10000",
1548 NULL
1549 };
1550
1551 /* unique token */
1552 ventoy_gen_preudo_uuid(uuid);
1553 scnprintf(g_cur_server_token, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1554 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
1555 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
1556
1557 /* option */
1558 scnprintf(addr, "%s:%s", ip, port);
1559 options[1] = addr;
1560
1561 memset(&callbacks, 0, sizeof(callbacks));
1562 callbacks.begin_request = ventoy_request_handler;
1563 g_ventoy_http_ctx = mg_start(&callbacks, NULL, options);
1564
1565 return g_ventoy_http_ctx ? 0 : 1;
1566 }
1567
1568 int ventoy_http_stop(void)
1569 {
1570 if (g_ventoy_http_ctx)
1571 {
1572 mg_stop(g_ventoy_http_ctx);
1573 }
1574 return 0;
1575 }
1576
1577 int ventoy_http_init(void)
1578 {
1579 pthread_mutex_init(&g_api_mutex, NULL);
1580
1581 ventoy_http_load_cfg();
1582
1583 ventoy_load_mbr_template();
1584
1585 return 0;
1586 }
1587
1588 void ventoy_http_exit(void)
1589 {
1590 pthread_mutex_destroy(&g_api_mutex);
1591
1592 check_free(g_efi_part_raw_img);
1593 g_efi_part_raw_img = NULL;
1594 }
1595
1596
1597 const char * ventoy_code_get_cur_language(void)
1598 {
1599 return g_cur_language;
1600 }
1601
1602 int ventoy_code_get_cur_part_style(void)
1603 {
1604 return g_cur_part_style;
1605 }
1606
1607 void ventoy_code_set_cur_part_style(int style)
1608 {
1609 pthread_mutex_lock(&g_api_mutex);
1610
1611 g_cur_part_style = style;
1612 ventoy_http_save_cfg();
1613
1614 pthread_mutex_unlock(&g_api_mutex);
1615 }
1616
1617 int ventoy_code_get_cur_show_all(void)
1618 {
1619 return g_cur_show_all;
1620 }
1621
1622 void ventoy_code_set_cur_show_all(int show_all)
1623 {
1624 pthread_mutex_lock(&g_api_mutex);
1625
1626 g_cur_show_all = show_all;
1627 ventoy_http_save_cfg();
1628
1629 pthread_mutex_unlock(&g_api_mutex);
1630 }
1631
1632 void ventoy_code_set_cur_language(const char *lang)
1633 {
1634 pthread_mutex_lock(&g_api_mutex);
1635
1636 scnprintf(g_cur_language, "%s", lang);
1637 ventoy_http_save_cfg();
1638
1639 pthread_mutex_unlock(&g_api_mutex);
1640 }
1641
1642 void ventoy_code_refresh_device(void)
1643 {
1644 if (g_current_progress == PT_FINISH)
1645 {
1646 g_disk_num = 0;
1647 ventoy_disk_enumerate_all();
1648 }
1649 }
1650
1651 int ventoy_code_is_busy(void)
1652 {
1653 return (g_current_progress == PT_FINISH) ? 0 : 1;
1654 }
1655
1656 int ventoy_code_get_percent(void)
1657 {
1658 return g_current_progress * 100 / PT_FINISH;
1659 }
1660
1661 int ventoy_code_get_result(void)
1662 {
1663 return g_cur_process_result;
1664 }
1665
1666 void ventoy_code_save_cfg(void)
1667 {
1668 ventoy_http_save_cfg();
1669 }