Skip to content

Commit 47373a5

Browse files
committed
Start NOR writer project: copies multi-uimg binaries from SD card to NOR flash
The intended use is for production flashing FSBL+SSBL+App via SD Card onto NOR flash
1 parent 07eee30 commit 47373a5

File tree

5 files changed

+130
-11
lines changed

5 files changed

+130
-11
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ SOURCES += $(SRCDIR)/gpt/gpt.cc
6262
endif
6363

6464

65+
ifneq ("$(NORFLASH_WRITER)","")
66+
SOURCES += $(SRCDIR)/boot_sd_write_nor.cc
67+
endif
68+
6569
INCLUDES = -I. \
6670
-I$(SRCDIR) \
6771
-I$(SRCDIR)/board_conf \
@@ -99,6 +103,10 @@ endif
99103
endif
100104
endif
101105

106+
ifneq ("$(NORFLASH_WRITER)","")
107+
ARCH_CFLAGS += -DNORFLASH_WRITER=1
108+
endif
109+
102110
AFLAGS = $(MCU)
103111

104112

@@ -182,10 +190,10 @@ all: Makefile $(ELF) $(UIMAGENAME) image
182190
@:
183191

184192
mp13x:
185-
$(MAKE) SERIES=stm32mp13x BOARD_CONF=brainboard-mp13_conf.hh
193+
$(MAKE) SERIES=stm32mp13x BOARD_CONF=mp133_devboard_v0.3_conf.hh NORFLASH_WRITER=1
186194

187195
mp13x-load:
188-
$(MAKE) load SERIES=stm32mp13x BOARD_CONF=brainboard-mp13_conf.hh
196+
$(MAKE) load SERIES=stm32mp13x BOARD_CONF=mp133_devboard_v0.3_conf.hh NORFLASH_WRITER=1
189197

190198
$(OBJDIR)/%.o: %.s
191199
@mkdir -p $(dir $@)

src/boot_media_loader.hh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ private:
110110

