Skip to content

Commit 94ebd27

Browse files
lawikfhunleth
authored andcommitted
Add more detail to ProvisioningInfo
This allows determining the way a chip is configured. Primarily useful to differentiate between volatile and regular NervesKey configurations.
1 parent fbbf9de commit 94ebd27

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

lib/nerves_key.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,19 @@ defmodule NervesKey do
507507
@doc """
508508
Return default provisioning info for a NervesKey
509509
510-
This function is used for pre-programmed NervesKey devices. The
511-
serial number is a Base32-encoded version of the ATECC508A/608A's globally unique
512-
serial number. No additional care is needed to keep the number unique.
510+
This function is particularly useful for pre-programmed NervesKey devices.
511+
Theserial number is a Base32-encoded version of the ATECC508A/608A's globally
512+
unique serial number. No additional care is needed to keep the number unique.
513+
514+
It also provides information about the provisioning mode. Helping identify a
515+
volatile configuration or custom, unrecognized configurations.
513516
"""
514517
@spec default_info(ATECC508A.Transport.t()) :: ProvisioningInfo.t()
515518
def default_info(transport) do
516-
{:ok, sn} = Config.device_sn(transport)
517-
518-
%ProvisioningInfo{manufacturer_sn: Base.encode32(sn, padding: false), board_name: "NervesKey"}
519+
case Config.provisioning_info(transport) do
520+
{:ok, info} -> info
521+
_ -> ProvisioningInfo.default()
522+
end
519523
end
520524

521525
@doc """

lib/nerves_key/config.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule NervesKey.Config do
1212
"""
1313

1414
alias ATECC508A.Configuration
15+
alias NervesKey.ProvisioningInfo
1516

1617
# See README.md for the SlotConfig and KeyConfig values. These are copied verbatim.
1718
@key_config <<0x33, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x1C,
@@ -422,6 +423,38 @@ defmodule NervesKey.Config do
422423
end
423424
end
424425

426+
@doc """
427+
Helper for getting provisioning information.
428+
"""
429+
@spec provisioning_info(ATECC508A.Transport.t()) ::
430+
{:error, atom()} | {:ok, ProvisioningInfo.t()}
431+
def provisioning_info(transport) do
432+
with {:ok, info} <- Configuration.read(transport) do
433+
mode =
434+
if info.lock_value == 0 do
435+
case Configuration.read_all_raw(transport) do
436+
{:ok, <<_::20-bytes, 0x87::8, 0x20::8, _::74-bytes, 0x33::8, 0x10::8, _::binary>>} ->
437+
:volatile
438+
439+
{:ok, <<_::20-bytes, 0x87::8, 0x20::8, _::binary>>} ->
440+
:regular
441+
442+
{:ok, _bin} ->
443+
:unrecognized
444+
end
445+
else
446+
:unprovisioned
447+
end
448+
449+
{:ok,
450+
%ProvisioningInfo{
451+
manufacturer_sn: Base.encode32(info.serial_number, padding: false),
452+
board_name: "NervesKey",
453+
mode: mode
454+
}}
455+
end
456+
end
457+
425458
@doc """
426459
Helper for getting information about the ATECC module.
427460
"""

lib/nerves_key/provisioning_info.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
defmodule NervesKey.ProvisioningInfo do
6-
defstruct [:manufacturer_sn, :board_name]
6+
defstruct manufacturer_sn: "", board_name: "NervesKey", mode: :unprovisioned
77

88
@type t :: %__MODULE__{
99
manufacturer_sn: binary(),
10-
board_name: binary()
10+
board_name: binary(),
11+
mode: :unprovisioned | :regular | :volatile | :unrecognized
1112
}
13+
14+
def default do
15+
%__MODULE__{}
16+
end
1217
end

0 commit comments

Comments
 (0)