Debian Codegraph @

docs/security.md

raw symbols SCIP
1 ---
2 title: Security and protection
3 ---
4
5 Many targets support some way of disabling JTAG/SWD access or protecting the flash from read-back or write.
6 In the text below, this is called a "locked" target, though each silicon vendor tends to have their
7 own terminology for this feature.
8
9 ## Supported Targets
10
11 pyOCD currently supports the security features of these target families:
12 - NXP Kinetis
13 - Nordic Semiconductor nRF52 families
14
15 Both built-in and CMSIS-Pack based targets from these families support security features.
16
17 ## Connecting to a locked target
18
19 If you attempt to run pyOCD Commander against a locked target, you'll encounter a message like this:
20
21 ``` bash
22 (venv) ~/devel/contrib/pyocd$ pyocd commander --target nrf52
23 0000265:WARNING:target_nRF52:NRF52832 APPROTECT enabled: not automatically unlocking
24 Exception while initing board: No cores were discovered!
25 Traceback (most recent call last):
26 File "devel/contrib/pyocd/pyocd/tools/pyocd.py", line 769, in connect
27 self.session.open(init_board=not self.args.no_init)
28 File "devel/contrib/pyocd/pyocd/core/session.py", line 371, in open
29 self._board.init()
30 File "devel/contrib/pyocd/pyocd/board/board.py", line 83, in init
31 self.target.init()
32 File "devel/contrib/pyocd/pyocd/core/coresight_target.py", line 164, in init
33 seq.invoke()
34 File "devel/contrib/pyocd/pyocd/utility/sequencer.py", line 208, in invoke
35 resultSequence = call()
36 File "devel/contrib/pyocd/pyocd/core/coresight_target.py", line 298, in check_for_cores
37 raise exceptions.DebugError("No cores were discovered!")
38 pyocd.core.exceptions.DebugError: No cores were discovered!
39 ```
40
41 pyOCD has noticed that the chip is locked. It attempts routine initialization anyway, but fails to find any cores.
42
43 For all other pyOCD commands that connect to the target, pyOCD will, by default, attempt to
44 automatically unlock the target.
45
46 Disabling the security features on supported targets is very straight-forward. It typically requires
47 performing a mass erase of all device memory. For those pyOCD targets with support for security
48 features, pyOCD can perform this unlock procedure for you.
49
50 <div class="alert alert-warning">
51 Unlocking a locked device will erase all data on the chip!
52 </div>
53
54 You can add the option `auto_unlock` to your [configuration]({% link _docs/configuration.md %}):
55
56 ```bash
57 (venv) ~/devel/contrib/pyocd$ pyocd commander --target nrf52 -O auto_unlock
58 0000264:WARNING:target_nRF52:NRF52832 APPROTECT enabled: will try to unlock via mass erase
59 Connected to NRF52832 [Halted]: I3FSNZOV
60 >>>
61 ```
62
63 Note that the default for `auto_unlock` is True. Only in pyOCD Commander is this default changed,
64 because of how Commander is intended to be used for low-level interaction and inspection of the
65 target.
66
67 You can also do this interactively with pyOCD Commander:
68
69 ```
70 (venv) $ pyocd commander --target nrf52840 -N
71 >>> initdp
72 >>> makeap 1
73 >>> status
74 Security: Locked
75 >>> unlock
76 >>> status
77 Security: Unlocked
78 >>> reinit
79 >>> status
80 Security: Unlocked
81 Core 0 status: Halted
82 >>>
83 ```
84
85 An explanation of some of the commands and options appears below.
86
87 ## Unsupported targets
88
89 If pyOCD doesn't support the security features of your MCU, you can still likely access them with
90 pyOCD Commander with the `no-init` option. A common pattern is that the security function locks out
91 the fully-functional AHB-AP, but leaves a second, proprietary AP unlocked. The second AP usually has
92 very few functions, mostly related to management (reset) or erase/unlock of the chip. Another method
93 used by some device families is to use only the standard AHB-AP, but when the device is locked the
94 accessible address range is limited to a small handful of registers.
95
96 Starting pyOCD Commander in `--no-init` mode is intended to help in these situations. It intentionally doesn't attempt
97 to interact with the target on startup. Without initialization, most commands will not work. You'll need to do some
98 manual initialization.
99
100 The usefulness of `--no-init` mode is that it allows Commander to start when initialization would
101 normally fail due to missing support for the target or security features of the MCU.
102
103 ### Useful Options
104
105 Once you've started Commander `--no-init`, the following commands are most useful.
106
107 <dl>
108 <dt><em>initdp</em></dt>
109 <dd>Powers on and initializes the on-chip *Debug Port*, allowing Commander to issue commands to the
110 target</dd>
111
112 <dt><em>makeap &lt;ap num&gt;</em></dt>
113 <dd>Targets have at least one, but sometimes more, *Access Ports*. Access port #0 is usually an AHB-AP that allows
114 debugging of the target. Access port #1, if it exists, is often proprietary to the vendor and allows
115 for certain functions to proceed *even if AP #0 is locked*.</dd>
116
117 <dt><em>readap &lt;APSEL&gt; &lt;address&gt;</em></dt>
118 <dd>Read from an AP register</dd>
119
120 <dt><em>writeap &lt;APSEL&gt; &lt;address&gt; &lt;value&gt;</em></dt>
121 <dd>Write to an AP register</dd>
122
123 <dt><em>status</em></dt>
124 <dd>Will show security status for supported targets. If the target is unlocked and the AHB-AP is
125 initialized, it will also show status for any cores detected (Running, Halted, &c)</dd>
126
127 <dt><em>reinit</em></dt>
128 <dd>This command will attempt to perform the normal initialization steps for the selected target. If you
129 manually unlock a target, you may want to run `reinit` so that the cores are accessible *without* restarting
130 Commander in normal mode (without `--no-init`)</dd>
131 </dl>
132
133 ### Example
134
135 Let's pretend the nRF52 family is unsupported. Here's an example session where we'll use `no-init` mode to manually
136 unlock the target:
137
138 ```
139 (venv) nock@nocko-wired:~/devel/contrib/pyocd$ pyocd commander -N
140 0000136:WARNING:board:Generic 'cortex_m' target type is selected; is this intentional? You will be able to debug but not program flash. To set the target type use the '--target' argument or 'target_override' option. Use 'pyocd list --targets' to see available targets types.
141 >>> initdp
142 ```
143 We use `initdp` to initialize the *Debug Port* and power on the on-chip debug hardware.
144
145 The [nRF52 Product Specification](https://infocenter.nordicsemi.com/pdf/nRF52832_PS_v1.1.pdf) says that the chip has a
146 proprietary *CTRL-AP* at index 1 that supports a few operations even if the main AP is locked.
147
148 Let's let pyOCD know about it with `makeap`:
149
150 ```
151 >>> makeap 1
152 AP#1 IDR = 0x02880000
153 ```
154
155 The IDR register matches the datasheet! The datasheet says that CTRL-AP (AP #1) has the following registers:
156
157 <dl>
158 <dt><em>RESET</em> (0x000)</dt>
159 <dd>Writing 1 to this register asserts reset on the chip. Writing 0 takes is out of reset</dd>
160
161 <dt><em>ERASEALL</em> (0x004)</dt>
162 <dd>Writing 1 to this register starts a mass erase of the chip <i>and removes APPROTECT</i> unlocking the chip</dd>
163
164 <dt><em>ERASEALLSTATUS</em> (0x008)</dt>
165 <dd>While the chip is busy doing a mass erase, this register will read 1. When the mass erase is complete, the register
166 will read 0.</dd>
167
168 <dt><em>APPROTECTSTATUS</em> (0x00C)</dt>
169 <dd>If this register is 0, then APPROTECT is enabled (chip is locked), if 1 the chip is unlocked</dd>
170 </dl>
171
172 We can read the register value with `readap` and write a new value with `writeap`. Let's see if the chip is locked by
173 reading *APPROTECTSTATUS* on AP #1:
174
175 ```
176 >>> readap 1 0x00C
177 AP register 0x100000c = 0x00000000
178 ```
179
180 This chip is locked! Let's unlock it. We need to write a 1 to *ERASEALL* (on AP #1) to start the mass erase:
181
182 ```
183 >>> writeap 1 0x004 1
184 ```
185
186 Let's see if it has finished, by reading *ERASEALLSTATUS*:
187
188 ```
189 >>> readap 1 0x008
190 AP register 0x1000008 = 0x00000001
191 >>> readap 1 0x008
192 AP register 0x1000008 = 0x00000000
193 ```
194
195 After the first `readap` it was still erasing, but by the time the second `readap` was issued it was complete.
196 Let's check the security status by reading *APPROTECTSTATUS* again:
197
198 ```
199 >>> readap 1 0x00C
200 AP register 0x100000c = 0x00000001
201 ```
202
203 It's unlocked! Let's check the `status`:
204
205 ```
206 >>> status
207 Security: Unlocked
208 >>> reinit
209 >>> status
210 Security: Unlocked
211 Core 0 status: Halted
212 >>>
213 ```
214
215 The security was unlocked, but there are no cores (many commands like halt, reset, and so on need a core). It doesn't exist
216 since we started Commander in `no-init` mode. Running `reinit` from the CLI finishes the initialization that's
217 possible now that the chip is unlocked.