Skip to content

Instantly share code, notes, and snippets.

@Baha2Odeh
Forked from wido/mkteslaemmcimg.sh
Created January 9, 2023 11:34
Show Gist options
  • Save Baha2Odeh/fd82c2764dbd9aaa369427593e2e9c1b to your computer and use it in GitHub Desktop.
Save Baha2Odeh/fd82c2764dbd9aaa369427593e2e9c1b to your computer and use it in GitHub Desktop.
Tesla Model S/X MCU1 eMMC image creator
#!/bin/bash
#
# Create a 8GB eMMC image for Tesla Model S/X MCU1
#
# Example usage: ./mkteslaemmcimg.sh ./vinXXXXX.img ./develop-2019.20.2.1-16-5659e07dfd.img
#
set -e
FIRMWARE=$2
IMAGE=$1
SIZE=7818182656
INDEX=$((10 + RANDOM % 100))
LOOP="loop${INDEX}"
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <image file> <firmware file>"
exit 255
fi
if [ -e "${IMAGE}" ]; then
echo "Error: ${IMAGE} already exists"
exit 1
fi
if [ ! -f "${FIRMWARE}" ]; then
echo "Error: ${FIRMWARE} does not exist"
exit 2
fi
echo "Info: Writing empty image file=${IMAGE} size=${SIZE}"
dd if=/dev/zero of=${IMAGE} bs=1024 count=7634944
echo "Info: Creating partitions on ${IMAGE}"
parted -s ${IMAGE} mklabel msdos
parted -s ${IMAGE} mkpart primary 4096s 2101247s
parted -s ${IMAGE} mkpart primary 2101248s 4198399s
parted -s ${IMAGE} mkpart primary 4198400s 4460543s
parted -s ${IMAGE} mkpart primary 4460544s 15269887s
echo "Info: Setting up loop device ${LOOP}"
sudo losetup -P ${LOOP} ${IMAGE}
echo "Info: Writing ${FIRMWARE} to partitions 1 and 2"
sudo dd if=${FIRMWARE} of=/dev/${LOOP}p1
sudo dd if=${FIRMWARE} of=/dev/${LOOP}p2
echo "Info: Creating empty ext3 filesystem on partitions 3 and 4"
sudo mkfs.ext3 /dev/${LOOP}p3
sudo mkfs.ext3 /dev/${LOOP}p4
echo "Info: Cleaning up loop device ${LOOP}"
sudo losetup -d /dev/${LOOP}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment