Initial commit

This commit is contained in:
Dominic Höglinger 2025-07-01 21:35:26 +00:00
commit b04dc85f36
4 changed files with 299 additions and 0 deletions

170
.gitignore vendored Normal file
View File

@ -0,0 +1,170 @@
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2025 dominic
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# IAPy: NXP IAP Python module for GDB
The purpose of this module is to patch in manual calls to the NXP IAP via GDB,
for when your programming adapter of choice will for whatever reason not cooperate.
Currently only sector erasing or calling the IAP directly is implemented.
It is highly recommended to overfeed the watchdog and set the system clock to something stable like the FRO.
Good luck, you'll need it.
## How to use
```gdb
# load IAPy
source iapy.py
# erase sectors
py iap_flash_sector_erase(0, 3)
# load image as usual
load
```
# Credits
- [LPC54113 programming via OpenOCD](https://www.dlbeer.co.nz/articles/lpc54113_openocd/index.html) by Daniel Beer

94
iapy.py Normal file
View File

@ -0,0 +1,94 @@
import gdb
import time
# get the current inferior, i.e. the connection to target
inferior = gdb.selected_inferior()
def target_u32read(address, endian='little'):
value = inferior.read_memory(address, 4)
return int.from_bytes(value, byteorder=endian)
def target_u32write(address, value, endian='little'):
data = value.to_bytes(4, byteorder=endian) # Adjust byteorder as needed
inferior.write_memory(address, data)
def target_regset(name, value):
gdb.execute(f'set ${name} = {value}')
def target_prepare():
"""
Prepares the target for IAP operations.
This implementation is an example for the LPC546xx series of chips.
"""
LPC546XX_WDT_PERIOD_MAX = 0xFFFFFF
LPC546XX_WDT_PROTECT = (1 << 4)
LPC546XX_MAINCLKSELA = 0x40000280
LPC546XX_MAINCLKSELB = 0x40000284
LPC546XX_AHBCLKDIV = 0x40000380
LPC546XX_FLASHCFG = 0x40000400
gdb.execute('interrupt')
# check if watchdog timer's on
wdt_mode = target_u32read(LPC546XX_WDT_MODE)
# if WDT on, we can't disable it, but we may be able to set a long period
if (wdt_mode and not(wdt_mode & LPC546XX_WDT_PROTECT)):
target_u32write(LPC546XX_WDT_CNT, LPC546XX_WDT_PERIOD_MAX)
# SYSCLK: 12MHz FRO
target_u32write(LPC546XX_MAINCLKSELA, 0)
# use MAINCLKSELA
target_u32write(LPC546XX_MAINCLKSELB, 0)
# div by 1
target_u32write(LPC546XX_AHBCLKDIV, 0)
# recommended default configuration
target_u32write(LPC546XX_FLASHCFG, 0x1A)
def iap_command(a0, a1=0, a2=0, a3=0, a4=0):
gdb.execute('interrupt')
# setup request
target_u32write(0x2000ffa0, a0)
target_u32write(0x2000ffa4, a1)
target_u32write(0x2000ffa8, a2)
target_u32write(0x2000ffac, a3)
target_u32write(0x2000ffb0, a4)
# clear result
target_u32write(0x2000ffc0, 0xAAAAAAAA)
target_u32write(0x2000ffc4, 0xAAAAAAAA)
target_u32write(0x2000ffc8, 0xAAAAAAAA)
target_u32write(0x2000ffcc, 0xAAAAAAAA)
target_u32write(0x2000ffd0, 0xAAAAAAAA)
# call setup
target_regset('lr', 0x00020001)
target_regset('r0', 0x2000ffa0)
target_regset('r1', 0x2000ffc0)
target_regset('pc', 0x03000205)
target_regset('primask', 1)
target_regset('msp', 0x2000ffa0)
# execute and wait
gdb.execute('continue')
time.sleep(0.5)
# gather results
res0 = target_u32read(0x2000ffc0)
res1 = target_u32read(0x2000ffc4)
res2 = target_u32read(0x2000ffc8)
res3 = target_u32read(0x2000ffcc)
res4 = target_u32read(0x2000ffd0)
return res0,res1,res2,res3,res4
def iap_flash_sector_erase(start_sector, end_sector, sysclk=12000):
print(f"IAPy: Erasing sectors {start_sector} to {end_sector}")
target_prepare()
# prepare sectors
iap_command(50, start_sector, end_sector)
# erase sectors
iap_command(52, start_sector, end_sector, sysclk)
gdb.execute('interrupt')