Skip to content

Instantly share code, notes, and snippets.

@stiltr
Created May 19, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stiltr/e5497774a1d5ce54afa29964a9639a72 to your computer and use it in GitHub Desktop.
Save stiltr/e5497774a1d5ce54afa29964a9639a72 to your computer and use it in GitHub Desktop.
Mender patches applied to mainline u-boot for Pi3 USB boot.
diff --git a/board/raspberrypi/rpi/rpi.c b/board/raspberrypi/rpi/rpi.c
index 45caa4d49e..99d34e8a49 100644
--- a/board/raspberrypi/rpi/rpi.c
+++ b/board/raspberrypi/rpi/rpi.c
@@ -389,9 +389,31 @@ static void set_serial_number(void)
env_set("serial#", serial_string);
}
+static void set_bootargs_orig(void)
+{
+ if (fdt_magic(fw_dtb_pointer) == FDT_MAGIC)
+ {
+ set_working_fdt_addr(fw_dtb_pointer);
+ int nodeoffset = fdt_path_offset(working_fdt, "/chosen");
+ int len;
+ const void* nodep = fdt_getprop(working_fdt, nodeoffset, "bootargs", &len);
+ if(len)
+ {
+ printf("Setting bootargs_orig from FDT\n");
+ env_set("bootargs_orig", (char*) nodep);
+ }
+ else
+ {
+ printf("Failed to get bootargs from FDT!");
+ env_set("bootargs_orig", NULL);
+ }
+ }
+}
+
int misc_init_r(void)
{
set_fdt_addr();
+ set_bootargs_orig();
set_fdtfile();
set_usbethaddr();
#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
diff --git a/configs/rpi_3_usb_defconfig b/configs/rpi_3_usb_defconfig
new file mode 100644
index 0000000000..67b8021e81
--- /dev/null
+++ b/configs/rpi_3_usb_defconfig
@@ -0,0 +1,49 @@
+CONFIG_ARM=y
+CONFIG_ARCH_CPU_INIT=y
+CONFIG_ARCH_BCM283X=y
+CONFIG_SYS_TEXT_BASE=0x00080000
+CONFIG_TARGET_RPI_3=y
+CONFIG_SYS_MALLOC_F_LEN=0x2000
+CONFIG_ENV_SIZE=0x4000
+CONFIG_NR_DRAM_BANKS=1
+CONFIG_DISTRO_DEFAULTS=y
+CONFIG_OF_BOARD_SETUP=y
+CONFIG_USE_PREBOOT=y
+CONFIG_PREBOOT="usb start"
+CONFIG_MISC_INIT_R=y
+# CONFIG_DISPLAY_CPUINFO is not set
+# CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_SYS_PROMPT="U-Boot> "
+CONFIG_CMD_GPIO=y
+CONFIG_CMD_MMC=y
+CONFIG_CMD_USB=y
+CONFIG_CMD_FS_UUID=y
+CONFIG_OF_EMBED=y
+CONFIG_DEFAULT_DEVICE_TREE="bcm2837-rpi-3-b"
+CONFIG_ENV_FAT_INTERFACE="usb"
+CONFIG_ENV_FAT_DEVICE_AND_PART="0:1"
+CONFIG_SYS_RELOC_GD_ENV_ADDR=y
+CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG=y
+CONFIG_DM_KEYBOARD=y
+CONFIG_DM_MMC=y
+CONFIG_MMC_SDHCI=y
+CONFIG_MMC_SDHCI_BCM2835=y
+CONFIG_PHYLIB=y
+CONFIG_DM_ETH=y
+CONFIG_PINCTRL=y
+# CONFIG_PINCTRL_GENERIC is not set
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
+CONFIG_USB=y
+CONFIG_DM_USB=y
+CONFIG_USB_DWC2=y
+CONFIG_USB_KEYBOARD=y
+CONFIG_USB_HOST_ETHER=y
+CONFIG_USB_ETHER_LAN78XX=y
+CONFIG_USB_ETHER_SMSC95XX=y
+CONFIG_DM_VIDEO=y
+# CONFIG_VIDEO_BPP8 is not set
+# CONFIG_VIDEO_BPP16 is not set
+CONFIG_SYS_WHITE_ON_BLACK=y
+CONFIG_CONSOLE_SCROLL_LINES=10
+CONFIG_PHYS_TO_BUS=y
+CONFIG_OF_LIBFDT_OVERLAY=y
diff --git a/include/config_mender.h b/include/config_mender.h
new file mode 100644
index 0000000000..197c5f085a
--- /dev/null
+++ b/include/config_mender.h
@@ -0,0 +1,68 @@
+/*
+ Copyright 2017 Northern.tech AS
+
+ 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.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef HEADER_CONFIG_MENDER_H
+#define HEADER_CONFIG_MENDER_H
+
+/* Avoid Mender specific code if we are just probing for configuration. */
+#ifndef MENDER_AUTO_PROBING
+
+#include <config_mender_defines.h>
+
+#ifdef CONFIG_ENV_IS_NOWHERE
+# error A CONFIG_ENV_IS_IN_<storage-type> define is required for Mender to work. For standard Mender setups this should be CONFIG_ENV_IS_IN_MMC for HD/SSD/MMC/SD storage setup, and CONFIG_ENV_IS_IN_UBI for Flash/UBI storage.
+#endif
+
+#ifndef CONFIG_BOOTCOUNT_LIMIT
+# error CONFIG_BOOTCOUNT_LIMIT is required for Mender to work. Make sure that: 1) All the instructions at docs.mender.io/devices/integrating-with-u-boot have been followed. 2) All required layers are included in bblayers.conf, including any board specific layers such as meta-mender-<board>
+#endif
+
+/* Currently Mender needs bootcount to reside in environment. */
+#ifndef CONFIG_BOOTCOUNT_ENV
+# error CONFIG_BOOTCOUNT_ENV is required for Mender to work. Make sure that: 1) All the instructions at docs.mender.io/devices/integrating-with-u-boot have been followed. 2) All required layers are included in bblayers.conf, including any board specific layers such as meta-mender-<board>
+#endif
+
+#if defined(CONFIG_ENV_OFFSET)
+# if CONFIG_ENV_OFFSET != MENDER_UBOOT_ENV_STORAGE_DEVICE_OFFSET_1
+# error CONFIG_ENV_OFFSET is not the same as MENDER_UBOOT_ENV_STORAGE_DEVICE_OFFSET_1. Either set it to the same value (check for example in the defconfig file), or make sure it is not defined at all. Make sure that: 1) All the instructions at docs.mender.io/devices/integrating-with-u-boot have been followed. 2) All required layers are included in bblayers.conf, including any board specific layers such as meta-mender-<board>
+# endif
+#else
+# define CONFIG_ENV_OFFSET MENDER_UBOOT_ENV_STORAGE_DEVICE_OFFSET_1
+#endif
+#if defined(CONFIG_ENV_OFFSET_REDUND)
+# if CONFIG_ENV_OFFSET_REDUND != MENDER_UBOOT_ENV_STORAGE_DEVICE_OFFSET_2
+# error CONFIG_ENV_OFFSET_REDUND is not the same as MENDER_ENV_OFFSET_REDUND. Either set it to the same value (check for example in the defconfig file), or make sure it is not defined at all. Make sure that: 1) All the instructions at docs.mender.io/devices/integrating-with-u-boot have been followed. 2) All required layers are included in bblayers.conf, including any board specific layers such as meta-mender-<board>
+# endif
+#else
+# define CONFIG_ENV_OFFSET_REDUND MENDER_UBOOT_ENV_STORAGE_DEVICE_OFFSET_2
+#endif
+#ifdef CONFIG_SYS_MMC_ENV_DEV
+# if CONFIG_SYS_MMC_ENV_DEV != MENDER_UBOOT_STORAGE_DEVICE
+# error CONFIG_SYS_MMC_ENV_DEV is not the same as MENDER_UBOOT_STORAGE_DEVICE. Either set it to the same value (check for example in the defconfig file), or make sure it is not defined at all. Make sure that: 1) All the instructions at docs.mender.io/devices/integrating-with-u-boot have been followed. 2) All required layers are included in bblayers.conf, including any board specific layers such as meta-mender-<board>
+# endif
+#else
+# define CONFIG_SYS_MMC_ENV_DEV MENDER_UBOOT_STORAGE_DEVICE
+#endif
+#ifndef CONFIG_SYS_MMC_ENV_PART
+ /* Use MMC partition zero to select whole user area of memory card. */
+# define CONFIG_SYS_MMC_ENV_PART 0
+#endif
+
+#endif /* !MENDER_AUTO_PROBING */
+
+#endif /* HEADER_CONFIG_MENDER_H */
diff --git a/include/config_mender_defines.h b/include/config_mender_defines.h
new file mode 100644
index 0000000000..90a6286192
--- /dev/null
+++ b/include/config_mender_defines.h
@@ -0,0 +1,24 @@
+/* AUTOGENERATED FILE - DO NOT EDIT! */
+/* This file is provided by the meta-mender layer. */
+
+#ifndef HEADER_CONFIG_MENDER_DEFINES_H
+#define HEADER_CONFIG_MENDER_DEFINES_H
+
+/* Shell variables */
+#define MENDER_BOOT_PART_NUMBER 1
+#define MENDER_BOOT_PART_NUMBER_HEX 1
+#define MENDER_ROOTFS_PART_A_NUMBER 2
+#define MENDER_ROOTFS_PART_A_NUMBER_HEX 2
+#define MENDER_ROOTFS_PART_B_NUMBER 3
+#define MENDER_ROOTFS_PART_B_NUMBER_HEX 3
+#define MENDER_UBOOT_STORAGE_INTERFACE "usb"
+#define MENDER_UBOOT_STORAGE_DEVICE 0
+
+/* BB variables. */
+#define MENDER_STORAGE_DEVICE_BASE "/dev/sda"
+#define MENDER_ROOTFS_PART_A_NAME "/dev/sda2"
+#define MENDER_ROOTFS_PART_B_NAME "/dev/sda3"
+
+#define MENDER_BOOT_KERNEL_TYPE "booti"
+#define MENDER_KERNEL_NAME "kernel8.img"
+#endif /* !HEADER_CONFIG_MENDER_DEFINES_H */
diff --git a/include/configs/rpi.h b/include/configs/rpi.h
index 834f1cd236..9dce94cc52 100644
--- a/include/configs/rpi.h
+++ b/include/configs/rpi.h
@@ -211,3 +211,11 @@
#endif
+
+#ifdef CONFIG_DM_VIDEO
+ #define CONFIG_BMP_32BPP
+#endif
+
+//Start Meder Configs
+#define CONFIG_BOOTCOUNT_LIMIT
+#define CONFIG_BOOTCOUNT_ENV
diff --git a/include/env_default.h b/include/env_default.h
index a657927e06..b27b702afb 100644
--- a/include/env_default.h
+++ b/include/env_default.h
@@ -8,6 +8,7 @@
*/
#include <env_callback.h>
+#include <env_mender.h>
#include <linux/stringify.h>
#ifdef DEFAULT_ENV_INSTANCE_EMBEDDED
@@ -22,6 +23,7 @@ static char default_environment[] = {
#else
const uchar default_environment[] = {
#endif
+ MENDER_ENV_SETTINGS
#ifndef CONFIG_USE_DEFAULT_ENV_FILE
#ifdef CONFIG_ENV_CALLBACK_LIST_DEFAULT
ENV_CALLBACK_VAR "=" CONFIG_ENV_CALLBACK_LIST_DEFAULT "\0"
@@ -32,8 +34,8 @@ const uchar default_environment[] = {
#ifdef CONFIG_USE_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
-#ifdef CONFIG_BOOTCOMMAND
- "bootcmd=" CONFIG_BOOTCOMMAND "\0"
+#ifdef CONFIG_MENDER_BOOTCOMMAND
+ "bootcmd=" CONFIG_MENDER_BOOTCOMMAND "\0"
#endif
#ifdef CONFIG_RAMBOOTCOMMAND
"ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
diff --git a/include/env_mender.h b/include/env_mender.h
new file mode 100644
index 0000000000..fa4f06f526
--- /dev/null
+++ b/include/env_mender.h
@@ -0,0 +1,121 @@
+/*
+ Copyright 2017 Northern.tech AS
+
+ 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.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef HEADER_ENV_MENDER_H
+#define HEADER_ENV_MENDER_H
+
+/* Avoid Mender specific code if we are just probing for configuration. */
+#ifdef MENDER_AUTO_PROBING
+#define MENDER_ENV_SETTINGS
+#else
+
+#include <config_mender_defines.h>
+
+#ifdef MENDER_NO_DEFAULT_ALTBOOTCMD
+# define MENDER_DEFAULT_ALTBOOTCMD
+#else
+# define MENDER_DEFAULT_ALTBOOTCMD "altbootcmd=run mender_altbootcmd; " \
+ "run bootcmd\0"
+#endif
+
+#define MENDER_ENV_SETTINGS \
+ MENDER_DEFAULT_ALTBOOTCMD \
+ \
+ "bootlimit=1\0" \
+ "bootcount=0\0" \
+ \
+ "upgrade_available=0\0" \
+ \
+ "mender_boot_part=" __stringify(MENDER_ROOTFS_PART_A_NUMBER) "\0" \
+ \
+ "mender_boot_part_hex=" __stringify(MENDER_ROOTFS_PART_A_NUMBER_HEX) "\0" \
+ \
+ "mender_uboot_boot=" MENDER_UBOOT_STORAGE_INTERFACE " " __stringify(MENDER_UBOOT_STORAGE_DEVICE) ":" __stringify(MENDER_BOOT_PART_NUMBER_HEX) "\0" \
+ \
+ "mender_uboot_if=" MENDER_UBOOT_STORAGE_INTERFACE "\0" \
+ \
+ "mender_uboot_dev=" __stringify(MENDER_UBOOT_STORAGE_DEVICE) "\0" \
+ \
+ "mender_boot_kernel_type=" MENDER_BOOT_KERNEL_TYPE "\0" \
+ \
+ "mender_kernel_name=" MENDER_KERNEL_NAME "\0" \
+ \
+ "mender_check_saveenv_canary=1\0" \
+ \
+ "mender_setup=" \
+ "if test \"${mender_saveenv_canary}\" != \"1\"; then " \
+ "setenv mender_saveenv_canary 1; " \
+ "saveenv; " \
+ "fi; " \
+ "if test \"${mender_systemd_machine_id}\" != \"\"; " \
+ "then " \
+ "setenv bootargs_orig systemd.machine_id=${mender_systemd_machine_id} " \
+ "${bootargs_orig}; " \
+ "fi; " \
+ "setenv mender_kernel_root " MENDER_STORAGE_DEVICE_BASE "${mender_boot_part}; " \
+ "if test ${mender_boot_part} = " __stringify(MENDER_ROOTFS_PART_A_NUMBER) "; " \
+ "then " \
+ "setenv mender_boot_part_name " MENDER_ROOTFS_PART_A_NAME "; " \
+ "else " \
+ "setenv mender_boot_part_name " MENDER_ROOTFS_PART_B_NAME "; " \
+ "fi; " \
+ "setenv mender_kernel_root_name ${mender_boot_part_name}; " \
+ "setenv mender_uboot_root " MENDER_UBOOT_STORAGE_INTERFACE " " __stringify(MENDER_UBOOT_STORAGE_DEVICE) ":${mender_boot_part_hex}; " \
+ "setenv mender_uboot_root_name ${mender_boot_part_name}; \0" \
+ \
+ "mender_altbootcmd=" \
+ "if test ${mender_boot_part} = " __stringify(MENDER_ROOTFS_PART_A_NUMBER) "; " \
+ "then " \
+ "setenv mender_boot_part " __stringify(MENDER_ROOTFS_PART_B_NUMBER) "; " \
+ "setenv mender_boot_part_hex " __stringify(MENDER_ROOTFS_PART_B_NUMBER_HEX) "; " \
+ "else " \
+ "setenv mender_boot_part " __stringify(MENDER_ROOTFS_PART_A_NUMBER) "; " \
+ "setenv mender_boot_part_hex " __stringify(MENDER_ROOTFS_PART_A_NUMBER_HEX) "; " \
+ "fi; " \
+ "setenv upgrade_available 0; " \
+ "saveenv; " \
+ "run mender_setup\0" \
+ \
+ "mender_try_to_recover=" \
+ "if test ${upgrade_available} = 1; " \
+ "then reset; " \
+ "fi\0"
+
+#define MENDER_BOOTARGS \
+ "setenv bootargs \"$bootargs_orig\"; " \
+ "setexpr bootargs sub root=/dev/\\\\\\\\S* root=\\\\$mender_kernel_root_name; "\
+ "setenv expand_bootargs \"setenv bootargs \\\\\"${bootargs}\\\\\"\"; "\
+ "run expand_bootargs; " \
+ "setenv expand_bootargs; "
+#define MENDER_LOAD_KERNEL_AND_FDT \
+ "if test \"${fdt_addr_r}\" != \"\"; then " \
+ "load ${mender_uboot_root} ${fdt_addr_r} /boot/${mender_dtb_name}; " \
+ "fi; " \
+ "load ${mender_uboot_root} ${kernel_addr_r} /boot/${mender_kernel_name}; "
+#define MENDER_LOAD_KERNEL_ONLY \
+ "load ${mender_uboot_root} ${kernel_addr_r} /srv/${mender_kernel_name}; "
+#define CONFIG_MENDER_BOOTCOMMAND \
+ "run mender_setup; " \
+ MENDER_BOOTARGS \
+ MENDER_LOAD_KERNEL_ONLY \
+ "${mender_boot_kernel_type} ${kernel_addr_r} - ${fdt_addr}; " \
+ "run mender_try_to_recover"
+
+#endif /* !MENDER_AUTO_PROBING */
+
+#endif /* HEADER_ENV_MENDER_H */
diff --git a/scripts/Makefile.autoconf b/scripts/Makefile.autoconf
index 00b8fb34aa..39e669eec6 100644
--- a/scripts/Makefile.autoconf
+++ b/scripts/Makefile.autoconf
@@ -109,6 +109,7 @@ define filechk_config_h
echo \#include \<configs/$(CONFIG_SYS_CONFIG_NAME).h\>; \
echo \#include \<asm/config.h\>; \
echo \#include \<linux/kconfig.h\>; \
+ echo \#include \<config_mender.h\>; \
echo \#include \<config_fallbacks.h\>;)
endef
$ git diff ecd4d99f654f3f7bfb96001891d69c3125e70b69 HEAD > mender.pi3.diff
$ git remote -v
origin git://git.denx.de/u-boot.git (fetch)
origin git://git.denx.de/u-boot.git (push)
$ grep CONFIG_PREBOOT .config
CONFIG_PREBOOT="usb start; load usb 0 $kernel_addr_r /u-boot_splash.bmp; bmp display $kernel_addr_r m m; load usb 0:1 ${kernel_addr_r} /uboot.env; env import -c ${kernel_addr_r} 0x4000;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment