Skip to content

Commit 97cf268

Browse files
committed
Add option to read image from raw SD/MMC device.
Signed-off-by: Zixun LI <[email protected]>
1 parent e05486a commit 97cf268

File tree

7 files changed

+112
-20
lines changed

7 files changed

+112
-20
lines changed

Config.in.app-image

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ menu "Demo Application Image Storage Setup"
77

88
config IMG_ADDRESS
99
string "Flash Offset for Demo-App"
10-
depends on DATAFLASH || FLASH || NANDFLASH
10+
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
1111
default "0x00008400" if DATAFLASH
1212
default "0x00040000" if NANDFLASH
1313
default "0x00000000" if SDCARD
1414

1515
config IMG_SIZE
1616
string "Demo-App Image Size"
17-
depends on DATAFLASH || FLASH || NANDFLASH
17+
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
1818
default "0x00010000" if LOAD_64KB
1919
default "0x00100000" if LOAD_1MB
2020
default "0x00400000" if LOAD_4MB

Config.in.kernel

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ config LINUX_KERNEL_ARG_STRING_FILE
8686
endif
8787

8888
config IMG_ADDRESS
89-
depends on DATAFLASH || FLASH || NANDFLASH
89+
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
9090
string "Flash Offset for Linux Kernel Image"
9191
default "0x00200000" if FLASH
9292
default "0x00040000" if DATAFLASH
@@ -109,11 +109,11 @@ config OF_LIBFDT
109109

110110
config OF_OVERRIDE_DTB_NAME
111111
string "Override Flattened Device Tree Blob filename"
112-
depends on OF_LIBFDT && SDCARD
112+
depends on OF_LIBFDT && (SDCARD && !FATFS)
113113

114114
config OF_OFFSET
115115
string "The Offset of Flash Device Tree Blob"
116-
depends on OF_LIBFDT && (DATAFLASH || FLASH || NANDFLASH)
116+
depends on OF_LIBFDT && (DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS))
117117
default "0x00008400" if DATAFLASH
118118
default "0x00180000" if NANDFLASH
119119
default "0x00100000" if FLASH

Config.in.u-boot

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ menu "U-Boot Image Storage Setup"
77

88
config IMG_ADDRESS
99
string "Flash Offset for U-Boot"
10-
depends on DATAFLASH || FLASH || NANDFLASH
10+
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
1111
default "0x00008000" if FLASH
1212
default "0x00008000" if DATAFLASH
1313
default "0x00040000" if NANDFLASH
@@ -16,7 +16,7 @@ config IMG_ADDRESS
1616

1717
config IMG_SIZE
1818
string "U-Boot Image Size"
19-
depends on DATAFLASH || FLASH || NANDFLASH
19+
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
2020
default "0x000a0000"
2121
help
2222
at91bootstrap will copy this size of U-Boot image

driver/Config.in.nvm

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ config SDHC_8BIT_SUPPORT
112112
used for another interface.
113113

114114
config FATFS
115-
bool
115+
bool "Load image from FAT partition"
116116
depends on SDCARD
117117
default y if SDCARD
118+
help
119+
Load next stage image from FAT12/16/32 partition. Otherwise from raw
120+
block device, image offset must be a multiple 512 bytes.
118121

119122
endmenu
120123

driver/common.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
load_function load_image;
1515
#endif
1616

17-
#ifdef CONFIG_SDCARD
17+
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
1818
char filename[FILENAME_BUF_LEN];
1919
#ifdef CONFIG_OF_LIBFDT
2020
char of_filename[FILENAME_BUF_LEN];
@@ -42,14 +42,14 @@ load_function get_image_load_func(void)
4242
#endif
4343
}
4444

