From eb135abe5aa13190160ba45fcd5defdc573c24ba Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Mon, 15 Jul 2019 10:50:59 +0200 Subject: ubiformat: improve generation of UBI image sequence 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 --- common/ubiformat.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/ubiformat.c') 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", -- cgit v1.2.3