blob: 3898041d106c926e85016bc69330b6dd7fba7745 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02002
3#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06004#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -06005#include <part.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02006#include <spl.h>
7#include <asm/u-boot.h>
8#include <ext4fs.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +02009#include <errno.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020010#include <image.h>
11
Simon Glass0649e912016-09-24 18:20:14 -060012int spl_load_image_ext(struct spl_image_info *spl_image,
13 struct blk_desc *block_dev, int partition,
14 const char *filename)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020015{
16 s32 err;
17 struct image_header *header;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080018 loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060019 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020020
Marek Vasut1dda46f2018-08-14 11:27:02 +020021 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020022
Simon Glassb89a8442016-02-29 15:25:48 -070023 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020024 printf("spl: no partition table found\n");
25 return -1;
26 }
27
28 ext4fs_set_blk_dev(block_dev, &part_info);
29
30 err = ext4fs_mount(0);
31 if (!err) {
32#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
33 printf("%s: ext4fs mount err - %d\n", __func__, err);
34#endif
35 goto end;
36 }
37
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080038 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020039 if (err < 0) {
40 puts("spl: ext4fs_open failed\n");
41 goto end;
42 }
Stefan Brünsdb5862d2016-11-06 18:33:57 +010043 err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010044 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020045 puts("spl: ext4fs_read failed\n");
46 goto end;
47 }
48
Simon Glass0649e912016-09-24 18:20:14 -060049 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020050 if (err < 0) {
Petr Kulhavy67715122016-06-18 12:21:17 +020051 puts("spl: ext: failed to parse image header\n");
Marek Vasut02266e22016-04-29 00:44:54 +020052 goto end;
53 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020054
Stefan Brünsdb5862d2016-11-06 18:33:57 +010055 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020056
57end:
58#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010059 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020060 printf("%s: error reading image %s, err - %d\n",
61 __func__, filename, err);
62#endif
63
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010064 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020065}
66
67#ifdef CONFIG_SPL_OS_BOOT
Simon Glass0649e912016-09-24 18:20:14 -060068int spl_load_image_ext_os(struct spl_image_info *spl_image,
69 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020070{
71 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080072 __maybe_unused loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060073 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020074 __maybe_unused char *file;
75
Simon Glassb89a8442016-02-29 15:25:48 -070076 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020077 printf("spl: no partition table found\n");
78 return -1;
79 }
80
81 ext4fs_set_blk_dev(block_dev, &part_info);
82
83 err = ext4fs_mount(0);
84 if (!err) {
85#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
86 printf("%s: ext4fs mount err - %d\n", __func__, err);
87#endif
88 return -1;
89 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020090#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass64b723f2017-08-03 12:22:12 -060091 file = env_get("falcon_args_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020092 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080093 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020094 if (err < 0) {
95 puts("spl: ext4fs_open failed\n");
96 goto defaults;
97 }
Stefan Brünsdb5862d2016-11-06 18:33:57 +010098 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010099 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200100 printf("spl: error reading image %s, err - %d, falling back to default\n",
101 file, err);
102 goto defaults;
103 }
Simon Glass64b723f2017-08-03 12:22:12 -0600104 file = env_get("falcon_image_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200105 if (file) {
Simon Glass0649e912016-09-24 18:20:14 -0600106 err = spl_load_image_ext(spl_image, block_dev,
107 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200108 if (err != 0) {
109 puts("spl: falling back to default\n");
110 goto defaults;
111 }
112
113 return 0;
114 } else {
115 puts("spl: falcon_image_file not set in environment, falling back to default\n");
116 }
117 } else {
118 puts("spl: falcon_args_file not set in environment, falling back to default\n");
119 }
120
121defaults:
122#endif
123
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800124 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200125 if (err < 0)
126 puts("spl: ext4fs_open failed\n");
127
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100128 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100129 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200130#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
131 printf("%s: error reading image %s, err - %d\n",
132 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
133#endif
134 return -1;
135 }
136
Simon Glass0649e912016-09-24 18:20:14 -0600137 return spl_load_image_ext(spl_image, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200138 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
139}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200140#else
Simon Glass0649e912016-09-24 18:20:14 -0600141int spl_load_image_ext_os(struct spl_image_info *spl_image,
142 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200143{
144 return -ENOSYS;
145}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200146#endif