blob: fe052236053c5959b8f632ce43c1ee4795938569 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guillaume GARDET592f9222014-10-15 17:53:12 +02002
3#include <common.h>
4#include <spl.h>
5#include <asm/u-boot.h>
6#include <ext4fs.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +02007#include <errno.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02008#include <image.h>
9
Simon Glassb4a6c2a2016-09-24 18:20:14 -060010int spl_load_image_ext(struct spl_image_info *spl_image,
11 struct blk_desc *block_dev, int partition,
12 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020013{
14 s32 err;
15 struct image_header *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080016 loff_t filelen, actlen;
Guillaume GARDET592f9222014-10-15 17:53:12 +020017 disk_partition_t part_info = {};
18
Marek Vasut04ce5422018-08-14 11:27:02 +020019 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET592f9222014-10-15 17:53:12 +020020
Simon Glass3e8bd462016-02-29 15:25:48 -070021 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020022 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 Ramasami9f12cd02014-11-17 14:39:36 -080036 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020037 if (err < 0) {
38 puts("spl: ext4fs_open failed\n");
39 goto end;
40 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010041 err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010042 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020043 puts("spl: ext4fs_read failed\n");
44 goto end;
45 }
46
Simon Glassb4a6c2a2016-09-24 18:20:14 -060047 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020048 if (err < 0) {
Petr Kulhavy9ab165d2016-06-18 12:21:17 +020049 puts("spl: ext: failed to parse image header\n");
Marek Vasut7e0f2262016-04-29 00:44:54 +020050 goto end;
51 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020052
Stefan Brüns66a47ff2016-11-06 18:33:57 +010053 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020054
55end:
56#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010057 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020058 printf("%s: error reading image %s, err - %d\n",
59 __func__, filename, err);
60#endif
61
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010062 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020063}
64
65#ifdef CONFIG_SPL_OS_BOOT
Simon Glassb4a6c2a2016-09-24 18:20:14 -060066int spl_load_image_ext_os(struct spl_image_info *spl_image,
67 struct blk_desc *block_dev, int partition)
Guillaume GARDET592f9222014-10-15 17:53:12 +020068{
69 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080070 __maybe_unused loff_t filelen, actlen;
Guillaume GARDET592f9222014-10-15 17:53:12 +020071 disk_partition_t part_info = {};
72 __maybe_unused char *file;
73
Simon Glass3e8bd462016-02-29 15:25:48 -070074 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020075 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 Kulhavy58c95d52016-06-14 12:06:36 +020088#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass00caae62017-08-03 12:22:12 -060089 file = env_get("falcon_args_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +020090 if (file) {
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080091 err = ext4fs_open(file, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020092 if (err < 0) {
93 puts("spl: ext4fs_open failed\n");
94 goto defaults;
95 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010096 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010097 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020098 printf("spl: error reading image %s, err - %d, falling back to default\n",
99 file, err);
100 goto defaults;
101 }
Simon Glass00caae62017-08-03 12:22:12 -0600102 file = env_get("falcon_image_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +0200103 if (file) {
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600104 err = spl_load_image_ext(spl_image, block_dev,
105 partition, file);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200106 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
119defaults:
120#endif
121
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800122 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200123 if (err < 0)
124 puts("spl: ext4fs_open failed\n");
125
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100126 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100127 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200128#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 Glassb4a6c2a2016-09-24 18:20:14 -0600135 return spl_load_image_ext(spl_image, block_dev, partition,
Guillaume GARDET592f9222014-10-15 17:53:12 +0200136 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
137}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200138#else
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600139int spl_load_image_ext_os(struct spl_image_info *spl_image,
140 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200141{
142 return -ENOSYS;
143}
Guillaume GARDET592f9222014-10-15 17:53:12 +0200144#endif