blob: ed499f11140c351e076fcfdea3f7f3997bc89479 [file] [log] [blame]
Simon Glassc8925112023-06-01 10:23:02 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Implementation of configuration editor
4 *
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
Simon Glass6a5af5f2023-08-14 16:40:27 -06009#define LOG_CATEGORY LOGC_EXPO
10
Simon Glass28bf4352023-08-14 16:40:33 -060011#include <abuf.h>
Simon Glass8df4a4e2023-08-14 16:40:25 -060012#include <cedit.h>
Simon Glassc8925112023-06-01 10:23:02 -060013#include <cli.h>
14#include <dm.h>
Simon Glass237f3752023-08-14 16:40:35 -060015#include <env.h>
Simon Glassc8925112023-06-01 10:23:02 -060016#include <expo.h>
Simon Glass2b91ca62023-08-14 16:40:37 -060017#include <malloc.h>
Simon Glassc8925112023-06-01 10:23:02 -060018#include <menu.h>
Simon Glass2b91ca62023-08-14 16:40:37 -060019#include <rtc.h>
Simon Glassc8925112023-06-01 10:23:02 -060020#include <video.h>
21#include <linux/delay.h>
22#include "scene_internal.h"
Rasmus Villemoesf741c232024-10-03 23:28:01 +020023#include <u-boot/schedule.h>
Simon Glassc8925112023-06-01 10:23:02 -060024
Simon Glass2b91ca62023-08-14 16:40:37 -060025enum {
26 CMOS_MAX_BITS = 2048,
27 CMOS_MAX_BYTES = CMOS_MAX_BITS / 8,
28};
29
30#define CMOS_BYTE(bit) ((bit) / 8)
31#define CMOS_BIT(bit) ((bit) % 8)
32
Simon Glass28bf4352023-08-14 16:40:33 -060033/**
34 * struct cedit_iter_priv - private data for cedit operations
35 *
36 * @buf: Buffer to use when writing settings to the devicetree
Simon Glassb1cd32b2023-08-14 16:40:34 -060037 * @node: Node to read from when reading settings from devicetree
Simon Glass237f3752023-08-14 16:40:35 -060038 * @verbose: true to show writing to environment variables
Simon Glass2b91ca62023-08-14 16:40:37 -060039 * @mask: Mask bits for the CMOS RAM. If a bit is set the byte containing it
40 * will be written
41 * @value: Value bits for CMOS RAM. This is the actual value written
Simon Glass4462fa32023-08-14 16:40:38 -060042 * @dev: RTC device to write to
Simon Glass28bf4352023-08-14 16:40:33 -060043 */
44struct cedit_iter_priv {
45 struct abuf *buf;
Simon Glassb1cd32b2023-08-14 16:40:34 -060046 ofnode node;
Simon Glass237f3752023-08-14 16:40:35 -060047 bool verbose;
Simon Glass2b91ca62023-08-14 16:40:37 -060048 u8 *mask;
49 u8 *value;
Simon Glass4462fa32023-08-14 16:40:38 -060050 struct udevice *dev;
Simon Glass28bf4352023-08-14 16:40:33 -060051};
52
Simon Glassc8925112023-06-01 10:23:02 -060053int cedit_arange(struct expo *exp, struct video_priv *vpriv, uint scene_id)
54{
Simon Glass377f18e2024-10-14 16:31:55 -060055 struct expo_arrange_info arr;
Simon Glassc8925112023-06-01 10:23:02 -060056 struct scene_obj_txt *txt;
57 struct scene_obj *obj;
58 struct scene *scn;
Simon Glass377f18e2024-10-14 16:31:55 -060059 int y, ret;
Simon Glassc8925112023-06-01 10:23:02 -060060
61 scn = expo_lookup_scene_id(exp, scene_id);
62 if (!scn)
63 return log_msg_ret("scn", -ENOENT);
64
65 txt = scene_obj_find_by_name(scn, "prompt");
66 if (txt)
67 scene_obj_set_pos(scn, txt->obj.id, 0, vpriv->ysize - 50);
68
69 txt = scene_obj_find_by_name(scn, "title");
70 if (txt)
71 scene_obj_set_pos(scn, txt->obj.id, 200, 10);
72
Simon Glass377f18e2024-10-14 16:31:55 -060073 memset(&arr, '\0', sizeof(arr));
74 ret = scene_calc_arrange(scn, &arr);
75 if (ret < 0)
76 return log_msg_ret("arr", ret);
77
Simon Glassc8925112023-06-01 10:23:02 -060078 y = 100;
79 list_for_each_entry(obj, &scn->obj_head, sibling) {
Simon Glassb7a64532023-10-01 19:13:24 -060080 switch (obj->type) {
81 case SCENEOBJT_NONE:
82 case SCENEOBJT_IMAGE:
83 case SCENEOBJT_TEXT:
84 break;
85 case SCENEOBJT_MENU:
Simon Glassc8925112023-06-01 10:23:02 -060086 scene_obj_set_pos(scn, obj->id, 50, y);
Simon Glass377f18e2024-10-14 16:31:55 -060087 scene_menu_arrange(scn, &arr,
88 (struct scene_obj_menu *)obj);
Simon Glassc8925112023-06-01 10:23:02 -060089 y += 50;
Simon Glassb7a64532023-10-01 19:13:24 -060090 break;
Simon Glass23c3eb42023-10-01 19:13:37 -060091 case SCENEOBJT_TEXTLINE:
92 scene_obj_set_pos(scn, obj->id, 50, y);
Simon Glass377f18e2024-10-14 16:31:55 -060093 scene_textline_arrange(scn, &arr,
Simon Glass23c3eb42023-10-01 19:13:37 -060094 (struct scene_obj_textline *)obj);
95 y += 50;
96 break;
Simon Glassc8925112023-06-01 10:23:02 -060097 }
98 }
99
100 return 0;
101}
102
Simon Glass6a5af5f2023-08-14 16:40:27 -0600103int cedit_prepare(struct expo *exp, struct video_priv **vid_privp,
104 struct scene **scnp)
Simon Glassc8925112023-06-01 10:23:02 -0600105{
Simon Glassc8925112023-06-01 10:23:02 -0600106 struct video_priv *vid_priv;
Simon Glassc8925112023-06-01 10:23:02 -0600107 struct udevice *dev;
108 struct scene *scn;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600109 uint scene_id;
Simon Glassc8925112023-06-01 10:23:02 -0600110 int ret;
111
Simon Glassc8925112023-06-01 10:23:02 -0600112 /* For now we only support a video console */
113 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
114 if (ret)
115 return log_msg_ret("vid", ret);
116 ret = expo_set_display(exp, dev);
117 if (ret)
118 return log_msg_ret("dis", ret);
119
120 ret = expo_first_scene_id(exp);
121 if (ret < 0)
122 return log_msg_ret("scn", ret);
123 scene_id = ret;
124
125 ret = expo_set_scene_id(exp, scene_id);
126 if (ret)
127 return log_msg_ret("sid", ret);
128
129 exp->popup = true;
130
131 /* This is not supported for now */
132 if (0)
133 expo_set_text_mode(exp, true);
134
135 vid_priv = dev_get_uclass_priv(dev);
136
137 scn = expo_lookup_scene_id(exp, scene_id);
138 scene_highlight_first(scn);
139
140 cedit_arange(exp, vid_priv, scene_id);
141
142 ret = expo_calc_dims(exp);
143 if (ret)
144 return log_msg_ret("dim", ret);
145
Simon Glass6a5af5f2023-08-14 16:40:27 -0600146 *vid_privp = vid_priv;
147 *scnp = scn;
148
149 return scene_id;
150}
151
152int cedit_run(struct expo *exp)
153{
Simon Glass6a5af5f2023-08-14 16:40:27 -0600154 struct video_priv *vid_priv;
155 uint scene_id;
156 struct scene *scn;
Simon Glass53a0a2f2024-10-14 16:31:57 -0600157 bool done, save;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600158 int ret;
159
Simon Glass6a5af5f2023-08-14 16:40:27 -0600160 ret = cedit_prepare(exp, &vid_priv, &scn);
161 if (ret < 0)
162 return log_msg_ret("prep", ret);
163 scene_id = ret;
164
Simon Glassc8925112023-06-01 10:23:02 -0600165 done = false;
Simon Glass53a0a2f2024-10-14 16:31:57 -0600166 save = false;
Simon Glassc8925112023-06-01 10:23:02 -0600167 do {
168 struct expo_action act;
169 int ichar, key;
170
171 ret = expo_render(exp);
172 if (ret)
173 break;
174
Simon Glass683d8832025-05-02 08:46:15 -0600175 ichar = cli_ch_process(&exp->cch, 0);
Simon Glassc8925112023-06-01 10:23:02 -0600176 if (!ichar) {
177 while (!ichar && !tstc()) {
178 schedule();
179 mdelay(2);
Simon Glass683d8832025-05-02 08:46:15 -0600180 ichar = cli_ch_process(&exp->cch, -ETIMEDOUT);
Simon Glassc8925112023-06-01 10:23:02 -0600181 }
182 if (!ichar) {
183 ichar = getchar();
Simon Glass683d8832025-05-02 08:46:15 -0600184 ichar = cli_ch_process(&exp->cch, ichar);
Simon Glassc8925112023-06-01 10:23:02 -0600185 }
186 }
187
188 key = 0;
189 if (ichar) {
190 key = bootmenu_conv_key(ichar);
Simon Glasseb8af2c2023-10-01 19:13:36 -0600191 if (key == BKEY_NONE || key >= BKEY_FIRST_EXTRA)
Simon Glassc8925112023-06-01 10:23:02 -0600192 key = ichar;
193 }
194 if (!key)
195 continue;
196
197 ret = expo_send_key(exp, key);
198 if (ret)
199 break;
200
201 ret = expo_action_get(exp, &act);
202 if (!ret) {
203 switch (act.type) {
204 case EXPOACT_POINT_OBJ:
205 scene_set_highlight_id(scn, act.select.id);
206 cedit_arange(exp, vid_priv, scene_id);
207 break;
208 case EXPOACT_OPEN:
209 scene_set_open(scn, act.select.id, true);
210 cedit_arange(exp, vid_priv, scene_id);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600211 switch (scn->highlight_id) {
212 case EXPOID_SAVE:
213 done = true;
214 save = true;
215 break;
216 case EXPOID_DISCARD:
217 done = true;
218 break;
219 }
Simon Glassc8925112023-06-01 10:23:02 -0600220 break;
221 case EXPOACT_CLOSE:
222 scene_set_open(scn, act.select.id, false);
223 cedit_arange(exp, vid_priv, scene_id);
224 break;
225 case EXPOACT_SELECT:
226 scene_set_open(scn, scn->highlight_id, false);
227 cedit_arange(exp, vid_priv, scene_id);
228 break;
229 case EXPOACT_QUIT:
230 log_debug("quitting\n");
231 done = true;
232 break;
233 default:
234 break;
235 }
236 }
237 } while (!done);
238
239 if (ret)
240 return log_msg_ret("end", ret);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600241 if (!save)
242 return -EACCES;
Simon Glassc8925112023-06-01 10:23:02 -0600243
244 return 0;
245}
Simon Glass28bf4352023-08-14 16:40:33 -0600246
247static int check_space(int ret, struct abuf *buf)
248{
249 if (ret == -FDT_ERR_NOSPACE) {
250 if (!abuf_realloc_inc(buf, CEDIT_SIZE_INC))
251 return log_msg_ret("spc", -ENOMEM);
252 ret = fdt_resize(abuf_data(buf), abuf_data(buf),
253 abuf_size(buf));
254 if (ret)
255 return log_msg_ret("res", -EFAULT);
256 }
257
258 return 0;
259}
260
Simon Glassdb6a0512023-10-01 19:13:23 -0600261/**
262 * get_cur_menuitem_text() - Get the text of the currently selected item
263 *
264 * Looks up the object for the current item, finds text object for it and looks
265 * up the string for that text
266 *
267 * @menu: Menu to look at
268 * @strp: Returns a pointer to the next
269 * Return: 0 if OK, -ENOENT if something was not found
270 */
Simon Glass237f3752023-08-14 16:40:35 -0600271static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
272 const char **strp)
273{
274 struct scene *scn = menu->obj.scene;
275 const struct scene_menitem *mi;
276 const struct scene_obj_txt *txt;
277 const char *str;
278
279 mi = scene_menuitem_find(menu, menu->cur_item_id);
280 if (!mi)
281 return log_msg_ret("mi", -ENOENT);
282
283 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
284 if (!txt)
285 return log_msg_ret("txt", -ENOENT);
286
287 str = expo_get_str(scn->expo, txt->str_id);
288 if (!str)
289 return log_msg_ret("str", -ENOENT);
290 *strp = str;
291
292 return 0;
293}
294
Simon Glass6f3e87a2024-10-14 16:32:00 -0600295/**
296 * get_cur_menuitem_val() - Get the value of a menu's current item
297 *
298 * Obtains the value of the current item in the menu. If no value, then
299 * enumerates the items of a menu (0, 1, 2) and returns the sequence number of
300 * the currently selected item. If the first item is selected, this returns 0;
301 * if the second, 1; etc.
302 *
303 * @menu: Menu to check
304 * @valp: Returns current-item value / sequence number
305 * Return: 0 on success, else -ve error value
306 */
307static int get_cur_menuitem_val(const struct scene_obj_menu *menu, int *valp)
308{
309 const struct scene_menitem *mi;
310 int seq;
311
312 seq = 0;
313 list_for_each_entry(mi, &menu->item_head, sibling) {
314 if (mi->id == menu->cur_item_id) {
315 *valp = mi->value == INT_MAX ? seq : mi->value;
316 return 0;
317 }
318 seq++;
319 }
320
321 return log_msg_ret("nf", -ENOENT);
322}
323
324/**
325 * write_dt_string() - Write a string to the devicetree, expanding if needed
326 *
327 * If this fails, it tries again after expanding the devicetree a little
328 *
329 * @buf: Buffer containing the devicetree
330 * @name: Property name to use
331 * @str: String value
332 * Return: 0 if OK, -EFAULT if something went horribly wrong
333 */
Simon Glassf78388b2023-10-01 19:13:28 -0600334static int write_dt_string(struct abuf *buf, const char *name, const char *str)
335{
336 int ret, i;
337
Simon Glassf78388b2023-10-01 19:13:28 -0600338 ret = -EAGAIN;
339 for (i = 0; ret && i < 2; i++) {
340 ret = fdt_property_string(abuf_data(buf), name, str);
341 if (!i) {
342 ret = check_space(ret, buf);
343 if (ret)
344 return log_msg_ret("rs2", -ENOMEM);
345 }
346 }
347
348 /* this should not happen */
349 if (ret)
350 return log_msg_ret("str", -EFAULT);
351
352 return 0;
353}
354
Simon Glass6f3e87a2024-10-14 16:32:00 -0600355/**
356 * write_dt_u32() - Write an int to the devicetree, expanding if needed
357 *
358 * If this fails, it tries again after expanding the devicetree a little
359 *
360 * @buf: Buffer containing the devicetree
361 * @name: Property name to use
362 * @lva: Integer value
363 * Return: 0 if OK, -EFAULT if something went horribly wrong
364 */
365static int write_dt_u32(struct abuf *buf, const char *name, uint val)
366{
367 int ret, i;
368
369 /* write the text of the current item */
370 ret = -EAGAIN;
371 for (i = 0; ret && i < 2; i++) {
372 ret = fdt_property_u32(abuf_data(buf), name, val);
373 if (!i) {
374 ret = check_space(ret, buf);
375 if (ret)
376 return log_msg_ret("rs2", -ENOMEM);
377 }
378 }
379
380 /* this should not happen */
381 if (ret)
382 return log_msg_ret("str", -EFAULT);
383
384 return 0;
385}
386
Simon Glass28bf4352023-08-14 16:40:33 -0600387static int h_write_settings(struct scene_obj *obj, void *vpriv)
388{
389 struct cedit_iter_priv *priv = vpriv;
390 struct abuf *buf = priv->buf;
Simon Glass23c3eb42023-10-01 19:13:37 -0600391 int ret;
Simon Glass28bf4352023-08-14 16:40:33 -0600392
393 switch (obj->type) {
394 case SCENEOBJT_NONE:
395 case SCENEOBJT_IMAGE:
396 case SCENEOBJT_TEXT:
397 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600398 case SCENEOBJT_TEXTLINE: {
399 const struct scene_obj_textline *tline;
400
401 tline = (struct scene_obj_textline *)obj;
402 ret = write_dt_string(buf, obj->name, abuf_data(&tline->buf));
403 if (ret)
404 return log_msg_ret("wr2", ret);
405 break;
406 }
Simon Glass28bf4352023-08-14 16:40:33 -0600407 case SCENEOBJT_MENU: {
408 const struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -0600409 const char *str;
410 char name[80];
Simon Glass6f3e87a2024-10-14 16:32:00 -0600411 int val;
Simon Glass28bf4352023-08-14 16:40:33 -0600412
Simon Glassf78388b2023-10-01 19:13:28 -0600413 /* write the ID of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600414 menu = (struct scene_obj_menu *)obj;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600415 ret = write_dt_u32(buf, obj->name, menu->cur_item_id);
Simon Glass28bf4352023-08-14 16:40:33 -0600416 if (ret)
Simon Glass6f3e87a2024-10-14 16:32:00 -0600417 return log_msg_ret("wrt", ret);
418
419 snprintf(name, sizeof(name), "%s-value", obj->name);
420 ret = get_cur_menuitem_val(menu, &val);
421 if (ret < 0)
422 return log_msg_ret("cur", ret);
423 ret = write_dt_u32(buf, name, val);
424 if (ret)
425 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600426
Simon Glass237f3752023-08-14 16:40:35 -0600427 ret = get_cur_menuitem_text(menu, &str);
428 if (ret)
429 return log_msg_ret("mis", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600430
Simon Glassf78388b2023-10-01 19:13:28 -0600431 /* write the text of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600432 snprintf(name, sizeof(name), "%s-str", obj->name);
Simon Glassf78388b2023-10-01 19:13:28 -0600433 ret = write_dt_string(buf, name, str);
Simon Glass28bf4352023-08-14 16:40:33 -0600434 if (ret)
Simon Glassf78388b2023-10-01 19:13:28 -0600435 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600436
437 break;
438 }
439 }
440
441 return 0;
442}
443
444int cedit_write_settings(struct expo *exp, struct abuf *buf)
445{
446 struct cedit_iter_priv priv;
447 void *fdt;
448 int ret;
449
Simon Glass6651e942025-05-01 07:37:01 -0600450 if (!abuf_init_size(buf, CEDIT_SIZE_INC))
Simon Glass28bf4352023-08-14 16:40:33 -0600451 return log_msg_ret("buf", -ENOMEM);
452
453 fdt = abuf_data(buf);
454 ret = fdt_create(fdt, abuf_size(buf));
455 if (!ret)
456 ret = fdt_finish_reservemap(fdt);
457 if (!ret)
458 ret = fdt_begin_node(fdt, "");
459 if (!ret)
460 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
461 if (ret) {
462 log_debug("Failed to start FDT (err=%d)\n", ret);
463 return log_msg_ret("sta", -EINVAL);
464 }
465
466 /* write out the items */
467 priv.buf = buf;
468 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
469 if (ret) {
470 log_debug("Failed to write settings (err=%d)\n", ret);
471 return log_msg_ret("set", ret);
472 }
473
474 ret = fdt_end_node(fdt);
475 if (!ret)
476 ret = fdt_end_node(fdt);
477 if (!ret)
478 ret = fdt_finish(fdt);
479 if (ret) {
480 log_debug("Failed to finish FDT (err=%d)\n", ret);
481 return log_msg_ret("fin", -EINVAL);
482 }
483
484 return 0;
485}
Simon Glassb1cd32b2023-08-14 16:40:34 -0600486
487static int h_read_settings(struct scene_obj *obj, void *vpriv)
488{
489 struct cedit_iter_priv *priv = vpriv;
490 ofnode node = priv->node;
491
492 switch (obj->type) {
493 case SCENEOBJT_NONE:
494 case SCENEOBJT_IMAGE:
495 case SCENEOBJT_TEXT:
496 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600497 case SCENEOBJT_TEXTLINE: {
498 const struct scene_obj_textline *tline;
499 const char *val;
500 int len;
501
502 tline = (struct scene_obj_textline *)obj;
503
504 val = ofnode_read_prop(node, obj->name, &len);
505 if (len >= tline->max_chars)
506 return log_msg_ret("str", -ENOSPC);
507 strcpy(abuf_data(&tline->buf), val);
508 break;
509 }
Simon Glassb1cd32b2023-08-14 16:40:34 -0600510 case SCENEOBJT_MENU: {
511 struct scene_obj_menu *menu;
512 uint val;
513
514 if (ofnode_read_u32(node, obj->name, &val))
515 return log_msg_ret("rd", -ENOENT);
516 menu = (struct scene_obj_menu *)obj;
517 menu->cur_item_id = val;
518
519 break;
520 }
521 }
522
523 return 0;
524}
525
526int cedit_read_settings(struct expo *exp, oftree tree)
527{
528 struct cedit_iter_priv priv;
529 ofnode root, node;
530 int ret;
531
532 root = oftree_root(tree);
533 if (!ofnode_valid(root))
534 return log_msg_ret("roo", -ENOENT);
535 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
536 if (!ofnode_valid(node))
537 return log_msg_ret("pat", -ENOENT);
538
539 /* read in the items */
540 priv.node = node;
541 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
542 if (ret) {
543 log_debug("Failed to read settings (err=%d)\n", ret);
544 return log_msg_ret("set", ret);
545 }
546
547 return 0;
548}
Simon Glass237f3752023-08-14 16:40:35 -0600549
550static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
551{
552 const struct scene_obj_menu *menu;
553 struct cedit_iter_priv *priv = vpriv;
554 char name[80], var[60];
555 const char *str;
556 int val, ret;
557
Simon Glass53a0a2f2024-10-14 16:31:57 -0600558 if (obj->id < EXPOID_BASE_ID)
559 return 0;
560
Simon Glass237f3752023-08-14 16:40:35 -0600561 snprintf(var, sizeof(var), "c.%s", obj->name);
562
Simon Glassb7a64532023-10-01 19:13:24 -0600563 switch (obj->type) {
564 case SCENEOBJT_NONE:
565 case SCENEOBJT_IMAGE:
566 case SCENEOBJT_TEXT:
567 break;
568 case SCENEOBJT_MENU:
569 menu = (struct scene_obj_menu *)obj;
570 val = menu->cur_item_id;
Simon Glass237f3752023-08-14 16:40:35 -0600571
Simon Glassb7a64532023-10-01 19:13:24 -0600572 if (priv->verbose)
573 printf("%s=%d\n", var, val);
Simon Glass237f3752023-08-14 16:40:35 -0600574
Simon Glassb7a64532023-10-01 19:13:24 -0600575 ret = env_set_ulong(var, val);
576 if (ret)
577 return log_msg_ret("set", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600578
Simon Glassb7a64532023-10-01 19:13:24 -0600579 ret = get_cur_menuitem_text(menu, &str);
580 if (ret)
581 return log_msg_ret("mis", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600582
Simon Glassb7a64532023-10-01 19:13:24 -0600583 snprintf(name, sizeof(name), "c.%s-str", obj->name);
584 if (priv->verbose)
585 printf("%s=%s\n", name, str);
586
587 ret = env_set(name, str);
588 if (ret)
589 return log_msg_ret("st2", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600590
591 ret = get_cur_menuitem_val(menu, &val);
592 if (ret < 0)
593 return log_msg_ret("cur", ret);
594 snprintf(name, sizeof(name), "c.%s-value", obj->name);
595 if (priv->verbose)
596 printf("%s=%d\n", name, val);
597
Simon Glassb7a64532023-10-01 19:13:24 -0600598 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600599 case SCENEOBJT_TEXTLINE: {
600 const struct scene_obj_textline *tline;
601
602 tline = (struct scene_obj_textline *)obj;
603 str = abuf_data(&tline->buf);
604 ret = env_set(var, str);
605 if (ret)
606 return log_msg_ret("set", ret);
607
608 if (priv->verbose)
609 printf("%s=%s\n", var, str);
610
611 break;
612 }
Simon Glassb7a64532023-10-01 19:13:24 -0600613 }
Simon Glass237f3752023-08-14 16:40:35 -0600614
615 return 0;
616}
617
618int cedit_write_settings_env(struct expo *exp, bool verbose)
619{
620 struct cedit_iter_priv priv;
621 int ret;
622
623 /* write out the items */
624 priv.verbose = verbose;
625 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
626 if (ret) {
627 log_debug("Failed to write settings to env (err=%d)\n", ret);
628 return log_msg_ret("set", ret);
629 }
630
631 return 0;
632}
Simon Glass0f2e5a62023-08-14 16:40:36 -0600633
634static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
635{
636 struct cedit_iter_priv *priv = vpriv;
637 struct scene_obj_menu *menu;
638 char var[60];
Simon Glass2b91ca62023-08-14 16:40:37 -0600639 int val;
Simon Glass0f2e5a62023-08-14 16:40:36 -0600640
Simon Glass53a0a2f2024-10-14 16:31:57 -0600641 if (obj->id < EXPOID_BASE_ID)
642 return 0;
643
Simon Glass0f2e5a62023-08-14 16:40:36 -0600644 snprintf(var, sizeof(var), "c.%s", obj->name);
645
Simon Glassb7a64532023-10-01 19:13:24 -0600646 switch (obj->type) {
647 case SCENEOBJT_NONE:
648 case SCENEOBJT_IMAGE:
649 case SCENEOBJT_TEXT:
650 break;
651 case SCENEOBJT_MENU:
652 menu = (struct scene_obj_menu *)obj;
653 val = env_get_ulong(var, 10, 0);
654 if (priv->verbose)
655 printf("%s=%d\n", var, val);
656 if (!val)
657 return log_msg_ret("get", -ENOENT);
Simon Glass0f2e5a62023-08-14 16:40:36 -0600658
Simon Glassb7a64532023-10-01 19:13:24 -0600659 /*
660 * note that no validation is done here, to make sure the ID is
Simon Glass330aa1e2024-10-14 16:31:59 -0600661 * valid and actually points to a menu item
Simon Glassb7a64532023-10-01 19:13:24 -0600662 */
663 menu->cur_item_id = val;
664 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600665 case SCENEOBJT_TEXTLINE: {
666 const struct scene_obj_textline *tline;
667 const char *value;
668
669 tline = (struct scene_obj_textline *)obj;
670 value = env_get(var);
671 if (value && strlen(value) >= tline->max_chars)
672 return log_msg_ret("str", -ENOSPC);
673 if (!value)
674 value = "";
675 if (priv->verbose)
676 printf("%s=%s\n", var, value);
677 strcpy(abuf_data(&tline->buf), value);
678 break;
679 }
Simon Glassb7a64532023-10-01 19:13:24 -0600680 }
Simon Glass0f2e5a62023-08-14 16:40:36 -0600681
682 return 0;
683}
684
685int cedit_read_settings_env(struct expo *exp, bool verbose)
686{
687 struct cedit_iter_priv priv;
688 int ret;
689
690 /* write out the items */
691 priv.verbose = verbose;
692 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
693 if (ret) {
694 log_debug("Failed to read settings from env (err=%d)\n", ret);
695 return log_msg_ret("set", ret);
696 }
697
698 return 0;
699}
Simon Glass2b91ca62023-08-14 16:40:37 -0600700
Simon Glass2b91ca62023-08-14 16:40:37 -0600701static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
702{
703 const struct scene_obj_menu *menu;
704 struct cedit_iter_priv *priv = vpriv;
705 int val, ret;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600706 uint i;
Simon Glass2b91ca62023-08-14 16:40:37 -0600707
Simon Glass53a0a2f2024-10-14 16:31:57 -0600708 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass2b91ca62023-08-14 16:40:37 -0600709 return 0;
710
711 menu = (struct scene_obj_menu *)obj;
712 val = menu->cur_item_id;
713
Simon Glass6f3e87a2024-10-14 16:32:00 -0600714 ret = get_cur_menuitem_val(menu, &val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600715 if (ret < 0)
716 return log_msg_ret("cur", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600717 log_debug("%s: val=%d\n", menu->obj.name, val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600718
719 /* figure out where to place this item */
720 if (!obj->bit_length)
721 return log_msg_ret("len", -EINVAL);
722 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
723 return log_msg_ret("bit", -E2BIG);
724
Simon Glass6f3e87a2024-10-14 16:32:00 -0600725 for (i = 0; i < obj->bit_length; i++, val >>= 1) {
Simon Glass2b91ca62023-08-14 16:40:37 -0600726 uint bitnum = obj->start_bit + i;
727
728 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600729 if (val & 1)
Simon Glass2b91ca62023-08-14 16:40:37 -0600730 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
731 log_debug("bit %x %x %x\n", bitnum,
732 priv->mask[CMOS_BYTE(bitnum)],
733 priv->value[CMOS_BYTE(bitnum)]);
734 }
735
736 return 0;
737}
738
739int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
740 bool verbose)
741{
742 struct cedit_iter_priv priv;
743 int ret, i, count, first, last;
744
745 /* write out the items */
746 priv.mask = calloc(1, CMOS_MAX_BYTES);
747 if (!priv.mask)
748 return log_msg_ret("mas", -ENOMEM);
749 priv.value = calloc(1, CMOS_MAX_BYTES);
750 if (!priv.value) {
751 free(priv.mask);
752 return log_msg_ret("val", -ENOMEM);
753 }
754
755 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
756 if (ret) {
757 log_debug("Failed to write CMOS (err=%d)\n", ret);
758 ret = log_msg_ret("set", ret);
759 goto done;
760 }
761
762 /* write the data to the RTC */
Simon Glass330aa1e2024-10-14 16:31:59 -0600763 log_debug("Writing CMOS\n");
Simon Glass2b91ca62023-08-14 16:40:37 -0600764 first = CMOS_MAX_BYTES;
765 last = -1;
766 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
767 if (priv.mask[i]) {
768 log_debug("Write byte %x: %x\n", i, priv.value[i]);
769 ret = rtc_write8(dev, i, priv.value[i]);
770 if (ret) {
771 ret = log_msg_ret("wri", ret);
772 goto done;
773 }
774 count++;
775 first = min(first, i);
776 last = max(last, i);
777 }
778 }
779 if (verbose) {
780 printf("Write %d bytes from offset %x to %x\n", count, first,
781 last);
782 }
783
784done:
785 free(priv.mask);
786 free(priv.value);
787 return ret;
788}
Simon Glass4462fa32023-08-14 16:40:38 -0600789
790static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
791{
792 struct cedit_iter_priv *priv = vpriv;
793 const struct scene_menitem *mi;
794 struct scene_obj_menu *menu;
795 int val, ret;
796 uint i;
797
Simon Glass53a0a2f2024-10-14 16:31:57 -0600798 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass4462fa32023-08-14 16:40:38 -0600799 return 0;
800
801 menu = (struct scene_obj_menu *)obj;
802
803 /* figure out where to place this item */
804 if (!obj->bit_length)
805 return log_msg_ret("len", -EINVAL);
806 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
807 return log_msg_ret("bit", -E2BIG);
808
809 val = 0;
810 for (i = 0; i < obj->bit_length; i++) {
811 uint bitnum = obj->start_bit + i;
812 uint offset = CMOS_BYTE(bitnum);
813
814 /* read the byte if not already read */
815 if (!priv->mask[offset]) {
816 ret = rtc_read8(priv->dev, offset);
817 if (ret < 0)
818 return log_msg_ret("rea", ret);
819 priv->value[offset] = ret;
820
821 /* mark it as read */
822 priv->mask[offset] = 0xff;
823 }
824
825 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
826 val |= BIT(i);
827 log_debug("bit %x %x\n", bitnum, val);
828 }
829
830 /* update the current item */
Simon Glass330aa1e2024-10-14 16:31:59 -0600831 log_debug("look for menuitem value %d in menu %d\n", val, menu->obj.id);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600832 mi = scene_menuitem_find_val(menu, val);
Simon Glass4462fa32023-08-14 16:40:38 -0600833 if (!mi)
834 return log_msg_ret("seq", -ENOENT);
835
836 menu->cur_item_id = mi->id;
837 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
838
839 return 0;
840}
841
842int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
843 bool verbose)
844{
845 struct cedit_iter_priv priv;
846 int ret, i, count, first, last;
847
848 /* read in the items */
849 priv.mask = calloc(1, CMOS_MAX_BYTES);
850 if (!priv.mask)
851 return log_msg_ret("mas", -ENOMEM);
852 priv.value = calloc(1, CMOS_MAX_BYTES);
853 if (!priv.value) {
854 free(priv.mask);
855 return log_msg_ret("val", -ENOMEM);
856 }
857 priv.dev = dev;
858
859 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
860 if (ret) {
861 log_debug("Failed to read CMOS (err=%d)\n", ret);
862 ret = log_msg_ret("set", ret);
863 goto done;
864 }
865
Simon Glass330aa1e2024-10-14 16:31:59 -0600866 /* indicate what bytes were read from the RTC */
Simon Glass4462fa32023-08-14 16:40:38 -0600867 first = CMOS_MAX_BYTES;
868 last = -1;
869 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
870 if (priv.mask[i]) {
871 log_debug("Read byte %x: %x\n", i, priv.value[i]);
872 count++;
873 first = min(first, i);
874 last = max(last, i);
875 }
876 }
877 if (verbose) {
878 printf("Read %d bytes from offset %x to %x\n", count, first,
879 last);
880 }
881
882done:
883 free(priv.mask);
884 free(priv.value);
885 return ret;
886}