misc: sig_header: add the checking board signature
authorJaehoon Chung <jh80.chung@samsung.com>
Mon, 20 Nov 2017 06:09:37 +0000 (15:09 +0900)
committerJaehoon Chung <jh80.chung@samsung.com>
Mon, 26 Mar 2018 02:31:20 +0000 (11:31 +0900)
This patch is based on Inha's patch.

Refer to commit dae47ef49af9
"gadged: thor: add board signatrue check when download 'u-boot-mmc.bin"

Changed from orignal version:
- Moved the location from samsung board directory to misc driver directory

First version added by:
Signed-off-by: Inha Song <ideal.song@samsung.com>
Upgrade:
Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Change-Id: Ia63fe8fd71b8c26ce20507e8126183ab4c55644c
Signed-off-by: Jaehoon Chung <jh80.chung@samsung.com>
drivers/misc/Makefile
drivers/misc/sig_header.c [new file with mode: 0644]
drivers/usb/gadget/f_thor.c
include/samsung/sighdr.h [new file with mode: 0644]

index 63f85a67076cdf94026177d9f25bdcadec1013f8..b14c0152ce3cefb7ffef58c413273b5af997bb77 100644 (file)
@@ -40,3 +40,4 @@ obj-$(CONFIG_RESET) += reset-uclass.o
 obj-$(CONFIG_FSL_DEVICE_DISABLE) += fsl_devdis.o
 obj-$(CONFIG_SENSORID) += sensorid-uclass.o
 obj-$(CONFIG_SENSORID_ARTIK) += sensorid_artik.o
+obj-$(CONFIG_SIG) += sig_header.o
diff --git a/drivers/misc/sig_header.c b/drivers/misc/sig_header.c
new file mode 100644 (file)
index 0000000..9d62c14
--- /dev/null
@@ -0,0 +1,59 @@
+#include <common.h>
+#include <errno.h>
+#include <samsung/sighdr.h>
+
+static 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)
+               return -EFAULT;
+
+       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 (strncmp(fname, "u-boot-mmc.bin", 14))
+               return 0;
+
+       /* can't found signature in target - download continue */
+       ret = get_image_signature(&bh_target, (phys_addr_t)CONFIG_SYS_TEXT_BASE,
+                                 CONFIG_SIG_IMAGE_SIZE);
+       if (ret)
+               return 0;
+
+       printf("U-Boot signature: \"%.*s\"\n", 16, bh_target.bd_name);
+
+       if (size != CONFIG_SIG_IMAGE_SIZE) {
+               printf("Bad file size for: %s.\n", fname);
+               printf("Expected: %#x bytes, has: %#x bytes.\n",
+                      CONFIG_SIG_IMAGE_SIZE, (unsigned)size);
+               return -EINVAL;
+       }
+       printf("%s ", fname);
+       /* can't found signature in address - download stop */
+       ret = get_image_signature(&bh_addr, dn_addr, CONFIG_SIG_IMAGE_SIZE);
+       if (ret) {
+               printf("signature not found.\n");
+               return -EFAULT;
+       }
+       printf("signature: \"%.*s\". ", 16, bh_addr.bd_name);
+
+       if (strncmp(bh_target.bd_name, bh_addr.bd_name,
+                   ARRAY_SIZE(bh_target.bd_name))) {
+               printf("Invalid!\n");
+               return -EPERM;
+       }
+
+       printf("OK!\n");
+       return 0;
+}
index 74443fe767e4d4e2b122fa887899da9ef5b5bd1d..479066e185366d30e4c004424657c5c620738c3a 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/usb/gadget.h>
 #include <linux/usb/composite.h>
 #include <linux/usb/cdc.h>
+#include <samsung/sighdr.h>
 #include <g_dnl.h>
 #include <dfu.h>
 
@@ -228,6 +229,14 @@ static int download_tail(long long int left, int cnt)
                return -ENXIO;
        }
 
+#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)
+               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 (file)
index 0000000..f97f51d
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __HEADER_H__
+#define __HEADER_H__
+
+#define HDR_BOOT_MAGIC         0x744f6f42      /* BoOt */
+
+#define HDR_SIZE               sizeof(struct sig_header)
+
+/* Size of u-boot-mmc.bin - should be always padded to 1MB */
+#define CONFIG_SIG_IMAGE_SIZE  SZ_1M
+
+/* 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