Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2022, Linaro Limited |
| 4 | * Copyright (c) 2022, Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 5 | */ |
| 6 | |
| 7 | #include <blk.h> |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <fwu.h> |
| 10 | #include <fwu_mdata.h> |
| 11 | #include <log.h> |
| 12 | #include <malloc.h> |
| 13 | #include <memalign.h> |
| 14 | #include <part.h> |
| 15 | |
| 16 | #include <dm/test.h> |
| 17 | #include <test/ut.h> |
| 18 | |
| 19 | #include "fwu_mdata_disk_image.h" |
| 20 | |
| 21 | /* Block size of compressed disk image */ |
| 22 | #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8 |
| 23 | |
| 24 | static struct udevice *mmc_dev; |
| 25 | static struct blk_desc *dev_desc; |
| 26 | |
| 27 | /* One 8 byte block of the compressed disk image */ |
| 28 | struct line { |
| 29 | size_t addr; |
| 30 | char *line; |
| 31 | }; |
| 32 | |
| 33 | /* Compressed disk image */ |
| 34 | struct compressed_disk_image { |
| 35 | size_t length; |
| 36 | struct line lines[]; |
| 37 | }; |
| 38 | |
| 39 | static const struct compressed_disk_image img = FWU_MDATA_DISK_IMG; |
| 40 | |
| 41 | /* Decompressed disk image */ |
| 42 | static u8 *image; |
| 43 | |
| 44 | static int setup_blk_device(struct unit_test_state *uts) |
| 45 | { |
| 46 | ut_assertok(uclass_get_device(UCLASS_MMC, 0, &mmc_dev)); |
| 47 | ut_assertok(blk_get_device_by_str("mmc", "0", &dev_desc)); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int populate_mmc_disk_image(struct unit_test_state *uts) |
| 53 | { |
| 54 | u8 *buf; |
| 55 | size_t i; |
| 56 | size_t addr; |
| 57 | size_t len; |
| 58 | |
| 59 | buf = malloc(img.length); |
| 60 | if (!buf) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | memset(buf, 0, img.length); |
| 64 | |
| 65 | for (i = 0; ; i++) { |
| 66 | if (!img.lines[i].line) |
| 67 | break; |
| 68 | addr = img.lines[i].addr; |
| 69 | len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE; |
| 70 | if (addr + len > img.length) |
| 71 | len = img.length - addr; |
| 72 | memcpy(buf + addr, img.lines[i].line, len); |
| 73 | } |
| 74 | image = buf; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int write_mmc_blk_device(struct unit_test_state *uts) |
| 80 | { |
| 81 | lbaint_t blkcnt; |
| 82 | |
| 83 | blkcnt = BLOCK_CNT(img.length, dev_desc); |
| 84 | |
| 85 | ut_asserteq(blkcnt, blk_dwrite(dev_desc, 0, blkcnt, image)); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int dm_test_fwu_mdata_read(struct unit_test_state *uts) |
| 91 | { |
| 92 | struct udevice *dev; |
| 93 | struct fwu_mdata mdata = { 0 }; |
| 94 | |
Marek Vasut | 95311f7 | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() |
| 97 | * to populate g_dev global pointer in that library. |
| 98 | */ |
| 99 | event_notify_null(EVT_MAIN_LOOP); |
| 100 | |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 101 | ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev)); |
| 102 | ut_assertok(setup_blk_device(uts)); |
| 103 | ut_assertok(populate_mmc_disk_image(uts)); |
| 104 | ut_assertok(write_mmc_blk_device(uts)); |
| 105 | |
Jassi Brar | 72168b9 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 106 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 107 | |
| 108 | ut_asserteq(mdata.version, 0x1); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | DM_TEST(dm_test_fwu_mdata_read, UT_TESTF_SCAN_FDT); |
| 113 | |
| 114 | static int dm_test_fwu_mdata_write(struct unit_test_state *uts) |
| 115 | { |
| 116 | u32 active_idx; |
| 117 | struct udevice *dev; |
| 118 | struct fwu_mdata mdata = { 0 }; |
| 119 | |
Marek Vasut | 95311f7 | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() |
| 122 | * to populate g_dev global pointer in that library. |
| 123 | */ |
| 124 | event_notify_null(EVT_MAIN_LOOP); |
| 125 | |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 126 | ut_assertok(setup_blk_device(uts)); |
| 127 | ut_assertok(populate_mmc_disk_image(uts)); |
| 128 | ut_assertok(write_mmc_blk_device(uts)); |
| 129 | |
| 130 | ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev)); |
| 131 | |
Jassi Brar | 72168b9 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 132 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 133 | |
| 134 | active_idx = (mdata.active_index + 1) % CONFIG_FWU_NUM_BANKS; |
| 135 | ut_assertok(fwu_set_active_index(active_idx)); |
| 136 | |
Jassi Brar | 72168b9 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 137 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 873cf8a | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 138 | ut_asserteq(mdata.active_index, active_idx); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | DM_TEST(dm_test_fwu_mdata_write, UT_TESTF_SCAN_FDT); |