blob: 4e80875828b915eb6aa1ecf2a2a444982545096c [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{
154 struct cli_ch_state s_cch, *cch = &s_cch;
155 struct video_priv *vid_priv;
156 uint scene_id;
157 struct scene *scn;
Simon Glass53a0a2f2024-10-14 16:31:57 -0600158 bool done, save;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600159 int ret;
160
161 cli_ch_init(cch);
162 ret = cedit_prepare(exp, &vid_priv, &scn);
163 if (ret < 0)
164 return log_msg_ret("prep", ret);
165 scene_id = ret;
166
Simon Glassc8925112023-06-01 10:23:02 -0600167 done = false;
Simon Glass53a0a2f2024-10-14 16:31:57 -0600168 save = false;
Simon Glassc8925112023-06-01 10:23:02 -0600169 do {
170 struct expo_action act;
171 int ichar, key;
172
173 ret = expo_render(exp);
174 if (ret)
175 break;
176
177 ichar = cli_ch_process(cch, 0);
178 if (!ichar) {
179 while (!ichar && !tstc()) {
180 schedule();
181 mdelay(2);
182 ichar = cli_ch_process(cch, -ETIMEDOUT);
183 }
184 if (!ichar) {
185 ichar = getchar();
186 ichar = cli_ch_process(cch, ichar);
187 }
188 }
189
190 key = 0;
191 if (ichar) {
192 key = bootmenu_conv_key(ichar);
Simon Glasseb8af2c2023-10-01 19:13:36 -0600193 if (key == BKEY_NONE || key >= BKEY_FIRST_EXTRA)
Simon Glassc8925112023-06-01 10:23:02 -0600194 key = ichar;
195 }
196 if (!key)
197 continue;
198
199 ret = expo_send_key(exp, key);
200 if (ret)
201 break;
202
203 ret = expo_action_get(exp, &act);
204 if (!ret) {
205 switch (act.type) {
206 case EXPOACT_POINT_OBJ:
207 scene_set_highlight_id(scn, act.select.id);
208 cedit_arange(exp, vid_priv, scene_id);
209 break;
210 case EXPOACT_OPEN:
211 scene_set_open(scn, act.select.id, true);
212 cedit_arange(exp, vid_priv, scene_id);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600213 switch (scn->highlight_id) {
214 case EXPOID_SAVE:
215 done = true;
216 save = true;
217 break;
218 case EXPOID_DISCARD:
219 done = true;
220 break;
221 }
Simon Glassc8925112023-06-01 10:23:02 -0600222 break;
223 case EXPOACT_CLOSE:
224 scene_set_open(scn, act.select.id, false);
225 cedit_arange(exp, vid_priv, scene_id);
226 break;
227 case EXPOACT_SELECT:
228 scene_set_open(scn, scn->highlight_id, false);
229 cedit_arange(exp, vid_priv, scene_id);
230 break;
231 case EXPOACT_QUIT:
232 log_debug("quitting\n");
233 done = true;
234 break;
235 default:
236 break;
237 }
238 }
239 } while (!done);
240
241 if (ret)
242 return log_msg_ret("end", ret);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600243 if (!save)
244 return -EACCES;
Simon Glassc8925112023-06-01 10:23:02 -0600245
246 return 0;
247}
Simon Glass28bf4352023-08-14 16:40:33 -0600248
249static int check_space(int ret, struct abuf *buf)
250{
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),
255 abuf_size(buf));
256 if (ret)
257 return log_msg_ret("res", -EFAULT);
258 }
259
260 return 0;
261}
262
Simon Glassdb6a0512023-10-01 19:13:23 -0600263/**
264 * get_cur_menuitem_text() - Get the text of the currently selected item
265 *
266 * Looks up the object for the current item, finds text object for it and looks
267 * up the string for that text
268 *
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
272 */
Simon Glass237f3752023-08-14 16:40:35 -0600273static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
274 const char **strp)
275{
276 struct scene *scn = menu->obj.scene;
277 const struct scene_menitem *mi;
278 const struct scene_obj_txt *txt;
279 const char *str;
280
281 mi = scene_menuitem_find(menu, menu->cur_item_id);
282 if (!mi)
283 return log_msg_ret("mi", -ENOENT);
284
285 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
286 if (!txt)
287 return log_msg_ret("txt", -ENOENT);
288
289 str = expo_get_str(scn->expo, txt->str_id);
290 if (!str)
291 return log_msg_ret("str", -ENOENT);
292 *strp = str;
293
294 return 0;
295}
296
Simon Glass6f3e87a2024-10-14 16:32:00 -0600297/**
298 * get_cur_menuitem_val() - Get the value of a menu's current item
299 *
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.
304 *
305 * @menu: Menu to check
306 * @valp: Returns current-item value / sequence number
307 * Return: 0 on success, else -ve error value
308 */
309static int get_cur_menuitem_val(const struct scene_obj_menu *menu, int *valp)
310{
311 const struct scene_menitem *mi;
312 int seq;
313
314 seq = 0;
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;
318 return 0;
319 }
320 seq++;
321 }
322
323 return log_msg_ret("nf", -ENOENT);
324}
325
326/**
327 * write_dt_string() - Write a string to the devicetree, expanding if needed
328 *
329 * If this fails, it tries again after expanding the devicetree a little
330 *
331 * @buf: Buffer containing the devicetree
332 * @name: Property name to use
333 * @str: String value
334 * Return: 0 if OK, -EFAULT if something went horribly wrong
335 */
Simon Glassf78388b2023-10-01 19:13:28 -0600336static int write_dt_string(struct abuf *buf, const char *name, const char *str)
337{
338 int ret, i;
339
Simon Glassf78388b2023-10-01 19:13:28 -0600340 ret = -EAGAIN;
341 for (i = 0; ret && i < 2; i++) {
342 ret = fdt_property_string(abuf_data(buf), name, str);
343 if (!i) {
344 ret = check_space(ret, buf);
345 if (ret)
346 return log_msg_ret("rs2", -ENOMEM);
347 }
348 }
349
350 /* this should not happen */
351 if (ret)
352 return log_msg_ret("str", -EFAULT);
353
354 return 0;
355}
356
Simon Glass6f3e87a2024-10-14 16:32:00 -0600357/**
358 * write_dt_u32() - Write an int to the devicetree, expanding if needed
359 *
360 * If this fails, it tries again after expanding the devicetree a little
361 *
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
366 */
367static int write_dt_u32(struct abuf *buf, const char *name, uint val)
368{
369 int ret, i;
370
371 /* write the text of the current item */
372 ret = -EAGAIN;
373 for (i = 0; ret && i < 2; i++) {
374 ret = fdt_property_u32(abuf_data(buf), name, val);
375 if (!i) {
376 ret = check_space(ret, buf);
377 if (ret)
378 return log_msg_ret("rs2", -ENOMEM);
379 }
380 }
381
382 /* this should not happen */
383 if (ret)
384 return log_msg_ret("str", -EFAULT);
385
386 return 0;
387}
388
Simon Glass28bf4352023-08-14 16:40:33 -0600389static int h_write_settings(struct scene_obj *obj, void *vpriv)
390{
391 struct cedit_iter_priv *priv = vpriv;
392 struct abuf *buf = priv->buf;
Simon Glass23c3eb42023-10-01 19:13:37 -0600393 int ret;
Simon Glass28bf4352023-08-14 16:40:33 -0600394
395 switch (obj->type) {
396 case SCENEOBJT_NONE:
397 case SCENEOBJT_IMAGE:
398 case SCENEOBJT_TEXT:
399 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600400 case SCENEOBJT_TEXTLINE: {
401 const struct scene_obj_textline *tline;
402
403 tline = (struct scene_obj_textline *)obj;
404 ret = write_dt_string(buf, obj->name, abuf_data(&tline->buf));
405 if (ret)
406 return log_msg_ret("wr2", ret);
407 break;
408 }
Simon Glass28bf4352023-08-14 16:40:33 -0600409 case SCENEOBJT_MENU: {
410 const struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -0600411 const char *str;
412 char name[80];
Simon Glass6f3e87a2024-10-14 16:32:00 -0600413 int val;
Simon Glass28bf4352023-08-14 16:40:33 -0600414
Simon Glassf78388b2023-10-01 19:13:28 -0600415 /* write the ID of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600416 menu = (struct scene_obj_menu *)obj;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600417 ret = write_dt_u32(buf, obj->name, menu->cur_item_id);
Simon Glass28bf4352023-08-14 16:40:33 -0600418 if (ret)
Simon Glass6f3e87a2024-10-14 16:32:00 -0600419 return log_msg_ret("wrt", ret);
420
421 snprintf(name, sizeof(name), "%s-value", obj->name);
422 ret = get_cur_menuitem_val(menu, &val);
423 if (ret < 0)
424 return log_msg_ret("cur", ret);
425 ret = write_dt_u32(buf, name, val);
426 if (ret)
427 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600428
Simon Glass237f3752023-08-14 16:40:35 -0600429 ret = get_cur_menuitem_text(menu, &str);
430 if (ret)
431 return log_msg_ret("mis", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600432
Simon Glassf78388b2023-10-01 19:13:28 -0600433 /* write the text of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600434 snprintf(name, sizeof(name), "%s-str", obj->name);
Simon Glassf78388b2023-10-01 19:13:28 -0600435 ret = write_dt_string(buf, name, str);
Simon Glass28bf4352023-08-14 16:40:33 -0600436 if (ret)
Simon Glassf78388b2023-10-01 19:13:28 -0600437 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600438
439 break;
440 }
441 }
442
443 return 0;
444}
445
446int cedit_write_settings(struct expo *exp, struct abuf *buf)
447{
448 struct cedit_iter_priv priv;
449 void *fdt;
450 int ret;
451
Simon Glass6651e942025-05-01 07:37:01 -0600452 if (!abuf_init_size(buf, CEDIT_SIZE_INC))
Simon Glass28bf4352023-08-14 16:40:33 -0600453 return log_msg_ret("buf", -ENOMEM);
454
455 fdt = abuf_data(buf);
456 ret = fdt_create(fdt, abuf_size(buf));
457 if (!ret)
458 ret = fdt_finish_reservemap(fdt);
459 if (!ret)
460 ret = fdt_begin_node(fdt, "");
461 if (!ret)
462 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
463 if (ret) {
464 log_debug("Failed to start FDT (err=%d)\n", ret);
465 return log_msg_ret("sta", -EINVAL);
466 }
467
468 /* write out the items */
469 priv.buf = buf;
470 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
471 if (ret) {
472 log_debug("Failed to write settings (err=%d)\n", ret);
473 return log_msg_ret("set", ret);
474 }
475
476 ret = fdt_end_node(fdt);
477 if (!ret)
478 ret = fdt_end_node(fdt);
479 if (!ret)
480 ret = fdt_finish(fdt);
481 if (ret) {
482 log_debug("Failed to finish FDT (err=%d)\n", ret);
483 return log_msg_ret("fin", -EINVAL);
484 }
485
486 return 0;
487}
Simon Glassb1cd32b2023-08-14 16:40:34 -0600488
489static int h_read_settings(struct scene_obj *obj, void *vpriv)
490{
491 struct cedit_iter_priv *priv = vpriv;
492 ofnode node = priv->node;
493
494 switch (obj->type) {
495 case SCENEOBJT_NONE:
496 case SCENEOBJT_IMAGE:
497 case SCENEOBJT_TEXT:
498 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600499 case SCENEOBJT_TEXTLINE: {
500 const struct scene_obj_textline *tline;
501 const char *val;
502 int len;
503
504 tline = (struct scene_obj_textline *)obj;
505
506 val = ofnode_read_prop(node, obj->name, &len);
507 if (len >= tline->max_chars)
508 return log_msg_ret("str", -ENOSPC);
509 strcpy(abuf_data(&tline->buf), val);
510 break;
511 }
Simon Glassb1cd32b2023-08-14 16:40:34 -0600512 case SCENEOBJT_MENU: {
513 struct scene_obj_menu *menu;
514 uint val;
515
516 if (ofnode_read_u32(node, obj->name, &val))
517 return log_msg_ret("rd", -ENOENT);
518 menu = (struct scene_obj_menu *)obj;
519 menu->cur_item_id = val;
520
521 break;
522 }
523 }
524
525 return 0;
526}
527
528int cedit_read_settings(struct expo *exp, oftree tree)
529{
530 struct cedit_iter_priv priv;
531 ofnode root, node;
532 int ret;
533
534 root = oftree_root(tree);
535 if (!ofnode_valid(root))
536 return log_msg_ret("roo", -ENOENT);
537 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
538 if (!ofnode_valid(node))
539 return log_msg_ret("pat", -ENOENT);
540
541 /* read in the items */
542 priv.node = node;
543 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
544 if (ret) {
545 log_debug("Failed to read settings (err=%d)\n", ret);
546 return log_msg_ret("set", ret);
547 }
548
549 return 0;
550}
Simon Glass237f3752023-08-14 16:40:35 -0600551
552static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
553{
554 const struct scene_obj_menu *menu;
555 struct cedit_iter_priv *priv = vpriv;
556 char name[80], var[60];
557 const char *str;
558 int val, ret;
559
Simon Glass53a0a2f2024-10-14 16:31:57 -0600560 if (obj->id < EXPOID_BASE_ID)
561 return 0;
562
Simon Glass237f3752023-08-14 16:40:35 -0600563 snprintf(var, sizeof(var), "c.%s", obj->name);
564
Simon Glassb7a64532023-10-01 19:13:24 -0600565 switch (obj->type) {
566 case SCENEOBJT_NONE:
567 case SCENEOBJT_IMAGE:
568 case SCENEOBJT_TEXT:
569 break;
570 case SCENEOBJT_MENU:
571 menu = (struct scene_obj_menu *)obj;
572 val = menu->cur_item_id;
Simon Glass237f3752023-08-14 16:40:35 -0600573
Simon Glassb7a64532023-10-01 19:13:24 -0600574 if (priv->verbose)
575 printf("%s=%d\n", var, val);
Simon Glass237f3752023-08-14 16:40:35 -0600576
Simon Glassb7a64532023-10-01 19:13:24 -0600577 ret = env_set_ulong(var, val);
578 if (ret)
579 return log_msg_ret("set", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600580
Simon Glassb7a64532023-10-01 19:13:24 -0600581 ret = get_cur_menuitem_text(menu, &str);
582 if (ret)
583 return log_msg_ret("mis", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600584
Simon Glassb7a64532023-10-01 19:13:24 -0600585 snprintf(name, sizeof(name), "c.%s-str", obj->name);
586 if (priv->verbose)
587 printf("%s=%s\n", name, str);
588
589 ret = env_set(name, str);
590 if (ret)
591 return log_msg_ret("st2", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600592
593 ret = get_cur_menuitem_val(menu, &val);
594 if (ret < 0)
595 return log_msg_ret("cur", ret);
596 snprintf(name, sizeof(name), "c.%s-value", obj->name);
597 if (priv->verbose)
598 printf("%s=%d\n", name, val);
599
Simon Glassb7a64532023-10-01 19:13:24 -0600600 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600601 case SCENEOBJT_TEXTLINE: {
602 const struct scene_obj_textline *tline;
603
604 tline = (struct scene_obj_textline *)obj;
605 str = abuf_data(&tline->buf);
606 ret = env_set(var, str);
607 if (ret)
608 return log_msg_ret("set", ret);
609
610 if (priv->verbose)
611 printf("%s=%s\n", var, str);
612
613 break;
614 }
Simon Glassb7a64532023-10-01 19:13:24 -0600615 }
Simon Glass237f3752023-08-14 16:40:35 -0600616
617 return 0;
618}
619
620int cedit_write_settings_env(struct expo *exp, bool verbose)
621{
622 struct cedit_iter_priv priv;
623 int ret;
624
625 /* write out the items */
626 priv.verbose = verbose;
627 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
628 if (ret) {
629 log_debug("Failed to write settings to env (err=%d)\n", ret);
630 return log_msg_ret("set", ret);
631 }
632
633 return 0;
634}
Simon Glass0f2e5a62023-08-14 16:40:36 -0600635
636static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
637{
638 struct cedit_iter_priv *priv = vpriv;
639 struct scene_obj_menu *menu;
640 char var[60];
Simon Glass2b91ca62023-08-14 16:40:37 -0600641 int val;
Simon Glass0f2e5a62023-08-14 16:40:36 -0600642
Simon Glass53a0a2f2024-10-14 16:31:57 -0600643 if (obj->id < EXPOID_BASE_ID)
644 return 0;
645
Simon Glass0f2e5a62023-08-14 16:40:36 -0600646 snprintf(var, sizeof(var), "c.%s", obj->name);
647
Simon Glassb7a64532023-10-01 19:13:24 -0600648 switch (obj->type) {
649 case SCENEOBJT_NONE:
650 case SCENEOBJT_IMAGE:
651 case SCENEOBJT_TEXT:
652 break;
653 case SCENEOBJT_MENU:
654 menu = (struct scene_obj_menu *)obj;
655 val = env_get_ulong(var, 10, 0);
656 if (priv->verbose)
657 printf("%s=%d\n", var, val);
658 if (!val)
659 return log_msg_ret("get", -ENOENT);
Simon Glass0f2e5a62023-08-14 16:40:36 -0600660
Simon Glassb7a64532023-10-01 19:13:24 -0600661 /*
662 * note that no validation is done here, to make sure the ID is
Simon Glass330aa1e2024-10-14 16:31:59 -0600663 * valid and actually points to a menu item
Simon Glassb7a64532023-10-01 19:13:24 -0600664 */
665 menu->cur_item_id = val;
666 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600667 case SCENEOBJT_TEXTLINE: {
668 const struct scene_obj_textline *tline;
669 const char *value;
670
671 tline = (struct scene_obj_textline *)obj;
672 value = env_get(var);
673 if (value && strlen(value) >= tline->max_chars)
674 return log_msg_ret("str", -ENOSPC);
675 if (!value)
676 value = "";
677 if (priv->verbose)
678 printf("%s=%s\n", var, value);
679 strcpy(abuf_data(&tline->buf), value);
680 break;
681 }
Simon Glassb7a64532023-10-01 19:13:24 -0600682 }
Simon Glass0f2e5a62023-08-14 16:40:36 -0600683
684 return 0;
685}
686
687int cedit_read_settings_env(struct expo *exp, bool verbose)
688{
689 struct cedit_iter_priv priv;
690 int ret;
691
692 /* write out the items */
693 priv.verbose = verbose;
694 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
695 if (ret) {
696 log_debug("Failed to read settings from env (err=%d)\n", ret);
697 return log_msg_ret("set", ret);
698 }
699
700 return 0;
701}
Simon Glass2b91ca62023-08-14 16:40:37 -0600702
Simon Glass2b91ca62023-08-14 16:40:37 -0600703static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
704{
705 const struct scene_obj_menu *menu;
706 struct cedit_iter_priv *priv = vpriv;
707 int val, ret;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600708 uint i;
Simon Glass2b91ca62023-08-14 16:40:37 -0600709
Simon Glass53a0a2f2024-10-14 16:31:57 -0600710 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass2b91ca62023-08-14 16:40:37 -0600711 return 0;
712
713 menu = (struct scene_obj_menu *)obj;
714 val = menu->cur_item_id;
715
Simon Glass6f3e87a2024-10-14 16:32:00 -0600716 ret = get_cur_menuitem_val(menu, &val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600717 if (ret < 0)
718 return log_msg_ret("cur", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600719 log_debug("%s: val=%d\n", menu->obj.name, val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600720
721 /* figure out where to place this item */
722 if (!obj->bit_length)
723 return log_msg_ret("len", -EINVAL);
724 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
725 return log_msg_ret("bit", -E2BIG);
726
Simon Glass6f3e87a2024-10-14 16:32:00 -0600727 for (i = 0; i < obj->bit_length; i++, val >>= 1) {
Simon Glass2b91ca62023-08-14 16:40:37 -0600728 uint bitnum = obj->start_bit + i;
729
730 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600731 if (val & 1)
Simon Glass2b91ca62023-08-14 16:40:37 -0600732 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
733 log_debug("bit %x %x %x\n", bitnum,
734 priv->mask[CMOS_BYTE(bitnum)],
735 priv->value[CMOS_BYTE(bitnum)]);
736 }
737
738 return 0;
739}
740
741int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
742 bool verbose)
743{
744 struct cedit_iter_priv priv;
745 int ret, i, count, first, last;
746
747 /* write out the items */
748 priv.mask = calloc(1, CMOS_MAX_BYTES);
749 if (!priv.mask)
750 return log_msg_ret("mas", -ENOMEM);
751 priv.value = calloc(1, CMOS_MAX_BYTES);
752 if (!priv.value) {
753 free(priv.mask);
754 return log_msg_ret("val", -ENOMEM);
755 }
756
757 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
758 if (ret) {
759 log_debug("Failed to write CMOS (err=%d)\n", ret);
760 ret = log_msg_ret("set", ret);
761 goto done;
762 }
763
764 /* write the data to the RTC */
Simon Glass330aa1e2024-10-14 16:31:59 -0600765 log_debug("Writing CMOS\n");
Simon Glass2b91ca62023-08-14 16:40:37 -0600766 first = CMOS_MAX_BYTES;
767 last = -1;
768 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
769 if (priv.mask[i]) {
770 log_debug("Write byte %x: %x\n", i, priv.value[i]);
771 ret = rtc_write8(dev, i, priv.value[i]);
772 if (ret) {
773 ret = log_msg_ret("wri", ret);
774 goto done;
775 }
776 count++;
777 first = min(first, i);
778 last = max(last, i);
779 }
780 }
781 if (verbose) {
782 printf("Write %d bytes from offset %x to %x\n", count, first,
783 last);
784 }
785
786done:
787 free(priv.mask);
788 free(priv.value);
789 return ret;
790}
Simon Glass4462fa32023-08-14 16:40:38 -0600791
792static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
793{
794 struct cedit_iter_priv *priv = vpriv;
795 const struct scene_menitem *mi;
796 struct scene_obj_menu *menu;
797 int val, ret;
798 uint i;
799
Simon Glass53a0a2f2024-10-14 16:31:57 -0600800 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass4462fa32023-08-14 16:40:38 -0600801 return 0;
802
803 menu = (struct scene_obj_menu *)obj;
804
805 /* figure out where to place this item */
806 if (!obj->bit_length)
807 return log_msg_ret("len", -EINVAL);
808 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
809 return log_msg_ret("bit", -E2BIG);
810
811 val = 0;
812 for (i = 0; i < obj->bit_length; i++) {
813 uint bitnum = obj->start_bit + i;
814 uint offset = CMOS_BYTE(bitnum);
815
816 /* read the byte if not already read */
817 if (!priv->mask[offset]) {
818 ret = rtc_read8(priv->dev, offset);
819 if (ret < 0)
820 return log_msg_ret("rea", ret);
821 priv->value[offset] = ret;
822
823 /* mark it as read */
824 priv->mask[offset] = 0xff;
825 }
826
827 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
828 val |= BIT(i);
829 log_debug("bit %x %x\n", bitnum, val);
830 }
831
832 /* update the current item */
Simon Glass330aa1e2024-10-14 16:31:59 -0600833 log_debug("look for menuitem value %d in menu %d\n", val, menu->obj.id);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600834 mi = scene_menuitem_find_val(menu, val);
Simon Glass4462fa32023-08-14 16:40:38 -0600835 if (!mi)
836 return log_msg_ret("seq", -ENOENT);
837
838 menu->cur_item_id = mi->id;
839 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
840
841 return 0;
842}
843
844int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
845 bool verbose)
846{
847 struct cedit_iter_priv priv;
848 int ret, i, count, first, last;
849
850 /* read in the items */
851 priv.mask = calloc(1, CMOS_MAX_BYTES);
852 if (!priv.mask)
853 return log_msg_ret("mas", -ENOMEM);
854 priv.value = calloc(1, CMOS_MAX_BYTES);
855 if (!priv.value) {
856 free(priv.mask);
857 return log_msg_ret("val", -ENOMEM);
858 }
859 priv.dev = dev;
860
861 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
862 if (ret) {
863 log_debug("Failed to read CMOS (err=%d)\n", ret);
864 ret = log_msg_ret("set", ret);
865 goto done;
866 }
867
Simon Glass330aa1e2024-10-14 16:31:59 -0600868 /* indicate what bytes were read from the RTC */
Simon Glass4462fa32023-08-14 16:40:38 -0600869 first = CMOS_MAX_BYTES;
870 last = -1;
871 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
872 if (priv.mask[i]) {
873 log_debug("Read byte %x: %x\n", i, priv.value[i]);
874 count++;
875 first = min(first, i);
876 last = max(last, i);
877 }
878 }
879 if (verbose) {
880 printf("Read %d bytes from offset %x to %x\n", count, first,
881 last);
882 }
883
884done:
885 free(priv.mask);
886 free(priv.value);
887 return ret;
888}