]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyvine.c
added Spanish (Latinoamérica) translation (#1865)
[Ventoy.git] / VtoyTool / vtoyvine.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 static int verbose = 0;
34 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
35
36 static int vine_patch_loader(unsigned char *buf, int len, int major)
37 {
38 int i;
39 int ptrlen;
40 unsigned int *data1;
41 unsigned int *data2;
42
43 /*
44 * http://vinelinux.ime.cmc.osaka-u.ac.jp/Vine-6.5/SRPMS/SRPMS.main/anaconda-vine-11.0.2.1-1vl7.src.rpm
45 * http://vinelinux.ime.cmc.osaka-u.ac.jp/Vine-6.5/SRPMS/SRPMS.main/kudzu-1.2.86-3vl6.src.rpm
46 * anaconda-vine-11.0.2.1
47 * isys/devnodes.c
48 * static struct devnum devices[] = {
49 * { "aztcd", 29, 0, 0 },
50 * { "pcd", 46, 0, 0 },
51 *
52 * Patch 29 ---> 253
53 */
54
55 ptrlen = (buf[4] == 1) ? 4 : 8;
56 debug("ELF %d bit major:%d ptrlen:%d\n", (buf[4] == 1) ? 32 : 64, major, ptrlen);
57
58 for (i = 0; i < len - 8 - 8 - ptrlen; i++)
59 {
60 data1 = (unsigned int *)(buf + i);
61 data2 = (unsigned int *)(buf + i + 8 + ptrlen);
62
63 if (data1[0] == 0x1D && data1[1] == 0x00 && data2[0] == 0x2E && data2[1] == 0x00)
64 {
65 debug("Find aztcd patch point at %d\n", i);
66 data1[0] = major;
67 break;
68 }
69 }
70
71 for (i = 0; i < len; i++)
72 {
73 if (buf[i] != '/')
74 {
75 continue;
76 }
77
78 data1 = (unsigned int *)(buf + i + 1);
79
80 /* /proc/ide */
81 if (data1[0] == 0x636F7270 && data1[1] == 0x6564692F)
82 {
83 debug("patch string %s\n", (char *)(buf + i));
84 buf[i + 1] = 'v';
85 buf[i + 2] = 't';
86 buf[i + 3] = 'o';
87 buf[i + 4] = 'y';
88 }
89 }
90
91 return 0;
92 }
93
94 int vtoyvine_main(int argc, char **argv)
95 {
96 int i;
97 int len;
98 unsigned char *buf;
99 FILE *fp;
100
101 for (i = 0; i < argc; i++)
102 {
103 if (argv[i][0] == '-' && argv[i][1] == 'v')
104 {
105 verbose = 1;
106 break;
107 }
108 }
109
110 fp = fopen(argv[1], "rb");
111 if (!fp)
112 {
113 fprintf(stderr, "Failed to open file %s err:%d\n", argv[1], errno);
114 return 1;
115 }
116
117 fseek(fp, 0, SEEK_END);
118 len = (int)ftell(fp);
119 debug("file length:%d\n", len);
120
121 fseek(fp, 0, SEEK_SET);
122
123 buf = (unsigned char *)malloc(len);
124 if (!buf)
125 {
126 fclose(fp);
127 return 1;
128 }
129
130 fread(buf, 1, len, fp);
131 fclose(fp);
132
133 vine_patch_loader(buf, len, (int)strtoul(argv[2], NULL, 10));
134
135 fp = fopen(argv[1], "wb+");
136 if (!fp)
137 {
138 fprintf(stderr, "Failed to open file %s err:%d\n", argv[1], errno);
139 free(buf);
140 return 1;
141 }
142
143 debug("write new data length:%d\n", len);
144 fwrite(buf, 1, len, fp);
145 fclose(fp);
146
147 free(buf);
148
149 return 0;
150 }
151
152 // wrapper main
153 #ifndef BUILD_VTOY_TOOL
154 int main(int argc, char **argv)
155 {
156 return vtoyvine_main(argc, argv);
157 }
158 #endif
159