· 6 years ago · May 01, 2019, 07:18 PM
1#!/bin/sh
2
3# ----------------------------------------------------------------
4# This script programs the BeagleBone's internal MMC from the uSD card.
5#
6# Note: while you CAN just copy the sd card varbatim to the mmc...
7# dd if=/dev/mmcblk0 of=/dev/mmcblk1
8# ...this isn't the entire story, you also get a cross section in time of all your files,
9# and you dont get to take advantage of the entre 4G on the MMC!
10#
11# By constrast this script builds the MMC device up from scratch, leaving all of it for your
12# root filesystem.
13# However it does require the following tools installed in buildroot.
14# - fdisk
15# - mkfs.ext4
16# - mkfs.fat
17# - dd (usually comes with busybox)
18# ----------------------------------------------------------------
19
20
21# --- Config ---
22
23# This is the usual device name of the internal MMC device
24dest=/dev/mmcblk1
25
26# This is the usual device name of the SD card
27src=/dev/mmcblk0
28
29
30# --- Functions ---
31
32# Syncs the filesystems and flushes the MMC's buffers
33flush_cache () {
34 sync
35 blockdev --flushbufs ${dest}
36}
37
38# Exits the script printing the passed in error message
39die() {
40 echo "Error: $1"
41 exit 1
42}
43
44
45# --- Go! ---
46
47# First check that our MMC and SD cards actually exist
48# If only /dev/mmcblk0 exists then there is probably no SD card inserted,
49# and we are probably running from the MMC!
50[ -b ${dest} ] || die "${dest} doesn't exist - are you sure you're running from the SD card?"
51[ -b ${src} ] || die "${src} doesn't exist - are you sure you're running from the SD card?"
52
53
54# Unmount any filesystems on the destintation device
55echo "Unmounting any MMC filesystems"
56umount ${dest}p*
57flush_cache
58
59
60# Erase 4MB from the start of the MMC device
61# This should be enough to wipe out the partition table
62#echo "Erasing MMC"
63#dd if=/dev/zero of=${dest} bs=1M count=4 || die "Unable to erase MMC"
64#flush_cache
65
66
67# Create two new partitions
68# One bootable FAT16 partition 16MB in length
69# One Linux type pertition filling the rest of the device
70echo "Creating partitions"
71sfdisk --quiet ${dest} << EOF
721,32768,0xE,*
73,,,-
74EOF
75[ $? -eq 0 ] || die "Unable to partition drive"
76
77
78# Create a FAT16 filesystem on the boot partition
79# It is important to use FAT16 as the BeagleBone's bootloader doesn't understand anything else
80echo "Creating FAT filesystem on the boot partition"
81mkfs.fat -F16 ${dest}p1 || die "Unable to create boot filesystem"
82
83# Create an ext4 filesystem on the rootfs partition
84echo "Creating ext4 filesystem on the rootfs partition"
85mkfs.ext4 -F -q ${dest}p2 || die "Unable to create roofs"
86
87
88# Mount the MMC's boot filesystem
89echo "Mounting the MMC's boot filesystem"
90mkdir -p /tmp/boot || die "Unable to create MMC boot mount point"
91mount ${dest}p1 /tmp/boot
92
93# (Re)Mount the SD card boot filesystem as readonly
94echo "(Re)Mounting the SD card boot filesystem as readonly"
95mkdir -p /boot || die "Unable to create SD boot mount point"
96umount ${src}p1
97mount -o ro ${src}p1 /boot || die "Unable to remount SD card boot filesystem"
98
99# Copy the MLO (second stage bootloader) across first
100# It is important that this is the first file in the partition as the first stage bootloader cannot handle anything else
101# Flush caches to make sure this is written first
102echo "Copying the MLO (second stage bootloader) across first"
103cp /boot/MLO /tmp/boot/ || die "Unable to copy MLO"
104flush_cache
105
106# Copy the rest of the boot files across
107echo "Copying the rest of the boot files across"
108cp /boot/u-boot.img /boot/zImage /boot/*.dtb /tmp/boot/ || die "Unable to copy boot files"
109
110# Modify uEnv.txt (U-Boot environment variables) to boot from MMC not SD card
111echo "Modifying U-Boot environment variables"
112sed 's/bootpart=0:1/bootpart=1:1/' /boot/uEnv.txt > /tmp/boot/uEnv.txt
113flush_cache
114
115# Unmount both boot filesystems
116umount /tmp/boot || die "Unable to unmount MMC boot filesystem"
117umount /boot
118
119
120# Mount the MMC rootfs partition
121echo "Mounting MMC rootfs"
122mkdir -p /tmp/rootfs || die "Unable to create MMC rootfs mount point"
123mount ${dest}p2 /tmp/rootfs || die "Unable to mount MMC rootfs"
124
125# Copy important files across from the SD card to the MMC
126echo "Copying over important files from the rootfs"
127cp -r /bin /etc /home /lib /lib32 /linuxrc /opt /root /run /sbin /srv /usr /var /tmp/rootfs/ || die "Unable to copy across important rootfs files"
128flush_cache
129
130# Unmount rootfs
131umount /tmp/rootfs || die "Unable to unmount MMC rootfs"
132
133
134# Success!
135echo "Success!"
136echo "Please remove your SD card and power cycle the beaglebone - it should now boot from the MMC"