blob: 2a6252229ca157f2b3534ea12214bfb0cbc412fb [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>
Simon Glass7b51b572019-08-01 09:46:52 -06004#include <env.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02005#include <spl.h>
6#include <asm/u-boot.h>
7#include <ext4fs.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +02008#include <errno.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02009#include <image.h>
10
Simon Glassb4a6c2a2016-09-24 18:20:14 -060011int spl_load_image_ext(struct spl_image_info *spl_image,
12 struct blk_desc *block_dev, int partition,
13 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020014{
15 s32 err;
16 struct image_header *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080017 loff_t filelen, actlen;
Guillaume GARDET592f9222014-10-15 17:53:12 +020018 disk_partition_t part_info = {};
19
Marek Vasut04ce5422018-08-14 11:27:02 +020020 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET592f9222014-10-15 17:53:12 +020021
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