blob: aa4171904864529e3358faf7cc61d624d94146de [file] [log] [blame]
Simon Glassf80ebb22023-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
7#include <common.h>
8#include <cedit.h>
Simon Glassfc9c0e02023-08-14 16:40:35 -06009#include <env.h>
Simon Glassf80ebb22023-08-14 16:40:26 -060010#include <expo.h>
Simon Glass2dee81f2023-08-14 16:40:33 -060011#include <mapmem.h>
12#include <dm/ofnode.h>
Simon Glassf80ebb22023-08-14 16:40:26 -060013#include <test/ut.h>
14#include "bootstd_common.h"
15#include <test/cedit-test.h>
16#include "../../boot/scene_internal.h"
17
18/* Check the cedit command */
19static int cedit_base(struct unit_test_state *uts)
20{
21 extern struct expo *cur_exp;
22 struct scene_obj_menu *menu;
23 struct scene_obj_txt *txt;
24 struct expo *exp;
25 struct scene *scn;
26
27 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
28
29 console_record_reset_enable();
30
31 /*
32 * ^N Move down to second menu
33 * ^M Open menu
34 * ^N Move down to second item
35 * ^M Select item
36 * \e Quit
37 */
38 console_in_puts("\x0e\x0d\x0e\x0d\e");
39 ut_assertok(run_command("cedit run", 0));
40
41 exp = cur_exp;
42 scn = expo_lookup_scene_id(exp, exp->scene_id);
43 ut_assertnonnull(scn);
44
45 menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE);
46 ut_assertnonnull(menu);
47
48 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
49 ut_assertnonnull(txt);
50 ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
51
52 ut_asserteq(ID_AC_ON, menu->cur_item_id);
53
54 return 0;
55}
56BOOTSTD_TEST(cedit_base, 0);
Simon Glass2dee81f2023-08-14 16:40:33 -060057
Simon Glass472317c2023-08-14 16:40:34 -060058/* Check the cedit write_fdt and read_fdt commands */
Simon Glass2dee81f2023-08-14 16:40:33 -060059static int cedit_fdt(struct unit_test_state *uts)
60{
Simon Glassc2bd2d32023-10-01 19:13:39 -060061 struct scene_obj_textline *tline;
Simon Glass2dee81f2023-08-14 16:40:33 -060062 struct video_priv *vid_priv;
63 extern struct expo *cur_exp;
Simon Glass472317c2023-08-14 16:40:34 -060064 struct scene_obj_menu *menu;
Simon Glass2dee81f2023-08-14 16:40:33 -060065 ulong addr = 0x1000;
66 struct ofprop prop;
67 struct scene *scn;
68 oftree tree;
69 ofnode node;
Simon Glassc2bd2d32023-10-01 19:13:39 -060070 char *str;
Simon Glass2dee81f2023-08-14 16:40:33 -060071 void *fdt;
72 int i;
73
74 console_record_reset_enable();
75 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
76
77 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
78
Simon Glass472317c2023-08-14 16:40:34 -060079 /* get a menu to fiddle with */
80 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
81 ut_assertnonnull(menu);
82 menu->cur_item_id = ID_CPU_SPEED_2;
83
Simon Glassc2bd2d32023-10-01 19:13:39 -060084 /* get a textline to fiddle with too */
85 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
86 ut_assertnonnull(tline);
87 str = abuf_data(&tline->buf);
88 strcpy(str, "my-machine");
89
Simon Glass2dee81f2023-08-14 16:40:33 -060090 ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0));
91 ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr));
92 ut_assert_nextlinen("1024 bytes read");
93
94 fdt = map_sysmem(addr, 1024);
95 tree = oftree_from_fdt(fdt);
96 node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME);
Simon Glassc2bd2d32023-10-01 19:13:39 -060097 ut_assert(ofnode_valid(node));
Simon Glass2dee81f2023-08-14 16:40:33 -060098
Simon Glass472317c2023-08-14 16:40:34 -060099 ut_asserteq(ID_CPU_SPEED_2,
Simon Glass2dee81f2023-08-14 16:40:33 -0600100 ofnode_read_u32_default(node, "cpu-speed", 0));
Simon Glass472317c2023-08-14 16:40:34 -0600101 ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str"));
Simon Glassc2bd2d32023-10-01 19:13:39 -0600102 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass2dee81f2023-08-14 16:40:33 -0600103
Simon Glassc2bd2d32023-10-01 19:13:39 -0600104 /* There should only be 5 properties */
Simon Glass2dee81f2023-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 Glassc2bd2d32023-10-01 19:13:39 -0600108 ut_asserteq(5, i);
Simon Glass2dee81f2023-08-14 16:40:33 -0600109
110 ut_assert_console_end();
111
Simon Glass472317c2023-08-14 16:40:34 -0600112 /* reset the expo */
113 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glassc2bd2d32023-10-01 19:13:39 -0600114 *str = '\0';
Simon Glass472317c2023-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 Glassc2bd2d32023-10-01 19:13:39 -0600119 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass472317c2023-08-14 16:40:34 -0600120
121 ut_assertnonnull(menu);
122 ut_assert_console_end();
123
Simon Glass2dee81f2023-08-14 16:40:33 -0600124 return 0;
125}
126BOOTSTD_TEST(cedit_fdt, 0);
Simon Glassfc9c0e02023-08-14 16:40:35 -0600127
Simon Glassbcf2b722023-08-14 16:40:36 -0600128/* Check the cedit write_env and read_env commands */
Simon Glassfc9c0e02023-08-14 16:40:35 -0600129static int cedit_env(struct unit_test_state *uts)
130{
Simon Glassc2bd2d32023-10-01 19:13:39 -0600131 struct scene_obj_textline *tline;
Simon Glassfc9c0e02023-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 Glassc2bd2d32023-10-01 19:13:39 -0600136 char *str;
Simon Glassfc9c0e02023-08-14 16:40:35 -0600137
138 console_record_reset_enable();
139 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
140
141 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn));
142
143 /* get a menu to fiddle with */
144 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
145 ut_assertnonnull(menu);
146 menu->cur_item_id = ID_CPU_SPEED_2;
147
Simon Glassc2bd2d32023-10-01 19:13:39 -0600148 /* get a textline to fiddle with too */
149 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
150 ut_assertnonnull(tline);
151 str = abuf_data(&tline->buf);
152 strcpy(str, "my-machine");
153
Simon Glassfc9c0e02023-08-14 16:40:35 -0600154 ut_assertok(run_command("cedit write_env -v", 0));
155 ut_assert_nextlinen("c.cpu-speed=7");
156 ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz");
157 ut_assert_nextlinen("c.power-loss=10");
158 ut_assert_nextlinen("c.power-loss-str=Always Off");
Simon Glassc2bd2d32023-10-01 19:13:39 -0600159 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glassfc9c0e02023-08-14 16:40:35 -0600160 ut_assert_console_end();
161
162 ut_asserteq(7, env_get_ulong("c.cpu-speed", 10, 0));
163 ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str"));
Simon Glassc2bd2d32023-10-01 19:13:39 -0600164 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glassfc9c0e02023-08-14 16:40:35 -0600165
Simon Glassbcf2b722023-08-14 16:40:36 -0600166 /* reset the expo */
167 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glassc2bd2d32023-10-01 19:13:39 -0600168 *str = '\0';
Simon Glassbcf2b722023-08-14 16:40:36 -0600169
170 ut_assertok(run_command("cedit read_env -v", 0));
171 ut_assert_nextlinen("c.cpu-speed=7");
172 ut_assert_nextlinen("c.power-loss=10");
Simon Glassc2bd2d32023-10-01 19:13:39 -0600173 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glassbcf2b722023-08-14 16:40:36 -0600174 ut_assert_console_end();
175
176 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glassc2bd2d32023-10-01 19:13:39 -0600177 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glassbcf2b722023-08-14 16:40:36 -0600178
Simon Glassfc9c0e02023-08-14 16:40:35 -0600179 return 0;
180}
181BOOTSTD_TEST(cedit_env, 0);
Simon Glasseb6c71b2023-08-14 16:40:37 -0600182
183/* Check the cedit write_cmos and read_cmos commands */
184static int cedit_cmos(struct unit_test_state *uts)
185{
186 struct scene_obj_menu *menu, *menu2;
187 struct video_priv *vid_priv;
188 extern struct expo *cur_exp;
189 struct scene *scn;
190
191 console_record_reset_enable();
192 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 Glasscfc402d2023-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 Glasseb6c71b2023-08-14 16:40:37 -0600220 return 0;
221}
222BOOTSTD_TEST(cedit_cmos, 0);