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