This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathab.go
More file actions
205 lines (172 loc) · 5.56 KB
/
Copy pathab.go
File metadata and controls
205 lines (172 loc) · 5.56 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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"hash/crc32"
"os"
)
const (
ABMetadataMiscPartitionOffset = 2048
MiscbufSize = 2080
ABMagic = "\000AB0"
ABMagicLen = 4
ABMajorVersion = 1
ABMinorVersion = 0
ABDataSize = 32
ABMaxPriority = 15
ABMaxTriesRemaining = 7
MiscDevicePath = "/dev/misc"
)
type ABSlotData struct {
Priority uint8 `json:"priority"`
TriesRemaining uint8 `json:"tries_remaining"`
SuccessfulBoot uint8 `json:"successful_boot"`
Reserved [1]uint8 `json:"-"`
}
type ABData struct {
Magic [ABMagicLen]uint8 `json:"-"`
VersionMajor uint8 `json:"version_major"`
VersionMinor uint8 `json:"version_minor"`
Reserved1 [2]uint8 `json:"-"`
Slots [2]ABSlotData `json:"slots"`
Reserved2 [12]uint8 `json:"-"`
CRC32 uint32 `json:"crc32"`
}
func (info *ABData) Validate() bool {
if !bytes.Equal(info.Magic[:], []byte(ABMagic)) {
fmt.Printf("Magic %s is incorrect.\n", string(info.Magic[:]))
return false
}
if info.VersionMajor > ABMajorVersion {
fmt.Printf("No support for given major version.\n")
return false
}
return true
}
func (info *ABData) Reset() {
*info = ABData{}
copy(info.Magic[:], ABMagic)
info.VersionMajor = ABMajorVersion
info.VersionMinor = ABMinorVersion
info.Slots[0].Priority = ABMaxPriority
info.Slots[0].TriesRemaining = ABMaxTriesRemaining
info.Slots[1].Priority = ABMaxPriority - 1
info.Slots[1].TriesRemaining = ABMaxTriesRemaining
}
func (info *ABData) DumpInfo() {
activeSlot := info.GetActiveSlot()
activeSlotLetter := "A"
if activeSlot == 1 {
activeSlotLetter = "B"
}
fmt.Printf("active slot number: %d\n", activeSlot)
fmt.Printf("active slot letter: %s\n\n", activeSlotLetter)
fmt.Printf("slot a priority = %d\n", info.Slots[0].Priority)
fmt.Printf("slot a tries_remaining = %d\n", info.Slots[0].TriesRemaining)
fmt.Printf("slot a successful_boot = %d\n\n", info.Slots[0].SuccessfulBoot)
fmt.Printf("slot b priority = %d\n", info.Slots[1].Priority)
fmt.Printf("slot b tries_remaining = %d\n", info.Slots[1].TriesRemaining)
fmt.Printf("slot b successful_boot = %d\n", info.Slots[1].SuccessfulBoot)
}
func (info *ABData) GetActiveSlot() int {
if info.Slots[0].Priority > info.Slots[1].Priority {
return 0
}
return 1
}
func (info *ABData) SetActiveSlot(slot int) {
otherSlot := 1 - slot
info.Slots[slot].Priority = ABMaxPriority
info.Slots[slot].TriesRemaining = ABMaxTriesRemaining
info.Slots[slot].SuccessfulBoot = 0
if info.Slots[otherSlot].Priority == ABMaxPriority {
info.Slots[otherSlot].Priority = ABMaxPriority - 1
}
}
func (info *ABData) Failover() {
newSlot := 1 - info.GetActiveSlot()
fmt.Printf("Failing over to slot %d...\n", newSlot)
info.SetActiveSlot(newSlot)
}
func (info *ABData) SetSuccessfulBoot(slot int) {
info.Slots[slot].TriesRemaining = ABMaxTriesRemaining
info.Slots[slot].SuccessfulBoot = 1
}
func (info *ABData) calculateCRC32() uint32 {
data := make([]byte, ABDataSize-4)
binary.BigEndian.PutUint32(data[len(data)-4:], 0)
copy(data, info.Magic[:])
return crc32.ChecksumIEEE(data)
}
func OpenAndLoadABData() (*ABData, error) {
file, err := os.OpenFile(MiscDevicePath, os.O_RDWR, 0)
if err != nil {
return nil, fmt.Errorf("failed to open misc partition: %v", err)
}
defer file.Close()
miscBuf := make([]byte, MiscbufSize)
if _, err := file.ReadAt(miscBuf, 0); err != nil {
return nil, fmt.Errorf("failed to read misc partition: %v", err)
}
info := &ABData{}
abData := miscBuf[ABMetadataMiscPartitionOffset : ABMetadataMiscPartitionOffset+ABDataSize]
if err := binary.Read(bytes.NewReader(abData), binary.BigEndian, info); err != nil {
return nil, fmt.Errorf("failed to parse AB data: %v", err)
}
if !info.Validate() {
return nil, fmt.Errorf("invalid AB data")
}
return info, nil
}
func (info *ABData) Save() error {
info.CRC32 = info.calculateCRC32()
file, err := os.OpenFile(MiscDevicePath, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("failed to open misc partition: %v", err)
}
defer file.Close()
miscBuf := make([]byte, MiscbufSize)
if _, err := file.ReadAt(miscBuf, 0); err != nil {
return fmt.Errorf("failed to read misc partition: %v", err)
}
buf := &bytes.Buffer{}
if err := binary.Write(buf, binary.BigEndian, info); err != nil {
return fmt.Errorf("failed to serialize AB data: %v", err)
}
copy(miscBuf[ABMetadataMiscPartitionOffset:], buf.Bytes())
if _, err := file.WriteAt(miscBuf, 0); err != nil {
return fmt.Errorf("failed to write misc partition: %v", err)
}
return nil
}
func (info *ABData) DumpJSON() error {
type JSONOutput struct {
ActiveSlot int `json:"active_slot"`
ActiveSlotLetter string `json:"active_slot_letter"`
VersionMajor uint8 `json:"version_major"`
VersionMinor uint8 `json:"version_minor"`
Slots [2]ABSlotData `json:"slots"`
CRC32 uint32 `json:"crc32"`
}
activeSlot := info.GetActiveSlot()
activeSlotLetter := "A"
if activeSlot == 1 {
activeSlotLetter = "B"
}
output := JSONOutput{
ActiveSlot: activeSlot,
ActiveSlotLetter: activeSlotLetter,
VersionMajor: info.VersionMajor,
VersionMinor: info.VersionMinor,
Slots: info.Slots,
CRC32: info.CRC32,
}
jsonData, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("failed to format JSON: %v", err)
}
fmt.Println(string(jsonData))
return nil
}