blob: f36863fe48d25859ee9af67a67140893b6e9e262 [file] [log] [blame]
Sean Anderson89346ad2022-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
Sean Anderson89346ad2022-03-22 16:59:19 -04006#include <image.h>
7#include <log.h>
8#include <semihosting.h>
9#include <spl.h>
Sean Anderson531d8372023-11-08 11:48:55 -050010#include <spl_load.h>
Sean Anderson89346ad2022-03-22 16:59:19 -040011
Heinrich Schuchardt22e22a22023-07-22 21:27:48 +020012static ulong smh_fit_read(struct spl_load_info *load, ulong file_offset,
13 ulong size, void *buf)
14{
Sean Anderson0087ec02023-11-08 11:48:36 -050015 long fd = *(long *)load->priv;
Heinrich Schuchardt22e22a22023-07-22 21:27:48 +020016 ulong ret;
17
Sean Anderson0087ec02023-11-08 11:48:36 -050018 if (smh_seek(fd, file_offset))
Heinrich Schuchardt22e22a22023-07-22 21:27:48 +020019 return 0;
Heinrich Schuchardt22e22a22023-07-22 21:27:48 +020020
Sean Anderson0087ec02023-11-08 11:48:36 -050021 ret = smh_read(fd, buf, size);
22 return ret < 0 ? 0 : ret;
Heinrich Schuchardt22e22a22023-07-22 21:27:48 +020023}
24
Sean Anderson89346ad2022-03-22 16:59:19 -040025static int spl_smh_load_image(struct spl_image_info *spl_image,
26 struct spl_boot_device *bootdev)
27{
28 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
29 int ret;
30 long fd, len;
Sean Anderson531d8372023-11-08 11:48:55 -050031 struct spl_load_info load;
Sean Anderson89346ad2022-03-22 16:59:19 -040032
33 fd = smh_open(filename, MODE_READ | MODE_BINARY);
34 if (fd < 0) {
35 log_debug("could not open %s: %ld\n", filename, fd);
36 return fd;
37 }
38
39 ret = smh_flen(fd);
40 if (ret < 0) {
41 log_debug("could not get length of image: %d\n", ret);
42 goto out;
43 }
44 len = ret;
45
Simon Glasse8066862024-08-22 07:55:02 -060046 spl_load_init(&load, smh_fit_read, &fd, 1);
Sean Anderson531d8372023-11-08 11:48:55 -050047 ret = spl_load(spl_image, bootdev, &load, len, 0);
Sean Anderson89346ad2022-03-22 16:59:19 -040048 if (ret)
49 log_debug("could not read %s: %d\n", filename, ret);
50out:
51 smh_close(fd);
52 return ret;
53}
54SPL_LOAD_IMAGE_METHOD("SEMIHOSTING", 0, BOOT_DEVICE_SMH, spl_smh_load_image);