45-
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
45+
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
4646
unsigned int get_image_load_offset(unsigned int addr)
4747
{
4848
#ifdef CONFIG_FLASH
4949
return (addr | 0x10000000);
5050
#endif
5151

52-
#if defined(CONFIG_NANDFLASH) || defined(CONFIG_DATAFLASH)
52+
#if defined(CONFIG_NANDFLASH) || defined(CONFIG_DATAFLASH) || defined(CONFIG_SDCARD)
5353
return addr;
5454
#endif
5555
}
@@ -58,14 +58,14 @@ unsigned int get_image_load_offset(unsigned int addr)
5858
void init_load_image(struct image_info *image)
5959
{
6060
memset(image, 0, sizeof(*image));
61-
#ifdef CONFIG_SDCARD
61+
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
6262
memset(filename, 0, FILENAME_BUF_LEN);
6363
#ifdef CONFIG_OF_LIBFDT
6464
memset(of_filename, 0, FILENAME_BUF_LEN);
6565
#endif
6666
#endif
6767

68-
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
68+
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
6969

7070
#if !defined(CONFIG_LOAD_LINUX) && !defined(CONFIG_LOAD_ANDROID)
7171
image->length = IMG_SIZE;
@@ -83,7 +83,7 @@ void init_load_image(struct image_info *image)
8383
image->of_dest = (unsigned char *)OF_ADDRESS;
8484
#endif
8585

86-
#ifdef CONFIG_SDCARD
86+
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
8787
image->filename = filename;
8888
strcpy(image->filename, IMAGE_NAME);
8989
#ifdef CONFIG_OF_LIBFDT

driver/sdcard.c

+90-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99

1010
#include "string.h"
1111

12+
#ifdef CONFIG_FATFS
1213
#include "ff.h"
14+
#else
15+
#include "media.h"
16+
#include "fdt.h"
17+
#endif
1318

1419
#include "debug.h"
1520

1621
#define CHUNK_SIZE 0x40000
1722

23+
#ifdef CONFIG_FATFS
1824
static int sdcard_loadimage(char *filename, BYTE *dest)
1925
{
2026
FIL file;
@@ -81,7 +87,7 @@ static int sdcard_read_cmd(char *cmdline_file, char *cmdline_args)
8187
ret = -1;
8288
goto read_fail;
8389
}
84-
90+
8591
ret = 0;
8692

8793
read_fail:
@@ -92,11 +98,43 @@ static int sdcard_read_cmd(char *cmdline_file, char *cmdline_args)
9298

9399
}
94100
#endif
101+
#else
102+
#if defined(CONFIG_LOAD_LINUX) || defined(CONFIG_LOAD_ANDROID)
103+
static int update_image_length(unsigned int offset,
104+
unsigned char *dest,
105+
unsigned char flag)
106+
{
107+
unsigned int length = 512;
108+
int ret;
109+
110+
dbg_info("SD/MMC: update image length from image\n");
111+
112+
memcpy(dest, (const char *)offset, length);
113+
114+
if (flag == KERNEL_IMAGE)
115+
return kernel_size(dest);
116+
#ifdef CONFIG_OF_LIBFDT
117+
else {
118+
ret = check_dt_blob_valid((void *)dest);
119+
if (!ret)
120+
return of_get_dt_total_size((void *)dest);
121+
}
122+
#endif
123+
return -1;
124+
}
125+
#endif
126+
#endif
127+
95128

96129
int load_sdcard(struct image_info *image)
97130
{
131+
#ifdef CONFIG_FATFS
98132
FATFS fs;
99133
FRESULT fret;
134+
#else
135+
unsigned int start_block;
136+
unsigned int block_count;
137+
#endif
100138
int ret;
101139

102140
#ifdef CONFIG_AT91_MCI
@@ -113,6 +151,7 @@ int load_sdcard(struct image_info *image)
113151
at91_sdhc_hw_init();
114152
#endif
115153

154+
#ifdef CONFIG_FATFS
116155
/* mount fs */
117156
fret = f_mount(0, &fs);
118157
if (fret != FR_OK) {
@@ -171,4 +210,54 @@ int load_sdcard(struct image_info *image)
171210
}
172211

173212
return 0;
213+
#else
214+
ret = sdcard_initialize();
215+
if (ret) {
216+
return ret;
217+
}
218+
219+
#if defined(CONFIG_LOAD_LINUX) || defined(CONFIG_LOAD_ANDROID)
220+
int length = update_image_length(image->offset, image->dest, KERNEL_IMAGE);
221+
if (length == -1)
222+
return -1;
223+
224+
image->length = length;
225+
#endif
226+
227+
dbg_info("SD/MMC: Copy %x bytes from %x to %x\n",
228+
image->length, image->offset, image->dest);
229+
230+
start_block = image->offset / 512;
231+
block_count = (image->length + 511) / 512;
232+
233+
ret = sdcard_block_read(start_block, block_count, image->dest);
234+
ret = ret == block_count ? 0 : -1;
235+
if (ret) {
236+
return ret;
237+
}
238+
239+
#ifdef CONFIG_OF_LIBFDT
240+
if(image->of_dest) {
241+
length = update_image_length(image->of_offset,
242+
image->of_dest, DT_BLOB);
243+
if (length == -1)
244+
return -1;
245+
246+
image->of_length = length;
247+
248+
dbg_info("SD/MMC: dt blob: Copy %x bytes from %x to %x\n",
249+
image->of_length, image->of_offset, image->of_dest);
250+
251+
start_block = image->of_offset / 512;
252+
block_count = (image->of_length + 511) / 512;
253+
254+
ret = sdcard_block_read(start_block, block_count, image->of_dest);
255+
ret = ret == block_count ? 0 : -1;
256+
if (ret) {
257+
return ret;
258+
}
259+
}
260+
#endif
261+
return 0;
262+
#endif
174263
}

include/common.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ enum {
3030
/* structure definition */
3131
struct image_info
3232
{
33-
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
33+
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
3434
unsigned int offset;
3535
unsigned int length;
3636
#endif
37-
#ifdef CONFIG_SDCARD
37+
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
3838
char *filename;
3939
#ifdef CONFIG_OVERRIDE_CMDLINE_FROM_EXT_FILE
4040
char *cmdline_file;
@@ -44,11 +44,11 @@ struct image_info
4444
unsigned char *dest;
4545

4646
#ifdef CONFIG_OF_LIBFDT
47-
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
47+
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
4848
unsigned int of_offset;
4949
unsigned int of_length;
5050
#endif
51-
#ifdef CONFIG_SDCARD
51+
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
5252
char *of_filename;
5353
#endif
5454
unsigned char *of_dest;
@@ -61,7 +61,7 @@ typedef int (*load_function)(struct image_info *image);
6161

6262
load_function get_image_load_func(void);
6363

64-
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
64+
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
6565
unsigned int get_image_load_offset(unsigned int addr);
6666
#endif
6767

0 commit comments

Comments
 (0)