1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# pyOCD debugger
# Copyright (c) 2020 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# 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 __future__ import print_function
import argparse
import sys
import traceback
import logging
import tempfile
import platform
from pyocd.core.helpers import ConnectHelper
from pyocd.probe.pydapaccess import DAPAccess
from pyocd.utility.mask import round_up_div
from pyocd.utility import conversion
from pyocd.core.memory_map import MemoryType
from pyocd.commands.execution_context import CommandExecutionContext
from pyocd.commands import commands
from test_util import (
Test,
TestResult,
get_session_options,
get_target_test_params,
binary_to_hex_file,
binary_to_elf_file,
get_test_binary_path,
)
class CommandsTestResult(TestResult):
def __init__(self):
super(CommandsTestResult, self).__init__(None, None, None)
self.name = "commands"
class CommandsTest(Test):
def __init__(self):
super(CommandsTest, self).__init__("Commands Test", commands_test)
def run(self, board):
try:
result = self.test_function(board.unique_id)
except Exception as e:
result = CommandsTestResult()
result.passed = False
print("Exception %s when testing board %s" % (e, board.unique_id))
traceback.print_exc(file=sys.stdout)
result.board = board
result.test = self
return result
def fix_windows_path(path: str) -> str:
"""Double backslashes in paths on Windows."""
if platform.system() == "Windows":
return path.replace('\\', '\\\\')
else:
return path
def commands_test(board_id):
with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session:
board = session.board
target = session.target
target_type = board.target_type
test_params = get_target_test_params(session)
session.probe.set_clock(test_params['test_clock'])
memory_map = board.target.get_memory_map()
boot_region = memory_map.get_boot_memory()
ram_region = memory_map.get_default_region_of_type(MemoryType.RAM)
ram_base = ram_region.start
boot_start_addr = boot_region.start
boot_end_addr = boot_region.end
boot_blocksize = boot_region.blocksize
binary_file = get_test_binary_path(board.test_binary)
# Generate an Intel hex file from the binary test file.
temp_test_hex_name = binary_to_hex_file(binary_file, boot_region.start)
temp_bin_file = tempfile.mktemp('.bin')
with open(binary_file, "rb") as f:
test_data = list(bytearray(f.read()))
test_data_length = len(test_data)
test_file_sectors = round_up_div(test_data_length, boot_blocksize)
boot_first_free_block_addr = boot_start_addr + test_file_sectors * boot_blocksize
reset_handler_addr = conversion.byte_list_to_u32le_list(test_data[4:8])[0]
test_pass_count = 0
test_count = 0
failed_commands = []
result = CommandsTestResult()
context = CommandExecutionContext()
context.attach_session(session)
COMMANDS_TO_TEST = [
"status",
"reset",
"reset halt",
"reg",
"reg general",
"reg all",
"reg r0",
"wreg r0 0x12345678",
"d pc",
"d --center pc 32",
"read64 0x%08x" % ((boot_start_addr + boot_blocksize) & ~7),
"read32 0x%08x" % ((boot_start_addr + boot_blocksize) & ~3),
"read16 0x%08x" % ((boot_start_addr + boot_blocksize) & ~1),
"read8 0x%08x" % (boot_start_addr + boot_blocksize),
"rd 0x%08x 16" % ram_base,
"rw 0x%08x 16" % ram_base,
"rh 0x%08x 16" % ram_base,
"rb 0x%08x 16" % ram_base,
"write64 0x%08x 0x1122334455667788 0xaabbccddeeff0011" % ram_base,
"write32 0x%08x 0x11223344 0x55667788" % ram_base,
"write16 0x%08x 0xabcd" % (ram_base + 8),
"write8 0x%08x 0 1 2 3 4 5 6" % (ram_base + 10),
"savemem 0x%08x 128 '%s'" % (boot_start_addr, fix_windows_path(temp_bin_file)),
"loadmem 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
"loadmem 0x%08x '%s'" % (boot_start_addr, fix_windows_path(binary_file)),
"load '%s'" % fix_windows_path(temp_test_hex_name),
"load '%s' 0x%08x" % (fix_windows_path(binary_file), boot_start_addr),
"compare 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
"compare 0x%08x 32 '%s'" % (ram_base, fix_windows_path(temp_bin_file)),
"fill 0x%08x 128 0xa5" % ram_base,
"fill 16 0x%08x 64 0x55aa" % (ram_base + 64),
"find 0x%08x 128 0xaa 0x55" % ram_base, # find that will pass
"find 0x%08x 128 0xff" % ram_base, # find that will fail
"find -n 0x%08x 128 0xff" % ram_base, # inverted find that will now pass
"erase 0x%08x" % (boot_first_free_block_addr),
"erase 0x%08x 1" % (boot_first_free_block_addr + boot_blocksize),
"go",
"halt",
"step",
"s 4",
"continue",
"h",
"break 0x%08x" % reset_handler_addr,
"lsbreak",
"rmbreak 0x%08x" % reset_handler_addr,
"watch 0x%08x" % ram_base,
"lswatch",
"rmwatch 0x%08x" % ram_base,
"watch 0x%08x rw 2" % ram_base,
"rmwatch 0x%08x" % ram_base,
"core",
"core 0",
"readdp 0", # read DPIDR
"writedp 0 0x1e", # write ABORT to clear error flags
"readap 0xfc", # read IDR
"readap 0 0xfc", # read IDR of AP#0
"writeap 0x4 0", # set TAR to 0
"writeap 0 0x4 0", # set TAR to 0 on AP#0
"gdbserver start",
"gdbserver status",
"gdbserver stop",
"probeserver start",
"probeserver status",
"probeserver stop",
"show probe-uid",
"show target",
"show cores",
"show map",
"show peripherals",
"show fault",
"show nreset",
"set nreset 1",
"show option reset_type",
"set option reset_type=sw",
"show mem-ap",
"set mem-ap 0",
"show hnonsec",
"set hnonsec 0",
"show hprot",
"set hprot 0x3", # set default hprot: data, priv
"show graph",
"show locked",
"show register-groups",
"show vector-catch",
"set vector-catch all",
"show step-into-interrupts",
"set step-into-interrupts 1",
"set log info",
"set frequency %d" % test_params['test_clock'],
# Semicolon-separated commands.
'rw 0x%08x ; rw 0x%08x' % (ram_base, ram_base + 4),
'rb 0x%08x;rb 0x%08x' % (ram_base, ram_base + 1),
'rb 0x%08x; rb 0x%08x' % (ram_base, ram_base + 1),
# Python and system commands.
'$2+ 2',
'$ target',
'!echo hello',
'!echo hi ; echo there', # semicolon in a sytem command (because semicolon separation is not supported for Python/system command lines)
' $ " ".join(["hello", "there"])',
' ! echo "yo dude" ',
# Commands not tested:
# "list",
# "erase", # chip erase
# "unlock",
# "exit",
# "initdp",
# "makeap",
# "reinit",
# "where",
# "symbol",
]
# For now we just verify that the commands run without raising an exception.
print("\n------ Testing commands ------")
def test_command(cmd, print_result=True):
try:
print("\nTEST: %s" % cmd)
context.process_command_line(cmd)
except Exception:
if print_result:
print("TEST FAILED")
failed_commands.append(cmd)
traceback.print_exc(file=sys.stdout)
return False
else:
if print_result:
print("TEST PASSED")
return True
for cmd in COMMANDS_TO_TEST:
if test_command(cmd):
test_pass_count += 1
test_count += 1
#
# Special command tests.
#
print("\n------ Additional command tests ------")
# set option
session.options['auto_unlock'] = False
if test_command('set option auto_unlock=on', print_result=False):
if session.options['auto_unlock'] is True:
test_pass_count += 1
print("TEST PASSED")
else:
print("TEST FAILED")
test_count += 1
print("\n\nTest Summary:")
print("Pass count %i of %i tests" % (test_pass_count, test_count))
if failed_commands:
for c in failed_commands:
print(" - '" + c + "'")
if test_pass_count == test_count:
print("COMMANDS TEST SCRIPT PASSED")
else:
print("COMMANDS TEST SCRIPT FAILED")
target.reset()
result.passed = test_count == test_pass_count
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='pyOCD commands test')
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
parser.add_argument('-u', '--uid', help='Debug probe unique ID')
parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
args = parser.parse_args()
level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(level=level)
DAPAccess.set_args(args.daparg)
commands_test(args.uid)
|