blob: 52018f610fe4764f2655f314069578ebd32489b5 [file] [log] [blame]
Sughosh Ganu873cf8a2022-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
Marek Vasut95311f72023-08-23 02:16:52 +020096 /*
97 * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks()
98 * to populate g_dev global pointer in that library.
99 */
100 event_notify_null(EVT_MAIN_LOOP);
101
Sughosh Ganu873cf8a2022-10-21 18:16:05 +0530102 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
103 ut_assertok(setup_blk_device(uts));
104 ut_assertok(populate_mmc_disk_image(uts));
105 ut_assertok(write_mmc_blk_device(uts));
106
Jassi Brar72168b92023-03-06 17:18:58 -0600107 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu873cf8a2022-10-21 18:16:05 +0530108
109 ut_asserteq(mdata.version, 0x1);
110
111 return 0;
112}
113DM_TEST(dm_test_fwu_mdata_read, UT_TESTF_SCAN_FDT);
114
115static int dm_test_fwu_mdata_write(struct unit_test_state *uts)
116{
117 u32 active_idx;
118 struct udevice *dev;
119 struct fwu_mdata mdata = { 0 };
120
Marek Vasut95311f72023-08-23 02:16:52 +0200121 /*
122 * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks()
123 * to populate g_dev global pointer in that library.
124 */
125 event_notify_null(EVT_MAIN_LOOP);
126
Sughosh Ganu873cf8a2022-10-21 18:16:05 +0530127 ut_assertok(setup_blk_device(uts));
128 ut_assertok(populate_mmc_disk_image(uts));
129 ut_assertok(write_mmc_blk_device(uts));
130
131 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
132
Jassi Brar72168b92023-03-06 17:18:58 -0600133 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu873cf8a2022-10-21 18:16:05 +0530134
135 active_idx = (mdata.active_index + 1) % CONFIG_FWU_NUM_BANKS;
136 ut_assertok(fwu_set_active_index(active_idx));
137
Jassi Brar72168b92023-03-06 17:18:58 -0600138 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu873cf8a2022-10-21 18:16:05 +0530139 ut_asserteq(mdata.active_index, active_idx);
140
141 return 0;
142}
143DM_TEST(dm_test_fwu_mdata_write, UT_TESTF_SCAN_FDT);