From df5348fb14e1cd6a5c581be2fa0efe20415739bf Mon Sep 17 00:00:00 2001 From: Yannick Heneault Date: Fri, 10 Aug 2018 09:40:08 -0400 Subject: [PATCH] emulator: support /dev/{u,}random configuration at compile time; default is still /dev/urandom (#396) --- Makefile.include | 4 ++++ emulator/setup.c | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Makefile.include b/Makefile.include index cc517be78..d1911aa92 100644 --- a/Makefile.include +++ b/Makefile.include @@ -98,6 +98,10 @@ CFLAGS += -I/usr/include/SDL2 -D_REENTRANT LDLIBS += -lSDL2 endif +ifdef RANDOM_DEV_FILE +CFLAGS += -DRANDOM_DEV_FILE=\"$(RANDOM_DEV_FILE)\" +endif + else ifdef APPVER CFLAGS += -DAPPVER=$(APPVER) diff --git a/emulator/setup.c b/emulator/setup.c index 8000aa4ea..61b4bad14 100644 --- a/emulator/setup.c +++ b/emulator/setup.c @@ -34,11 +34,15 @@ #define EMULATOR_FLASH_FILE "emulator.img" +#ifndef RANDOM_DEV_FILE +#define RANDOM_DEV_FILE "/dev/urandom" +#endif + uint8_t *emulator_flash_base = NULL; uint32_t __stack_chk_guard; -static int urandom = -1; +static int random_fd = -1; static void setup_urandom(void); static void setup_flash(void); @@ -53,17 +57,21 @@ void __attribute__((noreturn)) shutdown(void) { } void emulatorRandom(void *buffer, size_t size) { - ssize_t n = read(urandom, buffer, size); - if (n < 0 || ((size_t) n) != size) { - perror("Failed to read /dev/urandom"); - exit(1); - } + ssize_t n, len = 0; + do { + n = read(random_fd, (char*)buffer + len, size - len); + if (n < 0) { + perror("Failed to read " RANDOM_DEV_FILE); + exit(1); + } + len += n; + } while (len != (ssize_t)size); } static void setup_urandom(void) { - urandom = open("/dev/urandom", O_RDONLY); - if (urandom < 0) { - perror("Failed to open /dev/urandom"); + random_fd = open(RANDOM_DEV_FILE, O_RDONLY); + if (random_fd < 0) { + perror("Failed to open " RANDOM_DEV_FILE); exit(1); } }