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