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