Skip to content

Commit

Permalink
flash: Add flash patching recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
neuschaefer committed Sep 22, 2023
1 parent 481e179 commit 340bddb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions flash/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bin
34 changes: 34 additions & 0 deletions flash/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
all: flash-mon.bin flash-ecos-mon.bin flash-ecos-mon-double.bin

../monitor/monitor.bin:
+$(MAKE) -C ../monitor

monitor.bin: ../monitor/monitor.bin
cp ../monitor/monitor.bin .

boot2-ecos.bin: flash-dump.bin
../tools/extract-boot2.py $< $@

boot2-ecos-mon.bin: boot2-ecos.bin monitor.bin
cp $< $@
../tools/patch-ecos.py $@ monitor.bin

flash-mon.bin: flash-dump.bin monitor.bin
cp $< $@
@chmod +w $@
../tools/inject-boot2.py $@ monitor.bin

flash-ecos-mon.bin: flash-dump.bin boot2-ecos-mon.bin
cp $< $@
@chmod +w $@
../tools/inject-boot2.py $@ boot2-ecos-mon.bin

flash-ecos-mon-double.bin: flash-dump.bin boot2-ecos-mon.bin
cp $< $@
@chmod +w $@
../tools/inject-boot2.py $@ boot2-ecos-mon.bin boot2-ecos-mon.bin

clean:
rm -f monitor.bin boot2-ecos.bin boot2-ecos-mon.bin flash-mon.bin flash-ecos-mon.bin

.PHONY: clean
15 changes: 15 additions & 0 deletions flash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Flash modification recipes

Place your flash dump at `flash-dump.bin` and then just type `make` :-)

```
flash-dump.bin original flash dump
| | ../monitor/monitor.bin monitor program
| | v |
| `---> flash-mon.bin | flash image with monitor as boot2
| |
|----> boot2-ecos.bin v original eCos-based boot2
| `----> boot2-ecos-mon.bin eCos patched to jump into monitor instead of app
v |
flash-ecos-mon.bin <-' flash image with patched eCos
```
30 changes: 30 additions & 0 deletions tools/extract-boot2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import argparse, lzma

def extract(args):
with open(args.image, 'rb') as f:
data = f.read()
a = lzma.decompress(data[0x20100:])
b = lzma.decompress(data[0x720100:])

if not args.recovery and a != b:
print('Warning: main and recovery copies of boot2 are different! Extracting main only.')

with open(args.main, 'wb') as f:
f.write(a)
f.close()

if args.recovery:
with open(args.recovery, 'wb') as f:
f.write(b)
f.close()


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Extract boot2')
parser.add_argument('image', help='flash image')
parser.add_argument('main', help='filename of resulting program (main @ 0x20100)')
parser.add_argument('recovery', help='filename of resulting program (recovery @ 0x720100)', nargs='?')
args = parser.parse_args()
extract(args)

0 comments on commit 340bddb

Please sign in to comment.