mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* i.MX HABv4 rework and HABv3 support
@ 2016-01-29 10:43 Sascha Hauer
  2016-01-29 10:43 ` [PATCH 01/23] ARM: i.MX: Add HABv3 Kconfig variables Sascha Hauer
                   ` (22 more replies)
  0 siblings, 23 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

This series reworks the i.MX HABv4 support and adds support for HABv3 as
found on i.MX25, i.MX35 and i.MX51.

HABv4 support was implemented using a relatively complicated makefile
in images/Makefile.imxhabv4. Since the makefiles in images/ are
complicated enough already this is changed in this series. We now
call CST directly from imx-image. With this we can configure CST
from the imx-image configuration file. No additional Makefile changes
are needed, so we can remove Makefile.imxhabv4.

This series also adds HABv3 support. This is slightly different from
HABv4 support. For HABv3 the CSF needs the certificates in DER format
rather than PEM format. Also the super root key hash is not included
in the CSF but in the flash header.

This series has been tested on an i.MX25 for HABv3 and an i.MX6 for
HABv4.

Sascha

----------------------------------------------------------------
Sascha Hauer (23):
      ARM: i.MX: Add HABv3 Kconfig variables
      imx: hab: rename driver dir to hab/
      hab: Add HABv3 status report function
      scripts: imx-usb-loader: Make readonly arguments const
      scripts: imx-usb-loader: Move definitions up
      scripts: imx-image: Allow dcd offset 0x0
      scripts: imx-usb-loader: fully read images into memory
      scripts: imx-usb-loader: Move load_file up
      scripts: imx: Consolidate flash headers in imx tools
      scripts: imx-image: Add context struct to config parsers
      scripts: imx-image: move write_mem to context data
      scripts: imx-image: move check to context data
      scripts: imx: move macro definitions to common header file
      scripts: imx: move config file parser to separate file
      scripts: imx: make libusb variables global
      scripts: imx-usb-loader: Add -s and -i options
      scripts: imx: Drop double check
      scripts: imx-image: move more variables to context data
      scripts: imx-image: pass config data to add_header_*
      scripts: imx-image: Support adding a Super Root Key to the image
      scripts: imx: Create CSF files from imx config file
      scripts: imx: Allow to create signed images
      scripts: imx: Generate signed images with imx-image

 arch/arm/mach-imx/Kconfig                          |  34 +
 .../arm/mach-imx/include/mach/habv3-imx25-gencsf.h |  43 ++
 arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h |  44 ++
 drivers/Makefile                                   |   2 +-
 drivers/hab/Makefile                               |   2 +
 drivers/hab/habv3.c                                |  78 +++
 drivers/{habv4 => hab}/habv4.c                     |   2 +-
 drivers/habv4/Makefile                             |   1 -
 images/Makefile                                    |   1 -
 images/Makefile.imxhabv4                           |  48 --
 include/{habv4.h => hab.h}                         |   9 +
 scripts/habv4/gencsf.sh                            |  47 --
 scripts/habv4/habv4-imx28.csf.in                   |  33 -
 scripts/habv4/habv4-imx6.csf.in                    |  37 --
 scripts/imx/Makefile                               |   8 +-
 scripts/imx/imx-image.c                            | 724 +++++++++------------
 scripts/imx/imx-usb-loader.c                       | 666 ++++++++-----------
 scripts/imx/imx.c                                  | 442 +++++++++++++
 scripts/imx/imx.h                                  |  76 +++
 19 files changed, 1337 insertions(+), 960 deletions(-)
 create mode 100644 arch/arm/mach-imx/include/mach/habv3-imx25-gencsf.h
 create mode 100644 arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h
 create mode 100644 drivers/hab/Makefile
 create mode 100644 drivers/hab/habv3.c
 rename drivers/{habv4 => hab}/habv4.c (99%)
 delete mode 100644 drivers/habv4/Makefile
 delete mode 100644 images/Makefile.imxhabv4
 rename include/{habv4.h => hab.h} (86%)
 delete mode 100755 scripts/habv4/gencsf.sh
 delete mode 100644 scripts/habv4/habv4-imx28.csf.in
 delete mode 100644 scripts/habv4/habv4-imx6.csf.in
 create mode 100644 scripts/imx/imx.c
 create mode 100644 scripts/imx/imx.h


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 01/23] ARM: i.MX: Add HABv3 Kconfig variables
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 02/23] imx: hab: rename driver dir to hab/ Sascha Hauer
                   ` (21 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

For signing i.MX images with HABv3 we need several certificates. Add
their pathes to Kconfig variables to make them available to the build
system.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-imx/Kconfig | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
index f2dc52d..c78d3ed 100644
--- a/arch/arm/mach-imx/Kconfig
+++ b/arch/arm/mach-imx/Kconfig
@@ -728,6 +728,28 @@ config HABV4_IMG_CRT_PEM
 
 endif
 
+config HABV3
+	tristate "HABv3 support"
+	depends on ARCH_IMX25
+	help
+	  High Assurance Boot, as found on i.MX25.
+
+if HABV3
+
+config HABV3_SRK_PEM
+	string "Path to SRK Certificate (PEM)"
+	default "../crts/SRK1_sha256_2048_65537_v3_ca_x509_crt.pem"
+
+config HABV3_CSF_CRT_DER
+	string "Path to CSF certificate"
+	default "../crts/CSF1_1_sha256_2048_65537_v3_ca_crt.der"
+
+config HABV3_IMG_CRT_DER
+	string "Path to IMG certificate"
+	default "../crts/IMG1_1_sha256_2048_65537_v3_usr_crt.der"
+
+endif
+
 endmenu
 
 endif
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 02/23] imx: hab: rename driver dir to hab/
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
  2016-01-29 10:43 ` [PATCH 01/23] ARM: i.MX: Add HABv3 Kconfig variables Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 03/23] hab: Add HABv3 status report function Sascha Hauer
                   ` (20 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

There's not only HABv4 but also HABv3. No need to put the corresponding
code in separate directories, so rename the habv4 directory to hab.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-imx/Kconfig |   5 +
 drivers/Makefile          |   2 +-
 drivers/hab/Makefile      |   1 +
 drivers/hab/habv4.c       | 237 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/habv4/Makefile    |   1 -
 drivers/habv4/habv4.c     | 237 ----------------------------------------------
 include/hab.h             |  30 ++++++
 include/habv4.h           |  30 ------
 8 files changed, 274 insertions(+), 269 deletions(-)
 create mode 100644 drivers/hab/Makefile
 create mode 100644 drivers/hab/habv4.c
 delete mode 100644 drivers/habv4/Makefile
 delete mode 100644 drivers/habv4/habv4.c
 create mode 100644 include/hab.h
 delete mode 100644 include/habv4.h

diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
index c78d3ed..9e7be2e 100644
--- a/arch/arm/mach-imx/Kconfig
+++ b/arch/arm/mach-imx/Kconfig
@@ -688,8 +688,12 @@ config IMX_OCOTP_WRITE
 		mw -l -d /dev/imx-ocotp 0x8C 0x00001234
 		mw -l -d /dev/imx-ocotp 0x88 0x56789ABC
 
+config HAB
+	bool
+
 config HABV4
 	tristate "HABv4 support"
+	select HAB
 	depends on ARCH_IMX6
 	help
 	  High Assurance Boot, as found on i.MX28/i.MX6.
@@ -730,6 +734,7 @@ endif
 
 config HABV3
 	tristate "HABv3 support"
+	select HAB
 	depends on ARCH_IMX25
 	help
 	  High Assurance Boot, as found on i.MX25.
diff --git a/drivers/Makefile b/drivers/Makefile
index 3afbb61..8a8c8c4 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -30,4 +30,4 @@ obj-$(CONFIG_PCI) += pci/
 obj-y += rtc/
 obj-$(CONFIG_FIRMWARE) += firmware/
 obj-$(CONFIG_GENERIC_PHY) += phy/
-obj-$(CONFIG_HABV4) += habv4/
+obj-$(CONFIG_HAB) += hab/
diff --git a/drivers/hab/Makefile b/drivers/hab/Makefile
new file mode 100644
index 0000000..5c3b00c
--- /dev/null
+++ b/drivers/hab/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_HABV4) += habv4.o
diff --git a/drivers/hab/habv4.c b/drivers/hab/habv4.c
new file mode 100644
index 0000000..8521b88
--- /dev/null
+++ b/drivers/hab/habv4.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2014, 2015 Marc Kleine-Budde <mkl@pengutronix.de>
+ * Copyright (C) 2010 Freescale Semiconductor, Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#define pr_fmt(fmt)  "HABv4: " fmt
+
+#include <common.h>
+#include <hab.h>
+#include <types.h>
+
+#include <mach/generic.h>
+
+#define HABV4_RVT_IMX28 0xffff8af8
+#define HABV4_RVT_IMX6 0x00000094
+
+enum hab_tag {
+	HAB_TAG_IVT = 0xd1,		/* Image Vector Table */
+	HAB_TAG_DCD = 0xd2,		/* Device Configuration Data */
+	HAB_TAG_CSF = 0xd4,		/* Command Sequence File */
+	HAB_TAG_CRT = 0xd7, 		/* Certificate */
+	HAB_TAG_SIG = 0xd8,		/* Signature */
+	HAB_TAG_EVT = 0xdb,		/* Event */
+	HAB_TAG_RVT = 0xdd,		/* ROM Vector Table */
+	HAB_TAG_WRP = 0x81,		/* Wrapped Key */
+	HAB_TAG_MAC = 0xac,		/* Message Authentication Code */
+};
+
+/* Status definitions */
+enum hab_status {
+	HAB_STATUS_ANY = 0x00,		/* Match any status in report_event */
+	HAB_STATUS_FAILURE = 0x33,	/* Operation failed */
+	HAB_STATUS_WARNING = 0x69,	/* Operation completed with warning */
+	HAB_STATUS_SUCCESS = 0xf0,	/* Operation completed successfully */
+};
+
+/* Security Configuration definitions */
+enum hab_config {
+	HAB_CONFIG_FAB = 0x00,		/* Un-programmed IC */
+	HAB_CONFIG_RETURN = 0x33,	/* Field Return IC */
+	HAB_CONFIG_OPEN = 0xf0,		/* Non-secure IC */
+	HAB_CONFIG_CLOSED = 0xcc,	/* Secure IC */
+};
+
+/* State definitions */
+enum hab_state {
+	HAB_STATE_INITIAL = 0x33,	/* Initialising state (transitory) */
+	HAB_STATE_CHECK = 0x55,		/* Check state (non-secure) */
+	HAB_STATE_NONSECURE = 0x66,	/* Non-secure state */
+	HAB_STATE_TRUSTED = 0x99,	/* Trusted state */
+	HAB_STATE_SECURE = 0xaa,	/* Secure state */
+	HAB_STATE_FAIL_SOFT = 0xcc,	/* Soft fail state */
+	HAB_STATE_FAIL_HARD = 0xff,	/* Hard fail state (terminal) */
+	HAB_STATE_NONE = 0xf0,		/* No security state machine */
+};
+
+enum hab_target {
+	HAB_TARGET_MEMORY = 0x0f,	/* Check memory white list */
+	HAB_TARGET_PERIPHERAL = 0xf0,	/* Check peripheral white list*/
+	HAB_TARGET_ANY = 0x55,		/* Check memory & peripheral white list */
+};
+
+enum hab_assertion {
+	HAB_ASSERTION_BLOCK = 0x0,	/* Check if memory is authenticated after CSF */
+};
+
+struct hab_header {
+	uint8_t tag;
+	uint16_t len;			/* len including the header */
+	uint8_t par;
+} __packed;
+
+typedef enum hab_status hab_loader_callback_fn(void **start, uint32_t *bytes, const void *boot_data);
+
+struct habv4_rvt {
+	struct hab_header header;
+	enum hab_status (*entry)(void);
+	enum hab_status (*exit)(void);
+	enum hab_status (*check_target)(enum hab_target target, const void *start, uint32_t bytes);
+	void *(*authenticate_image)(uint8_t cid, uint32_t ivt_offset, void **start, uint32_t *bytes, hab_loader_callback_fn *loader);
+	enum hab_status (*run_dcd)(const void *dcd);
+	enum hab_status (*run_csf)(const void *csf, uint8_t cid);
+	enum hab_status (*assert)(enum hab_assertion assertion, const void *data, uint32_t count);
+	enum hab_status (*report_event)(enum hab_status status, uint32_t index, void *event, uint32_t *bytes);
+	enum hab_status (*report_status)(enum hab_config *config, enum hab_state *state);
+	void (*failsafe)(void);
+} __packed;
+
+static const struct habv4_rvt *__rvt;
+
+static inline const struct habv4_rvt *habv4_get_rvt(void)
+{
+	if (__rvt)
+		return __rvt;
+
+	if (cpu_is_mx28())
+		__rvt = (void *)HABV4_RVT_IMX28;
+	else if (cpu_is_mx6())
+		__rvt = (void *)HABV4_RVT_IMX6;
+
+	if (__rvt->header.tag != HAB_TAG_RVT) {
+		pr_err("ERROR - RVT not found!\n");
+		return NULL;
+	}
+
+	pr_info("Found RVT v%d.%d\n", __rvt->header.par >> 4,
+		__rvt->header.par & 0xf);
+
+	return __rvt;
+}
+
+static const char *habv4_get_status_str(enum hab_status status)
+{
+	switch (status) {
+	case HAB_STATUS_ANY:
+		return "Match any status in report_event"; break;
+	case HAB_STATUS_FAILURE:
+		return "Operation failed"; break;
+	case HAB_STATUS_WARNING:
+		return "Operation completed with warning"; break;
+	case HAB_STATUS_SUCCESS:
+		return "Operation completed successfully"; break;
+	}
+
+	return "<unknown>";
+}
+
+static const char *habv4_get_config_str(enum hab_config config)
+{
+	switch (config) {
+	case HAB_CONFIG_FAB:
+		return "Un-programmed IC"; break;
+	case HAB_CONFIG_RETURN:
+		return "Field Return IC"; break;
+	case HAB_CONFIG_OPEN:
+		return "Non-secure IC"; break;
+	case HAB_CONFIG_CLOSED:
+		return "Secure IC"; break;
+	}
+
+	return "<unknown>";
+}
+
+static const char *habv4_get_state_str(enum hab_state state)
+{
+	switch (state) {
+	case HAB_STATE_INITIAL:
+		return "Initialising state (transitory)"; break;
+	case HAB_STATE_CHECK:
+		return "Check state (non-secure)"; break;
+	case HAB_STATE_NONSECURE:
+		return "Non-secure state"; break;
+	case HAB_STATE_TRUSTED:
+		return "Trusted state"; break;
+	case HAB_STATE_SECURE:
+		return "Secure state"; break;
+	case HAB_STATE_FAIL_SOFT:
+		return "Soft fail state"; break;
+	case HAB_STATE_FAIL_HARD:
+		return "Hard fail state (terminal)"; break;
+	case HAB_STATE_NONE:
+		return "No security state machine"; break;
+	}
+
+	return "<unknown>";
+}
+
+static void habv4_display_event(uint8_t *data, uint32_t len)
+{
+	unsigned int i;
+
+	if (data && len) {
+		for (i = 0; i < len; i++) {
+			if (i == 0)
+				printf(" %02x", data[i]);
+			else if ((i % 8) == 0)
+				printf("\n %02x", data[i]);
+			else if ((i % 4) == 0)
+				printf("  %02x", data[i]);
+			else
+				printf(" %02x", data[i]);
+		}
+	}
+	printf("\n\n");
+}
+
+int habv4_get_status(void)
+{
+	const struct habv4_rvt *rvt = habv4_get_rvt();
+	uint8_t data[256];
+	uint32_t len = sizeof(data);
+	uint32_t index = 0;
+	enum hab_status status;
+	enum hab_config config = 0x0;
+	enum hab_state state = 0x0;
+
+	if (!rvt)
+		return -ENODEV;
+
+	status = rvt->report_status(&config, &state);
+	pr_info("Status: %s (0x%02x)\n", habv4_get_status_str(status), status);
+	pr_info("Config: %s (0x%02x)\n", habv4_get_config_str(config), config);
+	pr_info("State: %s (0x%02x)\n",	habv4_get_state_str(state), state);
+
+	if (status == HAB_STATUS_SUCCESS) {
+		pr_info("No HAB Failure Events Found!\n\n");
+		return 0;
+	}
+
+	while (rvt->report_event(HAB_STATUS_FAILURE, index, data, &len) == HAB_STATUS_SUCCESS) {
+		printf("-------- HAB Event %d --------\n"
+		       "event data:\n", index);
+
+		habv4_display_event(data, len);
+		len = sizeof(data);
+		index++;
+	}
+
+	/* Check reason for stopping */
+	if (rvt->report_event(HAB_STATUS_ANY, index, NULL, &len) == HAB_STATUS_SUCCESS)
+		pr_err("ERROR: Recompile with larger event data buffer (at least %d bytes)\n\n", len);
+
+	return -EPERM;
+}
diff --git a/drivers/habv4/Makefile b/drivers/habv4/Makefile
deleted file mode 100644
index 40b3253..0000000
--- a/drivers/habv4/Makefile
+++ /dev/null
@@ -1 +0,0 @@
-obj-y += habv4.o
diff --git a/drivers/habv4/habv4.c b/drivers/habv4/habv4.c
deleted file mode 100644
index 5ace0de..0000000
--- a/drivers/habv4/habv4.c
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright (C) 2014, 2015 Marc Kleine-Budde <mkl@pengutronix.de>
- * Copyright (C) 2010 Freescale Semiconductor, Inc.
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
-
-#define pr_fmt(fmt)  "HABv4: " fmt
-
-#include <common.h>
-#include <habv4.h>
-#include <types.h>
-
-#include <mach/generic.h>
-
-#define HABV4_RVT_IMX28 0xffff8af8
-#define HABV4_RVT_IMX6 0x00000094
-
-enum hab_tag {
-	HAB_TAG_IVT = 0xd1,		/* Image Vector Table */
-	HAB_TAG_DCD = 0xd2,		/* Device Configuration Data */
-	HAB_TAG_CSF = 0xd4,		/* Command Sequence File */
-	HAB_TAG_CRT = 0xd7, 		/* Certificate */
-	HAB_TAG_SIG = 0xd8,		/* Signature */
-	HAB_TAG_EVT = 0xdb,		/* Event */
-	HAB_TAG_RVT = 0xdd,		/* ROM Vector Table */
-	HAB_TAG_WRP = 0x81,		/* Wrapped Key */
-	HAB_TAG_MAC = 0xac,		/* Message Authentication Code */
-};
-
-/* Status definitions */
-enum hab_status {
-	HAB_STATUS_ANY = 0x00,		/* Match any status in report_event */
-	HAB_STATUS_FAILURE = 0x33,	/* Operation failed */
-	HAB_STATUS_WARNING = 0x69,	/* Operation completed with warning */
-	HAB_STATUS_SUCCESS = 0xf0,	/* Operation completed successfully */
-};
-
-/* Security Configuration definitions */
-enum hab_config {
-	HAB_CONFIG_FAB = 0x00,		/* Un-programmed IC */
-	HAB_CONFIG_RETURN = 0x33,	/* Field Return IC */
-	HAB_CONFIG_OPEN = 0xf0,		/* Non-secure IC */
-	HAB_CONFIG_CLOSED = 0xcc,	/* Secure IC */
-};
-
-/* State definitions */
-enum hab_state {
-	HAB_STATE_INITIAL = 0x33,	/* Initialising state (transitory) */
-	HAB_STATE_CHECK = 0x55,		/* Check state (non-secure) */
-	HAB_STATE_NONSECURE = 0x66,	/* Non-secure state */
-	HAB_STATE_TRUSTED = 0x99,	/* Trusted state */
-	HAB_STATE_SECURE = 0xaa,	/* Secure state */
-	HAB_STATE_FAIL_SOFT = 0xcc,	/* Soft fail state */
-	HAB_STATE_FAIL_HARD = 0xff,	/* Hard fail state (terminal) */
-	HAB_STATE_NONE = 0xf0,		/* No security state machine */
-};
-
-enum hab_target {
-	HAB_TARGET_MEMORY = 0x0f,	/* Check memory white list */
-	HAB_TARGET_PERIPHERAL = 0xf0,	/* Check peripheral white list*/
-	HAB_TARGET_ANY = 0x55,		/* Check memory & peripheral white list */
-};
-
-enum hab_assertion {
-	HAB_ASSERTION_BLOCK = 0x0,	/* Check if memory is authenticated after CSF */
-};
-
-struct hab_header {
-	uint8_t tag;
-	uint16_t len;			/* len including the header */
-	uint8_t par;
-} __packed;
-
-typedef enum hab_status hab_loader_callback_fn(void **start, uint32_t *bytes, const void *boot_data);
-
-struct habv4_rvt {
-	struct hab_header header;
-	enum hab_status (*entry)(void);
-	enum hab_status (*exit)(void);
-	enum hab_status (*check_target)(enum hab_target target, const void *start, uint32_t bytes);
-	void *(*authenticate_image)(uint8_t cid, uint32_t ivt_offset, void **start, uint32_t *bytes, hab_loader_callback_fn *loader);
-	enum hab_status (*run_dcd)(const void *dcd);
-	enum hab_status (*run_csf)(const void *csf, uint8_t cid);
-	enum hab_status (*assert)(enum hab_assertion assertion, const void *data, uint32_t count);
-	enum hab_status (*report_event)(enum hab_status status, uint32_t index, void *event, uint32_t *bytes);
-	enum hab_status (*report_status)(enum hab_config *config, enum hab_state *state);
-	void (*failsafe)(void);
-} __packed;
-
-static const struct habv4_rvt *__rvt;
-
-static inline const struct habv4_rvt *habv4_get_rvt(void)
-{
-	if (__rvt)
-		return __rvt;
-
-	if (cpu_is_mx28())
-		__rvt = (void *)HABV4_RVT_IMX28;
-	else if (cpu_is_mx6())
-		__rvt = (void *)HABV4_RVT_IMX6;
-
-	if (__rvt->header.tag != HAB_TAG_RVT) {
-		pr_err("ERROR - RVT not found!\n");
-		return NULL;
-	}
-
-	pr_info("Found RVT v%d.%d\n", __rvt->header.par >> 4,
-		__rvt->header.par & 0xf);
-
-	return __rvt;
-}
-
-static const char *habv4_get_status_str(enum hab_status status)
-{
-	switch (status) {
-	case HAB_STATUS_ANY:
-		return "Match any status in report_event"; break;
-	case HAB_STATUS_FAILURE:
-		return "Operation failed"; break;
-	case HAB_STATUS_WARNING:
-		return "Operation completed with warning"; break;
-	case HAB_STATUS_SUCCESS:
-		return "Operation completed successfully"; break;
-	}
-
-	return "<unknown>";
-}
-
-static const char *habv4_get_config_str(enum hab_config config)
-{
-	switch (config) {
-	case HAB_CONFIG_FAB:
-		return "Un-programmed IC"; break;
-	case HAB_CONFIG_RETURN:
-		return "Field Return IC"; break;
-	case HAB_CONFIG_OPEN:
-		return "Non-secure IC"; break;
-	case HAB_CONFIG_CLOSED:
-		return "Secure IC"; break;
-	}
-
-	return "<unknown>";
-}
-
-static const char *habv4_get_state_str(enum hab_state state)
-{
-	switch (state) {
-	case HAB_STATE_INITIAL:
-		return "Initialising state (transitory)"; break;
-	case HAB_STATE_CHECK:
-		return "Check state (non-secure)"; break;
-	case HAB_STATE_NONSECURE:
-		return "Non-secure state"; break;
-	case HAB_STATE_TRUSTED:
-		return "Trusted state"; break;
-	case HAB_STATE_SECURE:
-		return "Secure state"; break;
-	case HAB_STATE_FAIL_SOFT:
-		return "Soft fail state"; break;
-	case HAB_STATE_FAIL_HARD:
-		return "Hard fail state (terminal)"; break;
-	case HAB_STATE_NONE:
-		return "No security state machine"; break;
-	}
-
-	return "<unknown>";
-}
-
-static void habv4_display_event(uint8_t *data, uint32_t len)
-{
-	unsigned int i;
-
-	if (data && len) {
-		for (i = 0; i < len; i++) {
-			if (i == 0)
-				printf(" %02x", data[i]);
-			else if ((i % 8) == 0)
-				printf("\n %02x", data[i]);
-			else if ((i % 4) == 0)
-				printf("  %02x", data[i]);
-			else
-				printf(" %02x", data[i]);
-		}
-	}
-	printf("\n\n");
-}
-
-int habv4_get_status(void)
-{
-	const struct habv4_rvt *rvt = habv4_get_rvt();
-	uint8_t data[256];
-	uint32_t len = sizeof(data);
-	uint32_t index = 0;
-	enum hab_status status;
-	enum hab_config config = 0x0;
-	enum hab_state state = 0x0;
-
-	if (!rvt)
-		return -ENODEV;
-
-	status = rvt->report_status(&config, &state);
-	pr_info("Status: %s (0x%02x)\n", habv4_get_status_str(status), status);
-	pr_info("Config: %s (0x%02x)\n", habv4_get_config_str(config), config);
-	pr_info("State: %s (0x%02x)\n",	habv4_get_state_str(state), state);
-
-	if (status == HAB_STATUS_SUCCESS) {
-		pr_info("No HAB Failure Events Found!\n\n");
-		return 0;
-	}
-
-	while (rvt->report_event(HAB_STATUS_FAILURE, index, data, &len) == HAB_STATUS_SUCCESS) {
-		printf("-------- HAB Event %d --------\n"
-		       "event data:\n", index);
-
-		habv4_display_event(data, len);
-		len = sizeof(data);
-		index++;
-	}
-
-	/* Check reason for stopping */
-	if (rvt->report_event(HAB_STATUS_ANY, index, NULL, &len) == HAB_STATUS_SUCCESS)
-		pr_err("ERROR: Recompile with larger event data buffer (at least %d bytes)\n\n", len);
-
-	return -EPERM;
-}
diff --git a/include/hab.h b/include/hab.h
new file mode 100644
index 0000000..f9bf74f
--- /dev/null
+++ b/include/hab.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2014, 2015 Marc Kleine-Budde <mkl@pengutronix.de>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __HABV4_H
+#define __HABV4_H
+
+#ifdef CONFIG_HABV4
+int habv4_get_status(void);
+#else
+static inline int habv4_get_status(void)
+{
+	return -EPERM;
+}
+#endif
+
+#endif /* __HABV4_H */
diff --git a/include/habv4.h b/include/habv4.h
deleted file mode 100644
index f9bf74f..0000000
--- a/include/habv4.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2014, 2015 Marc Kleine-Budde <mkl@pengutronix.de>
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
-
-#ifndef __HABV4_H
-#define __HABV4_H
-
-#ifdef CONFIG_HABV4
-int habv4_get_status(void);
-#else
-static inline int habv4_get_status(void)
-{
-	return -EPERM;
-}
-#endif
-
-#endif /* __HABV4_H */
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 03/23] hab: Add HABv3 status report function
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
  2016-01-29 10:43 ` [PATCH 01/23] ARM: i.MX: Add HABv3 Kconfig variables Sascha Hauer
  2016-01-29 10:43 ` [PATCH 02/23] imx: hab: rename driver dir to hab/ Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 04/23] scripts: imx-usb-loader: Make readonly arguments const Sascha Hauer
                   ` (19 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Status reporting for HABv3 is different from HABv4. Add a status
report function for HABv3.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/hab/Makefile |  1 +
 drivers/hab/habv3.c  | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hab.h        |  9 ++++++
 3 files changed, 88 insertions(+)
 create mode 100644 drivers/hab/habv3.c

diff --git a/drivers/hab/Makefile b/drivers/hab/Makefile
index 5c3b00c..8528ef9 100644
--- a/drivers/hab/Makefile
+++ b/drivers/hab/Makefile
@@ -1 +1,2 @@
 obj-$(CONFIG_HABV4) += habv4.o
+obj-$(CONFIG_HABV3) += habv3.o
diff --git a/drivers/hab/habv3.c b/drivers/hab/habv3.c
new file mode 100644
index 0000000..70f31a3
--- /dev/null
+++ b/drivers/hab/habv3.c
@@ -0,0 +1,78 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+#define pr_fmt(fmt) "HABv3: " fmt
+
+#include <common.h>
+#include <hab.h>
+#include <io.h>
+
+struct hab_status {
+	u8 value;
+	const char *str;
+};
+
+static struct hab_status hab_status[] = {
+	{ 0x8d, "data specified is out of bounds" },
+	{ 0x55, "error during assert verification" },
+	{ 0x36, "hash verification failed" },
+	{ 0x33, "certificate parsing failed or the certificate contained an unsupported key" },
+	{ 0x35, "signature verification failed" },
+	{ 0x4B, "CSF command sequence contains unsupported command identifier" },
+	{ 0x4e, "absence of expected CSF header" },
+	{ 0x4d, "CSF length is unsupported" },
+	{ 0x2e, "CSF TYPE does not match processor TYPE" },
+	{ 0x2d, "CSF UID does not match either processor UID or generic UID" },
+	{ 0x3a, "CSF customer/product code does not match processor customer/product code" },
+	{ 0x87, "key indexis either unsupported, or an attempt is made to overwrite the SRK from a CSF command" },
+	{ 0x17, "SCC unexpectedly not in secure state" },
+	{ 0x1e, "secureRAM secret key invalid" },
+	{ 0x1d, "secureRAM initialization failure" },
+	{ 0x1b, "secureRAM self test failure" },
+	{ 0x2b, "secureRAM internal failure" },
+	{ 0x27, "secureRAM secrect key unexpectedly in use" },
+	{ 0x8b, "an attempt is made to read a key from the list of subordinate public keys at a location "
+		"where no key is installed" },
+	{ 0x8e, "algorithm type is either invalid or ortherwise unsupported" },
+	{ 0x66, "write operation to register failed" },
+	{ 0x63, "DCD invalid" },
+	{ 0x6f, "RAM application pointer is NULL or ERASED_FLASH" },
+	{ 0x69, "CSF missing when HAB TYPE is not HAB-disabled" },
+	{ 0x6a, "NANDFC boot buffer load failed" },
+	{ 0x6c, "Exception has occured" },
+	{ 0x67, "INT_BOOT fuse is blown but BOOT pins are set for external boot" },
+	{ 0x88, "Successful download completion" },
+};
+
+int imx_habv3_get_status(uint32_t status)
+{
+	int i;
+
+	if (status == 0xf0) {
+		pr_info("status OK\n");
+		return 0;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(hab_status); i++) {
+		if (hab_status[i].value == status) {
+			pr_err("status: 0x%02x: %s\n", status, hab_status[i].str);
+			return -EPERM;
+		}
+	}
+
+	pr_err("unknown status code 0x%02x\n", status);
+
+	return -EPERM;
+}
+
+int imx25_hab_get_status(void)
+{
+	return imx_habv3_get_status(readl(IOMEM(0x780018d4)));
+}
\ No newline at end of file
diff --git a/include/hab.h b/include/hab.h
index f9bf74f..411e995 100644
--- a/include/hab.h
+++ b/include/hab.h
@@ -27,4 +27,13 @@ static inline int habv4_get_status(void)
 }
 #endif
 
