5959
6060#include <assert.h>
6161
62+ static uint32_t swap32 (const uint32_t value )
63+ {
64+ return ((value >> 24 ) & 0xff ) | ((value >> 8 ) & 0xff00 ) | ((value << 8 ) & 0xff0000 ) | ((value << 24 ) & 0xff000000 );
65+ }
66+
67+ static void memcpy_swap (void * dest_v , const void * src_v , size_t count ) {
68+ uint8_t * dest = dest_v ;
69+ const uint8_t * src = src_v ;
70+ for (size_t i = 0 ; i < count ; i += 1 ) {
71+ dest [(i & ~3 )+ (~i & 3 )] = src [i ];
72+ }
73+ }
74+
6275typedef struct cortexar_priv {
6376 /* Base core information */
6477 cortex_priv_s base ;
@@ -286,10 +299,10 @@ static const uint16_t cortexar_spsr_encodings[5] = {
286299 * The fourth is `STRH r1, [r0], #+2` to store a uint16_t to [r0] from r1 and increment
287300 * the address in r0 by 2, writing the new address back to r0.
288301 */
289- #define ARM_LDRB_R0_R1_INSN 0xe4f01001U
290- #define ARM_LDRH_R0_R1_INSN 0xe0f010b2U
291- #define ARM_STRB_R1_R0_INSN 0xe4e01001U
292- #define ARM_STRH_R1_R0_INSN 0xe0e010b2U
302+ #define ARM_LDRB_R0_R1_INSN 0xe4d01001U
303+ #define ARM_LDRH_R0_R1_INSN 0xe0d010b2U
304+ #define ARM_STRB_R1_R0_INSN 0xe4c01001U
305+ #define ARM_STRH_R1_R0_INSN 0xe0c010b2U
293306
294307/* Instruction encodings for synchronisation barriers */
295308#define ARM_ISB_INSN 0xe57ff06fU
@@ -1021,7 +1034,7 @@ static inline bool cortexar_mem_read_fast(target_s *const target, uint32_t *cons
10211034 /* Run the transfer, hammering the DTR */
10221035 for (size_t offset = 0 ; offset < count ; ++ offset ) {
10231036 /* Read the next value, which is the value for the last instruction run */
1024- const uint32_t value = adiv5_dp_read (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRRX ));
1037+ const uint32_t value = swap32 ( adiv5_dp_read (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRRX ) ));
10251038 /* If we've run the instruction at least once, store it */
10261039 if (offset )
10271040 dest [offset - 1U ] = value ;
@@ -1031,7 +1044,7 @@ static inline bool cortexar_mem_read_fast(target_s *const target, uint32_t *cons
10311044 /* Go back into DCC Normal (Non-blocking) mode */
10321045 adiv5_dp_write (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DCSR ), dbg_dcsr | CORTEXAR_DBG_DCSR_DCC_NORMAL );
10331046 /* Grab the value of the last instruction run now it won't run again */
1034- dest [count - 1U ] = adiv5_dp_read (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRRX ));
1047+ dest [count - 1U ] = swap32 ( adiv5_dp_read (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRRX ) ));
10351048 /* Check if the instruction triggered a synchronous data abort */
10361049 return cortexar_check_data_abort (target , status );
10371050 }
@@ -1040,6 +1053,7 @@ static inline bool cortexar_mem_read_fast(target_s *const target, uint32_t *cons
10401053 for (size_t offset = 0 ; offset < count ; ++ offset ) {
10411054 if (!cortexar_run_read_insn (target , ARM_LDC_R0_POSTINC4_DTRTX_INSN , dest + offset ))
10421055 return false; /* Propagate failure if it happens */
1056+ dest [offset ] = swap32 (dest [offset ]);
10431057 }
10441058 return true; /* Signal success */
10451059}
@@ -1165,7 +1179,7 @@ static inline bool cortexar_mem_write_fast(target_s *const target, const uint32_
11651179 adiv5_dp_write (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_ITR ), ARM_STC_DTRRX_R0_POSTINC4_INSN );
11661180 /* Run the transfer, hammering the DTR */
11671181 for (size_t offset = 0 ; offset < count ; ++ offset )
1168- adiv5_dp_write (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRTX ), src [offset ]);
1182+ adiv5_dp_write (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DTRTX ), swap32 ( src [offset ]) );
11691183 /* Now read out the status from the DCSR in case anything went wrong */
11701184 const uint32_t status = adiv5_dp_read (priv -> base .ap -> dp , ADIV5_AP_DB (CORTEXAR_BANKED_DCSR ));
11711185 /* Go back into DCC Normal (Non-blocking) mode */
@@ -1176,7 +1190,7 @@ static inline bool cortexar_mem_write_fast(target_s *const target, const uint32_
11761190
11771191 /* Write each of the uint32_t's checking for failure */
11781192 for (size_t offset = 0 ; offset < count ; ++ offset ) {
1179- if (!cortexar_run_write_insn (target , ARM_STC_DTRRX_R0_POSTINC4_INSN , src [offset ]))
1193+ if (!cortexar_run_write_insn (target , ARM_STC_DTRRX_R0_POSTINC4_INSN , swap32 ( src [offset ]) ))
11801194 return false; /* Propagate failure if it happens */
11811195 }
11821196 return true; /* Signal success */
@@ -1277,11 +1291,11 @@ static void cortexar_regs_read(target_s *const target, void *const data)
12771291 const cortexar_priv_s * const priv = (cortexar_priv_s * )target -> priv ;
12781292 uint32_t * const regs = (uint32_t * )data ;
12791293 /* Copy the register values out from our cache */
1280- memcpy (regs , priv -> core_regs .r , sizeof (priv -> core_regs .r ));
1281- regs [CORTEX_REG_CPSR ] = priv -> core_regs .cpsr ;
1294+ memcpy_swap (regs , priv -> core_regs .r , sizeof (priv -> core_regs .r ));
1295+ regs [CORTEX_REG_CPSR ] = swap32 ( priv -> core_regs .cpsr ) ;
12821296 if (target -> target_options & TOPT_FLAVOUR_FLOAT ) {
1283- memcpy (regs + CORTEXAR_GENERAL_REG_COUNT , priv -> core_regs .d , sizeof (priv -> core_regs .d ));
1284- regs [CORTEX_REG_FPCSR ] = priv -> core_regs .fpcsr ;
1297+ memcpy_swap (regs + CORTEXAR_GENERAL_REG_COUNT , priv -> core_regs .d , sizeof (priv -> core_regs .d ));
1298+ regs [CORTEX_REG_FPCSR ] = swap32 ( priv -> core_regs .fpcsr ) ;
12851299 }
12861300}
12871301
@@ -1290,11 +1304,11 @@ static void cortexar_regs_write(target_s *const target, const void *const data)
12901304 cortexar_priv_s * const priv = (cortexar_priv_s * )target -> priv ;
12911305 const uint32_t * const regs = (const uint32_t * )data ;
12921306 /* Copy the new register values into our cache */
1293- memcpy (priv -> core_regs .r , regs , sizeof (priv -> core_regs .r ));
1294- priv -> core_regs .cpsr = regs [CORTEX_REG_CPSR ];
1307+ memcpy_swap (priv -> core_regs .r , regs , sizeof (priv -> core_regs .r ));
1308+ priv -> core_regs .cpsr = swap32 ( regs [CORTEX_REG_CPSR ]) ;
12951309 if (target -> target_options & TOPT_FLAVOUR_FLOAT ) {
1296- memcpy (priv -> core_regs .d , regs + CORTEXAR_GENERAL_REG_COUNT , sizeof (priv -> core_regs .d ));
1297- priv -> core_regs .fpcsr = regs [CORTEX_REG_FPCSR ];
1310+ memcpy_swap (priv -> core_regs .d , regs + CORTEXAR_GENERAL_REG_COUNT , sizeof (priv -> core_regs .d ));
1311+ priv -> core_regs .fpcsr = swap32 ( regs [CORTEX_REG_FPCSR ]) ;
12981312 }
12991313}
13001314
@@ -1339,7 +1353,7 @@ static size_t cortexar_reg_read(target_s *const target, const uint32_t reg, void
13391353 if (max < reg_width )
13401354 return 0 ;
13411355 /* Finally, copy the register data out and return the width */
1342- memcpy (data , reg_ptr , reg_width );
1356+ memcpy_swap (data , reg_ptr , reg_width );
13431357 return reg_width ;
13441358}
13451359
@@ -1354,7 +1368,7 @@ static size_t cortexar_reg_write(target_s *const target, const uint32_t reg, con
13541368 if (max < reg_width )
13551369 return 0 ;
13561370 /* Finally, copy the new register data in and return the width */
1357- memcpy (reg_ptr , data , reg_width );
1371+ memcpy_swap (reg_ptr , data , reg_width );
13581372 return reg_width ;
13591373}
13601374
0 commit comments