-
Notifications
You must be signed in to change notification settings - Fork 3
/
firmware.SConscript
175 lines (137 loc) · 4.21 KB
/
firmware.SConscript
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import subprocess
import os
from tempfile import mkstemp
Import("env")
Import("conf")
Import("objs")
image_elf_size = Action("$SIZE $TARGET", env["IMAGEELFSIZECOMSTR"])
image_elf_headers = Action("$READELF --program-headers $TARGET", env["IMAGEELFHDRCOMSTR"])
image_elf_sections = Action("$READELF --sections $TARGET", env["IMAGEELFHDRCOMSTR"])
###################################################################
# Linking / ELF image generation
###################################################################
image_elf = env.Program(
source = objs,
target = [
File(env["PORTFILE"] + ".elf"),
File(env["PORTFILE"] + ".map"),
],
)
if conf["FW_IMAGE_ELF"] == "y":
Alias("firmware", image_elf)
AddPostAction(image_elf, image_elf_size)
image_bin = env.Command(
target = env["PORTFILE"] + ".bin",
source = env["PORTFILE"] + ".elf",
action = Action("$OBJCOPY -O binary $SOURCE $TARGET", env["CREATEBINCOMSTR"])
)
if conf["ELF_IMAGE_TO_BIN"] == "y":
Alias("firmware", image_bin)
image_strip = env.Command(
target = env["PORTFILE"] + ".elf.strip",
source = env["PORTFILE"] + ".elf",
action = Action("$STRIP $SOURCE -o $TARGET", env["STRIPELFCOMSTR"])
)
# Signing of the image
sk = conf["ELF_SIGNING_KEY"]
image_elf_sign = env.Command(
target = env["PORTFILE"] + ".elf.sign",
source = env["PORTFILE"] + ".elf.strip",
action = [
Action(f'scripts/elfsign.py --sk {conf["ELF_SIGNING_KEY"]} --sign $SOURCE'),
]
)
if conf["ELF_IMAGE_XIP"] == "y":
Alias("firmware", image_strip)
AddPostAction(image_strip, image_elf_size)
AddPostAction(image_strip, image_elf_headers)
Alias("firmware-sign", image_elf_sign)
# AddPostAction(image_elf_sign, image_elf_sections)
###################################################################
# openocd programming
###################################################################
program_source = None
if conf["FW_IMAGE_ELF"] == "y":
program_source = image_bin
if conf["ELF_IMAGE_XIP"] == "y":
program_source = image_strip
oocd = env.Command(
source = program_source,
target = "oocd",
action = """
$OOCD \
-s /usr/share/openocd/scripts/ \
-f interface/%s.cfg \
-f target/%s.cfg \
-c "init" \
-c "reset init" \
-c "flash write_image erase $SOURCE %s bin" \
-c "reset" \
-c "shutdown"
""" % (env["OOCD_INTERFACE"], env["OOCD_TARGET"], conf["ELF_IMAGE_LOAD_ADDRESS"])
)
env.Alias("program", oocd);
###################################################################
# GDB programming
###################################################################
gdb_load = env.Command(
source = image_elf,
target = "gdb",
action = """
$GDB \
-ex "target extended-remote %s" \
-ex "load" \
-ex "monitor reset" \
-ex "set confirm off" \
-ex "quit" \
$SOURCE
""" % (conf["GDB_REMOTE_TARGET"])
)
env.Alias("gdb-program", gdb_load);
###################################################################
# GDB debugging
###################################################################
gdb_debug = env.Command(
source = image_elf,
target = "debug",
action = """
$GDB \
-ex "target extended-remote %s" \
$SOURCE
""" % (conf["GDB_REMOTE_TARGET"])
)
env.Alias("debug", gdb_debug);
###################################################################
# Size statistics
###################################################################
def stats_builder_func(target, source, env):
o = subprocess.run([env['NM'], '--size-sort', '-l', str(source[0])], capture_output=True, text=True).stdout
files = {}
for l in o.split('\n'):
cols = l.split()
if len(cols) == 3:
cols.append('other:0')
if len(cols) < 4:
continue
(code_len, section, name, filename,) = cols
(filename, line, ) = filename.split(':')
if filename:
filename = os.path.relpath(filename)
if files.get(filename) == None:
files[filename] = {}
files[filename][name] = code_len;
sizes = {}
for k, v in files.items():
total_size = 0;
for function_size in v.values():
total_size += int(function_size, base=16)
sizes[k] = total_size
for k in sorted(sizes, key=sizes.get):
print(k, sizes[k])
stats_builder = Builder(action=Action(stats_builder_func, env["STATSCOMSTR"]))
env.Append(BUILDERS={'Stats': stats_builder})
size_stats = env.Stats(
source = image_elf,
target = 'stats',
)
env.Alias("stats", size_stats);