-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvpatch.cpp
More file actions
376 lines (314 loc) · 10.7 KB
/
Copy pathnvpatch.cpp
File metadata and controls
376 lines (314 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdint.h>
#include <stdio.h>
#include <conio.h>
#include <i86.h>
#include "tinypci.h"
#define arrayof(a) (sizeof(a) / sizeof(a[0]))
// read one byte
uint8_t read_byte(FILE *f, uint32_t pos) {
uint8_t data;
fseek(f, pos, SEEK_SET);
fread(&data, sizeof(data), 1, f);
return data;
}
// read word
uint16_t read_word(FILE *f, uint32_t pos) {
uint16_t data;
fseek(f, pos, SEEK_SET);
fread(&data, sizeof(data), 1, f);
return data;
}
// read dword
uint32_t read_dword(FILE *f, uint32_t pos) {
uint32_t data;
fseek(f, pos, SEEK_SET);
fread(&data, sizeof(data), 1, f);
return data;
}
// read string of bytes
void read_bytes(FILE *f, uint32_t pos, void* data, uint32_t size) {
fseek(f, pos, SEEK_SET);
fread(data, size, 1, f);
}
// patch one byte
void patch_byte(FILE *f, uint32_t pos, uint8_t data) {
fseek(f, pos, SEEK_SET);
fwrite(&data, sizeof(data), 1, f);
}
// patch word
void patch_word(FILE *f, uint32_t pos, uint16_t data) {
fseek(f, pos, SEEK_SET);
fwrite(&data, sizeof(data), 1, f);
}
// patch dword
void patch_dword(FILE *f, uint32_t pos, uint32_t data) {
fseek(f, pos, SEEK_SET);
fwrite(&data, sizeof(data), 1, f);
}
// patch string of bytes
void patch_bytes(FILE *f, uint32_t pos, void* data, uint32_t size) {
fseek(f, pos, SEEK_SET);
fwrite(data, size, 1, f);
}
// patch multiple locations with same value
void patch_byte_list(FILE *f, uint32_t *pos, uint32_t poslen, uint8_t data) {
while (poslen-- != 0) {
patch_byte(f, *pos++, data);
}
}
void patch_word_list(FILE *f, uint32_t *pos, uint32_t poslen, uint16_t data) {
while (poslen-- != 0) {
patch_word(f, *pos++, data);
}
}
void patch_dword_list(FILE *f, uint32_t *pos, uint32_t poslen, uint32_t data) {
while (poslen-- != 0) {
patch_dword(f, *pos++, data);
}
}
// --------------
// nv realmode backdoor
uint32_t nvRead(uint32_t addr) {
// write address to realmode backdoor
outpw(0x3d4, 0x0338);
outpd(0x3d0, addr);
// read data
outpw(0x3d4, 0x0538);
return inpd(0x3d0);
}
void nvWrite(uint32_t addr, uint32_t data) {
// write address to realmode backdoor
outpw(0x3d4, 0x0338);
outpd(0x3d0, addr);
// read data
outpw(0x3d4, 0x0538);
outpd(0x3d0, data);
}
void nvLock() {
outpw(0x3d4, 0x0038);
}
uint32_t nvGetAddress() {
outpw(0x3d4, 0x0338);
return inpd(0x3d0);
}
void nvSetAddress(uint32_t addr) {
outpw(0x3d4, 0x0338);
outpd(0x3d0, addr);
}
void nvCrtcUnlock() {
outpw(0x3d4, 0x571f);
}
void nvCrtcLock() {
outpw(0x3d4, 0x991f);
}
// ---------------------
// get xtal frequency in kHz
uint32_t getXtalFreq() {
// unlock
nvCrtcUnlock();
uint32_t savedAddr = nvGetAddress();
// read PEXTDEV, get frequency
uint32_t pextdev = nvRead(0x000101000);
uint32_t xtalfreq = (pextdev & (1 << 22)) ? 27000 : ((pextdev & (1 << 6)) ? 14318 : 13500);
// lock it back
nvSetAddress(savedAddr);
nvLock();
nvCrtcLock();
return xtalfreq;
}
// ---------------------
// patch info
struct patchdata_t {
pciDeviceList* pcidev;
uint32_t vendorid, deviceid;
uint32_t xtal_freq;
};
// list of files to scan
struct filescan_t {
char *filename;
uint32_t fsize;
uint32_t match_ofs;
uint32_t match_sig;
int (*patchfunc)(FILE *f, patchdata_t* pd);
char *desc;
};
int sdd653_uvconfig_patch(FILE *f, patchdata_t* pd) {
// 0x28081: riva128 vendor id (word)
patch_word(f, 0x28081, pd->vendorid);
// 0x6ADD8: riva128 vendor id (word)
patch_word(f, 0x6ADD8, pd->vendorid);
// 0x6ADDA: riva128 device id (word)
patch_word(f, 0x6ADDA, pd->deviceid);
if ((pd->vendorid < 0x80) && (read_byte(f, 0x2805E) != 0x66)) {
// short vendor ID
// 0x2807F: riva128 device id (BYTE)
patch_byte(f, 0x2807F, pd->vendorid & 0xFF);
} else {
// long vendor ID - do beeeeg patch (disables NV1 support)
// 0x2805C: 74 -> EB
// 0x2805E: C4 5E 06 26 C7 47 02 01 00 -> 66 68 <device id dword> EB 1A 90
// 0x2807D: 66 6A <device id lobyte> -> EB DF 90
uint8_t patch1[] = {0x66, 0x68, 0x00, 0x00, 0x00, 0x00, 0xEB, 0x1A, 0x90};
uint8_t patch2[] = {0xEB, 0xDF, 0x90};
patch1[2] = (pd->deviceid >> 0) & 0xFF;
patch1[3] = (pd->deviceid >> 8) & 0xFF;
patch_byte(f, 0x2805C, 0xEB);
patch_bytes(f, 0x2805E, patch1, sizeof(patch1));
patch_bytes(f, 0x2807D, patch2, sizeof(patch2));
}
// 0x2AFBD: 7C -> EB : skip VESA info string check
patch_byte(f, 0x2AFBD, 0xEB);
// SDD 6.53 code uses sequencer for unlocking Riva128 extended registers,
// while TNT+ have the same unlock in CRTC
// example: UNIVBE.DRV 0x4568: mov dx, 0x3C4 / mov ax, 0x5706 / out dx, ax
// apparently NV BIOS up to GF2MX(?) unlocks extended regs if VESA 0x4F07 is invoked,
// so UNIVBE driver seems to work fine. on the other hand, GF4MX/FX BIOS locks extregs
// before returning to the caller, and SDD is stuck with broken scrolling and "out of range"
// so patch it out
uint32_t nvlock_port_patch[] = {
0x4C228, 0x4C31C, 0x4C34E, 0x4C374, 0x4C506, 0x4C8C6, 0x4C901, 0x4C934, 0x5CFCC, 0x5D0F6
};
uint32_t nvlock_data_patch[] = {
0x4C22B, 0x4C31F, 0x4C351, 0x4C377, 0x4C509, 0x4C8C9, 0x4C904, 0x4C937, 0x5CFC8, 0x5D0F2
};
patch_word_list(f, nvlock_port_patch, arrayof(nvlock_port_patch), 0x03D4);
patch_word_list(f, nvlock_data_patch, arrayof(nvlock_data_patch), 0x571F);
// patch xtal frequency
patch_dword(f, 0x1BA71, (pd->xtal_freq << 16) / 1000);
// TODO:
return 0; // success
}
int sdd67_uvconfig_patch(FILE *f, patchdata_t* pd) {
if (pd->deviceid <= 0x20) {
// riva 128/TNT don't need to be patched
printf("patch not required, skip\n");
return 1;
}
// 0x6B2EE: tnt2 device id (word)
patch_word(f, 0x6B2EE, pd->deviceid);
if ((pd->vendorid < 0x80) && (read_byte(f, 0x2999A) != 0x66)) {
// short vendor ID
// 0x2807F: tnt2 device id (BYTE)
patch_byte(f, 0x2807F, pd->vendorid & 0xFF);
} else {
// long vendor ID - do beeeeg patch (disables TNT support iirc)
// 0x29998: 74 -> EB
// 0x2999A: C4 5E 06 26 C7 47 02 03 00 -> 66 68 <device id dword> EB 1A 90
// 0x299B9: 66 6A <device id lobyte> -> EB DF 90
uint8_t patch1[] = {0x66, 0x68, 0x00, 0x00, 0x00, 0x00, 0xEB, 0x1A, 0x90};
uint8_t patch2[] = {0xEB, 0xDF, 0x90};
patch1[2] = (pd->deviceid >> 0) & 0xFF;
patch1[3] = (pd->deviceid >> 8) & 0xFF;
patch_byte(f, 0x29998, 0xEB);
patch_bytes(f, 0x2999A, patch1, sizeof(patch1));
patch_bytes(f, 0x299B9, patch2, sizeof(patch2));
}
// 0x2CAA2: 7C -> EB : skip VESA info string check
patch_byte(f, 0x2CAA2, 0xEB);
// patch xtal frequency
patch_dword(f, 0x29BA0, (pd->xtal_freq << 16) / 1000);
return 0; // success
}
int sdd653_config_patch(FILE *f, patchdata_t* pd) {
// 0x314F7: riva128 device id (dword)
patch_dword(f, 0x314F7, pd->deviceid);
// 0x314FC: riva128 vendor id (dword)
patch_dword(f, 0x314FC, pd->vendorid);
// 0x9FDB4: riva128 vendor id (word)
patch_word(f, 0x9FDB4, pd->vendorid);
// 0x9FDB6: riva128 device id (word)
patch_word(f, 0x9FDB6, pd->deviceid);
// 0x33D4E: 7C -> EB : skip VESA info string check
patch_byte(f, 0x33D4E, 0xEB);
// patch xtal frequency
patch_dword(f, 0x2673F, (pd->xtal_freq << 16) / 1000);
return 0;
}
int nv4vdd_patch(FILE *f, patchdata_t* pd) {
// TODO: try to find where and why fonts are corrupted and fix it
// TODO2: still does not work (memory size mismatch?)
// vendor/device ID patch
if (pd->deviceid < 0x30) {
// all TNT/TNT2 are supported out of the box, incl. Vanta/M64, skip patching
printf("patch not required, skip\n");
return 1;
}
// 0x37CFB: tnt device id (dword)
patch_word(f, 0x37CFB, pd->deviceid);
// patch xtal frequency
if (pd->xtal_freq == 27000) {
patch_dword(f, 0x13F19, pd->xtal_freq);
patch_dword(f, 0x2E95D, pd->xtal_freq);
}
return 0;
}
filescan_t filescan[] = {
// SDD 6.53 UVCONFIG.EXE
{"UVCONFIG.EXE", 451936, 0x31EA5, 0x54696353, sdd653_uvconfig_patch, "SDD 6.53 UVCONFIG.EXE"},
// SDD 6.53 CONFIG.EXE
{"CONFIG.EXE", 668095, 0x9AB8D, 0x54696353, sdd653_config_patch, "SDD 6.53 CONFIG.EXE"},
// SDD 6.7 UVCONFIG.EXE
{"UVCONFIG.EXE", 457724, 0x3AF2F, 0x54696353, sdd67_uvconfig_patch, "SDD 6.7 UVCONFIG.EXE"},
// Win3.x driver NV4VDD.VXD
{"NV4VDD.386", 241271, 0x2A140, 0x45D8908B, nv4vdd_patch, "Win3.x driver NV4VDD.386"},
{0}
};
int main() {
printf("NVidia DOS/Win3.x drivers patch utility - wbcbz7 24.o3.2o23\n");
// TODO: command line arguments
// find graphics devices..
pciDeviceList pcilist;
pciClass scanClass = {-1, 0, 0, 3}; // VGA device
if (tinypci::enumerateByClass(&pcilist, 1, scanClass) == 0) {
printf("error: unable to enumerate PCI devices!\n");
return 1;
}
if (pcilist.vendorId != 0x10DE) {
printf("error: no NVidia display controllers available!\n");
return 1;
}
// print info:
printf("PCI device: %02X:%02X.%01X = %04X:%04X\n",
pcilist.address.bus, pcilist.address.device, pcilist.address.function,
pcilist.vendorId, pcilist.deviceId);
patchdata_t patchdata = {&pcilist, pcilist.vendorId, pcilist.deviceId, getXtalFreq() };
printf("XTAL frequency: %d.%03d MHz\n", patchdata.xtal_freq / 1000, patchdata.xtal_freq % 1000);
printf("--------------------------\n");
// scan all files
FILE *f = NULL;
filescan_t *fscan = filescan;
uint32_t total_patched = 0;
for (; fscan->desc != 0; fscan++, total_patched++) {
printf("patch %s...", fscan->desc); fflush(stdout);
f = fopen(fscan->filename, "r+b");
if (f == NULL) {
printf("not found\n", fscan->filename);
continue;
}
// check file size
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
if (fsize != fscan->fsize) {
printf("size mismatch (%d != %d), skip\n", fsize, fscan->fsize);
fclose(f);
continue;
}
// check signature
if (read_dword(f, fscan->match_ofs) != fscan->match_sig) {
printf("signature mismatch, skip\n");
fclose(f);
continue;
}
// patch it!
if ((fscan->patchfunc != NULL) && (fscan->patchfunc(f, &patchdata)) == 0) {
printf("ok\n");
}
fclose(f);
}
if (total_patched == 0) {
printf("nothing to patch\n");
} else {
printf("done\n");
}
return 0;
}