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 |
| 13 | int spl_load_image_ext(block_dev_desc_t *block_dev, |
| 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 | |
| 25 | if (get_partition_info(block_dev, |
| 26 | partition, &part_info)) { |
| 27 | printf("spl: no partition table found\n"); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 32 | |
| 33 | err = ext4fs_mount(0); |
| 34 | if (!err) { |
| 35 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 36 | printf("%s: ext4fs mount err - %d\n", __func__, err); |
| 37 | #endif |
| 38 | goto end; |
| 39 | } |
| 40 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 41 | err = ext4fs_open(filename, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 42 | if (err < 0) { |
| 43 | puts("spl: ext4fs_open failed\n"); |
| 44 | goto end; |
| 45 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 46 | err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen); |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 47 | if (err < 0) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 48 | puts("spl: ext4fs_read failed\n"); |
| 49 | goto end; |
| 50 | } |
| 51 | |
| 52 | spl_parse_image_header(header); |
| 53 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 54 | err = ext4fs_read((char *)spl_image.load_addr, 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 |
| 67 | int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition) |
| 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 | |
| 74 | if (get_partition_info(block_dev, |
| 75 | partition, &part_info)) { |
| 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 | } |
| 89 | |
| 90 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
| 91 | file = getenv("falcon_args_file"); |
| 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 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 98 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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 | } |
| 104 | file = getenv("falcon_image_file"); |
| 105 | if (file) { |
| 106 | err = spl_load_image_ext(block_dev, partition, file); |
| 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 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 127 | err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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 | |
| 136 | return spl_load_image_ext(block_dev, partition, |
| 137 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
| 138 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 139 | #else |
| 140 | int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition) |
| 141 | { |
| 142 | return -ENOSYS; |
| 143 | } |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 144 | #endif |
| 145 | #endif |