3 #include <lunaix/boot_generic.h>
5 #include <asm/boot_stage.h>
6 #include <sys/boot/multiboot.h>
7 #include <sys-generic/bootmem.h>
9 #include <asm/mempart.h>
11 #include <klibc/string.h>
13 #define MEM_1M 0x100000UL
16 mb_parse_cmdline(struct boot_handoff* bhctx, char* cmdline)
21 slen = strlen(cmdline);
26 cmd = bootmem_alloc(slen + 1);
27 strncpy(cmd, cmdline, slen);
29 bhctx->kexec.len = slen;
30 bhctx->kexec.cmdline = cmd;
34 mb_parse_mmap(struct boot_handoff* bhctx,
35 struct multiboot_info* mb)
37 struct multiboot_mmap_entry *mb_mmap, *mb_mapent;
39 struct boot_mmapent *bmmap, *bmmapent;
41 mb_mmap = (struct multiboot_mmap_entry*)__ptr(mb->mmap_addr);
42 mmap_len = mb->mmap_length / sizeof(*mb_mmap);
44 bmmap = bootmem_alloc(sizeof(*bmmap) * mmap_len);
46 for (size_t i = 0; i < mmap_len; i++) {
47 mb_mapent = &mb_mmap[i];
50 if (mb_mapent->type == MULTIBOOT_MEMORY_AVAILABLE)
52 bmmapent->type = BOOT_MMAP_FREE;
55 else if (mb_mapent->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE)
57 bmmapent->type = BOOT_MMAP_RCLM;
61 bmmapent->type = BOOT_MMAP_RSVD;
64 bmmapent->start = mb_mapent->addr_low;
65 bmmapent->size = mb_mapent->len_low;
68 bhctx->mem.size = (mb->mem_upper << 10) + MEM_1M;
69 bhctx->mem.mmap = bmmap;
70 bhctx->mem.mmap_len = mmap_len;
74 mb_parse_mods(struct boot_handoff* bhctx,
75 struct multiboot_info* mb)
77 if (!mb->mods_count) {
78 bhctx->mods.mods_num = 0;
82 struct boot_modent* modents;
83 struct multiboot_mod_list* mods, *mod;
87 mods = (struct multiboot_mod_list*)__ptr(mb->mods_addr);
88 modents = bootmem_alloc(sizeof(*modents) * mb->mods_count);
90 for (size_t i = 0; i < mb->mods_count; i++) {
92 cmd = (char*)__ptr(mod->cmdline);
93 name_len = strlen(cmd);
94 mod_name = bootmem_alloc(name_len + 1);
96 modents[i] = (struct boot_modent){
97 .start = mod->mod_start,
102 strncpy(mod_name, cmd, name_len);
105 bhctx->mods.mods_num = mb->mods_count;
106 bhctx->mods.entries = modents;
110 mb_prepare_hook(struct boot_handoff* bhctx)
116 mb_release_hook(struct boot_handoff* bhctx)
122 mb_parse(struct boot_handoff* bhctx)
124 struct multiboot_info* mb;
126 mb = (struct multiboot_info*)__multiboot_addr;
128 /* Parse memory map */
129 if ((mb->flags & MULTIBOOT_INFO_MEM_MAP)) {
130 mb_parse_mmap(bhctx, mb);
134 if ((mb->flags & MULTIBOOT_INFO_CMDLINE)) {
135 mb_parse_cmdline(bhctx, (char*)__ptr(mb->cmdline));
138 /* Parse sys modules */
139 if ((mb->flags & MULTIBOOT_INFO_MODS)) {
140 mb_parse_mods(bhctx, mb);
143 bhctx->prepare = mb_prepare_hook;
144 bhctx->release = mb_release_hook;