blob: 2047248f39b0a3f608f7b33329f186508ea62c1e [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
Sean Anderson531d8372023-11-08 11:48:55 -050046 load.read = smh_fit_read;
47 spl_set_bl_len(&load, 1);
48 load.priv = &fd;
49 ret = spl_load(spl_image, bootdev, &load, len, 0);
Sean Anderson89346ad2022-03-22 16:59:19 -040050 if (ret)
51 log_debug("could not read %s: %d\n", filename, ret);
52out:
53 smh_close(fd);
54 return ret;
55}
56SPL_LOAD_IMAGE_METHOD("SEMIHOSTING", 0, BOOT_DEVICE_SMH, spl_smh_load_image);