xref: /openbmc/u-boot/common/spl/spl_fat.c (revision 7d2b4e77)
1773b5940SDan Murphy /*
2773b5940SDan Murphy  * (C) Copyright 2014
3773b5940SDan Murphy  * Texas Instruments, <www.ti.com>
4773b5940SDan Murphy  *
5773b5940SDan Murphy  * Dan Murphy <dmurphy@ti.com>
6773b5940SDan Murphy  *
7773b5940SDan Murphy  * SPDX-License-Identifier:	GPL-2.0+
8773b5940SDan Murphy  *
9773b5940SDan Murphy  * FAT Image Functions copied from spl_mmc.c
10773b5940SDan Murphy  */
11773b5940SDan Murphy 
12773b5940SDan Murphy #include <common.h>
13773b5940SDan Murphy #include <spl.h>
14773b5940SDan Murphy #include <asm/u-boot.h>
15773b5940SDan Murphy #include <fat.h>
16773b5940SDan Murphy #include <image.h>
17773b5940SDan Murphy 
18773b5940SDan Murphy static int fat_registered;
19773b5940SDan Murphy 
20773b5940SDan Murphy #ifdef CONFIG_SPL_FAT_SUPPORT
21773b5940SDan Murphy static int spl_register_fat_device(block_dev_desc_t *block_dev, int partition)
22773b5940SDan Murphy {
23773b5940SDan Murphy 	int err = 0;
24773b5940SDan Murphy 
25773b5940SDan Murphy 	if (fat_registered)
26773b5940SDan Murphy 		return err;
27773b5940SDan Murphy 
28773b5940SDan Murphy 	err = fat_register_device(block_dev, partition);
29773b5940SDan Murphy 	if (err) {
30773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
318cffe5bdSDan Murphy 		printf("%s: fat register err - %d\n", __func__, err);
32773b5940SDan Murphy #endif
33*7d2b4e77SGuillaume GARDET 		return err;
34773b5940SDan Murphy 	}
35773b5940SDan Murphy 
36773b5940SDan Murphy 	fat_registered = 1;
37773b5940SDan Murphy 
38773b5940SDan Murphy 	return err;
39773b5940SDan Murphy }
40773b5940SDan Murphy 
41773b5940SDan Murphy int spl_load_image_fat(block_dev_desc_t *block_dev,
42773b5940SDan Murphy 						int partition,
43773b5940SDan Murphy 						const char *filename)
44773b5940SDan Murphy {
45773b5940SDan Murphy 	int err;
46773b5940SDan Murphy 	struct image_header *header;
47773b5940SDan Murphy 
48773b5940SDan Murphy 	err = spl_register_fat_device(block_dev, partition);
498cffe5bdSDan Murphy 	if (err)
50773b5940SDan Murphy 		goto end;
51773b5940SDan Murphy 
52773b5940SDan Murphy 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
53773b5940SDan Murphy 						sizeof(struct image_header));
54773b5940SDan Murphy 
55773b5940SDan Murphy 	err = file_fat_read(filename, header, sizeof(struct image_header));
56773b5940SDan Murphy 	if (err <= 0)
57773b5940SDan Murphy 		goto end;
58773b5940SDan Murphy 
59773b5940SDan Murphy 	spl_parse_image_header(header);
60773b5940SDan Murphy 
61773b5940SDan Murphy 	err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
62773b5940SDan Murphy 
63773b5940SDan Murphy end:
64773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
65773b5940SDan Murphy 	if (err <= 0)
668cffe5bdSDan Murphy 		printf("%s: error reading image %s, err - %d\n",
678cffe5bdSDan Murphy 		       __func__, filename, err);
68773b5940SDan Murphy #endif
69773b5940SDan Murphy 
70773b5940SDan Murphy 	return (err <= 0);
71773b5940SDan Murphy }
72773b5940SDan Murphy 
73773b5940SDan Murphy #ifdef CONFIG_SPL_OS_BOOT
74773b5940SDan Murphy int spl_load_image_fat_os(block_dev_desc_t *block_dev, int partition)
75773b5940SDan Murphy {
76773b5940SDan Murphy 	int err;
77ae1590edSTom Rini 	__maybe_unused char *file;
78773b5940SDan Murphy 
79773b5940SDan Murphy 	err = spl_register_fat_device(block_dev, partition);
808cffe5bdSDan Murphy 	if (err)
818cffe5bdSDan Murphy 		return err;
82773b5940SDan Murphy 
83ae1590edSTom Rini #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
84ae1590edSTom Rini 	file = getenv("falcon_args_file");
85ae1590edSTom Rini 	if (file) {
86ae1590edSTom Rini 		err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
87ae1590edSTom Rini 		if (err <= 0) {
88ae1590edSTom Rini 			printf("spl: error reading image %s, err - %d, falling back to default\n",
89ae1590edSTom Rini 			       file, err);
90ae1590edSTom Rini 			goto defaults;
91ae1590edSTom Rini 		}
92ae1590edSTom Rini 		file = getenv("falcon_image_file");
93ae1590edSTom Rini 		if (file) {
94ae1590edSTom Rini 			err = spl_load_image_fat(block_dev, partition, file);
95ae1590edSTom Rini 			if (err != 0) {
96ae1590edSTom Rini 				puts("spl: falling back to default\n");
97ae1590edSTom Rini 				goto defaults;
98ae1590edSTom Rini 			}
99ae1590edSTom Rini 
100ae1590edSTom Rini 			return 0;
101ae1590edSTom Rini 		} else
102ae1590edSTom Rini 			puts("spl: falcon_image_file not set in environment, falling back to default\n");
103ae1590edSTom Rini 	} else
104ae1590edSTom Rini 		puts("spl: falcon_args_file not set in environment, falling back to default\n");
105ae1590edSTom Rini 
106ae1590edSTom Rini defaults:
107ae1590edSTom Rini #endif
108ae1590edSTom Rini 
109205b4f33SGuillaume GARDET 	err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
110773b5940SDan Murphy 			    (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
111773b5940SDan Murphy 	if (err <= 0) {
112773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1138cffe5bdSDan Murphy 		printf("%s: error reading image %s, err - %d\n",
114205b4f33SGuillaume GARDET 		       __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
115773b5940SDan Murphy #endif
116773b5940SDan Murphy 		return -1;
117773b5940SDan Murphy 	}
118773b5940SDan Murphy 
119773b5940SDan Murphy 	return spl_load_image_fat(block_dev, partition,
120205b4f33SGuillaume GARDET 			CONFIG_SPL_FS_LOAD_KERNEL_NAME);
121773b5940SDan Murphy }
122773b5940SDan Murphy #endif
123773b5940SDan Murphy #endif
124