forked from klauspost/ryzen-master-vbs-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (119 loc) · 3.76 KB
/
main.go
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
/*
The MIT License (MIT)
Copyright (c) 2020 Klaus Post
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/klauspost/cpuid/v2"
)
var patterns = []struct {
desc string
search, replace []byte
}{
{
desc: "Ryzen Master v.1.5 -> v2.2",
search: []byte{0x44, 0x39, 0x6D, 0xA8, 0x0F, 0x84, 0xF7},
replace: []byte{0x44, 0x39, 0x6D, 0xA8, 0x90, 0xe9, 0xf7},
},
{
desc: "Ryzen Master v2.3 -> 2.6.0",
search: []byte{0x44, 0x39, 0xad, 0xf8, 0, 0, 0, 0x0f, 0x84},
replace: []byte{0x44, 0x39, 0xad, 0xf8, 0, 0, 0, 0x90, 0xe9},
},
{
desc: "Ryzen Master v2.6.2 -> ?",
search: []byte{0x8D, 0x48, 0xFA, 0x83, 0xF9, 0x01, 0x0F, 0x87},
// mov edi, eax
// lea ecx, [rax-6]
// cmp ecx, 1
// ja loc_14001BF3A
replace: []byte{0x8D, 0x48, 0xFA, 0x83, 0xF9, 0x01, 0x90, 0xe9},
// mov edi, eax
// lea ecx, [rax-6]
// cmp ecx, 1
// nop
// jmp loc_14001BF3A
},
{
desc: "Ryzen Master Threadripper",
search: []byte{0x00, 0x39, 0x7D, 0x90, 0x0F, 0x84, 0xE8, 0x00},
replace: []byte{0x00, 0x39, 0x7D, 0x90, 0x90, 0xE9, 0xE8, 0x00},
},
{
desc: "Ryzen Master 1000 series CPU",
search: []byte{0x72, 0x62, 0x01, 0x00, 0x39, 0x74, 0x24, 0x68, 0x0f, 0x84},
replace: []byte{0x72, 0x62, 0x01, 0x00, 0x39, 0x74, 0x24, 0x68, 0x90, 0xe9},
},
}
func init() {
flag.Usage = func() {
fmt.Println("Usage: ryzen-master-vbs-patch [-p=patched] \"AMD Ryzen Master.exe\"")
flag.PrintDefaults()
}
}
var prefix = flag.String("p", "patched-", `Specify prefix for output file. Set to "", overwrite input.`)
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
args = []string{"AMD Ryzen Master.exe"}
}
for _, f := range args {
dir, file := filepath.Split(f)
err := patch(filepath.Join(dir, file), filepath.Join(dir, *prefix+file))
switch err {
case nil:
default:
fmt.Printf("\nError: %v", err)
os.Exit(1)
}
}
}
var errCannotPatch = errors.New("no byte sequence not found")
func patch(in string, out string) error {
if cpuid.CPU.BrandName != "" {
fmt.Printf("* Host CPU: %s\n\n", cpuid.CPU.BrandName)
}
fmt.Printf("Reading %q\n\n", in)
b, err := ioutil.ReadFile(in)
if err != nil {
return err
}
for i, pattern := range patterns {
c := bytes.Count(b, pattern.search)
fmt.Printf("%d. Checking patch for %q. ", i, pattern.desc)
fmt.Println("Matching byte sequences:", c, "(should be 1)")
if c != 1 {
if c = bytes.Count(b, pattern.replace); c > 0 {
fmt.Println(" -> Found", c, "replacement pattern(s). Is this already patched?\n")
}
continue
}
b = bytes.Replace(b, pattern.search, pattern.replace, -1)
fmt.Printf("Success! Writing to %q\n", out)
return ioutil.WriteFile(out, b, os.ModePerm)
}
return errCannotPatch
}