blob: fd19da0a0c0fcd4208d7772f7176fb14da0d77fa [file] [log] [blame]
Simon Glass44e4ec72023-08-14 16:40:26 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2023 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass44e4ec72023-08-14 16:40:26 -06007#include <cedit.h>
Simon Glass237f3752023-08-14 16:40:35 -06008#include <env.h>
Simon Glass44e4ec72023-08-14 16:40:26 -06009#include <expo.h>
Simon Glass28bf4352023-08-14 16:40:33 -060010#include <mapmem.h>
11#include <dm/ofnode.h>
Simon Glass44e4ec72023-08-14 16:40:26 -060012#include <test/ut.h>
13#include "bootstd_common.h"
14#include <test/cedit-test.h>
15#include "../../boot/scene_internal.h"
16
17/* Check the cedit command */
18static int cedit_base(struct unit_test_state *uts)
19{
20 extern struct expo *cur_exp;
21 struct scene_obj_menu *menu;
22 struct scene_obj_txt *txt;
23 struct expo *exp;
24 struct scene *scn;
25
26 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
27
28 console_record_reset_enable();
29
30 /*
31 * ^N Move down to second menu
32 * ^M Open menu
33 * ^N Move down to second item
34 * ^M Select item
35 * \e Quit
36 */
37 console_in_puts("\x0e\x0d\x0e\x0d\e");
38 ut_assertok(run_command("cedit run", 0));
39
40 exp = cur_exp;
41 scn = expo_lookup_scene_id(exp, exp->scene_id);
42 ut_assertnonnull(scn);
43
44 menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE);
45 ut_assertnonnull(menu);
46
47 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
48 ut_assertnonnull(txt);
49 ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
50
51 ut_asserteq(ID_AC_ON, menu->cur_item_id);
52
53 return 0;
54}
55BOOTSTD_TEST(cedit_base, 0);
Simon Glass28bf4352023-08-14 16:40:33 -060056
Simon Glassb1cd32b2023-08-14 16:40:34 -060057/* Check the cedit write_fdt and read_fdt commands */
Simon Glass28bf4352023-08-14 16:40:33 -060058static int cedit_fdt(struct unit_test_state *uts)
59{
Simon Glass70c579b2023-10-01 19:13:39 -060060 struct scene_obj_textline *tline;
Simon Glass28bf4352023-08-14 16:40:33 -060061 struct video_priv *vid_priv;
62 extern struct expo *cur_exp;
Simon Glassb1cd32b2023-08-14 16:40:34 -060063 struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -060064 ulong addr = 0x1000;
65 struct ofprop prop;
66 struct scene *scn;
67 oftree tree;
68 ofnode node;
Simon Glass70c579b2023-10-01 19:13:39 -060069 char *str;
Simon Glass28bf4352023-08-14 16:40:33 -060070 void *fdt;
71 int i;
72
73 console_record_reset_enable();
74 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
75
76 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
77
Simon Glassb1cd32b2023-08-14 16:40:34 -060078 /* get a menu to fiddle with */
79 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
80 ut_assertnonnull(menu);
81 menu->cur_item_id = ID_CPU_SPEED_2;
82
Simon Glass70c579b2023-10-01 19:13:39 -060083 /* get a textline to fiddle with too */
84 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
85 ut_assertnonnull(tline);
86 str = abuf_data(&tline->buf);
87 strcpy(str, "my-machine");
88
Simon Glass28bf4352023-08-14 16:40:33 -060089 ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0));
90 ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr));
91 ut_assert_nextlinen("1024 bytes read");
92
93 fdt = map_sysmem(addr, 1024);
94 tree = oftree_from_fdt(fdt);
95 node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME);
Simon Glass70c579b2023-10-01 19:13:39 -060096 ut_assert(ofnode_valid(node));
Simon Glass28bf4352023-08-14 16:40:33 -060097
Simon Glassb1cd32b2023-08-14 16:40:34 -060098 ut_asserteq(ID_CPU_SPEED_2,
Simon Glass28bf4352023-08-14 16:40:33 -060099 ofnode_read_u32_default(node, "cpu-speed", 0));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600100 ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600101 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass28bf4352023-08-14 16:40:33 -0600102
Simon Glass70c579b2023-10-01 19:13:39 -0600103 /* There should only be 5 properties */
Simon Glass28bf4352023-08-14 16:40:33 -0600104 for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop);
105 i++, ofnode_next_property(&prop))
106 ;
Simon Glass70c579b2023-10-01 19:13:39 -0600107 ut_asserteq(5, i);
Simon Glass28bf4352023-08-14 16:40:33 -0600108
Simon Glassb1cd32b2023-08-14 16:40:34 -0600109 ut_assert_console_end();
110
111 /* reset the expo */
112 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600113 *str = '\0';
Simon Glassb1cd32b2023-08-14 16:40:34 -0600114
115 /* load in the settings and make sure they update */
116 ut_assertok(run_command("cedit read_fdt hostfs - settings.dtb", 0));
117 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600118 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600119
120 ut_assertnonnull(menu);
Simon Glass28bf4352023-08-14 16:40:33 -0600121 ut_assert_console_end();
122
123 return 0;
124}
125BOOTSTD_TEST(cedit_fdt, 0);
Simon Glass237f3752023-08-14 16:40:35 -0600126
Simon Glass0f2e5a62023-08-14 16:40:36 -0600127/* Check the cedit write_env and read_env commands */
Simon Glass237f3752023-08-14 16:40:35 -0600128static int cedit_env(struct unit_test_state *uts)
129{
Simon Glass70c579b2023-10-01 19:13:39 -0600130 struct scene_obj_textline *tline;
Simon Glass237f3752023-08-14 16:40:35 -0600131 struct video_priv *vid_priv;
132 extern struct expo *cur_exp;
133 struct scene_obj_menu *menu;
134 struct scene *scn;
Simon Glass70c579b2023-10-01 19:13:39 -0600135 char *str;
Simon Glass237f3752023-08-14 16:40:35 -0600136
137 console_record_reset_enable();
138 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
139
140 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
141
142 /* get a menu to fiddle with */
143 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
144 ut_assertnonnull(menu);
145 menu->cur_item_id = ID_CPU_SPEED_2;
146
Simon Glass70c579b2023-10-01 19:13:39 -0600147 /* get a textline to fiddle with too */
148 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
149 ut_assertnonnull(tline);
150 str = abuf_data(&tline->buf);
151 strcpy(str, "my-machine");
152
Simon Glass237f3752023-08-14 16:40:35 -0600153 ut_assertok(run_command("cedit write_env -v", 0));
154 ut_assert_nextlinen("c.cpu-speed=7");
155 ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz");
156 ut_assert_nextlinen("c.power-loss=10");
157 ut_assert_nextlinen("c.power-loss-str=Always Off");
Simon Glass70c579b2023-10-01 19:13:39 -0600158 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass237f3752023-08-14 16:40:35 -0600159 ut_assert_console_end();
160
161 ut_asserteq(7, env_get_ulong("c.cpu-speed", 10, 0));
162 ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600163 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass237f3752023-08-14 16:40:35 -0600164
Simon Glass0f2e5a62023-08-14 16:40:36 -0600165 /* reset the expo */
166 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600167 *str = '\0';
Simon Glass0f2e5a62023-08-14 16:40:36 -0600168
169 ut_assertok(run_command("cedit read_env -v", 0));
170 ut_assert_nextlinen("c.cpu-speed=7");
171 ut_assert_nextlinen("c.power-loss=10");
Simon Glass70c579b2023-10-01 19:13:39 -0600172 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass0f2e5a62023-08-14 16:40:36 -0600173 ut_assert_console_end();
174
175 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600176 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass0f2e5a62023-08-14 16:40:36 -0600177
Simon Glass237f3752023-08-14 16:40:35 -0600178 return 0;
179}
180BOOTSTD_TEST(cedit_env, 0);
Simon Glass2b91ca62023-08-14 16:40:37 -0600181
182/* Check the cedit write_cmos and read_cmos commands */
183static int cedit_cmos(struct unit_test_state *uts)
184{
185 struct scene_obj_menu *menu, *menu2;
186 struct video_priv *vid_priv;
187 extern struct expo *cur_exp;
188 struct scene *scn;
189
190 console_record_reset_enable();
191 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
192
193 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
194
195 /* get the menus to fiddle with */
196 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
197 ut_assertnonnull(menu);
198 menu->cur_item_id = ID_CPU_SPEED_2;
199
200 menu2 = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU);
201 ut_assertnonnull(menu2);
202 menu2->cur_item_id = ID_AC_MEMORY;
203
204 ut_assertok(run_command("cedit write_cmos -v", 0));
205 ut_assert_nextlinen("Write 2 bytes from offset 80 to 84");
206 ut_assert_console_end();
207
Simon Glass4462fa32023-08-14 16:40:38 -0600208 /* reset the expo */
209 menu->cur_item_id = ID_CPU_SPEED_1;
210 menu2->cur_item_id = ID_AC_OFF;
211
212 ut_assertok(run_command("cedit read_cmos -v", 0));
213 ut_assert_nextlinen("Read 2 bytes from offset 80 to 84");
214 ut_assert_console_end();
215
216 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
217 ut_asserteq(ID_AC_MEMORY, menu2->cur_item_id);
218
Simon Glass2b91ca62023-08-14 16:40:37 -0600219 return 0;
220}
221BOOTSTD_TEST(cedit_cmos, 0);