blob: a42fbd009b5125428b927eeccf97bf52b20d4deb [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
13int spl_load_image_ext(block_dev_desc_t *block_dev,
14 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
25 if (get_partition_info(block_dev,
26 partition, &part_info)) {
27 printf("spl: no partition table found\n");
28 return -1;
29 }
30
31 ext4fs_set_blk_dev(block_dev, &part_info);
32
33 err = ext4fs_mount(0);
34 if (!err) {
35#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
36 printf("%s: ext4fs mount err - %d\n", __func__, err);
37#endif
38 goto end;
39 }
40
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080041 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020042 if (err < 0) {
43 puts("spl: ext4fs_open failed\n");
44 goto end;
45 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080046 err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010047 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020048 puts("spl: ext4fs_read failed\n");
49 goto end;
50 }
51
52 spl_parse_image_header(header);
53
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080054 err = ext4fs_read((char *)spl_image.load_addr, filelen, &actlen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020055
56end:
57#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010058 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020059 printf("%s: error reading image %s, err - %d\n",
60 __func__, filename, err);
61#endif
62
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010063 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020064}
65
66#ifdef CONFIG_SPL_OS_BOOT
67int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition)
68{
69 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080070 __maybe_unused loff_t filelen, actlen;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020071 disk_partition_t part_info = {};
72 __maybe_unused char *file;
73
74 if (get_partition_info(block_dev,
75 partition, &part_info)) {
76 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 }
89
90#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
91 file = getenv("falcon_args_file");
92 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 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080098 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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 }
104 file = getenv("falcon_image_file");
105 if (file) {
106 err = spl_load_image_ext(block_dev, partition, file);
107 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 Ramasamib3a2d5a2014-11-17 14:39:36 -0800123 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200124 if (err < 0)
125 puts("spl: ext4fs_open failed\n");
126
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800127 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100128 if (err < 0) {
Guillaume GARDET1eb410c2014-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
136 return spl_load_image_ext(block_dev, partition,
137 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
138}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200139#else
140int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition)
141{
142 return -ENOSYS;
143}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200144#endif
145#endif