diff options
author | Nicolas Boichat <drinkcat@google.com> | 2017-05-31 12:12:12 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-06-06 08:52:16 -0700 |
commit | 39db721f9ace13cc210278c88c3f364b5d67b5b8 (patch) | |
tree | 6a0ca910942bf8c41560e91b73a8a58d04bc146c /board/hammer/board.c | |
parent | 2f436f4cc9bd026ed83511401ab17d68f5f8e5a3 (diff) | |
download | chrome-ec-39db721f9ace13cc210278c88c3f364b5d67b5b8.tar.gz |
hammer: Add board_get_entropy function (and test)
This function will be used to generate some entropy using the
Clock Recovery System.
BRANCH=none
BUG=b:38486828
TEST=make BOARD=hammer -j tests
./util/flash_ec --board=hammer --image=build/hammer/test-entropy.bin
EC console: runtest
TEST=Test fails when no USB connection is active
TEST=Test passes when USB connection is active
TEST=Pasting the values into:
tr ';' '\n' | awk 'BEGIN { e = 0; tot=16384.0 }
{ p = $1/tot; if (p > 0) { e -= p*log(p)/log(2) } }
END { print e }'
shows an entropy > 4 bits per sample.
Change-Id: I2363c7bce42c72c33ef0bf3f099d709ee9c13d13
Reviewed-on: https://chromium-review.googlesource.com/518608
Commit-Ready: Nicolas Boichat <drinkcat@chromium.org>
Tested-by: Nicolas Boichat <drinkcat@chromium.org>
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Diffstat (limited to 'board/hammer/board.c')
-rw-r--r-- | board/hammer/board.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/board/hammer/board.c b/board/hammer/board.c index d50634aab8..b9f21acf1d 100644 --- a/board/hammer/board.c +++ b/board/hammer/board.c @@ -9,12 +9,14 @@ #include "touchpad_elan.h" #include "gpio.h" #include "hooks.h" +#include "hwtimer.h" #include "i2c.h" #include "keyboard_raw.h" #include "keyboard_scan.h" #include "pwm.h" #include "pwm_chip.h" #include "registers.h" +#include "rollback.h" #include "task.h" #include "timer.h" #include "update_fw.h" @@ -135,3 +137,30 @@ void board_usb_wake(void) gpio_set_flags(GPIO_BASE_DET, GPIO_INPUT); interrupt_enable(); } + +/* + * Get entropy based on Clock Recovery System, which is enabled on hammer to + * synchronize USB SOF with internal oscillator. + */ +int board_get_entropy(void *buffer, int len) +{ + int i = 0; + uint8_t *data = buffer; + uint32_t start; + /* We expect one SOF per ms, so wait at most 2ms. */ + const uint32_t timeout = 2*MSEC; + + for (i = 0; i < len; i++) { + STM32_CRS_ICR |= STM32_CRS_ICR_SYNCOKC; + start = __hw_clock_source_read(); + while (!(STM32_CRS_ISR & STM32_CRS_ISR_SYNCOKF)) { + if ((__hw_clock_source_read() - start) > timeout) + return 0; + usleep(500); + } + /* Pick 8 bits, including FEDIR and 7 LSB of FECAP. */ + data[i] = STM32_CRS_ISR >> 15; + } + + return 1; +} |