Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add option to read image from raw SD/MMC device. #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Config.in.app-image
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ menu "Demo Application Image Storage Setup"

config IMG_ADDRESS
string "Flash Offset for Demo-App"
depends on DATAFLASH || FLASH || NANDFLASH
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
default "0x00008400" if DATAFLASH
default "0x00040000" if NANDFLASH
default "0x00000000" if SDCARD

config IMG_SIZE
string "Demo-App Image Size"
depends on DATAFLASH || FLASH || NANDFLASH
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
default "0x00010000" if LOAD_64KB
default "0x00100000" if LOAD_1MB
default "0x00400000" if LOAD_4MB
Expand Down
6 changes: 3 additions & 3 deletions Config.in.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ config LINUX_KERNEL_ARG_STRING_FILE
endif

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

config OF_OVERRIDE_DTB_NAME
string "Override Flattened Device Tree Blob filename"
depends on OF_LIBFDT && SDCARD
depends on OF_LIBFDT && (SDCARD && !FATFS)

config OF_OFFSET
string "The Offset of Flash Device Tree Blob"
depends on OF_LIBFDT && (DATAFLASH || FLASH || NANDFLASH)
depends on OF_LIBFDT && (DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS))
default "0x00008400" if DATAFLASH
default "0x00180000" if NANDFLASH
default "0x00100000" if FLASH
Expand Down
4 changes: 2 additions & 2 deletions Config.in.u-boot
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ menu "U-Boot Image Storage Setup"

config IMG_ADDRESS
string "Flash Offset for U-Boot"
depends on DATAFLASH || FLASH || NANDFLASH
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
default "0x00008000" if FLASH
default "0x00008000" if DATAFLASH
default "0x00040000" if NANDFLASH
Expand All @@ -16,7 +16,7 @@ config IMG_ADDRESS

config IMG_SIZE
string "U-Boot Image Size"
depends on DATAFLASH || FLASH || NANDFLASH
depends on DATAFLASH || FLASH || NANDFLASH || (SDCARD && !FATFS)
default "0x000a0000"
help
at91bootstrap will copy this size of U-Boot image
Expand Down
5 changes: 4 additions & 1 deletion driver/Config.in.nvm
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ config SDHC_8BIT_SUPPORT
used for another interface.

config FATFS
bool
bool "Load image from FAT partition"
depends on SDCARD
default y if SDCARD
help
Load next stage image from FAT12/16/32 partition. Otherwise from raw
block device, image offset must be a multiple 512 bytes.

endmenu

Expand Down
12 changes: 6 additions & 6 deletions driver/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
load_function load_image;
#endif

#ifdef CONFIG_SDCARD
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
char filename[FILENAME_BUF_LEN];
#ifdef CONFIG_OF_LIBFDT
char of_filename[FILENAME_BUF_LEN];
Expand Down Expand Up @@ -42,14 +42,14 @@ load_function get_image_load_func(void)
#endif
}

#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
unsigned int get_image_load_offset(unsigned int addr)
{
#ifdef CONFIG_FLASH
return (addr | 0x10000000);
#endif

#if defined(CONFIG_NANDFLASH) || defined(CONFIG_DATAFLASH)
#if defined(CONFIG_NANDFLASH) || defined(CONFIG_DATAFLASH) || defined(CONFIG_SDCARD)
return addr;
#endif
}
Expand All @@ -58,14 +58,14 @@ unsigned int get_image_load_offset(unsigned int addr)
void init_load_image(struct image_info *image)
{
memset(image, 0, sizeof(*image));
#ifdef CONFIG_SDCARD
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
memset(filename, 0, FILENAME_BUF_LEN);
#ifdef CONFIG_OF_LIBFDT
memset(of_filename, 0, FILENAME_BUF_LEN);
#endif
#endif

#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))

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

#ifdef CONFIG_SDCARD
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
image->filename = filename;
strcpy(image->filename, IMAGE_NAME);
#ifdef CONFIG_OF_LIBFDT
Expand Down
91 changes: 90 additions & 1 deletion driver/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

#include "string.h"

#ifdef CONFIG_FATFS
#include "ff.h"
#else
#include "media.h"
#include "fdt.h"
#endif

#include "debug.h"

#define CHUNK_SIZE 0x40000

#ifdef CONFIG_FATFS
static int sdcard_loadimage(char *filename, BYTE *dest)
{
FIL file;
Expand Down Expand Up @@ -81,7 +87,7 @@ static int sdcard_read_cmd(char *cmdline_file, char *cmdline_args)
ret = -1;
goto read_fail;
}

ret = 0;

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

}
#endif
#else
#if defined(CONFIG_LOAD_LINUX) || defined(CONFIG_LOAD_ANDROID)
static int update_image_length(unsigned int offset,
unsigned char *dest,
unsigned char flag)
{
unsigned int length = 512;
int ret;

dbg_info("SD/MMC: update image length from image\n");

memcpy(dest, (const char *)offset, length);

if (flag == KERNEL_IMAGE)
return kernel_size(dest);
#ifdef CONFIG_OF_LIBFDT
else {
ret = check_dt_blob_valid((void *)dest);
if (!ret)
return of_get_dt_total_size((void *)dest);
}
#endif
return -1;
}
#endif
#endif


