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