stm32mp1: move check_header() to common code
authorYann Gautier <yann.gautier@st.com>
Fri, 19 Apr 2019 08:48:36 +0000 (10:48 +0200)
committerYann Gautier <yann.gautier@st.com>
Mon, 2 Sep 2019 15:52:55 +0000 (17:52 +0200)
This function can be used on several stm32mp devices, it is then moved in
plat/st/common/stm32mp_common.c.

Change-Id: I862debe39604410f71a9ddc28713026362e9ecda
Signed-off-by: Yann Gautier <yann.gautier@st.com>
drivers/st/io/io_stm32image.c
plat/st/common/include/stm32mp_common.h
plat/st/common/stm32mp_common.c

index dc2977d5a62a570e0269ff71af70c743eff80ffd..971dcce533331b0706f422a3bc37d7640399e4a0 100644 (file)
@@ -242,40 +242,6 @@ static int stm32image_partition_size(io_entity_t *entity, size_t *length)
        return 0;
 }
 
-static int check_header(boot_api_image_header_t *header, uintptr_t buffer)
-{
-       uint32_t i;
-       uint32_t img_checksum = 0;
-
-       /*
-        * Check header/payload validity:
-        *      - Header magic
-        *      - Header version
-        *      - Payload checksum
-        */
-       if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) {
-               ERROR("Header magic\n");
-               return -EINVAL;
-       }
-
-       if (header->header_version != BOOT_API_HEADER_VERSION) {
-               ERROR("Header version\n");
-               return -EINVAL;
-       }
-
-       for (i = 0; i < header->image_length; i++) {
-               img_checksum += *(uint8_t *)(buffer + i);
-       }
-
-       if (header->payload_checksum != img_checksum) {
-               ERROR("Checksum: 0x%x (awaited: 0x%x)\n", img_checksum,
-                     header->payload_checksum);
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
 /* Read data from a partition */
 static int stm32image_partition_read(io_entity_t *entity, uintptr_t buffer,
                                     size_t length, size_t *length_read)
@@ -368,7 +334,7 @@ static int stm32image_partition_read(io_entity_t *entity, uintptr_t buffer,
                        continue;
                }
 
-               result = check_header(header, buffer);
+               result = stm32mp_check_header(header, buffer);
                if (result != 0) {
                        ERROR("Header check failed\n");
                        *length_read = 0;
index 3dd6c567bae71993a9acd52e862aeca2604b3a74..e20308ee2f5726ceb43e54e17d888d39cbe06564 100644 (file)
@@ -10,6 +10,8 @@
 
 #include <stdbool.h>
 
+#include <platform_def.h>
+
 #include <arch_helpers.h>
 
 /* Functions to save and get boot context address given by ROM code */
@@ -94,4 +96,12 @@ static inline bool timeout_elapsed(uint64_t expire)
        return read_cntpct_el0() > expire;
 }
 
+/*
+ * Check that the STM32 header of a .stm32 binary image is valid
+ * @param header: pointer to the stm32 image header
+ * @param buffer: address of the binary image (payload)
+ * @return: 0 on success, negative value in case of error
+ */
+int stm32mp_check_header(boot_api_image_header_t *header, uintptr_t buffer);
+
 #endif /* STM32MP_COMMON_H */
index 5428a74efe90cd9523b81c92bc7d5b11caf29ac0..afa87f4877d0f9263e4fda5df240450360416bc6 100644 (file)
@@ -5,6 +5,7 @@
  */
 
 #include <assert.h>
+#include <errno.h>
 
 #include <platform_def.h>
 
@@ -116,3 +117,37 @@ uint32_t stm32_get_gpio_bank_offset(unsigned int bank)
 
        return bank * GPIO_BANK_OFFSET;
 }
+
+int stm32mp_check_header(boot_api_image_header_t *header, uintptr_t buffer)
+{
+       uint32_t i;
+       uint32_t img_checksum = 0U;
+
+       /*
+        * Check header/payload validity:
+        *      - Header magic
+        *      - Header version
+        *      - Payload checksum
+        */
+       if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) {
+               ERROR("Header magic\n");
+               return -EINVAL;
+       }
+
+       if (header->header_version != BOOT_API_HEADER_VERSION) {
+               ERROR("Header version\n");
+               return -EINVAL;
+       }
+
+       for (i = 0U; i < header->image_length; i++) {
+               img_checksum += *(uint8_t *)(buffer + i);
+       }
+
+       if (header->payload_checksum != img_checksum) {
+               ERROR("Checksum: 0x%x (awaited: 0x%x)\n", img_checksum,
+                     header->payload_checksum);
+               return -EINVAL;
+       }
+
+       return 0;
+}