Skip to content

Instantly share code, notes, and snippets.

@Baha2Odeh
Forked from appleguru/mkteslaemmcimg.sh
Created January 16, 2023 12:56
Show Gist options
  • Save Baha2Odeh/8470eeb55065ab81a33d75ba7ce276af to your computer and use it in GitHub Desktop.
Save Baha2Odeh/8470eeb55065ab81a33d75ba7ce276af 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: Creating tesla1/tesla2 tokens"
P3DIR=$(mktemp -d)
P4DIR=$(mktemp -d)
sudo mount /dev/${LOOP}p3 "${P3DIR}"
sudo mount /dev/${LOOP}p4 "${P4DIR}"
sudo mkdir -p "${P3DIR}/etc/saccess/"
RANDTOKEN=$(openssl rand -hex 8)
echo "Info: tesla1/tesla2 token: ${RANDTOKEN}"
sudo sh -c "echo ${RANDTOKEN} > ${P3DIR}/etc/saccess/tesla1"
sudo sh -c "echo ${RANDTOKEN} > ${P3DIR}/etc/saccess/tesla2"
sudo touch --date="2030/09/09 01:11" "${P3DIR}/etc/saccess/tesla1"
sudo touch --date="2030/09/09 01:11" "${P3DIR}/etc/saccess/tesla2"
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