-
Notifications
You must be signed in to change notification settings - Fork 131
Add unlock IDCODE support using indirect API #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||
# Copyright 2025 Jeremiah Gillis | ||||
# | ||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
# | ||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||
# | ||||
# Unless required by applicable law or agreed to in writing, software | ||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
|
||||
from .. import enums | ||||
from .. import errors | ||||
|
||||
import ctypes | ||||
|
||||
# Global variable to store the ID code | ||||
global_id_code = None | ||||
|
||||
|
||||
def set_unlock_idcode(self, id_code): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename the |
||||
"""Sets the J-Link unlock ``IDCODE`` and enables function redirect. This is | ||||
only supported by certain devices such as Renesas. | ||||
|
||||
Args: | ||||
self (JLink): the ``JLink`` instance | ||||
id_code (str): ``IDCODE`` to unlock debug access in hexadecimal format | ||||
|
||||
Returns: | ||||
``None`` | ||||
|
||||
Raises: | ||||
ValueError: if ``id_code`` is not a hexadecimal string. | ||||
JLinkException: if function is not found. | ||||
""" | ||||
try: | ||||
int(id_code, 16) | ||||
except ValueError: | ||||
raise ValueError('id_code must be a hexadecimal string.') | ||||
|
||||
global global_id_code | ||||
global_id_code = id_code | ||||
|
||||
self._dll.JLINK_GetpFunc.restype = ctypes.c_void_p | ||||
self.unlock_idcode_cb = enums.JLinkFunctions.UNLOCK_IDCODE_HOOK_PROTOTYPE(unlock_idcode_hook_dialog) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to bind the callback to the passed instance? It looks like we pass the value to the |
||||
function_ptr = self._dll.JLINK_GetpFunc(enums.JLinkIndirectFunctionIndex.SET_HOOK_DIALOG_UNLOCK_IDCODE) | ||||
if not function_ptr: | ||||
raise errors.JLinkException('Could not find Set Hook Dialog Unlock IDCODE function.') | ||||
|
||||
callback_override = ctypes.CFUNCTYPE(None, ctypes.c_void_p) | ||||
function = ctypes.cast(function_ptr, callback_override) | ||||
function(self.unlock_idcode_cb) | ||||
return | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
NIT: No need for the |
||||
|
||||
|
||||
def unlock_idcode_hook_dialog(title, msg, flags, id_code, max_num_bytes): | ||||
"""Unlocks debug access using J-Link IDCODE hook. | ||||
|
||||
Args: | ||||
title (str): title of the unlock id code dialog | ||||
msg (str): text of the unlock id code dialog | ||||
flags (int): flags specifying which values can be returned | ||||
id_code (void pointer): buffer pointer to store IDCODE | ||||
max_num_bytes (int): maximum number of bytes that can be written to IDCODE buffer | ||||
|
||||
Returns: | ||||
``enums.JLinkFlags.DLG_BUTTON_OK`` | ||||
""" | ||||
global global_id_code | ||||
code_bytes = bytes.fromhex(global_id_code) | ||||
data = ctypes.cast(id_code, ctypes.POINTER(ctypes.c_byte * max_num_bytes)) | ||||
data.contents[:min(len(code_bytes), max_num_bytes)] = code_bytes | ||||
|
||||
return enums.JLinkFlags.DLG_BUTTON_OK |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2025 Jeremiah Gillis | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pylink.enums as enums | ||
from pylink.errors import JLinkException | ||
import pylink.unlockers as unlock | ||
import ctypes | ||
|
||
import mock | ||
|
||
import unittest | ||
|
||
|
||
class TestUnlockIDCODE(unittest.TestCase): | ||
"""Tests the `unlock_idcode` submodule.""" | ||
|
||
def setUp(self): | ||
"""Called before each test. | ||
|
||
Performs setup. | ||
|
||
Args: | ||
self (TestUnlockIDCODE): the `TestUnlockIDCODE` instance | ||
|
||
Returns: | ||
`None` | ||
""" | ||
pass | ||
|
||
def tearDown(self): | ||
"""Called after each test. | ||
|
||
Performs teardown. | ||
|
||
Args: | ||
self (TestUnlockIDCODE): the `TestUnlockIDCODE` instance | ||
|
||
Returns: | ||
`None` | ||
""" | ||
pass | ||
|
||
def test_set_unlock_idcode_errors(self): | ||
"""Tests calling `set_unlock_idcode()` for errors. | ||
|
||
Args: | ||
self (TestUnlockIDCODE): the `TestUnlockIDCODE` instance | ||
|
||
Returns: | ||
`None` | ||
""" | ||
jlink = mock.Mock() | ||
|
||
with self.assertRaises(ValueError): | ||
unlock.set_unlock_idcode(jlink, '') | ||
|
||
with self.assertRaises(ValueError): | ||
unlock.set_unlock_idcode(jlink, '00112233445566778899AABBCCDDEEHH') | ||
|
||
jlink._dll.JLINK_GetpFunc.return_value = None | ||
with self.assertRaises(JLinkException): | ||
unlock.set_unlock_idcode(jlink, '00112233445566778899AABBCCDDEEFF') | ||
|
||
def test_set_unlock_idcode(self): | ||
"""Tests calling `set_unlock_idcode()` for success. | ||
|
||
Args: | ||
self (TestUnlockIDCODE): the `TestUnlockIDCODE` instance | ||
|
||
Returns: | ||
`None` | ||
""" | ||
jlink = mock.Mock() | ||
|
||
def dummy_function(address): | ||
return | ||
|
||
ptr_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p) | ||
jlink._dll.JLINK_GetpFunc.return_value = ptr_type(dummy_function) | ||
result = unlock.set_unlock_idcode(jlink, '00112233445566778899AABBCCDDEEFF') | ||
self.assertIsNone(result) | ||
|
||
id_code_t = ctypes.c_byte * 16 | ||
id_code = id_code_t() | ||
id_code_p = ctypes.cast(ctypes.pointer(id_code), ctypes.c_void_p) | ||
result = jlink.unlock_idcode_cb(ctypes.c_char_p('Test'.encode('utf-8')), | ||
ctypes.c_char_p('Unlock IDCODE'.encode('utf-8')), | ||
4, | ||
id_code_p, | ||
16) | ||
self.assertEqual(result, enums.JLinkFlags.DLG_BUTTON_OK) | ||
self.assertEqual(bytes(id_code).hex().upper(), '00112233445566778899AABBCCDDEEFF') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A global won't play well with multiple instances (e.g. if someone is debugging two chips at the same time). It looks like we need it for the callback in the dialog. Could we pass a partial function instead? That would allow us to prepopulate the
id_code
: