Guillaume GARDET | 1eb410c | 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 | 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 | |
| 12 | #ifdef CONFIG_SPL_EXT_SUPPORT |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 13 | int spl_load_image_ext(struct blk_desc *block_dev, |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 14 | int partition, |
| 15 | const char *filename) |
| 16 | { |
| 17 | s32 err; |
| 18 | struct image_header *header; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 19 | loff_t filelen, actlen; |
Guillaume GARDET | 1eb410c | 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 | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 25 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 1eb410c | 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 | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 40 | err = ext4fs_open(filename, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 41 | if (err < 0) { |
| 42 | puts("spl: ext4fs_open failed\n"); |
| 43 | goto end; |
| 44 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 45 | err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen); |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 46 | if (err < 0) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 47 | puts("spl: ext4fs_read failed\n"); |
| 48 | goto end; |
| 49 | } |
| 50 | |
| 51 | spl_parse_image_header(header); |
| 52 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 53 | err = ext4fs_read((char *)spl_image.load_addr, 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 | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 66 | int spl_load_image_ext_os(struct blk_desc *block_dev, int partition) |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 67 | { |
| 68 | int err; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 69 | __maybe_unused loff_t filelen, actlen; |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 70 | disk_partition_t part_info = {}; |
| 71 | __maybe_unused char *file; |
| 72 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 73 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 74 | printf("spl: no partition table found\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 79 | |
| 80 | err = ext4fs_mount(0); |
| 81 | if (!err) { |
| 82 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 83 | printf("%s: ext4fs mount err - %d\n", __func__, err); |
| 84 | #endif |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
| 89 | file = getenv("falcon_args_file"); |
| 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 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 96 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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 | } |
| 102 | file = getenv("falcon_image_file"); |
| 103 | if (file) { |
| 104 | err = spl_load_image_ext(block_dev, partition, file); |
| 105 | if (err != 0) { |
| 106 | puts("spl: falling back to default\n"); |
| 107 | goto defaults; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } else { |
| 112 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 113 | } |
| 114 | } else { |
| 115 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 116 | } |
| 117 | |
| 118 | defaults: |
| 119 | #endif |
| 120 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 121 | err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 122 | if (err < 0) |
| 123 | puts("spl: ext4fs_open failed\n"); |
| 124 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 125 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen); |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 126 | if (err < 0) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 127 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 128 | printf("%s: error reading image %s, err - %d\n", |
| 129 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
| 130 | #endif |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | return spl_load_image_ext(block_dev, partition, |
| 135 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
| 136 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 137 | #else |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 138 | int spl_load_image_ext_os(struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 139 | { |
| 140 | return -ENOSYS; |
| 141 | } |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 142 | #endif |
| 143 | #endif |