DeaD_EyE

mkroot for arch 2 + esp32 + rp2

Mar 23rd, 2022 (edited)
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. The target was to create an independent build environment
  5. for micropython-esp32 (master branch) on Arch-Linux.
  6.  
  7. - create image-file:
  8.  # no sudo
  9.  truncate -- size 16G image.img
  10.  # user still owns the image.img
  11. - sudo mount image-file as block device: losetup -f -P --show image.img
  12. - create a new partition:
  13.  sudo sfdisk /dev/loopXX <<EOF
  14.  2048
  15.  EOF
  16. - create a fs on new created partition:
  17.  mkfs.ext4 /dev/loopXXp1
  18. - sudo mount /dev/loopXXp1 /somewhere...
  19. - run chroot:
  20.  sudo arch-chroot /somewhere... <<EOF
  21.  # shell commands to execute in chroot
  22.  EOF
  23. - Download micropython and esp32-idf with git
  24.  https://github.com/micropython/micropython/blob/master/ports/esp32/README.md
  25. - compile it in new environment
  26.  
  27. ToDO:
  28. - flash esp32
  29. - catch all exceptions
  30. """
  31.  
  32. import subprocess
  33. from pathlib import Path
  34. from textwrap import dedent
  35.  
  36.  
  37. ARCH_PACKAGES = (
  38.     "base",
  39.     "linux",
  40.     "base-devel",
  41.     "arm-none-eabi-gcc",
  42.     "arm-none-eabi-newlib",
  43.     "cmake",
  44.     "python",
  45.     "python-pip",
  46.     "python-virtualenv",
  47.     "wget",
  48.     "git",
  49.     "libusb",
  50. )
  51.  
  52.  
  53. def mkimg(image: str | Path, size: str):
  54.     command = ["truncate", "--size", size, image]
  55.     subprocess.run(command, check=True)
  56.  
  57.  
  58. def mkpart(image: str | Path) -> str:
  59.     losetup = ["sudo", "losetup", "-f", "-P", "--show", image]
  60.     loop_dev = subprocess.run(
  61.         losetup, encoding="ascii", check=True, capture_output=True
  62.     ).stdout.strip()
  63.  
  64.     sfdisk_input = "2048\n"
  65.     sfdisk = ["sudo", "sfdisk", loop_dev]
  66.     subprocess.run(sfdisk, input=sfdisk_input, check=True, encoding="ascii")
  67.     return f"{loop_dev}p1"
  68.  
  69.  
  70. def mkfs(partition: str | Path):
  71.     mkfs = ["sudo", "mkfs.ext4", partition]
  72.     subprocess.run(mkfs, check=True)
  73.  
  74.  
  75. def mount(src, dst):
  76.     command = ["sudo", "mount", src, dst]
  77.     subprocess.run(command, check=True)
  78.  
  79.  
  80. def pacstrap(root: str | Path):
  81.     if not root.is_absolute():
  82.         raise RuntimeError("The supplied path is not an absolute path.")
  83.     command = ["sudo", "pacstrap", root, *ARCH_PACKAGES]
  84.     subprocess.run(command, check=True)
  85.  
  86.  
  87. def chroot(root: str | Path, script: str):
  88.     command = ["sudo", "arch-chroot", root]
  89.     subprocess.run(command, encoding="utf8", check=True, input=script)
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     SCRIPT = dedent("""
  94.    #!/bin/bash
  95.    useradd -m build
  96.    usermod -a -G uucp build
  97.    cat > /home/build/compile.sh <<EOF
  98.    #!/bin/bash
  99.  
  100.    set -e
  101.  
  102.    git clone --depth 1 https://github.com/micropython/micropython.git
  103.    git clone --depth 1 -b v4.4 --recursive https://github.com/espressif/esp-idf.git
  104.    pushd esp-idf
  105.    git checkout v4.4
  106.    git submodule update --depth 1 --init --recursive
  107.    ./install.sh
  108.    source export.sh
  109.    popd
  110.  
  111.    cd micropython
  112.    make -C mpy-cross
  113.    cd ports/esp32
  114.    make submodules
  115.    make
  116.    EOF
  117.  
  118.    chmod +x /home/build/compile.sh
  119.    su - build /home/build/compile.sh
  120.    """)
  121.  
  122.     ROOT = Path("/home/deadeye/Downloads/rootfs")
  123.     IMAGE = Path("/home/deadeye/Downloads/TEST.img")
  124.     IMAGE_SIZE = "16G"
  125.     mkimg(IMAGE, IMAGE_SIZE)
  126.     partition = mkpart(IMAGE)
  127.     mkfs(partition)
  128.     mount(partition, ROOT)
  129.     pacstrap(ROOT)
  130.     chroot(ROOT, SCRIPT)
  131.  
  132.  
  133. # OUTPUT
  134. # (seems to work)
  135.  
  136. # Project build complete. To flash, run this command:
  137. # /home/build/.espressif/python_env/idf4.4_py3.10_env/bin/python ../../../esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 460800 --before default_reset --after hard_reset --chip esp32  write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build-GENERIC/bootloader/bootloader.bin 0x8000 build-GENERIC/partition_table/partition-table.bin 0x10000 build-GENERIC/micropython.bin
  138. # or run 'idf.py -p (PORT) flash'
  139. # bootloader  @0x001000    21584  (    7088 remaining)
  140. # partitions  @0x008000     3072  (    1024 remaining)
  141. # application @0x010000  1466208  (  565408 remaining)
  142. # total                  1531744
Add Comment
Please, Sign In to add comment