Skip to content

Commit b5a95ba

Browse files
brycewilkinsmmahadevan108
authored andcommitted
drivers: hwinfo: Fix mcux device id
This fixes an issue where the unique device identifier is incorrectly copied from an address on the AHB bus to memory using memcpy. I verified corrected behavior by comparing `hwinfo devid` command output to memory view of the SoC using the debugger. Signed-off-by: Bryce Wilkins <[email protected]>
1 parent 13714a6 commit b5a95ba

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

drivers/hwinfo/hwinfo_mcux_syscon.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@
1010
#include <string.h>
1111
#include <zephyr/sys/byteorder.h>
1212

13+
#define UID_WORD_COUNT (DT_INST_REG_SIZE(0) / sizeof(uint32_t))
14+
15+
struct uid {
16+
uint32_t id[UID_WORD_COUNT];
17+
};
18+
1319
ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
1420
{
15-
uint8_t *uid_addr = (uint8_t *) DT_INST_REG_ADDR(0);
21+
volatile const uint32_t * const uid_addr = (uint32_t *) DT_INST_REG_ADDR(0);
22+
struct uid dev_id;
1623

1724
if (buffer == NULL) {
1825
return 0;
1926
}
2027

21-
if (length > DT_INST_REG_SIZE(0)) {
22-
length = DT_INST_REG_SIZE(0);
28+
for (size_t i = 0 ; i < UID_WORD_COUNT ; i++) {
29+
dev_id.id[i] = sys_cpu_to_be32(uid_addr[i]);
30+
}
31+
32+
if (length > sizeof(dev_id.id)) {
33+
length = sizeof(dev_id.id);
2334
}
2435

25-
memcpy(buffer, uid_addr, length);
36+
memcpy(buffer, dev_id.id, length);
2637

2738
return length;
2839
}

0 commit comments

Comments
 (0)