Merge tag 'dm-pull-22222' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[platform/kernel/u-boot.git] / common / spl / spl_ext.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <common.h>
4 #include <env.h>
5 #include <part.h>
6 #include <spl.h>
7 #include <asm/u-boot.h>
8 #include <ext4fs.h>
9 #include <errno.h>
10 #include <image.h>
11
12 int spl_load_image_ext(struct spl_image_info *spl_image,
13                        struct spl_boot_device *bootdev,
14                        struct blk_desc *block_dev, int partition,
15                        const char *filename)
16 {
17         s32 err;
18         struct image_header *header;
19         loff_t filelen, actlen;
20         struct disk_partition part_info = {};
21
22         header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
23
24         if (part_get_info(block_dev, partition, &part_info)) {
25                 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                 return -1;
37         }
38
39         err = ext4fs_open(filename, &filelen);
40         if (err < 0) {
41                 puts("spl: ext4fs_open failed\n");
42                 goto end;
43         }
44         err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
45         if (err < 0) {
46                 puts("spl: ext4fs_read failed\n");
47                 goto end;
48         }
49
50         err = spl_parse_image_header(spl_image, bootdev, header);
51         if (err < 0) {
52                 puts("spl: ext: failed to parse image header\n");
53                 goto end;
54         }
55
56         err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
57
58 end:
59 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
60         if (err < 0)
61                 printf("%s: error reading image %s, err - %d\n",
62                        __func__, filename, err);
63 #endif
64
65         return err < 0;
66 }
67
68 #if CONFIG_IS_ENABLED(OS_BOOT)
69 int spl_load_image_ext_os(struct spl_image_info *spl_image,
70                           struct spl_boot_device *bootdev,
71                           struct blk_desc *block_dev, int partition)
72 {
73         int err;
74         __maybe_unused loff_t filelen, actlen;
75         struct disk_partition part_info = {};
76         __maybe_unused char *file;
77
78         if (part_get_info(block_dev, partition, &part_info)) {
79                 printf("spl: no partition table found\n");
80                 return -1;
81         }
82
83         ext4fs_set_blk_dev(block_dev, &part_info);
84
85         err = ext4fs_mount(0);
86         if (!err) {
87 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
88                 printf("%s: ext4fs mount err - %d\n", __func__, err);
89 #endif
90                 return -1;
91         }
92 #if defined(CONFIG_SPL_ENV_SUPPORT)
93         file = env_get("falcon_args_file");
94         if (file) {
95                 err = ext4fs_open(file, &filelen);
96                 if (err < 0) {
97                         puts("spl: ext4fs_open failed\n");
98                         goto defaults;
99                 }
100                 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
101                 if (err < 0) {
102                         printf("spl: error reading image %s, err - %d, falling back to default\n",
103                                file, err);
104                         goto defaults;
105                 }
106                 file = env_get("falcon_image_file");
107                 if (file) {
108                         err = spl_load_image_ext(spl_image, bootdev, block_dev,
109                                                  partition, file);
110                         if (err != 0) {
111                                 puts("spl: falling back to default\n");
112                                 goto defaults;
113                         }
114
115                         return 0;
116                 } else {
117                         puts("spl: falcon_image_file not set in environment, falling back to default\n");
118                 }
119         } else {
120                 puts("spl: falcon_args_file not set in environment, falling back to default\n");
121         }
122
123 defaults:
124 #endif
125
126         err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
127         if (err < 0)
128                 puts("spl: ext4fs_open failed\n");
129
130         err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
131         if (err < 0) {
132 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
133                 printf("%s: error reading image %s, err - %d\n",
134                        __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
135 #endif
136                 return -1;
137         }
138
139         return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
140                         CONFIG_SPL_FS_LOAD_KERNEL_NAME);
141 }
142 #else
143 int spl_load_image_ext_os(struct spl_image_info *spl_image,
144                           struct spl_boot_device *bootdev,
145                           struct blk_desc *block_dev, int partition)
146 {
147         return -ENOSYS;
148 }
149 #endif