blob: 8faf230a9e2adb5fb6d29ef7ffcd36a00bca3f40 [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:
Simon Glass138a3972025-05-02 08:46:44 -060084 case SCENEOBJT_BOX:
Simon Glass5dc887d2025-05-02 08:46:46 -060085 case SCENEOBJT_TEXTEDIT:
Simon Glassb7a64532023-10-01 19:13:24 -060086 break;
87 case SCENEOBJT_MENU:
Simon Glassc8925112023-06-01 10:23:02 -060088 scene_obj_set_pos(scn, obj->id, 50, y);
Simon Glass377f18e2024-10-14 16:31:55 -060089 scene_menu_arrange(scn, &arr,
90 (struct scene_obj_menu *)obj);
Simon Glassc8925112023-06-01 10:23:02 -060091 y += 50;
Simon Glassb7a64532023-10-01 19:13:24 -060092 break;
Simon Glass23c3eb42023-10-01 19:13:37 -060093 case SCENEOBJT_TEXTLINE:
94 scene_obj_set_pos(scn, obj->id, 50, y);
Simon Glass377f18e2024-10-14 16:31:55 -060095 scene_textline_arrange(scn, &arr,
Simon Glass23c3eb42023-10-01 19:13:37 -060096 (struct scene_obj_textline *)obj);
97 y += 50;
98 break;
Simon Glassc8925112023-06-01 10:23:02 -060099 }
100 }
101
102 return 0;
103}
104
Simon Glass736bc0d2025-05-02 08:46:22 -0600105int cedit_prepare(struct expo *exp, struct udevice *vid_dev,
Simon Glass6a5af5f2023-08-14 16:40:27 -0600106 struct scene **scnp)
Simon Glassc8925112023-06-01 10:23:02 -0600107{
Simon Glass736bc0d2025-05-02 08:46:22 -0600108 struct udevice *dev = vid_dev;
Simon Glassc8925112023-06-01 10:23:02 -0600109 struct video_priv *vid_priv;
Simon Glassc8925112023-06-01 10:23:02 -0600110 struct scene *scn;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600111 uint scene_id;
Simon Glassc8925112023-06-01 10:23:02 -0600112 int ret;
113
Simon Glassc8925112023-06-01 10:23:02 -0600114 /* For now we only support a video console */
Simon Glassc8925112023-06-01 10:23:02 -0600115 ret = expo_set_display(exp, dev);
116 if (ret)
117 return log_msg_ret("dis", ret);
118
119 ret = expo_first_scene_id(exp);
120 if (ret < 0)
121 return log_msg_ret("scn", ret);
122 scene_id = ret;
123
124 ret = expo_set_scene_id(exp, scene_id);
125 if (ret)
126 return log_msg_ret("sid", ret);
127
128 exp->popup = true;
Simon Glass2d857262025-05-02 08:46:50 -0600129 exp->show_highlight = true;
Simon Glassc8925112023-06-01 10:23:02 -0600130
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 *scnp = scn;
147
148 return scene_id;
149}
150
Simon Glass14bd4132025-05-02 08:46:21 -0600151int cedit_do_action(struct expo *exp, struct scene *scn,
152 struct video_priv *vid_priv, struct expo_action *act)
153{
Simon Glass1b3388c2025-05-02 08:46:25 -0600154 int ret;
155
Simon Glass14bd4132025-05-02 08:46:21 -0600156 switch (act->type) {
157 case EXPOACT_NONE:
Simon Glass14bd4132025-05-02 08:46:21 -0600158 return -EAGAIN;
Simon Glass1b3388c2025-05-02 08:46:25 -0600159 case EXPOACT_POINT_ITEM:
160 ret = scene_menu_select_item(scn, scn->highlight_id,
161 act->select.id);
162 if (ret)
163 return log_msg_ret("cdp", ret);
164 break;
Simon Glass14bd4132025-05-02 08:46:21 -0600165 case EXPOACT_POINT_OBJ:
166 scene_set_highlight_id(scn, act->select.id);
167 cedit_arange(exp, vid_priv, scn->id);
168 break;
169 case EXPOACT_OPEN:
170 scene_set_open(scn, act->select.id, true);
171 cedit_arange(exp, vid_priv, scn->id);
172 switch (scn->highlight_id) {
173 case EXPOID_SAVE:
174 exp->done = true;
175 exp->save = true;
176 break;
177 case EXPOID_DISCARD:
178 exp->done = true;
179 break;
180 }
181 break;
182 case EXPOACT_CLOSE:
183 scene_set_open(scn, act->select.id, false);
184 cedit_arange(exp, vid_priv, scn->id);
185 break;
186 case EXPOACT_SELECT:
187 scene_set_open(scn, scn->highlight_id, false);
188 cedit_arange(exp, vid_priv, scn->id);
189 break;
190 case EXPOACT_QUIT:
191 log_debug("quitting\n");
192 exp->done = true;
193 break;
194 }
195
196 return 0;
197}
198
Simon Glass6a5af5f2023-08-14 16:40:27 -0600199int cedit_run(struct expo *exp)
200{
Simon Glass6a5af5f2023-08-14 16:40:27 -0600201 struct video_priv *vid_priv;
Simon Glass736bc0d2025-05-02 08:46:22 -0600202 struct udevice *dev;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600203 struct scene *scn;
Simon Glass736bc0d2025-05-02 08:46:22 -0600204 uint scene_id;
Simon Glass6a5af5f2023-08-14 16:40:27 -0600205 int ret;
206
Simon Glass736bc0d2025-05-02 08:46:22 -0600207 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
208 if (ret)
209 return log_msg_ret("vid", ret);
210 vid_priv = dev_get_uclass_priv(dev);
211
212 ret = cedit_prepare(exp, dev, &scn);
Simon Glass6a5af5f2023-08-14 16:40:27 -0600213 if (ret < 0)
214 return log_msg_ret("prep", ret);
215 scene_id = ret;
216
Simon Glass527d8642025-05-02 08:46:20 -0600217 exp->done = false;
218 exp->save = false;
Simon Glassc8925112023-06-01 10:23:02 -0600219 do {
220 struct expo_action act;
Simon Glassc8925112023-06-01 10:23:02 -0600221
Simon Glass137b4422025-05-02 08:46:16 -0600222 ret = expo_poll(exp, &act);
Simon Glass14bd4132025-05-02 08:46:21 -0600223 if (!ret)
224 cedit_do_action(exp, scn, vid_priv, &act);
225 else if (ret != -EAGAIN)
Simon Glass137b4422025-05-02 08:46:16 -0600226 return log_msg_ret("cep", ret);
Simon Glass527d8642025-05-02 08:46:20 -0600227 } while (!exp->done);
Simon Glassc8925112023-06-01 10:23:02 -0600228
229 if (ret)
230 return log_msg_ret("end", ret);
Simon Glass527d8642025-05-02 08:46:20 -0600231 if (!exp->save)
Simon Glass53a0a2f2024-10-14 16:31:57 -0600232 return -EACCES;
Simon Glassc8925112023-06-01 10:23:02 -0600233
234 return 0;
235}
Simon Glass28bf4352023-08-14 16:40:33 -0600236
237static int check_space(int ret, struct abuf *buf)
238{
239 if (ret == -FDT_ERR_NOSPACE) {
240 if (!abuf_realloc_inc(buf, CEDIT_SIZE_INC))
241 return log_msg_ret("spc", -ENOMEM);
242 ret = fdt_resize(abuf_data(buf), abuf_data(buf),
243 abuf_size(buf));
244 if (ret)
245 return log_msg_ret("res", -EFAULT);
246 }
247
248 return 0;
249}
250
Simon Glassdb6a0512023-10-01 19:13:23 -0600251/**
252 * get_cur_menuitem_text() - Get the text of the currently selected item
253 *
254 * Looks up the object for the current item, finds text object for it and looks
255 * up the string for that text
256 *
257 * @menu: Menu to look at
258 * @strp: Returns a pointer to the next
259 * Return: 0 if OK, -ENOENT if something was not found
260 */
Simon Glass237f3752023-08-14 16:40:35 -0600261static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
262 const char **strp)
263{
264 struct scene *scn = menu->obj.scene;
265 const struct scene_menitem *mi;
266 const struct scene_obj_txt *txt;
267 const char *str;
268
269 mi = scene_menuitem_find(menu, menu->cur_item_id);
270 if (!mi)
271 return log_msg_ret("mi", -ENOENT);
272
273 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
274 if (!txt)
275 return log_msg_ret("txt", -ENOENT);
276
Simon Glass9ef02aa2025-05-02 08:46:37 -0600277 str = expo_get_str(scn->expo, txt->gen.str_id);
Simon Glass237f3752023-08-14 16:40:35 -0600278 if (!str)
279 return log_msg_ret("str", -ENOENT);
280 *strp = str;
281
282 return 0;
283}
284
Simon Glass6f3e87a2024-10-14 16:32:00 -0600285/**
286 * get_cur_menuitem_val() - Get the value of a menu's current item
287 *
288 * Obtains the value of the current item in the menu. If no value, then
289 * enumerates the items of a menu (0, 1, 2) and returns the sequence number of
290 * the currently selected item. If the first item is selected, this returns 0;
291 * if the second, 1; etc.
292 *
293 * @menu: Menu to check
294 * @valp: Returns current-item value / sequence number
295 * Return: 0 on success, else -ve error value
296 */
297static int get_cur_menuitem_val(const struct scene_obj_menu *menu, int *valp)
298{
299 const struct scene_menitem *mi;
300 int seq;
301
302 seq = 0;
303 list_for_each_entry(mi, &menu->item_head, sibling) {
304 if (mi->id == menu->cur_item_id) {
305 *valp = mi->value == INT_MAX ? seq : mi->value;
306 return 0;
307 }
308 seq++;
309 }
310
311 return log_msg_ret("nf", -ENOENT);
312}
313
314/**
315 * write_dt_string() - Write a string to the devicetree, expanding if needed
316 *
317 * If this fails, it tries again after expanding the devicetree a little
318 *
319 * @buf: Buffer containing the devicetree
320 * @name: Property name to use
321 * @str: String value
322 * Return: 0 if OK, -EFAULT if something went horribly wrong
323 */
Simon Glassf78388b2023-10-01 19:13:28 -0600324static int write_dt_string(struct abuf *buf, const char *name, const char *str)
325{
326 int ret, i;
327
Simon Glassf78388b2023-10-01 19:13:28 -0600328 ret = -EAGAIN;
329 for (i = 0; ret && i < 2; i++) {
330 ret = fdt_property_string(abuf_data(buf), name, str);
331 if (!i) {
332 ret = check_space(ret, buf);
333 if (ret)
334 return log_msg_ret("rs2", -ENOMEM);
335 }
336 }
337
338 /* this should not happen */
339 if (ret)
340 return log_msg_ret("str", -EFAULT);
341
342 return 0;
343}
344
Simon Glass6f3e87a2024-10-14 16:32:00 -0600345/**
346 * write_dt_u32() - Write an int to the devicetree, expanding if needed
347 *
348 * If this fails, it tries again after expanding the devicetree a little
349 *
350 * @buf: Buffer containing the devicetree
351 * @name: Property name to use
352 * @lva: Integer value
353 * Return: 0 if OK, -EFAULT if something went horribly wrong
354 */
355static int write_dt_u32(struct abuf *buf, const char *name, uint val)
356{
357 int ret, i;
358
359 /* write the text of the current item */
360 ret = -EAGAIN;
361 for (i = 0; ret && i < 2; i++) {
362 ret = fdt_property_u32(abuf_data(buf), name, val);
363 if (!i) {
364 ret = check_space(ret, buf);
365 if (ret)
366 return log_msg_ret("rs2", -ENOMEM);
367 }
368 }
369
370 /* this should not happen */
371 if (ret)
372 return log_msg_ret("str", -EFAULT);
373
374 return 0;
375}
376
Simon Glass28bf4352023-08-14 16:40:33 -0600377static int h_write_settings(struct scene_obj *obj, void *vpriv)
378{
379 struct cedit_iter_priv *priv = vpriv;
380 struct abuf *buf = priv->buf;
Simon Glass23c3eb42023-10-01 19:13:37 -0600381 int ret;
Simon Glass28bf4352023-08-14 16:40:33 -0600382
383 switch (obj->type) {
384 case SCENEOBJT_NONE:
385 case SCENEOBJT_IMAGE:
386 case SCENEOBJT_TEXT:
Simon Glass138a3972025-05-02 08:46:44 -0600387 case SCENEOBJT_BOX:
Simon Glass5dc887d2025-05-02 08:46:46 -0600388 case SCENEOBJT_TEXTEDIT:
Simon Glass28bf4352023-08-14 16:40:33 -0600389 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600390 case SCENEOBJT_TEXTLINE: {
391 const struct scene_obj_textline *tline;
392
393 tline = (struct scene_obj_textline *)obj;
394 ret = write_dt_string(buf, obj->name, abuf_data(&tline->buf));
395 if (ret)
396 return log_msg_ret("wr2", ret);
397 break;
398 }
Simon Glass28bf4352023-08-14 16:40:33 -0600399 case SCENEOBJT_MENU: {
400 const struct scene_obj_menu *menu;
Simon Glass28bf4352023-08-14 16:40:33 -0600401 const char *str;
402 char name[80];
Simon Glass6f3e87a2024-10-14 16:32:00 -0600403 int val;
Simon Glass28bf4352023-08-14 16:40:33 -0600404
Simon Glassf78388b2023-10-01 19:13:28 -0600405 /* write the ID of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600406 menu = (struct scene_obj_menu *)obj;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600407 ret = write_dt_u32(buf, obj->name, menu->cur_item_id);
Simon Glass28bf4352023-08-14 16:40:33 -0600408 if (ret)
Simon Glass6f3e87a2024-10-14 16:32:00 -0600409 return log_msg_ret("wrt", ret);
410
411 snprintf(name, sizeof(name), "%s-value", obj->name);
412 ret = get_cur_menuitem_val(menu, &val);
413 if (ret < 0)
414 return log_msg_ret("cur", ret);
415 ret = write_dt_u32(buf, name, val);
416 if (ret)
417 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600418
Simon Glass237f3752023-08-14 16:40:35 -0600419 ret = get_cur_menuitem_text(menu, &str);
420 if (ret)
421 return log_msg_ret("mis", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600422
Simon Glassf78388b2023-10-01 19:13:28 -0600423 /* write the text of the current item */
Simon Glass28bf4352023-08-14 16:40:33 -0600424 snprintf(name, sizeof(name), "%s-str", obj->name);
Simon Glassf78388b2023-10-01 19:13:28 -0600425 ret = write_dt_string(buf, name, str);
Simon Glass28bf4352023-08-14 16:40:33 -0600426 if (ret)
Simon Glassf78388b2023-10-01 19:13:28 -0600427 return log_msg_ret("wr2", ret);
Simon Glass28bf4352023-08-14 16:40:33 -0600428
429 break;
430 }
431 }
432
433 return 0;
434}
435
436int cedit_write_settings(struct expo *exp, struct abuf *buf)
437{
438 struct cedit_iter_priv priv;
439 void *fdt;
440 int ret;
441
Simon Glass6651e942025-05-01 07:37:01 -0600442 if (!abuf_init_size(buf, CEDIT_SIZE_INC))
Simon Glass28bf4352023-08-14 16:40:33 -0600443 return log_msg_ret("buf", -ENOMEM);
444
445 fdt = abuf_data(buf);
446 ret = fdt_create(fdt, abuf_size(buf));
447 if (!ret)
448 ret = fdt_finish_reservemap(fdt);
449 if (!ret)
450 ret = fdt_begin_node(fdt, "");
451 if (!ret)
452 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
453 if (ret) {
454 log_debug("Failed to start FDT (err=%d)\n", ret);
455 return log_msg_ret("sta", -EINVAL);
456 }
457
458 /* write out the items */
459 priv.buf = buf;
460 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
461 if (ret) {
462 log_debug("Failed to write settings (err=%d)\n", ret);
463 return log_msg_ret("set", ret);
464 }
465
466 ret = fdt_end_node(fdt);
467 if (!ret)
468 ret = fdt_end_node(fdt);
469 if (!ret)
470 ret = fdt_finish(fdt);
471 if (ret) {
472 log_debug("Failed to finish FDT (err=%d)\n", ret);
473 return log_msg_ret("fin", -EINVAL);
474 }
475
476 return 0;
477}
Simon Glassb1cd32b2023-08-14 16:40:34 -0600478
479static int h_read_settings(struct scene_obj *obj, void *vpriv)
480{
481 struct cedit_iter_priv *priv = vpriv;
482 ofnode node = priv->node;
483
484 switch (obj->type) {
485 case SCENEOBJT_NONE:
486 case SCENEOBJT_IMAGE:
487 case SCENEOBJT_TEXT:
Simon Glass138a3972025-05-02 08:46:44 -0600488 case SCENEOBJT_BOX:
Simon Glass5dc887d2025-05-02 08:46:46 -0600489 case SCENEOBJT_TEXTEDIT:
Simon Glassb1cd32b2023-08-14 16:40:34 -0600490 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600491 case SCENEOBJT_TEXTLINE: {
492 const struct scene_obj_textline *tline;
493 const char *val;
494 int len;
495
496 tline = (struct scene_obj_textline *)obj;
497
498 val = ofnode_read_prop(node, obj->name, &len);
499 if (len >= tline->max_chars)
500 return log_msg_ret("str", -ENOSPC);
501 strcpy(abuf_data(&tline->buf), val);
502 break;
503 }
Simon Glassb1cd32b2023-08-14 16:40:34 -0600504 case SCENEOBJT_MENU: {
505 struct scene_obj_menu *menu;
506 uint val;
507
508 if (ofnode_read_u32(node, obj->name, &val))
509 return log_msg_ret("rd", -ENOENT);
510 menu = (struct scene_obj_menu *)obj;
511 menu->cur_item_id = val;
512
513 break;
514 }
515 }
516
517 return 0;
518}
519
520int cedit_read_settings(struct expo *exp, oftree tree)
521{
522 struct cedit_iter_priv priv;
523 ofnode root, node;
524 int ret;
525
526 root = oftree_root(tree);
527 if (!ofnode_valid(root))
528 return log_msg_ret("roo", -ENOENT);
529 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
530 if (!ofnode_valid(node))
531 return log_msg_ret("pat", -ENOENT);
532
533 /* read in the items */
534 priv.node = node;
535 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
536 if (ret) {
537 log_debug("Failed to read settings (err=%d)\n", ret);
538 return log_msg_ret("set", ret);
539 }
540
541 return 0;
542}
Simon Glass237f3752023-08-14 16:40:35 -0600543
544static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
545{
546 const struct scene_obj_menu *menu;
547 struct cedit_iter_priv *priv = vpriv;
548 char name[80], var[60];
549 const char *str;
550 int val, ret;
551
Simon Glass53a0a2f2024-10-14 16:31:57 -0600552 if (obj->id < EXPOID_BASE_ID)
553 return 0;
554
Simon Glass237f3752023-08-14 16:40:35 -0600555 snprintf(var, sizeof(var), "c.%s", obj->name);
556
Simon Glassb7a64532023-10-01 19:13:24 -0600557 switch (obj->type) {
558 case SCENEOBJT_NONE:
559 case SCENEOBJT_IMAGE:
560 case SCENEOBJT_TEXT:
Simon Glass138a3972025-05-02 08:46:44 -0600561 case SCENEOBJT_BOX:
Simon Glass5dc887d2025-05-02 08:46:46 -0600562 case SCENEOBJT_TEXTEDIT:
Simon Glassb7a64532023-10-01 19:13:24 -0600563 break;
564 case SCENEOBJT_MENU:
565 menu = (struct scene_obj_menu *)obj;
566 val = menu->cur_item_id;
Simon Glass237f3752023-08-14 16:40:35 -0600567
Simon Glassb7a64532023-10-01 19:13:24 -0600568 if (priv->verbose)
569 printf("%s=%d\n", var, val);
Simon Glass237f3752023-08-14 16:40:35 -0600570
Simon Glassb7a64532023-10-01 19:13:24 -0600571 ret = env_set_ulong(var, val);
572 if (ret)
573 return log_msg_ret("set", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600574
Simon Glassb7a64532023-10-01 19:13:24 -0600575 ret = get_cur_menuitem_text(menu, &str);
576 if (ret)
577 return log_msg_ret("mis", ret);
Simon Glass237f3752023-08-14 16:40:35 -0600578
Simon Glassb7a64532023-10-01 19:13:24 -0600579 snprintf(name, sizeof(name), "c.%s-str", obj->name);
580 if (priv->verbose)
581 printf("%s=%s\n", name, str);
582
583 ret = env_set(name, str);
584 if (ret)
585 return log_msg_ret("st2", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600586
587 ret = get_cur_menuitem_val(menu, &val);
588 if (ret < 0)
589 return log_msg_ret("cur", ret);
590 snprintf(name, sizeof(name), "c.%s-value", obj->name);
591 if (priv->verbose)
592 printf("%s=%d\n", name, val);
593
Simon Glassb7a64532023-10-01 19:13:24 -0600594 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600595 case SCENEOBJT_TEXTLINE: {
596 const struct scene_obj_textline *tline;
597
598 tline = (struct scene_obj_textline *)obj;
599 str = abuf_data(&tline->buf);
600 ret = env_set(var, str);
601 if (ret)
602 return log_msg_ret("set", ret);
603
604 if (priv->verbose)
605 printf("%s=%s\n", var, str);
606
607 break;
608 }
Simon Glassb7a64532023-10-01 19:13:24 -0600609 }
Simon Glass237f3752023-08-14 16:40:35 -0600610
611 return 0;
612}
613
614int cedit_write_settings_env(struct expo *exp, bool verbose)
615{
616 struct cedit_iter_priv priv;
617 int ret;
618
619 /* write out the items */
620 priv.verbose = verbose;
621 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
622 if (ret) {
623 log_debug("Failed to write settings to env (err=%d)\n", ret);
624 return log_msg_ret("set", ret);
625 }
626
627 return 0;
628}
Simon Glass0f2e5a62023-08-14 16:40:36 -0600629
630static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
631{
632 struct cedit_iter_priv *priv = vpriv;
633 struct scene_obj_menu *menu;
634 char var[60];
Simon Glass2b91ca62023-08-14 16:40:37 -0600635 int val;
Simon Glass0f2e5a62023-08-14 16:40:36 -0600636
Simon Glass53a0a2f2024-10-14 16:31:57 -0600637 if (obj->id < EXPOID_BASE_ID)
638 return 0;
639
Simon Glass0f2e5a62023-08-14 16:40:36 -0600640 snprintf(var, sizeof(var), "c.%s", obj->name);
641
Simon Glassb7a64532023-10-01 19:13:24 -0600642 switch (obj->type) {
643 case SCENEOBJT_NONE:
644 case SCENEOBJT_IMAGE:
645 case SCENEOBJT_TEXT:
Simon Glass138a3972025-05-02 08:46:44 -0600646 case SCENEOBJT_BOX:
Simon Glass5dc887d2025-05-02 08:46:46 -0600647 case SCENEOBJT_TEXTEDIT:
Simon Glassb7a64532023-10-01 19:13:24 -0600648 break;
649 case SCENEOBJT_MENU:
650 menu = (struct scene_obj_menu *)obj;
651 val = env_get_ulong(var, 10, 0);
652 if (priv->verbose)
653 printf("%s=%d\n", var, val);
654 if (!val)
655 return log_msg_ret("get", -ENOENT);
Simon Glass0f2e5a62023-08-14 16:40:36 -0600656
Simon Glassb7a64532023-10-01 19:13:24 -0600657 /*
658 * note that no validation is done here, to make sure the ID is
Simon Glass330aa1e2024-10-14 16:31:59 -0600659 * valid and actually points to a menu item
Simon Glassb7a64532023-10-01 19:13:24 -0600660 */
661 menu->cur_item_id = val;
662 break;
Simon Glass23c3eb42023-10-01 19:13:37 -0600663 case SCENEOBJT_TEXTLINE: {
664 const struct scene_obj_textline *tline;
665 const char *value;
666
667 tline = (struct scene_obj_textline *)obj;
668 value = env_get(var);
669 if (value && strlen(value) >= tline->max_chars)
670 return log_msg_ret("str", -ENOSPC);
671 if (!value)
672 value = "";
673 if (priv->verbose)
674 printf("%s=%s\n", var, value);
675 strcpy(abuf_data(&tline->buf), value);
676 break;
677 }
Simon Glassb7a64532023-10-01 19:13:24 -0600678 }
Simon Glass0f2e5a62023-08-14 16:40:36 -0600679
680 return 0;
681}
682
683int cedit_read_settings_env(struct expo *exp, bool verbose)
684{
685 struct cedit_iter_priv priv;
686 int ret;
687
688 /* write out the items */
689 priv.verbose = verbose;
690 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
691 if (ret) {
692 log_debug("Failed to read settings from env (err=%d)\n", ret);
693 return log_msg_ret("set", ret);
694 }
695
696 return 0;
697}
Simon Glass2b91ca62023-08-14 16:40:37 -0600698
Simon Glass2b91ca62023-08-14 16:40:37 -0600699static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
700{
701 const struct scene_obj_menu *menu;
702 struct cedit_iter_priv *priv = vpriv;
703 int val, ret;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600704 uint i;
Simon Glass2b91ca62023-08-14 16:40:37 -0600705
Simon Glass53a0a2f2024-10-14 16:31:57 -0600706 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass2b91ca62023-08-14 16:40:37 -0600707 return 0;
708
709 menu = (struct scene_obj_menu *)obj;
710 val = menu->cur_item_id;
711
Simon Glass6f3e87a2024-10-14 16:32:00 -0600712 ret = get_cur_menuitem_val(menu, &val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600713 if (ret < 0)
714 return log_msg_ret("cur", ret);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600715 log_debug("%s: val=%d\n", menu->obj.name, val);
Simon Glass2b91ca62023-08-14 16:40:37 -0600716
717 /* figure out where to place this item */
718 if (!obj->bit_length)
719 return log_msg_ret("len", -EINVAL);
720 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
721 return log_msg_ret("bit", -E2BIG);
722
Simon Glass6f3e87a2024-10-14 16:32:00 -0600723 for (i = 0; i < obj->bit_length; i++, val >>= 1) {
Simon Glass2b91ca62023-08-14 16:40:37 -0600724 uint bitnum = obj->start_bit + i;
725
726 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600727 if (val & 1)
Simon Glass2b91ca62023-08-14 16:40:37 -0600728 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
729 log_debug("bit %x %x %x\n", bitnum,
730 priv->mask[CMOS_BYTE(bitnum)],
731 priv->value[CMOS_BYTE(bitnum)]);
732 }
733
734 return 0;
735}
736
737int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
738 bool verbose)
739{
740 struct cedit_iter_priv priv;
741 int ret, i, count, first, last;
742
743 /* write out the items */
744 priv.mask = calloc(1, CMOS_MAX_BYTES);
745 if (!priv.mask)
746 return log_msg_ret("mas", -ENOMEM);
747 priv.value = calloc(1, CMOS_MAX_BYTES);
748 if (!priv.value) {
749 free(priv.mask);
750 return log_msg_ret("val", -ENOMEM);
751 }
752
753 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
754 if (ret) {
755 log_debug("Failed to write CMOS (err=%d)\n", ret);
756 ret = log_msg_ret("set", ret);
757 goto done;
758 }
759
760 /* write the data to the RTC */
Simon Glass330aa1e2024-10-14 16:31:59 -0600761 log_debug("Writing CMOS\n");
Simon Glass2b91ca62023-08-14 16:40:37 -0600762 first = CMOS_MAX_BYTES;
763 last = -1;
764 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
765 if (priv.mask[i]) {
766 log_debug("Write byte %x: %x\n", i, priv.value[i]);
767 ret = rtc_write8(dev, i, priv.value[i]);
768 if (ret) {
769 ret = log_msg_ret("wri", ret);
770 goto done;
771 }
772 count++;
773 first = min(first, i);
774 last = max(last, i);
775 }
776 }
777 if (verbose) {
778 printf("Write %d bytes from offset %x to %x\n", count, first,
779 last);
780 }
781
782done:
783 free(priv.mask);
784 free(priv.value);
785 return ret;
786}
Simon Glass4462fa32023-08-14 16:40:38 -0600787
788static int h_read_settings_cmos(struct scene_obj *obj, void *vpriv)
789{
790 struct cedit_iter_priv *priv = vpriv;
791 const struct scene_menitem *mi;
792 struct scene_obj_menu *menu;
793 int val, ret;
794 uint i;
795
Simon Glass53a0a2f2024-10-14 16:31:57 -0600796 if (obj->type != SCENEOBJT_MENU || obj->id < EXPOID_BASE_ID)
Simon Glass4462fa32023-08-14 16:40:38 -0600797 return 0;
798
799 menu = (struct scene_obj_menu *)obj;
800
801 /* figure out where to place this item */
802 if (!obj->bit_length)
803 return log_msg_ret("len", -EINVAL);
804 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
805 return log_msg_ret("bit", -E2BIG);
806
807 val = 0;
808 for (i = 0; i < obj->bit_length; i++) {
809 uint bitnum = obj->start_bit + i;
810 uint offset = CMOS_BYTE(bitnum);
811
812 /* read the byte if not already read */
813 if (!priv->mask[offset]) {
814 ret = rtc_read8(priv->dev, offset);
815 if (ret < 0)
816 return log_msg_ret("rea", ret);
817 priv->value[offset] = ret;
818
819 /* mark it as read */
820 priv->mask[offset] = 0xff;
821 }
822
823 if (priv->value[offset] & BIT(CMOS_BIT(bitnum)))
824 val |= BIT(i);
825 log_debug("bit %x %x\n", bitnum, val);
826 }
827
828 /* update the current item */
Simon Glass330aa1e2024-10-14 16:31:59 -0600829 log_debug("look for menuitem value %d in menu %d\n", val, menu->obj.id);
Simon Glass6f3e87a2024-10-14 16:32:00 -0600830 mi = scene_menuitem_find_val(menu, val);
Simon Glass4462fa32023-08-14 16:40:38 -0600831 if (!mi)
832 return log_msg_ret("seq", -ENOENT);
833
834 menu->cur_item_id = mi->id;
835 log_debug("Update menu %d cur_item_id %d\n", menu->obj.id, mi->id);
836
837 return 0;
838}
839
840int cedit_read_settings_cmos(struct expo *exp, struct udevice *dev,
841 bool verbose)
842{
843 struct cedit_iter_priv priv;
844 int ret, i, count, first, last;
845
846 /* read in the items */
847 priv.mask = calloc(1, CMOS_MAX_BYTES);
848 if (!priv.mask)
849 return log_msg_ret("mas", -ENOMEM);
850 priv.value = calloc(1, CMOS_MAX_BYTES);
851 if (!priv.value) {
852 free(priv.mask);
853 return log_msg_ret("val", -ENOMEM);
854 }
855 priv.dev = dev;
856
857 ret = expo_iter_scene_objs(exp, h_read_settings_cmos, &priv);
858 if (ret) {
859 log_debug("Failed to read CMOS (err=%d)\n", ret);
860 ret = log_msg_ret("set", ret);
861 goto done;
862 }
863
Simon Glass330aa1e2024-10-14 16:31:59 -0600864 /* indicate what bytes were read from the RTC */
Simon Glass4462fa32023-08-14 16:40:38 -0600865 first = CMOS_MAX_BYTES;
866 last = -1;
867 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
868 if (priv.mask[i]) {
869 log_debug("Read byte %x: %x\n", i, priv.value[i]);
870 count++;
871 first = min(first, i);
872 last = max(last, i);
873 }
874 }
875 if (verbose) {
876 printf("Read %d bytes from offset %x to %x\n", count, first,
877 last);
878 }
879
880done:
881 free(priv.mask);
882 free(priv.value);
883 return ret;
884}