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