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> 16339245b7SNikita Kiryanov #include <errno.h> 17773b5940SDan Murphy #include <image.h> 18773b5940SDan Murphy 19773b5940SDan Murphy static int fat_registered; 20773b5940SDan Murphy 21773b5940SDan Murphy #ifdef CONFIG_SPL_FAT_SUPPORT 224101f687SSimon Glass static int spl_register_fat_device(struct blk_desc *block_dev, int partition) 23773b5940SDan Murphy { 24773b5940SDan Murphy int err = 0; 25773b5940SDan Murphy 26773b5940SDan Murphy if (fat_registered) 27773b5940SDan Murphy return err; 28773b5940SDan Murphy 29773b5940SDan Murphy err = fat_register_device(block_dev, partition); 30773b5940SDan Murphy if (err) { 31773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 328cffe5bdSDan Murphy printf("%s: fat register err - %d\n", __func__, err); 33773b5940SDan Murphy #endif 347d2b4e77SGuillaume GARDET return err; 35773b5940SDan Murphy } 36773b5940SDan Murphy 37773b5940SDan Murphy fat_registered = 1; 38773b5940SDan Murphy 39773b5940SDan Murphy return err; 40773b5940SDan Murphy } 41773b5940SDan Murphy 424101f687SSimon Glass int spl_load_image_fat(struct blk_desc *block_dev, 43773b5940SDan Murphy int partition, 44773b5940SDan Murphy const char *filename) 45773b5940SDan Murphy { 46773b5940SDan Murphy int err; 47773b5940SDan Murphy struct image_header *header; 48773b5940SDan Murphy 49773b5940SDan Murphy err = spl_register_fat_device(block_dev, partition); 508cffe5bdSDan Murphy if (err) 51773b5940SDan Murphy goto end; 52773b5940SDan Murphy 53773b5940SDan Murphy header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - 54773b5940SDan Murphy sizeof(struct image_header)); 55773b5940SDan Murphy 56773b5940SDan Murphy err = file_fat_read(filename, header, sizeof(struct image_header)); 57773b5940SDan Murphy if (err <= 0) 58773b5940SDan Murphy goto end; 59773b5940SDan Murphy 607e0f2267SMarek Vasut err = spl_parse_image_header(header); 61*d550e82eSTom Rini if (err) 627e0f2267SMarek Vasut goto end; 63773b5940SDan Murphy 64773b5940SDan Murphy err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); 65773b5940SDan Murphy 66773b5940SDan Murphy end: 67773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 68773b5940SDan Murphy if (err <= 0) 698cffe5bdSDan Murphy printf("%s: error reading image %s, err - %d\n", 708cffe5bdSDan Murphy __func__, filename, err); 71773b5940SDan Murphy #endif 72773b5940SDan Murphy 73773b5940SDan Murphy return (err <= 0); 74773b5940SDan Murphy } 75773b5940SDan Murphy 76773b5940SDan Murphy #ifdef CONFIG_SPL_OS_BOOT 774101f687SSimon Glass int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) 78773b5940SDan Murphy { 79773b5940SDan Murphy int err; 80ae1590edSTom Rini __maybe_unused char *file; 81773b5940SDan Murphy 82773b5940SDan Murphy err = spl_register_fat_device(block_dev, partition); 838cffe5bdSDan Murphy if (err) 848cffe5bdSDan Murphy return err; 85773b5940SDan Murphy 86ae1590edSTom Rini #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) 87ae1590edSTom Rini file = getenv("falcon_args_file"); 88ae1590edSTom Rini if (file) { 89ae1590edSTom Rini err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); 90ae1590edSTom Rini if (err <= 0) { 91ae1590edSTom Rini printf("spl: error reading image %s, err - %d, falling back to default\n", 92ae1590edSTom Rini file, err); 93ae1590edSTom Rini goto defaults; 94ae1590edSTom Rini } 95ae1590edSTom Rini file = getenv("falcon_image_file"); 96ae1590edSTom Rini if (file) { 97ae1590edSTom Rini err = spl_load_image_fat(block_dev, partition, file); 98ae1590edSTom Rini if (err != 0) { 99ae1590edSTom Rini puts("spl: falling back to default\n"); 100ae1590edSTom Rini goto defaults; 101ae1590edSTom Rini } 102ae1590edSTom Rini 103ae1590edSTom Rini return 0; 104ae1590edSTom Rini } else 105ae1590edSTom Rini puts("spl: falcon_image_file not set in environment, falling back to default\n"); 106ae1590edSTom Rini } else 107ae1590edSTom Rini puts("spl: falcon_args_file not set in environment, falling back to default\n"); 108ae1590edSTom Rini 109ae1590edSTom Rini defaults: 110ae1590edSTom Rini #endif 111ae1590edSTom Rini 112205b4f33SGuillaume GARDET err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, 113773b5940SDan Murphy (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); 114773b5940SDan Murphy if (err <= 0) { 115773b5940SDan Murphy #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 1168cffe5bdSDan Murphy printf("%s: error reading image %s, err - %d\n", 117205b4f33SGuillaume GARDET __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); 118773b5940SDan Murphy #endif 119773b5940SDan Murphy return -1; 120773b5940SDan Murphy } 121773b5940SDan Murphy 122773b5940SDan Murphy return spl_load_image_fat(block_dev, partition, 123205b4f33SGuillaume GARDET CONFIG_SPL_FS_LOAD_KERNEL_NAME); 124773b5940SDan Murphy } 125339245b7SNikita Kiryanov #else 1264101f687SSimon Glass int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) 127339245b7SNikita Kiryanov { 128339245b7SNikita Kiryanov return -ENOSYS; 129339245b7SNikita Kiryanov } 130773b5940SDan Murphy #endif 131773b5940SDan Murphy #endif 132