Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 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> |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 17 | #include <image.h> |
| 18 | |
| 19 | static int fat_registered; |
| 20 | |
| 21 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 22 | static int spl_register_fat_device(struct blk_desc *block_dev, int partition) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 23 | { |
| 24 | int err = 0; |
| 25 | |
| 26 | if (fat_registered) |
| 27 | return err; |
| 28 | |
| 29 | err = fat_register_device(block_dev, partition); |
| 30 | if (err) { |
| 31 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 32 | printf("%s: fat register err - %d\n", __func__, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 33 | #endif |
Guillaume GARDET | 7d2b4e7 | 2014-10-15 17:53:14 +0200 | [diff] [blame] | 34 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | fat_registered = 1; |
| 38 | |
| 39 | return err; |
| 40 | } |
| 41 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 42 | int spl_load_image_fat(struct blk_desc *block_dev, |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 43 | int partition, |
| 44 | const char *filename) |
| 45 | { |
| 46 | int err; |
| 47 | struct image_header *header; |
| 48 | |
| 49 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 50 | if (err) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 51 | goto end; |
| 52 | |
| 53 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 54 | sizeof(struct image_header)); |
| 55 | |
| 56 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
| 57 | if (err <= 0) |
| 58 | goto end; |
| 59 | |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 60 | err = spl_parse_image_header(header); |
Tom Rini | d550e82 | 2016-05-23 11:51:13 -0400 | [diff] [blame] | 61 | if (err) |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 62 | goto end; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 63 | |
| 64 | err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); |
| 65 | |
| 66 | end: |
| 67 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 68 | if (err <= 0) |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 69 | printf("%s: error reading image %s, err - %d\n", |
| 70 | __func__, filename, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | return (err <= 0); |
| 74 | } |
| 75 | |
| 76 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 77 | int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 78 | { |
| 79 | int err; |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 80 | __maybe_unused char *file; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 81 | |
| 82 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 83 | if (err) |
| 84 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 85 | |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 86 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
| 87 | file = getenv("falcon_args_file"); |
| 88 | if (file) { |
| 89 | err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 90 | if (err <= 0) { |
| 91 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 92 | file, err); |
| 93 | goto defaults; |
| 94 | } |
| 95 | file = getenv("falcon_image_file"); |
| 96 | if (file) { |
| 97 | err = spl_load_image_fat(block_dev, partition, file); |
| 98 | if (err != 0) { |
| 99 | puts("spl: falling back to default\n"); |
| 100 | goto defaults; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } else |
| 105 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 106 | } else |
| 107 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 108 | |
| 109 | defaults: |
| 110 | #endif |
| 111 | |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 112 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 113 | (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 114 | if (err <= 0) { |
| 115 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 116 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 117 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 118 | #endif |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return spl_load_image_fat(block_dev, partition, |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 123 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 124 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 125 | #else |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 126 | int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 127 | { |
| 128 | return -ENOSYS; |
| 129 | } |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 130 | #endif |
| 131 | #endif |