blob: fd30a61f9adbed0728e488b068187fcf9f7d70f1 [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
19 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
20 sizeof(struct image_header));
21
Simon Glass3e8bd462016-02-29 15:25:48 -070022 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020023 printf("spl: no partition table found\n");
24 return -1;
25 }
26
27 ext4fs_set_blk_dev(block_dev, &part_info);
28
29 err = ext4fs_mount(0);
30 if (!err) {
31#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
32 printf("%s: ext4fs mount err - %d\n", __func__, err);
33#endif
34 goto end;
35 }
36
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080037 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020038 if (err < 0) {
39 puts("spl: ext4fs_open failed\n");
40 goto end;
41 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010042 err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010043 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020044 puts("spl: ext4fs_read failed\n");
45 goto end;
46 }
47
Simon Glassb4a6c2a2016-09-24 18:20:14 -060048 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020049 if (err < 0) {
Petr Kulhavy9ab165d2016-06-18 12:21:17 +020050 puts("spl: ext: failed to parse image header\n");
Marek Vasut7e0f2262016-04-29 00:44:54 +020051 goto end;
52 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020053
Stefan Brüns66a47ff2016-11-06 18:33:57 +010054 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020055
56end:
57#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010058 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020059 printf("%s: error reading image %s, err - %d\n",
60 __func__, filename, err);
61#endif
62
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010063 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020064}
65
66#ifdef CONFIG_SPL_OS_BOOT
Simon Glassb4a6c2a2016-09-24 18:20:14 -060067int spl_load_image_ext_os(struct spl_image_info *spl_image,
68 struct blk_desc *block_dev, int partition)
Guillaume GARDET592f9222014-10-15 17:53:12 +020069{
70 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080071 __maybe_unused loff_t filelen, actlen;
Guillaume GARDET592f9222014-10-15 17:53:12 +020072 disk_partition_t part_info = {};
73 __maybe_unused char *file;
74
Simon Glass3e8bd462016-02-29 15:25:48 -070075 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020076 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 }
Petr Kulhavy58c95d52016-06-14 12:06:36 +020089#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass00caae62017-08-03 12:22:12 -060090 file = env_get("falcon_args_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +020091 if (file) {
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080092 err = ext4fs_open(file, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020093 if (err < 0) {
94 puts("spl: ext4fs_open failed\n");
95 goto defaults;
96 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010097 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010098 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020099 printf("spl: error reading image %s, err - %d, falling back to default\n",
100 file, err);
101 goto defaults;
102 }
Simon Glass00caae62017-08-03 12:22:12 -0600103 file = env_get("falcon_image_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +0200104 if (file) {
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600105 err = spl_load_image_ext(spl_image, block_dev,
106 partition, file);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200107 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
120defaults:
121#endif
122
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800123 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200124 if (err < 0)
125 puts("spl: ext4fs_open failed\n");
126
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100127 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100128 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200129#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
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600136 return spl_load_image_ext(spl_image, block_dev, partition,
Guillaume GARDET592f9222014-10-15 17:53:12 +0200137 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
138}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200139#else
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600140int spl_load_image_ext_os(struct spl_image_info *spl_image,
141 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200142{
143 return -ENOSYS;
144}
Guillaume GARDET592f9222014-10-15 17:53:12 +0200145#endif