Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
test/test_daplink.py@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | # |
Pawel Zarembski |
0:01f31e923fe2 | 2 | # DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 3 | # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 4 | # SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 5 | # |
Pawel Zarembski |
0:01f31e923fe2 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 7 | # not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 8 | # You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 9 | # |
Pawel Zarembski |
0:01f31e923fe2 | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 11 | # |
Pawel Zarembski |
0:01f31e923fe2 | 12 | # Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 15 | # See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 16 | # limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 17 | # |
Pawel Zarembski |
0:01f31e923fe2 | 18 | |
Pawel Zarembski |
0:01f31e923fe2 | 19 | from __future__ import absolute_import |
Pawel Zarembski |
0:01f31e923fe2 | 20 | import future |
Pawel Zarembski |
0:01f31e923fe2 | 21 | import os |
Pawel Zarembski |
0:01f31e923fe2 | 22 | import binascii |
Pawel Zarembski |
0:01f31e923fe2 | 23 | import intelhex |
Pawel Zarembski |
0:01f31e923fe2 | 24 | import sys |
Pawel Zarembski |
0:01f31e923fe2 | 25 | |
Pawel Zarembski |
0:01f31e923fe2 | 26 | try: |
Pawel Zarembski |
0:01f31e923fe2 | 27 | from cStringIO import StringIO |
Pawel Zarembski |
0:01f31e923fe2 | 28 | except ImportError: |
Pawel Zarembski |
0:01f31e923fe2 | 29 | from io import StringIO |
Pawel Zarembski |
0:01f31e923fe2 | 30 | |
Pawel Zarembski |
0:01f31e923fe2 | 31 | from msd_test import (MassStorageTester, MOCK_DIR_LIST, MOCK_FILE_LIST, |
Pawel Zarembski |
0:01f31e923fe2 | 32 | MOCK_DIR_LIST_AFTER, MOCK_FILE_LIST_AFTER) |
Pawel Zarembski |
0:01f31e923fe2 | 33 | |
Pawel Zarembski |
0:01f31e923fe2 | 34 | TRIGGER_ASSERT_FILE_NAME = "ASSERT.ACT" |
Pawel Zarembski |
0:01f31e923fe2 | 35 | ASSERT_FILE_NAME = "ASSERT.TXT" |
Pawel Zarembski |
0:01f31e923fe2 | 36 | NEED_BL_FILE_NAME = "NEED_BL.TXT" |
Pawel Zarembski |
0:01f31e923fe2 | 37 | DAPLINK_VECTOR_TABLE_OFFSET = 0x00 |
Pawel Zarembski |
0:01f31e923fe2 | 38 | DAPLINK_BUILD_KEY_OFFSET = 0x20 |
Pawel Zarembski |
0:01f31e923fe2 | 39 | DAPLINK_HIC_ID_OFFSET = 0x24 |
Pawel Zarembski |
0:01f31e923fe2 | 40 | |
Pawel Zarembski |
0:01f31e923fe2 | 41 | |
Pawel Zarembski |
0:01f31e923fe2 | 42 | def intel_hex_get_sections(intel_hex): |
Pawel Zarembski |
0:01f31e923fe2 | 43 | """Return list of address, size tuples""" |
Pawel Zarembski |
0:01f31e923fe2 | 44 | addresses = intel_hex.addresses() |
Pawel Zarembski |
0:01f31e923fe2 | 45 | addresses.sort() |
Pawel Zarembski |
0:01f31e923fe2 | 46 | section_list = [] |
Pawel Zarembski |
0:01f31e923fe2 | 47 | section_start = None |
Pawel Zarembski |
0:01f31e923fe2 | 48 | last_addr = None |
Pawel Zarembski |
0:01f31e923fe2 | 49 | for addr in addresses: |
Pawel Zarembski |
0:01f31e923fe2 | 50 | if section_start is None: |
Pawel Zarembski |
0:01f31e923fe2 | 51 | section_start = addr |
Pawel Zarembski |
0:01f31e923fe2 | 52 | last_addr = addr |
Pawel Zarembski |
0:01f31e923fe2 | 53 | if addr == last_addr or addr == last_addr + 1: |
Pawel Zarembski |
0:01f31e923fe2 | 54 | last_addr = addr |
Pawel Zarembski |
0:01f31e923fe2 | 55 | else: |
Pawel Zarembski |
0:01f31e923fe2 | 56 | section_size = last_addr - section_start + 1 |
Pawel Zarembski |
0:01f31e923fe2 | 57 | section_list.append((section_start, section_size)) |
Pawel Zarembski |
0:01f31e923fe2 | 58 | section_start = addr |
Pawel Zarembski |
0:01f31e923fe2 | 59 | last_addr = addr |
Pawel Zarembski |
0:01f31e923fe2 | 60 | if section_start is not None: |
Pawel Zarembski |
0:01f31e923fe2 | 61 | section_size = last_addr - section_start + 1 |
Pawel Zarembski |
0:01f31e923fe2 | 62 | section_list.append((section_start, section_size)) |
Pawel Zarembski |
0:01f31e923fe2 | 63 | return section_list |
Pawel Zarembski |
0:01f31e923fe2 | 64 | |
Pawel Zarembski |
0:01f31e923fe2 | 65 | |
Pawel Zarembski |
0:01f31e923fe2 | 66 | def bin_data_to_hex_data(addr, data): |
Pawel Zarembski |
0:01f31e923fe2 | 67 | """Covert binary data to a string in intel hex format""" |
Pawel Zarembski |
0:01f31e923fe2 | 68 | intel_hex = intelhex.IntelHex() |
Pawel Zarembski |
0:01f31e923fe2 | 69 | if sys.version_info >= (3,0): |
Pawel Zarembski |
0:01f31e923fe2 | 70 | data = data.decode('latin1') |
Pawel Zarembski |
0:01f31e923fe2 | 71 | intel_hex.puts(addr, data) |
Pawel Zarembski |
0:01f31e923fe2 | 72 | sio = StringIO() |
Pawel Zarembski |
0:01f31e923fe2 | 73 | intel_hex.tofile(sio, format='hex') |
Pawel Zarembski |
0:01f31e923fe2 | 74 | hex_data = sio.getvalue() |
Pawel Zarembski |
0:01f31e923fe2 | 75 | return bytearray(hex_data.encode('latin1')) |
Pawel Zarembski |
0:01f31e923fe2 | 76 | |
Pawel Zarembski |
0:01f31e923fe2 | 77 | |
Pawel Zarembski |
0:01f31e923fe2 | 78 | class DLMassStorageTester(MassStorageTester): |
Pawel Zarembski |
0:01f31e923fe2 | 79 | """DAPLink mass storage tester""" |
Pawel Zarembski |
0:01f31e923fe2 | 80 | |
Pawel Zarembski |
0:01f31e923fe2 | 81 | def __init__(self, board, parent_test, test_name, test_mode): |
Pawel Zarembski |
0:01f31e923fe2 | 82 | super(DLMassStorageTester, self).__init__(board, parent_test, |
Pawel Zarembski |
0:01f31e923fe2 | 83 | test_name) |
Pawel Zarembski |
0:01f31e923fe2 | 84 | self._expected_mode = None |
Pawel Zarembski |
0:01f31e923fe2 | 85 | self._actual_mode = None |
Pawel Zarembski |
0:01f31e923fe2 | 86 | self._test_mode = test_mode |
Pawel Zarembski |
0:01f31e923fe2 | 87 | if self._test_mode == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 88 | self._crc_tag = board.KEY_BL_CRC |
Pawel Zarembski |
0:01f31e923fe2 | 89 | elif self._test_mode == board.MODE_BL: |
Pawel Zarembski |
0:01f31e923fe2 | 90 | self._crc_tag = board.KEY_IF_CRC |
Pawel Zarembski |
0:01f31e923fe2 | 91 | else: |
Pawel Zarembski |
0:01f31e923fe2 | 92 | assert False |
Pawel Zarembski |
0:01f31e923fe2 | 93 | |
Pawel Zarembski |
0:01f31e923fe2 | 94 | def _run(self, test_info): |
Pawel Zarembski |
0:01f31e923fe2 | 95 | assert self._expected_mode is not None |
Pawel Zarembski |
0:01f31e923fe2 | 96 | # Set board to the correct mode before running test |
Pawel Zarembski |
0:01f31e923fe2 | 97 | self.board.set_mode(self._test_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 98 | self._actual_mode = None |
Pawel Zarembski |
0:01f31e923fe2 | 99 | |
Pawel Zarembski |
0:01f31e923fe2 | 100 | super(DLMassStorageTester, self)._run(test_info) |
Pawel Zarembski |
0:01f31e923fe2 | 101 | |
Pawel Zarembski |
0:01f31e923fe2 | 102 | if self._actual_mode is None: |
Pawel Zarembski |
0:01f31e923fe2 | 103 | # Set expected mode if it hasn't been set in _check_data_correct |
Pawel Zarembski |
0:01f31e923fe2 | 104 | self._actual_mode = self.board.get_mode() |
Pawel Zarembski |
0:01f31e923fe2 | 105 | if self._expected_mode is not self._actual_mode: |
Pawel Zarembski |
0:01f31e923fe2 | 106 | test_info.failure("Wrong mode after test - Expected " |
Pawel Zarembski |
0:01f31e923fe2 | 107 | " %s got %s" % (self._expected_mode, |
Pawel Zarembski |
0:01f31e923fe2 | 108 | self._actual_mode)) |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | def set_expected_mode(self, mode): |
Pawel Zarembski |
0:01f31e923fe2 | 111 | self._expected_mode = mode |
Pawel Zarembski |
0:01f31e923fe2 | 112 | |
Pawel Zarembski |
0:01f31e923fe2 | 113 | def _check_data_correct(self, expected_data, test_info): |
Pawel Zarembski |
0:01f31e923fe2 | 114 | board = self.board |
Pawel Zarembski |
0:01f31e923fe2 | 115 | self._actual_mode = self.board.get_mode() |
Pawel Zarembski |
0:01f31e923fe2 | 116 | board.set_mode(self._test_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 117 | if self._crc_tag not in board.details_txt: |
Pawel Zarembski |
0:01f31e923fe2 | 118 | test_info.info("CRC not in details.txt") |
Pawel Zarembski |
0:01f31e923fe2 | 119 | return False |
Pawel Zarembski |
0:01f31e923fe2 | 120 | actual_crc32 = int(self.board.details_txt[self._crc_tag], 16) |
Pawel Zarembski |
0:01f31e923fe2 | 121 | expected_crc32 = binascii.crc32(expected_data[0:-4]) & 0xFFFFFFFF |
Pawel Zarembski |
0:01f31e923fe2 | 122 | test_info.info("Expected CRC: 0x%08x, actual crc: 0x%08x" % |
Pawel Zarembski |
0:01f31e923fe2 | 123 | (expected_crc32, actual_crc32)) |
Pawel Zarembski |
0:01f31e923fe2 | 124 | return actual_crc32 == expected_crc32 |
Pawel Zarembski |
0:01f31e923fe2 | 125 | |
Pawel Zarembski |
0:01f31e923fe2 | 126 | |
Pawel Zarembski |
0:01f31e923fe2 | 127 | def daplink_test(workspace, parent_test): |
Pawel Zarembski |
0:01f31e923fe2 | 128 | board = workspace.board |
Pawel Zarembski |
0:01f31e923fe2 | 129 | interface = workspace.if_firmware |
Pawel Zarembski |
0:01f31e923fe2 | 130 | test_info = parent_test.create_subtest('daplink_test') |
Pawel Zarembski |
0:01f31e923fe2 | 131 | |
Pawel Zarembski |
0:01f31e923fe2 | 132 | intel_hex = intelhex.IntelHex(interface.hex_path) |
Pawel Zarembski |
0:01f31e923fe2 | 133 | section_list = intel_hex_get_sections(intel_hex) |
Pawel Zarembski |
0:01f31e923fe2 | 134 | assert len(section_list) == 1, ("Only 1 section supported, found %s" % |
Pawel Zarembski |
0:01f31e923fe2 | 135 | len(section_list)) |
Pawel Zarembski |
0:01f31e923fe2 | 136 | start, length = section_list[0] |
Pawel Zarembski |
0:01f31e923fe2 | 137 | |
Pawel Zarembski |
0:01f31e923fe2 | 138 | bin_data = bytearray(intel_hex.tobinarray(start=start, size=length)) |
Pawel Zarembski |
0:01f31e923fe2 | 139 | sio = StringIO() |
Pawel Zarembski |
0:01f31e923fe2 | 140 | intel_hex.tofile(sio, format='hex') |
Pawel Zarembski |
0:01f31e923fe2 | 141 | hex_data = sio.getvalue() |
Pawel Zarembski |
0:01f31e923fe2 | 142 | hex_data = bytearray(hex_data.encode('latin1')) |
Pawel Zarembski |
0:01f31e923fe2 | 143 | |
Pawel Zarembski |
0:01f31e923fe2 | 144 | # Make sure asserts work as expected |
Pawel Zarembski |
0:01f31e923fe2 | 145 | test_assert(workspace, test_info) |
Pawel Zarembski |
0:01f31e923fe2 | 146 | |
Pawel Zarembski |
0:01f31e923fe2 | 147 | # Test loading a binary file with shutils |
Pawel Zarembski |
0:01f31e923fe2 | 148 | test = DLMassStorageTester(board, test_info, "Shutil binary file load " |
Pawel Zarembski |
0:01f31e923fe2 | 149 | "interface", board.MODE_BL) |
Pawel Zarembski |
0:01f31e923fe2 | 150 | test.set_shutils_copy(interface.bin_path) |
Pawel Zarembski |
0:01f31e923fe2 | 151 | test.set_expected_data(bin_data, start) |
Pawel Zarembski |
0:01f31e923fe2 | 152 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 153 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 154 | |
Pawel Zarembski |
0:01f31e923fe2 | 155 | # Test loading a hex file with shutils |
Pawel Zarembski |
0:01f31e923fe2 | 156 | test = DLMassStorageTester(board, test_info, "Shutil hex file load " |
Pawel Zarembski |
0:01f31e923fe2 | 157 | "interface", board.MODE_BL) |
Pawel Zarembski |
0:01f31e923fe2 | 158 | test.set_shutils_copy(interface.hex_path) |
Pawel Zarembski |
0:01f31e923fe2 | 159 | test.set_expected_data(bin_data, start) |
Pawel Zarembski |
0:01f31e923fe2 | 160 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 161 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 162 | |
Pawel Zarembski |
0:01f31e923fe2 | 163 | test_file_type('bin', board.MODE_BL, board, test_info, start, bin_data) |
Pawel Zarembski |
0:01f31e923fe2 | 164 | test_file_type('hex', board.MODE_BL, board, test_info, start, bin_data) |
Pawel Zarembski |
0:01f31e923fe2 | 165 | |
Pawel Zarembski |
0:01f31e923fe2 | 166 | # Test bootloader updates |
Pawel Zarembski |
0:01f31e923fe2 | 167 | firmware = workspace.bl_firmware |
Pawel Zarembski |
0:01f31e923fe2 | 168 | intel_hex = intelhex.IntelHex(firmware.hex_path) |
Pawel Zarembski |
0:01f31e923fe2 | 169 | section_list = intel_hex_get_sections(intel_hex) |
Pawel Zarembski |
0:01f31e923fe2 | 170 | assert len(section_list) == 1, ("Only 1 section supported, found %s" % |
Pawel Zarembski |
0:01f31e923fe2 | 171 | len(section_list)) |
Pawel Zarembski |
0:01f31e923fe2 | 172 | start, length = section_list[0] |
Pawel Zarembski |
0:01f31e923fe2 | 173 | |
Pawel Zarembski |
0:01f31e923fe2 | 174 | bin_data = bytearray(intel_hex.tobinarray(start=start, size=length)) |
Pawel Zarembski |
0:01f31e923fe2 | 175 | sio = StringIO() |
Pawel Zarembski |
0:01f31e923fe2 | 176 | intel_hex.tofile(sio, format='hex') |
Pawel Zarembski |
0:01f31e923fe2 | 177 | hex_data = sio.getvalue() |
Pawel Zarembski |
0:01f31e923fe2 | 178 | hex_data = bytearray(hex_data.encode('latin1')) |
Pawel Zarembski |
0:01f31e923fe2 | 179 | |
Pawel Zarembski |
0:01f31e923fe2 | 180 | # Test loading a binary file with shutils |
Pawel Zarembski |
0:01f31e923fe2 | 181 | test = DLMassStorageTester(board, test_info, "Shutil binary file load " |
Pawel Zarembski |
0:01f31e923fe2 | 182 | "bootloader", board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 183 | test.set_shutils_copy(firmware.bin_path) |
Pawel Zarembski |
0:01f31e923fe2 | 184 | test.set_expected_data(bin_data, start) |
Pawel Zarembski |
0:01f31e923fe2 | 185 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 186 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 187 | |
Pawel Zarembski |
0:01f31e923fe2 | 188 | # Test loading a hex file with shutils |
Pawel Zarembski |
0:01f31e923fe2 | 189 | test = DLMassStorageTester(board, test_info, "Shutil hex file load " |
Pawel Zarembski |
0:01f31e923fe2 | 190 | "bootloader", board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 191 | test.set_shutils_copy(firmware.hex_path) |
Pawel Zarembski |
0:01f31e923fe2 | 192 | test.set_expected_data(bin_data, start) |
Pawel Zarembski |
0:01f31e923fe2 | 193 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 194 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 195 | |
Pawel Zarembski |
0:01f31e923fe2 | 196 | test_file_type('bin', board.MODE_IF, board, test_info, start, bin_data) |
Pawel Zarembski |
0:01f31e923fe2 | 197 | test_file_type('hex', board.MODE_IF, board, test_info, start, bin_data) |
Pawel Zarembski |
0:01f31e923fe2 | 198 | |
Pawel Zarembski |
0:01f31e923fe2 | 199 | |
Pawel Zarembski |
0:01f31e923fe2 | 200 | def test_assert(workspace, parent_test): |
Pawel Zarembski |
0:01f31e923fe2 | 201 | """Test that asserts work and persist across modes""" |
Pawel Zarembski |
0:01f31e923fe2 | 202 | board = workspace.board |
Pawel Zarembski |
0:01f31e923fe2 | 203 | test_info = parent_test.create_subtest('Assert Test') |
Pawel Zarembski |
0:01f31e923fe2 | 204 | |
Pawel Zarembski |
0:01f31e923fe2 | 205 | board.set_assert_auto_manage(False) |
Pawel Zarembski |
0:01f31e923fe2 | 206 | |
Pawel Zarembski |
0:01f31e923fe2 | 207 | board.set_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 208 | |
Pawel Zarembski |
0:01f31e923fe2 | 209 | # Create a test assert file |
Pawel Zarembski |
0:01f31e923fe2 | 210 | test_info.info('Triggering assert by creating %s' % |
Pawel Zarembski |
0:01f31e923fe2 | 211 | TRIGGER_ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 212 | trigger_assert_path = board.get_file_path(TRIGGER_ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 213 | with open(trigger_assert_path, 'wb') as _: |
Pawel Zarembski |
0:01f31e923fe2 | 214 | pass |
Pawel Zarembski |
0:01f31e923fe2 | 215 | board.wait_for_remount(test_info) |
Pawel Zarembski |
0:01f31e923fe2 | 216 | |
Pawel Zarembski |
0:01f31e923fe2 | 217 | test_info.info('Checking that assert file was created') |
Pawel Zarembski |
0:01f31e923fe2 | 218 | board.set_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 219 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 220 | if not os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 221 | test_info.failure('Assert file not created') |
Pawel Zarembski |
0:01f31e923fe2 | 222 | |
Pawel Zarembski |
0:01f31e923fe2 | 223 | test_info.info('Checking that assert file persists if->bl') |
Pawel Zarembski |
0:01f31e923fe2 | 224 | board.set_mode(board.MODE_BL) |
Pawel Zarembski |
0:01f31e923fe2 | 225 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 226 | if not os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 227 | test_info.failure('Assert file not created') |
Pawel Zarembski |
0:01f31e923fe2 | 228 | |
Pawel Zarembski |
0:01f31e923fe2 | 229 | test_info.info('Checking that assert file persists bl->if') |
Pawel Zarembski |
0:01f31e923fe2 | 230 | board.set_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 231 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 232 | if not os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 233 | test_info.failure('Assert file not created') |
Pawel Zarembski |
0:01f31e923fe2 | 234 | |
Pawel Zarembski |
0:01f31e923fe2 | 235 | board.clear_assert() |
Pawel Zarembski |
0:01f31e923fe2 | 236 | |
Pawel Zarembski |
0:01f31e923fe2 | 237 | board.set_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 238 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 239 | if os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 240 | test_info.failure('Assert file not cleared correctly') |
Pawel Zarembski |
0:01f31e923fe2 | 241 | |
Pawel Zarembski |
0:01f31e923fe2 | 242 | board.set_mode(board.MODE_BL) |
Pawel Zarembski |
0:01f31e923fe2 | 243 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 244 | if os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 245 | test_info.failure('Assert file not cleared correctly') |
Pawel Zarembski |
0:01f31e923fe2 | 246 | |
Pawel Zarembski |
0:01f31e923fe2 | 247 | board.set_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 248 | assert_path = board.get_file_path(ASSERT_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 249 | if os.path.exists(assert_path): |
Pawel Zarembski |
0:01f31e923fe2 | 250 | test_info.failure('Assert file not cleared correctly') |
Pawel Zarembski |
0:01f31e923fe2 | 251 | |
Pawel Zarembski |
0:01f31e923fe2 | 252 | board.set_assert_auto_manage(True) |
Pawel Zarembski |
0:01f31e923fe2 | 253 | |
Pawel Zarembski |
0:01f31e923fe2 | 254 | |
Pawel Zarembski |
0:01f31e923fe2 | 255 | def test_file_type(file_type, board_mode, board, parent_test, |
Pawel Zarembski |
0:01f31e923fe2 | 256 | data_start, raw_data): |
Pawel Zarembski |
0:01f31e923fe2 | 257 | """Test updates of a given file type using the given mode""" |
Pawel Zarembski |
0:01f31e923fe2 | 258 | assert file_type in ('hex', 'bin'), 'Unsupported file type %s' % file_type |
Pawel Zarembski |
0:01f31e923fe2 | 259 | |
Pawel Zarembski |
0:01f31e923fe2 | 260 | if board_mode == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 261 | data_type = board.MODE_BL |
Pawel Zarembski |
0:01f31e923fe2 | 262 | elif board_mode == board.MODE_BL: |
Pawel Zarembski |
0:01f31e923fe2 | 263 | data_type = board.MODE_IF |
Pawel Zarembski |
0:01f31e923fe2 | 264 | else: |
Pawel Zarembski |
0:01f31e923fe2 | 265 | assert False |
Pawel Zarembski |
0:01f31e923fe2 | 266 | |
Pawel Zarembski |
0:01f31e923fe2 | 267 | test_info = parent_test.create_subtest('%s %s filetype test' % |
Pawel Zarembski |
0:01f31e923fe2 | 268 | (file_type, data_type)) |
Pawel Zarembski |
0:01f31e923fe2 | 269 | |
Pawel Zarembski |
0:01f31e923fe2 | 270 | def get_file_name(base='image'): |
Pawel Zarembski |
0:01f31e923fe2 | 271 | """Get the file name to be used for loading""" |
Pawel Zarembski |
0:01f31e923fe2 | 272 | return base + '.' + file_type |
Pawel Zarembski |
0:01f31e923fe2 | 273 | |
Pawel Zarembski |
0:01f31e923fe2 | 274 | def get_file_content(addr, bin_data): |
Pawel Zarembski |
0:01f31e923fe2 | 275 | """Get the file contents to be used for loading""" |
Pawel Zarembski |
0:01f31e923fe2 | 276 | if file_type == 'bin': |
Pawel Zarembski |
0:01f31e923fe2 | 277 | return bytearray(bin_data) # Make a copy |
Pawel Zarembski |
0:01f31e923fe2 | 278 | elif file_type == 'hex': |
Pawel Zarembski |
0:01f31e923fe2 | 279 | return bin_data_to_hex_data(addr, bin_data) |
Pawel Zarembski |
0:01f31e923fe2 | 280 | |
Pawel Zarembski |
0:01f31e923fe2 | 281 | # Test partial update |
Pawel Zarembski |
0:01f31e923fe2 | 282 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 283 | local_data = get_file_content(data_start, raw_data[0:len(raw_data) // 2]) |
Pawel Zarembski |
0:01f31e923fe2 | 284 | test = DLMassStorageTester(board, test_info, "Load partial", |
Pawel Zarembski |
0:01f31e923fe2 | 285 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 286 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 287 | test.set_expected_data(None) |
Pawel Zarembski |
0:01f31e923fe2 | 288 | test.set_expected_failure_msg("In application programming failed because " |
Pawel Zarembski |
0:01f31e923fe2 | 289 | "the update sent was incomplete.", "interface") |
Pawel Zarembski |
0:01f31e923fe2 | 290 | test.set_expected_mode(board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 291 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 292 | # If bootloader is missing then this should be indicated by a file |
Pawel Zarembski |
0:01f31e923fe2 | 293 | if board_mode == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 294 | if not os.path.isfile(board.get_file_path(NEED_BL_FILE_NAME)): |
Pawel Zarembski |
0:01f31e923fe2 | 295 | test_info.failure("Bootloader missing but file %s not present" % |
Pawel Zarembski |
0:01f31e923fe2 | 296 | NEED_BL_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 297 | test_info.info("Testing switch to bootloader") |
Pawel Zarembski |
0:01f31e923fe2 | 298 | try: |
Pawel Zarembski |
0:01f31e923fe2 | 299 | board.set_mode(board.MODE_BL) |
Pawel Zarembski |
0:01f31e923fe2 | 300 | test_info.failure("Board switched to bootloader mode") |
Pawel Zarembski |
0:01f31e923fe2 | 301 | except Exception: |
Pawel Zarembski |
0:01f31e923fe2 | 302 | pass |
Pawel Zarembski |
0:01f31e923fe2 | 303 | finally: |
Pawel Zarembski |
0:01f31e923fe2 | 304 | if board.get_mode() == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 305 | test_info.info("Device able to recover from bad BL") |
Pawel Zarembski |
0:01f31e923fe2 | 306 | else: |
Pawel Zarembski |
0:01f31e923fe2 | 307 | test_info.failure("Device in wrong mode") |
Pawel Zarembski |
0:01f31e923fe2 | 308 | |
Pawel Zarembski |
0:01f31e923fe2 | 309 | # Test loading a normal image |
Pawel Zarembski |
0:01f31e923fe2 | 310 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 311 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 312 | test = DLMassStorageTester(board, test_info, "Normal Load", |
Pawel Zarembski |
0:01f31e923fe2 | 313 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 314 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 315 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 316 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 317 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 318 | |
Pawel Zarembski |
0:01f31e923fe2 | 319 | # Wrong starting address |
Pawel Zarembski |
0:01f31e923fe2 | 320 | if file_type != 'bin': |
Pawel Zarembski |
0:01f31e923fe2 | 321 | mode_to_error = { |
Pawel Zarembski |
0:01f31e923fe2 | 322 | board.MODE_IF: ('The starting address for the bootloader ' |
Pawel Zarembski |
0:01f31e923fe2 | 323 | 'update is wrong.'), |
Pawel Zarembski |
0:01f31e923fe2 | 324 | board.MODE_BL: ('The starting address for the interface ' |
Pawel Zarembski |
0:01f31e923fe2 | 325 | 'update is wrong.') |
Pawel Zarembski |
0:01f31e923fe2 | 326 | } |
Pawel Zarembski |
0:01f31e923fe2 | 327 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 328 | local_data = get_file_content(data_start + 0x400, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 329 | test = DLMassStorageTester(board, test_info, "Wrong Address", |
Pawel Zarembski |
0:01f31e923fe2 | 330 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 331 | test.set_expected_failure_msg(mode_to_error[board_mode], 'user') |
Pawel Zarembski |
0:01f31e923fe2 | 332 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 333 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 334 | test.set_expected_mode(board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 335 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 336 | |
Pawel Zarembski |
0:01f31e923fe2 | 337 | # Test flushes during update |
Pawel Zarembski |
0:01f31e923fe2 | 338 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 339 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 340 | test = DLMassStorageTester(board, test_info, "Load with flushes", |
Pawel Zarembski |
0:01f31e923fe2 | 341 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 342 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 343 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 344 | test.set_flush_size(0x1000) |
Pawel Zarembski |
0:01f31e923fe2 | 345 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 346 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 347 | |
Pawel Zarembski |
0:01f31e923fe2 | 348 | # Test bad crc |
Pawel Zarembski |
0:01f31e923fe2 | 349 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 350 | local_raw_data = bytearray(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 351 | local_raw_data[-1] = (local_raw_data[-1] + 1) % 0x100 # Corrupt CRC |
Pawel Zarembski |
0:01f31e923fe2 | 352 | local_data = get_file_content(data_start, local_raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 353 | test = DLMassStorageTester(board, test_info, 'Wrong CRC', |
Pawel Zarembski |
0:01f31e923fe2 | 354 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 355 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 356 | if board_mode == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 357 | test.set_expected_failure_msg('The bootloader CRC did not pass.', 'interface') |
Pawel Zarembski |
0:01f31e923fe2 | 358 | test.set_expected_data(None) |
Pawel Zarembski |
0:01f31e923fe2 | 359 | elif board_mode == board.MODE_BL: |
Pawel Zarembski |
0:01f31e923fe2 | 360 | # Interface images can be from other vendors and be missing |
Pawel Zarembski |
0:01f31e923fe2 | 361 | # the crc, so don't treat this as an error |
Pawel Zarembski |
0:01f31e923fe2 | 362 | test.set_expected_data(local_raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 363 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 364 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 365 | # If bootloader is missing then this should be indicated by a file |
Pawel Zarembski |
0:01f31e923fe2 | 366 | if (board_mode == board.MODE_IF and |
Pawel Zarembski |
0:01f31e923fe2 | 367 | not os.path.isfile(board.get_file_path(NEED_BL_FILE_NAME))): |
Pawel Zarembski |
0:01f31e923fe2 | 368 | test_info.failure("Bootloader missing but file %s not present" % |
Pawel Zarembski |
0:01f31e923fe2 | 369 | NEED_BL_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 370 | |
Pawel Zarembski |
0:01f31e923fe2 | 371 | # Test load with extra padding |
Pawel Zarembski |
0:01f31e923fe2 | 372 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 373 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 374 | local_data.extend(b'\xFF' * 0x1000) |
Pawel Zarembski |
0:01f31e923fe2 | 375 | test = DLMassStorageTester(board, test_info, "Padded load", board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 376 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 377 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 378 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 379 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 380 | |
Pawel Zarembski |
0:01f31e923fe2 | 381 | # Test bad crc in file data |
Pawel Zarembski |
0:01f31e923fe2 | 382 | # Note - crc is only a requirment for loading bootloades |
Pawel Zarembski |
0:01f31e923fe2 | 383 | if board_mode == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 384 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 385 | local_raw_data = bytearray(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 386 | local_raw_data[0x100] = (local_raw_data[0x100] + 1) % 0x100 # Corrupt CRC |
Pawel Zarembski |
0:01f31e923fe2 | 387 | local_data = get_file_content(data_start, local_raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 388 | test = DLMassStorageTester(board, test_info, 'Wrong data CRC', |
Pawel Zarembski |
0:01f31e923fe2 | 389 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 390 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 391 | test.set_expected_failure_msg('The bootloader CRC did not pass.', 'interface') |
Pawel Zarembski |
0:01f31e923fe2 | 392 | test.set_expected_data(None) |
Pawel Zarembski |
0:01f31e923fe2 | 393 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 394 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 395 | # If bootloader is missing then this should be indicated by a file |
Pawel Zarembski |
0:01f31e923fe2 | 396 | if not os.path.isfile(board.get_file_path(NEED_BL_FILE_NAME)): |
Pawel Zarembski |
0:01f31e923fe2 | 397 | test_info.failure("Bootloader missing but file %s not present" % |
Pawel Zarembski |
0:01f31e923fe2 | 398 | NEED_BL_FILE_NAME) |
Pawel Zarembski |
0:01f31e923fe2 | 399 | |
Pawel Zarembski |
0:01f31e923fe2 | 400 | # Restore a good image |
Pawel Zarembski |
0:01f31e923fe2 | 401 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 402 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 403 | test = DLMassStorageTester(board, test_info, "Normal Load", |
Pawel Zarembski |
0:01f31e923fe2 | 404 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 405 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 406 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 407 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 408 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 409 | |
Pawel Zarembski |
0:01f31e923fe2 | 410 | # Test wrong HIC ID |
Pawel Zarembski |
0:01f31e923fe2 | 411 | # Bootloader should perform interface update regardless of key |
Pawel Zarembski |
0:01f31e923fe2 | 412 | if data_type == board.MODE_IF: |
Pawel Zarembski |
0:01f31e923fe2 | 413 | local_raw_data = bytearray(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 414 | local_raw_data[DAPLINK_HIC_ID_OFFSET] = \ |
Pawel Zarembski |
0:01f31e923fe2 | 415 | (local_raw_data[DAPLINK_HIC_ID_OFFSET] + 1) % 0x100 |
Pawel Zarembski |
0:01f31e923fe2 | 416 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 417 | local_data = get_file_content(data_start, local_raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 418 | test = DLMassStorageTester(board, test_info, "Wrong HIC ID", |
Pawel Zarembski |
0:01f31e923fe2 | 419 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 420 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 421 | test.set_expected_data(local_raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 422 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 423 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 424 | |
Pawel Zarembski |
0:01f31e923fe2 | 425 | # TODO future - Wrong type |
Pawel Zarembski |
0:01f31e923fe2 | 426 | |
Pawel Zarembski |
0:01f31e923fe2 | 427 | # Test a normal load with dummy files created beforehand |
Pawel Zarembski |
0:01f31e923fe2 | 428 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 429 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 430 | test = DLMassStorageTester(board, test_info, "Extra Files", board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 431 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 432 | test.add_mock_dirs(MOCK_DIR_LIST) |
Pawel Zarembski |
0:01f31e923fe2 | 433 | test.add_mock_files(MOCK_FILE_LIST) |
Pawel Zarembski |
0:01f31e923fe2 | 434 | test.add_mock_dirs_after_load(MOCK_DIR_LIST_AFTER) |
Pawel Zarembski |
0:01f31e923fe2 | 435 | test.add_mock_files_after_load(MOCK_FILE_LIST_AFTER) |
Pawel Zarembski |
0:01f31e923fe2 | 436 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 437 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 438 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 439 | |
Pawel Zarembski |
0:01f31e923fe2 | 440 | # Restore good image |
Pawel Zarembski |
0:01f31e923fe2 | 441 | file_name = get_file_name() |
Pawel Zarembski |
0:01f31e923fe2 | 442 | local_data = get_file_content(data_start, raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 443 | test = DLMassStorageTester(board, test_info, "Restore image", |
Pawel Zarembski |
0:01f31e923fe2 | 444 | board_mode) |
Pawel Zarembski |
0:01f31e923fe2 | 445 | test.set_programming_data(local_data, file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 446 | test.set_expected_data(raw_data) |
Pawel Zarembski |
0:01f31e923fe2 | 447 | test.set_expected_mode(board.MODE_IF) |
Pawel Zarembski |
0:01f31e923fe2 | 448 | test.run() |
Pawel Zarembski |
0:01f31e923fe2 | 449 |