Skip to content

Commit 431f33b

Browse files
committed
Fix up D0 Low Load
1 parent ec2da07 commit 431f33b

File tree

1 file changed

+97
-3
lines changed

1 file changed

+97
-3
lines changed

apps/d0_lowload/src/main.c

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ extern void unlz4(const void *aSource, void *aDestination, uint32_t FileLen);
4646
#define DTB_SRC_ADDR 0x58080000 // 64k
4747
#define DTB_DST_ADDR 0x51ff8000
4848

49+
#define BOOT_HDR_SRC_ADDR 0x58080000 // 64k
50+
4951
static struct bflb_device_s *uart0;
5052

5153
#if (__riscv_xlen == 64)
@@ -102,9 +104,101 @@ const pmp_config_entry_t pmp_entry_tab[8] = {
102104

103105
#endif
104106

107+
typedef enum {
108+
VM_BOOT_SECTION_HEADER = 0,
109+
VM_BOOT_SECTION_DTB,
110+
VM_BOOT_SECTION_OPENSBI,
111+
VM_BOOT_SECTION_KERNEL,
112+
VM_BOOT_SECTION_MAX,
113+
} vm_boot_section_t;
114+
115+
typedef struct __attribute__((packed, aligned(4))) {
116+
uint32_t magic;
117+
uint32_t version;
118+
uint32_t sections;
119+
uint32_t crc;
120+
struct {
121+
uint32_t magic;
122+
uint32_t type;
123+
uint32_t offset;
124+
uint32_t size;
125+
} section[VM_BOOT_SECTION_MAX];
126+
} vm_boot_header_t;
127+
128+
static char *sections[] = {
129+
"BootHeader",
130+
"dtb",
131+
"OpenSBI",
132+
"Kernel",
133+
"Unknown"
134+
};
135+
136+
static char *get_sectionstr(uint32_t type)
137+
{
138+
if (type < VM_BOOT_SECTION_MAX) {
139+
return sections[type];
140+
}
141+
142+
return sections[VM_BOOT_SECTION_MAX];
143+
}
144+
145+
105146
void linux_load()
106147
{
107148
LOG_I("linux load start... \r\n");
149+
150+
vm_boot_header_t *header = (vm_boot_header_t *)BOOT_HDR_SRC_ADDR;
151+
if (header->magic != 0x4c4d5642) {
152+
LOG_E("invalid boot header magic: 0x%08x\r\n", header->magic);
153+
return;
154+
}
155+
156+
if (header->version != 1) {
157+
LOG_E("invalid boot header version: %d\r\n", header->version);
158+
return;
159+
}
160+
161+
if (header->sections != 3) {
162+
LOG_E("invalid boot header sections: %d\r\n", header->sections);
163+
return;
164+
}
165+
166+
for (int i = 0; i < header->sections; i++) {
167+
if (header->section[i].magic != 0x5c2381b2) {
168+
LOG_E("invalid boot section magic: 0x%08x\r\n", header->section[i].magic);
169+
continue;
170+
}
171+
LOG_I("Section %s(%d) - Start 0x%08x, Size %ld\r\n", get_sectionstr(header->section[i].type), header->section[i].type, BOOT_HDR_SRC_ADDR + header->section[i].offset, header->section[i].size);
172+
switch (header->section[i].type) {
173+
case VM_BOOT_SECTION_DTB:
174+
LOG_I("Copying DTB to 0x%08x...\r\n", DTB_DST_ADDR);
175+
memcpy((void *)DTB_DST_ADDR, (void *)(BOOT_HDR_SRC_ADDR + header->section[i].offset), header->section[i].size);
176+
LOG_I("Done!\r\n");
177+
break;
178+
case VM_BOOT_SECTION_OPENSBI:
179+
LOG_I("Copying OpenSBI to 0x%08x...\r\n", OPENSBI_DST_ADDR);
180+
memcpy((void *)OPENSBI_DST_ADDR, (void *)(BOOT_HDR_SRC_ADDR + header->section[i].offset), header->section[i].size);
181+
LOG_I("Done!\r\n");
182+
break;
183+
case VM_BOOT_SECTION_KERNEL:
184+
LOG_I("Uncompressing Kernel to 0x%08x...\r\n", VM_LINUX_DST_ADDR);
185+
unlz4((const void *)(BOOT_HDR_SRC_ADDR + header->section[i].offset), (void *)VM_LINUX_DST_ADDR, header->section[i].size);
186+
LOG_I("Done!\r\n");
187+
break;
188+
default:
189+
LOG_E("Unhandled Section %d\r\n", header->section[i].type);
190+
return;
191+
}
192+
__DMB();
193+
__ISB();
194+
}
195+
LOG_I("CRC: %08x\r\n", header->crc);
196+
csi_dcache_clean_invalid();
197+
198+
return;
199+
200+
201+
108202
uint32_t *pSrc, *pDest;
109203
uint32_t header_kernel_len = 0;
110204
header_kernel_len = *(volatile uint32_t *)(VM_LINUX_SRC_ADDR - 4);
@@ -144,6 +238,8 @@ extern void bflb_uart_set_console(struct bflb_device_s *dev);
144238

145239
int main(void)
146240
{
241+
board_init();
242+
LOG_I("");
147243
struct bflb_device_s *gpio;
148244

149245
gpio = bflb_device_get_by_name("gpio");
@@ -166,14 +262,12 @@ int main(void)
166262
LOG_I("D0 start...\r\n");
167263
uint64_t start_time, stop_time;
168264

169-
CPU_MTimer_Delay_MS(100);
170-
171265
void (*opensbi)(int hart_id, int fdt_addr) = (void (*)(int hart_id, int fdt_addr))OPENSBI_DST_ADDR;
172266

173267
start_time = bflb_mtimer_get_time_us();
174268
linux_load();
175269
stop_time = bflb_mtimer_get_time_us();
176-
LOG_I("\r\nload time: %ld us \r\n", (stop_time - start_time));
270+
LOG_I("load time: %ld us \r\n", (stop_time - start_time));
177271

178272
__ASM volatile("csrw mcor, %0"
179273
:

0 commit comments

Comments
 (0)