bloblist: Checksum the entire bloblist

Use a sinple 8-bit checksum for bloblist, as specified by the spec
version 0.9.
Spec v0.9 specifies that the entire bloblist area is checksummed,
including unused portions. Update the code to follow this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Co-developed-by: Raymond Mao <raymond.mao@linaro.org>
Signed-off-by: Raymond Mao <raymond.mao@linaro.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
This commit is contained in:
Simon Glass 2023-12-27 13:07:05 -08:00
parent 47e1047b0c
commit 997dac6ede
3 changed files with 14 additions and 14 deletions

View file

@ -13,6 +13,7 @@
#include <malloc.h> #include <malloc.h>
#include <mapmem.h> #include <mapmem.h>
#include <spl.h> #include <spl.h>
#include <tables_csum.h>
#include <asm/global_data.h> #include <asm/global_data.h>
#include <u-boot/crc.h> #include <u-boot/crc.h>
@ -318,16 +319,10 @@ int bloblist_resize(uint tag, int new_size)
static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr) static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
{ {
struct bloblist_rec *rec; u8 chksum;
u32 chksum;
chksum = crc32(0, (unsigned char *)hdr, chksum = table_compute_checksum(hdr, hdr->alloced);
offsetof(struct bloblist_hdr, chksum)); chksum += hdr->chksum;
foreach_rec(rec, hdr) {
chksum = crc32(chksum, (void *)rec, rec_hdr_size(rec));
chksum = crc32(chksum, (void *)rec + rec_hdr_size(rec),
rec->size);
}
return chksum; return chksum;
} }

View file

@ -174,11 +174,10 @@ enum bloblist_tag_t {
* sizeof(bloblist_hdr) since we need at least that much space to store a * sizeof(bloblist_hdr) since we need at least that much space to store a
* valid bloblist * valid bloblist
* @spare: Spare space (for future use) * @spare: Spare space (for future use)
* @chksum: CRC32 for the entire bloblist allocated area. Since any of the * @chksum: checksum for the entire bloblist allocated area. Since any of the
* blobs can be altered after being created, this checksum is only valid * blobs can be altered after being created, this checksum is only valid
* when the bloblist is finalised before jumping to the next stage of boot. * when the bloblist is finalised before jumping to the next stage of boot.
* Note that chksum is last to make it easier to exclude it from the * This is the value needed to make all checksummed bytes sum to 0
* checksum calculation.
*/ */
struct bloblist_hdr { struct bloblist_hdr {
u32 magic; u32 magic;

View file

@ -237,12 +237,18 @@ static int bloblist_test_checksum(struct unit_test_state *uts)
*data2 -= 1; *data2 -= 1;
/* /*
* Changing data outside the range of valid data should not affect * Changing data outside the range of valid data should affect the
* the checksum. * checksum.
*/ */
ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
data[TEST_SIZE]++; data[TEST_SIZE]++;
ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
data[TEST_SIZE]--;
ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
data2[TEST_SIZE2]++; data2[TEST_SIZE2]++;
ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
data[TEST_SIZE]--;
ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE)); ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
return 0; return 0;