blob: 4d1b99bc2eaaabfdee756b11a61f4f0bce348825 [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
Simon Glass44e4ec72023-08-14 16:40:26 -060028 /*
29 * ^N Move down to second menu
30 * ^M Open menu
31 * ^N Move down to second item
32 * ^M Select item
33 * \e Quit
Simon Glass53a0a2f2024-10-14 16:31:57 -060034 *
35 * cedit_run() returns -EACCESS so this command returns CMD_RET_FAILURE
Simon Glass44e4ec72023-08-14 16:40:26 -060036 */
37 console_in_puts("\x0e\x0d\x0e\x0d\e");
Simon Glass53a0a2f2024-10-14 16:31:57 -060038 ut_asserteq(1, run_command("cedit run", 0));
Simon Glass44e4ec72023-08-14 16:40:26 -060039
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}
Simon Glassf0425022024-08-22 07:57:54 -060055BOOTSTD_TEST(cedit_base, UTF_CONSOLE);
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
Simon Glass28bf4352023-08-14 16:40:33 -060073 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
74
75 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
76
Simon Glassb1cd32b2023-08-14 16:40:34 -060077 /* get a menu to fiddle with */
78 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
79 ut_assertnonnull(menu);
80 menu->cur_item_id = ID_CPU_SPEED_2;
81
Simon Glass70c579b2023-10-01 19:13:39 -060082 /* get a textline to fiddle with too */
83 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
84 ut_assertnonnull(tline);
85 str = abuf_data(&tline->buf);
86 strcpy(str, "my-machine");
87
Simon Glass28bf4352023-08-14 16:40:33 -060088 ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0));
89 ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr));
90 ut_assert_nextlinen("1024 bytes read");
91
92 fdt = map_sysmem(addr, 1024);
93 tree = oftree_from_fdt(fdt);
94 node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME);
Simon Glass70c579b2023-10-01 19:13:39 -060095 ut_assert(ofnode_valid(node));
Simon Glass28bf4352023-08-14 16:40:33 -060096
Simon Glassb1cd32b2023-08-14 16:40:34 -060097 ut_asserteq(ID_CPU_SPEED_2,
Simon Glass28bf4352023-08-14 16:40:33 -060098 ofnode_read_u32_default(node, "cpu-speed", 0));
Simon Glass6f3e87a2024-10-14 16:32:00 -060099 ut_asserteq(3,
100 ofnode_read_u32_default(node, "cpu-speed-value", 0));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600101 ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600102 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass28bf4352023-08-14 16:40:33 -0600103
Simon Glass6f3e87a2024-10-14 16:32:00 -0600104 /* There should only be 7 properties */
Simon Glass28bf4352023-08-14 16:40:33 -0600105 for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop);
106 i++, ofnode_next_property(&prop))
107 ;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600108 ut_asserteq(7, i);
Simon Glass28bf4352023-08-14 16:40:33 -0600109
Simon Glassb1cd32b2023-08-14 16:40:34 -0600110 ut_assert_console_end();
111
112 /* reset the expo */
113 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600114 *str = '\0';
Simon Glassb1cd32b2023-08-14 16:40:34 -0600115
116 /* load in the settings and make sure they update */
117 ut_assertok(run_command("cedit read_fdt hostfs - settings.dtb", 0));
118 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600119 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600120
121 ut_assertnonnull(menu);
Simon Glass28bf4352023-08-14 16:40:33 -0600122 ut_assert_console_end();
123
124 return 0;
125}
Simon Glassf0425022024-08-22 07:57:54 -0600126BOOTSTD_TEST(cedit_fdt, UTF_CONSOLE);
Simon Glass237f3752023-08-14 16:40:35 -0600127
Simon Glass0f2e5a62023-08-14 16:40:36 -0600128/* Check the cedit write_env and read_env commands */
Simon Glass237f3752023-08-14 16:40:35 -0600129static int cedit_env(struct unit_test_state *uts)
130{
Simon Glass70c579b2023-10-01 19:13:39 -0600131 struct scene_obj_textline *tline;
Simon Glass237f3752023-08-14 16:40:35 -0600132 struct video_priv *vid_priv;
133 extern struct expo *cur_exp;
134 struct scene_obj_menu *menu;
135 struct scene *scn;
Simon Glass70c579b2023-10-01 19:13:39 -0600136 char *str;
Simon Glass237f3752023-08-14 16:40:35 -0600137
Simon Glass237f3752023-08-14 16:40:35 -0600138 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));
Simon Glass53a0a2f2024-10-14 16:31:57 -0600154 ut_assert_nextlinen("c.cpu-speed=11");
Simon Glass237f3752023-08-14 16:40:35 -0600155 ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz");
Simon Glass6f3e87a2024-10-14 16:32:00 -0600156 ut_assert_nextlinen("c.cpu-speed-value=3");
Simon Glass53a0a2f2024-10-14 16:31:57 -0600157 ut_assert_nextlinen("c.power-loss=14");
Simon Glass237f3752023-08-14 16:40:35 -0600158 ut_assert_nextlinen("c.power-loss-str=Always Off");
Simon Glass6f3e87a2024-10-14 16:32:00 -0600159 ut_assert_nextlinen("c.power-loss-value=0");
Simon Glass70c579b2023-10-01 19:13:39 -0600160 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass237f3752023-08-14 16:40:35 -0600161 ut_assert_console_end();
162
Simon Glass53a0a2f2024-10-14 16:31:57 -0600163 ut_asserteq(11, env_get_ulong("c.cpu-speed", 10, 0));
Simon Glass237f3752023-08-14 16:40:35 -0600164 ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600165 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass237f3752023-08-14 16:40:35 -0600166
Simon Glass0f2e5a62023-08-14 16:40:36 -0600167 /* reset the expo */
168 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600169 *str = '\0';
Simon Glass0f2e5a62023-08-14 16:40:36 -0600170
171 ut_assertok(run_command("cedit read_env -v", 0));
Simon Glass53a0a2f2024-10-14 16:31:57 -0600172 ut_assert_nextlinen("c.cpu-speed=11");
173 ut_assert_nextlinen("c.power-loss=14");
Simon Glass70c579b2023-10-01 19:13:39 -0600174 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass0f2e5a62023-08-14 16:40:36 -0600175 ut_assert_console_end();
176
177 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600178 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass0f2e5a62023-08-14 16:40:36 -0600179
Simon Glass237f3752023-08-14 16:40:35 -0600180 return 0;
181}
Simon Glassf0425022024-08-22 07:57:54 -0600182BOOTSTD_TEST(cedit_env, UTF_CONSOLE);
Simon Glass2b91ca62023-08-14 16:40:37 -0600183
184/* Check the cedit write_cmos and read_cmos commands */
185static int cedit_cmos(struct unit_test_state *uts)
186{
187 struct scene_obj_menu *menu, *menu2;
188 struct video_priv *vid_priv;
189 extern struct expo *cur_exp;
190 struct scene *scn;
191
Simon Glass2b91ca62023-08-14 16:40:37 -0600192 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
193
194 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
195
196 /* get the menus to fiddle with */
197 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
198 ut_assertnonnull(menu);
199 menu->cur_item_id = ID_CPU_SPEED_2;
200
201 menu2 = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU);
202 ut_assertnonnull(menu2);
203 menu2->cur_item_id = ID_AC_MEMORY;
204
205 ut_assertok(run_command("cedit write_cmos -v", 0));
206 ut_assert_nextlinen("Write 2 bytes from offset 80 to 84");
207 ut_assert_console_end();
208
Simon Glass4462fa32023-08-14 16:40:38 -0600209 /* reset the expo */
210 menu->cur_item_id = ID_CPU_SPEED_1;
211 menu2->cur_item_id = ID_AC_OFF;
212
213 ut_assertok(run_command("cedit read_cmos -v", 0));
214 ut_assert_nextlinen("Read 2 bytes from offset 80 to 84");
215 ut_assert_console_end();
216
217 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
218 ut_asserteq(ID_AC_MEMORY, menu2->cur_item_id);
219
Simon Glass2b91ca62023-08-14 16:40:37 -0600220 return 0;
221}
Simon Glassf0425022024-08-22 07:57:54 -0600222BOOTSTD_TEST(cedit_cmos, UTF_CONSOLE);