|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Linux host partition API test |
| 7 | + */ |
| 8 | + |
| 9 | +#include <string.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include "esp_partition.h" |
| 12 | +#include "esp_private/partition_linux.h" |
| 13 | +#include "unity.h" |
| 14 | +#include "unity_fixture.h" |
| 15 | + |
| 16 | +const char *TAG = "partition_bdl_test"; |
| 17 | + |
| 18 | +TEST_GROUP(partition_bdl); |
| 19 | + |
| 20 | +TEST_SETUP(partition_bdl) |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +TEST_TEAR_DOWN(partition_bdl) |
| 25 | +{ |
| 26 | +} |
| 27 | + |
| 28 | +/* Test all the basic Block Device Layer APIs working correctly over emulated esp_partition instance. |
| 29 | + * NOR flash behavior expected. |
| 30 | + * */ |
| 31 | +TEST(partition_bdl, test_partition_bdl_ops) |
| 32 | +{ |
| 33 | + //get the block-device interface instance |
| 34 | + esp_blockdev_handle_t part_blockdev = NULL; |
| 35 | + TEST_ESP_OK(esp_partition_get_blockdev(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage1", &part_blockdev)); |
| 36 | + |
| 37 | + const size_t data_size = 256; |
| 38 | + uint8_t test_data[data_size]; |
| 39 | + const off_t target_addr = 3*4*1024; |
| 40 | + uint8_t data_buffer[data_size]; |
| 41 | + memset((void*)data_buffer, 0, data_size); |
| 42 | + |
| 43 | + //erase the first sector data from the blockdev and check it's really wiped |
| 44 | + TEST_ESP_OK(part_blockdev->erase(part_blockdev, target_addr, part_blockdev->geometry.erase_size)); |
| 45 | + memset((void*)test_data, 0xFF, data_size); //erased NOR flash sector contains only 1s |
| 46 | + |
| 47 | + TEST_ESP_OK(part_blockdev->read(part_blockdev, data_buffer, sizeof(data_buffer), target_addr, data_size)); |
| 48 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer, data_size)); |
| 49 | + |
| 50 | + //write to the blockdev |
| 51 | + memset((void*)test_data, 'A', data_size); |
| 52 | + TEST_ESP_OK(part_blockdev->write(part_blockdev, test_data, target_addr, data_size)); |
| 53 | + |
| 54 | + //read from the blockdev the data written before |
| 55 | + TEST_ESP_OK(part_blockdev->read(part_blockdev, data_buffer, sizeof(data_buffer), target_addr, data_size)); |
| 56 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer, data_size)); |
| 57 | + |
| 58 | + //release the BDL object - nothing to check here, the BDL memory is just freed |
| 59 | + esp_partition_release_blockdev(part_blockdev); |
| 60 | +} |
| 61 | + |
| 62 | +/* Test parallel access to two independent partitions through BDL interface. |
| 63 | + * Use both 'esp_partition_t' and type-subtype-label BDL getters. |
| 64 | + * NOR flash behavior expected. |
| 65 | + * */ |
| 66 | +TEST(partition_bdl, test_two_partitions_bdl_ops) |
| 67 | +{ |
| 68 | + //get the block-device interface instance for partition 'storage1' |
| 69 | + esp_blockdev_handle_t part_blockdev_1 = NULL; |
| 70 | + TEST_ESP_OK(esp_partition_get_blockdev(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage1", &part_blockdev_1)); |
| 71 | + |
| 72 | + //get pointer to esp_partition_t object for partition 'storage2' |
| 73 | + esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage2"); |
| 74 | + TEST_ASSERT_NOT_NULL(iter); |
| 75 | + esp_partition_t *part = (esp_partition_t*)esp_partition_get(iter); |
| 76 | + TEST_ASSERT_NOT_NULL(part); |
| 77 | + esp_partition_iterator_release(iter); |
| 78 | + |
| 79 | + esp_blockdev_handle_t part_blockdev_2 = NULL; |
| 80 | + TEST_ESP_OK(esp_partition_ptr_get_blockdev(part, &part_blockdev_2)); |
| 81 | + |
| 82 | + //erase & write & read data on both partitions in parallel |
| 83 | + const size_t data_size = 256; |
| 84 | + uint8_t test_data[data_size]; |
| 85 | + const off_t target_addr = 3*4*1024; |
| 86 | + uint8_t data_buffer_1[data_size]; |
| 87 | + uint8_t data_buffer_2[data_size]; |
| 88 | + memset((void*)data_buffer_1, 0, data_size); |
| 89 | + memset((void*)data_buffer_2, 0, data_size); |
| 90 | + |
| 91 | + //erase the first sector data from the blockdev and check it's really wiped |
| 92 | + TEST_ESP_OK(part_blockdev_1->erase(part_blockdev_1, target_addr, part_blockdev_1->geometry.erase_size)); |
| 93 | + TEST_ESP_OK(part_blockdev_2->erase(part_blockdev_2, target_addr, part_blockdev_2->geometry.erase_size)); |
| 94 | + memset((void*)test_data, 0xFF, data_size); //erased NOR flash sector contains only 1s |
| 95 | + |
| 96 | + TEST_ESP_OK(part_blockdev_1->read(part_blockdev_1, data_buffer_1, sizeof(data_buffer_1), target_addr, data_size)); |
| 97 | + TEST_ESP_OK(part_blockdev_2->read(part_blockdev_2, data_buffer_2, sizeof(data_buffer_2), target_addr, data_size)); |
| 98 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer_1, data_size)); |
| 99 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer_2, data_size)); |
| 100 | + |
| 101 | + //write to the blockdev 1 |
| 102 | + memset((void*)test_data, 'A', data_size); |
| 103 | + TEST_ESP_OK(part_blockdev_1->write(part_blockdev_1, test_data, target_addr, data_size)); |
| 104 | + |
| 105 | + //read the data written before from the blockdev 1 |
| 106 | + TEST_ESP_OK(part_blockdev_1->read(part_blockdev_1, data_buffer_1, sizeof(data_buffer_1), target_addr, data_size)); |
| 107 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer_1, data_size)); |
| 108 | + |
| 109 | + //write to the blockdev 2 |
| 110 | + memset((void*)test_data, 'B', data_size); |
| 111 | + TEST_ESP_OK(part_blockdev_2->write(part_blockdev_2, test_data, target_addr, data_size)); |
| 112 | + |
| 113 | + //read the data written before from the blockdev 2 |
| 114 | + TEST_ESP_OK(part_blockdev_2->read(part_blockdev_2, data_buffer_2, sizeof(data_buffer_2), target_addr, data_size)); |
| 115 | + TEST_ASSERT_EQUAL(0, memcmp(test_data, data_buffer_2, data_size)); |
| 116 | + |
| 117 | + //release the BDL object - nothing to check here, the BDL memory is just freed |
| 118 | + esp_partition_release_blockdev(part_blockdev_1); |
| 119 | + esp_partition_release_blockdev(part_blockdev_2); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_GROUP_RUNNER(partition_bdl) |
| 123 | +{ |
| 124 | + RUN_TEST_CASE(partition_bdl, test_partition_bdl_ops); |
| 125 | + RUN_TEST_CASE(partition_bdl, test_two_partitions_bdl_ops); |
| 126 | +} |
| 127 | + |
| 128 | +static void run_all_tests(void) |
| 129 | +{ |
| 130 | + RUN_TEST_GROUP(partition_bdl); |
| 131 | +} |
| 132 | + |
| 133 | +int main(int argc, char **argv) |
| 134 | +{ |
| 135 | + UNITY_MAIN_FUNC(run_all_tests); |
| 136 | + return 0; |
| 137 | +} |
0 commit comments