add trng test and management commands

This commit is contained in:
bunnie 2021-06-08 21:29:23 +08:00
parent 2174ab7677
commit abbd1574e5
8 changed files with 394 additions and 86 deletions

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-06-08 00:08:29]]></description>
<description><![CDATA[Litex SoC 2021-06-08 20:07:03]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1210,10 +1210,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``54f46891``, so this value is 1425303697, which should be enough to check out
``a97001d2``, so this value is 2842690002, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x54f46891</resetValue>
<resetValue>0xa97001d2</resetValue>
<size>32</size>
<fields>
<field>
@ -1230,7 +1230,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x0f</resetValue>
<resetValue>0x10</resetValue>
<size>32</size>
<fields>
<field>
@ -3439,7 +3439,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x5243fcb</resetValue>
<resetValue>0x67ec0023</resetValue>
<size>32</size>
<fields>
<field>
@ -3454,7 +3454,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x34203b8b</resetValue>
<resetValue>0x64b8f094</resetValue>
<size>32</size>
<fields>
<field>
@ -4235,7 +4235,7 @@ avalanche generator]]></description>
<register>
<name>RO_NIST</name>
<addressOffset>0x0018</addressOffset>
<resetValue>0x1a433</resetValue>
<resetValue>0x1a443</resetValue>
<size>32</size>
<fields>
<field>
@ -4306,14 +4306,21 @@ generators]]></description>
<msb>7</msb>
<bitRange>[7:4]</bitRange>
<lsb>4</lsb>
<description><![CDATA[Inducates a failure in the repcount test for the ring oscillators]]></description>
<description><![CDATA[Indicates a failure in the repcount test for the ring oscillators]]></description>
</field>
<field>
<name>ro_adaptive</name>
<msb>11</msb>
<bitRange>[11:8]</bitRange>
<lsb>8</lsb>
<description><![CDATA[Inducates a failure in the adaptive proportion test for the ring oscillators]]></description>
<description><![CDATA[Indicates a failure in the adaptive proportion test for the ring oscillators]]></description>
</field>
<field>
<name>ro_miniruns</name>
<msb>15</msb>
<bitRange>[15:12]</bitRange>
<lsb>12</lsb>
<description><![CDATA[Indicates a failure in the miniruns test]]></description>
</field>
</fields>
</register>
@ -4342,7 +4349,7 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
@ -4371,7 +4378,7 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
@ -4400,7 +4407,7 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
@ -4429,7 +4436,7 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
@ -4458,7 +4465,7 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
@ -4487,13 +4494,123 @@ generators]]></description>
<msb>17</msb>
<bitRange>[17:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[instantaneous `b` value for core 0 repetition count test]]></description>
<description><![CDATA[max `b` value for core 0 repetition count test]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUNSLIMIT1</name>
<addressOffset>0x003c</addressOffset>
<resetValue>0x1269b3</resetValue>
<size>32</size>
<fields>
<field>
<name>min</name>
<msb>10</msb>
<bitRange>[10:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Minimum runs limit for runs of length 1]]></description>
</field>
<field>
<name>max</name>
<msb>21</msb>
<bitRange>[21:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[Minimum runs limit for runs of length 1]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUNSLIMIT2</name>
<addressOffset>0x0040</addressOffset>
<resetValue>0xa10bd</resetValue>
<size>32</size>
<fields>
<field>
<name>min</name>
<msb>10</msb>
<bitRange>[10:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Minimum runs limit for runs of length 2]]></description>
</field>
<field>
<name>max</name>
<msb>21</msb>
<bitRange>[21:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[Minimum runs limit for runs of length 2]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUNSLIMIT3</name>
<addressOffset>0x0044</addressOffset>
<resetValue>0x5984d</resetValue>
<size>32</size>
<fields>
<field>
<name>min</name>
<msb>10</msb>
<bitRange>[10:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Minimum runs limit for runs of length 3]]></description>
</field>
<field>
<name>max</name>
<msb>21</msb>
<bitRange>[21:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[Minimum runs limit for runs of length 3]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUNSLIMIT4</name>
<addressOffset>0x0048</addressOffset>
<resetValue>0x3281a</resetValue>
<size>32</size>
<fields>
<field>
<name>min</name>
<msb>10</msb>
<bitRange>[10:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Minimum runs limit for runs of length 4]]></description>
</field>
<field>
<name>max</name>
<msb>21</msb>
<bitRange>[21:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[Minimum runs limit for runs of length 4]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUNSLIMIT5</name>
<addressOffset>0x004c</addressOffset>
<resetValue>0x1d805</resetValue>
<size>32</size>
<fields>
<field>
<name>min</name>
<msb>10</msb>
<bitRange>[10:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Minimum runs limit for runs of length 5]]></description>
</field>
<field>
<name>max</name>
<msb>21</msb>
<bitRange>[21:11]</bitRange>
<lsb>11</lsb>
<description><![CDATA[Minimum runs limit for runs of length 5]]></description>
</field>
</fields>
</register>
<register>
<name>RO_RUN0_CTRL</name>
<addressOffset>0x003c</addressOffset>
<addressOffset>0x0050</addressOffset>
<resetValue>0x400</resetValue>
<size>32</size>
<fields>
@ -4511,7 +4628,7 @@ undefined behavior occurs]]></description>
<name>RO_RUN0_FRESH</name>
<description><![CDATA[The current data corresponding to a runlength of (bitposition +1) has been
updated at least once since the last readout of any register]]></description>
<addressOffset>0x0040</addressOffset>
<addressOffset>0x0054</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4526,7 +4643,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN0_COUNT1</name>
<description><![CDATA[Count of sequence length 1 runs seen in the past window]]></description>
<addressOffset>0x0044</addressOffset>
<addressOffset>0x0058</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4541,7 +4658,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN0_COUNT2</name>
<description><![CDATA[Count of sequence length 2 runs seen in the past window]]></description>
<addressOffset>0x0048</addressOffset>
<addressOffset>0x005c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4556,7 +4673,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN0_COUNT3</name>
<description><![CDATA[Count of sequence length 3 runs seen in the past window]]></description>
<addressOffset>0x004c</addressOffset>
<addressOffset>0x0060</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4571,7 +4688,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN0_COUNT4</name>
<description><![CDATA[Count of sequence length 4 runs seen in the past window]]></description>
<addressOffset>0x0050</addressOffset>
<addressOffset>0x0064</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4586,7 +4703,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN0_COUNT5</name>
<description><![CDATA[Count of sequence length 5 runs seen in the past window]]></description>
<addressOffset>0x0054</addressOffset>
<addressOffset>0x0068</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4600,7 +4717,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>RO_RUN1_CTRL</name>
<addressOffset>0x0058</addressOffset>
<addressOffset>0x006c</addressOffset>
<resetValue>0x400</resetValue>
<size>32</size>
<fields>
@ -4618,7 +4735,7 @@ undefined behavior occurs]]></description>
<name>RO_RUN1_FRESH</name>
<description><![CDATA[The current data corresponding to a runlength of (bitposition +1) has been
updated at least once since the last readout of any register]]></description>
<addressOffset>0x005c</addressOffset>
<addressOffset>0x0070</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4633,7 +4750,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN1_COUNT1</name>
<description><![CDATA[Count of sequence length 1 runs seen in the past window]]></description>
<addressOffset>0x0060</addressOffset>
<addressOffset>0x0074</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4648,7 +4765,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN1_COUNT2</name>
<description><![CDATA[Count of sequence length 2 runs seen in the past window]]></description>
<addressOffset>0x0064</addressOffset>
<addressOffset>0x0078</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4663,7 +4780,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN1_COUNT3</name>
<description><![CDATA[Count of sequence length 3 runs seen in the past window]]></description>
<addressOffset>0x0068</addressOffset>
<addressOffset>0x007c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4678,7 +4795,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN1_COUNT4</name>
<description><![CDATA[Count of sequence length 4 runs seen in the past window]]></description>
<addressOffset>0x006c</addressOffset>
<addressOffset>0x0080</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4693,7 +4810,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN1_COUNT5</name>
<description><![CDATA[Count of sequence length 5 runs seen in the past window]]></description>
<addressOffset>0x0070</addressOffset>
<addressOffset>0x0084</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4707,7 +4824,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>RO_RUN2_CTRL</name>
<addressOffset>0x0074</addressOffset>
<addressOffset>0x0088</addressOffset>
<resetValue>0x400</resetValue>
<size>32</size>
<fields>
@ -4725,7 +4842,7 @@ undefined behavior occurs]]></description>
<name>RO_RUN2_FRESH</name>
<description><![CDATA[The current data corresponding to a runlength of (bitposition +1) has been
updated at least once since the last readout of any register]]></description>
<addressOffset>0x0078</addressOffset>
<addressOffset>0x008c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4740,7 +4857,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN2_COUNT1</name>
<description><![CDATA[Count of sequence length 1 runs seen in the past window]]></description>
<addressOffset>0x007c</addressOffset>
<addressOffset>0x0090</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4755,7 +4872,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN2_COUNT2</name>
<description><![CDATA[Count of sequence length 2 runs seen in the past window]]></description>
<addressOffset>0x0080</addressOffset>
<addressOffset>0x0094</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4770,7 +4887,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN2_COUNT3</name>
<description><![CDATA[Count of sequence length 3 runs seen in the past window]]></description>
<addressOffset>0x0084</addressOffset>
<addressOffset>0x0098</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4785,7 +4902,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN2_COUNT4</name>
<description><![CDATA[Count of sequence length 4 runs seen in the past window]]></description>
<addressOffset>0x0088</addressOffset>
<addressOffset>0x009c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4800,7 +4917,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN2_COUNT5</name>
<description><![CDATA[Count of sequence length 5 runs seen in the past window]]></description>
<addressOffset>0x008c</addressOffset>
<addressOffset>0x00a0</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4814,7 +4931,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>RO_RUN3_CTRL</name>
<addressOffset>0x0090</addressOffset>
<addressOffset>0x00a4</addressOffset>
<resetValue>0x400</resetValue>
<size>32</size>
<fields>
@ -4832,7 +4949,7 @@ undefined behavior occurs]]></description>
<name>RO_RUN3_FRESH</name>
<description><![CDATA[The current data corresponding to a runlength of (bitposition +1) has been
updated at least once since the last readout of any register]]></description>
<addressOffset>0x0094</addressOffset>
<addressOffset>0x00a8</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4847,7 +4964,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN3_COUNT1</name>
<description><![CDATA[Count of sequence length 1 runs seen in the past window]]></description>
<addressOffset>0x0098</addressOffset>
<addressOffset>0x00ac</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4862,7 +4979,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN3_COUNT2</name>
<description><![CDATA[Count of sequence length 2 runs seen in the past window]]></description>
<addressOffset>0x009c</addressOffset>
<addressOffset>0x00b0</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4877,7 +4994,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN3_COUNT3</name>
<description><![CDATA[Count of sequence length 3 runs seen in the past window]]></description>
<addressOffset>0x00a0</addressOffset>
<addressOffset>0x00b4</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4892,7 +5009,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN3_COUNT4</name>
<description><![CDATA[Count of sequence length 4 runs seen in the past window]]></description>
<addressOffset>0x00a4</addressOffset>
<addressOffset>0x00b8</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4907,7 +5024,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>RO_RUN3_COUNT5</name>
<description><![CDATA[Count of sequence length 5 runs seen in the past window]]></description>
<addressOffset>0x00a8</addressOffset>
<addressOffset>0x00bc</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4921,7 +5038,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION0_CTRL</name>
<addressOffset>0x00ac</addressOffset>
<addressOffset>0x00c0</addressOffset>
<resetValue>0x190140</resetValue>
<size>32</size>
<fields>
@ -4950,7 +5067,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION0_STAT</name>
<addressOffset>0x00b0</addressOffset>
<addressOffset>0x00c4</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4972,7 +5089,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION0_LAST_ERR</name>
<addressOffset>0x00b4</addressOffset>
<addressOffset>0x00c8</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -4994,7 +5111,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION1_CTRL</name>
<addressOffset>0x00b8</addressOffset>
<addressOffset>0x00cc</addressOffset>
<resetValue>0x190140</resetValue>
<size>32</size>
<fields>
@ -5023,7 +5140,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION1_STAT</name>
<addressOffset>0x00bc</addressOffset>
<addressOffset>0x00d0</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -5045,7 +5162,7 @@ updated at least once since the last readout of any register]]></description>
</register>
<register>
<name>AV_EXCURSION1_LAST_ERR</name>
<addressOffset>0x00c0</addressOffset>
<addressOffset>0x00d4</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -5065,10 +5182,39 @@ updated at least once since the last readout of any register]]></description>
</field>
</fields>
</register>
<register>
<name>READY</name>
<addressOffset>0x00d8</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>av_excursion</name>
<msb>1</msb>
<bitRange>[1:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[ready bits from the excursion test]]></description>
</field>
<field>
<name>av_adaprop</name>
<msb>3</msb>
<bitRange>[3:2]</bitRange>
<lsb>2</lsb>
<description><![CDATA[ready bits from the adaptive proportion test]]></description>
</field>
<field>
<name>ro_adaprop</name>
<msb>7</msb>
<bitRange>[7:4]</bitRange>
<lsb>4</lsb>
<description><![CDATA[ready bits from the adaptive proportion test]]></description>
</field>
</fields>
</register>
<register>
<name>EV_STATUS</name>
<description><![CDATA[Triggered by a failure in the avalanche generator core 1 on-line excursion test]]></description>
<addressOffset>0x00c4</addressOffset>
<addressOffset>0x00dc</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -5112,7 +5258,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>EV_PENDING</name>
<description><![CDATA[Triggered by a failure in the avalanche generator core 1 on-line excursion test]]></description>
<addressOffset>0x00c8</addressOffset>
<addressOffset>0x00e0</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -5156,7 +5302,7 @@ updated at least once since the last readout of any register]]></description>
<register>
<name>EV_ENABLE</name>
<description><![CDATA[Triggered by a failure in the avalanche generator core 1 on-line excursion test]]></description>
<addressOffset>0x00cc</addressOffset>
<addressOffset>0x00e4</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -5200,7 +5346,7 @@ updated at least once since the last readout of any register]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0xd0</size>
<size>0xe8</size>
<usage>registers</usage>
</addressBlock>
<interrupt>

