]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoydump.c
LiveCD
[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 reserved[31];
124 }ventoy_os_param;
125
126 #pragma pack()
127
128 #ifndef O_BINARY
129 #define O_BINARY 0
130 #endif
131 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
132 #define MMAP_FLAGS MAP_SHARED
133 #else
134 #define MMAP_FLAGS MAP_PRIVATE
135 #endif
136
137 #define SEARCH_MEM_START 0x80000
138 #define SEARCH_MEM_LEN 0x1c000
139
140 static int verbose = 0;
141 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
142
143 static ventoy_guid vtoy_guid = VENTOY_GUID;
144
145 static const char *g_ventoy_fs[ventoy_fs_max] =
146 {
147 "exfat", "ntfs", "ext*", "xfs", "udf", "fat"
148 };
149
150 static int vtoy_check_os_param(ventoy_os_param *param)
151 {
152 uint32_t i;
153 uint8_t chksum = 0;
154 uint8_t *buf = (uint8_t *)param;
155
156 if (memcmp(&param->guid, &vtoy_guid, sizeof(ventoy_guid)))
157 {
158 uint8_t *data1 = (uint8_t *)(&param->guid);
159 uint8_t *data2 = (uint8_t *)(&vtoy_guid);
160
161 for (i = 0; i < 16; i++)
162 {
163 if (data1[i] != data2[i])
164 {
165 debug("guid not equal i = %u, 0x%02x, 0x%02x\n", i, data1[i], data2[i]);
166 }
167 }
168 return 1;
169 }
170
171 for (i = 0; i < sizeof(ventoy_os_param); i++)
172 {
173 chksum += buf[i];
174 }
175
176 if (chksum)
177 {
178 debug("Invalid checksum 0x%02x\n", chksum);
179 return 1;
180 }
181
182 return 0;
183 }
184
185 static int vtoy_os_param_from_file(const char *filename, ventoy_os_param *param)
186 {
187 int fd = 0;
188 int rc = 0;
189
190 fd = open(filename, O_RDONLY | O_BINARY);
191 if (fd < 0)
192 {
193 fprintf(stderr, "Failed to open file %s error %d\n", filename, errno);
194 return errno;
195 }
196
197 read(fd, param, sizeof(ventoy_os_param));
198
199 if (vtoy_check_os_param(param) == 0)
200 {
201 debug("find ventoy os param in file %s\n", filename);
202 }
203 else
204 {
205 debug("ventoy os pararm NOT found in file %s\n", filename);
206 rc = 1;
207 }
208
209 close(fd);
210 return rc;
211 }
212
213 static void vtoy_dump_os_param(ventoy_os_param *param)
214 {
215 printf("################# dump os param ################\n");
216
217 printf("param->chksum = 0x%x\n", param->chksum);
218 printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
219 param->vtoy_disk_guid[0], param->vtoy_disk_guid[1],
220 param->vtoy_disk_guid[2], param->vtoy_disk_guid[3]);
221 printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param->vtoy_disk_size);
222 printf("param->vtoy_disk_part_id = %u\n", param->vtoy_disk_part_id);
223 printf("param->vtoy_disk_part_type = %u\n", param->vtoy_disk_part_type);
224 printf("param->vtoy_img_path = <%s>\n", param->vtoy_img_path);
225 printf("param->vtoy_img_size = <%llu>\n", (unsigned long long)param->vtoy_img_size);
226 printf("param->vtoy_img_location_addr = <0x%llx>\n", (unsigned long long)param->vtoy_img_location_addr);
227 printf("param->vtoy_img_location_len = <%u>\n", param->vtoy_img_location_len);
228 printf("param->vtoy_reserved[0] = 0x%llx\n", (unsigned long long)param->vtoy_reserved[0]);
229 printf("param->vtoy_reserved[1] = 0x%llx\n", (unsigned long long)param->vtoy_reserved[1]);
230
231 printf("\n");
232 }
233
234 static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid)
235 {
236 int i = 0;
237 int fd = 0;
238 char devdisk[128] = {0};
239
240 snprintf(devdisk, sizeof(devdisk) - 1, "/dev/%s", diskname);
241
242 fd = open(devdisk, O_RDONLY | O_BINARY);
243 if (fd >= 0)
244 {
245 lseek(fd, 0x180, SEEK_SET);
246 read(fd, vtguid, 16);
247 close(fd);
248
249 debug("GUID for %s: <", devdisk);
250 for (i = 0; i < 16; i++)
251 {
252 debug("%02x", vtguid[i]);
253 }
254 debug(">\n");
255
256 return 0;
257 }
258 else
259 {
260 debug("failed to open %s %d\n", devdisk, errno);
261 return errno;
262 }
263 }
264
265 static unsigned long long vtoy_get_disk_size_in_byte(const char *disk)
266 {
267 int fd;
268 int rc;
269 unsigned long long size = 0;
270 char diskpath[256] = {0};
271 char sizebuf[64] = {0};
272
273 // Try 1: get size from sysfs
274 snprintf(diskpath, sizeof(diskpath) - 1, "/sys/block/%s/size", disk);
275 if (access(diskpath, F_OK) >= 0)
276 {
277 debug("get disk size from sysfs for %s\n", disk);
278
279 fd = open(diskpath, O_RDONLY | O_BINARY);
280 if (fd >= 0)
281 {
282 read(fd, sizebuf, sizeof(sizebuf));
283 size = strtoull(sizebuf, NULL, 10);
284 close(fd);
285 return (size * 512);
286 }
287 }
288 else
289 {
290 debug("%s not exist \n", diskpath);
291 }
292
293 // Try 2: get size from ioctl
294 snprintf(diskpath, sizeof(diskpath) - 1, "/dev/%s", disk);
295 fd = open(diskpath, O_RDONLY);
296 if (fd >= 0)
297 {
298 debug("get disk size from ioctl for %s\n", disk);
299 rc = ioctl(fd, BLKGETSIZE64, &size);
300 if (rc == -1)
301 {
302 size = 0;
303 debug("failed to ioctl %d\n", rc);
304 }
305 close(fd);
306 }
307 else
308 {
309 debug("failed to open %s %d\n", diskpath, errno);
310 }
311
312 debug("disk %s size %llu bytes\n", disk, (unsigned long long)size);
313 return size;
314 }
315
316 static int vtoy_is_possible_blkdev(const char *name)
317 {
318 if (name[0] == '.')
319 {
320 return 0;
321 }
322
323 /* /dev/ramX */
324 if (name[0] == 'r' && name[1] == 'a' && name[2] == 'm')
325 {
326 return 0;
327 }
328
329 /* /dev/loopX */
330 if (name[0] == 'l' && name[1] == 'o' && name[2] == 'o' && name[3] == 'p')
331 {
332 return 0;
333 }
334
335 /* /dev/dm-X */
336 if (name[0] == 'd' && name[1] == 'm' && name[2] == '-' && IS_DIGIT(name[3]))
337 {
338 return 0;
339 }
340
341 /* /dev/srX */
342 if (name[0] == 's' && name[1] == 'r' && IS_DIGIT(name[2]))
343 {
344 return 0;
345 }
346
347 return 1;
348 }
349
350 static int vtoy_find_disk_by_size(unsigned long long size, char *diskname)
351 {
352 unsigned long long cursize = 0;
353 DIR* dir = NULL;
354 struct dirent* p = NULL;
355 int rc = 0;
356
357 dir = opendir("/sys/block");
358 if (!dir)
359 {
360 return 0;
361 }
362
363 while ((p = readdir(dir)) != NULL)
364 {
365 if (!vtoy_is_possible_blkdev(p->d_name))
366 {
367 debug("disk %s is filted by name\n", p->d_name);
368 continue;
369 }
370
371 cursize = vtoy_get_disk_size_in_byte(p->d_name);
372 debug("disk %s size %llu\n", p->d_name, (unsigned long long)cursize);
373 if (cursize == size)
374 {
375 sprintf(diskname, "%s", p->d_name);
376 rc++;
377 }
378 }
379 closedir(dir);
380 return rc;
381 }
382
383 static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname)
384 {
385 int rc = 0;
386 int count = 0;
387 DIR* dir = NULL;
388 struct dirent* p = NULL;
389 uint8_t vtguid[16];
390
391 dir = opendir("/sys/block");
392 if (!dir)
393 {
394 return 0;
395 }
396
397 while ((p = readdir(dir)) != NULL)
398 {
399 if (!vtoy_is_possible_blkdev(p->d_name))
400 {
401 debug("disk %s is filted by name\n", p->d_name);
402 continue;
403 }
404
405 memset(vtguid, 0, sizeof(vtguid));
406 rc = vtoy_get_disk_guid(p->d_name, vtguid);
407 if (rc == 0 && memcmp(vtguid, guid, 16) == 0)
408 {
409 sprintf(diskname, "%s", p->d_name);
410 count++;
411 }
412 }
413 closedir(dir);
414
415 return count;
416 }
417
418 static int vtoy_printf_iso_path(ventoy_os_param *param)
419 {
420 printf("%s\n", param->vtoy_img_path);
421 return 0;
422 }
423
424 static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
425 {
426 int cnt = 0;
427 char *path = param->vtoy_img_path;
428 const char *fs;
429
430 cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
431 if (cnt > 1)
432 {
433 cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
434 }
435 else if (cnt == 0)
436 {
437 cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
438 debug("find 0 disk by size, try with guid cnt=%d...\n", cnt);
439 }
440
441 if (param->vtoy_disk_part_type < ventoy_fs_max)
442 {
443 fs = g_ventoy_fs[param->vtoy_disk_part_type];
444 }
445 else
446 {
447 fs = "unknown";
448 }
449
450 if (1 == cnt)
451 {
452 printf("/dev/%s#%s#%s\n", diskname, fs, path);
453 return 0;
454 }
455 else
456 {
457 return 1;
458 }
459 }
460
461 static int vtoy_check_device(ventoy_os_param *param, const char *device)
462 {
463 unsigned long long size;
464 uint8_t vtguid[16] = {0};
465
466 debug("vtoy_check_device for <%s>\n", device);
467
468 size = vtoy_get_disk_size_in_byte(device);
469 vtoy_get_disk_guid(device, vtguid);
470
471 debug("param->vtoy_disk_size=%llu size=%llu\n",
472 (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
473
474 if ((param->vtoy_disk_size == size || param->vtoy_disk_size == size + 512) &&
475 memcmp(vtguid, param->vtoy_disk_guid, 16) == 0)
476 {
477 debug("<%s> is right ventoy disk\n", device);
478 return 0;
479 }
480 else
481 {
482 debug("<%s> is NOT right ventoy disk\n", device);
483 return 1;
484 }
485 }
486
487 /*
488 * Find disk and image path from ventoy runtime data.
489 * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
490 *
491 * -f datafile os param data file.
492 * -c /dev/xxx check ventoy disk
493 * -v be verbose
494 * -l also print image disk location
495 */
496 int vtoydump_main(int argc, char **argv)
497 {
498 int rc;
499 int ch;
500 int print_path = 0;
501 char filename[256] = {0};
502 char diskname[256] = {0};
503 char device[64] = {0};
504 ventoy_os_param *param = NULL;
505
506 while ((ch = getopt(argc, argv, "c:f:p:v::")) != -1)
507 {
508 if (ch == 'f')
509 {
510 strncpy(filename, optarg, sizeof(filename) - 1);
511 }
512 else if (ch == 'v')
513 {
514 verbose = 1;
515 }
516 else if (ch == 'c')
517 {
518 strncpy(device, optarg, sizeof(device) - 1);
519 }
520 else if (ch == 'p')
521 {
522 print_path = 1;
523 strncpy(filename, optarg, sizeof(filename) - 1);
524 }
525 else
526 {
527 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
528 return 1;
529 }
530 }
531
532 if (filename[0] == 0)
533 {
534 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
535 return 1;
536 }
537
538 param = malloc(sizeof(ventoy_os_param));
539 if (NULL == param)
540 {
541 fprintf(stderr, "failed to alloc memory with size %d error %d\n",
542 (int)sizeof(ventoy_os_param), errno);
543 return 1;
544 }
545
546 memset(param, 0, sizeof(ventoy_os_param));
547
548 debug("get os pararm from file %s\n", filename);
549 rc = vtoy_os_param_from_file(filename, param);
550 if (rc)
551 {
552 debug("ventoy os param not found %d\n", rc);
553 goto end;
554 }
555
556 if (verbose)
557 {
558 vtoy_dump_os_param(param);
559 }
560
561 if (print_path)
562 {
563 rc = vtoy_printf_iso_path(param);
564 }
565 else if (device[0])
566 {
567 rc = vtoy_check_device(param, device);
568 }
569 else
570 {
571 // print os param, you can change the output format in the function
572 rc = vtoy_print_os_param(param, diskname);
573 }
574
575 end:
576 if (param)
577 {
578 free(param);
579 }
580 return rc;
581 }
582
583 // wrapper main
584 #ifndef BUILD_VTOY_TOOL
585 int main(int argc, char **argv)
586 {
587 return vtoydump_main(argc, argv);
588 }
589 #endif
590