]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoydump.c
Update German translation (#2612)
[Ventoy.git] / VtoyTool / vtoydump.c
1 /******************************************************************************
2 * vtoydump.c ---- Dump ventoy os parameters
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <linux/fs.h>
34 #include <dirent.h>
35 #include "vtoytool.h"
36
37 #ifndef O_BINARY
38 #define O_BINARY 0
39 #endif
40 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
41 #define MMAP_FLAGS MAP_SHARED
42 #else
43 #define MMAP_FLAGS MAP_PRIVATE
44 #endif
45
46 #define SEARCH_MEM_START 0x80000
47 #define SEARCH_MEM_LEN 0x1c000
48
49 static int verbose = 0;
50 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
51
52 static ventoy_guid vtoy_guid = VENTOY_GUID;
53
54 static const char *g_ventoy_fs[ventoy_fs_max] =
55 {
56 "exfat", "ntfs", "ext*", "xfs", "udf", "fat"
57 };
58
59 static int vtoy_check_os_param(ventoy_os_param *param)
60 {
61 uint32_t i;
62 uint8_t chksum = 0;
63 uint8_t *buf = (uint8_t *)param;
64
65 if (memcmp(&param->guid, &vtoy_guid, sizeof(ventoy_guid)))
66 {
67 uint8_t *data1 = (uint8_t *)(&param->guid);
68 uint8_t *data2 = (uint8_t *)(&vtoy_guid);
69
70 for (i = 0; i < 16; i++)
71 {
72 if (data1[i] != data2[i])
73 {
74 debug("guid not equal i = %u, 0x%02x, 0x%02x\n", i, data1[i], data2[i]);
75 }
76 }
77 return 1;
78 }
79
80 for (i = 0; i < sizeof(ventoy_os_param); i++)
81 {
82 chksum += buf[i];
83 }
84
85 if (chksum)
86 {
87 debug("Invalid checksum 0x%02x\n", chksum);
88 return 1;
89 }
90
91 return 0;
92 }
93
94 static int vtoy_os_param_from_file(const char *filename, ventoy_os_param *param)
95 {
96 int fd = 0;
97 int rc = 0;
98
99 fd = open(filename, O_RDONLY | O_BINARY);
100 if (fd < 0)
101 {
102 fprintf(stderr, "Failed to open file %s error %d\n", filename, errno);
103 return errno;
104 }
105
106 read(fd, param, sizeof(ventoy_os_param));
107
108 if (vtoy_check_os_param(param) == 0)
109 {
110 debug("find ventoy os param in file %s\n", filename);
111 }
112 else
113 {
114 debug("ventoy os pararm NOT found in file %s\n", filename);
115 rc = 1;
116 }
117
118 close(fd);
119 return rc;
120 }
121
122 static void vtoy_dump_os_param(ventoy_os_param *param)
123 {
124 printf("################# dump os param ################\n");
125
126 printf("param->chksum = 0x%x\n", param->chksum);
127 printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
128 param->vtoy_disk_guid[0], param->vtoy_disk_guid[1],
129 param->vtoy_disk_guid[2], param->vtoy_disk_guid[3]);
130
131 printf("param->vtoy_disk_signature = %02x %02x %02x %02x\n",
132 param->vtoy_disk_signature[0], param->vtoy_disk_signature[1],
133 param->vtoy_disk_signature[2], param->vtoy_disk_signature[3]);
134
135 printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param->vtoy_disk_size);
136 printf("param->vtoy_disk_part_id = %u\n", param->vtoy_disk_part_id);
137 printf("param->vtoy_disk_part_type = %u\n", param->vtoy_disk_part_type);
138 printf("param->vtoy_img_path = <%s>\n", param->vtoy_img_path);
139 printf("param->vtoy_img_size = <%llu>\n", (unsigned long long)param->vtoy_img_size);
140 printf("param->vtoy_img_location_addr = <0x%llx>\n", (unsigned long long)param->vtoy_img_location_addr);
141 printf("param->vtoy_img_location_len = <%u>\n", param->vtoy_img_location_len);
142 printf("param->vtoy_reserved = %02x %02x %02x %02x %02x %02x %02x %02x\n",
143 param->vtoy_reserved[0],
144 param->vtoy_reserved[1],
145 param->vtoy_reserved[2],
146 param->vtoy_reserved[3],
147 param->vtoy_reserved[4],
148 param->vtoy_reserved[5],
149 param->vtoy_reserved[6],
150 param->vtoy_reserved[7]
151 );
152
153 printf("\n");
154 }
155
156 static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid, uint8_t *vtsig)
157 {
158 int i = 0;
159 int fd = 0;
160 char devdisk[128] = {0};
161
162 snprintf(devdisk, sizeof(devdisk) - 1, "/dev/%s", diskname);
163
164 fd = open(devdisk, O_RDONLY | O_BINARY);
165 if (fd >= 0)
166 {
167 lseek(fd, 0x180, SEEK_SET);
168 read(fd, vtguid, 16);
169
170 lseek(fd, 0x1b8, SEEK_SET);
171 read(fd, vtsig, 4);
172 close(fd);
173
174 debug("GUID for %s: <", devdisk);
175 for (i = 0; i < 16; i++)
176 {
177 debug("%02x", vtguid[i]);
178 }
179 debug(">\n");
180
181 return 0;
182 }
183 else
184 {
185 debug("failed to open %s %d\n", devdisk, errno);
186 return errno;
187 }
188 }
189
190 static unsigned long long vtoy_get_disk_size_in_byte(const char *disk)
191 {
192 int fd;
193 int rc;
194 unsigned long long size = 0;
195 char diskpath[256] = {0};
196 char sizebuf[64] = {0};
197
198 // Try 1: get size from sysfs
199 snprintf(diskpath, sizeof(diskpath) - 1, "/sys/block/%s/size", disk);
200 if (access(diskpath, F_OK) >= 0)
201 {
202 debug("get disk size from sysfs for %s\n", disk);
203
204 fd = open(diskpath, O_RDONLY | O_BINARY);
205 if (fd >= 0)
206 {
207 read(fd, sizebuf, sizeof(sizebuf));
208 size = strtoull(sizebuf, NULL, 10);
209 close(fd);
210 return (size * 512);
211 }
212 }
213 else
214 {
215 debug("%s not exist \n", diskpath);
216 }
217
218 // Try 2: get size from ioctl
219 snprintf(diskpath, sizeof(diskpath) - 1, "/dev/%s", disk);
220 fd = open(diskpath, O_RDONLY);
221 if (fd >= 0)
222 {
223 debug("get disk size from ioctl for %s\n", disk);
224 rc = ioctl(fd, BLKGETSIZE64, &size);
225 if (rc == -1)
226 {
227 size = 0;
228 debug("failed to ioctl %d\n", rc);
229 }
230 close(fd);
231 }
232 else
233 {
234 debug("failed to open %s %d\n", diskpath, errno);
235 }
236
237 debug("disk %s size %llu bytes\n", disk, (unsigned long long)size);
238 return size;
239 }
240
241 static int vtoy_is_possible_blkdev(const char *name)
242 {
243 if (name[0] == '.')
244 {
245 return 0;
246 }
247
248 /* /dev/ramX */
249 if (name[0] == 'r' && name[1] == 'a' && name[2] == 'm')
250 {
251 return 0;
252 }
253
254 /* /dev/loopX */
255 if (name[0] == 'l' && name[1] == 'o' && name[2] == 'o' && name[3] == 'p')
256 {
257 return 0;
258 }
259
260 /* /dev/dm-X */
261 if (name[0] == 'd' && name[1] == 'm' && name[2] == '-' && IS_DIGIT(name[3]))
262 {
263 return 0;
264 }
265
266 /* /dev/srX */
267 if (name[0] == 's' && name[1] == 'r' && IS_DIGIT(name[2]))
268 {
269 return 0;
270 }
271
272 return 1;
273 }
274
275 static int vtoy_find_disk_by_size(unsigned long long size, char *diskname)
276 {
277 unsigned long long cursize = 0;
278 DIR* dir = NULL;
279 struct dirent* p = NULL;
280 int rc = 0;
281
282 dir = opendir("/sys/block");
283 if (!dir)
284 {
285 return 0;
286 }
287
288 while ((p = readdir(dir)) != NULL)
289 {
290 if (!vtoy_is_possible_blkdev(p->d_name))
291 {
292 debug("disk %s is filted by name\n", p->d_name);
293 continue;
294 }
295
296 cursize = vtoy_get_disk_size_in_byte(p->d_name);
297 debug("disk %s size %llu\n", p->d_name, (unsigned long long)cursize);
298 if (cursize == size)
299 {
300 sprintf(diskname, "%s", p->d_name);
301 rc++;
302 }
303 }
304 closedir(dir);
305 return rc;
306 }
307
308 int vtoy_find_disk_by_guid(ventoy_os_param *param, char *diskname)
309 {
310 int rc = 0;
311 int count = 0;
312 DIR* dir = NULL;
313 struct dirent* p = NULL;
314 uint8_t vtguid[16];
315 uint8_t vtsig[16];
316
317 dir = opendir("/sys/block");
318 if (!dir)
319 {
320 return 0;
321 }
322
323 while ((p = readdir(dir)) != NULL)
324 {
325 if (!vtoy_is_possible_blkdev(p->d_name))
326 {
327 debug("disk %s is filted by name\n", p->d_name);
328 continue;
329 }
330
331 memset(vtguid, 0, sizeof(vtguid));
332 memset(vtsig, 0, sizeof(vtsig));
333 rc = vtoy_get_disk_guid(p->d_name, vtguid, vtsig);
334 if (rc == 0 && memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
335 memcmp(vtsig, param->vtoy_disk_signature, 4) == 0)
336 {
337 sprintf(diskname, "%s", p->d_name);
338 count++;
339 }
340 }
341 closedir(dir);
342
343 return count;
344 }
345
346 static int vtoy_find_disk_by_sig(uint8_t *sig, char *diskname)
347 {
348 int rc = 0;
349 int count = 0;
350 DIR* dir = NULL;
351 struct dirent* p = NULL;
352 uint8_t vtguid[16];
353 uint8_t vtsig[16];
354
355 dir = opendir("/sys/block");
356 if (!dir)
357 {
358 return 0;
359 }
360
361 while ((p = readdir(dir)) != NULL)
362 {
363 if (!vtoy_is_possible_blkdev(p->d_name))
364 {
365 debug("disk %s is filted by name\n", p->d_name);
366 continue;
367 }
368
369 memset(vtguid, 0, sizeof(vtguid));
370 memset(vtsig, 0, sizeof(vtsig));
371 rc = vtoy_get_disk_guid(p->d_name, vtguid, vtsig);
372 if (rc == 0 && memcmp(vtsig, sig, 4) == 0)
373 {
374 sprintf(diskname, "%s", p->d_name);
375 count++;
376 }
377 }
378 closedir(dir);
379
380 return count;
381 }
382
383 static int vtoy_printf_iso_path(ventoy_os_param *param)
384 {
385 printf("%s\n", param->vtoy_img_path);
386 return 0;
387 }
388
389 static int vtoy_printf_fs(ventoy_os_param *param)
390 {
391 const char *fs[] =
392 {
393 "exfat", "ntfs", "ext", "xfs", "udf", "fat"
394 };
395
396 if (param->vtoy_disk_part_type < 6)
397 {
398 printf("%s\n", fs[param->vtoy_disk_part_type]);
399 }
400 else
401 {
402 printf("unknown\n");
403 }
404 return 0;
405 }
406
407 static int vtoy_vlnk_printf(ventoy_os_param *param, char *diskname)
408 {
409 int cnt = 0;
410 uint8_t disk_sig[4];
411 uint8_t mbr[512];
412 int fd = -1;
413 char diskpath[128];
414 uint8_t check[8] = { 0x56, 0x54, 0x00, 0x47, 0x65, 0x00, 0x48, 0x44 };
415
416 memcpy(disk_sig, param->vtoy_reserved + 7, 4);
417
418 debug("vlnk disk sig: %02x %02x %02x %02x \n", disk_sig[0], disk_sig[1], disk_sig[2], disk_sig[3]);
419
420 cnt = vtoy_find_disk_by_sig(disk_sig, diskname);
421 if (cnt == 1)
422 {
423 snprintf(diskpath, sizeof(diskpath), "/dev/%s", diskname);
424 fd = open(diskpath, O_RDONLY | O_BINARY);
425 if (fd >= 0)
426 {
427 memset(mbr, 0, sizeof(mbr));
428 read(fd, mbr, sizeof(mbr));
429 close(fd);
430
431 if (memcmp(mbr + 0x190, check, 8) == 0)
432 {
433 printf("/dev/%s", diskname);
434 return 0;
435 }
436 else
437 {
438 debug("check data failed /dev/%s\n", diskname);
439 }
440 }
441 }
442
443 debug("find count=%d\n", cnt);
444 printf("unknown");
445 return 1;
446 }
447
448 static int vtoy_check_iso_path_alpnum(ventoy_os_param *param)
449 {
450 char c;
451 int i = 0;
452
453 while (param->vtoy_img_path[i])
454 {
455 c = param->vtoy_img_path[i];
456
457 if (isalnum(c) || c == '_' || c == '-')
458 {
459
460 }
461 else
462 {
463 return 1;
464 }
465 i++;
466 }
467
468 return 0;
469 }
470
471 static int vtoy_check_device(ventoy_os_param *param, const char *device)
472 {
473 unsigned long long size;
474 uint8_t vtguid[16] = {0};
475 uint8_t vtsig[4] = {0};
476
477 debug("vtoy_check_device for <%s>\n", device);
478
479 size = vtoy_get_disk_size_in_byte(device);
480 vtoy_get_disk_guid(device, vtguid, vtsig);
481
482 debug("param->vtoy_disk_size=%llu size=%llu\n",
483 (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
484
485 if (memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
486 memcmp(vtsig, param->vtoy_disk_signature, 4) == 0)
487 {
488 debug("<%s> is right ventoy disk\n", device);
489 return 0;
490 }
491 else
492 {
493 debug("<%s> is NOT right ventoy disk\n", device);
494 return 1;
495 }
496 }
497
498 static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
499 {
500 int fd, size;
501 int cnt = 0;
502 char *path = param->vtoy_img_path;
503 const char *fs;
504 char diskpath[256] = {0};
505 char sizebuf[64] = {0};
506
507 cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
508 debug("find disk by size %llu, cnt=%d...\n", (unsigned long long)param->vtoy_disk_size, cnt);
509 if (1 == cnt)
510 {
511 if (vtoy_check_device(param, diskname) != 0)
512 {
513 cnt = 0;
514 }
515 }
516 else
517 {
518 cnt = vtoy_find_disk_by_guid(param, diskname);
519 debug("find disk by guid cnt=%d...\n", cnt);
520 }
521
522 if (param->vtoy_disk_part_type < ventoy_fs_max)
523 {
524 fs = g_ventoy_fs[param->vtoy_disk_part_type];
525 }
526 else
527 {
528 fs = "unknown";
529 }
530
531 if (1 == cnt)
532 {
533 if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd"))
534 {
535 snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%sp2/size", diskname);
536 }
537 else
538 {
539 snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%s2/size", diskname);
540 }
541
542 if (param->vtoy_reserved[6] == 0 && access(diskpath, F_OK) >= 0)
543 {
544 debug("get part size from sysfs for %s\n", diskpath);
545
546 fd = open(diskpath, O_RDONLY | O_BINARY);
547 if (fd >= 0)
548 {
549 read(fd, sizebuf, sizeof(sizebuf));
550 size = (int)strtoull(sizebuf, NULL, 10);
551 close(fd);
552 if ((size != (64 * 1024)) && (size != (8 * 1024)))
553 {
554 debug("sizebuf=<%s> size=%d\n", sizebuf, size);
555 return 1;
556 }
557 }
558 }
559 else
560 {
561 debug("%s not exist \n", diskpath);
562 }
563
564 printf("/dev/%s#%s#%s\n", diskname, fs, path);
565 return 0;
566 }
567 else
568 {
569 return 1;
570 }
571 }
572
573 /*
574 * Find disk and image path from ventoy runtime data.
575 * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
576 *
577 * -f datafile os param data file.
578 * -c /dev/xxx check ventoy disk
579 * -v be verbose
580 * -l also print image disk location
581 */
582 int vtoydump_main(int argc, char **argv)
583 {
584 int rc;
585 int ch;
586 int print_path = 0;
587 int check_ascii = 0;
588 int print_fs = 0;
589 int vlnk_print = 0;
590 char filename[256] = {0};
591 char diskname[256] = {0};
592 char device[64] = {0};
593 ventoy_os_param *param = NULL;
594
595 while ((ch = getopt(argc, argv, "a:c:f:p:t:s:v::")) != -1)
596 {
597 if (ch == 'f')
598 {
599 strncpy(filename, optarg, sizeof(filename) - 1);
600 }
601 else if (ch == 'v')
602 {
603 verbose = 1;
604 }
605 else if (ch == 'c')
606 {
607 strncpy(device, optarg, sizeof(device) - 1);
608 }
609 else if (ch == 'p')
610 {
611 print_path = 1;
612 strncpy(filename, optarg, sizeof(filename) - 1);
613 }
614 else if (ch == 'a')
615 {
616 check_ascii = 1;
617 strncpy(filename, optarg, sizeof(filename) - 1);
618 }
619 else if (ch == 't')
620 {
621 vlnk_print = 1;
622 strncpy(filename, optarg, sizeof(filename) - 1);
623 }
624 else if (ch == 's')
625 {
626 print_fs = 1;
627 strncpy(filename, optarg, sizeof(filename) - 1);
628 }
629 else
630 {
631 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
632 return 1;
633 }
634 }
635
636 if (filename[0] == 0)
637 {
638 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
639 return 1;
640 }
641
642 param = malloc(sizeof(ventoy_os_param));
643 if (NULL == param)
644 {
645 fprintf(stderr, "failed to alloc memory with size %d error %d\n",
646 (int)sizeof(ventoy_os_param), errno);
647 return 1;
648 }
649
650 memset(param, 0, sizeof(ventoy_os_param));
651
652 debug("get os pararm from file %s\n", filename);
653 rc = vtoy_os_param_from_file(filename, param);
654 if (rc)
655 {
656 debug("ventoy os param not found %d %d\n", rc, ENOENT);
657 if (ENOENT == rc)
658 {
659 debug("now try with file %s\n", "/ventoy/ventoy_os_param");
660 rc = vtoy_os_param_from_file("/ventoy/ventoy_os_param", param);
661 if (rc)
662 {
663 goto end;
664 }
665 }
666 else
667 {
668 goto end;
669 }
670 }
671
672 if (verbose)
673 {
674 vtoy_dump_os_param(param);
675 }
676
677 if (print_path)
678 {
679 rc = vtoy_printf_iso_path(param);
680 }
681 else if (print_fs)
682 {
683 rc = vtoy_printf_fs(param);
684 }
685 else if (vlnk_print)
686 {
687 rc = vtoy_vlnk_printf(param, diskname);
688 }
689 else if (device[0])
690 {
691 rc = vtoy_check_device(param, device);
692 }
693 else if (check_ascii)
694 {
695 rc = vtoy_check_iso_path_alpnum(param);
696 }
697 else
698 {
699 // print os param, you can change the output format in the function
700 rc = vtoy_print_os_param(param, diskname);
701 }
702
703 end:
704 if (param)
705 {
706 free(param);
707 }
708 return rc;
709 }
710
711 // wrapper main
712 #ifndef BUILD_VTOY_TOOL
713 int main(int argc, char **argv)
714 {
715 return vtoydump_main(argc, argv);
716 }
717 #endif
718