Sughosh Ganu | 77079e7 | 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 | 77079e7 | 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 | |
Sughosh Ganu | d02bcd6 | 2024-03-22 16:27:30 +0530 | [diff] [blame] | 95 | ut_assertok(setup_blk_device(uts)); |
| 96 | ut_assertok(populate_mmc_disk_image(uts)); |
| 97 | ut_assertok(write_mmc_blk_device(uts)); |
| 98 | |
Marek Vasut | 4ede2df | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() |
| 101 | * to populate g_dev global pointer in that library. |
| 102 | */ |
| 103 | event_notify_null(EVT_MAIN_LOOP); |
| 104 | |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 105 | ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev)); |
Sughosh Ganu | d02bcd6 | 2024-03-22 16:27:30 +0530 | [diff] [blame] | 106 | ut_assertok(fwu_init()); |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 107 | |
Jassi Brar | 72c5827 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 108 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 109 | |
| 110 | ut_asserteq(mdata.version, 0x1); |
| 111 | |
| 112 | return 0; |
| 113 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 114 | DM_TEST(dm_test_fwu_mdata_read, UTF_SCAN_FDT); |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 115 | |
| 116 | static int dm_test_fwu_mdata_write(struct unit_test_state *uts) |
| 117 | { |
| 118 | u32 active_idx; |
| 119 | struct udevice *dev; |
| 120 | struct fwu_mdata mdata = { 0 }; |
| 121 | |
Sughosh Ganu | d02bcd6 | 2024-03-22 16:27:30 +0530 | [diff] [blame] | 122 | ut_assertok(setup_blk_device(uts)); |
| 123 | ut_assertok(populate_mmc_disk_image(uts)); |
| 124 | ut_assertok(write_mmc_blk_device(uts)); |
| 125 | |
Marek Vasut | 4ede2df | 2023-08-23 02:16:52 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() |
| 128 | * to populate g_dev global pointer in that library. |
| 129 | */ |
| 130 | event_notify_null(EVT_MAIN_LOOP); |
| 131 | |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 132 | ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev)); |
| 133 | |
Sughosh Ganu | d02bcd6 | 2024-03-22 16:27:30 +0530 | [diff] [blame] | 134 | ut_assertok(fwu_init()); |
Jassi Brar | 72c5827 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 135 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 136 | |
| 137 | active_idx = (mdata.active_index + 1) % CONFIG_FWU_NUM_BANKS; |
| 138 | ut_assertok(fwu_set_active_index(active_idx)); |
| 139 | |
Jassi Brar | 72c5827 | 2023-03-06 17:18:58 -0600 | [diff] [blame] | 140 | ut_assertok(fwu_get_mdata(&mdata)); |
Sughosh Ganu | 77079e7 | 2022-10-21 18:16:05 +0530 | [diff] [blame] | 141 | ut_asserteq(mdata.active_index, active_idx); |
| 142 | |
| 143 | return 0; |
| 144 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 145 | DM_TEST(dm_test_fwu_mdata_write, UTF_SCAN_FDT); |