nmhw: redundant boot of NRSW bootloader images

- support loading of regular and NRSW packed uboot images
- add image size sanity check
- add crc check for uboot images to ensure integrity
- boot order:
  - nrsw, main location
  - regular, main location
  - nrsw, alternative location
  - regular, alternative location
This commit is contained in:
Rene Straub 2019-10-09 15:26:00 +02:00
parent 161b30f72e
commit a0e3f2f124
1 changed files with 106 additions and 60 deletions

View File

@ -15,31 +15,34 @@
#include <errno.h> #include <errno.h>
#include <mmc.h> #include <mmc.h>
#include <image.h> #include <image.h>
#include <crc.h>
DECLARE_GLOBAL_DATA_PTR; DECLARE_GLOBAL_DATA_PTR;
static int image_nm_magic(const struct nm_header *header) #define UBOOT_MIN_SIZE_BYTES (128*1024)
#define UBOOT_MAX_SIZE_BYTES (2500*1024)
static int image_nm_header_ok(const struct nm_header *header)
{ {
u32 start_tag = __be32_to_cpu(header->nm_start_tag); u32 start_tag = __be32_to_cpu(header->nm_start_tag);
u32 length = __be32_to_cpu(header->nm_length); u32 length = __be32_to_cpu(header->nm_length);
printf("checking nm header. start tag = 0x%08x, length = %d\n", start_tag, length);
if ((start_tag == 0x424c5354) && if ((start_tag == 0x424c5354) &&
(length > 128*1024) && (length < 2500*1024)) (length >= UBOOT_MIN_SIZE_BYTES) && (length <= UBOOT_MAX_SIZE_BYTES))
{ {
printf("valid netmodule bootloader found\n");
return 0; return 0;
} }
else { else {
return -1; return -EFAULT;
} }
} }
static int mmc_load_netmodule(struct mmc *mmc, ulong sector, static int mmc_load_netmodule(struct mmc *mmc, ulong sector,
const struct nm_header *header) const struct nm_header *header)
{ {
u32 nm_header_size = sizeof(struct nm_header) -sizeof(struct image_header);
const struct image_header *im_header; const struct image_header *im_header;
const __be32* end_tag_position; const __be32* end_tag_position;
u32 image_size_sectors; u32 image_size_sectors;
@ -48,43 +51,44 @@ static int mmc_load_netmodule(struct mmc *mmc, ulong sector,
int ret; int ret;
im_header = &(header->header); im_header = &(header->header);
printf("nm header %p\n", header);
printf("im header %p\n", im_header);
/* decode header and fill out spl_image structure */ /* decode header and fill out spl_image structure */
ret = spl_parse_image_header(im_header); ret = spl_parse_image_header(im_header);
printf("mmc_load_netmodule checking header 1: %d\n", ret); if (ret) {
if (ret)
return ret; return ret;
}
printf("spl header: load addr 0x%08x\n", spl_image.load_addr); /*
printf("spl header: size %d\n", spl_image.size); * convert size to sectors - round up,
/* convert size to sectors - round up,
* add 256 bytes for NetModule header, 4 bytes for NetModule trailer * add 256 bytes for NetModule header, 4 bytes for NetModule trailer
*/ */
image_size_sectors = (spl_image.size + 256 + 4 + mmc->read_bl_len - 1) / image_size_sectors = (spl_image.size + nm_header_size + sizeof(u32) +
mmc->read_bl_len - 1) /
mmc->read_bl_len; mmc->read_bl_len;
printf("mmc_load_netmodule 3: %u\n", image_size_sectors);
/* Read the header too to avoid extra memcpy, /*
* Read the header too to avoid extra memcpy,
* compensate the load address for the NetModule header * compensate the load address for the NetModule header
*/ */
count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
(void *)(ulong)spl_image.load_addr - 256); (void *)(ulong)spl_image.load_addr - nm_header_size);
debug("read %x sectors to %x\n", image_size_sectors, debug("read %x sectors to %x\n", image_size_sectors,
spl_image.load_addr); spl_image.load_addr);
if (count != image_size_sectors) if (count != image_size_sectors) {
puts(", read error");
return -EIO; return -EIO;
}
/* Check NetModule end tag to be sure image is completely written */ /* Check NetModule end tag to be sure image is completely written */
end_tag_position = (const __be32*)(spl_image.load_addr + spl_image.size); end_tag_position = (const __be32*)(spl_image.load_addr + spl_image.size);
printf("end tag address %p\n", end_tag_position);
end_tag = __be32_to_cpu(*end_tag_position); end_tag = __be32_to_cpu(*end_tag_position);
printf("end tag 0x%08x\n", end_tag); if (end_tag != 0x424c454e) {
if (end_tag != 0x424c454e) puts(", end marker fail");
return -EFAULT; return -EFAULT;
}
puts(", end marker ok");
return 0; return 0;
} }
@ -95,12 +99,12 @@ static int mmc_load_image_raw_sector_netmodule(struct mmc *mmc, unsigned long se
struct nm_header *header; struct nm_header *header;
int ret = 0; int ret = 0;
printf("\ntrying to load NM packaged u-boot from sector %lu\n", sector); printf("Checking NM U-Boot at sector 0x%lx: ", sector);
header = (struct nm_header *)(CONFIG_SYS_TEXT_BASE - header = (struct nm_header *)(CONFIG_SYS_TEXT_BASE -
sizeof(struct nm_header)); sizeof(struct nm_header));
/* read image header to find the image size & load address */ /* read image header to find the image size and load address */
count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header); count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
debug("hdr read sector %lx, count=%lu\n", sector, count); debug("hdr read sector %lx, count=%lu\n", sector, count);
if (count == 0) { if (count == 0) {
@ -108,34 +112,66 @@ static int mmc_load_image_raw_sector_netmodule(struct mmc *mmc, unsigned long se
goto end; goto end;
} }
printf("header loaded to %p\n", header); if (image_nm_header_ok(header)) {
puts("hdr fail");
if (image_nm_magic(header) == 0) {
ret = mmc_load_netmodule(mmc, sector, header);
printf("NM image loaded, ret = %d\n", ret);
} else {
ret = -EFAULT; ret = -EFAULT;
goto end;
} }
puts("hdr ok");
ret = mmc_load_netmodule(mmc, sector, header);
end: end:
if (ret) { if (ret) {
puts(" -> skipping\n");
return -1; return -1;
} }
puts("\n");
return 0; return 0;
} }
static int mmc_load_legacy(struct mmc *mmc, ulong sector, static int mmc_load_legacy(struct mmc *mmc, ulong sector,
struct image_header *header) struct image_header *header)
{ {
u32 header_size = sizeof(struct image_header);
u32 image_size_sectors; u32 image_size_sectors;
u32 image_size_bytes;
u32 data_crc;
u32 data_crc_memory;
unsigned long count; unsigned long count;
int ret; int ret;
/*
* Note: spl_parse_image_header() always returns 0
* Own header check required here.
*/
if (image_get_magic(header) != IH_MAGIC) {
puts("hdr fail");
return -EFAULT;
}
ret = spl_parse_image_header(header); ret = spl_parse_image_header(header);
if (ret) if (ret)
return ret; return ret;
puts("hdr ok");
/* TODO: This assumes a header is present (e.g. no PPC images) */
/* Could also check spl_image.flags & SPL_COPY_PAYLOAD_ONLY above */
image_size_bytes = __be32_to_cpu(header->ih_size);
data_crc = __be32_to_cpu(header->ih_dcrc);
if ((image_size_bytes < UBOOT_MIN_SIZE_BYTES) || (image_size_bytes > UBOOT_MAX_SIZE_BYTES)) {
puts(", data len fail");
return -EFAULT;
}
puts(", data len ok");
/* convert size to sectors - round up */ /* convert size to sectors - round up */
image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
mmc->read_bl_len; mmc->read_bl_len;
@ -143,10 +179,22 @@ static int mmc_load_legacy(struct mmc *mmc, ulong sector,
/* Read the header too to avoid extra memcpy */ /* Read the header too to avoid extra memcpy */
count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
(void *)(ulong)spl_image.load_addr); (void *)(ulong)spl_image.load_addr);
debug("read %x sectors to %x\n", image_size_sectors, debug("read %x sectors to %x\n", image_size_sectors,
spl_image.load_addr); spl_image.load_addr);
if (count != image_size_sectors) if (count != image_size_sectors) {
puts(", read error");
return -EIO; return -EIO;
}
/* Check data CRC */
data_crc_memory = crc32(0, (const u8*)(spl_image.load_addr + header_size), image_size_bytes);
if (data_crc_memory != data_crc) {
puts(", data crc fail");
return -EFAULT;
}
puts(", data crc ok");
return 0; return 0;
} }
@ -165,6 +213,8 @@ static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
struct image_header *header; struct image_header *header;
int ret = 0; int ret = 0;
printf("Checking U-Boot at sector 0x%lx: ", sector);
header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
sizeof(struct image_header)); sizeof(struct image_header));
@ -172,6 +222,11 @@ static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header); count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
debug("hdr read sector %lx, count=%lu\n", sector, count); debug("hdr read sector %lx, count=%lu\n", sector, count);
if (count == 0) { if (count == 0) {
/*
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
puts("mmc_load_image_raw_sector: mmc block read error\n");
#endif
*/
ret = -EIO; ret = -EIO;
goto end; goto end;
} }
@ -193,12 +248,12 @@ static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
end: end:
if (ret) { if (ret) {
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT puts(" -> skipping\n");
puts("mmc_load_image_raw_sector: mmc block read error\n");
#endif
return -1; return -1;
} }
puts("\n");
return 0; return 0;
} }
@ -430,39 +485,30 @@ int spl_mmc_load_image(u32 boot_device)
return err; return err;
#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
if (*(u32*)0x90000004 == 0x12345678) { /* Try to load NetModule packed bootloader from main location */
printf("Testing redundancy boot\n");
/* Try to load NetModule packed bootloader from
* main location */
err = mmc_load_image_raw_sector_netmodule(mmc, err = mmc_load_image_raw_sector_netmodule(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
if (!err) if (!err)
return err; /* found -> ok */ return err; /* found -> ok */
/* Try to load NetModule packed bootloader from
* alternate location */
err = mmc_load_image_raw_sector_netmodule(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_ALTERNATE);
if (!err)
return err; /* found -> ok */
/* Try to load regular bootloader from
* alternate location */
err = mmc_load_image_raw_sector(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_ALTERNATE);
if (!err) {
puts("starting alternate bootloader\n");
return err; /* found -> ok */
}
}
/* Try to load bootloader from main location */ /* Try to load bootloader from main location */
err = mmc_load_image_raw_sector(mmc, err = mmc_load_image_raw_sector(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
if (!err) if (!err)
return err; /* found -> ok */ return err; /* found -> ok */
/* Try to load NetModule packed bootloader from alternate location */
err = mmc_load_image_raw_sector_netmodule(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_ALTERNATE);
if (!err)
return err; /* found -> ok */
/* Try to load regular bootloader from alternate location */
err = mmc_load_image_raw_sector(mmc,
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_ALTERNATE);
if (!err) {
return err; /* found -> ok */
}
#endif #endif
/* If RAW mode fails, try FS mode. */ /* If RAW mode fails, try FS mode. */
case MMCSD_MODE_FS: case MMCSD_MODE_FS: