diff --git a/resources/boot.cmd b/resources/boot.cmd index ad80b1b..d847b0d 100644 --- a/resources/boot.cmd +++ b/resources/boot.cmd @@ -1,3 +1,54 @@ +# static config +setenv boot_partition_a 2 +setenv boot_partition_b 3 +setenv boot_limit 2 +setenv boot_partition_base "/dev/mmcblk0p" + + +# set default values if env not set +if printenv boot_count; then +else + setenv boot_count 1 +fi + +if printenv boot_partition; then + # check if valid partition a or b + if test ${boot_partition} -ne ${boot_partition_a} && test ${boot_partition} -ne ${boot_partition_b}; then + setenv boot_partition ${boot_partition_a} + fi +else + setenv boot_partition ${boot_partition_a} +fi + + +# switch boot partition if boot count exceed limit +if test ${boot_count} -ge ${boot_limit}; then + echo "!!! Boot limit exceed !!!" + + if test ${boot_partition} -eq ${boot_partition_a}; then + setenv boot_partition ${boot_partition_b} + else + setenv boot_partition ${boot_partition_a} + fi + setenv boot_count 0 + + echo "Switch active partition to ${boot_partition_base}${boot_partition}" +fi + +# increase boot_count +setexpr boot_count ${boot_count} + 1 + +# store settings +saveenv + +# load bootargs from pi boot loader fdt addr ${fdt_addr} && fdt get value bootargs /chosen bootargs -ext4load mmc 0:2 ${kernel_addr_r} /boot/uImage + +# overwrite boot partition +setexpr bootargs sub " root=[^ ]+" " root=${boot_partition_base}${boot_partition}" "${bootargs}" + +# load kernel and boot +ext4load mmc 0:${boot_partition} ${kernel_addr_r} /boot/uImage bootm ${kernel_addr_r} - ${fdt_addr} + +reset \ No newline at end of file diff --git a/resources/build.sh b/resources/build.sh index 15c45bd..de7902b 100755 --- a/resources/build.sh +++ b/resources/build.sh @@ -10,14 +10,14 @@ set -e : ${TIME_ZONE:="Etc/UTC"} : ${HOST_NAME:="alpine"} : ${ROOT_PASSWORD:="alpine"} -: ${IMG_NAME:="alpine-${ALPINE_BRANCH}-sdcard.img"} +: ${IMG_NAME:="alpine-${ALPINE_BRANCH}-sdcard"} # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # static config # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # RES_PATH=/resources/ -BASE_PACKAGES="alpine-base tzdata parted ifupdown e2fsprogs-extra util-linux coreutils linux-rpi2" +BASE_PACKAGES="alpine-base tzdata parted ifupdown e2fsprogs-extra util-linux coreutils linux-rpi2 uboot-tools" WORK_PATH="/work" OUTPUT_PATH="/output" @@ -149,9 +149,59 @@ tmpfs /tmp tmpfs defaults 0 0 tmpfs /run tmpfs defaults 0 0 EOF -# custom +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +echo ">> Move persistent data to /data" + +# prepare /data +cat >${ROOTFS_PATH}/etc/local.d/20-data_prepare.start <${ROOTFS_PATH}/etc/fw_env.config <${ROOTFS_PATH}/etc/local.d/99-uboot.start <> Compress images" # copy final image -cp ${IMAGE_PATH}/sdcard.img ${OUTPUT_PATH}/${IMG_NAME} +gzip -c ${IMAGE_PATH}/sdcard.img > ${OUTPUT_PATH}/${IMG_NAME}.img.gz +gzip -c ${IMAGE_PATH}/rootfs.ext4 > ${OUTPUT_PATH}/${IMG_NAME}_update.img.gz + +# create checksums +cd ${OUTPUT_PATH}/ +sha256sum ${IMG_NAME}.img.gz > ${IMG_NAME}.img.gz.sha256 +sha256sum ${IMG_NAME}_update.img.gz > ${IMG_NAME}_update.img.gz.sha256 diff --git a/resources/resizedata.sh b/resources/resizedata.sh index 3689938..30e269a 100755 --- a/resources/resizedata.sh +++ b/resources/resizedata.sh @@ -1,4 +1,10 @@ #!/bin/sh -e + +# resize already done? +if [ -f /data/resize_done ]; then + return 0 +fi + logger -t "rc.resizedata" "Expanding root partition" # Detect root partition device @@ -57,4 +63,4 @@ partprobe /dev/${ROOT_DEV} && mount -a -chmod -x /etc/local.d/90-resizedata.start \ No newline at end of file +touch /data/resize_done diff --git a/resources/scripts/ab_active b/resources/scripts/ab_active new file mode 100755 index 0000000..f6c66ec --- /dev/null +++ b/resources/scripts/ab_active @@ -0,0 +1,11 @@ + #!/bin/bash +set -e + +# get current partition index +current_idx=$(rdev | sed 's#/dev/mmcblk0p\([^ ]*\).*#\1#') + +if [ $current_idx -eq 2 ]; then + echo "Active partition: A" +else + echo "Active partition: B" +fi diff --git a/resources/scripts/ab_flash b/resources/scripts/ab_flash new file mode 100755 index 0000000..034210c --- /dev/null +++ b/resources/scripts/ab_flash @@ -0,0 +1,40 @@ + #!/bin/bash +set -e + +image_file=$1 + +if [ -z $image_file ]; then + echo "USAGE: $0 [IMAGE_PATH]" + return 1 +fi + +# change to directory containing update file +cd $(dirname $image_file) + +# check integrity of image +sha256sum -c ${image_file}.sha256 + +# get current partition index +current_idx=$(rdev | sed 's#/dev/mmcblk0p\([^ ]*\).*#\1#') + +if [ $current_idx -eq 2 ]; then + echo "Start update for partition B" + flash_idx=3 +else + echo "Start update for partition A" + flash_idx=2 +fi + +flash_device="/dev/mmcblk0p${flash_idx}" + +# flash device +gunzip -c ${image_file} | dd of=${flash_device} status=progress + + +# switch active partition +mount -o remount,rw /uboot +fw_setenv boot_partition ${flash_idx} +sync +mount -o remount,ro /uboot + +echo "Update complete -> please reboot"