blob: 73d33f54fc134b81c1169f97900102310d4729b6 [file] [log] [blame]
Dan Murphy773b5942014-01-16 11:23:29 -06001/*
2 * (C) Copyright 2014
3 * Texas Instruments, <www.ti.com>
4 *
5 * Dan Murphy <dmurphy@ti.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * FAT Image Functions copied from spl_mmc.c
10 */
11
12#include <common.h>
13#include <spl.h>
14#include <asm/u-boot.h>
15#include <fat.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +020016#include <errno.h>
Dan Murphy773b5942014-01-16 11:23:29 -060017#include <image.h>
Lokesh Vutla97ca3642016-05-24 10:34:39 +053018#include <libfdt.h>
Dan Murphy773b5942014-01-16 11:23:29 -060019
20static int fat_registered;
21
22#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Glass4101f682016-02-29 15:25:34 -070023static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -060024{
25 int err = 0;
26
27 if (fat_registered)
28 return err;
29
30 err = fat_register_device(block_dev, partition);
31 if (err) {
32#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -060033 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphy773b5942014-01-16 11:23:29 -060034#endif
Guillaume GARDET7d2b4e72014-10-15 17:53:14 +020035 return err;
Dan Murphy773b5942014-01-16 11:23:29 -060036 }
37
38 fat_registered = 1;
39
40 return err;
41}
42
Lokesh Vutla97ca3642016-05-24 10:34:39 +053043static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
44 ulong size, void *buf)
45{
46 loff_t actread;
47 int ret;
48 char *filename = (char *)load->filename;
49
50 ret = fat_read_file(filename, buf, file_offset, size, &actread);
51 if (ret)
52 return ret;
53
54 return actread;
55}
56
Simon Glass4101f682016-02-29 15:25:34 -070057int spl_load_image_fat(struct blk_desc *block_dev,
Dan Murphy773b5942014-01-16 11:23:29 -060058 int partition,
59 const char *filename)
60{
61 int err;
62 struct image_header *header;
63
64 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060065 if (err)
Dan Murphy773b5942014-01-16 11:23:29 -060066 goto end;
67
68 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
69 sizeof(struct image_header));
70
71 err = file_fat_read(filename, header, sizeof(struct image_header));
72 if (err <= 0)
73 goto end;
74
Lokesh Vutla97ca3642016-05-24 10:34:39 +053075 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
76 image_get_magic(header) == FDT_MAGIC) {
77 struct spl_load_info load;
Dan Murphy773b5942014-01-16 11:23:29 -060078
Lokesh Vutla97ca3642016-05-24 10:34:39 +053079 debug("Found FIT\n");
80 load.read = spl_fit_read;
81 load.bl_len = 1;
82 load.filename = (void *)filename;
83 load.priv = NULL;
84
85 return spl_load_simple_fit(&load, 0, header);
86 } else {
87 err = spl_parse_image_header(header);
88 if (err)
89 goto end;
90
Michal Simek1eefe142016-04-27 16:07:20 +020091 err = file_fat_read(filename,
92 (u8 *)(uintptr_t)spl_image.load_addr, 0);
Lokesh Vutla97ca3642016-05-24 10:34:39 +053093 }
Dan Murphy773b5942014-01-16 11:23:29 -060094
95end:
96#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
97 if (err <= 0)
Dan Murphy8cffe5b2014-01-16 11:23:30 -060098 printf("%s: error reading image %s, err - %d\n",
99 __func__, filename, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600100#endif
101
102 return (err <= 0);
103}
104
105#ifdef CONFIG_SPL_OS_BOOT
Simon Glass4101f682016-02-29 15:25:34 -0700106int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -0600107{
108 int err;
Tom Riniae1590e2014-03-28 12:03:42 -0400109 __maybe_unused char *file;
Dan Murphy773b5942014-01-16 11:23:29 -0600110
111 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600112 if (err)
113 return err;
Dan Murphy773b5942014-01-16 11:23:29 -0600114
Tom Riniae1590e2014-03-28 12:03:42 -0400115#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
116 file = getenv("falcon_args_file");
117 if (file) {
118 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
119 if (err <= 0) {
120 printf("spl: error reading image %s, err - %d, falling back to default\n",
121 file, err);
122 goto defaults;
123 }
124 file = getenv("falcon_image_file");
125 if (file) {
126 err = spl_load_image_fat(block_dev, partition, file);
127 if (err != 0) {
128 puts("spl: falling back to default\n");
129 goto defaults;
130 }
131
132 return 0;
133 } else
134 puts("spl: falcon_image_file not set in environment, falling back to default\n");
135 } else
136 puts("spl: falcon_args_file not set in environment, falling back to default\n");
137
138defaults:
139#endif
140
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200141 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Dan Murphy773b5942014-01-16 11:23:29 -0600142 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
143 if (err <= 0) {
144#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600145 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200146 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600147#endif
148 return -1;
149 }
150
151 return spl_load_image_fat(block_dev, partition,
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200152 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphy773b5942014-01-16 11:23:29 -0600153}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200154#else
Simon Glass4101f682016-02-29 15:25:34 -0700155int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200156{
157 return -ENOSYS;
158}
Dan Murphy773b5942014-01-16 11:23:29 -0600159#endif
160#endif