root/loader/sx1/resetcode/main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. copy_and_restart

   1 
   2 /*
   3  * Operating system has died. Known functions will be killed
   4  * after memmove.
   5  *
   6  * Make sure stack is not used.
   7  */
   8  #define LED_AF (void*)0xC02200B4       //LED_AF
   9  #define DELAY 5000000
  10 
  11 void __attribute__((noreturn)) copy_and_restart(void *dst_void, const void *src_void, long length) {
  12 
  13         {
  14                 
  15                 volatile long *p = (void*)LED_AF;       // turned off later, so assumed to be power
  16       
  17         int counter;
  18 
  19         // DEBUG: blink led
  20         counter = DELAY; *p = 0x46;  while (counter--) { asm("nop\n nop\n"); };
  21         counter = DELAY; *p = 0x44;  while (counter--) { asm("nop\n nop\n"); };
  22 
  23                 char *dst = dst_void;
  24                 const char *src = src_void;
  25 
  26                 if (src < dst && dst < src + length)
  27                 {
  28                         /* Have to copy backwards */
  29                         src += length;
  30                         dst += length;
  31                         while (length--)
  32                         {
  33                                 *--dst = *--src;
  34                         }
  35                 }
  36                 else
  37                 {
  38                         while (length--)
  39                         {
  40                                 *dst++ = *src++;
  41                         }
  42                 }
  43         }
  44 
  45         asm volatile (   //found at FF829920
  46                 
  47   //               "STMFD   SP!, {R4,LR}\n"
  48   //             "MOV     R4, R0\n"
  49   //              "BL      sub_FF81F200\n"
  50     //             "BL      sub_FF87E17C\n"
  51       //           "BL      sub_FF85EC24\n"
  52         //         "CMP     R4, #3\n"
  53           //       "BLNE    sub_FF84E034\n"             
  54                     //     "BL      sub_FF8206E8\n"
  55                       //   "BL      sub_FF84A02C\n"
  56                     // "MOV     R0, #3\n"
  57         //               "BL      sub_FF850B54\n"
  58                 //         "BL      sub_FF827A6C\n"
  59                 
  60                  "MRS     R0, CPSR\n"
  61                  "BIC     R0, R0, #0x3F\n"
  62                  "ORR     R0, R0, #0xD3\n"
  63                  "MSR     CPSR, R0\n"
  64                  "LDR     R1, =0xC0200000\n"
  65                  "MVN     R0, #0\n"
  66                  "STR     R0, [R1,#0x10C]\n"
  67                  "STR     R0, [R1,#0xC]\n"
  68                  "STR     R0, [R1,#0x1C]\n"
  69                  "STR     R0, [R1,#0x2C]\n"
  70                  "STR     R0, [R1,#0x3C]\n"
  71                  "STR     R0, [R1,#0x4C]\n"
  72                  "STR     R0, [R1,#0x5C]\n"
  73                  "STR     R0, [R1,#0x6C]\n"
  74                  "STR     R0, [R1,#0x7C]\n"
  75                  "STR     R0, [R1,#0x8C]\n"
  76                  "STR     R0, [R1,#0x9C]\n"
  77                  "STR     R0, [R1,#0xAC]\n"
  78                  "STR     R0, [R1,#0xBC]\n"
  79                  "STR     R0, [R1,#0xCC]\n"
  80                  "STR     R0, [R1,#0xDC]\n"
  81                  "STR     R0, [R1,#0xEC]\n"
  82                  "CMP     R4, #7\n"
  83                  "STR     R0, [R1,#0xFC]\n"
  84                  "LDMEQFD SP!, {R4,PC}\n"
  85                  "MOV     R0, #0x78\n"
  86                  "MCR     p15, 0, R0,c1,c0\n"
  87                  "MOV     R0, #0\n"
  88                  "MCR     p15, 0, R0,c7,c10, 4\n"
  89                  "MCR     p15, 0, R0,c7,c5\n"
  90                  "MCR     p15, 0, R0,c7,c6\n"
  91                  "MOV     R0, #0x80000006\n"
  92                  "MCR     p15, 0, R0,c9,c1\n"
  93                  "MCR     p15, 0, R0,c9,c1, 1\n"
  94                  "MRC     p15, 0, R0,c1,c0\n"
  95                  "ORR     R0, R0, #0x50000\n"
  96                  "MCR     p15, 0, R0,c1,c0\n"
  97                  "LDR     R0, =0x12345678\n"
  98                  "MOV     R1, #0x80000000\n"
  99                  "STR     R0, [R1,#0xFFC]\n"
 100               // "LDR     R0, =loc_FF810000\n"
 101                  "MOV     R0, %0\n"              // new jump-vector
 102                  "LDMFD   SP!, {R4,LR}\n"
 103                  "BX      R0\n"
 104                  : : "r"(dst_void) : "memory","r0","r1","r2","r3","r4");
 105 
 106         while(1);
 107 }

/* [<][>][^][v][top][bottom][index][help] */