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