1 // SPDX-License-Identifier: GPL-2.0+
3 * Implementation of configuration editor
5 * Copyright 2023 Google LLC
9 #define LOG_CATEGORY LOGC_EXPO
21 #include <linux/delay.h>
22 #include "scene_internal.h"
23 #include <u-boot/schedule.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 expo_arrange_info arr;
56 struct scene_obj_txt *txt;
57 struct scene_obj *obj;
61 scn = expo_lookup_scene_id(exp, scene_id);
63 return log_msg_ret("scn", -ENOENT);
65 txt = scene_obj_find_by_name(scn, "prompt");
67 scene_obj_set_pos(scn, txt->obj.id, 0, vpriv->ysize - 50);
69 txt = scene_obj_find_by_name(scn, "title");
71 scene_obj_set_pos(scn, txt->obj.id, 200, 10);
73 memset(&arr, '\0', sizeof(arr));
74 ret = scene_calc_arrange(scn, &arr);
76 return log_msg_ret("arr", ret);
79 list_for_each_entry(obj, &scn->obj_head, sibling) {
86 scene_obj_set_pos(scn, obj->id, 50, y);
87 scene_menu_arrange(scn, &arr,
88 (struct scene_obj_menu *)obj);
91 case SCENEOBJT_TEXTLINE:
92 scene_obj_set_pos(scn, obj->id, 50, y);
93 scene_textline_arrange(scn, &arr,
94 (struct scene_obj_textline *)obj);
103 int cedit_prepare(struct expo *exp, struct video_priv **vid_privp,
106 struct video_priv *vid_priv;
112 /* For now we only support a video console */
113 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
115 return log_msg_ret("vid", ret);
116 ret = expo_set_display(exp, dev);
118 return log_msg_ret("dis", ret);
120 ret = expo_first_scene_id(exp);
122 return log_msg_ret("scn", ret);
125 ret = expo_set_scene_id(exp, scene_id);
127 return log_msg_ret("sid", ret);
131 /* This is not supported for now */
133 expo_set_text_mode(exp, true);
135 vid_priv = dev_get_uclass_priv(dev);
137 scn = expo_lookup_scene_id(exp, scene_id);
138 scene_highlight_first(scn);
140 cedit_arange(exp, vid_priv, scene_id);
142 ret = expo_calc_dims(exp);
144 return log_msg_ret("dim", ret);
146 *vid_privp = vid_priv;
152 int cedit_run(struct expo *exp)
154 struct cli_ch_state s_cch, *cch = &s_cch;
155 struct video_priv *vid_priv;
162 ret = cedit_prepare(exp, &vid_priv, &scn);
164 return log_msg_ret("prep", ret);
170 struct expo_action act;
173 ret = expo_render(exp);
177 ichar = cli_ch_process(cch, 0);
179 while (!ichar && !tstc()) {
182 ichar = cli_ch_process(cch, -ETIMEDOUT);
186 ichar = cli_ch_process(cch, ichar);
192 key = bootmenu_conv_key(ichar);
193 if (key == BKEY_NONE || key >= BKEY_FIRST_EXTRA)
199 ret = expo_send_key(exp, key);
203 ret = expo_action_get(exp, &act);
206 case EXPOACT_POINT_OBJ:
207 scene_set_highlight_id(scn, act.select.id);
208 cedit_arange(exp, vid_priv, scene_id);
211 scene_set_open(scn, act.select.id, true);
212 cedit_arange(exp, vid_priv, scene_id);
213 switch (scn->highlight_id) {
224 scene_set_open(scn, act.select.id, false);
225 cedit_arange(exp, vid_priv, scene_id);
228 scene_set_open(scn, scn->highlight_id, false);
229 cedit_arange(exp, vid_priv, scene_id);
232 log_debug("quitting\n");
242 return log_msg_ret("end", ret);
249 static int check_space(int ret, struct abuf *buf)
251 if (ret == -FDT_ERR_NOSPACE) {
252 if (!abuf_realloc_inc(buf, CEDIT_SIZE_INC))
253 return log_msg_ret("spc", -ENOMEM);
254 ret = fdt_resize(abuf_data(buf), abuf_data(buf),
257 return log_msg_ret("res", -EFAULT);
264 * get_cur_menuitem_text() - Get the text of the currently selected item
266 * Looks up the object for the current item, finds text object for it and looks
267 * up the string for that text
269 * @menu: Menu to look at
270 * @strp: Returns a pointer to the next
271 * Return: 0 if OK, -ENOENT if something was not found
273 static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
276 struct scene *scn = menu->obj.scene;
277 const struct scene_menitem *mi;
278 const struct scene_obj_txt *txt;
281 mi = scene_menuitem_find(menu, menu->cur_item_id);
283 return log_msg_ret("mi", -ENOENT);
285 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
287 return log_msg_ret("txt", -ENOENT);
289 str = expo_get_str(scn->expo, txt->str_id);
291 return log_msg_ret("str", -ENOENT);
298 * get_cur_menuitem_val() - Get the value of a menu's current item
300 * Obtains the value of the current item in the menu. If no value, then
301 * enumerates the items of a menu (0, 1, 2) and returns the sequence number of
302 * the currently selected item. If the first item is selected, this returns 0;
303 * if the second, 1; etc.
305 * @menu: Menu to check
306 * @valp: Returns current-item value / sequence number
307 * Return: 0 on success, else -ve error value
309 static int get_cur_menuitem_val(const struct scene_obj_menu *menu, int *valp)
311 const struct scene_menitem *mi;
315 list_for_each_entry(mi, &menu->item_head, sibling) {
316 if (mi->id == menu->cur_item_id) {
317 *valp = mi->value == INT_MAX ? seq : mi->value;
323 return log_msg_ret("nf", -ENOENT);
327 * write_dt_string() - Write a string to the devicetree, expanding if needed
329 * If this fails, it tries again after expanding the devicetree a little
331 * @buf: Buffer containing the devicetree
332 * @name: Property name to use
334 * Return: 0 if OK, -EFAULT if something went horribly wrong
336 static int write_dt_string(struct abuf *buf, const char *name, const char *str)
341 for (i = 0; ret && i < 2; i++) {
342 ret = fdt_property_string(abuf_data(buf), name, str);
344 ret = check_space(ret, buf);
346 return log_msg_ret("rs2", -ENOMEM);
350 /* this should not happen */
352 return log_msg_ret("str", -EFAULT);
358 * write_dt_u32() - Write an int to the devicetree, expanding if needed
360 * If this fails, it tries again after expanding the devicetree a little
362 * @buf: Buffer containing the devicetree
363 * @name: Property name to use
364 * @lva: Integer value
365 * Return: 0 if OK, -EFAULT if something went horribly wrong
367 static int write_dt_u32(struct abuf *buf, const char *name, uint val)
371 /* write the text of the current item */
373 for (i = 0; ret && i < 2; i++) {
374 ret = fdt_property_u32(abuf_data(buf), name, val);
376 ret = check_space(ret, buf);
378 return log_msg_ret("rs2", -ENOMEM);
382 /* this should not happen */
384 return log_msg_ret("str", -EFAULT);
389 static int h_write_settings(struct scene_obj *obj, void *vpriv)
391 struct cedit_iter_priv *priv = vpriv;
392 struct abuf *buf = priv->buf;
397 case SCENEOBJT_IMAGE:
400 case SCENEOBJT_TEXTLINE: {
401 const struct scene_obj_textline *tline;
403 tline = (struct scene_obj_textline *)obj;
404 ret = write_dt_string(buf, obj->name, abuf_data(&tline->buf));
406 return log_msg_ret("wr2", ret);
409 case SCENEOBJT_MENU: {
410 const struct scene_obj_menu *menu;
415 /* write the ID of the current item */
416 menu = (struct scene_obj_menu *)obj;
417 ret = write_dt_u32(buf, obj->name, menu->cur_item_id);
419 return log_msg_ret("wrt", ret);
421 snprintf(name, sizeof(name), "%s-value", obj->name);
422 ret = get_cur_menuitem_val(menu, &val);
424 return log_msg_ret("cur", ret);
425 ret = write_dt_u32(buf, name, val);
427 return log_msg_ret("wr2", ret);
429 ret = get_cur_menuitem_text(menu, &str);
431 return log_msg_ret("mis", ret);
433 /* write the text of the current item */
434 snprintf(name, sizeof(name), "%s-str", obj->name);
435 ret = write_dt_string(buf, name, str);
437 return log_msg_ret("wr2", ret);
446 int cedit_write_settings(struct expo *exp, struct abuf *buf)
448 struct cedit_iter_priv priv;
453 if (!abuf_realloc(buf, CEDIT_SIZE_INC))
454 return log_msg_ret("buf", -ENOMEM);
456 fdt = abuf_data(buf);
457 ret = fdt_create(fdt, abuf_size(buf));
459 ret = fdt_finish_reservemap(fdt);
461 ret = fdt_begin_node(fdt, "");
463 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
465 log_debug("Failed to start FDT (err=%d)\n", ret);
466 return log_msg_ret("sta", -EINVAL);
469 /* write out the items */
471 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
473 log_debug("Failed to write settings (err=%d)\n", ret);
474 return log_msg_ret("set", ret);
477 ret = fdt_end_node(fdt);
479 ret = fdt_end_node(fdt);
481 ret = fdt_finish(fdt);
483 log_debug("Failed to finish FDT (err=%d)\n", ret);
484 return log_msg_ret("fin", -EINVAL);
490 static int h_read_settings(struct scene_obj *obj, void *vpriv)
492 struct cedit_iter_priv *priv = vpriv;
493 ofnode node = priv->node;
497 case SCENEOBJT_IMAGE:
500 case SCENEOBJT_TEXTLINE: {
501 const struct scene_obj_textline *tline;
505 tline = (struct scene_obj_textline *)obj;
507 val = ofnode_read_prop(node, obj->name, &len);
508 if (len >= tline->max_chars)
509 return log_msg_ret("str", -ENOSPC);
510 strcpy(abuf_data(&tline->buf), val);
513 case SCENEOBJT_MENU: {
514 struct scene_obj_menu *menu;
517 if (ofnode_read_u32(node, obj->name, &val))
518 return log_msg_ret("rd", -ENOENT);
519 menu = (struct scene_obj_menu *)obj;
520 menu->cur_item_id = val;
529 int cedit_read_settings(struct expo *exp, oftree tree)
531 struct cedit_iter_priv priv;
535 root = oftree_root(tree);
536 if (!ofnode_valid(root))
537 return log_msg_ret("roo", -ENOENT);
538 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
539 if (!ofnode_valid(node))
540 return log_msg_ret("pat", -ENOENT);
542 /* read in the items */
544 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
546 log_debug("Failed to read settings (err=%d)\n", ret);
547 return log_msg_ret("set", ret);
553 static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
555 const struct scene_obj_menu *menu;
556 struct cedit_iter_priv *priv = vpriv;
557 char name[80], var[60];
561 if (obj->id < EXPOID_BASE_ID)
564 snprintf(var, sizeof(var), "c.%s", obj->name);
568 case SCENEOBJT_IMAGE:
572 menu = (struct scene_obj_menu *)obj;
573 val = menu->cur_item_id;
576 printf("%s=%d\n", var, val);
578 ret = env_set_ulong(var, val);
580 return log_msg_ret("set", ret);
582 ret = get_cur_menuitem_text(menu, &str);
584 return log_msg_ret("mis", ret);
586 snprintf(name, sizeof(name), "c.%s-str", obj->name);
588 printf("%s=%s\n", name, str);
590 ret = env_set(name, str);
592 return log_msg_ret("st2", ret);
594 ret = get_cur_menuitem_val(menu, &val);
596 return log_msg_ret("cur", ret);
597 snprintf(name, sizeof(name), "c.%s-value", obj->name);
599 printf("%s=%d\n", name, val);
602 case SCENEOBJT_TEXTLINE: {
603 const struct scene_obj_textline *tline;
605 tline = (struct scene_obj_textline *)obj;
606 str = abuf_data(&tline->buf);
607 ret = env_set(var, str);
609 return log_msg_ret("set", ret);
612 printf("%s=%s\n", var, str);
621 int cedit_write_settings_env(struct expo *exp, bool verbose)
623 struct cedit_iter_priv priv;
626 /* write out the items */
627 priv.verbose = verbose;
628 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
630 log_debug("Failed to write settings to env (err=%d)\n", ret);
631 return log_msg_ret("set", ret);
637 static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
639 struct cedit_iter_priv *priv = vpriv;
640 struct scene_obj_menu *menu;
644 if (obj->id < EXPOID_BASE_ID)
647 snprintf(var, sizeof(var), "c.%s", obj->name);
651 case SCENEOBJT_IMAGE:
655 menu = (struct scene_obj_menu *)obj;
656 val = env_get_ulong(var, 10, 0);
658 printf("%s=%d\n", var, val);
660 return log_msg_ret("get", -ENOENT);
663 * note that no validation is done here, to make sure the ID is
664 * valid and actually points to a menu item
666 menu->cur_item_id = val;
668 case SCENEOBJT_TEXTLINE: {
669 const struct scene_obj_textline *tline;
672 tline = (struct scene_obj_textline *)obj;
673 value = env_get(var);
674 if (value && strlen(value) >= tline->max_chars)
675 return log_msg_ret("str", -ENOSPC);
679 printf("%s=%s\n", var, value);
680 strcpy(abuf_data(&tline->buf), value);
688 int cedit_read_settings_env(struct expo *exp, bool verbose)
690 struct cedit_iter_priv priv;
693 /* write out the items */
694 priv.verbose = verbose;
695 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
697 log_debug("Failed to read settings from env (err=%d)\n", ret);
698 return log_msg_ret("set", ret);
704 static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
706 const struct scene_obj_menu *menu;
707 struct cedit_iter_priv *priv = vpriv;
711 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
714 menu = (struct scene_obj_menu *)obj;
715 val = menu->cur_item_id;
717 ret = get_cur_menuitem_val(menu, &val);
719 return log_msg_ret("cur", ret);
720 log_debug("%s: val=%d\n", menu->obj.name, val);
722 /* figure out where to place this item */
723 if (!obj->bit_length)
724 return log_msg_ret("len", -EINVAL);
725 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
726 return log_msg_ret("bit", -E2BIG);
728 for (i = 0; i < obj->bit_length; i++, val >>= 1) {
729 uint bitnum = obj->start_bit + i;
731 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
733 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
734 log_debug("bit %x %x %x\n", bitnum,
735 priv->mask[CMOS_BYTE(bitnum)],
736 priv->value[CMOS_BYTE(bitnum)]);
742 int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
745 struct cedit_iter_priv priv;
746 int ret, i, count, first, last;
748 /* write out the items */
749 priv.mask = calloc(1, CMOS_MAX_BYTES);
751 return log_msg_ret("mas", -ENOMEM);
752 priv.value = calloc(1, CMOS_MAX_BYTES);
755 return log_msg_ret("val", -ENOMEM);
758 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
760 log_debug("Failed to write CMOS (err=%d)\n", ret);
761 ret = log_msg_ret("set", ret);
765 /* write the data to the RTC */
766 log_debug("Writing CMOS\n");
767 first = CMOS_MAX_BYTES;
769 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
771 log_debug("Write byte %x: %x\n", i, priv.value[i]);
772 ret = rtc_write8(dev, i, priv.value[i]);
774 ret = log_msg_ret("wri", ret);
778 first = min(first, i);
783 printf("Write %d bytes from offset %x to %x\n", count, first,
793 static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
795 struct cedit_iter_priv *priv = vpriv;
796 const struct scene_menitem *mi;
797 struct scene_obj_menu *menu;
801 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
804 menu = (struct scene_obj_menu *)obj;
806 /* figure out where to place this item */
807 if (!obj->bit_length)
808 return log_msg_ret("len", -EINVAL);
809 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
810 return log_msg_ret("bit", -E2BIG);
813 for (i = 0; i < obj->bit_length; i++) {
814 uint bitnum = obj->start_bit + i;
815 uint offset = CMOS_BYTE(bitnum);
817 /* read the byte if not already read */
818 if (!priv->mask[offset]) {
819 ret = rtc_read8(priv->dev, offset);
821 return log_msg_ret("rea", ret);
822 priv->value[offset] = ret;
824 /* mark it as read */
825 priv->mask[offset] = 0xff;
828 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
830 log_debug("bit %x %x\n", bitnum, val);
833 /* update the current item */
834 log_debug("look for menuitem value %d in menu %d\n", val, menu->obj.id);
835 mi = scene_menuitem_find_val(menu, val);
837 return log_msg_ret("seq", -ENOENT);
839 menu->cur_item_id = mi->id;
840 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
845 int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
848 struct cedit_iter_priv priv;
849 int ret, i, count, first, last;
851 /* read in the items */
852 priv.mask = calloc(1, CMOS_MAX_BYTES);
854 return log_msg_ret("mas", -ENOMEM);
855 priv.value = calloc(1, CMOS_MAX_BYTES);
858 return log_msg_ret("val", -ENOMEM);
862 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
864 log_debug("Failed to read CMOS (err=%d)\n", ret);
865 ret = log_msg_ret("set", ret);
869 /* indicate what bytes were read from the RTC */
870 first = CMOS_MAX_BYTES;
872 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
874 log_debug("Read byte %x: %x\n", i, priv.value[i]);
876 first = min(first, i);
881 printf("Read %d bytes from offset %x to %x\n", count, first,