Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: hwinfo: fix endianness for sam0 and nrf #24203

Merged

Conversation

sslupsky
Copy link
Contributor

@sslupsky sslupsky commented Apr 8, 2020

HWINFO drivers should be responsible for ensuring that
the data structure is a sequence of bytes. That is not
what the current sam0 and nordic drivers do. The drivers
read the data as u32_t and then memcpy the data to a
buffer. This ensures the data has the endianness of the
underlying MCU, which in this case is Cortex M0 which
is little endian.

This commit fixes the endianness so the data can be
interpreted as a "left to right sequence of bytes".

Fixes #23444
Fixes #24103

pabigot
pabigot previously approved these changes Apr 9, 2020
Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nordic code behaves as I expect, converting the 64-bit little-endian integer to a big-endian octet sequence.

Atmel code looks right by inspection given SAM D21 Family Data Sheet section 10.3.3:

Each device has a unique 128-bit serial number which is a concatenation of four 32-bit words contained at the following addresses.... The uniqueness of the serial number is guaranteed only when using all 128 bits.

@eobalski eobalski self-requested a review April 9, 2020 13:19
@eobalski eobalski added the area: USB Universal Serial Bus label Apr 9, 2020
@alexanderwachter
Copy link
Member

This change has a huge impact for existing devices. They will change their ID after updating the Zephyr version! Please announce this change in the Mailing list first and maybe ask @carlescufi for a slot to discuss it in the API meeting.

@alexanderwachter alexanderwachter added the area: API Changes to public APIs label Apr 9, 2020
@carlescufi
Copy link
Member

@sslupsky here's what I suggest we do with this change:

  1. Add an entry for this in the "API Changes" section of the 2.3 release notes in this PR
  2. Send an email to the mailing list

@carlescufi carlescufi added the DNM This PR should not be merged (Do Not Merge) label Apr 9, 2020
@carlescufi
Copy link
Member

DNM for now

@pabigot pabigot dismissed their stale review April 9, 2020 15:02

needs docs

@pabigot pabigot self-requested a review April 9, 2020 15:03
@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

@carlescufi This PR corrects a bug in the existing implementation for the sam0 and nordic drivers. The change does have an impact on other zephyr components as @pabigot pointed our regarding the Bluetooth workaround for this bug. That workaround will have to be addressed. I can include this in the "API Changes" but is it correct to call this an "API change"?

There may be other drivers affected but some are ok. For instance, I took a quick look at the sam driver yesterday and that one appears to be ok.

Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was apparently not clear originally (so things like USB were making assumptions about how to interpret it):

The documentation for this API should be updated to make clear that the returned value is not supposed to be interpreted based on vendor-specific assumptions of byte order. It should express the identifier as a raw byte sequence, doing any endian conversion necessary so that a hex representation of the bytes produces the intended serial number.

@stephanosio stephanosio added the Release Notes To be mentioned in the release notes label Apr 9, 2020
@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

Does sound ok for the API Changes:

  • HWINFO

    • HWINFO drivers are responsible for ensuring that the ID data structure is a
      sequence of bytes. The returned ID value is not supposed to be interpreted
      based on vendor-specific assumptions of byte order. It should express the
      identifier as a raw byte sequence, doing any endian conversion necessary so
      that a hex representation of the bytes produces the intended serial number.

@pabigot
Copy link
Collaborator

pabigot commented Apr 9, 2020

Does sound ok for the API Changes:

That looks like release notes. The hwinfo.h header needs to include this information.

@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

I added the text to hwinfo.h as well.

@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

I will skinny the release notes to:

  • HWINFO

    • HWINFO drivers are responsible for ensuring that the ID data structure is a
      sequence of bytes.

@alexanderwachter
Copy link
Member

@carlescufi This PR corrects a bug in the existing implementation for the sam0 and nordic drivers.

IMO it is still not a bug since the device doesn't apply any structure on the ID. But I agree that tools in production may interpret the values differently. So changing the byte-order is ok for me.

The change does have an impact on other zephyr components as @pabigot pointed our regarding the Bluetooth workaround for this bug. That workaround will have to be addressed. I can include this in the "API Changes" but is it correct to call this an "API change"?

The changes have an impact on users that use the hwinfo API to identify their devices!
That's a big surprise for everyone if the device changes the HARDWARE ID after a SOFTWARE update,

There may be other drivers affected but some are ok. For instance, I took a quick look at the sam driver yesterday and that one appears to be ok.

As mentioned before, this is a public API that is used by other users too. AFAIK foundries.io uses it to identify devices.
If the impact of this "bug" isn't too bad, I would recommend not to fix it.

@carlescufi
Copy link
Member

As mentioned before, this is a public API that is used by other users too. AFAIK foundries.io uses it to identify devices.
If the impact of this "bug" isn't too bad, I would recommend not to fix it.

Right, but the API was not stable until after the 2.2 release, so I see not problem in switching the endianness at this point. We have not pushed a release with the hwinfo API being stable, so I still support this change.

@carlescufi
Copy link
Member

I added the text to hwinfo.h as well.

@sslupsky
have you pushed? I don't see it in the PR yet

@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

I pushed the recommended changes.

@@ -21,6 +21,11 @@ No security vulnerabilities received.
API Changes
***********

* HWINFO

* HWINFO drivers are responsible for ensuring that the ID data structure is a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sslupsky could you add a bit more info here? About the actual change in API behavior, so users know what to expect. Please also list which platforms in particular changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Clarified the behaviour and referenced the platforms changed.

Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. As a follow-up we need to review all existing implementations to ensure that what they produce conforms to the "don't rearrange this" policy.

@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

Which mailing list? users?

@carlescufi
Copy link
Member

Which mailing list? users?

Yes, users@ and devel@ would be good actually. Thanks!

HWINFO drivers should be responsible for ensuring that
the data structure is a sequence of bytes. That is not
what the current sam0 and nordic drivers do. The drivers
read the data as u32_t and then memcpy the data to a
buffer. This ensures the data has the endianness of the
underlying MCU, which in this case is Cortex M0 which
is little endian.

This commit fixes the endianness so the data can be
interpreted as a "left to right sequence of bytes".

This commit updates the API doc to provide clarification
of the data structure.
Add to 2.3 release notes.

Fixes zephyrproject-rtos#23444, zephyrproject-rtos#24103

Signed-off-by: Steven Slupsky <sslupsky@gmail.com>
@sslupsky
Copy link
Contributor Author

sslupsky commented Apr 9, 2020

@carlescufi Sent email to users@ and devel@

@carlescufi carlescufi merged commit c54f7ec into zephyrproject-rtos:master Apr 9, 2020
@carlescufi carlescufi removed the DNM This PR should not be merged (Do Not Merge) label Apr 9, 2020
@pabigot
Copy link
Collaborator

pabigot commented Apr 10, 2020

To complete this task:

This covers all currently implemented targets.

@alexanderwachter
Copy link
Member

Thanks for updating all drivers @pabigot

@nandojve nandojve mentioned this pull request Apr 18, 2020
61 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Documentation area: USB Universal Serial Bus platform: Microchip SAM Microchip SAM Platform (formerly Atmel SAM) platform: nRF Nordic nRFx Release Notes To be mentioned in the release notes
Projects
None yet
8 participants