blob: b7680632f95f65f4778d40f77f0c7463aae3591e [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>
Sughosh Ganu77079e72022-10-21 18:16:05 +05308#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
24static struct udevice *mmc_dev;
25static struct blk_desc *dev_desc;
26
27/* One 8 byte block of the compressed disk image */
28struct line {
29 size_t addr;
30 char *line;
31};
32
33/* Compressed disk image */
34struct compressed_disk_image {
35 size_t length;
36 struct line lines[];
37};
38
39static const struct compressed_disk_image img = FWU_MDATA_DISK_IMG;
40
41/* Decompressed disk image */
42static u8 *image;
43
44static 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
52static 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
79static 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
90static int dm_test_fwu_mdata_read(struct unit_test_state *uts)
91{
92 struct udevice *dev;
93 struct fwu_mdata mdata = { 0 };
94
Sughosh Ganud02bcd62024-03-22 16:27:30 +053095 ut_assertok(setup_blk_device(uts));
96 ut_assertok(populate_mmc_disk_image(uts));
97 ut_assertok(write_mmc_blk_device(uts));
98
Marek Vasut4ede2df2023-08-23 02:16:52 +020099 /*
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 Ganu77079e72022-10-21 18:16:05 +0530105 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
Sughosh Ganud02bcd62024-03-22 16:27:30 +0530106 ut_assertok(fwu_init());
Sughosh Ganu77079e72022-10-21 18:16:05 +0530107
Jassi Brar72c58272023-03-06 17:18:58 -0600108 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530109
110 ut_asserteq(mdata.version, 0x1);
111
112 return 0;
113}
Simon Glass1a92f832024-08-22 07:57:48 -0600114DM_TEST(dm_test_fwu_mdata_read, UTF_SCAN_FDT);
Sughosh Ganu77079e72022-10-21 18:16:05 +0530115
116static 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 Ganud02bcd62024-03-22 16:27:30 +0530122 ut_assertok(setup_blk_device(uts));
123 ut_assertok(populate_mmc_disk_image(uts));
124 ut_assertok(write_mmc_blk_device(uts));
125
Marek Vasut4ede2df2023-08-23 02:16:52 +0200126 /*
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 Ganu77079e72022-10-21 18:16:05 +0530132 ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev));
133
Sughosh Ganud02bcd62024-03-22 16:27:30 +0530134 ut_assertok(fwu_init());
Jassi Brar72c58272023-03-06 17:18:58 -0600135 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530136
137 active_idx = (mdata.active_index + 1) % CONFIG_FWU_NUM_BANKS;
138 ut_assertok(fwu_set_active_index(active_idx));
139
Jassi Brar72c58272023-03-06 17:18:58 -0600140 ut_assertok(fwu_get_mdata(&mdata));
Sughosh Ganu77079e72022-10-21 18:16:05 +0530141 ut_asserteq(mdata.active_index, active_idx);
142
143 return 0;
144}
Simon Glass1a92f832024-08-22 07:57:48 -0600145DM_TEST(dm_test_fwu_mdata_write, UTF_SCAN_FDT);