Checksumming and USB port searching works
This commit is contained in:
77
firmware/firmware.go
Normal file
77
firmware/firmware.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package firmware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
// FwExists check if file exists
|
||||
func FwExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
}
|
||||
|
||||
func ChkHeader(fwfile string) bool {
|
||||
fmt.Println("checking ", fwfile)
|
||||
// SCS has two different file layouts, one for the "Dragons", the other for any older modem
|
||||
var offset uint32
|
||||
if path.Ext(fwfile) == ".dr7" {
|
||||
offset = 0
|
||||
} else {
|
||||
offset = 4
|
||||
}
|
||||
r, err := os.Open(fwfile)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
var header [2]byte
|
||||
_, err = io.ReadFull(r, header[:])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if offset == 0 {
|
||||
if header != [2]byte{0x50, 0x34} {
|
||||
fmt.Println("ERROR: wrong firmware header")
|
||||
//return false
|
||||
}
|
||||
} else {
|
||||
if header != [2]byte{0x60, 0xea} {
|
||||
fmt.Println("ERROR: wrong firmware header")
|
||||
//return false
|
||||
}
|
||||
}
|
||||
|
||||
r.Close()
|
||||
firmware, err := ioutil.ReadFile(fwfile)
|
||||
if err != nil {
|
||||
fmt.Println("ERROR: ReadFile operation failed")
|
||||
}
|
||||
|
||||
var size uint32
|
||||
if offset == 0 {
|
||||
size = binary.LittleEndian.Uint32(firmware[4:8]) // Dragons
|
||||
} else {
|
||||
size = uint32(binary.LittleEndian.Uint16(firmware[2:4])) // PTC-3 and Co
|
||||
}
|
||||
a := make([]byte, 4)
|
||||
//binary.LittleEndian.PutUint32(a, crc32.ChecksumIEEE(firmware[4:size+4])) // PTC-3
|
||||
binary.LittleEndian.PutUint32(a, crc32.ChecksumIEEE(firmware[offset:offset+size]))
|
||||
//if bytes.Equal(a, firmware[4+size:8+size]) { // PTC-3
|
||||
if bytes.Equal(a, firmware[offset+size:offset+size+4]) {
|
||||
fmt.Println("CRC checksum ok!")
|
||||
} else {
|
||||
fmt.Println("CRC checksum NOT ok!")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
Reference in New Issue
Block a user