]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyloader.c
DragonFly BSD support
[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 // call hook script
96 rc = system(g_hook_cmd);
97 debug("system return code =<%d> errno=<%d>\n", rc, errno);
98 }
99
100 cmdline = (char *)malloc(CMDLINE_BUF_LEN);
101 if (!cmdline)
102 {
103 fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
104 return 1;
105 }
106 memset(cmdline, 0, CMDLINE_BUF_LEN);
107
108 if (access(CMDLINE_FILE, F_OK) >= 0)
109 {
110 rc = vtoy_read_file_to_buf(CMDLINE_FILE, cmdline, CMDLINE_BUF_LEN - 1);
111 if (rc)
112 {
113 return rc;
114 }
115 }
116
117 len = (int)((argc + MAX_EXT_PARAM) * sizeof(char *));
118 cmdlist = (char **)malloc(len);
119 if (!cmdlist)
120 {
121 free(cmdline);
122 fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
123 return 1;
124 }
125 memset(cmdlist, 0, len);
126
127 for (i = 0; i < argc; i++)
128 {
129 cmdlist[i] = argv[i];
130 }
131
132 cmdlist[0] = g_exec_file;
133 debug("g_exec_file=<%s>\n", g_exec_file);
134
135 pos = cmdline;
136 while ((*pos) && i < MAX_EXT_PARAM)
137 {
138 cmdlist[i++] = pos;
139
140 while (*pos)
141 {
142 if (*pos == '\r')
143 {
144 *pos = ' ';
145 }
146 else if (*pos == '\n')
147 {
148 *pos = 0;
149 pos++;
150 break;
151 }
152
153 pos++;
154 }
155 }
156
157 debug("execv [%s]...\n", cmdlist[0]);
158
159 execv(cmdlist[0], cmdlist);
160
161 return 0;
162 }
163
164
165 // wrapper main
166 #ifndef BUILD_VTOY_TOOL
167 int main(int argc, char **argv)
168 {
169 return vtoyloader_main(argc, argv);
170 }
171 #endif
172