blob: f17c6b949416c271c3172abb5d03856ca7b0b18e [file] [log] [blame]
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02001/*
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 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;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020019 disk_partition_t part_info = {};
20
21 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
22 sizeof(struct image_header));
23
Simon Glassb89a8442016-02-29 15:25:48 -070024 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020025 printf("spl: no partition table found\n");
26 return -1;
27 }
28
29 ext4fs_set_blk_dev(block_dev, &part_info);
30
31 err = ext4fs_mount(0);
32 if (!err) {
33#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
34 printf("%s: ext4fs mount err - %d\n", __func__, err);
35#endif
36 goto end;
37 }
38
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080039 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020040 if (err < 0) {
41 puts("spl: ext4fs_open failed\n");
42 goto end;
43 }
Stefan Brünsdb5862d2016-11-06 18:33:57 +010044 err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010045 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020046 puts("spl: ext4fs_read failed\n");
47 goto end;
48 }
49
Simon Glass0649e912016-09-24 18:20:14 -060050 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020051 if (err < 0) {
Petr Kulhavy67715122016-06-18 12:21:17 +020052 puts("spl: ext: failed to parse image header\n");
Marek Vasut02266e22016-04-29 00:44:54 +020053 goto end;
54 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020055
Stefan Brünsdb5862d2016-11-06 18:33:57 +010056 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020057
58end:
59#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010060 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020061 printf("%s: error reading image %s, err - %d\n",
62 __func__, filename, err);
63#endif
64
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010065 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020066}
67
68#ifdef CONFIG_SPL_OS_BOOT
Simon Glass0649e912016-09-24 18:20:14 -060069int spl_load_image_ext_os(struct spl_image_info *spl_image,
70 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020071{
72 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080073 __maybe_unused loff_t filelen, actlen;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020074 disk_partition_t part_info = {};
75 __maybe_unused char *file;
76
Simon Glassb89a8442016-02-29 15:25:48 -070077 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020078 printf("spl: no partition table found\n");
79 return -1;
80 }
81
82 ext4fs_set_blk_dev(block_dev, &part_info);
83
84 err = ext4fs_mount(0);
85 if (!err) {
86#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
87 printf("%s: ext4fs mount err - %d\n", __func__, err);
88#endif
89 return -1;
90 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020091#if defined(CONFIG_SPL_ENV_SUPPORT)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020092 file = getenv("falcon_args_file");
93 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080094 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020095 if (err < 0) {
96 puts("spl: ext4fs_open failed\n");
97 goto defaults;
98 }
Stefan Brünsdb5862d2016-11-06 18:33:57 +010099 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100100 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200101 printf("spl: error reading image %s, err - %d, falling back to default\n",
102 file, err);
103 goto defaults;
104 }
105 file = getenv("falcon_image_file");
106 if (file) {
Simon Glass0649e912016-09-24 18:20:14 -0600107 err = spl_load_image_ext(spl_image, block_dev,
108 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200109 if (err != 0) {
110 puts("spl: falling back to default\n");
111 goto defaults;
112 }
113
114 return 0;
115 } else {
116 puts("spl: falcon_image_file not set in environment, falling back to default\n");
117 }
118 } else {
119 puts("spl: falcon_args_file not set in environment, falling back to default\n");
120 }
121
122defaults:
123#endif
124
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800125 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200126 if (err < 0)
127 puts("spl: ext4fs_open failed\n");
128
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100129 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100130 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200131#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
132 printf("%s: error reading image %s, err - %d\n",
133 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
134#endif
135 return -1;
136 }
137
Simon Glass0649e912016-09-24 18:20:14 -0600138 return spl_load_image_ext(spl_image, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200139 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
140}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200141#else
Simon Glass0649e912016-09-24 18:20:14 -0600142int spl_load_image_ext_os(struct spl_image_info *spl_image,
143 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200144{
145 return -ENOSYS;
146}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200147#endif