Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: GPL-2.0+
/*
 * Provide a menu of available bootflows and related options
 *
 * Copyright 2022 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY UCLASS_BOOTSTD

#include <bootflow.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <cli.h>
#include <dm.h>
#include <expo.h>
#include <malloc.h>
#include <menu.h>
#include <video_console.h>
#include <watchdog.h>
#include <linux/delay.h>
#include "bootflow_internal.h"

/**
 * struct menu_priv - information about the menu
 *
 * @num_bootflows: Number of bootflows in the menu
 * @last_bootdev: bootdev of the last bootflow added to the menu, NULL if none
 */
struct menu_priv {
	int num_bootflows;
	struct udevice *last_bootdev;
};

static int bootflow_menu_set_item_props(struct scene *scn,
					int i, const struct bootflow *bflow)
{
	struct expo *exp = scn->expo;
	struct abuf *buf;
	int ret;

	scene_obj_set_hide(scn, ITEM_PREVIEW + i, true);
	ret = scene_obj_set_hide(scn, ITEM_BOX + i, true);
	ret |= scene_obj_set_hide(scn, ITEM_VERSION_NAME + i, true);
	scene_obj_set_hide(scn, ITEM_VERIFIED + i, true);
	ret |= scene_obj_set_hide(scn, ITEM_KEY + i, false);
	scene_obj_set_hide(scn, ITEM_LOCKED + i, true);
	scene_obj_set_hide(scn, ITEM_PASS_LABEL + i, true);
	scene_obj_set_hide(scn, ITEM_PASS_EDIT + i, true);
	if (ret)
		return log_msg_ret("msp", ret);

	ret = expo_edit_str(exp, STR_DESC + i, NULL, &buf);
	if (ret)
		return log_msg_ret("msr", ret);
	abuf_printf(buf, "%s", bflow->os_name ? bflow->os_name : bflow->name);

	ret = expo_edit_str(exp, STR_LABEL + i, NULL, &buf);
	if (!ret)
		abuf_printf(buf, "%s", bootflow_guess_label(bflow));

	return 0;
}

