For packed and/or unaligned accesses, the write functions reordered the
source buffer in place. Causing in the best case a segfault, in the worst
case silent data corruption.
Rewrite the data fetching to directly match the byte lane mapping
according to IHI0031C, without destroying the buffer.
Also slightly clean up variable usage and harmonize all the write
functions.
Change-Id: I9a01cfc5578653f9ec02043ff6b61a7a20f90d67
Signed-off-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
Reviewed-on: http://openocd.zylin.com/1646
Tested-by: jenkins
Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
-/*****************************************************************************
-* *
-* mem_ap_write_buf(struct adiv5_dap *dap, uint8_t *buffer, int count, uint32_t address, bool addr_incr) *
-* *
-* Write a buffer in target order (little endian) *
-* *
-*****************************************************************************/
int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address, bool addr_incr)
{
int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
uint32_t adr = address;
int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address, bool addr_incr)
{
int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
uint32_t adr = address;
- const uint8_t *pBuffer = buffer;
- uint32_t incr_flag = CSW_ADDRINC_OFF;
-
- count >>= 2;
- wcount = count;
-
- /* if we have an unaligned access - reorder data */
- if (adr & 0x3u) {
- for (writecount = 0; writecount < count; writecount++) {
- int i;
- uint32_t outvalue;
- memcpy(&outvalue, pBuffer, sizeof(uint32_t));
+ uint32_t incr_flag = addr_incr ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
- for (i = 0; i < 4; i++) {
- *((uint8_t *)pBuffer + (adr & 0x3)) = outvalue;
- outvalue >>= 8;
- adr++;
- }
- pBuffer += sizeof(uint32_t);
- }
- }
- /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+ /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
blocksize = wcount;
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
blocksize = wcount;
if (blocksize == 0)
blocksize = 1;
if (blocksize == 0)
blocksize = 1;
- if (addr_incr)
- incr_flag = CSW_ADDRINC_SINGLE;
-
retval = dap_setup_accessport(dap, CSW_32BIT | incr_flag, address);
if (retval != ERROR_OK)
return retval;
for (writecount = 0; writecount < blocksize; writecount++) {
retval = dap_setup_accessport(dap, CSW_32BIT | incr_flag, address);
if (retval != ERROR_OK)
return retval;
for (writecount = 0; writecount < blocksize; writecount++) {
- uint32_t tmp;
- tmp = buf_get_u32(buffer + 4 * writecount, 0, 32);
- retval = dap_queue_ap_write(dap, AP_REG_DRW, tmp);
+ uint32_t outvalue = 0;
+ outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+
+ retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
break;
}
retval = dap_run(dap);
if (retval == ERROR_OK) {
if (retval != ERROR_OK)
break;
}
retval = dap_run(dap);
if (retval == ERROR_OK) {
- wcount = wcount - blocksize;
- address = address + 4 * blocksize;
- buffer = buffer + 4 * blocksize;
+ address += 4 * blocksize;
const uint8_t *buffer, int count, uint32_t address)
{
int retval = ERROR_OK;
const uint8_t *buffer, int count, uint32_t address)
{
int retval = ERROR_OK;
- int wcount, blocksize, writecount, i;
+ int wcount, blocksize, writecount;
wcount = count >> 1;
while (wcount > 0) {
int nbytes;
wcount = count >> 1;
while (wcount > 0) {
int nbytes;
- /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+ /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
- address += nbytes >> 1;
+ address += nbytes;
+ buffer += nbytes;
- uint32_t outvalue;
- memcpy(&outvalue, buffer, sizeof(uint32_t));
- for (i = 0; i < nbytes; i++) {
- *((uint8_t *)buffer + (address & 0x3)) = outvalue;
- outvalue >>= 8;
- address++;
- }
+ uint32_t outvalue = 0;
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
- memcpy(&outvalue, buffer, sizeof(uint32_t));
retval = dap_queue_ap_write(dap,
AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
retval = dap_queue_ap_write(dap,
AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
writecount -= nbytes >> 1;
} while (writecount);
writecount -= nbytes >> 1;
} while (writecount);
retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
if (retval != ERROR_OK)
return retval;
retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
if (retval != ERROR_OK)
return retval;
- uint16_t svalue;
- memcpy(&svalue, buffer, sizeof(uint16_t));
- uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
+
+ uint32_t outvalue = 0;
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+
retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
break;
retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
break;
- address += 2;
- buffer += 2;
const uint8_t *buffer, int count, uint32_t address)
{
int retval = ERROR_OK;
const uint8_t *buffer, int count, uint32_t address)
{
int retval = ERROR_OK;
- int wcount, blocksize, writecount, i;
+ int wcount, blocksize, writecount;
wcount = count;
while (wcount > 0) {
int nbytes;
wcount = count;
while (wcount > 0) {
int nbytes;
- /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+ /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
if (wcount < blocksize)
- uint32_t outvalue;
- memcpy(&outvalue, buffer, sizeof(uint32_t));
- for (i = 0; i < nbytes; i++) {
- *((uint8_t *)buffer + (address & 0x3)) = outvalue;
- outvalue >>= 8;
- address++;
- }
+ uint32_t outvalue = 0;
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+ outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
- memcpy(&outvalue, buffer, sizeof(uint32_t));
retval = dap_queue_ap_write(dap,
AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
retval = dap_queue_ap_write(dap,
AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
writecount -= nbytes;
} while (writecount);
writecount -= nbytes;
} while (writecount);
retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
if (retval != ERROR_OK)
return retval;
retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
if (retval != ERROR_OK)
return retval;
- uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
+ uint32_t outvalue = (uint32_t)*buffer++ << 8 * (address++ & 0x3);
retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
break;
retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
if (retval != ERROR_OK)
break;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)