int load_sdcard(struct image_info *image)
{
#ifdef CONFIG_FATFS
FATFS fs;
FRESULT fret;
#else
unsigned int start_block;
unsigned int block_count;
#endif
int ret;

#ifdef CONFIG_AT91_MCI
Expand All @@ -113,6 +151,7 @@ int load_sdcard(struct image_info *image)
at91_sdhc_hw_init();
#endif

#ifdef CONFIG_FATFS
/* mount fs */
fret = f_mount(0, &fs);
if (fret != FR_OK) {
Expand Down Expand Up @@ -171,4 +210,54 @@ int load_sdcard(struct image_info *image)
}

return 0;
#else
ret = sdcard_initialize();
if (ret) {
return ret;
}

#if defined(CONFIG_LOAD_LINUX) || defined(CONFIG_LOAD_ANDROID)
int length = update_image_length(image->offset, image->dest, KERNEL_IMAGE);
if (length == -1)
return -1;

image->length = length;
#endif

dbg_info("SD/MMC: Copy %x bytes from %x to %x\n",
image->length, image->offset, image->dest);

start_block = image->offset / 512;
block_count = (image->length + 511) / 512;

ret = sdcard_block_read(start_block, block_count, image->dest);
ret = ret == block_count ? 0 : -1;
if (ret) {
return ret;
}

#ifdef CONFIG_OF_LIBFDT
if(image->of_dest) {
length = update_image_length(image->of_offset,
image->of_dest, DT_BLOB);
if (length == -1)
return -1;

image->of_length = length;

dbg_info("SD/MMC: dt blob: Copy %x bytes from %x to %x\n",
image->of_length, image->of_offset, image->of_dest);

start_block = image->of_offset / 512;
block_count = (image->of_length + 511) / 512;

ret = sdcard_block_read(start_block, block_count, image->of_dest);
ret = ret == block_count ? 0 : -1;
if (ret) {
return ret;
}
}
#endif
return 0;
#endif
}
10 changes: 5 additions & 5 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ enum {
/* structure definition */
struct image_info
{
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
unsigned int offset;
unsigned int length;
#endif
#ifdef CONFIG_SDCARD
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
char *filename;
#ifdef CONFIG_OVERRIDE_CMDLINE_FROM_EXT_FILE
char *cmdline_file;
Expand All @@ -44,11 +44,11 @@ struct image_info
unsigned char *dest;

#ifdef CONFIG_OF_LIBFDT
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
unsigned int of_offset;
unsigned int of_length;
#endif
#ifdef CONFIG_SDCARD
#if defined(CONFIG_SDCARD) && defined(CONFIG_FATFS)
char *of_filename;
#endif
unsigned char *of_dest;
Expand All @@ -61,7 +61,7 @@ typedef int (*load_function)(struct image_info *image);

load_function get_image_load_func(void);

#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH)
#if defined(CONFIG_DATAFLASH) || defined(CONFIG_NANDFLASH) || defined(CONFIG_FLASH) || (defined(CONFIG_SDCARD) && !defined(CONFIG_FATFS))
unsigned int get_image_load_offset(unsigned int addr);
#endif

Expand Down