blob: b7b9cc510e05fa091a07c1296578828440cd4143 [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;
Simon Glassc8925112023-06-01 10:23:02 -0600169
Simon Glass137b4422025-05-02 08:46:16 -0600170 ret = expo_poll(exp, &act);
Simon Glassc8925112023-06-01 10:23:02 -0600171 if (!ret) {
172 switch (act.type) {
173 case EXPOACT_POINT_OBJ:
174 scene_set_highlight_id(scn, act.select.id);
175 cedit_arange(exp, vid_priv, scene_id);
176 break;
177 case EXPOACT_OPEN:
178 scene_set_open(scn, act.select.id, true);
179 cedit_arange(exp, vid_priv, scene_id);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600180 switch (scn->highlight_id) {
181 case EXPOID_SAVE:
182 done = true;
183 save = true;
184 break;
185 case EXPOID_DISCARD:
186 done = true;
187 break;
188 }
Simon Glassc8925112023-06-01 10:23:02 -0600189 break;
190 case EXPOACT_CLOSE:
191 scene_set_open(scn, act.select.id, false);
192 cedit_arange(exp, vid_priv, scene_id);
193 break;
194 case EXPOACT_SELECT:
195 scene_set_open(scn, scn->highlight_id, false);
196 cedit_arange(exp, vid_priv, scene_id);
197 break;
198 case EXPOACT_QUIT:
199 log_debug("quitting\n");
200 done = true;
201 break;
202 default:
203 break;
204 }
Simon Glass137b4422025-05-02 08:46:16 -0600205 } else if (ret != -EAGAIN) {
206 return log_msg_ret("cep", ret);
Simon Glassc8925112023-06-01 10:23:02 -0600207 }
208 } while (!done);
209
210 if (ret)
211 return log_msg_ret("end", ret);
Simon Glass53a0a2f2024-10-14 16:31:57 -0600212 if (!save)
213 return -EACCES;
Simon Glassc8925112023-06-01 10:23:02 -0600214
215 return 0;
216}
Simon Glass28bf4352023-08-14 16:40:33 -0600217
218static int check_space(int ret, struct abuf *buf)
219{
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),
224 abuf_size(buf));
225 if (ret)
226 return log_msg_ret("res", -EFAULT);
227 }
228
229 return 0;
230}
231
Simon Glassdb6a0512023-10-01 19:13:23 -0600232/**
233 * get_cur_menuitem_text() - Get the text of the currently selected item
234 *
235 * Looks up the object for the current item, finds text object for it and looks
236 * up the string for that text
237 *
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
241 */
Simon Glass237f3752023-08-14 16:40:35 -0600242static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
243 const char **strp)
244{
245 struct scene *scn = menu->obj.scene;
246 const struct scene_menitem *mi;
247 const struct scene_obj_txt *txt;
248 const char *str;
249
250 mi = scene_menuitem_find(menu, menu->cur_item_id);
251 if (!mi)
252 return log_msg_ret("mi", -ENOENT);
253
254 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
255 if (!txt)
256 return log_msg_ret("txt", -ENOENT);
257
258 str = expo_get_str(scn->expo, txt->str_id);
259 if (!str)
260 return log_msg_ret("str", -ENOENT);
261 *strp = str;
262
263 return 0;
264}
265
Simon Glass6f3e87a2024-10-14 16:32:00 -0600266/**
267 * get_cur_menuitem_val() - Get the value of a menu's current item
268 *
269 * Obtains the value of the current item in the menu. If no value, then
270 * enumerates the items of a menu (0, 1, 2) and returns the sequence number of
271 * the currently selected item. If the first item is selected, this returns 0;
272 * if the second, 1; etc.
273 *
274 * @menu: Menu to check
275 * @valp: Returns current-item value / sequence number
276 * Return: 0 on success, else -ve error value
277 */
278static int get_cur_menuitem_val(const struct scene_obj_menu *menu, int *valp)
279{
280 const struct scene_menitem *mi;
281 int seq;
282
283 seq = 0;
284 list_for_each_entry(mi, &menu->item_head, sibling) {
285 if (mi->id == menu->cur_item_id) {
286 *valp = mi->value == INT_MAX ? seq : mi->value;
287 return 0;
288 }
289 seq++;
290 }
291
292 return log_msg_ret("nf", -ENOENT);
293}
294
295/**
296 * write_dt_string() - Write a string to the devicetree, expanding if needed
297 *
298 * If this fails, it tries again after expanding the devicetree a little
299 *
300 * @buf: Buffer containing the devicetree
301 * @name: Property name to use
302 * @str: String value
303 * Return: 0 if OK, -EFAULT if something went horribly wrong
304 */
Simon Glassf78388b2023-10-01 19:13:28 -0600305static int write_dt_string(struct abuf *buf, const char *name, const char *str)
306{
307 int ret, i;
308
Simon Glassf78388b2023-10-01 19:13:28 -0600309 ret = -EAGAIN;
310 for (i = 0; ret && i < 2; i++) {
311 ret = fdt_property_string(abuf_data(buf), name, str);
312 if (!i) {
313 ret = check_space(ret, buf);
314 if (ret)
315 return log_msg_ret("rs2", -ENOMEM);
316 }
317 }
318
319 /* this should not happen */
320 if (ret)
321 return log_msg_ret("str", -EFAULT);
322
323 return 0;
324}
325
Simon Glass6f3e87a2024-10-14 16:32:00 -0600326/**
327 * write_dt_u32() - Write an int 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 * @lva: Integer value
334 * Return: 0 if OK, -EFAULT if something went horribly wrong
335 */
336static int write_dt_u32(struct abuf *buf, const char *name, uint val)
337{
338 int ret, i;
339
340 /* write the text of the current item */
341 ret = -EAGAIN;
342 for (i = 0; ret && i < 2; i++) {
343 ret = fdt_property_u32(abuf_data(buf), name, val);
344 if (!i) {
345 ret = check_space(ret, buf);
346 if (ret)
347 return log_msg_ret("rs2", -ENOMEM);
348 }
349 }
350
351 /* this should not happen */
352 if (ret)
353 return log_msg_ret("str", -EFAULT);
354
355 return 0;
356}
357
Simon Glass28bf4352023-08-14 16:40:33 -0600358static int h_write_settings(struct scene_obj *obj, void *vpriv)
359{
360 struct cedit_iter_priv *priv = vpriv;
361 struct abuf *buf = priv->buf;
Simon Glass23c3eb42023-10-01 19:13:37 -0600362 int ret;
Simon Glass28bf4352023-08-14 16:40:33 -0600363
364 switch (obj->type) {
365 case SCENEOBJT_NONE:
366 case SCENEOBJT_IMAGE:
367 case SCENEOBJT_TEXT:
368 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600369 case SCENEOBJT_TEXTLINE: {
370 const struct scene_obj_textline *tline;
371
372 tline = (struct scene_obj_textline *)obj;
373 ret = write_dt_string(buf, obj->name, abuf_data(&tline->buf));
374 if (ret)
375 return log_msg_ret("wr2", ret);
376 break;
377 }
Simon Glass28bf4352023-08-14 16:40:33 -0600378 case SCENEOBJT_MENU: {
379 const struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -0600380 const char *str;
381 char name[80];
Simon Glass6f3e87a2024-10-14 16:32:00 -0600382 int val;
Simon Glass28bf4352023-08-14 16:40:33 -0600383
Simon Glassf78388b2023-10-01 19:13:28 -0600384 /* write the ID of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600385 menu = (struct scene_obj_menu *)obj;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600386 ret = write_dt_u32(buf, obj->name, menu->cur_item_id);
Simon Glass28bf4352023-08-14 16:40:33 -0600387 if (ret)
Simon Glass6f3e87a2024-10-14 16:32:00 -0600388 return log_msg_ret("wrt", ret);
389
390 snprintf(name, sizeof(name), "%s-value", obj->name);
391 ret = get_cur_menuitem_val(menu, &val);
392 if (ret < 0)
393 return log_msg_ret("cur", ret);
394 ret = write_dt_u32(buf, name, val);
395 if (ret)
396 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600397
Simon Glass237f3752023-08-14 16:40:35 -0600398 ret = get_cur_menuitem_text(menu, &str);
399 if (ret)
400 return log_msg_ret("mis", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600401
Simon Glassf78388b2023-10-01 19:13:28 -0600402 /* write the text of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600403 snprintf(name, sizeof(name), "%s-str", obj->name);
Simon Glassf78388b2023-10-01 19:13:28 -0600404 ret = write_dt_string(buf, name, str);
Simon Glass28bf4352023-08-14 16:40:33 -0600405 if (ret)
Simon Glassf78388b2023-10-01 19:13:28 -0600406 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600407
408 break;
409 }
410 }
411
412 return 0;
413}
414
415int cedit_write_settings(struct expo *exp, struct abuf *buf)
416{
417 struct cedit_iter_priv priv;
418 void *fdt;
419 int ret;
420
Simon Glass6651e942025-05-01 07:37:01 -0600421 if (!abuf_init_size(buf, CEDIT_SIZE_INC))
Simon Glass28bf4352023-08-14 16:40:33 -0600422 return log_msg_ret("buf", -ENOMEM);
423
424 fdt = abuf_data(buf);
425 ret = fdt_create(fdt, abuf_size(buf));
426 if (!ret)
427 ret = fdt_finish_reservemap(fdt);
428 if (!ret)
429 ret = fdt_begin_node(fdt, "");
430 if (!ret)
431 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
432 if (ret) {
433 log_debug("Failed to start FDT (err=%d)\n", ret);
434 return log_msg_ret("sta", -EINVAL);
435 }
436
437 /* write out the items */
438 priv.buf = buf;
439 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
440 if (ret) {
441 log_debug("Failed to write settings (err=%d)\n", ret);
442 return log_msg_ret("set", ret);
443 }
444
445 ret = fdt_end_node(fdt);
446 if (!ret)
447 ret = fdt_end_node(fdt);
448 if (!ret)
449 ret = fdt_finish(fdt);
450 if (ret) {
451 log_debug("Failed to finish FDT (err=%d)\n", ret);
452 return log_msg_ret("fin", -EINVAL);
453 }
454
455 return 0;
456}
Simon Glassb1cd32b2023-08-14 16:40:34 -0600457
458static int h_read_settings(struct scene_obj *obj, void *vpriv)
459{
460 struct cedit_iter_priv *priv = vpriv;
461 ofnode node = priv->node;
462
463 switch (obj->type) {
464 case SCENEOBJT_NONE:
465 case SCENEOBJT_IMAGE:
466 case SCENEOBJT_TEXT:
467 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600468 case SCENEOBJT_TEXTLINE: {
469 const struct scene_obj_textline *tline;
470 const char *val;
471 int len;
472
473 tline = (struct scene_obj_textline *)obj;
474
475 val = ofnode_read_prop(node, obj->name, &len);
476 if (len >= tline->max_chars)
477 return log_msg_ret("str", -ENOSPC);
478 strcpy(abuf_data(&tline->buf), val);
479 break;
480 }
Simon Glassb1cd32b2023-08-14 16:40:34 -0600481 case SCENEOBJT_MENU: {
482 struct scene_obj_menu *menu;
483 uint val;
484
485 if (ofnode_read_u32(node, obj->name, &val))
486 return log_msg_ret("rd", -ENOENT);
487 menu = (struct scene_obj_menu *)obj;
488 menu->cur_item_id = val;
489
490 break;
491 }
492 }
493
494 return 0;
495}
496
497int cedit_read_settings(struct expo *exp, oftree tree)
498{
499 struct cedit_iter_priv priv;
500 ofnode root, node;
501 int ret;
502
503 root = oftree_root(tree);
504 if (!ofnode_valid(root))
505 return log_msg_ret("roo", -ENOENT);
506 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
507 if (!ofnode_valid(node))
508 return log_msg_ret("pat", -ENOENT);
509
510 /* read in the items */
511 priv.node = node;
512 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
513 if (ret) {
514 log_debug("Failed to read settings (err=%d)\n", ret);
515 return log_msg_ret("set", ret);
516 }
517
518 return 0;
519}
Simon Glass237f3752023-08-14 16:40:35 -0600520
521static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
522{
523 const struct scene_obj_menu *menu;
524 struct cedit_iter_priv *priv = vpriv;
525 char name[80], var[60];
526 const char *str;
527 int val, ret;
528
Simon Glass53a0a2f2024-10-14 16:31:57 -0600529 if (obj->id < EXPOID_BASE_ID)
530 return 0;
531
Simon Glass237f3752023-08-14 16:40:35 -0600532 snprintf(var, sizeof(var), "c.%s", obj->name);
533
Simon Glassb7a64532023-10-01 19:13:24 -0600534 switch (obj->type) {
535 case SCENEOBJT_NONE:
536 case SCENEOBJT_IMAGE:
537 case SCENEOBJT_TEXT:
538 break;
539 case SCENEOBJT_MENU:
540 menu = (struct scene_obj_menu *)obj;
541 val = menu->cur_item_id;
Simon Glass237f3752023-08-14 16:40:35 -0600542
Simon Glassb7a64532023-10-01 19:13:24 -0600543 if (priv->verbose)
544 printf("%s=%d\n", var, val);
Simon Glass237f3752023-08-14 16:40:35 -0600545
Simon Glassb7a64532023-10-01 19:13:24 -0600546 ret = env_set_ulong(var, val);
547 if (ret)
548 return log_msg_ret("set", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600549
Simon Glassb7a64532023-10-01 19:13:24 -0600550 ret = get_cur_menuitem_text(menu, &str);
551 if (ret)
552 return log_msg_ret("mis", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600553
Simon Glassb7a64532023-10-01 19:13:24 -0600554 snprintf(name, sizeof(name), "c.%s-str", obj->name);
555 if (priv->verbose)
556 printf("%s=%s\n", name, str);
557
558 ret = env_set(name, str);
559 if (ret)
560 return log_msg_ret("st2", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600561
562 ret = get_cur_menuitem_val(menu, &val);
563 if (ret < 0)
564 return log_msg_ret("cur", ret);
565 snprintf(name, sizeof(name), "c.%s-value", obj->name);
566 if (priv->verbose)
567 printf("%s=%d\n", name, val);
568
Simon Glassb7a64532023-10-01 19:13:24 -0600569 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600570 case SCENEOBJT_TEXTLINE: {
571 const struct scene_obj_textline *tline;
572
573 tline = (struct scene_obj_textline *)obj;
574 str = abuf_data(&tline->buf);
575 ret = env_set(var, str);
576 if (ret)
577 return log_msg_ret("set", ret);
578
579 if (priv->verbose)
580 printf("%s=%s\n", var, str);
581
582 break;
583 }
Simon Glassb7a64532023-10-01 19:13:24 -0600584 }
Simon Glass237f3752023-08-14 16:40:35 -0600585
586 return 0;
587}
588
589int cedit_write_settings_env(struct expo *exp, bool verbose)
590{
591 struct cedit_iter_priv priv;
592 int ret;
593
594 /* write out the items */
595 priv.verbose = verbose;
596 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
597 if (ret) {
598 log_debug("Failed to write settings to env (err=%d)\n", ret);
599 return log_msg_ret("set", ret);
600 }
601
602 return 0;
603}
Simon Glass0f2e5a62023-08-14 16:40:36 -0600604
605static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
606{
607 struct cedit_iter_priv *priv = vpriv;
608 struct scene_obj_menu *menu;
609 char var[60];
Simon Glass2b91ca62023-08-14 16:40:37 -0600610 int val;
Simon Glass0f2e5a62023-08-14 16:40:36 -0600611
Simon Glass53a0a2f2024-10-14 16:31:57 -0600612 if (obj->id < EXPOID_BASE_ID)
613 return 0;
614
Simon Glass0f2e5a62023-08-14 16:40:36 -0600615 snprintf(var, sizeof(var), "c.%s", obj->name);
616
Simon Glassb7a64532023-10-01 19:13:24 -0600617 switch (obj->type) {
618 case SCENEOBJT_NONE:
619 case SCENEOBJT_IMAGE:
620 case SCENEOBJT_TEXT:
621 break;
622 case SCENEOBJT_MENU:
623 menu = (struct scene_obj_menu *)obj;
624 val = env_get_ulong(var, 10, 0);
625 if (priv->verbose)
626 printf("%s=%d\n", var, val);
627 if (!val)
628 return log_msg_ret("get", -ENOENT);
Simon Glass0f2e5a62023-08-14 16:40:36 -0600629
Simon Glassb7a64532023-10-01 19:13:24 -0600630 /*
631 * note that no validation is done here, to make sure the ID is
Simon Glass330aa1e2024-10-14 16:31:59 -0600632 * valid and actually points to a menu item
Simon Glassb7a64532023-10-01 19:13:24 -0600633 */
634 menu->cur_item_id = val;
635 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600636 case SCENEOBJT_TEXTLINE: {
637 const struct scene_obj_textline *tline;
638 const char *value;
639
640 tline = (struct scene_obj_textline *)obj;
641 value = env_get(var);
642 if (value && strlen(value) >= tline->max_chars)
643 return log_msg_ret("str", -ENOSPC);
644 if (!value)
645 value = "";
646 if (priv->verbose)
647 printf("%s=%s\n", var, value);
648 strcpy(abuf_data(&tline->buf), value);
649 break;
650 }
Simon Glassb7a64532023-10-01 19:13:24 -0600651 }
Simon Glass0f2e5a62023-08-14 16:40:36 -0600652
653 return 0;
654}
655
656int cedit_read_settings_env(struct expo *exp, bool verbose)
657{
658 struct cedit_iter_priv priv;
659 int ret;
660
661 /* write out the items */
662 priv.verbose = verbose;
663 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
664 if (ret) {
665 log_debug("Failed to read settings from env (err=%d)\n", ret);
666 return log_msg_ret("set", ret);
667 }
668
669 return 0;
670}
Simon Glass2b91ca62023-08-14 16:40:37 -0600671
Simon Glass2b91ca62023-08-14 16:40:37 -0600672static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
673{
674 const struct scene_obj_menu *menu;
675 struct cedit_iter_priv *priv = vpriv;
676 int val, ret;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600677 uint i;
Simon Glass2b91ca62023-08-14 16:40:37 -0600678
Simon Glass53a0a2f2024-10-14 16:31:57 -0600679 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass2b91ca62023-08-14 16:40:37 -0600680 return 0;
681
682 menu = (struct scene_obj_menu *)obj;
683 val = menu->cur_item_id;
684
Simon Glass6f3e87a2024-10-14 16:32:00 -0600685 ret = get_cur_menuitem_val(menu, &val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600686 if (ret < 0)
687 return log_msg_ret("cur", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600688 log_debug("%s: val=%d\n", menu->obj.name, val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600689
690 /* figure out where to place this item */
691 if (!obj->bit_length)
692 return log_msg_ret("len", -EINVAL);
693 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
694 return log_msg_ret("bit", -E2BIG);
695
Simon Glass6f3e87a2024-10-14 16:32:00 -0600696 for (i = 0; i < obj->bit_length; i++, val >>= 1) {
Simon Glass2b91ca62023-08-14 16:40:37 -0600697 uint bitnum = obj->start_bit + i;
698
699 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600700 if (val & 1)
Simon Glass2b91ca62023-08-14 16:40:37 -0600701 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
702 log_debug("bit %x %x %x\n", bitnum,
703 priv->mask[CMOS_BYTE(bitnum)],
704 priv->value[CMOS_BYTE(bitnum)]);
705 }
706
707 return 0;
708}
709
710int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
711 bool verbose)
712{
713 struct cedit_iter_priv priv;
714 int ret, i, count, first, last;
715
716 /* write out the items */
717 priv.mask = calloc(1, CMOS_MAX_BYTES);
718 if (!priv.mask)
719 return log_msg_ret("mas", -ENOMEM);
720 priv.value = calloc(1, CMOS_MAX_BYTES);
721 if (!priv.value) {
722 free(priv.mask);
723 return log_msg_ret("val", -ENOMEM);
724 }
725
726 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
727 if (ret) {
728 log_debug("Failed to write CMOS (err=%d)\n", ret);
729 ret = log_msg_ret("set", ret);
730 goto done;
731 }
732
733 /* write the data to the RTC */
Simon Glass330aa1e2024-10-14 16:31:59 -0600734 log_debug("Writing CMOS\n");
Simon Glass2b91ca62023-08-14 16:40:37 -0600735 first = CMOS_MAX_BYTES;
736 last = -1;
737 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
738 if (priv.mask[i]) {
739 log_debug("Write byte %x: %x\n", i, priv.value[i]);
740 ret = rtc_write8(dev, i, priv.value[i]);
741 if (ret) {
742 ret = log_msg_ret("wri", ret);
743 goto done;
744 }
745 count++;
746 first = min(first, i);
747 last = max(last, i);
748 }
749 }
750 if (verbose) {
751 printf("Write %d bytes from offset %x to %x\n", count, first,
752 last);
753 }
754
755done:
756 free(priv.mask);
757 free(priv.value);
758 return ret;
759}
Simon Glass4462fa32023-08-14 16:40:38 -0600760
761static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
762{
763 struct cedit_iter_priv *priv = vpriv;
764 const struct scene_menitem *mi;
765 struct scene_obj_menu *menu;
766 int val, ret;
767 uint i;
768
Simon Glass53a0a2f2024-10-14 16:31:57 -0600769 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass4462fa32023-08-14 16:40:38 -0600770 return 0;
771
772 menu = (struct scene_obj_menu *)obj;
773
774 /* figure out where to place this item */
775 if (!obj->bit_length)
776 return log_msg_ret("len", -EINVAL);
777 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
778 return log_msg_ret("bit", -E2BIG);
779
780 val = 0;
781 for (i = 0; i < obj->bit_length; i++) {
782 uint bitnum = obj->start_bit + i;
783 uint offset = CMOS_BYTE(bitnum);
784
785 /* read the byte if not already read */
786 if (!priv->mask[offset]) {
787 ret = rtc_read8(priv->dev, offset);
788 if (ret < 0)
789 return log_msg_ret("rea", ret);
790 priv->value[offset] = ret;
791
792 /* mark it as read */
793 priv->mask[offset] = 0xff;
794 }
795
796 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
797 val |= BIT(i);
798 log_debug("bit %x %x\n", bitnum, val);
799 }
800
801 /* update the current item */
Simon Glass330aa1e2024-10-14 16:31:59 -0600802 log_debug("look for menuitem value %d in menu %d\n", val, menu->obj.id);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600803 mi = scene_menuitem_find_val(menu, val);
Simon Glass4462fa32023-08-14 16:40:38 -0600804 if (!mi)
805 return log_msg_ret("seq", -ENOENT);
806
807 menu->cur_item_id = mi->id;
808 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
809
810 return 0;
811}
812
813int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
814 bool verbose)
815{
816 struct cedit_iter_priv priv;
817 int ret, i, count, first, last;
818
819 /* read in the items */
820 priv.mask = calloc(1, CMOS_MAX_BYTES);
821 if (!priv.mask)
822 return log_msg_ret("mas", -ENOMEM);
823 priv.value = calloc(1, CMOS_MAX_BYTES);
824 if (!priv.value) {
825 free(priv.mask);
826 return log_msg_ret("val", -ENOMEM);
827 }
828 priv.dev = dev;
829
830 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
831 if (ret) {
832 log_debug("Failed to read CMOS (err=%d)\n", ret);
833 ret = log_msg_ret("set", ret);
834 goto done;
835 }
836
Simon Glass330aa1e2024-10-14 16:31:59 -0600837 /* indicate what bytes were read from the RTC */
Simon Glass4462fa32023-08-14 16:40:38 -0600838 first = CMOS_MAX_BYTES;
839 last = -1;
840 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
841 if (priv.mask[i]) {
842 log_debug("Read byte %x: %x\n", i, priv.value[i]);
843 count++;
844 first = min(first, i);
845 last = max(last, i);
846 }
847 }
848 if (verbose) {
849 printf("Read %d bytes from offset %x to %x\n", count, first,
850 last);
851 }
852
853done:
854 free(priv.mask);
855 free(priv.value);
856 return ret;
857}