9c63272b34b2261cd77cce868b93b31b0b6ce905
[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         asm ("mov sp, %0" : : "r" (sp));
30         asm ("mov lr, %0" : : "r" (pc));
31         asm ("bx lr");
32 }
33
34 #define AO_BOOT_SIGNAL  0x5a5aa5a5
35 #define AO_BOOT_CHECK   0xc3c33c3c
36
37 struct ao_boot {
38         uint32_t        *base;
39         uint32_t        signal;
40         uint32_t        check;
41 };
42
43 static struct ao_boot __attribute__ ((section(".boot"))) ao_boot;
44         
45 void
46 ao_boot_check_chain(void)
47 {
48         if (ao_boot.signal == AO_BOOT_SIGNAL && ao_boot.check == AO_BOOT_CHECK) {
49                 ao_boot.signal = 0;
50                 ao_boot.check = 0;
51                 ao_boot_chain(ao_boot.base);
52         }
53 }
54
55 void
56 ao_boot_reboot(uint32_t *base)
57 {
58         ao_boot.base = base;
59         ao_boot.signal = AO_BOOT_SIGNAL;
60         ao_boot.check = AO_BOOT_CHECK;
61         ao_arch_reboot();
62 }