blob: 91481fcc134a9de791c15dc6d773943ba1064ec1 [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>
16#include <image.h>
17
18static int fat_registered;
19
20#ifdef CONFIG_SPL_FAT_SUPPORT
21static int spl_register_fat_device(block_dev_desc_t *block_dev, int partition)
22{
23 int err = 0;
24
25 if (fat_registered)
26 return err;
27
28 err = fat_register_device(block_dev, partition);
29 if (err) {
30#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -060031 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphy773b5942014-01-16 11:23:29 -060032#endif
33 hang();
34 }
35
36 fat_registered = 1;
37
38 return err;
39}
40
41int spl_load_image_fat(block_dev_desc_t *block_dev,
42 int partition,
43 const char *filename)
44{
45 int err;
46 struct image_header *header;
47
48 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060049 if (err)
Dan Murphy773b5942014-01-16 11:23:29 -060050 goto end;
51
52 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
53 sizeof(struct image_header));
54
55 err = file_fat_read(filename, header, sizeof(struct image_header));
56 if (err <= 0)
57 goto end;
58
59 spl_parse_image_header(header);
60
61 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
62
63end:
64#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
65 if (err <= 0)
Dan Murphy8cffe5b2014-01-16 11:23:30 -060066 printf("%s: error reading image %s, err - %d\n",
67 __func__, filename, err);
Dan Murphy773b5942014-01-16 11:23:29 -060068#endif
69
70 return (err <= 0);
71}
72
73#ifdef CONFIG_SPL_OS_BOOT
74int spl_load_image_fat_os(block_dev_desc_t *block_dev, int partition)
75{
76 int err;
Tom Riniae1590e2014-03-28 12:03:42 -040077 __maybe_unused char *file;
Dan Murphy773b5942014-01-16 11:23:29 -060078
79 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060080 if (err)
81 return err;
Dan Murphy773b5942014-01-16 11:23:29 -060082
Tom Riniae1590e2014-03-28 12:03:42 -040083#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
84 file = getenv("falcon_args_file");
85 if (file) {
86 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
87 if (err <= 0) {
88 printf("spl: error reading image %s, err - %d, falling back to default\n",
89 file, err);
90 goto defaults;
91 }
92 file = getenv("falcon_image_file");
93 if (file) {
94 err = spl_load_image_fat(block_dev, partition, file);
95 if (err != 0) {
96 puts("spl: falling back to default\n");
97 goto defaults;
98 }
99
100 return 0;
101 } else
102 puts("spl: falcon_image_file not set in environment, falling back to default\n");
103 } else
104 puts("spl: falcon_args_file not set in environment, falling back to default\n");
105
106defaults:
107#endif
108
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200109 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Dan Murphy773b5942014-01-16 11:23:29 -0600110 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
111 if (err <= 0) {
112#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600113 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200114 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600115#endif
116 return -1;
117 }
118
119 return spl_load_image_fat(block_dev, partition,
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200120 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphy773b5942014-01-16 11:23:29 -0600121}
122#endif
123#endif