]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyloader.c
Fix the order issue in TreeView mode. (#3218)
[Ventoy.git] / VtoyTool / vtoyloader.c
1 /******************************************************************************
2 * vtoyloader.c ---- ventoy loader (wapper for binary loader)
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
33 #define MAX_EXT_PARAM 256
34 #define CMDLINE_BUF_LEN (1024 * 1024 * 1)
35 #define EXEC_PATH_FILE "/ventoy/loader_exec_file"
36 #define CMDLINE_FILE "/ventoy/loader_exec_cmdline"
37 #define HOOK_CMD_FILE "/ventoy/loader_hook_cmd"
38 #define DEBUG_FLAG_FILE "/ventoy/loader_debug"
39
40 static int verbose = 0;
41 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
42
43 static char g_exec_file[512];
44 static char g_hook_cmd[512];
45
46 static int vtoy_read_file_to_buf(const char *file, void *buf, int buflen)
47 {
48 FILE *fp;
49
50 fp = fopen(file, "r");
51 if (!fp)
52 {
53 fprintf(stderr, "Failed to open file %s err:%d\n", file, errno);
54 return 1;
55 }
56 fread(buf, 1, buflen, fp);
57 fclose(fp);
58
59 return 0;
60 }
61
62 int vtoyloader_main(int argc, char **argv)
63 {
64 int i;
65 int len;
66 int rc;
67 char *pos;
68 char *cmdline;
69 char **cmdlist;
70
71 if (access(DEBUG_FLAG_FILE, F_OK) >= 0)
72 {
73 verbose = 1;
74 }
75
76 debug("ventoy loader ...\n");
77
78 rc = vtoy_read_file_to_buf(EXEC_PATH_FILE, g_exec_file, sizeof(g_exec_file) - 1);
79 if (rc)
80 {
81 return rc;
82 }
83
84 if (access(g_exec_file, F_OK) < 0)
85 {
86 fprintf(stderr, "File %s not exist\n", g_exec_file);
87 return 1;
88 }
89
90 if (access(HOOK_CMD_FILE, F_OK) >= 0)
91 {
92 rc = vtoy_read_file_to_buf(HOOK_CMD_FILE, g_hook_cmd, sizeof(g_hook_cmd) - 1);
93 debug("g_hook_cmd=<%s>\n", g_hook_cmd);
94 }
95
96 cmdline = (char *)malloc(CMDLINE_BUF_LEN);
97 if (!cmdline)
98 {
99 fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
100 return 1;
101 }
102 memset(cmdline, 0, CMDLINE_BUF_LEN);
103
104 if (access(CMDLINE_FILE, F_OK) >= 0)
105 {
106 rc = vtoy_read_file_to_buf(CMDLINE_FILE, cmdline, CMDLINE_BUF_LEN - 1);
107 if (rc)
108 {
109 return rc;
110 }
111 }
112
113 len = (int)((argc + MAX_EXT_PARAM) * sizeof(char *));
114 cmdlist = (char **)malloc(len);
115 if (!cmdlist)
116 {
117 free(cmdline);
118 fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
119 return 1;
120 }
121 memset(cmdlist, 0, len);
122
123 for (i = 0; i < argc; i++)
124 {
125 cmdlist[i] = argv[i];
126 }
127
128 cmdlist[0] = g_exec_file;
129 debug("g_exec_file=<%s>\n", g_exec_file);
130
131 pos = cmdline;
132 while ((*pos) && i < MAX_EXT_PARAM)
133 {
134 cmdlist[i++] = pos;
135
136 while (*pos)
137 {
138 if (*pos == '\r')
139 {
140 *pos = ' ';
141 }
142 else if (*pos == '\n')
143 {
144 *pos = 0;
145 pos++;
146 break;
147 }
148
149 pos++;
150 }
151 }
152
153 debug("execv [%s]...\n", cmdlist[0]);
154
155 // call hook script
156 if (g_hook_cmd[0])
157 {
158 rc = system(g_hook_cmd);
159 debug("system return code =<%d> errno=<%d>\n", rc, errno);
160 }
161
162 execv(cmdlist[0], cmdlist);
163
164 return 0;
165 }
166
167
168 // wrapper main
169 #ifndef BUILD_VTOY_TOOL
170 int main(int argc, char **argv)
171 {
172 return vtoyloader_main(argc, argv);
173 }
174 #endif
175