Browse Source

Chitu board support (e.g., Tronxy X5s) (#15493)

J.C. Nelson 5 years ago
parent
commit
167ecd8620

+ 14
- 0
buildroot/share/PlatformIO/ldscripts/chitu_f103.ld View File

@@ -0,0 +1,14 @@
1
+MEMORY
2
+{
3
+  ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
4
+  rom (rx)  : ORIGIN = 0x08008800, LENGTH = 512K - 32K
5
+}
6
+
7
+/* Provide memory region aliases for common.inc */
8
+REGION_ALIAS("REGION_TEXT", rom);
9
+REGION_ALIAS("REGION_DATA", ram);
10
+REGION_ALIAS("REGION_BSS", ram);
11
+REGION_ALIAS("REGION_RODATA", rom);
12
+
13
+/* Let common.inc handle the real work. */
14
+INCLUDE common.inc

+ 119
- 0
buildroot/share/PlatformIO/scripts/chitu_crypt.py View File

@@ -0,0 +1,119 @@
1
+Import("env")
2
+import struct
3
+
4
+# Relocate firmware from 0x08000000 to 0x08008800
5
+for define in env['CPPDEFINES']:
6
+    if define[0] == "VECT_TAB_ADDR":
7
+        env['CPPDEFINES'].remove(define)
8
+env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x8008800"))
9
+env.Replace(LDSCRIPT_PATH="buildroot/share/PlatformIO/ldscripts/chitu_f103.ld")
10
+
11
+def calculate_crc(contents, seed):
12
+    accumulating_xor_value = seed;
13
+
14
+    for i in range(0, len(contents), 4):
15
+        value = struct.unpack('<I', contents[ i : i + 4])[0]
16
+        accumulating_xor_value = accumulating_xor_value ^ value
17
+    return accumulating_xor_value
18
+
19
+def xor_block(r0, r1, block_number, block_size, file_key):
20
+    # This is the loop counter
21
+    loop_counter = 0x0
22
+
23
+    # This is the key length
24
+    key_length = 0x18
25
+
26
+    # This is an initial seed
27
+    xor_seed = 0x4bad
28
+
29
+    # This is the block counter
30
+    block_number = xor_seed * block_number
31
+
32
+    #load the xor key from the file
33
+    r7 =  file_key
34
+
35
+    for loop_counter in range(0, block_size):
36
+        # meant to make sure different bits of the key are used.
37
+        xor_seed = int(loop_counter/key_length)
38
+
39
+        # IP is a scratch register / R12
40
+        ip = loop_counter - (key_length * xor_seed)
41
+
42
+        # xor_seed = (loop_counter * loop_counter) + block_number
43
+        xor_seed = (loop_counter * loop_counter) + block_number
44
+
45
+        # shift the xor_seed left by the bits in IP.
46
+        xor_seed = xor_seed >> ip
47
+
48
+        # load a byte into IP
49
+        ip = r0[loop_counter]
50
+
51
+        # XOR the seed with r7
52
+        xor_seed = xor_seed ^ r7
53
+
54
+        # and then with IP
55
+        xor_seed = xor_seed ^ ip
56
+
57
+        #Now store the byte back
58
+        r1[loop_counter] = xor_seed & 0xFF
59
+
60
+        #increment the loop_counter
61
+        loop_counter = loop_counter + 1
62
+
63
+
64
+def encrypt_file(input, output_file, file_length):
65
+    input_file = bytearray(input.read())
66
+    block_size = 0x800
67
+    key_length = 0x18
68
+    file_key = 0xDAB27F94
69
+
70
+    xor_crc = 0xef3d4323;
71
+
72
+    # the input file is exepcted to be in chunks of 0x800
73
+    # so round the size
74
+    while len(input_file) % block_size != 0:
75
+        input_file.extend(b'0x0')
76
+
77
+    # write the file header
78
+    output_file.write(struct.pack(">I", 0x443D2D3F))
79
+    # encrypt the contents using a known file header key
80
+
81
+    # write the file_key
82
+    output_file.write(struct.pack(">I", 0x947FB2DA))
83
+
84
+    #TODO - how to enforce that the firmware aligns to block boundaries?
85
+    block_count = int(len(input_file) / block_size)
86
+    print "Block Count is ", block_count
87
+    for block_number in range(0, block_count):
88
+        block_offset = (block_number * block_size)
89
+        block_end = block_offset + block_size
90
+        block_array = bytearray(input_file[block_offset: block_end])
91
+        xor_block(block_array, block_array, block_number, block_size, file_key)
92
+        for n in range (0, block_size):
93
+            input_file[block_offset + n] = block_array[n]
94
+
95
+        # update the expected CRC value.
96
+        xor_crc = calculate_crc(block_array, xor_crc)
97
+
98
+    # write CRC
99
+    output_file.write(struct.pack("<I", xor_crc))
100
+
101
+    # finally, append the encrypted results.
102
+    output_file.write(input_file)
103
+    return
104
+
105
+
106
+# Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
107
+def encrypt(source, target, env):
108
+    import os
109
+
110
+    firmware = open(target[0].path, "rb")
111
+    update = open(target[0].dir.path +'/update.cbd', "wb")
112
+    length = os.path.getsize(target[0].path)
113
+
114
+    encrypt_file(firmware, update, length)
115
+
116
+    firmware.close()
117
+    update.close()
118
+
119
+env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);

