BeagleBone Black Extracting eMMC contents

=Intro= There are lots of ways to extract the contents of the eMMC to save off and reuse. I'm proposing a method using Buildroot and an initramfs such that you can simply drop a few files from a .zip onto a normal, FAT-formatted SD card to perform the extraction. There are several things really handy here, such as the ability to edit autorun.sh to be whatever script you want to run on your board at boot. In the archive, I only have the necessary autorun.sh for saving your eMMC content. The flip-side is provided here in the text such that you need to go through a couple of steps before you trash your eMMC.

Steps to save
The steps for saving off your eMMC contents to a file: IMPORTANT: Due to the way the environment sets the partition mmcblk1, some newer boards (shipped with Debian 7.5 or newer) won't work with the autorun.sh. As a workaround, instead of holding down the S2 button, just power the board with the uSD card inserted.
 * Get a 4GB or larger uSD card that is FAT formatted.
 * Make sure that the partition on the uSD card is marked as active.
 * Download beagleboneblack-save-emmc.zip and extract the contents onto your uSD card.
 * Eject uSD card from your computer, insert into powered-off BeagleBone Black and apply power to your board while holding down the S2 button (release button a couple seconds after boot).

IMPORTANT: On Rev C BeagleBone Blacks from element14, you need to create a folder titled "dtbs" and copy am335x-boneblack.dtb into it in order for the SD card to be booted.

IMPORTANT: On Rev C BeagleBone Greens from Seed Studio, you need to create a folder titled "dtbs" and copy am335x-boneblack.dtb to dtbs/am335x-bonegreen.dtb (note the name change) in order for the SD card to be booted.

IMPORTANT: Despite what the above is saying the dtb file may actually need to be on the card itself rather than in a "dtbs" subdirectory. Put it in both places just to be safe. Also you may need to add the following lines to the uEnv.txt file: mmcdev=0 mmcpart=1
 * You'll notice USR0 (the LED closest to the S1 button in the corner) will (after about 20 seconds) start to blink steadily, rather than the double-pulse "heartbeat" pattern that is typical when your BeagleBone Black is running the typical Linux kernel configuration.
 * It'll run for a bit under 10 minutes and then USR0 will stay ON steady. That's your cue to remove power, remove the uSD card and put it back into your computer.
 * You should see a file called BeagleBoneBlack-eMMC-image-XXXXX.img.gz, where XXXXX is a set of random numbers. Save off this file to use for restoring your image later.

NOTE: Because the date won't be set on your board, you might want to adjust the date on the file to remember when you made it.

NOTE: Delete the file if you want to make room for a new backup image.

NOTE: If you plan to use Windows Win32 Disk Imager, you'll need to uncompress the image. It is compressed due to some FAT (non-FAT32) partitions not being able to store more than 2GB files.

NOTE: To mark a partition as active in Windows 7: Open command prompt and enter the following commands: diskpart -> list disk -> select disk x (where x is the uSD card) -> list partition -> select partition 1 (assuming the uSD card has 1 partition) -> active.

Performing restore/flashing
To restore the file, make sure there is a valid BeagleBoneBlack-eMMC-image-XXXX.img file on the uSD card and edit autorun.sh with your favorite text editor to contain the following: echo timer > /sys/class/leds/beaglebone\:green\:usr0/trigger gunzip -c /mnt/BBB-eMMC-XXXXX.img.gz | dd of=/dev/mmcblk1 bs=16M UUID=$(/sbin/blkid -c /dev/null -s UUID -o value /dev/mmcblk1p2) mkdir -p /mnt mount /dev/mmcblk1p2 /mnt sed -i "s/^uuid=.*\$/uuid=$UUID/" /mnt/boot/uEnv.txt umount /mnt sync echo default-on > /sys/class/leds/beaglebone\:green\:usr0/trigger
 * 1) !/bin/sh

NOTE: Be certain to replace the 'XXXXX' above with the proper name of your image file.

NOTE: You can share and use other people's image files, but be sure to have them uncompressed on the card or add decompression to the script.

