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