bloblist: Put the magic number first
authorSimon Glass <sjg@chromium.org>
Thu, 13 Jan 2022 02:26:16 +0000 (19:26 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 13 Jan 2022 16:13:41 +0000 (09:13 -0700)
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 <sjg@chromium.org>
include/bloblist.h
test/bloblist.c

index 9f007c7..29ea5a7 100644 (file)
@@ -13,6 +13,8 @@
 #ifndef __BLOBLIST_H
 #define __BLOBLIST_H
 
+#include <mapmem.h>
+
 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;
@@ -112,6 +114,25 @@ struct bloblist_rec {
 };
 
 /**
+ * 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
  *
  * Searches the bloblist and returns the blob with the matching tag
index b48be38..525e94b 100644 (file)
@@ -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));