Advertisement
DefKorns

gw

Jan 24th, 2022
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.82 KB | None | 0 0
  1. #!/bin/bash
  2. # PATHS
  3. export OPENOCD="/opt/openocd-git/bin/openocd"
  4. export GCC_PATH="$PWD/gcc-arm-none-eabi-10.3-2021.10/bin"
  5. export PATH=$GCC_PATH:$PATH
  6. export PATH=$OPENOCD:$PATH
  7.  
  8. # VARIABLES
  9. #For best compatibility, please use Ubuntu 20.04 (0.10.0) or greater!
  10. packages=("binutils-arm-none-eabi" "python3" "libhidapi-hidraw0" "libftdi1" "libftdi1-2" "git" "make" "python3-pip")
  11. openocd=(openocd-git_*_amd64.deb)
  12. options=("Backup & Restore Tools" "Retro-Go" "Custom Firmware" "Exit")
  13. chips=("4Mb" "64Mb" "Quit")
  14. gwVersion=("Mario" "Zelda" "Quit")
  15. cfwDir="$PWD/game-and-watch-patch"
  16. backupDir="$PWD/game-and-watch-backup/backups"
  17. backupErr="Can't find any backup files. In order to proceed you need to extract them from your gnw system, using 'Backup & Restore Tools'"
  18.  
  19. installDependencies() {
  20.   ## Prompt the user
  21.   read -rp "Do you want to install missing libraries? [Y/n]: " answer
  22.   ## Set the default value if no answer was given
  23.   answer=${answer:Y}
  24.   ## If the answer matches y or Y, install
  25.   echo "Installing required tools..."
  26.   [[ $answer =~ [Yy] ]] && sudo apt-get install "${packages[@]}"
  27. }
  28.  
  29. errorMsg() {
  30.   echo "$backupErr"
  31.   exit
  32. }
  33.  
  34. toolsStep() {
  35.   clear
  36.   PS3='Please select what do you want to do: '
  37.   # options=("Backup & Restore Tools" "Retro-Go" "Custom Firmware" "Exit")
  38.   echo ""
  39.   # local opt
  40.  
  41.   select opt in "${options[@]}"; do
  42.     case $opt in
  43.     "Backup & Restore Tools")
  44.       echo "Before continue please check the correct pinout https://imgur.com/6IxzPi9. Press return when ready!"
  45.       read -rn 1
  46.       echo "Running Backup & Restore Tools Sanity Checks:"
  47.       echo ""
  48.       cd game-and-watch-backup || exit
  49.       OPENOCD="/opt/openocd-git/bin/openocd"
  50.       ./1_sanity_check.sh "$ADAPTER" "$DEVICE"
  51.       echo ""
  52.       OPENOCD="/opt/openocd-git/bin/openocd"
  53.       ./2_backup_flash.sh "$ADAPTER" "$DEVICE"
  54.       echo ""
  55.       OPENOCD="/opt/openocd-git/bin/openocd"
  56.       ./3_backup_internal_flash.sh "$ADAPTER" "$DEVICE"
  57.       echo ""
  58.       OPENOCD="/opt/openocd-git/bin/openocd"
  59.       ./4_unlock_device.sh "$ADAPTER" "$DEVICE"
  60.       echo ""
  61.       OPENOCD="/opt/openocd-git/bin/openocd"
  62.       ./5_restore.sh "$ADAPTER" "$DEVICE"
  63.       cd ..
  64.       ;;
  65.     "Retro-Go")
  66.       echo "Running Retro-Go:"
  67.       echo ""
  68.  
  69.       cd game-and-watch-retro-go || exit
  70.       romChecker
  71.       # OPENOCD="/opt/openocd-git/bin/openocd"
  72.       # GCC_PATH="../gcc-arm-none-eabi-10.3-2021.10/bin"
  73.       # uncomment next 2 lines assuming you have upgraded the external flash to something larger than 4MB.
  74.  
  75.       # flashSize
  76.       # make -j"$(nproc)" "$LF" flash
  77.  
  78.       # comment next line if you use the ones above.
  79.       # make -j"$(nproc)" flash
  80.       make clean
  81.       OPENOCD="/opt/openocd-git/bin/openocd"
  82.       GCC_PATH="../gcc-arm-none-eabi-10.3-2021.10/bin"
  83.       [ "$DEVICE" == "mario" ] && make -j"$(nproc)" INTFLASH_BANK=2 flash
  84.  
  85.       if [ "$DEVICE" == "zelda" ]; then
  86.         [ "$CHIP" == "4Mb" ] && make -j"$(nproc)" INTFLASH_BANK=2 EXTFLASH_SIZE=1802240 EXTFLASH_OFFSET=851968 GNW_TARGET=zelda EXTENDED=1 flash
  87.         [ "$CHIP" == "64Mb" ] && make -j"$(nproc)" EXTFLASH_SIZE_MB=60 EXTFLASH_OFFSET=4194304 INTFLASH_BANK=2 flash
  88.       fi
  89.       cd ..
  90.       ;;
  91.     "Custom Firmware")
  92.       echo "Custom firmware for the newer Nintendo Game and Watch consoles."
  93.       echo ""
  94.  
  95.       [ ! -f "$backupDir/internal_flash_backup_${DEVICE}.bin" ] || [ ! -f "$backupDir/flash_backup_${DEVICE}.bin" ] && errorMsg
  96.  
  97.       [ ! -f "$cfwDir/flash_backup_${DEVICE}.bin" ] && cp "$backupDir/flash_backup_${DEVICE}.bin" "$cfwDir/flash_backup_${DEVICE}.bin"
  98.       [ ! -f "$cfwDir/internal_flash_backup_${DEVICE}.bin" ] && cp "$backupDir/internal_flash_backup_${DEVICE}.bin" "$cfwDir/internal_flash_backup_${DEVICE}.bin"
  99.  
  100.       cd game-and-watch-patch || exit
  101.  
  102.       make clean
  103.       OPENOCD="/opt/openocd-git/bin/openocd"
  104.       [ "$DEVICE" == "mario" ] && make PATCH_PARAMS="--device=mario --internal-only" ADAPTER="$ADAPTER" flash_patched
  105.  
  106.       if [ "$DEVICE" == "zelda" ]; then
  107.         [ "$CHIP" == "4Mb" ] && make PATCH_PARAMS="--device=zelda --extended --no-la --no-sleep-images --extended" ADAPTER="$ADAPTER" flash
  108.         [ "$CHIP" == "64Mb" ] && make PATCH_PARAMS="--device=zelda" ADAPTER="$ADAPTER" LARGE_FLASH=1 flash_patched
  109.       fi
  110.  
  111.       cd ..
  112.       cd game-and-watch-retro-go || exit
  113.       romChecker
  114.       make clean
  115.       OPENOCD="/opt/openocd-git/bin/openocd"
  116.       GCC_PATH="../gcc-arm-none-eabi-10.3-2021.10/bin"
  117.       [ "$DEVICE" == "mario" ] && make -j"$(nproc)" INTFLASH_BANK=2 flash
  118.  
  119.       if [ "$DEVICE" == "zelda" ]; then
  120.         [ "$CHIP" == "4Mb" ] && make -j"$(nproc)" INTFLASH_BANK=2 EXTFLASH_SIZE=1802240 EXTFLASH_OFFSET=851968 GNW_TARGET=zelda EXTENDED=1 flash
  121.         [ "$CHIP" == "64Mb" ] && make -j"$(nproc)" EXTFLASH_SIZE_MB=60 EXTFLASH_OFFSET=4194304 INTFLASH_BANK=2 flash
  122.       fi
  123.       ;;
  124.     "Exit")
  125.       exit
  126.       ;;
  127.     *) echo "invalid option $REPLY" ;;
  128.     esac
  129.   done
  130. }
  131.  
  132. romChecker() {
  133.   roms="col gb gg gw nes pce sg sms"
  134.   for rom in $roms; do
  135.     files=$(find "roms/$rom" -maxdepth 1 -type f -name "*.$rom" 2>/dev/null | wc -l)
  136.     has_games="false"
  137.     if [ "$files" != "0" ]; then
  138.       has_games="true"
  139.       break
  140.     fi
  141.   done
  142.  
  143.   if [ "${has_games}" = "false" ]; then
  144.     echo "Didn't found any rom files!!"
  145.     echo ""
  146.     echo "Please place extracted roms on the correct directory and try again."
  147.     echo ""
  148.     echo "COL roms in 'game-and-watch-retro-go/roms/col/';"
  149.     echo "GB roms in 'game-and-watch-retro-go/roms/gb/';"
  150.     echo "GG roms in 'game-and-watch-retro-go/roms/gg/';"
  151.     echo "GW roms in 'game-and-watch-retro-go/roms/gw/';"
  152.     echo "NES roms in 'game-and-watch-retro-go/roms/nes/';"
  153.     echo "PCE roms in 'game-and-watch-retro-go/roms/pce/';"
  154.     echo "SG roms in 'game-and-watch-retro-go/roms/sg/';"
  155.     echo "SMS roms in 'game-and-watch-retro-go/roms/sms/';"
  156.     echo ""
  157.     exit
  158.   fi
  159. }
  160.  
  161. optionsDefaultValue() {
  162.   local item i=0 numItems=$#
  163.  
  164.   # Print numbered menu items, based on the arguments passed.
  165.   for item; do # Short for: for item in "$@"; do
  166.     printf '%s\n' "$((++i))) $item"
  167.   done >&2 # Print to stderr, as `select` does.
  168.  
  169.   # Prompt the user for the index of the desired item.
  170.   while :; do
  171.     printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
  172.     read -r index
  173.     # Make sure that the input is either empty or that a valid index was entered.
  174.     [[ -z $index ]] && break # empty input
  175.     ((index >= 1 && index <= numItems)) 2>/dev/null || {
  176.       echo "Invalid selection. Please try again." >&2
  177.       continue
  178.     }
  179.     break
  180.   done
  181.  
  182.   # Output the selected item, if any.
  183.   [[ -n $index ]] && printf %s "${@:index:1}"
  184.  
  185. }
  186.  
  187. chipSize() {
  188.   echo ""
  189.   echo "Select the size of your flash chip.
  190. 4 is the default:"
  191.   echo ""
  192.  
  193.   # chips=("4Mb" "64Mb" "Quit")
  194.   chp=$(optionsDefaultValue "${chips[@]}")
  195.  
  196.   case $chp in
  197.   "" | "4Mb")
  198.     CHIP="4Mb"
  199.     ;;
  200.   "64Mb")
  201.     CHIP="64Mb"
  202.     ;;
  203.   "Quit")
  204.     exit
  205.     ;;
  206.   esac
  207.   echo ""
  208.   echo "You selected a $CHIP Flash Chip"
  209.   selectDebugger
  210. }
  211.  
  212. selectDebugger() {
  213.   echo ""
  214.   echo "You must configure this for the debug adapter you're using!
  215. stlink is the default:"
  216.   echo ""
  217.  
  218.   adapters=("J-Link" "Raspberry Pi" "ST-LINK" "Quit")
  219.   adp=$(optionsDefaultValue "${adapters[@]}")
  220.  
  221.   case $adp in
  222.   "J-Link")
  223.     ADAPTER="jlink"
  224.     ;;
  225.   "Raspberry Pi")
  226.     ADAPTER="rpi"
  227.     ;;
  228.   "" | "ST-LINK")
  229.     ADAPTER="stlink"
  230.     ;;
  231.   "Quit")
  232.     exit
  233.     ;;
  234.   esac
  235.   echo ""
  236.   echo "You choose $ADAPTER as your ARM debug probe"
  237.   toolsStep
  238. }
  239.  
  240. selectGWType() {
  241.   echo ""
  242.   echo "Please select your Game & Watch type.
  243. Mario is the default:"
  244.   echo ""
  245.  
  246.   # gwVersion=("Mario" "Zelda" "Quit")
  247.   gw=$(optionsDefaultValue "${gwVersion[@]}")
  248.  
  249.   case $gw in
  250.   "" | "Mario")
  251.     DEVICE="mario"
  252.     ;;
  253.   "Zelda")
  254.     DEVICE="zelda"
  255.     chipSize
  256.     ;;
  257.   "Quit")
  258.     exit
  259.     ;;
  260.   *) echo "invalid option $REPLY" ;;
  261.   esac
  262.   echo ""
  263.   echo "You choose $DEVICE as your Game & Watch"
  264.   selectDebugger
  265. }
  266.  
  267. getRequirements() {
  268.   ## Run the installDependencies function if any of the libraries are missing.
  269.   dpkg -s "${packages[@]}" >/dev/null 2>&1 || installDependencies
  270.  
  271.   [ ! -f "$PWD/openocd-git.deb.zip" ] && echo "Downloading OpenOCD..."
  272.   [ ! -f "$PWD/openocd-git.deb.zip" ] && wget https://nightly.link/kbeckmann/ubuntu-openocd-git-builder/workflows/docker/master/openocd-git.deb.zip
  273.  
  274.   if [ ! -e "${openocd[0]}" ]; then
  275.     echo "Unpacking download..."
  276.     unzip -u openocd-git.deb.zip
  277.   fi
  278.  
  279.   dpkg -s openocd-git >/dev/null 2>&1 || sudo dpkg -i openocd-git_*_amd64.deb
  280.   dpkg -s openocd-git >/dev/null 2>&1 || echo "Installing OpenOCD..."
  281.   dpkg -s openocd-git >/dev/null 2>&1 || sudo apt-get -y -f install
  282.  
  283.   if [ ! -d "game-and-watch-backup" ]; then
  284.     echo "Cloning and building the Backup & Restore Tools:"
  285.     git clone https://github.com/ghidraninja/game-and-watch-backup
  286.     cd game-and-watch-backup || exit
  287.     cd ..
  288.   fi
  289.  
  290.   if [ ! -d "game-and-watch-patch" ]; then
  291.     echo "Cloning Custom Firmware:"
  292.     git clone https://github.com/BrianPugh/game-and-watch-patch
  293.     cd game-and-watch-patch || exit
  294.     pip3 install -r requirements.txt
  295.     make download_sdk
  296.     cd ..
  297.  
  298.   fi
  299.  
  300.   [ ! -f "$GCC_PATH/arm-none-eabi-gcc" ] && echo "Extracting gcc-arm-none-eabi"
  301.   [ ! -f "$PWD/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2" ] && wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
  302.   [ ! -f "$GCC_PATH/arm-none-eabi-gcc" ] && tar -xf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
  303.  
  304.   if [ ! -d "game-and-watch-retro-go" ]; then
  305.     echo "Cloning and building Retro-Go"
  306.     git clone --recurse-submodules https://github.com/kbeckmann/game-and-watch-retro-go
  307.   fi
  308. }
  309.  
  310. getRequirements
  311.  
  312. selectGWType
  313.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement