Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <inttypes.h>
#include <string.h>

// #define USE_SHORT_POINTER
#ifdef USE_SHORT_POINTER
  typedef uint16_t shortPtr;
#else
  typedef uintptr_t shortPtr;
#endif

typedef struct _mmap {
  uint8_t update[0x5C00];
  uint8_t update_func[0x0400];
  uint8_t program[0x5B80];
  uint8_t iv[0x0080];
} mmap_t;

volatile static mmap_t mem;

volatile void* memset_volatile(volatile void* p_mem, int c, size_t len) {
    volatile char* p = p_mem;
    while (len-- > 0) {
        *p++ = c;
    }
    return p_mem;
}


int main(void) {
  printf("%p, %p, %p, %p\n",
     &mem.update, &mem.update_func, &mem.program, &mem.iv);

  printf("sizeof update: %zd, update_func: %zd, program: %zd, iv: %zd\n",
    sizeof(mem.update), sizeof(mem.update_func), sizeof(mem.program), sizeof(mem.iv));

  memset_volatile(mem.update, 0x00, sizeof(mem.update));
  memset_volatile(mem.update_func, 0x01, sizeof(mem.update_func));
  memset_volatile(mem.program, 0x02, sizeof(mem.program));
  memset_volatile(mem.iv, 0x03, sizeof(mem.iv));

  printf("[0] update: %x, %x, program: %x, iv:%x\n",
     mem.update[0], mem.update_func[0], mem.program[0], mem.iv[0]);

  shortPtr updateSectionPtr;
  shortPtr programSectionPtr;
	uint16_t tempData;

  shortPtr update_start = (shortPtr)mem.update;
  shortPtr update_end = (shortPtr)mem.update + (sizeof(mem.update) - 1);

  shortPtr update_func_start = (shortPtr)mem.update_func;
  shortPtr update_func_end = (shortPtr)mem.update_func + (sizeof(mem.update_func) - 1);
  
  shortPtr program_start = (shortPtr)mem.program;
  shortPtr program_end = (shortPtr)mem.program + (sizeof(mem.program) - 1);

  shortPtr iv_start = (shortPtr)&mem.iv;
  shortPtr iv_end = (shortPtr)&mem.iv + (sizeof(mem.iv) - 1);

  shortPtr section_gap = program_start - update_start;

#ifdef USE_SHORT_POINTER
  printf("update_start: %x, update_end: %x, update_func_start: %x, update_func_end: %x\r\n",
    update_start,  update_end,  update_func_start, update_func_end);
#else
  printf("update_start: %p, update_end: %p, update_func_start: %p, update_func_end: %p\r\n",
    update_start,  update_end,  update_func_start, update_func_end);
#endif

  printf("program_start: %p, program_end: %p, iv_start: %p, iv_end: %p\r\n",
     program_start, program_end,  iv_start,  iv_end);

  printf("section_gap: %p\n", section_gap);

	for (updateSectionPtr = update_start; updateSectionPtr <= update_end; updateSectionPtr += 2) {
		programSectionPtr = updateSectionPtr + section_gap;
    // printf("updateSectionPtr: %p, programSectionPtr: %p\r\n",
    //   (uint64_t) updateSectionPtr, (uint64_t) programSectionPtr);
		tempData = *(uint16_t*)programSectionPtr; // store old word on stack
		// *programSectionPtr = *updateSectionPtr; // copy word from update to program
		// *updateSectionPtr  = tempData; // save old word into update
	}

  printf("[0] update: %x, update_func: %x, program: %x, iv: %x\r\n",
   mem.update[0],  mem.update_func[0],  mem.program[0],  mem.iv[0]);

  return 0;
}