4 * Ma Haijun <mahaijuns@gmail.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/u-boot.h>
27 #include <asm/utils.h>
33 /* should be implemented by board */
34 extern void spl_block_device_init(void);
36 block_dev_desc_t * spl_get_block_device(void)
38 block_dev_desc_t * device;
40 spl_block_device_init();
42 device = get_dev(CONFIG_SPL_BLOCKDEV_INTERFACE, CONFIG_SPL_BLOCKDEV_ID);
44 printf("blk device %s%d not exists\n",
45 CONFIG_SPL_BLOCKDEV_INTERFACE,
46 CONFIG_SPL_BLOCKDEV_ID);
53 #ifdef CONFIG_SPL_FAT_SUPPORT
54 static int block_load_image_fat(const char *filename)
57 struct image_header *header;
59 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
60 sizeof(struct image_header));
62 err = file_fat_read(filename, header, sizeof(struct image_header));
66 spl_parse_image_header(header);
68 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
72 printf("spl: error reading image %s, err - %d\n",
78 #ifdef CONFIG_SPL_OS_BOOT
79 static int block_load_image_fat_os(void)
83 err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
84 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
89 return block_load_image_fat(CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
93 void spl_block_load_image(void)
96 block_dev_desc_t * device;
98 device = spl_get_block_device();
99 err = fat_register_device(device, CONFIG_BLOCKDEV_FAT_BOOT_PARTITION);
101 printf("spl: fat register err - %d\n", err);
104 #ifdef CONFIG_SPL_OS_BOOT
105 if (spl_start_uboot() || block_load_image_fat_os())
108 err = block_load_image_fat(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
113 #elif defined(CONFIG_SPL_EXT4_SUPPORT) /* end CONFIG_SPL_FAT_SUPPORT */
114 static int block_load_image_ext4(const char *filename)
117 struct image_header *header;
119 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
120 sizeof(struct image_header));
122 err = ext4_read_file(filename, header, 0, sizeof(struct image_header));
126 spl_parse_image_header(header);
128 err = ext4_read_file(filename, (u8 *)spl_image.load_addr, 0, 0);
134 #ifdef CONFIG_SPL_OS_BOOT
135 static int block_load_image_ext4_os(void)
139 err = ext4_read_file(CONFIG_SPL_EXT4_LOAD_ARGS_NAME,
140 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, 0);
145 return block_load_image_ext4(CONFIG_SPL_EXT4_LOAD_KERNEL_NAME);
149 void spl_block_load_image(void)
152 block_dev_desc_t * device;
154 device = spl_get_block_device();
155 err = ext4_register_device(device, CONFIG_BLOCKDEV_EXT4_BOOT_PARTITION);
159 #ifdef CONFIG_SPL_OS_BOOT
160 if (spl_start_uboot() || block_load_image_ext4_os())
163 err = block_load_image_ext4(CONFIG_SPL_EXT4_LOAD_PAYLOAD_NAME);
168 #else /* end CONFIG_SPL_EXT4_SUPPORT */
169 static int block_load_image_raw(block_dev_desc_t * device, lbaint_t sector)
172 u32 image_size_sectors;
173 struct image_header *header;
175 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
176 sizeof(struct image_header));
178 /* read image header to find the image size & load address */
179 n = device->block_read(device->dev, sector, 1, header);
182 printf("spl: blk read err\n");
186 spl_parse_image_header(header);
188 /* convert size to sectors - round up */
189 image_size_sectors = (spl_image.size + 512 - 1) / 512;
190 n = device->block_read(device->dev, sector, image_size_sectors,
191 (void *)spl_image.load_addr);
193 if (n != image_size_sectors) {
194 printf("spl: blk read err\n");
200 #ifdef CONFIG_SPL_OS_BOOT
201 static int block_load_image_raw_os(block_dev_desc_t * device)
205 n = device->block_read(device->dev, CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTOR,
206 CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS,
207 (u32 *)CONFIG_SYS_SPL_ARGS_ADDR);
208 /* flush cache after read */
209 flush_cache(addr, CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS * 512);
211 if (n != CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS) {
212 printf("args blk read error\n");
216 return block_load_image_raw(device, CONFIG_SYS_BLOCK_RAW_MODE_KERNEL_SECTOR);
220 void spl_block_load_image(void)
223 block_dev_desc_t * device;
225 device = spl_get_block_device();
226 #ifdef CONFIG_SPL_OS_BOOT
227 if (spl_start_uboot() || block_load_image_raw_os(device))
230 err = block_load_image_raw(device,
231 CONFIG_SYS_BLOCK_RAW_MODE_U_BOOT_SECTOR);
236 #endif /* CONFIG_SPL_FAT_SUPPORT */