forked from Teeed/rboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rboot-stage2a.c
77 lines (60 loc) · 1.73 KB
/
rboot-stage2a.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
//////////////////////////////////////////////////
// rBoot open source boot loader for ESP8266.
// Copyright 2015 Richard A Burton
// See license.txt for license terms.
//////////////////////////////////////////////////
#ifdef RBOOT_INTEGRATION
#include <rboot-integration.h>
#endif
#include "rboot-private.h"
usercode* NOINLINE load_rom(uint32 readpos) {
uint8 sectcount;
uint8 *writepos;
uint32 remaining;
usercode* usercode;
rom_header header;
section_header section;
// read rom header
SPIRead(readpos, &header, sizeof(rom_header));
readpos += sizeof(rom_header);
// create function pointer for entry point
usercode = header.entry;
// copy all the sections
for (sectcount = header.count; sectcount > 0; sectcount--) {
// read section header
SPIRead(readpos, §ion, sizeof(section_header));
readpos += sizeof(section_header);
// get section address and length
writepos = section.address;
remaining = section.length;
while (remaining > 0) {
// work out how much to read, up to 16 bytes at a time
uint32 readlen = (remaining < READ_SIZE) ? remaining : READ_SIZE;
// read the block
SPIRead(readpos, writepos, readlen);
readpos += readlen;
// increment next write position
writepos += readlen;
// decrement remaining count
remaining -= readlen;
}
}
return usercode;
}
#ifdef BOOT_NO_ASM
void call_user_start(uint32 readpos) {
usercode* user;
user = load_rom(readpos);
user();
}
#else
void call_user_start(uint32 readpos) {
__asm volatile (
"mov a15, a0\n" // store return addr, we already splatted a15!
"call0 load_rom\n" // load the rom
"mov a0, a15\n" // restore return addr
"jx a2\n" // now jump to the rom code
);
}
#endif