blob: d761b264c139aba5c0229af4d02139e6626660eb [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>
18
19static int fat_registered;
20
21#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Glass4101f682016-02-29 15:25:34 -070022static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -060023{
24 int err = 0;
25
26 if (fat_registered)
27 return err;
28
29 err = fat_register_device(block_dev, partition);
30 if (err) {
31#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -060032 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphy773b5942014-01-16 11:23:29 -060033#endif
Guillaume GARDET7d2b4e72014-10-15 17:53:14 +020034 return err;
Dan Murphy773b5942014-01-16 11:23:29 -060035 }
36
37 fat_registered = 1;
38
39 return err;
40}
41
Simon Glass4101f682016-02-29 15:25:34 -070042int spl_load_image_fat(struct blk_desc *block_dev,
Dan Murphy773b5942014-01-16 11:23:29 -060043 int partition,
44 const char *filename)
45{
46 int err;
47 struct image_header *header;
48
49 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060050 if (err)
Dan Murphy773b5942014-01-16 11:23:29 -060051 goto end;
52
53 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
54 sizeof(struct image_header));
55
56 err = file_fat_read(filename, header, sizeof(struct image_header));
57 if (err <= 0)
58 goto end;
59
60 spl_parse_image_header(header);
61
62 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
63
64end:
65#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
66 if (err <= 0)
Dan Murphy8cffe5b2014-01-16 11:23:30 -060067 printf("%s: error reading image %s, err - %d\n",
68 __func__, filename, err);
Dan Murphy773b5942014-01-16 11:23:29 -060069#endif
70
71 return (err <= 0);
72}
73
74#ifdef CONFIG_SPL_OS_BOOT
Simon Glass4101f682016-02-29 15:25:34 -070075int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -060076{
77 int err;
Tom Riniae1590e2014-03-28 12:03:42 -040078 __maybe_unused char *file;
Dan Murphy773b5942014-01-16 11:23:29 -060079
80 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060081 if (err)
82 return err;
Dan Murphy773b5942014-01-16 11:23:29 -060083
Tom Riniae1590e2014-03-28 12:03:42 -040084#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
85 file = getenv("falcon_args_file");
86 if (file) {
87 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
88 if (err <= 0) {
89 printf("spl: error reading image %s, err - %d, falling back to default\n",
90 file, err);
91 goto defaults;
92 }
93 file = getenv("falcon_image_file");
94 if (file) {
95 err = spl_load_image_fat(block_dev, partition, file);
96 if (err != 0) {
97 puts("spl: falling back to default\n");
98 goto defaults;
99 }
100
101 return 0;
102 } else
103 puts("spl: falcon_image_file not set in environment, falling back to default\n");
104 } else
105 puts("spl: falcon_args_file not set in environment, falling back to default\n");
106
107defaults:
108#endif
109
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200110 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Dan Murphy773b5942014-01-16 11:23:29 -0600111 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
112 if (err <= 0) {
113#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600114 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200115 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600116#endif
117 return -1;
118 }
119
120 return spl_load_image_fat(block_dev, partition,
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200121 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphy773b5942014-01-16 11:23:29 -0600122}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200123#else
Simon Glass4101f682016-02-29 15:25:34 -0700124int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200125{
126 return -ENOSYS;
127}
Dan Murphy773b5942014-01-16 11:23:29 -0600128#endif
129#endif