# Firmware configuration file for the LinkIt Smart

# 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                        |
# +----------------------------+
# | p0*: Rootfs A (squashfs)   |
# +----------------------------+
# | p0*: Rootfs B (squashfs)   |
# +----------------------------+
# | p1: Application (FAT32)    |
# +----------------------------+
# | p2: Unused                 |
# +----------------------------+
# | p3: Unused                 |
# +----------------------------+
#
# The p0 partition points to whichever of Rootfs A or B that
# is active.

# Let the rootfs have room to grow up to 64 MiB and align
# it to the nearest 1 MB boundary
define(ROOTFS_A_PART_OFFSET, 2048)
define(ROOTFS_A_PART_COUNT, 131072)
define(ROOTFS_B_PART_OFFSET, 133120)
define(ROOTFS_B_PART_COUNT, 131072)

# Application partition
# NOTE: Keep the total amount used under 1.78 GiB so that
#       everything fits in the "2 GB" eMMC.
define(APP_PART_OFFSET, 264192)
define(APP_PART_COUNT, 1048576)

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

file-resource rootfs.img {
    host-path = ${ROOTFS}
}
file-resource uImage {
    host-path = "${NERVES_SYSTEM}/images/uImage"
    assert-size-lte = 30720  # 15 MiB
}

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

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

# This firmware task writes everything to the destination media
task complete {
    on-init {
        mbr_write(mbr-a)
    }

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

    on-finish {
        fat_mkfs(${APP_PART_OFFSET}, ${APP_PART_COUNT})
        fat_setlabel(${APP_PART_OFFSET}, "APPDATA")
    }
}

task upgrade.a {
    # This task upgrades the A partition
    require-partition-offset(0, ${ROOTFS_B_PART_OFFSET})

    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 rootfs
        mbr_write(mbr-a)
    }

    on-error {
    }
}

task upgrade.b {
    # This task upgrades the B partition
    require-partition-offset(0, ${ROOTFS_A_PART_OFFSET})

    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 {
    }
}

task write_kernel {
    on-resource uImage {
        # It is expected that this is pointed at /dev/mtdblock3
        raw_write(0)
    }
}