NOTE: The UUID replacement is required for recent Debian images. If you have something other than a 2 partition image, you might need to adjust or remove the lines that configure the UUID. They should be generally safe, but be aware that the newer u-boots that ship with BeagleBone Black attempt to use the UUID and pointing the '/dev/mmcblk1p2' to the right root partition is important.

NOTE: if you have difficulty booting a Rev. C BBB with this tool mount the SD card and make the following changes:

cd /mnt #or wherever you have mounted the card mkdir dtbs cp am335x-boneblack.dtb dtbs/am335x-boneblack.dtb cp am335x-boneblack.dtb dtbs/am335x-bonegreen.dtb
 * 1) if you have a BeageBoneGreen just change the name:

You may also try booting with the barrel jack (AC power) rather than the USB port.

=Sources= This image was built using Buildroot. The sources are at https://github.com/jadonk/buildroot with tag save-emmc-0.0.1. Download via https://github.com/jadonk/buildroot/releases/tag/save-emmc-0.0.1 or clone the git repo. It is a small fork from git://git.buildroot.net/buildroot tag e9f6011617528646768e69203e85fe64364b7efd.

Build steps
To build, 'make beagleboneblack_defconfig; make; ./mkuimage.sh'. Output files (am335x-boneblack.dtb, MLO, u-boot.img and uImage) will be in the output/images subdirectory. The following files were created manually.

uEnv.txt
bootpart=0:1 bootdir= fdtaddr=0x81FF0000 optargs=quiet capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN uenvcmd=load mmc 0 ${loadaddr} uImage;run loadfdt;setenv bootargs console=${console} ${optargs};bootm ${loadaddr} - ${fdtaddr}

autorun.sh
echo timer > /sys/class/leds/beaglebone\:green\:usr0/trigger·
 * 1) !/bin/sh

IMAGEFILE=/mnt/BBB-eMMC-$RANDOM.img.gz dd if=/dev/mmcblk1 bs=16M | gzip -c > $IMAGEFILE
 * 1) un-comment the following 2 lines to perform a backup


 * 1) un-comment the following 6 lines to perform a restore (be sure to replace XXXXX with your image name)
 * 2) gunzip -c /mnt/BBB-eMMC-XXXXX.img.gz | dd of=/dev/mmcblk1 bs=16M
 * 3) UUID=$(/sbin/blkid -c /dev/null -s UUID -o value /dev/mmcblk1p2)
 * 4) mkdir -p /mnt
 * 5) mount /dev/mmcblk1p2 /mnt
 * 6) sed -i "s/^uuid=.*\$/uuid=$UUID/" /mnt/boot/uEnv.txt
 * 7) umount /mnt

sync echo default-on > /sys/class/leds/beaglebone\:green\:usr0/trigger

Other sources used
The kernel is based on https://github.com/beagleboard/kernel/commit/9fdb452245a58158a4bea787cdc663c17681bcfe, but I applied the patches, added firmware and uploaded it to https://github.com/beagleboard/linux/commit/ddd36e546e53d3c493075bbebd6188ee843208f9 to pull down in the Buildroot makefile. The link to the source for the firmware is in the commit.

=Alternatives= For many BeagleBone Debian users, there is a simple alternative of using the /opt/scripts/tools/eMMC/beaglebone-black-make-microSD-flasher-from-eMMC.sh to write an image from the on-board eMMC to a microSD card in a way that the microSD card becomes a "flasher" to write to other BeagleBone eMMCs. Simply boot off the on-board eMMC that you wish to copy, insert a blank microSD card at least 4GB, run the script as root and wait for the script to complete. At that point, you shut down your board and remove the microSD card. Booting off that microSD card on other BeagleBones should result in the typical "flasher" process writing the contents onto that BeagleBone's eMMC.

The source code for the script is at https://github.com/RobertCNelson/boot-scripts/blob/master/tools/eMMC/beaglebone-black-make-microSD-flasher-from-eMMC.sh.