Allow dumping of MS2130 rom code (using special firmware)

This commit is contained in:
Bertold Van den Bergh 2023-11-01 02:40:46 +01:00
parent d05392d22a
commit 9866c7b404
4 changed files with 50 additions and 25 deletions

View file

@ -23,7 +23,6 @@ type dumpCodeParams struct {
addrLoad int addrLoad int
addrHook int addrHook int
valueHook byte valueHook byte
delay time.Duration
} }
func (d *DumpROM) Run(c *Context) error { func (d *DumpROM) Run(c *Context) error {
@ -44,7 +43,6 @@ func (d *DumpROM) Run(c *Context) error {
p.addrLoad = 0xC800 p.addrLoad = 0xC800
p.addrHook = 8 p.addrHook = 8
p.valueHook = 1 p.valueHook = 1
p.delay = 25 * time.Millisecond
} else if strings.Contains(devType, "MS2109") { } else if strings.Contains(devType, "MS2109") {
p.addrMailbox = 0xCBF0 p.addrMailbox = 0xCBF0
p.addrTemp = 0xD300 p.addrTemp = 0xD300
@ -52,6 +50,13 @@ func (d *DumpROM) Run(c *Context) error {
p.addrLoad = 0xCC20 p.addrLoad = 0xCC20
p.addrHook = 4 p.addrHook = 4
p.valueHook = 1 << 2 p.valueHook = 1 << 2
} else if strings.Contains(devType, "MS2130") {
p.addrMailbox = 0x7C00
p.addrTemp = 0x7D00
p.addrTempLen = 256
p.addrHook = 0x7D00
p.addrHook = 8
p.valueHook = 1
} else { } else {
return mshal.ErrorUnknownDevice return mshal.ErrorUnknownDevice
} }
@ -109,9 +114,11 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
time.Sleep(time.Second) time.Sleep(time.Second)
/* Write new code */ /* Write new code */
if p.addrLoad > 0 {
if _, err := xdata.Access(true, p.addrLoad, dumpBlob); err != nil { if _, err := xdata.Access(true, p.addrLoad, dumpBlob); err != nil {
return nil, err return nil, err
} }
}
/* Enable USB/Periodic hook */ /* Enable USB/Periodic hook */
if err := mshal.WriteByte(config, p.addrHook, p.valueHook); err != nil { if err := mshal.WriteByte(config, p.addrHook, p.valueHook); err != nil {
@ -142,16 +149,23 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
return nil, err return nil, err
} }
time.Sleep(p.delay) timeout := time.Now().Add(500 * time.Millisecond)
for {
ack, err := mshal.ReadByte(xdata, p.addrMailbox) ack, err := mshal.ReadByte(xdata, p.addrMailbox)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if ack != 0 { if ack != 0 {
if time.Now().After(timeout) {
return nil, mshal.ErrorPatchFailed return nil, mshal.ErrorPatchFailed
} }
} else {
break
}
time.Sleep(20 * time.Millisecond)
}
_, err = xdata.Access(false, p.addrTemp, buf[index:(index+remaining)]) _, err = xdata.Access(false, p.addrTemp, buf[index:(index+remaining)])
if err != nil { if err != nil {
@ -163,9 +177,6 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
fmt.Printf("Dumping code: %d bytes read.\n", addr) fmt.Printf("Dumping code: %d bytes read.\n", addr)
} }
/* Remove overwritten code from dump */
buf = bytes.ReplaceAll(buf, dumpBlob, orig)
/* Disable USB hook */ /* Disable USB hook */
if err := mshal.WriteByte(config, p.addrHook, 0); err != nil { if err := mshal.WriteByte(config, p.addrHook, 0); err != nil {
return nil, err return nil, err
@ -175,9 +186,14 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
time.Sleep(25 * time.Millisecond) time.Sleep(25 * time.Millisecond)
/* Put original code back */ /* Put original code back */
if p.addrLoad > 0 {
/* Remove overwritten code from dump */
buf = bytes.ReplaceAll(buf, dumpBlob, orig)
if _, err := xdata.Access(true, p.addrLoad, orig); err != nil { if _, err := xdata.Access(true, p.addrLoad, orig); err != nil {
return nil, err return nil, err
} }
}
/* Re-enable old hooks */ /* Re-enable old hooks */
_, err = config.Access(true, 0, configOld) _, err = config.Access(true, 0, configOld)

View file

@ -1,4 +0,0 @@
sudo ./cli write RAM 0xc190 77
sudo ./cli write RAM 0xc191 01
sudo ./cli write RAM 0xc190 0x77

Binary file not shown.

View file

@ -14,7 +14,7 @@ func calcSum(f []byte) uint16 {
return csum return csum
} }
func CheckImage(f []byte) error { func work(f []byte, fix bool) error {
if len(f) < 0x30+4 { if len(f) < 0x30+4 {
return errors.New("file too short (hdr)") return errors.New("file too short (hdr)")
} }
@ -32,11 +32,24 @@ func CheckImage(f []byte) error {
hdrSum := calcSum(f[2:12]) + calcSum(f[16:0x30]) hdrSum := calcSum(f[2:12]) + calcSum(f[16:0x30])
codeSum := calcSum(f[0x30:end]) codeSum := calcSum(f[0x30:end])
if !fix {
if hdrImg := binary.BigEndian.Uint16(f[end:]); hdrSum != hdrImg { if hdrImg := binary.BigEndian.Uint16(f[end:]); hdrSum != hdrImg {
return fmt.Errorf("header checksum mismatch: %x != %x", hdrSum, hdrImg) return fmt.Errorf("header checksum mismatch: %x != %x", hdrSum, hdrImg)
} else if codeImg := binary.BigEndian.Uint16(f[end+2:]); codeSum != codeImg { } else if codeImg := binary.BigEndian.Uint16(f[end+2:]); codeSum != codeImg {
return fmt.Errorf("code checksum mismatch: %x != %x", codeSum, codeImg) return fmt.Errorf("code checksum mismatch: %x != %x", codeSum, codeImg)
} }
} else {
binary.BigEndian.PutUint16(f[end:], hdrSum)
binary.BigEndian.PutUint16(f[end+2:], codeSum)
}
return nil return nil
} }
func CheckImage(f []byte) error {
return work(f, false)
}
func FixImage(f []byte) {
work(f, true)
}