blob: 9d37fd352113dab7459a66fa9a90066bfe08f328 [file] [log] [blame]
Guillaume GARDET592f9222014-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>
9#include <image.h>
10
11#ifdef CONFIG_SPL_EXT_SUPPORT
12int spl_load_image_ext(block_dev_desc_t *block_dev,
13 int partition,
14 const char *filename)
15{
16 s32 err;
17 struct image_header *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080018 loff_t filelen, actlen;
Guillaume GARDET592f9222014-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
24 if (get_partition_info(block_dev,
25 partition, &part_info)) {
26 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 Ramasami9f12cd02014-11-17 14:39:36 -080040 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020041 if (err < 0) {
42 puts("spl: ext4fs_open failed\n");
43 goto end;
44 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080045 err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010046 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020047 puts("spl: ext4fs_read failed\n");
48 goto end;
49 }
50
51 spl_parse_image_header(header);
52
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080053 err = ext4fs_read((char *)spl_image.load_addr, filelen, &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020054
55end:
56#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010057 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020058 printf("%s: error reading image %s, err - %d\n",
59 __func__, filename, err);
60#endif
61
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010062 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020063}
64
65#ifdef CONFIG_SPL_OS_BOOT
66int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition)
67{
68 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080069 __maybe_unused loff_t filelen, actlen;
Guillaume GARDET592f9222014-10-15 17:53:12 +020070 disk_partition_t part_info = {};
71 __maybe_unused char *file;
72
73 if (get_partition_info(block_dev,
74 partition, &part_info)) {
75 printf("spl: no partition table found\n");
76 return -1;
77 }
78
79 ext4fs_set_blk_dev(block_dev, &part_info);
80
81 err = ext4fs_mount(0);
82 if (!err) {
83#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
84 printf("%s: ext4fs mount err - %d\n", __func__, err);
85#endif
86 return -1;
87 }
88
89#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
90 file = getenv("falcon_args_file");
91 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 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080097 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 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 }
103 file = getenv("falcon_image_file");
104 if (file) {
105 err = spl_load_image_ext(block_dev, partition, file);
106 if (err != 0) {
107 puts("spl: falling back to default\n");
108 goto defaults;
109 }
110
111 return 0;
112 } else {
113 puts("spl: falcon_image_file not set in environment, falling back to default\n");
114 }
115 } else {
116 puts("spl: falcon_args_file not set in environment, falling back to default\n");
117 }
118
119defaults:
120#endif
121
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800122 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200123 if (err < 0)
124 puts("spl: ext4fs_open failed\n");
125
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800126 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100127 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200128#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129 printf("%s: error reading image %s, err - %d\n",
130 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
131#endif
132 return -1;
133 }
134
135 return spl_load_image_ext(block_dev, partition,
136 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
137}
138#endif
139#endif