Skip to content

Commit f1429a4

Browse files
committed
update image_def and generate linker script
1 parent e034ec2 commit f1429a4

File tree

9 files changed

+231
-135
lines changed

9 files changed

+231
-135
lines changed

build.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ pub fn MicroBuild(port_select: PortSelect) type {
569569
.chip_name = target.chip.name,
570570
.memory_regions = target.chip.memory_regions,
571571
.generate = linker_script_options.generate,
572+
.ram_image = target.ram_image,
572573
};
573574

574575
const args_str = std.json.stringifyAlloc(

core/src/cpus/cortex_m.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pub const startup_logic = struct {
591591
extern var microzig_bss_end: u8;
592592
extern const microzig_data_load_start: u8;
593593

594-
pub fn ram_image_entrypoint() linksection(".entry") callconv(.naked) void {
594+
pub fn ram_image_entrypoint() linksection("microzig_ram_start") callconv(.naked) void {
595595
asm volatile (
596596
\\
597597
// Set VTOR to point to ram table
@@ -687,17 +687,17 @@ pub fn export_startup_logic() void {
687687
@export(&startup_logic.ram_image_entrypoint, .{
688688
.name = "_entry_point",
689689
.linkage = .strong,
690+
})
691+
else
692+
@export(&startup_logic._vector_table, .{
693+
.name = "_vector_table",
694+
.section = "microzig_flash_start",
695+
.linkage = .strong,
690696
});
691697

692698
@export(&startup_logic._start, .{
693699
.name = "_start",
694700
});
695-
696-
@export(&startup_logic._vector_table, .{
697-
.name = "_vector_table",
698-
.section = "microzig_flash_start",
699-
.linkage = .strong,
700-
});
701701
}
702702

703703
const scs_base = 0xE000E000;

port/raspberrypi/rp2xxx/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ pub fn init(dep: *std.Build.Dependency) Self {
196196
},
197197
}),
198198
.pico2_arm_flashless = chip_rp2350_arm.derive(.{
199+
.entry = .{ .symbol_name = "_entry_point" },
199200
.ram_image = true,
200201
.linker_script = .{
201-
.generate = .memory_regions,
202202
.file = b.path("ld/rp2350/arm_ram_image_sections.ld"),
203203
},
204204
.board = .{
Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,18 @@
1-
SECTIONS
2-
{
3-
/DISCARD/ :
4-
{
5-
*(.entry)
6-
}
7-
8-
.flash_start :
9-
{
10-
KEEP(*(microzig_flash_start))
11-
} > ram0
12-
1+
SECTIONS {
132
.bootmeta :
143
{
154
__bootmeta_start__ = .;
165
KEEP(*(.bootmeta))
176
__bootmeta_end__ = .;
187
} > ram0
8+
}
9+
INSERT AFTER .ram_start;
1910

20-
.text :
21-
{
22-
*(.text*)
23-
KEEP(*(.ram_text))
24-
*(.rodata*)
25-
} > ram0
26-
27-
.ARM.extab : {
28-
*(.ARM.extab* .gnu.linkonce.armextab.*)
29-
} > ram0
30-
31-
.ARM.exidx : {
32-
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
33-
} > ram0
34-
35-
.data :
36-
{
37-
*(.data*)
38-
} > ram0
39-
40-
.bss :
11+
SECTIONS {
12+
.ram_vectors (NOLOAD) :
4113
{
42-
*(.bss*)
14+
KEEP(*(ram_vectors))
4315
} > ram0
44-
45-
/* Unused, but set as extern in startup_logic */
46-
microzig_data_start = .;
47-
microzig_data_end = .;
48-
microzig_bss_start = .;
49-
microzig_bss_end = .;
50-
microzig_data_load_start = .;
5116
}
17+
INSERT AFTER .bss;
18+

port/raspberrypi/rp2xxx/src/cpus/hazard3.zig

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ var ram_vectors: [vector_count]Handler = undefined;
169169
pub const startup_logic = struct {
170170
extern fn microzig_main() noreturn;
171171

172-
pub export fn _start() linksection("microzig_flash_start") callconv(.naked) noreturn {
172+
pub export fn _start() linksection(if (microzig.config.ram_image)
173+
"microzig_ram_start"
174+
else
175+
"microzig_flash_start") callconv(.naked) noreturn {
173176
asm volatile (
174177
\\.option push
175178
\\.option norelax
@@ -199,24 +202,26 @@ pub const startup_logic = struct {
199202
}
200203

201204
pub export fn _start_c() callconv(.c) noreturn {
202-
root.initialize_system_memories();
203-
204-
// Move vector table to RAM if requested
205-
if (interrupt.has_ram_vectors()) {
206-
if (interrupt.has_ram_vectors_section()) {
207-
@export(&ram_vectors, .{
208-
.name = "_ram_vectors",
209-
.section = "ram_vectors",
210-
.linkage = .strong,
211-
});
212-
} else {
213-
@export(&ram_vectors, .{
214-
.name = "_ram_vectors",
215-
.linkage = .strong,
216-
});
205+
if (!microzig.config.ram_image) {
206+
root.initialize_system_memories();
207+
208+
// Move vector table to RAM if requested
209+
if (interrupt.has_ram_vectors()) {
210+
if (interrupt.has_ram_vectors_section()) {
211+
@export(&ram_vectors, .{
212+
.name = "_ram_vectors",
213+
.section = "ram_vectors",
214+
.linkage = .strong,
215+
});
216+
} else {
217+
@export(&ram_vectors, .{
218+
.name = "_ram_vectors",
219+
.linkage = .strong,
220+
});
221+
}
222+
223+
@memcpy(&ram_vectors, &startup_logic.external_interrupt_table);
217224
}
218-
219-
@memcpy(&ram_vectors, &startup_logic.external_interrupt_table);
220225
}
221226

222227
microzig_main();
@@ -316,7 +321,7 @@ pub fn export_startup_logic() void {
316321

317322
@export(&startup_logic.external_interrupt_table, .{
318323
.name = "_external_interrupt_table",
319-
.section = "flash_vectors",
324+
.section = if (!microzig.config.ram_image) "flash_vectors" else ".data",
320325
.linkage = .strong,
321326
});
322327
}

port/raspberrypi/rp2xxx/src/hal.zig

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ pub const watchdog = @import("hal/watchdog.zig");
3030
pub const cyw49_pio_spi = @import("hal/cyw43_pio_spi.zig");
3131
pub const drivers = @import("hal/drivers.zig");
3232
pub const compatibility = @import("hal/compatibility.zig");
33-
pub const image_def = @import("hal/image_def.zig");
33+
pub const bootmeta = @import("hal/bootmeta.zig");
3434

3535
comptime {
3636
// HACK: tests can't access microzig. maybe there's a better way to do this.
3737
if (!builtin.is_test and compatibility.chip == .RP2350) {
38-
_ = image_def;
38+
_ = bootmeta;
3939
}
4040

4141
// On the RP2040, we need to import the `atomic.zig` file to export some global
@@ -46,8 +46,16 @@ comptime {
4646
}
4747
}
4848

49-
pub const HAL_Options = struct {
50-
image_def_security: image_def.Security = .secure,
49+
pub const HAL_Options = switch (compatibility.chip) {
50+
.RP2040 => struct {},
51+
.RP2350 => struct {
52+
image_def_exe_security: bootmeta.ImageDef.ImageTypeFlags.ExeSecurity = .secure,
53+
54+
/// Next metadata block to link after image_def. **Last block in the
55+
/// chain must link back to the first one** (to
56+
/// `bootmeta.image_def_block`).
57+
next_metadata_block: ?*const anyopaque = null,
58+
},
5159
};
5260

5361
/// A default clock configuration with sensible defaults that will work
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/// Documentation taken from section 5.9.5.1 of the rp2350 datasheet.
2+
const std = @import("std");
3+
const root = @import("root");
4+
const microzig = @import("microzig");
5+
const arch = @import("compatibility.zig").arch;
6+
7+
pub const image_def_block: Block(extern struct {
8+
image_def: ImageDef,
9+
entry_point: EntryPoint(false),
10+
}) = .{
11+
.items = .{
12+
.image_def = .{
13+
.image_type_flags = .{
14+
.image_type = .exe,
15+
.exe_security = root.microzig_options.hal.image_def_exe_security,
16+
.cpu = std.meta.stringToEnum(ImageDef.ImageTypeFlags.Cpu, @tagName(arch)).?,
17+
.chip = .RP2350,
18+
.try_before_you_buy = false,
19+
},
20+
},
21+
.entry_point = .{
22+
.entry = if (microzig.config.ram_image and arch == .arm)
23+
&microzig.cpu.startup_logic.ram_image_entrypoint
24+
else
25+
&microzig.cpu.startup_logic._start,
26+
.sp = microzig.config.end_of_stack,
27+
},
28+
},
29+
.link = root.microzig_options.hal.next_metadata_block,
30+
};
31+
32+
comptime {
33+
@export(&image_def_block, .{
34+
.name = "_image_def_block",
35+
.section = ".bootmeta",
36+
.linkage = .strong,
37+
});
38+
}
39+
40+
pub fn Block(Items: type) type {
41+
return extern struct {
42+
header: u32 = 0xffffded3,
43+
items: Items,
44+
last_item: u32 = 0x000000ff | ((@sizeOf(Items) / 4) << 8),
45+
link: ?*const anyopaque = null,
46+
footer: u32 = 0xab123579,
47+
};
48+
}
49+
50+
pub const ImageDef = packed struct {
51+
item_type: u8 = 0x42,
52+
block_size: u8 = 0x01,
53+
image_type_flags: ImageTypeFlags,
54+
55+
pub const ImageTypeFlags = packed struct {
56+
image_type: ImageType,
57+
exe_security: ExeSecurity,
58+
reserved0: u2 = 0,
59+
cpu: Cpu,
60+
reserved1: u1 = 0,
61+
chip: Chip,
62+
try_before_you_buy: bool,
63+
64+
pub const ImageType = enum(u4) {
65+
invalid = 0,
66+
exe = 1,
67+
data = 2,
68+
};
69+
70+
pub const ExeSecurity = enum(u2) {
71+
unspecified = 0,
72+
non_secure = 1,
73+
secure = 2,
74+
};
75+
76+
pub const Cpu = enum(u3) {
77+
arm = 0,
78+
riscv = 1,
79+
};
80+
81+
pub const Chip = enum(u3) {
82+
RP2040 = 0,
83+
RP2350 = 1,
84+
};
85+
};
86+
};
87+
88+
pub fn EntryPoint(with_stack_limit: bool) type {
89+
if (with_stack_limit) {
90+
return extern struct {
91+
header: packed struct {
92+
item_type: u8 = 0x44,
93+
block_size: u8 = 0x04,
94+
padding: u16 = 0,
95+
} = .{},
96+
entry: *const anyopaque,
97+
sp: u32,
98+
sp_limit: u32,
99+
};
100+
} else {
101+
return extern struct {
102+
header: packed struct {
103+
item_type: u8 = 0x44,
104+
block_size: u8 = 0x03,
105+
padding: u16 = 0,
106+
} = .{},
107+
entry: *const anyopaque,
108+
sp: u32,
109+
};
110+
}
111+
}

port/raspberrypi/rp2xxx/src/hal/image_def.zig

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)