blob: 621ba647d96f2ff597f6dab3176fbeb26d568ca9 [file] [log] [blame]
Sughosh Ganu77079e72022-10-21 18:16:05 +05301// 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>
8#include <common.h>
9#include <dm.h>
10#include <fwu.h>
11#include <fwu_mdata.h>
12#include <log.h>
13#include <malloc.h>
14#include <memalign.h>
15#include <part.h>
16
17#include <dm/test.h>
18#include <test/ut.h>
19
20#include "fwu_mdata_disk_image.h"
21
22/* Block size of compressed disk image */
23#define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
24
25static struct udevice *mmc_dev;
26static struct blk_desc *dev_desc;
27
28/* One 8 byte block of the compressed disk image */
29struct line {
30 size_t addr;
31 char *line;
32};
33
34/* Compressed disk image */
35struct compressed_disk_image {
36 size_t length;
37 struct line lines[];
38};
39
40static const struct compressed_disk_image img = FWU_MDATA_DISK_IMG;
41
42/* Decompressed disk image */
43static u8 *image;
44
45static int setup_blk_device(struct unit_test_state *uts)
46{
47 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &mmc_dev));
48 ut_assertok(blk_get_device_by_str("mmc", "0", &dev_desc));
49
50 return 0;
51}
52
53static int populate_mmc_disk_image(struct unit_test_state *uts)
54{
55 u8 *buf;
56 size_t i;
57 size_t addr;
58 size_t len;
59
60 buf = malloc(img.length);
61 if (!buf)
62 return -ENOMEM;
63
64 memset(buf, 0, img.length);
65
66 for (i = 0; ; i++) {
67 if (!img.lines[i].line)
68 break;
69 addr = img.lines[i].addr;
70 len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
71 if (addr + len > img.length)
72 len = img.length - addr;
73 memcpy(buf + addr, img.lines[i].line, len);
74 }
75 image = buf;
76
77 return 0;
78}
79
80static int write_mmc_blk_device(struct unit_test_state *uts)
81{
82 lbaint_t blkcnt;
83
84 blkcnt = BLOCK_CNT(img.length, dev_desc);
85
86 ut_asserteq(blkcnt, blk_dwrite(dev_desc, 0, blkcnt, image));
87
88 return 0;
89}
90
91static int dm_test_fwu_mdata_read(struct unit_test_state *uts)
92{
93 struct udevice *dev;
94 struct fwu_mdata mdata = { 0 };
95
Sughosh Ganud02bcd62024-03-22 16:27:30 +053096 ut_assertok(setup_blk_device(uts));
97 ut_assertok(populate_mmc_disk_image(uts));
98 ut_assertok(write_mmc_blk_device(uts));
99
Marek Vasut4ede2df2023-08-23 02:16:52 +0200100 /*
101 * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks()
102 * to populate g_dev global pointer in that library.
103 */
104 event_notify_null(EVT_MAIN_LOOP);
105
Sughosh Ganu77079e72022-10-21 18:16:05 +0530106 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
Sughosh Ganud02bcd62024-03-22 16:27:30 +0530107 ut_assertok(fwu_init());
Sughosh Ganu77079e72022-10-21 18:16:05 +0530108
Jassi Brar72c58272023-03-06 17:18:58 -0600109 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530110
111 ut_asserteq(mdata.version, 0x1);
112
113 return 0;
114}
115DM_TEST(dm_test_fwu_mdata_read, UT_TESTF_SCAN_FDT);
116
117static int dm_test_fwu_mdata_write(struct unit_test_state *uts)
118{
119 u32 active_idx;
120 struct udevice *dev;
121 struct fwu_mdata mdata = { 0 };
122
Sughosh Ganud02bcd62024-03-22 16:27:30 +0530123 ut_assertok(setup_blk_device(uts));
124 ut_assertok(populate_mmc_disk_image(uts));
125 ut_assertok(write_mmc_blk_device(uts));
126
Marek Vasut4ede2df2023-08-23 02:16:52 +0200127 /*
128 * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks()
129 * to populate g_dev global pointer in that library.
130 */
131 event_notify_null(EVT_MAIN_LOOP);
132
Sughosh Ganu77079e72022-10-21 18:16:05 +0530133
134 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
135
Sughosh Ganud02bcd62024-03-22 16:27:30 +0530136 ut_assertok(fwu_init());
Jassi Brar72c58272023-03-06 17:18:58 -0600137 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530138
139 active_idx = (mdata.active_index + 1) % CONFIG_FWU_NUM_BANKS;
140 ut_assertok(fwu_set_active_index(active_idx));
141
Jassi Brar72c58272023-03-06 17:18:58 -0600142 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530143 ut_asserteq(mdata.active_index, active_idx);
144
145 return 0;
146}
147DM_TEST(dm_test_fwu_mdata_write, UT_TESTF_SCAN_FDT);