|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2024 Nordic Semiconductor ASA |
| 3 | + * Copyright (c) 2015 Runtime Inc |
| 4 | + * Copyright (c) 2020 Gerson Fernando Budke <[email protected]> |
| 5 | + * Copyright 2024 NXP |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: Apache-2.0 |
| 8 | + */ |
| 9 | + |
| 10 | +#include <zephyr/ztest.h> |
| 11 | +#include <zephyr/drivers/flash.h> |
| 12 | +#include <zephyr/storage/flash_map.h> |
| 13 | + |
| 14 | +#define SLOT1_PARTITION slot1_partition |
| 15 | +#define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION) |
| 16 | +#define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION) |
| 17 | +#define SLOT1_PARTITION_NODE DT_NODELABEL(SLOT1_PARTITION) |
| 18 | +#define SLOT1_PARTITION_OFFSET FIXED_PARTITION_OFFSET(SLOT1_PARTITION) |
| 19 | + |
| 20 | +ZTEST(flash_map_sha, test_flash_area_check_int_sha256) |
| 21 | +{ |
| 22 | + /* for i in {1..16}; do echo $'0123456789abcdef\nfedcba98765432' >> tst.sha; done |
| 23 | + * hexdump tst.sha |
| 24 | + */ |
| 25 | + uint8_t tst_vec[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 26 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 27 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 28 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 29 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 30 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 31 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 32 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 33 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 34 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 35 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 36 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 37 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 38 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 39 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 40 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 41 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 42 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 43 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 44 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 45 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 46 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 47 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 48 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 49 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 50 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 51 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 52 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 53 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 54 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 55 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 56 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 57 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 58 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 59 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 60 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 61 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 62 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 63 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 64 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 65 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 66 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 67 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 68 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 69 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 70 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 71 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 72 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 73 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 74 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 75 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 76 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 77 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 78 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 79 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 80 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 81 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 82 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 83 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 84 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, |
| 85 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 86 | + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 87 | + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, |
| 88 | + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a }; |
| 89 | + /* sha256sum tst.sha */ |
| 90 | + uint8_t tst_sha[] = { 0xae, 0xed, 0x7d, 0x59, 0x53, 0xbd, 0xb7, 0x28, |
| 91 | + 0x3e, 0x59, 0xc2, 0x65, 0x59, 0x62, 0xe3, 0x7e, |
| 92 | + 0xfa, 0x97, 0xbd, 0x76, 0xf6, 0xac, 0xc3, 0x92, |
| 93 | + 0x59, 0x48, 0x4e, 0xc0, 0xaf, 0xa8, 0x49, 0x65 }; |
| 94 | + |
| 95 | + const struct flash_area *fa; |
| 96 | + struct flash_area_check fac = { NULL, 0, -1, NULL, 0 }; |
| 97 | + uint8_t buffer[16]; |
| 98 | + int rc; |
| 99 | + |
| 100 | + rc = flash_area_open(SLOT1_PARTITION_ID, &fa); |
| 101 | + zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc); |
| 102 | + rc = flash_area_erase(fa, 0, fa->fa_size); |
| 103 | + zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc); |
| 104 | + rc = flash_area_write(fa, 0, tst_vec, sizeof(tst_vec)); |
| 105 | + zassert_true(rc == 0, "Flash img write, error %d\n", rc); |
| 106 | + |
| 107 | + rc = flash_area_check_int_sha256(NULL, NULL); |
| 108 | + zassert_true(rc == -EINVAL, "Flash area check int 256 params 1, 2\n"); |
| 109 | + rc = flash_area_check_int_sha256(NULL, &fac); |
| 110 | + zassert_true(rc == -EINVAL, "Flash area check int 256 params 2\n"); |
| 111 | + rc = flash_area_check_int_sha256(fa, NULL); |
| 112 | + zassert_true(rc == -EINVAL, "Flash area check int 256 params 1\n"); |
| 113 | + |
| 114 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 115 | + zassert_true(rc == -EINVAL, "Flash area check int 256 fac match\n"); |
| 116 | + fac.match = tst_sha; |
| 117 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 118 | + zassert_true(rc == -EINVAL, "Flash area check int 256 fac clen\n"); |
| 119 | + fac.clen = sizeof(tst_vec); |
| 120 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 121 | + zassert_true(rc == -EINVAL, "Flash area check int 256 fac off\n"); |
| 122 | + fac.off = 0; |
| 123 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 124 | + zassert_true(rc == -EINVAL, "Flash area check int 256 fac rbuf\n"); |
| 125 | + fac.rbuf = buffer; |
| 126 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 127 | + zassert_true(rc == -EINVAL, "Flash area check int 256 fac rblen\n"); |
| 128 | + fac.rblen = sizeof(buffer); |
| 129 | + |
| 130 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 131 | + zassert_true(rc == 0, "Flash area check int 256 OK, error %d\n", rc); |
| 132 | + tst_sha[0] = 0x00; |
| 133 | + rc = flash_area_check_int_sha256(fa, &fac); |
| 134 | + zassert_false(rc == 0, "Flash area check int 256 wrong sha\n"); |
| 135 | + |
| 136 | + flash_area_close(fa); |
| 137 | +} |
| 138 | + |
| 139 | +ZTEST_SUITE(flash_map_sha, NULL, NULL, NULL, NULL, NULL); |
0 commit comments