summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJulius Werner <jwerner@chromium.org>2021-04-16 16:48:32 -0700
committerJulius Werner <jwerner@chromium.org>2021-04-21 02:06:26 +0000
commitc893197352acc9b53c1beef5082cbc0271f63688 (patch)
treeb975712387bd54bd0101a736adbb2a6fe5b824bb /tests
parentb03e497ef16e9e38ba9220d31131a6bfdef35390 (diff)
commonlib/region: Turn addrspace_32bit into a more official API
We had the addrspace_32bit rdev in prog_loaders.c for a while to help represent memory ranges as an rdev, and we've found it useful for a couple of things that have nothing to do with program loading. This patch moves the concept straight into commonlib/region.c so it is no longer anchored in such a weird place, and easier to use in unit tests. Also expand the concept to the whole address space (there's no real need to restrict it to 32 bits in 64-bit environments) and introduce an rdev_chain_mem() helper function to make it a bit easier to use. Replace some direct uses of struct mem_region_device with this new API where it seems to make sense. Signed-off-by: Julius Werner <jwerner@chromium.org> Change-Id: Ie4c763b77f77d227768556a9528681d771a08dca Reviewed-on: https://review.coreboot.org/c/coreboot/+/52533 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/commonlib/region-test.c23
-rw-r--r--tests/lib/fmap-test.c49
-rw-r--r--tests/lib/region_file-test.c89
3 files changed, 67 insertions, 94 deletions
diff --git a/tests/commonlib/region-test.c b/tests/commonlib/region-test.c
index 545463e1dd..fab5961173 100644
--- a/tests/commonlib/region-test.c
+++ b/tests/commonlib/region-test.c
@@ -333,32 +333,33 @@ static void test_mem_rdev(void **state)
u8 backing[size];
u8 scratch[size];
int i;
- struct mem_region_device mem = MEM_REGION_DEV_RW_INIT(backing, size);
+ struct region_device mem;
+ rdev_chain_mem_rw(&mem, backing, size);
/* Test writing to and reading from full mapping. */
memset(backing, 0xa5, size);
- u8 *mapping = rdev_mmap_full(&mem.rdev);
+ u8 *mapping = rdev_mmap_full(&mem);
assert_non_null(mapping);
for (i = 0; i < size; i++)
assert_int_equal(mapping[i], 0xa5);
memset(mapping, 0x5a, size);
for (i = 0; i < size; i++)
assert_int_equal(backing[i], 0x5a);
- assert_int_equal(rdev_munmap(&mem.rdev, mapping), 0);
+ assert_int_equal(rdev_munmap(&mem, mapping), 0);
/* Test read/write/erase of single bytes. */
for (i = 0; i < size; i++) {
u8 val = i + 0xaa;
scratch[0] = val;
- assert_int_equal(rdev_writeat(&mem.rdev, &scratch, i, 1), 1);
+ assert_int_equal(rdev_writeat(&mem, &scratch, i, 1), 1);
assert_int_equal(backing[i], val);
assert_int_equal(scratch[0], val);
val = i + 0x55;
backing[i] = val;
- assert_int_equal(rdev_readat(&mem.rdev, &scratch, i, 1), 1);
+ assert_int_equal(rdev_readat(&mem, &scratch, i, 1), 1);
assert_int_equal(scratch[0], val);
assert_int_equal(backing[i], val);
- assert_int_equal(rdev_eraseat(&mem.rdev, i, 1), 1);
+ assert_int_equal(rdev_eraseat(&mem, i, 1), 1);
assert_int_equal(backing[i], 0);
}
@@ -368,25 +369,25 @@ static void test_mem_rdev(void **state)
memset(backing, 0, size);
memset(scratch, 0, size);
memset(scratch + offs, 0x39, chunk);
- assert_int_equal(rdev_writeat(&mem.rdev, scratch + offs, offs, chunk), chunk);
+ assert_int_equal(rdev_writeat(&mem, scratch + offs, offs, chunk), chunk);
assert_memory_equal(backing, scratch, size);
memset(backing, 0, size);
- assert_int_equal(rdev_readat(&mem.rdev, scratch + offs, offs, chunk), chunk);
+ assert_int_equal(rdev_readat(&mem, scratch + offs, offs, chunk), chunk);
assert_memory_equal(backing, scratch, size);
memset(scratch + offs + 1, 0, chunk - 1);
- assert_int_equal(rdev_eraseat(&mem.rdev, offs + 1, chunk - 1), chunk - 1);
+ assert_int_equal(rdev_eraseat(&mem, offs + 1, chunk - 1), chunk - 1);
assert_memory_equal(backing, scratch, size);
/* Test mapping of larger chunk. */
memset(backing, 0, size);
- mapping = rdev_mmap(&mem.rdev, offs, chunk);
+ mapping = rdev_mmap(&mem, offs, chunk);
assert_non_null(mapping);
memset(scratch, 0x93, size);
memcpy(mapping, scratch, chunk);
memset(scratch, 0, size);
memset(scratch + offs, 0x93, chunk);
assert_memory_equal(backing, scratch, size);
- assert_int_equal(rdev_munmap(&mem.rdev, mapping), 0);
+ assert_int_equal(rdev_munmap(&mem, mapping), 0);
assert_memory_equal(backing, scratch, size);
}
diff --git a/tests/lib/fmap-test.c b/tests/lib/fmap-test.c
index 372bb85a86..444af5a705 100644
--- a/tests/lib/fmap-test.c
+++ b/tests/lib/fmap-test.c
@@ -11,8 +11,8 @@
#include <tests/lib/fmap/fmap_data.h>
#include <tests/lib/fmap/fmap_config.h>
-static struct mem_region_device mem_rdev_rw;
-static struct mem_region_device mem_rdev_ro;
+static struct region_device flash_rdev_rw;
+static struct region_device flash_rdev_ro;
static char *flash_buffer = NULL;
static size_t flash_buffer_size = 0;
@@ -38,32 +38,15 @@ static void prepare_flash_buffer(void)
static int setup_fmap(void **state)
{
prepare_flash_buffer();
-
- mem_rdev_rw = (struct mem_region_device)
- MEM_REGION_DEV_RW_INIT(flash_buffer, FMAP_SECTION_FLASH_SIZE);
-
- mem_rdev_ro = (struct mem_region_device)
- MEM_REGION_DEV_RO_INIT(flash_buffer, FMAP_SECTION_FLASH_SIZE);
-
+ rdev_chain_mem_rw(&flash_rdev_rw, flash_buffer, FMAP_SECTION_FLASH_SIZE);
+ rdev_chain_mem(&flash_rdev_ro, flash_buffer, FMAP_SECTION_FLASH_SIZE);
return 0;
}
static int teardown_fmap(void **state)
{
- struct mem_region_device empty = {
- .base = NULL,
- .rdev = {
- .root = NULL,
- .ops = NULL,
- .region = {
- .offset = 0,
- .size = 0
- }
- }
- };
-
- mem_rdev_rw = empty;
- mem_rdev_ro = empty;
+ rdev_chain_mem_rw(&flash_rdev_rw, NULL, 0);
+ rdev_chain_mem(&flash_rdev_ro, NULL, 0);
free(flash_buffer);
flash_buffer = NULL;
@@ -79,12 +62,12 @@ void boot_device_init(void)
const struct region_device *boot_device_ro(void)
{
- return &mem_rdev_rw.rdev;
+ return &flash_rdev_ro;
}
const struct region_device *boot_device_rw(void)
{
- return &mem_rdev_rw.rdev;
+ return &flash_rdev_rw;
}
static void test_fmap_locate_area_as_rdev(void **state)
@@ -93,21 +76,21 @@ static void test_fmap_locate_area_as_rdev(void **state)
struct region_device rdev;
assert_int_not_equal(-1, fmap_locate_area_as_rdev("RO_VPD", &rdev));
- assert_int_equal(FMAP_SECTION_RO_VPD_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_RO_VPD_START, rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_RO_VPD_SIZE, region_device_sz(&rdev));
/* Check if locating area second time works */
assert_int_not_equal(-1, fmap_locate_area_as_rdev("RO_VPD", &rdev));
- assert_int_equal(FMAP_SECTION_RO_VPD_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_RO_VPD_START, rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_RO_VPD_SIZE, region_device_sz(&rdev));
assert_int_not_equal(-1, fmap_locate_area_as_rdev("RECOVERY_MRC_CACHE", &rdev));
- assert_int_equal(FMAP_SECTION_RECOVERY_MRC_CACHE_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_RECOVERY_MRC_CACHE_START,
+ rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_RECOVERY_MRC_CACHE_SIZE, region_device_sz(&rdev));
/* Expect error when writing to read-only area */
- assert_int_equal(-1, rdev_writeat(&rdev, buffer,
- region_device_offset(&rdev), sizeof(buffer)));
+ assert_int_equal(-1, rdev_writeat(&rdev, buffer, 0, sizeof(buffer)));
/* Expect error when looking for incorrect area */
assert_int_equal(-1, fmap_locate_area_as_rdev("NONEXISTENT_AREA", &rdev));
@@ -131,16 +114,16 @@ static void test_fmap_locate_area_as_rdev_rw(void **state)
dummy_data[i] = '0' + i % ('9' - '0');
assert_int_not_equal(-1, fmap_locate_area_as_rdev_rw("RW_SECTION_A", &rdev));
- assert_int_equal(FMAP_SECTION_RW_SECTION_A_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_RW_SECTION_A_START, rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_RW_SECTION_A_SIZE, region_device_sz(&rdev));
/* Check if locating area second time works */
assert_int_not_equal(-1, fmap_locate_area_as_rdev_rw("RW_SECTION_A", &rdev));
- assert_int_equal(FMAP_SECTION_RW_SECTION_A_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_RW_SECTION_A_START, rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_RW_SECTION_A_SIZE, region_device_sz(&rdev));
assert_int_not_equal(-1, fmap_locate_area_as_rdev_rw("MISC_RW", &rdev));
- assert_int_equal(FMAP_SECTION_MISC_RW_START, region_device_offset(&rdev));
+ assert_ptr_equal(flash_buffer + FMAP_SECTION_MISC_RW_START, rdev_mmap_full(&rdev));
assert_int_equal(FMAP_SECTION_MISC_RW_SIZE, region_device_sz(&rdev));
diff --git a/tests/lib/region_file-test.c b/tests/lib/region_file-test.c
index 6bd1382ad9..aeeaa4efcb 100644
--- a/tests/lib/region_file-test.c
+++ b/tests/lib/region_file-test.c
@@ -8,17 +8,15 @@
#include <commonlib/region.h>
#include <tests/lib/region_file_data.h>
-static void clear_region_file(struct mem_region_device *mrdev)
+static void clear_region_file(struct region_device *rdev)
{
- uint8_t *mem_buffer = (uint8_t *)mrdev->base;
-
- memset(mem_buffer, 0xff, REGION_FILE_BUFFER_SIZE);
+ memset(rdev_mmap_full(rdev), 0xff, REGION_FILE_BUFFER_SIZE);
}
static int setup_region_file_test_group(void **state)
{
void *mem_buffer = malloc(REGION_FILE_BUFFER_SIZE);
- struct mem_region_device *dev = malloc(sizeof(struct mem_region_device));
+ struct region_device *dev = malloc(sizeof(struct region_device));
if (mem_buffer == NULL || dev == NULL) {
free(mem_buffer);
@@ -26,8 +24,7 @@ static int setup_region_file_test_group(void **state)
return -1;
}
- *dev = (struct mem_region_device)
- MEM_REGION_DEV_RW_INIT(mem_buffer, REGION_FILE_BUFFER_SIZE);
+ rdev_chain_mem_rw(dev, mem_buffer, REGION_FILE_BUFFER_SIZE);
*state = dev;
clear_region_file(dev);
@@ -37,8 +34,8 @@ static int setup_region_file_test_group(void **state)
static int teardown_region_file_test_group(void **state)
{
- struct mem_region_device *dev = *state;
- void *mem_buffer = dev->base;
+ struct region_device *dev = *state;
+ void *mem_buffer = rdev_mmap_full(dev);
free(mem_buffer);
free(dev);
@@ -51,7 +48,7 @@ static int teardown_region_file_test_group(void **state)
everything twice is known, but acceptable as it grants safety and makes tests independent. */
static int setup_teardown_region_file_test(void **state)
{
- struct mem_region_device *dev = *state;
+ struct region_device *dev = *state;
clear_region_file(dev);
@@ -60,116 +57,110 @@ static int setup_teardown_region_file_test(void **state)
static void test_region_file_init_empty(void **state)
{
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
+ struct region_device *rdev = *state;
struct region_file regf;
/* Test general approach using valid mem_region_device with buffer filled with 0xff.
Parameters cannot be NULL. */
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(RF_EMPTY, regf.slot);
}
static void test_region_file_init_invalid_metadata(void **state)
{
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
- uint16_t *mem_buffer16 = (uint16_t *)mdev->base;
+ struct region_device *rdev = *state;
+ uint16_t *mem_buffer16 = (uint16_t *)rdev_mmap_full(rdev);
struct region_file regf;
/* Set number of metadata blocks to 0 */
mem_buffer16[0] = 0;
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(RF_NEED_TO_EMPTY, regf.slot);
}
static void test_region_file_init_valid_no_data(void **state)
{
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
- uint16_t *mem_buffer16 = (uint16_t *)mdev->base;
+ struct region_device *rdev = *state;
+ uint16_t *mem_buffer16 = (uint16_t *)rdev_mmap_full(rdev);
struct region_file regf;
/* Manually allocate 4 metadata blocks and no data. */
mem_buffer16[0] = 4;
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(0, regf.slot);
}
static void test_region_file_init_invalid_data_offset(void **state)
{
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
- uint16_t *mem_buffer16 = (uint16_t *)mdev->base;
+ struct region_device *rdev = *state;
+ uint16_t *mem_buffer16 = (uint16_t *)rdev_mmap_full(rdev);
struct region_file regf;
/* Manually allocate 4 metadata blocks and no data. */
mem_buffer16[0] = 4;
mem_buffer16[1] = 4;
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(RF_NEED_TO_EMPTY, regf.slot);
/* Set data size to be larger than region */
mem_buffer16[0] = 4;
mem_buffer16[1] = 4 + 4096;
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(RF_NEED_TO_EMPTY, regf.slot);
}
static void test_region_file_init_correct_data_offset(void **state)
{
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
- uint16_t *mem_buffer16 = (uint16_t *)mdev->base;
+ struct region_device *rdev = *state;
+ uint16_t *mem_buffer16 = (uint16_t *)rdev_mmap_full(rdev);
struct region_file regf;
/* Set data size to 8 blocks which is correct value. */
mem_buffer16[0] = 4;
mem_buffer16[1] = 4 + 8;
- assert_int_equal(0, region_file_init(&regf, mrdev));
+ assert_int_equal(0, region_file_init(&regf, rdev));
assert_int_equal(1, regf.slot);
}
static void test_region_file_init_real_data(void **state)
{
- struct mem_region_device dev = MEM_REGION_DEV_RW_INIT(region_file_data_buffer1,
- REGION_FILE_BUFFER_SIZE);
- struct region_device *rdev = &dev.rdev;
+ struct region_device rdev;
struct region_file regf;
+ rdev_chain_mem_rw(&rdev, region_file_data_buffer1, REGION_FILE_BUFFER_SIZE);
+
/* Check on real example with one update */
- assert_int_equal(0, region_file_init(&regf, rdev));
+ assert_int_equal(0, region_file_init(&regf, &rdev));
/* There is one update available */
assert_int_equal(1, regf.slot);
/* Check on real example with multiple updates */
- dev = (struct mem_region_device) MEM_REGION_DEV_RW_INIT(region_file_data_buffer2,
- REGION_FILE_BUFFER_SIZE);
- rdev = &dev.rdev;
- assert_int_equal(0, region_file_init(&regf, rdev));
+ rdev_chain_mem_rw(&rdev, region_file_data_buffer2, REGION_FILE_BUFFER_SIZE);
+ assert_int_equal(0, region_file_init(&regf, &rdev));
/* There are three update available */
assert_int_equal(3, regf.slot);
}
static void test_region_file_init_invalid_region_device(void **state)
{
- struct mem_region_device bad_dev = MEM_REGION_DEV_RW_INIT(NULL, 0);
+ struct region_device bad_dev;
struct region_file regf;
+ rdev_chain_mem_rw(&bad_dev, NULL, 0);
+
/* Expect fail when passing invalid region_device. */
- assert_int_equal(-1, region_file_init(&regf, &bad_dev.rdev));
+ assert_int_equal(-1, region_file_init(&regf, &bad_dev));
}
static void test_region_file_data(void **state)
{
/* region_device with empty data buffer */
- struct mem_region_device *mdev = *state;
- struct region_device *mrdev = &mdev->rdev;
+ struct region_device *mrdev = *state;
/* region_device with prepared data buffer */
- struct mem_region_device dev = MEM_REGION_DEV_RW_INIT(region_file_data_buffer1,
- REGION_FILE_BUFFER_SIZE);
- struct region_device *rdev = &dev.rdev;
+ struct region_device rdev;
+ rdev_chain_mem_rw(&rdev, region_file_data_buffer1, REGION_FILE_BUFFER_SIZE);
+
struct region_file regf;
struct region_device read_rdev;
int ret;
@@ -182,7 +173,7 @@ static void test_region_file_data(void **state)
/* Check if region_file_data() correctly returns region_device for hardcoded
region_file data with update of 256 bytes */
- ret = region_file_init(&regf, rdev);
+ ret = region_file_init(&regf, &rdev);
assert_int_equal(0, ret);
ret = region_file_data(&regf, &read_rdev);
assert_int_equal(0, ret);
@@ -192,8 +183,7 @@ static void test_region_file_data(void **state)
static void test_region_file_update_data(void **state)
{
- struct mem_region_device *dev = *state;
- struct region_device *rdev = &dev->rdev;
+ struct region_device *rdev = *state;
struct region_file regf;
struct region_device read_rdev;
const size_t dummy_data_size = 256;
@@ -235,8 +225,7 @@ static void test_region_file_update_data(void **state)
static void test_region_file_update_data_arr(void **state)
{
- struct mem_region_device *dev = *state;
- struct region_device *rdev = &dev->rdev;
+ struct region_device *rdev = *state;
struct region_file regf;
struct region_device read_rdev;
const size_t dummy_data_size = 256;