+ 27
- 13
platformio.ini View File

@@ -527,6 +527,33 @@ src_filter    = ${common.default_src_filter} +<src/HAL/HAL_TEENSY31_32>
527 527
 monitor_speed = 250000
528 528
 
529 529
 #
530
+# Malyan M200 (STM32F103CB)
531
+#
532
+[env:STM32F103CB_malyan]
533
+platform    = ststm32
534
+framework   = arduino
535
+board       = malyanM200
536
+build_flags = !python Marlin/src/HAL/HAL_STM32F1/build_flags.py -DMCU_STM32F103CB -D __STM32F1__=1 -std=c++1y -D MOTHERBOARD="BOARD_MALYAN_M200" -DSERIAL_USB -ffunction-sections -fdata-sections -Wl,--gc-sections
537
+  -DDEBUG_LEVEL=0 -D__MARLIN_FIRMWARE__
538
+src_filter  = ${common.default_src_filter} +<src/HAL/HAL_STM32F1>
539
+lib_ignore  = Adafruit NeoPixel, LiquidCrystal, LiquidTWI2, TMCStepper, U8glib-HAL, SPI
540
+
541
+#
542
+# Chitu boards like Tronxy X5s (STM32F103ZET6)
543
+#
544
+[env:chitu_f103]
545
+platform      = ststm32
546
+framework     = arduino
547
+board         = genericSTM32F103ZE
548
+extra_scripts = buildroot/share/PlatformIO/scripts/chitu_crypt.py
549
+build_flags   = !python Marlin/src/HAL/HAL_STM32F1/build_flags.py
550
+  ${common.build_flags} -DSTM32F1xx -std=gnu++14
551
+build_unflags = -std=gnu++11 -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG= -DERROR_LED_PORT=GPIOE -DERROR_LED_PIN=6
552
+src_filter    = ${common.default_src_filter} +<src/HAL/HAL_STM32F1>
553
+lib_deps      = ${common.lib_deps}
554
+lib_ignore    = Adafruit NeoPixel
555
+
556
+#
530 557
 # Teensy 3.5 / 3.6 (ARM Cortex-M4)
531 558
 #
532 559
 [env:teensy35]
@@ -541,19 +568,6 @@ src_filter    = ${common.default_src_filter} +<src/HAL/HAL_TEENSY35_36>
541 568
 monitor_speed = 250000
542 569
 
543 570
 #
544
-# Malyan M200 (STM32F103CB)
545
-#
546
-[env:STM32F103CB_malyan]
547
-platform    = ststm32
548
-framework   = arduino
549
-board       = malyanM200
550
-build_flags = !python Marlin/src/HAL/HAL_STM32F1/build_flags.py -DMCU_STM32F103CB -D __STM32F1__=1 -std=c++1y -D MOTHERBOARD="BOARD_MALYAN_M200" -DSERIAL_USB -ffunction-sections -fdata-sections -Wl,--gc-sections
551
-  -DDEBUG_LEVEL=0 -D__MARLIN_FIRMWARE__
552
-src_filter  = ${common.default_src_filter} +<src/HAL/HAL_STM32F1>
553
-#-<frameworks>
554
-lib_ignore  = Adafruit NeoPixel, LiquidCrystal, LiquidTWI2, TMCStepper, U8glib-HAL, SPI
555
-
556
-#
557 571
 # Espressif ESP32
558 572
 #
559 573
 [env:esp32]

Loading…
Cancel
Save