]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoyfat/vtoyfat_linux.c
DragonFly BSD support
[Ventoy.git] / vtoyfat / vtoyfat_linux.c
1 /******************************************************************************
2 * vtoyfat.c ---- Parse fat file system
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 <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <fat_filelib.h>
29
30 static int g_disk_fd = 0;
31
32 static int vtoy_disk_read(uint32 sector, uint8 *buffer, uint32 sector_count)
33 {
34 lseek(g_disk_fd, sector * 512, SEEK_SET);
35 read(g_disk_fd, buffer, sector_count * 512);
36
37 return 1;
38 }
39
40 static int check_secure_boot(void)
41 {
42 void *flfile = NULL;
43
44 flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
45 if (flfile)
46 {
47 fl_fclose(flfile);
48 return 0;
49 }
50
51 return 1;
52 }
53
54 static int get_ventoy_version(void)
55 {
56 int rc = 1;
57 int size = 0;
58 char *buf = NULL;
59 char *pos = NULL;
60 char *end = NULL;
61 void *flfile = NULL;
62
63 flfile = fl_fopen("/grub/grub.cfg", "rb");
64 if (flfile)
65 {
66 fl_fseek(flfile, 0, SEEK_END);
67 size = (int)fl_ftell(flfile);
68
69 fl_fseek(flfile, 0, SEEK_SET);
70
71 buf = malloc(size + 1);
72 if (buf)
73 {
74 fl_fread(buf, 1, size, flfile);
75 buf[size] = 0;
76
77 pos = strstr(buf, "VENTOY_VERSION=");
78 if (pos)
79 {
80 pos += strlen("VENTOY_VERSION=");
81 if (*pos == '"')
82 {
83 pos++;
84 }
85
86 end = pos;
87 while (*end != 0 && *end != '"' && *end != '\r' && *end != '\n')
88 {
89 end++;
90 }
91
92 *end = 0;
93
94 printf("%s\n", pos);
95 rc = 0;
96 }
97 free(buf);
98 }
99
100 fl_fclose(flfile);
101 }
102
103 return rc;
104 }
105
106 int main(int argc, char **argv)
107 {
108 int op = 0;
109 int rc = 1;
110 char *disk;
111
112 if (argc != 2 && argc != 3)
113 {
114 printf("Usage: vtoyfat /dev/sdbs \n");
115 printf("Usage: vtoyfat -s /dev/sdbs \n");
116 return 1;
117 }
118
119 if (argv[1][0] == '-' && argv[1][1] == 'T')
120 {
121 return 0;
122 }
123
124 disk = argv[1];
125 if (argv[1][0] == '-' && argv[1][1] == 's')
126 {
127 op = 1;
128 disk = argv[2];
129 }
130
131 g_disk_fd = open(disk, O_RDONLY);
132 if (g_disk_fd < 0)
133 {
134 printf("Failed to open %s\n", disk);
135 return 1;
136 }
137
138 fl_init();
139
140 if (0 == fl_attach_media(vtoy_disk_read, NULL))
141 {
142 if (op == 0)
143 {
144 rc = get_ventoy_version();
145 }
146 else
147 {
148 rc = check_secure_boot();
149 }
150 }
151
152 fl_shutdown();
153
154 close(g_disk_fd);
155
156 return rc;
157 }
158