aboutsummaryrefslogtreecommitdiff
path: root/ubi-utils
diff options
context:
space:
mode:
Diffstat (limited to 'ubi-utils')
-rw-r--r--ubi-utils/src/common.c32
-rw-r--r--ubi-utils/src/common.h1
-rw-r--r--ubi-utils/src/ubiformat.c5
-rw-r--r--ubi-utils/src/ubinize.c7
4 files changed, 37 insertions, 8 deletions
diff --git a/ubi-utils/src/common.c b/ubi-utils/src/common.c
index 3fd470b..da5156d 100644
--- a/ubi-utils/src/common.c
+++ b/ubi-utils/src/common.c
@@ -23,10 +23,13 @@
* Adrian Hunter
*/
+#include <sys/time.h>
+#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
+#include <unistd.h>
#include "common.h"
/**
@@ -175,3 +178,32 @@ void ubiutils_print_text(FILE *stream, const char *text, int width)
++p;
}
}
+
+/**
+ * ubiutils_srand - randomly seed the standard pseudo-random generator.
+ *
+ * This helper function seeds the standard libc pseudo-random generator with a
+ * more or less random value to make sure the 'rand()' call does not return the
+ * same sequence every time UBI utilities run. Returns zero in case of success
+ * and a %-1 in case of error.
+ */
+int ubiutils_srand(void)
+{
+ struct timeval tv;
+ struct timezone tz;
+ unsigned int seed;
+
+ /*
+ * Just assume that a combination of the PID + current time is a
+ * reasonably random number.
+ */
+ if (gettimeofday(&tv, &tz))
+ return -1;
+
+ seed = (unsigned int)tv.tv_sec;
+ seed += (unsigned int)tv.tv_usec;
+ seed *= getpid();
+ seed %= RAND_MAX;
+ srand(seed);
+ return 0;
+}
diff --git a/ubi-utils/src/common.h b/ubi-utils/src/common.h
index 955c50f..d14cb48 100644
--- a/ubi-utils/src/common.h
+++ b/ubi-utils/src/common.h
@@ -78,6 +78,7 @@ static inline int is_power_of_2(unsigned long long n)
long long ubiutils_get_bytes(const char *str);
void ubiutils_print_bytes(long long bytes, int bracket);
void ubiutils_print_text(FILE *stream, const char *txt, int len);
+int ubiutils_srand(void);
#ifdef __cplusplus
}
diff --git a/ubi-utils/src/ubiformat.c b/ubi-utils/src/ubiformat.c
index 54363d7..62375a6 100644
--- a/ubi-utils/src/ubiformat.c
+++ b/ubi-utils/src/ubiformat.c
@@ -29,7 +29,6 @@
*/
#define MAX_CONSECUTIVE_BAD_BLOCKS 4
-#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
@@ -130,8 +129,8 @@ static const struct option long_options[] = {
static int parse_opt(int argc, char * const argv[])
{
- srand(getpid());
- args.image_seq = random();
+ ubiutils_srand();
+ args.image_seq = rand();
while (1) {
int key;
diff --git a/ubi-utils/src/ubinize.c b/ubi-utils/src/ubinize.c
index 74ddc0f..ab5b0b8 100644
--- a/ubi-utils/src/ubinize.c
+++ b/ubi-utils/src/ubinize.c
@@ -25,12 +25,9 @@
*/
#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
-#include <unistd.h>
#include <fcntl.h>
#include <mtd/ubi-media.h>
@@ -157,8 +154,8 @@ static struct args args = {
static int parse_opt(int argc, char * const argv[])
{
- srand(getpid());
- args.image_seq = random();
+ ubiutils_srand();
+ args.image_seq = rand();
while (1) {
int key;