Simon Glass | 44e4ec7 | 2023-08-14 16:40:26 -0600 | [diff] [blame] | 1 | // 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 Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 9 | #include <env.h> |
Simon Glass | 44e4ec7 | 2023-08-14 16:40:26 -0600 | [diff] [blame] | 10 | #include <expo.h> |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 11 | #include <mapmem.h> |
| 12 | #include <dm/ofnode.h> |
Simon Glass | 44e4ec7 | 2023-08-14 16:40:26 -0600 | [diff] [blame] | 13 | #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 */ |
| 19 | static 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 | } |
| 56 | BOOTSTD_TEST(cedit_base, 0); |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 57 | |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 58 | /* Check the cedit write_fdt and read_fdt commands */ |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 59 | static int cedit_fdt(struct unit_test_state *uts) |
| 60 | { |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 61 | struct scene_obj_textline *tline; |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 62 | struct video_priv *vid_priv; |
| 63 | extern struct expo *cur_exp; |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 64 | struct scene_obj_menu *menu; |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 65 | ulong addr = 0x1000; |
| 66 | struct ofprop prop; |
| 67 | struct scene *scn; |
| 68 | oftree tree; |
| 69 | ofnode node; |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 70 | char *str; |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 71 | 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 Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 79 | /* 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 84 | /* 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 Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 90 | 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 97 | ut_assert(ofnode_valid(node)); |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 98 | |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 99 | ut_asserteq(ID_CPU_SPEED_2, |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 100 | ofnode_read_u32_default(node, "cpu-speed", 0)); |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 101 | ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str")); |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 102 | ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name")); |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 103 | |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 104 | /* There should only be 5 properties */ |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 105 | for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop); |
| 106 | i++, ofnode_next_property(&prop)) |
| 107 | ; |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 108 | ut_asserteq(5, i); |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 109 | |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 110 | ut_assert_console_end(); |
| 111 | |
| 112 | /* reset the expo */ |
| 113 | menu->cur_item_id = ID_CPU_SPEED_1; |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 114 | *str = '\0'; |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 115 | |
| 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 119 | ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name")); |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 120 | |
| 121 | ut_assertnonnull(menu); |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 122 | ut_assert_console_end(); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | BOOTSTD_TEST(cedit_fdt, 0); |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 127 | |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 128 | /* Check the cedit write_env and read_env commands */ |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 129 | static int cedit_env(struct unit_test_state *uts) |
| 130 | { |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 131 | struct scene_obj_textline *tline; |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 132 | struct video_priv *vid_priv; |
| 133 | extern struct expo *cur_exp; |
| 134 | struct scene_obj_menu *menu; |
| 135 | struct scene *scn; |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 136 | char *str; |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 137 | |
| 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 148 | /* 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 Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 154 | 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 159 | ut_assert_nextlinen("c.machine-name=my-machine"); |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 160 | 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 164 | ut_asserteq_str("my-machine", env_get("c.machine-name")); |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 165 | |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 166 | /* reset the expo */ |
| 167 | menu->cur_item_id = ID_CPU_SPEED_1; |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 168 | *str = '\0'; |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 169 | |
| 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 Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 173 | ut_assert_nextlinen("c.machine-name=my-machine"); |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 174 | ut_assert_console_end(); |
| 175 | |
| 176 | ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id); |
Simon Glass | 70c579b | 2023-10-01 19:13:39 -0600 | [diff] [blame] | 177 | ut_asserteq_str("my-machine", env_get("c.machine-name")); |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 178 | |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | BOOTSTD_TEST(cedit_env, 0); |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 182 | |
| 183 | /* Check the cedit write_cmos and read_cmos commands */ |
| 184 | static 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 Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 209 | /* 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 Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | BOOTSTD_TEST(cedit_cmos, 0); |