Skip to content

hww: _api_info write byte for initialized status #1229

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

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

NicolaLS
Copy link
Contributor

Write another byte for the initialized status in the _api_info function. This field can be used to check whether a device can be unlocked or not. The unlocked byte is not sufficient because unlocked = false could mean the device is locked or the device is uninitialized.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

Awesome, thanks. Please add a CHANGELOG entry

src/hww.c Outdated
@@ -97,6 +99,10 @@ static size_t _api_info(uint8_t* buf)
*current = keystore_is_locked() ? 0x00 : 0x01;
current++;

// 1 byte initialized status
*current = memory_is_initialized() ? 0x00 : 0x01;
Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't 0x01 for "is_initialized" be more intuitive? If one interprets 0x00 as false and 0x01 as true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I just copy and pasted the above without thinking much. I'll change it, and also change the switch in the go api then.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

Thanks! Please also, in a new commit, adjust the Py lib in the same repo, add a CHANGELOG entry to the Py lib and bump the pylib version (major version bump as it's an incompatible change)

https://github.com/BitBoxSwiss/bitbox02-firmware/blob/master/py/bitbox02/bitbox02/communication/bitbox_api_protocol.py#L683

This is useful to get the version of the firmware when a usb descriptor is not available
(via BitBoxBridge, etc.).
Returns (version, platform, edition, unlocked, initialized).
This is useful to get the version of the firmware or the device unlocked/initialized status
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the This is useful (...) part could be improved but I don't understand it well enough and also I'm bad at constructing concise sentences.

-> this is useful if there was no handshake yet too, otherwise we could just call the other info request that already has the initialized attribute in the response...how could this be phrased ? (when I tried to use the other request in api-go it would throw an error like this: handshake must come first

but I guess it is fine to leave it as is?

@NicolaLS NicolaLS requested a review from benma June 18, 2024 13:51
Comment on lines 712 to 713
initialized_byte = response[0]
initialized = {0x00: False, 0x01: True}[initialized_byte]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will crash on firmware versions that don't support this yet. Maybe one way of dealing with this is to return Optional[bool] and document that it is None if not supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yes thanks, it would throw index out of range for initialized_byte = response[0] should I just try except it and set initialized to None in except or is there a better way of handling this (checking somehow if it is supported ?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can check the firmware version, there are other examples. For now let's expect this to go into v9.20.0, but we can adjust the number if it lands quicker.

@NicolaLS NicolaLS force-pushed the hww-info-initialized branch 2 times, most recently from 6fcdc15 to 5b74fa5 Compare June 21, 2024 01:56
@NicolaLS NicolaLS requested a review from benma June 21, 2024 01:58
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

CI complains about code formatting. Apply black py/bitbox02/bitbox02/communication/bitbox_api_protocol.py to format the file.

Returns (version, platform, edition, unlocked, initialized).
This is useful to get the version of the firmware or the device unlocked/initialized status
when a usb descriptor is not available (via BitBoxBridge, etc.). In case the firmware does not
respond with the initialized byte yet None is returned.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The docstring could be more explicit: "The initialized status is supported from firmware v9.20.0 (it is None if not supported)"

@NicolaLS NicolaLS force-pushed the hww-info-initialized branch from 5b74fa5 to c915e94 Compare June 25, 2024 14:28
@NicolaLS
Copy link
Contributor Author

NicolaLS commented Jun 25, 2024

@benma thanks! rebased and re-formatted/changed docstring as suggested.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

please also bump version strings to 9.20.0, which is now possible after we released 9.19.0

https://github.com/BitBoxSwiss/bitbox02-firmware/blob/master/CMakeLists.txt#L99-L100

return (version_str, platform, edition, unlocked)

initialized = None
if version_str >= semver.VersionInfo(9, 20, 0):
Copy link
Collaborator

@benma benma Jul 2, 2024

Choose a reason for hiding this comment

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

Please test your changes - this check does not work, it crashes because version_str is not a valid version string with the v prefix.

Use parse_device_version(version_str) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, thanks.

Write another byte for the initialized status in the _api_info function.
This field can be used to check whether a device can be unlocked or not.
The unlocked byte is not sufficient because unlocked = false could mean
the device is locked or the device is uninitialized.
@NicolaLS NicolaLS force-pushed the hww-info-initialized branch from c915e94 to 62b4f29 Compare July 2, 2024 14:52
The device now returns an additional byte from REQ_INFO: 0x00 if the
device is not initialized and 0x01 if it is. Read this byte in get_info
from the REQ_INFO response in case the firmware version supports it, and
add it to the tuple that is returned, None if the firmware does not
support it yet.
@NicolaLS NicolaLS force-pushed the hww-info-initialized branch from 62b4f29 to 349983a Compare July 2, 2024 15:01
@NicolaLS NicolaLS requested a review from benma July 2, 2024 15:21
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

tACK, thanks!

@benma benma merged commit 34b38f1 into BitBoxSwiss:master Jul 3, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants