blob: ade5496600930c4f2a84ac76d1a9467576dcb3fa [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
12#ifdef CONFIG_SPL_EXT_SUPPORT
Simon Glasse3394752016-02-29 15:25:34 -070013int spl_load_image_ext(struct blk_desc *block_dev,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020014 int partition,
15 const char *filename)
16{
17 s32 err;
18 struct image_header *header;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080019 loff_t filelen, actlen;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020020 disk_partition_t part_info = {};
21
22 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
23 sizeof(struct image_header));
24
Simon Glassb89a8442016-02-29 15:25:48 -070025 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020026 printf("spl: no partition table found\n");
27 return -1;
28 }
29
30 ext4fs_set_blk_dev(block_dev, &part_info);
31
32 err = ext4fs_mount(0);
33 if (!err) {
34#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
35 printf("%s: ext4fs mount err - %d\n", __func__, err);
36#endif
37 goto end;
38 }
39
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080040 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020041 if (err < 0) {
42 puts("spl: ext4fs_open failed\n");
43 goto end;
44 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080045 err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010046 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020047 puts("spl: ext4fs_read failed\n");
48 goto end;
49 }
50
Marek Vasut02266e22016-04-29 00:44:54 +020051 err = spl_parse_image_header(header);
52 if (err < 0) {
53 puts("spl: ext4fs_read failed\n");
54 goto end;
55 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020056
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080057 err = ext4fs_read((char *)spl_image.load_addr, filelen, &actlen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020058
59end:
60#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010061 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020062 printf("%s: error reading image %s, err - %d\n",
63 __func__, filename, err);
64#endif
65
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010066 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020067}
68
69#ifdef CONFIG_SPL_OS_BOOT
Simon Glasse3394752016-02-29 15:25:34 -070070int spl_load_image_ext_os(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 }
91
92#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
93 file = getenv("falcon_args_file");
94 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080095 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020096 if (err < 0) {
97 puts("spl: ext4fs_open failed\n");
98 goto defaults;
99 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800100 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100101 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200102 printf("spl: error reading image %s, err - %d, falling back to default\n",
103 file, err);
104 goto defaults;
105 }
106 file = getenv("falcon_image_file");
107 if (file) {
108 err = spl_load_image_ext(block_dev, partition, file);
109 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
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800129 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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
138 return spl_load_image_ext(block_dev, partition,
139 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
140}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200141#else
Simon Glasse3394752016-02-29 15:25:34 -0700142int spl_load_image_ext_os(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
147#endif