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