blob: d0ae964f1b80382b42101270484b0b06fb1fb58f [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001From c047b15fba9da36616bc9a346d9fe4d76e7ca4b2 Mon Sep 17 00:00:00 2001
2From: Rui Miguel Silva <rui.silva@linaro.org>
3Date: Thu, 24 Jun 2021 09:25:00 +0100
4Subject: [PATCH 01/27] cmd: load: add load command for memory mapped
5
6cp.b is used a lot as a way to load binaries to memory and execute
7them, however we may need to integrate this with the efi subsystem to
8set it up as a bootdev.
9
10So, introduce a loadm command that will be consistent with the other
11loadX commands and will call the efi API's.
12
13ex: loadm $kernel_addr $kernel_addr_r $kernel_size
14
15with this a kernel with CONFIG_EFI_STUB enabled will be loaded and
16then subsequently booted with bootefi command.
17
18Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
19---
20 README | 1 +
21 cmd/Kconfig | 6 ++++
22 cmd/bootefi.c | 12 ++++++++
23 cmd/load.c | 48 ++++++++++++++++++++++++++++++++
24 include/efi_loader.h | 2 ++
25 lib/efi_loader/efi_device_path.c | 9 ++++++
26 6 files changed, 78 insertions(+)
27
28diff --git a/README b/README
29index f51f392111f9..049bcd108980 100644
30--- a/README
31+++ b/README
32@@ -2760,6 +2760,7 @@ rarpboot- boot image via network using RARP/TFTP protocol
33 diskboot- boot from IDE devicebootd - boot default, i.e., run 'bootcmd'
34 loads - load S-Record file over serial line
35 loadb - load binary file over serial line (kermit mode)
36+loadm - load binary blob from source address to destination address
37 md - memory display
38 mm - memory modify (auto-incrementing)
39 nm - memory modify (constant address)
40diff --git a/cmd/Kconfig b/cmd/Kconfig
41index 5e25e45fd288..ff50102a89c7 100644
42--- a/cmd/Kconfig
43+++ b/cmd/Kconfig
44@@ -1076,6 +1076,12 @@ config CMD_LOADB
45 help
46 Load a binary file over serial line.
47
48+config CMD_LOADM
49+ bool "loadm"
50+ default y
51+ help
52+ Load a binary over memory mapped.
53+
54 config CMD_LOADS
55 bool "loads"
56 default y
57diff --git a/cmd/bootefi.c b/cmd/bootefi.c
58index 53d9f0e0dcca..8d492ea9e70c 100644
59--- a/cmd/bootefi.c
60+++ b/cmd/bootefi.c
61@@ -34,6 +34,18 @@ static struct efi_device_path *bootefi_device_path;
62 static void *image_addr;
63 static size_t image_size;
64
65+/**
66+ * efi_get_image_parameters() - return image parameters
67+ *
68+ * @img_addr: address of loaded image in memory
69+ * @img_size: size of loaded image
70+ */
71+void efi_get_image_parameters(void **img_addr, size_t *img_size)
72+{
73+ *img_addr = image_addr;
74+ *img_size = image_size;
75+}
76+
77 /**
78 * efi_clear_bootdev() - clear boot device
79 */
80diff --git a/cmd/load.c b/cmd/load.c
81index 7e4a552d90ef..1224a7f85bb3 100644
82--- a/cmd/load.c
83+++ b/cmd/load.c
84@@ -1063,6 +1063,44 @@ static ulong load_serial_ymodem(ulong offset, int mode)
85
86 #endif
87
88+#if defined(CONFIG_CMD_LOADM)
89+static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
90+ char *const argv[])
91+{
92+ ulong addr, dest, size;
93+ void *src, *dst;
94+
95+ if (argc != 4)
96+ return CMD_RET_USAGE;
97+
98+ addr = simple_strtoul(argv[1], NULL, 16);
99+
100+ dest = simple_strtoul(argv[2], NULL, 16);
101+
102+ size = simple_strtoul(argv[3], NULL, 16);
103+
104+ if (!size) {
105+ printf("loadm: can not load zero bytes\n");
106+ return 1;
107+ }
108+
109+ src = map_sysmem(addr, size);
110+ dst = map_sysmem(dest, size);
111+
112+ memcpy(dst, src, size);
113+
114+ unmap_sysmem(src);
115+ unmap_sysmem(dst);
116+
117+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
118+ efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
119+
120+ printf("loaded bin to memory: size: %lu\n", size);
121+
122+ return 0;
123+}
124+#endif
125+
126 /* -------------------------------------------------------------------- */
127
128 #if defined(CONFIG_CMD_LOADS)
129@@ -1137,3 +1175,13 @@ U_BOOT_CMD(
130 );
131
132 #endif /* CONFIG_CMD_LOADB */
133+
134+#if defined(CONFIG_CMD_LOADM)
135+U_BOOT_CMD(
136+ loadm, 4, 0, do_load_memory_bin,
137+ "load binary blob from source address to destination address",
138+ "[src_addr] [dst_addr] [size]\n"
139+ " - load a binary blob from one memory location to other"
140+ " from src_addr to dst_addr by size bytes"
141+);
142+#endif /* CONFIG_CMD_LOADM */
143diff --git a/include/efi_loader.h b/include/efi_loader.h
144index af36639ec6a7..126db279dd3e 100644
145--- a/include/efi_loader.h
146+++ b/include/efi_loader.h
147@@ -585,6 +585,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
148 void efi_save_gd(void);
149 /* Call this to relocate the runtime section to an address space */
150 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
151+/* Call this to get image parameters */
152+void efi_get_image_parameters(void **img_addr, size_t *img_size);
153 /* Add a new object to the object list. */
154 void efi_add_handle(efi_handle_t obj);
155 /* Create handle */
156diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
157index 0542aaae16c7..d8dc59b2c95c 100644
158--- a/lib/efi_loader/efi_device_path.c
159+++ b/lib/efi_loader/efi_device_path.c
160@@ -1138,6 +1138,8 @@ efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
161 {
162 struct blk_desc *desc = NULL;
163 struct disk_partition fs_partition;
164+ size_t image_size;
165+ void *image_addr;
166 int part = 0;
167 char *filename;
168 char *s;
169@@ -1153,6 +1155,13 @@ efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
170 } else if (!strcmp(dev, "Uart")) {
171 if (device)
172 *device = efi_dp_from_uart();
173+ } else if (!strcmp(dev, "Mem")) {
174+ efi_get_image_parameters(&image_addr, &image_size);
175+
176+ if (device)
177+ *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
178+ (uintptr_t)image_addr,
179+ image_size);
180 } else {
181 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
182 1);
183--
1842.30.2
185