-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
113 lines (101 loc) · 3.35 KB
/
Copy pathdecoder.go
File metadata and controls
113 lines (101 loc) · 3.35 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
package flashdecoder
import (
"errors"
"flashDecoder/flashids"
"flashDecoder/flashs"
flashinfo "flashDecoder/info"
"flashDecoder/utils"
"fmt"
"regexp"
"strings"
)
type Decoder interface {
Check(partNumber string) bool
Decode(partNumber string) flashinfo.Flashinfo
}
type IdDecoder interface {
Check(id []byte) bool
Decode(partNumber string) flashinfo.Flashinfo
}
var VendorIds = map[byte]IdDecoder{}
var flashdecoders []Decoder
var flashIdDecoders []IdDecoder
func init() {
flashdecoders = append(flashdecoders, flashs.MicronDecoderDefault())
flashdecoders = append(flashdecoders, flashs.MicronFpgaDecoderDefault())
flashdecoders = append(flashdecoders, flashs.IntelDecoderDefault())
flashdecoders = append(flashdecoders, flashs.KioxiaDecoderDefault())
flashdecoders = append(flashdecoders, flashs.PhisonDecoderDefault())
flashdecoders = append(flashdecoders, flashs.SamsungDecoderDefault())
flashdecoders = append(flashdecoders, flashs.SpecTekDecoderDefault())
flashdecoders = append(flashdecoders, flashs.WesternDigitalShortCodeDecoderDefault())
flashdecoders = append(flashdecoders, flashs.WesternDigitalDecoderDefault())
flashdecoders = append(flashdecoders, flashs.SkHynix3DDecoderDefault())
flashdecoders = append(flashdecoders, flashs.SkHynixLegacyDecoderDefault())
flashdecoders = append(flashdecoders, flashs.SkHynixDecoderDefault())
flashdecoders = append(flashdecoders, flashs.YangTzeDecoderDefault())
flashIdDecoders = append(flashIdDecoders, flashids.BasicIDDecoderDefault())
mdb, _ := utils.LoadMdb()
if mdb.Micron == nil {
mdb.Micron = utils.Micron
}
utils.Mdb = mdb
}
func Decode(partNumber string) (flashinfo.Flashinfo, error) {
partNumber = strings.ToUpper(partNumber)
if strings.TrimSpace(partNumber) == "" {
return flashinfo.Flashinfo{}, errors.New("empty partNumber")
}
for _, v := range flashdecoders {
if v.Check(partNumber) {
info := v.Decode(partNumber)
if info.Retry {
info.Retry = false
return Decode(info.PartNumber)
} else {
if info.Unsupported_Reason == "" {
return info, nil
} else {
return info, errors.New(info.Unsupported_Reason)
}
}
}
}
return flashinfo.Flashinfo{}, errors.New("no Support flash partNumber found")
}
func DecodeID(id string) (flashinfo.Flashinfo, error) {
normalized, bytes, err := normalizeFlashID(id)
if err != nil {
return flashinfo.Flashinfo{}, err
}
for _, decoder := range flashIdDecoders {
if decoder.Check(bytes) {
info := decoder.Decode(normalized)
if info.Unsupported_Reason == "" {
return info, nil
}
return info, errors.New(info.Unsupported_Reason)
}
}
return flashinfo.Flashinfo{}, fmt.Errorf("no flash id decoder matched: %s", normalized)
}
func normalizeFlashID(raw string) (string, []byte, error) {
cleaned := strings.ToUpper(strings.TrimSpace(raw))
if cleaned == "" {
return "", nil, errors.New("empty flash id")
}
hexOnly := regexp.MustCompile(`[^0-9A-F]`).ReplaceAllString(cleaned, "")
if len(hexOnly) < 2 || len(hexOnly)%2 != 0 {
return "", nil, fmt.Errorf("invalid flash id: %s", raw)
}
bytes := make([]byte, 0, len(hexOnly)/2)
for offset := 0; offset < len(hexOnly); offset += 2 {
var value byte
_, parseErr := fmt.Sscanf(hexOnly[offset:offset+2], "%02X", &value)
if parseErr != nil {
return "", nil, fmt.Errorf("invalid flash id: %s", raw)
}
bytes = append(bytes, value)
}
return hexOnly, bytes, nil
}