blob: 725745aba55d1e80fed650d96500a99501f09f2d [file] [log] [blame]
Simon Glassa0874dc2023-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 Glass8d0f8902023-08-14 16:40:27 -06009#define LOG_CATEGORY LOGC_EXPO
10
Simon Glassa0874dc2023-06-01 10:23:02 -060011#include <common.h>
Simon Glass2dee81f2023-08-14 16:40:33 -060012#include <abuf.h>
Simon Glass040b0462023-08-14 16:40:25 -060013#include <cedit.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060014#include <cli.h>
15#include <dm.h>
Simon Glassfc9c0e02023-08-14 16:40:35 -060016#include <env.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060017#include <expo.h>
Simon Glasseb6c71b2023-08-14 16:40:37 -060018#include <malloc.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060019#include <menu.h>
Simon Glasseb6c71b2023-08-14 16:40:37 -060020#include <rtc.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060021#include <video.h>
22#include <linux/delay.h>
23#include "scene_internal.h"
24
Simon Glasseb6c71b2023-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 Glass2dee81f2023-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 Glass472317c2023-08-14 16:40:34 -060037 * @node: Node to read from when reading settings from devicetree
Simon Glassfc9c0e02023-08-14 16:40:35 -060038 * @verbose: true to show writing to environment variables
Simon Glasseb6c71b2023-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 Glass2dee81f2023-08-14 16:40:33 -060042 */
43struct cedit_iter_priv {
44 struct abuf *buf;
Simon Glass472317c2023-08-14 16:40:34 -060045 ofnode node;
Simon Glassfc9c0e02023-08-14 16:40:35 -060046 bool verbose;
Simon Glasseb6c71b2023-08-14 16:40:37 -060047 u8 *mask;
48 u8 *value;
Simon Glass2dee81f2023-08-14 16:40:33 -060049};
50
Simon Glassa0874dc2023-06-01 10:23:02 -060051int cedit_arange(struct expo *exp, struct video_priv *vpriv, uint scene_id)
52{
53 struct scene_obj_txt *txt;
54 struct scene_obj *obj;
55 struct scene *scn;
56 int y;
57
58 scn = expo_lookup_scene_id(exp, scene_id);
59 if (!scn)
60 return log_msg_ret("scn", -ENOENT);
61
62 txt = scene_obj_find_by_name(scn, "prompt");
63 if (txt)
64 scene_obj_set_pos(scn, txt->obj.id, 0, vpriv->ysize - 50);
65
66 txt = scene_obj_find_by_name(scn, "title");
67 if (txt)
68 scene_obj_set_pos(scn, txt->obj.id, 200, 10);
69
70 y = 100;
71 list_for_each_entry(obj, &scn->obj_head, sibling) {
72 if (obj->type == SCENEOBJT_MENU) {
73 scene_obj_set_pos(scn, obj->id, 50, y);
74 scene_menu_arrange(scn, (struct scene_obj_menu *)obj);
75 y += 50;
76 }
77 }
78
79 return 0;
80}
81
Simon Glass8d0f8902023-08-14 16:40:27 -060082int cedit_prepare(struct expo *exp, struct video_priv **vid_privp,
83 struct scene **scnp)
Simon Glassa0874dc2023-06-01 10:23:02 -060084{
Simon Glassa0874dc2023-06-01 10:23:02 -060085 struct video_priv *vid_priv;
Simon Glassa0874dc2023-06-01 10:23:02 -060086 struct udevice *dev;
87 struct scene *scn;
Simon Glass8d0f8902023-08-14 16:40:27 -060088 uint scene_id;
Simon Glassa0874dc2023-06-01 10:23:02 -060089 int ret;
90
Simon Glassa0874dc2023-06-01 10:23:02 -060091 /* For now we only support a video console */
92 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
93 if (ret)
94 return log_msg_ret("vid", ret);
95 ret = expo_set_display(exp, dev);
96 if (ret)
97 return log_msg_ret("dis", ret);
98
99 ret = expo_first_scene_id(exp);
100 if (ret < 0)
101 return log_msg_ret("scn", ret);
102 scene_id = ret;
103
104 ret = expo_set_scene_id(exp, scene_id);
105 if (ret)
106 return log_msg_ret("sid", ret);
107
108 exp->popup = true;
109
110 /* This is not supported for now */
111 if (0)
112 expo_set_text_mode(exp, true);
113
114 vid_priv = dev_get_uclass_priv(dev);
115
116 scn = expo_lookup_scene_id(exp, scene_id);
117 scene_highlight_first(scn);
118
119 cedit_arange(exp, vid_priv, scene_id);
120
121 ret = expo_calc_dims(exp);
122 if (ret)
123 return log_msg_ret("dim", ret);
124
Simon Glass8d0f8902023-08-14 16:40:27 -0600125 *vid_privp = vid_priv;
126 *scnp = scn;
127
128 return scene_id;
129}
130
131int cedit_run(struct expo *exp)
132{
133 struct cli_ch_state s_cch, *cch = &s_cch;
134 struct video_priv *vid_priv;
135 uint scene_id;
136 struct scene *scn;
137 bool done;
138 int ret;
139
140 cli_ch_init(cch);
141 ret = cedit_prepare(exp, &vid_priv, &scn);
142 if (ret < 0)
143 return log_msg_ret("prep", ret);
144 scene_id = ret;
145
Simon Glassa0874dc2023-06-01 10:23:02 -0600146 done = false;
147 do {
148 struct expo_action act;
149 int ichar, key;
150
151 ret = expo_render(exp);
152 if (ret)
153 break;
154
155 ichar = cli_ch_process(cch, 0);
156 if (!ichar) {
157 while (!ichar && !tstc()) {
158 schedule();
159 mdelay(2);
160 ichar = cli_ch_process(cch, -ETIMEDOUT);
161 }
162 if (!ichar) {
163 ichar = getchar();
164 ichar = cli_ch_process(cch, ichar);
165 }
166 }
167
168 key = 0;
169 if (ichar) {
170 key = bootmenu_conv_key(ichar);
171 if (key == BKEY_NONE)
172 key = ichar;
173 }
174 if (!key)
175 continue;
176
177 ret = expo_send_key(exp, key);
178 if (ret)
179 break;
180
181 ret = expo_action_get(exp, &act);
182 if (!ret) {
183 switch (act.type) {
184 case EXPOACT_POINT_OBJ:
185 scene_set_highlight_id(scn, act.select.id);
186 cedit_arange(exp, vid_priv, scene_id);
187 break;
188 case EXPOACT_OPEN:
189 scene_set_open(scn, act.select.id, true);
190 cedit_arange(exp, vid_priv, scene_id);
191 break;
192 case EXPOACT_CLOSE:
193 scene_set_open(scn, act.select.id, false);
194 cedit_arange(exp, vid_priv, scene_id);
195 break;
196 case EXPOACT_SELECT:
197 scene_set_open(scn, scn->highlight_id, false);
198 cedit_arange(exp, vid_priv, scene_id);
199 break;
200 case EXPOACT_QUIT:
201 log_debug("quitting\n");
202 done = true;
203 break;
204 default:
205 break;
206 }
207 }
208 } while (!done);
209
210 if (ret)
211 return log_msg_ret("end", ret);
212
213 return 0;
214}
Simon Glass2dee81f2023-08-14 16:40:33 -0600215
216static int check_space(int ret, struct abuf *buf)
217{
218 if (ret == -FDT_ERR_NOSPACE) {
219 if (!abuf_realloc_inc(buf, CEDIT_SIZE_INC))
220 return log_msg_ret("spc", -ENOMEM);
221 ret = fdt_resize(abuf_data(buf), abuf_data(buf),
222 abuf_size(buf));
223 if (ret)
224 return log_msg_ret("res", -EFAULT);
225 }
226
227 return 0;
228}
229
Simon Glassfc9c0e02023-08-14 16:40:35 -0600230static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
231 const char **strp)
232{
233 struct scene *scn = menu->obj.scene;
234 const struct scene_menitem *mi;
235 const struct scene_obj_txt *txt;
236 const char *str;
237
238 mi = scene_menuitem_find(menu, menu->cur_item_id);
239 if (!mi)
240 return log_msg_ret("mi", -ENOENT);
241
242 txt = scene_obj_find(scn, mi->label_id, SCENEOBJT_TEXT);
243 if (!txt)
244 return log_msg_ret("txt", -ENOENT);
245
246 str = expo_get_str(scn->expo, txt->str_id);
247 if (!str)
248 return log_msg_ret("str", -ENOENT);
249 *strp = str;
250
251 return 0;
252}
253
Simon Glass2dee81f2023-08-14 16:40:33 -0600254static int h_write_settings(struct scene_obj *obj, void *vpriv)
255{
256 struct cedit_iter_priv *priv = vpriv;
257 struct abuf *buf = priv->buf;
258
259 switch (obj->type) {
260 case SCENEOBJT_NONE:
261 case SCENEOBJT_IMAGE:
262 case SCENEOBJT_TEXT:
263 break;
264 case SCENEOBJT_MENU: {
265 const struct scene_obj_menu *menu;
Simon Glass2dee81f2023-08-14 16:40:33 -0600266 const char *str;
267 char name[80];
268 int ret, i;
269
270 menu = (struct scene_obj_menu *)obj;
271 ret = -EAGAIN;
272 for (i = 0; ret && i < 2; i++) {
273 ret = fdt_property_u32(abuf_data(buf), obj->name,
274 menu->cur_item_id);
275 if (!i) {
276 ret = check_space(ret, buf);
277 if (ret)
278 return log_msg_ret("res", -ENOMEM);
279 }
280 }
281 /* this should not happen */
282 if (ret)
283 return log_msg_ret("wrt", -EFAULT);
284
Simon Glassfc9c0e02023-08-14 16:40:35 -0600285 ret = get_cur_menuitem_text(menu, &str);
286 if (ret)
287 return log_msg_ret("mis", ret);
Simon Glass2dee81f2023-08-14 16:40:33 -0600288
289 snprintf(name, sizeof(name), "%s-str", obj->name);
290 ret = -EAGAIN;
291 for (i = 0; ret && i < 2; i++) {
292 ret = fdt_property_string(abuf_data(buf), name, str);
293 if (!i) {
294 ret = check_space(ret, buf);
295 if (ret)
296 return log_msg_ret("rs2", -ENOMEM);
297 }
298 }
299
300 /* this should not happen */
301 if (ret)
302 return log_msg_ret("wr2", -EFAULT);
303
304 break;
305 }
306 }
307
308 return 0;
309}
310
311int cedit_write_settings(struct expo *exp, struct abuf *buf)
312{
313 struct cedit_iter_priv priv;
314 void *fdt;
315 int ret;
316
317 abuf_init(buf);
318 if (!abuf_realloc(buf, CEDIT_SIZE_INC))
319 return log_msg_ret("buf", -ENOMEM);
320
321 fdt = abuf_data(buf);
322 ret = fdt_create(fdt, abuf_size(buf));
323 if (!ret)
324 ret = fdt_finish_reservemap(fdt);
325 if (!ret)
326 ret = fdt_begin_node(fdt, "");
327 if (!ret)
328 ret = fdt_begin_node(fdt, CEDIT_NODE_NAME);
329 if (ret) {
330 log_debug("Failed to start FDT (err=%d)\n", ret);
331 return log_msg_ret("sta", -EINVAL);
332 }
333
334 /* write out the items */
335 priv.buf = buf;
336 ret = expo_iter_scene_objs(exp, h_write_settings, &priv);
337 if (ret) {
338 log_debug("Failed to write settings (err=%d)\n", ret);
339 return log_msg_ret("set", ret);
340 }
341
342 ret = fdt_end_node(fdt);
343 if (!ret)
344 ret = fdt_end_node(fdt);
345 if (!ret)
346 ret = fdt_finish(fdt);
347 if (ret) {
348 log_debug("Failed to finish FDT (err=%d)\n", ret);
349 return log_msg_ret("fin", -EINVAL);
350 }
351
352 return 0;
353}
Simon Glass472317c2023-08-14 16:40:34 -0600354
355static int h_read_settings(struct scene_obj *obj, void *vpriv)
356{
357 struct cedit_iter_priv *priv = vpriv;
358 ofnode node = priv->node;
359
360 switch (obj->type) {
361 case SCENEOBJT_NONE:
362 case SCENEOBJT_IMAGE:
363 case SCENEOBJT_TEXT:
364 break;
365 case SCENEOBJT_MENU: {
366 struct scene_obj_menu *menu;
367 uint val;
368
369 if (ofnode_read_u32(node, obj->name, &val))
370 return log_msg_ret("rd", -ENOENT);
371 menu = (struct scene_obj_menu *)obj;
372 menu->cur_item_id = val;
373
374 break;
375 }
376 }
377
378 return 0;
379}
380
381int cedit_read_settings(struct expo *exp, oftree tree)
382{
383 struct cedit_iter_priv priv;
384 ofnode root, node;
385 int ret;
386
387 root = oftree_root(tree);
388 if (!ofnode_valid(root))
389 return log_msg_ret("roo", -ENOENT);
390 node = ofnode_find_subnode(root, CEDIT_NODE_NAME);
391 if (!ofnode_valid(node))
392 return log_msg_ret("pat", -ENOENT);
393
394 /* read in the items */
395 priv.node = node;
396 ret = expo_iter_scene_objs(exp, h_read_settings, &priv);
397 if (ret) {
398 log_debug("Failed to read settings (err=%d)\n", ret);
399 return log_msg_ret("set", ret);
400 }
401
402 return 0;
403}
Simon Glassfc9c0e02023-08-14 16:40:35 -0600404
405static int h_write_settings_env(struct scene_obj *obj, void *vpriv)
406{
407 const struct scene_obj_menu *menu;
408 struct cedit_iter_priv *priv = vpriv;
409 char name[80], var[60];
410 const char *str;
411 int val, ret;
412
413 if (obj->type != SCENEOBJT_MENU)
414 return 0;
415
416 menu = (struct scene_obj_menu *)obj;
417 val = menu->cur_item_id;
418 snprintf(var, sizeof(var), "c.%s", obj->name);
419
420 if (priv->verbose)
421 printf("%s=%d\n", var, val);
422
423 ret = env_set_ulong(var, val);
424 if (ret)
425 return log_msg_ret("set", ret);
426
427 ret = get_cur_menuitem_text(menu, &str);
428 if (ret)
429 return log_msg_ret("mis", ret);
430
431 snprintf(name, sizeof(name), "c.%s-str", obj->name);
432 if (priv->verbose)
433 printf("%s=%s\n", name, str);
434
435 ret = env_set(name, str);
436 if (ret)
437 return log_msg_ret("st2", ret);
438
439 return 0;
440}
441
442int cedit_write_settings_env(struct expo *exp, bool verbose)
443{
444 struct cedit_iter_priv priv;
445 int ret;
446
447 /* write out the items */
448 priv.verbose = verbose;
449 ret = expo_iter_scene_objs(exp, h_write_settings_env, &priv);
450 if (ret) {
451 log_debug("Failed to write settings to env (err=%d)\n", ret);
452 return log_msg_ret("set", ret);
453 }
454
455 return 0;
456}
Simon Glassbcf2b722023-08-14 16:40:36 -0600457
458static int h_read_settings_env(struct scene_obj *obj, void *vpriv)
459{
460 struct cedit_iter_priv *priv = vpriv;
461 struct scene_obj_menu *menu;
462 char var[60];
Simon Glasseb6c71b2023-08-14 16:40:37 -0600463 int val;
Simon Glassbcf2b722023-08-14 16:40:36 -0600464
465 if (obj->type != SCENEOBJT_MENU)
466 return 0;
467
468 menu = (struct scene_obj_menu *)obj;
469 val = menu->cur_item_id;
470 snprintf(var, sizeof(var), "c.%s", obj->name);
471
472 val = env_get_ulong(var, 10, 0);
473 if (priv->verbose)
474 printf("%s=%d\n", var, val);
475 if (!val)
476 return log_msg_ret("get", -ENOENT);
477
478 /*
479 * note that no validation is done here, to make sure the ID is valid
480 * and actually points to a menu item
481 */
482 menu->cur_item_id = val;
483
484 return 0;
485}
486
487int cedit_read_settings_env(struct expo *exp, bool verbose)
488{
489 struct cedit_iter_priv priv;
490 int ret;
491
492 /* write out the items */
493 priv.verbose = verbose;
494 ret = expo_iter_scene_objs(exp, h_read_settings_env, &priv);
495 if (ret) {
496 log_debug("Failed to read settings from env (err=%d)\n", ret);
497 return log_msg_ret("set", ret);
498 }
499
500 return 0;
501}
Simon Glasseb6c71b2023-08-14 16:40:37 -0600502
503/**
504 * get_cur_menuitem_seq() - Get the sequence number of a menu's current item
505 *
506 * Enumerates the items of a menu (0, 1, 2) and returns the sequence number of
507 * the currently selected item. If the first item is selected, this returns 0;
508 * if the second, 1; etc.
509 *
510 * @menu: Menu to check
511 * Return: Sequence number on success, else -ve error value
512 */
513static int get_cur_menuitem_seq(const struct scene_obj_menu *menu)
514{
515 const struct scene_menitem *mi;
516 int seq, found;
517
518 seq = 0;
519 found = -1;
520 list_for_each_entry(mi, &menu->item_head, sibling) {
521 if (mi->id == menu->cur_item_id) {
522 found = seq;
523 break;
524 }
525 seq++;
526 }
527
528 if (found == -1)
529 return log_msg_ret("nf", -ENOENT);
530
531 return found;
532}
533
534static int h_write_settings_cmos(struct scene_obj *obj, void *vpriv)
535{
536 const struct scene_obj_menu *menu;
537 struct cedit_iter_priv *priv = vpriv;
538 int val, ret;
539 uint i, seq;
540
541 if (obj->type != SCENEOBJT_MENU)
542 return 0;
543
544 menu = (struct scene_obj_menu *)obj;
545 val = menu->cur_item_id;
546
547 ret = get_cur_menuitem_seq(menu);
548 if (ret < 0)
549 return log_msg_ret("cur", ret);
550 seq = ret;
551 log_debug("%s: seq=%d\n", menu->obj.name, seq);
552
553 /* figure out where to place this item */
554 if (!obj->bit_length)
555 return log_msg_ret("len", -EINVAL);
556 if (obj->start_bit + obj->bit_length > CMOS_MAX_BITS)
557 return log_msg_ret("bit", -E2BIG);
558
559 for (i = 0; i < obj->bit_length; i++, seq >>= 1) {
560 uint bitnum = obj->start_bit + i;
561
562 priv->mask[CMOS_BYTE(bitnum)] |= 1 << CMOS_BIT(bitnum);
563 if (seq & 1)
564 priv->value[CMOS_BYTE(bitnum)] |= BIT(CMOS_BIT(bitnum));
565 log_debug("bit %x %x %x\n", bitnum,
566 priv->mask[CMOS_BYTE(bitnum)],
567 priv->value[CMOS_BYTE(bitnum)]);
568 }
569
570 return 0;
571}
572
573int cedit_write_settings_cmos(struct expo *exp, struct udevice *dev,
574 bool verbose)
575{
576 struct cedit_iter_priv priv;
577 int ret, i, count, first, last;
578
579 /* write out the items */
580 priv.mask = calloc(1, CMOS_MAX_BYTES);
581 if (!priv.mask)
582 return log_msg_ret("mas", -ENOMEM);
583 priv.value = calloc(1, CMOS_MAX_BYTES);
584 if (!priv.value) {
585 free(priv.mask);
586 return log_msg_ret("val", -ENOMEM);
587 }
588
589 ret = expo_iter_scene_objs(exp, h_write_settings_cmos, &priv);
590 if (ret) {
591 log_debug("Failed to write CMOS (err=%d)\n", ret);
592 ret = log_msg_ret("set", ret);
593 goto done;
594 }
595
596 /* write the data to the RTC */
597 first = CMOS_MAX_BYTES;
598 last = -1;
599 for (i = 0, count = 0; i < CMOS_MAX_BYTES; i++) {
600 if (priv.mask[i]) {
601 log_debug("Write byte %x: %x\n", i, priv.value[i]);
602 ret = rtc_write8(dev, i, priv.value[i]);
603 if (ret) {
604 ret = log_msg_ret("wri", ret);
605 goto done;
606 }
607 count++;
608 first = min(first, i);
609 last = max(last, i);
610 }
611 }
612 if (verbose) {
613 printf("Write %d bytes from offset %x to %x\n", count, first,
614 last);
615 }
616
617done:
618 free(priv.mask);
619 free(priv.value);
620 return ret;
621}