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
addrHook int
valueHook byte
delay time.Duration
}
func (d *DumpROM) Run(c *Context) error {
@ -44,7 +43,6 @@ func (d *DumpROM) Run(c *Context) error {
p.addrLoad = 0xC800
p.addrHook = 8
p.valueHook = 1
p.delay = 25 * time.Millisecond
} else if strings.Contains(devType, "MS2109") {
p.addrMailbox = 0xCBF0
p.addrTemp = 0xD300
@ -52,6 +50,13 @@ func (d *DumpROM) Run(c *Context) error {
p.addrLoad = 0xCC20
p.addrHook = 4
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 {
return mshal.ErrorUnknownDevice
}
@ -109,9 +114,11 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
time.Sleep(time.Second)
/* Write new code */
if p.addrLoad > 0 {
if _, err := xdata.Access(true, p.addrLoad, dumpBlob); err != nil {
return nil, err
}
}
/* Enable USB/Periodic hook */
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
}
time.Sleep(p.delay)
timeout := time.Now().Add(500 * time.Millisecond)
for {
ack, err := mshal.ReadByte(xdata, p.addrMailbox)
if err != nil {
return nil, err
}
if ack != 0 {
if time.Now().After(timeout) {
return nil, mshal.ErrorPatchFailed
}
} else {
break
}
time.Sleep(20 * time.Millisecond)
}
_, err = xdata.Access(false, p.addrTemp, buf[index:(index+remaining)])
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)
}
/* Remove overwritten code from dump */
buf = bytes.ReplaceAll(buf, dumpBlob, orig)
/* Disable USB hook */
if err := mshal.WriteByte(config, p.addrHook, 0); err != nil {
return nil, err
@ -175,9 +186,14 @@ func (d *DumpROM) work(ms *mshal.HAL, p dumpCodeParams) ([]byte, error) {
time.Sleep(25 * time.Millisecond)
/* 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 {
return nil, err
}
}
/* Re-enable old hooks */
_, 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
}
func CheckImage(f []byte) error {
func work(f []byte, fix bool) error {
if len(f) < 0x30+4 {
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])
codeSum := calcSum(f[0x30:end])
if !fix {
if hdrImg := binary.BigEndian.Uint16(f[end:]); hdrSum != hdrImg {
return fmt.Errorf("header checksum mismatch: %x != %x", hdrSum, hdrImg)
} else if codeImg := binary.BigEndian.Uint16(f[end+2:]); 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
}
func CheckImage(f []byte) error {
return work(f, false)
}
func FixImage(f []byte) {
work(f, true)
}