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