77#include <stdlib.h>
88#include <string.h>
99#include <stdbool.h>
10+ #include <stdint.h>
11+
12+ #define le_uint16_t le_uint16_t
13+ #define le_uint32_t le_uint32_t
14+ #define le_int32_t le_int32_t
15+
16+ typedef struct { uint8_t v [2 ]; } le_uint16_t ;
17+ typedef struct { uint8_t v [4 ]; } le_uint32_t ;
18+ typedef le_uint32_t le_int32_t ;
1019
1120#include "picoboot_connection.h"
1221
@@ -25,6 +34,16 @@ static bool verbose;
2534#define PRODUCT_ID_PICOPROBE 0x0004u
2635#define PRODUCT_ID_MICROPYTHON 0x0005u
2736
37+ static uint32_t le32_to_host (le_uint32_t le )
38+ {
39+ return le .v [0 ] | (le .v [1 ] << 8 ) | (le .v [2 ] << 16 ) | (le .v [3 ] << 24 );
40+ }
41+
42+ static le_uint32_t host_to_le32 (uint32_t host )
43+ {
44+ return (le_uint32_t ){{ host , host >>8 , host >>16 , host >>24 }};
45+ }
46+
2847uint32_t crc32_for_byte (uint32_t remainder ) {
2948 const uint32_t POLYNOMIAL = 0x4C11DB7 ;
3049 remainder <<= 24u ;
@@ -200,8 +219,8 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
200219 int ret ;
201220
202221 static int token = 1 ;
203- cmd -> dMagic = PICOBOOT_MAGIC ;
204- cmd -> dToken = token ++ ;
222+ cmd -> dMagic = host_to_le32 ( PICOBOOT_MAGIC ) ;
223+ cmd -> dToken = host_to_le32 ( token ++ ) ;
205224 ret = libusb_bulk_transfer (usb_device , out_ep , (uint8_t * ) cmd , sizeof (struct picoboot_cmd ), & sent , 3000 );
206225
207226 if (ret != 0 || sent != sizeof (struct picoboot_cmd )) {
@@ -214,22 +233,22 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
214233 timeout = one_time_bulk_timeout ;
215234 one_time_bulk_timeout = 0 ;
216235 }
217- if (cmd -> dTransferLength != 0 ) {
218- assert (buf_size >= cmd -> dTransferLength );
236+ if (le32_to_host ( cmd -> dTransferLength ) != 0 ) {
237+ assert (buf_size >= le32_to_host ( cmd -> dTransferLength ) );
219238 if (cmd -> bCmdId & 0x80u ) {
220- if (verbose ) output (" receive %d...\n" , cmd -> dTransferLength );
239+ if (verbose ) output (" receive %d...\n" , ( int ) le32_to_host ( cmd -> dTransferLength ) );
221240 int received = 0 ;
222- ret = libusb_bulk_transfer (usb_device , in_ep , buffer , cmd -> dTransferLength , & received , timeout );
223- if (ret != 0 || received != (int ) cmd -> dTransferLength ) {
224- output (" ...failed to receive data %d %d/%d\n" , ret , received , cmd -> dTransferLength );
241+ ret = libusb_bulk_transfer (usb_device , in_ep , buffer , le32_to_host ( cmd -> dTransferLength ) , & received , timeout );
242+ if (ret != 0 || received != (int ) le32_to_host ( cmd -> dTransferLength ) ) {
243+ output (" ...failed to receive data %d %d/%d\n" , ret , received , ( int ) le32_to_host ( cmd -> dTransferLength ) );
225244 if (!ret ) ret = 1 ;
226245 return ret ;
227246 }
228247 } else {
229- if (verbose ) output (" send %d...\n" , cmd -> dTransferLength );
230- ret = libusb_bulk_transfer (usb_device , out_ep , buffer , cmd -> dTransferLength , & sent , timeout );
231- if (ret != 0 || sent != (int ) cmd -> dTransferLength ) {
232- output (" ...failed to send data %d %d/%d\n" , ret , sent , cmd -> dTransferLength );
248+ if (verbose ) output (" send %d...\n" , ( int ) le32_to_host ( cmd -> dTransferLength ) );
249+ ret = libusb_bulk_transfer (usb_device , out_ep , buffer , le32_to_host ( cmd -> dTransferLength ) , & sent , timeout );
250+ if (ret != 0 || sent != (int ) le32_to_host ( cmd -> dTransferLength ) ) {
251+ output (" ...failed to send data %d %d/%d\n" , ret , sent , ( int ) le32_to_host ( cmd -> dTransferLength ) );
233252 if (!ret ) ret = 1 ;
234253 picoboot_cmd_status_verbose (usb_device , NULL , true);
235254 return ret ;
@@ -242,10 +261,10 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
242261 uint8_t spoon [64 ];
243262 if (cmd -> bCmdId & 0x80u ) {
244263 if (verbose ) output ("zero length out\n" );
245- ret = libusb_bulk_transfer (usb_device , out_ep , spoon , 1 , & received , cmd -> dTransferLength == 0 ? timeout : 3000 );
264+ ret = libusb_bulk_transfer (usb_device , out_ep , spoon , 1 , & received , le32_to_host ( cmd -> dTransferLength ) == 0 ? timeout : 3000 );
246265 } else {
247266 if (verbose ) output ("zero length in\n" );
248- ret = libusb_bulk_transfer (usb_device , in_ep , spoon , 1 , & received , cmd -> dTransferLength == 0 ? timeout : 3000 );
267+ ret = libusb_bulk_transfer (usb_device , in_ep , spoon , 1 , & received , le32_to_host ( cmd -> dTransferLength ) == 0 ? timeout : 3000 );
249268 }
250269 return ret ;
251270}
@@ -256,7 +275,7 @@ int picoboot_exclusive_access(libusb_device_handle *usb_device, uint8_t exclusiv
256275 cmd .bCmdId = PC_EXCLUSIVE_ACCESS ;
257276 cmd .exclusive_cmd .bExclusive = exclusive ;
258277 cmd .bCmdSize = sizeof (struct picoboot_exclusive_cmd );
259- cmd .dTransferLength = 0 ;
278+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
260279 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
261280}
262281
@@ -265,7 +284,7 @@ int picoboot_exit_xip(libusb_device_handle *usb_device) {
265284 if (verbose ) output ("EXIT_XIP\n" );
266285 cmd .bCmdId = PC_EXIT_XIP ;
267286 cmd .bCmdSize = 0 ;
268- cmd .dTransferLength = 0 ;
287+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
269288 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
270289}
271290
@@ -274,7 +293,7 @@ int picoboot_enter_cmd_xip(libusb_device_handle *usb_device) {
274293 if (verbose ) output ("ENTER_CMD_XIP\n" );
275294 cmd .bCmdId = PC_ENTER_CMD_XIP ;
276295 cmd .bCmdSize = 0 ;
277- cmd .dTransferLength = 0 ;
296+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
278297 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
279298}
280299
@@ -283,10 +302,10 @@ int picoboot_reboot(libusb_device_handle *usb_device, uint32_t pc, uint32_t sp,
283302 if (verbose ) output ("REBOOT %08x %08x %u\n" , (uint ) pc , (uint ) sp , (uint ) delay_ms );
284303 cmd .bCmdId = PC_REBOOT ;
285304 cmd .bCmdSize = sizeof (cmd .reboot_cmd );
286- cmd .dTransferLength = 0 ;
287- cmd .reboot_cmd .dPC = pc ;
288- cmd .reboot_cmd .dSP = sp ;
289- cmd .reboot_cmd .dDelayMS = delay_ms ;
305+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
306+ cmd .reboot_cmd .dPC = host_to_le32 ( pc ) ;
307+ cmd .reboot_cmd .dSP = host_to_le32 ( sp ) ;
308+ cmd .reboot_cmd .dDelayMS = host_to_le32 ( delay_ms ) ;
290309 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
291310}
292311
@@ -297,8 +316,8 @@ int picoboot_exec(libusb_device_handle *usb_device, uint32_t addr) {
297316 if (verbose ) output ("EXEC %08x\n" , (uint ) addr );
298317 cmd .bCmdId = PC_EXEC ;
299318 cmd .bCmdSize = sizeof (cmd .address_only_cmd );
300- cmd .dTransferLength = 0 ;
301- cmd .address_only_cmd .dAddr = addr ;
319+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
320+ cmd .address_only_cmd .dAddr = host_to_le32 ( addr ) ;
302321 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
303322}
304323
@@ -307,9 +326,9 @@ int picoboot_flash_erase(libusb_device_handle *usb_device, uint32_t addr, uint32
307326 if (verbose ) output ("FLASH_ERASE %08x+%08x\n" , (uint ) addr , (uint ) len );
308327 cmd .bCmdId = PC_FLASH_ERASE ;
309328 cmd .bCmdSize = sizeof (cmd .range_cmd );
310- cmd .range_cmd .dAddr = addr ;
311- cmd .range_cmd .dSize = len ;
312- cmd .dTransferLength = 0 ;
329+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
330+ cmd .range_cmd .dSize = host_to_le32 ( len ) ;
331+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
313332 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
314333}
315334
@@ -318,8 +337,8 @@ int picoboot_vector(libusb_device_handle *usb_device, uint32_t addr) {
318337 if (verbose ) output ("VECTOR %08x\n" , (uint ) addr );
319338 cmd .bCmdId = PC_VECTORIZE_FLASH ;
320339 cmd .bCmdSize = sizeof (cmd .address_only_cmd );
321- cmd .range_cmd .dAddr = addr ;
322- cmd .dTransferLength = 0 ;
340+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
341+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
323342 return picoboot_cmd (usb_device , & cmd , NULL , 0 );
324343}
325344
@@ -328,8 +347,8 @@ int picoboot_write(libusb_device_handle *usb_device, uint32_t addr, uint8_t *buf
328347 if (verbose ) output ("WRITE %08x+%08x\n" , (uint ) addr , (uint ) len );
329348 cmd .bCmdId = PC_WRITE ;
330349 cmd .bCmdSize = sizeof (cmd .range_cmd );
331- cmd .range_cmd .dAddr = addr ;
332- cmd .range_cmd .dSize = cmd .dTransferLength = len ;
350+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
351+ cmd .range_cmd .dSize = cmd .dTransferLength = host_to_le32 ( len ) ;
333352 return picoboot_cmd (usb_device , & cmd , buffer , len );
334353}
335354
@@ -339,8 +358,8 @@ int picoboot_read(libusb_device_handle *usb_device, uint32_t addr, uint8_t *buff
339358 struct picoboot_cmd cmd ;
340359 cmd .bCmdId = PC_READ ;
341360 cmd .bCmdSize = sizeof (cmd .range_cmd );
342- cmd .range_cmd .dAddr = addr ;
343- cmd .range_cmd .dSize = cmd .dTransferLength = len ;
361+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
362+ cmd .range_cmd .dSize = cmd .dTransferLength = host_to_le32 ( len ) ;
344363 int ret = picoboot_cmd (usb_device , & cmd , buffer , len );
345364 if (!ret && len < 256 && verbose ) {
346365 for (uint32_t i = 0 ; i < len ; i += 32 ) {
@@ -422,4 +441,4 @@ int picoboot_peek(libusb_device_handle *usb_device, uint32_t addr, uint32_t *dat
422441 return ret ;
423442 return picoboot_read (usb_device , PEEK_POKE_CODE_LOC + picoboot_peek_cmd_len , (uint8_t * ) data , sizeof (uint32_t ));
424443}
425- #endif
444+ #endif
0 commit comments