Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2023 |
| 4 | * Ventana Micro Systems Inc. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <spl.h> |
| 10 | #include <image.h> |
| 11 | #include <fs.h> |
Sean Anderson | b27c5f8 | 2023-11-08 11:48:41 -0500 | [diff] [blame^] | 12 | #include <asm/cache.h> |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 13 | #include <asm/io.h> |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 14 | |
| 15 | struct blk_dev { |
| 16 | const char *ifname; |
| 17 | char dev_part_str[8]; |
| 18 | }; |
| 19 | |
| 20 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 21 | ulong size, void *buf) |
| 22 | { |
| 23 | loff_t actlen; |
| 24 | int ret; |
| 25 | struct blk_dev *dev = (struct blk_dev *)load->priv; |
| 26 | |
| 27 | ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY); |
| 28 | if (ret) { |
| 29 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 30 | dev->ifname, dev->dev_part_str, ret); |
| 31 | return ret; |
| 32 | } |
| 33 | |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 34 | ret = fs_read(load->filename, virt_to_phys(buf), file_offset, size, |
| 35 | &actlen); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 36 | if (ret < 0) { |
| 37 | printf("spl: error reading image %s. Err - %d\n", |
| 38 | load->filename, ret); |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | return actlen; |
| 43 | } |
| 44 | |
| 45 | int spl_blk_load_image(struct spl_image_info *spl_image, |
| 46 | struct spl_boot_device *bootdev, |
| 47 | enum uclass_id uclass_id, int devnum, int partnum) |
| 48 | { |
Heinrich Schuchardt | 5048844 | 2023-07-21 14:09:43 +0200 | [diff] [blame] | 49 | const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 50 | struct legacy_img_hdr *header; |
| 51 | struct blk_desc *blk_desc; |
| 52 | loff_t actlen, filesize; |
| 53 | struct blk_dev dev; |
Sean Anderson | cc35160 | 2023-11-08 11:48:33 -0500 | [diff] [blame] | 54 | int ret = -ENODEV; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 55 | |
| 56 | blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum); |
| 57 | if (!blk_desc) { |
| 58 | printf("blk desc for %d %d not found\n", uclass_id, devnum); |
Heinrich Schuchardt | 6eba2bc | 2023-09-06 14:25:11 +0200 | [diff] [blame] | 59 | return -ENODEV; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | blk_show_device(uclass_id, devnum); |
| 63 | header = spl_get_load_buffer(-sizeof(*header), sizeof(*header)); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 64 | |
| 65 | dev.ifname = blk_get_uclass_name(uclass_id); |
Heinrich Schuchardt | 83a91f3 | 2023-07-22 12:45:44 +0200 | [diff] [blame] | 66 | snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x", |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 67 | devnum, partnum); |
| 68 | ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY); |
| 69 | if (ret) { |
| 70 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 71 | dev.ifname, dev.dev_part_str, ret); |
| 72 | goto out; |
| 73 | } |
| 74 | |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 75 | ret = fs_read(filename, virt_to_phys(header), 0, |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 76 | sizeof(struct legacy_img_hdr), &actlen); |
| 77 | if (ret) { |
| 78 | printf("spl: unable to read file %s. Err - %d\n", filename, |
| 79 | ret); |
| 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 84 | image_get_magic(header) == FDT_MAGIC) { |
| 85 | struct spl_load_info load; |
| 86 | |
| 87 | debug("Found FIT\n"); |
| 88 | load.read = spl_fit_read; |
Sean Anderson | b27c5f8 | 2023-11-08 11:48:41 -0500 | [diff] [blame^] | 89 | load.bl_len = ARCH_DMA_MINALIGN; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 90 | load.filename = (void *)filename; |
| 91 | load.priv = &dev; |
| 92 | |
| 93 | return spl_load_simple_fit(spl_image, &load, 0, header); |
| 94 | } |
| 95 | |
| 96 | ret = spl_parse_image_header(spl_image, bootdev, header); |
| 97 | if (ret) { |
| 98 | printf("spl: unable to parse image header. Err - %d\n", |
| 99 | ret); |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY); |
| 104 | if (ret) { |
| 105 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 106 | dev.ifname, dev.dev_part_str, ret); |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | ret = fs_size(filename, &filesize); |
| 111 | if (ret) { |
| 112 | printf("spl: unable to get file size: %s. Err - %d\n", |
| 113 | filename, ret); |
| 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY); |
| 118 | if (ret) { |
| 119 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 120 | dev.ifname, dev.dev_part_str, ret); |
| 121 | goto out; |
| 122 | } |
| 123 | |
| 124 | ret = fs_read(filename, (ulong)spl_image->load_addr, 0, filesize, |
| 125 | &actlen); |
| 126 | if (ret) |
| 127 | printf("spl: unable to read file %s. Err - %d\n", |
| 128 | filename, ret); |
| 129 | out: |
| 130 | return ret; |
| 131 | } |