Binary file not shown.

View file

@ -92,6 +92,7 @@ mod accel; use accel::*;
mod sha; use sha::*;
mod ecup; use ecup::*;
mod aes; use aes::*;
mod trng_cmd; use trng_cmd::*;
mod fcc; use fcc::*;
mod pds; // dependency of the FCC file
@ -112,6 +113,7 @@ pub struct CmdEnv {
sha_cmd: Sha,
ecup_cmd: EcUpdate,
aes_cmd: Aes,
trng_cmd: TrngCmd,
fcc_cmd: Fcc,
}
@ -146,6 +148,7 @@ impl CmdEnv {
sha_cmd: sha,
ecup_cmd: ecup,
aes_cmd: aes,
trng_cmd: TrngCmd::new(),
fcc_cmd: fcc,
}
@ -175,6 +178,7 @@ impl CmdEnv {
&mut self.sha_cmd,
&mut self.ecup_cmd,
&mut self.aes_cmd,
&mut self.trng_cmd,
&mut self.fcc_cmd,
];

View file

@ -0,0 +1,82 @@
use crate::{ShellCmdApi, CommonEnv};
use xous_ipc::String;
#[derive(Debug)]
pub struct TrngCmd {
}
impl TrngCmd {
pub fn new() -> Self {
TrngCmd {}
}
}
impl<'a> ShellCmdApi<'a> for TrngCmd {
cmd_api!(trng);
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
use core::fmt::Write;
let mut ret = String::<1024>::new();
let helpstring = "trng [avnist] [ronist] [runs] [excur] [errs] [pump]";
let mut tokens = args.as_str().unwrap().split(' ');
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"avnist" => {
let ht = env.trng.get_health_tests().unwrap();
write!(ret, "AV NIST stats: {:?}", ht.av_nist).unwrap();
}
"ronist" => {
let ht = env.trng.get_health_tests().unwrap();
write!(ret, "RO NIST stats: {:?}", ht.ro_nist).unwrap();
}
"runs" => {
let ht = env.trng.get_health_tests().unwrap();
for core in 0..4 {
write!(ret, "RO {}: ", core).unwrap();
for bin in 0..5 {
write!(ret, "{} ", ht.ro_miniruns[core].run_count[bin]).unwrap();
}
write!(ret, "\n").unwrap();
}
}
"excur" => {
let ht = env.trng.get_health_tests().unwrap();
write!(ret, "AV0: {:.0}/{:.0} mV\n"
,(ht.av_excursion[0].min as f64 / 4096.0) * 1000.0
,(ht.av_excursion[0].max as f64 / 4096.0) * 1000.0
).unwrap();
write!(ret, "AV0 delta: {:.0} mV\n"
,((ht.av_excursion[0].max as f64 - ht.av_excursion[0].min as f64) / 4096.0) * 1000.0
).unwrap();
write!(ret, "AV1: {:.0}/{:.0} mV\n"
,(ht.av_excursion[1].min as f64 / 4096.0) * 1000.0
,(ht.av_excursion[1].max as f64 / 4096.0) * 1000.0
).unwrap();
write!(ret, "AV1 delta: {:.0} mV\n"
,((ht.av_excursion[1].max as f64 - ht.av_excursion[1].min as f64) / 4096.0) * 1000.0
).unwrap();
}
"pump" => {
for i in 0..4 {
log::debug!("pump round {}", i);
let mut buf: [u32; 1024] = [0; 1024];
env.trng.fill_buf(&mut buf).unwrap();
log::debug!("pump samples: {:x}, {:x}, {:x}", buf[0], buf[512], buf[1023]);
}
write!(ret, "Pumped 4x1k values out of the engine").unwrap();
}
"errs" => {
write!(ret, "TRNG error stats: {:?}", env.trng.get_error_stats().unwrap()).unwrap();
}
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
} else {
write!(ret, "{}", helpstring).unwrap();
}
Ok(Some(ret))
}
}