+#ifdef CONFIG_HABV3
+int imx25_hab_get_status(void);
+#else
+static inline int imx25_hab_get_status(void)
+{
+	return -EPERM;
+}
+#endif
+
 #endif /* __HABV4_H */
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 04/23] scripts: imx-usb-loader: Make readonly arguments const
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (2 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 03/23] hab: Add HABv3 status report function Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 05/23] scripts: imx-usb-loader: Move definitions up Sascha Hauer
                   ` (18 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

The buffers passed to dump_long and dump_bytes are not (and shouldn't
be) modified. Make the arguments const.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 94b3763..afe24d1 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -187,9 +187,10 @@ static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id)
 	return NULL;
 }
 
-static void dump_long(unsigned char *src, unsigned cnt, unsigned addr)
+static void dump_long(const void *src, unsigned cnt, unsigned addr)
 {
-	unsigned *p = (unsigned *)src;
+	const unsigned *p = (unsigned *)src;
+
 	while (cnt >= 32) {
 		printf("%08x: %08x %08x %08x %08x  %08x %08x %08x %08x\n",
 				addr, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
@@ -208,9 +209,9 @@ static void dump_long(unsigned char *src, unsigned cnt, unsigned addr)
 	}
 }
 
-static void dump_bytes(unsigned char *src, unsigned cnt, unsigned addr)
+static void dump_bytes(const void *src, unsigned cnt, unsigned addr)
 {
-	unsigned char *p = src;
+	const unsigned char *p = src;
 	int i;
 
 	while (cnt >= 16) {
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 05/23] scripts: imx-usb-loader: Move definitions up
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (3 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 04/23] scripts: imx-usb-loader: Make readonly arguments const Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 06/23] scripts: imx-image: Allow dcd offset 0x0 Sascha Hauer
                   ` (17 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

The macros are needed earlier in a later patch.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index afe24d1..58fa12d 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -35,6 +35,11 @@
 
 #define get_min(a, b) (((a) < (b)) ? (a) : (b))
 
+#define FT_APP	0xaa
+#define FT_CSF	0xcc
+#define FT_DCD	0xee
+#define FT_LOAD_ONLY	0x00
+
 int verbose;
 
 struct mach_id {
@@ -1113,11 +1118,6 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 	return transfer_size;
 }
 
-#define FT_APP	0xaa
-#define FT_CSF	0xcc
-#define FT_DCD	0xee
-#define FT_LOAD_ONLY	0x00
-
 static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		struct usb_work *curr, int verify)
 {
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 06/23] scripts: imx-image: Allow dcd offset 0x0
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (4 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 05/23] scripts: imx-usb-loader: Move definitions up Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 07/23] scripts: imx-usb-loader: fully read images into memory Sascha Hauer
                   ` (16 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

0 is a valid offset for the dcd, so do not use this value to detect
an unset dcd offset. Use 0xffffffff instead.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index e8d9dbf..a85833b 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -38,7 +38,7 @@
 #define CSF_LEN 0x2000		/* length of the CSF (needed for HAB) */
 
 static uint32_t image_load_addr;
-static uint32_t image_dcd_offset;
+static uint32_t image_dcd_offset = 0xffffffff;
 static uint32_t dcdtable[MAX_DCD];
 static int curdcd;
 static int header_version;
@@ -733,7 +733,7 @@ int main(int argc, char *argv[])
 	if (!buf)
 		exit(1);
 
-	if (!image_dcd_offset) {
+	if (image_dcd_offset == 0xffffffff) {
 		fprintf(stderr, "no dcd offset given ('dcdofs'). Defaulting to 0x%08x\n",
 			FLASH_HEADER_OFFSET);
 		image_dcd_offset = FLASH_HEADER_OFFSET;
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 07/23] scripts: imx-usb-loader: fully read images into memory
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (5 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 06/23] scripts: imx-image: Allow dcd offset 0x0 Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 08/23] scripts: imx-usb-loader: Move load_file up Sascha Hauer
                   ` (15 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

imx-usb-loader tries to safe memory by reading the image in chunks.
This is unnecessarily complicated. The images are small, so fully read
them into memory and store them in a single buffer. This makes handling
them a lot easier.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 246 +++++++++++++------------------------------
 1 file changed, 75 insertions(+), 171 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 58fa12d..fa2ce47 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -747,81 +747,42 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_
 }
 
 static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id,
