Platform: Check that ProgramData programmed all the data

ProgramData returns the number of data items programmed or a status
error code. Status error codes may be 0 to indicate success or
negative to indicate failure.

Presumably the API may return the number of bytes written so that
large writes can be retried on partial write failure without having to
re-write all of the data.

In this patch we add check for a partial write.

Change-Id: I434c7ac358237c72ea3c8f542e7c45783566b4f1
Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
diff --git a/platform/ext/common/template/flash_otp_nv_counters_backend.c b/platform/ext/common/template/flash_otp_nv_counters_backend.c
index e9fdff6..4b77cae 100644
--- a/platform/ext/common/template/flash_otp_nv_counters_backend.c
+++ b/platform/ext/common/template/flash_otp_nv_counters_backend.c
@@ -357,13 +357,23 @@
             input_idx += input_copy_size;
         }
 
+        uint32_t num_items = copy_size / data_width;
+
         err = (enum tfm_plat_err_t)OTP_NV_COUNTERS_FLASH_DEV.ProgramData(TFM_OTP_NV_COUNTERS_AREA_ADDR + idx,
                                                     block,
-                                                    copy_size / data_width);
+                                                    num_items);
         if (err < 0) {
             return TFM_PLAT_ERR_SYSTEM_ERR;
         }
 
+        /* When err is positive it contains the number of data items
+         * successfully programmed. Check that every byte of
+         * programming succeeded.
+         */
+        if (err > 0 && err != num_items) {
+            return TFM_PLAT_ERR_SYSTEM_ERR;
+        }
+
         if (idx >= offset && idx < offset + cnt) {
             memset(block, 0, sizeof(block));
         }