3 #include <lunaix/boot_generic.h>
4 #include <lunaix/generic/bootmem.h>
6 #include <sys/boot/bstage.h>
7 #include <sys/boot/multiboot.h>
8 #include <sys/mm/mempart.h>
10 #include <klibc/string.h>
12 #define MEM_1M 0x100000UL
15 mb_parse_cmdline(struct boot_handoff* bhctx, char* cmdline)
20 slen = strlen(cmdline);
25 cmd = bootmem_alloc(slen + 1);
26 strncpy(cmd, cmdline, slen);
28 bhctx->kexec.len = slen;
29 bhctx->kexec.cmdline = cmd;
33 mb_parse_mmap(struct boot_handoff* bhctx,
34 struct multiboot_info* mb)
36 struct multiboot_mmap_entry *mb_mmap, *mb_mapent;
38 struct boot_mmapent *bmmap, *bmmapent;
40 mb_mmap = (struct multiboot_mmap_entry*)__ptr(mb->mmap_addr);
41 mmap_len = mb->mmap_length / sizeof(*mb_mmap);
43 bmmap = bootmem_alloc(sizeof(*bmmap) * mmap_len);
45 for (size_t i = 0; i < mmap_len; i++) {
46 mb_mapent = &mb_mmap[i];
49 if (mb_mapent->type == MULTIBOOT_MEMORY_AVAILABLE)
51 bmmapent->type = BOOT_MMAP_FREE;
54 else if (mb_mapent->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE)
56 bmmapent->type = BOOT_MMAP_RCLM;
60 bmmapent->type = BOOT_MMAP_RSVD;
63 bmmapent->start = mb_mapent->addr_low;
64 bmmapent->size = mb_mapent->len_low;
67 bhctx->mem.size = (mb->mem_upper << 10) + MEM_1M;
68 bhctx->mem.mmap = bmmap;
69 bhctx->mem.mmap_len = mmap_len;
73 mb_parse_mods(struct boot_handoff* bhctx,
74 struct multiboot_info* mb)
76 if (!mb->mods_count) {
77 bhctx->mods.mods_num = 0;
81 struct boot_modent* modents;
82 struct multiboot_mod_list* mods, *mod;
86 mods = (struct multiboot_mod_list*)__ptr(mb->mods_addr);
87 modents = bootmem_alloc(sizeof(*modents) * mb->mods_count);
89 for (size_t i = 0; i < mb->mods_count; i++) {
91 cmd = (char*)__ptr(mod->cmdline);
92 name_len = strlen(cmd);
93 mod_name = bootmem_alloc(name_len + 1);
95 modents[i] = (struct boot_modent){
96 .start = mod->mod_start,
101 strncpy(mod_name, cmd, name_len);
104 bhctx->mods.mods_num = mb->mods_count;
105 bhctx->mods.entries = modents;
109 mb_prepare_hook(struct boot_handoff* bhctx)
115 mb_release_hook(struct boot_handoff* bhctx)
121 mb_parse(struct boot_handoff* bhctx)
123 struct multiboot_info* mb;
125 mb = (struct multiboot_info*)__multiboot_addr;
127 /* Parse memory map */
128 if ((mb->flags & MULTIBOOT_INFO_MEM_MAP)) {
129 mb_parse_mmap(bhctx, mb);
133 if ((mb->flags & MULTIBOOT_INFO_CMDLINE)) {
134 mb_parse_cmdline(bhctx, (char*)__ptr(mb->cmdline));
137 /* Parse sys modules */
138 if ((mb->flags & MULTIBOOT_INFO_MODS)) {
139 mb_parse_mods(bhctx, mb);
142 bhctx->prepare = mb_prepare_hook;
143 bhctx->release = mb_release_hook;