int bootflow_menu_set_props(struct expo *exp, struct scene *scn, bool has_logo,
			    const char *title)
{
	struct expo_theme *theme = &exp->theme;
	struct menu_priv *priv = exp->priv;
	struct bootstd_priv *std;
	struct abuf *buf;
	int i, ret = 0;
	bool use_font;

	ret |= scene_obj_set_bbox(scn, OBJ_BOX, 30, 90, 1366 - 30, 720);
	scene_box_set_fill(scn, OBJ_BOX, false);
	ret |= scene_obj_set_pos(scn, OBJ_MENU, MARGIN_LEFT, 100);
	ret |= scene_obj_set_bbox(scn, OBJ_MENU_TITLE, 0, 32,
				  1366, 60);
	ret |= scene_obj_set_halign(scn, OBJ_MENU_TITLE, SCENEOA_CENTRE);

	if (has_logo)
		ret |= scene_obj_set_pos(scn, OBJ_U_BOOT_LOGO, 1165, 100);

	ret |= scene_obj_set_bbox(scn, OBJ_PROMPT1A, 0, 590,
				  1366, 590 + 40);
	ret |= scene_obj_set_bbox(scn, OBJ_PROMPT1B, 0, 620,
				  1366, 620 + 40);
	ret |= scene_obj_set_bbox(scn, OBJ_PROMPT2, 100, 650,
				  1366 - 100, 700);
	ret |= scene_obj_set_bbox(scn, OBJ_AUTOBOOT, 0, 720,
				  1366, 750);
	ret |= scene_obj_set_halign(scn, OBJ_PROMPT1A, SCENEOA_CENTRE);
	ret |= scene_obj_set_halign(scn, OBJ_PROMPT1B, SCENEOA_CENTRE);
	ret |= scene_obj_set_halign(scn, OBJ_PROMPT2, SCENEOA_CENTRE);
	ret |= scene_obj_set_valign(scn, OBJ_PROMPT2, SCENEOA_CENTRE);
	ret |= scene_obj_set_halign(scn, OBJ_AUTOBOOT, SCENEOA_CENTRE);
	ret |= scene_menu_set_pointer(scn, OBJ_MENU, OBJ_POINTER);

	if (ret)
		return log_msg_ret("msp", ret);

	use_font = IS_ENABLED(CONFIG_CONSOLE_TRUETYPE);
	scene_obj_set_hide(scn, OBJ_PROMPT1A, use_font);
	scene_obj_set_hide(scn, OBJ_PROMPT1B, !use_font);
	scene_obj_set_hide(scn, OBJ_AUTOBOOT, use_font);

	/* Set the title and prompt texts */
	ret = expo_edit_str(exp, STR_MENU_TITLE, NULL, &buf);
	if (ret)
		return log_msg_ret("mss", ret);
	abuf_printf(buf, "%s", title);

	ret = expo_edit_str(exp, STR_PROMPT1A, NULL, &buf);
	if (!ret)
		abuf_printf(buf, "Use the \x18 and \x19 keys to select which "
			    "entry is highlighted.");

	ret = expo_edit_str(exp, STR_PROMPT1B, NULL, &buf);
	if (!ret)
		abuf_printf(buf, "Use the UP and DOWN keys to select which "
			    "entry is highlighted.");

	ret = expo_edit_str(exp, STR_PROMPT2, NULL, &buf);
	if (!ret)
		abuf_printf(buf, "Press enter to boot the selected OS, 'e' to "
			    "edit the commands before booting or 'c' for a "
			    "command-line. ESC to return to previous menu");

	/* hide a few things we don't use */
	scene_obj_set_hide(scn, OBJ_OTHER_LOGO, true);
	scene_obj_set_hide(scn, OBJ_SETTINGS, true);
	scene_obj_set_hide(scn, OBJ_HELP, true);

	/* select the menu and hide the pointer */
	scene_set_highlight_id(scn, OBJ_MENU);
	scene_obj_set_hide(scn, OBJ_POINTER, false);

	/* tell the menu to lay out its objects */
	scene_obj_set_manual(scn, OBJ_MENU, false);

	scene_obj_set_hide(scn, OBJ_POINTER, false);

	theme->white_on_black = true;
	ret = expo_apply_theme(exp, true);
	if (ret)
		return log_msg_ret("mat", ret);

	expo_set_mouse_enable(exp, false);

	exp->show_highlight = true;

	ret = bootstd_get_priv(&std);
	if (ret)
		return ret;

	for (i = 0; i < priv->num_bootflows; i++) {
		const struct bootflow *bflow;

		bflow = alist_get(&std->bootflows, i, struct bootflow);
		if (!bflow)
			return log_msg_ret("bmb", -ENOENT);
		ret = bootflow_menu_set_item_props(scn, i, bflow);
		if (ret)
			return log_msg_ret("mst", ret);
	}

	return 0;
}

int bootflow_menu_new(struct expo **expp)
{
	struct scene_obj_menu *menu;
	struct menu_priv *priv;
	struct scene *scn;
	struct expo *exp;
	void *logo;
	int ret;

	priv = calloc(1, sizeof(*priv));
	if (!priv)
		return log_msg_ret("prv", -ENOMEM);

	ret = expo_new("bootflows", priv, &exp);
	if (ret)
		return log_msg_ret("exp", ret);
	expo_req_size(exp, 1366, 768);

	expo_set_mouse_enable(exp, true);

	ret = scene_new(exp, "main", MAIN, &scn);
	if (ret < 0)
		return log_msg_ret("scn", ret);

	ret = scene_box(scn, "box", OBJ_BOX, 2, false, NULL);
	if (ret < 0)
		return log_msg_ret("bmb", ret);

	ret = scene_menu(scn, "main", OBJ_MENU, &menu);
	ret |= scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
			     "", NULL);

	logo = video_get_u_boot_logo(NULL);
	if (logo)
		ret |= scene_img(scn, "ulogo", OBJ_U_BOOT_LOGO, logo, NULL);

	ret |= scene_txt_str(scn, "prompt1a", OBJ_PROMPT1A, STR_PROMPT1A,
	     "", NULL);
	ret |= scene_txt_str(scn, "prompt1b", OBJ_PROMPT1B, STR_PROMPT1B,
	     "", NULL);
	ret |= scene_txt_str(scn, "prompt2", OBJ_PROMPT2, STR_PROMPT2,
	     "", NULL);
	ret |= scene_txt_str(scn, "autoboot", OBJ_AUTOBOOT, STR_AUTOBOOT,
	     "The highlighted entry will be executed automatically in %ds.",
	     NULL);

	ret |= scene_txt_str(scn, "cur_item", OBJ_POINTER, STR_POINTER, ">",
			     NULL);
	if (ret < 0)
		return log_msg_ret("new", -EINVAL);

	ret = bootflow_menu_set_props(exp, scn, logo, "U-Boot - Boot Menu");
	if (ret < 0)
		return log_msg_ret("nep", -EINVAL);

	*expp = exp;

	return 0;
}

