-#define POLY 0x04c11db7
+#include <stdio.h>
-/* Theory of operation:
- * (arm-elf-objdump -b binary -m arm -M reg-names-raw -D RSB_S2_SINGLE.bin)
- *
- * 440: push {r4, r5, r6, r7, r8, r9, r10, r11, r14}
- * 444: mov r11, r0
- * 448: mov r10, r1
- * 44c: mov r14, r2
- * 450: mov r6, #0 ; 0x0
- * 454: b 0x4a0
- * 458: add r3, r6, r10
- * 45c: ldrb r3, [r3]
- * 460: lsl r3, r3, #24
- * 464: eor r11, r11, r3
- * 468: mov r5, #8 ; 0x8
- * 46c: and r3, r11, #-2147483648 ; 0x80000000
- * 470: cmp r3, #0 ; 0x0
- * 474: beq 0x48c
- * 478: lsl r3, r11, #1
- * 47c: ldr r12, [pc, #64] ; 0x4c4
- * 480: eor r0, r3, r12
- * 484: mov r11, r0
- * 488: b 0x490
- * 48c: lsl r11, r11, #1
- * 490: sub r5, r5, #1 ; 0x1
- * 494: cmp r5, #0 ; 0x0
- * 498: bne 0x46c
- * 49c: add r6, r6, #1 ; 0x1
- * 4a0: cmp r6, r14
- * 4a4: blt 0x458
- * 4a8: mov r0, r11
- * 4ac: pop {r4, r5, r6, r7, r8, r9, r10, r11, r15}
- * 4c4: DATA: 0x04c11db7
- */
+#define POLY 0x04c11db7
-unsigned int rsb_crc(unsigned int r11, unsigned char *r10, unsigned int r14) {
- unsigned int r6 = 0;
- unsigned int r3;
- int r5;
+unsigned int rsb_crc(unsigned int r11_crc, unsigned char *r10_buf, unsigned int r14_len) {
+ unsigned int r6_pos = 0;
+ unsigned int r3_data;
+ int r5_bit;
- while (r6 < r14) {
- r3 = (*(r6+r10)) << 24;
- r11 = r11 ^ r3;
+ while (r6_pos < r14_len) {
+ r3_data = (*(r6_pos+r10_buf)) << 24;
+ r11_crc = r11_crc ^ r3_data;
- r5 = 8;
+ r5_bit = 8;
do {
- r3 = r11 & 0x80000000;
+ r3_data = r11_crc & 0x80000000;
- if (r3 != 0) {
- r3 = r11 << 1;
- r11 = r3 ^ POLY;
+ if (r3_data != 0) {
+ r3_data = r11_crc << 1;
+ r11_crc = r3_data ^ POLY;
} else {
- r11 = r11 << 1;
+ r11_crc = r11_crc << 1;
}
- r5--;
- } while (r5);
+ r5_bit--;
+ } while (r5_bit);
- r6++;
+ r6_pos++;
}
- return r11;
+ return r11_crc;
+}
+
+unsigned int rsb_crc2(unsigned char *r0_buf, unsigned int r1_buflen, unsigned int r2_magic, unsigned int *crc_out) {
+ unsigned int r4_len;
+ unsigned int file_crc;
+
+ r4_len = *(unsigned int*)(r0_buf + 0x20);
+
+ if (*((unsigned int*)(r0_buf + 0x24)) != r2_magic)
+ return 2; /* MAGIC does not match */
+
+ if (r1_buflen < r4_len)
+ return 3; /* image to small */
+
+ *crc_out = ~rsb_crc(~0x0, r0_buf, r4_len);
+
+ file_crc = *((unsigned int*)(r0_buf + r4_len));
+
+ if (file_crc != *crc_out)
+ return 4;
+
+ return 0;
}