111111
// Look for entry point in Kernel type images
112112
if (type == BootImageDef::IH_TYPE_KERNEL) {
113-
if (entry_point >= load_addr && entry_point < (load_addr + size)) {
113+
if (load_addr >= 0x60000000 && (load_addr + size) <= 0x90000000) {
114+
log("Setting entry point to FMC or QSPI memory: 0x", Hex{entry_point}, "\n");
115+
_entry_point = entry_point;
116+
117+
} else if (entry_point >= load_addr && entry_point < (load_addr + size)) {
114118
if (_entry_point.has_value())
115119
pr_err("Error: more than one kernel image with a valid entry point found.\n");
116120
else {
@@ -195,14 +199,21 @@ private:
195199
bool valid_addr(uint64_t addr)
196200
{
197201
#ifdef STM32MP13
198-
// TODO: other sectors on MP13?
202+
if (addr >= 0x2FFE0000 && addr <= 0x30008000)
203+
return true;
204+
199205
#else
200206
if (addr >= RETRAM_BASE && addr <= (RETRAM_BASE + STM32MP15x_RETRAM_SIZE))
201207
return true;
202-
#endif
203208

204209
if (addr >= SRAM_BASE && addr <= (SRAM_BASE + STM32MP15x_SRAM_SIZE))
205210
return true;
211+
#endif
212+
213+
#ifdef NORFLASH_WRITER
214+
if (addr >= 0x60000000 && addr <= 0x90000000)
215+
return true;
216+
#endif
206217

207218
if (addr >= STM32_DDR_BASE && addr <= STM32_DDR_END)
208219
return true;

src/boot_sd.hh

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <cstdint>
1010
#include <span>
1111

12+
#ifdef NORFLASH_WRITER
13+
#include "boot_sd_write_nor.hh"
14+
#endif
15+
1216
struct BootSDLoader : BootLoader {
1317
BootSDLoader()
1418
{
@@ -144,8 +148,10 @@ private:
144148
return false;
145149
}
146150

151+
auto load_addr = (uint32_t)data.data();
152+
147153
debug(" SD read ", data.size(), " bytes from 0x", Hex{address}, "-0x", Hex{address + data.size()});
148-
debug(" to 0x", Hex{(uint32_t)data.data()}, "-0x", Hex{(uint32_t)data.data() + data.size()}, "\n");
154+
debug(" to 0x", Hex{load_addr}, "-0x", Hex{load_addr + data.size()}, "\n");
149155

150156
uint32_t aligned_addr = (address / BlockSize) * BlockSize;
151157
if (aligned_addr < address) {
@@ -160,8 +166,16 @@ private:
160166
debug(" Copying last ", bytes_to_keep, " bytes from tmp to ", Hex{(uint32_t)data.data()}, "\n");
161167

162168
auto source = std::span<uint8_t>{&tmp[bytes_to_drop], &tmp[512]};
163-
for (auto i = 0; auto &d : data.subspan(0, bytes_to_keep))
164-
d = source[i++];
169+
170+
#ifdef NORFLASH_WRITER
171+
if (load_addr >= 0x6000'0000 && load_addr < 0x9000'0000) {
172+
nor_writer.write((uint32_t)data.data(), source);
173+
} else
174+
#endif
175+
{
176+
for (auto i = 0; auto &d : data.subspan(0, bytes_to_keep))
177+
d = source[i++];
178+
}
165179

166180
if (data.size() == bytes_to_keep) {
167181
return true;
@@ -191,7 +205,7 @@ private:
191205
uint32_t numblocks = bytes_to_read / BlockSize;
192206

193207
debug(" Reading ", numblocks, " block(s)\n");
194-
if (HAL_SD_ReadBlocks(&hsd, read_ptr, block_num, numblocks, timeout) != HAL_OK)
208+
if (read_sd_multi_blocks(&hsd, read_ptr, block_num, numblocks, timeout) != HAL_OK)
195209
read_error();
196210

197211
uint32_t bytes_read = numblocks * BlockSize;
@@ -216,15 +230,57 @@ private:
216230

217231
debug(" Copying first ", bytes_to_copy, " from tmp to ", Hex{uint32_t(read_ptr)}, "\n");
218232

219-
for (unsigned i = 0; i < bytes_to_copy; i++) {
220-
*read_ptr++ = tmp[i];
233+
#ifdef NORFLASH_WRITER
234+
if ((uint32_t)read_ptr >= 0x6000'0000 && (uint32_t)read_ptr < 0x9000'0000) {
235+
nor_writer.write((uint32_t)read_ptr, std::span<const uint8_t>{tmp, bytes_to_copy});
236+
} else
237+
#endif
238+
{
239+
for (unsigned i = 0; i < bytes_to_copy; i++)
240+
*read_ptr++ = tmp[i];
221241
}
242+
222243
bytes_to_read -= bytes_to_copy;
223244
block_num++;
224245
}
225246
return true;
226247
}
227248

249+
// Reads entire aligned blocks
250+
HAL_StatusTypeDef read_sd_multi_blocks(
251+
SD_HandleTypeDef *hsd, uint8_t *read_ptr, uint32_t block_num, uint32_t numblocks, uint32_t timeout)
252+
{
253+
#ifdef NORFLASH_WRITER
254+
if ((uint32_t)read_ptr >= 0x6000'0000 && (uint32_t)read_ptr < 0x9000'0000) {
255+
256+
while (numblocks--) {
257+
alignas(4) uint8_t tmp[BlockSize];
258+
259+
debug(" ", "Reading block #", block_num, " from SD Card to temp memory\n");
260+
261+
if (auto res = HAL_SD_ReadBlocks(hsd, tmp, block_num, 1, timeout); res == HAL_OK) {
262+
263+
if (nor_writer.write((uint32_t)read_ptr, tmp)) {
264+
debug("Wrote to NOR at 0x", Hex{(uint32_t)read_ptr}, "\n");
265+
} else {
266+
pr_err("Failed to write SD block #", block_num, " to NOR at 0x", Hex{(uint32_t)read_ptr}, "\n");
267+
}
268+
269+
} else {
270+
pr_err("Failed to reading block #", block_num, " from SD Card into temp memory.\n");
271+
return res;
272+
}
273+
274+
block_num++;
275+
read_ptr += BlockSize;
276+
}
277+
return HAL_OK;
278+
279+
} else
280+
#endif
281+
return HAL_SD_ReadBlocks(hsd, read_ptr, block_num, numblocks, timeout);
282+
}
283+
228284
void init_error()
229285
{
230286
_has_error = true;
@@ -244,4 +300,8 @@ private:
244300
}
245301

246302
constexpr static uint32_t BlockSize = 512;
303+
304+
#ifdef NORFLASH_WRITER
305+
NorFlashWriter nor_writer;
306+
#endif
247307
};

src/boot_sd_write_nor.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "boot_sd_write_nor.hh"
2+
#include "print.hh"
3+
#include "print_messages.hh"
4+
5+
NorFlashWriter::NorFlashWriter() {}
6+
7+
bool NorFlashWriter::write(uint32_t nor_addr, std::span<const uint8_t> bytes)
8+
{
9+
// TODO: skip 0x7008'0000 - 0x40?
10+
// It loads the uimg header which could overwrite a previous image
11+
12+
// Skip out of range addresses
13+
if (nor_addr < 0x7000'0000) {
14+
bytes = bytes.subspan(0x7000'0000 - nor_addr);
15+
nor_addr = 0x7000'0000;
16+
}
17+
18+
debug("Writing to ", Hex{nor_addr}, " ", bytes.size(), " bytes\n");
19+
20+
// if (nor_addr < 0x7000'0000) {
21+
// int i = 0;
22+
// for (auto b : bytes) {
23+
// print(Hex{b}, " ");
24+
// i++;
25+
// if (i % 16 == 0)
26+
// print("\n");
27+
// }
28+
// }
29+
return true;
30+
}

src/boot_sd_write_nor.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <span>
5+
6+
struct NorFlashWriter {
7+
NorFlashWriter();
8+
9+
bool write(uint32_t nor_addr, std::span<const uint8_t> bytes);
10+
};

0 commit comments

Comments
 (0)