-		FILE *xfile, unsigned offset, unsigned addr, unsigned size,
-		unsigned char *verify_buffer, unsigned verify_cnt)
+			 const void *buf, unsigned len, unsigned addr)
 {
-	int mismatch = 0;
-	unsigned char file_buf[1024];
-	fseek(xfile, offset + verify_cnt, SEEK_SET);
-
-	while (size) {
-		unsigned char mem_buf[64];
-		unsigned char *p = file_buf;
-		int cnt = addr & 0x3f;
-		int request = get_min(size, sizeof(file_buf));
-
-		if (cnt) {
-			cnt = 64 - cnt;
-			if (request > cnt)
-				request = cnt;
-		}
-
-		if (verify_cnt) {
-			p = verify_buffer;
-			cnt = get_min(request, verify_cnt);
-			verify_buffer += cnt;
-			verify_cnt -= cnt;
-		} else {
-			cnt = fread(p, 1, request, xfile);
-			if (cnt <= 0) {
-				printf("Unexpected end of file, request=0x%0x, size=0x%x, cnt=%i\n",
-						request, size, cnt);
-				return -1;
-			}
-		}
-
-		size -= cnt;
-
-		while (cnt) {
-			int ret;
-
-			request = get_min(cnt, sizeof(mem_buf));
+	int ret, mismatch = 0;
+	void *readbuf;
+	unsigned offset = 0, now;
 
-			ret = read_memory(h, p_id, addr, mem_buf, request);
-			if (ret < 0)
-				return ret;
+	readbuf = malloc(len);
+	if (!readbuf)
+		return -ENOMEM;
 
-			if (memcmp(p, mem_buf, request)) {
-				unsigned char * m = mem_buf;
-				if (!mismatch)
-					printf("!!!!mismatch\n");
-				mismatch++;
-
-				while (request) {
-					unsigned req = get_min(request, 32);
-					if (memcmp(p, m, req)) {
-						dump_long(p, req, offset);
-						dump_long(m, req, addr);
-						printf("\n");
-					}
-					p += req;
-					m+= req;
-					offset += req;
-					addr += req;
-					cnt -= req;
-					request -= req;
-				}
-				if (mismatch >= 5)
-					return -1;
-			}
-			p += request;
-			offset += request;
-			addr += request;
-			cnt -= request;
+	ret = read_memory(h, p_id, addr, readbuf, len);
+	if (ret < 0)
+		goto err;
+
+	while (len) {
+		now = get_min(len, 32);
+
+		if (memcmp(buf + offset, readbuf + offset, now)) {
+			printf("mismatch at offset 0x%08x. expected:\n", offset);
+			dump_long(buf + offset, now, addr + offset);
+			printf("read:\n");
+			dump_long(readbuf + offset, now, addr + offset);
+			ret = -EINVAL;
+			mismatch++;
+			if (mismatch > 4)
+				goto err;
 		}
+
+		len -= now;
+		offset += now;
 	}
 
-	return mismatch ? -1 : 0;
+err:
+	free(readbuf);
+
+	return ret;
 }
 
 static int is_header(struct usb_id *p_id, unsigned char *p)
@@ -995,8 +956,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
 }
 
 static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
-		unsigned char *p, int cnt, unsigned char *buf, unsigned buf_cnt,
-		unsigned dladdr, unsigned fsize, unsigned char type, FILE* xfile)
+		void *buf, unsigned len, unsigned dladdr, unsigned char type)
 {
 	static unsigned char dl_command[] = {
 		0x04,
@@ -1010,18 +970,19 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 	int last_trans, err;
 	int retry = 0;
 	unsigned transfer_size = 0;
-	int max = p_id->mach_id->max_transfer;
 	unsigned char tmp[64];
+	void *p;
+	int cnt;
 
 	dl_command[2] = (unsigned char)(dladdr >> 24);
 	dl_command[3] = (unsigned char)(dladdr >> 16);
 	dl_command[4] = (unsigned char)(dladdr >> 8);
 	dl_command[5] = (unsigned char)(dladdr);
 
-	dl_command[7] = (unsigned char)(fsize >> 24);
-	dl_command[8] = (unsigned char)(fsize >> 16);
-	dl_command[9] = (unsigned char)(fsize >> 8);
-	dl_command[10] = (unsigned char)(fsize);
+	dl_command[7] = (unsigned char)(len >> 24);
+	dl_command[8] = (unsigned char)(len >> 16);
+	dl_command[9] = (unsigned char)(len >> 8);
+	dl_command[10] = (unsigned char)(len);
 	dl_command[15] =  type;
 
 	for (;;) {
@@ -1045,61 +1006,23 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
 	}
 
-	while (1) {
-		int retry;
+	p = buf;
+	cnt = len;
 
-		if (cnt > (int)(fsize - transfer_size))
-			cnt = (fsize - transfer_size);
+	while (1) {
+		int now = get_min(cnt, p_id->mach_id->max_transfer);
 
-		if (cnt <= 0)
+		if (!now)
 			break;
 
-		retry = 0;
-
-		while (cnt) {
-			err = transfer(h, 2, p, get_min(cnt, max), &last_trans, p_id);
-			if (err) {
-				printf("out err=%i, last_trans=%i cnt=0x%x max=0x%x transfer_size=0x%X retry=%i\n",
-						err, last_trans, cnt, max, transfer_size, retry);
-				if (retry >= 10) {
-					printf("Giving up\n");
-					return err;
-				}
-				if (max >= 16)
-					max >>= 1;
-				else
-					max <<= 1;
-				usleep(10000);
-				retry++;
-				continue;
-			}
-			max = p_id->mach_id->max_transfer;
-			retry = 0;
-			if (cnt < last_trans) {
-				printf("error: last_trans=0x%x, attempted only=0%x\n", last_trans, cnt);
-				cnt = last_trans;
-			}
-			if (!last_trans) {
-				printf("Nothing last_trans, err=%i\n", err);
-				break;
-			}
-			p += last_trans;
-			cnt -= last_trans;
-			transfer_size += last_trans;
+		err = transfer(h, 2, p, now, &now, p_id);
+		if (err) {
+			printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
+			return err;
 		}
 
-		if (!last_trans)
-			break;
-
-		if (feof(xfile))
-			break;
-
-		cnt = fsize - transfer_size;
-		if (cnt <= 0)
-			break;
-
-		cnt = fread(buf, 1 , get_min(cnt, buf_cnt), xfile);
-		p = buf;
+		p += now;
+		cnt -= now;
 	}
 
 	if (p_id->mach_id->mode == MODE_HID) {
@@ -1131,10 +1054,9 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 	int cnt;
 	unsigned file_base;
 	int last_trans, err;
-#define BUF_SIZE (1024*16)
 	unsigned char *buf = NULL;
+	unsigned char *image;
 	unsigned char *verify_buffer = NULL;
-	unsigned verify_cnt;
 	unsigned char *p;
 	unsigned char tmp[64];
 	unsigned dladdr = 0;
@@ -1143,7 +1065,6 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 	unsigned header_addr = 0;
 
 	unsigned skip = 0;
-	unsigned transfer_size=0;
 	int retry = 0;
 
 	xfile = fopen(curr->filename, "rb" );
@@ -1152,23 +1073,26 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		return -5;
 	}
 
-	buf = malloc(BUF_SIZE);
-	if (!buf) {
-		printf("error, out of memory\n");
+	fsize = get_file_size(xfile);
+	if (fsize < 0x20) {
+		printf("error, file: %s is too small\n", curr->filename);
 		ret = -2;
 		goto cleanup;
 	}
 
-	fsize = get_file_size(xfile);
-
-	cnt = fread(buf, 1 , BUF_SIZE, xfile);
-
-	if (cnt < 0x20) {
-		printf("error, file: %s is too small\n", curr->filename);
+	buf = malloc(fsize);
+	if (!buf) {
+		printf("error, out of memory\n");
 		ret = -2;
 		goto cleanup;
 	}
 
+	cnt = fread(buf, 1 , fsize, xfile);
+	if (cnt < fsize) {
+		printf("error, cannot read %s\n", curr->filename);
+		return -1;
+	}
+
 	max_length = fsize;
 
 	ret = process_header(h, p_id, curr, buf, cnt,
@@ -1212,22 +1136,9 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	skip = dladdr - file_base;
 
-	if (skip > cnt) {
-		if (skip > fsize) {
-			printf("skip(0x%08x) > fsize(0x%08x) file_base=0x%08x, header_offset=0x%x\n",
-					skip, fsize, file_base, header_offset);
-			ret = -4;
-			goto cleanup;
-		}
-
-		fseek(xfile, skip, SEEK_SET);
-		cnt -= skip;
-		fsize -= skip;
-		skip = 0;
-		cnt = fread(buf, 1 , BUF_SIZE, xfile);
-	}
+	image = buf + skip;
 
-	p = &buf[skip];
+	p = image;
 	cnt -= skip;
 	fsize -= skip;
 
@@ -1235,13 +1146,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		fsize = max_length;
 
 	if (verify) {
-		/*
-		 * we need to save header for verification
-		 * because some of the file is changed
-		 * before download
-		 */
-		verify_buffer = malloc(cnt);
-		verify_cnt = cnt;
+		verify_buffer = malloc(64);
 
 		if (!verify_buffer) {
 			printf("error, out of memory\n");
@@ -1249,7 +1154,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 			goto cleanup;
 		}
 
-		memcpy(verify_buffer, p, cnt);
+		memcpy(verify_buffer, p, 64);
 
 		if ((type == FT_APP) && (p_id->mach_id->mode != MODE_HID)) {
 			type = FT_LOAD_ONLY;
@@ -1260,19 +1165,16 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 	printf("loading binary file(%s) to %08x, skip=0x%x, fsize=%u type=%d...\n",
 			curr->filename, dladdr, skip, fsize, type);
 
-	ret = load_file(h, p_id, p, cnt, buf, BUF_SIZE,
-			dladdr, fsize, type, xfile);
+	ret = load_file(h, p_id, image, fsize, dladdr, type);
 	if (ret < 0)
 		goto cleanup;
 
 	printf("binary file successfully loaded\n");
 
-	transfer_size = ret;
-
 	if (verify) {
 		printf("verifying file...\n");
 
-		ret = verify_memory(h, p_id, xfile, skip, dladdr, fsize, verify_buffer, verify_cnt);
+		ret = verify_memory(h, p_id, image, fsize, dladdr);
 		if (ret < 0) {
 			printf("verifying failed\n");
 			goto cleanup;
@@ -1281,11 +1183,13 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		printf("file successfully verified\n");
 
 		if (verify == 2) {
-			if (verify_cnt > 64)
-				verify_cnt = 64;
-			ret = load_file(h, p_id, verify_buffer, verify_cnt,
-					buf, BUF_SIZE, dladdr, verify_cnt,
-					FT_APP, xfile);
+			/*
+			 * In bulk mode we do not have an explicit jump command,
+			 * so we load part of the image again with type FT_APP
+			 * this time.
+			 */
+			ret = load_file(h, p_id, verify_buffer, 64,
+					dladdr, FT_APP);
 			if (ret < 0)
 				goto cleanup;
 
@@ -1324,7 +1228,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
 	}
 
-	ret = (fsize == transfer_size) ? 0 : -16;
+	ret = 0;
 cleanup:
 	fclose(xfile);
 	free(verify_buffer);
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 08/23] scripts: imx-usb-loader: Move load_file up
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (6 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 07/23] scripts: imx-usb-loader: fully read images into memory Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 09/23] scripts: imx: Consolidate flash headers in imx tools Sascha Hauer
                   ` (14 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

To avoid forward declaration in a later patch.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 172 +++++++++++++++++++++----------------------
 1 file changed, 86 insertions(+), 86 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index fa2ce47..4db39d6 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -582,6 +582,92 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 	return err;
 }
 
+static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
+		void *buf, unsigned len, unsigned dladdr, unsigned char type)
+{
+	static unsigned char dl_command[] = {
+		0x04,
+		0x04,
+		V(0),		/* address */
+		0x00,		/* format */
+		V(0x00000020),	/* data count */
+		V(0),		/* data */
+		0xaa,		/* type */
+	};
+	int last_trans, err;
+	int retry = 0;
+	unsigned transfer_size = 0;
+	unsigned char tmp[64];
+	void *p;
+	int cnt;
+
+	dl_command[2] = (unsigned char)(dladdr >> 24);
+	dl_command[3] = (unsigned char)(dladdr >> 16);
+	dl_command[4] = (unsigned char)(dladdr >> 8);
+	dl_command[5] = (unsigned char)(dladdr);
+
+	dl_command[7] = (unsigned char)(len >> 24);
+	dl_command[8] = (unsigned char)(len >> 16);
+	dl_command[9] = (unsigned char)(len >> 8);
+	dl_command[10] = (unsigned char)(len);
+	dl_command[15] =  type;
+
+	for (;;) {
+		err = transfer(h, 1, dl_command, 16, &last_trans, p_id);
+		if (!err)
+			break;
+
+		printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
+
+		if (retry > 5)
+			return -4;
+		retry++;
+	}
+
+	retry = 0;
+
+	if (p_id->mach_id->mode == MODE_BULK) {
+		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+		if (err)
+			printf("in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
+					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
+	}
+
+	p = buf;
+	cnt = len;
+
+	while (1) {
+		int now = get_min(cnt, p_id->mach_id->max_transfer);
+
+		if (!now)
+			break;
+
+		err = transfer(h, 2, p, now, &now, p_id);
+		if (err) {
+			printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
+			return err;
+		}
+
+		p += now;
+		cnt -= now;
+	}
+
+	if (p_id->mach_id->mode == MODE_HID) {
+		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+		if (err)
+			printf("3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
+					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
+		err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id);
+		if (err)
+			printf("4 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
+					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
+	} else {
+		do_status(h, p_id);
+	}
+
+	return transfer_size;
+}
+
 static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_id,
 		struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt)
 {
@@ -955,92 +1041,6 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
 	return -ENODEV;
 }
 
-static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
-		void *buf, unsigned len, unsigned dladdr, unsigned char type)
-{
-	static unsigned char dl_command[] = {
-		0x04,
-		0x04,
-		V(0),		/* address */
-		0x00,		/* format */
-		V(0x00000020),	/* data count */
-		V(0),		/* data */
-		0xaa,		/* type */
-	};
-	int last_trans, err;
-	int retry = 0;
-	unsigned transfer_size = 0;
-	unsigned char tmp[64];
-	void *p;
-	int cnt;
-
-	dl_command[2] = (unsigned char)(dladdr >> 24);
-	dl_command[3] = (unsigned char)(dladdr >> 16);
-	dl_command[4] = (unsigned char)(dladdr >> 8);
-	dl_command[5] = (unsigned char)(dladdr);
-
-	dl_command[7] = (unsigned char)(len >> 24);
-	dl_command[8] = (unsigned char)(len >> 16);
-	dl_command[9] = (unsigned char)(len >> 8);
-	dl_command[10] = (unsigned char)(len);
-	dl_command[15] =  type;
-
-	for (;;) {
-		err = transfer(h, 1, dl_command, 16, &last_trans, p_id);
-		if (!err)
-			break;
-
-		printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
-
-		if (retry > 5)
-			return -4;
-		retry++;
-	}
-
-	retry = 0;
-
-	if (p_id->mach_id->mode == MODE_BULK) {
-		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
-		if (err)
-			printf("in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
-					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
-	}
-
-	p = buf;
-	cnt = len;
-
-	while (1) {
-		int now = get_min(cnt, p_id->mach_id->max_transfer);
-
-		if (!now)
-			break;
-
-		err = transfer(h, 2, p, now, &now, p_id);
-		if (err) {
-			printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
-			return err;
-		}
-
-		p += now;
-		cnt -= now;
-	}
-
-	if (p_id->mach_id->mode == MODE_HID) {
-		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
-		if (err)
-			printf("3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
-					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
-		err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id);
-		if (err)
-			printf("4 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
-					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
-	} else {
-		do_status(h, p_id);
-	}
-
-	return transfer_size;
-}
-
 static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		struct usb_work *curr, int verify)
 {
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 09/23] scripts: imx: Consolidate flash headers in imx tools
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (7 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 08/23] scripts: imx-usb-loader: Move load_file up Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 10/23] scripts: imx-image: Add context struct to config parsers Sascha Hauer
                   ` (13 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Both imx-image and imx-usb-loader use their own variants of the
i.MX flash header definitions. Consolidate them to avoid code
duplication.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c      | 48 +-----------------------
 scripts/imx/imx-usb-loader.c | 88 ++++++++++++++------------------------------
 scripts/imx/imx.h            | 52 ++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 106 deletions(-)
 create mode 100644 scripts/imx/imx.h

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index a85833b..4b6359a 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -27,6 +27,8 @@
 #include <fcntl.h>
 #include <endian.h>
 
+#include "imx.h"
+
 #include <include/filetype.h>
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
@@ -51,20 +53,8 @@ static int prepare_sign;
  * i.MX flash header v1 handling. Found on i.MX35 and i.MX51
  * ============================================================================
  */
-struct imx_flash_header {
-	uint32_t app_code_jump_vector;
-	uint32_t app_code_barker;
-	uint32_t app_code_csf;
-	uint32_t dcd_ptr_ptr;
-	uint32_t super_root_key;
-	uint32_t dcd;
-	uint32_t app_dest;
-	uint32_t dcd_barker;
-	uint32_t dcd_block_len;
-} __attribute__((packed));
 
 #define FLASH_HEADER_OFFSET 0x400
-#define DCD_BARKER       0xb17219e9
 
 static uint32_t bb_header[] = {
 	0xea0003fe,	/* b 0x1000 */
@@ -144,40 +134,6 @@ static int write_mem_v1(uint32_t addr, uint32_t val, int width)
  * ============================================================================
  */
 
-struct imx_boot_data {
-	uint32_t start;
-	uint32_t size;
-	uint32_t plugin;
-} __attribute__((packed));
-
-#define TAG_IVT_HEADER	0xd1
-#define IVT_VERSION	0x40
-#define TAG_DCD_HEADER	0xd2
-#define DCD_VERSION	0x40
-#define TAG_WRITE	0xcc
-#define TAG_CHECK	0xcf
-
-struct imx_ivt_header {
-	uint8_t tag;
-	uint16_t length;
-	uint8_t version;
-} __attribute__((packed));
-
-struct imx_flash_header_v2 {
-	struct imx_ivt_header header;
-
-	uint32_t entry;
-	uint32_t reserved1;
-	uint32_t dcd_ptr;
-	uint32_t boot_data_ptr;
-	uint32_t self;
-	uint32_t csf;
-	uint32_t reserved2;
-
-	struct imx_boot_data boot_data;
-	struct imx_ivt_header dcd_header;
-} __attribute__((packed));
-
 static int add_header_v2(void *buf, int offset, uint32_t loadaddr, uint32_t imagesize)
 {
 	struct imx_flash_header_v2 *hdr;
diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 4db39d6..2bee709 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <libusb.h>
 #include <getopt.h>
+#include "imx.h"
 
 #define get_min(a, b) (((a) < (b)) ? (a) : (b))
 
@@ -401,39 +402,6 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id)
 	return err;
 }
 
-struct boot_data {
-	uint32_t dest;
-	uint32_t image_len;
-	uint32_t plugin;
-};
-
-struct imx_flash_header_v2 {
-#define IVT_BARKER 0x402000d1
-	uint32_t barker;
-	uint32_t start_addr;
-	uint32_t reserv1;
-	uint32_t dcd_ptr;
-	uint32_t boot_data_ptr;	/* struct boot_data * */
-	uint32_t self_ptr;	/* struct imx_flash_header_v2 *, this - boot_data.start = offset linked at */
-	uint32_t app_code_csf;
-	uint32_t reserv2;
-};
-
-/*
- * MX51 header type
- */
-struct imx_flash_header_v1 {
-	uint32_t app_start_addr;
-#define APP_BARKER	0xb1
-#define DCD_BARKER	0xb17219e9
-	uint32_t app_barker;
-	uint32_t csf_ptr;
-	uint32_t dcd_ptr_ptr;
-	uint32_t srk_ptr;
-	uint32_t dcd_ptr;
-	uint32_t app_dest_ptr;
-};
-
 #define V(a) (((a) >> 24) & 0xff), (((a) >> 16) & 0xff), (((a) >> 8) & 0xff), ((a) & 0xff)
 
 static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
@@ -673,13 +641,13 @@ static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_
 {
 	unsigned char *dcd_end;
 	unsigned m_length;
-#define cvt_dest_to_src		(((unsigned char *)hdr) - hdr->self_ptr)
+#define cvt_dest_to_src		(((unsigned char *)hdr) - hdr->self)
 	unsigned char* dcd;
 	unsigned char* file_end = file_start + cnt;
 	int err = 0;
 
 	if (!hdr->dcd_ptr) {
-		printf("No dcd table, barker=%x\n", hdr->barker);
+		printf("No dcd table in this ivt\n");
 		return 0; /* nothing to do */
 	}
 
@@ -739,27 +707,27 @@ static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_
 	return err;
 }
 
-static int get_dcd_range_old(struct imx_flash_header_v1 *hdr,
+static int get_dcd_range_old(struct imx_flash_header *hdr,
 		unsigned char *file_start, unsigned cnt,
 		unsigned char **pstart, unsigned char **pend)
 {
 	unsigned char *dcd_end;
 	unsigned m_length;
-#define cvt_dest_to_src_old		(((unsigned char *)&hdr->dcd_ptr) - hdr->dcd_ptr_ptr)
+#define cvt_dest_to_src_old		(((unsigned char *)&hdr->dcd) - hdr->dcd_ptr_ptr)
 	unsigned char* dcd;
 	unsigned val;
 	unsigned char* file_end = file_start + cnt;
 
-	if (!hdr->dcd_ptr) {
-		printf("No dcd table, barker=%x\n", hdr->app_barker);
-		*pstart = *pend = ((unsigned char *)hdr) + sizeof(struct imx_flash_header_v1);
+	if (!hdr->dcd) {
+		printf("No dcd table, barker=%x\n", hdr->app_code_barker);
+		*pstart = *pend = ((unsigned char *)hdr) + sizeof(struct imx_flash_header);
 		return 0; /* nothing to do */
 	}
 
-	dcd = hdr->dcd_ptr + cvt_dest_to_src_old;
+	dcd = hdr->dcd + cvt_dest_to_src_old;
 
 	if ((dcd < file_start) || ((dcd + 8) > file_end)) {
-		printf("bad dcd_ptr %08x\n", hdr->dcd_ptr);
+		printf("bad dcd_ptr %08x\n", hdr->dcd);
 		return -1;
 	}
 
@@ -787,7 +755,7 @@ static int get_dcd_range_old(struct imx_flash_header_v1 *hdr,
 }
 
 static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_id,
-		struct imx_flash_header_v1 *hdr, unsigned char *file_start, unsigned cnt)
+		struct imx_flash_header *hdr, unsigned char *file_start, unsigned cnt)
 {
 	unsigned val;
 	unsigned char *dcd_end;
@@ -873,16 +841,16 @@ err:
 
 static int is_header(struct usb_id *p_id, unsigned char *p)
 {
-	struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p;
+	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 
 	switch (p_id->mach_id->header_type) {
 	case HDR_MX51:
-		if (ohdr->app_barker == 0xb1)
+		if (ohdr->app_code_barker == 0xb1)
 			return 1;
 		break;
 	case HDR_MX53:
-		if (hdr->barker == IVT_BARKER)
+		if (hdr->header.tag == TAG_IVT_HEADER && hdr->header.version == IVT_VERSION)
 			return 1;
 	}
 
@@ -892,14 +860,14 @@ static int is_header(struct usb_id *p_id, unsigned char *p)
 static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsigned char *p,
 		unsigned char *file_start, unsigned cnt)
 {
-	struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p;
+	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 	int ret = 0;
 
 	switch (p_id->mach_id->header_type) {
 	case HDR_MX51:
 		ret = write_dcd_table_old(h, p_id, ohdr, file_start, cnt);
-		ohdr->dcd_ptr = 0;
+		ohdr->dcd = 0;
 
 		break;
 	case HDR_MX53:
@@ -915,13 +883,13 @@ static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsi
 static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id,
 		unsigned char *p, unsigned char *file_start, unsigned cnt)
 {
-	struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p;
+	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 
 	switch (p_id->mach_id->header_type) {
 	case HDR_MX51:
-		printf("clear dcd_ptr=0x%08x\n", ohdr->dcd_ptr);
-		ohdr->dcd_ptr = 0;
+		printf("clear dcd_ptr=0x%08x\n", ohdr->dcd);
+		ohdr->dcd = 0;
 		break;
 	case HDR_MX53:
 		printf("clear dcd_ptr=0x%08x\n", hdr->dcd_ptr);
@@ -939,13 +907,13 @@ static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *fi
 	switch (p_id->mach_id->header_type) {
 	case HDR_MX51:
 	{
-		struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p;
+		struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 		unsigned char *dcd_end;
 		unsigned char* dcd;
 		int err = get_dcd_range_old(ohdr, file_start, cnt, &dcd, &dcd_end);
 
-		*dladdr = ohdr->app_dest_ptr;
-		*header_addr = ohdr->dcd_ptr_ptr - offsetof(struct imx_flash_header_v1, dcd_ptr);
+		*dladdr = ohdr->app_dest;
+		*header_addr = ohdr->dcd_ptr_ptr - offsetof(struct imx_flash_header, dcd);
 		*plugin = 0;
 		if (err >= 0)
 			*max_length = dcd_end[0] | (dcd_end[1] << 8) | (dcd_end[2] << 16) | (dcd_end[3] << 24);
@@ -957,18 +925,18 @@ static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *fi
 		unsigned char *bd;
 		struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 
-		*dladdr = hdr->self_ptr;
-		*header_addr = hdr->self_ptr;
+		*dladdr = hdr->self;
+		*header_addr = hdr->self;
 		bd = hdr->boot_data_ptr + cvt_dest_to_src;
 		if ((bd < file_start) || ((bd + 4) > file_end)) {
 			printf("bad boot_data_ptr %08x\n", hdr->boot_data_ptr);
 			return -1;
 		}
 
-		*dladdr = ((struct boot_data *)bd)->dest;
-		*max_length = ((struct boot_data *)bd)->image_len;
-		*plugin = ((struct boot_data *)bd)->plugin;
-		((struct boot_data *)bd)->plugin = 0;
+		*dladdr = ((struct imx_boot_data *)bd)->start;
+		*max_length = ((struct imx_boot_data *)bd)->size;
+		*plugin = ((struct imx_boot_data *)bd)->plugin;
+		((struct imx_boot_data *)bd)->plugin = 0;
 
 		hdr->boot_data_ptr = 0;
 
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
new file mode 100644
index 0000000..e986545
--- /dev/null
+++ b/scripts/imx/imx.h
@@ -0,0 +1,52 @@
+/*
+ * ============================================================================
+ * i.MX flash header v1 handling. Found on i.MX35 and i.MX51
+ * ============================================================================
+ */
+#define DCD_BARKER       0xb17219e9
+
+struct imx_flash_header {
+	uint32_t app_code_jump_vector;
+	uint32_t app_code_barker;
+	uint32_t app_code_csf;
+	uint32_t dcd_ptr_ptr;
+	uint32_t super_root_key;
+	uint32_t dcd;
+	uint32_t app_dest;
+	uint32_t dcd_barker;
+	uint32_t dcd_block_len;
+} __attribute__((packed));
+
+struct imx_boot_data {
+	uint32_t start;
+	uint32_t size;
+	uint32_t plugin;
+} __attribute__((packed));
+
+#define TAG_IVT_HEADER	0xd1
+#define IVT_VERSION	0x40
+#define TAG_DCD_HEADER	0xd2
+#define DCD_VERSION	0x40
+#define TAG_WRITE	0xcc
+#define TAG_CHECK	0xcf
+
+struct imx_ivt_header {
+	uint8_t tag;
+	uint16_t length;
+	uint8_t version;
+} __attribute__((packed));
+
+struct imx_flash_header_v2 {
+	struct imx_ivt_header header;
+
+	uint32_t entry;
+	uint32_t reserved1;
+	uint32_t dcd_ptr;
+	uint32_t boot_data_ptr;
+	uint32_t self;
+	uint32_t csf;
+	uint32_t reserved2;
+
+	struct imx_boot_data boot_data;
+	struct imx_ivt_header dcd_header;
+} __attribute__((packed));
\ No newline at end of file
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 10/23] scripts: imx-image: Add context struct to config parsers
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (8 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 09/23] scripts: imx: Consolidate flash headers in imx tools Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 11/23] scripts: imx-image: move write_mem to context data Sascha Hauer
                   ` (12 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

This adds a context struct to the config parser. This is a first
step to make the config parser usable from both imx-image and
imx-usb-loader which will be needed later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 66 +++++++++++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 30 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 4b6359a..707852b 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -39,12 +39,15 @@
 #define HEADER_LEN 0x1000	/* length of the blank area + IVT + DCD */
 #define CSF_LEN 0x2000		/* length of the CSF (needed for HAB) */
 
-static uint32_t image_load_addr;
-static uint32_t image_dcd_offset = 0xffffffff;
+struct config_data {
+	uint32_t image_load_addr;
+	uint32_t image_dcd_offset;
+	int header_version;
+	int cpu_type;
+};
+
 static uint32_t dcdtable[MAX_DCD];
 static int curdcd;
-static int header_version;
-static int cpu_type;
 static int add_barebox_header;
 static int prepare_sign;
 
@@ -232,7 +235,7 @@ static int parse_line(char *line, char *argv[])
 
 struct command {
 	const char *name;
-	int (*parse)(int argc, char *argv[]);
+	int (*parse)(struct config_data *data, int argc, char *argv[]);
 };
 
 static uint32_t last_write_cmd;
@@ -299,7 +302,7 @@ static void do_cmd_check_usage(void)
 			"while_any_bit_set:    while ((*addr & mask) != 0)\n");
 }
 
-static int do_cmd_check(int argc, char *argv[])
+static int do_cmd_check(struct config_data *data, int argc, char *argv[])
 {
 	uint32_t addr, mask, cmd;
 	int i, width;
@@ -351,7 +354,7 @@ static int do_cmd_check(int argc, char *argv[])
 	return 0;
 }
 
-static int do_cmd_write_mem(int argc, char *argv[])
+static int do_cmd_write_mem(struct config_data *data, int argc, char *argv[])
 {
 	uint32_t addr, val, width;
 	char *end;
@@ -391,7 +394,7 @@ static int do_cmd_write_mem(int argc, char *argv[])
 		return -EINVAL;
 	};
 
-	switch (header_version) {
+	switch (data->header_version) {
 	case 1:
 		return write_mem_v1(addr, val, width);
 	case 2:
@@ -401,22 +404,22 @@ static int do_cmd_write_mem(int argc, char *argv[])
 	}
 }
 
-static int do_loadaddr(int argc, char *argv[])
+static int do_loadaddr(struct config_data *data, int argc, char *argv[])
 {
 	if (argc < 2)
 		return -EINVAL;
 
-	image_load_addr = strtoul(argv[1], NULL, 0);
+	data->image_load_addr = strtoul(argv[1], NULL, 0);
 
 	return 0;
 }
 
-static int do_dcd_offset(int argc, char *argv[])
+static int do_dcd_offset(struct config_data *data, int argc, char *argv[])
 {
 	if (argc < 2)
 		return -EINVAL;
 
-	image_dcd_offset = strtoul(argv[1], NULL, 0);
+	data->image_dcd_offset = strtoul(argv[1], NULL, 0);
 
 	return 0;
 }
@@ -435,7 +438,7 @@ static struct soc_type socs[] = {
 	{ .name = "imx6", .header_version = 2, .cpu_type = 6 },
 };
 
-static int do_soc(int argc, char *argv[])
+static int do_soc(struct config_data *data, int argc, char *argv[])
 {
 	char *soc;
 	int i;
@@ -447,8 +450,8 @@ static int do_soc(int argc, char *argv[])
 
 	for (i = 0; i < ARRAY_SIZE(socs); i++) {
 		if (!strcmp(socs[i].name, soc)) {
-			header_version = socs[i].header_version;
-			cpu_type = socs[i].cpu_type;
+			data->header_version = socs[i].header_version;
+			data->cpu_type = socs[i].cpu_type;
 			return 0;
 		}
 	}
@@ -480,7 +483,7 @@ struct command cmds[] = {
 	},
 };
 
-static char *readcmd(FILE *f)
+static char *readcmd(struct config_data *data, FILE *f)
 {
 	static char *buf;
 	char *str;
@@ -509,7 +512,7 @@ static char *readcmd(FILE *f)
 	}
 }
 
-static int parse_config(const char *filename)
+static int parse_config(struct config_data *data, const char *filename)
 {
 	FILE *f;
 	int lineno = 0;
@@ -524,7 +527,7 @@ static int parse_config(const char *filename)
 	}
 
 	while (1) {
-		line = readcmd(f);
+		line = readcmd(data, f);
 		if (!line)
 			break;
 
@@ -542,7 +545,7 @@ static int parse_config(const char *filename)
 
 		for (i = 0; i < ARRAY_SIZE(cmds); i++) {
 			if (!strcmp(cmds[i].name, argv[0])) {
-				ret = cmds[i].parse(nargs, argv);
+				ret = cmds[i].parse(data, nargs, argv);
 				if (ret) {
 					fprintf(stderr, "error in line %d: %s\n",
 							lineno, strerror(-ret));
@@ -628,6 +631,9 @@ int main(int argc, char *argv[])
 	int infd, outfd;
 	int dcd_only = 0;
 	int now = 0;
+	struct config_data data = {
+		.image_dcd_offset = 0xffffffff,
+	};
 
 	while ((opt = getopt(argc, argv, "c:hf:o:bdp")) != -1) {
 		switch (opt) {
@@ -681,7 +687,7 @@ int main(int argc, char *argv[])
 		image_size = s.st_size;
 	}
 
-	ret = parse_config(configfile);
+	ret = parse_config(&data, configfile);
 	if (ret)
 		exit(1);
 
@@ -689,18 +695,18 @@ int main(int argc, char *argv[])
 	if (!buf)
 		exit(1);
 
-	if (image_dcd_offset == 0xffffffff) {
+	if (data.image_dcd_offset == 0xffffffff) {
 		fprintf(stderr, "no dcd offset given ('dcdofs'). Defaulting to 0x%08x\n",
 			FLASH_HEADER_OFFSET);
-		image_dcd_offset = FLASH_HEADER_OFFSET;
+		data.image_dcd_offset = FLASH_HEADER_OFFSET;
 	}
 
-	if (!header_version) {
+	if (!data.header_version) {
 		fprintf(stderr, "no SoC given. (missing 'soc' in config)\n");
 		exit(1);
 	}
 
-	if (header_version == 2)
+	if (data.header_version == 2)
 		check_last_dcd(0);
 
 	if (dcd_only) {
@@ -720,19 +726,19 @@ int main(int argc, char *argv[])
 	 */
 	load_size = roundup(image_size + HEADER_LEN, 0x1000);
 
-	if (cpu_type == 35)
+	if (data.cpu_type == 35)
 		load_size += HEADER_LEN;
 
-	switch (header_version) {
+	switch (data.header_version) {
 	case 1:
-		add_header_v1(buf, image_dcd_offset, image_load_addr, load_size);
+		add_header_v1(buf, data.image_dcd_offset, data.image_load_addr, load_size);
 		break;
 	case 2:
-		add_header_v2(buf, image_dcd_offset, image_load_addr, load_size);
+		add_header_v2(buf, data.image_dcd_offset, data.image_load_addr, load_size);
 		break;
 	default:
 		fprintf(stderr, "Congratulations! You're welcome to implement header version %d\n",
-				header_version);
+				data.header_version);
 		exit(1);
 	}
 
@@ -766,7 +772,7 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	if (cpu_type == 35) {
+	if (data.cpu_type == 35) {
 		ret = xwrite(outfd, buf, HEADER_LEN);
 		if (ret < 0) {
 			perror("write");
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 11/23] scripts: imx-image: move write_mem to context data
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (9 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 10/23] scripts: imx-image: Add context struct to config parsers Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 12/23] scripts: imx-image: move check " Sascha Hauer
                   ` (11 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Move the config parsers write_mem function to context data to make
it possible to pass in a different version of this function later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 707852b..56c6464 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -44,6 +44,7 @@ struct config_data {
 	uint32_t image_dcd_offset;
 	int header_version;
 	int cpu_type;
+	int (*write_mem)(struct config_data *data, uint32_t addr, uint32_t val, int width);
 };
 
 static uint32_t dcdtable[MAX_DCD];
@@ -394,14 +395,7 @@ static int do_cmd_write_mem(struct config_data *data, int argc, char *argv[])
 		return -EINVAL;
 	};
 
-	switch (data->header_version) {
-	case 1:
-		return write_mem_v1(addr, val, width);
-	case 2:
-		return write_mem_v2(addr, val, width);
-	default:
-		return -EINVAL;
-	}
+	return data->write_mem(data, addr, val, width);
 }
 
 static int do_loadaddr(struct config_data *data, int argc, char *argv[])
@@ -618,6 +612,18 @@ static int write_dcd(const char *outfile)
 	return 0;
 }
 
+static int write_mem(struct config_data *data, uint32_t addr, uint32_t val, int width)
+{
+	switch (data->header_version) {
+	case 1:
+		return write_mem_v1(addr, val, width);
+	case 2:
+		return write_mem_v2(addr, val, width);
+	default:
+		return -EINVAL;
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	int opt, ret;
@@ -633,6 +639,7 @@ int main(int argc, char *argv[])
 	int now = 0;
 	struct config_data data = {
 		.image_dcd_offset = 0xffffffff,
+		.write_mem = write_mem,
 	};
 
 	while ((opt = getopt(argc, argv, "c:hf:o:bdp")) != -1) {
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 12/23] scripts: imx-image: move check to context data
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (10 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 11/23] scripts: imx-image: move write_mem to context data Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 13/23] scripts: imx: move macro definitions to common header file Sascha Hauer
                   ` (10 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Move the config parsers check function to context data to make
it possible to pass in a different version of this function later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 56c6464..04b650e 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -44,6 +44,7 @@ struct config_data {
 	uint32_t image_dcd_offset;
 	int header_version;
 	int cpu_type;
+	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
 	int (*write_mem)(struct config_data *data, uint32_t addr, uint32_t val, int width);
 };
 
@@ -314,6 +315,9 @@ static int do_cmd_check(struct config_data *data, int argc, char *argv[])
 		return -EINVAL;
 	}
 
+	if (!data->check)
+		return -ENOSYS;
+
 	width = strtoul(argv[1], NULL, 0) >> 3;
 	scmd = argv[2];
 	addr = strtoul(argv[3], NULL, 0);
@@ -346,13 +350,7 @@ static int do_cmd_check(struct config_data *data, int argc, char *argv[])
 
 	cmd = (TAG_CHECK << 24) | (i << 3) | width | ((sizeof(uint32_t) * 3) << 8);
 
-	check_last_dcd(cmd);
-
-	dcdtable[curdcd++] = htobe32(cmd);
-	dcdtable[curdcd++] = htobe32(addr);
-	dcdtable[curdcd++] = htobe32(mask);
-
-	return 0;
+	return data->check(data, cmd, addr, mask);
 }
 
 static int do_cmd_write_mem(struct config_data *data, int argc, char *argv[])
@@ -612,6 +610,22 @@ static int write_dcd(const char *outfile)
 	return 0;
 }
 
+static int check(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask)
+{
+	if (curdcd > MAX_DCD - 3) {
+		fprintf(stderr, "At maximum %d dcd entried are allowed\n", MAX_DCD);
+		return -ENOMEM;
+	}
+
+	check_last_dcd(cmd);
+
+	dcdtable[curdcd++] = htobe32(cmd);
+	dcdtable[curdcd++] = htobe32(addr);
+	dcdtable[curdcd++] = htobe32(mask);
+
+	return 0;
+}
+
 static int write_mem(struct config_data *data, uint32_t addr, uint32_t val, int width)
 {
 	switch (data->header_version) {
@@ -640,6 +654,7 @@ int main(int argc, char *argv[])
 	struct config_data data = {
 		.image_dcd_offset = 0xffffffff,
 		.write_mem = write_mem,
+		.check = check,
 	};
 
 	while ((opt = getopt(argc, argv, "c:hf:o:bdp")) != -1) {
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 13/23] scripts: imx: move macro definitions to common header file
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (11 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 12/23] scripts: imx-image: move check " Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 18:04   ` Sam Ravnborg
  2016-01-29 10:43 ` [PATCH 14/23] scripts: imx: move config file parser to separate file Sascha Hauer
                   ` (9 subsequent siblings)
  22 siblings, 1 reply; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

ARRAY_SIZE and offsetof are used by both imx-usb-loader and imx-image.
Move their definitions to the common header file.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c      | 2 --
 scripts/imx/imx-usb-loader.c | 5 -----
 scripts/imx/imx.h            | 5 +++++
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 04b650e..03afcb6 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -31,8 +31,6 @@
 
 #include <include/filetype.h>
 
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
-#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 
 #define MAX_DCD 1024
diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 2bee709..5a84721 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -59,11 +59,6 @@ struct mach_id {
 	unsigned short max_transfer;
 };
 
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
-#endif
-
 struct usb_work {
 	char filename[256];
 	unsigned char dcd;
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index e986545..1d23e11 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -1,3 +1,8 @@
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
+#endif
+
 /*
  * ============================================================================
  * i.MX flash header v1 handling. Found on i.MX35 and i.MX51
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 14/23] scripts: imx: move config file parser to separate file
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (12 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 13/23] scripts: imx: move macro definitions to common header file Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 15/23] scripts: imx: make libusb variables global Sascha Hauer
                   ` (8 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

To make the config parser usable by imx-usb-loader also move
it to a separate file.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/Makefile    |   4 +-
 scripts/imx/imx-image.c | 326 ----------------------------------------------
 scripts/imx/imx.c       | 336 ++++++++++++++++++++++++++++++++++++++++++++++++
 scripts/imx/imx.h       |  13 +-
 4 files changed, 350 insertions(+), 329 deletions(-)
 create mode 100644 scripts/imx/imx.c

diff --git a/scripts/imx/Makefile b/scripts/imx/Makefile
index ee0acc1..6883659 100644
--- a/scripts/imx/Makefile
+++ b/scripts/imx/Makefile
@@ -8,5 +8,5 @@ HOSTLOADLIBES_imx-usb-loader  = `pkg-config --libs libusb-1.0`
 
 HOSTCFLAGS_imx-image.o = -I$(srctree)
 
-imx-usb-loader-objs := imx-usb-loader.o
-imx-image-objs := imx-image.o
+imx-usb-loader-objs := imx-usb-loader.o imx.o
+imx-image-objs := imx-image.o imx.o
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 03afcb6..d6ed120 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -37,15 +37,6 @@
 #define HEADER_LEN 0x1000	/* length of the blank area + IVT + DCD */
 #define CSF_LEN 0x2000		/* length of the CSF (needed for HAB) */
 
-struct config_data {
-	uint32_t image_load_addr;
-	uint32_t image_dcd_offset;
-	int header_version;
-	int cpu_type;
-	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
-	int (*write_mem)(struct config_data *data, uint32_t addr, uint32_t val, int width);
-};
-
 static uint32_t dcdtable[MAX_DCD];
 static int curdcd;
 static int add_barebox_header;
@@ -194,50 +185,6 @@ static void usage(const char *prgname)
 	exit(1);
 }
 
-#define MAXARGS 5
-
-static int parse_line(char *line, char *argv[])
-{
-	int nargs = 0;
-
-	while (nargs < MAXARGS) {
-
-		/* skip any white space */
-		while ((*line == ' ') || (*line == '\t'))
-			++line;
-
-		if (*line == '\0')	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-
-		if (*line == '\0') {	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-			return nargs;
-		}
-
-		argv[nargs++] = line;	/* begin of argument string	*/
-
-		/* find end of string */
-		while (*line && (*line != ' ') && (*line != '\t'))
-			++line;
-
-		if (*line == '\0') {	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-			return nargs;
-		}
-
-		*line++ = '\0';		/* terminate current arg	 */
-	}
-
-	printf("** Too many args (max. %d) **\n", MAXARGS);
-
-	return nargs;
-}
-
-struct command {
-	const char *name;
-	int (*parse)(struct config_data *data, int argc, char *argv[]);
-};
-
 static uint32_t last_write_cmd;
 static int last_cmd_len;
 static uint32_t *last_dcd;
@@ -283,279 +230,6 @@ static int write_mem_v2(uint32_t addr, uint32_t val, int width)
 	return 0;
 }
 
-static const char *check_cmds[] = {
-	"while_all_bits_clear",		/* while ((*address & mask) == 0); */
-	"while_all_bits_set"	,	/* while ((*address & mask) == mask); */
-	"while_any_bit_clear",		/* while ((*address & mask) != mask); */
-	"while_any_bit_set",		/* while ((*address & mask) != 0); */
-};
-
-static void do_cmd_check_usage(void)
-{
-	fprintf(stderr,
-			"usage: check <width> <cmd> <addr> <mask>\n"
-			"<width> access width in bytes [1|2|4]\n"
-			"with <cmd> one of:\n"
-			"while_all_bits_clear: while ((*addr & mask) == 0)\n"
-			"while_all_bits_set:   while ((*addr & mask) == mask)\n"
-			"while_any_bit_clear:  while ((*addr & mask) != mask)\n"
-			"while_any_bit_set:    while ((*addr & mask) != 0)\n");
-}
-
-static int do_cmd_check(struct config_data *data, int argc, char *argv[])
-{
-	uint32_t addr, mask, cmd;
-	int i, width;
-	const char *scmd;
-
-	if (argc < 5) {
-		do_cmd_check_usage();
-		return -EINVAL;
-	}
-
-	if (!data->check)
-		return -ENOSYS;
-
-	width = strtoul(argv[1], NULL, 0) >> 3;
-	scmd = argv[2];
-	addr = strtoul(argv[3], NULL, 0);
-	mask = strtoul(argv[4], NULL, 0);
-
-	switch (width) {
-	case 1:
-	case 2:
-	case 4:
-		break;
-	default:
-		fprintf(stderr, "illegal width %d\n", width);
-		return -EINVAL;
-	};
-
-	if (curdcd > MAX_DCD - 3) {
-		fprintf(stderr, "At maximum %d dcd entried are allowed\n", MAX_DCD);
-		return -ENOMEM;
-	}
-
-	for (i = 0; i < ARRAY_SIZE(check_cmds); i++) {
-		if (!strcmp(scmd, check_cmds[i]))
-			break;
-	}
-
-	if (i == ARRAY_SIZE(check_cmds)) {
-		do_cmd_check_usage();
-		return -EINVAL;
-	}
-
-	cmd = (TAG_CHECK << 24) | (i << 3) | width | ((sizeof(uint32_t) * 3) << 8);
-
-	return data->check(data, cmd, addr, mask);
-}
-
-static int do_cmd_write_mem(struct config_data *data, int argc, char *argv[])
-{
-	uint32_t addr, val, width;
-	char *end;
-
-	if (argc != 4) {
-		fprintf(stderr, "usage: wm [8|16|32] <addr> <val>\n");
-		return -EINVAL;
-	}
-
-	width = strtoul(argv[1], &end, 0);
-	if (*end != '\0') {
-		fprintf(stderr, "illegal width token \"%s\"\n", argv[1]);
-		return -EINVAL;
-	}
-
-	addr = strtoul(argv[2], &end, 0);
-	if (*end != '\0') {
-		fprintf(stderr, "illegal address token \"%s\"\n", argv[2]);
-		return -EINVAL;
-	}
-
-	val = strtoul(argv[3], &end, 0);
-	if (*end != '\0') {
-		fprintf(stderr, "illegal value token \"%s\"\n", argv[3]);
-		return -EINVAL;
-	}
-
-	width >>= 3;
-
-	switch (width) {
-	case 1:
-	case 2:
-	case 4:
-		break;
-	default:
-		fprintf(stderr, "illegal width %d\n", width);
-		return -EINVAL;
-	};
-
-	return data->write_mem(data, addr, val, width);
-}
-
-static int do_loadaddr(struct config_data *data, int argc, char *argv[])
-{
-	if (argc < 2)
-		return -EINVAL;
-
-	data->image_load_addr = strtoul(argv[1], NULL, 0);
-
-	return 0;
-}
-
-static int do_dcd_offset(struct config_data *data, int argc, char *argv[])
-{
-	if (argc < 2)
-		return -EINVAL;
-
-	data->image_dcd_offset = strtoul(argv[1], NULL, 0);
-
-	return 0;
-}
-
-struct soc_type {
-	char *name;
-	int header_version;
-	int cpu_type;
-};
-
-static struct soc_type socs[] = {
-	{ .name = "imx25", .header_version = 1, .cpu_type = 25},
-	{ .name = "imx35", .header_version = 1, .cpu_type = 35 },
-	{ .name = "imx51", .header_version = 1, .cpu_type = 51 },
-	{ .name = "imx53", .header_version = 2, .cpu_type = 53 },
-	{ .name = "imx6", .header_version = 2, .cpu_type = 6 },
-};
-
-static int do_soc(struct config_data *data, int argc, char *argv[])
-{
-	char *soc;
-	int i;
-
-	if (argc < 2)
-		return -EINVAL;
-
-	soc = argv[1];
-
-	for (i = 0; i < ARRAY_SIZE(socs); i++) {
-		if (!strcmp(socs[i].name, soc)) {
-			data->header_version = socs[i].header_version;
-			data->cpu_type = socs[i].cpu_type;
-			return 0;
-		}
-	}
-
-	fprintf(stderr, "unkown SoC type \"%s\". Known SoCs are:\n", soc);
-	for (i = 0; i < ARRAY_SIZE(socs); i++)
-		fprintf(stderr, "%s ", socs[i].name);
-	fprintf(stderr, "\n");
-
-	return -EINVAL;
-}
-
-struct command cmds[] = {
-	{
-		.name = "wm",
-		.parse = do_cmd_write_mem,
-	}, {
-		.name = "check",
-		.parse = do_cmd_check,
-	}, {
-		.name = "loadaddr",
-		.parse = do_loadaddr,
-	}, {
-		.name = "dcdofs",
-		.parse = do_dcd_offset,
-	}, {
-		.name = "soc",
-		.parse = do_soc,
-	},
-};
-
-static char *readcmd(struct config_data *data, FILE *f)
-{
-	static char *buf;
-	char *str;
-	ssize_t ret;
-
-	if (!buf) {
-		buf = malloc(4096);
-		if (!buf)
-			return NULL;
-	}
-
-	str = buf;
-	*str = 0;
-
-	while (1) {
-		ret = fread(str, 1, 1, f);
-		if (!ret)
-			return strlen(buf) ? buf : NULL;
-
-		if (*str == '\n' || *str == ';') {
-			*str = 0;
-			return buf;
-		}
-
-		str++;
-	}
-}
-
-static int parse_config(struct config_data *data, const char *filename)
-{
-	FILE *f;
-	int lineno = 0;
-	char *line = NULL, *tmp;
-	char *argv[MAXARGS];
-	int nargs, i, ret = 0;
-
-	f = fopen(filename, "r");
-	if (!f) {
-		fprintf(stderr, "Error: %s - Can't open DCD file\n", filename);
-		exit(1);
-	}
-
-	while (1) {
-		line = readcmd(data, f);
-		if (!line)
-			break;
-
-		lineno++;
-
-		tmp = strchr(line, '#');
-		if (tmp)
-			*tmp = 0;
-
-		nargs = parse_line(line, argv);
-		if (!nargs)
-			continue;
-
-		ret = -ENOENT;
-
-		for (i = 0; i < ARRAY_SIZE(cmds); i++) {
-			if (!strcmp(cmds[i].name, argv[0])) {
-				ret = cmds[i].parse(data, nargs, argv);
-				if (ret) {
-					fprintf(stderr, "error in line %d: %s\n",
-							lineno, strerror(-ret));
-					goto cleanup;
-				}
-				break;
-			}
-		}
-
-		if (ret == -ENOENT) {
-			fprintf(stderr, "no such command: %s\n", argv[0]);
-			goto cleanup;
-		}
-	}
-
-cleanup:
-	fclose(f);
-	return ret;
-}
-
 static int xread(int fd, void *buf, int len)
 {
 	int ret;
diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
new file mode 100644
index 0000000..fa705ff
--- /dev/null
+++ b/scripts/imx/imx.c
@@ -0,0 +1,336 @@
+/*
+ * (C) Copyright 2016 Sascha Hauer, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include "imx.h"
+
+#define MAXARGS 5
+
+static int parse_line(char *line, char *argv[])
+{
+	int nargs = 0;
+
+	while (nargs < MAXARGS) {
+
+		/* skip any white space */
+		while ((*line == ' ') || (*line == '\t'))
+			++line;
+
+		if (*line == '\0')	/* end of line, no more args	*/
+			argv[nargs] = NULL;
+
+		if (*line == '\0') {	/* end of line, no more args	*/
+			argv[nargs] = NULL;
+			return nargs;
+		}
+
+		argv[nargs++] = line;	/* begin of argument string	*/
+
+		/* find end of string */
+		while (*line && (*line != ' ') && (*line != '\t'))
+			++line;
+
+		if (*line == '\0') {	/* end of line, no more args	*/
+			argv[nargs] = NULL;
+			return nargs;
+		}
+
+		*line++ = '\0';		/* terminate current arg	 */
+	}
+
+	printf("** Too many args (max. %d) **\n", MAXARGS);
+
+	return nargs;
+}
+
+struct command {
+	const char *name;
+	int (*parse)(struct config_data *data, int argc, char *argv[]);
+};
+
+static const char *check_cmds[] = {
+	"while_all_bits_clear",		/* while ((*address & mask) == 0); */
+	"while_all_bits_set"	,	/* while ((*address & mask) == mask); */
+	"while_any_bit_clear",		/* while ((*address & mask) != mask); */
+	"while_any_bit_set",		/* while ((*address & mask) != 0); */
+};
+
+static void do_cmd_check_usage(void)
+{
+	fprintf(stderr,
+			"usage: check <width> <cmd> <addr> <mask>\n"
+			"<width> access width in bytes [1|2|4]\n"
+			"with <cmd> one of:\n"
+			"while_all_bits_clear: while ((*addr & mask) == 0)\n"
+			"while_all_bits_set:   while ((*addr & mask) == mask)\n"
+			"while_any_bit_clear:  while ((*addr & mask) != mask)\n"
+			"while_any_bit_set:    while ((*addr & mask) != 0)\n");
+}
+
+static int do_cmd_check(struct config_data *data, int argc, char *argv[])
+{
+	uint32_t addr, mask, cmd;
+	int i, width;
+	const char *scmd;
+
+	if (argc < 5) {
+		do_cmd_check_usage();
+		return -EINVAL;
+	}
+
+	if (!data->check)
+		return -ENOSYS;
+
+	width = strtoul(argv[1], NULL, 0) >> 3;
+	scmd = argv[2];
+	addr = strtoul(argv[3], NULL, 0);
+	mask = strtoul(argv[4], NULL, 0);
+
+	switch (width) {
+	case 1:
+	case 2:
+	case 4:
+		break;
+	default:
+		fprintf(stderr, "illegal width %d\n", width);
+		return -EINVAL;
+	};
+
+	for (i = 0; i < ARRAY_SIZE(check_cmds); i++) {
+		if (!strcmp(scmd, check_cmds[i]))
+			break;
+	}
+
+	if (i == ARRAY_SIZE(check_cmds)) {
+		do_cmd_check_usage();
+		return -EINVAL;
+	}
+
+	cmd = (TAG_CHECK << 24) | (i << 3) | width | ((sizeof(uint32_t) * 3) << 8);
+
+	return data->check(data, cmd, addr, mask);
+}
+
+static int do_cmd_write_mem(struct config_data *data, int argc, char *argv[])
+{
+	uint32_t addr, val, width;
+	char *end;
+
+	if (argc != 4) {
+		fprintf(stderr, "usage: wm [8|16|32] <addr> <val>\n");
+		return -EINVAL;
+	}
+
+	width = strtoul(argv[1], &end, 0);
+	if (*end != '\0') {
+		fprintf(stderr, "illegal width token \"%s\"\n", argv[1]);
+		return -EINVAL;
+	}
+
+	addr = strtoul(argv[2], &end, 0);
+	if (*end != '\0') {
+		fprintf(stderr, "illegal address token \"%s\"\n", argv[2]);
+		return -EINVAL;
+	}
+
+	val = strtoul(argv[3], &end, 0);
+	if (*end != '\0') {
+		fprintf(stderr, "illegal value token \"%s\"\n", argv[3]);
+		return -EINVAL;
+	}
+
+	width >>= 3;
+
+	switch (width) {
+	case 1:
+	case 2:
+	case 4:
+		break;
+	default:
+		fprintf(stderr, "illegal width %d\n", width);
+		return -EINVAL;
+	};
+
+	return data->write_mem(data, addr, val, width);
+}
+
+static int do_loadaddr(struct config_data *data, int argc, char *argv[])
+{
+	if (argc < 2)
+		return -EINVAL;
+
+	data->image_load_addr = strtoul(argv[1], NULL, 0);
+
+	return 0;
+}
+
+static int do_dcd_offset(struct config_data *data, int argc, char *argv[])
+{
+	if (argc < 2)
+		return -EINVAL;
+
+	data->image_dcd_offset = strtoul(argv[1], NULL, 0);
+
+	return 0;
+}
+
+struct soc_type {
+	char *name;
+	int header_version;
+	int cpu_type;
+};
+
+static struct soc_type socs[] = {
+	{ .name = "imx25", .header_version = 1, .cpu_type = 25},
+	{ .name = "imx35", .header_version = 1, .cpu_type = 35 },
+	{ .name = "imx51", .header_version = 1, .cpu_type = 51 },
+	{ .name = "imx53", .header_version = 2, .cpu_type = 53 },
+	{ .name = "imx6", .header_version = 2, .cpu_type = 6 },
+};
+
+static int do_soc(struct config_data *data, int argc, char *argv[])
+{
+	char *soc;
+	int i;
+
+	if (argc < 2)
+		return -EINVAL;
+
+	soc = argv[1];
+
+	for (i = 0; i < ARRAY_SIZE(socs); i++) {
+		if (!strcmp(socs[i].name, soc)) {
+			data->header_version = socs[i].header_version;
+			data->cpu_type = socs[i].cpu_type;
+			return 0;
+		}
+	}
+
+	fprintf(stderr, "unkown SoC type \"%s\". Known SoCs are:\n", soc);
+	for (i = 0; i < ARRAY_SIZE(socs); i++)
+		fprintf(stderr, "%s ", socs[i].name);
+	fprintf(stderr, "\n");
+
+	return -EINVAL;
+}
+
+struct command cmds[] = {
+	{
+		.name = "wm",
+		.parse = do_cmd_write_mem,
+	}, {
+		.name = "check",
+		.parse = do_cmd_check,
+	}, {
+		.name = "loadaddr",
+		.parse = do_loadaddr,
+	}, {
+		.name = "dcdofs",
+		.parse = do_dcd_offset,
+	}, {
+		.name = "soc",
+		.parse = do_soc,
+	},
+};
+
+static char *readcmd(struct config_data *data, FILE *f)
+{
+	static char *buf;
+	char *str;
+	ssize_t ret;
+
+	if (!buf) {
+		buf = malloc(4096);
+		if (!buf)
+			return NULL;
+	}
+
+	str = buf;
+	*str = 0;
+
+	while (1) {
+		ret = fread(str, 1, 1, f);
+		if (!ret)
+			return strlen(buf) ? buf : NULL;
+
+		if (*str == '\n' || *str == ';') {
+			*str = 0;
+			return buf;
+		}
+
+		str++;
+	}
+}
+
+int parse_config(struct config_data *data, const char *filename)
+{
+	FILE *f;
+	int lineno = 0;
+	char *line = NULL, *tmp;
+	char *argv[MAXARGS];
+	int nargs, i, ret = 0;
+
+	f = fopen(filename, "r");
+	if (!f) {
+		fprintf(stderr, "Error: %s - Can't open DCD file\n", filename);
+		exit(1);
+	}
+
+	while (1) {
+		line = readcmd(data, f);
+		if (!line)
+			break;
+
+		lineno++;
+
+		tmp = strchr(line, '#');
+		if (tmp)
+			*tmp = 0;
+
+		nargs = parse_line(line, argv);
+		if (!nargs)
+			continue;
+
+		ret = -ENOENT;
+
+		for (i = 0; i < ARRAY_SIZE(cmds); i++) {
+			if (!strcmp(cmds[i].name, argv[0])) {
+				ret = cmds[i].parse(data, nargs, argv);
+				if (ret) {
+					fprintf(stderr, "error in line %d: %s\n",
+							lineno, strerror(-ret));
+					goto cleanup;
+				}
+				break;
+			}
+		}
+
+		if (ret == -ENOENT) {
+			fprintf(stderr, "no such command: %s\n", argv[0]);
+			goto cleanup;
+		}
+	}
+
+cleanup:
+	fclose(f);
+	return ret;
+}
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 1d23e11..41a54fb 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -54,4 +54,15 @@ struct imx_flash_header_v2 {
 
 	struct imx_boot_data boot_data;
 	struct imx_ivt_header dcd_header;
-} __attribute__((packed));
\ No newline at end of file
+} __attribute__((packed));
+
+struct config_data {
+	uint32_t image_load_addr;
+	uint32_t image_dcd_offset;
+	int header_version;
+	int cpu_type;
+	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
+	int (*write_mem)(struct config_data *data, uint32_t addr, uint32_t val, int width);
+};
+
+int parse_config(struct config_data *data, const char *filename);
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 15/23] scripts: imx: make libusb variables global
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (13 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 14/23] scripts: imx: move config file parser to separate file Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 16/23] scripts: imx-usb-loader: Add -s and -i options Sascha Hauer
                   ` (7 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

No need to pass the libusb context through from function to function.
Make them globally visible.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 173 ++++++++++++++++++++-----------------------
 1 file changed, 81 insertions(+), 92 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 5a84721..7925d2d 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -42,6 +42,8 @@
 #define FT_LOAD_ONLY	0x00
 
 int verbose;
+static struct libusb_device_handle *usb_dev_handle;
+static struct usb_id *usb_id;
 
 struct mach_id {
 	struct mach_id * next;
@@ -292,12 +294,11 @@ static long get_file_size(FILE *xfile)
  * EP2IN - bulk in
  * (max packet size of 512 bytes)
  */
-static int transfer(struct libusb_device_handle *h, int report, unsigned char *p, unsigned cnt,
-		int* last_trans, struct usb_id *p_id)
+static int transfer(int report, unsigned char *p, unsigned cnt, int *last_trans)
 {
 	int err;
-	if (cnt > p_id->mach_id->max_transfer)
-		cnt = p_id->mach_id->max_transfer;
+	if (cnt > usb_id->mach_id->max_transfer)
+		cnt = usb_id->mach_id->max_transfer;
 
 	if (verbose > 4) {
 		printf("report=%i\n", report);
@@ -305,9 +306,10 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p
 			dump_bytes(p, cnt, 0);
 	}
 
-	if (p_id->mach_id->mode == MODE_BULK) {
+	if (usb_id->mach_id->mode == MODE_BULK) {
 		*last_trans = 0;
-		err = libusb_bulk_transfer(h, (report < 3) ? 1 : 2 + EP_IN, p, cnt, last_trans, 1000);
+		err = libusb_bulk_transfer(usb_dev_handle,
+					   (report < 3) ? 1 : 2 + EP_IN, p, cnt, last_trans, 1000);
 	} else {
 		unsigned char tmp[1028];
 
@@ -315,7 +317,7 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p
 
 		if (report < 3) {
 			memcpy(&tmp[1], p, cnt);
-			err = libusb_control_transfer(h,
+			err = libusb_control_transfer(usb_dev_handle,
 					CTRL_OUT,
 					HID_SET_REPORT,
 					(HID_REPORT_TYPE_OUTPUT << 8) | report,
@@ -327,7 +329,8 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p
 		} else {
 			*last_trans = 0;
 			memset(&tmp[1], 0, cnt);
-			err = libusb_interrupt_transfer(h, 1 + EP_IN, tmp, cnt + 1, last_trans, 1000);
+			err = libusb_interrupt_transfer(usb_dev_handle,
+							1 + EP_IN, tmp, cnt + 1, last_trans, 1000);
 			if (err >= 0) {
 				if (tmp[0] == (unsigned char)report) {
 					if (*last_trans > 1) {
@@ -349,7 +352,7 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p
 	return err;
 }
 
-int do_status(libusb_device_handle *h, struct usb_id *p_id)
+int do_status(void)
 {
 	int last_trans;
 	unsigned char tmp[64];
@@ -364,14 +367,14 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id)
 	};
 
 	for (;;) {
-		err = transfer(h, 1, (unsigned char*)status_command, 16, &last_trans, p_id);
+		err = transfer(1, (unsigned char*)status_command, 16, &last_trans);
 
 		if (verbose > 2)
 			printf("report 1, wrote %i bytes, err=%i\n", last_trans, err);
 
 		memset(tmp, 0, sizeof(tmp));
 
-		err = transfer(h, 3, tmp, 64, &last_trans, p_id);
+		err = transfer(3, tmp, 64, &last_trans);
 
 		if (verbose > 2) {
 			printf("report 3, read %i bytes, err=%i\n", last_trans, err);
@@ -387,8 +390,8 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id)
 			break;
 	}
 
-	if (p_id->mach_id->mode == MODE_HID) {
-		err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id);
+	if (usb_id->mach_id->mode == MODE_HID) {
+		err = transfer(4, tmp, sizeof(tmp), &last_trans);
 		if (err)
 			printf("4 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
@@ -399,8 +402,7 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id)
 
 #define V(a) (((a) >> 24) & 0xff), (((a) >> 16) & 0xff), (((a) >> 8) & 0xff), ((a) & 0xff)
 
-static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
-		unsigned addr, unsigned char *dest, unsigned cnt)
+static int read_memory(unsigned addr, unsigned char *dest, unsigned cnt)
 {
 	static unsigned char read_reg_command[] = {
 		1,
@@ -428,7 +430,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 	read_reg_command[10] = (unsigned char)(cnt);
 
 	for (;;) {
-		err = transfer(h, 1, read_reg_command, 16, &last_trans, p_id);
+		err = transfer(1, read_reg_command, 16, &last_trans);
 		if (!err)
 			break;
 		printf("read_reg_command err=%i, last_trans=%i\n", err, last_trans);
@@ -438,7 +440,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 		retry++;
 	}
 
-	err = transfer(h, 3, tmp, 4, &last_trans, p_id);
+	err = transfer(3, tmp, 4, &last_trans);
 	if (err) {
 		printf("r3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 				err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
@@ -449,7 +451,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	while (rem) {
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
-		err = transfer(h, 4, tmp, 64, &last_trans, p_id);
+		err = transfer(4, tmp, 64, &last_trans);
 		if (err) {
 			printf("r4 in err=%i, last_trans=%i  %02x %02x %02x %02x cnt=%u rem=%d\n",
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem);
@@ -473,8 +475,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 	return err;
 }
 
-static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id,
-		unsigned addr, unsigned val, int width)
+static int write_memory(unsigned addr, unsigned val, int width)
 {
 	int retry = 0;
 	int last_trans;
@@ -517,7 +518,7 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 	write_reg_command[14] = (unsigned char)(val);
 
 	for (;;) {
-		err = transfer(h, 1, write_reg_command, 16, &last_trans, p_id);
+		err = transfer(1, write_reg_command, 16, &last_trans);
 		if (!err)
 			break;
 		printf("write_reg_command err=%i, last_trans=%i\n", err, last_trans);
@@ -529,7 +530,7 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	memset(tmp, 0, sizeof(tmp));
 
-	err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+	err = transfer(3, tmp, sizeof(tmp), &last_trans);
 	if (err) {
 		printf("w3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 				err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
@@ -538,15 +539,14 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	memset(tmp, 0, sizeof(tmp));
 
-	err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id);
+	err = transfer(4, tmp, sizeof(tmp), &last_trans);
 	if (err)
 		printf("w4 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 				err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
 	return err;
 }
 
-static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
-		void *buf, unsigned len, unsigned dladdr, unsigned char type)
+static int load_file(void *buf, unsigned len, unsigned dladdr, unsigned char type)
 {
 	static unsigned char dl_command[] = {
 		0x04,
@@ -576,7 +576,7 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 	dl_command[15] =  type;
 
 	for (;;) {
-		err = transfer(h, 1, dl_command, 16, &last_trans, p_id);
+		err = transfer(1, dl_command, 16, &last_trans);
 		if (!err)
 			break;
 
@@ -589,8 +589,8 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	retry = 0;
 
-	if (p_id->mach_id->mode == MODE_BULK) {
-		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+	if (usb_id->mach_id->mode == MODE_BULK) {
+		err = transfer(3, tmp, sizeof(tmp), &last_trans);
 		if (err)
 			printf("in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
@@ -600,12 +600,12 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 	cnt = len;
 
 	while (1) {
-		int now = get_min(cnt, p_id->mach_id->max_transfer);
+		int now = get_min(cnt, usb_id->mach_id->max_transfer);
 
 		if (!now)
 			break;
 
-		err = transfer(h, 2, p, now, &now, p_id);
+		err = transfer(2, p, now, &now);
 		if (err) {
 			printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
 			return err;
@@ -615,24 +615,23 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
 		cnt -= now;
 	}
 
-	if (p_id->mach_id->mode == MODE_HID) {
-		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+	if (usb_id->mach_id->mode == MODE_HID) {
+		err = transfer(3, tmp, sizeof(tmp), &last_trans);
 		if (err)
 			printf("3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
-		err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id);
+		err = transfer(4, tmp, sizeof(tmp), &last_trans);
 		if (err)
 			printf("4 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
 					err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
 	} else {
-		do_status(h, p_id);
+		do_status();
 	}
 
 	return transfer_size;
 }
 
-static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_id,
-		struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt)
+static int write_dcd_table_ivt(struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt)
 {
 	unsigned char *dcd_end;
 	unsigned m_length;
@@ -694,7 +693,7 @@ static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_
 			unsigned val = (dcd[4] << 24) | (dcd[5] << 16) | (dcd[6] << 8) | dcd[7];
 
 			dcd += 8;
-			err = write_memory(h, p_id, addr, val, 4);
+			err = write_memory(addr, val, 4);
 			if (err < 0)
 				return err;
 		}
@@ -749,8 +748,7 @@ static int get_dcd_range_old(struct imx_flash_header *hdr,
 	return 0;
 }
 
-static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_id,
-		struct imx_flash_header *hdr, unsigned char *file_start, unsigned cnt)
+static int write_dcd_table_old(struct imx_flash_header *hdr, unsigned char *file_start, unsigned cnt)
 {
 	unsigned val;
 	unsigned char *dcd_end;
@@ -771,14 +769,14 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_
 		case 1:
 			if (verbose > 1)
 				printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val);
-			err = write_memory(h, p_id, addr, val, 1);
+			err = write_memory(addr, val, 1);
 			if (err < 0)
 				return err;
 			break;
 		case 4:
 			if (verbose > 1)
 				printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val);
-			err = write_memory(h, p_id, addr, val, 4);
+			err = write_memory(addr, val, 4);
 			if (err < 0)
 				return err;
 			break;
@@ -795,8 +793,7 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_
 	return err;
 }
 
-static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id,
-			 const void *buf, unsigned len, unsigned addr)
+static int verify_memory(const void *buf, unsigned len, unsigned addr)
 {
 	int ret, mismatch = 0;
 	void *readbuf;
@@ -806,7 +803,7 @@ static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id,
 	if (!readbuf)
 		return -ENOMEM;
 
-	ret = read_memory(h, p_id, addr, readbuf, len);
+	ret = read_memory(addr, readbuf, len);
 	if (ret < 0)
 		goto err;
 
@@ -834,12 +831,12 @@ err:
 	return ret;
 }
 
-static int is_header(struct usb_id *p_id, unsigned char *p)
+static int is_header(unsigned char *p)
 {
 	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 
-	switch (p_id->mach_id->header_type) {
+	switch (usb_id->mach_id->header_type) {
 	case HDR_MX51:
 		if (ohdr->app_code_barker == 0xb1)
 			return 1;
@@ -852,21 +849,19 @@ static int is_header(struct usb_id *p_id, unsigned char *p)
 	return 0;
 }
 
-static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsigned char *p,
-		unsigned char *file_start, unsigned cnt)
+static int perform_dcd(unsigned char *p, unsigned char *file_start, unsigned cnt)
 {
 	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 	int ret = 0;
 
-	switch (p_id->mach_id->header_type) {
+	switch (usb_id->mach_id->header_type) {
 	case HDR_MX51:
-		ret = write_dcd_table_old(h, p_id, ohdr, file_start, cnt);
-		ohdr->dcd = 0;
+		ret = write_dcd_table_old(ohdr, file_start, cnt);
 
 		break;
 	case HDR_MX53:
-		ret = write_dcd_table_ivt(h, p_id, hdr, file_start, cnt);
+		ret = write_dcd_table_ivt(hdr, file_start, cnt);
 		hdr->dcd_ptr = 0;
 
 		break;
@@ -875,13 +870,12 @@ static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsi
 	return ret;
 }
 
-static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id,
-		unsigned char *p, unsigned char *file_start, unsigned cnt)
+static int clear_dcd_ptr(unsigned char *p, unsigned char *file_start, unsigned cnt)
 {
 	struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 
-	switch (p_id->mach_id->header_type) {
+	switch (usb_id->mach_id->header_type) {
 	case HDR_MX51:
 		printf("clear dcd_ptr=0x%08x\n", ohdr->dcd);
 		ohdr->dcd = 0;
@@ -894,12 +888,12 @@ static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id,
 	return 0;
 }
 
-static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *file_start,
+static int get_dl_start(unsigned char *p, unsigned char *file_start,
 		unsigned cnt, unsigned *dladdr, unsigned *max_length, unsigned *plugin,
 		unsigned *header_addr)
 {
 	unsigned char* file_end = file_start + cnt;
-	switch (p_id->mach_id->header_type) {
+	switch (usb_id->mach_id->header_type) {
 	case HDR_MX51:
 	{
 		struct imx_flash_header *ohdr = (struct imx_flash_header *)p;
@@ -941,8 +935,7 @@ static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *fi
 	return 0;
 }
 
-static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
-		struct usb_work *curr, unsigned char *buf, int cnt,
+static int process_header(struct usb_work *curr, unsigned char *buf, int cnt,
 		unsigned *p_dladdr, unsigned *p_max_length, unsigned *p_plugin,
 		unsigned *p_header_addr)
 {
@@ -955,17 +948,17 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	for (header_offset = 0; header_offset < header_max; header_offset += header_inc, p += header_inc) {
 
-		if (!is_header(p_id, p))
+		if (!is_header(p))
 			continue;
 
-		ret = get_dl_start(p_id, p, buf, cnt, p_dladdr, p_max_length, p_plugin, p_header_addr);
+		ret = get_dl_start(p, buf, cnt, p_dladdr, p_max_length, p_plugin, p_header_addr);
 		if (ret < 0) {
 			printf("!!get_dl_start returned %i\n", ret);
 			return ret;
 		}
 
 		if (curr->dcd) {
-			ret = perform_dcd(h, p_id, p, buf, cnt);
+			ret = perform_dcd(p, buf, cnt);
 			if (ret < 0) {
 				printf("!!perform_dcd returned %i\n", ret);
 				return ret;
@@ -978,7 +971,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
 		}
 
 		if (curr->clear_dcd) {
-			ret = clear_dcd_ptr(h, p_id, p, buf, cnt);
+			ret = clear_dcd_ptr(p, buf, cnt);
 			if (ret < 0) {
 				printf("!!clear_dcd returned %i\n", ret);
 				return ret;
@@ -1004,8 +997,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
 	return -ENODEV;
 }
 
-static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
-		struct usb_work *curr, int verify)
+static int do_irom_download(struct usb_work *curr, int verify)
 {
 	static unsigned char jump_command[] = {0x0b,0x0b, V(0),  0x00, V(0x00000000), V(0), 0x00};
 
@@ -1058,7 +1050,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	max_length = fsize;
 
-	ret = process_header(h, p_id, curr, buf, cnt,
+	ret = process_header(curr, buf, cnt,
 			&dladdr, &max_length, &plugin, &header_addr);
 	if (ret < 0)
 		goto cleanup;
@@ -1087,7 +1079,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 
 	type = (curr->plug || curr->jump_mode) ? FT_APP : FT_LOAD_ONLY;
 
-	if (p_id->mach_id->mode == MODE_BULK && type == FT_APP) {
+	if (usb_id->mach_id->mode == MODE_BULK && type == FT_APP) {
 		/* No jump command, dladdr should point to header */
 		dladdr = header_addr;
 	}
@@ -1119,7 +1111,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 
 		memcpy(verify_buffer, p, 64);
 
-		if ((type == FT_APP) && (p_id->mach_id->mode != MODE_HID)) {
+		if ((type == FT_APP) && (usb_id->mach_id->mode != MODE_HID)) {
 			type = FT_LOAD_ONLY;
 			verify = 2;
 		}
@@ -1128,7 +1120,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 	printf("loading binary file(%s) to %08x, skip=0x%x, fsize=%u type=%d...\n",
 			curr->filename, dladdr, skip, fsize, type);
 
-	ret = load_file(h, p_id, image, fsize, dladdr, type);
+	ret = load_file(image, fsize, dladdr, type);
 	if (ret < 0)
 		goto cleanup;
 
@@ -1137,7 +1129,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 	if (verify) {
 		printf("verifying file...\n");
 
-		ret = verify_memory(h, p_id, image, fsize, dladdr);
+		ret = verify_memory(image, fsize, dladdr);
 		if (ret < 0) {
 			printf("verifying failed\n");
 			goto cleanup;
@@ -1151,15 +1143,14 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 			 * so we load part of the image again with type FT_APP
 			 * this time.
 			 */
-			ret = load_file(h, p_id, verify_buffer, 64,
-					dladdr, FT_APP);
+			ret = load_file(verify_buffer, 64, dladdr, FT_APP);
 			if (ret < 0)
 				goto cleanup;
 
 		}
 	}
 
-	if (p_id->mach_id->mode == MODE_HID && type == FT_APP) {
+	if (usb_id->mach_id->mode == MODE_HID && type == FT_APP) {
 		printf("jumping to 0x%08x\n", header_addr);
 
 		jump_command[2] = (unsigned char)(header_addr >> 24);
@@ -1171,7 +1162,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		retry = 0;
 
 		for (;;) {
-			err = transfer(h, 1, jump_command, 16, &last_trans, p_id);
+			err = transfer(1, jump_command, 16, &last_trans);
 			if (!err)
 				break;
 
@@ -1184,7 +1175,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
 		}
 
 		memset(tmp, 0, sizeof(tmp));
-		err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id);
+		err = transfer(3, tmp, sizeof(tmp), &last_trans);
 
 		if (err)
 			printf("j3 in err=%i, last_trans=%i  %02x %02x %02x %02x\n",
@@ -1211,7 +1202,6 @@ static void usage(const char *prgname)
 
 int main(int argc, char *argv[])
 {
-	struct usb_id *p_id = NULL;
 	struct mach_id *mach;
 	libusb_device **devs;
 	libusb_device *dev;
@@ -1219,7 +1209,6 @@ int main(int argc, char *argv[])
 	int err;
 	int ret = 1;
 	ssize_t cnt;
-	libusb_device_handle *h = NULL;
 	int config = 0;
 	int verify = 0;
 	struct usb_work w = {};
@@ -1267,7 +1256,7 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
-	err = libusb_open(dev, &h);
+	err = libusb_open(dev, &usb_dev_handle);
 	if (err) {
 		fprintf(stderr, "Could not open device vid=0x%x pid=0x%x err=%d\n",
 				mach->vid, mach->pid, err);
@@ -1276,44 +1265,44 @@ int main(int argc, char *argv[])
 
 	libusb_free_device_list(devs, 1);
 
-	libusb_get_configuration(h, &config);
+	libusb_get_configuration(usb_dev_handle, &config);
 
-	if (libusb_kernel_driver_active(h, 0))
-		 libusb_detach_kernel_driver(h, 0);
+	if (libusb_kernel_driver_active(usb_dev_handle, 0))
+		 libusb_detach_kernel_driver(usb_dev_handle, 0);
 
-	err = libusb_claim_interface(h, 0);
+	err = libusb_claim_interface(usb_dev_handle, 0);
 	if (err) {
 		printf("Claim failed\n");
 		goto out;
 	}
 
-	p_id = malloc(sizeof(*p_id));
-	if (!p_id) {
+	usb_id = malloc(sizeof(*usb_id));
+	if (!usb_id) {
 		perror("malloc");
 		exit(1);
 	}
 
-	p_id->mach_id = mach;
+	usb_id->mach_id = mach;
 
-	err = do_status(h, p_id);
+	err = do_status();
 	if (err) {
 		printf("status failed\n");
 		goto out;
 	}
 
-	err = do_irom_download(h, p_id, &w, verify);
+	err = do_irom_download(&w, verify);
 	if (err) {
-		err = do_status(h, p_id);
+		err = do_status();
 		goto out;
 	}
 
 	ret = 0;
 out:
-	if (p_id)
-		free(p_id);
+	if (usb_id)
+		free(usb_id);
 
-	if (h)
-		libusb_close(h);
+	if (usb_dev_handle)
+		libusb_close(usb_dev_handle);
 
 	libusb_exit(NULL);
 
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 16/23] scripts: imx-usb-loader: Add -s and -i options
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (14 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 15/23] scripts: imx: make libusb variables global Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 17/23] scripts: imx: Drop double check Sascha Hauer
                   ` (6 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Normally imx-usb-loader interprets and executes the DCD table from
an uploaded image and invalidates the DCD before uploading the image
itself to prevent the i.MX ROM code from executing it again. With HAB
signed images this is not possible since invalidating the DCD table
modifies the image which also makes the signature invalid.
To support this usecase add two new options to imx-usb-loader:

The -i option allows to pass in an external config file which can be
used to setup SDRAM. The DCD table in the image can then be made empty
so that the ROM does not see a second SDRAM setup.

The -s option allows to skip interpreting the DCD table in the image.
This may when some setup stuff is still in the images DCD table but
shall be executed by the ROM and not by imx-usb-loader.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 7925d2d..064212e 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -42,6 +42,7 @@
 #define FT_LOAD_ONLY	0x00
 
 int verbose;
+static int skip_image_dcd;
 static struct libusb_device_handle *usb_dev_handle;
 static struct usb_id *usb_id;
 
@@ -855,6 +856,9 @@ static int perform_dcd(unsigned char *p, unsigned char *file_start, unsigned cnt
 	struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p;
 	int ret = 0;
 
+	if (skip_image_dcd)
+		return 0;
+
 	switch (usb_id->mach_id->header_type) {
 	case HDR_MX51:
 		ret = write_dcd_table_old(ohdr, file_start, cnt);
@@ -1191,10 +1195,28 @@ cleanup:
 	return ret;
 }
 
+static int write_mem(struct config_data *data, uint32_t addr, uint32_t val, int width)
+{
+	printf("wr 0x%08x 0x%08x\n", addr, val);
+
+	return write_memory(addr, val, width);
+}
+
+static int parse_initfile(const char *filename)
+{
+	struct config_data data = {
+		.write_mem = write_mem,
+	};
+
+	return parse_config(&data, filename);
+}
+
 static void usage(const char *prgname)
 {
 	fprintf(stderr, "usage: %s [OPTIONS] [FILENAME]\n\n"
 		"-c           check correctness of flashed image\n"
+		"-i <cfgfile> Specify custom SoC initialization file\n"
+		"-s           skip DCD included in image\n"
 		"-v           verbose (give multiple times to increase)\n"
 		"-h           this help\n", prgname);
 	exit(1);
@@ -1213,8 +1235,9 @@ int main(int argc, char *argv[])
 	int verify = 0;
 	struct usb_work w = {};
 	int opt;
+	char *initfile = NULL;
 
-	while ((opt = getopt(argc, argv, "cvh")) != -1) {
+	while ((opt = getopt(argc, argv, "cvhi:s")) != -1) {
 		switch (opt) {
 		case 'c':
 			verify = 1;
@@ -1224,6 +1247,12 @@ int main(int argc, char *argv[])
 			break;
 		case 'h':
 			usage(argv[0]);
+		case 'i':
+			initfile = optarg;
+			break;
+		case 's':
+			skip_image_dcd = 1;
+			break;
 		default:
 			exit(1);
 		}
@@ -1290,6 +1319,12 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
+	if (initfile) {
+		err = parse_initfile(initfile);
+		if (err)
+			goto out;
+	}
+
 	err = do_irom_download(&w, verify);
 	if (err) {
 		err = do_status();
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 17/23] scripts: imx: Drop double check
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (15 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 16/23] scripts: imx-usb-loader: Add -s and -i options Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 18/23] scripts: imx-image: move more variables to context data Sascha Hauer
                   ` (5 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

We have the same "if (*line == '\0')" check twice. Remove one of them.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index fa705ff..79abed2 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -35,9 +35,6 @@ static int parse_line(char *line, char *argv[])
 		while ((*line == ' ') || (*line == '\t'))
 			++line;
 
-		if (*line == '\0')	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-
 		if (*line == '\0') {	/* end of line, no more args	*/
 			argv[nargs] = NULL;
 			return nargs;
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 18/23] scripts: imx-image: move more variables to context data
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (16 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 17/23] scripts: imx: Drop double check Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:43 ` [PATCH 19/23] scripts: imx-image: pass config data to add_header_* Sascha Hauer
                   ` (4 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 17 ++++++++---------
 scripts/imx/imx.h       |  3 +++
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index d6ed120..cb3c242 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -315,9 +315,8 @@ int main(int argc, char *argv[])
 	int opt, ret;
 	char *configfile = NULL;
 	char *imagename = NULL;
-	char *outfile = NULL;
 	void *buf;
-	size_t image_size = 0, load_size, insize;
+	size_t insize;
 	void *infile;
 	struct stat s;
 	int infd, outfd;
@@ -338,7 +337,7 @@ int main(int argc, char *argv[])
 			imagename = optarg;
 			break;
 		case 'o':
-			outfile = optarg;
+			data.outfile = optarg;
 			break;
 		case 'b':
 			add_barebox_header = 1;
@@ -366,7 +365,7 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	if (!outfile) {
+	if (!data.outfile) {
 		fprintf(stderr, "output file not given\n");
 		exit(1);
 	}
@@ -378,7 +377,7 @@ int main(int argc, char *argv[])
 			exit(1);
 		}
 
-		image_size = s.st_size;
+		data.image_size = s.st_size;
 	}
 
 	ret = parse_config(&data, configfile);
@@ -404,7 +403,7 @@ int main(int argc, char *argv[])
 		check_last_dcd(0);
 
 	if (dcd_only) {
-		ret = write_dcd(outfile);
+		ret = write_dcd(data.outfile);
 		if (ret)
 			exit(1);
 		exit (0);
@@ -418,10 +417,10 @@ int main(int argc, char *argv[])
 	 * - i.MX6 SPI NOR boot corrupts the last few bytes of an image loaded
 	 *   in ver funy ways when the image size is not 4 byte aligned
 	 */
-	load_size = roundup(image_size + HEADER_LEN, 0x1000);
+	data.load_size = roundup(data.image_size + HEADER_LEN, 0x1000);
 
 	if (data.cpu_type == 35)
-		load_size += HEADER_LEN;
+		data.load_size += HEADER_LEN;
 
 	switch (data.header_version) {
 	case 1:
@@ -454,7 +453,7 @@ int main(int argc, char *argv[])
 	xread(infd, infile, insize);
 	close(infd);
 
-	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+	outfd = open(data.outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (outfd < 0) {
 		perror("open");
 		exit(1);
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 41a54fb..6466d8c 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -59,6 +59,9 @@ struct imx_flash_header_v2 {
 struct config_data {
 	uint32_t image_load_addr;
 	uint32_t image_dcd_offset;
+	uint32_t image_size;
+	uint32_t load_size;
+	char *outfile;
 	int header_version;
 	int cpu_type;
 	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 19/23] scripts: imx-image: pass config data to add_header_*
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (17 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 18/23] scripts: imx-image: move more variables to context data Sascha Hauer
@ 2016-01-29 10:43 ` Sascha Hauer
  2016-01-29 10:44 ` [PATCH 20/23] scripts: imx-image: Support adding a Super Root Key to the image Sascha Hauer
                   ` (3 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:43 UTC (permalink / raw)
  To: Barebox List

Now that we have a config data struct we can pass it to add_header_*
to reduce the argument count.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index cb3c242..4fc4994 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -73,11 +73,14 @@ static uint32_t bb_header[] = {
 	0x55555555,
 };
 
-static int add_header_v1(void *buf, int offset, uint32_t loadaddr, uint32_t imagesize)
+static int add_header_v1(struct config_data *data, void *buf)
 {
 	struct imx_flash_header *hdr;
 	int dcdsize = curdcd * sizeof(uint32_t);
 	uint32_t *psize = buf + ARM_HEAD_SIZE_OFFSET;
+	int offset = data->image_dcd_offset;
+	uint32_t loadaddr = data->image_load_addr;
+	uint32_t imagesize = data->load_size;
 
 	if (add_barebox_header) {
 		memcpy(buf, bb_header, sizeof(bb_header));
@@ -128,11 +131,14 @@ static int write_mem_v1(uint32_t addr, uint32_t val, int width)
  * ============================================================================
  */
 
-static int add_header_v2(void *buf, int offset, uint32_t loadaddr, uint32_t imagesize)
+static int add_header_v2(struct config_data *data, void *buf)
 {
 	struct imx_flash_header_v2 *hdr;
 	int dcdsize = curdcd * sizeof(uint32_t);
 	uint32_t *psize = buf + ARM_HEAD_SIZE_OFFSET;
+	int offset = data->image_dcd_offset;
+	uint32_t loadaddr = data->image_load_addr;
+	uint32_t imagesize = data->load_size;
 
 	if (add_barebox_header)
 		memcpy(buf, bb_header, sizeof(bb_header));
@@ -424,10 +430,10 @@ int main(int argc, char *argv[])
 
 	switch (data.header_version) {
 	case 1:
-		add_header_v1(buf, data.image_dcd_offset, data.image_load_addr, load_size);
+		add_header_v1(&data, buf);
 		break;
 	case 2:
-		add_header_v2(buf, data.image_dcd_offset, data.image_load_addr, load_size);
+		add_header_v2(&data, buf);
 		break;
 	default:
 		fprintf(stderr, "Congratulations! You're welcome to implement header version %d\n",
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 20/23] scripts: imx-image: Support adding a Super Root Key to the image
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (18 preceding siblings ...)
  2016-01-29 10:43 ` [PATCH 19/23] scripts: imx-image: pass config data to add_header_* Sascha Hauer
@ 2016-01-29 10:44 ` Sascha Hauer
  2016-01-29 10:44 ` [PATCH 21/23] scripts: imx: Create CSF files from imx config file Sascha Hauer
                   ` (2 subsequent siblings)
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:44 UTC (permalink / raw)
  To: Barebox List

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-imx/Kconfig |   7 +++
 scripts/imx/Makefile      |   4 ++
 scripts/imx/imx-image.c   | 130 ++++++++++++++++++++++++++++++++++++++++++++++
 scripts/imx/imx.h         |   1 +
 4 files changed, 142 insertions(+)

diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
index 9e7be2e..9c7d51f 100644
--- a/arch/arm/mach-imx/Kconfig
+++ b/arch/arm/mach-imx/Kconfig
@@ -67,6 +67,12 @@ config ARCH_IMX_IMXIMAGE
 	help
 	  if enabled the imx-image tool is compiled
 
+config ARCH_IMX_IMXIMAGE_SSL_SUPPORT
+	bool
+	help
+	  This enables SSL support for the imx-image tool. This is required
+	  for created images for HABv3. This adds openssl to the build dependencies
+
 config ARCH_IMX_XLOAD
 	bool
 	depends on ARCH_IMX51
@@ -735,6 +741,7 @@ endif
 config HABV3
 	tristate "HABv3 support"
 	select HAB
+	select ARCH_IMX_IMXIMAGE_SSL_SUPPORT
 	depends on ARCH_IMX25
 	help
 	  High Assurance Boot, as found on i.MX25.
diff --git a/scripts/imx/Makefile b/scripts/imx/Makefile
index 6883659..d9f0c51 100644
--- a/scripts/imx/Makefile
+++ b/scripts/imx/Makefile
@@ -7,6 +7,10 @@ HOSTCFLAGS_imx-usb-loader.o = `pkg-config --cflags libusb-1.0`
 HOSTLOADLIBES_imx-usb-loader  = `pkg-config --libs libusb-1.0`
 
 HOSTCFLAGS_imx-image.o = -I$(srctree)
+ifdef CONFIG_ARCH_IMX_IMXIMAGE_SSL_SUPPORT
+HOSTCFLAGS_imx-image.o += -DIMXIMAGE_SSL_SUPPORT
+HOSTLOADLIBES_imx-image  = `pkg-config --libs openssl`
+endif
 
 imx-usb-loader-objs := imx-usb-loader.o imx.o
 imx-image-objs := imx-image.o imx.o
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 4fc4994..79f644d 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -73,6 +73,130 @@ static uint32_t bb_header[] = {
 	0x55555555,
 };
 
+struct hab_rsa_public_key {
+	uint8_t rsa_exponent[4]; /* RSA public exponent */
+	uint32_t rsa_modulus; /* RSA modulus pointer */
+	uint16_t exponent_size; /* Exponent size in bytes */
+	uint16_t modulus_size; /* Modulus size in bytes*/
+	uint8_t init_flag; /* Indicates if key initialized */
+};
+
+#ifdef IMXIMAGE_SSL_SUPPORT
+#define PUBKEY_ALGO_LEN 2048
+
+#include <openssl/x509v3.h>
+#include <openssl/bn.h>
+#include <openssl/asn1.h>
+#include <openssl/x509.h>
+#include <openssl/x509_vfy.h>
+#include <openssl/pem.h>
+#include <openssl/bio.h>
+
+static int extract_key(const char *certfile, uint8_t **modulus, int *modulus_len,
+	uint8_t **exponent, int *exponent_len)
+{
+	char buf[PUBKEY_ALGO_LEN];
+	int pubkey_algonid;
+	const char *sslbuf;
+	EVP_PKEY *pkey;
+	FILE *fp;
+	X509 *cert;
+	RSA *rsa_key;
+
+	fp = fopen(certfile, "r");
+	if (!fp) {
+		fprintf(stderr, "unable to open certfile: %s\n", certfile);
+		return -errno;
+	}
+
+	cert = PEM_read_X509(fp, NULL, NULL, NULL);
+	if (!cert) {
+		fprintf(stderr, "unable to parse certificate in: %s\n", certfile);
+		fclose(fp);
+		return -errno;
+	}
+
+	fclose(fp);
+
+	pubkey_algonid = OBJ_obj2nid(cert->cert_info->key->algor->algorithm);
+	if (pubkey_algonid == NID_undef) {
+		fprintf(stderr, "unable to find specified public key algorithm name.\n");
+		return -EINVAL;
+	}
+
+	if (pubkey_algonid != NID_rsaEncryption)
+		return -EINVAL;
+
+	sslbuf = OBJ_nid2ln(pubkey_algonid);
+	strncpy(buf, sslbuf, PUBKEY_ALGO_LEN);
+
+	pkey = X509_get_pubkey(cert);
+	if (!pkey) {
+		fprintf(stderr, "unable to extract public key from certificate");
+		return -EINVAL;
+	}
+
+	rsa_key = pkey->pkey.rsa;
+	if (!rsa_key) {
+		fprintf(stderr, "unable to extract RSA public key");
+		return -EINVAL;
+	}
+
+	*modulus_len = BN_num_bytes(rsa_key->n);
+	*modulus = malloc(*modulus_len);
+	BN_bn2bin(rsa_key->n, *modulus);
+
+	*exponent_len = BN_num_bytes(rsa_key->e);
+	*exponent = malloc(*exponent_len);
+	BN_bn2bin(rsa_key->e, *exponent);
+
+	EVP_PKEY_free(pkey);
+	X509_free(cert);
+
+	return 0;
+}
+
+static int add_srk(void *buf, int offset, uint32_t loadaddr, const char *srkfile)
+{
+	struct imx_flash_header *hdr = buf + offset;
+	struct hab_rsa_public_key *key = buf + 0xc00;
+	uint8_t *exponent = NULL, *modulus = NULL, *modulus_dest;
+	int exponent_len = 0, modulus_len = 0;
+	int ret;
+
+	hdr->super_root_key = loadaddr + 0xc00;
+
+	key->init_flag = 1;
+	key->exponent_size = htole16(3);
+
+	ret = extract_key(srkfile, &modulus, &modulus_len, &exponent, &exponent_len);
+	if (ret)
+		return ret;
+
+	modulus_dest = (void *)(key + 1);
+
+	memcpy(modulus_dest, modulus, modulus_len);
+
+	key->modulus_size = htole16(modulus_len);
+	key->rsa_modulus = htole32(hdr->super_root_key + sizeof(*key));
+
+	if (exponent_len > 4)
+		return -EINVAL;
+
+	key->exponent_size = exponent_len;
+	memcpy(&key->rsa_exponent, exponent, key->exponent_size);
+
+	return 0;
+}
+#else
+static int add_srk(void *buf, int offset, uint32_t loadaddr, const char *srkfile)
+{
+	fprintf(stderr, "This version of imx-image is compiled without SSL support\n");
+
+	return -EINVAL;
+}
+#endif /* IMXIMAGE_SSL_SUPPORT */
+
 static int add_header_v1(struct config_data *data, void *buf)
 {
 	struct imx_flash_header *hdr;
@@ -431,6 +555,12 @@ int main(int argc, char *argv[])
 	switch (data.header_version) {
 	case 1:
 		add_header_v1(&data, buf);
+		if (data.srkfile) {
+			ret = add_srk(buf, data.image_dcd_offset, data.image_load_addr,
+				      data.srkfile);
+			if (ret)
+				exit(1);
+		}
 		break;
 	case 2:
 		add_header_v2(&data, buf);
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 6466d8c..1ef4ac6 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -62,6 +62,7 @@ struct config_data {
 	uint32_t image_size;
 	uint32_t load_size;
 	char *outfile;
+	char *srkfile;
 	int header_version;
 	int cpu_type;
 	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 21/23] scripts: imx: Create CSF files from imx config file
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (19 preceding siblings ...)
  2016-01-29 10:44 ` [PATCH 20/23] scripts: imx-image: Support adding a Super Root Key to the image Sascha Hauer
@ 2016-01-29 10:44 ` Sascha Hauer
  2016-01-29 10:44 ` [PATCH 22/23] scripts: imx: Allow to create signed images Sascha Hauer
  2016-01-29 10:44 ` [PATCH 23/23] scripts: imx: Generate signed images with imx-image Sascha Hauer
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:44 UTC (permalink / raw)
  To: Barebox List

This is the first step to support creating signed images directly
with the imx-image tool. i.MX images must be signed using the Freescale
CST tool. CST needs informations already present in the imx-image tool,
so it's convenient to call CST directly from imx-image.
CST takes CSF files (Command Sequence Files) as input. This patch
supports generating CSF files from the imx-image configuration file.
This adds three new commands to the config file:

hab <str>:  All options to the hab command are directly passed through to
            the CSF.
hab_blocks: This generates the "Blocks =" line in the CSF. This is the
            place where the CSF needs information which is contained in
	    the imx-image tool: The image size, the image filename and
	    the load address.
super_root_key <file>: For HABv3 the super root key hash is needed in
                       the i.MX flash header. This command is used to
		       specify the path to the super root key. Needed
		       for HABv3 only.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 scripts/imx/imx.h |   2 +
 2 files changed, 109 insertions(+), 1 deletion(-)

diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index 79abed2..583cb2c 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -16,6 +16,7 @@
  *
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -23,7 +24,7 @@
 #include <errno.h>
 #include "imx.h"
 
-#define MAXARGS 5
+#define MAXARGS 32
 
 static int parse_line(char *line, char *argv[])
 {
@@ -230,6 +231,102 @@ static int do_soc(struct config_data *data, int argc, char *argv[])
 	return -EINVAL;
 }
 
+static int hab_add_str(struct config_data *data, const char *str)
+{
+	int len = strlen(str);
+
+	if (data->csf_space < len)
+		return -ENOMEM;
+
+	strcat(data->csf, str);
+
+	data->csf_space -= len;
+
+	return 0;
+}
+
+static int do_hab(struct config_data *data, int argc, char *argv[])
+{
+	int i, ret;
+
+	if (!data->csf) {
+		data->csf_space = 0x10000;
+
+		data->csf = malloc(data->csf_space + 1);
+		if (!data->csf)
+			return -ENOMEM;
+	}
+
+	for (i = 1; i < argc; i++) {
+		ret = hab_add_str(data, argv[i]);
+		if (ret)
+			return ret;
+
+		ret = hab_add_str(data, " ");
+		if (ret)
+			return ret;
+	}
+
+	ret = hab_add_str(data, "\n");
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
+{
+	char *str;
+	int ret;
+
+	if (!data->csf)
+		return -EINVAL;
+
+	ret = asprintf(&str, "Blocks = 0x%08x 0 %d \"%s\"\n",
+		       data->image_load_addr,
+		       data->load_size, data->outfile);
+	if (ret < 0)
+		return -ENOMEM;
+
+	ret = hab_add_str(data, str);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int do_super_root_key(struct config_data *data, int argc, char *argv[])
+{
+	int len;
+	char *srkfile;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: super_root_key <keyfile>\n");
+		return -EINVAL;
+	}
+
+	if (data->cpu_type != 35 && data->cpu_type != 25) {
+		fprintf(stderr, "Warning: The super_root_key command is meaningless "
+			"on non HABv3 based SoCs\n");
+		return 0;
+	}
+
+	srkfile = argv[1];
+
+	if (*srkfile == '"')
+		srkfile++;
+
+	data->srkfile = strdup(srkfile);
+	if (!data->srkfile)
+		return -ENOMEM;
+
+	len = strlen(data->srkfile);
+	if (data->srkfile[len - 1] == '"')
+		data->srkfile[len - 1] = 0;
+
+	return 0;
+}
+
 struct command cmds[] = {
 	{
 		.name = "wm",
@@ -246,6 +343,15 @@ struct command cmds[] = {
 	}, {
 		.name = "soc",
 		.parse = do_soc,
+	}, {
+		.name = "hab",
+		.parse = do_hab,
+	}, {
+		.name = "hab_blocks",
+		.parse = do_hab_blocks,
+	}, {
+		.name = "super_root_key",
+		.parse = do_super_root_key,
 	},
 };
 
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 1ef4ac6..0b1d234 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -67,6 +67,8 @@ struct config_data {
 	int cpu_type;
 	int (*check)(struct config_data *data, uint32_t cmd, uint32_t addr, uint32_t mask);
 	int (*write_mem)(struct config_data *data, uint32_t addr, uint32_t val, int width);
+	int csf_space;
+	char *csf;
 };
 
 int parse_config(struct config_data *data, const char *filename);
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 22/23] scripts: imx: Allow to create signed images
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (20 preceding siblings ...)
  2016-01-29 10:44 ` [PATCH 21/23] scripts: imx: Create CSF files from imx config file Sascha Hauer
@ 2016-01-29 10:44 ` Sascha Hauer
  2016-01-29 10:44 ` [PATCH 23/23] scripts: imx: Generate signed images with imx-image Sascha Hauer
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:44 UTC (permalink / raw)
  To: Barebox List

This patch allows to call CST directly from imx-image to create signed
images. CST is called whenever the config file contains the hab <str>
commands which means a CSF is generated.
Calling CST requires some quirks. First of all CST returns successfully
whenever a CSF exists, no matter is the CSF actually contains something
sensible or not. So to detect if CST has been called successfully we
have to check if it generated output, not if it returned successfully.
Then CST uses csfsig.bin as a temporary file which breaks when the tool
is called multiple times at once, something which often happens in
parallel builds. We therefore have to lock accesses to this file using
flock().

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 175 +++++++++++++++++++++++++++++++++++++++++-------
 scripts/imx/imx.c       |   3 +
 scripts/imx/imx.h       |   2 +
 3 files changed, 157 insertions(+), 23 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 79f644d..1a771c4 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -15,6 +15,7 @@
  * GNU General Public License for more details.
  *
  */
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <unistd.h>
 #include <getopt.h>
@@ -26,7 +27,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <endian.h>
-
+#include <sys/file.h>
 #include "imx.h"
 
 #include <include/filetype.h>
@@ -34,13 +35,11 @@
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 
 #define MAX_DCD 1024
-#define HEADER_LEN 0x1000	/* length of the blank area + IVT + DCD */
 #define CSF_LEN 0x2000		/* length of the CSF (needed for HAB) */
 
 static uint32_t dcdtable[MAX_DCD];
 static int curdcd;
 static int add_barebox_header;
-static int prepare_sign;
 
 /*
  * ============================================================================
@@ -230,6 +229,11 @@ static int add_header_v1(struct config_data *data, void *buf)
 
 	buf += dcdsize;
 
+	if (data->csf) {
+		hdr->app_code_csf = loadaddr + imagesize;
+		imagesize += CSF_LEN;
+	}
+
 	*(uint32_t *)buf = imagesize;
 
 	return 0;
@@ -282,7 +286,7 @@ static int add_header_v2(struct config_data *data, void *buf)
 	hdr->boot_data.start	= loadaddr;
 	hdr->boot_data.size	= imagesize;
 
-	if (prepare_sign) {
+	if (data->csf) {
 		hdr->csf = loadaddr + imagesize;
 		hdr->boot_data.size += CSF_LEN;
 	}
@@ -310,7 +314,6 @@ static void usage(const char *prgname)
 		"-b           add barebox header to image. If used, barebox recognizes\n"
 		"             the image as regular barebox image which can be used as\n"
 		"             second stage image\n"
-		"-p           prepare image for signing\n"
 		"-h           this help\n", prgname);
 	exit(1);
 }
@@ -440,6 +443,132 @@ static int write_mem(struct config_data *data, uint32_t addr, uint32_t val, int
 	}
 }
 
+/*
+ * This uses the Freescale Code Signing Tool (CST) to sign the image.
+ * The cst is expected to be executable as 'cst' or if exists, the content
+ * of the environment variable 'CST' is used.
+ */
+static int hab_sign(struct config_data *data)
+{
+	int fd, outfd, ret, lockfd;
+	char *csffile, *command;
+	struct stat s;
+	char *cst;
+	void *buf;
+
+	cst = getenv("CST");
+	if (!cst)
+		cst = "cst";
+
+	ret = asprintf(&csffile, "%s.csfbin", data->outfile);
+	if (ret < 0)
+		exit(1);
+
+	ret = stat(csffile, &s);
+	if (!ret) {
+		if (S_ISREG(s.st_mode)) {
+			ret = unlink(csffile);
+			if (ret) {
+				fprintf(stderr, "Cannot remove %s: %s\n",
+					csffile, strerror(errno));
+				return -errno;
+			}
+		} else {
+			fprintf(stderr, "%s exists and is no regular file\n",
+				csffile);
+			return -EINVAL;
+		}
+	}
+
+	ret = asprintf(&command, "%s -o %s", cst, csffile);
+	if (ret < 0)
+		return -ENOMEM;
+
+	/*
+	 * The cst uses "csfsig.bin" as temporary file. This of course breaks when it's
+	 * called multiple times as often happens with parallel builds. Until cst learns
+	 * how to properly create temporary files without races lock accesses to this
+	 * file.
+	 */
+	lockfd = open("csfsig.bin", O_CREAT, S_IRWXU);
+	if (lockfd < 0) {
+		fprintf(stderr, "Cannot open csfsig.bin: %s\n", strerror(errno));
+		return -errno;
+	}
+
+	ret = flock(lockfd, LOCK_EX);
+	if (ret) {
+		fprintf(stderr, "Cannot lock csfsig.bin: %s\n", strerror(errno));
+		return -errno;
+	}
+
+	FILE *f = popen(command, "w");
+	if (!f) {
+		perror("popen");
+		return -errno;
+	}
+
+	fwrite(data->csf, 1, strlen(data->csf) + 1, f);
+
+	pclose(f);
+
+	flock(lockfd, LOCK_UN);
+	close(lockfd);
+
+	/*
+	 * the Freescale code signing tool doesn't fail if there
+	 * are errors in the command sequence file, it just doesn't
+	 * produce any output, so we have to check for existence of
+	 * the output file rather than checking the return value of
+	 * the cst call.
+	 */
+	fd = open(csffile, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "Failed to open %s: %s\n", csffile, strerror(errno));
+		fprintf(stderr, "%s failed\n", cst);
+		return -errno;
+	}
+
+	ret = fstat(fd, &s);
+	if (ret < 0) {
+		fprintf(stderr, "stat failed: %s\n", strerror(errno));
+		return -errno;
+	}
+
+	buf = malloc(CSF_LEN);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0x5a, CSF_LEN);
+
+	if (s.st_size > CSF_LEN) {
+		fprintf(stderr, "CSF file size exceeds maximum CSF len of %d bytes\n",
+			CSF_LEN);
+	}
+
+	ret = xread(fd, buf, s.st_size);
+	if (ret < 0) {
+		fprintf(stderr, "read failed: %s\n", strerror(errno));
+		return -errno;
+	}
+
+	outfd = open(data->outfile, O_WRONLY | O_APPEND);
+
+	ret = xwrite(outfd, buf, CSF_LEN);
+	if (ret < 0) {
+		fprintf(stderr, "write failed: %s\n", strerror(errno));
+		return -errno;
+	}
+
+	ret = close(outfd);
+	if (ret) {
+		perror("close");
+		exit(1);
+	}
+
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	int opt, ret;
@@ -458,7 +587,7 @@ int main(int argc, char *argv[])
 		.check = check,
 	};
 
-	while ((opt = getopt(argc, argv, "c:hf:o:bdp")) != -1) {
+	while ((opt = getopt(argc, argv, "c:hf:o:bd")) != -1) {
 		switch (opt) {
 		case 'c':
 			configfile = optarg;
@@ -475,9 +604,6 @@ int main(int argc, char *argv[])
 		case 'd':
 			dcd_only = 1;
 			break;
-		case 'p':
-			prepare_sign = 1;
-			break;
 		case 'h':
 			usage(argv[0]);
 		default:
@@ -510,6 +636,16 @@ int main(int argc, char *argv[])
 		data.image_size = s.st_size;
 	}
 
+	/*
+	 * Add HEADER_LEN to the image size for the blank aera + IVT + DCD.
+	 * Align up to a 4k boundary, because:
+	 * - at least i.MX5 NAND boot only reads full NAND pages and misses the
+	 *   last partial NAND page.
+	 * - i.MX6 SPI NOR boot corrupts the last few bytes of an image loaded
+	 *   in ver funy ways when the image size is not 4 byte aligned
+	 */
+	data.load_size = roundup(data.image_size + HEADER_LEN, 0x1000);
+
 	ret = parse_config(&data, configfile);
 	if (ret)
 		exit(1);
@@ -539,19 +675,6 @@ int main(int argc, char *argv[])
 		exit (0);
 	}
 
-	/*
-	 * Add HEADER_LEN to the image size for the blank aera + IVT + DCD.
-	 * Align up to a 4k boundary, because:
-	 * - at least i.MX5 NAND boot only reads full NAND pages and misses the
-	 *   last partial NAND page.
-	 * - i.MX6 SPI NOR boot corrupts the last few bytes of an image loaded
-	 *   in ver funy ways when the image size is not 4 byte aligned
-	 */
-	data.load_size = roundup(data.image_size + HEADER_LEN, 0x1000);
-
-	if (data.cpu_type == 35)
-		data.load_size += HEADER_LEN;
-
 	switch (data.header_version) {
 	case 1:
 		add_header_v1(&data, buf);
@@ -617,7 +740,7 @@ int main(int argc, char *argv[])
 
 	/* pad until next 4k boundary */
 	now = 4096 - (insize % 4096);
-	if (prepare_sign && now) {
+	if (data.csf && now) {
 		memset(buf, 0x5a, now);
 
 		ret = xwrite(outfd, buf, now);
@@ -633,5 +756,11 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	if (data.csf) {
+		ret = hab_sign(&data);
+		if (ret)
+			exit(1);
+	}
+
 	exit(0);
 }
diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index 583cb2c..cebc3d6 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -228,6 +228,9 @@ static int do_soc(struct config_data *data, int argc, char *argv[])
 		fprintf(stderr, "%s ", socs[i].name);
 	fprintf(stderr, "\n");
 
+	if (data->cpu_type == 35)
+		data->load_size += HEADER_LEN;
+
 	return -EINVAL;
 }
 
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 0b1d234..69fe943 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -3,6 +3,8 @@
 #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
 #endif
 
+#define HEADER_LEN 0x1000	/* length of the blank area + IVT + DCD */
+
 /*
  * ============================================================================
  * i.MX flash header v1 handling. Found on i.MX35 and i.MX51
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 23/23] scripts: imx: Generate signed images with imx-image
  2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
                   ` (21 preceding siblings ...)
  2016-01-29 10:44 ` [PATCH 22/23] scripts: imx: Allow to create signed images Sascha Hauer
@ 2016-01-29 10:44 ` Sascha Hauer
  22 siblings, 0 replies; 27+ messages in thread
From: Sascha Hauer @ 2016-01-29 10:44 UTC (permalink / raw)
  To: Barebox List

The imx-image tool can now generate signed images itself, so we can
switch to this mechanism:

- Move the CSF templates to header files which can be included by the
  flash config files
- remove images/Makefile.imxhabv4 which is no longer necessary.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 .../arm/mach-imx/include/mach/habv3-imx25-gencsf.h | 43 +++++++++++++++++++
 arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h | 44 ++++++++++++++++++++
 images/Makefile                                    |  1 -
 images/Makefile.imxhabv4                           | 48 ----------------------
 scripts/habv4/gencsf.sh                            | 47 ---------------------
 scripts/habv4/habv4-imx28.csf.in                   | 33 ---------------
 scripts/habv4/habv4-imx6.csf.in                    | 37 -----------------
 7 files changed, 87 insertions(+), 166 deletions(-)
 create mode 100644 arch/arm/mach-imx/include/mach/habv3-imx25-gencsf.h
 create mode 100644 arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h
 delete mode 100644 images/Makefile.imxhabv4
 delete mode 100755 scripts/habv4/gencsf.sh
 delete mode 100644 scripts/habv4/habv4-imx28.csf.in
 delete mode 100644 scripts/habv4/habv4-imx6.csf.in

diff --git a/arch/arm/mach-imx/include/mach/habv3-imx25-gencsf.h b/arch/arm/mach-imx/include/mach/habv3-imx25-gencsf.h
new file mode 100644
index 0000000..4b81d49
--- /dev/null
+++ b/arch/arm/mach-imx/include/mach/habv3-imx25-gencsf.h
@@ -0,0 +1,43 @@
+/*
+ * This snippet can be included from a i.MX flash header configuration
+ * file for generating signed images. The necessary keys/certificates
+ * are expected in these config variables:
+ *
+ * CONFIG_HABV3_SRK_PEM
+ * CONFIG_HABV3_SRK_PEM
+ * CONFIG_HABV3_IMG_CRT_PEM
+ */
+super_root_key CONFIG_HABV3_SRK_PEM
+
+hab [Header]
+hab Version = 3.0
+hab Security Configuration = Engineering
+hab Hash Algorithm = SHA256
+hab Engine = RTIC
+hab Certificate Format = WTLS
+hab Signature Format = PKCS1
+hab UID = Generic
+hab Code = 0x00
+
+hab [Install SRK]
+hab File = "not-used"
+
+hab [Install CSFK]
+hab File = CONFIG_HABV3_CSF_CRT_DER
+
+hab [Authenticate CSF]
+/* below is the command that unlock the access to the DryIce registers */
+
+hab [Write Data]
+hab Width = 4
+hab Address Data = 0x53FFC03C 0xCA693569
+
+hab [Install Key]
+hab Verification index = 1
+hab Target index = 2
+hab File = CONFIG_HABV3_IMG_CRT_DER
+
+hab [Authenticate Data]
+hab Verification index = 2
+
+hab_blocks
diff --git a/arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h b/arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h
new file mode 100644
index 0000000..1a143a8
--- /dev/null
+++ b/arch/arm/mach-imx/include/mach/habv4-imx6-gencsf.h
@@ -0,0 +1,44 @@
+/*
+ * This snippet can be included from a i.MX flash header configuration
+ * file for generating signed images. The necessary keys/certificates
+ * are expected in these config variables:
+ *
+ * CONFIG_HABV4_TABLE_BIN
+ * CONFIG_HABV4_CSF_CRT_PEM
+ * CONFIG_HABV4_IMG_CRT_PEM
+ */
+
+hab [Header]
+hab Version = 4.1
+hab Hash Algorithm = sha256
+hab Engine Configuration = 0
+hab Certificate Format = X509
+hab Signature Format = CMS
+hab Engine = CAAM
+
+hab [Install SRK]
+hab File = CONFIG_HABV4_TABLE_BIN
+hab # SRK index within SRK-Table 0..3
+hab Source index = 0
+
+hab [Install CSFK]
+hab File = CONFIG_HABV4_CSF_CRT_PEM
+
+hab [Authenticate CSF]
+
+hab [Unlock]
+hab Engine = CAAM
+hab Features = RNG
+
+hab [Install Key]
+/* verification key index in key store (0, 2...5) */
+hab Verification index = 0
+/* target key index in key store (2...5) */
+hab Target index = 2
+hab File = CONFIG_HABV4_IMG_CRT_PEM
+
+hab [Authenticate Data]
+/* verification key index in key store (2...5) */
+hab Verification index = 2
+
+hab_blocks
\ No newline at end of file
diff --git a/images/Makefile b/images/Makefile
index a5f589b..f2dd9ab 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -104,7 +104,6 @@ objboard = $(objtree)/arch/$(ARCH)/boards
 
 include $(srctree)/images/Makefile.am33xx
 include $(srctree)/images/Makefile.imx
-include $(srctree)/images/Makefile.imxhabv4
 include $(srctree)/images/Makefile.mvebu
 include $(srctree)/images/Makefile.mxs
 include $(srctree)/images/Makefile.omap3
diff --git a/images/Makefile.imxhabv4 b/images/Makefile.imxhabv4
deleted file mode 100644
index 9eb9538..0000000
--- a/images/Makefile.imxhabv4
+++ /dev/null
@@ -1,48 +0,0 @@
-# -*-makefile-*-
-#
-# barebox image generation Makefile for HABv4 images
-#
-
-# default csf templates
-havb4_imx6csf = $(srctree)/scripts/habv4/habv4-imx6.csf.in
-habv4_imx2csf = $(srctree)/scripts/habv4/habv4-imx28.csf.in
-
-# %.imximg.prep - Convert in i.MX image, with preparation for signature
-# ----------------------------------------------------------------
-quiet_cmd_imx_prep_image = IMX-PREP-IMG $@
-      cmd_imx_prep_image = $(CPP) $(imxcfg_cpp_flags) -o $(imximg-tmp) $(word 2,$^) ; \
-			   $< -o $@ -b -c $(imximg-tmp) -p -f $(word 3,$^)
-
-.SECONDEXPANSION:
-$(obj)/%.imximg.prep: $(objtree)/scripts/imx/imx-image $$(CFG_%.imximg) $(obj)/%
-	$(call if_changed,imx_prep_image)
-
-# %.habv4.csf - create Command Sequence File from template
-# ----------------------------------------------------------------
-quiet_cmd_csf = CSF     $@
-      cmd_csf = TABLE_BIN=$(CONFIG_HABV4_TABLE_BIN) \
-		CSF_CRT_PEM=$(CONFIG_HABV4_CSF_CRT_PEM) \
-		IMG_CRT_PEM=$(CONFIG_HABV4_IMG_CRT_PEM) \
-		$< -f $(word 2,$^) -c $(word 3,$^) -i $(word 4,$^) -o $@
-
-.SECONDEXPANSION:
-$(obj)/%.habv4.csf: $(srctree)/scripts/habv4/gencsf.sh $(obj)/%.prep $$(CFG_%) $$(CSF_%)
-	$(call if_changed,csf)
-
-# %.habv4.sig - create signature and pad to 0x2000
-# ----------------------------------------------------------------
-CST = cst
-quiet_cmd_habv4_sig = HAB4SIG $@
-      cmd_habv4_sig = $(CST) -o $(imximg-tmp) < $(word 2,$^) > /dev/null; \
-		      $(OBJCOPY) -I binary -O binary --pad-to 0x2000 --gap-fill=0x5a $(imximg-tmp) $@
-
-$(obj)/%.habv4.sig: $(obj)/%.prep $(obj)/%.habv4.csf
-	$(call if_changed,habv4_sig)
-
-# %.imximg.signed - concatenate bootloader and signature
-# ----------------------------------------------------------------
-quiet_cmd_cat = CAT     $@
-      cmd_cat = cat $^ > $@
-
-$(obj)/%.imximg.signed: $(obj)/%.imximg.prep $(obj)/%.imximg.habv4.sig
-	$(call if_changed,cat)
diff --git a/scripts/habv4/gencsf.sh b/scripts/habv4/gencsf.sh
deleted file mode 100755
index 2c1c34a..0000000
--- a/scripts/habv4/gencsf.sh
+++ /dev/null
@@ -1,47 +0,0 @@
-#!/bin/sh
-
-set -e
-
-while getopts "f:c:i:o:" opt; do
-    case $opt in
-	f)
-	    file=$OPTARG
-	    ;;
-	c)
-	    cfg=$OPTARG
-	    ;;
-	i)
-	    in=$OPTARG
-	    ;;
-	o)
-	    out=$OPTARG
-	    ;;
-	\?)
-	    echo "Invalid option: -$OPTARG" >&2
-	    exit 1
-	;;
-    esac
-done
-
-if [ ! -e $file -o ! -e $cfg -o ! -e $in ]; then
-    echo "file not found!"
-    exit 1
-fi
-
-#
-# extract and set as shell vars:
-# loadaddr=
-# dcdofs=
-#
-eval $(sed -n -e "s/^[[:space:]]*\(loadaddr\|dcdofs\)[[:space:]]*\(0x[0-9]*\)/\1=\2/p" $cfg)
-
-length=$(stat -c '%s' $file)
-
-sed -e "s:@TABLE_BIN@:$TABLE_BIN:" \
-    -e "s:@CSF_CRT_PEM@:$CSF_CRT_PEM:" \
-    -e "s:@IMG_CRT_PEM@:$IMG_CRT_PEM:" \
-    -e "s:@LOADADDR@:$loadaddr:" \
-    -e "s:@OFFSET@:0:" \
-    -e "s:@LENGTH@:$length:" \
-    -e "s:@FILE@:$file:" \
-    $in > $out
diff --git a/scripts/habv4/habv4-imx28.csf.in b/scripts/habv4/habv4-imx28.csf.in
deleted file mode 100644
index 5efd25b..0000000
--- a/scripts/habv4/habv4-imx28.csf.in
+++ /dev/null
@@ -1,33 +0,0 @@
-[Header]
-Version = 4.0
-Hash Algorithm = sha256
-Engine Configuration = 0
-Certificate Format = X509
-Signature Format = CMS
-Engine = DCP
-
-[Install SRK]
-File = "@TABLE_BIN@"
-# SRK index within SRK-Table 0..3
-Source index = 0
-
-[Install CSFK]
-File = "@CSF_CRT_PEM@"
-
-[Authenticate CSF]
-
-[Install Key]
-# verification key index in key store (0, 2...5)
-Verification index = 0
-# target key index in key store (2...5)
-Target index = 2
-File = "@IMG_CRT_PEM@"
-
-[Authenticate Data]
-# verification key index in key store (2...5)
-Verification index = 2
-# "starting load address in memory"
-# "starting offset within the source file"
-# "length (in bytes)"
-# "file (binary)"
-Blocks = @LOADADDR@ @OFFSET@ @LENGTH@ "@FILE@"
diff --git a/scripts/habv4/habv4-imx6.csf.in b/scripts/habv4/habv4-imx6.csf.in
deleted file mode 100644
index 11a5db9..0000000
--- a/scripts/habv4/habv4-imx6.csf.in
+++ /dev/null
@@ -1,37 +0,0 @@
-[Header]
-Version = 4.1
-Hash Algorithm = sha256
-Engine Configuration = 0
-Certificate Format = X509
-Signature Format = CMS
-Engine = CAAM
-
-[Install SRK]
-File = "@TABLE_BIN@"
-# SRK index within SRK-Table 0..3
-Source index = 0
-
-[Install CSFK]
-File = "@CSF_CRT_PEM@"
-
-[Authenticate CSF]
-
-[Unlock]
-Engine = CAAM
-Features = RNG
-
-[Install Key]
-# verification key index in key store (0, 2...5)
-Verification index = 0
-# target key index in key store (2...5)
-Target index = 2
-File = "@IMG_CRT_PEM@"
-
-[Authenticate Data]
-# verification key index in key store (2...5)
-Verification index = 2
-# "starting load address in memory"
-# "starting offset within the source file"
-# "length (in bytes)"
-# "file (binary)"
-Blocks = @LOADADDR@ @OFFSET@ @LENGTH@ "@FILE@"
-- 
2.7.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH 13/23] scripts: imx: move macro definitions to common header file
  2016-01-29 10:43 ` [PATCH 13/23] scripts: imx: move macro definitions to common header file Sascha Hauer
@ 2016-01-29 18:04   ` Sam Ravnborg
  2016-02-01  9:18     ` Sascha Hauer
  0 siblings, 1 reply; 27+ messages in thread
From: Sam Ravnborg @ 2016-01-29 18:04 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On Fri, Jan 29, 2016 at 11:43:53AM +0100, Sascha Hauer wrote:
> ARRAY_SIZE and offsetof are used by both imx-usb-loader and imx-image.
> Move their definitions to the common header file.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  scripts/imx/imx-image.c      | 2 --
>  scripts/imx/imx-usb-loader.c | 5 -----
>  scripts/imx/imx.h            | 5 +++++
>  3 files changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
> index e986545..1d23e11 100644
> --- a/scripts/imx/imx.h
> +++ b/scripts/imx/imx.h
> @@ -1,3 +1,8 @@
> +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
This macro is already defined in include/linux/kernel.h,
so it would be better to use this version.


> +#ifndef offsetof
> +#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
> +#endif
Likewise for this - use the version in include/linux/stddef.h

Only from manual inspection - I did not try to replace the
definitions to check for any incompatibilities.

	Sam

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH 13/23] scripts: imx: move macro definitions to common header file
  2016-01-29 18:04   ` Sam Ravnborg
@ 2016-02-01  9:18     ` Sascha Hauer
  2016-02-01 10:06       ` Sam Ravnborg
  0 siblings, 1 reply; 27+ messages in thread
From: Sascha Hauer @ 2016-02-01  9:18 UTC (permalink / raw)
  To: Sam Ravnborg; +Cc: Barebox List

On Fri, Jan 29, 2016 at 07:04:38PM +0100, Sam Ravnborg wrote:
> On Fri, Jan 29, 2016 at 11:43:53AM +0100, Sascha Hauer wrote:
> > ARRAY_SIZE and offsetof are used by both imx-usb-loader and imx-image.
> > Move their definitions to the common header file.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  scripts/imx/imx-image.c      | 2 --
> >  scripts/imx/imx-usb-loader.c | 5 -----
> >  scripts/imx/imx.h            | 5 +++++
> >  3 files changed, 5 insertions(+), 7 deletions(-)
> > 
> > diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
> > index e986545..1d23e11 100644
> > --- a/scripts/imx/imx.h
> > +++ b/scripts/imx/imx.h
> > @@ -1,3 +1,8 @@
> > +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
> This macro is already defined in include/linux/kernel.h,
> so it would be better to use this version.

It's defined in the barebox version of this file, but not in
/usr/include/linux/kernel.h which would be used for compiling programs
for the host. Adding a -I$(srctree)/include to the host compiler options
is also something we don't want.
What other options do we have to avoid duplicating these over and over
again? Maybe adding a scripts/include directory which contains such
useful defines? I just found tools/include in the Linux Kernel which
already has a collection of such defines. Maybe this is a good starting
point.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH 13/23] scripts: imx: move macro definitions to common header file
  2016-02-01  9:18     ` Sascha Hauer
@ 2016-02-01 10:06       ` Sam Ravnborg
  0 siblings, 0 replies; 27+ messages in thread
From: Sam Ravnborg @ 2016-02-01 10:06 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On Mon, Feb 01, 2016 at 10:18:10AM +0100, Sascha Hauer wrote:
> On Fri, Jan 29, 2016 at 07:04:38PM +0100, Sam Ravnborg wrote:
> > On Fri, Jan 29, 2016 at 11:43:53AM +0100, Sascha Hauer wrote:
> > > ARRAY_SIZE and offsetof are used by both imx-usb-loader and imx-image.
> > > Move their definitions to the common header file.
> > > 
> > > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > > ---
> > >  scripts/imx/imx-image.c      | 2 --
> > >  scripts/imx/imx-usb-loader.c | 5 -----
> > >  scripts/imx/imx.h            | 5 +++++
> > >  3 files changed, 5 insertions(+), 7 deletions(-)
> > > 
> > > diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
> > > index e986545..1d23e11 100644
> > > --- a/scripts/imx/imx.h
> > > +++ b/scripts/imx/imx.h
> > > @@ -1,3 +1,8 @@
> > > +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
> > This macro is already defined in include/linux/kernel.h,
> > so it would be better to use this version.
> 
> It's defined in the barebox version of this file, but not in
> /usr/include/linux/kernel.h which would be used for compiling programs
> for the host. Adding a -I$(srctree)/include to the host compiler options
> is also something we don't want.
I my short review I did not realize this was used by host code.

> What other options do we have to avoid duplicating these over and over
> again? Maybe adding a scripts/include directory which contains such
> useful defines? I just found tools/include in the Linux Kernel which
> already has a collection of such defines. Maybe this is a good starting
> point.
In the kernel we have /usr/include which contains exported (ABI) headers,
and tools/include for diverse host tools.
IMO tools/include would be a nice place to add ARRAY_SIZE and similar
stuff that can be shared between host tools.
And then locate all non-trivial host related tools in tools/xxx.

The build infrastructure should be updated so the headers are picked
up without adding any -I flags.

	Sam

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2016-02-01 10:07 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-29 10:43 i.MX HABv4 rework and HABv3 support Sascha Hauer
2016-01-29 10:43 ` [PATCH 01/23] ARM: i.MX: Add HABv3 Kconfig variables Sascha Hauer
2016-01-29 10:43 ` [PATCH 02/23] imx: hab: rename driver dir to hab/ Sascha Hauer
2016-01-29 10:43 ` [PATCH 03/23] hab: Add HABv3 status report function Sascha Hauer
2016-01-29 10:43 ` [PATCH 04/23] scripts: imx-usb-loader: Make readonly arguments const Sascha Hauer
2016-01-29 10:43 ` [PATCH 05/23] scripts: imx-usb-loader: Move definitions up Sascha Hauer
2016-01-29 10:43 ` [PATCH 06/23] scripts: imx-image: Allow dcd offset 0x0 Sascha Hauer
2016-01-29 10:43 ` [PATCH 07/23] scripts: imx-usb-loader: fully read images into memory Sascha Hauer
2016-01-29 10:43 ` [PATCH 08/23] scripts: imx-usb-loader: Move load_file up Sascha Hauer
2016-01-29 10:43 ` [PATCH 09/23] scripts: imx: Consolidate flash headers in imx tools Sascha Hauer
2016-01-29 10:43 ` [PATCH 10/23] scripts: imx-image: Add context struct to config parsers Sascha Hauer
2016-01-29 10:43 ` [PATCH 11/23] scripts: imx-image: move write_mem to context data Sascha Hauer
2016-01-29 10:43 ` [PATCH 12/23] scripts: imx-image: move check " Sascha Hauer
2016-01-29 10:43 ` [PATCH 13/23] scripts: imx: move macro definitions to common header file Sascha Hauer
2016-01-29 18:04   ` Sam Ravnborg
2016-02-01  9:18     ` Sascha Hauer
2016-02-01 10:06       ` Sam Ravnborg
2016-01-29 10:43 ` [PATCH 14/23] scripts: imx: move config file parser to separate file Sascha Hauer
2016-01-29 10:43 ` [PATCH 15/23] scripts: imx: make libusb variables global Sascha Hauer
2016-01-29 10:43 ` [PATCH 16/23] scripts: imx-usb-loader: Add -s and -i options Sascha Hauer
2016-01-29 10:43 ` [PATCH 17/23] scripts: imx: Drop double check Sascha Hauer
2016-01-29 10:43 ` [PATCH 18/23] scripts: imx-image: move more variables to context data Sascha Hauer
2016-01-29 10:43 ` [PATCH 19/23] scripts: imx-image: pass config data to add_header_* Sascha Hauer
2016-01-29 10:44 ` [PATCH 20/23] scripts: imx-image: Support adding a Super Root Key to the image Sascha Hauer
2016-01-29 10:44 ` [PATCH 21/23] scripts: imx: Create CSF files from imx config file Sascha Hauer
2016-01-29 10:44 ` [PATCH 22/23] scripts: imx: Allow to create signed images Sascha Hauer
2016-01-29 10:44 ` [PATCH 23/23] scripts: imx: Generate signed images with imx-image Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox