root/loader/sx130is/resetcode/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. copy_and_restart

   1 /*
   2 *********************
   3 SX130
   4 ********************** 
   5 */
   6 
   7 
   8 /*
   9  * Operating system has died. Known functions will be killed
  10  * after memmove.
  11  *
  12  * Make sure stack is not used.
  13  */
  14 
  15 
  16 void __attribute__((noreturn)) copy_and_restart(void *dst_void, const void *src_void, long length)
  17 {
  18 
  19         {
  20                 char *dst = dst_void;
  21                 const char *src = src_void;
  22 
  23                 if (src < dst && dst < src + length) {
  24                         /* Have to copy backwards */
  25                         src += length;
  26                         dst += length;
  27                         while (length--)
  28                         {
  29                                 *--dst = *--src;
  30                         }
  31                 }
  32                 else {
  33                         while (length--) {
  34                                 *dst++ = *src++;
  35                         }
  36                 }
  37         }
  38 
  39         // DEBUG: jump to regular firmware-boot (causing a boot loop)
  40         //dst_void = (void*) 0xFF810000;
  41 
  42         // resetcode here:
  43         // s95 100e @FF83C5E8
  44         // sx130 101c @FF83BE74
  45         // code is the same
  46         asm volatile (
  47                 "MRS     R0, CPSR\n"
  48                 "BIC     R0, R0, #0x3F\n"
  49                 "ORR     R0, R0, #0xD3\n"
  50                 "MSR     CPSR, R0\n"
  51                 "LDR     R1, =0xC0200000\n"
  52                 "MOV    R0, #0xFFFFFFFF\n"
  53                 "STR     R0, [R1,#0x10C]\n"
  54                 "STR     R0, [R1,#0xC]\n"
  55                 "STR     R0, [R1,#0x1C]\n"
  56                 "STR     R0, [R1,#0x2C]\n"
  57                 "STR     R0, [R1,#0x3C]\n"
  58                 "STR     R0, [R1,#0x4C]\n"
  59                 "STR     R0, [R1,#0x5C]\n"
  60                 "STR     R0, [R1,#0x6C]\n"
  61                 "STR     R0, [R1,#0x7C]\n"
  62                 "STR     R0, [R1,#0x8C]\n"
  63                 "STR     R0, [R1,#0x9C]\n"
  64                 "STR     R0, [R1,#0xAC]\n"
  65                 "STR     R0, [R1,#0xBC]\n"
  66                 "STR     R0, [R1,#0xCC]\n"
  67                 "STR     R0, [R1,#0xDC]\n"
  68                 "STR     R0, [R1,#0xEC]\n"
  69                 "CMP     R4, #7\n"
  70                 "STR     R0, [R1,#0xFC]\n"
  71                 "LDMEQFD SP!, {R4,PC}\n"
  72                 "MOV     R0, #0x78\n"
  73                 "MCR     p15, 0, R0,c1,c0\n"
  74                 "MOV     R0, #0\n"
  75                 "MCR     p15, 0, R0,c7,c10, 4\n"
  76                 "MCR     p15, 0, R0,c7,c5\n"
  77                 "MCR     p15, 0, R0,c7,c6\n"
  78                 "MOV     R0, #0x80000006\n"
  79                 "MCR     p15, 0, R0,c9,c1\n"
  80                 "MCR     p15, 0, R0,c9,c1, 1\n"
  81                 "MRC     p15, 0, R0,c1,c0\n"
  82                 "ORR     R0, R0, #0x50000\n"
  83                 "MCR     p15, 0, R0,c1,c0\n"
  84                 "LDR     R0, =0x12345678\n"
  85                 "MOV     R1, #0x80000000\n"
  86                 "STR     R0, [R1,#0xFFC]\n"
  87 
  88           //"LDR     R0, =0xFF810000\n"   // original jump-vector
  89                 "MOV     R0, %0\n"              // new jump-vector
  90 
  91                 "LDMFD   SP!, {R4,LR}\n"
  92                 "BX      R0\n"
  93                 : : "r"(dst_void) : "memory","r0","r1","r2","r3","r4"
  94         );
  95 
  96         while(1);
  97 }

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