\subsection{generate a sdcard image}
\begin{frame}[fragile]
\frametitle{sdcard generation}
create the file: 'meta-foo/classes/image\_sdcard.bbclass':
\begin{verbatim}
inherit image_types

IMAGE_BOOTLOADER ?= "u-boot"

# Handle u-boot suffixes
UBOOT_SUFFIX ?= "bin"
UBOOT_PADDING ?= "0"
UBOOT_SUFFIX_SDCARD ?= "${UBOOT_SUFFIX}"

# Linux bootstream
IMAGE_DEPENDS_linux.sb = "virtual/kernel:do_deploy"

# Boot partition volume id
BOOTDD_VOLUME_ID ?= "Boot ${MACHINE}"
# Boot partition size [in KiB]
BOOT_SPACE ?= "8192"
# Set alignment to 4MB [in KiB]
IMAGE_ROOTFS_ALIGNMENT = "4096"
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{sdcard generation \#2}
\begin{verbatim}
IMAGE_DEPENDS_sdcard = "parted-native:do_populate_sysroot \
  dosfstools-native:do_populate_sysroot \
  mtools-native:do_populate_sysroot \
  virtual/kernel:do_deploy \
  ${@d.getVar('IMAGE_BOOTLOADER', True) and \
    'd.getVar('IMAGE_BOOTLOADER', True) + ':do_deploy' or ''}"

SDCARD = "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.sdcard"
SDCARD_GENERATION_COMMAND_ti33x = "generate_ti_sdcard"
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{sdcard generation \#3}
\begin{verbatim}
generate_ti_sdcard () {
  parted -s ${SDCARD} mklabel msdos

  parted -s ${SDCARD} unit KiB mkpart primary fat32 \
   ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ \
   ${BOOT_SPACE_ALIGNED})

  parted -s ${SDCARD} unit KiB mkpart primary \
   $(expr  ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED}) \
   $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED} \+ \
     $ROOTFS_SIZE)

  parted -s ${SDCARD} set 1 boot on
  parted ${SDCARD} print

  BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDCARD} unit b print \
    | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 1024 }')

  mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C \
    ${WORKDIR}/boot.img $BOOT_BLOCKS
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{sdcard generation \#4}
\begin{verbatim}
  # copy files to /boot
  mcopy -i ${WORKDIR}/boot.img -s \
    ${DEPLOY_DIR_IMAGE}/MLO-${MACHINE} ::/MLO
  mcopy -i ${WORKDIR}/boot.img -s \
    ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.img ::/u-boot.img
  mmd -i ${WORKDIR}/boot.img  ::/boot
  mcopy -i ${WORKDIR}/boot.img -s \
    ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${MACHINE}.bin \
    ::/boot/${KERNEL_IMAGETYPE}
  mcopy -i ${WORKDIR}/boot.img -s \
    ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-am335x-boneblack.dtb \
    ::/boot/am335x-boneblack.dtb
  mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/uEnv.txt \
    ::/uEnv.txt

  # Burn Partition
  dd if=${WORKDIR}/boot.img of=${SDCARD} conv=notrunc seek=1 \
    bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
  sync
  dd if=${SDCARD_ROOTFS} of=${SDCARD} conv=notrunc seek=1 \
    bs=$(expr ${BOOT_SPACE_ALIGNED} \* 1024 + \
         ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
  sync
}
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{sdcard generation \#5}
\begin{verbatim}
IMAGE_CMD_sdcard () {
  if [ -z "${SDCARD_ROOTFS}" ]; then
    bberror "SDCARD_ROOTFS is undefined."
    exit 1
  fi

  # Align boot partition and calculate total SD card image size
  BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
  BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - \
    ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
  SDCARD_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + \
    ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE + ${IMAGE_ROOTFS_ALIGNMENT})

  # Initialize a sparse file
  dd if=/dev/zero of=${SDCARD} bs=1 count=0 \
    seek=$(expr 1024 \* ${SDCARD_SIZE})

  ${SDCARD_GENERATION_COMMAND}
}

# The sdcard requires the rootfs filesystem to be built before using
# it so we must make this dependency explicit.
IMAGE_TYPEDEP_sdcard = "${@d.getVar('SDCARD_ROOTFS', 1).split('.')[-1]}"
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{configure machine to use sdcard imagetype}
add the following lines to 'meta-foo/conf/machine/beaglebone-black.conf':
\begin{verbatim}
IMAGE_CLASSES += "image_sdcard"
SDCARD_ROOTFS ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ext3"
IMAGE_FSTYPES += "sdcard"
\end{verbatim}
\end{frame}
