]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Web/ventoy_http.c
Fix a bug for Linux GUI program when update from old release. (#1590)
[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("/ENROLL_THIS_KEY_IN_MOKMANAGER.cer");
499
500 file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
501 vlog("Open bootx64 efi file %p \n", file);
502 if (file)
503 {
504 if (filebuf)
505 {
506 fl_fwrite(filebuf, 1, size, file);
507 }
508
509 fl_fflush(file);
510 fl_fclose(file);
511 }
512
513 if (filebuf)
514 {
515 free(filebuf);
516 }
517 }
518
519 file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");
520 vlog("Open ventoy efi file %p\n", file);
521 if (file)
522 {
523 fl_fseek(file, 0, SEEK_END);
524 size = (int)fl_ftell(file);
525 fl_fseek(file, 0, SEEK_SET);
526
527 vlog("ventoy efi file size %d ...\n", size);
528
529 filebuf = (char *)malloc(size);
530 if (filebuf)
531 {
532 fl_fread(filebuf, 1, size, file);
533 }
534
535 fl_fclose(file);
536
537 vlog("Now delete all efi files ...\n");
538 fl_remove("/EFI/BOOT/BOOTIA32.EFI");
539 fl_remove("/EFI/BOOT/grubia32.efi");
540 fl_remove("/EFI/BOOT/grubia32_real.efi");
541 fl_remove("/EFI/BOOT/mmia32.efi");
542
543 file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");
544 vlog("Open bootia32 efi file %p\n", file);
545 if (file)
546 {
547 if (filebuf)
548 {
549 fl_fwrite(filebuf, 1, size, file);
550 }
551
552 fl_fflush(file);
553 fl_fclose(file);
554 }
555
556 if (filebuf)
557 {
558 free(filebuf);
559 }
560 }
561
562 }
563 else
564 {
565 rc = 1;
566 }
567
568 fl_shutdown();
569
570 return rc;
571 }
572
573 static int ventoy_check_efi_part_data(int fd, uint64_t offset)
574 {
575 int i;
576 ssize_t len;
577 char *buf;
578
579 buf = malloc(SIZE_1MB);
580 if (!buf)
581 {
582 return 0;
583 }
584
585 lseek(fd, offset, SEEK_SET);
586 for (i = 0; i < 32; i++)
587 {
588 len = read(fd, buf, SIZE_1MB);
589 if (len != SIZE_1MB || memcmp(buf, g_efi_part_raw_img + i * SIZE_1MB, SIZE_1MB))
590 {
591 vlog("part2 data check failed i=%d len:%llu\n", i, (_ull)len);
592 return 1;
593 }
594
595 g_current_progress = PT_CHECK_PART2 + (i / 4);
596 }
597
598 return 0;
599 }
600
601 static int ventoy_write_efipart(int fd, uint64_t offset, uint32_t secureboot)
602 {
603 int i;
604 ssize_t len;
605
606 vlog("Formatting part2 EFI offset:%llu ...\n", (_ull)offset);
607 lseek(fd, offset, SEEK_SET);
608
609 VentoyProcSecureBoot((int)secureboot);
610
611 g_current_progress = PT_WRITE_VENTOY_START;
612 for (i = 0; i < 32; i++)
613 {
614 len = write(fd, g_efi_part_raw_img + i * SIZE_1MB, SIZE_1MB);
615 vlog("write disk writelen:%lld datalen:%d [ %s ]\n",
616 (_ll)len, SIZE_1MB, (len == SIZE_1MB) ? "success" : "failed");
617
618 if (len != SIZE_1MB)
619 {
620 vlog("failed to format part2 EFI\n");
621 return 1;
622 }
623
624 g_current_progress = PT_WRITE_VENTOY_START + i / 4;
625 }
626
627 return 0;
628 }
629
630 static int VentoyFillBackupGptHead(VTOY_GPT_INFO *pInfo, VTOY_GPT_HDR *pHead)
631 {
632 uint64_t LBA;
633 uint64_t BackupLBA;
634
635 memcpy(pHead, &pInfo->Head, sizeof(VTOY_GPT_HDR));
636
637 LBA = pHead->EfiStartLBA;
638 BackupLBA = pHead->EfiBackupLBA;
639
640 pHead->EfiStartLBA = BackupLBA;
641 pHead->EfiBackupLBA = LBA;
642 pHead->PartTblStartLBA = BackupLBA + 1 - 33;
643
644 pHead->Crc = 0;
645 pHead->Crc = ventoy_crc32(pHead, pHead->Length);
646
647 return 0;
648 }
649
650 static int ventoy_write_gpt_part_table(int fd, uint64_t disksize, VTOY_GPT_INFO *gpt)
651 {
652 ssize_t len;
653 off_t offset;
654 VTOY_GPT_HDR BackupHead;
655
656 VentoyFillBackupGptHead(gpt, &BackupHead);
657
658 offset = lseek(fd, disksize - 512, SEEK_SET);
659 len = write(fd, &BackupHead, sizeof(VTOY_GPT_HDR));
660 vlog("write backup gpt part table off:%llu len:%llu\n", (_ull)offset, (_ull)len);
661 if (offset != disksize - 512 || len != sizeof(VTOY_GPT_HDR))
662 {
663 return 1;
664 }
665
666 offset = lseek(fd, disksize - 512 * 33, SEEK_SET);
667 len = write(fd, gpt->PartTbl, sizeof(gpt->PartTbl));
668 vlog("write main gpt part table off:%llu len:%llu\n", (_ull)offset, (_ull)len);
669 if (offset != disksize - 512 * 33 || len != sizeof(gpt->PartTbl))
670 {
671 return 1;
672 }
673
674 offset = lseek(fd, 0, SEEK_SET);
675 len = write(fd, gpt, sizeof(VTOY_GPT_INFO));
676 vlog("write gpt part head off:%llu len:%llu\n", (_ull)offset, (_ull)len);
677 if (offset != 0 || len != sizeof(VTOY_GPT_INFO))
678 {
679 return 1;
680 }
681
682 return 0;
683 }
684
685 static void * ventoy_update_thread(void *data)
686 {
687 int fd;
688 int updateMBR;
689 ssize_t len;
690 off_t offset;
691 MBR_HEAD MBR;
692 ventoy_disk *disk = NULL;
693 ventoy_thread_data *thread = (ventoy_thread_data *)data;
694
695 vdebug("ventoy_update_thread run ...\n");
696
697 fd = thread->diskfd;
698 disk = thread->disk;
699
700 g_current_progress = PT_PRAPARE_FOR_CLEAN;
701 vdebug("check disk %s\n", disk->disk_name);
702 if (ventoy_is_disk_mounted(disk->disk_path))
703 {
704 vlog("disk is mounted, now try to unmount it ...\n");
705 ventoy_try_umount_disk(disk->disk_path);
706 }
707
708 if (ventoy_is_disk_mounted(disk->disk_path))
709 {
710 vlog("%s is mounted and can't umount!\n", disk->disk_path);
711 goto err;
712 }
713 else
714 {
715 vlog("disk is not mounted now, we can do continue ...\n");
716 }
717
718 g_current_progress = PT_LOAD_CORE_IMG;
719 ventoy_unxz_stg1_img();
720
721 g_current_progress = PT_LOAD_DISK_IMG;
722 ventoy_unxz_efipart_img();
723
724 g_current_progress = PT_FORMAT_PART2;
725
726 vlog("Formatting part2 EFI ...\n");
727 if (0 != ventoy_write_efipart(fd, disk->vtoydata.part2_start_sector * 512, thread->secure_boot))
728 {
729 vlog("Failed to format part2 efi ...\n");
730 goto err;
731 }
732
733 g_current_progress = PT_WRITE_STG1_IMG;
734
735 vlog("Writing legacy grub ...\n");
736 if (0 != ventoy_write_legacy_grub(fd, disk->vtoydata.partition_style))
737 {
738 vlog("ventoy_write_legacy_grub failed ...\n");
739 goto err;
740 }
741
742 offset = lseek(fd, 512 * 2040, SEEK_SET);
743 len = write(fd, disk->vtoydata.rsvdata, sizeof(disk->vtoydata.rsvdata));
744 vlog("Writing reserve data offset:%llu len:%llu ...\n", (_ull)offset, (_ull)len);
745
746 updateMBR = 0;
747 memcpy(&MBR, &(disk->vtoydata.gptinfo.MBR), 512);
748
749 if (disk->vtoydata.partition_style == 0 && MBR.PartTbl[0].Active == 0)
750 {
751 MBR.PartTbl[0].Active = 0x80;
752 MBR.PartTbl[1].Active = 0;
753 MBR.PartTbl[2].Active = 0;
754 MBR.PartTbl[3].Active = 0;
755 updateMBR = 1;
756 vlog("set MBR partition 1 active flag enabled\n");
757 }
758
759 if (MBR.BootCode[0x190] != 0x56 || MBR.BootCode[0x191] != 0x54)
760 {
761 vlog("set VT data %02x %02x\n", MBR.BootCode[0x190], MBR.BootCode[0x191]);
762 MBR.BootCode[0x190] = 0x56;
763 MBR.BootCode[0x191] = 0x54;
764 updateMBR = 1;
765 }
766
767 if (updateMBR)
768 {
769 offset = lseek(fd, 0, SEEK_SET);
770 len = write(fd, &MBR, 512);
771 vlog("update MBR offset:%llu len:%llu\n", (_ull)offset, (_ull)len);
772 }
773
774 g_current_progress = PT_SYNC_DATA1;
775
776 vlog("fsync data1...\n");
777 fsync(fd);
778 vtoy_safe_close_fd(fd);
779
780 g_current_progress = PT_SYNC_DATA2;
781
782 vlog("====================================\n");
783 vlog("====== ventoy update success ======\n");
784 vlog("====================================\n");
785 goto end;
786
787 err:
788 g_cur_process_result = 1;
789 vtoy_safe_close_fd(fd);
790
791 end:
792 g_current_progress = PT_FINISH;
793
794 check_free(thread);
795
796 return NULL;
797 }
798
799 static void * ventoy_install_thread(void *data)
800 {
801 int fd;
802 ssize_t len;
803 off_t offset;
804 MBR_HEAD MBR;
805 ventoy_disk *disk = NULL;
806 VTOY_GPT_INFO *gpt = NULL;
807 ventoy_thread_data *thread = (ventoy_thread_data *)data;
808 uint64_t Part1StartSector = 0;
809 uint64_t Part1SectorCount = 0;
810 uint64_t Part2StartSector = 0;
811
812 vdebug("ventoy_install_thread run ...\n");
813
814 fd = thread->diskfd;
815 disk = thread->disk;
816
817 g_current_progress = PT_PRAPARE_FOR_CLEAN;
818 vdebug("check disk %s\n", disk->disk_name);
819 if (ventoy_is_disk_mounted(disk->disk_path))
820 {
821 vlog("disk is mounted, now try to unmount it ...\n");
822 ventoy_try_umount_disk(disk->disk_path);
823 }
824
825 if (ventoy_is_disk_mounted(disk->disk_path))
826 {
827 vlog("%s is mounted and can't umount!\n", disk->disk_path);
828 goto err;
829 }
830 else
831 {
832 vlog("disk is not mounted now, we can do continue ...\n");
833 }
834
835 g_current_progress = PT_DEL_ALL_PART;
836 ventoy_clean_disk(fd, disk->size_in_byte);
837
838 g_current_progress = PT_LOAD_CORE_IMG;
839 ventoy_unxz_stg1_img();
840
841 g_current_progress = PT_LOAD_DISK_IMG;
842 ventoy_unxz_efipart_img();
843
844 if (thread->partstyle)
845 {
846 vdebug("Fill GPT part table\n");
847 gpt = zalloc(sizeof(VTOY_GPT_INFO));
848 ventoy_fill_gpt(disk->size_in_byte, thread->reserveBytes, thread->align4kb, gpt);
849 Part1StartSector = gpt->PartTbl[0].StartLBA;
850 Part1SectorCount = gpt->PartTbl[0].LastLBA - Part1StartSector + 1;
851 Part2StartSector = gpt->PartTbl[1].StartLBA;
852 }
853 else
854 {
855 vdebug("Fill MBR part table\n");
856 ventoy_fill_mbr(disk->size_in_byte, thread->reserveBytes, thread->align4kb, &MBR);
857 Part1StartSector = MBR.PartTbl[0].StartSectorId;
858 Part1SectorCount = MBR.PartTbl[0].SectorCount;
859 Part2StartSector = MBR.PartTbl[1].StartSectorId;
860 }
861
862 vlog("Part1StartSector:%llu Part1SectorCount:%llu Part2StartSector:%llu\n",
863 (_ull)Part1StartSector, (_ull)Part1SectorCount, (_ull)Part2StartSector);
864
865 if (thread->partstyle != disk->partstyle)
866 {
867 vlog("Wait for format part1 (partstyle changed) ...\n");
868 sleep(1);
869 }
870
871 g_current_progress = PT_FORMAT_PART1;
872 vlog("Formatting part1 exFAT %s ...\n", disk->disk_path);
873 if (0 != mkexfat_main(disk->disk_path, fd, Part1SectorCount))
874 {
875 vlog("Failed to format exfat ...\n");
876 goto err;
877 }
878
879 g_current_progress = PT_FORMAT_PART2;
880 vlog("Formatting part2 EFI ...\n");
881 if (0 != ventoy_write_efipart(fd, Part2StartSector * 512, thread->secure_boot))
882 {
883 vlog("Failed to format part2 efi ...\n");
884 goto err;
885 }
886
887 g_current_progress = PT_WRITE_STG1_IMG;
888 vlog("Writing legacy grub ...\n");
889 if (0 != ventoy_write_legacy_grub(fd, thread->partstyle))
890 {
891 vlog("ventoy_write_legacy_grub failed ...\n");
892 goto err;
893 }
894
895 g_current_progress = PT_SYNC_DATA1;
896 vlog("fsync data1...\n");
897 fsync(fd);
898 vtoy_safe_close_fd(fd);
899
900 /* reopen for check part2 data */
901 vlog("Checking part2 efi data %s ...\n", disk->disk_path);
902 g_current_progress = PT_CHECK_PART2;
903 fd = open(disk->disk_path, O_RDONLY | O_BINARY);
904 if (fd < 0)
905 {
906 vlog("failed to open %s for check fd:%d err:%d\n", disk->disk_path, fd, errno);
907 goto err;
908 }
909
910 if (0 == ventoy_check_efi_part_data(fd, Part2StartSector * 512))
911 {
912 vlog("efi part data check success\n");
913 }
914 else
915 {
916 vlog("efi part data check failed\n");
917 goto err;
918 }
919
920 vtoy_safe_close_fd(fd);
921
922 /* reopen for write part table */
923 g_current_progress = PT_WRITE_PART_TABLE;
924 vlog("Writting Partition Table style:%d...\n", thread->partstyle);
925
926 fd = open(disk->disk_path, O_RDWR | O_BINARY);
927 if (fd < 0)
928 {
929 vlog("failed to open %s for part table fd:%d err:%d\n", disk->disk_path, fd, errno);
930 goto err;
931 }
932
933 if (thread->partstyle)
934 {
935 ventoy_write_gpt_part_table(fd, disk->size_in_byte, gpt);
936 }
937 else
938 {
939 offset = lseek(fd, 0, SEEK_SET);
940 len = write(fd, &MBR, 512);
941 vlog("Writting MBR Partition Table %llu %llu\n", (_ull)offset, (_ull)len);
942 if (offset != 0 || len != 512)
943 {
944 goto err;
945 }
946 }
947
948 g_current_progress = PT_SYNC_DATA2;
949 vlog("fsync data2...\n");
950 fsync(fd);
951 vtoy_safe_close_fd(fd);
952
953
954 vlog("====================================\n");
955 vlog("====== ventoy install success ======\n");
956 vlog("====================================\n");
957 goto end;
958
959 err:
960 g_cur_process_result = 1;
961 vtoy_safe_close_fd(fd);
962
963 end:
964 g_current_progress = PT_FINISH;
965
966 check_free(gpt);
967 check_free(thread);
968
969 return NULL;
970 }
971
972 static int ventoy_api_clean(struct mg_connection *conn, VTOY_JSON *json)
973 {
974 int i = 0;
975 int fd = 0;
976 ventoy_disk *disk = NULL;
977 const char *diskname = NULL;
978 char path[128];
979
980 if (g_current_progress != PT_FINISH)
981 {
982 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
983 return 0;
984 }
985
986 diskname = vtoy_json_get_string_ex(json, "disk");
987 if (diskname == NULL)
988 {
989 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
990 return 0;
991 }
992
993 for (i = 0; i < g_disk_num; i++)
994 {
995 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
996 {
997 disk = g_disk_list + i;
998 break;
999 }
1000 }
1001
1002 if (disk == NULL)
1003 {
1004 vlog("disk %s not found\n", diskname);
1005 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1006 return 0;
1007 }
1008
1009 scnprintf(path, "/sys/block/%s", diskname);
1010 if (access(path, F_OK) < 0)
1011 {
1012 vlog("File %s not exist anymore\n", path);
1013 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1014 return 0;
1015 }
1016
1017 vlog("==================================\n");
1018 vlog("===== ventoy clean %s =====\n", disk->disk_path);
1019 vlog("==================================\n");
1020
1021 if (ventoy_is_disk_mounted(disk->disk_path))
1022 {
1023 vlog("disk is mounted, now try to unmount it ...\n");
1024 ventoy_try_umount_disk(disk->disk_path);
1025 }
1026
1027 if (ventoy_is_disk_mounted(disk->disk_path))
1028 {
1029 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1030 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1031 return 0;
1032 }
1033 else
1034 {
1035 vlog("disk is not mounted now, we can do the clean ...\n");
1036 }
1037
1038 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1039 if (fd < 0)
1040 {
1041 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1042 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1043 return 0;
1044 }
1045
1046 vdebug("start clean %s ...\n", disk->disk_model);
1047 ventoy_clean_disk(fd, disk->size_in_byte);
1048
1049 vtoy_safe_close_fd(fd);
1050
1051 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1052 return 0;
1053 }
1054
1055 static int ventoy_api_install(struct mg_connection *conn, VTOY_JSON *json)
1056 {
1057 int i = 0;
1058 int ret = 0;
1059 int fd = 0;
1060 uint32_t align4kb = 0;
1061 uint32_t style = 0;
1062 uint32_t secure_boot = 0;
1063 uint64_t reserveBytes = 0;
1064 ventoy_disk *disk = NULL;
1065 const char *diskname = NULL;
1066 const char *reserve_space = NULL;
1067 ventoy_thread_data *thread = NULL;
1068 char path[128];
1069
1070 if (g_current_progress != PT_FINISH)
1071 {
1072 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
1073 return 0;
1074 }
1075
1076 diskname = vtoy_json_get_string_ex(json, "disk");
1077 reserve_space = vtoy_json_get_string_ex(json, "reserve_space");
1078 ret += vtoy_json_get_uint(json, "partstyle", &style);
1079 ret += vtoy_json_get_uint(json, "secure_boot", &secure_boot);
1080 ret += vtoy_json_get_uint(json, "align_4kb", &align4kb);
1081
1082 if (diskname == NULL || reserve_space == NULL || ret != JSON_SUCCESS)
1083 {
1084 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1085 return 0;
1086 }
1087
1088 reserveBytes = (uint64_t)strtoull(reserve_space, NULL, 10);
1089
1090 for (i = 0; i < g_disk_num; i++)
1091 {
1092 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
1093 {
1094 disk = g_disk_list + i;
1095 break;
1096 }
1097 }
1098
1099 if (disk == NULL)
1100 {
1101 vlog("disk %s not found\n", diskname);
1102 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1103 return 0;
1104 }
1105
1106 scnprintf(path, "/sys/block/%s", diskname);
1107 if (access(path, F_OK) < 0)
1108 {
1109 vlog("File %s not exist anymore\n", path);
1110 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1111 return 0;
1112 }
1113
1114 if (disk->size_in_byte > 2199023255552ULL && style == 0)
1115 {
1116 vlog("disk %s is more than 2TB and GPT is needed\n", path);
1117 ventoy_json_result(conn, VTOY_JSON_MBR_2TB_RET);
1118 return 0;
1119 }
1120
1121 if ((reserveBytes + VTOYEFI_PART_BYTES * 2) > disk->size_in_byte)
1122 {
1123 vlog("reserve space %llu is too big for disk %s %llu\n", (_ull)reserveBytes, path, (_ull)disk->size_in_byte);
1124 ventoy_json_result(conn, VTOY_JSON_INVALID_RSV_RET);
1125 return 0;
1126 }
1127
1128 vlog("==================================================================================\n");
1129 vlog("===== ventoy install %s style:%s secureboot:%u align4K:%u reserve:%llu =========\n",
1130 disk->disk_path, (style ? "GPT" : "MBR"), secure_boot, align4kb, (_ull)reserveBytes);
1131 vlog("==================================================================================\n");
1132
1133 if (ventoy_is_disk_mounted(disk->disk_path))
1134 {
1135 vlog("disk is mounted, now try to unmount it ...\n");
1136 ventoy_try_umount_disk(disk->disk_path);
1137 }
1138
1139 if (ventoy_is_disk_mounted(disk->disk_path))
1140 {
1141 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1142 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1143 return 0;
1144 }
1145 else
1146 {
1147 vlog("disk is not mounted now, we can do the install ...\n");
1148 }
1149
1150 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1151 if (fd < 0)
1152 {
1153 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1154 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1155 return 0;
1156 }
1157
1158 vdebug("start install thread %s ...\n", disk->disk_model);
1159 thread = zalloc(sizeof(ventoy_thread_data));
1160 if (!thread)
1161 {
1162 vtoy_safe_close_fd(fd);
1163 vlog("failed to alloc thread data err:%d\n", errno);
1164 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1165 return 0;
1166 }
1167
1168 g_current_progress = PT_START;
1169 g_cur_process_result = 0;
1170 scnprintf(g_cur_process_type, "%s", "install");
1171 scnprintf(g_cur_process_diskname, "%s", disk->disk_name);
1172
1173 thread->disk = disk;
1174 thread->diskfd = fd;
1175 thread->align4kb = align4kb;
1176 thread->partstyle = style;
1177 thread->secure_boot = secure_boot;
1178 thread->reserveBytes = reserveBytes;
1179
1180 mg_start_thread(ventoy_install_thread, thread);
1181
1182 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1183 return 0;
1184 }
1185
1186 static int ventoy_api_update(struct mg_connection *conn, VTOY_JSON *json)
1187 {
1188 int i = 0;
1189 int ret = 0;
1190 int fd = 0;
1191 uint32_t secure_boot = 0;
1192 ventoy_disk *disk = NULL;
1193 const char *diskname = NULL;
1194 ventoy_thread_data *thread = NULL;
1195 char path[128];
1196
1197 if (g_current_progress != PT_FINISH)
1198 {
1199 ventoy_json_result(conn, VTOY_JSON_BUSY_RET);
1200 return 0;
1201 }
1202
1203 diskname = vtoy_json_get_string_ex(json, "disk");
1204 ret += vtoy_json_get_uint(json, "secure_boot", &secure_boot);
1205 if (diskname == NULL || ret != JSON_SUCCESS)
1206 {
1207 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1208 return 0;
1209 }
1210
1211 for (i = 0; i < g_disk_num; i++)
1212 {
1213 if (strcmp(g_disk_list[i].disk_name, diskname) == 0)
1214 {
1215 disk = g_disk_list + i;
1216 break;
1217 }
1218 }
1219
1220 if (disk == NULL)
1221 {
1222 vlog("disk %s not found\n", diskname);
1223 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1224 return 0;
1225 }
1226
1227 if (disk->vtoydata.ventoy_valid == 0)
1228 {
1229 vlog("disk %s is not ventoy disk\n", diskname);
1230 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1231 return 0;
1232 }
1233
1234 scnprintf(path, "/sys/block/%s", diskname);
1235 if (access(path, F_OK) < 0)
1236 {
1237 vlog("File %s not exist anymore\n", path);
1238 ventoy_json_result(conn, VTOY_JSON_NOTFOUND_RET);
1239 return 0;
1240 }
1241
1242 vlog("==========================================================\n");
1243 vlog("===== ventoy update %s new_secureboot:%u =========\n", disk->disk_path, secure_boot);
1244 vlog("==========================================================\n");
1245
1246 vlog("%s version:%s partstyle:%u oldsecureboot:%u reserve:%llu\n",
1247 disk->disk_path, disk->vtoydata.ventoy_ver,
1248 disk->vtoydata.partition_style,
1249 disk->vtoydata.secure_boot_flag,
1250 (_ull)(disk->vtoydata.preserved_space)
1251 );
1252
1253 if (ventoy_is_disk_mounted(disk->disk_path))
1254 {
1255 vlog("disk is mounted, now try to unmount it ...\n");
1256 ventoy_try_umount_disk(disk->disk_path);
1257 }
1258
1259 if (ventoy_is_disk_mounted(disk->disk_path))
1260 {
1261 vlog("%s is mounted and can't umount!\n", disk->disk_path);
1262 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1263 return 0;
1264 }
1265 else
1266 {
1267 vlog("disk is not mounted now, we can do the update ...\n");
1268 }
1269
1270 fd = open(disk->disk_path, O_RDWR | O_BINARY);
1271 if (fd < 0)
1272 {
1273 vlog("failed to open %s fd:%d err:%d\n", disk->disk_path, fd, errno);
1274 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1275 return 0;
1276 }
1277
1278 vdebug("start update thread %s ...\n", disk->disk_model);
1279 thread = zalloc(sizeof(ventoy_thread_data));
1280 if (!thread)
1281 {
1282 vtoy_safe_close_fd(fd);
1283 vlog("failed to alloc thread data err:%d\n", errno);
1284 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1285 return 0;
1286 }
1287
1288 g_current_progress = PT_START;
1289 g_cur_process_result = 0;
1290 scnprintf(g_cur_process_type, "%s", "update");
1291 scnprintf(g_cur_process_diskname, "%s", disk->disk_name);
1292
1293 thread->disk = disk;
1294 thread->diskfd = fd;
1295 thread->secure_boot = secure_boot;
1296
1297 mg_start_thread(ventoy_update_thread, thread);
1298
1299 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1300 return 0;
1301 }
1302
1303
1304 static int ventoy_api_refresh_device(struct mg_connection *conn, VTOY_JSON *json)
1305 {
1306 (void)json;
1307
1308 if (g_current_progress == PT_FINISH)
1309 {
1310 g_disk_num = 0;
1311 ventoy_disk_enumerate_all();
1312 }
1313
1314 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1315 return 0;
1316 }
1317
1318 static int ventoy_api_get_dev_list(struct mg_connection *conn, VTOY_JSON *json)
1319 {
1320 int i = 0;
1321 int rc = 0;
1322 int pos = 0;
1323 int buflen = 0;
1324 uint32_t alldev = 0;
1325 char *buf = NULL;
1326 ventoy_disk *cur = NULL;
1327
1328 rc = vtoy_json_get_uint(json, "alldev", &alldev);
1329 if (JSON_SUCCESS != rc)
1330 {
1331 alldev = 0;
1332 }
1333
1334 buflen = g_disk_num * 1024;
1335 buf = (char *)malloc(buflen + 1024);
1336 if (!buf)
1337 {
1338 ventoy_json_result(conn, VTOY_JSON_FAILED_RET);
1339 return 0;
1340 }
1341
1342 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1343 VTOY_JSON_FMT_OBJ_BEGIN();
1344 VTOY_JSON_FMT_KEY("list");
1345 VTOY_JSON_FMT_ARY_BEGIN();
1346
1347 for (i = 0; i < g_disk_num; i++)
1348 {
1349 cur = g_disk_list + i;
1350
1351 if (alldev == 0 && cur->type != VTOY_DEVICE_USB)
1352 {
1353 continue;
1354 }
1355
1356 VTOY_JSON_FMT_OBJ_BEGIN();
1357 VTOY_JSON_FMT_STRN("name", cur->disk_name);
1358 VTOY_JSON_FMT_STRN("model", cur->disk_model);
1359 VTOY_JSON_FMT_STRN("size", cur->human_readable_size);
1360 VTOY_JSON_FMT_UINT("vtoy_valid", cur->vtoydata.ventoy_valid);
1361 VTOY_JSON_FMT_STRN("vtoy_ver", cur->vtoydata.ventoy_ver);
1362 VTOY_JSON_FMT_UINT("vtoy_secure_boot", cur->vtoydata.secure_boot_flag);
1363 VTOY_JSON_FMT_UINT("vtoy_partstyle", cur->vtoydata.partition_style);
1364 VTOY_JSON_FMT_OBJ_ENDEX();
1365 }
1366
1367 VTOY_JSON_FMT_ARY_END();
1368 VTOY_JSON_FMT_OBJ_END();
1369 VTOY_JSON_FMT_END(pos);
1370
1371 ventoy_json_buffer(conn, buf, pos);
1372 return 0;
1373 }
1374
1375 static JSON_CB g_ventoy_json_cb[] =
1376 {
1377 { "sysinfo", ventoy_api_sysinfo },
1378 { "sel_language", ventoy_api_set_language },
1379 { "sel_partstyle", ventoy_api_set_partstyle },
1380 { "refresh_device", ventoy_api_refresh_device },
1381 { "get_dev_list", ventoy_api_get_dev_list },
1382 { "install", ventoy_api_install },
1383 { "update", ventoy_api_update },
1384 { "clean", ventoy_api_clean },
1385 { "get_percent", ventoy_api_get_percent },
1386 };
1387
1388 static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
1389 {
1390 int i;
1391 const char *token = NULL;
1392 const char *method = NULL;
1393
1394 method = vtoy_json_get_string_ex(json, "method");
1395 if (!method)
1396 {
1397 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
1398 return 0;
1399 }
1400
1401 if (strcmp(method, "sysinfo"))
1402 {
1403 token = vtoy_json_get_string_ex(json, "token");
1404 if (token == NULL || strcmp(token, g_cur_server_token))
1405 {
1406 ventoy_json_result(conn, VTOY_JSON_TOKEN_ERR_RET);
1407 return 0;
1408 }
1409 }
1410
1411 for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
1412 {
1413 if (strcmp(method, g_ventoy_json_cb[i].method) == 0)
1414 {
1415 g_ventoy_json_cb[i].callback(conn, json);
1416 break;
1417 }
1418 }
1419
1420 return 0;
1421 }
1422
1423 int ventoy_func_handler(const char *jsonstr, char *jsonbuf, int buflen)
1424 {
1425 int i;
1426 const char *method = NULL;
1427 VTOY_JSON *json = NULL;
1428
1429 g_pub_out_buf = jsonbuf;
1430 g_pub_out_max = buflen;
1431
1432 json = vtoy_json_create();
1433 if (JSON_SUCCESS == vtoy_json_parse(json, jsonstr))
1434 {
1435 pthread_mutex_lock(&g_api_mutex);
1436
1437 method = vtoy_json_get_string_ex(json->pstChild, "method");
1438 for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
1439 {
1440 if (method && strcmp(method, g_ventoy_json_cb[i].method) == 0)
1441 {
1442 g_ventoy_json_cb[i].callback(NULL, json->pstChild);
1443 break;
1444 }
1445 }
1446
1447 pthread_mutex_unlock(&g_api_mutex);
1448 }
1449 else
1450 {
1451 ventoy_json_result(NULL, VTOY_JSON_INVALID_RET);
1452 }
1453
1454 vtoy_json_destroy(json);
1455 return 0;
1456 }
1457
1458 static int ventoy_request_handler(struct mg_connection *conn)
1459 {
1460 int post_data_len;
1461 int post_buf_len;
1462 VTOY_JSON *json = NULL;
1463 char *post_data_buf = NULL;
1464 const struct mg_request_info *ri = NULL;
1465 char stack_buf[512];
1466
1467 ri = mg_get_request_info(conn);
1468
1469 if (strcmp(ri->uri, "/vtoy/json") == 0)
1470 {
1471 if (ri->content_length > 500)
1472 {
1473 post_data_buf = malloc(ri->content_length + 4);
1474 post_buf_len = ri->content_length + 1;
1475 }
1476 else
1477 {
1478 post_data_buf = stack_buf;
1479 post_buf_len = sizeof(stack_buf);
1480 }
1481
1482 post_data_len = mg_read(conn, post_data_buf, post_buf_len);
1483 post_data_buf[post_data_len] = 0;
1484
1485 json = vtoy_json_create();
1486 if (JSON_SUCCESS == vtoy_json_parse(json, post_data_buf))
1487 {
1488 pthread_mutex_lock(&g_api_mutex);
1489 ventoy_json_handler(conn, json->pstChild);
1490 pthread_mutex_unlock(&g_api_mutex);
1491 }
1492 else
1493 {
1494 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
1495 }
1496
1497 vtoy_json_destroy(json);
1498
1499 if (post_data_buf != stack_buf)
1500 {
1501 free(post_data_buf);
1502 }
1503 return 1;
1504 }
1505 else
1506 {
1507 return 0;
1508 }
1509 }
1510
1511 int ventoy_http_start(const char *ip, const char *port)
1512 {
1513 uint8_t uuid[16];
1514 char addr[128];
1515 struct mg_callbacks callbacks;
1516 const char *options[] =
1517 {
1518 "listening_ports", "24680",
1519 "document_root", "WebUI",
1520 "error_log_file", g_log_file,
1521 "request_timeout_ms", "10000",
1522 NULL
1523 };
1524
1525 /* unique token */
1526 ventoy_gen_preudo_uuid(uuid);
1527 scnprintf(g_cur_server_token, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1528 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
1529 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
1530
1531 /* option */
1532 scnprintf(addr, "%s:%s", ip, port);
1533 options[1] = addr;
1534
1535 memset(&callbacks, 0, sizeof(callbacks));
1536 callbacks.begin_request = ventoy_request_handler;
1537 g_ventoy_http_ctx = mg_start(&callbacks, NULL, options);
1538
1539 return g_ventoy_http_ctx ? 0 : 1;
1540 }
1541
1542 int ventoy_http_stop(void)
1543 {
1544 if (g_ventoy_http_ctx)
1545 {
1546 mg_stop(g_ventoy_http_ctx);
1547 }
1548 return 0;
1549 }
1550
1551 int ventoy_http_init(void)
1552 {
1553 pthread_mutex_init(&g_api_mutex, NULL);
1554
1555 ventoy_http_load_cfg();
1556
1557 ventoy_load_mbr_template();
1558
1559 return 0;
1560 }
1561
1562 void ventoy_http_exit(void)
1563 {
1564 pthread_mutex_destroy(&g_api_mutex);
1565
1566 check_free(g_efi_part_raw_img);
1567 g_efi_part_raw_img = NULL;
1568 }
1569
1570
1571 const char * ventoy_code_get_cur_language(void)
1572 {
1573 return g_cur_language;
1574 }
1575
1576 int ventoy_code_get_cur_part_style(void)
1577 {
1578 return g_cur_part_style;
1579 }
1580
1581 void ventoy_code_set_cur_part_style(int style)
1582 {
1583 pthread_mutex_lock(&g_api_mutex);
1584
1585 g_cur_part_style = style;
1586 ventoy_http_save_cfg();
1587
1588 pthread_mutex_unlock(&g_api_mutex);
1589 }
1590
1591 int ventoy_code_get_cur_show_all(void)
1592 {
1593 return g_cur_show_all;
1594 }
1595
1596 void ventoy_code_set_cur_show_all(int show_all)
1597 {
1598 pthread_mutex_lock(&g_api_mutex);
1599
1600 g_cur_show_all = show_all;
1601 ventoy_http_save_cfg();
1602
1603 pthread_mutex_unlock(&g_api_mutex);
1604 }
1605
1606 void ventoy_code_set_cur_language(const char *lang)
1607 {
1608 pthread_mutex_lock(&g_api_mutex);
1609
1610 scnprintf(g_cur_language, "%s", lang);
1611 ventoy_http_save_cfg();
1612
1613 pthread_mutex_unlock(&g_api_mutex);
1614 }
1615
1616 void ventoy_code_refresh_device(void)
1617 {
1618 if (g_current_progress == PT_FINISH)
1619 {
1620 g_disk_num = 0;
1621 ventoy_disk_enumerate_all();
1622 }
1623 }
1624
1625 int ventoy_code_is_busy(void)
1626 {
1627 return (g_current_progress == PT_FINISH) ? 0 : 1;
1628 }
1629
1630 int ventoy_code_get_percent(void)
1631 {
1632 return g_current_progress * 100 / PT_FINISH;
1633 }
1634
1635 int ventoy_code_get_result(void)
1636 {
1637 return g_cur_process_result;
1638 }
1639
1640 void ventoy_code_save_cfg(void)
1641 {
1642 ventoy_http_save_cfg();
1643 }