mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] ubiformat: improve generation of UBI image sequence
@ 2019-07-15  9:09 Sascha Hauer
  2019-07-16  8:09 ` Uwe Kleine-König
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2019-07-15  9:09 UTC (permalink / raw)
  To: Barebox List

The UBI ec_hdr has an image_seq field. During attaching UBI expects
that its value is the same for all eraseblocks. The value should be
changed with every ubiformat and is used to detect half written images.

In barebox we use a pseudo random number generated with rand() for this
value. The ubiformat command calls srand(get_time_ns()) to initialize
the pseudo random numbber generator. This is done in the option parser,
so when ubiformat() is called directly (from fastboot for example) the
pseudo random number generator is not initialized and we get the same
values after every barebox restart.

This patch moves the pseudo random number generator initialization
to the place where the numbers are generated. Also use random32()
rather than rand() to generate 32bit values rather than 15bit values
(0 - RAND_MAX).

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/ubiformat.c | 1 -
 common/ubiformat.c   | 6 ++++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/commands/ubiformat.c b/commands/ubiformat.c
index a88672d6c2..7537e6b567 100644
--- a/commands/ubiformat.c
+++ b/commands/ubiformat.c
@@ -58,7 +58,6 @@
 static int parse_opt(int argc, char *argv[], struct ubiformat_args *args,
 		     char **node)
 {
-	srand(get_time_ns());
 	memset(args, 0, sizeof(*args));
 
 	while (1) {
diff --git a/common/ubiformat.c b/common/ubiformat.c
index 1968bd98f8..fe02270b78 100644
--- a/common/ubiformat.c
+++ b/common/ubiformat.c
@@ -481,8 +481,10 @@ int ubiformat(struct mtd_info *mtd, struct ubiformat_args *args)
 	if (!args->ubi_ver)
 		args->ubi_ver = 1;
 
-	if (!args->image_seq)
-		args->image_seq = rand();
+	if (!args->image_seq) {
+		srand(get_time_ns());
+		args->image_seq = random32();
+	}
 
 	if (!is_power_of_2(mtd->writesize)) {
 		errmsg("min. I/O size is %d, but should be power of 2",
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ubiformat: improve generation of UBI image sequence
  2019-07-15  9:09 [PATCH] ubiformat: improve generation of UBI image sequence Sascha Hauer
@ 2019-07-16  8:09 ` Uwe Kleine-König
  0 siblings, 0 replies; 2+ messages in thread
From: Uwe Kleine-König @ 2019-07-16  8:09 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On Mon, Jul 15, 2019 at 11:09:00AM +0200, Sascha Hauer wrote:
> The UBI ec_hdr has an image_seq field. During attaching UBI expects
> that its value is the same for all eraseblocks. The value should be
> changed with every ubiformat and is used to detect half written images.
> 
> In barebox we use a pseudo random number generated with rand() for this
> value. The ubiformat command calls srand(get_time_ns()) to initialize
> the pseudo random numbber generator. This is done in the option parser,

s/bb/b/

> so when ubiformat() is called directly (from fastboot for example) the
> pseudo random number generator is not initialized and we get the same
> values after every barebox restart.

I don't understand the problem (i.e. where is srand() called up to now),
but that's not important. I wonder if you could just use some data from
the NAND to (additionally) initialize the rng?

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-16  8:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-15  9:09 [PATCH] ubiformat: improve generation of UBI image sequence Sascha Hauer
2019-07-16  8:09 ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox