--- /dev/null
+/*
+ * Copyright (C) 2017 O.S. Systems Software LTDA.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+/*
+ * This header helps to add support for UpdateHub in U-Boot. It
+ * assumes your U-Boot has support for bootcount (either using a SoC
+ * feature or the environment fallback).
+ */
+
+#ifndef __UPDATEHUB_H
+#define __UPDATEHUB_H
+
+#define CONFIG_BOOTCOUNT_LIMIT
+
+#ifdef CONFIG_ENV_IS_NOWHERE
+# error Environment saving is required for UpdateHub use
+#endif
+
+#define UPDATEHUB_ENV \
+ "upgrade_available=0\0" \
+ "bootlimit=1\0" \
+ "bootcount=0\0" \
+ \
+ "updatehub_find_root_a=" UPDATEHUB_FIND_ROOT_A ";\0" \
+ "updatehub_load_os_a=" UPDATEHUB_LOAD_OS_A ";\0" \
+ \
+ "updatehub_find_root_b=" UPDATEHUB_FIND_ROOT_B ";\0" \
+ "updatehub_load_os_b=" UPDATEHUB_LOAD_OS_B ";\0" \
+ \
+ "updatehub_bootargs=setenv bootargs " UPDATEHUB_BOOTARGS ";\0" \
+ \
+ "updatehub_altbootcmd=" \
+ "if test ${updatehub_active} = 0; then " \
+ "echo Bootcount limit reached. Reverting to image B; " \
+ "setenv updatehub_active 1; " \
+ "else " \
+ "echo Bootcount limit reached. Reverting to image A; " \
+ "setenv updatehub_active 1; " \
+ "fi; " \
+ "setenv upgrade_available 0; " \
+ "saveenv;\0" \
+ "altbootcmd=run updatehub_altbootcmd bootcmd;\0"
+
+#undef CONFIG_BOOTCOMMAND
+#define CONFIG_BOOTCOMMAND \
+ "if test -z \"${updatehub_active}\"; then " \
+ "echo Ensuring environment is accessible in Linux...; " \
+ "setenv updatehub_active 0; " \
+ "saveenv; " \
+ "fi; " \
+ "if test ${updatehub_active} = 0; then " \
+ "echo Loading system from A; " \
+ "run updatehub_find_root_a updatehub_load_os_a updatehub_bootargs; " \
+ "else " \
+ "if test ${updatehub_active} = 1; then " \
+ "echo Loading system from B; " \
+ "run updatehub_find_root_b updatehub_load_os_b updatehub_bootargs; " \
+ "else " \
+ "echo Invalid updatehub_active value, resetting it; " \
+ "setenv updatehub_active 0; " \
+ "saveenv; " \
+ "reset; " \
+ "fi; " \
+ "fi;" \
+ UPDATEHUB_BOOTCMD
+
+#endif /* __UPDATEHUB_H */