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
|
/* reloads an nvidia card's 0xc0000 bios with a valid copy for execution
* (all done in a copy of system memory, no bios overwriting involved)
*
* portions of code below taken from Nouveau's nv_bios.c and nvclock
* and are copyright their respective authors
*/
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "x86emu.h"
#define Bool bool
#define TRUE true
#define FALSE false
#define ScrnInfoPtr void *
#define NV_PROM_SIZE 0x10000
static uint32_t *PMC;
static uint32_t *PRAMIN;
static uint8_t *PROM;
static uint32_t *PNV50;
static void *map_dev_mem (int fd, unsigned long Base, unsigned long Size)
{
void *base;
int mapflags = MAP_SHARED;
unsigned long realBase, alignOff;
realBase = Base & ~(getpagesize() - 1);
alignOff = Base - realBase;
base = mmap((caddr_t)0, Size + alignOff, PROT_READ|PROT_WRITE,
mapflags, fd, (off_t)realBase);
return (void *) ((char *)base + alignOff);
}
static void unmap_dev_mem (unsigned long Base, unsigned long Size)
{
unsigned long alignOff = Base - (Base & ~(getpagesize() - 1));
munmap((caddr_t)(Base - alignOff), (Size + alignOff));
}
static Bool nv_cksum(const uint8_t *data, unsigned int offset, unsigned int length)
{
/* there's a few checksums in the BIOS, so here's a generic checking function */
int i;
uint8_t sum = 0;
for (i = 0; i < length; i++)
sum += data[offset + i];
if (sum)
return TRUE;
return FALSE;
}
static Bool NVValidVBIOS(ScrnInfoPtr pScrn, const uint8_t *data)
{
/* check for BIOS signature */
if (!(data[0] == 0x55 && data[1] == 0xAA)) {
printf("... BIOS signature not found\n");
return FALSE;
}
if (nv_cksum(data, 0, data[2] * 512)) {
printf("... BIOS checksum invalid\n");
return FALSE;
} else
printf( "... appears to be valid\n");
return TRUE;
}
static void NVShadowVBIOS_PROM(ScrnInfoPtr pScrn, uint8_t *data)
{
int i;
printf("Attempting to locate BIOS image in PROM");
/* enable ROM access */
PMC[0x1850 / 4] = 0x0;
for (i = 0; i < NV_PROM_SIZE; i++) {
/* according to nvclock, we need that to work around a 6600GT/6800LE bug */
data[i] = PROM[i];
data[i] = PROM[i];
data[i] = PROM[i];
data[i] = PROM[i];
data[i] = PROM[i];
}
/* disable ROM access */
PMC[0x1850 / 4] = 0x1;
}
static void NVShadowVBIOS_PRAMIN(ScrnInfoPtr pScrn, uint32_t *data)
{
const uint32_t *pramin = PRAMIN;
int arch = PMC[0] >> 20 & 0x1ff;
uint32_t old_bar0_pramin = 0;
printf("Attempting to locate BIOS image in PRAMIN");
if (arch > 0x50) {
uint32_t vbios_vram;
vbios_vram = (PNV50[0x9f04/4] & ~0xff) << 8;
if (!vbios_vram) {
vbios_vram = PMC[0x1700/4] << 16;
vbios_vram += 0xf0000;
}
old_bar0_pramin = PMC[0x1700/4];
PMC[0x1700/4] = vbios_vram >> 16;
}
memcpy(data, pramin, NV_PROM_SIZE);
if (arch > 0x50)
PMC[0x1700/4] = old_bar0_pramin;
}
static Bool NVShadowVBIOS(ScrnInfoPtr pScrn, uint32_t *data)
{
NVShadowVBIOS_PROM(pScrn, (uint8_t *)data);
if (NVValidVBIOS(pScrn, (uint8_t *)data))
return TRUE;
NVShadowVBIOS_PRAMIN(pScrn, data);
if (NVValidVBIOS(pScrn, (uint8_t *)data))
return TRUE;
return FALSE;
}
int fake_bios(int fd, uint8_t *bios)
{
uint8_t *emumem, *sysmem;
int i;
/* this does not get free'd until exit */
if (!(emumem = malloc(1024 * 1024))) {
printf("Couldn't allocate fake system memory\n");
return 1;
}
sysmem = map_dev_mem(fd, 0, 1024*1024);
for (i = 0; i < 1024*1024; i++)
emumem[i] = sysmem[i];
for (i = 0; i < NV_PROM_SIZE; i++)
emumem[0xc0000 + i] = bios[i];
unmap_dev_mem((unsigned long)sysmem, 1024*1024);
M.mem_base = (uintptr_t)emumem;
return 0;
}
int reload_nv_bios(uintptr_t pcimemaddr)
{
int fd;
uint8_t bios[NV_PROM_SIZE];
if( (fd = open("/dev/mem", O_RDWR)) == -1 ) {
printf("Can't open /dev/mem\n");
return 0;
}
printf("Using card memory region at 0x%x\n", pcimemaddr);
PMC = map_dev_mem(fd, pcimemaddr + 0x000000, 0xffff);
PRAMIN = map_dev_mem(fd, pcimemaddr + 0x700000, NV_PROM_SIZE);
PROM = map_dev_mem(fd, pcimemaddr + 0x300000, NV_PROM_SIZE);
PNV50 = map_dev_mem(fd, pcimemaddr + 0x610000, 0xffff);
if (NVShadowVBIOS(NULL, (uint32_t *)bios)) {
if (fake_bios(fd, bios))
return 1;
} else
return 1;
close(fd);
unmap_dev_mem((unsigned long)PMC, NV_PROM_SIZE);
unmap_dev_mem((unsigned long)PRAMIN, NV_PROM_SIZE);
unmap_dev_mem((unsigned long)PROM, NV_PROM_SIZE);
unmap_dev_mem((unsigned long)PNV50, 0xffff);
return 0;
}
|