View file

@ -258,7 +258,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_";
#[xous::xous_main]
fn xmain() -> ! {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Debug);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();

View file

@ -36,11 +36,20 @@ pub struct TrngErrors {
pub server_underruns: Option<u16>,
}
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct TrngBuf {
pub data: [u32; 1024],
pub len: u16,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Opcode {
/// Get one or two 32-bit words of TRNG data
GetTrng,
/// Fill a buffer with random data
FillTrng,
/// Suspend/resume callback
SuspendResume,

View file

@ -41,6 +41,23 @@ impl Trng {
panic!("unexpected return value: {:#?}", response);
}
}
pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> {
let mut tb = api::TrngBuf { data: [0; 1024], len: 0 };
if data.len() > tb.data.len() {
return Err(xous::Error::OutOfMemory);
}
tb.len = data.len() as u16;
let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::FillTrng.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let rtb: api::TrngBuf = buf.to_original().unwrap();
if rtb.len as usize != data.len() {
return Err(xous::Error::InternalError)
}
for (&src, dst) in rtb.data.iter().zip(data.iter_mut()) {
*dst = src;
}
Ok(())
}
pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
if self.error_sid.is_none() {
let sid = xous::create_server().unwrap();

View file

@ -20,7 +20,7 @@ struct ScalarCallback {
#[cfg(target_os = "none")]
mod implementation {
use utralib::generated::*;
use crate::api::{ExcursionTest, MiniRunsTest, NistTests, HealthTests, TrngErrors};
use crate::api::{ExcursionTest, MiniRunsTest, NistTests, HealthTests, TrngErrors, TrngBuf};
use susres::{RegManager, RegOrField, SuspendResume};
use num_traits::*;
@ -29,10 +29,13 @@ mod implementation {
susres_manager: RegManager::<{utra::trng_server::TRNG_SERVER_NUMREGS}>, // probably can be reduced to save space?
conn: xous::CID,
errors: TrngErrors,
err_stat: HealthTests,
}
fn trng_handler(_irq_no: usize, arg: *mut usize) {
let trng = unsafe { &mut *(arg as *mut Trng) };
// cache a copy of the stats in the interrupt handler, so we can diagnose later
trng.err_stat = trng.get_tests();
let pending = trng.csr.r(utra::trng_server::EV_PENDING);
if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION0, 1)) != 0 {
@ -75,14 +78,15 @@ mod implementation {
trng.errors.kernel_underruns = Some(trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) as u16);
}
}
// reset any error flags
// reset any error flags. try to do this a bit away from the pending clear, so it has time to take effect
trng.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1);
// clear the pending interrupt(s)
trng.csr.wo(utra::trng_server::EV_PENDING, pending);
// notify the main loop of the error condition
xous::try_send_message(trng.conn,
xous::Message::new_scalar(crate::api::Opcode::ErrorNotification.to_usize().unwrap(), 0, 0, 0, 0)).map(|_|()).unwrap();
// clear the pending interrupt(s)
trng.csr.wo(utra::trng_server::EV_PENDING, pending);
}
impl Trng {
@ -107,7 +111,8 @@ mod implementation {
ro_adaptive_errs: None,
kernel_underruns: None,
server_underruns: None,
}
},
err_stat: HealthTests::default(),
};
///// configure power settings and which generator to use
@ -203,8 +208,23 @@ mod implementation {
pub fn get_errors(&self) -> TrngErrors {
self.errors
}
pub fn get_err_stats(&self) -> HealthTests {
self.err_stat
}
pub fn get_tests(&self) -> HealthTests {
// the fresh bit gets reset on the first read of the register. Ensure these reads happen first before the structure is initialized.
let av_nist_fresh0 = if self.csr.rf(utra::trng_server::NIST_AV_STAT0_FRESH) == 0 {false} else {true};
let av_nist_fresh1 = if self.csr.rf(utra::trng_server::NIST_AV_STAT1_FRESH) == 0 {false} else {true};
let ro_mr_fresh0 = if self.csr.rf(utra::trng_server::RO_RUN0_FRESH_RO_RUN0_FRESH) == 0 {false} else {true};
let ro_mr_fresh1 = if self.csr.rf(utra::trng_server::RO_RUN1_FRESH_RO_RUN1_FRESH) == 0 {false} else {true};
let ro_mr_fresh2 = if self.csr.rf(utra::trng_server::RO_RUN2_FRESH_RO_RUN2_FRESH) == 0 {false} else {true};
let ro_mr_fresh3 = if self.csr.rf(utra::trng_server::RO_RUN3_FRESH_RO_RUN3_FRESH) == 0 {false} else {true};
let ro_nist_fresh0 = if self.csr.rf(utra::trng_server::NIST_RO_STAT0_FRESH) == 0 {false} else {true};
let ro_nist_fresh1 = if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true};
let ro_nist_fresh2 = if self.csr.rf(utra::trng_server::NIST_RO_STAT2_FRESH) == 0 {false} else {true};
let ro_nist_fresh3 = if self.csr.rf(utra::trng_server::NIST_RO_STAT3_FRESH) == 0 {false} else {true};
// now initialize the return structure
HealthTests {
av_excursion: [
ExcursionTest {
@ -218,18 +238,19 @@ mod implementation {
],
av_nist: [
NistTests {
fresh: av_nist_fresh0,
adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT0_FRESH) == 0 {false} else {true},
},
NistTests {
fresh: av_nist_fresh1,
adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT1_FRESH) == 0 {false} else {true},
},
],
ro_miniruns: [
MiniRunsTest {
fresh: ro_mr_fresh0,
run_count: [
self.csr.r(utra::trng_server::RO_RUN0_COUNT1) as u16,
self.csr.r(utra::trng_server::RO_RUN0_COUNT2) as u16,
@ -237,9 +258,9 @@ mod implementation {
self.csr.r(utra::trng_server::RO_RUN0_COUNT4) as u16,
self.csr.r(utra::trng_server::RO_RUN0_COUNT5) as u16,
],
fresh: if self.csr.rf(utra::trng_server::RO_RUN0_FRESH_RO_RUN0_FRESH) == 0 {false} else {true},
},
MiniRunsTest {
fresh: ro_mr_fresh1,
run_count: [
self.csr.r(utra::trng_server::RO_RUN1_COUNT1) as u16,
self.csr.r(utra::trng_server::RO_RUN1_COUNT2) as u16,
@ -247,9 +268,9 @@ mod implementation {
self.csr.r(utra::trng_server::RO_RUN1_COUNT4) as u16,
self.csr.r(utra::trng_server::RO_RUN1_COUNT5) as u16,
],
fresh: if self.csr.rf(utra::trng_server::RO_RUN1_FRESH_RO_RUN1_FRESH) == 0 {false} else {true},
},
MiniRunsTest {
fresh: ro_mr_fresh2,
run_count: [
self.csr.r(utra::trng_server::RO_RUN2_COUNT1) as u16,
self.csr.r(utra::trng_server::RO_RUN2_COUNT2) as u16,
@ -257,9 +278,9 @@ mod implementation {
self.csr.r(utra::trng_server::RO_RUN2_COUNT4) as u16,
self.csr.r(utra::trng_server::RO_RUN2_COUNT5) as u16,
],
fresh: if self.csr.rf(utra::trng_server::RO_RUN2_FRESH_RO_RUN2_FRESH) == 0 {false} else {true},
},
MiniRunsTest {
fresh: ro_mr_fresh3,
run_count: [
self.csr.r(utra::trng_server::RO_RUN3_COUNT1) as u16,
self.csr.r(utra::trng_server::RO_RUN3_COUNT2) as u16,
@ -267,37 +288,46 @@ mod implementation {
self.csr.r(utra::trng_server::RO_RUN3_COUNT4) as u16,
self.csr.r(utra::trng_server::RO_RUN3_COUNT5) as u16,
],
fresh: if self.csr.rf(utra::trng_server::RO_RUN3_FRESH_RO_RUN3_FRESH) == 0 {false} else {true},
},
],
ro_nist: [
NistTests {
fresh: ro_nist_fresh0,
adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT0_FRESH) == 0 {false} else {true},
},
NistTests {
fresh: ro_nist_fresh1,
adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true},
},
NistTests {
fresh: ro_nist_fresh2,
adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true},
},
NistTests {
fresh: ro_nist_fresh3,
adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16,
repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16,
fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true},
},
]
}
}
pub fn get_data_eager(&self) -> u32 {
pub fn get_data_eager(&mut self) -> u32 {
let mut timeout = 0;
while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 {
if timeout > 100 {
log::debug!("TRNG ran out of data, blocked on READY: 0x{:x}", self.csr.r(utra::trng_server::READY));
log::debug!("ROstats: 0x{:x} 0x{:x} 0x{:x} 0x{:x}",
self.csr.r(utra::trng_server::NIST_RO_STAT0), self.csr.r(utra::trng_server::NIST_RO_STAT1),
self.csr.r(utra::trng_server::NIST_RO_STAT2), self.csr.r(utra::trng_server::NIST_RO_STAT3));
self.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1);
timeout = 0;
}
xous::yield_slice();
timeout += 1;
}
self.csr.rf(utra::trng_server::DATA_DATA)
}
@ -309,20 +339,18 @@ mod implementation {
}
}
pub fn get_trng(&self, count: usize) -> [u32; 2] {
// TODO: use SHA hardware unit to robustify the TRNG output against potential hardware failures
// TODO: health monitoring of raw TRNG output
pub fn get_buf(&mut self, len: u16) -> TrngBuf {
let mut tb = TrngBuf {data: [0; 1024], len};
for i in 0..len as usize {
tb.data[i] = self.get_data_eager();
}
tb
}
pub fn get_trng(&mut self, count: usize) -> [u32; 2] {
let mut ret: [u32; 2] = [0, 0];
/*
in the final implementation the algorithm should be:
1) check fullness of software-whitened pool
2) if software pool is full enough, return values from there
3) if pool is low, activate hardware TRNG and refill the pool (uses SHA unit)
4) during pool-filling, perform statistics on the hardware TRNG output to check health
5) confirm health is OK
*/
// eventually this will come from a hardware 'urandom' style interface
// for now, we just take data directly from the hardware-managed raw TRNG pool
ret[0] = self.get_data_eager();
// we don't just draw down TRNGs if not requested, because they are a finite resource
@ -347,16 +375,16 @@ mod implementation {
// a stub to try to avoid breaking hosted mode for as long as possible.
#[cfg(not(target_os = "none"))]
mod implementation {
use log::info;
pub struct Trng {
seed: u32,
msgcount: u16, // re-print the message every time we rollover
}
impl Trng {
pub fn new() -> Trng {
Trng {
seed: 0x1afe_cafe,
msgcount: 0,
}
}
@ -370,8 +398,24 @@ mod implementation {
#[allow(dead_code)]
pub fn wait_full(&self) { }
pub fn get_buf(&self, len: u16) -> TrngBuf {
if msgcount < 10 {
log::info!("hosted mode TRNG is *not* random, it is an LFSR");
}
self.msgcount += 1;
let mut tb = TrngBuf {data: [0; 1024], len};
for i in 0..len as usize {
self.seed = self.move_lfsr(self.seed);
tb.data[i] = self.seed;
}
tb
}
pub fn get_trng(&mut self, _count: usize) -> [u32; 2] {
info!("hosted mode TRNG is *not* random, it is an LFSR");
if msgcount < 10 {
log::info!("hosted mode TRNG is *not* random, it is an LFSR");
}
self.msgcount += 1;
let mut ret: [u32; 2] = [0; 2];
self.seed = self.move_lfsr(self.seed);
ret[0] = self.seed;
@ -566,7 +610,7 @@ fn xmain() -> ! {
use crate::implementation::Trng;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Debug);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
@ -613,6 +657,7 @@ fn xmain() -> ! {
},
Some(api::Opcode::ErrorNotification) => {
log::error!("Got an error condition in the TRNG. Syndrome: {:?}", trng.get_errors());
log::error!("Stats: {:?}", trng.get_err_stats());
send_event(&error_cb_conns);
},
Some(api::Opcode::HealthStats) => {
@ -623,6 +668,11 @@ fn xmain() -> ! {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
buffer.replace(trng.get_errors()).unwrap();
},
Some(api::Opcode::FillTrng) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let len = buffer.as_flat::<TrngBuf, _>().unwrap().len;
buffer.replace(trng.get_buf(len)).unwrap();
},
Some(api::Opcode::Quit) => {
break
},