Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3D939C433EF for ; Thu, 13 Jan 2022 02:27:12 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6D7E3833B4; Thu, 13 Jan 2022 03:26:59 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="co1o2QIv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4AA62833B5; Thu, 13 Jan 2022 03:26:56 +0100 (CET) Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4C227832E9 for ; Thu, 13 Jan 2022 03:26:52 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd2c.google.com with SMTP id n137so451575iod.4 for ; Wed, 12 Jan 2022 18:26:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=A4i/WubqVOZqxyaXLI/Leu9Ut+esy2LBNXtwXYD1u5Y=; b=co1o2QIvmadFfa6gGkTUC6RvhXTNljlMhqakDoFjyMzsBcBbdgZgn3C5AQ2xTAB5R8 rtsrn7STUf+D9an5DT5GxImKnDPoxX9z1ia6/84oKC7xbf5k8Ny3YJ2BBO62OyxGcuQs M010GnTAwq2cLfRr+qXPAYHyLs48kSFORA+VM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A4i/WubqVOZqxyaXLI/Leu9Ut+esy2LBNXtwXYD1u5Y=; b=3TSMSGvNQqi6Oj371gFzzpE2BRVxesljjryZqJujyviy6IYdcWlWYRRkqrldp/rnC6 X/V1OmKJ40nF8YeEIqLj0kyY86CglbIMgrZN2zS/y+uhFz4o+jtGUwkQV3w6Dw7/eUKy f8eZm+5a4ScsGi5JiuN4OfA1JncnBLVTc5rkwigkaliIBjAM8SLWYW4kDXG+8lc3agHs 49Uc4NVgBh8BvqVYcAl8sYYa+oQKZur7iMWrBrxywBDbXz9+OptGW9bQuMTaNpF4gP6o DdkzkqyikugWC0f1Te6zS3cnrF522NKMpo7w6Y9CD9TiLKUZVtJlOQ8SPAtPg9Aiif9M OL4g== X-Gm-Message-State: AOAM530OXv9QVoOReSWHFhtW81KGrvcmp/EXKBdJhN1ijR74o4riboid dio9lATYhhJup63ImD3BpqKiuN8em3S1BA== X-Google-Smtp-Source: ABdhPJyMKDX5LE9ZDsDyoGiSzE1jNlImR1q6Ig26RuO4E6U9rzsGU50u3JxL9Zcx2DY6PsdSBvfi2A== X-Received: by 2002:a5e:9709:: with SMTP id w9mr1202851ioj.86.1642040810881; Wed, 12 Jan 2022 18:26:50 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id e17sm965634ili.37.2022.01.12.18.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jan 2022 18:26:50 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Simon Glass Subject: [PATCH v2 03/12] bloblist: Put the magic number first Date: Wed, 12 Jan 2022 19:26:16 -0700 Message-Id: <20220113022625.413990-4-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog In-Reply-To: <20220113022625.413990-1-sjg@chromium.org> References: <20220113022625.413990-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean It seems best to put the magic number right at the start of the bloblist header, so it is easier to check. This is how devicetree works. Make this change now, before other projects make use of bloblist. Other changes may be needed / discussed, but that is TBD. Add a checker function as well. Signed-off-by: Simon Glass --- (no changes since v1) include/bloblist.h | 25 +++++++++++++++++++++++-- test/bloblist.c | 7 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/bloblist.h b/include/bloblist.h index 9f007c7a94d..29ea5a768a6 100644 --- a/include/bloblist.h +++ b/include/bloblist.h @@ -13,6 +13,8 @@ #ifndef __BLOBLIST_H #define __BLOBLIST_H +#include + enum { BLOBLIST_VERSION = 0, BLOBLIST_MAGIC = 0xb00757a3, @@ -59,11 +61,11 @@ enum bloblist_tag_t { * Each bloblist record is aligned to a 16-byte boundary and follows immediately * from the last. * + * @magic: BLOBLIST_MAGIC * @version: BLOBLIST_VERSION * @hdr_size: Size of this header, normally sizeof(struct bloblist_hdr). The * first bloblist_rec starts at this offset from the start of the header * @flags: Space for BLOBLISTF_... flags (none yet) - * @magic: BLOBLIST_MAGIC * @size: Total size of the bloblist (non-zero if valid) including this header. * The bloblist extends for this many bytes from the start of this header. * When adding new records, the bloblist can grow up to this size. @@ -78,10 +80,10 @@ enum bloblist_tag_t { * calculation. */ struct bloblist_hdr { + u32 magic; u32 version; u32 hdr_size; u32 flags; - u32 magic; u32 size; u32 alloced; @@ -111,6 +113,25 @@ struct bloblist_rec { u32 spare; }; +/** + * bloblist_check_magic() - return a bloblist if the magic matches + * + * @addr: Address to check + * @return pointer to bloblist, if the magic matches, else NULL + */ +static inline void *bloblist_check_magic(ulong addr) +{ + u32 *ptr; + + if (!addr) + return NULL; + ptr = map_sysmem(addr, 0); + if (*ptr != BLOBLIST_MAGIC) + return NULL; + + return ptr; +} + /** * bloblist_find() - Find a blob * diff --git a/test/bloblist.c b/test/bloblist.c index b48be38dc3e..525e94b7217 100644 --- a/test/bloblist.c +++ b/test/bloblist.c @@ -71,7 +71,9 @@ static int bloblist_test_init(struct unit_test_state *uts) hdr = clear_bloblist(); ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR)); ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR)); hdr->version++; ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); @@ -83,6 +85,11 @@ static int bloblist_test_init(struct unit_test_state *uts) ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); ut_assertok(bloblist_finish()); ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); + + hdr->magic++; + ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR)); + hdr->magic--; + hdr->flags++; ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); -- 2.34.1.575.g55b058a8bb-goog