1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2261d2760SDarwin Rambo /*
3261d2760SDarwin Rambo * Copyright 2014 Broadcom Corporation
4261d2760SDarwin Rambo */
5261d2760SDarwin Rambo
6261d2760SDarwin Rambo /*
7261d2760SDarwin Rambo * Minimal semihosting implementation for reading files into memory. If more
8261d2760SDarwin Rambo * features like writing files or console output are required they can be
9261d2760SDarwin Rambo * added later. This code has been tested on arm64/aarch64 fastmodel only.
10261d2760SDarwin Rambo * An untested placeholder exists for armv7 architectures, but since they
11261d2760SDarwin Rambo * are commonly available in silicon now, fastmodel usage makes less sense
12261d2760SDarwin Rambo * for them.
13261d2760SDarwin Rambo */
14261d2760SDarwin Rambo #include <common.h>
15202a674bSLinus Walleij #include <command.h>
16261d2760SDarwin Rambo
17261d2760SDarwin Rambo #define SYSOPEN 0x01
18261d2760SDarwin Rambo #define SYSCLOSE 0x02
19261d2760SDarwin Rambo #define SYSREAD 0x06
20261d2760SDarwin Rambo #define SYSFLEN 0x0C
21261d2760SDarwin Rambo
22261d2760SDarwin Rambo #define MODE_READ 0x0
23261d2760SDarwin Rambo #define MODE_READBIN 0x1
24261d2760SDarwin Rambo
25261d2760SDarwin Rambo /*
26261d2760SDarwin Rambo * Call the handler
27261d2760SDarwin Rambo */
smh_trap(unsigned int sysnum,void * addr)28e769f686SLinus Walleij static noinline long smh_trap(unsigned int sysnum, void *addr)
29261d2760SDarwin Rambo {
304e1ef150SLinus Walleij register long result asm("r0");
31261d2760SDarwin Rambo #if defined(CONFIG_ARM64)
32261d2760SDarwin Rambo asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
33432a6241SVadzim Dambrouski #elif defined(CONFIG_CPU_V7M)
34432a6241SVadzim Dambrouski asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
35261d2760SDarwin Rambo #else
36261d2760SDarwin Rambo /* Note - untested placeholder */
37261d2760SDarwin Rambo asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
38261d2760SDarwin Rambo #endif
39261d2760SDarwin Rambo return result;
40261d2760SDarwin Rambo }
41261d2760SDarwin Rambo
42261d2760SDarwin Rambo /*
439be5c661SLinus Walleij * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
449be5c661SLinus Walleij * descriptor or -1 on error.
459be5c661SLinus Walleij */
smh_open(const char * fname,char * modestr)469be5c661SLinus Walleij static long smh_open(const char *fname, char *modestr)
479be5c661SLinus Walleij {
489be5c661SLinus Walleij long fd;
499be5c661SLinus Walleij unsigned long mode;
509be5c661SLinus Walleij struct smh_open_s {
519be5c661SLinus Walleij const char *fname;
529be5c661SLinus Walleij unsigned long mode;
539be5c661SLinus Walleij size_t len;
549be5c661SLinus Walleij } open;
559be5c661SLinus Walleij
569be5c661SLinus Walleij debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
579be5c661SLinus Walleij
589be5c661SLinus Walleij /* Check the file mode */
599be5c661SLinus Walleij if (!(strcmp(modestr, "r"))) {
609be5c661SLinus Walleij mode = MODE_READ;
619be5c661SLinus Walleij } else if (!(strcmp(modestr, "rb"))) {
629be5c661SLinus Walleij mode = MODE_READBIN;
639be5c661SLinus Walleij } else {
649be5c661SLinus Walleij printf("%s: ERROR mode \'%s\' not supported\n", __func__,
659be5c661SLinus Walleij modestr);
669be5c661SLinus Walleij return -1;
679be5c661SLinus Walleij }
689be5c661SLinus Walleij
699be5c661SLinus Walleij open.fname = fname;
709be5c661SLinus Walleij open.len = strlen(fname);
719be5c661SLinus Walleij open.mode = mode;
729be5c661SLinus Walleij
739be5c661SLinus Walleij /* Open the file on the host */
749be5c661SLinus Walleij fd = smh_trap(SYSOPEN, &open);
759be5c661SLinus Walleij if (fd == -1)
769be5c661SLinus Walleij printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
779be5c661SLinus Walleij fname);
789be5c661SLinus Walleij
799be5c661SLinus Walleij return fd;
809be5c661SLinus Walleij }
819be5c661SLinus Walleij
829be5c661SLinus Walleij /*
839be5c661SLinus Walleij * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
849be5c661SLinus Walleij */
smh_read(long fd,void * memp,size_t len)859be5c661SLinus Walleij static long smh_read(long fd, void *memp, size_t len)
869be5c661SLinus Walleij {
879be5c661SLinus Walleij long ret;
889be5c661SLinus Walleij struct smh_read_s {
899be5c661SLinus Walleij long fd;
909be5c661SLinus Walleij void *memp;
919be5c661SLinus Walleij size_t len;
929be5c661SLinus Walleij } read;
939be5c661SLinus Walleij
947bdf75caSVadzim Dambrouski debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
959be5c661SLinus Walleij
969be5c661SLinus Walleij read.fd = fd;
979be5c661SLinus Walleij read.memp = memp;
989be5c661SLinus Walleij read.len = len;
999be5c661SLinus Walleij
1009be5c661SLinus Walleij ret = smh_trap(SYSREAD, &read);
1019be5c661SLinus Walleij if (ret < 0) {
1029be5c661SLinus Walleij /*
1039be5c661SLinus Walleij * The ARM handler allows for returning partial lengths,
1049be5c661SLinus Walleij * but in practice this never happens so rather than create
1059be5c661SLinus Walleij * hard to maintain partial read loops and such, just fail
1069be5c661SLinus Walleij * with an error message.
1079be5c661SLinus Walleij */
1087bdf75caSVadzim Dambrouski printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n",
1099be5c661SLinus Walleij __func__, ret, fd, len, memp);
1109be5c661SLinus Walleij return -1;
1119be5c661SLinus Walleij }
1129be5c661SLinus Walleij
1139be5c661SLinus Walleij return 0;
1149be5c661SLinus Walleij }
1159be5c661SLinus Walleij
1169be5c661SLinus Walleij /*
1179be5c661SLinus Walleij * Close the file using the file descriptor
1189be5c661SLinus Walleij */
smh_close(long fd)1199be5c661SLinus Walleij static long smh_close(long fd)
1209be5c661SLinus Walleij {
1219be5c661SLinus Walleij long ret;
1229be5c661SLinus Walleij
1239be5c661SLinus Walleij debug("%s: fd %ld\n", __func__, fd);
1249be5c661SLinus Walleij
1259be5c661SLinus Walleij ret = smh_trap(SYSCLOSE, &fd);
1269be5c661SLinus Walleij if (ret == -1)
1279be5c661SLinus Walleij printf("%s: ERROR fd %ld\n", __func__, fd);
1289be5c661SLinus Walleij
1299be5c661SLinus Walleij return ret;
1309be5c661SLinus Walleij }
1319be5c661SLinus Walleij
1329be5c661SLinus Walleij /*
1339be5c661SLinus Walleij * Get the file length from the file descriptor
1349be5c661SLinus Walleij */
smh_len_fd(long fd)1359be5c661SLinus Walleij static long smh_len_fd(long fd)
1369be5c661SLinus Walleij {
1379be5c661SLinus Walleij long ret;
1389be5c661SLinus Walleij
1399be5c661SLinus Walleij debug("%s: fd %ld\n", __func__, fd);
1409be5c661SLinus Walleij
1419be5c661SLinus Walleij ret = smh_trap(SYSFLEN, &fd);
1429be5c661SLinus Walleij if (ret == -1)
1439be5c661SLinus Walleij printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
1449be5c661SLinus Walleij
1459be5c661SLinus Walleij return ret;
1469be5c661SLinus Walleij }
1479be5c661SLinus Walleij
smh_load_file(const char * const name,ulong load_addr,ulong * end_addr)148202a674bSLinus Walleij static int smh_load_file(const char * const name, ulong load_addr,
149202a674bSLinus Walleij ulong *end_addr)
150202a674bSLinus Walleij {
151202a674bSLinus Walleij long fd;
152202a674bSLinus Walleij long len;
153202a674bSLinus Walleij long ret;
154202a674bSLinus Walleij
155202a674bSLinus Walleij fd = smh_open(name, "rb");
156202a674bSLinus Walleij if (fd == -1)
157202a674bSLinus Walleij return -1;
158202a674bSLinus Walleij
159202a674bSLinus Walleij len = smh_len_fd(fd);
160202a674bSLinus Walleij if (len < 0) {
161202a674bSLinus Walleij smh_close(fd);
162202a674bSLinus Walleij return -1;
163202a674bSLinus Walleij }
164202a674bSLinus Walleij
165202a674bSLinus Walleij ret = smh_read(fd, (void *)load_addr, len);
166202a674bSLinus Walleij smh_close(fd);
167202a674bSLinus Walleij
168202a674bSLinus Walleij if (ret == 0) {
169202a674bSLinus Walleij *end_addr = load_addr + len - 1;
170202a674bSLinus Walleij printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
171202a674bSLinus Walleij name,
172202a674bSLinus Walleij load_addr,
173202a674bSLinus Walleij *end_addr,
174202a674bSLinus Walleij len);
175202a674bSLinus Walleij } else {
176202a674bSLinus Walleij printf("read failed\n");
177202a674bSLinus Walleij return 0;
178202a674bSLinus Walleij }
179202a674bSLinus Walleij
180202a674bSLinus Walleij return 0;
181202a674bSLinus Walleij }
182202a674bSLinus Walleij
do_smhload(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])183202a674bSLinus Walleij static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
184202a674bSLinus Walleij {
185202a674bSLinus Walleij if (argc == 3 || argc == 4) {
186202a674bSLinus Walleij ulong load_addr;
187202a674bSLinus Walleij ulong end_addr = 0;
188072c8c4cSRyan Harkin int ret;
189202a674bSLinus Walleij char end_str[64];
190202a674bSLinus Walleij
191202a674bSLinus Walleij load_addr = simple_strtoul(argv[2], NULL, 16);
192202a674bSLinus Walleij if (!load_addr)
193202a674bSLinus Walleij return -1;
194202a674bSLinus Walleij
195202a674bSLinus Walleij ret = smh_load_file(argv[1], load_addr, &end_addr);
196202a674bSLinus Walleij if (ret < 0)
197072c8c4cSRyan Harkin return CMD_RET_FAILURE;
198202a674bSLinus Walleij
199202a674bSLinus Walleij /* Optionally save returned end to the environment */
200202a674bSLinus Walleij if (argc == 4) {
201202a674bSLinus Walleij sprintf(end_str, "0x%08lx", end_addr);
202382bee57SSimon Glass env_set(argv[3], end_str);
203202a674bSLinus Walleij }
204202a674bSLinus Walleij } else {
205202a674bSLinus Walleij return CMD_RET_USAGE;
206202a674bSLinus Walleij }
207202a674bSLinus Walleij return 0;
208202a674bSLinus Walleij }
209202a674bSLinus Walleij
210202a674bSLinus Walleij U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
211202a674bSLinus Walleij "<file> 0x<address> [end var]\n"
212202a674bSLinus Walleij " - load a semihosted file to the address specified\n"
213202a674bSLinus Walleij " if the optional [end var] is specified, the end\n"
214202a674bSLinus Walleij " address of the file will be stored in this environment\n"
215202a674bSLinus Walleij " variable.\n");
216