xref: /openbmc/u-boot/common/spl/spl_fat.c (revision 9fc2ed40)
1 /*
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 
18 static int fat_registered;
19 
20 #ifdef CONFIG_SPL_FAT_SUPPORT
21 static 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
31 		printf("%s: fat register err - %d\n", __func__, err);
32 #endif
33 		hang();
34 	}
35 
36 	fat_registered = 1;
37 
38 	return err;
39 }
40 
41 int 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);
49 	if (err)
50 		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 
63 end:
64 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
65 	if (err <= 0)
66 		printf("%s: error reading image %s, err - %d\n",
67 		       __func__, filename, err);
68 #endif
69 
70 	return (err <= 0);
71 }
72 
73 #ifdef CONFIG_SPL_OS_BOOT
74 int spl_load_image_fat_os(block_dev_desc_t *block_dev, int partition)
75 {
76 	int err;
77 	__maybe_unused char *file;
78 
79 	err = spl_register_fat_device(block_dev, partition);
80 	if (err)
81 		return err;
82 
83 #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 
106 defaults:
107 #endif
108 
109 	err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
110 			    (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
111 	if (err <= 0) {
112 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
113 		printf("%s: error reading image %s, err - %d\n",
114 		       __func__, CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
115 #endif
116 		return -1;
117 	}
118 
119 	return spl_load_image_fat(block_dev, partition,
120 			CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
121 }
122 #endif
123 #endif
124