1 // SPDX-License-Identifier: GPL-2.0+
3 * Implementation of configuration editor
5 * Copyright 2023 Google LLC
9 #define LOG_CATEGORY LOGC_EXPO
22 #include <linux/delay.h>
23 #include "scene_internal.h"
27 CMOS_MAX_BYTES = CMOS_MAX_BITS / 8,
30 #define CMOS_BYTE(bit) ((bit) / 8)
31 #define CMOS_BIT(bit) ((bit) % 8)
34 * struct cedit_iter_priv - private data for cedit operations
36 * @buf: Buffer to use when writing settings to the devicetree
37 * @node: Node to read from when reading settings from devicetree
38 * @verbose: true to show writing to environment variables
39 * @mask: Mask bits for the CMOS RAM. If a bit is set the byte containing it
41 * @value: Value bits for CMOS RAM. This is the actual value written
42 * @dev: RTC device to write to
44 struct cedit_iter_priv {
53 int cedit_arange(struct expo *exp, struct video_priv *vpriv, uint scene_id)
55 struct scene_obj_txt *txt;
56 struct scene_obj *obj;
60 scn = expo_lookup_scene_id(exp, scene_id);
62 return log_msg_ret("scn", -ENOENT);
64 txt = scene_obj_find_by_name(scn, "prompt");
66 scene_obj_set_pos(scn, txt->obj.id, 0, vpriv->ysize - 50);
68 txt = scene_obj_find_by_name(scn, "title");
70 scene_obj_set_pos(scn, txt->obj.id, 200, 10);
73 list_for_each_entry(obj, &scn->obj_head, sibling) {
74 if (obj->type == SCENEOBJT_MENU) {
75 scene_obj_set_pos(scn, obj->id, 50, y);
76 scene_menu_arrange(scn, (struct scene_obj_menu *)obj);
84 int cedit_prepare(struct expo *exp, struct video_priv **vid_privp,
87 struct video_priv *vid_priv;
93 /* For now we only support a video console */
94 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
96 return log_msg_ret("vid", ret);
97 ret = expo_set_display(exp, dev);
99 return log_msg_ret("dis", ret);
101 ret = expo_first_scene_id(exp);
103 return log_msg_ret("scn", ret);
106 ret = expo_set_scene_id(exp, scene_id);
108 return log_msg_ret("sid", ret);
112 /* This is not supported for now */
114 expo_set_text_mode(exp, true);
116 vid_priv = dev_get_uclass_priv(dev);
118 scn = expo_lookup_scene_id(exp, scene_id);
119 scene_highlight_first(scn);
121 cedit_arange(exp, vid_priv, scene_id);
123 ret = expo_calc_dims(exp);
125 return log_msg_ret("dim", ret);
127 *vid_privp = vid_priv;
133 int cedit_run(struct expo *exp)
135 struct cli_ch_state s_cch, *cch = &s_cch;
136 struct video_priv *vid_priv;
143 ret = cedit_prepare(exp, &vid_priv, &scn);
145 return log_msg_ret("prep", ret);
150 struct expo_action act;
153 ret = expo_render(exp);
157 ichar = cli_ch_process(cch, 0);
159 while (!ichar && !tstc()) {
162 ichar = cli_ch_process(cch, -ETIMEDOUT);
166 ichar = cli_ch_process(cch, ichar);
172 key = bootmenu_conv_key(ichar);
173 if (key == BKEY_NONE)
179 ret = expo_send_key(exp, key);
183 ret = expo_action_get(exp, &act);
186 case EXPOACT_POINT_OBJ:
187 scene_set_highlight_id(scn, act.select.id);
188 cedit_arange(exp, vid_priv, scene_id);
191 scene_set_open(scn, act.select.id, true);
192 cedit_arange(exp, vid_priv, scene_id);
195 scene_set_open(scn, act.select.id, false);
196 cedit_arange(exp, vid_priv, scene_id);
199 scene_set_open(scn, scn->highlight_id, false);
200 cedit_arange(exp, vid_priv, scene_id);
203 log_debug("quitting\n");
213 return log_msg_ret("end", ret);
218 static int check_space(int ret, struct abuf *buf)
220 if (ret == -FDT_ERR_NOSPACE) {
221 if (!abuf_realloc_inc(buf, CEDIT_SIZE_INC))
222 return log_msg_ret("spc", -ENOMEM);
223 ret = fdt_resize(abuf_data(buf), abuf_data(buf),
226 return log_msg_ret("res", -EFAULT);
233 * get_cur_menuitem_text() - Get the text of the currently selected item
235 * Looks up the object for the current item, finds text object for it and looks
236 * up the string for that text
238 * @menu: Menu to look at
239 * @strp: Returns a pointer to the next
240 * Return: 0 if OK, -ENOENT if something was not found
242 static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
245 struct scene *scn = menu->obj.scene;
246 const struct scene_menitem *mi;
247 const struct scene_obj_txt *txt;
250 mi = scene_menuitem_find(menu, menu->cur_item_id);
252 return log_msg_ret("mi", -ENOENT);
254 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
256 return log_msg_ret("txt", -ENOENT);
258 str = expo_get_str(scn->expo, txt->str_id);
260 return log_msg_ret("str", -ENOENT);
266 static int h_write_settings(struct scene_obj *obj, void *vpriv)
268 struct cedit_iter_priv *priv = vpriv;
269 struct abuf *buf = priv->buf;
273 case SCENEOBJT_IMAGE:
276 case SCENEOBJT_MENU: {
277 const struct scene_obj_menu *menu;
282 menu = (struct scene_obj_menu *)obj;
284 for (i = 0; ret && i < 2; i++) {
285 ret = fdt_property_u32(abuf_data(buf), obj->name,
288 ret = check_space(ret, buf);
290 return log_msg_ret("res", -ENOMEM);
293 /* this should not happen */
295 return log_msg_ret("wrt", -EFAULT);
297 ret = get_cur_menuitem_text(menu, &str);
299 return log_msg_ret("mis", ret);
301 snprintf(name, sizeof(name), "%s-str", obj->name);
303 for (i = 0; ret && i < 2; i++) {
304 ret = fdt_property_string(abuf_data(buf), name, str);
306 ret = check_space(ret, buf);
308 return log_msg_ret("rs2", -ENOMEM);
312 /* this should not happen */
314 return log_msg_ret("wr2", -EFAULT);
323 int cedit_write_settings(struct expo *exp, struct abuf *buf)
325 struct cedit_iter_priv priv;
330 if (!abuf_realloc(buf, CEDIT_SIZE_INC))
331 return log_msg_ret("buf", -ENOMEM);
333 fdt = abuf_data(buf);
334 ret = fdt_create(fdt, abuf_size(buf));
336 ret = fdt_finish_reservemap(fdt);
338 ret = fdt_begin_node(fdt, "");
340 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
342 log_debug("Failed to start FDT (err=%d)\n", ret);
343 return log_msg_ret("sta", -EINVAL);
346 /* write out the items */
348 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
350 log_debug("Failed to write settings (err=%d)\n", ret);
351 return log_msg_ret("set", ret);
354 ret = fdt_end_node(fdt);
356 ret = fdt_end_node(fdt);
358 ret = fdt_finish(fdt);
360 log_debug("Failed to finish FDT (err=%d)\n", ret);
361 return log_msg_ret("fin", -EINVAL);
367 static int h_read_settings(struct scene_obj *obj, void *vpriv)
369 struct cedit_iter_priv *priv = vpriv;
370 ofnode node = priv->node;
374 case SCENEOBJT_IMAGE:
377 case SCENEOBJT_MENU: {
378 struct scene_obj_menu *menu;
381 if (ofnode_read_u32(node, obj->name, &val))
382 return log_msg_ret("rd", -ENOENT);
383 menu = (struct scene_obj_menu *)obj;
384 menu->cur_item_id = val;
393 int cedit_read_settings(struct expo *exp, oftree tree)
395 struct cedit_iter_priv priv;
399 root = oftree_root(tree);
400 if (!ofnode_valid(root))
401 return log_msg_ret("roo", -ENOENT);
402 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
403 if (!ofnode_valid(node))
404 return log_msg_ret("pat", -ENOENT);
406 /* read in the items */
408 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
410 log_debug("Failed to read settings (err=%d)\n", ret);
411 return log_msg_ret("set", ret);
417 static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
419 const struct scene_obj_menu *menu;
420 struct cedit_iter_priv *priv = vpriv;
421 char name[80], var[60];
425 if (obj->type != SCENEOBJT_MENU)
428 menu = (struct scene_obj_menu *)obj;
429 val = menu->cur_item_id;
430 snprintf(var, sizeof(var), "c.%s", obj->name);
433 printf("%s=%d\n", var, val);
435 ret = env_set_ulong(var, val);
437 return log_msg_ret("set", ret);
439 ret = get_cur_menuitem_text(menu, &str);
441 return log_msg_ret("mis", ret);
443 snprintf(name, sizeof(name), "c.%s-str", obj->name);
445 printf("%s=%s\n", name, str);
447 ret = env_set(name, str);
449 return log_msg_ret("st2", ret);
454 int cedit_write_settings_env(struct expo *exp, bool verbose)
456 struct cedit_iter_priv priv;
459 /* write out the items */
460 priv.verbose = verbose;
461 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
463 log_debug("Failed to write settings to env (err=%d)\n", ret);
464 return log_msg_ret("set", ret);
470 static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
472 struct cedit_iter_priv *priv = vpriv;
473 struct scene_obj_menu *menu;
477 if (obj->type != SCENEOBJT_MENU)
480 menu = (struct scene_obj_menu *)obj;
481 val = menu->cur_item_id;
482 snprintf(var, sizeof(var), "c.%s", obj->name);
484 val = env_get_ulong(var, 10, 0);
486 printf("%s=%d\n", var, val);
488 return log_msg_ret("get", -ENOENT);
491 * note that no validation is done here, to make sure the ID is valid
492 * and actually points to a menu item
494 menu->cur_item_id = val;
499 int cedit_read_settings_env(struct expo *exp, bool verbose)
501 struct cedit_iter_priv priv;
504 /* write out the items */
505 priv.verbose = verbose;
506 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
508 log_debug("Failed to read settings from env (err=%d)\n", ret);
509 return log_msg_ret("set", ret);
516 * get_cur_menuitem_seq() - Get the sequence number of a menu's current item
518 * Enumerates the items of a menu (0, 1, 2) and returns the sequence number of
519 * the currently selected item. If the first item is selected, this returns 0;
520 * if the second, 1; etc.
522 * @menu: Menu to check
523 * Return: Sequence number on success, else -ve error value
525 static int get_cur_menuitem_seq(const struct scene_obj_menu *menu)
527 const struct scene_menitem *mi;
532 list_for_each_entry(mi, &menu->item_head, sibling) {
533 if (mi->id == menu->cur_item_id) {
541 return log_msg_ret("nf", -ENOENT);
546 static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
548 const struct scene_obj_menu *menu;
549 struct cedit_iter_priv *priv = vpriv;
553 if (obj->type != SCENEOBJT_MENU)
556 menu = (struct scene_obj_menu *)obj;
557 val = menu->cur_item_id;
559 ret = get_cur_menuitem_seq(menu);
561 return log_msg_ret("cur", ret);
563 log_debug("%s: seq=%d\n", menu->obj.name, seq);
565 /* figure out where to place this item */
566 if (!obj->bit_length)
567 return log_msg_ret("len", -EINVAL);
568 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
569 return log_msg_ret("bit", -E2BIG);
571 for (i = 0; i < obj->bit_length; i++, seq >>= 1) {
572 uint bitnum = obj->start_bit + i;
574 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
576 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
577 log_debug("bit %x %x %x\n", bitnum,
578 priv->mask[CMOS_BYTE(bitnum)],
579 priv->value[CMOS_BYTE(bitnum)]);
585 int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
588 struct cedit_iter_priv priv;
589 int ret, i, count, first, last;
591 /* write out the items */
592 priv.mask = calloc(1, CMOS_MAX_BYTES);
594 return log_msg_ret("mas", -ENOMEM);
595 priv.value = calloc(1, CMOS_MAX_BYTES);
598 return log_msg_ret("val", -ENOMEM);
601 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
603 log_debug("Failed to write CMOS (err=%d)\n", ret);
604 ret = log_msg_ret("set", ret);
608 /* write the data to the RTC */
609 first = CMOS_MAX_BYTES;
611 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
613 log_debug("Write byte %x: %x\n", i, priv.value[i]);
614 ret = rtc_write8(dev, i, priv.value[i]);
616 ret = log_msg_ret("wri", ret);
620 first = min(first, i);
625 printf("Write %d bytes from offset %x to %x\n", count, first,
635 static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
637 struct cedit_iter_priv *priv = vpriv;
638 const struct scene_menitem *mi;
639 struct scene_obj_menu *menu;
643 if (obj->type != SCENEOBJT_MENU)
646 menu = (struct scene_obj_menu *)obj;
648 /* figure out where to place this item */
649 if (!obj->bit_length)
650 return log_msg_ret("len", -EINVAL);
651 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
652 return log_msg_ret("bit", -E2BIG);
655 for (i = 0; i < obj->bit_length; i++) {
656 uint bitnum = obj->start_bit + i;
657 uint offset = CMOS_BYTE(bitnum);
659 /* read the byte if not already read */
660 if (!priv->mask[offset]) {
661 ret = rtc_read8(priv->dev, offset);
663 return log_msg_ret("rea", ret);
664 priv->value[offset] = ret;
666 /* mark it as read */
667 priv->mask[offset] = 0xff;
670 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
672 log_debug("bit %x %x\n", bitnum, val);
675 /* update the current item */
676 mi = scene_menuitem_find_seq(menu, val);
678 return log_msg_ret("seq", -ENOENT);
680 menu->cur_item_id = mi->id;
681 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
686 int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
689 struct cedit_iter_priv priv;
690 int ret, i, count, first, last;
692 /* read in the items */
693 priv.mask = calloc(1, CMOS_MAX_BYTES);
695 return log_msg_ret("mas", -ENOMEM);
696 priv.value = calloc(1, CMOS_MAX_BYTES);
699 return log_msg_ret("val", -ENOMEM);
703 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
705 log_debug("Failed to read CMOS (err=%d)\n", ret);
706 ret = log_msg_ret("set", ret);
710 /* read the data to the RTC */
711 first = CMOS_MAX_BYTES;
713 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
715 log_debug("Read byte %x: %x\n", i, priv.value[i]);
717 first = min(first, i);
722 printf("Read %d bytes from offset %x to %x\n", count, first,