dm: core: Update ofnode_read_fmap_entry() to read hashes

At present this function uses the old format for reading hashes. Add
support for the current format.

Add a test while we are here.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2021-01-21 13:57:14 -07:00
parent 017d421828
commit ff5fa7d626
4 changed files with 61 additions and 9 deletions

View File

@ -90,6 +90,16 @@
wp-ro {
image-pos = <0xf000>;
size = <0x1000>;
used = <0x884>;
compress = "lz4";
uncomp-size = <0xcf8>;
hash {
algo = "sha256";
value = [00 01 02 03 04 05 06 07
08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17
18 19 1a 1b 1c 1d 1e 1f];
};
};
rw {
image-pos = <0x10000>;

View File

@ -14,16 +14,17 @@
int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry)
{
const char *prop;
ofnode subnode;
if (ofnode_read_u32(node, "image-pos", &entry->offset)) {
debug("Node '%s' has bad/missing 'image-pos' property\n",
ofnode_get_name(node));
return log_ret(-ENOENT);
return log_msg_ret("image-pos", -ENOENT);
}
if (ofnode_read_u32(node, "size", &entry->length)) {
debug("Node '%s' has bad/missing 'size' property\n",
ofnode_get_name(node));
return log_ret(-ENOENT);
return log_msg_ret("size", -ENOENT);
}
entry->used = ofnode_read_s32_default(node, "used", entry->length);
prop = ofnode_read_string(node, "compress");
@ -31,18 +32,20 @@ int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry)
if (!strcmp(prop, "lz4"))
entry->compress_algo = FMAP_COMPRESS_LZ4;
else
return log_msg_ret("Unknown compression algo",
-EINVAL);
return log_msg_ret("compression algo", -EINVAL);
} else {
entry->compress_algo = FMAP_COMPRESS_NONE;
}
entry->unc_length = ofnode_read_s32_default(node, "uncomp-size",
entry->length);
prop = ofnode_read_string(node, "hash");
if (prop)
entry->hash_size = strlen(prop);
entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
entry->hash = (uint8_t *)prop;
subnode = ofnode_find_subnode(node, "hash");
if (ofnode_valid(subnode)) {
prop = ofnode_read_prop(subnode, "value", &entry->hash_size);
/* Assume it is sha256 */
entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
entry->hash = (uint8_t *)prop;
}
return 0;
}

View File

@ -42,6 +42,7 @@ obj-y += fdtdec.o
obj-$(CONFIG_UT_DM) += nop.o
obj-y += ofnode.o
obj-y += ofread.o
obj-y += of_extra.o
obj-$(CONFIG_OSD) += osd.o
obj-$(CONFIG_DM_VIDEO) += panel.o
obj-$(CONFIG_DM_PCI) += pci.o

38
test/dm/of_extra.c Normal file
View File

@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <dm.h>
#include <dm/of_extra.h>
#include <dm/test.h>
#include <test/ut.h>
#include <u-boot/sha256.h>
static int dm_test_ofnode_read_fmap_entry(struct unit_test_state *uts)
{
const char hash_expect[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
struct fmap_entry entry;
ofnode node;
node = ofnode_path("/cros-ec/flash/wp-ro");
ut_assertok(ofnode_read_fmap_entry(node, &entry));
ut_asserteq(0xf000, entry.offset);
ut_asserteq(0x1000, entry.length);
ut_asserteq(0x884, entry.used);
ut_asserteq(FMAP_COMPRESS_LZ4, entry.compress_algo);
ut_asserteq(0xcf8, entry.unc_length);
ut_asserteq(FMAP_HASH_SHA256, entry.hash_algo);
ut_asserteq(SHA256_SUM_LEN, entry.hash_size);
ut_asserteq_mem(hash_expect, entry.hash, SHA256_SUM_LEN);
return 0;
}
DM_TEST(dm_test_ofnode_read_fmap_entry, 0);