int bootflow_menu_add(struct expo *exp, struct bootflow *bflow, int seq,
		      struct scene **scnp)
{
	struct menu_priv *priv = exp->priv;
	struct scene_obj_textline *tline;
	char str[2], *key;
	struct scene *scn;
	uint preview_id;
	uint scene_id;
	bool add_gap;
	int ret;

	ret = expo_first_scene_id(exp);
	if (ret < 0)
		return log_msg_ret("scn", ret);
	scene_id = ret;
	scn = expo_lookup_scene_id(exp, scene_id);

	*str = seq < 10 ? '0' + seq : 'A' + seq - 10;
	str[1] = '\0';
	key = strdup(str);
	if (!key)
		return log_msg_ret("key", -ENOMEM);

	add_gap = priv->last_bootdev != bflow->dev;

	/* disable this gap for now, since it looks a little ugly */
	add_gap = false;
	priv->last_bootdev = bflow->dev;

	ret = expo_str(exp, "prompt", STR_POINTER, ">");
	ret |= scene_txt_str(scn, "label", ITEM_LABEL + seq,
			      STR_LABEL + seq, "", NULL);
	ret |= scene_txt_str(scn, "desc", ITEM_DESC + seq, STR_DESC + seq,
			     "", NULL);
	ret |= scene_txt_str(scn, "key", ITEM_KEY + seq, STR_KEY + seq, key,
			      NULL);
	ret |= scene_box(scn, "item-box", ITEM_BOX + seq, 1, false, NULL);
	ret |= scene_txt_str(scn, "version", ITEM_VERSION_NAME + seq,
			     STR_VERSION_NAME + seq, "", NULL);

	preview_id = 0;
	if (bflow->logo) {
		preview_id = ITEM_PREVIEW + seq;
		ret |= scene_img(scn, "preview", preview_id,
				     bflow->logo, NULL);
	}
	ret |= scene_menuitem(scn, OBJ_MENU, "item", ITEM + seq,
				  ITEM_KEY + seq, ITEM_LABEL + seq,
				  ITEM_DESC + seq, preview_id,
				  add_gap ? SCENEMIF_GAP_BEFORE : 0,
				  NULL);
	if (ret < 0)
		return log_msg_ret("itm", -EINVAL);

	/*
	 * Create passphrase textline with label and edit field (12 chars). Show
	 * characters as asterisks
	 */
	ret = scene_textline(scn, "passphrase", ITEM_PASS + seq, 12, &tline);
	if (ret < 0)
		return log_msg_ret("itp", -EINVAL);
	tline->obj.flags |= SCENEOF_PASSWORD;
	ret = scene_txt_str(scn, "pass_label", ITEM_PASS_LABEL + seq, 0,
			    "Passphrase:", NULL);
	if (ret < 0)
		return log_msg_ret("itl", -EINVAL);
	tline->label_id = ret;

	ret = scene_txt_str(scn, "pass_edit", ITEM_PASS_EDIT + seq, 0,
			    "", NULL);
	if (ret < 0)
		return log_msg_ret("ite", -EINVAL);
	tline->edit_id = ret;

