blob: 8f63ccbe3ef06e05fce0490616893ff21ecf8871 [file] [log] [blame]
Simon Glass311bd352023-01-06 08:52:43 -06001.. SPDX-License-Identifier: GPL-2.0+
2
3Expo menu
4=========
5
6U-Boot provides a menu implementation for use with selecting bootflows and
7changing U-Boot settings. This is in early stages of development.
8
9Motivation
10----------
11
12U-Boot already has a text-based menu system accessed via the
13:doc:`../usage/cmd/bootmenu`. This works using environment variables, or via
14some EFI-specific hacks.
15
16The command makes use of a lower-level `menu` implementation, which is quite
17flexible and can be used to make menu hierarchies.
18
19However this system is not flexible enough for use with standard boot. It does
20not support a graphical user interface and cannot currently support anything
21more than a very simple list of items. While it does support multiple menus in
22hierarchies, these are implemented by the caller. See for example `eficonfig.c`.
23
24Another challenge with the current menu implementation is that it controls
25the event loop, such that bootmenu_loop() does not return until a key is
26pressed. This makes it difficult to implement dynamic displays or to do other
27things while the menu is running, such as searching for more bootflows.
28
29For these reasons an attempt has been made to develop a more flexible system
30which can handle menus as well as other elements. This is called 'expo', short
31for exposition, in an attempt to avoid common words like display, screen, menu
32and the like. The primary goal is to support Verified Boot for Embedded (VBE),
33although it is available to any boot method, using the 'bootflow menu' command.
34
35Efforts have been made to use common code with the existing menu, including
36key processing in particular.
37
38Previous work looked at integrating Nuklear into U-Boot. This works fine and
39could provide a way to provide a more flexible UI, perhaps with expo dealing
40with the interface to Nuklear. But this is quite a big step and it may be years
41before this becomes desirable, if at all. For now, U-Boot only needs a fairly
42simple set of menus and options, so rendering them directly is fairly
43straightforward.
44
45Concepts
46--------
47
48The creator of the expo is here called a `controller` and it controls most
49aspects of the expo. This is the code that you must write to use expo.
50
51An `expo` is a set of scenes which can be presented to the user one at a time,
52to show information and obtain input from the user.
53
54A `scene` is a collection of objects which are displayed together on the screen.
55Only one scene is visible at a time and scenes do not share objects.
56
57A `scene object` is something that appears in the scene, such as some text, an
58image or a menu. Objects can be positioned and hidden.
59
60A `menu object` contains a title, a set of `menu items` and a pointer to the
61current item. Menu items consist of a keypress (indicating what to press to
62select the item), label and description. All three are shown in a single line
63within the menu. Items can also have a preview image, which is shown when the
64item is highlighted.
65
Simon Glass00f6c402023-10-01 19:13:40 -060066A `textline object` contains a label and an editable string.
67
Simon Glass138a3972025-05-02 08:46:44 -060068A `box object` is a rectangle with a given line width. It is not filled.
69
Simon Glass00f6c402023-10-01 19:13:40 -060070All components have a name. This is mostly for debugging, so it is easy to see
71what object is referred to, although the name is also used for saving values.
72Of course the ID numbers can help as well, but they are less easy to
73distinguish.
Simon Glass311bd352023-01-06 08:52:43 -060074
75While the expo implementation provides support for handling keypresses and
76rendering on the display or serial port, it does not actually deal with reading
77input from the user, nor what should be done when a particular menu item is
78selected. This is deliberate since having the event loop outside the expo is
79more flexible, particularly in a single-threaded environment like U-Boot.
80
81Everything within an expo has a unique ID number. This is done so that it is
82easy to refer to things after the expo has been created. The expectation is that
83the controller declares an enum containing all of the elements in the expo,
84passing the ID of each object as it is created. When a menu item is selected,
85its ID is returned. When a object's font or position needs to change, the ID is
86passed to expo functions to indicate which object it is. It is possible for expo
87to auto-allocate IDs, but this is not recommended. The use of IDs is a
88convenience, removing the need for the controller to store pointers to objects,
89or even the IDs of objects. Programmatic creation of many items in a loop can be
90handled by allocating space in the enum for a maximum number of items, then
91adding the loop count to the enum values to obtain unique IDs.
92
Simon Glass53a0a2f2024-10-14 16:31:57 -060093Some standard IDs are reserved for certain purposes. These are defined by
94`enum expo_id_t` and start at 1. `EXPOID_BASE_ID` defines the first ID which
95can be used for an expo.
96
97An ID of 0 is invalid. If this is specified in an expo call then a valid
98'dynamic IDs is allocated. Use expo_set_dynamic_start() to set the start
99value, so that they are allocated above the starting (enum) IDs.
Simon Glass6e9e4152023-06-01 10:22:47 -0600100
Simon Glass311bd352023-01-06 08:52:43 -0600101All text strings are stored in a structure attached to the expo, referenced by
102a text ID. This makes it easier at some point to implement multiple languages or
103to support Unicode strings.
104
105Menu objects do not have their own text and image objects. Instead they simply
106refer to objects which have been created. So a menu item is just a collection
107of IDs of text and image objects. When adding a menu item you must create these
108objects first, then create the menu item, passing in the relevant IDs.
109
110Creating an expo
111----------------
112
Simon Glass61300722023-06-01 10:23:01 -0600113To create an expo programmatically, use `expo_new()` followed by `scene_new()`
114to create a scene. Then add objects to the scene, using functions like
115`scene_txt_str()` and `scene_menu()`. For every menu item, add text and image
116objects, then create the menu item with `scene_menuitem()`, referring to those
117objects.
118
119To create an expo using a description file, see :ref:`expo_format` below.
Simon Glass311bd352023-01-06 08:52:43 -0600120
121Layout
122------
123
124Individual objects can be positioned using `scene_obj_set_pos()`. Menu items
125cannot be positioned manually: this is done by `scene_arrange()` which is called
126automatically when something changes. The menu itself determines the position of
127its items.
128
129Rendering
130---------
131
132Rendering is performed by calling `expo_render()`. This uses either the
133vidconsole, if present, or the serial console in `text mode`. Expo handles
134presentation automatically in either case, without any change in how the expo is
135created.
136
137For the vidconsole, Truetype fonts can be used if enabled, to enhance the
138quality of the display. For text mode, each menu item is shown in a single line,
139allowing easy selection using arrow keys.
140
141Input
142-----
143
144The controller is responsible for collecting keyboard input. A good way to do
145this is to use `cli_ch_process()`, since it handles conversion of escape
146sequences into keys. However, expo has some special menu-key codes for
147navigating the interface. These are defined in `enum bootmenu_key` and include
148`BKEY_UP` for moving up and `BKEY_SELECT` for selecting an item. You can use
Simon Glass00f6c402023-10-01 19:13:40 -0600149`bootmenu_conv_key()` to convert an ASCII key into one of these, but if it
150returns a value >= `BKEY_FIRST_EXTRA` then you should pass the unmodified ASCII
151key to the expo, since it may be used by textline objects.
Simon Glass311bd352023-01-06 08:52:43 -0600152
153Once a keypress is decoded, call `expo_send_key()` to send it to the expo. This
154may cause an update to the expo state and may produce an action.
155
156Actions
157-------
158
159Call `expo_action_get()` in the event loop to check for any actions that the
160expo wants to report. These can include selecting a particular menu item, or
161quitting the menu. Processing of these is the responsibility of your controller.
162
163Event loop
164----------
165
166Expo is intended to be used in an event loop. For an example loop, see
167`bootflow_menu_run()`. It is possible to perform other work in your event loop,
168such as scanning devices for more bootflows.
169
170Themes
171------
172
Simon Glassc999e172023-06-01 10:22:53 -0600173Expo supports simple themes, for setting the font size, for example. Use the
174expo_apply_theme() function to load a theme, passing a node with the required
175properties:
176
177font-size
178 Font size to use for all text (type: u32)
179
Simon Glass86f1ac52023-06-01 10:23:00 -0600180menu-inset
181 Number of pixels to inset the menu on the sides and top (type: u32)
182
183menuitem-gap-y
184 Number of pixels between menu items
185
Simon Glass377f18e2024-10-14 16:31:55 -0600186menu-title-margin-x
187 Number of pixels between right side of menu title to the left size of the
188 menu labels
189
Simon Glass61300722023-06-01 10:23:01 -0600190Pop-up mode
191-----------
192
193Expos support two modes. The simple mode is used for selecting from a single
194menu, e.g. when choosing with OS to boot. In this mode the menu items are shown
195in a list (label, > pointer, key and description) and can be chosen using arrow
196keys and enter::
197
198 U-Boot Boot Menu
199
200 UP and DOWN to choose, ENTER to select
201
202 mmc1 > 0 Fedora-Workstation-armhfp-31-1.9
203 mmc3 1 Armbian
204
205The popup mode allows multiple menus to be present in a scene. Each is shown
206just as its title and label, as with the `CPU Speed` and `AC Power` menus here::
207
208 Test Configuration
209
210
211 CPU Speed <2 GHz> (highlighted)
212
213 AC Power Always Off
214
215
216 UP and DOWN to choose, ENTER to select
217
218
Simon Glassc8925112023-06-01 10:23:02 -0600219.. _expo_format:
220
Simon Glass61300722023-06-01 10:23:01 -0600221Expo Format
222-----------
223
224It can be tedious to create a complex expo using code. Expo supports a
225data-driven approach, where the expo description is in a devicetree file. This
226makes it easier and faster to create and edit the description. An expo builder
227is provided to convert this format into an expo structure.
228
229Layout of the expo scenes is handled automatically, based on a set of simple
Simon Glassc8925112023-06-01 10:23:02 -0600230rules. The :doc:`../usage/cmd/cedit` can be used to load a configuration
231and create an expo from it.
Simon Glass61300722023-06-01 10:23:01 -0600232
233Top-level node
234~~~~~~~~~~~~~~
235
236The top-level node has the following properties:
237
238dynamic-start
239 type: u32, optional
240
241 Specifies the start of the dynamically allocated objects. This results in
242 a call to expo_set_dynamic_start().
243
244The top-level node has the following subnodes:
245
246scenes
247 Specifies the scenes in the expo, each one being a subnode
248
249strings
250 Specifies the strings in the expo, each one being a subnode
251
252`scenes` node
253~~~~~~~~~~~~~
254
255Contains a list of scene subnodes. The name of each subnode is passed as the
256name to `scene_new()`.
257
258`strings` node
259~~~~~~~~~~~~~~
260
261Contains a list of string subnodes. The name of each subnode is ignored.
262
263`strings` subnodes
264~~~~~~~~~~~~~~~~~~
265
266Each subnode defines a string which can be used by scenes and objects. Each
267string has an ID number which is used to refer to it.
268
269The `strings` subnodes have the following properties:
270
271id
272 type: u32, required
273
274 Specifies the ID number for the string.
275
276value:
277 type: string, required
278
279 Specifies the string text. For now only a single value is supported. Future
280 work may add support for multiple languages by using a value for each
281 language.
282
283Scene nodes (`scenes` subnodes)
284~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285
286Each subnode of the `scenes` node contains a scene description.
287
288Most properties can use either a string or a string ID. For example, a `title`
289property can be used to provide the title for a menu; alternatively a `title-id`
290property can provide the string ID of the title. If both are present, the
291ID takes preference, except that if a string with that ID does not exist, it
292falls back to using the string from the property (`title` in this example). The
293description below shows these are alternative properties with the same
294description.
295
296The scene nodes have the following properties:
297
298id
299 type: u32, required
300
301 Specifies the ID number for the string.
302
303title / title-id
304 type: string / u32, required
305
306 Specifies the title of the scene. This is shown at the top of the scene.
307
308prompt / prompt-id
309 type: string / u32, required
310
311 Specifies a prompt for the scene. This is shown at the bottom of the scene.
312
313The scene nodes have a subnode for each object in the scene.
314
315Object nodes
316~~~~~~~~~~~~
317
318The object-node name is used as the name of the object, e.g. when calling
319`scene_menu()` to create a menu.
320
321Object nodes have the following common properties:
322
323type
324 type: string, required
325
326 Specifies the type of the object. Valid types are:
327
328 "menu"
329 Menu containing items which can be selected by the user
330
Simon Glass00f6c402023-10-01 19:13:40 -0600331 "textline"
332 A line of text which can be edited
333
Simon Glass61300722023-06-01 10:23:01 -0600334id
335 type: u32, required
336
337 Specifies the ID of the object. This is used when referring to the object.
338
Simon Glass2b91ca62023-08-14 16:40:37 -0600339Where CMOS RAM is used for reading and writing settings, the following
340additional properties are required:
341
342start-bit
343 Specifies the first bit in the CMOS RAM to use for this setting. For a RAM
344 with 0x100 bytes, there are 0x800 bit locations. For example, register 0x80
345 holds bits 0x400 to 0x407.
346
347bit-length
348 Specifies the number of CMOS RAM bits to use for this setting. The bits
349 extend from `start-bit` to `start-bit + bit-length - 1`. Note that the bits
350 must be contiguous.
Simon Glass61300722023-06-01 10:23:01 -0600351
352Menu nodes have the following additional properties:
353
354title / title-id
355 type: string / u32, required
356
357 Specifies the title of the menu. This is shown to the left of the area for
358 this menu.
359
360item-id
361 type: u32 list, required
362
363 Specifies the ID for each menu item. These are used for checking which item
364 has been selected.
365
Simon Glass100389f2024-10-14 16:31:58 -0600366item-value
367 type: u32 list, optional
368
369 Specifies the value for each menu item. These are used for saving and
370 loading. If this is omitted the value is its position in the menu (0..n-1).
371 Valid values are positive and negative integers INT_MIN...(INT_MAX - 1).
372
Simon Glass61300722023-06-01 10:23:01 -0600373item-label / item-label-id
374 type: string list / u32 list, required
375
376 Specifies the label for each item in the menu. These are shown to the user.
377 In 'popup' mode these form the items in the menu.
378
379key-label / key-label-id
380 type: string list / u32 list, optional
381
382 Specifies the key for each item in the menu. These are currently only
383 intended for use in simple mode.
384
385desc-label / desc-label-id
386 type: string list / u32 list, optional
387
388 Specifies the description for each item in the menu. These are currently
389 only intended for use in simple mode.
390
Simon Glass00f6c402023-10-01 19:13:40 -0600391Textline nodes have the following additional properties:
392
393label / label-id
394 type: string / u32, required
395
396 Specifies the label of the textline. This is shown to the left of the area
397 for this textline.
398
399edit-id
400 type: u32, required
401
402 Specifies the ID of the of the editable text object. This can be used to
403 obtain the text from the textline
404
405max-chars:
406 type: u32, required
407
408 Specifies the maximum number of characters permitted to be in the textline.
409 The user will be prevented from adding more.
410
Simon Glass61300722023-06-01 10:23:01 -0600411
412Expo layout
413~~~~~~~~~~~
414
415The `expo_arrange()` function can be called to arrange the expo objects in a
416suitable manner. For each scene it puts the title at the top, the prompt at the
417bottom and the objects in order from top to bottom.
418
Simon Glass40eb3c32023-08-14 16:40:29 -0600419
420.. _expo_example:
421
Simon Glass61300722023-06-01 10:23:01 -0600422Expo format example
423~~~~~~~~~~~~~~~~~~~
424
425This example shows an expo with a single scene consisting of two menus. The
426scene title is specified using a string from the strings table, but all other
427strings are provided inline in the nodes where they are used.
428
429::
430
Simon Glassb1263bc2023-08-14 16:40:28 -0600431 /* this comment is parsed by the expo.py tool to insert the values below
Simon Glass61300722023-06-01 10:23:01 -0600432
Simon Glassb1263bc2023-08-14 16:40:28 -0600433 enum {
Simon Glass53a0a2f2024-10-14 16:31:57 -0600434 ID_PROMPT = EXPOID_BASE_ID,
Simon Glassb1263bc2023-08-14 16:40:28 -0600435 ID_SCENE1,
436 ID_SCENE1_TITLE,
Simon Glass61300722023-06-01 10:23:01 -0600437
Simon Glassb1263bc2023-08-14 16:40:28 -0600438 ID_CPU_SPEED,
439 ID_CPU_SPEED_TITLE,
440 ID_CPU_SPEED_1,
441 ID_CPU_SPEED_2,
442 ID_CPU_SPEED_3,
443
444 ID_POWER_LOSS,
445 ID_AC_OFF,
446 ID_AC_ON,
447 ID_AC_MEMORY,
Simon Glass61300722023-06-01 10:23:01 -0600448
Simon Glass00f6c402023-10-01 19:13:40 -0600449 ID_MACHINE_NAME,
450 ID_MACHINE_NAME_EDIT,
451
Simon Glassb1263bc2023-08-14 16:40:28 -0600452 ID_DYNAMIC_START,
453 */
Simon Glass61300722023-06-01 10:23:01 -0600454
455 &cedit {
456 dynamic-start = <ID_DYNAMIC_START>;
457
458 scenes {
459 main {
460 id = <ID_SCENE1>;
461
462 /* value refers to the matching id in /strings */
463 title-id = <ID_SCENE1_TITLE>;
464
465 /* simple string is used as it is */
466 prompt = "UP and DOWN to choose, ENTER to select";
467
468 /* defines a menu within the scene */
469 cpu-speed {
470 type = "menu";
471 id = <ID_CPU_SPEED>;
472
473 /*
474 * has both string and ID. The string is ignored
475 * if the ID is present and points to a string
476 */
477 title = "CPU speed";
478 title-id = <ID_CPU_SPEED_TITLE>;
479
480 /* menu items as simple strings */
481 item-label = "2 GHz", "2.5 GHz", "3 GHz";
482
483 /* IDs for the menu items */
484 item-id = <ID_CPU_SPEED_1 ID_CPU_SPEED_2
485 ID_CPU_SPEED_3>;
Simon Glass100389f2024-10-14 16:31:58 -0600486
487 /* values for the menu items */
488 item-value = <(-1) 3 6>;
Simon Glass61300722023-06-01 10:23:01 -0600489 };
490
491 power-loss {
492 type = "menu";
493 id = <ID_POWER_LOSS>;
494
495 title = "AC Power";
496 item-label = "Always Off", "Always On",
497 "Memory";
498
499 item-id = <ID_AC_OFF ID_AC_ON ID_AC_MEMORY>;
500 };
Simon Glass00f6c402023-10-01 19:13:40 -0600501
502 machine-name {
503 id = <ID_MACHINE_NAME>;
504 type = "textline";
505 max-chars = <20>;
506 title = "Machine name";
507 edit-id = <ID_MACHINE_NAME_EDIT>;
Simon Glass61300722023-06-01 10:23:01 -0600508 };
509 };
510
511 strings {
512 title {
513 id = <ID_SCENE1_TITLE>;
514 value = "Test Configuration";
515 value-es = "configuración de prueba";
516 };
517 };
518 };
519
Simon Glass311bd352023-01-06 08:52:43 -0600520
521API documentation
522-----------------
523
524.. kernel-doc:: include/expo.h
525
526Future ideas
527------------
528
529Some ideas for future work:
530
531- Default menu item and a timeout
Simon Glass311bd352023-01-06 08:52:43 -0600532- Image formats other than BMP
533- Use of ANSI sequences to control a serial terminal
534- Colour selection
Simon Glass00f6c402023-10-01 19:13:40 -0600535- Support for more widgets, e.g. numeric, radio/option
Simon Glass311bd352023-01-06 08:52:43 -0600536- Mouse support
537- Integrate Nuklear, NxWidgets or some other library for a richer UI
538- Optimise rendering by only updating the display with changes since last render
539- Use expo to replace the existing menu implementation
540- Add a Kconfig option to drop the names to save code / data space
541- Add a Kconfig option to disable vidconsole support to save code / data space
542- Support both graphical and text menus at the same time on different devices
Simon Glass311bd352023-01-06 08:52:43 -0600543- Support unicode
544- Support curses for proper serial-terminal menus
Simon Glass61300722023-06-01 10:23:01 -0600545- Add support for large menus which need to scroll
Simon Glass2b91ca62023-08-14 16:40:37 -0600546- Update expo.py tool to check for overlapping names and CMOS locations
Simon Glass311bd352023-01-06 08:52:43 -0600547
548.. Simon Glass <sjg@chromium.org>
549.. 7-Oct-22