From 4a760066a846960c2cb363027109a033d5ef0332 Mon Sep 17 00:00:00 2001 From: Inha Song Date: Mon, 28 Oct 2013 15:52:53 +0900 Subject: [PATCH] board:samsung: add u-boot-mmc.bin sign check to thor Changes: - add call to check_board_signature() before do dfu_write in thor downlaoder new files: - board/samsung/common/sig_header.c - include/samsung/sighdr.h Signed-off-by: Inha Song Signed-off-by: Przemyslaw Marczak --- board/samsung/common/Makefile | 1 + board/samsung/common/sig_header.c | 52 +++++++++++++++++++++++++++++++++++++++ drivers/usb/gadget/f_thor.c | 11 +++++++++ include/samsung/sighdr.h | 20 +++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 board/samsung/common/sig_header.c create mode 100644 include/samsung/sighdr.h diff --git a/board/samsung/common/Makefile b/board/samsung/common/Makefile index 93347ef..701ef79 100644 --- a/board/samsung/common/Makefile +++ b/board/samsung/common/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_SOFT_I2C_MULTI_BUS) += multi_i2c.o obj-$(CONFIG_USBDOWNLOAD_GADGET) += gadget.o obj-$(CONFIG_MISC_COMMON) += misc.o +obj-$(CONFIG_SIG) += sig_header.o ifndef CONFIG_SPL_BUILD obj-$(CONFIG_BOARD_COMMON) += board.o diff --git a/board/samsung/common/sig_header.c b/board/samsung/common/sig_header.c new file mode 100644 index 0000000..0219d59 --- /dev/null +++ b/board/samsung/common/sig_header.c @@ -0,0 +1,52 @@ +#include +#include + +//static struct sig_header * + +int get_image_signature(struct sig_header *hdr, + phys_addr_t base_addr, + phys_size_t size) +{ + memcpy((void *)hdr, (const void *)(base_addr + size - HDR_SIZE), + HDR_SIZE); + + if (hdr->magic != HDR_BOOT_MAGIC) { + printf("no sign found\n"); + debug("can't found signature\n"); + return 1; + } + + return 0; +} + +int check_board_signature(char *fname, phys_addr_t dn_addr, phys_size_t size) +{ + struct sig_header bh_target; + struct sig_header bh_addr; + int ret; + + /* u-boot-mmc.bin */ + if (strcmp(fname, "u-boot-mmc.bin")) + return 0; + + /* can't found signature in target - download continue */ + ret = get_image_signature(&bh_target, (phys_addr_t)CONFIG_SYS_TEXT_BASE, + size); + if (ret) + return 0; + + /* can't found signature in address - download stop */ + ret = get_image_signature(&bh_addr, dn_addr, size); + if (ret) + return -1; + + if (strncmp(bh_target.bd_name, bh_addr.bd_name, + ARRAY_SIZE(bh_target.bd_name))) { + debug("board name Inconsistency\n"); + return -1; + } + + debug("board signature check OK\n"); + + return 0; +} diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c index 650b249..54dd17c 100644 --- a/drivers/usb/gadget/f_thor.c +++ b/drivers/usb/gadget/f_thor.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -220,6 +221,16 @@ static int download_tail(long long int left, int cnt) debug("%s: left: %llu cnt: %d\n", __func__, left, cnt); +#ifdef CONFIG_SIG + /* check board signature when download u-boot-mmc.bin */ + ret = check_board_signature(f_name, (phys_addr_t)transfer_buffer, + (phys_size_t)thor_file_size); + if (ret) { + printf("Wrong board signature in file: %s.\n", f_name); + return ret; + } +#endif + if (left) { ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++); if (ret) { diff --git a/include/samsung/sighdr.h b/include/samsung/sighdr.h new file mode 100644 index 0000000..571e010 --- /dev/null +++ b/include/samsung/sighdr.h @@ -0,0 +1,20 @@ +#ifndef __HEADER_H__ +#define __HEADER_H__ + +#define HDR_BOOT_MAGIC 0x744f6f42 /* BoOt */ + +#define HDR_SIZE sizeof(struct sig_header) + +/* HDR_SIZE - 512 */ +struct sig_header { + uint32_t magic; /* image magic number */ + uint32_t size; /* image data size */ + uint32_t valid; /* valid flag */ + char date[12]; /* image creation timestamp - YYMMDDHH */ + char version[24]; /* image version */ + char bd_name[16]; /* target board name */ + char reserved[448]; /* reserved */ +}; + +int check_board_signature(char *fname, phys_addr_t dn_addr, phys_size_t size); +#endif -- 2.7.4