# Firmware configuration file for the Beaglebone Black

# Default paths if not specified via the commandline
define(ROOTFS, "${NERVES_SYSTEM}/images/rootfs.squashfs")

# This configuration file will create an image that
# has an MBR and the following 3 partitions:
#
# +----------------------------+
# | MBR                        |
# +----------------------------+
# | p1*: Rootfs A (squashfs)   |
# +----------------------------+
# | p1*: Rootfs B (squashfs)   |
# +----------------------------+
# | p2: Application (FAT32)    |
# +----------------------------+
# | p3: Used (FAT32)           |
# +----------------------------+
#
# The p1 partition points to whichever of Rootfs A or B that
# is active. The p3 is used as a hack to pad any generated
# image file out so that it includes room for p2. Qemu wants
# this since it won't expand a raw image file.

# Let the rootfs have room to grow up to 128 MiB. Don't
# worry about boundaries since we're using qemu
define(ROOTFS_A_PART_OFFSET, 1)
define(ROOTFS_A_PART_COUNT, 262143)
define(ROOTFS_B_PART_OFFSET, 262144)
define(ROOTFS_B_PART_COUNT, 262143)

# Application partition (128 MiB)
define(APP_PART_OFFSET, 524288)
define(APP_PART_COUNT, 262144)

define(PADDING_PART_OFFSET, 786432)
define(PADDING_PART_COUNT, 2048)

# Firmware metadata
meta-product = "Nerves Firmware"
meta-description = ""
meta-version = ${NERVES_SDK_VERSION}
meta-platform = "qemu-vexpress"
meta-architecture = "arm"
meta-author = "Frank Hunleth"

file-resource rootfs.img {
    host-path = ${ROOTFS}
}

mbr mbr-a {
    # partition 0 is unused
    partition 1 {
        block-offset = ${ROOTFS_A_PART_OFFSET}
        block-count = ${ROOTFS_A_PART_COUNT}
        type = 0x83 # Linux
    }
    partition 2 {
        block-offset = ${APP_PART_OFFSET}
        block-count = ${APP_PART_COUNT}
        type = 0xc # FAT32
    }
    # partition 3 is unused
}

mbr mbr-b {
    # partition 0 is unused
    partition 1 {
        block-offset = ${ROOTFS_B_PART_OFFSET}
        block-count = ${ROOTFS_B_PART_COUNT}
        type = 0x83 # Linux
    }
    partition 2 {
        block-offset = ${APP_PART_OFFSET}
        block-count = ${APP_PART_COUNT}
        type = 0xc # FAT32
    }
    # partition 3 is unused
}

# This firmware task writes everything to the destination media
task complete {
    # Only match if not mounted
    require-unmounted-destination = true

    # Everything that gets written can be verified on the fly.
    # This speeds things up, since we don't care about detecting
    # errors before data gets written.
    verify-on-the-fly = true

    on-init {
        mbr_write(mbr-a)
    }

    on-resource rootfs.img {
        # write to the first rootfs partition
        raw_write(${ROOTFS_A_PART_OFFSET})
    }

    on-finish {
        # Initialize the app partition last so that the boot
        # partition can be written in one go.
        fat_mkfs(${APP_PART_OFFSET}, ${APP_PART_COUNT})
        fat_setlabel(${APP_PART_OFFSET}, "APPDATA")

        fat_mkfs(${PADDING_PART_OFFSET}, ${PADDING_PART_COUNT})
    }
}

task upgrade.a {
    # This task upgrades the A partition
    require-partition1-offset = ${ROOTFS_B_PART_OFFSET}

    # Since the upgrade won't run until it has been finalized, it's ok
    # to write data as it is read.
    verify-on-the-fly = true

    on-init {
    }

    on-resource rootfs.img {
        # write to the first rootfs partition
        raw_write(${ROOTFS_A_PART_OFFSET})
    }

    on-finish {
	# Switch over to boot the new firmware
        mbr_write(mbr-a)
    }

    on-error {
    }
}

task upgrade.b {
    # This task upgrades the B partition
    require-partition1-offset = ${ROOTFS_A_PART_OFFSET}

    # Since the upgrade won't run until it has been finalized, it's ok
    # to write data as it is read.
    verify-on-the-fly = true

    on-init {
    }

    on-resource rootfs.img {
        # write to the first rootfs partition
        raw_write(${ROOTFS_B_PART_OFFSET})
    }

    on-finish {
	# Switch over to boot the new firmware
        mbr_write(mbr-b)
    }

    on-error {
    }
}
