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