blob: 1f7af8e5d790ed2fd542efc9fcb9a813145334f3 [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
34 */
35 console_in_puts("\x0e\x0d\x0e\x0d\e");
36 ut_assertok(run_command("cedit run", 0));
37
38 exp = cur_exp;
39 scn = expo_lookup_scene_id(exp, exp->scene_id);
40 ut_assertnonnull(scn);
41
42 menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE);
43 ut_assertnonnull(menu);
44
45 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
46 ut_assertnonnull(txt);
47 ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
48
49 ut_asserteq(ID_AC_ON, menu->cur_item_id);
50
51 return 0;
52}
Simon Glassf0425022024-08-22 07:57:54 -060053BOOTSTD_TEST(cedit_base, UTF_CONSOLE);
Simon Glass28bf4352023-08-14 16:40:33 -060054
Simon Glassb1cd32b2023-08-14 16:40:34 -060055/* Check the cedit write_fdt and read_fdt commands */
Simon Glass28bf4352023-08-14 16:40:33 -060056static int cedit_fdt(struct unit_test_state *uts)
57{
Simon Glass70c579b2023-10-01 19:13:39 -060058 struct scene_obj_textline *tline;
Simon Glass28bf4352023-08-14 16:40:33 -060059 struct video_priv *vid_priv;
60 extern struct expo *cur_exp;
Simon Glassb1cd32b2023-08-14 16:40:34 -060061 struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -060062 ulong addr = 0x1000;
63 struct ofprop prop;
64 struct scene *scn;
65 oftree tree;
66 ofnode node;
Simon Glass70c579b2023-10-01 19:13:39 -060067 char *str;
Simon Glass28bf4352023-08-14 16:40:33 -060068 void *fdt;
69 int i;
70
Simon Glass28bf4352023-08-14 16:40:33 -060071 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
72
73 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
74
Simon Glassb1cd32b2023-08-14 16:40:34 -060075 /* get a menu to fiddle with */
76 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
77 ut_assertnonnull(menu);
78 menu->cur_item_id = ID_CPU_SPEED_2;
79
Simon Glass70c579b2023-10-01 19:13:39 -060080 /* get a textline to fiddle with too */
81 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
82 ut_assertnonnull(tline);
83 str = abuf_data(&tline->buf);
84 strcpy(str, "my-machine");
85
Simon Glass28bf4352023-08-14 16:40:33 -060086 ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0));
87 ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr));
88 ut_assert_nextlinen("1024 bytes read");
89
90 fdt = map_sysmem(addr, 1024);
91 tree = oftree_from_fdt(fdt);
92 node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME);
Simon Glass70c579b2023-10-01 19:13:39 -060093 ut_assert(ofnode_valid(node));
Simon Glass28bf4352023-08-14 16:40:33 -060094
Simon Glassb1cd32b2023-08-14 16:40:34 -060095 ut_asserteq(ID_CPU_SPEED_2,
Simon Glass28bf4352023-08-14 16:40:33 -060096 ofnode_read_u32_default(node, "cpu-speed", 0));
Simon Glassb1cd32b2023-08-14 16:40:34 -060097 ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -060098 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass28bf4352023-08-14 16:40:33 -060099
Simon Glass70c579b2023-10-01 19:13:39 -0600100 /* There should only be 5 properties */
Simon Glass28bf4352023-08-14 16:40:33 -0600101 for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop);
102 i++, ofnode_next_property(&prop))
103 ;
Simon Glass70c579b2023-10-01 19:13:39 -0600104 ut_asserteq(5, i);
Simon Glass28bf4352023-08-14 16:40:33 -0600105
Simon Glassb1cd32b2023-08-14 16:40:34 -0600106 ut_assert_console_end();
107
108 /* reset the expo */
109 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600110 *str = '\0';
Simon Glassb1cd32b2023-08-14 16:40:34 -0600111
112 /* load in the settings and make sure they update */
113 ut_assertok(run_command("cedit read_fdt hostfs - settings.dtb", 0));
114 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600115 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600116
117 ut_assertnonnull(menu);
Simon Glass28bf4352023-08-14 16:40:33 -0600118 ut_assert_console_end();
119
120 return 0;
121}
Simon Glassf0425022024-08-22 07:57:54 -0600122BOOTSTD_TEST(cedit_fdt, UTF_CONSOLE);
Simon Glass237f3752023-08-14 16:40:35 -0600123
Simon Glass0f2e5a62023-08-14 16:40:36 -0600124/* Check the cedit write_env and read_env commands */
Simon Glass237f3752023-08-14 16:40:35 -0600125static int cedit_env(struct unit_test_state *uts)
126{
Simon Glass70c579b2023-10-01 19:13:39 -0600127 struct scene_obj_textline *tline;
Simon Glass237f3752023-08-14 16:40:35 -0600128 struct video_priv *vid_priv;
129 extern struct expo *cur_exp;
130 struct scene_obj_menu *menu;
131 struct scene *scn;
Simon Glass70c579b2023-10-01 19:13:39 -0600132 char *str;
Simon Glass237f3752023-08-14 16:40:35 -0600133
Simon Glass237f3752023-08-14 16:40:35 -0600134 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
135
136 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
137
138 /* get a menu to fiddle with */
139 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
140 ut_assertnonnull(menu);
141 menu->cur_item_id = ID_CPU_SPEED_2;
142
Simon Glass70c579b2023-10-01 19:13:39 -0600143 /* get a textline to fiddle with too */
144 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
145 ut_assertnonnull(tline);
146 str = abuf_data(&tline->buf);
147 strcpy(str, "my-machine");
148
Simon Glass237f3752023-08-14 16:40:35 -0600149 ut_assertok(run_command("cedit write_env -v", 0));
150 ut_assert_nextlinen("c.cpu-speed=7");
151 ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz");
152 ut_assert_nextlinen("c.power-loss=10");
153 ut_assert_nextlinen("c.power-loss-str=Always Off");
Simon Glass70c579b2023-10-01 19:13:39 -0600154 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass237f3752023-08-14 16:40:35 -0600155 ut_assert_console_end();
156
157 ut_asserteq(7, env_get_ulong("c.cpu-speed", 10, 0));
158 ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600159 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass237f3752023-08-14 16:40:35 -0600160
Simon Glass0f2e5a62023-08-14 16:40:36 -0600161 /* reset the expo */
162 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600163 *str = '\0';
Simon Glass0f2e5a62023-08-14 16:40:36 -0600164
165 ut_assertok(run_command("cedit read_env -v", 0));
166 ut_assert_nextlinen("c.cpu-speed=7");
167 ut_assert_nextlinen("c.power-loss=10");
Simon Glass70c579b2023-10-01 19:13:39 -0600168 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass0f2e5a62023-08-14 16:40:36 -0600169 ut_assert_console_end();
170
171 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600172 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass0f2e5a62023-08-14 16:40:36 -0600173
Simon Glass237f3752023-08-14 16:40:35 -0600174 return 0;
175}
Simon Glassf0425022024-08-22 07:57:54 -0600176BOOTSTD_TEST(cedit_env, UTF_CONSOLE);
Simon Glass2b91ca62023-08-14 16:40:37 -0600177
178/* Check the cedit write_cmos and read_cmos commands */
179static int cedit_cmos(struct unit_test_state *uts)
180{
181 struct scene_obj_menu *menu, *menu2;
182 struct video_priv *vid_priv;
183 extern struct expo *cur_exp;
184 struct scene *scn;
185
Simon Glass2b91ca62023-08-14 16:40:37 -0600186 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
187
188 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
189
190 /* get the menus to fiddle with */
191 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
192 ut_assertnonnull(menu);
193 menu->cur_item_id = ID_CPU_SPEED_2;
194
195 menu2 = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU);
196 ut_assertnonnull(menu2);
197 menu2->cur_item_id = ID_AC_MEMORY;
198
199 ut_assertok(run_command("cedit write_cmos -v", 0));
200 ut_assert_nextlinen("Write 2 bytes from offset 80 to 84");
201 ut_assert_console_end();
202
Simon Glass4462fa32023-08-14 16:40:38 -0600203 /* reset the expo */
204 menu->cur_item_id = ID_CPU_SPEED_1;
205 menu2->cur_item_id = ID_AC_OFF;
206
207 ut_assertok(run_command("cedit read_cmos -v", 0));
208 ut_assert_nextlinen("Read 2 bytes from offset 80 to 84");
209 ut_assert_console_end();
210
211 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
212 ut_asserteq(ID_AC_MEMORY, menu2->cur_item_id);
213
Simon Glass2b91ca62023-08-14 16:40:37 -0600214 return 0;
215}
Simon Glassf0425022024-08-22 07:57:54 -0600216BOOTSTD_TEST(cedit_cmos, UTF_CONSOLE);