blob: 941fa911040ea31b6439a8564149e3e7c04131b2 [file] [log] [blame]
Sean Anderson8e1c9fe2022-03-22 16:59:19 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
4 */
5
6#include <common.h>
7#include <image.h>
8#include <log.h>
9#include <semihosting.h>
10#include <spl.h>
Sean Anderson9b9c6aa2023-11-08 11:48:55 -050011#include <spl_load.h>
Sean Anderson8e1c9fe2022-03-22 16:59:19 -040012
Heinrich Schuchardt688d62b2023-07-22 21:27:48 +020013static ulong smh_fit_read(struct spl_load_info *load, ulong file_offset,
14 ulong size, void *buf)
15{
Sean Anderson517e7082023-11-08 11:48:36 -050016 long fd = *(long *)load->priv;
Heinrich Schuchardt688d62b2023-07-22 21:27:48 +020017 ulong ret;
18
Sean Anderson517e7082023-11-08 11:48:36 -050019 if (smh_seek(fd, file_offset))
Heinrich Schuchardt688d62b2023-07-22 21:27:48 +020020 return 0;
Heinrich Schuchardt688d62b2023-07-22 21:27:48 +020021
Sean Anderson517e7082023-11-08 11:48:36 -050022 ret = smh_read(fd, buf, size);
23 return ret < 0 ? 0 : ret;
Heinrich Schuchardt688d62b2023-07-22 21:27:48 +020024}
25
Sean Anderson8e1c9fe2022-03-22 16:59:19 -040026static int spl_smh_load_image(struct spl_image_info *spl_image,
27 struct spl_boot_device *bootdev)
28{
29 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
30 int ret;
31 long fd, len;
Sean Anderson9b9c6aa2023-11-08 11:48:55 -050032 struct spl_load_info load;
Sean Anderson8e1c9fe2022-03-22 16:59:19 -040033
34 fd = smh_open(filename, MODE_READ | MODE_BINARY);
35 if (fd < 0) {
36 log_debug("could not open %s: %ld\n", filename, fd);
37 return fd;
38 }
39
40 ret = smh_flen(fd);
41 if (ret < 0) {
42 log_debug("could not get length of image: %d\n", ret);
43 goto out;
44 }
45 len = ret;
46
Sean Anderson9b9c6aa2023-11-08 11:48:55 -050047 load.read = smh_fit_read;
48 spl_set_bl_len(&load, 1);
49 load.priv = &fd;
50 ret = spl_load(spl_image, bootdev, &load, len, 0);
Sean Anderson8e1c9fe2022-03-22 16:59:19 -040051 if (ret)
52 log_debug("could not read %s: %d\n", filename, ret);
53out:
54 smh_close(fd);
55 return ret;
56}
57SPL_LOAD_IMAGE_METHOD("SEMIHOSTING", 0, BOOT_DEVICE_SMH, spl_smh_load_image);