]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyksym.c
Don't force to use max resolution for VMware/VirtualBox. (#3140)
[Ventoy.git] / VtoyTool / vtoyksym.c
1 /******************************************************************************
2 * vtoyksym.c ---- ventoy ksym
3 *
4 * Copyright (c) 2021, 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 <ctype.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 static int verbose = 0;
29 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
30
31 int vtoyksym_main(int argc, char **argv)
32 {
33 int i;
34 int len = 0;
35 unsigned long long addr1 = 0;
36 unsigned long long addr2 = 0;
37 char sym[256];
38 char line[1024];
39 char *start = NULL;
40 const char *name = NULL;
41 FILE *fp;
42
43 for (i = 0; i < argc; i++)
44 {
45 if (argv[i][0] == '-' && argv[i][1] == 'p')
46 {
47 printf("%d", getpagesize());
48 return 0;
49 }
50 }
51
52 for (i = 0; i < argc; i++)
53 {
54 if (argv[i][0] == '-' && argv[i][1] == 'v')
55 {
56 verbose = 1;
57 break;
58 }
59 }
60
61 name = argv[2] ? argv[2] : "/proc/kallsyms";
62 fp = fopen(name, "r");
63 if (!fp)
64 {
65 fprintf(stderr, "Failed to open file %s err:%d\n", name, errno);
66 return 1;
67 }
68
69 debug("open %s success\n", name);
70
71 snprintf(sym, sizeof(sym), " %s", argv[1]);
72 debug("lookup for <%s>\n", sym);
73
74 len = (int)strlen(sym);
75 while (fgets(line, sizeof(line), fp))
76 {
77 start = strstr(line, sym);
78 if (start && (start > line) && isspace(*(start + len)))
79 {
80 addr1 = strtoull(line, NULL, 16);
81 if (!fgets(line, sizeof(line), fp))
82 {
83 addr1 = 0;
84 fprintf(stderr, "Failed to read next line\n");
85 }
86 else
87 {
88 addr2 = strtoull(line, NULL, 16);
89 }
90
91 debug("addr1=<0x%llx> addr2=<0x%llx>\n", addr1, addr2);
92 break;
93 }
94 }
95
96 if (addr1 > addr2)
97 {
98 debug("Invalid addr range\n");
99 printf("0 0\n");
100 }
101 else
102 {
103 printf("0x%llx %llu\n", addr1, addr2 - addr1);
104 }
105
106 fclose(fp);
107
108 return 0;
109 }
110
111 // wrapper main
112 #ifndef BUILD_VTOY_TOOL
113 int main(int argc, char **argv)
114 {
115 return vtoyksym_main(argc, argv);
116 }
117 #endif
118