AVRISP-MKII Clone: Add length checks to SPI Multi and XPROG read/write commands.
[pub/lufa.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGProtocol.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2021.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * XPROG Protocol handler, to process V2 Protocol wrapped XPROG commands used in Atmel programmer devices.
34 */
35
36 #define INCLUDE_FROM_XPROGPROTOCOL_C
37 #include "XPROGProtocol.h"
38
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
40 /** Base absolute address for the target's NVM controller for PDI programming */
41 uint32_t XPROG_Param_NVMBase = 0x010001C0;
42
43 /** Size in bytes of the target's EEPROM page */
44 uint16_t XPROG_Param_EEPageSize = 32;
45
46 /** Address of the TPI device's NVMCMD register for TPI programming */
47 uint8_t XPROG_Param_NVMCMDRegAddr = 0x33;
48
49 /** Address of the TPI device's NVMCSR register for TPI programming */
50 uint8_t XPROG_Param_NVMCSRRegAddr = 0x32;
51
52 /** Currently selected XPROG programming protocol */
53 uint8_t XPROG_SelectedProtocol = XPROG_PROTOCOL_PDI;
54
55 /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI/TPI
56 * programming.
57 */
58 void XPROGProtocol_SetMode(void)
59 {
60 struct
61 {
62 uint8_t Protocol;
63 } SetMode_XPROG_Params;
64
65 Endpoint_Read_Stream_LE(&SetMode_XPROG_Params, sizeof(SetMode_XPROG_Params), NULL);
66
67 Endpoint_ClearOUT();
68 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
69 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
70
71 XPROG_SelectedProtocol = SetMode_XPROG_Params.Protocol;
72
73 Endpoint_Write_8(CMD_XPROG_SETMODE);
74 Endpoint_Write_8((SetMode_XPROG_Params.Protocol != XPROG_PROTOCOL_JTAG) ? STATUS_CMD_OK : STATUS_CMD_FAILED);
75 Endpoint_ClearIN();
76 }
77
78 /** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be
79 * removed and processed so that the underlying XPROG command can be handled.
80 */
81 void XPROGProtocol_Command(void)
82 {
83 uint8_t XPROGCommand = Endpoint_Read_8();
84
85 switch (XPROGCommand)
86 {
87 case XPROG_CMD_ENTER_PROGMODE:
88 XPROGProtocol_EnterXPROGMode();
89 break;
90 case XPROG_CMD_LEAVE_PROGMODE:
91 XPROGProtocol_LeaveXPROGMode();
92 break;
93 case XPROG_CMD_ERASE:
94 XPROGProtocol_Erase();
95 break;
96 case XPROG_CMD_WRITE_MEM:
97 XPROGProtocol_WriteMemory();
98 break;
99 case XPROG_CMD_READ_MEM:
100 XPROGProtocol_ReadMemory();
101 break;
102 case XPROG_CMD_CRC:
103 XPROGProtocol_ReadCRC();
104 break;
105 case XPROG_CMD_SET_PARAM:
106 XPROGProtocol_SetParam();
107 break;
108 }
109 }
110
111 /** Handler for the XPROG ENTER_PROGMODE command to establish a connection with the attached device. */
112 static void XPROGProtocol_EnterXPROGMode(void)
113 {
114 Endpoint_ClearOUT();
115 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
116 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
117
118 bool NVMBusEnabled = false;
119
120 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
121 NVMBusEnabled = XMEGANVM_EnablePDI();
122 else if (XPROG_SelectedProtocol == XPROG_PROTOCOL_TPI)
123 NVMBusEnabled = TINYNVM_EnableTPI();
124
125 Endpoint_Write_8(CMD_XPROG);
126 Endpoint_Write_8(XPROG_CMD_ENTER_PROGMODE);
127 Endpoint_Write_8(NVMBusEnabled ? XPROG_ERR_OK : XPROG_ERR_FAILED);
128 Endpoint_ClearIN();
129 }
130
131 /** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with
132 * the attached device.
133 */
134 static void XPROGProtocol_LeaveXPROGMode(void)
135 {
136 Endpoint_ClearOUT();
137 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
138 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
139
140 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
141 XMEGANVM_DisablePDI();
142 else
143 TINYNVM_DisableTPI();
144
145 #if defined(XCK_RESCUE_CLOCK_ENABLE) && defined(ENABLE_ISP_PROTOCOL)
146 /* If the XCK rescue clock option is enabled, we need to restart it once the
147 * XPROG mode has been exited, since the XPROG protocol stops it after use. */
148 ISPTarget_ConfigureRescueClock();
149 #endif
150
151 Endpoint_Write_8(CMD_XPROG);
152 Endpoint_Write_8(XPROG_CMD_LEAVE_PROGMODE);
153 Endpoint_Write_8(XPROG_ERR_OK);
154 Endpoint_ClearIN();
155 }
156
157 /** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */
158 static void XPROGProtocol_Erase(void)
159 {
160 uint8_t ReturnStatus = XPROG_ERR_OK;
161
162 struct
163 {
164 uint8_t MemoryType;
165 uint32_t Address;
166 } Erase_XPROG_Params;
167
168 Endpoint_Read_Stream_LE(&Erase_XPROG_Params, sizeof(Erase_XPROG_Params), NULL);
169 Erase_XPROG_Params.Address = SwapEndian_32(Erase_XPROG_Params.Address);
170
171 Endpoint_ClearOUT();
172 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
173 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
174
175 uint8_t EraseCommand;
176
177 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
178 {
179 /* Determine which NVM command to send to the device depending on the memory to erase */
180 switch (Erase_XPROG_Params.MemoryType)
181 {
182 case XPROG_ERASE_CHIP:
183 EraseCommand = XMEGA_NVM_CMD_CHIPERASE;
184 break;
185 case XPROG_ERASE_APP:
186 EraseCommand = XMEGA_NVM_CMD_ERASEAPPSEC;
187 break;
188 case XPROG_ERASE_BOOT:
189 EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSEC;
190 break;
191 case XPROG_ERASE_EEPROM:
192 EraseCommand = XMEGA_NVM_CMD_ERASEEEPROM;
193 break;
194 case XPROG_ERASE_APP_PAGE:
195 EraseCommand = XMEGA_NVM_CMD_ERASEAPPSECPAGE;
196 break;
197 case XPROG_ERASE_BOOT_PAGE:
198 EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSECPAGE;
199 break;
200 case XPROG_ERASE_EEPROM_PAGE:
201 EraseCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGE;
202 break;
203 case XPROG_ERASE_USERSIG:
204 EraseCommand = XMEGA_NVM_CMD_ERASEUSERSIG;
205 break;
206 default:
207 EraseCommand = XMEGA_NVM_CMD_NOOP;
208 break;
209 }
210
211 /* Erase the target memory, indicate timeout if occurred */
212 if (!(XMEGANVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
213 ReturnStatus = XPROG_ERR_TIMEOUT;
214 }
215 else
216 {
217 if (Erase_XPROG_Params.MemoryType == XPROG_ERASE_CHIP)
218 EraseCommand = TINY_NVM_CMD_CHIPERASE;
219 else
220 EraseCommand = TINY_NVM_CMD_SECTIONERASE;
221
222 /* Erase the target memory, indicate timeout if occurred */
223 if (!(TINYNVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
224 ReturnStatus = XPROG_ERR_TIMEOUT;
225 }
226
227 Endpoint_Write_8(CMD_XPROG);
228 Endpoint_Write_8(XPROG_CMD_ERASE);
229 Endpoint_Write_8(ReturnStatus);
230 Endpoint_ClearIN();
231 }
232
233 /** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */
234 static void XPROGProtocol_WriteMemory(void)
235 {
236 uint8_t ReturnStatus = XPROG_ERR_OK;
237
238 struct
239 {
240 uint8_t MemoryType;
241 uint8_t PageMode;
242 uint32_t Address;
243 uint16_t Length;
244 uint8_t ProgData[256];
245 } WriteMemory_XPROG_Params;
246
247 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params, (sizeof(WriteMemory_XPROG_Params) -
248 sizeof(WriteMemory_XPROG_Params).ProgData), NULL);
249 WriteMemory_XPROG_Params.Address = SwapEndian_32(WriteMemory_XPROG_Params.Address);
250 WriteMemory_XPROG_Params.Length = SwapEndian_16(WriteMemory_XPROG_Params.Length);
251 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length, NULL);
252
253 if (WriteMemory_XPROG_Params.Length >= sizeof(WriteMemory_XPROG_Params.ProgData))
254 {
255 Endpoint_StallTransaction();
256 return;
257 }
258
259 // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need
260 // to catch this and discard it before continuing on with packet processing to prevent communication issues
261 if (((sizeof(uint8_t) + sizeof(WriteMemory_XPROG_Params) - sizeof(WriteMemory_XPROG_Params.ProgData)) +
262 WriteMemory_XPROG_Params.Length) % AVRISP_DATA_EPSIZE == 0)
263 {
264 Endpoint_ClearOUT();
265 Endpoint_WaitUntilReady();
266 }
267
268 Endpoint_ClearOUT();
269 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
270 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
271
272 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
273 {
274 /* Assume FLASH page programming by default, as it is the common case */
275 uint8_t WriteCommand = XMEGA_NVM_CMD_WRITEFLASHPAGE;
276 uint8_t WriteBuffCommand = XMEGA_NVM_CMD_LOADFLASHPAGEBUFF;
277 uint8_t EraseBuffCommand = XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF;
278 bool PagedMemory = true;
279
280 switch (WriteMemory_XPROG_Params.MemoryType)
281 {
282 case XPROG_MEM_TYPE_APPL:
283 WriteCommand = XMEGA_NVM_CMD_WRITEAPPSECPAGE;
284 break;
285 case XPROG_MEM_TYPE_BOOT:
286 WriteCommand = XMEGA_NVM_CMD_WRITEBOOTSECPAGE;
287 break;
288 case XPROG_MEM_TYPE_EEPROM:
289 WriteCommand = XMEGA_NVM_CMD_ERASEWRITEEEPROMPAGE;
290 WriteBuffCommand = XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF;
291 EraseBuffCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF;
292 break;
293 case XPROG_MEM_TYPE_USERSIG:
294 WriteCommand = XMEGA_NVM_CMD_WRITEUSERSIG;
295 break;
296 case XPROG_MEM_TYPE_FUSE:
297 WriteCommand = XMEGA_NVM_CMD_WRITEFUSE;
298 PagedMemory = false;
299 break;
300 case XPROG_MEM_TYPE_LOCKBITS:
301 WriteCommand = XMEGA_NVM_CMD_WRITELOCK;
302 PagedMemory = false;
303 break;
304 }
305
306 /* Send the appropriate memory write commands to the device, indicate timeout if occurred */
307 if ((PagedMemory && !(XMEGANVM_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand,
308 WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address,
309 WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length))) ||
310 (!PagedMemory && !(XMEGANVM_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address,
311 WriteMemory_XPROG_Params.ProgData[0]))))
312 {
313 ReturnStatus = XPROG_ERR_TIMEOUT;
314 }
315 }
316 else
317 {
318 /* Send write command to the TPI device, indicate timeout if occurred */
319 if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData,
320 WriteMemory_XPROG_Params.Length)))
321 {
322 ReturnStatus = XPROG_ERR_TIMEOUT;
323 }
324 }
325
326 Endpoint_Write_8(CMD_XPROG);
327 Endpoint_Write_8(XPROG_CMD_WRITE_MEM);
328 Endpoint_Write_8(ReturnStatus);
329 Endpoint_ClearIN();
330 }
331
332 /** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the
333 * attached device.
334 */
335 static void XPROGProtocol_ReadMemory(void)
336 {
337 uint8_t ReturnStatus = XPROG_ERR_OK;
338
339 struct
340 {
341 uint8_t MemoryType;
342 uint32_t Address;
343 uint16_t Length;
344 } ReadMemory_XPROG_Params;
345
346 uint8_t ReadBuffer[256];
347
348 Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params, sizeof(ReadMemory_XPROG_Params), NULL);
349 ReadMemory_XPROG_Params.Address = SwapEndian_32(ReadMemory_XPROG_Params.Address);
350 ReadMemory_XPROG_Params.Length = SwapEndian_16(ReadMemory_XPROG_Params.Length);
351
352 if (ReadMemory_XPROG_Params.Length >= sizeof(ReadBuffer))
353 {
354 Endpoint_StallTransaction();
355 return;
356 }
357
358 Endpoint_ClearOUT();
359 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
360 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
361
362 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
363 {
364 /* Read the PDI target's memory, indicate timeout if occurred */
365 if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
366 ReturnStatus = XPROG_ERR_TIMEOUT;
367 }
368 else
369 {
370 /* Read the TPI target's memory, indicate timeout if occurred */
371 if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
372 ReturnStatus = XPROG_ERR_TIMEOUT;
373 }
374
375 Endpoint_Write_8(CMD_XPROG);
376 Endpoint_Write_8(XPROG_CMD_READ_MEM);
377 Endpoint_Write_8(ReturnStatus);
378
379 if (ReturnStatus == XPROG_ERR_OK)
380 Endpoint_Write_Stream_LE(ReadBuffer, ReadMemory_XPROG_Params.Length, NULL);
381
382 Endpoint_ClearIN();
383 }
384
385 /** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the
386 * attached device's memory and a data set on the host.
387 */
388 static void XPROGProtocol_ReadCRC(void)
389 {
390 uint8_t ReturnStatus = XPROG_ERR_OK;
391
392 struct
393 {
394 uint8_t CRCType;
395 } ReadCRC_XPROG_Params;
396
397 Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params), NULL);
398
399 Endpoint_ClearOUT();
400 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
401 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
402
403 uint32_t MemoryCRC;
404
405 if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
406 {
407 uint8_t CRCCommand;
408
409 /* Determine which NVM command to send to the device depending on the memory to CRC */
410 switch (ReadCRC_XPROG_Params.CRCType)
411 {
412 case XPROG_CRC_APP:
413 CRCCommand = XMEGA_NVM_CMD_APPCRC;
414 break;
415 case XPROG_CRC_BOOT:
416 CRCCommand = XMEGA_NVM_CMD_BOOTCRC;
417 break;
418 default:
419 CRCCommand = XMEGA_NVM_CMD_FLASHCRC;
420 break;
421 }
422
423 /* Perform and retrieve the memory CRC, indicate timeout if occurred */
424 if (!(XMEGANVM_GetMemoryCRC(CRCCommand, &MemoryCRC)))
425 ReturnStatus = XPROG_ERR_TIMEOUT;
426 }
427 else
428 {
429 /* TPI does not support memory CRC */
430 ReturnStatus = XPROG_ERR_FAILED;
431 }
432
433 Endpoint_Write_8(CMD_XPROG);
434 Endpoint_Write_8(XPROG_CMD_CRC);
435 Endpoint_Write_8(ReturnStatus);
436
437 if (ReturnStatus == XPROG_ERR_OK)
438 {
439 Endpoint_Write_8(MemoryCRC >> 16);
440 Endpoint_Write_16_LE(MemoryCRC & 0xFFFF);
441 }
442
443 Endpoint_ClearIN();
444 }
445
446 /** Handler for the XPROG SET_PARAM command to set a XPROG parameter for use when communicating with the
447 * attached device.
448 */
449 static void XPROGProtocol_SetParam(void)
450 {
451 uint8_t ReturnStatus = XPROG_ERR_OK;
452
453 uint8_t XPROGParam = Endpoint_Read_8();
454
455 /* Determine which parameter is being set, store the new parameter value */
456 switch (XPROGParam)
457 {
458 case XPROG_PARAM_NVMBASE:
459 XPROG_Param_NVMBase = Endpoint_Read_32_BE();
460 break;
461 case XPROG_PARAM_EEPPAGESIZE:
462 XPROG_Param_EEPageSize = Endpoint_Read_16_BE();
463 break;
464 case XPROG_PARAM_NVMCMD_REG:
465 XPROG_Param_NVMCMDRegAddr = Endpoint_Read_8();
466 break;
467 case XPROG_PARAM_NVMCSR_REG:
468 XPROG_Param_NVMCSRRegAddr = Endpoint_Read_8();
469 break;
470 case XPROG_PARAM_UNKNOWN_1:
471 /* TODO: Undocumented parameter added in AVRStudio 5.1, purpose unknown. Must ACK and discard or
472 the communication with AVRStudio 5.1 will fail.
473 */
474 Endpoint_Discard_16();
475 break;
476 default:
477 ReturnStatus = XPROG_ERR_FAILED;
478 break;
479 }
480
481 Endpoint_ClearOUT();
482 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
483 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
484
485 Endpoint_Write_8(CMD_XPROG);
486 Endpoint_Write_8(XPROG_CMD_SET_PARAM);
487 Endpoint_Write_8(ReturnStatus);
488 Endpoint_ClearIN();
489 }
490
491 #endif
492