1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Commands for UPL handoff generation
4  *
5  * Copyright 2024 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #define LOG_CATEGORY UCLASS_BOOTSTD
10 
11 #include <abuf.h>
12 #include <alist.h>
13 #include <command.h>
14 #include <display_options.h>
15 #include <env.h>
16 #include <mapmem.h>
17 #include <string.h>
18 #include <upl.h>
19 #include <dm/ofnode.h>
20 #include <test/ut.h>
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
do_upl_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])24 static int do_upl_info(struct cmd_tbl *cmdtp, int flag, int argc,
25 		       char *const argv[])
26 {
27 	const struct upl *upl = gd_upl();
28 
29 	printf("UPL state: %sactive\n", upl ? "" : "in");
30 	if (!upl)
31 		return 0;
32 	if (argc > 1 && !strcmp("-v", argv[1])) {
33 		int i;
34 
35 		printf("fit %lx\n", upl->fit);
36 		printf("conf_offset %x\n", upl->conf_offset);
37 		for (i = 0; i < upl->image.count; i++) {
38 			const struct upl_image *img =
39 				alist_get(&upl->image, i, struct upl_image);
40 
41 			printf("image %d: load %lx size %lx offset %x: %s\n", i,
42 			       img->load, img->size, img->offset,
43 			       img->description);
44 		}
45 	}
46 
47 	return 0;
48 }
49 
do_upl_write(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])50 static int do_upl_write(struct cmd_tbl *cmdtp, int flag, int argc,
51 			char *const argv[])
52 {
53 	struct upl s_upl, *upl = &s_upl;
54 	struct unit_test_state uts = { 0 };
55 	struct abuf buf;
56 	oftree tree;
57 	ulong addr;
58 	int ret;
59 
60 	upl_get_test_data(&uts, upl);
61 
62 	log_debug("Writing UPL\n");
63 	ret = upl_create_handoff_tree(upl, &tree);
64 	if (ret) {
65 		log_err("Failed to write (err=%dE)\n", ret);
66 		return CMD_RET_FAILURE;
67 	}
68 
69 	log_debug("Flattening\n");
70 	ret = oftree_to_fdt(tree, &buf);
71 	if (ret) {
72 		log_err("Failed to write (err=%dE)\n", ret);
73 		return CMD_RET_FAILURE;
74 	}
75 	addr = map_to_sysmem(abuf_data(&buf));
76 	printf("UPL handoff written to %lx size %zx\n", addr, abuf_size(&buf));
77 	if (env_set_hex("upladdr", addr) ||
78 	    env_set_hex("uplsize", abuf_size(&buf))) {
79 		printf("Cannot set env var\n");
80 		return CMD_RET_FAILURE;
81 	}
82 
83 	log_debug("done\n");
84 
85 	return 0;
86 }
87 
do_upl_read(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])88 static int do_upl_read(struct cmd_tbl *cmdtp, int flag, int argc,
89 		       char *const argv[])
90 {
91 	struct upl s_upl, *upl = &s_upl;
92 	oftree tree;
93 	ulong addr;
94 	int ret;
95 
96 	if (argc < 1)
97 		return CMD_RET_USAGE;
98 	addr = hextoul(argv[1], NULL);
99 
100 	printf("Reading UPL at %lx\n", addr);
101 	tree = oftree_from_fdt(map_sysmem(addr, 0));
102 	ret = upl_read_handoff(upl, tree);
103 	if (ret) {
104 		log_err("Failed to read (err=%dE)\n", ret);
105 		return CMD_RET_FAILURE;
106 	}
107 
108 	return 0;
109 }
110 
111 U_BOOT_LONGHELP(upl,
112 	"info [-v]     - Check UPL status\n"
113 	"upl read <addr>   - Read handoff information\n"
114 	"upl write         - Write handoff information");
115 
116 U_BOOT_CMD_WITH_SUBCMDS(upl, "Universal Payload support", upl_help_text,
117 	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_upl_info),
118 	U_BOOT_SUBCMD_MKENT(read, 2, 1, do_upl_read),
119 	U_BOOT_SUBCMD_MKENT(write, 1, 1, do_upl_write));
120