doc: Document the Apogee Lockout setting
[fw/altos] / src / stm / ao_boot_chain.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_boot.h>
20
21 void
22 ao_boot_chain(uint32_t *base)
23 {
24         uint32_t        sp;
25         uint32_t        pc;
26
27         sp = base[0];
28         pc = base[1];
29         if (0x08000100 <= pc && pc <= 0x08200000 && (pc & 1) == 1) {
30                 asm ("mov sp, %0" : : "r" (sp));
31                 asm ("mov lr, %0" : : "r" (pc));
32                 asm ("bx lr");
33         }
34 }
35
36 #define AO_BOOT_SIGNAL  0x5a5aa5a5
37 #define AO_BOOT_CHECK   0xc3c33c3c
38
39 struct ao_boot {
40         uint32_t        *base;
41         uint32_t        signal;
42         uint32_t        check;
43 };
44
45 static struct ao_boot __attribute__ ((section(".boot"))) ao_boot;
46         
47 int
48 ao_boot_check_chain(void)
49 {
50         if (ao_boot.signal == AO_BOOT_SIGNAL && ao_boot.check == AO_BOOT_CHECK) {
51                 ao_boot.signal = 0;
52                 ao_boot.check = 0;
53                 if (ao_boot.base == 0)
54                         return 0;
55                 ao_boot_chain(ao_boot.base);
56         }
57         return 1;
58 }
59
60 void
61 ao_boot_reboot(uint32_t *base)
62 {
63         ao_boot.base = base;
64         ao_boot.signal = AO_BOOT_SIGNAL;
65         ao_boot.check = AO_BOOT_CHECK;
66         ao_arch_reboot();
67 }