From 7f10f8932eb6ca377bcc69fae2bd9b72e4aca172 Mon Sep 17 00:00:00 2001
From: "J.C. Nelson" <32139633+xC0000005@users.noreply.github.com>
Date: Fri, 5 Aug 2022 22:09:46 -0700
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Trigorilla=20Pro=20disk=20based?=
 =?UTF-8?q?=20update=20(#24591)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/pins/pins.h                        |  2 +-
 .../share/PlatformIO/scripts/chitu_crypt.py   | 21 ++++++++++++++-----
 ini/stm32f1.ini                               | 19 +++++++++++++++++
 3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h
index a60365d980c..37e222d004f 100644
--- a/Marlin/src/pins/pins.h
+++ b/Marlin/src/pins/pins.h
@@ -590,7 +590,7 @@
 #elif MB(CREALITY_V25S1)
   #include "stm32f1/pins_CREALITY_V25S1.h"      // STM32F1                                env:STM32F103RE_creality_smartPro env:STM32F103RE_creality_smartPro_maple
 #elif MB(TRIGORILLA_PRO)
-  #include "stm32f1/pins_TRIGORILLA_PRO.h"      // STM32F1                                env:trigorilla_pro env:trigorilla_pro_maple
+  #include "stm32f1/pins_TRIGORILLA_PRO.h"      // STM32F1                                env:trigorilla_pro env:trigorilla_pro_maple env:trigorilla_pro_disk
 #elif MB(FLY_MINI)
   #include "stm32f1/pins_FLY_MINI.h"            // STM32F1                                env:FLY_MINI env:FLY_MINI_maple
 #elif MB(FLSUN_HISPEED)
diff --git a/buildroot/share/PlatformIO/scripts/chitu_crypt.py b/buildroot/share/PlatformIO/scripts/chitu_crypt.py
index 54b83757134..76792030cf7 100644
--- a/buildroot/share/PlatformIO/scripts/chitu_crypt.py
+++ b/buildroot/share/PlatformIO/scripts/chitu_crypt.py
@@ -4,7 +4,9 @@
 #
 import pioutil
 if pioutil.is_pio_build():
-	import struct,uuid
+	import struct,uuid,marlin
+
+	board = marlin.env.BoardConfig()
 
 	def calculate_crc(contents, seed):
 		accumulating_xor_value = seed;
@@ -104,12 +106,21 @@ if pioutil.is_pio_build():
 	# Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
 	def encrypt(source, target, env):
 		from pathlib import Path
+
 		fwpath = Path(target[0].path)
 		fwsize = fwpath.stat().st_size
-		fwfile = fwpath.open("rb")
-		upfile = Path(target[0].dir.path, 'update.cbd').open("wb")
-		encrypt_file(fwfile, upfile, fwsize)
 
-	import marlin
+		enname = board.get("build.crypt_chitu")
+		enpath = Path(target[0].dir.path)
+
+		fwfile = fwpath.open("rb")
+		enfile = (enpath / enname).open("wb")
+
+		print(f"Encrypting {fwpath} to {enname}")
+		encrypt_file(fwfile, enfile, fwsize)
+		fwfile.close()
+		enfile.close()
+		fwpath.unlink()
+
 	marlin.relocate_firmware("0x08008800")
 	marlin.add_post_action(encrypt);
diff --git a/ini/stm32f1.ini b/ini/stm32f1.ini
index c0415c5f846..d68704216fb 100644
--- a/ini/stm32f1.ini
+++ b/ini/stm32f1.ini
@@ -378,12 +378,31 @@ build_flags          = ${stm32_variant.build_flags}
 build_unflags        = ${stm32_variant.build_unflags}
                        -DUSBCON -DUSBD_USE_CDC
 
+#
+# TRIGORILLA PRO DISK BASED (STM32F103ZET6)
+# Builds for Trigorilla to update from SD
+#
+[env:trigorilla_pro_disk]
+extends              = stm32_variant
+board                = genericSTM32F103ZE
+board_build.crypt_chitu = update.zw
+board_build.variant  = MARLIN_F103Zx
+board_build.offset   = 0x8800
+build_flags          = ${stm32_variant.build_flags}
+                       -DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
+build_unflags        = ${stm32_variant.build_unflags}
+                       -DUSBCON -DUSBD_USE_CDC
+extra_scripts        = ${stm32_variant.extra_scripts}
+                       buildroot/share/PlatformIO/scripts/chitu_crypt.py
+
+
 #
 # Chitu boards like Tronxy X5s (STM32F103ZET6)
 #
 [env:chitu_f103]
 extends              = stm32_variant
 board                = genericSTM32F103ZE
+board_build.crypt_chitu = update.cbd
 board_build.variant  = MARLIN_F103Zx
 board_build.offset   = 0x8800
 build_flags          = ${stm32_variant.build_flags}