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