Sean Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
| 4 | */ |
| 5 | |
Sean Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 6 | #include <image.h> |
| 7 | #include <log.h> |
| 8 | #include <semihosting.h> |
| 9 | #include <spl.h> |
Sean Anderson | 531d837 | 2023-11-08 11:48:55 -0500 | [diff] [blame] | 10 | #include <spl_load.h> |
Sean Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 11 | |
Heinrich Schuchardt | 22e22a2 | 2023-07-22 21:27:48 +0200 | [diff] [blame] | 12 | static ulong smh_fit_read(struct spl_load_info *load, ulong file_offset, |
| 13 | ulong size, void *buf) |
| 14 | { |
Sean Anderson | 0087ec0 | 2023-11-08 11:48:36 -0500 | [diff] [blame] | 15 | long fd = *(long *)load->priv; |
Heinrich Schuchardt | 22e22a2 | 2023-07-22 21:27:48 +0200 | [diff] [blame] | 16 | ulong ret; |
| 17 | |
Sean Anderson | 0087ec0 | 2023-11-08 11:48:36 -0500 | [diff] [blame] | 18 | if (smh_seek(fd, file_offset)) |
Heinrich Schuchardt | 22e22a2 | 2023-07-22 21:27:48 +0200 | [diff] [blame] | 19 | return 0; |
Heinrich Schuchardt | 22e22a2 | 2023-07-22 21:27:48 +0200 | [diff] [blame] | 20 | |
Sean Anderson | 0087ec0 | 2023-11-08 11:48:36 -0500 | [diff] [blame] | 21 | ret = smh_read(fd, buf, size); |
| 22 | return ret < 0 ? 0 : ret; |
Heinrich Schuchardt | 22e22a2 | 2023-07-22 21:27:48 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Sean Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 25 | static 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 Anderson | 531d837 | 2023-11-08 11:48:55 -0500 | [diff] [blame] | 31 | struct spl_load_info load; |
Sean Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 32 | |
| 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 Anderson | 531d837 | 2023-11-08 11:48:55 -0500 | [diff] [blame] | 46 | 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 Anderson | 89346ad | 2022-03-22 16:59:19 -0400 | [diff] [blame] | 50 | if (ret) |
| 51 | log_debug("could not read %s: %d\n", filename, ret); |
| 52 | out: |
| 53 | smh_close(fd); |
| 54 | return ret; |
| 55 | } |
| 56 | SPL_LOAD_IMAGE_METHOD("SEMIHOSTING", 0, BOOT_DEVICE_SMH, spl_smh_load_image); |