Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0+ |
| 3 | */ |
| 4 | |
| 5 | #include <common.h> |
| 6 | #include <spl.h> |
| 7 | #include <asm/u-boot.h> |
| 8 | #include <ext4fs.h> |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 9 | #include <errno.h> |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 10 | #include <image.h> |
| 11 | |
| 12 | #ifdef CONFIG_SPL_EXT_SUPPORT |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 13 | int spl_load_image_ext(struct spl_image_info *spl_image, |
| 14 | struct blk_desc *block_dev, int partition, |
| 15 | const char *filename) |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 16 | { |
| 17 | s32 err; |
| 18 | struct image_header *header; |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 19 | loff_t filelen, actlen; |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 20 | disk_partition_t part_info = {}; |
| 21 | |
| 22 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 23 | sizeof(struct image_header)); |
| 24 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 25 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 26 | printf("spl: no partition table found\n"); |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 31 | |
| 32 | err = ext4fs_mount(0); |
| 33 | if (!err) { |
| 34 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 35 | printf("%s: ext4fs mount err - %d\n", __func__, err); |
| 36 | #endif |
| 37 | goto end; |
| 38 | } |
| 39 | |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 40 | err = ext4fs_open(filename, &filelen); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 41 | if (err < 0) { |
| 42 | puts("spl: ext4fs_open failed\n"); |
| 43 | goto end; |
| 44 | } |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 45 | err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen); |
Guillaume GARDET | d3e488e | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 46 | if (err < 0) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 47 | puts("spl: ext4fs_read failed\n"); |
| 48 | goto end; |
| 49 | } |
| 50 | |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 51 | err = spl_parse_image_header(spl_image, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 52 | if (err < 0) { |
Petr Kulhavy | 9ab165d | 2016-06-18 12:21:17 +0200 | [diff] [blame] | 53 | puts("spl: ext: failed to parse image header\n"); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 54 | goto end; |
| 55 | } |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 56 | |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 57 | err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 58 | |
| 59 | end: |
| 60 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Guillaume GARDET | d3e488e | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 61 | if (err < 0) |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 62 | printf("%s: error reading image %s, err - %d\n", |
| 63 | __func__, filename, err); |
| 64 | #endif |
| 65 | |
Guillaume GARDET | d3e488e | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 66 | return err < 0; |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 70 | int spl_load_image_ext_os(struct spl_image_info *spl_image, |
| 71 | struct blk_desc *block_dev, int partition) |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 72 | { |
| 73 | int err; |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 74 | __maybe_unused loff_t filelen, actlen; |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 75 | disk_partition_t part_info = {}; |
| 76 | __maybe_unused char *file; |
| 77 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 78 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 79 | printf("spl: no partition table found\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 84 | |
| 85 | err = ext4fs_mount(0); |
| 86 | if (!err) { |
| 87 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 88 | printf("%s: ext4fs mount err - %d\n", __func__, err); |
| 89 | #endif |
| 90 | return -1; |
| 91 | } |
Petr Kulhavy | 58c95d5 | 2016-06-14 12:06:36 +0200 | [diff] [blame] | 92 | #if defined(CONFIG_SPL_ENV_SUPPORT) |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 93 | file = getenv("falcon_args_file"); |
| 94 | if (file) { |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 95 | err = ext4fs_open(file, &filelen); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 96 | if (err < 0) { |
| 97 | puts("spl: ext4fs_open failed\n"); |
| 98 | goto defaults; |
| 99 | } |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 100 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen); |
Guillaume GARDET | d3e488e | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 101 | if (err < 0) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 102 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 103 | file, err); |
| 104 | goto defaults; |
| 105 | } |
| 106 | file = getenv("falcon_image_file"); |
| 107 | if (file) { |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 108 | err = spl_load_image_ext(spl_image, block_dev, |
| 109 | partition, file); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 110 | if (err != 0) { |
| 111 | puts("spl: falling back to default\n"); |
| 112 | goto defaults; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } else { |
| 117 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 118 | } |
| 119 | } else { |
| 120 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 121 | } |
| 122 | |
| 123 | defaults: |
| 124 | #endif |
| 125 | |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 126 | err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 127 | if (err < 0) |
| 128 | puts("spl: ext4fs_open failed\n"); |
| 129 | |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 130 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen); |
Guillaume GARDET | d3e488e | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 131 | if (err < 0) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 132 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 133 | printf("%s: error reading image %s, err - %d\n", |
| 134 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
| 135 | #endif |
| 136 | return -1; |
| 137 | } |
| 138 | |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 139 | return spl_load_image_ext(spl_image, block_dev, partition, |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 140 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
| 141 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 142 | #else |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 143 | int spl_load_image_ext_os(struct spl_image_info *spl_image, |
| 144 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 145 | { |
| 146 | return -ENOSYS; |
| 147 | } |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 148 | #endif |
| 149 | #endif |