	/* Create message text (hidden by default) for success/error feedback */
	ret = scene_txt_str(scn, "pass_msg", ITEM_PASS_MSG + seq,
			    STR_PASS_MSG + seq, "", NULL);
	if (ret < 0)
		return log_msg_ret("ipm", -EINVAL);
	scene_obj_set_hide(scn, ITEM_PASS_MSG + seq, true);

	ret = bootflow_menu_set_item_props(scn, seq, bflow);
	if (ret)
		return log_msg_ret("itp", -EINVAL);

	priv->num_bootflows++;
	*scnp = scn;

	return 0;
}

int bootflow_menu_add_all(struct expo *exp)
{
	struct bootflow *bflow;
	struct scene *scn;
	int ret, i;

	for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
	     ret = bootflow_next_glob(&bflow), i++) {
		struct bootmeth_uc_plat *ucp;

		if (bflow->state != BOOTFLOWST_READY)
			continue;

		/* No media to show for BOOTMETHF_GLOBAL bootmeths */
		ucp = dev_get_uclass_plat(bflow->method);
		if (ucp->flags & BOOTMETHF_GLOBAL)
			continue;

		ret = bootflow_menu_add(exp, bflow, i, &scn);
		if (ret)
			return log_msg_ret("bao", ret);
	}

	return 0;
}

int bootflow_menu_setup(struct bootstd_priv *std, bool text_mode,
			struct expo **expp)
{
	struct udevice *dev;
	struct expo *exp;
	int ret;

	ret = bootflow_menu_new(&exp);
	if (ret)
		return log_msg_ret("bmn", ret);

	/* For now we only support a video console */
	ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
	if (ret)
		return log_msg_ret("vid", ret);
	ret = expo_set_display(exp, dev);
	if (ret)
		return log_msg_ret("dis", ret);

	ret = expo_set_scene_id(exp, MAIN);
	if (ret)
		return log_msg_ret("scn", ret);

	if (text_mode)
		expo_set_text_mode(exp, text_mode);

	*expp = exp;

	return 0;
}

int bootflow_menu_start(struct bootstd_priv *std, bool text_mode,
			struct expo **expp)
{
	struct expo *exp;
	int ret;

	ret = bootflow_menu_setup(std, text_mode, &exp);
	if (ret)
		return log_msg_ret("bmd", ret);

	ret = bootflow_menu_add_all(exp);
	if (ret)
		return log_msg_ret("bma", ret);

	if (ofnode_valid(std->theme)) {
		ret = expo_setup_theme(exp, std->theme);
		if (ret)
			return log_msg_ret("thm", ret);
	}

	ret = expo_calc_dims(exp);
	if (ret)
		return log_msg_ret("bmd", ret);

	ret = expo_arrange(exp);
	if (ret)
		return log_msg_ret("arr", ret);

	*expp = exp;

	return 0;
}

int bootflow_menu_poll(struct expo *exp, int *seqp)
{
	struct bootflow *sel_bflow;
	struct expo_action act;
	struct scene *scn;
	int item, ret;

	sel_bflow = NULL;

	scn = expo_lookup_scene_id(exp, exp->scene_id);

	item = scene_menu_get_cur_item(scn, OBJ_MENU);
	*seqp = item > 0 ? item - ITEM : -1;

	ret = expo_poll(exp, &act);
	if (ret)
		return log_msg_ret("bmp", ret);

	switch (act.type) {
	case EXPOACT_SELECT:
		*seqp = act.select.id - ITEM;
		break;
	case EXPOACT_POINT_ITEM: {
		struct scene *scn = expo_lookup_scene_id(exp, MAIN);

		if (!scn)
			return log_msg_ret("bms", -ENOENT);
		ret = scene_menu_select_item(scn, OBJ_MENU, act.select.id);
		if (ret)
			return log_msg_ret("bmp", ret);
		if (act.select.changed)
			return -EREMCHG;
		return -ERESTART;
	}
	case EXPOACT_QUIT:
		return -EPIPE;
	case EXPOACT_CLICK:
		if (act.select.id == OBJ_SETTINGS)
			return -ECOMM;  /* layout change request */
		return -EAGAIN;
	case EXPOACT_SETTINGS:
		return -ECOMM;  /* layout change request */
	default:
		return -EAGAIN;
	}

	return 0;
}