xref: /openbmc/u-boot/common/spl/spl_fat.c (revision 3cbeb0f0)
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 
78 	err = spl_register_fat_device(block_dev, partition);
79 	if (err)
80 		return err;
81 
82 	err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
83 			    (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
84 	if (err <= 0) {
85 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
86 		printf("%s: error reading image %s, err - %d\n",
87 		       __func__, CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
88 #endif
89 		return -1;
90 	}
91 
92 	return spl_load_image_fat(block_dev, partition,
93 			CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
94 }
95 #endif
96 #endif
97