3 #include <lunaix/boot_generic.h>
4 #include <sys/boot/bstage.h>
5 #include <sys/boot/multiboot.h>
6 #include <sys/mm/mempart.h>
8 #define BHCTX_ALLOC 4096
10 u8_t bhctx_buffer[BHCTX_ALLOC] boot_bss;
12 #define check_buffer(ptr) \
13 if ((ptr) >= ((ptr_t)bhctx_buffer + BHCTX_ALLOC)) { \
18 mb_memcpy(u8_t* destination, u8_t* base, unsigned int size)
21 for (; i < size; i++) {
22 *(destination + i) = *(base + i);
28 mb_strcpy(char* destination, char* base)
32 while ((c = base[i])) {
52 mb_parse_cmdline(struct boot_handoff* bhctx, void* buffer, char* cmdline)
56 size_t slen = mb_strlen(cmdline);
62 mb_memcpy(buffer, (u8_t*)cmdline, slen);
63 bhctx->kexec.len = slen;
64 bhctx->kexec.cmdline = buffer;
70 mb_parse_mmap(struct boot_handoff* bhctx,
71 struct multiboot_info* mb,
74 struct multiboot_mmap_entry* mb_mmap =
75 (struct multiboot_mmap_entry*)mb->mmap_addr;
76 size_t mmap_len = mb->mmap_length / sizeof(struct multiboot_mmap_entry);
78 struct boot_mmapent* bmmap = (struct boot_mmapent*)buffer;
79 for (size_t i = 0; i < mmap_len; i++) {
80 struct boot_mmapent* bmmapent = &bmmap[i];
81 struct multiboot_mmap_entry* mb_mapent = &mb_mmap[i];
83 if (mb_mapent->type == MULTIBOOT_MEMORY_AVAILABLE) {
84 bmmapent->type = BOOT_MMAP_FREE;
85 } else if (mb_mapent->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
86 bmmapent->type = BOOT_MMAP_RCLM;
88 bmmapent->type = BOOT_MMAP_RSVD;
91 bmmapent->start = mb_mapent->addr_low;
92 bmmapent->size = mb_mapent->len_low;
95 bhctx->mem.size = (mb->mem_upper << 10) + MEM_1M;
96 bhctx->mem.mmap = bmmap;
97 bhctx->mem.mmap_len = mmap_len;
99 return mmap_len * sizeof(struct boot_mmapent);
103 mb_parse_mods(struct boot_handoff* bhctx,
104 struct multiboot_info* mb,
107 if (!mb->mods_count) {
108 bhctx->mods.mods_num = 0;
112 struct boot_modent* modents = (struct boot_modent*)buffer;
113 struct multiboot_mod_list* mods = (struct multiboot_mod_list*)mb->mods_addr;
115 ptr_t mod_str_ptr = (ptr_t)&modents[mb->mods_count];
117 for (size_t i = 0; i < mb->mods_count; i++) {
118 struct multiboot_mod_list* mod = &mods[i];
119 modents[i] = (struct boot_modent){ .start = mod->mod_start,
121 .str = (char*)mod_str_ptr };
122 mod_str_ptr += mb_strcpy((char*)mod_str_ptr, (char*)mod->cmdline);
125 bhctx->mods.mods_num = mb->mods_count;
126 bhctx->mods.entries = modents;
128 return mod_str_ptr - (ptr_t)buffer;
132 mb_prepare_hook(struct boot_handoff* bhctx)
138 mb_release_hook(struct boot_handoff* bhctx)
143 #define align_addr(addr) (((addr) + (sizeof(ptr_t) - 1)) & ~(sizeof(ptr_t) - 1))
145 struct boot_handoff* boot_text
146 mb_parse(struct multiboot_info* mb)
148 struct boot_handoff* bhctx = (struct boot_handoff*)bhctx_buffer;
149 ptr_t bhctx_ex = (ptr_t)&bhctx[1];
151 *bhctx = (struct boot_handoff){ };
153 /* Parse memory map */
154 if ((mb->flags & MULTIBOOT_INFO_MEM_MAP)) {
155 bhctx_ex += mb_parse_mmap(bhctx, mb, (void*)bhctx_ex);
156 bhctx_ex = align_addr(bhctx_ex);
160 if ((mb->flags & MULTIBOOT_INFO_CMDLINE)) {
162 mb_parse_cmdline(bhctx, (void*)bhctx_ex, (char*)mb->cmdline);
163 bhctx_ex = align_addr(bhctx_ex);
166 /* Parse sys modules */
167 if ((mb->flags & MULTIBOOT_INFO_MODS)) {
168 bhctx_ex += mb_parse_mods(bhctx, mb, (void*)bhctx_ex);
169 bhctx_ex = align_addr(bhctx_ex);
172 check_buffer(bhctx_ex);
174 bhctx->prepare = mb_prepare_hook;
175 bhctx->release = mb_release_hook;