52f7ab63ba8ca84e0971ad46f8457630f0684eac
[reactos.git] / reactos / base / applications / utils / patchnv4 / patchnv4.c
1 /* $Id$
2 *
3 * Patch the NVidia miniport driver to work with ReactOS
4 *
5 * Should become obsolete
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 struct Patch
12 {
13 long Offset;
14 unsigned char ExpectedValue;
15 unsigned char NewValue;
16 };
17
18 static struct Patch Patches[ ] =
19 {
20 { 0x1EBA9, 0x30, 0x3C },
21 { 0x1EBAA, 0xC0, 0xF0 },
22 { 0x1EC0B, 0x04, 0x01 },
23 { 0x1EC67, 0x30, 0x3C },
24 { 0x1EC68, 0xC0, 0xF0 }
25 };
26
27 int
28 main(int argc, char *argv[])
29 {
30 static char OriginalName[] = "nv4_mini.sys";
31 static char TempName[] = "nv4_mini.tmp";
32 static char BackupName[] = "nv4_mini.sys.orig";
33 FILE *File;
34 unsigned char *Buffer;
35 long Size;
36 unsigned n;
37
38 /* Read the whole file in memory */
39 File = fopen(OriginalName, "rb");
40 if (NULL == File)
41 {
42 perror("Unable to open original file");
43 exit(1);
44 }
45 if (fseek(File, 0, SEEK_END))
46 {
47 perror("Unable to determine file length");
48 fclose(File);
49 exit(1);
50 }
51 Size = ftell(File);
52 if (-1 == Size)
53 {
54 perror("Unable to determine file length");
55 fclose(File);
56 exit(1);
57 }
58 Buffer = malloc(Size);
59 if (NULL == Buffer)
60 {
61 perror("Can't allocate buffer");
62 fclose(File);
63 exit(1);
64 }
65 rewind(File);
66 if (Size != fread(Buffer, 1, Size, File))
67 {
68 perror("Error reading from original file");
69 free(Buffer);
70 fclose(File);
71 exit(1);
72 }
73 fclose(File);
74
75 /* Patch the file */
76 for (n = 0; n < sizeof(Patches) / sizeof(struct Patch); n++)
77 {
78 if (Buffer[Patches[n].Offset] != Patches[n].ExpectedValue)
79 {
80 fprintf(stderr, "Expected value 0x%02x at offset 0x%lx but found 0x%02x\n",
81 Patches[n].ExpectedValue, Patches[n].Offset,
82 Buffer[Patches[n].Offset]);
83 free(Buffer);
84 exit(1);
85 }
86 Buffer[Patches[n].Offset] = Patches[n].NewValue;
87 }
88
89 /* Write the new file */
90 File = fopen(TempName, "wb");
91 if (NULL == File)
92 {
93 perror("Unable to open output file");
94 free(Buffer);
95 exit(1);
96 }
97 if (Size != fwrite(Buffer, 1, Size, File))
98 {
99 perror("Error writing to output file");
100 fclose(File);
101 remove(TempName);
102 free(Buffer);
103 exit(1);
104 }
105 fclose(File);
106 free(Buffer);
107
108 /* Rename the original file, removing an existing backup */
109 remove(BackupName);
110 if (0 != rename(OriginalName, BackupName))
111 {
112 perror("Failed to rename original file");
113 remove(TempName);
114 exit(1);
115 }
116
117 /* Rename the new file */
118 if (0 != rename(TempName, OriginalName))
119 {
120 perror("Failed to rename new file");
121 remove(TempName);
122 rename(BackupName, OriginalName);
123 exit(1);
124 }
125
126 return 0;
127 }