blob: f27b4c277b645ed0555a6df27fb3e04d9e9a240b [file] [log] [blame]
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04008 */
9#include <common.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070010#include <spl.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040011#include <asm/u-boot.h>
12#include <asm/utils.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013#include <mmc.h>
14#include <fat.h>
Simon Glassefb21722011-10-10 08:55:19 +000015#include <version.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040016#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017
18DECLARE_GLOBAL_DATA_PTR;
19
Peter Korsgaard721931f2013-05-13 08:36:28 +000020static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040021{
Peter Korsgaard2e222102013-03-21 04:55:17 +000022 unsigned long err;
23 u32 image_size_sectors;
24 struct image_header *header;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040025
26 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
27 sizeof(struct image_header));
28
29 /* read image header to find the image size & load address */
Peter Korsgaard721931f2013-05-13 08:36:28 +000030 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaard2e222102013-03-21 04:55:17 +000031 if (err == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040032 goto end;
33
Tom Rinie4c444b2013-06-28 14:43:01 -040034 if (image_get_magic(header) != IH_MAGIC)
35 return -1;
36
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040037 spl_parse_image_header(header);
38
39 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070040 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
41 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040042
43 /* Read the header too to avoid extra memcpy */
Peter Korsgaard721931f2013-05-13 08:36:28 +000044 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
45 (void *)spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040046
47end:
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000048 if (err == 0)
Peter Korsgaard2e222102013-03-21 04:55:17 +000049 printf("spl: mmc blk read err - %lu\n", err);
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000050
51 return (err == 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040052}
53
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000054#ifdef CONFIG_SPL_OS_BOOT
55static int mmc_load_image_raw_os(struct mmc *mmc)
56{
57 if (!mmc->block_dev.block_read(0,
58 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
59 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
60 (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
61 printf("mmc args blk read error\n");
62 return -1;
63 }
64
65 return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
66}
67#endif
68
Tom Rini0da113e2012-08-10 09:27:14 -070069#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000070static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040071{
Peter Korsgaard2e222102013-03-21 04:55:17 +000072 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040073 struct image_header *header;
74
75 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
76 sizeof(struct image_header));
77
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000078 err = file_fat_read(filename, header, sizeof(struct image_header));
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040079 if (err <= 0)
80 goto end;
81
82 spl_parse_image_header(header);
83
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000084 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040085
86end:
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000087 if (err <= 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040088 printf("spl: error reading image %s, err - %d\n",
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000089 filename, err);
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000090
91 return (err <= 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040092}
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +000093
94#ifdef CONFIG_SPL_OS_BOOT
95static int mmc_load_image_fat_os(struct mmc *mmc)
96{
97 int err;
98
99 err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
100 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
101 if (err <= 0) {
102 printf("spl: error reading image %s, err - %d\n",
103 CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
104 return -1;
105 }
106
107 return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
108}
109#endif
110
Tom Rini0da113e2012-08-10 09:27:14 -0700111#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400112
113void spl_mmc_load_image(void)
114{
115 struct mmc *mmc;
116 int err;
117 u32 boot_mode;
118
119 mmc_initialize(gd->bd);
120 /* We register only one device. So, the dev id is always 0 */
121 mmc = find_mmc_device(0);
122 if (!mmc) {
123 puts("spl: mmc device not found!!\n");
124 hang();
125 }
126
127 err = mmc_init(mmc);
128 if (err) {
129 printf("spl: mmc init failed: err - %d\n", err);
130 hang();
131 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000132
Tom Rini37189a12012-08-14 09:19:44 -0700133 boot_mode = spl_boot_mode();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400134 if (boot_mode == MMCSD_MODE_RAW) {
135 debug("boot mode - RAW\n");
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000136#ifdef CONFIG_SPL_OS_BOOT
137 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
138#endif
Peter Korsgaard721931f2013-05-13 08:36:28 +0000139 err = mmc_load_image_raw(mmc,
140 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Tom Rini0da113e2012-08-10 09:27:14 -0700141#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400142 } else if (boot_mode == MMCSD_MODE_FAT) {
143 debug("boot mode - FAT\n");
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000144
145 err = fat_register_device(&mmc->block_dev,
146 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
147 if (err) {
148 printf("spl: fat register err - %d\n", err);
149 hang();
150 }
151
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000152#ifdef CONFIG_SPL_OS_BOOT
153 if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
154#endif
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000155 err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
Tom Rini0da113e2012-08-10 09:27:14 -0700156#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400157 } else {
158 puts("spl: wrong MMC boot mode\n");
159 hang();
160 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000161
162 if (err)
163 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400164}